-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_prototype.go
141 lines (118 loc) · 2.98 KB
/
metrics_prototype.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
package main
import (
"archive/zip"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"regexp"
"strings"
)
var (
diag string
)
func init() {
flag.StringVar(&diag, "diag", "", "agent diagnostics zip file")
}
func main() {
log.SetFlags(0)
flag.Parse()
if diag == "" {
log.Fatal("-diag is required")
}
zipListing, err := zip.OpenReader(diag)
if err != nil {
log.Fatal(err)
}
defer zipListing.Close()
filepath_metrics_re, err := regexp.Compile("components/.+/input_metrics.json")
if err != nil {
return
}
var inputMetrics []map[string]interface{}
for _, file := range zipListing.File {
match := filepath_metrics_re.MatchString(file.Name)
if match {
fmt.Printf("Analyzing %s\n", file.Name)
reader, err := file.Open()
if err != nil {
log.Fatal("Failed to read input file.", err)
}
defer reader.Close()
dec := json.NewDecoder(reader)
for {
var result []map[string]interface{}
if err := dec.Decode(&result); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
if len(result) == 0 {
// sometimes we have empty metrics files
continue
}
inputMetrics = append(inputMetrics, result...)
}
}
}
var metricWarnings []MetricWarning
for _, metricset := range inputMetrics {
for _, check := range checks {
if metricset["input"] == check.Input {
matchCount := 0
for _, condition := range check.Conditions {
value := getMetricValue(condition.MetricName, metricset)
var threshold float64 = float64(condition.Threshold)
if condition.Operator == GreaterThan && value > threshold ||
condition.Operator == GreaterThanEquals && value >= threshold ||
condition.Operator == LessThan && value < threshold ||
condition.Operator == LessThanEquals && value <= threshold {
matchCount++
}
}
if matchCount == len(check.Conditions) {
warning := MetricWarning{
InputName: metricset["id"].(string),
Check: check,
}
metricWarnings = append(metricWarnings, warning)
}
}
}
}
if len(metricWarnings) > 0 {
for _, warning := range metricWarnings {
fmt.Printf("Alert triggered for: %s refer to documentation: %s\n", warning.InputName, warning.Check.DocLink)
}
} else {
fmt.Println("no warnings identified")
}
}
func getMetricValue(metricName string, metricset map[string]interface{}) float64 {
var value float64
metricNameParts := strings.Split(metricName, ".")
if len(metricNameParts) > 1 {
// histogram metric
metric := metricset[metricNameParts[0]]
jsonBytes, _ := json.Marshal(metric)
dec := json.NewDecoder(strings.NewReader(string(jsonBytes)))
for {
var hm HistogramMetric
if err := dec.Decode(&hm); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
value = hm[metricNameParts[1]][metricNameParts[2]]
}
} else {
// numerical metric
if nm, ok := metricset[metricName].(float64); ok {
value = nm
} else {
log.Fatal("unable to parse numerical metric")
}
}
return value
}