Exemplo n.º 1
0
func (this *HeartbeatController) Post() {
	jsonResult := make(map[string]interface{})
	jsonResult["success"] = false

	var req model.HeartbeatRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); nil != err {
		logger.Errorln("parseForm error", err)
		jsonResult["message"] = fmt.Sprintf("parseForm error: %v", err.Error())
	} else if "" == req.Hostname {
		jsonResult["message"] = "hostname is blank"
	} else {
		logger.Debugln("Heartbeat Request===>>>", req)
		strIp := this.Ctx.Request.Header.Get("X-Forwarded-For")
		if "" == strIp {
			strIp = this.Ctx.Request.Header.Get("Proxy-Client-IP")
		}
		if "" == strIp {
			strIp = this.Ctx.Request.Header.Get("WL-Proxy-Client-IP")
		}
		if "" == strIp {
			ip, _, err := net.SplitHostPort(this.Ctx.Request.RemoteAddr)
			if nil != err {
				logger.Errorln("SplitHostPort error", this.Ctx.Request.RemoteAddr)
			} else {
				strIp = ip
			}
		}

		go store.ParseHeartbeatRequest(&req, strIp)

		resp := model.HeartbeatResponse{
			ErrorMessage:  "",
			DesiredAgents: store.HostAgents.Status(req.Hostname),
		}
		logger.Debugln("Heartbeat Response<<<===", resp)

		this.Data["json"] = &resp
		jsonResult["success"] = true
	}

	if false == jsonResult["success"] {
		this.Data["json"] = &jsonResult
	}

	this.ServeJSON()
}
Exemplo n.º 2
0
func heartbeat() {
	hostname, err := utils.Hostname(g.Config().Hostname)
	if err != nil {
		return
	}

	desiredAgents := ReadDesiredAgents()
	heartbeatRequest := BuildHeartbeatRequest(hostname, desiredAgents)
	logger.Debugln("===>>>", heartbeatRequest)

	bs, err := json.Marshal(heartbeatRequest)
	if err != nil {
		logger.Errorln("encode heartbeat request fail", err)
		return
	}

	url := fmt.Sprintf("http://%s/api/heartbeat", g.Config().Server)
	httpRequest := httplib.Post(url).SetTimeout(time.Second*10, time.Minute)
	httpRequest.Body(bs)
	httpResponse, err := httpRequest.Bytes()
	if err != nil {
		log.Printf("curl %s fail %v", url, err)
		return
	}

	var heartbeatResponse model.HeartbeatResponse
	err = json.Unmarshal(httpResponse, &heartbeatResponse)
	if err != nil {
		logger.Errorln("decode heartbeat response fail", err)
		return
	}

	logger.Debugln("<<<<====", heartbeatResponse)

	HandleHeartbeatResponse(&heartbeatResponse, desiredAgents)
}
Exemplo n.º 3
0
func heartbeat() {
	strIp := getIp()
	if "" == strIp || lastIp == strIp {
		logger.Debug("strIp:%s, lastIp: %s.\n", strIp, lastIp)
		return
	}

	logger.Debugln("domainMap's size:", len(domainMap))
	if 0 == len(domainMap) {
		return
	}

	var modifyResult bool
	for _, domain := range g.Config().Domains {
		if _, ok := domainMap[domain.DomainName]; ok {
			if recordResults, exists := recordMap[domain.DomainName]; exists {
				for _, recordResult := range recordResults {
					if existsRecordName(domain.DomainName, recordResult.Name) && strIp != recordResult.Value {
						modifyResult = ModifyRecord(domainMap[domain.DomainName], recordResult.Id, recordResult.Name, strIp)
					}
				}
			}
		}
	}

	if modifyResult {
		lastIp = strIp
		logger.Infoln("last ip have changed into ", strIp)

		if g.Config().Redis.Enabled {
			// send to redis
			rc := g.RedisConnPool.Get()
			defer rc.Close()
			rc.Do("LPUSH", "COMMAND_udai", g.Config().ExecuteCommand)
		}
	}
}