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 | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 24 | "encoding/json" |
| 25 | "fmt" |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 26 | "sync" |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 27 | "time" |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 28 | |
| 29 | clientruntime "github.com/go-openapi/runtime/client" |
| 30 | "github.com/go-openapi/strfmt" |
| 31 | "github.com/prometheus/alertmanager/api/v2/client" |
| 32 | "github.com/prometheus/alertmanager/api/v2/client/alert" |
| 33 | "github.com/prometheus/alertmanager/api/v2/models" |
| 34 | "github.com/spf13/viper" |
| 35 | |
| 36 | "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm" |
| 37 | app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 38 | ) |
| 39 | |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 40 | type AlertStatus string |
| 41 | |
| 42 | const ( |
| 43 | AlertStatusActive = "active" |
| 44 | AlertStatusResolved = "resolved" |
| 45 | ) |
| 46 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 47 | type AlarmAdapter struct { |
| 48 | amHost string |
| 49 | amBaseUrl string |
| 50 | amSchemes []string |
| 51 | alertInterval int |
| 52 | activeAlarms []alarm.Alarm |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 53 | mutex sync.Mutex |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 54 | rmrReady bool |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 55 | postClear bool |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 58 | var Version string |
| 59 | var Hash string |
| 60 | |
| 61 | // Main function |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 62 | func main() { |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 63 | NewAlarmAdapter("", 0).Run(true) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 66 | func NewAlarmAdapter(amHost string, alertInterval int) *AlarmAdapter { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 67 | if alertInterval == 0 { |
| 68 | alertInterval = viper.GetInt("promAlertManager.alertInterval") |
| 69 | } |
| 70 | |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 71 | if amHost == "" { |
| 72 | amHost = viper.GetString("promAlertManager.address") |
| 73 | } |
| 74 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 75 | return &AlarmAdapter{ |
| 76 | rmrReady: false, |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 77 | amHost: amHost, |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 78 | amBaseUrl: viper.GetString("promAlertManager.baseUrl"), |
| 79 | amSchemes: []string{viper.GetString("promAlertManager.schemes")}, |
| 80 | alertInterval: alertInterval, |
| 81 | activeAlarms: make([]alarm.Alarm, 0), |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func (a *AlarmAdapter) Run(sdlcheck bool) { |
| 86 | app.Logger.SetMdc("alarmAdapter", fmt.Sprintf("%s:%s", Version, Hash)) |
| 87 | app.SetReadyCB(func(d interface{}) { a.rmrReady = true }, true) |
| 88 | app.Resource.InjectStatusCb(a.StatusCB) |
| 89 | |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 90 | app.Resource.InjectRoute("/ric/v1/alarms", a.GetActiveAlarms, "GET") |
| 91 | app.Resource.InjectRoute("/ric/v1/alarms", a.RaiseAlarm, "POST") |
| 92 | app.Resource.InjectRoute("/ric/v1/alarms", a.ClearAlarm, "DELETE") |
Mohamed Abukar | 61bdef5 | 2020-03-09 16:46:12 +0200 | [diff] [blame] | 93 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 94 | // Start background timer for re-raising alerts |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 95 | a.postClear = sdlcheck |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 96 | go a.StartAlertTimer() |
| 97 | |
| 98 | app.RunWithParams(a, sdlcheck) |
| 99 | } |
| 100 | |
| 101 | func (a *AlarmAdapter) StartAlertTimer() { |
| 102 | tick := time.Tick(time.Duration(a.alertInterval) * time.Millisecond) |
| 103 | for range tick { |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 104 | a.mutex.Lock() |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 105 | for _, m := range a.activeAlarms { |
| 106 | app.Logger.Info("Re-raising alarm: %v", m) |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 107 | a.PostAlert(a.GenerateAlertLabels(m, AlertStatusActive)) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 108 | } |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 109 | a.mutex.Unlock() |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | func (a *AlarmAdapter) Consume(rp *app.RMRParams) (err error) { |
| 114 | app.Logger.Info("Message received!") |
| 115 | |
| 116 | defer app.Rmr.Free(rp.Mbuf) |
| 117 | switch rp.Mtype { |
| 118 | case alarm.RIC_ALARM_UPDATE: |
| 119 | a.HandleAlarms(rp) |
| 120 | default: |
| 121 | app.Logger.Info("Unknown Message Type '%d', discarding", rp.Mtype) |
| 122 | } |
| 123 | |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | func (a *AlarmAdapter) HandleAlarms(rp *app.RMRParams) (*alert.PostAlertsOK, error) { |
| 128 | var m alarm.AlarmMessage |
| 129 | if err := json.Unmarshal(rp.Payload, &m); err != nil { |
| 130 | app.Logger.Error("json.Unmarshal failed: %v", err) |
| 131 | return nil, err |
| 132 | } |
| 133 | app.Logger.Info("newAlarm: %v", m) |
| 134 | |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 135 | if _, ok := alarm.RICAlarmDefinitions[m.Alarm.SpecificProblem]; !ok { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 136 | app.Logger.Warn("Alarm (SP='%d') not recognized, ignoring ...", m.Alarm.SpecificProblem) |
| 137 | return nil, nil |
| 138 | } |
| 139 | |
| 140 | // Suppress duplicate alarms |
| 141 | idx, found := a.IsMatchFound(m.Alarm) |
| 142 | if found && m.AlarmAction != alarm.AlarmActionClear { |
| 143 | app.Logger.Info("Duplicate alarm ... suppressing!") |
| 144 | return nil, nil |
| 145 | } |
| 146 | |
| 147 | // Clear alarm if found from active alarm list |
| 148 | if m.AlarmAction == alarm.AlarmActionClear { |
| 149 | if found { |
| 150 | a.activeAlarms = a.RemoveAlarm(a.activeAlarms, idx) |
| 151 | app.Logger.Info("Active alarm cleared!") |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 152 | |
| 153 | if a.postClear { |
| 154 | return a.PostAlert(a.GenerateAlertLabels(m.Alarm, AlertStatusResolved)) |
| 155 | } |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 156 | } |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 157 | app.Logger.Info("No matching alarm found, ignoring!") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 158 | return nil, nil |
| 159 | } |
| 160 | |
| 161 | // New alarm -> update active alarms and post to Alert Manager |
| 162 | if m.AlarmAction == alarm.AlarmActionRaise { |
| 163 | a.UpdateActiveAlarms(m.Alarm) |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 164 | return a.PostAlert(a.GenerateAlertLabels(m.Alarm, AlertStatusActive)) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return nil, nil |
| 168 | } |
| 169 | |
| 170 | func (a *AlarmAdapter) IsMatchFound(newAlarm alarm.Alarm) (int, bool) { |
| 171 | for i, m := range a.activeAlarms { |
| 172 | if m.ManagedObjectId == newAlarm.ManagedObjectId && m.ApplicationId == newAlarm.ApplicationId && |
| 173 | m.SpecificProblem == newAlarm.SpecificProblem && m.IdentifyingInfo == newAlarm.IdentifyingInfo { |
| 174 | return i, true |
| 175 | } |
| 176 | } |
| 177 | return -1, false |
| 178 | } |
| 179 | |
| 180 | func (a *AlarmAdapter) RemoveAlarm(alarms []alarm.Alarm, i int) []alarm.Alarm { |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 181 | a.mutex.Lock() |
| 182 | defer a.mutex.Unlock() |
| 183 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 184 | copy(alarms[i:], alarms[i+1:]) |
| 185 | return alarms[:len(alarms)-1] |
| 186 | } |
| 187 | |
| 188 | func (a *AlarmAdapter) UpdateActiveAlarms(newAlarm alarm.Alarm) { |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 189 | a.mutex.Lock() |
| 190 | defer a.mutex.Unlock() |
| 191 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 192 | // For now just keep the active alarms in-memory. Use SDL later |
| 193 | a.activeAlarms = append(a.activeAlarms, newAlarm) |
| 194 | } |
| 195 | |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 196 | func (a *AlarmAdapter) GenerateAlertLabels(newAlarm alarm.Alarm, status AlertStatus) (models.LabelSet, models.LabelSet) { |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 197 | alarmDef := alarm.RICAlarmDefinitions[newAlarm.SpecificProblem] |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 198 | amLabels := models.LabelSet{ |
Mohamed Abukar | b2f29a8 | 2020-03-17 09:31:55 +0200 | [diff] [blame^] | 199 | "status": string(status), |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 200 | "alertname": alarmDef.AlarmText, |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 201 | "severity": string(newAlarm.PerceivedSeverity), |
| 202 | "service": fmt.Sprintf("%s:%s", newAlarm.ManagedObjectId, newAlarm.ApplicationId), |
| 203 | "system_name": "RIC", |
| 204 | } |
| 205 | amAnnotations := models.LabelSet{ |
| 206 | "description": newAlarm.IdentifyingInfo, |
| 207 | "additional_info": newAlarm.AdditionalInfo, |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 208 | "summary": alarmDef.EventType, |
| 209 | "instructions": alarmDef.OperationInstructions, |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | return amLabels, amAnnotations |
| 213 | } |
| 214 | |
| 215 | func (a *AlarmAdapter) NewAlertmanagerClient() *client.Alertmanager { |
| 216 | cr := clientruntime.New(a.amHost, a.amBaseUrl, a.amSchemes) |
| 217 | return client.New(cr, strfmt.Default) |
| 218 | } |
| 219 | |
| 220 | func (a *AlarmAdapter) PostAlert(amLabels, amAnnotations models.LabelSet) (*alert.PostAlertsOK, error) { |
| 221 | pa := &models.PostableAlert{ |
| 222 | Alert: models.Alert{ |
| 223 | GeneratorURL: strfmt.URI(""), |
| 224 | Labels: amLabels, |
| 225 | }, |
| 226 | Annotations: amAnnotations, |
| 227 | } |
| 228 | alertParams := alert.NewPostAlertsParams().WithAlerts(models.PostableAlerts{pa}) |
| 229 | |
| 230 | app.Logger.Info("Posting alerts: labels: %v, annotations: %v", amLabels, amAnnotations) |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 231 | ok, err := a.NewAlertmanagerClient().Alert.PostAlerts(alertParams) |
| 232 | if err != nil { |
| 233 | app.Logger.Error("Posting alerts to '%s/%s' failed with error: %v", a.amHost, a.amBaseUrl, err) |
| 234 | } |
| 235 | return ok, err |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | func (a *AlarmAdapter) StatusCB() bool { |
| 239 | if !a.rmrReady { |
| 240 | app.Logger.Info("RMR not ready yet!") |
| 241 | } |
| 242 | |
| 243 | return a.rmrReady |
Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 244 | } |