Пример #1
0
func (s *ServerController) CheckPing(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := struct {
		IP string `json:"ip"`
	}{}
	err := r.GetPayload(&payload)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	p := new(live.Ping)
	p.Type = live.PingType_Network
	p.Host = payload.IP

	if err := p.Check(); err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	return helper.CreateResult(true, p.LastStatus, "")
}
Пример #2
0
func ConfigService(data m.ServiceNew, statuslive string) string {
	var (
		err          error
		pingtype     lv.PingTypeEnum
		valtypestart lv.ValidationTypeEnum
		valtypestop  lv.ValidationTypeEnum
		// exedtype lv.CommandTypeEnum
	)
	svc := lv.NewService()

	if data.Ping.Type == "PingType_Command" {
		pingtype = lv.PingType_Command
	} else if data.Ping.Type == "PingType_Network" {
		pingtype = lv.PingType_Network
	} else if data.Ping.Type == "PingType_HttpStatus" {
		pingtype = lv.PingType_HttpStatus
	} else if data.Ping.Type == "PingType_HttpBody" {
		pingtype = lv.PingType_HttpBody
	} else {
		pingtype = lv.PingType_Custom
	}

	if data.Ping.Type == "PingType_Command" {
		var resptype lv.ResponseEnum
		if data.Ping.ResponseType == "Response_Contains" {
			resptype = lv.Response_Contains
		} else if data.Ping.ResponseType == "Response_Equals" {
			resptype = lv.Response_Equals
		} else {
			resptype = lv.Response_RegEx
		}
		svc.Ping = func() *lv.Ping {
			p := new(lv.Ping)
			p.Type = pingtype
			p.Command = data.Ping.Command
			p.CommandParms = data.Ping.CommandParm
			p.ResponseType = resptype
			p.ResponseValue = "RUNNING"
			return p
		}()
	} else if data.Ping.Type == "PingType_Network" || data.Ping.Type == "PingType_HttpStatus" {
		svc.Ping = func() *lv.Ping {
			p := new(lv.Ping)
			p.Type = pingtype
			p.Host = "http://" + data.Ping.Host + ":" + data.Ping.Port
			return p
		}()
	} else if data.Ping.Type == "PingType_HttpBody" {
		var bodytype lv.HttpBodyEnum
		if data.Ping.HttpBodyType == "HttpBody_Contains" {
			bodytype = lv.HttpBody_Contains
		} else {
			bodytype = lv.HttpBody_Equals
		}
		svc.Ping = func() *lv.Ping {
			p := new(lv.Ping)
			p.Type = pingtype
			p.Host = "http://" + data.Ping.Host + ":" + data.Ping.Port
			p.HttpBodySearch = "RUNNING"
			p.HttpBodyType = bodytype
			return p
		}()
	}

	svc.RestartAfterNCritical = data.Service.RestartAfterNCritical
	svc.Interval = time.Duration(data.Service.Interval) * time.Second

	if data.ExedCommandStart.ValidationType == "ValidationType_Contain" {
		valtypestart = lv.ValidationType_Contain
	} else if data.ExedCommandStart.ValidationType == "ValidationType_Equal" {
		valtypestart = lv.ValidationType_Equal
	} else {
		valtypestart = lv.ValidationType_Regex
	}
	if data.ExedCommandStop.ValidationType == "ValidationType_Contain" {
		valtypestop = lv.ValidationType_Contain
	} else if data.ExedCommandStop.ValidationType == "ValidationType_Equal" {
		valtypestop = lv.ValidationType_Equal
	} else {
		valtypestop = lv.ValidationType_Regex
	}

	// Exec Start
	if data.ExedCommandStart.Type == "CommandType_Local" {
		svc.CommandStart = &lv.Command{
			Type:            lv.CommandType_Local,
			CommandText:     data.ExedCommandStart.CommandText,
			CommandParms:    data.ExedCommandStart.CommandParm,
			ValidationType:  valtypestart,
			ValidationValue: "RUNNING",
		}
	} else if data.ExedCommandStart.Type == "CommandType_SSH" {
		if data.ExedCommandStart.SshAuthType == "SSHAuthType_Password" {
			svc.CommandStart = &lv.Command{
				Type: lv.CommandType_SSH,
				SshClient: &lv.SshSetting{
					SSHHost:     data.ExedCommandStart.SshHost + ":" + data.ExedCommandStart.SshPort,
					SSHUser:     data.ExedCommandStart.SshUser,
					SSHPassword: data.ExedCommandStart.SshPassword,
					SSHAuthType: lv.SSHAuthType_Password,
				},
				CommandText:     data.ExedCommandStart.CommandText,
				ValidationType:  valtypestart,
				ValidationValue: "running",
			}
		} else {
			svc.CommandStart = &lv.Command{
				Type: lv.CommandType_SSH,
				SshClient: &lv.SshSetting{
					SSHHost:        data.ExedCommandStart.SshHost + ":" + data.ExedCommandStart.SshPort,
					SSHUser:        data.ExedCommandStart.SshUser,
					SSHKeyLocation: data.ExedCommandStart.SshKeyLocation,
					SSHAuthType:    lv.SSHAuthType_Certificate,
				},
				CommandText:     data.ExedCommandStart.CommandText,
				ValidationType:  valtypestart,
				ValidationValue: "running",
			}
		}
	} else {
		var valrestaunth lv.RESTAuthTypeEnum
		if data.ExedCommandStart.RestAuthType == "RESTAuthType_None" {
			valrestaunth = lv.RESTAuthType_None
		} else {
			valrestaunth = lv.RESTAuthType_Basic
		}
		svc.CommandStart = &lv.Command{
			Type:            lv.CommandType_REST,
			RESTUrl:         data.ExedCommandStart.RestUrl,
			RESTMethod:      data.ExedCommandStart.RestMenthod, //POST,GET
			RESTUser:        data.ExedCommandStart.RestUser,
			RESTPassword:    data.ExedCommandStart.RestPassword,
			RESTAuthType:    valrestaunth,
			ValidationType:  valtypestart,
			ValidationValue: "SUCCESS",
		}
	}

	// Exec Stop
	if data.ExedCommandStop.Type == "CommandType_Local" {
		svc.CommandStop = &lv.Command{
			Type:            lv.CommandType_Local,
			CommandText:     data.ExedCommandStop.CommandText,
			CommandParms:    data.ExedCommandStop.CommandParm,
			ValidationType:  valtypestop,
			ValidationValue: "STOP_PENDING",
		}
	} else if data.ExedCommandStop.Type == "CommandType_SSH" {
		if data.ExedCommandStop.SshAuthType == "SSHAuthType_Password" {
			svc.CommandStop = &lv.Command{
				Type: lv.CommandType_SSH,
				SshClient: &lv.SshSetting{
					SSHHost:     data.ExedCommandStop.SshHost + ":" + data.ExedCommandStop.SshPort,
					SSHUser:     data.ExedCommandStop.SshUser,
					SSHPassword: data.ExedCommandStop.SshPassword,
					SSHAuthType: lv.SSHAuthType_Password,
				},
				CommandText:     data.ExedCommandStop.CommandText,
				ValidationType:  valtypestop,
				ValidationValue: "running",
			}
		} else {
			svc.CommandStop = &lv.Command{
				Type: lv.CommandType_SSH,
				SshClient: &lv.SshSetting{
					SSHHost:        data.ExedCommandStop.SshHost + ":" + data.ExedCommandStop.SshPort,
					SSHUser:        data.ExedCommandStop.SshUser,
					SSHKeyLocation: data.ExedCommandStop.SshKeyLocation,
					SSHAuthType:    lv.SSHAuthType_Certificate,
				},
				CommandText:     data.ExedCommandStop.CommandText,
				ValidationType:  valtypestop,
				ValidationValue: "running",
			}
		}
	} else {
		var valrestaunth lv.RESTAuthTypeEnum
		if data.ExedCommandStop.RestAuthType == "RESTAuthType_None" {
			valrestaunth = lv.RESTAuthType_None
		} else {
			valrestaunth = lv.RESTAuthType_Basic
		}
		svc.CommandStop = &lv.Command{
			Type:            lv.CommandType_REST,
			RESTUrl:         data.ExedCommandStop.RestUrl,
			RESTMethod:      data.ExedCommandStop.RestMenthod, //POST,GET
			RESTUser:        data.ExedCommandStop.RestUser,
			RESTPassword:    data.ExedCommandStop.RestPassword,
			RESTAuthType:    valrestaunth,
			ValidationType:  valtypestop,
			ValidationValue: "SUCCESS",
		}
	}

	svc.EmailError = data.Service.EmailWarning
	svc.EmailWarning = data.Service.EmailError

	svc.Mail = &lv.EmailSetting{
		SenderEmail:   "*****@*****.**",
		HostEmail:     "smtp.office365.com",
		PortEmail:     587,
		UserEmail:     "*****@*****.**",
		PasswordEmail: "B920Support",
	}

	svc.Log, err = toolkit.NewLog(false, true, logservicepath+"/", "LogService"+strconv.Itoa(data.Service.ID), "20060102")
	if err != nil {
		fmt.Println("Error Start Log : %s", err.Error())
	}
	datasvc := modelsvc{}
	datasvc.ID = data.Service.ID
	datasvc.svc = svc

	if statuslive == "Start" && data.Service.StatusService == "Start" {
		if len(arrsvc) == 0 && data.Service.StatusService == "Start" {
			svc.KeepAlive()
			arrsvc = append(arrsvc, datasvc)
			return "Preparing"
		}
		for j := 0; j < len(arrsvc); j++ {
			if arrsvc[j].ID != data.Service.ID && data.Service.StatusService == "Start" {
				if statuslive == "Start" {
					svc.KeepAlive()
					arrsvc = append(arrsvc, datasvc)
					return "Preparing"
				}
			} else if data.Service.StatusService == "Start" {
				if statuslive == "Start" {
					svc.KeepAlive()
					arrsvc[j] = datasvc
					return "Preparing"
				}
			}
		}
	} else if statuslive == "Live" && data.Service.StatusService == "Start" {
		for j := 0; j < len(arrsvc); j++ {
			if arrsvc[j].ID == data.Service.ID {
				if arrsvc[j].svc.MonitorStatus == "Running" {
					if arrsvc[j].svc.PingStatus == "OK" {
						return arrsvc[j].svc.PingStatus
					} else if arrsvc[j].svc.PingStatus == "Fail" {
						return arrsvc[j].svc.PingStatus
					} else {
						return arrsvc[j].svc.PingStatus
					}
				}
			}
		}
	}
	return "Fail"
}