forked from Cepave/open-falcon-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
209 lines (179 loc) · 4.95 KB
/
collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package collector
import (
"bytes"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
cmodel "github.com/open-falcon/common/model"
cron "github.com/toolkits/cron"
nhttpclient "github.com/toolkits/http/httpclient"
ntime "github.com/toolkits/time"
"github.com/Cepave/open-falcon-backend/modules/task/g"
"github.com/Cepave/open-falcon-backend/modules/task/proc"
)
var (
collectorCron = cron.New()
srcUrlFmt = "http://%s/statistics/all"
destUrl = "http://127.0.0.1:1988/v1/push"
)
func Start() {
if !g.Config().Collector.Enable {
log.Println("collector.Start warning, not enable")
return
}
// init url
if g.Config().Collector.DestUrl != "" {
destUrl = g.Config().Collector.DestUrl
}
if g.Config().Collector.SrcUrlFmt != "" {
srcUrlFmt = g.Config().Collector.SrcUrlFmt
}
// start
go startCollectorCron()
log.Println("collector.Start, ok")
}
func startCollectorCron() {
collectorCron.AddFuncCC("0 * * * * ?", func() { collect() }, 1)
collectorCron.Start()
}
func collect() {
startTs := time.Now().Unix()
_collect()
endTs := time.Now().Unix()
log.Printf("collect, start %s, ts %ds\n", ntime.FormatTs(startTs), endTs-startTs)
// statistics
proc.CollectorCronCnt.Incr()
}
func _collect() {
clientGet := nhttpclient.GetHttpClient("collector.get", 10*time.Second, 20*time.Second)
tags := "type=statistics,pdl=falcon"
for _, host := range g.Config().Collector.Cluster {
ts := time.Now().Unix()
jsonList := make([]*cmodel.JsonMetaData, 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 cmodel.JsonMetaData
jmdCnt.Endpoint = hostName
jmdCnt.Metric = itemName
jmdCnt.Timestamp = ts
jmdCnt.Step = 60
jmdCnt.Value = int64(item["Cnt"].(float64))
jmdCnt.CounterType = "GAUGE"
jmdCnt.Tags = myTags
jsonList = append(jsonList, &jmdCnt)
}
if item["Qps"] != nil {
var jmdQps cmodel.JsonMetaData
jmdQps.Endpoint = hostName
jmdQps.Metric = itemName + ".Qps"
jmdQps.Timestamp = ts
jmdQps.Step = 60
jmdQps.Value = int64(item["Qps"].(float64))
jmdQps.CounterType = "GAUGE"
jmdQps.Tags = myTags
jsonList = append(jsonList, &jmdQps)
}
}
// format result
err = sendToTransfer(jsonList, destUrl)
if err != nil {
log.Println(hostNamePort, "send to transfer error,", err.Error())
}
}
// collector.alive
_collectorAlive()
}
func _collectorAlive() error {
hostname, err := os.Hostname()
if err != nil {
log.Println("get hostname failed,", err)
return err
}
var jmdCnt cmodel.JsonMetaData
jmdCnt.Endpoint = hostname
jmdCnt.Metric = "falcon.task.alive"
jmdCnt.Timestamp = time.Now().Unix()
jmdCnt.Step = 60
jmdCnt.Value = 0
jmdCnt.CounterType = "GAUGE"
jmdCnt.Tags = ""
jsonList := make([]*cmodel.JsonMetaData, 0)
jsonList = append(jsonList, &jmdCnt)
err = sendToTransfer(jsonList, destUrl)
if err != nil {
log.Println("send task.alive failed,", err)
return err
}
return nil
}
func sendToTransfer(items []*cmodel.JsonMetaData, destUrl string) error {
if len(items) < 1 {
return nil
}
// format result
jsonBody, err := json.Marshal(items)
if err != nil {
return fmt.Errorf("json.Marshal failed with %v", err)
}
// 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")
clientPost := nhttpclient.GetHttpClient("collector.post", 5*time.Second, 10*time.Second)
postResp, err := clientPost.Do(req)
if err != nil {
return fmt.Errorf("post to %s, resquest failed with %v", destUrl, err)
}
defer postResp.Body.Close()
if postResp.StatusCode/100 != 2 {
return fmt.Errorf("post to %s, got bad response %d", destUrl, postResp.StatusCode)
}
return nil
}
type Dto struct {
Msg string `json:"msg"`
Data []map[string]interface{} `json:"data"`
}