This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
check.go
85 lines (67 loc) · 2.47 KB
/
check.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
package main
import (
"context"
"fmt"
"strings"
"github.com/urfave/cli"
)
// Return codes that represents service status
const (
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
)
// Check checks metric data
func Check(c *cli.Context) error {
client, err := NewClient(c.GlobalString("subscription-id"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("construct api client failed: %s", err.Error()), UNKNOWN)
}
if c.String("metric-name") == "" {
return cli.NewExitError("missing metric-name", UNKNOWN)
}
if strings.Contains(c.String("metric-name"), ",") {
return cli.NewExitError("the metric-name option can only specify a single metric name", UNKNOWN)
}
input := buildFetchMetricDataInput(c)
warningOver := c.Float64("warning-over")
warningUnder := c.Float64("warning-under")
criticalOver := c.Float64("critical-over")
criticalUnder := c.Float64("critical-under")
return _check(client, input, warningOver, warningUnder, criticalOver, criticalUnder)
}
func _check(client *Client, input FetchMetricDataInput, warningOver, warningUnder, criticalOver, criticalUnder float64) error {
metrics, err := FetchMetricData(context.TODO(), client, input)
if err != nil {
return cli.NewExitError(fmt.Sprintf("fetch metric data failed: %s", err.Error()), UNKNOWN)
}
v, ok := metrics[input.metricNames[0]]
if !ok {
return cli.NewExitError("UNKNOWN - No datapoint", UNKNOWN)
}
var data float64
switch input.aggregation {
case "Total":
data = *v.Total
case "Average":
data = *v.Average
case "Maximum":
data = *v.Maximum
case "Minimum":
data = *v.Minimum
}
if criticalOver != 0 && data > criticalOver {
return cli.NewExitError(fmt.Sprintf("CRITICAL - %s %s is %f that over than %f", input.resource, input.metricNames[0], data, criticalOver), CRITICAL)
}
if criticalUnder != 0 && data < criticalUnder {
return cli.NewExitError(fmt.Sprintf("CRITICAL - %s %s is %f that under than %f", input.resource, input.metricNames[0], data, criticalUnder), CRITICAL)
}
if warningOver != 0 && data > warningOver {
return cli.NewExitError(fmt.Sprintf("WARNING - %s %s is %f that over than %f", input.resource, input.metricNames[0], data, warningOver), WARNING)
}
if warningUnder != 0 && data < warningUnder {
return cli.NewExitError(fmt.Sprintf("WARNING - %s %s is %f that under than %f", input.resource, input.metricNames[0], data, warningUnder), WARNING)
}
return cli.NewExitError(fmt.Sprintf("OK - %s %s is %f", input.resource, input.metricNames[0], data), OK)
}