Compare commits
No commits in common. "3af2cd4d17d252b0ed7b8c565f0fb9ad62810db6" and "fe9571c4665231eca8082a72d4f54b6a6b506e50" have entirely different histories.
3af2cd4d17
...
fe9571c466
14
main.go
14
main.go
@ -205,7 +205,7 @@ func entryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { /
|
||||
logger.Error("entry", "error", err)
|
||||
}
|
||||
|
||||
// Multiple triggers can use the same datapoint.
|
||||
// Multiple triggers can use the same datapoint.
|
||||
for _, trigger := range triggers {
|
||||
var out any
|
||||
out, err = trigger.Run()
|
||||
@ -217,25 +217,25 @@ func entryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { /
|
||||
logger.Debug("entry", "datapoint", dpoint, "value", value, "trigger", trigger, "result", out)
|
||||
|
||||
var problemID int
|
||||
var problemState string
|
||||
switch v := out.(type) {
|
||||
case bool:
|
||||
// Trigger returning true - a problem occurred
|
||||
if v {
|
||||
problemID, err = ProblemStart(trigger)
|
||||
logger.Info("FOO", "problemID", problemID, "err==nil", err == nil)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err).Log()
|
||||
logger.Error("entry", "error", err)
|
||||
}
|
||||
problemState = "PROBLEM"
|
||||
|
||||
} else {
|
||||
// A problem didn't occur.
|
||||
problemID, err = ProblemClose(trigger)
|
||||
logger.Info("FOO", "problemID", problemID, "err==nil", err == nil)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err).Log()
|
||||
logger.Error("entry", "error", err)
|
||||
}
|
||||
problemState = "OK"
|
||||
}
|
||||
|
||||
// Has a change in problem state happened?
|
||||
@ -246,7 +246,7 @@ func entryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { /
|
||||
logger.Debug("notification", "trigger", trigger.ID, "state", "change")
|
||||
}
|
||||
|
||||
err = notificationManager.Send(problemID, []byte(problemState+": "+trigger.Name), func(notificationService *notification.Service, err error) {
|
||||
err = notificationManager.Send(problemID, []byte(trigger.Name), func(notificationService *notification.Service, err error) {
|
||||
logger.Info(
|
||||
"notification",
|
||||
"service", (*notificationService).GetType(),
|
||||
@ -617,12 +617,12 @@ func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) {
|
||||
PAGE: "datapoint_values",
|
||||
MENU: "datapoints",
|
||||
Icon: "datapoints",
|
||||
Label: "Values for " + datapoint.Name,
|
||||
Label: "Values for "+datapoint.Name,
|
||||
}
|
||||
|
||||
page.Data = map[string]any{
|
||||
"Datapoint": datapoint,
|
||||
"Values": values,
|
||||
"Values": values,
|
||||
}
|
||||
page.Render(w)
|
||||
return
|
||||
|
78
nodata.go
78
nodata.go
@ -4,95 +4,38 @@ import (
|
||||
// External
|
||||
werr "git.gibonuddevalla.se/go/wrappederror"
|
||||
|
||||
// Internal
|
||||
"smon/notification"
|
||||
|
||||
// Standard
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
nodataAreaID int
|
||||
nodataSectionID int
|
||||
)
|
||||
|
||||
// nodataLoop checks if datapoint last_value is larger than the nodata_problem_seconds period and
|
||||
// marks them as problems. They are then notified.
|
||||
func nodataLoop() {
|
||||
var datapoints []Datapoint
|
||||
var ids []int
|
||||
var err error
|
||||
|
||||
// TODO - should be configurable
|
||||
ticker := time.NewTicker(time.Second * 5)
|
||||
for {
|
||||
<-ticker.C
|
||||
datapoints, err = nodataDatapoints()
|
||||
ids, err = nodataDatapointIDs()
|
||||
if err != nil {
|
||||
err = werr.Wrap(err).Log()
|
||||
logger.Error("nodata", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(datapoints) == 0 {
|
||||
if len(ids) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, datapoint := range datapoints {
|
||||
nodataNotify(datapoint, "[NODATA] problem")
|
||||
}
|
||||
|
||||
logger.Info("nodata", "problem_ids", datapoints)
|
||||
logger.Info("nodata", "problem_ids", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func nodataNotify(datapoint Datapoint, state string) (err error) {
|
||||
err = notificationManager.Send(-1, []byte(state+": "+datapoint.Name), func(notificationService *notification.Service, err error) {
|
||||
logger.Info(
|
||||
"notification",
|
||||
"service", (*notificationService).GetType(),
|
||||
"datapointID", datapoint.ID,
|
||||
"prio", (*notificationService).GetPrio(),
|
||||
"ok", true,
|
||||
)
|
||||
|
||||
var errBody any
|
||||
if err != nil {
|
||||
errBody, _ = json.Marshal(err)
|
||||
} else {
|
||||
errBody = nil
|
||||
}
|
||||
_, err = service.Db.Conn.Exec(
|
||||
`
|
||||
INSERT INTO notification_send(notification_id, datapoint_nodata_id, uuid, ok, error)
|
||||
SELECT
|
||||
id, $3, '', $4, $5
|
||||
FROM notification
|
||||
WHERE
|
||||
service=$1 AND
|
||||
prio=$2
|
||||
`,
|
||||
(*notificationService).GetType(),
|
||||
(*notificationService).GetPrio(),
|
||||
datapoint.ID,
|
||||
err == nil,
|
||||
errBody,
|
||||
)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err).Log()
|
||||
logger.Error("entry", "error", err)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
err = werr.Wrap(err).Log()
|
||||
logger.Error("notification", "error", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func nodataDatapoints() (datapoints []Datapoint, err error) {
|
||||
datapoints = []Datapoint{}
|
||||
func nodataDatapointIDs() (ids []int, err error) {
|
||||
ids = []int{}
|
||||
|
||||
var rows *sql.Rows
|
||||
rows, err = service.Db.Conn.Query(`
|
||||
@ -111,8 +54,7 @@ func nodataDatapoints() (datapoints []Datapoint, err error) {
|
||||
WHERE
|
||||
datapoint.id = subquery.id
|
||||
RETURNING
|
||||
datapoint.id,
|
||||
datapoint.name
|
||||
datapoint.id
|
||||
|
||||
`)
|
||||
if err != nil {
|
||||
@ -121,13 +63,13 @@ func nodataDatapoints() (datapoints []Datapoint, err error) {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var dp Datapoint
|
||||
var id int
|
||||
for rows.Next() {
|
||||
if err = rows.Scan(&dp.ID, &dp.Name); err != nil {
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
err = werr.Wrap(err)
|
||||
return
|
||||
}
|
||||
datapoints = append(datapoints, dp)
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -1,3 +0,0 @@
|
||||
ALTER TABLE public.notification_send ALTER COLUMN problem_id DROP NOT NULL;
|
||||
ALTER TABLE public.notification_send ADD datapoint_nodata_id int4 NULL;
|
||||
ALTER TABLE public.notification_send ADD CONSTRAINT notification_send_datapoint_fk FOREIGN KEY (datapoint_nodata_id) REFERENCES public.datapoint(id) ON DELETE CASCADE ON UPDATE CASCADE;
|
Loading…
Reference in New Issue
Block a user