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
|
package internal
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
)
type config struct {
EmailTo string
EmailFrom string
SMTPServer string `json:"SMTPServer,omitempty"`
SMTPDisable bool `json:"SMTPDisable,omitempty"` // TODO: Document this option
StateDir string `json:"StateDir,omitempty"`
HTMLStatusFile string `json:"HTMLStatusFile,omitempty"` // Path to HTML status file
HTMLDisable bool `json:"HTMLDisable,omitempty"` // Disable HTML status page generation
StatusPageURL string `json:"StatusPageURL,omitempty"` // URL to the HTML status page for email notifications
CheckTimeoutS int
CheckConcurrency int
StaleThreshold int `json:"StaleThreshold,omitempty"`
Federated []string `json:"Federated,omitempty"` // TODO: Document this option
PrometheusHosts []string `json:"PrometheusHosts,omitempty"`
PrometheusTimeoutS int `json:"PrometheusTimeoutS,omitempty"`
PrometheusOnlyIfNotExists string `json:"PrometheusOnlyIfNotExists,omitempty"` // Suppress Prometheus alerts if this file exists and is recent
PrometheusOnlyIfNotExistsMaxS int `json:"PrometheusOnlyIfNotExistsMaxS,omitempty"` // Max age in seconds for suppression file (default 86400)
Checks map[string]check
}
func newConfig(configFile string) (config, error) {
var conf config
file, err := os.Open(configFile)
if err != nil {
return conf, err
}
defer file.Close()
bytes, err := io.ReadAll(file)
if err != nil {
return conf, err
}
err = json.Unmarshal(bytes, &conf)
if err != nil {
return conf, err
}
if conf.SMTPServer == "" {
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
conf.SMTPServer = fmt.Sprintf("%s:25", hostname)
log.Println("Set SMTPServer to " + conf.SMTPServer)
}
if conf.StateDir == "" {
conf.StateDir = "."
log.Println("Set StateDir to " + conf.StateDir)
}
if conf.StaleThreshold == 0 {
conf.StaleThreshold = 3600 // Default to 1 hour
}
if conf.PrometheusTimeoutS == 0 {
conf.PrometheusTimeoutS = 2 // Default to 2 seconds
}
if conf.PrometheusOnlyIfNotExistsMaxS == 0 {
conf.PrometheusOnlyIfNotExistsMaxS = 86400 // Default to 24 hours
}
if !conf.HTMLDisable && conf.HTMLStatusFile == "" {
conf.HTMLStatusFile = "/var/www/htdocs/buetow.org/self/gogios/index.html"
log.Println("Set HTMLStatusFile to " + conf.HTMLStatusFile)
}
// Default URL for the status page link in email notifications
if conf.StatusPageURL == "" {
conf.StatusPageURL = "https://gogios.buetow.org"
log.Println("Set StatusPageURL to " + conf.StatusPageURL)
}
return conf, nil
}
func (conf config) sanityCheck() error {
for name, check := range conf.Checks {
for _, depName := range check.DependsOn {
if _, ok := conf.Checks[depName]; !ok {
return fmt.Errorf("check '%s' depends on non existant check '%s'", name, depName)
}
}
}
return nil
}
|