diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d9b5a67..7a8d93d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v6 with: - go-version: 1.24 + go-version: 1.26 - name: Vet run: go vet ./... diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 6ec6004..7eb1958 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -17,4 +17,4 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v9 with: - version: v2.1.6 + version: v2.11.2 diff --git a/alerts.go b/alerts.go index f01c53d..388a0cd 100644 --- a/alerts.go +++ b/alerts.go @@ -2,11 +2,11 @@ package main import ( "fmt" - "strconv" "strings" "github.com/NETWAYS/check_sophos_central/api" "github.com/NETWAYS/go-check" + "github.com/NETWAYS/go-check/perfdata" ) type AlertOverview struct { @@ -17,7 +17,7 @@ type AlertOverview struct { Output []string } -// Retrieve and process Alerts. +// CheckAlerts retrieves and processes 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{} @@ -80,7 +80,6 @@ func (o *AlertOverview) GetSummary() string { return "alerts: " + strings.Join(states, ", ") } -// nolint: gocritic func (o *AlertOverview) GetStatus() int { if o.High > 0 { return check.Critical @@ -104,10 +103,12 @@ func (o *AlertOverview) GetOutput() (s string) { } 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() + tmp := perfdata.PerfdataList{ + {Label: "alerts", Value: o.Total}, + {Label: "alerts_high", Value: o.High}, + {Label: "alerts_medium", Value: o.Medium}, + {Label: "alerts_low", Value: o.Low}, + } + + return tmp.String() } diff --git a/api/client.go b/api/client.go index 3202cbe..1d961ee 100644 --- a/api/client.go +++ b/api/client.go @@ -76,7 +76,6 @@ func (c *Client) NewDataRequest(method, url string, body io.Reader) (req *http.R } req, err = http.NewRequestWithContext(context.Background(), method, c.DataURL+"/"+url, body) - if err != nil { err = fmt.Errorf("could not create http request: %w", err) return @@ -88,7 +87,7 @@ func (c *Client) NewDataRequest(method, url string, body io.Reader) (req *http.R } func (c *Client) Do(req *http.Request) (res *http.Response, err error) { - res, err = c.HTTPClient.Do(req) + res, err = c.HTTPClient.Do(req) //nolint:gosec if err != nil { err = fmt.Errorf("HTTP request failed: %w", err) } diff --git a/api/response.go b/api/response.go index b3e159d..3027680 100644 --- a/api/response.go +++ b/api/response.go @@ -29,6 +29,7 @@ type PageInfo struct { Total int `json:"total"` } +// GetResults fetches data from the API via HTTP // nolint: funlen func (c *Client) GetResults(request *http.Request) (items []json.RawMessage, err error) { var ( diff --git a/endpoints.go b/endpoints.go index 53e4fb7..32a304f 100644 --- a/endpoints.go +++ b/endpoints.go @@ -2,10 +2,10 @@ package main import ( "fmt" - "strconv" "github.com/NETWAYS/check_sophos_central/api" "github.com/NETWAYS/go-check" + "github.com/NETWAYS/go-check/perfdata" ) type EndpointOverview struct { @@ -104,11 +104,13 @@ func (o *EndpointOverview) GetOutput(limit int) (s string) { } 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() + tmp := perfdata.PerfdataList{ + {Label: "endpoints_total", Value: o.Total}, + {Label: "endpoints_good", Value: len(o.Good)}, + {Label: "endpoints_bad", Value: len(o.Bad)}, + {Label: "endpoints_suspicious", Value: len(o.Suspicious)}, + {Label: "endpoints_unknown", Value: len(o.Unknown)}, + } + + return tmp.String() } diff --git a/go.mod b/go.mod index 4ea0600..3f6abb1 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module github.com/NETWAYS/check_sophos_central -go 1.24.0 +go 1.25.0 require ( github.com/NETWAYS/go-check v0.6.4 github.com/jarcoal/httpmock v1.4.1 github.com/spf13/pflag v1.0.10 - golang.org/x/oauth2 v0.35.0 + golang.org/x/oauth2 v0.36.0 ) diff --git a/go.sum b/go.sum index 9e4e437..9d68e43 100644 --- a/go.sum +++ b/go.sum @@ -8,5 +8,5 @@ github.com/maxatome/go-testdeep v1.14.0 h1:rRlLv1+kI8eOI3OaBXZwb3O7xY3exRzdW5QyX github.com/maxatome/go-testdeep v1.14.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM= github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -golang.org/x/oauth2 v0.35.0 h1:Mv2mzuHuZuY2+bkyWXIHMfhNdJAdwW3FuWeCPYN5GVQ= -golang.org/x/oauth2 v0.35.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA= +golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs= +golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q= diff --git a/main_test.go b/main_test.go index 6b64dd9..05908fe 100644 --- a/main_test.go +++ b/main_test.go @@ -39,7 +39,7 @@ func TestMainAlerts_GetPerfdata(t *testing.T) { }{ "simple-overview": { ao: AlertOverview{}, - expected: "'alerts'=0 'alerts_high'=0 'alerts_medium'=0 'alerts_low'=0", + expected: "alerts=0 alerts_high=0 alerts_medium=0 alerts_low=0", }, } diff --git a/perfdata.go b/perfdata.go deleted file mode 100644 index 3a263e4..0000000 --- a/perfdata.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "fmt" - "strings" -) - -type Perfdata struct { - Name string - Value string - Warn string - Crit string - Min string - Max string -} - -type PerfdataList []Perfdata - -func (p Perfdata) String() (s string) { - s = fmt.Sprintf("'%s'=%s;%s;%s;%s;%s", p.Name, p.Value, p.Warn, p.Crit, p.Min, p.Max) - s = strings.TrimRight(s, ";") - - return -} - -func (pl PerfdataList) String() (s string) { - for _, p := range pl { - s += p.String() + " " - } - - s = strings.Trim(s, " ") - - return -}