1
0
mirror of https://github.com/nttgin/BGPalerter.git synced 2024-05-19 06:50:08 +00:00
Files
nttgin-BGPalerter/monitors/monitor.js

180 lines
5.5 KiB
JavaScript
Raw Normal View History

2019-06-28 03:46:48 +02:00
2019-07-10 18:29:01 +02:00
/*
* BSD 3-Clause License
*
* Copyright (c) 2019, NTT Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2019-06-28 03:46:48 +02:00
export default class Monitor {
constructor(name, channel, params, env) {
this.config = env.config;
this.pubSub = env.pubSub;
this.logger = env.logger;
this.input = env.input;
this.params = params;
2019-06-28 03:46:48 +02:00
this.name = name;
this.channel = channel;
this.monitored = [];
this.alerts = {};
this.sent = {};
this.updateMonitoredPrefixes();
setInterval(this._publish, this.config.checkStaleNotificationsSeconds * 1000)
};
updateMonitoredPrefixes = () => {
this.monitored = this.input.getMonitoredPrefixes();
};
monitor = (message) =>
new Promise((resolve, reject) => {
reject("You must implement a monitor method");
});
filter = (message) => {
throw new Error('The method filter must be implemented in ' + this.name);
};
squashAlerts = (alerts) => {
throw new Error('The method squashAlerts must be implemented in ' + this.name);
};
_squash = (alerts) => {
2019-07-09 02:46:08 +02:00
const message = this.squashAlerts(alerts);
2019-06-28 03:46:48 +02:00
2019-07-09 02:46:08 +02:00
if (message) {
const firstAlert = alerts[0];
const id = firstAlert.id;
let earliest = Infinity;
let latest = -Infinity;
2019-06-28 03:46:48 +02:00
2019-07-09 02:46:08 +02:00
for (let alert of alerts) {
2019-06-28 03:46:48 +02:00
2019-07-09 02:46:08 +02:00
earliest = Math.min(alert.timestamp, earliest);
latest = Math.max(alert.timestamp, latest);
if (id !== alert.id) {
throw new Error('Squash MUST receive a list of events all with the same ID.');
}
2019-06-28 03:46:48 +02:00
}
2019-07-09 02:46:08 +02:00
return {
id,
origin: this.name,
earliest,
latest,
affected: firstAlert.affected,
message,
data: alerts.map(a => {
return {
extra: a.extra,
matchedRule: a.matchedRule,
matchedMessage: a.matchedMessage,
timestamp: a.timestamp
};
})
}
2019-06-28 03:46:48 +02:00
}
};
publishAlert = (id, message, affected, matchedRule, matchedMessage, extra) => {
2019-06-28 03:46:48 +02:00
const context = {
id,
2019-06-29 03:35:53 +02:00
timestamp: new Date().getTime(),
2019-06-28 03:46:48 +02:00
message,
affected,
matchedRule,
matchedMessage,
extra
2019-06-28 03:46:48 +02:00
};
if (!this.alerts[id]) {
this.alerts[id] = [];
}
this.alerts[id].push(context);
if (!this.sent[id]) {
this._publish();
}
};
_clean = (group) => {
if (new Date().getTime() > group.latest + (this.config.clearNotificationQueueAfterSeconds * 1000)) {
delete this.alerts[group.id];
2019-06-29 03:35:53 +02:00
delete this.sent[group.id];
2019-06-28 03:46:48 +02:00
return true;
}
return false;
};
_checkLastSent = (group) => {
const lastTimeSent = this.sent[group.id];
if (lastTimeSent) {
const isThereSomethingNew = lastTimeSent < group.latest;
const isItTimeToSend = new Date().getTime() > lastTimeSent + (this.config.notificationIntervalSeconds * 1000);
return isThereSomethingNew && isItTimeToSend;
} else {
return true;
}
};
_publish = () => {
for (let id in this.alerts) {
const group = this._squash(this.alerts[id]);
2019-07-09 02:46:08 +02:00
if (group) {
if (this._checkLastSent(group)) {
this.sent[group.id] = new Date().getTime();
this._publishOnChannel(group);
}
2019-06-28 03:46:48 +02:00
2019-07-09 02:46:08 +02:00
this._clean(group);
}
2019-06-28 03:46:48 +02:00
}
};
_publishOnChannel = (alert) => {
2019-06-29 03:35:53 +02:00
this.pubSub.publish(this.channel, alert);
2019-06-28 03:46:48 +02:00
return alert;
}
2019-06-14 18:04:20 +02:00
}