F release step 2 of 2
[ric-plt/alarm-go.git] / cli / alarm-cli.go
index b213ec6..0b4af6b 100755 (executable)
@@ -11,13 +11,13 @@ import (
        "sync"
        "time"
 
        "sync"
        "time"
 
-       "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
+       "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm"
        clientruntime "github.com/go-openapi/runtime/client"
        "github.com/go-openapi/strfmt"
        "github.com/jedib0t/go-pretty/table"
        "github.com/prometheus/alertmanager/api/v2/client"
        "github.com/prometheus/alertmanager/api/v2/client/alert"
        clientruntime "github.com/go-openapi/runtime/client"
        "github.com/go-openapi/strfmt"
        "github.com/jedib0t/go-pretty/table"
        "github.com/prometheus/alertmanager/api/v2/client"
        "github.com/prometheus/alertmanager/api/v2/client/alert"
-       "github.com/spf13/viper"
+       "github.com/prometheus/alertmanager/api/v2/models"
        "github.com/thatisuday/commando"
 )
 
        "github.com/thatisuday/commando"
 )
 
@@ -69,7 +69,21 @@ func main() {
                SetVersion("1.0.0").
                SetDescription("This CLI tool provides management interface to SEP alarm system")
 
                SetVersion("1.0.0").
                SetDescription("This CLI tool provides management interface to SEP alarm system")
 
-       // Get active alarms
+       registerActiveCmd(alarmManagerHost)
+       registerHistoryCmd(alarmManagerHost)
+       registerRaiseCmd(alarmManagerHost)
+       registerClearCmd(alarmManagerHost)
+       registerDefineCmd(alarmManagerHost)
+       registerUndefineCmd(alarmManagerHost)
+       registerConfigureCmd(alarmManagerHost)
+       registerPerfCmd(alarmManagerHost)
+       registerAlertCmd(alertManagerHost)
+
+       // parse command-line arguments
+       commando.Parse(nil)
+}
+
+func registerActiveCmd(alarmManagerHost string) {
        commando.
                Register("active").
                SetShortDescription("Displays the SEP active alarms").
        commando.
                Register("active").
                SetShortDescription("Displays the SEP active alarms").
@@ -79,7 +93,9 @@ func main() {
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        displayAlarms(getAlarms(flags, "active"), false)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        displayAlarms(getAlarms(flags, "active"), false)
                })
+}
 
 
+func registerHistoryCmd(alarmManagerHost string) {
        // Get alarm history
        commando.
                Register("history").
        // Get alarm history
        commando.
                Register("history").
@@ -90,7 +106,9 @@ func main() {
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        displayAlarms(getAlarms(flags, "history"), true)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        displayAlarms(getAlarms(flags, "history"), true)
                })
+}
 
 
+func registerRaiseCmd(alarmManagerHost string) {
        // Raise an alarm
        commando.
                Register("raise").
        // Raise an alarm
        commando.
                Register("raise").
@@ -108,6 +126,9 @@ func main() {
                        postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise, nil)
                })
 
                        postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise, nil)
                })
 
+}
+
+func registerClearCmd(alarmManagerHost string) {
        // Clear an alarm
        commando.
                Register("clear").
        // Clear an alarm
        commando.
                Register("clear").
@@ -123,6 +144,9 @@ func main() {
                        postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear, nil)
                })
 
                        postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear, nil)
                })
 
+}
+
+func registerConfigureCmd(alarmManagerHost string) {
        // Configure an alarm manager
        commando.
                Register("configure").
        // Configure an alarm manager
        commando.
                Register("configure").
@@ -134,6 +158,9 @@ func main() {
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarmConfig(flags)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarmConfig(flags)
                })
+}
+
+func registerDefineCmd(alarmManagerHost string) {
        // Create alarm definition
        commando.
                Register("define").
        // Create alarm definition
        commando.
                Register("define").
@@ -149,7 +176,10 @@ func main() {
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarmDefinition(flags)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        postAlarmDefinition(flags)
                })
-               // Delete alarm definition
+}
+
+func registerUndefineCmd(alarmManagerHost string) {
+       // Delete alarm definition
        commando.
                Register("undefine").
                SetShortDescription("Define alarm with given parameters").
        commando.
                Register("undefine").
                SetShortDescription("Define alarm with given parameters").
@@ -159,7 +189,10 @@ func main() {
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        deleteAlarmDefinition(flags)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        deleteAlarmDefinition(flags)
                })
-               // Conduct performance test for alarm-go
+}
+
+func registerPerfCmd(alarmManagerHost string) {
+       // Conduct performance test for alarm-go
        commando.
                Register("perf").
                SetShortDescription("Conduct performance test with given parameters").
        commando.
                Register("perf").
                SetShortDescription("Conduct performance test with given parameters").
@@ -173,10 +206,12 @@ func main() {
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        conductperformancetest(flags)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        conductperformancetest(flags)
                })
+}
 
 
+func registerAlertCmd(alertManagerHost string) {
        // Get alerts from Prometheus Alert Manager
        commando.
        // Get alerts from Prometheus Alert Manager
        commando.
-               Register("gapam").
+               Register("alerts").
                SetShortDescription("Get alerts from Prometheus Alert Manager").
                AddFlag("active", "Active alerts in Prometheus Alert Manager", commando.Bool, true).
                AddFlag("inhibited", "Inhibited alerts in Prometheus Alert Manager", commando.Bool, true).
                SetShortDescription("Get alerts from Prometheus Alert Manager").
                AddFlag("active", "Active alerts in Prometheus Alert Manager", commando.Bool, true).
                AddFlag("inhibited", "Inhibited alerts in Prometheus Alert Manager", commando.Bool, true).
@@ -187,9 +222,6 @@ func main() {
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        displayAlerts(flags)
                })
                SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
                        displayAlerts(flags)
                })
-
-       // parse command-line arguments
-       commando.Parse(nil)
 }
 
 func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.Alarm) {
 }
 
 func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.Alarm) {
@@ -539,7 +571,7 @@ func readPerfAlarmDefinitionFromJson() error {
        } else {
                fmt.Printf("ReadPerfAlarmDefinitionFromJson: ioutil.ReadFile failed with error: %v, filename: %s\n", err, filename)
                fmt.Printf("ReadPerfAlarmDefinitionFromJson: current directory: %s\n", getCurrentDirectory())
        } else {
                fmt.Printf("ReadPerfAlarmDefinitionFromJson: ioutil.ReadFile failed with error: %v, filename: %s\n", err, filename)
                fmt.Printf("ReadPerfAlarmDefinitionFromJson: current directory: %s\n", getCurrentDirectory())
-       
+
                return err
        }
        return nil
                return err
        }
        return nil
@@ -616,6 +648,64 @@ func raiseClearAlarmOverPeriod(alarmobject *alarm.Alarm, flags map[string]comman
        }
 }
 
        }
 }
 
+func dispalyAlertAnnotations(t table.Writer, gettableAlert *models.GettableAlert) {
+       var annotationmap map[string]string
+       annotationmap = make(map[string]string)
+       for key, item := range gettableAlert.Annotations {
+               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})
+       }
+       if gettableAlert.Fingerprint != nil {
+               t.AppendRow([]interface{}{"Fingerprint", *gettableAlert.Fingerprint})
+       }
+       for key, item := range gettableAlert.Receivers {
+               if gettableAlert.Receivers != nil {
+                       t.AppendRow([]interface{}{key, *item.Name})
+               }
+       }
+       if gettableAlert.StartsAt != nil {
+               t.AppendRow([]interface{}{"StartsAt", *gettableAlert.StartsAt})
+       }
+       if gettableAlert.Status != nil {
+               t.AppendRow([]interface{}{"InhibitedBy", gettableAlert.Status.InhibitedBy})
+               t.AppendRow([]interface{}{"SilencedBy", gettableAlert.Status.SilencedBy})
+               t.AppendRow([]interface{}{"State", *gettableAlert.Status.State})
+       }
+       if gettableAlert.UpdatedAt != nil {
+               t.AppendRow([]interface{}{"UpdatedAt", *gettableAlert.UpdatedAt})
+       }
+       t.AppendRow([]interface{}{"GeneratorURL", gettableAlert.Alert.GeneratorURL})
+       displayAlertLabels(t, gettableAlert)
+}
+
 func displayAlerts(flags map[string]commando.FlagValue) {
        resp, err := getAlerts(flags)
        if err != nil {
 func displayAlerts(flags map[string]commando.FlagValue) {
        resp, err := getAlerts(flags)
        if err != nil {
@@ -632,38 +722,11 @@ func displayAlerts(flags map[string]commando.FlagValue) {
        t.SetOutputMirror(os.Stdout)
        t.AppendHeader(table.Row{"Alerts from Prometheus Alert Manager"})
        for _, gettableAlert := range resp.Payload {
        t.SetOutputMirror(os.Stdout)
        t.AppendHeader(table.Row{"Alerts from Prometheus Alert Manager"})
        for _, gettableAlert := range resp.Payload {
-               t.AppendRow([]interface{}{"------------------------------------"})
-               if gettableAlert != nil {
-                       for key, item := range gettableAlert.Annotations {
-                               t.AppendRow([]interface{}{key, item})
-                       }
-                       if gettableAlert.EndsAt != nil {
-                               t.AppendRow([]interface{}{"EndsAt", *gettableAlert.EndsAt})
-                       }
-                       if gettableAlert.Fingerprint != nil {
-                               t.AppendRow([]interface{}{"Fingerprint", *gettableAlert.Fingerprint})
-                       }
-                       for key, item := range gettableAlert.Receivers {
-                               if gettableAlert.Receivers != nil {
-                                       t.AppendRow([]interface{}{key, *item.Name})
-                               }
-                       }
-                       if gettableAlert.StartsAt != nil {
-                               t.AppendRow([]interface{}{"StartsAt", *gettableAlert.StartsAt})
-                       }
-                       if gettableAlert.Status != nil {
-                               t.AppendRow([]interface{}{"InhibitedBy", gettableAlert.Status.InhibitedBy})
-                               t.AppendRow([]interface{}{"SilencedBy", gettableAlert.Status.SilencedBy})
-                               t.AppendRow([]interface{}{"State", *gettableAlert.Status.State})
-                       }
-                       if gettableAlert.UpdatedAt != nil {
-                               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})
-                       }
+               if gettableAlert == nil {
+                       continue
                }
                }
+
+               displaySingleAlert(t, gettableAlert)
        }
        t.SetStyle(table.StyleColoredBright)
        t.Render()
        }
        t.SetStyle(table.StyleColoredBright)
        t.Render()
@@ -676,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()
        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
 
        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 {
        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
 }
        }
        return resp, err
 }
@@ -708,4 +766,4 @@ func getCurrentDirectory() string {
                fmt.Println(err)
        }
        return dir
                fmt.Println(err)
        }
        return dir
-}
\ No newline at end of file
+}