-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
275 lines (233 loc) · 6.41 KB
/
main.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
//go:generate go run github.com/Al2Klimov/go-gen-source-repos
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
. "github.com/Al2Klimov/go-monplug-utils"
"github.com/masif-upgrader/agent/v1"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
)
type loadThresholds struct {
warn, crit OptionalThreshold
}
func (lt *loadThresholds) mkFlags(cli *flag.FlagSet, subject string) {
cli.Var(<.warn, subject+"-warn", "e.g. @~:42")
cli.Var(<.crit, subject+"-crit", "e.g. @~:42")
}
type loadsThresholds struct {
m1, m5, m15 loadThresholds
}
func (lt *loadsThresholds) mkFlags(cli *flag.FlagSet, subject string) {
lt.m1.mkFlags(cli, subject+"-1m")
lt.m5.mkFlags(cli, subject+"-5m")
lt.m15.mkFlags(cli, subject+"-15m")
}
type allThresholds struct {
query, install, update, configure, remove, purge, error loadsThresholds
}
func main() {
os.Exit(ExecuteCheck(onTerminal, checkMasifupgraderAgent))
}
func onTerminal() (output string) {
return fmt.Sprintf(
"For the terms of use, the source code and the authors\n"+
"see the projects this program is assembled from:\n\n %s\n",
strings.Join(GithubcomAl2klimovGo_gen_source_repos, "\n "),
)
}
var loadsThresholdsToMetrics = []struct {
subject string
threshold func(*allThresholds) *loadsThresholds
metric func(*v1.Load) *[3]float64
}{
{
"query",
func(at *allThresholds) *loadsThresholds { return &at.query },
func(load *v1.Load) *[3]float64 { return &load.Query },
},
{
"install",
func(at *allThresholds) *loadsThresholds { return &at.install },
func(load *v1.Load) *[3]float64 { return &load.Install },
},
{
"update",
func(at *allThresholds) *loadsThresholds { return &at.update },
func(load *v1.Load) *[3]float64 { return &load.Update },
},
{
"configure",
func(at *allThresholds) *loadsThresholds { return &at.configure },
func(load *v1.Load) *[3]float64 { return &load.Configure },
},
{
"remove",
func(at *allThresholds) *loadsThresholds { return &at.remove },
func(load *v1.Load) *[3]float64 { return &load.Remove },
},
{
"purge",
func(at *allThresholds) *loadsThresholds { return &at.purge },
func(load *v1.Load) *[3]float64 { return &load.Purge },
},
{
"error",
func(at *allThresholds) *loadsThresholds { return &at.error },
func(load *v1.Load) *[3]float64 { return &load.Error },
},
}
var loadThresholdsToMetrics = []struct {
subject string
threshold func(*loadsThresholds) *loadThresholds
metric func(*[3]float64) float64
}{
{
"1m",
func(lt *loadsThresholds) *loadThresholds { return <.m1 },
func(load *[3]float64) float64 { return load[0] },
},
{
"5m",
func(lt *loadsThresholds) *loadThresholds { return <.m5 },
func(load *[3]float64) float64 { return load[1] },
},
{
"15m",
func(lt *loadsThresholds) *loadThresholds { return <.m15 },
func(load *[3]float64) float64 { return load[2] },
},
}
func checkMasifupgraderAgent() (output string, perfdata PerfdataCollection, errs map[string]error) {
cli := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
restsock := cli.String("restsock", "", "ReST API socket path")
var respTime loadThresholds
respTime.mkFlags(cli, "resptime")
var threshold allThresholds
threshold.query.mkFlags(cli, "query")
threshold.install.mkFlags(cli, "install")
threshold.update.mkFlags(cli, "update")
threshold.configure.mkFlags(cli, "configure")
threshold.remove.mkFlags(cli, "remove")
threshold.purge.mkFlags(cli, "purge")
threshold.error.mkFlags(cli, "error")
if errCli := cli.Parse(os.Args[1:]); errCli != nil {
os.Exit(3)
}
if *restsock == "" {
fmt.Fprintln(os.Stderr, "-restsock missing")
cli.Usage()
os.Exit(3)
}
client := http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
var dialer net.Dialer
return dialer.DialContext(ctx, "unix", *restsock)
},
},
}
start := time.Now()
resp, errReq := client.Get("http://localhost/v1/load")
if errReq != nil {
return "", nil, map[string]error{
fmt.Sprintf("GET http://%s/v1/load", url.PathEscape(*restsock)): errReq,
}
}
body, errRA := ioutil.ReadAll(resp.Body)
if errRA != nil {
return "", nil, map[string]error{
fmt.Sprintf("GET http://%s/v1/load", url.PathEscape(*restsock)): errRA,
}
}
end := time.Now()
if resp.StatusCode != 200 {
return "", nil, map[string]error{
fmt.Sprintf("GET http://%s/v1/load", url.PathEscape(*restsock)): errors.New(resp.Status),
}
}
var payload v1.Load
if errJU := json.Unmarshal(body, &payload); errJU != nil {
return "", nil, map[string]error{
fmt.Sprintf("GET http://%s/v1/load", url.PathEscape(*restsock)): errJU,
}
}
perfdata = append(
make(PerfdataCollection, 0, 2+len(loadsThresholdsToMetrics)*len(loadThresholdsToMetrics)),
Perfdata{
Label: "time",
UOM: "us",
Value: float64(end.Sub(start)) / float64(time.Microsecond),
Warn: respTime.warn,
Crit: respTime.crit,
Min: OptionalNumber{true, 0},
},
Perfdata{
Label: "size",
UOM: "B",
Value: float64(len(body)),
Min: OptionalNumber{true, 0},
},
)
for _, lsttm := range loadsThresholdsToMetrics {
lst := lsttm.threshold(&threshold)
lsm := lsttm.metric(&payload)
for _, lttm := range loadThresholdsToMetrics {
lt := lttm.threshold(lst)
perfdata = append(perfdata, Perfdata{
Label: fmt.Sprintf("%s_%s", lsttm.subject, lttm.subject),
Value: lttm.metric(lsm),
Warn: lt.warn,
Crit: lt.crit,
Min: OptionalNumber{true, 0},
})
}
}
var failedThresholds []*Perfdata = nil
for i := range perfdata {
if pd := &perfdata[i]; pd.GetStatus() != Ok {
failedThresholds = append(failedThresholds, pd)
}
}
if failedThresholds == nil {
output = `<p style="color: #070;">OK</p>`
} else {
sort.Slice(failedThresholds, func(i, j int) bool {
lhs := failedThresholds[i]
rhs := failedThresholds[j]
lhss := lhs.GetStatus()
rhss := rhs.GetStatus()
if lhss == rhss {
return lhs.Label < rhs.Label
} else {
return lhss > rhss
}
})
var out bytes.Buffer
out.Write([]byte(`<p><b>Some threshold assertions failed:</b></p><ul>`))
colors := map[PerfdataStatus]string{Warning: "770", Critical: "700"}
for _, ft := range failedThresholds {
fmt.Fprintf(
&out,
`<li style="color: #%s;">%s = %s</li>`,
colors[ft.GetStatus()],
ft.Label,
strconv.FormatFloat(ft.Value, 'f', -1, 64),
)
}
out.Write([]byte(`</ul>`))
output = out.String()
}
return
}