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
|
package internal
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
type checkState struct {
Status nagiosCode
PrevStatus nagiosCode
output string
}
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
}
checkState := checkState{result.status, prevStatus, result.output}
s.checks[result.name] = checkState
log.Println(result.name, checkState)
}
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
var changed bool
f := func(filter func(n nagiosCode) bool) int {
var count int
for name, checkState := range s.checks {
if !filter(checkState.Status) {
continue
}
count++
if checkState.Status != checkState.PrevStatus {
sb.WriteString(nagiosCode(checkState.PrevStatus).Str())
sb.WriteString("->")
changed = true
}
sb.WriteString(nagiosCode(checkState.Status).Str())
sb.WriteString(": ")
sb.WriteString(name)
sb.WriteString(" ==>> ")
sb.WriteString(checkState.output)
sb.WriteString("\n")
}
return count
}
sb.WriteString("This is the recent Gogios report!\n\n")
numCriticals := f(func(n nagiosCode) bool { return n == 2 })
if numCriticals > 0 {
sb.WriteString("\n")
}
numWarnings := f(func(n nagiosCode) bool { return n == 1 })
if numWarnings > 0 {
sb.WriteString("\n")
}
numUnknowns := f(func(n nagiosCode) bool { return n > 2 })
if numUnknowns > 0 {
sb.WriteString("\n")
}
numOks := f(func(n nagiosCode) bool { return n == 0 })
if numOks > 0 {
sb.WriteString("\n")
}
sb.WriteString("Have a nice day!\n")
subject := fmt.Sprintf("GOGIOS Report [C:%d W:%d U:%d OK:%d]",
numCriticals, numWarnings, numUnknowns, numOks)
return subject, sb.String(), changed
}
|