+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{}})
+ }
+ }
+}
+