7 "github.com/jedib0t/go-pretty/table"
8 "github.com/thatisuday/commando"
14 "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
17 type CliAlarmDefinitions struct {
18 AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"`
25 SetExecutableName("alarm-cli").
27 SetDescription("This CLI tool provides management interface to SEP alarm system")
32 SetShortDescription("Displays the SEP active alarms").
33 SetDescription("This command displays more information about the SEP active alarms").
34 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
35 AddFlag("port", "Alarm manager host address", commando.String, "8080").
36 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
37 displayAlarms(getAlarms(flags, "active"), false)
43 SetShortDescription("Displays the SEP alarm history").
44 SetDescription("This command displays more information about the SEP alarm history").
45 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
46 AddFlag("port", "Alarm manager host address", commando.String, "8080").
47 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
48 displayAlarms(getAlarms(flags, "history"), true)
54 SetShortDescription("Raises alarm with given parameters").
55 AddFlag("moid", "Managed object Id", commando.String, nil).
56 AddFlag("apid", "Application Id", commando.String, nil).
57 AddFlag("sp", "Specific problem Id", commando.Int, nil).
58 AddFlag("severity", "Perceived severity", commando.String, nil).
59 AddFlag("iinfo", "Application identifying info", commando.String, nil).
60 AddFlag("aai", "Application additional info", commando.String, "-").
61 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
62 AddFlag("port", "Alarm manager host address", commando.String, "8080").
63 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
64 postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise)
70 SetShortDescription("Raises alarm with given parameters").
71 AddFlag("moid", "Managed object Id", commando.String, nil).
72 AddFlag("apid", "Application Id", commando.String, nil).
73 AddFlag("sp", "Specific problem Id", commando.Int, nil).
74 AddFlag("iinfo", "Application identifying info", commando.String, nil).
75 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
76 AddFlag("port", "Alarm manager host address", commando.String, "8080").
77 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
78 postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear)
81 // Configure an alarm manager
83 Register("configure").
84 SetShortDescription("Configure alarm manager with given parameters").
85 AddFlag("mal", "max active alarms", commando.Int, nil).
86 AddFlag("mah", "max alarm history", commando.Int, nil).
87 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
88 AddFlag("port", "Alarm manager host address", commando.String, "8080").
89 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
90 postAlarmConfig(flags)
92 // Create alarm defenition
95 SetShortDescription("Define alarm with given parameters").
96 AddFlag("aid", "alarm identifier", commando.Int, nil).
97 AddFlag("atx", "alarm text", commando.String, nil).
98 AddFlag("ety", "event type", commando.String, nil).
99 AddFlag("oin", "operation instructions", commando.String, nil).
100 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
101 AddFlag("port", "Alarm manager host address", commando.String, "8080").
102 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
103 postAlarmDefinition(flags)
106 // parse command-line arguments
110 func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.Alarm) {
111 a.ManagedObjectId, _ = flags["moid"].GetString()
112 a.ApplicationId, _ = flags["apid"].GetString()
113 a.SpecificProblem, _ = flags["sp"].GetInt()
114 a.IdentifyingInfo, _ = flags["iinfo"].GetString()
117 s, _ := flags["severity"].GetString()
118 a.PerceivedSeverity = alarm.Severity(s)
122 a.AdditionalInfo, _ = flags["aai"].GetString()
128 func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (alarms []alarm.AlarmMessage) {
129 host, _ := flags["host"].GetString()
130 port, _ := flags["port"].GetString()
131 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/%s", host, port, action)
132 resp, err := http.Get(targetUrl)
133 if err != nil || resp == nil || resp.Body == nil {
134 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
138 defer resp.Body.Close()
140 body, err := ioutil.ReadAll(resp.Body)
142 fmt.Println("ioutil.ReadAll failed: %v", err)
146 json.Unmarshal([]byte(body), &alarms)
150 func postAlarm(flags map[string]commando.FlagValue, a alarm.Alarm, action alarm.AlarmAction) {
151 host, _ := flags["host"].GetString()
152 port, _ := flags["port"].GetString()
153 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms", host, port)
155 m := alarm.AlarmMessage{Alarm: a, AlarmAction: action}
156 jsonData, err := json.Marshal(m)
158 fmt.Println("json.Marshal failed: %v", err)
162 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
163 if err != nil || resp == nil {
164 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
169 func displayAlarms(alarms []alarm.AlarmMessage, isHistory bool) {
170 t := table.NewWriter()
171 t.SetOutputMirror(os.Stdout)
173 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "ACTION", "TIME"})
175 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME"})
178 for _, a := range alarms {
179 alarmTime := time.Unix(0, a.AlarmTime).Format("02/01/2006, 15:04:05")
181 t.AppendRows([]table.Row{
182 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, a.AlarmAction, alarmTime},
185 t.AppendRows([]table.Row{
186 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, alarmTime},
191 t.SetStyle(table.StyleColoredBright)
195 func postAlarmConfig(flags map[string]commando.FlagValue) {
196 host, _ := flags["host"].GetString()
197 port, _ := flags["port"].GetString()
198 maxactivealarms, _ := flags["mal"].GetInt()
199 maxalarmhistory, _ := flags["mah"].GetInt()
200 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/config", host, port)
202 m := alarm.AlarmConfigParams{MaxActiveAlarms: maxactivealarms, MaxAlarmHistory: maxalarmhistory}
203 jsonData, err := json.Marshal(m)
205 fmt.Println("json.Marshal failed: %v", err)
209 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
210 if err != nil || resp == nil {
211 fmt.Println("Couldn't fetch post alarm configuration due to error: %v", err)
216 func postAlarmDefinition(flags map[string]commando.FlagValue) {
217 host, _ := flags["host"].GetString()
218 port, _ := flags["port"].GetString()
219 alarmid, _ := flags["aid"].GetInt()
220 alarmtxt, _ := flags["atx"].GetString()
221 etype, _ := flags["ety"].GetString()
222 operation, _ := flags["oin"].GetString()
223 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define", host, port)
225 var alarmdefinition alarm.AlarmDefinition
226 alarmdefinition.AlarmId = alarmid
227 alarmdefinition.AlarmText = alarmtxt
228 alarmdefinition.EventType = etype
229 alarmdefinition.OperationInstructions = operation
231 m := CliAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarmdefinition}}
232 jsonData, err := json.Marshal(m)
234 fmt.Println("json.Marshal failed: %v", err)
238 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
239 if err != nil || resp == nil {
240 fmt.Println("Couldn't fetch post alarm configuration due to error: %v", err)