X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=cli%2Falarm-cli.go;h=9a18015d7416c19181eeaf03077c27c8349a4986;hb=105030feb8fabd8b4ddff552c53e905146b2ea5f;hp=afe9da92358bbc32a12950c97eb0abe5ff961901;hpb=6f73fa33d550b6ff5ca2590757e98cec88254812;p=ric-plt%2Falarm-go.git diff --git a/cli/alarm-cli.go b/cli/alarm-cli.go index afe9da9..9a18015 100755 --- a/cli/alarm-cli.go +++ b/cli/alarm-cli.go @@ -8,12 +8,17 @@ import ( "net/http" "os" "strconv" + "sync" "time" "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/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/spf13/viper" "github.com/thatisuday/commando" - "sync" ) type CliAlarmDefinitions struct { @@ -28,6 +33,11 @@ type RicPerfAlarmObjects struct { AlarmObjects []*alarm.Alarm `json:"alarmobjects"` } +type AlarmNotification struct { + alarm.AlarmMessage + alarm.AlarmDefinition +} + var CLIPerfAlarmObjects map[int]*alarm.Alarm var wg sync.WaitGroup @@ -43,6 +53,15 @@ const ( ) func main() { + alarmManagerHost := "localhost" + if h := os.Getenv("ALARM_MANAGER_HOST"); h != "" { + alarmManagerHost = h + } + + alertManagerHost := "localhost" + if h := os.Getenv("ALERT_MANAGER_HOST"); h != "" { + alertManagerHost = h + } // configure commando commando. @@ -55,7 +74,7 @@ func main() { Register("active"). SetShortDescription("Displays the SEP active alarms"). SetDescription("This command displays more information about the SEP active alarms"). - AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost). AddFlag("port", "Alarm manager host address", commando.String, "8080"). SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { displayAlarms(getAlarms(flags, "active"), false) @@ -66,7 +85,7 @@ func main() { Register("history"). SetShortDescription("Displays the SEP alarm history"). SetDescription("This command displays more information about the SEP alarm history"). - AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost). AddFlag("port", "Alarm manager host address", commando.String, "8080"). SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { displayAlarms(getAlarms(flags, "history"), true) @@ -82,7 +101,7 @@ func main() { AddFlag("severity", "Perceived severity", commando.String, nil). AddFlag("iinfo", "Application identifying info", commando.String, nil). AddFlag("aai", "Application additional info", commando.String, "-"). - AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost). AddFlag("port", "Alarm manager host address", commando.String, "8080"). AddFlag("if", "http or rmr used as interface", commando.String, "http"). SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { @@ -97,7 +116,7 @@ func main() { AddFlag("apid", "Application Id", commando.String, nil). AddFlag("sp", "Specific problem Id", commando.Int, nil). AddFlag("iinfo", "Application identifying info", commando.String, nil). - AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost). AddFlag("port", "Alarm manager host address", commando.String, "8080"). AddFlag("if", "http or rmr used as interface", commando.String, "http"). SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { @@ -110,7 +129,7 @@ func main() { SetShortDescription("Configure alarm manager with given parameters"). AddFlag("mal", "max active alarms", commando.Int, nil). AddFlag("mah", "max alarm history", commando.Int, nil). - AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost). AddFlag("port", "Alarm manager host address", commando.String, "8080"). SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { postAlarmConfig(flags) @@ -123,7 +142,7 @@ func main() { AddFlag("atx", "alarm text", commando.String, nil). AddFlag("ety", "event type", commando.String, nil). AddFlag("oin", "operation instructions", commando.String, nil). - AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost). AddFlag("port", "Alarm manager host address", commando.String, "8080"). SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { postAlarmDefinition(flags) @@ -133,7 +152,7 @@ func main() { Register("undefine"). SetShortDescription("Define alarm with given parameters"). AddFlag("aid", "alarm identifier", commando.Int, nil). - AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost). AddFlag("port", "Alarm manager host address", commando.String, "8080"). SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { deleteAlarmDefinition(flags) @@ -146,13 +165,27 @@ func main() { AddFlag("nal", "number of alarms", commando.Int, nil). AddFlag("aps", "alarms per sec", commando.Int, nil). AddFlag("tim", "total time of test", commando.Int, nil). - AddFlag("host", "Alarm manager host address", commando.String, "localhost"). + AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost). AddFlag("port", "Alarm manager host address", commando.String, "8080"). AddFlag("if", "http or rmr used as interface", commando.String, "http"). SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { conductperformancetest(flags) }) + // Get alerts from Prometheus Alert Manager + commando. + Register("gapam"). + 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). + AddFlag("silenced", "Silenced alerts in Prometheus Alert Manager", commando.Bool, true). + AddFlag("unprocessed", "Unprocessed alerts in Prometheus Alert Manager", commando.Bool, true). + AddFlag("host", "Prometheus Alert Manager host address", commando.String, alertManagerHost). + AddFlag("port", "Prometheus Alert Manager port", commando.String, "9093"). + SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) { + displayAlerts(flags) + }) + // parse command-line arguments commando.Parse(nil) } @@ -174,7 +207,7 @@ func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.A return } -func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (alarms []alarm.AlarmMessage) { +func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (alarms []AlarmNotification) { host, _ := flags["host"].GetString() port, _ := flags["port"].GetString() targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/%s", host, port, action) @@ -246,26 +279,27 @@ func postAlarm(flags map[string]commando.FlagValue, a alarm.Alarm, action alarm. targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms", host, port) postAlarmWithHttpIf(targetUrl, a, action) } + fmt.Println("command executed successfully!") } -func displayAlarms(alarms []alarm.AlarmMessage, isHistory bool) { +func displayAlarms(alarms []AlarmNotification, isHistory bool) { t := table.NewWriter() t.SetOutputMirror(os.Stdout) if isHistory { - t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "ACTION", "TIME"}) + t.AppendHeader(table.Row{"ID", "SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "ACTION", "TIME"}) } else { - t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME"}) + t.AppendHeader(table.Row{"ID", "SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME"}) } for _, a := range alarms { alarmTime := time.Unix(0, a.AlarmTime).Format("02/01/2006, 15:04:05") if isHistory { t.AppendRows([]table.Row{ - {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, a.AlarmAction, alarmTime}, + {a.AlarmId, a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, a.AlarmAction, alarmTime}, }) } else { t.AppendRows([]table.Row{ - {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, alarmTime}, + {a.AlarmId, a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, alarmTime}, }) } } @@ -561,3 +595,89 @@ func raiseClearAlarmOverPeriod(alarmobject *alarm.Alarm, flags map[string]comman } } } + +func displayAlerts(flags map[string]commando.FlagValue) { + resp, err := getAlerts(flags) + if err != nil { + fmt.Println(err) + return + } + + if resp == nil { + fmt.Println("resp= nil") + return + } + + t := table.NewWriter() + 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}) + } + } + } + t.SetStyle(table.StyleColoredBright) + t.Render() +} + +func getAlerts(flags map[string]commando.FlagValue) (*alert.GetAlertsOK, error) { + active, _ := flags["active"].GetBool() + inhibited, _ := flags["inhibited"].GetBool() + silenced, _ := flags["silenced"].GetBool() + 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 + } + + 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) + } + return resp, err +} + +func newAlertManagerClient(amAddress string, amBaseUrl string, amSchemes []string) *client.Alertmanager { + cr := clientruntime.New(amAddress, amBaseUrl, amSchemes) + return client.New(cr, strfmt.Default) +}