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

58 lines
1.9 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-07-09 02:46:08 +02:00
this.threshold = (params.threshold != null) ? params.threshold : 10;
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;
2019-07-09 02:46:08 +02:00
if (peers >= this.threshold) {
return (peers === 1) ?
`The prefix ${alerts[0].matchedMessage.prefix} it's no longer visible (withdrawn) from the peer ${alerts[0].matchedMessage.peer}.` :
`The prefix ${alerts[0].matchedMessage.prefix} has been withdrawn. It is no longer visible from ${peers} peers.`;
} else {
return false;
}
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];
2019-07-09 02:46:08 +02:00
let key = match.prefix;
this.publishAlert(key,
2019-07-06 22:40:39 +02:00
`The prefix ${match.prefix} has been withdrawn.`,
match.asn,
matches[0],
message,
{});
}
resolve(true);
2019-06-30 13:31:05 +02:00
});
}