summaryrefslogtreecommitdiff
path: root/internal/run.go
blob: 21b9b6dade255aff45c73e4e80f7380e0727bdf3 (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
package internal

import (
	"context"
	"fmt"
	"log"
	"os"
)

func Run(ctx context.Context, configFile string, renotify, force bool) error {
	conf, err := newConfig(configFile)
	if err != nil {
		return err
	}

	if err := conf.sanityCheck(); err != nil {
		notifyError(conf, err)
	}

	state, err := newState(conf)
	if err != nil {
		notifyError(conf, err)
	}

	// Load notification state for batching (tracks when last email was sent
	// and what the check states were at that time)
	notifyStateData, err := newNotifyState(conf.StateDir)
	if err != nil {
		log.Println("warning: failed to load notification state:", err)
	}

	state = runChecks(ctx, state, conf)
	state = mergePrometheusAlerts(ctx, state, conf)
	state = mergeFederated(ctx, state, conf)

	if err := state.persist(); err != nil {
		notifyError(conf, err)
	}

	subject, body, doNotify := state.report(renotify, force, conf.StatusPageURL, conf)

	// Apply notification batching when MinNotifyIntervalS is configured.
	// Force flag bypasses batching to allow immediate notifications when needed.
	if doNotify && conf.MinNotifyIntervalS > 0 && !force {
		if notifyStateData.intervalElapsed(conf.MinNotifyIntervalS) {
			// Interval has elapsed - only notify if state changed since last notification
			if !notifyStateData.hasChanges(state) {
				doNotify = false
				log.Println("Notification suppressed: interval elapsed but no state changes since last notification")
			}
		} else {
			// Interval has not elapsed - suppress notification
			doNotify = false
			log.Println("Notification suppressed: minimum interval not elapsed")
		}
	}

	if doNotify {
		if err := notify(conf, subject, body); err != nil {
			log.Println("error:", err)
			return nil
		}
		// Record notification timestamp and state snapshot for batching
		if err := notifyStateData.recordNotification(state); err != nil {
			log.Println("warning: failed to save notification state:", err)
		}
	}

	// Text and HTML reports always update regardless of notification batching
	if err := persistReport(subject, body, conf); err != nil {
		notifyError(conf, err)
	}

	// Generate HTML status page (unless disabled)
	if !conf.HTMLDisable {
		if err := persistHTMLReport(state, subject, conf); err != nil {
			notifyError(conf, err)
		}
		if err := persistJSONReport(state, subject, conf); err != nil {
			notifyError(conf, err)
		}
	}

	return nil
}

func persistReport(subject, body string, conf config) error {
	reportFile := fmt.Sprintf("%s/report.txt", conf.StateDir)
	tmpFile := fmt.Sprintf("%s.tmp", reportFile)

	f, err := os.Create(tmpFile)
	if err != nil {
		return err
	}
	defer f.Close()

	if _, err = f.WriteString(fmt.Sprintf("%s\n\n", subject)); err != nil {
		return err
	}
	if _, err = f.WriteString(body); err != nil {
		return err
	}
	return os.Rename(tmpFile, reportFile)
}