moncheck - add hostname to notifications

This can help with determining which exact check connection broke the
check.
This commit is contained in:
Gibheer 2019-01-11 13:02:37 +01:00
parent f804446ad3
commit 7ed255d591
1 changed files with 10 additions and 5 deletions

View File

@ -67,15 +67,20 @@ func main() {
log.Fatalf("could not open database connection: %s", err)
}
hostname, err := os.Hostname()
if err != nil {
log.Fatalf("could not resolve hostname: %s", err)
}
for i := 0; i < config.Workers; i++ {
go check(i, db, waitDuration, timeout)
go check(i, db, waitDuration, timeout, hostname)
}
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
}
func check(thread int, db *sql.DB, waitDuration, timeout time.Duration) {
func check(thread int, db *sql.DB, waitDuration, timeout time.Duration, hostname string) {
for {
tx, err := db.Begin()
if err != nil {
@ -167,10 +172,10 @@ where check_id = $1`, id, &states, &msg, states.ToOK()); err != nil {
tx.Rollback()
continue
}
if _, err := tx.Exec(`insert into notifications(check_id, states, output, mapping_id, notifier_id)
select $1, $2, $3, $4, cn.notifier_id
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); err != nil {
where cn.check_id = $1`, &id, &states, &msg, &mapId, &hostname); err != nil {
log.Printf("[%d] could not create notification for '%d': %s", thread, id, err)
tx.Rollback()
continue