-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
254 lines (218 loc) · 6.7 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
package main
import (
"fmt"
"os"
"sort"
"time"
monitoring "cloud.google.com/go/monitoring/apiv3/v2"
googlepb "github.com/golang/protobuf/ptypes/timestamp"
"github.com/jessevdk/go-flags"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
)
const (
OK = iota
WARNING
CRITICAL
UNKNOWN
)
type Options struct {
Project string `short:"g" long:"project" required:"true" description:"GCP project id." `
Auth string `short:"a" long:"auth" required:"true" default:"~/gcp_auth_key.json" description:"GCP authenticate key." `
Metric string `short:"m" long:"metric" required:"true" description:"Monitoring metric." `
Filter string `short:"f" long:"filter" required:"false" default:"" description:"Filter query." `
Delay int64 `short:"d" long:"delay" required:"false" default:"4" description:"Shift the acquisition period." `
Period int64 `short:"p" long:"period" required:"false" default:"5" description:"Metric acquisition period." `
Evalution string `short:"e" long:"evalution" required:"false" default:"MAX" description:"Metric evaluate type." `
Critical float64 `short:"c" long:"critical" required:"false" default:"0.0" description:"Critical threshold." `
Warning float64 `short:"w" long:"warning" required:"false" default:"0.0" description:"Warning threshold." `
Verbose []bool `short:"v" long:"verbose" required:"false" description:"Verbose option." `
}
type CheckResult struct {
Status int
ResourceID string
ResourceFullName string
ThresholdWarning float64
ThresholdCritical float64
Metric float64
Message string
}
func main() {
var opts Options
parser := flags.NewParser(&opts, flags.IgnoreUnknown)
_, err := parser.Parse()
if err != nil {
parser.WriteHelp(os.Stdout)
fmt.Printf("UNKNOWN: Missing required arguments. \n")
os.Exit(UNKNOWN)
}
verbose(opts.Verbose, opts)
err = os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", opts.Auth)
if err != nil {
fmt.Printf("UNKNOWN: Missing required arguments. \n")
os.Exit(UNKNOWN)
}
ctx := context.Background()
c, err := monitoring.NewMetricClient(ctx)
if err != nil {
fmt.Printf("UNKNOWN: GCP SDK Client request failed. \n")
os.Exit(UNKNOWN)
}
var filter = fmt.Sprintf("metric.type = \"%s\" ", opts.Metric)
if len(opts.Filter) != 0 {
filter += fmt.Sprintf("AND %s ", opts.Filter)
}
verbose(opts.Verbose, filter)
unixNow := time.Now().Unix()
req := &monitoringpb.ListTimeSeriesRequest{
Name: "projects/" + opts.Project,
Filter: filter,
Interval: &monitoringpb.TimeInterval{
EndTime: &googlepb.Timestamp{
Seconds: unixNow - (opts.Delay * 60),
},
StartTime: &googlepb.Timestamp{
Seconds: unixNow - ((opts.Delay + opts.Period) * 60),
},
},
}
status := OK
resourceType := ""
var checkResults []CheckResult
it := c.ListTimeSeries(ctx, req)
for {
resp, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
verbose(opts.Verbose, err)
fmt.Printf("UNKNOWN: Failed to fetch time series. \n")
os.Exit(UNKNOWN)
}
var value CheckResult
value.ThresholdWarning = opts.Warning
value.ThresholdCritical = opts.Critical
resourceType = resp.Resource.Type
verbose(opts.Verbose, resp.Metric)
verbose(opts.Verbose, resp.Resource)
length := len(resp.Points)
if length == 0 {
status = UNKNOWN
value.Status = UNKNOWN
value.Message = "Time series is empty."
continue
}
value.Metric = evaluate(opts.Evalution, resp.ValueType.String(), resp.Points)
verbose(opts.Verbose, value.Metric)
labelsMap := resp.Resource.GetLabels()
value.ResourceID = labelsMap["instance_id"]
value.ResourceFullName = "Project=" + labelsMap["project_id"] + " Zone=" + labelsMap["zone"] + " ID=" + labelsMap["instance_id"]
if opts.Warning > 0.0 && value.Metric < opts.Warning {
value.Status = OK
value.Message = metric2string(value, opts)
}
if opts.Warning > 0.0 && value.Metric >= opts.Warning {
if status <= WARNING {
status = WARNING
}
value.Status = WARNING
value.Message = metric2string(value, opts)
}
if opts.Critical > 0.0 && value.Metric >= opts.Critical {
status = CRITICAL
value.Status = CRITICAL
value.Message = metric2string(value, opts)
}
checkResults = append(checkResults, value)
}
if status == OK {
performance := getPerformanceData(checkResults)
fmt.Printf("OK: All %d %s metrics less then tresholds %s \n", len(checkResults), resourceType, performance)
} else {
output(checkResults)
}
os.Exit(status)
}
func getPerformanceData(data []CheckResult) string {
performance := "|"
for _, value := range data {
if value.Status == UNKNOWN {
performance += fmt.Sprintf("'%s'=U;%d;%d;0;0 ", value.ResourceID, int(value.ThresholdWarning), int(value.ThresholdCritical))
} else {
performance += fmt.Sprintf("'%s'=%f;%d;%d;0;0 ", value.ResourceID, value.Metric, int(value.ThresholdWarning), int(value.ThresholdCritical))
}
}
return performance
}
func metric2string(data CheckResult, opts Options) string {
statusStr := ""
switch data.Status {
case OK:
statusStr = "OK"
case WARNING:
statusStr = "WARNING"
case CRITICAL:
statusStr = "CRITICAL"
case UNKNOWN:
statusStr = "UNKNOWN"
default:
statusStr = "UNKNOWN"
}
return fmt.Sprintf("%s: %s [warn=%d:crit=%d:datapoints=%d]\n", statusStr, data.ResourceFullName, int(opts.Warning), int(opts.Critical), int(data.Metric))
}
func output(results []CheckResult) {
sort.Slice(results, func(i, j int) bool {
return results[i].Status > results[j].Status
})
message := ""
for _, value := range results {
message += value.Message
}
performance := getPerformanceData(results)
fmt.Println(message + performance)
}
func evaluate(evaluateType string, valueType string, points []*monitoringpb.Point) float64 {
var ret float64
switch evaluateType {
case "LAST":
ret = getFloatValue(valueType, points[0].GetValue())
case "SUM":
for _, point := range points {
ret += getFloatValue(valueType, point.GetValue())
}
case "MAX":
var current float64
for _, point := range points {
current = getFloatValue(valueType, point.GetValue())
if current < ret {
continue
}
ret = current
}
}
return ret
}
func getFloatValue(valueType string, typedValue *monitoringpb.TypedValue) float64 {
var ret float64
switch valueType {
case "INT64":
ret = float64(typedValue.GetInt64Value())
case "DOUBLE":
ret = typedValue.GetDoubleValue()
case "DISTRIBUTION":
ret = typedValue.GetDistributionValue().GetMean()
default:
// Expected "BOOL" "STRING" "MONEY", these cases are unsupported.
}
return ret
}
func verbose(flag []bool, value interface{}) {
if len(flag) == 0 {
return
}
if flag[0] {
fmt.Println(value)
}
}