summaryrefslogtreecommitdiff
path: root/internal/state.go
blob: e33d5cb0396123306c0c4a7beab75d93baa376e4 (plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package internal

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"strings"
	"time"
)

type checkState struct {
	Status        nagiosCode
	PrevStatus    nagiosCode
	Epoch         int64  `json:"Epoch,omitempty"`
	Output        string `json:"Output,omitempty"`
	FederatedFrom string `json:"FederatedFrom,omitempty"`
}

func (cs checkState) federated() bool {
	return cs.FederatedFrom != ""
}

func (cs checkState) changed() bool {
	return cs.Status != cs.PrevStatus
}

type state struct {
	stateFile  string
	checks     map[string]checkState
	staleEpoch int64
}

func newState(conf config) (state, error) {
	s := state{
		stateFile:  fmt.Sprintf("%s/state.json", conf.StateDir),
		checks:     make(map[string]checkState),
		staleEpoch: time.Now().Unix() - int64(conf.StaleThreshold),
	}

	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 := io.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 := conf.Checks[name]; !ok && !strings.HasPrefix(name, "Prometheus") {
			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 := nagiosUnknown
	prevState, ok := s.checks[result.name]
	if ok {
		prevStatus = prevState.Status
	}

	cs := checkState{result.status, prevStatus, result.epoch, result.output, result.federatedFrom}
	s.checks[result.name] = cs
	log.Println(result.name, cs)
}

func (s state) age(name string) time.Duration {
	if prevState, ok := s.checks[name]; ok {
		return time.Since(time.Unix(prevState.Epoch, 0))
	}

	return time.Duration(0)
}

// To be used to merge the state of another server running Gogios
func (s state) merge(other state) error {
	for name, cs := range other.checks {
		if _, ok := s.checks[name]; ok {
			return fmt.Errorf("can't merge state due to duplicate check name '%s'", name)
		}
		s.checks[name] = cs
	}
	return nil
}

func (s state) mergeFromBytes(bytes []byte) error {
	var other state
	if err := json.Unmarshal(bytes, &other.checks); err != nil {
		return err
	}
	return s.merge(other)
}

func (s state) persist() error {
	stateDir := filepath.Dir(s.stateFile)
	if _, err := os.Stat(stateDir); os.IsNotExist(err) {
		if err := os.MkdirAll(stateDir, 0o755); err != nil {
			return err
		}
	}

	jsonData, err := json.Marshal(s.checks)
	if err != nil {
		return err
	}

	return os.WriteFile(s.stateFile, jsonData, os.ModePerm)
}

// report generates the notification email content.
// statusPageURL is included as a link to the HTML status page.
// conf is used to determine which checks should be suppressed from the report.
func (s state) report(
	renotify, force bool,
	statusPageURL string,
	conf config,
	passive bool,
	passiveReason string,
) (string, string, bool) {
	var sb strings.Builder

	sb.WriteString("This is the recent Gogios report!\n\n")
	if passive {
		sb.WriteString("NOTE: Passive mode active, checks were skipped.\n")
		if passiveReason != "" {
			sb.WriteString("Reason: ")
			sb.WriteString(passiveReason)
			sb.WriteString("\n")
		}
		sb.WriteString("\n")
	}

	sb.WriteString("# Alerts with status changed:\n\n")
	changed := s.reportChanged(&sb, conf)
	if !changed {
		sb.WriteString("There were no status changes...\n\n")
	}

	sb.WriteString("# Unhandled alerts:\n\n")
	numCriticals, numWarnings, numUnknown, numOK := s.reportUnhandled(&sb, conf)
	hasUnhandled := (numCriticals + numWarnings + numUnknown) > 0
	if !hasUnhandled {
		sb.WriteString("There are no unhandled alerts...\n\n")
	}

	sb.WriteString("# Stale alerts:\n\n")
	numStale := s.reportStaleAlerts(&sb, conf)
	if numStale == 0 {
		sb.WriteString("There are no stale alerts...\n\n")
	}

	sb.WriteString("# Suppressed alerts:\n\n")
	numSuppressed := s.reportSuppressed(&sb, conf)
	if numSuppressed == 0 {
		sb.WriteString("There are no suppressed alerts...\n\n")
	}

	sb.WriteString("# Status page:\n\n")
	sb.WriteString(statusPageURL)
	sb.WriteString("\n\n")

	sb.WriteString("Have a nice day!\n")

	subject := fmt.Sprintf("GOGIOS Report [C:%d W:%d U:%d S:%d SU:%d OK:%d]",
		numCriticals, numWarnings, numUnknown, numStale, numSuppressed, numOK)

	doNotify := force || (changed || (renotify && hasUnhandled))
	return subject, sb.String(), doNotify
}

func (s state) reportChanged(sb *strings.Builder, conf config) (changed bool) {
	if 0 < s.reportBy(sb, true, false, conf, func(cs checkState) bool {
		return cs.Status == nagiosCritical && cs.changed()
	}) {
		changed = true
	}

	if 0 < s.reportBy(sb, true, false, conf, func(cs checkState) bool {
		return cs.Status == nagiosWarning && cs.changed()
	}) {
		changed = true
	}

	if 0 < s.reportBy(sb, true, false, conf, func(cs checkState) bool {
		return cs.Status == nagiosUnknown && cs.changed()
	}) {
		changed = true
	}

	if 0 < s.reportBy(sb, true, false, conf, func(cs checkState) bool {
		return cs.Status == nagiosOk && cs.changed()
	}) {
		changed = true
	}

	return
}

func (s state) reportUnhandled(sb *strings.Builder, conf config) (numCriticals, numWarnings,
	numUnknown, numOK int,
) {
	numCriticals = s.reportBy(sb, false, false, conf, func(cs checkState) bool {
		return cs.Status == nagiosCritical
	})

	numWarnings = s.reportBy(sb, false, false, conf, func(cs checkState) bool {
		return cs.Status == nagiosWarning
	})

	numUnknown = s.reportBy(sb, false, false, conf, func(cs checkState) bool {
		return cs.Status == nagiosUnknown
	})

	numOK = s.countBy(conf, func(cs checkState) bool {
		return cs.Status == nagiosOk
	})

	return
}

func (s state) reportStaleAlerts(sb *strings.Builder, conf config) int {
	// Only report stale alerts that are not OK, since stale OK alerts aren't concerning
	return s.reportBy(sb, false, true, conf, func(cs checkState) bool {
		return cs.Epoch < s.staleEpoch && cs.Status != nagiosOk
	})
}

// reportSuppressed lists all non-OK checks that are currently suppressed via OnlyIfNotExists.
// This provides visibility into which alerts are being muted during maintenance windows.
// OK checks are never shown as suppressed since there's nothing to suppress.
func (s state) reportSuppressed(sb *strings.Builder, conf config) (count int) {
	for name, cs := range s.checks {
		if cs.Status == nagiosOk || !isCheckSuppressed(name, conf) {
			continue // OK checks are never shown as suppressed
		}
		count++

		sb.WriteString(nagiosCode(cs.Status).Str())
		sb.WriteString(": ")
		sb.WriteString(name)
		sb.WriteString(": ")
		sb.WriteString(cs.Output)
		if cs.federated() {
			sb.WriteString(" [federated from ")
			sb.WriteString(cs.FederatedFrom)
			sb.WriteString("]")
		}
		sb.WriteString(" [SUPPRESSED]")
		sb.WriteString("\n")
	}

	if count > 0 {
		sb.WriteString("\n")
	}
	return
}

// reportBy iterates over checks matching the filter and writes them to sb.
// Non-OK checks that are suppressed via OnlyIfNotExists are excluded from the report.
// OK checks are never suppressed since there's nothing to suppress.
func (s state) reportBy(sb *strings.Builder, showStatusChange, isStaleReport bool,
	conf config, filter func(cs checkState) bool,
) (count int) {
	for name, cs := range s.checks {
		if !filter(cs) {
			continue
		}
		if !isStaleReport && cs.Epoch < s.staleEpoch {
			continue // skip stale checks in non-stale report
		}
		if cs.Status != nagiosOk && isCheckSuppressed(name, conf) {
			continue // skip suppressed checks (OK checks are never suppressed)
		}
		count++

		if showStatusChange && 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)
		if cs.federated() {
			sb.WriteString(" [federated from ")
			sb.WriteString(cs.FederatedFrom)
			sb.WriteString("]")
		}

		if isStaleReport {
			lastCheckedAgo := time.Since(time.Unix(cs.Epoch, 0))
			sb.WriteString(fmt.Sprintf(" (last checked %v ago)", lastCheckedAgo))
		}

		sb.WriteString("\n")
	}

	if count > 0 {
		sb.WriteString("\n")
	}
	return
}

// countBy counts checks matching the filter, excluding suppressed non-OK checks.
// OK checks are never suppressed since there's nothing to suppress.
func (s state) countBy(conf config, filter func(cs checkState) bool) (count int) {
	for name, cs := range s.checks {
		if cs.Status != nagiosOk && isCheckSuppressed(name, conf) {
			continue // skip suppressed checks (OK checks are never suppressed)
		}
		if filter(cs) {
			count++
		}
	}
	return
}