"io/ioutil"
"net/http"
"os"
+ "strconv"
+ "strings"
"time"
- "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
+ "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm"
app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
clientruntime "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
tick := time.Tick(time.Duration(a.alertInterval) * time.Millisecond)
for range tick {
a.mutex.Lock()
+
+ a.ProcessAlerts()
for _, m := range a.activeAlarms {
app.Logger.Info("Re-raising alarm: %v", m)
a.PostAlert(a.GenerateAlertLabels(m.AlarmId, m.Alarm, AlertStatusActive, m.AlarmTime))
}
func (a *AlarmManager) GenerateAlertLabels(alarmId int, newAlarm alarm.Alarm, status AlertStatus, alarmTime int64) (models.LabelSet, models.LabelSet) {
+ if strings.Contains(newAlarm.ApplicationId, "FM") {
+ app.Logger.Info("Alarm '%d' is originated from FM, ignoring ...", alarmId)
+ return 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),
+ "info": newAlarm.IdentifyingInfo,
"system_name": "RIC",
}
amAnnotations := models.LabelSet{
"identifying_info": newAlarm.IdentifyingInfo,
"additional_info": newAlarm.AdditionalInfo,
"description": fmt.Sprintf("%s:%s", newAlarm.IdentifyingInfo, newAlarm.AdditionalInfo),
+ "summary": newAlarm.IdentifyingInfo,
"instructions": alarmDef.OperationInstructions,
"timestamp": fmt.Sprintf("%s", time.Unix(0, alarmTime).Format("02/01/2006, 15:04:05")),
}
return amLabels, amAnnotations
}
-func (a *AlarmManager) NewAlertmanagerClient() *client.Alertmanager {
+func (a *AlarmManager) NewAlertmanagerClient() *client.AlertmanagerAPI {
cr := clientruntime.New(a.amHost, a.amBaseUrl, a.amSchemes)
return client.New(cr, strfmt.Default)
}
func (a *AlarmManager) PostAlert(amLabels, amAnnotations models.LabelSet) (*alert.PostAlertsOK, error) {
+ if len(amLabels) == 0 || len(amAnnotations) == 0 {
+ return &alert.PostAlertsOK{}, nil
+ }
+
pa := &models.PostableAlert{
Alert: models.Alert{
- GeneratorURL: strfmt.URI(""),
+ GeneratorURL: strfmt.URI("http://service-ricplt-alarmmanager-http.ricplt:8080/ric/v1/alarms"),
Labels: amLabels,
},
Annotations: amAnnotations,
app.Logger.Info("Posting alerts: labels: %+v, annotations: %+v", amLabels, amAnnotations)
ok, err := a.NewAlertmanagerClient().Alert.PostAlerts(alertParams)
if err != nil {
- app.Logger.Error("Posting alerts to '%s/%s' failed with error: %v", a.amHost, a.amBaseUrl, err)
+ app.Logger.Error("Posting alerts to '%s/%s' failed: %v", a.amHost, a.amBaseUrl, err)
}
return ok, err
}
+func (a *AlarmManager) GetAlerts() (*alert.GetAlertsOK, error) {
+ active := true
+ alertParams := alert.NewGetAlertsParams()
+ alertParams.Active = &active
+ resp, err := a.NewAlertmanagerClient().Alert.GetAlerts(alertParams)
+ if err != nil {
+ app.Logger.Error("Getting alerts from '%s/%s' failed: %v", a.amHost, a.amBaseUrl, err)
+ return resp, nil
+ }
+ app.Logger.Info("GetAlerts: %+v", resp)
+
+ return resp, err
+}
+
+func (a *AlarmManager) ProcessAlerts() {
+ resp, err := a.GetAlerts()
+ if err != nil || resp == nil {
+ app.Logger.Error("Getting alerts from '%s/%s' failed: %v", a.amHost, a.amBaseUrl, err)
+ return
+ }
+
+ var buildAlarm = func(alert *models.GettableAlert) alarm.Alarm {
+ a := alarm.Alarm{ManagedObjectId: "SEP", ApplicationId: "FM"}
+
+ if v, ok := alert.Alert.Labels["specific_problem"]; ok {
+ sp, _ := strconv.Atoi(v)
+ a.SpecificProblem = sp
+ }
+
+ if v, ok := alert.Alert.Labels["severity"]; ok {
+ a.PerceivedSeverity = alarm.Severity(fmt.Sprint(v))
+ }
+
+ if v, ok := alert.Alert.Labels["name"]; ok {
+ a.AdditionalInfo = v
+ }
+
+ if v, ok := alert.Annotations["description"]; ok {
+ a.IdentifyingInfo = v
+ }
+
+ return a
+ }
+
+ // Remove cleared alerts first
+ for _, m := range a.activeAlarms {
+ if m.ApplicationId != "FM" {
+ continue
+ }
+
+ found := false
+ for _, alert := range resp.Payload {
+ v, ok := alert.Alert.Labels["service"]
+ if !ok || !strings.Contains(v, "FM") {
+ continue
+ }
+
+ a := buildAlarm(alert)
+ if m.ManagedObjectId == a.ManagedObjectId && m.ApplicationId == a.ApplicationId &&
+ m.SpecificProblem == a.SpecificProblem && m.IdentifyingInfo == a.IdentifyingInfo {
+ found = true
+ break
+ }
+ }
+
+ if !found {
+ m.AlarmAction = alarm.AlarmActionClear
+ go a.ProcessAlarm(&m)
+ }
+ }
+
+ for _, alert := range resp.Payload {
+ v, ok := alert.Alert.Labels["service"]
+ if ok && strings.Contains(v, "FM") {
+ m := alarm.AlarmMessage{Alarm: buildAlarm(alert), AlarmAction: alarm.AlarmActionRaise, AlarmTime: time.Now().UnixNano()}
+ go a.ProcessAlarm(&AlarmNotification{m, alarm.AlarmDefinition{}})
+ }
+ }
+}
+
func (a *AlarmManager) StatusCB() bool {
if !a.rmrReady {
app.Logger.Info("RMR not ready yet!")
}
-
return a.rmrReady
}
func (a *AlarmManager) ConfigChangeCB(configparam string) {
-
a.maxActiveAlarms = app.Config.GetInt("controls.maxActiveAlarms")
+ if a.maxActiveAlarms == 0 {
+ a.maxActiveAlarms = 5000
+ }
+
a.maxAlarmHistory = app.Config.GetInt("controls.maxAlarmHistory")
+ if a.maxAlarmHistory == 0 {
+ a.maxAlarmHistory = 20000
+ }
+
a.alertInterval = viper.GetInt("controls.promAlertManager.alertInterval")
a.amHost = viper.GetString("controls.promAlertManager.address")
var alarmpersistentinfo AlarmPersistentInfo
byteValue, rerr := ioutil.ReadFile(a.alarmInfoPvFile)
if rerr != nil {
- app.Logger.Error("ararminfo.json file read error %v", rerr)
+ app.Logger.Info("Unable to read alarminfo.json : %v", rerr)
} else {
err := json.Unmarshal(byteValue, &alarmpersistentinfo)
if err != nil {
a.ReadAlarmInfoFromPersistentVolume()
- app.RunWithParams(a, sdlcheck)
+ time.Sleep(8 * time.Second)
+ app.RunWithRunParams(a, app.RunParams{SdlCheck: sdlcheck, DisableAlarmClient: true})
}
func NewAlarmManager(amHost string, alertInterval int, clearAlarm bool) *AlarmManager {
amHost = viper.GetString("controls.promAlertManager.address")
}
+ maxActiveAlarms := app.Config.GetInt("controls.maxActiveAlarms")
+ if maxActiveAlarms == 0 {
+ maxActiveAlarms = 5000
+ }
+
+ maxAlarmHistory := app.Config.GetInt("controls.maxAlarmHistory")
+ if maxAlarmHistory == 0 {
+ maxAlarmHistory = 20000
+ }
+
return &AlarmManager{
rmrReady: false,
postClear: clearAlarm,
activeAlarms: make([]AlarmNotification, 0),
alarmHistory: make([]AlarmNotification, 0),
uniqueAlarmId: 0,
- maxActiveAlarms: app.Config.GetInt("controls.maxActiveAlarms"),
- maxAlarmHistory: app.Config.GetInt("controls.maxAlarmHistory"),
+ maxActiveAlarms: maxActiveAlarms,
+ maxAlarmHistory: maxAlarmHistory,
exceededActiveAlarmOn: false,
exceededAlarmHistoryOn: false,
alarmInfoPvFile: app.Config.GetString("controls.alarmInfoPvFile"),