summaryrefslogtreecommitdiff
path: root/internal/suppress.go
blob: a270656b6b163b1ceb7df043e244b91a54ac8347 (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
package internal

import (
	"log"
	"os"
	"strings"
	"time"
)

// isSuppressed checks if alerts should be suppressed based on a file's existence and age.
// Returns true if the file exists AND its modification time is within maxAgeS seconds of now.
// Returns false if filePath is empty, file doesn't exist, or file is too old.
func isSuppressed(filePath string, maxAgeS int) bool {
	if filePath == "" {
		return false
	}
	info, err := os.Stat(filePath)
	if err != nil {
		return false // file doesn't exist or other error
	}
	age := time.Since(info.ModTime())
	return age <= time.Duration(maxAgeS)*time.Second
}

// isCheckSuppressed determines if a check should be suppressed from email reports.
// For Prometheus checks (name starts with "Prometheus"): uses PrometheusOnlyIfNotExists config.
// For regular checks: uses per-check OnlyIfNotExists config if set.
func isCheckSuppressed(name string, conf config) bool {
	// Check if this is a Prometheus alert (name starts with "Prometheus")
	if strings.HasPrefix(name, "Prometheus") {
		if isSuppressed(conf.PrometheusOnlyIfNotExists, conf.PrometheusOnlyIfNotExistsMaxS) {
			log.Printf("Suppressing %s: file %s exists and is recent", name, conf.PrometheusOnlyIfNotExists)
			return true
		}
		return false
	}

	// For regular checks, look up the check config
	chk, ok := conf.Checks[name]
	if !ok {
		return false // check not found in config (e.g., federated)
	}

	if chk.OnlyIfNotExists == "" {
		return false
	}

	// Use per-check max age if set, otherwise use global Prometheus default
	maxAgeS := chk.OnlyIfNotExistsMaxS
	if maxAgeS == 0 {
		maxAgeS = conf.PrometheusOnlyIfNotExistsMaxS
	}

	if isSuppressed(chk.OnlyIfNotExists, maxAgeS) {
		log.Printf("Suppressing %s: file %s exists and is recent", name, chk.OnlyIfNotExists)
		return true
	}
	return false
}