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