-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
214 lines (145 loc) · 4.46 KB
/
monitor.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
210
211
212
213
214
package main
import (
"database/sql"
"encoding/json"
"io/ioutil"
"log"
"net"
"net/http"
"path"
"strconv"
"strings"
"jarvis"
_ "github.com/mattn/go-sqlite3"
)
const (
SQL_UPDATE_NODE = `UPDATE nodes SET type = ?, os = ?, cpu = ?, core = ?, mem = ?,
disk = ?, uptime = ?, atime = datetime('now','localtime') WHERE id = ?;`
SQL_UPDATE_NODE_UPTIME = `UPDATE nodes SET uptime = ?, atime = datetime('now','localtime')
WHERE id = ?;`
SQL_SELECT_NODE_METRICS = `SELECT m.id, m.type, m.detector, b.params, m.md5
FROM metrics AS m, metric_bindings AS b
WHERE m.id = b.metric AND b.node = ?;`
// SQL_SELECT_NEW_METRICS = `SELECT name, type, detector, params, md5 FROM metrics
// WHERE id IN (SELECT metric FROM metric_bindings AS b, nodes AS n
// WHERE b.atime > n.atime AND b.node = n.id AND n.id = ?);`
)
func (v *Vis) runMonitor() {
mux := http.NewServeMux()
mux.HandleFunc(jarvis.URL_INDEX, safeHandler(v.handleIndex))
mux.HandleFunc(jarvis.URL_LOGIN, safeHandler(v.handleLogin))
mux.HandleFunc(jarvis.URL_PING, safeHandler(v.handlePing))
mux.HandleFunc(jarvis.URL_REPORT, safeHandler(v.handleReport))
mux.Handle(jarvis.URL_DETECTOR, http.FileServer(http.Dir(path.Join(v.root, jarvis.DIR_PUBLIC))))
server := &http.Server{Addr: v.config.MonitorAddr, Handler: mux}
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
//=-=-=-=-=-=-=-=-=-=-=-=
func (self *Vis) handleIndex(w http.ResponseWriter, r *http.Request) {
}
func (self *Vis) handleLogin(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
check(err)
var login jarvis.Login
err = json.Unmarshal(body, &login)
check(err)
var loginRsp jarvis.LoginRsp
loginRsp.ID = self.getNodeID(login.Type, login.Addr, r.RemoteAddr)
loginRsp.Metrics = self.getMetrics(loginRsp.ID)
respData, err := json.Marshal(loginRsp)
check(err)
if _, err = w.Write(respData); err != nil {
log.Println("[ERRO]", "Write response failed.")
}
_, err = self.db.Exec(SQL_UPDATE_NODE, login.Type, login.OS, login.CPU,
login.Core, login.Mem, login.Disk, login.Uptime, loginRsp.ID)
check(err)
}
func (self *Vis) handlePing(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
check(err)
var ping jarvis.Ping
err = json.Unmarshal(body, &ping)
check(err)
var pingRsp jarvis.PingRsp
pingRsp.ID = self.getNodeID(ping.Type, ping.Addr, r.RemoteAddr)
pingRsp.Metrics = self.getMetrics(pingRsp.ID)
respData, err := json.Marshal(pingRsp)
check(err)
if _, err = w.Write(respData); err != nil {
log.Println("[ERRO]", "Write response failed.")
}
_, err = self.db.Exec(SQL_UPDATE_NODE_UPTIME, ping.Uptime, ping.ID)
check(err)
}
func (self *Vis) getNodeID(typ, addr, remote string) (node int64) {
// node id
switch {
case strings.HasPrefix(addr, "127.0.0.1"):
fallthrough
case strings.HasPrefix(addr, "0.0.0.0"):
fallthrough
case strings.HasPrefix(addr, "localhost"):
_, op, e := net.SplitHostPort(addr)
if e == nil {
h, _, e := net.SplitHostPort(remote)
if e == nil {
addr = h + ":" + op
}
}
}
nodes := model.GetNodes()
err := nodes.GetIDFor(addr)
if err == sql.ErrNoRows {
n, e := nodes.Add(typ, addr)
check(e)
node = n.ID
} else {
check(err)
}
return
}
func (self *Vis) getMetrics(node int64) (metrics map[string]jarvis.MetricConfig) {
// metrics
rows, err := self.db.Query(SQL_SELECT_NODE_METRICS, node)
check(err)
defer rows.Close()
for rows.Next() {
var metric jarvis.MetricConfig
var params string
err = rows.Scan(&metric.ID, &metric.Type, &metric.Detector, ¶ms, &metric.MD5)
check(err)
if len(params) > 0 {
err = json.Unmarshal([]byte(params), &metric.Params)
check(err)
}
metrics[strconv.FormatInt(metric.ID, 10)] = metric
}
check(rows.Err())
return
}
func (self *Vis) handleReport(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
check(err)
var report jarvis.MetricReport
err = json.Unmarshal(body, &report)
check(err)
metricsRecords := model.GetMetricsRecords()
for metric, value := range report.Metrics {
mid, err := strconv.ParseInt(metric, 10, 0)
if err != nil {
log.Println("[WARN]", "report invalid metric id", metric, "from node", report.ID)
continue
}
err = metricsRecords.Add(report.ID, mid, value)
if err != nil {
log.Println("[WARN]", "new metric record:", report.ID, id, value, err)
}
}
if _, err = w.Write([]byte(jarvis.COMMON_RSP_OK)); err != nil {
log.Println("[WARN]", "Write response failed.")
}
}