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.
This commit is contained in:
Gibheer 2019-04-10 09:09:15 +02:00
parent 532f55d640
commit 2bfe75c8a7

View File

@ -142,30 +142,23 @@ func check(thread int, db *sql.DB, waitDuration, timeout time.Duration, hostname
cancel() cancel()
state = 0 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() msg := output.String()
if _, err := tx.Exec(`update active_checks ac 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 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, &states, &msg, states.ToOK()); err != nil { where check_id = $1`, id, &state, &msg, states.ToOK()); err != nil {
log.Printf("[%d] could not update row '%d': %s", thread, id, err) log.Printf("[%d] could not update row '%d': %s", thread, id, err)
tx.Rollback() tx.Rollback()
continue continue
} }
if _, err := tx.Exec(`insert into notifications(check_id, states, output, mapping_id, notifier_id, check_host) 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 select $1, array_agg(ml.target), $2, $3, cn.notifier_id, $4
from checks_notify cn from active_checks ac
where cn.check_id = $1`, &id, &states, &msg, &mapId, &hostname); err != nil { 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) log.Printf("[%d] could not create notification for '%d': %s", thread, id, err)
tx.Rollback() tx.Rollback()
continue continue