From ddc0053ce8eda6001df8de1b30cf2395c625846e Mon Sep 17 00:00:00 2001 From: Gibheer Date: Tue, 20 Nov 2018 21:47:35 +0100 Subject: moncheck - add function to remove acknowledge An acknowledge should be set to false when the alarm switches to an ok state. --- cmd/moncheck/main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/moncheck/main.go b/cmd/moncheck/main.go index 82b3d1f..fbc2b57 100644 --- a/cmd/moncheck/main.go +++ b/cmd/moncheck/main.go @@ -131,7 +131,9 @@ func check(thread int, db *sql.DB, waitDuration time.Duration) { } msg := output.String() - if _, err := tx.Exec("update active_checks set next_time = now() + intval, states = $2 where check_id = $1", id, &states); err != nil { + if _, err := tx.Exec(`update active_checks +set next_time = now() + intval, states = $2, msg = $3, acknowledged = case when $4 then false else acknowledged end +where check_id = $1`, id, &states, &msg, states.ToOK()); err != nil { log.Printf("[%d] could not update row '%d': %s", thread, id, err) tx.Rollback() continue @@ -203,3 +205,18 @@ func (s *States) Add(state int) { *s = append([]int{state}, vals[:statePos]...) return } + +// ToOK returns true when the state returns from != 0 to 0. +func (s *States) ToOK() bool { + vals := *s + if len(vals) == 0 { + return false + } + if len(vals) <= 1 { + return vals[0] == 0 + } + if vals[0] == 0 && vals[1] > 0 { + return true + } + return false +} -- cgit v1.2.3-70-g09d2