package alarm
import (
+ "bytes"
"encoding/json"
"errors"
"fmt"
+ "io/ioutil"
"log"
+ "net/http"
+ "os"
"time"
"unsafe"
- "os"
- "io/ioutil"
)
/*
// The identities are used when raising/clearing alarms, unless provided by the applications.
func InitAlarm(mo, id string) (*RICAlarm, error) {
r := &RICAlarm{
- moId: mo,
- appId: id,
+ moId: mo,
+ appId: id,
+ managerUrl: ALARM_MANAGER_HTTP_URL,
+ rmrEndpoint: ALARM_MANAGER_RMR_URL,
+ }
+
+ //
+ // http service information (used in case of no rmr connectivity)
+ //
+ if os.Getenv("ALARM_MANAGER_URL") != "" {
+ r.managerUrl = os.Getenv("ALARM_MANAGER_URL")
+ }
+
+ if os.Getenv("ALARM_MANAGER_SERVICE_NAME") != "" && os.Getenv("ALARM_MANAGER_SERVICE_PORT") != "" {
+ r.rmrEndpoint = fmt.Sprintf("%s:%s", os.Getenv("ALARM_MANAGER_SERVICE_NAME"), os.Getenv("ALARM_MANAGER_SERVICE_PORT"))
+ }
+
+ if os.Getenv("ALARM_IF_RMR") == "" {
+ if r.moId == "my-pod" {
+ r.rmrEndpoint = "127.0.0.1:4560"
+ } else if r.moId == "my-pod-lib" {
+ r.rmrEndpoint = "127.0.0.1:4588"
+ }
}
+
go InitRMR(r)
return r, nil
ApplicationId: r.appId,
SpecificProblem: sp,
PerceivedSeverity: severity,
- AdditionalInfo: ainfo,
IdentifyingInfo: iinfo,
+ AdditionalInfo: ainfo,
}
}
// Create a new AlarmMessage instance
func (r *RICAlarm) NewAlarmMessage(a Alarm, alarmAction AlarmAction) AlarmMessage {
- alarmTime := time.Now().UnixNano() / 1000
+ alarmTime := time.Now().UnixNano()
return AlarmMessage{a, alarmAction, alarmTime}
}
return fmt.Sprintf(s, a.ManagedObjectId, a.ApplicationId, a.SpecificProblem, a.PerceivedSeverity, a.IdentifyingInfo)
}
-func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
- if r.rmrCtx == nil || !r.rmrReady {
- return errors.New("RMR no ready yet!")
+func (r *RICAlarm) sendAlarmUpdateReqWithHttp(payload []byte) error {
+ 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("HttpError=Post failed with error: %v", err)
}
+ log.Printf("Alarm posted to %s [status=%d]", url, resp.StatusCode)
+ return nil
+}
- log.Println("Sending alarm: ", r.AlarmString(a))
- payload, err := json.Marshal(a)
- if err != nil {
- return err
+func (r *RICAlarm) sendAlarmUpdateReqWithRmr(payload []byte) error {
+ if r.rmrCtx == nil || !r.rmrReady {
+ return fmt.Errorf("RmrError=rmr not ready")
}
-
datap := C.CBytes(payload)
defer C.free(datap)
meid := C.CString("ric")
defer C.free(unsafe.Pointer(meid))
if state := C.rmrSend(r.rmrCtx, RIC_ALARM_UPDATE, datap, C.int(len(payload)), meid); state != C.RMR_OK {
- log.Println("rmrSend failed with error: ", state)
- return errors.New(fmt.Sprintf("rmrSend failed with error: %d", state))
+ return errors.New(fmt.Sprintf("RmrError=rmrSend failed with error: %d", state))
}
+ log.Printf("Alarm sent via rmr to %s", r.rmrEndpoint)
return nil
}
+func (r *RICAlarm) sendAlarmUpdateReq(a AlarmMessage) error {
+
+ 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))
+
+ // --
+ // Try rmr sending
+ // --
+ err = r.sendAlarmUpdateReqWithRmr(payload)
+
+ // --
+ // Try http posting if rmr is not done for some reason: rmrSend error, rmr not initialized yet etc.
+ // --
+ if err != nil {
+ if httperr := r.sendAlarmUpdateReqWithHttp(payload); httperr != nil {
+ err = fmt.Errorf("%s and %s", err.Error(), httperr.Error())
+ } else {
+ err = nil
+ }
+ }
+
+ if err != nil {
+ log.Printf("Alarm sent error %s", err.Error())
+ }
+ return err
+}
+
func (r *RICAlarm) ReceiveMessage(cb func(AlarmMessage)) error {
if rbuf := C.rmrRcv(r.rmrCtx); rbuf != nil {
payload := C.GoBytes(unsafe.Pointer(rbuf.payload), C.int(rbuf.len))
func InitRMR(r *RICAlarm) error {
// Setup static RT for alarm system
- endpoint := "service-ricplt-alarmadapter-rmr.ricplt:4560"
- 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)
+ alarmRT := fmt.Sprintf("newrt|start\nrte|13111|%s\nnewrt|end\n", r.rmrEndpoint)
alarmRTFile := "/tmp/alarm.rt"
if err := ioutil.WriteFile(alarmRTFile, []byte(alarmRT), 0644); err != nil {
return errors.New("rmrInit failed!")
}
+
+func (r *RICAlarm) IsRMRReady() bool {
+ return r.rmrReady
+}