Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 AT&T Intellectual Property. |
| 3 | * Copyright (c) 2020 Nokia. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | * platform project (RICP). |
| 19 | */ |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 20 | |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 21 | package main |
| 22 | |
| 23 | import ( |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 24 | "bytes" |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 25 | "encoding/json" |
| 26 | "fmt" |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 27 | "io/ioutil" |
| 28 | "net/http" |
| 29 | "os" |
Mohamed Abukar | 48ac36c | 2022-03-27 07:47:08 +0000 | [diff] [blame] | 30 | "strconv" |
| 31 | "strings" |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 32 | "time" |
| 33 | |
Juha Hyttinen | 281d060 | 2021-12-23 10:14:40 +0200 | [diff] [blame] | 34 | "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm" |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 35 | app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 36 | clientruntime "github.com/go-openapi/runtime/client" |
| 37 | "github.com/go-openapi/strfmt" |
| 38 | "github.com/prometheus/alertmanager/api/v2/client" |
| 39 | "github.com/prometheus/alertmanager/api/v2/client/alert" |
| 40 | "github.com/prometheus/alertmanager/api/v2/models" |
| 41 | "github.com/spf13/viper" |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 42 | ) |
| 43 | |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 44 | func (a *AlarmManager) ClearExpiredAlarms(m AlarmNotification, idx int, mLocked bool) bool { |
| 45 | d, ok := alarm.RICAlarmDefinitions[m.Alarm.SpecificProblem] |
| 46 | if !ok || d.TimeToLive == 0 { |
| 47 | return false |
| 48 | } |
| 49 | |
| 50 | elapsed := (time.Now().UnixNano() - m.AlarmTime) / 1e9 |
| 51 | if int(elapsed) >= d.TimeToLive { |
| 52 | app.Logger.Info("Alarm (sp=%d id=%d) with TTL=%d expired, clearing ...", m.Alarm.SpecificProblem, m.AlarmId, d.TimeToLive) |
| 53 | |
| 54 | m.AlarmAction = alarm.AlarmActionClear |
| 55 | m.AlarmTime = time.Now().UnixNano() |
| 56 | |
| 57 | if !mLocked { // For testing purpose |
| 58 | a.mutex.Lock() |
| 59 | } |
| 60 | a.ProcessClearAlarm(&m, d, idx) |
| 61 | return true |
| 62 | } |
| 63 | return false |
| 64 | } |
| 65 | |
| 66 | func (a *AlarmManager) StartTTLTimer(interval int) { |
| 67 | tick := time.Tick(time.Duration(interval) * time.Second) |
| 68 | for range tick { |
| 69 | a.mutex.Lock() |
| 70 | for idx, m := range a.activeAlarms { |
| 71 | if a.ClearExpiredAlarms(m, idx, true) { |
| 72 | a.mutex.Lock() // ClearExpiredAlarms unlocks the mutex, so re-lock here |
| 73 | continue |
| 74 | } |
| 75 | } |
| 76 | a.mutex.Unlock() |
| 77 | } |
| 78 | } |
| 79 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 80 | func (a *AlarmManager) StartAlertTimer() { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 81 | tick := time.Tick(time.Duration(a.alertInterval) * time.Millisecond) |
| 82 | for range tick { |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 83 | a.mutex.Lock() |
Mohamed Abukar | 48ac36c | 2022-03-27 07:47:08 +0000 | [diff] [blame] | 84 | |
| 85 | a.ProcessAlerts() |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 86 | for _, m := range a.activeAlarms { |
| 87 | app.Logger.Info("Re-raising alarm: %v", m) |
vipin | ba2ef5b | 2020-11-06 11:24:48 +0000 | [diff] [blame] | 88 | a.PostAlert(a.GenerateAlertLabels(m.AlarmId, m.Alarm, AlertStatusActive, m.AlarmTime)) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 89 | } |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 90 | a.mutex.Unlock() |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 94 | func (a *AlarmManager) Consume(rp *app.RMRParams) (err error) { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 95 | app.Logger.Info("Message received!") |
| 96 | |
| 97 | defer app.Rmr.Free(rp.Mbuf) |
| 98 | switch rp.Mtype { |
| 99 | case alarm.RIC_ALARM_UPDATE: |
| 100 | a.HandleAlarms(rp) |
| 101 | default: |
| 102 | app.Logger.Info("Unknown Message Type '%d', discarding", rp.Mtype) |
| 103 | } |
| 104 | |
| 105 | return nil |
| 106 | } |
| 107 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 108 | func (a *AlarmManager) HandleAlarms(rp *app.RMRParams) (*alert.PostAlertsOK, error) { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 109 | var m alarm.AlarmMessage |
Lott, Christopher (cl778h) | 3e8e2aa | 2020-06-03 08:52:14 -0400 | [diff] [blame] | 110 | app.Logger.Info("Received JSON: %s", rp.Payload) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 111 | if err := json.Unmarshal(rp.Payload, &m); err != nil { |
| 112 | app.Logger.Error("json.Unmarshal failed: %v", err) |
| 113 | return nil, err |
| 114 | } |
| 115 | app.Logger.Info("newAlarm: %v", m) |
| 116 | |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 117 | return a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}}) |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 118 | } |
| 119 | |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 120 | func (a *AlarmManager) ProcessAlarm(m *AlarmNotification) (*alert.PostAlertsOK, error) { |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 121 | a.mutex.Lock() |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 122 | alarmDef := &alarm.AlarmDefinition{} |
| 123 | var ok bool |
| 124 | if alarmDef, ok = alarm.RICAlarmDefinitions[m.Alarm.SpecificProblem]; !ok { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 125 | app.Logger.Warn("Alarm (SP='%d') not recognized, suppressing ...", m.Alarm.SpecificProblem) |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 126 | a.mutex.Unlock() |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 127 | return nil, nil |
| 128 | } |
| 129 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 130 | idx, found := a.IsMatchFound(m.Alarm) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 131 | // Suppress duplicate alarms |
vipin | 4cedd50 | 2020-09-25 05:58:31 +0000 | [diff] [blame] | 132 | if found && m.AlarmAction == alarm.AlarmActionRaise { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 133 | app.Logger.Info("Duplicate alarm found, suppressing ...") |
Anssi Mannila | fe07bd1 | 2020-09-24 14:02:57 +0300 | [diff] [blame] | 134 | if m.PerceivedSeverity == a.activeAlarms[idx].PerceivedSeverity { |
| 135 | // Duplicate with same severity found |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 136 | a.mutex.Unlock() |
Anssi Mannila | fe07bd1 | 2020-09-24 14:02:57 +0300 | [diff] [blame] | 137 | return nil, nil |
| 138 | } else { |
| 139 | // Remove duplicate with different severity |
| 140 | a.activeAlarms = a.RemoveAlarm(a.activeAlarms, idx, "active") |
| 141 | } |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Clear alarm if found from active alarm list |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 145 | if found && m.AlarmAction == alarm.AlarmActionClear { |
| 146 | return a.ProcessClearAlarm(m, alarmDef, idx) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // New alarm -> update active alarms and post to Alert Manager |
| 150 | if m.AlarmAction == alarm.AlarmActionRaise { |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 151 | return a.ProcessRaiseAlarm(m, alarmDef) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 152 | } |
| 153 | |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 154 | a.mutex.Unlock() |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 155 | return nil, nil |
| 156 | } |
| 157 | |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 158 | func (a *AlarmManager) ProcessRaiseAlarm(m *AlarmNotification, alarmDef *alarm.AlarmDefinition) (*alert.PostAlertsOK, error) { |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 159 | app.Logger.Debug("Raise alarmDef.RaiseDelay = %v, AlarmNotification = %v", alarmDef.RaiseDelay, *m) |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 160 | |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 161 | // RaiseDelay > 0 in an alarm object in active alarm table indicates that raise delay is still ongoing for the alarm |
| 162 | m.AlarmDefinition.RaiseDelay = alarmDef.RaiseDelay |
| 163 | a.UpdateAlarmFields(a.GenerateAlarmId(), m) |
| 164 | a.UpdateActiveAlarmList(m) |
| 165 | a.mutex.Unlock() |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 166 | |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 167 | if alarmDef.RaiseDelay > 0 { |
| 168 | timerDelay(alarmDef.RaiseDelay) |
| 169 | a.mutex.Lock() |
| 170 | // Alarm may have been deleted from active alarms table during delay or table index may have changed |
| 171 | idx, found := a.IsMatchFound(m.Alarm) |
| 172 | if found { |
| 173 | // Alarm is not showed in active alarms or alarm history via CLI before RaiseDelay has elapsed, i.e the value is 0 |
| 174 | a.activeAlarms[idx].AlarmDefinition.RaiseDelay = 0 |
| 175 | app.Logger.Debug("Raise after delay alarmDef.RaiseDelay = %v, AlarmNotification = %v", alarmDef.RaiseDelay, *m) |
| 176 | a.mutex.Unlock() |
| 177 | } else { |
| 178 | app.Logger.Debug("Alarm deleted during raise delay. AlarmNotification = %v", *m) |
| 179 | a.mutex.Unlock() |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 180 | return nil, nil |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 181 | } |
| 182 | } |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 183 | |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 184 | m.AlarmDefinition.RaiseDelay = 0 |
| 185 | a.UpdateAlarmHistoryList(m) |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 186 | a.WriteAlarmInfoToPersistentVolume() |
| 187 | |
| 188 | // Send alarm notification to NOMA, if enabled |
| 189 | if app.Config.GetBool("controls.noma.enabled") { |
| 190 | return a.PostAlarm(m) |
| 191 | } |
vipin | ba2ef5b | 2020-11-06 11:24:48 +0000 | [diff] [blame] | 192 | return a.PostAlert(a.GenerateAlertLabels(m.AlarmId, m.Alarm, AlertStatusActive, m.AlarmTime)) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 195 | func (a *AlarmManager) ProcessClearAlarm(m *AlarmNotification, alarmDef *alarm.AlarmDefinition, idx int) (*alert.PostAlertsOK, error) { |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 196 | app.Logger.Debug("Clear alarmDef.ClearDelay = %v, AlarmNotification = %v", alarmDef.ClearDelay, *m) |
| 197 | if alarmDef.ClearDelay > 0 { |
| 198 | a.mutex.Unlock() |
| 199 | timerDelay(alarmDef.ClearDelay) |
| 200 | app.Logger.Debug("Clear after delay alarmDef.ClearDelay = %v, AlarmNotification = %v", alarmDef.ClearDelay, *m) |
| 201 | a.mutex.Lock() |
| 202 | // Another alarm clear may have happened during delay and active alarms table index changed |
| 203 | var found bool |
| 204 | idx, found = a.IsMatchFound(m.Alarm) |
| 205 | if !found { |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 206 | a.mutex.Unlock() |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 207 | return nil, nil |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | a.UpdateAlarmFields(a.activeAlarms[idx].AlarmId, m) |
| 211 | a.alarmHistory = append(a.alarmHistory, *m) |
| 212 | a.activeAlarms = a.RemoveAlarm(a.activeAlarms, idx, "active") |
| 213 | if (len(a.alarmHistory) >= a.maxAlarmHistory) && (a.exceededAlarmHistoryOn == false) { |
| 214 | app.Logger.Warn("alarm history count exceeded maxAlarmHistory threshold") |
| 215 | a.GenerateThresholdAlarm(alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD, "history") |
| 216 | } |
| 217 | |
| 218 | if a.exceededActiveAlarmOn && m.Alarm.SpecificProblem == alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD { |
| 219 | a.exceededActiveAlarmOn = false |
| 220 | } |
| 221 | |
| 222 | if a.exceededAlarmHistoryOn && m.Alarm.SpecificProblem == alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD { |
| 223 | a.exceededAlarmHistoryOn = false |
| 224 | } |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 225 | a.WriteAlarmInfoToPersistentVolume() |
| 226 | |
| 227 | a.mutex.Unlock() |
| 228 | if a.postClear && app.Config.GetBool("controls.noma.enabled") { |
| 229 | m.PerceivedSeverity = alarm.SeverityCleared |
| 230 | return a.PostAlarm(m) |
| 231 | } |
| 232 | return nil, nil |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | func timerDelay(delay int) { |
| 236 | timer := time.NewTimer(time.Duration(delay) * time.Second) |
| 237 | <-timer.C |
| 238 | } |
| 239 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 240 | func (a *AlarmManager) IsMatchFound(newAlarm alarm.Alarm) (int, bool) { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 241 | for i, m := range a.activeAlarms { |
| 242 | if m.ManagedObjectId == newAlarm.ManagedObjectId && m.ApplicationId == newAlarm.ApplicationId && |
Mohamed Abukar | 0c38973 | 2020-09-17 14:47:50 +0300 | [diff] [blame] | 243 | m.SpecificProblem == newAlarm.SpecificProblem && m.IdentifyingInfo == newAlarm.IdentifyingInfo { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 244 | return i, true |
| 245 | } |
| 246 | } |
| 247 | return -1, false |
| 248 | } |
| 249 | |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 250 | func (a *AlarmManager) RemoveAlarm(alarms []AlarmNotification, i int, listName string) []AlarmNotification { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 251 | app.Logger.Info("Alarm '%+v' deleted from the '%s' list", alarms[i], listName) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 252 | copy(alarms[i:], alarms[i+1:]) |
| 253 | return alarms[:len(alarms)-1] |
| 254 | } |
| 255 | |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 256 | func (a *AlarmManager) GenerateAlarmId() int { |
Mohamed Abukar | f5a8e71 | 2020-10-19 16:58:17 +0300 | [diff] [blame] | 257 | a.uniqueAlarmId++ // @todo: generate a unique ID |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 258 | return a.uniqueAlarmId |
| 259 | } |
| 260 | |
| 261 | func (a *AlarmManager) UpdateAlarmFields(alarmId int, newAlarm *AlarmNotification) { |
| 262 | alarmDef := alarm.RICAlarmDefinitions[newAlarm.SpecificProblem] |
| 263 | newAlarm.AlarmId = alarmId |
Mohamed Abukar | f5a8e71 | 2020-10-19 16:58:17 +0300 | [diff] [blame] | 264 | newAlarm.AlarmText = alarmDef.AlarmText |
| 265 | newAlarm.EventType = alarmDef.EventType |
| 266 | } |
| 267 | |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 268 | func (a *AlarmManager) GenerateThresholdAlarm(sp int, data string) bool { |
| 269 | thresholdAlarm := a.alarmClient.NewAlarm(sp, alarm.SeverityWarning, "threshold", data) |
| 270 | thresholdMessage := alarm.AlarmMessage{ |
| 271 | Alarm: thresholdAlarm, |
| 272 | AlarmAction: alarm.AlarmActionRaise, |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 273 | AlarmTime: time.Now().UnixNano(), |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 274 | } |
vipin | 78b2b0a | 2020-10-28 10:10:18 +0000 | [diff] [blame] | 275 | alarmDef := alarm.RICAlarmDefinitions[sp] |
| 276 | alarmId := a.GenerateAlarmId() |
| 277 | alarmDef.AlarmId = alarmId |
| 278 | a.activeAlarms = append(a.activeAlarms, AlarmNotification{thresholdMessage, *alarmDef}) |
| 279 | a.alarmHistory = append(a.alarmHistory, AlarmNotification{thresholdMessage, *alarmDef}) |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 280 | |
| 281 | return true |
| 282 | } |
| 283 | |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 284 | func (a *AlarmManager) UpdateActiveAlarmList(newAlarm *AlarmNotification) { |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 285 | /* If maximum number of active alarms is reached, an error log writing is made, and new alarm indicating the problem is raised. |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 286 | The attempt to raise the alarm next time will be suppressed when found as duplicate. */ |
Mohamed Abukar | f5a8e71 | 2020-10-19 16:58:17 +0300 | [diff] [blame] | 287 | if (len(a.activeAlarms) >= a.maxActiveAlarms) && (a.exceededActiveAlarmOn == false) { |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 288 | app.Logger.Warn("active alarm count exceeded maxActiveAlarms threshold") |
| 289 | a.exceededActiveAlarmOn = a.GenerateThresholdAlarm(alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD, "active") |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 290 | } |
| 291 | |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 292 | // @todo: For now just keep the active alarms in-memory. Use SDL later for persistence |
| 293 | a.activeAlarms = append(a.activeAlarms, *newAlarm) |
| 294 | } |
| 295 | |
| 296 | func (a *AlarmManager) UpdateAlarmHistoryList(newAlarm *AlarmNotification) { |
| 297 | /* If maximum number of events in alarm history is reached, an error log writing is made, |
| 298 | and new alarm indicating the problem is raised. The attempt to add new event time will |
| 299 | be suppressed */ |
| 300 | |
Mohamed Abukar | f5a8e71 | 2020-10-19 16:58:17 +0300 | [diff] [blame] | 301 | if (len(a.alarmHistory) >= a.maxAlarmHistory) && (a.exceededAlarmHistoryOn == false) { |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 302 | app.Logger.Warn("alarm history count exceeded maxAlarmHistory threshold") |
| 303 | a.exceededAlarmHistoryOn = a.GenerateThresholdAlarm(alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD, "history") |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 304 | } |
| 305 | |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 306 | // @todo: For now just keep the alarms history in-memory. Use SDL later for persistence |
Mohamed Abukar | 0c38973 | 2020-09-17 14:47:50 +0300 | [diff] [blame] | 307 | a.alarmHistory = append(a.alarmHistory, *newAlarm) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 310 | func (a *AlarmManager) PostAlarm(m *AlarmNotification) (*alert.PostAlertsOK, error) { |
| 311 | result, err := json.Marshal(m) |
| 312 | if err != nil { |
| 313 | app.Logger.Info("json.Marshal failed: %v", err) |
| 314 | return nil, err |
| 315 | } |
| 316 | |
| 317 | fullUrl := fmt.Sprintf("%s/%s", app.Config.GetString("controls.noma.host"), app.Config.GetString("controls.noma.alarmUrl")) |
| 318 | app.Logger.Info("Posting alarm to '%s'", fullUrl) |
| 319 | |
| 320 | resp, err := http.Post(fullUrl, "application/json", bytes.NewReader(result)) |
| 321 | if err != nil || resp == nil { |
| 322 | app.Logger.Info("Unable to post alarm to '%s': %v", fullUrl, err) |
| 323 | } |
| 324 | |
| 325 | return nil, err |
| 326 | } |
| 327 | |
vipin | ba2ef5b | 2020-11-06 11:24:48 +0000 | [diff] [blame] | 328 | func (a *AlarmManager) GenerateAlertLabels(alarmId int, newAlarm alarm.Alarm, status AlertStatus, alarmTime int64) (models.LabelSet, models.LabelSet) { |
Mohamed Abukar | 48ac36c | 2022-03-27 07:47:08 +0000 | [diff] [blame] | 329 | if strings.Contains(newAlarm.ApplicationId, "FM") { |
| 330 | app.Logger.Info("Alarm '%d' is originated from FM, ignoring ...", alarmId) |
| 331 | return models.LabelSet{}, models.LabelSet{} |
| 332 | } |
| 333 | |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 334 | alarmDef := alarm.RICAlarmDefinitions[newAlarm.SpecificProblem] |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 335 | amLabels := models.LabelSet{ |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame] | 336 | "status": string(status), |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 337 | "alertname": alarmDef.AlarmText, |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 338 | "severity": string(newAlarm.PerceivedSeverity), |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 339 | "service": fmt.Sprintf("%s/%s", newAlarm.ManagedObjectId, newAlarm.ApplicationId), |
Mohamed Abukar | 273c8e8 | 2021-10-21 10:02:07 +0300 | [diff] [blame] | 340 | "info": newAlarm.IdentifyingInfo, |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 341 | "system_name": "RIC", |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 342 | } |
| 343 | amAnnotations := models.LabelSet{ |
vipin | ba2ef5b | 2020-11-06 11:24:48 +0000 | [diff] [blame] | 344 | "alarm_id": fmt.Sprintf("%d", alarmId), |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 345 | "specific_problem": fmt.Sprintf("%d", newAlarm.SpecificProblem), |
| 346 | "event_type": alarmDef.EventType, |
| 347 | "identifying_info": newAlarm.IdentifyingInfo, |
| 348 | "additional_info": newAlarm.AdditionalInfo, |
| 349 | "description": fmt.Sprintf("%s:%s", newAlarm.IdentifyingInfo, newAlarm.AdditionalInfo), |
Mohamed Abukar | 48ac36c | 2022-03-27 07:47:08 +0000 | [diff] [blame] | 350 | "summary": newAlarm.IdentifyingInfo, |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 351 | "instructions": alarmDef.OperationInstructions, |
| 352 | "timestamp": fmt.Sprintf("%s", time.Unix(0, alarmTime).Format("02/01/2006, 15:04:05")), |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return amLabels, amAnnotations |
| 356 | } |
| 357 | |
Juha Hyttinen | 5f9b88b | 2023-06-02 10:04:15 +0300 | [diff] [blame] | 358 | func (a *AlarmManager) NewAlertmanagerClient() *client.AlertmanagerAPI { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 359 | cr := clientruntime.New(a.amHost, a.amBaseUrl, a.amSchemes) |
| 360 | return client.New(cr, strfmt.Default) |
| 361 | } |
| 362 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 363 | func (a *AlarmManager) PostAlert(amLabels, amAnnotations models.LabelSet) (*alert.PostAlertsOK, error) { |
Mohamed Abukar | 48ac36c | 2022-03-27 07:47:08 +0000 | [diff] [blame] | 364 | if len(amLabels) == 0 || len(amAnnotations) == 0 { |
| 365 | return &alert.PostAlertsOK{}, nil |
| 366 | } |
| 367 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 368 | pa := &models.PostableAlert{ |
| 369 | Alert: models.Alert{ |
Mohamed Abukar | 48ac36c | 2022-03-27 07:47:08 +0000 | [diff] [blame] | 370 | GeneratorURL: strfmt.URI("http://service-ricplt-alarmmanager-http.ricplt:8080/ric/v1/alarms"), |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 371 | Labels: amLabels, |
| 372 | }, |
| 373 | Annotations: amAnnotations, |
| 374 | } |
| 375 | alertParams := alert.NewPostAlertsParams().WithAlerts(models.PostableAlerts{pa}) |
| 376 | |
Mohamed Abukar | 643241f | 2020-06-09 15:26:00 +0300 | [diff] [blame] | 377 | app.Logger.Info("Posting alerts: labels: %+v, annotations: %+v", amLabels, amAnnotations) |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 378 | ok, err := a.NewAlertmanagerClient().Alert.PostAlerts(alertParams) |
| 379 | if err != nil { |
Mohamed Abukar | 48ac36c | 2022-03-27 07:47:08 +0000 | [diff] [blame] | 380 | app.Logger.Error("Posting alerts to '%s/%s' failed: %v", a.amHost, a.amBaseUrl, err) |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 381 | } |
| 382 | return ok, err |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Mohamed Abukar | 48ac36c | 2022-03-27 07:47:08 +0000 | [diff] [blame] | 385 | func (a *AlarmManager) GetAlerts() (*alert.GetAlertsOK, error) { |
| 386 | active := true |
| 387 | alertParams := alert.NewGetAlertsParams() |
| 388 | alertParams.Active = &active |
| 389 | resp, err := a.NewAlertmanagerClient().Alert.GetAlerts(alertParams) |
| 390 | if err != nil { |
| 391 | app.Logger.Error("Getting alerts from '%s/%s' failed: %v", a.amHost, a.amBaseUrl, err) |
| 392 | return resp, nil |
| 393 | } |
| 394 | app.Logger.Info("GetAlerts: %+v", resp) |
| 395 | |
| 396 | return resp, err |
| 397 | } |
| 398 | |
| 399 | func (a *AlarmManager) ProcessAlerts() { |
| 400 | resp, err := a.GetAlerts() |
| 401 | if err != nil || resp == nil { |
| 402 | app.Logger.Error("Getting alerts from '%s/%s' failed: %v", a.amHost, a.amBaseUrl, err) |
| 403 | return |
| 404 | } |
| 405 | |
| 406 | var buildAlarm = func(alert *models.GettableAlert) alarm.Alarm { |
| 407 | a := alarm.Alarm{ManagedObjectId: "SEP", ApplicationId: "FM"} |
| 408 | |
| 409 | if v, ok := alert.Alert.Labels["specific_problem"]; ok { |
| 410 | sp, _ := strconv.Atoi(v) |
| 411 | a.SpecificProblem = sp |
| 412 | } |
| 413 | |
| 414 | if v, ok := alert.Alert.Labels["severity"]; ok { |
| 415 | a.PerceivedSeverity = alarm.Severity(fmt.Sprint(v)) |
| 416 | } |
| 417 | |
| 418 | if v, ok := alert.Alert.Labels["name"]; ok { |
| 419 | a.AdditionalInfo = v |
| 420 | } |
| 421 | |
| 422 | if v, ok := alert.Annotations["description"]; ok { |
| 423 | a.IdentifyingInfo = v |
| 424 | } |
| 425 | |
| 426 | return a |
| 427 | } |
| 428 | |
| 429 | // Remove cleared alerts first |
| 430 | for _, m := range a.activeAlarms { |
| 431 | if m.ApplicationId != "FM" { |
| 432 | continue |
| 433 | } |
| 434 | |
| 435 | found := false |
| 436 | for _, alert := range resp.Payload { |
| 437 | v, ok := alert.Alert.Labels["service"] |
| 438 | if !ok || !strings.Contains(v, "FM") { |
| 439 | continue |
| 440 | } |
| 441 | |
| 442 | a := buildAlarm(alert) |
| 443 | if m.ManagedObjectId == a.ManagedObjectId && m.ApplicationId == a.ApplicationId && |
| 444 | m.SpecificProblem == a.SpecificProblem && m.IdentifyingInfo == a.IdentifyingInfo { |
| 445 | found = true |
| 446 | break |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if !found { |
| 451 | m.AlarmAction = alarm.AlarmActionClear |
| 452 | go a.ProcessAlarm(&m) |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | for _, alert := range resp.Payload { |
| 457 | v, ok := alert.Alert.Labels["service"] |
| 458 | if ok && strings.Contains(v, "FM") { |
| 459 | m := alarm.AlarmMessage{Alarm: buildAlarm(alert), AlarmAction: alarm.AlarmActionRaise, AlarmTime: time.Now().UnixNano()} |
| 460 | go a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}}) |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 465 | func (a *AlarmManager) StatusCB() bool { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 466 | if !a.rmrReady { |
| 467 | app.Logger.Info("RMR not ready yet!") |
| 468 | } |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 469 | return a.rmrReady |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 470 | } |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 471 | |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 472 | func (a *AlarmManager) ConfigChangeCB(configparam string) { |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 473 | a.maxActiveAlarms = app.Config.GetInt("controls.maxActiveAlarms") |
Anssi Mannila | ac56b89 | 2020-11-20 14:50:00 +0200 | [diff] [blame] | 474 | if a.maxActiveAlarms == 0 { |
| 475 | a.maxActiveAlarms = 5000 |
| 476 | } |
| 477 | |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 478 | a.maxAlarmHistory = app.Config.GetInt("controls.maxAlarmHistory") |
Anssi Mannila | ac56b89 | 2020-11-20 14:50:00 +0200 | [diff] [blame] | 479 | if a.maxAlarmHistory == 0 { |
| 480 | a.maxAlarmHistory = 20000 |
| 481 | } |
Mohamed Abukar | 2a74ea5 | 2021-03-07 10:10:58 +0200 | [diff] [blame] | 482 | |
Anssi Mannila | 4450a89 | 2020-09-25 10:24:29 +0300 | [diff] [blame] | 483 | a.alertInterval = viper.GetInt("controls.promAlertManager.alertInterval") |
| 484 | a.amHost = viper.GetString("controls.promAlertManager.address") |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 485 | |
| 486 | app.Logger.Debug("ConfigChangeCB: maxActiveAlarms %v", a.maxActiveAlarms) |
| 487 | app.Logger.Debug("ConfigChangeCB: maxAlarmHistory = %v", a.maxAlarmHistory) |
Anssi Mannila | 4450a89 | 2020-09-25 10:24:29 +0300 | [diff] [blame] | 488 | app.Logger.Debug("ConfigChangeCB: alertInterval %v", a.alertInterval) |
| 489 | app.Logger.Debug("ConfigChangeCB: amHost = %v", a.amHost) |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 490 | |
| 491 | return |
| 492 | } |
| 493 | |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 494 | func (a *AlarmManager) ReadAlarmDefinitionFromJson() { |
| 495 | |
| 496 | filename := os.Getenv("DEF_FILE") |
| 497 | file, err := ioutil.ReadFile(filename) |
| 498 | if err == nil { |
| 499 | data := RicAlarmDefinitions{} |
| 500 | err = json.Unmarshal([]byte(file), &data) |
| 501 | if err == nil { |
| 502 | for _, alarmDefinition := range data.AlarmDefinitions { |
| 503 | _, exists := alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] |
| 504 | if exists { |
| 505 | app.Logger.Error("ReadAlarmDefinitionFromJson: alarm definition already exists for %v", alarmDefinition.AlarmId) |
| 506 | } else { |
| 507 | app.Logger.Debug("ReadAlarmDefinitionFromJson: alarm %v", alarmDefinition.AlarmId) |
| 508 | ricAlarmDefintion := new(alarm.AlarmDefinition) |
| 509 | ricAlarmDefintion.AlarmId = alarmDefinition.AlarmId |
| 510 | ricAlarmDefintion.AlarmText = alarmDefinition.AlarmText |
| 511 | ricAlarmDefintion.EventType = alarmDefinition.EventType |
| 512 | ricAlarmDefintion.OperationInstructions = alarmDefinition.OperationInstructions |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 513 | ricAlarmDefintion.RaiseDelay = alarmDefinition.RaiseDelay |
| 514 | ricAlarmDefintion.ClearDelay = alarmDefinition.ClearDelay |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 515 | ricAlarmDefintion.TimeToLive = alarmDefinition.TimeToLive |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 516 | alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] = ricAlarmDefintion |
| 517 | } |
| 518 | } |
| 519 | } else { |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 520 | app.Logger.Error("ReadAlarmDefinitionFromJson: json.Unmarshal failed with error %v", err) |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 521 | } |
| 522 | } else { |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 523 | app.Logger.Error("ReadAlarmDefinitionFromJson: ioutil.ReadFile failed with error %v", err) |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
vipin | 78b2b0a | 2020-10-28 10:10:18 +0000 | [diff] [blame] | 527 | func (a *AlarmManager) ReadAlarmInfoFromPersistentVolume() { |
| 528 | var alarmpersistentinfo AlarmPersistentInfo |
| 529 | byteValue, rerr := ioutil.ReadFile(a.alarmInfoPvFile) |
| 530 | if rerr != nil { |
manoj1 | 66f8726 | 2022-05-27 08:06:58 -0400 | [diff] [blame] | 531 | app.Logger.Info("Unable to read alarminfo.json : %v", rerr) |
vipin | 78b2b0a | 2020-10-28 10:10:18 +0000 | [diff] [blame] | 532 | } else { |
| 533 | err := json.Unmarshal(byteValue, &alarmpersistentinfo) |
| 534 | if err != nil { |
| 535 | app.Logger.Error("alarmpersistentinfo json unmarshal error %v", err) |
| 536 | } else { |
| 537 | a.uniqueAlarmId = alarmpersistentinfo.UniqueAlarmId |
| 538 | a.activeAlarms = make([]AlarmNotification, len(alarmpersistentinfo.ActiveAlarms)) |
| 539 | a.alarmHistory = make([]AlarmNotification, len(alarmpersistentinfo.AlarmHistory)) |
| 540 | copy(a.activeAlarms, alarmpersistentinfo.ActiveAlarms) |
| 541 | copy(a.alarmHistory, alarmpersistentinfo.AlarmHistory) |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | func (a *AlarmManager) WriteAlarmInfoToPersistentVolume() { |
| 547 | var alarmpersistentinfo AlarmPersistentInfo |
| 548 | alarmpersistentinfo.UniqueAlarmId = a.uniqueAlarmId |
| 549 | alarmpersistentinfo.ActiveAlarms = make([]AlarmNotification, len(a.activeAlarms)) |
| 550 | alarmpersistentinfo.AlarmHistory = make([]AlarmNotification, len(a.alarmHistory)) |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 551 | |
vipin | 78b2b0a | 2020-10-28 10:10:18 +0000 | [diff] [blame] | 552 | copy(alarmpersistentinfo.ActiveAlarms, a.activeAlarms) |
| 553 | copy(alarmpersistentinfo.AlarmHistory, a.alarmHistory) |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 554 | |
vipin | 78b2b0a | 2020-10-28 10:10:18 +0000 | [diff] [blame] | 555 | wdata, err := json.MarshalIndent(alarmpersistentinfo, "", " ") |
| 556 | if err != nil { |
| 557 | app.Logger.Error("alarmpersistentinfo json marshal error %v", err) |
| 558 | } else { |
| 559 | werr := ioutil.WriteFile(a.alarmInfoPvFile, wdata, 0777) |
| 560 | if werr != nil { |
| 561 | app.Logger.Error("alarminfo.json file write error %v", werr) |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 566 | func (a *AlarmManager) Run(sdlcheck bool, ttlInterval int) { |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 567 | app.Logger.SetMdc("alarmManager", fmt.Sprintf("%s:%s", Version, Hash)) |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 568 | app.SetReadyCB(func(d interface{}) { a.rmrReady = true }, true) |
| 569 | app.Resource.InjectStatusCb(a.StatusCB) |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 570 | app.AddConfigChangeListener(a.ConfigChangeCB) |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 571 | |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 572 | alarm.RICAlarmDefinitions = make(map[int]*alarm.AlarmDefinition) |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 573 | a.ReadAlarmDefinitionFromJson() |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 574 | |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 575 | a.InjectRoutes() |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 576 | |
| 577 | // Start background timer for re-raising alerts |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 578 | go a.StartAlertTimer() |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 579 | go a.StartTTLTimer(ttlInterval) |
| 580 | |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 581 | a.alarmClient, _ = alarm.InitAlarm("SEP", "ALARMMANAGER") |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 582 | |
vipin | 78b2b0a | 2020-10-28 10:10:18 +0000 | [diff] [blame] | 583 | a.ReadAlarmInfoFromPersistentVolume() |
| 584 | |
Juha Hyttinen | 5f9b88b | 2023-06-02 10:04:15 +0300 | [diff] [blame] | 585 | time.Sleep(8 * time.Second) |
| 586 | app.RunWithRunParams(a, app.RunParams{SdlCheck: sdlcheck, DisableAlarmClient: true}) |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 587 | } |
| 588 | |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 589 | func NewAlarmManager(amHost string, alertInterval int, clearAlarm bool) *AlarmManager { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 590 | if alertInterval == 0 { |
| 591 | alertInterval = viper.GetInt("controls.promAlertManager.alertInterval") |
| 592 | } |
| 593 | |
| 594 | if amHost == "" { |
| 595 | amHost = viper.GetString("controls.promAlertManager.address") |
| 596 | } |
| 597 | |
Anssi Mannila | ac56b89 | 2020-11-20 14:50:00 +0200 | [diff] [blame] | 598 | maxActiveAlarms := app.Config.GetInt("controls.maxActiveAlarms") |
| 599 | if maxActiveAlarms == 0 { |
| 600 | maxActiveAlarms = 5000 |
| 601 | } |
| 602 | |
| 603 | maxAlarmHistory := app.Config.GetInt("controls.maxAlarmHistory") |
| 604 | if maxAlarmHistory == 0 { |
| 605 | maxAlarmHistory = 20000 |
| 606 | } |
| 607 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 608 | return &AlarmManager{ |
Mohamed Abukar | f5a8e71 | 2020-10-19 16:58:17 +0300 | [diff] [blame] | 609 | rmrReady: false, |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 610 | postClear: clearAlarm, |
Mohamed Abukar | f5a8e71 | 2020-10-19 16:58:17 +0300 | [diff] [blame] | 611 | amHost: amHost, |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 612 | amBaseUrl: app.Config.GetString("controls.promAlertManager.baseUrl"), |
| 613 | amSchemes: []string{app.Config.GetString("controls.promAlertManager.schemes")}, |
Mohamed Abukar | f5a8e71 | 2020-10-19 16:58:17 +0300 | [diff] [blame] | 614 | alertInterval: alertInterval, |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 615 | activeAlarms: make([]AlarmNotification, 0), |
| 616 | alarmHistory: make([]AlarmNotification, 0), |
| 617 | uniqueAlarmId: 0, |
Anssi Mannila | ac56b89 | 2020-11-20 14:50:00 +0200 | [diff] [blame] | 618 | maxActiveAlarms: maxActiveAlarms, |
| 619 | maxAlarmHistory: maxAlarmHistory, |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 620 | exceededActiveAlarmOn: false, |
| 621 | exceededAlarmHistoryOn: false, |
vipin | 78b2b0a | 2020-10-28 10:10:18 +0000 | [diff] [blame] | 622 | alarmInfoPvFile: app.Config.GetString("controls.alarmInfoPvFile"), |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | // Main function |
| 627 | func main() { |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 628 | NewAlarmManager("", 0, true).Run(true, 10) |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 629 | } |