Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.1.6
version: v2.11.2
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ linters:
- varnamelen
- wrapcheck
- funlen
- wsl
exclusions:
generated: lax
presets:
Expand Down
2 changes: 0 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -81,7 +80,6 @@ func (c *Config) NewClient() *client.Client {
KeyFile: c.KeyFile,
CertFile: c.CertFile,
})

if err != nil {
check.ExitError(err)
}
Expand Down
15 changes: 8 additions & 7 deletions cmd/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand All @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
17 changes: 10 additions & 7 deletions cmd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ $ check_elasticsearch snapshot --number 5
client := cliConfig.NewClient()

snapResponse, err := client.Snapshot(repository, snapshot)

if err != nil {
check.ExitError(err)
}
Expand All @@ -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)
}
}

Expand Down
16 changes: 4 additions & 12 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -131,16 +128,15 @@ 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())
}

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)
}
Expand Down Expand Up @@ -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()
Expand All @@ -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())
}
Expand All @@ -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)
}
Expand All @@ -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())
}
Expand All @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/elasticsearch/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down