-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.go
113 lines (91 loc) · 2.19 KB
/
alerts.go
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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/NETWAYS/check_sophos_central/api"
"github.com/NETWAYS/go-check"
)
type AlertOverview struct {
Total int
High int
Medium int
Low int
Output []string
}
// Retrieve and process Alerts.
// alertsToExclude is a list of strings that can the used to exclude alerts.
func CheckAlerts(client *api.Client, names EndpointNames, alertsToExclude []string) (o *AlertOverview, err error) {
o = &AlertOverview{}
alerts, err := client.GetAlerts()
if err != nil {
return
}
for _, alert := range alerts {
if matches(alert.String(), alertsToExclude) {
// If the alert matches a regex from the list we can skip it.
continue
}
o.Total++
switch strings.ToLower(alert.Severity) {
case "high":
o.High++
case "medium":
o.Medium++
default:
o.Low++
}
agentName := alert.ManagedAgent.ID
if val, ok := names[agentName]; ok {
agentName = val
}
output := fmt.Sprintf("%s [%s] %s (%s) %s",
//nolint: gosmopolitan
alert.RaisedAt.Local().Format("2006-01-02 15:04 MST"),
alert.Severity, agentName, alert.Product, alert.Description)
o.Output = append(o.Output, output)
}
return
}
func (o *AlertOverview) GetSummary() string {
if o.Total == 0 {
return "no alerts"
}
var states []string
if o.High > 0 {
states = append(states, fmt.Sprintf("%d high", o.High))
}
if o.Medium > 0 {
states = append(states, fmt.Sprintf("%d medium", o.Medium))
}
if o.Low > 0 {
states = append(states, fmt.Sprintf("%d low", o.Low))
}
return "alerts: " + strings.Join(states, ", ")
}
// nolint: gocritic
func (o *AlertOverview) GetStatus() int {
if o.High > 0 {
return check.Critical
} else if o.Medium > 0 || o.Low > 0 {
return check.Warning
}
return check.OK
}
func (o *AlertOverview) GetOutput() (s string) {
if o.Total == 0 {
return
}
s = "\n## Alerts\n"
s += strings.Join(o.Output, "\n")
s += "\n"
return
}
func (o *AlertOverview) GetPerfdata() string {
return PerfdataList{
{Name: "alerts", Value: strconv.Itoa(o.Total)},
{Name: "alerts_high", Value: strconv.Itoa(o.High)},
{Name: "alerts_medium", Value: strconv.Itoa(o.Medium)},
{Name: "alerts_low", Value: strconv.Itoa(o.Low)},
}.String()
}