-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
127 lines (102 loc) · 3.04 KB
/
check.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
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"errors"
"os"
"regexp"
"strings"
"github.com/NETWAYS/check_sophos_central/api"
"github.com/NETWAYS/go-check"
"github.com/spf13/pflag"
)
// Matches a list of regular expressions against a string.
func matches(input string, regexToExclude []string) bool {
for _, regex := range regexToExclude {
re := regexp.MustCompile(regex)
if re.MatchString(input) {
return true
}
}
return false
}
type Config struct {
ClientID string
ClientSecret string
APIBaseURL string
ShowAll bool
PageSize uint32
ExcludeAlerts []string
ExcludeEndpoints []string
}
func BuildConfigFlags(fs *pflag.FlagSet) (config *Config) {
config = &Config{}
fs.StringVar(&config.ClientID, "client-id", "", "API Client ID (env:SOPHOS_CLIENT_ID)")
fs.StringVar(&config.ClientSecret, "client-secret", "", "API Client Secret (env:SOPHOS_CLIENT_SECRET)")
fs.BoolVar(&config.ShowAll, "show-all", false, "List all non-ok endpoints")
fs.Uint32Var(&config.PageSize, "page-size", api.DefaultPageSize, "Amount of objects to fetch during each API call")
fs.StringArrayVar(&config.ExcludeAlerts, "exclude-alert", []string{}, "Alerts to ignore. Can be used multiple times and supports regex.") //nolint:lll
fs.StringArrayVar(&config.ExcludeEndpoints, "exclude-endpoint", []string{}, "Endpoints to ignore. Can be used multiple times and supports regex.") //nolint:lll
fs.StringVar(&config.APIBaseURL, "api", api.DefaultURL, "API Base URL")
return
}
func (c *Config) SetFromEnv() {
if c.ClientID == "" {
c.ClientID = os.Getenv("SOPHOS_CLIENT_ID")
}
if c.ClientSecret == "" {
c.ClientSecret = os.Getenv("SOPHOS_CLIENT_SECRET")
}
}
func (c *Config) Validate() error {
if c.ClientID == "" || c.ClientSecret == "" {
return errors.New("client-id and client-secret are required")
}
return nil
}
func (c *Config) Run() (rc int, output string, err error) {
// Setup API client.
client := api.NewClient(c.ClientID, c.ClientSecret)
client.PageSize = c.PageSize
err = client.WhoAmI()
if err != nil {
return
}
// Retrieve and check endpoints.
endpoints, names, err := CheckEndpoints(client, c.ExcludeEndpoints)
if err != nil {
return
}
// Retrieve and check alerts.
alerts, err := CheckAlerts(client, names, c.ExcludeAlerts)
if err != nil {
return
}
// Build output.
limit := 5
if c.ShowAll {
limit = 0
}
output = alerts.GetSummary() + " - " + endpoints.GetSummary() + "\n"
output += alerts.GetOutput()
output += endpoints.GetOutput(limit)
// Build rc
rcAlerts := alerts.GetStatus()
rcEndpoints := endpoints.GetStatus()
// nolint: gocritic
if rcAlerts == check.Critical || rcEndpoints == check.Critical {
rc = check.Critical
} else if rcEndpoints > rcAlerts {
rc = rcEndpoints
} else {
rc = rcAlerts
}
// Add Perfdata.
output += "| " + alerts.GetPerfdata() + " " + endpoints.GetPerfdata()
return
}
func JoinEmphasis(elems []string, sep string, limit int) string {
if limit > 0 && len(elems) > limit {
elems = elems[0:limit]
elems = append(elems, "...")
}
return strings.Join(elems, sep)
}