forked from mperham/inspeqtor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
277 lines (239 loc) · 6.07 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
package inspeqtor
import (
"bufio"
"bytes"
"errors"
"fmt"
"github.com/mperham/inspeqtor/util"
"io"
"math"
"net"
"os"
"strings"
"time"
)
/*
* 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,
"show": sparkline,
"♡": heart,
}
)
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() {
c, err := i.Socket.Accept()
if err != nil {
if i.Valid {
util.Warn("Unix socket shutdown: %s", err.Error())
}
return
}
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 a command line in time: %s", err.Error())
}
fields := strings.Fields(line)
funk := CommandHandlers[fields[0]]
if funk == nil {
util.Warn("Unknown command: %s", strings.TrimSpace(line))
io.WriteString(c, "Unknown command: "+line)
return
}
funk(i, fields[1:], c)
}
func startDeploy(i *Inspeqtor, args []string, resp io.Writer) {
length := time.Duration(i.GlobalConfig.Top.DeployLength) * time.Second
i.SilenceUntil = time.Now().Add(length)
util.Info("Starting deploy")
io.WriteString(resp, "Starting deploy, now silenced\n")
}
func finishDeploy(i *Inspeqtor, args []string, resp io.Writer) {
i.SilenceUntil = time.Now()
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 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, ""
}
}