From 105030feb8fabd8b4ddff552c53e905146b2ea5f Mon Sep 17 00:00:00 2001 From: Mohamed Abukar Date: Thu, 22 Oct 2020 18:08:34 +0300 Subject: [PATCH] Support for NOMA + some minor code refactoring Change-Id: Ie3572faff690c9125e8a4b35e0b5119de093d9c1 Signed-off-by: Mohamed Abukar --- build/container-tag.yaml | 2 +- cli/alarm-cli.go | 58 ++++++++++++-------- config/config-file.json | 5 ++ go.mod | 2 + manager/cmd/manager.go | 131 +++++++++++++++++++++++++++++--------------- manager/cmd/manager_test.go | 2 +- manager/cmd/restapi.go | 2 +- manager/cmd/types.go | 6 +- 8 files changed, 135 insertions(+), 73 deletions(-) diff --git a/build/container-tag.yaml b/build/container-tag.yaml index 7b6e635..d624928 100644 --- a/build/container-tag.yaml +++ b/build/container-tag.yaml @@ -2,4 +2,4 @@ # By default this file is in the docker build directory, # but the location can configured in the JJB template. --- -tag: 0.5.4 +tag: 0.5.5 diff --git a/cli/alarm-cli.go b/cli/alarm-cli.go index f666292..9a18015 100755 --- a/cli/alarm-cli.go +++ b/cli/alarm-cli.go @@ -17,8 +17,8 @@ import ( "github.com/jedib0t/go-pretty/table" "github.com/prometheus/alertmanager/api/v2/client" "github.com/prometheus/alertmanager/api/v2/client/alert" - "github.com/thatisuday/commando" "github.com/spf13/viper" + "github.com/thatisuday/commando" ) type CliAlarmDefinitions struct { @@ -33,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 @@ -48,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. @@ -60,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) @@ -71,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) @@ -87,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) { @@ -102,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) { @@ -115,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) @@ -128,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) @@ -138,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) @@ -151,7 +165,7 @@ 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) { @@ -166,7 +180,7 @@ func main() { 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, nil). + 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) @@ -193,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) @@ -265,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}, }) } } @@ -596,11 +611,11 @@ func displayAlerts(flags map[string]commando.FlagValue) { t := table.NewWriter() t.SetOutputMirror(os.Stdout) t.AppendHeader(table.Row{"Alerts from Prometheus Alert Manager"}) - for _, gettableAlert := range resp.Payload{ + for _, gettableAlert := range resp.Payload { t.AppendRow([]interface{}{"------------------------------------"}) if gettableAlert != nil { for key, item := range gettableAlert.Annotations { - t.AppendRow([]interface{}{key, item}) + t.AppendRow([]interface{}{key, item}) } if gettableAlert.EndsAt != nil { t.AppendRow([]interface{}{"EndsAt", *gettableAlert.EndsAt}) @@ -610,7 +625,7 @@ func displayAlerts(flags map[string]commando.FlagValue) { } for key, item := range gettableAlert.Receivers { if gettableAlert.Receivers != nil { - t.AppendRow([]interface{}{key, *item.Name}) + t.AppendRow([]interface{}{key, *item.Name}) } } if gettableAlert.StartsAt != nil { @@ -626,14 +641,14 @@ func displayAlerts(flags map[string]commando.FlagValue) { } t.AppendRow([]interface{}{"GeneratorURL", gettableAlert.Alert.GeneratorURL}) for key, item := range gettableAlert.Alert.Labels { - t.AppendRow([]interface{}{key, item}) + 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() @@ -666,4 +681,3 @@ func newAlertManagerClient(amAddress string, amBaseUrl string, amSchemes []strin cr := clientruntime.New(amAddress, amBaseUrl, amSchemes) return client.New(cr, strfmt.Default) } - diff --git a/config/config-file.json b/config/config-file.json index 9f0597e..73a6c92 100755 --- a/config/config-file.json +++ b/config/config-file.json @@ -20,6 +20,11 @@ "schemes": "http", "alertInterval": 30000 }, + "noma": { + "enabled": false, + "host": "http://service-ricplt-noma-http:8087", + "alarmUrl": "ric/v1/noma/alarms" + }, "maxActiveAlarms": 5000, "maxAlarmHistory": 20000 } diff --git a/go.mod b/go.mod index b6ae937..0fb43fa 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,8 @@ require ( github.com/gorilla/mux v1.7.1 github.com/jedib0t/go-pretty v4.3.0+incompatible github.com/mattn/go-runewidth v0.0.9 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect github.com/prometheus/alertmanager v0.20.0 github.com/spf13/viper v1.6.2 github.com/stretchr/testify v1.5.1 diff --git a/manager/cmd/manager.go b/manager/cmd/manager.go index b7f9ce2..1a950db 100755 --- a/manager/cmd/manager.go +++ b/manager/cmd/manager.go @@ -21,6 +21,7 @@ package main import ( + "bytes" "encoding/json" "fmt" "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm" @@ -32,6 +33,7 @@ import ( "github.com/prometheus/alertmanager/api/v2/models" "github.com/spf13/viper" "io/ioutil" + "net/http" "os" "time" ) @@ -71,10 +73,10 @@ func (a *AlarmManager) HandleAlarms(rp *app.RMRParams) (*alert.PostAlertsOK, err } app.Logger.Info("newAlarm: %v", m) - return a.ProcessAlarm(&AlarmInformation{m, alarm.AlarmDefinition{}}) + return a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}}) } -func (a *AlarmManager) ProcessAlarm(m *AlarmInformation) (*alert.PostAlertsOK, error) { +func (a *AlarmManager) ProcessAlarm(m *AlarmNotification) (*alert.PostAlertsOK, error) { a.mutex.Lock() if _, ok := alarm.RICAlarmDefinitions[m.Alarm.SpecificProblem]; !ok { app.Logger.Warn("Alarm (SP='%d') not recognized, suppressing ...", m.Alarm.SpecificProblem) @@ -99,24 +101,30 @@ func (a *AlarmManager) ProcessAlarm(m *AlarmInformation) (*alert.PostAlertsOK, e // Clear alarm if found from active alarm list if m.AlarmAction == alarm.AlarmActionClear { if found { + a.UpdateAlarmFields(a.activeAlarms[idx].AlarmId, m) a.alarmHistory = append(a.alarmHistory, *m) a.activeAlarms = a.RemoveAlarm(a.activeAlarms, idx, "active") if (len(a.alarmHistory) >= a.maxAlarmHistory) && (a.exceededAlarmHistoryOn == false) { - app.Logger.Error("alarm history count exceeded maxAlarmHistory threshold") - histAlarm := a.alarmClient.NewAlarm(alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD, alarm.SeverityWarning, "threshold", "history") - am := alarm.AlarmMessage{Alarm: histAlarm, AlarmAction: alarm.AlarmActionRaise, AlarmTime: (time.Now().UnixNano())} - histAlarmMessage := AlarmInformation{am, alarm.AlarmDefinition{}} - a.activeAlarms = append(a.activeAlarms, histAlarmMessage) - a.alarmHistory = append(a.alarmHistory, histAlarmMessage) + app.Logger.Warn("alarm history count exceeded maxAlarmHistory threshold") + a.GenerateThresholdAlarm(alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD, "history") } - if (a.exceededActiveAlarmOn == true) && (m.Alarm.SpecificProblem == alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD) { + + if a.exceededActiveAlarmOn && m.Alarm.SpecificProblem == alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD { a.exceededActiveAlarmOn = false } - if (a.exceededAlarmHistoryOn == true) && (m.Alarm.SpecificProblem == alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD) { + + if a.exceededAlarmHistoryOn && m.Alarm.SpecificProblem == alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD { a.exceededAlarmHistoryOn = false } + if a.postClear { a.mutex.Unlock() + + // Send alarm notification to NOMA, if enabled + if app.Config.GetBool("controls.noma.enabled") { + m.PerceivedSeverity = alarm.SeverityCleared + return a.PostAlarm(m) + } return a.PostAlert(a.GenerateAlertLabels(m.Alarm, AlertStatusResolved, m.AlarmTime)) } } @@ -127,8 +135,14 @@ func (a *AlarmManager) ProcessAlarm(m *AlarmInformation) (*alert.PostAlertsOK, e // New alarm -> update active alarms and post to Alert Manager if m.AlarmAction == alarm.AlarmActionRaise { + a.UpdateAlarmFields(a.GenerateAlarmId(), m) a.UpdateAlarmLists(m) a.mutex.Unlock() + + // Send alarm notification to NOMA, if enabled + if app.Config.GetBool("controls.noma.enabled") { + return a.PostAlarm(m) + } return a.PostAlert(a.GenerateAlertLabels(m.Alarm, AlertStatusActive, m.AlarmTime)) } @@ -146,64 +160,91 @@ func (a *AlarmManager) IsMatchFound(newAlarm alarm.Alarm) (int, bool) { return -1, false } -func (a *AlarmManager) RemoveAlarm(alarms []AlarmInformation, i int, listName string) []AlarmInformation { +func (a *AlarmManager) RemoveAlarm(alarms []AlarmNotification, i int, listName string) []AlarmNotification { app.Logger.Info("Alarm '%+v' deleted from the '%s' list", alarms[i], listName) copy(alarms[i:], alarms[i+1:]) return alarms[:len(alarms)-1] } -func (a *AlarmManager) UpdateAlarmFields(newAlarm *AlarmInformation) { - alarmDef := alarm.RICAlarmDefinitions[newAlarm.SpecificProblem] - newAlarm.AlarmId = a.uniqueAlarmId +func (a *AlarmManager) GenerateAlarmId() int { a.uniqueAlarmId++ // @todo: generate a unique ID + return a.uniqueAlarmId +} + +func (a *AlarmManager) UpdateAlarmFields(alarmId int, newAlarm *AlarmNotification) { + alarmDef := alarm.RICAlarmDefinitions[newAlarm.SpecificProblem] + newAlarm.AlarmId = alarmId newAlarm.AlarmText = alarmDef.AlarmText newAlarm.EventType = alarmDef.EventType } -func (a *AlarmManager) UpdateAlarmLists(newAlarm *AlarmInformation) { +func (a *AlarmManager) GenerateThresholdAlarm(sp int, data string) bool { + thresholdAlarm := a.alarmClient.NewAlarm(sp, alarm.SeverityWarning, "threshold", data) + thresholdMessage := alarm.AlarmMessage{ + Alarm: thresholdAlarm, + AlarmAction: alarm.AlarmActionRaise, + AlarmTime: (time.Now().UnixNano()), + } + a.activeAlarms = append(a.activeAlarms, AlarmNotification{thresholdMessage, alarm.AlarmDefinition{}}) + a.alarmHistory = append(a.alarmHistory, AlarmNotification{thresholdMessage, alarm.AlarmDefinition{}}) + + return true +} + +func (a *AlarmManager) UpdateAlarmLists(newAlarm *AlarmNotification) { /* If maximum number of active alarms is reached, an error log writing is made, and new alarm indicating the problem is raised. The attempt to raise the alarm next time will be supressed when found as duplicate. */ if (len(a.activeAlarms) >= a.maxActiveAlarms) && (a.exceededActiveAlarmOn == false) { - app.Logger.Error("active alarm count exceeded maxActiveAlarms threshold") - actAlarm := a.alarmClient.NewAlarm(alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD, alarm.SeverityWarning, "threshold", "active") - actAlarmMessage := alarm.AlarmMessage{Alarm: actAlarm, AlarmAction: alarm.AlarmActionRaise, AlarmTime: (time.Now().UnixNano())} - a.activeAlarms = append(a.activeAlarms, AlarmInformation{actAlarmMessage, alarm.AlarmDefinition{}}) - a.alarmHistory = append(a.alarmHistory, AlarmInformation{actAlarmMessage, alarm.AlarmDefinition{}}) - a.exceededActiveAlarmOn = true + app.Logger.Warn("active alarm count exceeded maxActiveAlarms threshold") + a.exceededActiveAlarmOn = a.GenerateThresholdAlarm(alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD, "active") } if (len(a.alarmHistory) >= a.maxAlarmHistory) && (a.exceededAlarmHistoryOn == false) { - app.Logger.Error("alarm history count exceeded maxAlarmHistory threshold") - histAlarm := a.alarmClient.NewAlarm(alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD, alarm.SeverityWarning, "threshold", "history") - histAlarmMessage := alarm.AlarmMessage{Alarm: histAlarm, AlarmAction: alarm.AlarmActionRaise, AlarmTime: (time.Now().UnixNano())} - a.activeAlarms = append(a.activeAlarms, AlarmInformation{histAlarmMessage, alarm.AlarmDefinition{}}) - a.alarmHistory = append(a.alarmHistory, AlarmInformation{histAlarmMessage, alarm.AlarmDefinition{}}) - a.exceededAlarmHistoryOn = true + app.Logger.Warn("alarm history count exceeded maxAlarmHistory threshold") + a.exceededAlarmHistoryOn = a.GenerateThresholdAlarm(alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD, "history") } - a.UpdateAlarmFields(newAlarm) - // @todo: For now just keep the alarms (both active and history) in-memory. Use SDL later for persistence a.activeAlarms = append(a.activeAlarms, *newAlarm) a.alarmHistory = append(a.alarmHistory, *newAlarm) } +func (a *AlarmManager) PostAlarm(m *AlarmNotification) (*alert.PostAlertsOK, error) { + result, err := json.Marshal(m) + if err != nil { + app.Logger.Info("json.Marshal failed: %v", err) + return nil, err + } + + fullUrl := fmt.Sprintf("%s/%s", app.Config.GetString("controls.noma.host"), app.Config.GetString("controls.noma.alarmUrl")) + app.Logger.Info("Posting alarm to '%s'", fullUrl) + + resp, err := http.Post(fullUrl, "application/json", bytes.NewReader(result)) + if err != nil || resp == nil { + app.Logger.Info("Unable to post alarm to '%s': %v", fullUrl, err) + } + + return nil, err +} + func (a *AlarmManager) GenerateAlertLabels(newAlarm alarm.Alarm, status AlertStatus, alarmTime int64) (models.LabelSet, models.LabelSet) { alarmDef := alarm.RICAlarmDefinitions[newAlarm.SpecificProblem] amLabels := models.LabelSet{ "status": string(status), "alertname": alarmDef.AlarmText, "severity": string(newAlarm.PerceivedSeverity), - "service": fmt.Sprintf("%s:%s", newAlarm.ManagedObjectId, newAlarm.ApplicationId), - "system_name": fmt.Sprintf("RIC:%s:%s", newAlarm.ManagedObjectId, newAlarm.ApplicationId), + "service": fmt.Sprintf("%s/%s", newAlarm.ManagedObjectId, newAlarm.ApplicationId), + "system_name": "RIC", } amAnnotations := models.LabelSet{ - "alarm_id": fmt.Sprintf("%d", alarmDef.AlarmId), - "description": fmt.Sprintf("%d:%s:%s", newAlarm.SpecificProblem, newAlarm.IdentifyingInfo, newAlarm.AdditionalInfo), - "additional_info": newAlarm.AdditionalInfo, - "summary": alarmDef.EventType, - "instructions": alarmDef.OperationInstructions, - "timestamp": fmt.Sprintf("%s", time.Unix(0, alarmTime).Format("02/01/2006, 15:04:05")), + "alarm_id": fmt.Sprintf("%d", alarmDef.AlarmId), + "specific_problem": fmt.Sprintf("%d", newAlarm.SpecificProblem), + "event_type": alarmDef.EventType, + "identifying_info": newAlarm.IdentifyingInfo, + "additional_info": newAlarm.AdditionalInfo, + "description": fmt.Sprintf("%s:%s", newAlarm.IdentifyingInfo, newAlarm.AdditionalInfo), + "instructions": alarmDef.OperationInstructions, + "timestamp": fmt.Sprintf("%s", time.Unix(0, alarmTime).Format("02/01/2006, 15:04:05")), } return amLabels, amAnnotations @@ -306,14 +347,13 @@ func (a *AlarmManager) Run(sdlcheck bool) { app.Resource.InjectRoute("/ric/v1/alarms/define/{alarmId}", a.GetAlarmDefinition, "GET") // Start background timer for re-raising alerts - a.postClear = sdlcheck go a.StartAlertTimer() a.alarmClient, _ = alarm.InitAlarm("SEP", "ALARMMANAGER") app.RunWithParams(a, sdlcheck) } -func NewAlarmManager(amHost string, alertInterval int) *AlarmManager { +func NewAlarmManager(amHost string, alertInterval int, clearAlarm bool) *AlarmManager { if alertInterval == 0 { alertInterval = viper.GetInt("controls.promAlertManager.alertInterval") } @@ -324,13 +364,14 @@ func NewAlarmManager(amHost string, alertInterval int) *AlarmManager { return &AlarmManager{ rmrReady: false, + postClear: clearAlarm, amHost: amHost, - amBaseUrl: viper.GetString("controls.promAlertManager.baseUrl"), - amSchemes: []string{viper.GetString("controls.promAlertManager.schemes")}, + amBaseUrl: app.Config.GetString("controls.promAlertManager.baseUrl"), + amSchemes: []string{app.Config.GetString("controls.promAlertManager.schemes")}, alertInterval: alertInterval, - activeAlarms: make([]AlarmInformation, 0), - alarmHistory: make([]AlarmInformation, 0), - uniqueAlarmId: 1, + activeAlarms: make([]AlarmNotification, 0), + alarmHistory: make([]AlarmNotification, 0), + uniqueAlarmId: 0, maxActiveAlarms: app.Config.GetInt("controls.maxActiveAlarms"), maxAlarmHistory: app.Config.GetInt("controls.maxAlarmHistory"), exceededActiveAlarmOn: false, @@ -340,5 +381,5 @@ func NewAlarmManager(amHost string, alertInterval int) *AlarmManager { // Main function func main() { - NewAlarmManager("", 0).Run(true) + NewAlarmManager("", 0, true).Run(true) } diff --git a/manager/cmd/manager_test.go b/manager/cmd/manager_test.go index 8cae6c2..2589a91 100755 --- a/manager/cmd/manager_test.go +++ b/manager/cmd/manager_test.go @@ -48,7 +48,7 @@ var eventChan chan string // Test cases func TestMain(M *testing.M) { - alarmManager = NewAlarmManager("localhost:9093", 500) + alarmManager = NewAlarmManager("localhost:9093", 500, false) alarmManager.alertInterval = 20000 go alarmManager.Run(false) time.Sleep(time.Duration(10) * time.Second) diff --git a/manager/cmd/restapi.go b/manager/cmd/restapi.go index 33aac0d..412c71c 100755 --- a/manager/cmd/restapi.go +++ b/manager/cmd/restapi.go @@ -180,7 +180,7 @@ func (a *AlarmManager) doAction(w http.ResponseWriter, r *http.Request, isRaiseA m.AlarmTime = time.Now().UnixNano() } - _, err := a.ProcessAlarm(&AlarmInformation{m, alarm.AlarmDefinition{}}) + _, err := a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}}) return err } diff --git a/manager/cmd/types.go b/manager/cmd/types.go index 7af8ec6..7a0fd90 100755 --- a/manager/cmd/types.go +++ b/manager/cmd/types.go @@ -31,8 +31,8 @@ type AlarmManager struct { amBaseUrl string amSchemes []string alertInterval int - activeAlarms []AlarmInformation - alarmHistory []AlarmInformation + activeAlarms []AlarmNotification + alarmHistory []AlarmNotification uniqueAlarmId int mutex sync.Mutex rmrReady bool @@ -44,7 +44,7 @@ type AlarmManager struct { exceededAlarmHistoryOn bool } -type AlarmInformation struct { +type AlarmNotification struct { alarm.AlarmMessage alarm.AlarmDefinition } -- 2.16.6