forked from mperham/inspeqtor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
356 lines (305 loc) · 8.11 KB
/
commands.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package inspeqtor
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"expvar"
"fmt"
"io"
"math"
"net"
"os"
"strings"
"time"
"github.com/mperham/inspeqtor/util"
)
/*
* Commands are ways for the external world to communicate with Inspeqtor
* via its command socket.
*/
type commandFunc func(*Inspeqtor, []string, io.Writer)
var (
CommandHandlers = map[string]commandFunc{
"start": startDeploy,
"finish": finishDeploy,
"status": currentStatus,
"export": export,
"show": sparkline,
"♡": heart,
"--help": showHelp,
"help": showHelp,
}
)
func (i *Inspeqtor) openSocket(path string) error {
if i.Socket != nil {
return errors.New("Socket is already open!")
}
socket, err := net.Listen("unix", path)
if err != nil {
return err
}
i.Socket = socket
return nil
}
func (i *Inspeqtor) acceptCommand() bool {
c, err := i.Socket.Accept()
if err != nil {
select {
case <-i.Stopping:
// we're stopping or reloading, no big deal...
default:
util.Warn("%v", err)
}
return false
}
defer c.Close()
c.SetDeadline(time.Now().Add(2 * time.Second))
reader := bufio.NewReader(c)
line, err := reader.ReadString('\n')
if err != nil {
util.Info("Did not receive command line in time: %s", err.Error())
return true
}
fields := strings.Fields(line)
if len(fields) == 0 {
showHelp(i, []string{}, c)
return true
}
funk := CommandHandlers[fields[0]]
if funk == nil {
util.Warn("Unknown command: %s", strings.TrimSpace(line))
io.WriteString(c, "Unknown command: "+line)
return true
}
funk(i, fields[1:], c)
return true
}
func showHelp(i *Inspeqtor, args []string, resp io.Writer) {
io.WriteString(resp, `inspeqtorctl [command] [args]
Commands:
status - show the current state of all tracked metrics
start deploy - start a deploy window, mutes all alerts
finish deploy - close a deploy window, enables alerts
export - dump the current state of all metrics as JSON
show ["host"|service] [metric] - show sparkline graph of metric
Examples:
inspeqtorctl show host load:5
inspeqtorctl show mysql mysql:Queries
`)
}
func startDeploy(i *Inspeqtor, args []string, resp io.Writer) {
length := time.Duration(i.GlobalConfig.DeployLength) * time.Second
i.SilenceUntil = time.Now().Add(length)
counters.Get("deploy").(*expvar.Int).Set(1)
util.Info("Starting deploy")
io.WriteString(resp, "Starting deploy, now silenced\n")
}
func finishDeploy(i *Inspeqtor, args []string, resp io.Writer) {
// silence for a cycle, give processes a little time to
// settle before alerting again. We don't want a restart
// during a deploy to send email for those events.
i.SilenceUntil = time.Now().Add(time.Duration(i.GlobalConfig.CycleTime) * time.Second)
counters.Get("deploy").(*expvar.Int).Set(0)
util.Info("Finished deploy")
io.WriteString(resp, "Finished deploy, volume turned to 11\n")
}
func currentStatus(i *Inspeqtor, args []string, resp io.Writer) {
io.WriteString(resp, fmt.Sprintf(
"%s %s, uptime: %s, pid: %d\n", Name, VERSION, time.Now().Sub(i.StartedAt).String(), os.Getpid()))
io.WriteString(resp, "\n")
io.WriteString(resp, fmt.Sprintf("Host: %s\n", i.Host.Name()))
store := i.Host.Metrics()
for _, fam := range store.Families() {
for _, met := range store.MetricNames(fam) {
name := fam
if met != "" {
name = name + ":" + met
}
var rule *Rule
for _, r := range i.Host.Rules() {
if r.Metric() == name {
rule = r
}
}
if rule != nil {
io.WriteString(resp, fmt.Sprintf("%-1s %-35s %-15s %s\n", rule.DisplayState(), name, store.Display(fam, met), rule.DisplayThreshold))
} else {
io.WriteString(resp, fmt.Sprintf(" %-35s %-15s\n", name, store.Display(fam, met)))
}
}
}
for _, svc := range i.Services {
io.WriteString(resp, "\n")
io.WriteString(resp, fmt.Sprintf("Service: %s\n", svc))
store := svc.Metrics()
for _, fam := range store.Families() {
for _, met := range store.MetricNames(fam) {
name := fam
if met != "" {
name = name + ":" + met
}
var rule *Rule
for _, r := range svc.Rules() {
if r.Metric() == name {
rule = r
}
}
if rule != nil {
io.WriteString(resp, fmt.Sprintf("%-1s %-35s %-15s %s\n", rule.DisplayState(), name, store.Display(fam, met), rule.DisplayThreshold))
} else {
io.WriteString(resp, fmt.Sprintf(" %-35s %-15s\n", name, store.Display(fam, met)))
}
}
}
}
}
func export(i *Inspeqtor, args []string, resp io.Writer) {
dump := map[string]interface{}{}
dump["exported_at"] = time.Now().Unix()
host := map[string]interface{}{}
host["metrics"] = pullMetrics(i.Host)
host["name"] = i.Host.Name()
dump["host"] = host
svcs := map[string]interface{}{}
for _, service := range i.Services {
s := service.(*Service)
svc := map[string]interface{}{}
svc["metrics"] = pullMetrics(service)
svc["pid"] = s.Process.Pid
svc["name"] = service.Name()
svcs[service.Name()] = svc
}
dump["services"] = svcs
err := json.NewEncoder(resp).Encode(dump)
if err != nil {
io.WriteString(resp, fmt.Sprintf("Unable to dump JSON: %s", err.Error()))
}
}
func pullMetrics(thing Checkable) map[string]interface{} {
metrics := map[string]interface{}{}
store := thing.Metrics()
for _, fam := range store.Families() {
famdata := map[string]interface{}{}
for _, met := range store.MetricNames(fam) {
famdata[met] = store.Get(fam, met)
}
metrics[fam] = famdata
}
return metrics
}
func heart(i *Inspeqtor, args []string, resp io.Writer) {
io.WriteString(resp, "Awwww, I love you too.\n")
}
// Beautiful, love this Go feature where you
// can slice out only the methods you need for simplicity
// of testing...
type displayable interface {
At(int) *float64
Displayable(float64) string
Size() int
}
func sparkline(i *Inspeqtor, args []string, resp io.Writer) {
if len(args) < 2 {
io.WriteString(resp, "show [target] [metric]\n")
return
}
targetName := args[0]
var target Checkable
if targetName == "host" {
target = i.Host
} else {
for _, s := range i.Services {
if s.Name() == targetName {
target = s
}
}
}
if target == nil {
io.WriteString(resp, fmt.Sprintf("Invalid target: %s\n", targetName))
return
}
output := buildSparkline(target, args[1], func(family, name string) displayable {
return target.Metrics().Metric(family, name)
})
io.WriteString(resp, output)
}
func buildSparkline(target Checkable, metric string, buf func(string, string) displayable) string {
family, name := parseMetric(metric)
buff := buf(family, name)
if buff == nil {
return fmt.Sprintf("Unknown metric: %s\n", metric)
}
sz := buff.Size()
values := make([]float64, sz)
for i := 0; i > -sz; i-- {
v := buff.At(i)
if v == nil {
util.Warn("BUG: Nil data in ring buffer: %d %d", sz, i)
return "Inspeqtor bug, error building graph\n"
}
values[-i] = *v
}
// does not work for some reason, SO to the rescue!
//sort.Reverse(sort.Float64Slice(values))
for i, j := 0, len(values)-1; i < j; i, j = i+1, j-1 {
values[i], values[j] = values[j], values[i]
}
var min, max, sum, avg float64
min = math.MaxFloat64
for _, val := range values {
if min > val {
min = val
}
if max < val {
max = val
}
sum += val
}
if len(values) > 0 {
avg = sum / float64(len(values))
}
var resp bytes.Buffer
resp.WriteString(fmt.Sprintf("%s %s min %s max %s avg %s\n",
target.Name(),
metric,
buff.Displayable(min),
buff.Displayable(max),
buff.Displayable(avg)))
runes := []string{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"}
tick := (max - min) / 8
for _, x := range values {
diff := int((x - min) / tick)
if diff > 7 {
diff = 7
}
if diff < 0 {
diff = 0
}
resp.WriteString(runes[diff])
}
resp.WriteString("\n")
return string(resp.Bytes())
}
func parseMetric(metric string) (string, string) {
if strings.Index(metric, ":") > 0 {
fields := strings.Split(metric, ":")
family := fields[0]
name := ""
if len(fields) > 1 {
name = fields[1]
}
return family, name
} else if strings.Index(metric, "(") > 0 {
fields := strings.Split(metric, "(")
family := fields[0]
name := ""
if len(fields) > 1 {
name = fields[1]
name = name[0 : len(name)-1]
}
return family, name
} else {
return metric, ""
}
}