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-09-15 20:40:51 +02:00
|
|
|
import yargs from 'yargs';
|
2020-08-04 20:35:21 +02:00
|
|
|
import fs from "fs";
|
|
|
|
import yaml from "js-yaml";
|
2019-06-28 03:46:48 +02:00
|
|
|
|
2019-09-15 20:40:51 +02:00
|
|
|
const params = yargs
|
|
|
|
.usage('Usage: $0 <command> [options]')
|
2019-06-28 03:46:48 +02:00
|
|
|
|
2019-09-15 20:40:51 +02:00
|
|
|
.command('$0', 'Run BGPalerter (default)', function () {
|
2020-06-10 21:27:29 +02:00
|
|
|
yargs
|
|
|
|
.alias('v', 'version')
|
|
|
|
.nargs('v', 0)
|
|
|
|
.describe('v', 'Show version number')
|
|
|
|
|
|
|
|
.alias('c', 'config')
|
2020-06-10 12:04:26 +02:00
|
|
|
.nargs('c', 1)
|
|
|
|
.describe('c', 'Config file to load')
|
|
|
|
|
2020-07-25 15:59:45 +02:00
|
|
|
.alias('t', 'test')
|
|
|
|
.nargs('t', 0)
|
2020-07-29 19:42:55 +02:00
|
|
|
.describe('t', 'Test the configuration with fake BGP updates')
|
2020-07-25 15:59:45 +02:00
|
|
|
|
2020-06-10 21:27:29 +02:00
|
|
|
.alias('d', 'data-volume')
|
|
|
|
.nargs('d', 1)
|
|
|
|
.describe('d', 'A directory where configuration and data is persisted')
|
2019-09-15 20:40:51 +02:00
|
|
|
})
|
2020-06-10 12:04:26 +02:00
|
|
|
|
2019-09-15 20:40:51 +02:00
|
|
|
.command('generate', 'Generate prefixes to monitor', function () {
|
2020-06-10 21:27:29 +02:00
|
|
|
yargs
|
|
|
|
.alias('v', 'version')
|
|
|
|
.nargs('v', 0)
|
|
|
|
.describe('v', 'Show version number')
|
|
|
|
|
|
|
|
.alias('o', 'output')
|
2019-09-15 20:40:51 +02:00
|
|
|
.nargs('o', 1)
|
|
|
|
.describe('o', 'Write to file')
|
2019-09-17 17:18:54 +02:00
|
|
|
|
|
|
|
.alias('a', 'asn')
|
|
|
|
.nargs('a', 1)
|
|
|
|
.describe('a', 'AS number to monitor')
|
|
|
|
|
|
|
|
.alias('e', 'exclude')
|
|
|
|
.nargs('e', 1)
|
2020-06-10 12:04:26 +02:00
|
|
|
.describe('e', 'Comma-separated list of prefixes to exclude')
|
|
|
|
|
2019-10-27 13:39:54 +01:00
|
|
|
.alias('p', 'prefixes')
|
|
|
|
.nargs('p', 1)
|
2020-06-10 12:04:26 +02:00
|
|
|
.describe('p', 'Comma-separated list of prefixes to include')
|
2019-10-27 13:39:54 +01:00
|
|
|
|
2019-11-10 17:41:36 -08:00
|
|
|
.alias('l', 'prefixes-file')
|
|
|
|
.nargs('l', 1)
|
2020-06-10 12:04:26 +02:00
|
|
|
.describe('l', 'File containing the prefixes to include in the monitoring. One prefix for each line')
|
2019-10-27 13:39:54 +01:00
|
|
|
|
2019-09-17 22:24:36 +02:00
|
|
|
.alias('i', 'ignore-delegated')
|
|
|
|
.nargs('i', 0)
|
|
|
|
.describe('i', 'Ignore delegated prefixes')
|
2019-09-17 17:18:54 +02:00
|
|
|
|
2019-11-28 17:56:33 +01:00
|
|
|
.alias('s', 'monitor-as')
|
|
|
|
.nargs('s', 1)
|
|
|
|
.describe('s', 'List of monitored ASes to be added for generic monitoring in options.monitorASns.')
|
|
|
|
|
|
|
|
.alias('m', 'monitor-as-origin')
|
|
|
|
.nargs('m', 0)
|
|
|
|
.describe('m', 'Automatically generate list of monitored ASes (options.monitorASns) from prefix origins.')
|
|
|
|
|
2020-05-26 14:50:16 +02:00
|
|
|
.alias('x', 'proxy')
|
|
|
|
.nargs('x', 1)
|
|
|
|
.describe('x', 'HTTP/HTTPS proxy to use')
|
|
|
|
|
2020-07-30 13:43:11 +02:00
|
|
|
.alias('g', 'group')
|
|
|
|
.nargs('g', 1)
|
|
|
|
.describe('x', 'Define a user group for all the generated rules.')
|
|
|
|
|
2020-07-31 02:54:38 +02:00
|
|
|
.alias('A', 'append')
|
|
|
|
.nargs('A', 0)
|
|
|
|
.describe('A', 'Append the new configuration to the previous one.')
|
|
|
|
|
2020-07-29 19:31:16 +02:00
|
|
|
.alias('D', 'debug')
|
|
|
|
.nargs('D', 0)
|
|
|
|
.describe('D', 'Provide verbose output for debugging')
|
|
|
|
|
|
|
|
.alias('H', 'historical')
|
|
|
|
.nargs('H', 0)
|
|
|
|
.describe('H', 'Use historical visibility data for generating prefix list (prefixes visible in the last week).')
|
|
|
|
|
2021-04-27 19:00:11 +02:00
|
|
|
.alias('u', 'upstreams')
|
|
|
|
.nargs('u', 0)
|
2021-05-05 21:32:56 +02:00
|
|
|
.describe('u', 'Detect a list of allowed upstream ASes and enable detection of new left-side ASes')
|
2021-04-27 19:00:11 +02:00
|
|
|
|
|
|
|
.alias('n', 'downstreams')
|
|
|
|
.nargs('n', 0)
|
2021-05-05 21:32:56 +02:00
|
|
|
.describe('n', 'Detect a list of allowed downstream ASes and enable detection of new right-side ASes.')
|
2021-04-27 19:00:11 +02:00
|
|
|
|
2019-10-27 13:39:54 +01:00
|
|
|
.demandOption(['o']);
|
2019-09-15 20:40:51 +02:00
|
|
|
})
|
|
|
|
.example('$0 generate -a 2914 -o prefixes.yml', 'Generate prefixes for AS2914')
|
|
|
|
.help('h')
|
|
|
|
.alias('h', 'help')
|
|
|
|
.epilog('Copyright (c) 2019, NTT Ltd')
|
|
|
|
.argv;
|
2019-07-09 02:46:08 +02:00
|
|
|
|
2019-09-15 20:40:51 +02:00
|
|
|
switch(params._[0]) {
|
|
|
|
case "generate":
|
2019-11-28 17:56:33 +01:00
|
|
|
const generatePrefixes = require("./src/generatePrefixesList");
|
2020-07-29 19:31:16 +02:00
|
|
|
const debug = !!params.D;
|
|
|
|
const historical = !!params.H;
|
2019-10-27 13:39:54 +01:00
|
|
|
let prefixes = null;
|
2019-11-28 17:56:33 +01:00
|
|
|
let monitoredASes = false;
|
2019-11-10 17:41:36 -08:00
|
|
|
if (params.pf) {
|
|
|
|
throw new Error("The argument --pf has been deprecated. Use -l instead");
|
|
|
|
}
|
|
|
|
if (params.p && params.l) {
|
|
|
|
throw new Error("The argument -p is not compatible with the argument -l");
|
2019-10-27 13:39:54 +01:00
|
|
|
} else if (params.p) {
|
2020-01-18 21:57:19 +01:00
|
|
|
prefixes = params.p.split(",");
|
2019-11-10 17:41:36 -08:00
|
|
|
} else if (params.l) {
|
2019-10-27 13:39:54 +01:00
|
|
|
const fs = require("fs");
|
2019-11-10 17:41:36 -08:00
|
|
|
if (fs.existsSync(params.l)) {
|
|
|
|
prefixes = fs.readFileSync(params.l, 'utf8').split(/\r?\n/).filter(i => i && true);
|
2019-10-27 13:39:54 +01:00
|
|
|
} else {
|
2020-03-07 03:51:51 +01:00
|
|
|
throw new Error("The prefix list file (-l) is not readable");
|
2019-10-27 13:39:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 17:56:33 +01:00
|
|
|
if (params.s && params.m) {
|
|
|
|
throw new Error("You can specify -s or -m, not both");
|
|
|
|
} else if (params.s) {
|
2020-02-27 16:56:53 -08:00
|
|
|
monitoredASes = (params.s) ? params.s.toString().split(",") : null;
|
2019-11-28 17:56:33 +01:00
|
|
|
} else if (params.m) {
|
|
|
|
monitoredASes = true;
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:43:11 +02:00
|
|
|
const inputParameters = {
|
|
|
|
asnList: (params.a) ? params.a.toString().split(",") : null,
|
|
|
|
outputFile: params.o,
|
2020-08-08 19:25:04 +02:00
|
|
|
exclude: (params.e) ? params.e.toString().split(",") : null,
|
2020-07-30 13:43:11 +02:00
|
|
|
excludeDelegated: params.i || false,
|
2019-11-28 17:56:33 +01:00
|
|
|
prefixes,
|
2020-05-26 14:50:16 +02:00
|
|
|
monitoredASes,
|
2020-07-30 13:43:11 +02:00
|
|
|
httpProxy: params.x || null,
|
2020-07-29 19:31:16 +02:00
|
|
|
debug,
|
2020-07-30 13:43:11 +02:00
|
|
|
historical,
|
2020-08-08 19:25:04 +02:00
|
|
|
group: params.g || null,
|
2020-08-03 16:00:08 +02:00
|
|
|
append: !!params.A,
|
2020-08-04 20:35:21 +02:00
|
|
|
logger: null,
|
2021-04-27 19:00:11 +02:00
|
|
|
upstreams: !!params.u,
|
|
|
|
downstreams: !!params.n,
|
2020-08-04 20:35:21 +02:00
|
|
|
getCurrentPrefixesList: () => {
|
2021-01-08 19:17:22 +01:00
|
|
|
return Promise.resolve(yaml.load(fs.readFileSync(params.o, "utf8")));
|
2020-08-04 20:35:21 +02:00
|
|
|
}
|
2020-07-30 13:43:11 +02:00
|
|
|
};
|
|
|
|
|
2020-08-04 20:35:21 +02:00
|
|
|
if (!inputParameters.outputFile) {
|
|
|
|
throw new Error("Output file not specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
generatePrefixes(inputParameters)
|
|
|
|
.then(content => {
|
|
|
|
fs.writeFileSync(params.o, yaml.dump(content));
|
|
|
|
process.exit(0);
|
|
|
|
});
|
2019-10-27 13:39:54 +01:00
|
|
|
|
2019-09-15 20:40:51 +02:00
|
|
|
break;
|
2019-07-09 02:46:08 +02:00
|
|
|
|
2019-09-15 20:40:51 +02:00
|
|
|
default: // Run monitor
|
2020-07-25 15:59:45 +02:00
|
|
|
global.DRY_RUN = !!params.t;
|
2020-07-29 19:42:55 +02:00
|
|
|
if (global.DRY_RUN) console.log("Testing BGPalerter configuration. WARNING: remove -t option for production monitoring.");
|
2019-11-27 02:04:21 +01:00
|
|
|
const Worker = require("./src/worker").default;
|
2021-03-02 04:42:14 +01:00
|
|
|
module.exports = new Worker({
|
|
|
|
configFile: params.c,
|
|
|
|
volume: params.d,
|
|
|
|
groupFile: params.E
|
|
|
|
});
|
2019-06-28 03:46:48 +02:00
|
|
|
}
|