verifying alerts at alertmanager 02/5002/1
authorvipin <vipin.mavila@nokia.com>
Fri, 6 Nov 2020 11:24:48 +0000 (11:24 +0000)
committervipin <vipin.mavila@nokia.com>
Fri, 6 Nov 2020 11:25:04 +0000 (11:25 +0000)
Change-Id: I3d1e86696c846c52333c747023001cfd9fdb4e4d
Signed-off-by: vipin <vipin.mavila@nokia.com>
cli/alarm-cli.go
manager/cmd/manager.go

index 775b806..241792a 100755 (executable)
@@ -18,7 +18,6 @@ import (
        "github.com/prometheus/alertmanager/api/v2/client"
        "github.com/prometheus/alertmanager/api/v2/client/alert"
        "github.com/prometheus/alertmanager/api/v2/models"
-       "github.com/spf13/viper"
        "github.com/thatisuday/commando"
 )
 
@@ -649,11 +648,38 @@ func raiseClearAlarmOverPeriod(alarmobject *alarm.Alarm, flags map[string]comman
        }
 }
 
-func displaySingleAlert(t table.Writer, gettableAlert *models.GettableAlert) {
-       t.AppendRow([]interface{}{"------------------------------------"})
+func dispalyAlertAnnotations(t table.Writer, gettableAlert *models.GettableAlert) {
+       var annotationmap map[string]string
+       annotationmap = make(map[string]string)
        for key, item := range gettableAlert.Annotations {
-               t.AppendRow([]interface{}{key, item})
+               annotationmap[key] = item
        }
+       t.AppendRow([]interface{}{"alarm_id", annotationmap["alarm_id"]})
+       t.AppendRow([]interface{}{"specific_problem", annotationmap["specific_problem"]})
+       t.AppendRow([]interface{}{"timestamp", annotationmap["timestamp"]})
+       t.AppendRow([]interface{}{"event_type", annotationmap["event_type"]})
+       t.AppendRow([]interface{}{"description", annotationmap["description"]})
+       t.AppendRow([]interface{}{"additional_info", annotationmap["additional_info"]})
+       t.AppendRow([]interface{}{"identifying_info", annotationmap["identifying_info"]})
+       t.AppendRow([]interface{}{"instructions", annotationmap["instructions"]})
+}
+
+func displayAlertLabels(t table.Writer, gettableAlert *models.GettableAlert) {
+       var labelmap map[string]string
+       labelmap = make(map[string]string)
+       for key, item := range gettableAlert.Alert.Labels {
+               labelmap[key] = item
+       }
+       t.AppendRow([]interface{}{"alertname", labelmap["alertname"]})
+       t.AppendRow([]interface{}{"status", labelmap["status"]})
+       t.AppendRow([]interface{}{"severity", labelmap["severity"]})
+       t.AppendRow([]interface{}{"system_name", labelmap["system_name"]})
+       t.AppendRow([]interface{}{"service", labelmap["service"]})
+}
+
+func displaySingleAlert(t table.Writer, gettableAlert *models.GettableAlert) {
+       t.AppendRow([]interface{}{"------------------------------------"})
+       dispalyAlertAnnotations(t, gettableAlert)
        if gettableAlert.EndsAt != nil {
                t.AppendRow([]interface{}{"EndsAt", *gettableAlert.EndsAt})
        }
@@ -677,9 +703,7 @@ func displaySingleAlert(t table.Writer, gettableAlert *models.GettableAlert) {
                t.AppendRow([]interface{}{"UpdatedAt", *gettableAlert.UpdatedAt})
        }
        t.AppendRow([]interface{}{"GeneratorURL", gettableAlert.Alert.GeneratorURL})
-       for key, item := range gettableAlert.Alert.Labels {
-               t.AppendRow([]interface{}{key, item})
-       }
+       displayAlertLabels(t, gettableAlert)
 }
 
 func displayAlerts(flags map[string]commando.FlagValue) {
@@ -715,23 +739,18 @@ func getAlerts(flags map[string]commando.FlagValue) (*alert.GetAlertsOK, error)
        unprocessed, _ := flags["unprocessed"].GetBool()
        amHost, _ := flags["host"].GetString()
        amPort, _ := flags["port"].GetString()
-       var amAddress string
-       if amHost == "" {
-               amAddress = viper.GetString("controls.promAlertManager.address")
-       } else {
-               amAddress = amHost + ":" + amPort
-       }
+       amAddress := amHost + ":" + amPort
+       amBaseUrl := "api/v2"
+       amSchemes := []string{"http"}
 
        alertParams := alert.NewGetAlertsParams()
        alertParams.Active = &active
        alertParams.Inhibited = &inhibited
        alertParams.Silenced = &silenced
        alertParams.Unprocessed = &unprocessed
-       amBaseUrl := viper.GetString("controls.promAlertManager.baseUrl")
-       amSchemes := []string{viper.GetString("controls.promAlertManager.schemes")}
        resp, err := newAlertManagerClient(amAddress, amBaseUrl, amSchemes).Alert.GetAlerts(alertParams)
        if err != nil {
-               err = fmt.Errorf("GetAlerts from '%s%s' failed with error: %v", amAddress, amBaseUrl, err)
+               err = fmt.Errorf("GetAlerts from amAddress = %s with amBaseUrl = %s failed with error: %v", amAddress, amBaseUrl, err)
        }
        return resp, err
 }
index 10f5115..6ccf4ac 100755 (executable)
@@ -81,7 +81,7 @@ func (a *AlarmManager) StartAlertTimer() {
                a.mutex.Lock()
                for _, m := range a.activeAlarms {
                        app.Logger.Info("Re-raising alarm: %v", m)
-                       a.PostAlert(a.GenerateAlertLabels(m.Alarm, AlertStatusActive, m.AlarmTime))
+                       a.PostAlert(a.GenerateAlertLabels(m.AlarmId, m.Alarm, AlertStatusActive, m.AlarmTime))
                }
                a.mutex.Unlock()
        }
@@ -185,7 +185,7 @@ func (a *AlarmManager) ProcessRaiseAlarm(m *AlarmNotification, alarmDef *alarm.A
        if app.Config.GetBool("controls.noma.enabled") {
                return a.PostAlarm(m)
        }
-       return a.PostAlert(a.GenerateAlertLabels(m.Alarm, AlertStatusActive, m.AlarmTime))
+       return a.PostAlert(a.GenerateAlertLabels(m.AlarmId, m.Alarm, AlertStatusActive, m.AlarmTime))
 }
 
 func (a *AlarmManager) ProcessClearAlarm(m *AlarmNotification, alarmDef *alarm.AlarmDefinition, idx int) (*alert.PostAlertsOK, error) {
@@ -321,7 +321,7 @@ func (a *AlarmManager) PostAlarm(m *AlarmNotification) (*alert.PostAlertsOK, err
        return nil, err
 }
 
-func (a *AlarmManager) GenerateAlertLabels(newAlarm alarm.Alarm, status AlertStatus, alarmTime int64) (models.LabelSet, models.LabelSet) {
+func (a *AlarmManager) GenerateAlertLabels(alarmId int, newAlarm alarm.Alarm, status AlertStatus, alarmTime int64) (models.LabelSet, models.LabelSet) {
        alarmDef := alarm.RICAlarmDefinitions[newAlarm.SpecificProblem]
        amLabels := models.LabelSet{
                "status":      string(status),
@@ -331,7 +331,7 @@ func (a *AlarmManager) GenerateAlertLabels(newAlarm alarm.Alarm, status AlertSta
                "system_name": "RIC",
        }
        amAnnotations := models.LabelSet{
-               "alarm_id":         fmt.Sprintf("%d", alarmDef.AlarmId),
+               "alarm_id":         fmt.Sprintf("%d", alarmId),
                "specific_problem": fmt.Sprintf("%d", newAlarm.SpecificProblem),
                "event_type":       alarmDef.EventType,
                "identifying_info": newAlarm.IdentifyingInfo,