7 "github.com/jedib0t/go-pretty/table"
8 "github.com/thatisuday/commando"
14 "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
21 SetExecutableName("alarm-cli").
23 SetDescription("This CLI tool provides management interface to SEP alarm system")
28 SetShortDescription("Displays the SEP active alarms").
29 SetDescription("This command displays more information about the SEP active alarms").
30 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
31 AddFlag("port", "Alarm manager host address", commando.String, "8080").
32 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
33 displayAlarms(getAlarms(flags, "active"), false)
39 SetShortDescription("Displays the SEP alarm history").
40 SetDescription("This command displays more information about the SEP alarm history").
41 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
42 AddFlag("port", "Alarm manager host address", commando.String, "8080").
43 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
44 displayAlarms(getAlarms(flags, "history"), true)
50 SetShortDescription("Raises alarm with given parameters").
51 AddFlag("moid", "Managed object Id", commando.String, nil).
52 AddFlag("apid", "Application Id", commando.String, nil).
53 AddFlag("sp", "Specific problem Id", commando.Int, nil).
54 AddFlag("severity", "Perceived severity", commando.String, nil).
55 AddFlag("iinfo", "Application identifying info", commando.String, nil).
56 AddFlag("aai", "Application additional info", commando.String, "-").
57 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
58 AddFlag("port", "Alarm manager host address", commando.String, "8080").
59 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
60 postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise)
66 SetShortDescription("Raises alarm with given parameters").
67 AddFlag("moid", "Managed object Id", commando.String, nil).
68 AddFlag("apid", "Application Id", commando.String, nil).
69 AddFlag("sp", "Specific problem Id", commando.Int, nil).
70 AddFlag("iinfo", "Application identifying info", commando.String, nil).
71 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
72 AddFlag("port", "Alarm manager host address", commando.String, "8080").
73 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
74 postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear)
77 // parse command-line arguments
81 func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.Alarm) {
82 a.ManagedObjectId, _ = flags["moid"].GetString()
83 a.ApplicationId, _ = flags["apid"].GetString()
84 a.SpecificProblem, _ = flags["sp"].GetInt()
85 a.IdentifyingInfo, _ = flags["iinfo"].GetString()
88 s, _ := flags["severity"].GetString()
89 a.PerceivedSeverity = alarm.Severity(s)
93 a.AdditionalInfo, _ = flags["aai"].GetString()
99 func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (alarms []alarm.AlarmMessage) {
100 host, _ := flags["host"].GetString()
101 port, _ := flags["port"].GetString()
102 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/%s", host, port, action)
103 resp, err := http.Get(targetUrl)
104 if err != nil || resp == nil || resp.Body == nil {
105 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
109 defer resp.Body.Close()
111 body, err := ioutil.ReadAll(resp.Body)
113 fmt.Println("ioutil.ReadAll failed: %v", err)
117 json.Unmarshal([]byte(body), &alarms)
121 func postAlarm(flags map[string]commando.FlagValue, a alarm.Alarm, action alarm.AlarmAction) {
122 host, _ := flags["host"].GetString()
123 port, _ := flags["port"].GetString()
124 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms", host, port)
126 m := alarm.AlarmMessage{Alarm: a, AlarmAction: action}
127 jsonData, err := json.Marshal(m)
129 fmt.Println("json.Marshal failed: %v", err)
133 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
134 if err != nil || resp == nil {
135 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
140 func displayAlarms(alarms []alarm.AlarmMessage, isHistory bool) {
141 t := table.NewWriter()
142 t.SetOutputMirror(os.Stdout)
144 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "ACTION", "TIME"})
146 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME"})
149 for _, a := range alarms {
150 alarmTime := time.Unix(0, a.AlarmTime).Format("02/01/2006, 15:04:05")
152 t.AppendRows([]table.Row{
153 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, a.AlarmAction, alarmTime},
156 t.AppendRows([]table.Row{
157 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, alarmTime},
162 t.SetStyle(table.StyleColoredBright)