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
|
package internal
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
type checkState struct {
Status nagiosCode
PrevStatus nagiosCode
output string
}
func (cs checkState) changed() bool {
return cs.Status != cs.PrevStatus
}
type state struct {
stateFile string
checks map[string]checkState
}
func readState(config config) (state, error) {
s := state{
stateFile: fmt.Sprintf("%s/state.json", config.StateDir),
checks: make(map[string]checkState),
}
if _, err := os.Stat(s.stateFile); err != nil {
// OK, may be first run with no state yet.
return s, nil
}
file, err := os.Open(s.stateFile)
if err != nil {
return s, err
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return s, err
}
if err := json.Unmarshal(bytes, &s.checks); err != nil {
return s, err
}
var obsolete []string
for name := range s.checks {
if _, ok := config.Checks[name]; !ok {
obsolete = append(obsolete, name)
}
}
for _, name := range obsolete {
delete(s.checks, name)
log.Printf("State of %s is obsolete (removed)", name)
}
return s, nil
}
func (s state) update(result checkResult) {
prevStatus := unknown
prevState, ok := s.checks[result.name]
if ok {
prevStatus = prevState.Status
}
cs := checkState{result.status, prevStatus, result.output}
s.checks[result.name] = cs
log.Println(result.name, cs)
}
func (s state) persist() error {
jsonData, err := json.Marshal(s.checks)
if err != nil {
return err
}
return ioutil.WriteFile(s.stateFile, jsonData, os.ModePerm)
}
func (s state) report() (string, string, bool) {
var sb strings.Builder
sb.WriteString("This is the recent Gogios report!\n\n")
sb.WriteString("# Alerts with status changed:\n\n")
changed := s.reportChanged(&sb)
sb.WriteString("# Unhandled alerts:\n\n")
numCriticals, numWarnings, numUnknown, numOK := s.reportUnhandled(&sb)
sb.WriteString("Have a nice day!\n")
subject := fmt.Sprintf("GOGIOS Report [C:%d W:%d U:%d OK:%d]",
numCriticals, numWarnings, numUnknown, numOK)
return subject, sb.String(), changed || numCriticals > 0
}
func (s state) reportChanged(sb *strings.Builder) (changed bool) {
if 0 < s.reportBy(sb, func(cs checkState) bool {
return cs.Status == critical && cs.changed()
}) {
changed = true
}
if 0 < s.reportBy(sb, func(cs checkState) bool {
return cs.Status == warning && cs.changed()
}) {
changed = true
}
if 0 < s.reportBy(sb, func(cs checkState) bool {
return cs.Status == unknown && cs.changed()
}) {
changed = true
}
if 0 < s.reportBy(sb, func(cs checkState) bool {
return cs.Status == ok && cs.changed()
}) {
changed = true
}
return
}
func (s state) reportUnhandled(sb *strings.Builder) (numCriticals, numWarnings,
numUnknown, numOK int) {
numCriticals = s.reportBy(sb, func(cs checkState) bool { return cs.Status == critical })
numWarnings = s.reportBy(sb, func(cs checkState) bool { return cs.Status == warning })
numUnknown = s.reportBy(sb, func(cs checkState) bool { return cs.Status == unknown })
numOK = s.countBy(func(cs checkState) bool { return cs.Status == ok })
return
}
func (s state) reportBy(sb *strings.Builder,
filter func(cs checkState) bool) (count int) {
for name, cs := range s.checks {
if !filter(cs) {
continue
}
count++
if cs.changed() {
sb.WriteString(nagiosCode(cs.PrevStatus).Str())
sb.WriteString("->")
}
sb.WriteString(nagiosCode(cs.Status).Str())
sb.WriteString(": ")
sb.WriteString(name)
sb.WriteString(" ==>> ")
sb.WriteString(cs.output)
sb.WriteString("\n")
}
if count > 0 {
sb.WriteString("\n")
}
return
}
func (s state) countBy(filter func(cs checkState) bool) (count int) {
for _, cs := range s.checks {
if filter(cs) {
count++
}
}
return
}
|