Compare commits

..

No commits in common. "3af2cd4d17d252b0ed7b8c565f0fb9ad62810db6" and "fe9571c4665231eca8082a72d4f54b6a6b506e50" have entirely different histories.

3 changed files with 17 additions and 78 deletions

14
main.go
View File

@ -205,7 +205,7 @@ func entryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { /
logger.Error("entry", "error", err) logger.Error("entry", "error", err)
} }
// Multiple triggers can use the same datapoint. // Multiple triggers can use the same datapoint.
for _, trigger := range triggers { for _, trigger := range triggers {
var out any var out any
out, err = trigger.Run() 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) logger.Debug("entry", "datapoint", dpoint, "value", value, "trigger", trigger, "result", out)
var problemID int var problemID int
var problemState string
switch v := out.(type) { switch v := out.(type) {
case bool: case bool:
// Trigger returning true - a problem occurred // Trigger returning true - a problem occurred
if v { if v {
problemID, err = ProblemStart(trigger) problemID, err = ProblemStart(trigger)
logger.Info("FOO", "problemID", problemID, "err==nil", err == nil)
if err != nil { if err != nil {
err = werr.Wrap(err).Log() err = werr.Wrap(err).Log()
logger.Error("entry", "error", err) logger.Error("entry", "error", err)
} }
problemState = "PROBLEM"
} else { } else {
// A problem didn't occur. // A problem didn't occur.
problemID, err = ProblemClose(trigger) problemID, err = ProblemClose(trigger)
logger.Info("FOO", "problemID", problemID, "err==nil", err == nil)
if err != nil { if err != nil {
err = werr.Wrap(err).Log() err = werr.Wrap(err).Log()
logger.Error("entry", "error", err) logger.Error("entry", "error", err)
} }
problemState = "OK"
} }
// Has a change in problem state happened? // 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") 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( logger.Info(
"notification", "notification",
"service", (*notificationService).GetType(), "service", (*notificationService).GetType(),
@ -617,12 +617,12 @@ func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) {
PAGE: "datapoint_values", PAGE: "datapoint_values",
MENU: "datapoints", MENU: "datapoints",
Icon: "datapoints", Icon: "datapoints",
Label: "Values for " + datapoint.Name, Label: "Values for "+datapoint.Name,
} }
page.Data = map[string]any{ page.Data = map[string]any{
"Datapoint": datapoint, "Datapoint": datapoint,
"Values": values, "Values": values,
} }
page.Render(w) page.Render(w)
return return

View File

@ -4,95 +4,38 @@ import (
// External // External
werr "git.gibonuddevalla.se/go/wrappederror" werr "git.gibonuddevalla.se/go/wrappederror"
// Internal
"smon/notification"
// Standard // Standard
"database/sql" "database/sql"
"encoding/json"
"time" "time"
) )
var (
nodataAreaID int
nodataSectionID int
)
// nodataLoop checks if datapoint last_value is larger than the nodata_problem_seconds period and // nodataLoop checks if datapoint last_value is larger than the nodata_problem_seconds period and
// marks them as problems. They are then notified. // marks them as problems. They are then notified.
func nodataLoop() { func nodataLoop() {
var datapoints []Datapoint var ids []int
var err error var err error
// TODO - should be configurable // TODO - should be configurable
ticker := time.NewTicker(time.Second * 5) ticker := time.NewTicker(time.Second * 5)
for { for {
<-ticker.C <-ticker.C
datapoints, err = nodataDatapoints() ids, err = nodataDatapointIDs()
if err != nil { if err != nil {
err = werr.Wrap(err).Log() err = werr.Wrap(err).Log()
logger.Error("nodata", "error", err) logger.Error("nodata", "error", err)
continue continue
} }
if len(datapoints) == 0 { if len(ids) == 0 {
continue continue
} }
for _, datapoint := range datapoints { logger.Info("nodata", "problem_ids", ids)
nodataNotify(datapoint, "[NODATA] problem")
}
logger.Info("nodata", "problem_ids", datapoints)
} }
} }
func nodataNotify(datapoint Datapoint, state string) (err error) { func nodataDatapointIDs() (ids []int, err error) {
err = notificationManager.Send(-1, []byte(state+": "+datapoint.Name), func(notificationService *notification.Service, err error) { ids = []int{}
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{}
var rows *sql.Rows var rows *sql.Rows
rows, err = service.Db.Conn.Query(` rows, err = service.Db.Conn.Query(`
@ -111,8 +54,7 @@ func nodataDatapoints() (datapoints []Datapoint, err error) {
WHERE WHERE
datapoint.id = subquery.id datapoint.id = subquery.id
RETURNING RETURNING
datapoint.id, datapoint.id
datapoint.name
`) `)
if err != nil { if err != nil {
@ -121,13 +63,13 @@ func nodataDatapoints() (datapoints []Datapoint, err error) {
} }
defer rows.Close() defer rows.Close()
var dp Datapoint var id int
for rows.Next() { for rows.Next() {
if err = rows.Scan(&dp.ID, &dp.Name); err != nil { if err = rows.Scan(&id); err != nil {
err = werr.Wrap(err) err = werr.Wrap(err)
return return
} }
datapoints = append(datapoints, dp) ids = append(ids, id)
} }
return return
} }

View File

@ -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;