Skip to content

Commit ea3ac23

Browse files
committed
[squash] simplify
1 parent 0b10c1d commit ea3ac23

File tree

5 files changed

+27
-32
lines changed

5 files changed

+27
-32
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ Examples:
153153
| total=2 firing=1 pending=0 inactive=1
154154
155155
Flags:
156+
-S, --state-label-key string Use the given AlertRule label to override the exit state for firing alerts.
157+
If this flag is set the plugin looks for warning/critical/ok in the provided label key
156158
--exclude-alert stringArray Alerts to ignore. Can be used multiple times and supports regex.
157159
--exclude-label stringArray The label of one or more specific alerts to exclude.
158160
This parameter can be repeated e.g.: '--exclude-label prio=high --exclude-label another=example'
@@ -168,14 +170,11 @@ Flags:
168170
If no name is given, all alerts will be evaluated
169171
-T, --no-alerts-state string State to assign when no alerts are found (0, 1, 2, 3, OK, WARNING, CRITICAL, UNKNOWN). If not set this defaults to OK (default "OK")
170172
-P, --problems Display only alerts which status is not inactive/OK. Note that in combination with the --name flag this might result in no alerts being displayed
171-
-K, --state-labels-key string Label key for the --use-state-labels. Can be used to override the default label key 'icingaState' (default "icingaState")
172-
-S, --use-state-labels Use the AlertRule label 'icingaState' to override the exit state for firing alerts.
173-
If this flag is set the plugin looks for warning/critical/ok in the label 'icingaState'
174173
```
175174
176-
The `--use-state-labels` can be used to override the exit code for firing alerts.
177-
If the flag is active, the plugin looks for the label key (icingaState) on the AlertRule and uses
178-
the specified as label values (warning/critical/ok).
175+
The `--state-label-key` can be used to override the exit code for firing alerts.
176+
When the flag is set, the plugin looks for the given label key on the AlertRule and uses
177+
the specified as label values (`warning/critical/ok`) as exit code..
179178
180179
#### Checking all defined alerts
181180

cmd/alert.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ import (
1616
)
1717

1818
type AlertConfig struct {
19-
AlertName []string
20-
Group []string
21-
ExcludeAlerts []string
22-
ExcludeLabels []string
23-
IncludeLabels []string
24-
ProblemsOnly bool
25-
HandleStateLabels bool
26-
HandleStateLabelKey string
27-
NoAlertsState string
19+
AlertName []string
20+
Group []string
21+
ExcludeAlerts []string
22+
ExcludeLabels []string
23+
IncludeLabels []string
24+
ProblemsOnly bool
25+
StateLabelKey string
26+
NoAlertsState string
2827
}
2928

3029
var cliAlertConfig AlertConfig
@@ -148,7 +147,7 @@ inactive = 0`,
148147
if len(rl.AlertingRule.Alerts) == 0 {
149148
// Counting states for perfdata. We don't use the state-label override here
150149
// to have the acutal count from Prometheus
151-
switch rl.GetStatus(false, "") {
150+
switch rl.GetStatus("") {
152151
case 0:
153152
counterInactive++
154153
case 1:
@@ -159,7 +158,7 @@ inactive = 0`,
159158

160159
sc := result.NewPartialResult()
161160

162-
_ = sc.SetState(rl.GetStatus(cliAlertConfig.HandleStateLabels, cliAlertConfig.HandleStateLabelKey))
161+
_ = sc.SetState(rl.GetStatus(cliAlertConfig.StateLabelKey))
163162
sc.Output = rl.GetOutput()
164163
overall.AddSubcheck(sc)
165164
}
@@ -170,7 +169,7 @@ inactive = 0`,
170169
for _, alert := range rl.AlertingRule.Alerts {
171170
// Counting states for perfdata. We don't use the state-label override here
172171
// to have the acutal count from Prometheus
173-
switch rl.GetStatus(false, "") {
172+
switch rl.GetStatus("") {
174173
case 0:
175174
counterInactive++
176175
case 1:
@@ -181,7 +180,7 @@ inactive = 0`,
181180

182181
sc := result.NewPartialResult()
183182

184-
_ = sc.SetState(rl.GetStatus(cliAlertConfig.HandleStateLabels, cliAlertConfig.HandleStateLabelKey))
183+
_ = sc.SetState(rl.GetStatus(cliAlertConfig.StateLabelKey))
185184
// Set the alert in the internal Type to generate the output
186185
rl.Alert = alert
187186
sc.Output = rl.GetOutput()
@@ -253,12 +252,9 @@ func init() {
253252
fs.BoolVarP(&cliAlertConfig.ProblemsOnly, "problems", "P", false,
254253
"Display only alerts which status is not inactive/OK. Note that in combination with the --name flag this might result in no alerts being displayed")
255254

256-
fs.BoolVarP(&cliAlertConfig.HandleStateLabels, "use-state-labels", "S", false,
257-
"Use the AlertRule label 'icingaState' to override the exit state for firing alerts."+
258-
"\nIf this flag is set the plugin looks for warning/critical/ok in the label 'icingaState'")
259-
260-
fs.StringVarP(&cliAlertConfig.HandleStateLabelKey, "state-labels-key", "K", "icingaState",
261-
"Label key for the --use-state-labels. Can be used to override the default label key 'icingaState'")
255+
fs.StringVarP(&cliAlertConfig.StateLabelKey, "state-label-key", "S", "",
256+
"Use the given AlertRule label to override the exit state for firing alerts."+
257+
"\nIf this flag is set the plugin looks for warning/critical/ok in the provided label key")
262258
}
263259

264260
// Function to convert state to integer.

cmd/alert_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ exit status 2
310310
w.WriteHeader(http.StatusOK)
311311
w.Write(loadTestdata(alertTestDataSet1))
312312
})),
313-
args: []string{"run", "../main.go", "alert", "--use-state-labels", "--state-labels-key=icinga"},
313+
args: []string{"run", "../main.go", "alert", "--state-label-key=icinga"},
314314
expected: `[WARNING] - 3 Alerts: 1 Firing - 1 Pending - 1 Inactive
315315
\_ [OK] [HostOutOfMemory] is inactive
316316
\_ [WARNING] [SqlAccessDeniedRate] - Job: [mysql] on Instance: [localhost] is pending - value: 0.40 - {"alertname":"SqlAccessDeniedRate","instance":"localhost","job":"mysql","severity":"warning"}

internal/alert/alert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func FlattenRules(groups []v1.RuleGroup, wantedGroups []string) []Rule {
5858
return rules
5959
}
6060

61-
func (a *Rule) GetStatus(handleStateLabels bool, labelKey string) (status int) {
61+
func (a *Rule) GetStatus(labelKey string) (status int) {
6262
state := a.AlertingRule.State
6363

6464
switch state {
@@ -72,7 +72,7 @@ func (a *Rule) GetStatus(handleStateLabels bool, labelKey string) (status int) {
7272
status = check.Unknown
7373
}
7474

75-
if state == string(v1.AlertStateFiring) && handleStateLabels {
75+
if state == string(v1.AlertStateFiring) && labelKey != "" {
7676
stateLabel, ok := a.AlertingRule.Labels[model.LabelName(labelKey)]
7777
// If there is no such label key, we're done
7878
if !ok {

internal/alert/alert_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func TestGetStatus(t *testing.T) {
4848
Alert: ar.Alerts[0],
4949
}
5050

51-
actual := r.GetStatus(false, "")
51+
actual := r.GetStatus("")
5252
if actual != check.Critical {
5353
t.Error("\nActual: ", actual, "\nExpected: ", check.Critical)
5454
}
5555

5656
r.AlertingRule.State = "pending"
57-
actual = r.GetStatus(false, "")
57+
actual = r.GetStatus("")
5858
if actual != check.Warning {
5959
t.Error("\nActual: ", actual, "\nExpected: ", check.Warning)
6060
}
@@ -97,13 +97,13 @@ func TestGetStatus_WithLabel(t *testing.T) {
9797
Alert: ar.Alerts[0],
9898
}
9999

100-
actual := r.GetStatus(true, "icingaState")
100+
actual := r.GetStatus("icingaState")
101101
if actual != check.OK {
102102
t.Error("\nActual: ", actual, "\nExpected: ", check.Critical)
103103
}
104104

105105
r.AlertingRule.State = "pending"
106-
actual = r.GetStatus(true, "icingaState")
106+
actual = r.GetStatus("icingaState")
107107
if actual != check.Warning {
108108
t.Error("\nActual: ", actual, "\nExpected: ", check.Warning)
109109
}

0 commit comments

Comments
 (0)