package alarm
import (
+ "bytes"
"encoding/json"
"errors"
"fmt"
+ "io/ioutil"
"log"
- "sync"
+ "net/http"
+ "os"
"time"
"unsafe"
)
/*
#cgo CFLAGS: -I../
-#cgo LDFLAGS: -lrmr_nng -lnng
+#cgo LDFLAGS: -lrmr_si
#include "utils.h"
*/
import "C"
-// Severity for alarms
-type Severity string
-
-// Possible values for Severity
-const (
- SeverityUnspecified Severity = "UNSPECIFIED"
- SeverityCritical Severity = "CRITICAL"
- SeverityMajor Severity = "MAJOR"
- SeverityMinor Severity = "MINOR"
- SeverityWarning Severity = "WARNING"
- SeverityCleared Severity = "CLEARED"
- SeverityDefault Severity = "DEFAULT"
-)
-
-// Alarm object - see README for more information
-type Alarm struct {
- ManagedObjectId string `json:"managedObjectId"`
- ApplicationId string `json:"applicationId"`
- SpecificProblem int `json:"specificProblem"`
- PerceivedSeverity Severity `json:"perceivedSeverity"`
- AdditionalInfo string `json:"additionalInfo"`
- IdentifyingInfo string `json:"identifyingInfo"`
-}
-
-// Alarm actions
-type AlarmAction string
-
-// Possible values for alarm actions
-const (
- AlarmActionRaise AlarmAction = "RAISE"
- AlarmActionClear AlarmAction = "CLEAR"
- AlarmActionClearAll AlarmAction = "CLEARALL"
-)
-
-type AlarmMessage struct {
- Alarm
- AlarmAction
- AlarmTime int64
-}
-
-// RICAlarm is an alarm instance
-type RICAlarm struct {
- moId string
- appId string
- rmrCtx unsafe.Pointer
- mutex sync.Mutex
-}
-
-const (
- RIC_ALARM_UPDATE = 13111
- RIC_ALARM_QUERY = 13112
-)
-
// InitAlarm is the init routine which returns a new alarm instance.
// The MO and APP identities are given as a parameters.
// The identities are used when raising/clearing alarms, unless provided by the applications.
func InitAlarm(mo, id string) (*RICAlarm, error) {
- if ctx := C.rmrInit(); ctx != nil {
- r := &RICAlarm{
- moId: mo,
- appId: id,
- rmrCtx: ctx,
- }
+ r := &RICAlarm{
+ moId: mo,
+ appId: id,
+ managerUrl: ALARM_MANAGER_HTTP_URL,
+ }
+
+ if os.Getenv("ALARM_MANAGER_URL") != "" {
+ r.managerUrl = os.Getenv("ALARM_MANAGER_URL")
+ }
- return r, nil
+ if os.Getenv("ALARM_IF_RMR") != "" {
+ go InitRMR(r)
}
- return nil, errors.New("rmrInit failed!")
+ return r, nil
}
// Create a new Alarm instance
return AlarmMessage{a, alarmAction, alarmTime}
}
+func (r *RICAlarm) SetManagedObjectId(mo string) {
+ r.moId = mo
+}
+
+func (r *RICAlarm) SetApplicationId(app string) {
+ r.appId = app
+}
+
// Raise a RIC alarm
func (r *RICAlarm) Raise(a Alarm) error {
r.mutex.Lock()
}
func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
- log.Println("Sending alarm: ", r.AlarmString(a))
-
payload, err := json.Marshal(a)
if err != nil {
+ log.Println("json.Marshal failed with error: ", err)
return err
}
+ log.Println("Sending alarm: ", fmt.Sprintf("%s", payload))
+
+ if r.rmrCtx == nil || !r.rmrReady {
+ url := fmt.Sprintf("%s/%s", r.managerUrl, "ric/v1/alarms")
+ resp, err := http.Post(url, "application/json", bytes.NewReader(payload))
+ if err != nil || resp == nil {
+ return fmt.Errorf("Unable to send alarm: %v", err)
+ }
+ log.Printf("Alarm posted to %s [status=%d]", url, resp.StatusCode)
+ return nil
+ }
datap := C.CBytes(payload)
defer C.free(datap)
}
return errors.New("rmrRcv failed!")
}
+
+func InitRMR(r *RICAlarm) error {
+ // Setup static RT for alarm system
+ endpoint := ALARM_MANAGER_RMR_URL
+ if r.moId == "my-pod" {
+ endpoint = "localhost:4560"
+ } else if r.moId == "my-pod-lib" {
+ endpoint = "localhost:4588"
+ }
+
+ alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", endpoint)
+ alarmRTFile := "/tmp/alarm.rt"
+
+ if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil {
+ log.Println("ioutil.WriteFile failed with error: ", err)
+ return err
+ }
+
+ os.Setenv("RMR_SEED_RT", alarmRTFile)
+ os.Setenv("RMR_RTG_SVC", "-1")
+
+ if ctx := C.rmrInit(); ctx != nil {
+ r.rmrCtx = ctx
+ r.rmrReady = true
+ return nil
+ }
+
+ return errors.New("rmrInit failed!")
+}