moncheck - move notifications to dedicated table
It would be nice to support multiple notification mechanisms with a way to disable all or part of them. So for now, this is bound to the check but may change again in the future. Apart from that, all notifications get written to the notification table, so that the notifiers can do their job and ACK their own notifications.
This commit is contained in:
parent
02e8142b6d
commit
af843d76e1
@ -81,7 +81,7 @@ func check(thread int, db *sql.DB, waitDuration, timeout time.Duration) {
|
|||||||
log.Printf("[%d] could not start transaction: %s", thread, err)
|
log.Printf("[%d] could not start transaction: %s", thread, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rows, err := tx.Query(`select check_id, cmdLine, states, notify, mapping_id
|
rows, err := tx.Query(`select check_id, cmdLine, states, mapping_id
|
||||||
from active_checks
|
from active_checks
|
||||||
where next_time < now()
|
where next_time < now()
|
||||||
and enabled
|
and enabled
|
||||||
@ -97,7 +97,6 @@ func check(thread int, db *sql.DB, waitDuration, timeout time.Duration) {
|
|||||||
id int64
|
id int64
|
||||||
cmdLine []string
|
cmdLine []string
|
||||||
states States
|
states States
|
||||||
notify bool
|
|
||||||
mapId int
|
mapId int
|
||||||
state int
|
state int
|
||||||
)
|
)
|
||||||
@ -108,7 +107,7 @@ func check(thread int, db *sql.DB, waitDuration, timeout time.Duration) {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
err := rows.Scan(&id, pq.Array(&cmdLine), &states, ¬ify, &mapId)
|
err := rows.Scan(&id, pq.Array(&cmdLine), &states, &mapId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("could not scan values: %s", err)
|
log.Printf("could not scan values: %s", err)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
@ -166,13 +165,14 @@ where check_id = $1`, id, &states, &msg, states.ToOK()); err != nil {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if notify {
|
if _, err := tx.Exec(`insert into notifications(check_id, states, output, mapping_id, notifier_id)
|
||||||
if _, err := tx.Exec("insert into notifications(check_id, states, output, mapping_id) values ($1, $2, $3, $4);", &id, &states, &msg, &mapId); err != nil {
|
select $1, $2, $3, $4, cn.notifier_id
|
||||||
|
from checks_notify cn
|
||||||
|
where cn.check_id = $1`, &id, &states, &msg, &mapId); 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
|
||||||
}
|
}
|
||||||
}
|
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,8 @@ create table public.mapping_level(
|
|||||||
|
|
||||||
CREATE TABLE public.notifier (
|
CREATE TABLE public.notifier (
|
||||||
id serial NOT NULL primary key,
|
id serial NOT NULL primary key,
|
||||||
name text NOT NULL
|
name text NOT NULL,
|
||||||
|
settings jsonb not null '{}'::jsonb,
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE public.groups (
|
CREATE TABLE public.groups (
|
||||||
@ -57,8 +58,6 @@ CREATE TABLE public.checks (
|
|||||||
updated timestamp with time zone DEFAULT now() NOT NULL,
|
updated timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
last_refresh timestamp with time zone,
|
last_refresh timestamp with time zone,
|
||||||
enabled boolean DEFAULT true NOT NULL,
|
enabled boolean DEFAULT true NOT NULL,
|
||||||
notifier_id integer NOT NULL,
|
|
||||||
notify boolean DEFAULT true NOT NULL,
|
|
||||||
message text NOT NULL,
|
message text NOT NULL,
|
||||||
unique(node_id, command_id, options)
|
unique(node_id, command_id, options)
|
||||||
);
|
);
|
||||||
@ -71,20 +70,27 @@ CREATE TABLE public.active_checks (
|
|||||||
states integer[] DEFAULT ARRAY[0] NOT NULL,
|
states integer[] DEFAULT ARRAY[0] NOT NULL,
|
||||||
intval interval NOT NULL,
|
intval interval NOT NULL,
|
||||||
enabled boolean NOT NULL,
|
enabled boolean NOT NULL,
|
||||||
notify boolean NOT NULL,
|
|
||||||
notice text,
|
notice text,
|
||||||
msg text NOT NULL,
|
msg text NOT NULL,
|
||||||
acknowledged boolean DEFAULT false NOT NULL
|
acknowledged boolean DEFAULT false NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
create table checks_notify(
|
||||||
|
check_id bigint not null references checks(id),
|
||||||
|
notifier_id bigint not null references notifier(id),
|
||||||
|
enabled bool not null default true,
|
||||||
|
unique(check_id, notifier_id)
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE public.notifications (
|
CREATE TABLE public.notifications (
|
||||||
id bigserial NOT NULL primary key,
|
id bigserial NOT NULL primary key,
|
||||||
check_id bigint NOT NULL references checks(id) on delete cascade,
|
check_id bigint NOT NULL references checks(id) on delete cascade,
|
||||||
|
mapping_id integer not null references mappings(id),
|
||||||
|
notifier_id integer not null references notifier(id),
|
||||||
states integer[] NOT NULL,
|
states integer[] NOT NULL,
|
||||||
output text,
|
output text,
|
||||||
inserted timestamp with time zone DEFAULT now() NOT NULL,
|
inserted timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
sent timestamp with time zone,
|
sent timestamp with time zone,
|
||||||
mapping_id integer not null references mappings(id),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user