Skip to content

Commit f22a44e

Browse files
authored
feat: add cmdline option to set returned state if no snapshots are found (#62)
* feat: add cmdline option to set returned state if no snapshots are found
1 parent a9b94d2 commit f22a44e

File tree

3 files changed

+114
-18
lines changed

3 files changed

+114
-18
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ Usage:
149149
check_elasticsearch snapshot [flags]
150150
151151
Flags:
152-
-a, --all Check all retrieved snapshots. If not set only the latest snapshot is checked
153-
-N, --number int Check latest N number snapshots. If not set only the latest snapshot is checked (default 1)
154-
-r, --repository string Comma-separated list of snapshot repository names used to limit the request (default "*")
155-
-s, --snapshot string Comma-separated list of snapshot names to retrieve. Wildcard (*) expressions are supported (default "*")
156-
-h, --help help for snapshot
152+
-a, --all Check all retrieved snapshots. If not set only the latest snapshot is checked
153+
-N, --number int Check latest N number snapshots. If not set only the latest snapshot is checked (default 1)
154+
-r, --repository string Comma-separated list of snapshot repository names used to limit the request (default "*")
155+
-s, --snapshot string Comma-separated list of snapshot names to retrieve. Wildcard (*) expressions are supported (default "*")
156+
-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")
157+
-h, --help help for snapshot
157158
```
158159

159160
Examples:

cmd/snapshot.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"strings"
66

7+
"errors"
8+
79
"github.com/NETWAYS/go-check"
810
"github.com/NETWAYS/go-check/result"
911
"github.com/spf13/cobra"
@@ -37,6 +39,13 @@ $ check_elasticsearch snapshot --number 5
3739
snapshot, _ := cmd.Flags().GetString("snapshot")
3840
numberOfSnapshots, _ := cmd.Flags().GetInt("number")
3941
evalAllSnapshots, _ := cmd.Flags().GetBool("all")
42+
noSnapshotsState, _ := cmd.Flags().GetString("no-snapshots-state")
43+
44+
// Convert --no-snapshots-state to integer and validate input
45+
noSnapshotsStateInt, err := convertStateToInt(noSnapshotsState)
46+
if err != nil {
47+
check.ExitError(fmt.Errorf("invalid value for --no-snapshots-state: %s", noSnapshotsState))
48+
}
4049

4150
var (
4251
rc int
@@ -61,7 +70,7 @@ $ check_elasticsearch snapshot --number 5
6170
numberOfSnapshots = len(snapResponse.Snapshots)
6271
}
6372

64-
// Evaluate snashots given their states
73+
// Evaluate snapshots given their states
6574
sStates := make([]int, 0, len(snapResponse.Snapshots))
6675

6776
// Check status for each snapshot
@@ -90,25 +99,59 @@ $ check_elasticsearch snapshot --number 5
9099
}
91100
}
92101

102+
if len(snapResponse.Snapshots) == 0 {
103+
switch noSnapshotsStateInt {
104+
case 0:
105+
sStates = append(sStates, check.OK)
106+
case 1:
107+
sStates = append(sStates, check.Warning)
108+
case 2:
109+
sStates = append(sStates, check.Critical)
110+
case 3:
111+
sStates = append(sStates, check.Unknown)
112+
}
113+
}
114+
93115
rc = result.WorstState(sStates...)
94116

95-
switch rc {
96-
case check.OK:
97-
output = "All evaluated snapshots are in state SUCCESS."
98-
case check.Warning:
99-
output = "At least one evaluated snapshot is in state PARTIAL."
100-
case check.Critical:
101-
output = "At least one evaluated snapshot is in state FAILED."
102-
case check.Unknown:
103-
output = "At least one evaluated snapshot is in state IN_PROGRESS."
104-
default:
105-
output = "Could not evaluate status of snapshots"
117+
if len(snapResponse.Snapshots) == 0 {
118+
output = "No snapshots found."
119+
} else {
120+
switch rc {
121+
case check.OK:
122+
output = "All evaluated snapshots are in state SUCCESS."
123+
case check.Warning:
124+
output = "At least one evaluated snapshot is in state PARTIAL."
125+
case check.Critical:
126+
output = "At least one evaluated snapshot is in state FAILED."
127+
case check.Unknown:
128+
output = "At least one evaluated snapshot is in state IN_PROGRESS."
129+
default:
130+
output = "Could not evaluate status of snapshots"
131+
}
106132
}
107133

108134
check.ExitRaw(rc, output, "repository:", repository, "snapshot:", snapshot, summary.String())
109135
},
110136
}
111137

138+
// Function to convert state to integer.
139+
func convertStateToInt(state string) (int, error) {
140+
state = strings.ToUpper(state)
141+
switch state {
142+
case "OK", "0":
143+
return 0, nil
144+
case "WARNING", "1":
145+
return 1, nil
146+
case "CRITICAL", "2":
147+
return 2, nil
148+
case "UNKNOWN", "3":
149+
return 3, nil
150+
default:
151+
return 0, errors.New("invalid state")
152+
}
153+
}
154+
112155
func init() {
113156
rootCmd.AddCommand(snapshotCmd)
114157

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

168+
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")
169+
125170
snapshotCmd.MarkFlagsMutuallyExclusive("number", "all")
126171
}

cmd/snapshot_test.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type SnapshotTest struct {
4545
func TestSnapshotCmd(t *testing.T) {
4646
tests := []SnapshotTest{
4747
{
48-
name: "no-snapshot",
48+
name: "snapshot-invalid-response",
4949
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5050
w.Header().Set("X-Elastic-Product", "Elasticsearch")
5151
w.WriteHeader(http.StatusOK)
@@ -54,6 +54,56 @@ func TestSnapshotCmd(t *testing.T) {
5454
args: []string{"run", "../main.go", "snapshot"},
5555
expected: "[UNKNOWN] - could not decode snapshot response",
5656
},
57+
{
58+
name: "snapshot-none-available-ok",
59+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
60+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
61+
w.WriteHeader(http.StatusOK)
62+
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
63+
})),
64+
args: []string{"run", "../main.go", "snapshot", "--no-snapshots-state", "OK"},
65+
expected: "[OK] - No snapshots found",
66+
},
67+
{
68+
name: "snapshot-none-available-warning",
69+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
70+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
71+
w.WriteHeader(http.StatusOK)
72+
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
73+
})),
74+
args: []string{"run", "../main.go", "snapshot", "--no-snapshots-state", "WARNING"},
75+
expected: "[WARNING] - No snapshots found",
76+
},
77+
{
78+
name: "snapshot-none-available-critical",
79+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
80+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
81+
w.WriteHeader(http.StatusOK)
82+
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
83+
})),
84+
args: []string{"run", "../main.go", "snapshot", "--no-snapshots-state", "CRITICAL"},
85+
expected: "[CRITICAL] - No snapshots found",
86+
},
87+
{
88+
name: "snapshot-none-available-unknown",
89+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
90+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
91+
w.WriteHeader(http.StatusOK)
92+
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
93+
})),
94+
args: []string{"run", "../main.go", "snapshot", "--no-snapshots-state", "UNKNOWN"},
95+
expected: "[UNKNOWN] - No snapshots found",
96+
},
97+
{
98+
name: "snapshot-none-available-default",
99+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
100+
w.Header().Set("X-Elastic-Product", "Elasticsearch")
101+
w.WriteHeader(http.StatusOK)
102+
w.Write([]byte(`{"snapshots":[],"total":0,"remaining":0}`))
103+
})),
104+
args: []string{"run", "../main.go", "snapshot"},
105+
expected: "[UNKNOWN] - No snapshots found",
106+
},
57107
{
58108
name: "snapshot-ok",
59109
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)