From 3133d9f1d0507ade338b0e61ec5426f31183a113 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Thu, 12 Nov 2026 13:47:56 +0100 Subject: [PATCH] Fix alert state when no alerts are included --- cmd/alert.go | 8 +++++++- cmd/alert_test.go | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cmd/alert.go b/cmd/alert.go index 72255bc..81cb7ca 100644 --- a/cmd/alert.go +++ b/cmd/alert.go @@ -110,6 +110,8 @@ inactive = 0`, } var overall goresult.Overall + // We need to count how many times we added an alert for the no-alerts-state + var counterPartialResult int for _, rl := range rules { // If it's not the Alert we're looking for, Skip! @@ -172,6 +174,8 @@ inactive = 0`, sc.SetState(rlStatus) sc.SetOutput(rl.GetOutput()) overall.AddSubcheck(sc) + + counterPartialResult++ } // Handle active alerts @@ -225,6 +229,8 @@ inactive = 0`, rl.Alert = alert sc.SetOutput(rl.GetOutput()) overall.AddSubcheck(sc) + + counterPartialResult++ } } } @@ -239,7 +245,7 @@ inactive = 0`, } // When there are no alerts we add an empty PartialResult just to have consistent output - if counterAlert == 0 { + if counterPartialResult == 0 { sc := goresult.NewPartialResult() sc.SetDefaultState(noAlertsState) sc.SetOutput("No alerts retrieved") diff --git a/cmd/alert_test.go b/cmd/alert_test.go index 604752f..5b08a2b 100644 --- a/cmd/alert_test.go +++ b/cmd/alert_test.go @@ -248,6 +248,19 @@ exit status 2 \_ [CRITICAL] [BlackboxTLS] - Job: [blackbox] on Instance: [https://localhost:443] is firing - value: -6065338.00 - {"alertname":"TLS","icinga":"ok","instance":"https://localhost:443","job":"blackbox","severity":"critical","team":"network"} |total=3 firing=1 pending=1 inactive=1 exit status 2 +`, + }, + { + name: "alert-include-label-no-alerts-state", + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write(loadTestdata(alertTestDataSet1)) + })), + args: []string{"run", "../main.go", "alert", "--include-label", "foo=bar", "--no-alerts-state", "1", "-P", "true"}, + expected: `[WARNING] - No alerts retrieved +\_ [WARNING] No alerts retrieved +|total=2 firing=1 pending=1 inactive=0 +exit status 1 `, }, {