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)
105 // Delete alarm defenition
107 Register("undefine").
108 SetShortDescription("Define alarm with given parameters").
109 AddFlag("aid", "alarm identifier", commando.Int, nil).
110 AddFlag("host", "Alarm manager host address", commando.String, "localhost").
111 AddFlag("port", "Alarm manager host address", commando.String, "8080").
112 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
113 deleteAlarmDefinition(flags)
116 // parse command-line arguments
120 func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.Alarm) {
121 a.ManagedObjectId, _ = flags["moid"].GetString()
122 a.ApplicationId, _ = flags["apid"].GetString()
123 a.SpecificProblem, _ = flags["sp"].GetInt()
124 a.IdentifyingInfo, _ = flags["iinfo"].GetString()
127 s, _ := flags["severity"].GetString()
128 a.PerceivedSeverity = alarm.Severity(s)
132 a.AdditionalInfo, _ = flags["aai"].GetString()
138 func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (alarms []alarm.AlarmMessage) {
139 host, _ := flags["host"].GetString()
140 port, _ := flags["port"].GetString()
141 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/%s", host, port, action)
142 resp, err := http.Get(targetUrl)
143 if err != nil || resp == nil || resp.Body == nil {
144 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
148 defer resp.Body.Close()
150 body, err := ioutil.ReadAll(resp.Body)
152 fmt.Println("ioutil.ReadAll failed: %v", err)
156 json.Unmarshal([]byte(body), &alarms)
160 func postAlarm(flags map[string]commando.FlagValue, a alarm.Alarm, action alarm.AlarmAction) {
161 host, _ := flags["host"].GetString()
162 port, _ := flags["port"].GetString()
163 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms", host, port)
165 m := alarm.AlarmMessage{Alarm: a, AlarmAction: action}
166 jsonData, err := json.Marshal(m)
168 fmt.Println("json.Marshal failed: %v", err)
172 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
173 if err != nil || resp == nil {
174 fmt.Println("Couldn't fetch active alarm list due to error: %v", err)
179 func displayAlarms(alarms []alarm.AlarmMessage, isHistory bool) {
180 t := table.NewWriter()
181 t.SetOutputMirror(os.Stdout)
183 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "ACTION", "TIME"})
185 t.AppendHeader(table.Row{"SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME"})
188 for _, a := range alarms {
189 alarmTime := time.Unix(0, a.AlarmTime).Format("02/01/2006, 15:04:05")
191 t.AppendRows([]table.Row{
192 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, a.AlarmAction, alarmTime},
195 t.AppendRows([]table.Row{
196 {a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, alarmTime},
201 t.SetStyle(table.StyleColoredBright)
205 func postAlarmConfig(flags map[string]commando.FlagValue) {
206 host, _ := flags["host"].GetString()
207 port, _ := flags["port"].GetString()
208 maxactivealarms, _ := flags["mal"].GetInt()
209 maxalarmhistory, _ := flags["mah"].GetInt()
210 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/config", host, port)
212 m := alarm.AlarmConfigParams{MaxActiveAlarms: maxactivealarms, MaxAlarmHistory: maxalarmhistory}
213 jsonData, err := json.Marshal(m)
215 fmt.Println("json.Marshal failed: %v", err)
219 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
220 if err != nil || resp == nil {
221 fmt.Println("Couldn't fetch post alarm configuration due to error: %v", err)
226 func postAlarmDefinition(flags map[string]commando.FlagValue) {
227 host, _ := flags["host"].GetString()
228 port, _ := flags["port"].GetString()
229 alarmid, _ := flags["aid"].GetInt()
230 alarmtxt, _ := flags["atx"].GetString()
231 etype, _ := flags["ety"].GetString()
232 operation, _ := flags["oin"].GetString()
233 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define", host, port)
235 var alarmdefinition alarm.AlarmDefinition
236 alarmdefinition.AlarmId = alarmid
237 alarmdefinition.AlarmText = alarmtxt
238 alarmdefinition.EventType = etype
239 alarmdefinition.OperationInstructions = operation
241 m := CliAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarmdefinition}}
242 jsonData, err := json.Marshal(m)
244 fmt.Println("json.Marshal failed: %v", err)
248 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
249 if err != nil || resp == nil {
250 fmt.Println("Couldn't post alarm definition due to error: %v", err)
255 func deleteAlarmDefinition(flags map[string]commando.FlagValue) {
256 host, _ := flags["host"].GetString()
257 port, _ := flags["port"].GetString()
258 alarmid, _ := flags["aid"].GetInt()
259 salarmid := strconv.FormatUint(uint64(alarmid), 10)
260 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define/%s", host, port, salarmid)
262 client := &http.Client{}
263 req, err := http.NewRequest("DELETE", targetUrl, nil)
264 if err != nil || req == nil {
265 fmt.Println("Couldn't make delete request due to error: %v", err)
268 resp, errr := client.Do(req)
269 if errr != nil || resp == nil {
270 fmt.Println("Couldn't send delete request due to error: %v", err)