blob: 8d66c8ec42f5e110e1345dcb3da8ac02bab83a59 [file] [log] [blame]
Mohamed Abukar0c389732020-09-17 14:47:50 +03001package main
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
Mohamed Abukar0c389732020-09-17 14:47:50 +03007 "io/ioutil"
8 "net/http"
9 "os"
Anssi Mannila500878a2020-09-30 08:57:04 +030010 "strconv"
Anssi Mannila00894a42020-10-19 11:36:26 +030011 "sync"
Mohamed Abukar0c389732020-09-17 14:47:50 +030012 "time"
Anssi Mannila500878a2020-09-30 08:57:04 +030013
Juha Hyttinen281d0602021-12-23 10:14:40 +020014 "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm"
Anssi Mannila00894a42020-10-19 11:36:26 +030015 clientruntime "github.com/go-openapi/runtime/client"
16 "github.com/go-openapi/strfmt"
Anssi Mannila500878a2020-09-30 08:57:04 +030017 "github.com/jedib0t/go-pretty/table"
Anssi Mannila00894a42020-10-19 11:36:26 +030018 "github.com/prometheus/alertmanager/api/v2/client"
19 "github.com/prometheus/alertmanager/api/v2/client/alert"
Mohamed Abukare92822c2020-10-30 14:48:08 +020020 "github.com/prometheus/alertmanager/api/v2/models"
Mohamed Abukar105030f2020-10-22 18:08:34 +030021 "github.com/thatisuday/commando"
Mohamed Abukar0c389732020-09-17 14:47:50 +030022)
23
vipin54a3a4f2020-09-23 12:19:58 +000024type CliAlarmDefinitions struct {
vipin4cedd502020-09-25 05:58:31 +000025 AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"`
vipin54a3a4f2020-09-23 12:19:58 +000026}
27
Anssi Mannila500878a2020-09-30 08:57:04 +030028type AlarmClient struct {
29 alarmer *alarm.RICAlarm
30}
31
vipin6f73fa32020-10-06 06:51:53 +000032type RicPerfAlarmObjects struct {
33 AlarmObjects []*alarm.Alarm `json:"alarmobjects"`
34}
35
Mohamed Abukar105030f2020-10-22 18:08:34 +030036type AlarmNotification struct {
37 alarm.AlarmMessage
38 alarm.AlarmDefinition
39}
40
vipin6f73fa32020-10-06 06:51:53 +000041var CLIPerfAlarmObjects map[int]*alarm.Alarm
42
43var wg sync.WaitGroup
44
45var CliPerfAlarmDefinitions CliAlarmDefinitions
46
47const (
48 Raise string = "RAISE"
49 Clear string = "CLEAR"
50 End string = "END"
51 PeakTestDuration int = 60
52 OneSecondDuration int = 1
53)
54
Mohamed Abukar0c389732020-09-17 14:47:50 +030055func main() {
Mohamed Abukar105030f2020-10-22 18:08:34 +030056 alarmManagerHost := "localhost"
57 if h := os.Getenv("ALARM_MANAGER_HOST"); h != "" {
58 alarmManagerHost = h
59 }
60
61 alertManagerHost := "localhost"
62 if h := os.Getenv("ALERT_MANAGER_HOST"); h != "" {
63 alertManagerHost = h
64 }
Mohamed Abukar0c389732020-09-17 14:47:50 +030065
66 // configure commando
67 commando.
68 SetExecutableName("alarm-cli").
69 SetVersion("1.0.0").
70 SetDescription("This CLI tool provides management interface to SEP alarm system")
71
Mohamed Abukar2336a842020-10-30 16:19:38 +020072 registerActiveCmd(alarmManagerHost)
73 registerHistoryCmd(alarmManagerHost)
74 registerRaiseCmd(alarmManagerHost)
75 registerClearCmd(alarmManagerHost)
76 registerDefineCmd(alarmManagerHost)
77 registerUndefineCmd(alarmManagerHost)
78 registerConfigureCmd(alarmManagerHost)
79 registerPerfCmd(alarmManagerHost)
80 registerAlertCmd(alertManagerHost)
81
82 // parse command-line arguments
83 commando.Parse(nil)
84}
85
86func registerActiveCmd(alarmManagerHost string) {
Mohamed Abukar0c389732020-09-17 14:47:50 +030087 commando.
88 Register("active").
89 SetShortDescription("Displays the SEP active alarms").
90 SetDescription("This command displays more information about the SEP active alarms").
Mohamed Abukar105030f2020-10-22 18:08:34 +030091 AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost).
Mohamed Abukar0c389732020-09-17 14:47:50 +030092 AddFlag("port", "Alarm manager host address", commando.String, "8080").
93 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
94 displayAlarms(getAlarms(flags, "active"), false)
95 })
Mohamed Abukar2336a842020-10-30 16:19:38 +020096}
Mohamed Abukar0c389732020-09-17 14:47:50 +030097
Mohamed Abukar2336a842020-10-30 16:19:38 +020098func registerHistoryCmd(alarmManagerHost string) {
Mohamed Abukar0c389732020-09-17 14:47:50 +030099 // Get alarm history
100 commando.
101 Register("history").
102 SetShortDescription("Displays the SEP alarm history").
103 SetDescription("This command displays more information about the SEP alarm history").
Mohamed Abukar105030f2020-10-22 18:08:34 +0300104 AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost).
Mohamed Abukar0c389732020-09-17 14:47:50 +0300105 AddFlag("port", "Alarm manager host address", commando.String, "8080").
106 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
107 displayAlarms(getAlarms(flags, "history"), true)
108 })
Mohamed Abukar2336a842020-10-30 16:19:38 +0200109}
Mohamed Abukar0c389732020-09-17 14:47:50 +0300110
Mohamed Abukar2336a842020-10-30 16:19:38 +0200111func registerRaiseCmd(alarmManagerHost string) {
Mohamed Abukar0c389732020-09-17 14:47:50 +0300112 // Raise an alarm
113 commando.
114 Register("raise").
115 SetShortDescription("Raises alarm with given parameters").
116 AddFlag("moid", "Managed object Id", commando.String, nil).
117 AddFlag("apid", "Application Id", commando.String, nil).
118 AddFlag("sp", "Specific problem Id", commando.Int, nil).
119 AddFlag("severity", "Perceived severity", commando.String, nil).
120 AddFlag("iinfo", "Application identifying info", commando.String, nil).
121 AddFlag("aai", "Application additional info", commando.String, "-").
Mohamed Abukar105030f2020-10-22 18:08:34 +0300122 AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost).
Mohamed Abukar0c389732020-09-17 14:47:50 +0300123 AddFlag("port", "Alarm manager host address", commando.String, "8080").
Anssi Mannila500878a2020-09-30 08:57:04 +0300124 AddFlag("if", "http or rmr used as interface", commando.String, "http").
Mohamed Abukar0c389732020-09-17 14:47:50 +0300125 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
vipin6f73fa32020-10-06 06:51:53 +0000126 postAlarm(flags, readAlarmParams(flags, false), alarm.AlarmActionRaise, nil)
Mohamed Abukar0c389732020-09-17 14:47:50 +0300127 })
128
Mohamed Abukar2336a842020-10-30 16:19:38 +0200129}
130
131func registerClearCmd(alarmManagerHost string) {
Mohamed Abukar0c389732020-09-17 14:47:50 +0300132 // Clear an alarm
133 commando.
134 Register("clear").
135 SetShortDescription("Raises alarm with given parameters").
136 AddFlag("moid", "Managed object Id", commando.String, nil).
137 AddFlag("apid", "Application Id", commando.String, nil).
138 AddFlag("sp", "Specific problem Id", commando.Int, nil).
139 AddFlag("iinfo", "Application identifying info", commando.String, nil).
Mohamed Abukar105030f2020-10-22 18:08:34 +0300140 AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost).
Mohamed Abukar0c389732020-09-17 14:47:50 +0300141 AddFlag("port", "Alarm manager host address", commando.String, "8080").
Anssi Mannila500878a2020-09-30 08:57:04 +0300142 AddFlag("if", "http or rmr used as interface", commando.String, "http").
Mohamed Abukar0c389732020-09-17 14:47:50 +0300143 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
vipin6f73fa32020-10-06 06:51:53 +0000144 postAlarm(flags, readAlarmParams(flags, true), alarm.AlarmActionClear, nil)
Mohamed Abukar0c389732020-09-17 14:47:50 +0300145 })
146
Mohamed Abukar2336a842020-10-30 16:19:38 +0200147}
148
149func registerConfigureCmd(alarmManagerHost string) {
vipin541eb502020-09-22 12:04:59 +0000150 // Configure an alarm manager
151 commando.
152 Register("configure").
153 SetShortDescription("Configure alarm manager with given parameters").
154 AddFlag("mal", "max active alarms", commando.Int, nil).
155 AddFlag("mah", "max alarm history", commando.Int, nil).
Mohamed Abukar105030f2020-10-22 18:08:34 +0300156 AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost).
vipin541eb502020-09-22 12:04:59 +0000157 AddFlag("port", "Alarm manager host address", commando.String, "8080").
158 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
159 postAlarmConfig(flags)
160 })
Mohamed Abukar2336a842020-10-30 16:19:38 +0200161}
162
163func registerDefineCmd(alarmManagerHost string) {
Anssi Mannila500878a2020-09-30 08:57:04 +0300164 // Create alarm definition
vipin54a3a4f2020-09-23 12:19:58 +0000165 commando.
166 Register("define").
vipin4cedd502020-09-25 05:58:31 +0000167 SetShortDescription("Define alarm with given parameters").
168 AddFlag("aid", "alarm identifier", commando.Int, nil).
169 AddFlag("atx", "alarm text", commando.String, nil).
170 AddFlag("ety", "event type", commando.String, nil).
171 AddFlag("oin", "operation instructions", commando.String, nil).
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200172 AddFlag("rad", "Raise alarm delay", commando.Int, 0).
173 AddFlag("cad", "Clear alarm delay", commando.Int, 0).
Mohamed Abukar105030f2020-10-22 18:08:34 +0300174 AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost).
vipin54a3a4f2020-09-23 12:19:58 +0000175 AddFlag("port", "Alarm manager host address", commando.String, "8080").
vipin4cedd502020-09-25 05:58:31 +0000176 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
177 postAlarmDefinition(flags)
178 })
Mohamed Abukar2336a842020-10-30 16:19:38 +0200179}
180
181func registerUndefineCmd(alarmManagerHost string) {
182 // Delete alarm definition
vipin4cedd502020-09-25 05:58:31 +0000183 commando.
184 Register("undefine").
185 SetShortDescription("Define alarm with given parameters").
186 AddFlag("aid", "alarm identifier", commando.Int, nil).
Mohamed Abukar105030f2020-10-22 18:08:34 +0300187 AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost).
vipin4cedd502020-09-25 05:58:31 +0000188 AddFlag("port", "Alarm manager host address", commando.String, "8080").
189 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
190 deleteAlarmDefinition(flags)
191 })
Mohamed Abukar2336a842020-10-30 16:19:38 +0200192}
193
194func registerPerfCmd(alarmManagerHost string) {
195 // Conduct performance test for alarm-go
vipin6f73fa32020-10-06 06:51:53 +0000196 commando.
197 Register("perf").
198 SetShortDescription("Conduct performance test with given parameters").
199 AddFlag("prf", "performance profile id", commando.Int, nil).
200 AddFlag("nal", "number of alarms", commando.Int, nil).
201 AddFlag("aps", "alarms per sec", commando.Int, nil).
202 AddFlag("tim", "total time of test", commando.Int, nil).
Mohamed Abukar105030f2020-10-22 18:08:34 +0300203 AddFlag("host", "Alarm manager host address", commando.String, alarmManagerHost).
vipin6f73fa32020-10-06 06:51:53 +0000204 AddFlag("port", "Alarm manager host address", commando.String, "8080").
205 AddFlag("if", "http or rmr used as interface", commando.String, "http").
206 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
207 conductperformancetest(flags)
208 })
Mohamed Abukar2336a842020-10-30 16:19:38 +0200209}
vipin541eb502020-09-22 12:04:59 +0000210
Mohamed Abukar2336a842020-10-30 16:19:38 +0200211func registerAlertCmd(alertManagerHost string) {
Anssi Mannila00894a42020-10-19 11:36:26 +0300212 // Get alerts from Prometheus Alert Manager
213 commando.
Mohamed Abukar2336a842020-10-30 16:19:38 +0200214 Register("alerts").
Anssi Mannila00894a42020-10-19 11:36:26 +0300215 SetShortDescription("Get alerts from Prometheus Alert Manager").
216 AddFlag("active", "Active alerts in Prometheus Alert Manager", commando.Bool, true).
217 AddFlag("inhibited", "Inhibited alerts in Prometheus Alert Manager", commando.Bool, true).
218 AddFlag("silenced", "Silenced alerts in Prometheus Alert Manager", commando.Bool, true).
219 AddFlag("unprocessed", "Unprocessed alerts in Prometheus Alert Manager", commando.Bool, true).
Mohamed Abukar105030f2020-10-22 18:08:34 +0300220 AddFlag("host", "Prometheus Alert Manager host address", commando.String, alertManagerHost).
Anssi Mannila00894a42020-10-19 11:36:26 +0300221 AddFlag("port", "Prometheus Alert Manager port", commando.String, "9093").
222 SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
223 displayAlerts(flags)
224 })
Mohamed Abukar0c389732020-09-17 14:47:50 +0300225}
226
227func readAlarmParams(flags map[string]commando.FlagValue, clear bool) (a alarm.Alarm) {
228 a.ManagedObjectId, _ = flags["moid"].GetString()
229 a.ApplicationId, _ = flags["apid"].GetString()
230 a.SpecificProblem, _ = flags["sp"].GetInt()
231 a.IdentifyingInfo, _ = flags["iinfo"].GetString()
232
233 if !clear {
234 s, _ := flags["severity"].GetString()
235 a.PerceivedSeverity = alarm.Severity(s)
236 }
237
238 if !clear {
239 a.AdditionalInfo, _ = flags["aai"].GetString()
240 }
Mohamed Abukar0c389732020-09-17 14:47:50 +0300241 return
242}
243
Mohamed Abukar105030f2020-10-22 18:08:34 +0300244func getAlarms(flags map[string]commando.FlagValue, action alarm.AlarmAction) (alarms []AlarmNotification) {
Mohamed Abukar0c389732020-09-17 14:47:50 +0300245 host, _ := flags["host"].GetString()
246 port, _ := flags["port"].GetString()
247 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/%s", host, port, action)
248 resp, err := http.Get(targetUrl)
249 if err != nil || resp == nil || resp.Body == nil {
vipin6f73fa32020-10-06 06:51:53 +0000250 fmt.Println("Couldn't fetch active alarm list due to error: ", err)
Mohamed Abukar0c389732020-09-17 14:47:50 +0300251 return alarms
252 }
253
254 defer resp.Body.Close()
255
256 body, err := ioutil.ReadAll(resp.Body)
257 if err != nil {
vipin6f73fa32020-10-06 06:51:53 +0000258 fmt.Println("ioutil.ReadAll failed: ", err)
Mohamed Abukar0c389732020-09-17 14:47:50 +0300259 return alarms
260 }
261
262 json.Unmarshal([]byte(body), &alarms)
Mohamed Abukar0c389732020-09-17 14:47:50 +0300263 return alarms
264}
265
vipin6f73fa32020-10-06 06:51:53 +0000266func postAlarmWithRmrIf(a alarm.Alarm, action alarm.AlarmAction, alarmClient *AlarmClient) {
267 if alarmClient == nil {
268 alarmClient = NewAlarmClient("my-pod", "my-app")
269 }
270 if alarmClient == nil {
Anssi Mannila500878a2020-09-30 08:57:04 +0300271 return
272 }
273
vipin6f73fa32020-10-06 06:51:53 +0000274 // Wait until RMR is up-and-running
275 for !alarmClient.alarmer.IsRMRReady() {
276 time.Sleep(100 * time.Millisecond)
277 }
Mohamed Abukar0c389732020-09-17 14:47:50 +0300278
vipin6f73fa32020-10-06 06:51:53 +0000279 if action == alarm.AlarmActionRaise {
280 alarmClient.alarmer.Raise(a)
281 }
282
283 if action == alarm.AlarmActionClear {
284 alarmClient.alarmer.Clear(a)
285 }
286 return
287}
288
289func postAlarmWithHttpIf(targetUrl string, a alarm.Alarm, action alarm.AlarmAction) {
Mohamed Abukar0c389732020-09-17 14:47:50 +0300290 m := alarm.AlarmMessage{Alarm: a, AlarmAction: action}
291 jsonData, err := json.Marshal(m)
292 if err != nil {
vipin6f73fa32020-10-06 06:51:53 +0000293 fmt.Println("json.Marshal failed: ", err)
Mohamed Abukar0c389732020-09-17 14:47:50 +0300294 return
295 }
296
297 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
298 if err != nil || resp == nil {
vipin6f73fa32020-10-06 06:51:53 +0000299 fmt.Println("Couldn't fetch active alarm list due to error: ", err)
Mohamed Abukar0c389732020-09-17 14:47:50 +0300300 return
301 }
302}
303
vipin6f73fa32020-10-06 06:51:53 +0000304func postAlarm(flags map[string]commando.FlagValue, a alarm.Alarm, action alarm.AlarmAction, alarmClient *AlarmClient) {
305 // Check the interface to be used for raise or clear the alarm
306 rmr_or_http, _ := flags["if"].GetString()
307 if rmr_or_http == "rmr" {
308 postAlarmWithRmrIf(a, action, alarmClient)
309 } else {
310
311 host, _ := flags["host"].GetString()
312 port, _ := flags["port"].GetString()
313 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms", host, port)
314 postAlarmWithHttpIf(targetUrl, a, action)
315 }
Mohamed Abukar105030f2020-10-22 18:08:34 +0300316 fmt.Println("command executed successfully!")
vipin6f73fa32020-10-06 06:51:53 +0000317}
318
Mohamed Abukar105030f2020-10-22 18:08:34 +0300319func displayAlarms(alarms []AlarmNotification, isHistory bool) {
Mohamed Abukar0c389732020-09-17 14:47:50 +0300320 t := table.NewWriter()
321 t.SetOutputMirror(os.Stdout)
322 if isHistory {
Mohamed Abukar105030f2020-10-22 18:08:34 +0300323 t.AppendHeader(table.Row{"ID", "SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "ACTION", "TIME"})
Mohamed Abukar0c389732020-09-17 14:47:50 +0300324 } else {
Mohamed Abukar105030f2020-10-22 18:08:34 +0300325 t.AppendHeader(table.Row{"ID", "SP", "MOID", "APPID", "IINFO", "SEVERITY", "AAI", "TIME"})
Mohamed Abukar0c389732020-09-17 14:47:50 +0300326 }
327
328 for _, a := range alarms {
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200329 // Do not show alarm before raiseDelay has elapsed
Mohamed Abukar0c389732020-09-17 14:47:50 +0300330 alarmTime := time.Unix(0, a.AlarmTime).Format("02/01/2006, 15:04:05")
331 if isHistory {
332 t.AppendRows([]table.Row{
Mohamed Abukar105030f2020-10-22 18:08:34 +0300333 {a.AlarmId, a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, a.AlarmAction, alarmTime},
Mohamed Abukar0c389732020-09-17 14:47:50 +0300334 })
335 } else {
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200336 if a.AlarmDefinition.RaiseDelay == 0 {
337 t.AppendRows([]table.Row{
338 {a.AlarmId, a.SpecificProblem, a.ManagedObjectId, a.ApplicationId, a.IdentifyingInfo, a.PerceivedSeverity, a.AdditionalInfo, alarmTime},
339 })
340 }
Mohamed Abukar0c389732020-09-17 14:47:50 +0300341 }
342 }
343
344 t.SetStyle(table.StyleColoredBright)
345 t.Render()
346}
vipin541eb502020-09-22 12:04:59 +0000347
348func postAlarmConfig(flags map[string]commando.FlagValue) {
349 host, _ := flags["host"].GetString()
350 port, _ := flags["port"].GetString()
351 maxactivealarms, _ := flags["mal"].GetInt()
352 maxalarmhistory, _ := flags["mah"].GetInt()
353 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/config", host, port)
354
355 m := alarm.AlarmConfigParams{MaxActiveAlarms: maxactivealarms, MaxAlarmHistory: maxalarmhistory}
356 jsonData, err := json.Marshal(m)
357 if err != nil {
vipin6f73fa32020-10-06 06:51:53 +0000358 fmt.Println("json.Marshal failed: ", err)
vipin541eb502020-09-22 12:04:59 +0000359 return
360 }
361
362 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
363 if err != nil || resp == nil {
vipin6f73fa32020-10-06 06:51:53 +0000364 fmt.Println("Couldn't fetch post alarm configuration due to error: ", err)
vipin541eb502020-09-22 12:04:59 +0000365 return
366 }
367}
vipin54a3a4f2020-09-23 12:19:58 +0000368
369func postAlarmDefinition(flags map[string]commando.FlagValue) {
vipin4cedd502020-09-25 05:58:31 +0000370 host, _ := flags["host"].GetString()
371 port, _ := flags["port"].GetString()
372 alarmid, _ := flags["aid"].GetInt()
373 alarmtxt, _ := flags["atx"].GetString()
374 etype, _ := flags["ety"].GetString()
375 operation, _ := flags["oin"].GetString()
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200376 raiseDelay, _ := flags["rad"].GetInt()
377 clearDelay, _ := flags["cad"].GetInt()
378
vipin4cedd502020-09-25 05:58:31 +0000379 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define", host, port)
vipin54a3a4f2020-09-23 12:19:58 +0000380
381 var alarmdefinition alarm.AlarmDefinition
vipin4cedd502020-09-25 05:58:31 +0000382 alarmdefinition.AlarmId = alarmid
383 alarmdefinition.AlarmText = alarmtxt
384 alarmdefinition.EventType = etype
385 alarmdefinition.OperationInstructions = operation
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200386 alarmdefinition.RaiseDelay = raiseDelay
387 alarmdefinition.ClearDelay = clearDelay
vipin54a3a4f2020-09-23 12:19:58 +0000388
389 m := CliAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarmdefinition}}
vipin4cedd502020-09-25 05:58:31 +0000390 jsonData, err := json.Marshal(m)
391 if err != nil {
vipin6f73fa32020-10-06 06:51:53 +0000392 fmt.Println("json.Marshal failed: ", err)
vipin4cedd502020-09-25 05:58:31 +0000393 return
394 }
vipin54a3a4f2020-09-23 12:19:58 +0000395
vipin4cedd502020-09-25 05:58:31 +0000396 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
397 if err != nil || resp == nil {
vipin6f73fa32020-10-06 06:51:53 +0000398 fmt.Println("Couldn't post alarm definition due to error: ", err)
vipin4cedd502020-09-25 05:58:31 +0000399 return
400 }
401}
402
403func deleteAlarmDefinition(flags map[string]commando.FlagValue) {
404 host, _ := flags["host"].GetString()
405 port, _ := flags["port"].GetString()
406 alarmid, _ := flags["aid"].GetInt()
407 salarmid := strconv.FormatUint(uint64(alarmid), 10)
408 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define/%s", host, port, salarmid)
409
410 client := &http.Client{}
411 req, err := http.NewRequest("DELETE", targetUrl, nil)
412 if err != nil || req == nil {
vipin6f73fa32020-10-06 06:51:53 +0000413 fmt.Println("Couldn't make delete request due to error: ", err)
vipin4cedd502020-09-25 05:58:31 +0000414 return
415 }
416 resp, errr := client.Do(req)
417 if errr != nil || resp == nil {
vipin6f73fa32020-10-06 06:51:53 +0000418 fmt.Println("Couldn't send delete request due to error: ", err)
vipin4cedd502020-09-25 05:58:31 +0000419 return
420 }
vipin54a3a4f2020-09-23 12:19:58 +0000421}
Anssi Mannila500878a2020-09-30 08:57:04 +0300422
423// NewAlarmClient returns a new AlarmClient.
424func NewAlarmClient(moId, appId string) *AlarmClient {
425 alarmInstance, err := alarm.InitAlarm(moId, appId)
426 if err == nil {
427 return &AlarmClient{
428 alarmer: alarmInstance,
429 }
430 }
431 fmt.Println("Failed to create alarmInstance", err)
432 return nil
433}
vipin6f73fa32020-10-06 06:51:53 +0000434
435// Conduct performance testing
436func conductperformancetest(flags map[string]commando.FlagValue) {
437 var readerror error
438 var senderror error
439 var readobjerror error
440 host, _ := flags["host"].GetString()
441 port, _ := flags["port"].GetString()
442 targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define", host, port)
443 readerror = readPerfAlarmDefinitionFromJson()
444 if readerror == nil {
445 senderror = sendPerfAlarmDefinitionToAlarmManager(targetUrl)
446 if senderror == nil {
447 fmt.Println("sent performance alarm definitions to alarm manager")
448 CLIPerfAlarmObjects = make(map[int]*alarm.Alarm)
449 readobjerror = readPerfAlarmObjectFromJson()
450 if readobjerror == nil {
451 profile, _ := flags["prf"].GetInt()
452 if profile == 1 {
453 fmt.Println("starting peak performance test")
454 peakPerformanceTest(flags)
455 } else if profile == 2 {
456 fmt.Println("starting endurance test")
457 enduranceTest(flags)
458 } else {
459 fmt.Println("Unknown profile, received profile = ", profile)
460 }
461 } else {
462 fmt.Println("reading performance alarm objects from json file failed ")
463 }
464 } else {
465 fmt.Println("sending performance alarm definitions to alarm manager failed ")
466 }
467
468 } else {
469 fmt.Println("reading performance alarm definitions from json file failed ")
470 }
vipin6f73fa32020-10-06 06:51:53 +0000471}
472
473func peakPerformanceTest(flags map[string]commando.FlagValue) {
474 nalarms, _ := flags["nal"].GetInt()
475 var count int = 0
476 for aid, obj := range CLIPerfAlarmObjects {
477 count = count + 1
478 if count <= nalarms {
479 fmt.Println("peakPerformanceTest: invoking worker routine ", count, aid, *obj)
480 wg.Add(1)
481 go raiseClearAlarmOnce(obj, flags)
482 } else {
483 break
484 }
485 }
486 fmt.Println("peakPerformanceTest: Waiting for workers to finish")
487 wg.Wait()
488 fmt.Println("peakPerformanceTest: Wait completed")
489}
490
491func enduranceTest(flags map[string]commando.FlagValue) {
492 alarmspersec, _ := flags["aps"].GetInt()
493 var count int = 0
494 for aid, obj := range CLIPerfAlarmObjects {
495 count = count + 1
496 if count <= alarmspersec {
497 fmt.Println("enduranceTest: invoking worker routine ", count, aid, *obj)
498 wg.Add(1)
499 go raiseClearAlarmOverPeriod(obj, flags)
500 } else {
501 break
502 }
503 }
504 fmt.Println("enduranceTest: Waiting for workers to finish")
505 wg.Wait()
506 fmt.Println("enduranceTest: Wait completed")
507}
508
509func readPerfAlarmObjectFromJson() error {
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200510
vipin6f73fa32020-10-06 06:51:53 +0000511 filename := os.Getenv("PERF_OBJ_FILE")
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200512 fmt.Printf("readPerfAlarmObjectFromJson: filename = %s\n", filename)
513
vipin6f73fa32020-10-06 06:51:53 +0000514 file, err := ioutil.ReadFile(filename)
515 if err == nil {
516 data := RicPerfAlarmObjects{}
517 err = json.Unmarshal([]byte(file), &data)
518 if err == nil {
519 for _, alarmObject := range data.AlarmObjects {
520 ricAlarmObject := new(alarm.Alarm)
521 ricAlarmObject.ManagedObjectId = alarmObject.ManagedObjectId
522 ricAlarmObject.ApplicationId = alarmObject.ApplicationId
523 ricAlarmObject.SpecificProblem = alarmObject.SpecificProblem
524 ricAlarmObject.PerceivedSeverity = alarmObject.PerceivedSeverity
525 ricAlarmObject.AdditionalInfo = alarmObject.AdditionalInfo
526 ricAlarmObject.IdentifyingInfo = alarmObject.IdentifyingInfo
527 CLIPerfAlarmObjects[alarmObject.SpecificProblem] = ricAlarmObject
528 }
529 } else {
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200530 fmt.Println("readPerfAlarmObjectFromJson: json.Unmarshal failed with error: ", err)
vipin6f73fa32020-10-06 06:51:53 +0000531 return err
532 }
533 } else {
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200534 fmt.Printf("readPerfAlarmObjectFromJson: ioutil.ReadFile failed with error: %v, filename: %s\n", err, filename)
535 fmt.Printf("readPerfAlarmObjectFromJson: current directory: %s\n", getCurrentDirectory())
vipin6f73fa32020-10-06 06:51:53 +0000536 return err
537 }
538 return nil
539}
540
541func readPerfAlarmDefinitionFromJson() error {
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200542
vipin6f73fa32020-10-06 06:51:53 +0000543 filename := os.Getenv("PERF_DEF_FILE")
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200544 fmt.Printf("ReadPerfAlarmDefinitionFromJson: filename = %s\n", filename)
545
vipin6f73fa32020-10-06 06:51:53 +0000546 file, err := ioutil.ReadFile(filename)
547 if err == nil {
548 data := CliAlarmDefinitions{}
549 err = json.Unmarshal([]byte(file), &data)
550 if err == nil {
551 for _, alarmDefinition := range data.AlarmDefinitions {
552 _, exists := alarm.RICAlarmDefinitions[alarmDefinition.AlarmId]
553 if exists {
554 fmt.Println("ReadPerfAlarmDefinitionFromJson: alarm definition already exists for ", alarmDefinition.AlarmId)
555 } else {
556 fmt.Println("ReadPerfAlarmDefinitionFromJson: alarm ", alarmDefinition.AlarmId)
557 ricAlarmDefintion := new(alarm.AlarmDefinition)
558 ricAlarmDefintion.AlarmId = alarmDefinition.AlarmId
559 ricAlarmDefintion.AlarmText = alarmDefinition.AlarmText
560 ricAlarmDefintion.EventType = alarmDefinition.EventType
561 ricAlarmDefintion.OperationInstructions = alarmDefinition.OperationInstructions
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200562 ricAlarmDefintion.RaiseDelay = alarmDefinition.RaiseDelay
563 ricAlarmDefintion.ClearDelay = alarmDefinition.ClearDelay
vipin6f73fa32020-10-06 06:51:53 +0000564 CliPerfAlarmDefinitions.AlarmDefinitions = append(CliPerfAlarmDefinitions.AlarmDefinitions, ricAlarmDefintion)
565 }
566 }
567 } else {
568 fmt.Println("ReadPerfAlarmDefinitionFromJson: json.Unmarshal failed with error: ", err)
569 return err
570 }
571 } else {
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200572 fmt.Printf("ReadPerfAlarmDefinitionFromJson: ioutil.ReadFile failed with error: %v, filename: %s\n", err, filename)
573 fmt.Printf("ReadPerfAlarmDefinitionFromJson: current directory: %s\n", getCurrentDirectory())
Mohamed Abukar2336a842020-10-30 16:19:38 +0200574
vipin6f73fa32020-10-06 06:51:53 +0000575 return err
576 }
577 return nil
578}
579
580func sendPerfAlarmDefinitionToAlarmManager(targetUrl string) error {
581
582 jsonData, err := json.Marshal(CliPerfAlarmDefinitions)
583 if err != nil {
584 fmt.Println("sendPerfAlarmDefinitionToAlarmManager: json.Marshal failed: ", err)
585 return err
586 }
587
588 resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
589 if err != nil || resp == nil {
590 fmt.Println("sendPerfAlarmDefinitionToAlarmManager: Couldn't post alarm definition to targeturl due to error: ", targetUrl, err)
591 return err
592 }
593 return nil
594}
595
596func wakeUpAfterTime(timeinseconds int, chn chan string, action string) {
597 time.Sleep(time.Second * time.Duration(timeinseconds))
598 chn <- action
599}
600
601func raiseClearAlarmOnce(alarmobject *alarm.Alarm, flags map[string]commando.FlagValue) {
602 var alarmClient *AlarmClient = nil
603 defer wg.Done()
604 chn := make(chan string, 1)
605 rmr_or_http, _ := flags["if"].GetString()
606 if rmr_or_http == "rmr" {
607 alarmClient = NewAlarmClient("my-pod", "my-app")
608 }
609 postAlarm(flags, *alarmobject, alarm.AlarmActionRaise, alarmClient)
610 go wakeUpAfterTime(PeakTestDuration, chn, Clear)
611 select {
612 case res := <-chn:
613 if res == Clear {
614 postAlarm(flags, *alarmobject, alarm.AlarmActionClear, alarmClient)
615 go wakeUpAfterTime(PeakTestDuration, chn, End)
616 } else if res == End {
617 return
618 }
619 }
620}
621
622func raiseClearAlarmOverPeriod(alarmobject *alarm.Alarm, flags map[string]commando.FlagValue) {
623 var alarmClient *AlarmClient = nil
624 defer wg.Done()
625 timeinminutes, _ := flags["tim"].GetInt()
626 timeinseconds := timeinminutes * 60
627 chn := make(chan string, 1)
628 rmr_or_http, _ := flags["if"].GetString()
629 if rmr_or_http == "rmr" {
630 alarmClient = NewAlarmClient("my-pod", "my-app")
631 }
632 postAlarm(flags, *alarmobject, alarm.AlarmActionRaise, alarmClient)
633 go wakeUpAfterTime(OneSecondDuration, chn, Clear)
634 go wakeUpAfterTime(timeinseconds, chn, End)
635 for {
636 select {
637 case res := <-chn:
638 if res == Raise {
639 postAlarm(flags, *alarmobject, alarm.AlarmActionRaise, alarmClient)
640 go wakeUpAfterTime(OneSecondDuration, chn, Clear)
641 } else if res == Clear {
642 postAlarm(flags, *alarmobject, alarm.AlarmActionClear, alarmClient)
643 go wakeUpAfterTime(OneSecondDuration, chn, Raise)
644 } else if res == End {
645 return
646 }
647 }
648 }
649}
Anssi Mannila00894a42020-10-19 11:36:26 +0300650
vipinba2ef5b2020-11-06 11:24:48 +0000651func dispalyAlertAnnotations(t table.Writer, gettableAlert *models.GettableAlert) {
652 var annotationmap map[string]string
653 annotationmap = make(map[string]string)
654 for key, item := range gettableAlert.Annotations {
655 annotationmap[key] = item
656 }
657 t.AppendRow([]interface{}{"alarm_id", annotationmap["alarm_id"]})
658 t.AppendRow([]interface{}{"specific_problem", annotationmap["specific_problem"]})
659 t.AppendRow([]interface{}{"timestamp", annotationmap["timestamp"]})
660 t.AppendRow([]interface{}{"event_type", annotationmap["event_type"]})
661 t.AppendRow([]interface{}{"description", annotationmap["description"]})
662 t.AppendRow([]interface{}{"additional_info", annotationmap["additional_info"]})
663 t.AppendRow([]interface{}{"identifying_info", annotationmap["identifying_info"]})
664 t.AppendRow([]interface{}{"instructions", annotationmap["instructions"]})
665}
666
667func displayAlertLabels(t table.Writer, gettableAlert *models.GettableAlert) {
668 var labelmap map[string]string
669 labelmap = make(map[string]string)
670 for key, item := range gettableAlert.Alert.Labels {
671 labelmap[key] = item
672 }
673 t.AppendRow([]interface{}{"alertname", labelmap["alertname"]})
674 t.AppendRow([]interface{}{"status", labelmap["status"]})
675 t.AppendRow([]interface{}{"severity", labelmap["severity"]})
676 t.AppendRow([]interface{}{"system_name", labelmap["system_name"]})
677 t.AppendRow([]interface{}{"service", labelmap["service"]})
678}
679
Mohamed Abukare92822c2020-10-30 14:48:08 +0200680func displaySingleAlert(t table.Writer, gettableAlert *models.GettableAlert) {
681 t.AppendRow([]interface{}{"------------------------------------"})
vipinba2ef5b2020-11-06 11:24:48 +0000682 dispalyAlertAnnotations(t, gettableAlert)
Mohamed Abukare92822c2020-10-30 14:48:08 +0200683 if gettableAlert.EndsAt != nil {
684 t.AppendRow([]interface{}{"EndsAt", *gettableAlert.EndsAt})
685 }
686 if gettableAlert.Fingerprint != nil {
687 t.AppendRow([]interface{}{"Fingerprint", *gettableAlert.Fingerprint})
688 }
689 for key, item := range gettableAlert.Receivers {
690 if gettableAlert.Receivers != nil {
691 t.AppendRow([]interface{}{key, *item.Name})
692 }
693 }
694 if gettableAlert.StartsAt != nil {
695 t.AppendRow([]interface{}{"StartsAt", *gettableAlert.StartsAt})
696 }
697 if gettableAlert.Status != nil {
698 t.AppendRow([]interface{}{"InhibitedBy", gettableAlert.Status.InhibitedBy})
699 t.AppendRow([]interface{}{"SilencedBy", gettableAlert.Status.SilencedBy})
700 t.AppendRow([]interface{}{"State", *gettableAlert.Status.State})
701 }
702 if gettableAlert.UpdatedAt != nil {
703 t.AppendRow([]interface{}{"UpdatedAt", *gettableAlert.UpdatedAt})
704 }
705 t.AppendRow([]interface{}{"GeneratorURL", gettableAlert.Alert.GeneratorURL})
vipinba2ef5b2020-11-06 11:24:48 +0000706 displayAlertLabels(t, gettableAlert)
Mohamed Abukare92822c2020-10-30 14:48:08 +0200707}
708
Anssi Mannila00894a42020-10-19 11:36:26 +0300709func displayAlerts(flags map[string]commando.FlagValue) {
710 resp, err := getAlerts(flags)
711 if err != nil {
712 fmt.Println(err)
713 return
714 }
715
716 if resp == nil {
717 fmt.Println("resp= nil")
718 return
719 }
720
721 t := table.NewWriter()
722 t.SetOutputMirror(os.Stdout)
723 t.AppendHeader(table.Row{"Alerts from Prometheus Alert Manager"})
Mohamed Abukar105030f2020-10-22 18:08:34 +0300724 for _, gettableAlert := range resp.Payload {
Mohamed Abukare92822c2020-10-30 14:48:08 +0200725 if gettableAlert == nil {
726 continue
Anssi Mannila00894a42020-10-19 11:36:26 +0300727 }
Mohamed Abukare92822c2020-10-30 14:48:08 +0200728
729 displaySingleAlert(t, gettableAlert)
Anssi Mannila00894a42020-10-19 11:36:26 +0300730 }
731 t.SetStyle(table.StyleColoredBright)
732 t.Render()
733}
Mohamed Abukar105030f2020-10-22 18:08:34 +0300734
Anssi Mannila00894a42020-10-19 11:36:26 +0300735func getAlerts(flags map[string]commando.FlagValue) (*alert.GetAlertsOK, error) {
736 active, _ := flags["active"].GetBool()
737 inhibited, _ := flags["inhibited"].GetBool()
738 silenced, _ := flags["silenced"].GetBool()
739 unprocessed, _ := flags["unprocessed"].GetBool()
740 amHost, _ := flags["host"].GetString()
741 amPort, _ := flags["port"].GetString()
vipinba2ef5b2020-11-06 11:24:48 +0000742 amAddress := amHost + ":" + amPort
743 amBaseUrl := "api/v2"
744 amSchemes := []string{"http"}
Anssi Mannila00894a42020-10-19 11:36:26 +0300745
746 alertParams := alert.NewGetAlertsParams()
747 alertParams.Active = &active
748 alertParams.Inhibited = &inhibited
749 alertParams.Silenced = &silenced
750 alertParams.Unprocessed = &unprocessed
Anssi Mannila00894a42020-10-19 11:36:26 +0300751 resp, err := newAlertManagerClient(amAddress, amBaseUrl, amSchemes).Alert.GetAlerts(alertParams)
752 if err != nil {
vipinba2ef5b2020-11-06 11:24:48 +0000753 err = fmt.Errorf("GetAlerts from amAddress = %s with amBaseUrl = %s failed with error: %v", amAddress, amBaseUrl, err)
Anssi Mannila00894a42020-10-19 11:36:26 +0300754 }
755 return resp, err
756}
757
Juha Hyttinen5f9b88b2023-06-02 10:04:15 +0300758func newAlertManagerClient(amAddress string, amBaseUrl string, amSchemes []string) *client.AlertmanagerAPI {
Anssi Mannila00894a42020-10-19 11:36:26 +0300759 cr := clientruntime.New(amAddress, amBaseUrl, amSchemes)
760 return client.New(cr, strfmt.Default)
761}
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200762
763func getCurrentDirectory() string {
764 dir, err := os.Getwd()
765 if err != nil {
766 fmt.Println(err)
767 }
768 return dir
Mohamed Abukar2336a842020-10-30 16:19:38 +0200769}