From 2bfe75c8a702508284ddb75a8db3a1b4911b9635 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Wed, 10 Apr 2019 09:09:15 +0200 Subject: cmd/moncheck: use mapping only for notifications This should make the frontend much easier to handle than before. It isn't the best solution, but it makes it easier in the frontend for now. --- cmd/moncheck/main.go | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/cmd/moncheck/main.go b/cmd/moncheck/main.go index 7253c07..62a1551 100644 --- a/cmd/moncheck/main.go +++ b/cmd/moncheck/main.go @@ -142,30 +142,23 @@ func check(thread int, db *sql.DB, waitDuration, timeout time.Duration, hostname cancel() state = 0 } - - mappedState := state - err = db.QueryRow(`select target - from mapping_level - where mapping_id = $1 and source = $2`, mapId, state).Scan(&mappedState) - if err != nil { - log.Printf("[%d] could not fetch error mapping for check '%d' and source code '%d': %s", thread, id, state, err) - tx.Rollback() - continue - } - states.Add(mappedState) msg := output.String() if _, err := tx.Exec(`update active_checks ac -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 { + set next_time = now() + intval, states = ARRAY[$2::int] || states[1:4], msg = $3, acknowledged = case when $4 then false else acknowledged end +where check_id = $1`, id, &state, &msg, states.ToOK()); err != nil { log.Printf("[%d] could not update row '%d': %s", thread, id, err) tx.Rollback() continue } if _, err := tx.Exec(`insert into notifications(check_id, states, output, mapping_id, notifier_id, check_host) - select $1, $2, $3, $4, cn.notifier_id, $5 - from checks_notify cn - where cn.check_id = $1`, &id, &states, &msg, &mapId, &hostname); err != nil { + select $1, array_agg(ml.target), $2, $3, cn.notifier_id, $4 + from active_checks ac + cross join lateral unnest(ac.states) s + join checks_notify cn on ac.check_id = cn.check_id + join mapping_level ml on ac.mapping_id = ml.mapping_id and s.s = ml.source + where ac.check_id = $1 + group by cn.notifier_id;`, &id, &msg, &mapId, &hostname); err != nil { log.Printf("[%d] could not create notification for '%d': %s", thread, id, err) tx.Rollback() continue -- cgit v1.2.3-70-g09d2