X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=cli%2Falarm-cli.go;h=0b4af6bcfd024c639e51a95c8792e1db604fb96d;hb=63b5df0f4b2cb5f9d979501bf28f27d1ee36769f;hp=1b630cdd1001b7548c7db299223292c1dae6a11d;hpb=e92822cba904bab04c5c71a9ffa99a1d89409640;p=ric-plt%2Falarm-go.git diff --git a/cli/alarm-cli.go b/cli/alarm-cli.go index 1b630cd..0b4af6b 100755 --- a/cli/alarm-cli.go +++ b/cli/alarm-cli.go @@ -11,14 +11,13 @@ import ( "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" "github.com/prometheus/alertmanager/api/v2/models" - "github.com/spf13/viper" "github.com/thatisuday/commando" ) @@ -70,7 +69,21 @@ func main() { 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"). @@ -80,7 +93,9 @@ func main() { 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"). @@ -91,7 +106,9 @@ func main() { 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"). @@ -109,6 +126,9 @@ func main() { postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise, nil) }) +} + +func registerClearCmd(alarmManagerHost string) { // Clear an alarm commando. Register("clear"). @@ -124,6 +144,9 @@ func main() { postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear, nil) }) +} + +func registerConfigureCmd(alarmManagerHost string) { // Configure an alarm manager commando. Register("configure"). @@ -135,6 +158,9 @@ func main() { SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { postAlarmConfig(flags) }) +} + +func registerDefineCmd(alarmManagerHost string) { // Create alarm definition commando. Register("define"). @@ -150,7 +176,10 @@ func main() { 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"). @@ -160,7 +189,10 @@ func main() { 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"). @@ -174,10 +206,12 @@ func main() { 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. - 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). @@ -188,9 +222,6 @@ func main() { 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) { @@ -540,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()) - + return err } return nil @@ -617,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}) } @@ -645,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) { @@ -683,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 } @@ -715,4 +766,4 @@ func getCurrentDirectory() string { fmt.Println(err) } return dir -} \ No newline at end of file +}