1
0
mirror of https://github.com/nttgin/BGPalerter.git synced 2024-05-19 06:50:08 +00:00
Files
nttgin-BGPalerter/monitors/monitorVisibility.js
2019-07-09 04:29:36 +02:00

58 lines
1.9 KiB
JavaScript

import Monitor from "./monitor";
import ipUtils from "../ipUtils";
export default class MonitorVisibility extends Monitor {
constructor(name, channel, params, env){
super(name, channel, params, env);
this.threshold = (params.threshold != null) ? params.threshold : 10;
};
updateMonitoredPrefixes = () => {
this.monitored = this.input.getMonitoredMoreSpecifics();
};
filter = (message) => {
return message.type === 'withdrawal';
};
squashAlerts = (alerts) => {
const peers = [...new Set(alerts.map(alert => alert.matchedMessage.peer))].length;
if (peers >= this.threshold) {
return (peers === 1) ?
`The prefix ${alerts[0].matchedMessage.prefix} (${alerts[0].matchedRule.description}) it's no longer visible (withdrawn) from the peer ${alerts[0].matchedMessage.peer}.` :
`The prefix ${alerts[0].matchedMessage.prefix} (${alerts[0].matchedRule.description}) has been withdrawn. It is no longer visible from ${peers} peers.`;
} else {
return false;
}
};
monitor = (message) =>
new Promise((resolve, reject) => {
const messagePrefix = message.prefix;
let matches = this.monitored.filter(item => {
return item.prefix === messagePrefix;
});
if (matches.length > 1) {
matches = [matches.sort((a, b) => ipUtils.sortByPrefixLength(a.prefix, b.prefix)).pop()];
}
if (matches.length !== 0) {
const match = matches[0];
let key = match.prefix;
this.publishAlert(key,
`The prefix ${match.prefix} has been withdrawn.`,
match.asn,
matches[0],
message,
{});
}
resolve(true);
});
}