Example #1
0
func NewMetricValue(metric string, val interface{}, dataType string, tags ...string) *model.MetricValue {
	mv := model.MetricValue{
		Metric: metric,
		Value:  val,
		Type:   dataType,
	}

	size := len(tags)

	if size > 0 {
		mv.Tags = strings.Join(tags, ",")
	}

	return &mv
}
Example #2
0
func NewMetricValueIp(TS int64, ip, metric string, val interface{}, dataType string, tags ...string) *model.MetricValue {
	sec := int64(g.Config().Transfer.Interval)

	mv := model.MetricValue{
		Metric:    metric,
		Value:     val,
		Type:      dataType,
		Endpoint:  ip,
		Step:      sec,
		Timestamp: TS,
	}

	size := len(tags)

	if size > 0 {
		mv.Tags = strings.Join(tags, ",")
	}

	return &mv
}
Example #3
0
func main() {
	flag.StringVar(&CONFIGFILE, "f", "./cfg.json", "Path of config file")
	flag.Int64Var(&STEP, "s", 60, "internal")
	flag.Parse()

	config, err := ioutil.ReadFile(CONFIGFILE)
	if err != nil {
		log.Fatal("read config file error")
		return
	}
	var conf Config
	err = json.Unmarshal(config, &conf)
	if err != nil {
		log.Fatal("render json error")
		return
	}

	endpoints, err := GetList(conf.GetListURL)
	if err != nil || len(endpoints) == 0 {
		log.Fatal("can't get endpoint list")
	}
	//	for _, endpoint := range endpoints {
	//		fmt.Println(endpoint.Endpoint)
	//	}
	m := CheckLast(conf.GetLastURL, endpoints)
	//	fmt.Println(len(m))
	//	for _, val := range m {
	//		fmt.Printf("%v - %v - %v - %v - %v\n", val.Endpoint, val.Metric, val.Timestamp, val.Type, val.Value)
	//	}
	if len(m) != 0 {
		err = PushMetric(conf.PushMetricURL, m)
	} else {
		err = nil
	}

	if err == nil {
		//		log.Printf("Check agent alive ok, upload with %v metric.\n", len(m))
	} else {
		log.Fatal("Check agent alive error, with %v", err.Error())
	}
	resArray := []*model.MetricValue{}
	res := model.MetricValue{}
	res.Endpoint, _ = os.Hostname()
	res.Timestamp = time.Now().Unix()
	res.Metric = "agent.not_alive.num"
	res.Step = 60
	res.Type = "GAUGE"
	res.Value = len(m)
	resArray = append(resArray, &res)
	output, err := json.Marshal(&resArray)
	if err != nil {
		log.Fatal("parser metric json error")
	} else {
		fmt.Printf(string(output))
	}
}
Example #4
0
func _collect() {
	clientGet := nhttpclient.GetHttpClient("collector.get", 10*time.Second, 20*time.Second)
	clientPost := nhttpclient.GetHttpClient("collector.post", 5*time.Second, 10*time.Second)

	tags := "type=statistics,pdl=falcon"

	for _, host := range g.Config().Collector.Cluster {
		ts := time.Now().Unix()
		jsonList := make([]model.MetricValue, 0)

		// get statistics by http-get
		hostInfo := strings.Split(host, ",") // "module,hostname:port"
		if len(hostInfo) != 2 {
			continue
		}
		hostModule := hostInfo[0]
		hostNamePort := hostInfo[1]

		hostNamePortList := strings.Split(hostNamePort, ":")
		if len(hostNamePortList) != 2 {
			continue
		}
		hostName := hostNamePortList[0]
		hostPort := hostNamePortList[1]

		myTags := tags + ",module=" + hostModule + ",port=" + hostPort
		srcUrl := fmt.Sprintf(srcUrlFmt, hostNamePort)
		reqGet, _ := http.NewRequest("GET", srcUrl, nil)
		reqGet.Header.Set("Connection", "close")
		getResp, err := clientGet.Do(reqGet)
		if err != nil {
			log.Printf(hostNamePort+", get statistics error,", err)
			continue
		}
		defer getResp.Body.Close()

		body, err := ioutil.ReadAll(getResp.Body)
		if err != nil {
			log.Println(hostNamePort+", get statistics error,", err)
			continue
		}

		var data Dto
		err = json.Unmarshal(body, &data)
		if err != nil {
			log.Println(hostNamePort+", get statistics error,", err)
			continue
		}

		for _, item := range data.Data {
			if item["Name"] == nil {
				continue
			}
			itemName := item["Name"].(string)

			if item["Cnt"] != nil {
				var jmdCnt model.MetricValue
				jmdCnt.Endpoint = hostName
				jmdCnt.Metric = itemName
				jmdCnt.Timestamp = ts
				jmdCnt.Step = 60
				jmdCnt.Value = int64(item["Cnt"].(float64))
				jmdCnt.Type = "GAUGE"
				jmdCnt.Tags = myTags
				jsonList = append(jsonList, jmdCnt)
			}

			if item["Qps"] != nil {
				var jmdQps model.MetricValue
				jmdQps.Endpoint = hostName
				jmdQps.Metric = itemName + ".Qps"
				jmdQps.Timestamp = ts
				jmdQps.Step = 60
				jmdQps.Value = int64(item["Qps"].(float64))
				jmdQps.Type = "GAUGE"
				jmdQps.Tags = myTags
				jsonList = append(jsonList, jmdQps)
			}
		}

		if len(jsonList) < 1 { //没取到数据
			log.Println("get null from ", hostNamePort)
			continue
		}

		// format result
		jsonBody, err := json.Marshal(jsonList)
		if err != nil {
			log.Println(hostNamePort+", format body error,", err)
			continue
		}

		// send by http-post
		req, err := http.NewRequest("POST", destUrl, bytes.NewBuffer(jsonBody))
		req.Header.Set("Content-Type", "application/json; charset=UTF-8")
		req.Header.Set("Connection", "close")
		postResp, err := clientPost.Do(req)
		if err != nil {
			log.Println(hostNamePort+", post to dest error,", err)
			continue
		}
		defer postResp.Body.Close()

		if postResp.StatusCode/100 != 2 {
			log.Println(hostNamePort+", post to dest, bad response,", postResp.StatusCode)
		}
	}
}