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

48 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-06-30 13:31:05 +02:00
import Monitor from "./monitor";
import ipUtils from "../ipUtils";
export default class MonitorVisibility extends Monitor {
2019-07-06 22:40:39 +02:00
constructor(name, channel, params, env){
super(name, channel, params, env);
2019-06-30 13:31:05 +02:00
};
updateMonitoredPrefixes = () => {
this.monitored = this.input.getMonitoredMoreSpecifics();
};
filter = (message) => {
2019-07-06 22:40:39 +02:00
return message.type === 'withdrawal';
2019-06-30 13:31:05 +02:00
};
squashAlerts = (alerts) => {
2019-07-06 22:40:39 +02:00
const peers = [...new Set(alerts.map(alert => alert.matchedMessage.peer))].length;
return `The prefix ${alerts[0].matchedMessage.prefix} has been withdrawn. It is no longer visible from ${peers} peer(s).`;
2019-06-30 13:31:05 +02:00
};
2019-07-06 22:40:39 +02:00
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];
this.publishAlert(match.prefix,
`The prefix ${match.prefix} has been withdrawn.`,
match.asn,
matches[0],
message,
{});
}
resolve(true);
2019-06-30 13:31:05 +02:00
});
}