Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings for the number of repeated failures to happen before sending the notifications #99

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 26 additions & 6 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ class Monitor extends BeanModel {

start(io) {
let previousBeat = null;

const beat = async () => {
if (! previousBeat) {
previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [
this.id
])
]);
}

let bean = R.dispense("heartbeat")
Expand Down Expand Up @@ -111,9 +110,13 @@ class Monitor extends BeanModel {
bean.msg = error.message;
}

// Mark as important if status changed
if (! previousBeat || previousBeat.status !== bean.status) {
bean.important = true;
// Do the nottifications if status changed or if two subsequent beats are faulty
if (! previousBeat || previousBeat.status !== bean.status || (previousBeat && previousBeat.status !== 1 && bean.status != 1)) {
// Don't mark as important two subsequent faulty beats
let repeatedFailures = (previousBeat && previousBeat.status !== 1 && bean.status != 1);
let goUpAfterRepeatedFailures = false;
if(repeatedFailures) bean.important = false;
else bean.important = true;

// Do not send if first beat is UP
if (previousBeat || bean.status !== 1) {
Expand All @@ -133,7 +136,24 @@ class Monitor extends BeanModel {
let msg = `[${this.name}] [${text}] ${bean.msg}`;

for(let notification of notificationList) {
promiseList.push(Notification.send(JSON.parse(notification.config), msg, await this.toJSON(), bean.toJSON()));
let notificationConfig = JSON.parse(notification.config);
//Check for how many subsequent failures should happen before sending the notification
notificationConfig.failThreshold = notificationConfig.failThreshold || 1;
if(repeatedFailures && notificationConfig.failThreshold > 1) {
let limit = notificationConfig.failThreshold;
let query = await R.find("heartbeat", " monitor_id = ? ORDER BY time DESC LIMIT " + limit, [
this.id
]);
query = query.map(val => val.status);
// All the heartbeats are failed except the oldest one
// Current beat is failed too
goUpAfterRepeatedFailures = query.slice(0,-1).every( val => val === query[0] );
repeatedFailures = bean.status !== 1 && bean.status === query[0] && query.slice(0,-1).every( val => val === query[0] ) && [...query].reverse()[0] !== [...query].reverse()[1];
}
if((notificationConfig.failThreshold <= 1 && bean.status === 1) ||
(notificationConfig.failThreshold > 1 && bean.status === 1 && goUpAfterRepeatedFailures) ||
(notificationConfig.failThreshold <= 1 && !repeatedFailures) ||
(notificationConfig.failThreshold > 1 && repeatedFailures)) promiseList.push(Notification.send(notificationConfig, msg, await this.toJSON(), bean.toJSON()));
}

await Promise.all(promiseList);
Expand Down
3 changes: 3 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ let needSetup = false;
try {
checkLogin(socket)

//Sanitize the threshold
notification.failThreshold = Math.abs(parseInt(notification.failThreshold) || 1);

await Notification.save(notification, notificationID, socket.userID)
await sendNotificationList(socket)

Expand Down
9 changes: 9 additions & 0 deletions src/components/NotificationDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
<input type="text" class="form-control" id="name" required v-model="notification.name">
</div>

<div class="mb-3">
<label for="type" class="form-label">Number of failures in a row before sending the notification</label>
<input type="text" class="form-control" id="fail-threshold" required v-model="notification.failThreshold">
<div class="form-text">
<p>Only positive integer numbers allowed. Numbers higher than 1 may cause more heavy queries & more load for the server!</p>
</div>
</div>

<template v-if="notification.type === 'telegram'">
<div class="mb-3">
<label for="telegram-bot-token" class="form-label">Bot Token</label>
Expand Down Expand Up @@ -368,6 +376,7 @@ export default {
// Default set to Telegram
this.notification.type = "telegram"
this.notification.gotifyPriority = 8
this.notification.gotifyPriority = 1
}

this.modal.show()
Expand Down