Skip to content

Commit 821fd65

Browse files
authored
Merge pull request #84 from NETWAYS/fix/alert-panic
Fix conditional for subcheck perfdata
2 parents a4017c7 + e11c155 commit 821fd65

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

cmd/alert.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,20 @@ inactive = 0`,
8787

8888
// If there are no rules we can exit early
8989
if len(rules) == 0 {
90+
// Just an empty PerfdataList to have consistent perfdata output
91+
pdlist := perfdata.PerfdataList{
92+
{Label: "total", Value: 0},
93+
{Label: "firing", Value: 0},
94+
{Label: "pending", Value: 0},
95+
{Label: "inactive", Value: 0},
96+
}
97+
9098
// Since the user is expecting the state of a certain alert and
9199
// it that is not present it might be noteworthy.
92100
if cliAlertConfig.AlertName != nil {
93-
check.ExitRaw(check.Unknown, "No such alert defined")
101+
check.ExitRaw(check.Unknown, "No such alert defined", "|", pdlist.String())
94102
}
95-
check.ExitRaw(noAlertsState, "No alerts defined")
103+
check.ExitRaw(noAlertsState, "No alerts defined", "|", pdlist.String())
96104
}
97105

98106
// Set initial capacity to reduce memory allocations
@@ -173,26 +181,23 @@ inactive = 0`,
173181
}
174182

175183
counterAlert := counterFiring + counterPending + counterInactive
176-
if len(cliAlertConfig.AlertName) > 1 || counterAlert > 1 {
177-
perfList := perfdata.PerfdataList{
178-
{Label: "total", Value: counterAlert},
179-
{Label: "firing", Value: counterFiring},
180-
{Label: "pending", Value: counterPending},
181-
{Label: "inactive", Value: counterInactive},
182-
}
183184

184-
overall.PartialResults[0].Perfdata = append(overall.PartialResults[0].Perfdata, perfList...)
185+
perfList := perfdata.PerfdataList{
186+
{Label: "total", Value: counterAlert},
187+
{Label: "firing", Value: counterFiring},
188+
{Label: "pending", Value: counterPending},
189+
{Label: "inactive", Value: counterInactive},
185190
}
186191

187-
if len(cliAlertConfig.AlertName) == 1 && counterAlert == 1 {
188-
perfList := perfdata.PerfdataList{
189-
{Label: "firing", Value: counterFiring},
190-
{Label: "pending", Value: counterPending},
191-
{Label: "inactive", Value: counterInactive},
192-
}
193-
overall.PartialResults[0].Perfdata = append(overall.PartialResults[0].Perfdata, perfList...)
192+
// When there are no alerts we add an empty PartialResult just to have consistent output
193+
if len(overall.PartialResults) == 0 {
194+
sc := result.NewPartialResult()
195+
sc.Output = "No alerts retrieved"
196+
overall.AddSubcheck(sc)
194197
}
195198

199+
overall.PartialResults[0].Perfdata = append(overall.PartialResults[0].Perfdata, perfList...)
200+
196201
overall.Summary = fmt.Sprintf("%d Alerts: %d Firing - %d Pending - %d Inactive",
197202
counterAlert,
198203
counterFiring,

cmd/alert_test.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestAlertCmd(t *testing.T) {
4747
w.Write([]byte(`{"status":"success","data":{"groups":[]}}`))
4848
})),
4949
args: []string{"run", "../main.go", "alert"},
50-
expected: "[OK] - No alerts defined\n",
50+
expected: "[OK] - No alerts defined | total=0 firing=0 pending=0 inactive=0\n",
5151
},
5252
{
5353
name: "alert-none-with-problems",
@@ -56,7 +56,7 @@ func TestAlertCmd(t *testing.T) {
5656
w.Write([]byte(`{"status":"success","data":{"groups":[]}}`))
5757
})),
5858
args: []string{"run", "../main.go", "alert", "--problems"},
59-
expected: "[OK] - No alerts defined\n",
59+
expected: "[OK] - No alerts defined | total=0 firing=0 pending=0 inactive=0\n",
6060
},
6161
{
6262
name: "alert-none-with-no-state",
@@ -65,7 +65,7 @@ func TestAlertCmd(t *testing.T) {
6565
w.Write([]byte(`{"status":"success","data":{"groups":[]}}`))
6666
})),
6767
args: []string{"run", "../main.go", "alert", "--no-alerts-state", "3"},
68-
expected: "[UNKNOWN] - No alerts defined\nexit status 3\n",
68+
expected: "[UNKNOWN] - No alerts defined | total=0 firing=0 pending=0 inactive=0\nexit status 3\n",
6969
},
7070
{
7171
name: "alert-none-with-name",
@@ -74,7 +74,7 @@ func TestAlertCmd(t *testing.T) {
7474
w.Write([]byte(`{"status":"success","data":{"groups":[]}}`))
7575
})),
7676
args: []string{"run", "../main.go", "alert", "--name", "MyPreciousAlert"},
77-
expected: "[UNKNOWN] - No such alert defined\nexit status 3\n",
77+
expected: "[UNKNOWN] - No such alert defined | total=0 firing=0 pending=0 inactive=0\nexit status 3\n",
7878
},
7979
{
8080
name: "alert-default",
@@ -116,6 +116,7 @@ exit status 2
116116
args: []string{"run", "../main.go", "alert", "--problems", "-g", "TLS"},
117117
expected: `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
118118
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
119+
|total=1 firing=1 pending=0 inactive=0
119120
120121
exit status 2
121122
`,
@@ -144,6 +145,7 @@ exit status 2
144145
args: []string{"run", "../main.go", "alert", "--problems", "--exclude-alert", "Sql.*DeniedRate"},
145146
expected: `[CRITICAL] - 1 Alerts: 1 Firing - 0 Pending - 0 Inactive
146147
\_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00
148+
|total=1 firing=1 pending=0 inactive=0
147149
148150
exit status 2
149151
`,
@@ -163,17 +165,27 @@ exit status 2
163165
w.WriteHeader(http.StatusOK)
164166
w.Write(loadTestdata(alertTestDataSet2))
165167
})),
166-
args: []string{"run", "../main.go", "alert", "--name", "NoSuchAlert"},
167-
expected: "[UNKNOWN] - 0 Alerts: 0 Firing - 0 Pending - 0 Inactive\n\nexit status 3\n",
168+
args: []string{"run", "../main.go", "alert", "--name", "NoSuchAlert"},
169+
expected: `[UNKNOWN] - 0 Alerts: 0 Firing - 0 Pending - 0 Inactive
170+
\_ [UNKNOWN] No alerts retrieved
171+
|total=0 firing=0 pending=0 inactive=0
172+
173+
exit status 3
174+
`,
168175
},
169176
{
170177
name: "alert-inactive-with-problems",
171178
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
172179
w.WriteHeader(http.StatusOK)
173180
w.Write(loadTestdata(alertTestDataSet2))
174181
})),
175-
args: []string{"run", "../main.go", "alert", "--name", "InactiveAlert", "--problems"},
176-
expected: "[UNKNOWN] - 0 Alerts: 0 Firing - 0 Pending - 0 Inactive\n\nexit status 3\n",
182+
args: []string{"run", "../main.go", "alert", "--name", "InactiveAlert", "--problems"},
183+
expected: `[UNKNOWN] - 0 Alerts: 0 Firing - 0 Pending - 0 Inactive
184+
\_ [UNKNOWN] No alerts retrieved
185+
|total=0 firing=0 pending=0 inactive=0
186+
187+
exit status 3
188+
`,
177189
},
178190
{
179191
name: "alert-multiple-alerts",
@@ -211,7 +223,7 @@ exit status 2
211223
w.Write(loadTestdata(alertTestDataSet2))
212224
})),
213225
args: []string{"run", "../main.go", "alert", "--name", "InactiveAlert"},
214-
expected: "[OK] - 1 Alerts: 0 Firing - 0 Pending - 1 Inactive\n\\_ [OK] [InactiveAlert] is inactive\n|firing=0 pending=0 inactive=1\n\n",
226+
expected: "[OK] - 1 Alerts: 0 Firing - 0 Pending - 1 Inactive\n\\_ [OK] [InactiveAlert] is inactive\n|total=1 firing=0 pending=0 inactive=1\n\n",
215227
},
216228
{
217229
name: "alert-recording-rule",
@@ -220,7 +232,7 @@ exit status 2
220232
w.Write(loadTestdata(alertTestDataSet4))
221233
})),
222234
args: []string{"run", "../main.go", "alert", "--name", "InactiveAlert"},
223-
expected: "[OK] - 1 Alerts: 0 Firing - 0 Pending - 1 Inactive\n\\_ [OK] [InactiveAlert] is inactive\n|firing=0 pending=0 inactive=1\n\n",
235+
expected: "[OK] - 1 Alerts: 0 Firing - 0 Pending - 1 Inactive\n\\_ [OK] [InactiveAlert] is inactive\n|total=1 firing=0 pending=0 inactive=1\n\n",
224236
},
225237
}
226238

0 commit comments

Comments
 (0)