-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.go
114 lines (90 loc) · 2.46 KB
/
endpoints.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
114
package main
import (
"fmt"
"strconv"
"github.com/NETWAYS/check_sophos_central/api"
"github.com/NETWAYS/go-check"
)
type EndpointOverview struct {
Total int
Good []string
Suspicious []string
Bad []string
Unknown []string
}
type EndpointNames map[string]string
func CheckEndpoints(client *api.Client, endpointsToExclude []string) (o *EndpointOverview, names EndpointNames, err error) { //nolint:lll
o = &EndpointOverview{}
names = EndpointNames{}
endpoints, err := client.GetEndpoints()
if err != nil {
return
}
for _, endpoint := range endpoints {
if matches(endpoint.String(), endpointsToExclude) {
// If the endpoint matches a regex from the list
// we can skip it
continue
}
names[endpoint.ID] = endpoint.Hostname
o.Total++
switch endpoint.Health.Overall {
case "good":
o.Good = append(o.Good, endpoint.Hostname)
case "suspicious":
o.Suspicious = append(o.Suspicious, endpoint.Hostname)
case "bad":
o.Bad = append(o.Bad, endpoint.Hostname)
default:
o.Unknown = append(o.Unknown, endpoint.Hostname)
}
}
return
}
func (o *EndpointOverview) GetSummary() (s string) {
s = fmt.Sprintf("endpoints: %d good", len(o.Good))
if len(o.Bad) > 0 {
s += fmt.Sprintf(", %d bad", len(o.Bad))
}
if len(o.Suspicious) > 0 {
s += fmt.Sprintf(", %d suspicious", len(o.Suspicious))
}
if len(o.Unknown) > 0 {
s += fmt.Sprintf(", %d unknown", len(o.Unknown))
}
return
}
func (o *EndpointOverview) GetStatus() int {
// nolint: gocritic
if len(o.Bad) > 0 {
return check.Critical
} else if len(o.Suspicious) > 0 || len(o.Unknown) > 0 {
return check.Warning
}
return check.OK
}
func (o *EndpointOverview) GetOutput(limit int) (s string) {
if o.Total == 0 {
return
}
s = "\n## Endpoints\n"
if len(o.Bad) > 0 {
s += fmt.Sprintf("bad: %s\n", JoinEmphasis(o.Bad, ", ", limit))
}
if len(o.Suspicious) > 0 {
s += fmt.Sprintf("suspicious: %s\n", JoinEmphasis(o.Suspicious, ", ", limit))
}
if len(o.Unknown) > 0 {
s += fmt.Sprintf("unknown: %s\n", JoinEmphasis(o.Unknown, ", ", limit))
}
return
}
func (o *EndpointOverview) GetPerfdata() string {
return PerfdataList{
{Name: "endpoints_total", Value: strconv.Itoa(o.Total)},
{Name: "endpoints_good", Value: strconv.Itoa(len(o.Good))},
{Name: "endpoints_bad", Value: strconv.Itoa(len(o.Bad))},
{Name: "endpoints_suspicious", Value: strconv.Itoa(len(o.Suspicious))},
{Name: "endpoints_unknown", Value: strconv.Itoa(len(o.Unknown))},
}.String()
}