Skip to content

Commit

Permalink
feat: add cmdline option to set returned state if no snapshots are fo…
Browse files Browse the repository at this point in the history
…und (#62)

* feat: add cmdline option to set returned state if no snapshots are found
  • Loading branch information
sczech authored Oct 21, 2024
1 parent a9b94d2 commit f22a44e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 18 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ Usage:
check_elasticsearch snapshot [flags]
Flags:
-a, --all Check all retrieved snapshots. If not set only the latest snapshot is checked
-N, --number int Check latest N number snapshots. If not set only the latest snapshot is checked (default 1)
-r, --repository string Comma-separated list of snapshot repository names used to limit the request (default "*")
-s, --snapshot string Comma-separated list of snapshot names to retrieve. Wildcard (*) expressions are supported (default "*")
-h, --help help for snapshot
-a, --all Check all retrieved snapshots. If not set only the latest snapshot is checked
-N, --number int Check latest N number snapshots. If not set only the latest snapshot is checked (default 1)
-r, --repository string Comma-separated list of snapshot repository names used to limit the request (default "*")
-s, --snapshot string Comma-separated list of snapshot names to retrieve. Wildcard (*) expressions are supported (default "*")
-T, --no-snapshots-state string Set exit code to return if no snapshots are found. Supported values are 0, 1, 2, 3, OK, Warning, Critical, Unknown (case-insensitive - default "Unknown")
-h, --help help for snapshot
```

Examples:
Expand Down
69 changes: 57 additions & 12 deletions cmd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

"errors"

"github.com/NETWAYS/go-check"
"github.com/NETWAYS/go-check/result"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,6 +39,13 @@ $ check_elasticsearch snapshot --number 5
snapshot, _ := cmd.Flags().GetString("snapshot")
numberOfSnapshots, _ := cmd.Flags().GetInt("number")
evalAllSnapshots, _ := cmd.Flags().GetBool("all")
noSnapshotsState, _ := cmd.Flags().GetString("no-snapshots-state")

// Convert --no-snapshots-state to integer and validate input
noSnapshotsStateInt, err := convertStateToInt(noSnapshotsState)
if err != nil {
check.ExitError(fmt.Errorf("invalid value for --no-snapshots-state: %s", noSnapshotsState))
}

var (
rc int
Expand All @@ -61,7 +70,7 @@ $ check_elasticsearch snapshot --number 5
numberOfSnapshots = len(snapResponse.Snapshots)
}

// Evaluate snashots given their states
// Evaluate snapshots given their states
sStates := make([]int, 0, len(snapResponse.Snapshots))

// Check status for each snapshot
Expand Down Expand Up @@ -90,25 +99,59 @@ $ check_elasticsearch snapshot --number 5
}
}

if len(snapResponse.Snapshots) == 0 {
switch noSnapshotsStateInt {
case 0:
sStates = append(sStates, check.OK)
case 1:
sStates = append(sStates, check.Warning)
case 2:
sStates = append(sStates, check.Critical)
case 3:
sStates = append(sStates, check.Unknown)
}
}

rc = result.WorstState(sStates...)

switch rc {
case check.OK:
output = "All evaluated snapshots are in state SUCCESS."
case check.Warning:
output = "At least one evaluated snapshot is in state PARTIAL."
case check.Critical:
output = "At least one evaluated snapshot is in state FAILED."
case check.Unknown:
output = "At least one evaluated snapshot is in state IN_PROGRESS."
default:
output = "Could not evaluate status of snapshots"
if len(snapResponse.Snapshots) == 0 {
output = "No snapshots found."
} else {
switch rc {
case check.OK:
output = "All evaluated snapshots are in state SUCCESS."
case check.Warning:
output = "At least one evaluated snapshot is in state PARTIAL."
case check.Critical:
output = "At least one evaluated snapshot is in state FAILED."
case check.Unknown:
output = "At least one evaluated snapshot is in state IN_PROGRESS."
default:
output = "Could not evaluate status of snapshots"
}
}

check.ExitRaw(rc, output, "repository:", repository, "snapshot:", snapshot, summary.String())
},
}

// Function to convert state to integer.
func convertStateToInt(state string) (int, error) {
state = strings.ToUpper(state)
switch state {
case "OK", "0":
return 0, nil
case "WARNING", "1":
return 1, nil
case "CRITICAL", "2":
return 2, nil
case "UNKNOWN", "3":
return 3, nil
default:
return 0, errors.New("invalid state")
}
}

func init() {
rootCmd.AddCommand(snapshotCmd)

Expand All @@ -122,5 +165,7 @@ func init() {
fs.IntP("number", "N", 1, "Check latest N number snapshots. If not set only the latest snapshot is checked")
fs.BoolP("all", "a", false, "Check all retrieved snapshots. If not set only the latest snapshot is checked")

fs.StringP("no-snapshots-state", "T", "UNKNOWN", "State to assign when no snapshots are found (0, 1, 2, 3, OK, WARNING, CRITICAL, UNKNOWN). If not set this defaults to UNKNOWN")

snapshotCmd.MarkFlagsMutuallyExclusive("number", "all")
}
52 changes: 51 additions & 1 deletion cmd/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type SnapshotTest struct {
func TestSnapshotCmd(t *testing.T) {
tests := []SnapshotTest{
{
name: "no-snapshot",
name: "snapshot-invalid-response",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Elastic-Product", "Elasticsearch")
w.WriteHeader(http.StatusOK)
Expand All @@ -54,6 +54,56 @@ func TestSnapshotCmd(t *testing.T) {
args: []string{"run", "../main.go", "snapshot"},
expected: "[UNKNOWN] - could not decode snapshot response",
},
{
name: "snapshot-none-available-ok",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Elastic-Product", "Elasticsearch")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
})),
args: []string{"run", "../main.go", "snapshot", "--no-snapshots-state", "OK"},
expected: "[OK] - No snapshots found",
},
{
name: "snapshot-none-available-warning",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Elastic-Product", "Elasticsearch")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
})),
args: []string{"run", "../main.go", "snapshot", "--no-snapshots-state", "WARNING"},
expected: "[WARNING] - No snapshots found",
},
{
name: "snapshot-none-available-critical",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Elastic-Product", "Elasticsearch")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
})),
args: []string{"run", "../main.go", "snapshot", "--no-snapshots-state", "CRITICAL"},
expected: "[CRITICAL] - No snapshots found",
},
{
name: "snapshot-none-available-unknown",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Elastic-Product", "Elasticsearch")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
})),
args: []string{"run", "../main.go", "snapshot", "--no-snapshots-state", "UNKNOWN"},
expected: "[UNKNOWN] - No snapshots found",
},
{
name: "snapshot-none-available-default",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Elastic-Product", "Elasticsearch")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
})),
args: []string{"run", "../main.go", "snapshot"},
expected: "[UNKNOWN] - No snapshots found",
},
{
name: "snapshot-ok",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit f22a44e

Please sign in to comment.