blob: 76112f9634811fc02e37acb97409997fb706b1d1 [file] [log] [blame]
Mohamed Abukar3e038152020-03-04 10:01:45 +02001/*
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 Abukar4e7e7122020-03-04 10:01:45 +020020
Mohamed Abukar3e038152020-03-04 10:01:45 +020021package main
22
23import (
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020024 "encoding/json"
25 "fmt"
Mohamed Abukaraf0c5702020-03-11 10:29:40 +020026 "sync"
Mohamed Abukarb2f29a82020-03-17 09:31:55 +020027 "time"
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020028
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 Abukar3e038152020-03-04 10:01:45 +020038)
39
Mohamed Abukarb2f29a82020-03-17 09:31:55 +020040type AlertStatus string
41
42const (
43 AlertStatusActive = "active"
44 AlertStatusResolved = "resolved"
45)
46
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020047type AlarmAdapter struct {
48 amHost string
49 amBaseUrl string
50 amSchemes []string
51 alertInterval int
52 activeAlarms []alarm.Alarm
Mohamed Abukarb2f29a82020-03-17 09:31:55 +020053 mutex sync.Mutex
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020054 rmrReady bool
Mohamed Abukarb2f29a82020-03-17 09:31:55 +020055 postClear bool
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020056}
57
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020058var Version string
59var Hash string
60
61// Main function
Mohamed Abukar3e038152020-03-04 10:01:45 +020062func main() {
Mohamed Abukar61bdef52020-03-09 16:46:12 +020063 NewAlarmAdapter("", 0).Run(true)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020064}
65
Mohamed Abukar61bdef52020-03-09 16:46:12 +020066func NewAlarmAdapter(amHost string, alertInterval int) *AlarmAdapter {
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020067 if alertInterval == 0 {
68 alertInterval = viper.GetInt("promAlertManager.alertInterval")
69 }
70
Mohamed Abukar61bdef52020-03-09 16:46:12 +020071 if amHost == "" {
72 amHost = viper.GetString("promAlertManager.address")
73 }
74
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020075 return &AlarmAdapter{
76 rmrReady: false,
Mohamed Abukar61bdef52020-03-09 16:46:12 +020077 amHost: amHost,
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020078 amBaseUrl: viper.GetString("promAlertManager.baseUrl"),
79 amSchemes: []string{viper.GetString("promAlertManager.schemes")},
80 alertInterval: alertInterval,
81 activeAlarms: make([]alarm.Alarm, 0),
82 }
83}
84
85func (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 Abukaraf0c5702020-03-11 10:29:40 +020090 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 Abukar61bdef52020-03-09 16:46:12 +020093
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020094 // Start background timer for re-raising alerts
Mohamed Abukarb2f29a82020-03-17 09:31:55 +020095 a.postClear = sdlcheck
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020096 go a.StartAlertTimer()
97
98 app.RunWithParams(a, sdlcheck)
99}
100
101func (a *AlarmAdapter) StartAlertTimer() {
102 tick := time.Tick(time.Duration(a.alertInterval) * time.Millisecond)
103 for range tick {
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200104 a.mutex.Lock()
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200105 for _, m := range a.activeAlarms {
106 app.Logger.Info("Re-raising alarm: %v", m)
Mohamed Abukarb2f29a82020-03-17 09:31:55 +0200107 a.PostAlert(a.GenerateAlertLabels(m, AlertStatusActive))
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200108 }
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200109 a.mutex.Unlock()
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200110 }
111}
112
113func (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
127func (a *AlarmAdapter) HandleAlarms(rp *app.RMRParams) (*alert.PostAlertsOK, error) {
128 var m alarm.AlarmMessage
Lott, Christopher (cl778h)3e8e2aa2020-06-03 08:52:14 -0400129 app.Logger.Info("Received JSON: %s", rp.Payload)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200130 if err := json.Unmarshal(rp.Payload, &m); err != nil {
131 app.Logger.Error("json.Unmarshal failed: %v", err)
132 return nil, err
133 }
134 app.Logger.Info("newAlarm: %v", m)
135
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200136 if _, ok := alarm.RICAlarmDefinitions[m.Alarm.SpecificProblem]; !ok {
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200137 app.Logger.Warn("Alarm (SP='%d') not recognized, ignoring ...", m.Alarm.SpecificProblem)
138 return nil, nil
139 }
140
141 // Suppress duplicate alarms
142 idx, found := a.IsMatchFound(m.Alarm)
143 if found && m.AlarmAction != alarm.AlarmActionClear {
144 app.Logger.Info("Duplicate alarm ... suppressing!")
145 return nil, nil
146 }
147
148 // Clear alarm if found from active alarm list
149 if m.AlarmAction == alarm.AlarmActionClear {
150 if found {
151 a.activeAlarms = a.RemoveAlarm(a.activeAlarms, idx)
152 app.Logger.Info("Active alarm cleared!")
Mohamed Abukarb2f29a82020-03-17 09:31:55 +0200153
154 if a.postClear {
155 return a.PostAlert(a.GenerateAlertLabels(m.Alarm, AlertStatusResolved))
156 }
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200157 }
Mohamed Abukarb2f29a82020-03-17 09:31:55 +0200158 app.Logger.Info("No matching alarm found, ignoring!")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200159 return nil, nil
160 }
161
162 // New alarm -> update active alarms and post to Alert Manager
163 if m.AlarmAction == alarm.AlarmActionRaise {
164 a.UpdateActiveAlarms(m.Alarm)
Mohamed Abukarb2f29a82020-03-17 09:31:55 +0200165 return a.PostAlert(a.GenerateAlertLabels(m.Alarm, AlertStatusActive))
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200166 }
167
168 return nil, nil
169}
170
171func (a *AlarmAdapter) IsMatchFound(newAlarm alarm.Alarm) (int, bool) {
172 for i, m := range a.activeAlarms {
173 if m.ManagedObjectId == newAlarm.ManagedObjectId && m.ApplicationId == newAlarm.ApplicationId &&
174 m.SpecificProblem == newAlarm.SpecificProblem && m.IdentifyingInfo == newAlarm.IdentifyingInfo {
175 return i, true
176 }
177 }
178 return -1, false
179}
180
181func (a *AlarmAdapter) RemoveAlarm(alarms []alarm.Alarm, i int) []alarm.Alarm {
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200182 a.mutex.Lock()
183 defer a.mutex.Unlock()
184
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200185 copy(alarms[i:], alarms[i+1:])
186 return alarms[:len(alarms)-1]
187}
188
189func (a *AlarmAdapter) UpdateActiveAlarms(newAlarm alarm.Alarm) {
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200190 a.mutex.Lock()
191 defer a.mutex.Unlock()
192
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200193 // For now just keep the active alarms in-memory. Use SDL later
194 a.activeAlarms = append(a.activeAlarms, newAlarm)
195}
196
Mohamed Abukarb2f29a82020-03-17 09:31:55 +0200197func (a *AlarmAdapter) GenerateAlertLabels(newAlarm alarm.Alarm, status AlertStatus) (models.LabelSet, models.LabelSet) {
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200198 alarmDef := alarm.RICAlarmDefinitions[newAlarm.SpecificProblem]
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200199 amLabels := models.LabelSet{
Mohamed Abukarb2f29a82020-03-17 09:31:55 +0200200 "status": string(status),
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200201 "alertname": alarmDef.AlarmText,
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200202 "severity": string(newAlarm.PerceivedSeverity),
203 "service": fmt.Sprintf("%s:%s", newAlarm.ManagedObjectId, newAlarm.ApplicationId),
Mohamed Abukar643241f2020-06-09 15:26:00 +0300204 "system_name": fmt.Sprintf("RIC:%s:%s", newAlarm.ManagedObjectId, newAlarm.ApplicationId),
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200205 }
206 amAnnotations := models.LabelSet{
Mohamed Abukar643241f2020-06-09 15:26:00 +0300207 "alarm_id": fmt.Sprintf("%d", alarmDef.AlarmId),
208 "description": fmt.Sprintf("%d:%s:%s", newAlarm.SpecificProblem, newAlarm.IdentifyingInfo, newAlarm.AdditionalInfo),
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200209 "additional_info": newAlarm.AdditionalInfo,
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200210 "summary": alarmDef.EventType,
211 "instructions": alarmDef.OperationInstructions,
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200212 }
213
214 return amLabels, amAnnotations
215}
216
217func (a *AlarmAdapter) NewAlertmanagerClient() *client.Alertmanager {
218 cr := clientruntime.New(a.amHost, a.amBaseUrl, a.amSchemes)
219 return client.New(cr, strfmt.Default)
220}
221
222func (a *AlarmAdapter) PostAlert(amLabels, amAnnotations models.LabelSet) (*alert.PostAlertsOK, error) {
223 pa := &models.PostableAlert{
224 Alert: models.Alert{
225 GeneratorURL: strfmt.URI(""),
226 Labels: amLabels,
227 },
228 Annotations: amAnnotations,
229 }
230 alertParams := alert.NewPostAlertsParams().WithAlerts(models.PostableAlerts{pa})
231
Mohamed Abukar643241f2020-06-09 15:26:00 +0300232 app.Logger.Info("Posting alerts: labels: %+v, annotations: %+v", amLabels, amAnnotations)
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200233 ok, err := a.NewAlertmanagerClient().Alert.PostAlerts(alertParams)
234 if err != nil {
235 app.Logger.Error("Posting alerts to '%s/%s' failed with error: %v", a.amHost, a.amBaseUrl, err)
236 }
237 return ok, err
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200238}
239
240func (a *AlarmAdapter) StatusCB() bool {
241 if !a.rmrReady {
242 app.Logger.Info("RMR not ready yet!")
243 }
244
245 return a.rmrReady
Mohamed Abukar3e038152020-03-04 10:01:45 +0200246}