diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 7d53aa0..fb12032 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/.golangci.yml b/.golangci.yml index 821ec6f..d738f9e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -25,6 +25,7 @@ linters: - varnamelen - wrapcheck - funlen + - wsl exclusions: generated: lax presets: diff --git a/cmd/config.go b/cmd/config.go index fca3cfe..89a37ce 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -66,7 +66,6 @@ func (c *Config) NewClient() *client.Client { for _, host := range c.Hostnames { u, errParse := url.Parse(host) - if errParse != nil { check.ExitError(errParse) } @@ -81,7 +80,6 @@ func (c *Config) NewClient() *client.Client { KeyFile: c.KeyFile, CertFile: c.CertFile, }) - if err != nil { check.ExitError(err) } diff --git a/cmd/ingest.go b/cmd/ingest.go index f157ac9..09af692 100644 --- a/cmd/ingest.go +++ b/cmd/ingest.go @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -// To store the CLI parameters. +// PipelineConfig stores the CLI parameters. type PipelineConfig struct { PipelineNames []string FailedWarning string @@ -63,9 +63,7 @@ var ingestCmd = &cobra.Command{ for _, node := range stats.Nodes { for pipelineName, pp := range node.Ingest.Pipelines { - pipelineMatched, regexErr := matches(pipelineName, cliPipelineConfig.PipelineNames) - if regexErr != nil { check.ExitRaw(check.Unknown, "Invalid regular expression provided:", regexErr.Error()) } @@ -76,15 +74,19 @@ var ingestCmd = &cobra.Command{ } summary.WriteString("\n \\_") + if failedCrit.DoesViolate(pp.Failed) { states = append(states, check.Critical) - summary.WriteString(fmt.Sprintf(ingestOutput, "[CRITICAL]", pipelineName, pp.Failed)) + + fmt.Fprintf(&summary, ingestOutput, "[CRITICAL]", pipelineName, pp.Failed) } else if failedWarn.DoesViolate(pp.Failed) { states = append(states, check.Warning) - summary.WriteString(fmt.Sprintf(ingestOutput, "[WARNING]", pipelineName, pp.Failed)) + + fmt.Fprintf(&summary, ingestOutput, "[WARNING]", pipelineName, pp.Failed) } else { states = append(states, check.OK) - summary.WriteString(fmt.Sprintf(ingestOutput, "[OK]", pipelineName, pp.Failed)) + + fmt.Fprintf(&summary, ingestOutput, "[OK]", pipelineName, pp.Failed) } perfList.Add(&perfdata.Perfdata{ @@ -142,7 +144,6 @@ func init() { func matches(input string, regexToMatch []string) (bool, error) { for _, regex := range regexToMatch { re, err := regexp.Compile(regex) - if err != nil { return false, err } diff --git a/cmd/query.go b/cmd/query.go index 23e0c92..3277c72 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -44,19 +44,20 @@ https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query- cliQueryConfig.Index, cliQueryConfig.Query, cliQueryConfig.MessageKey) - if err != nil { check.ExitError(err) } - output.WriteString(fmt.Sprintf("Search query hits: %d", total)) + fmt.Fprintf(&output, "Search query hits: %d", total) if len(messages) > 0 { output.WriteString("\n") + for _, msg := range messages { if len(msg) > cliQueryConfig.MessageLen { msg = msg[0:cliQueryConfig.MessageLen] } + output.WriteString(msg + "\n") } } diff --git a/cmd/root.go b/cmd/root.go index c0fa434..63888e0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,7 +26,8 @@ func Execute(version string) { rootCmd.Version = version rootCmd.VersionTemplate() - if err := rootCmd.Execute(); err != nil { + err := rootCmd.Execute() + if err != nil { check.ExitError(err) } } diff --git a/cmd/snapshot.go b/cmd/snapshot.go index f4767d2..badb9ff 100644 --- a/cmd/snapshot.go +++ b/cmd/snapshot.go @@ -55,7 +55,6 @@ $ check_elasticsearch snapshot --number 5 client := cliConfig.NewClient() snapResponse, err := client.Snapshot(repository, snapshot) - if err != nil { check.ExitError(err) } @@ -77,25 +76,29 @@ $ check_elasticsearch snapshot --number 5 var summary strings.Builder for _, snap := range snapResponse.Snapshots[:numberOfSnapshots] { - summary.WriteString("\n \\_") switch snap.State { default: sStates = append(sStates, check.Unknown) - summary.WriteString(fmt.Sprintf("[UNKNOWN] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository)) + + fmt.Fprintf(&summary, "[UNKNOWN] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository) case "SUCCESS": sStates = append(sStates, check.OK) - summary.WriteString(fmt.Sprintf("[OK] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository)) + + fmt.Fprintf(&summary, "[OK] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository) case "PARTIAL": sStates = append(sStates, check.Warning) - summary.WriteString(fmt.Sprintf("[WARNING] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository)) + + fmt.Fprintf(&summary, "[WARNING] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository) case "FAILED": sStates = append(sStates, check.Critical) - summary.WriteString(fmt.Sprintf("[CRITICAL] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository)) + + fmt.Fprintf(&summary, "[CRITICAL] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository) case "IN PROGRESS": sStates = append(sStates, check.Unknown) - summary.WriteString(fmt.Sprintf("[UNKNOWN] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository)) + + fmt.Fprintf(&summary, "[UNKNOWN] Snapshot: %s, State %s, Repository: %s", snap.Snapshot, snap.State, snap.Repository) } } diff --git a/internal/client/client.go b/internal/client/client.go index a9d81af..1504325 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -40,8 +40,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) { req.URL, _ = url.Parse(u) - resp, errDo := c.Client.Do(req) - + resp, errDo := c.Client.Do(req) //nolint: gosec if errDo != nil { // If there's an error we try the next host continue @@ -69,7 +68,6 @@ func (c *Client) Health() (*es.HealthResponse, error) { } resp, err := c.Perform(req) - if err != nil { return r, fmt.Errorf("could not fetch cluster health: %s", err.Error()) } @@ -81,7 +79,6 @@ func (c *Client) Health() (*es.HealthResponse, error) { defer resp.Body.Close() err = json.NewDecoder(resp.Body).Decode(r) - if err != nil { return r, fmt.Errorf("error parsing the response body: %w", err) } @@ -131,7 +128,6 @@ func (c *Client) SearchMessages(index string, query string, messageKey string) ( req.URL.RawQuery = p.Encode() resp, err := c.Perform(req) - if err != nil { return total, messages, fmt.Errorf("could not execute search request: %s", err.Error()) } @@ -139,8 +135,8 @@ func (c *Client) SearchMessages(index string, query string, messageKey string) ( var response es.SearchResponse defer resp.Body.Close() - err = json.NewDecoder(resp.Body).Decode(&response) + err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { return total, messages, fmt.Errorf("error parsing the response body: %w", err) } @@ -170,7 +166,7 @@ func (c *Client) SearchMessages(index string, query string, messageKey string) ( return total, messages, nil } -// NodeStates retrieves the Cluster's node state +// NodeStats retrieves the Cluster's node statistics func (c *Client) NodeStats() (*es.ClusterStats, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -186,7 +182,6 @@ func (c *Client) NodeStats() (*es.ClusterStats, error) { } resp, err := c.Perform(req) - if err != nil { return r, fmt.Errorf("could not fetch cluster nodes statistics: %s", err.Error()) } @@ -196,8 +191,8 @@ func (c *Client) NodeStats() (*es.ClusterStats, error) { } defer resp.Body.Close() - err = json.NewDecoder(resp.Body).Decode(r) + err = json.NewDecoder(resp.Body).Decode(r) if err != nil { return r, fmt.Errorf("error parsing the response body: %w", err) } @@ -216,13 +211,11 @@ func (c *Client) Snapshot(repository string, snapshot string) (*es.SnapshotRespo // Retrieve snapshots in descending order to get latest req, err := http.NewRequestWithContext(ctx, http.MethodGet, u+"?order=desc", nil) - if err != nil { return r, fmt.Errorf("error creating request: %w", err) } resp, err := c.Perform(req) - if err != nil { return r, fmt.Errorf("could not fetch snapshots: %s", err.Error()) } @@ -234,7 +227,6 @@ func (c *Client) Snapshot(repository string, snapshot string) (*es.SnapshotRespo defer resp.Body.Close() err = json.NewDecoder(resp.Body).Decode(r) - if err != nil { return r, fmt.Errorf("could not decode snapshot response: %w", err) } diff --git a/internal/elasticsearch/api.go b/internal/elasticsearch/api.go index 7619f7e..6f325dd 100644 --- a/internal/elasticsearch/api.go +++ b/internal/elasticsearch/api.go @@ -23,6 +23,7 @@ type HealthResponse struct { ActiveShardsPercentAsNumber float64 `json:"active_shards_percent_as_number"` } +// SearchResponse represents the answer to an elastic search query // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-api-response-body type SearchResponse struct { Hits SearchHits `json:"hits"` @@ -78,11 +79,13 @@ type SearchRequest struct { Query Query `json:"query"` } +// Query represents a query against elastic search // https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html type Query struct { QueryString *QueryString `json:"query_string,omitempty"` } +// QueryString, what the name says // https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html type QueryString struct { Query string `json:"query"`