summaryrefslogtreecommitdiff
path: root/internal/config.go
blob: 2ade8029c55f720c0d1a831b5608e4b9dfd54277 (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
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
	CheckTimeoutS    int
	CheckConcurrency int
	StaleThreshold   int      `json:"StaleThreshold,omitempty"`
	Federated        []string `json:"Federated,omitempty"` // TODO: Document this option
	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.HTMLDisable && conf.HTMLStatusFile == "" {
		conf.HTMLStatusFile = "/var/www/htdocs/buetow.org/self/gogios/index.html"
		log.Println("Set HTMLStatusFile to " + conf.HTMLStatusFile)
	}

	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
}