mirror of
https://github.com/nttgin/BGPalerter.git
synced 2024-05-19 06:50:08 +00:00
minor refactoring and debug logs removed
This commit is contained in:
@@ -41,82 +41,7 @@ export default class InputYml extends Input {
|
|||||||
|
|
||||||
constructor(config){
|
constructor(config){
|
||||||
super(config);
|
super(config);
|
||||||
this.prefixes = [];
|
this._update(config);
|
||||||
this.asns = [];
|
|
||||||
|
|
||||||
if (!config.monitoredPrefixesFiles || config.monitoredPrefixesFiles.length === 0) {
|
|
||||||
throw new Error("The monitoredPrefixesFiles key is missing in the config file");
|
|
||||||
}
|
|
||||||
|
|
||||||
const uniquePrefixes = {};
|
|
||||||
const uniqueAsns = {};
|
|
||||||
for (let prefixesFile of config.monitoredPrefixesFiles) {
|
|
||||||
|
|
||||||
let monitoredPrefixesFile = {};
|
|
||||||
let fileContent;
|
|
||||||
|
|
||||||
if (fs.existsSync('./' + prefixesFile)) {
|
|
||||||
fileContent = fs.readFileSync('./' + prefixesFile, 'utf8');
|
|
||||||
try {
|
|
||||||
monitoredPrefixesFile = yaml.safeLoad(fileContent) || {};
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error("The file " + prefixesFile + " is not valid yml: " + error.message.split(":")[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.keys(monitoredPrefixesFile).length === 0) {
|
|
||||||
throw new Error("No prefixes to monitor in " + prefixesFile + ". Please read https://github.com/nttgin/BGPalerter/blob/master/docs/prefixes.md");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.validate(monitoredPrefixesFile)) {
|
|
||||||
|
|
||||||
if (monitoredPrefixesFile.options && monitoredPrefixesFile.options.monitorASns) {
|
|
||||||
this.asns = Object
|
|
||||||
.keys(monitoredPrefixesFile.options.monitorASns)
|
|
||||||
.map(asn => {
|
|
||||||
if (uniqueAsns[asn]) {
|
|
||||||
throw new Error("Duplicate entry for monitored AS " + asn);
|
|
||||||
}
|
|
||||||
uniqueAsns[asn] = true;
|
|
||||||
return Object.assign({
|
|
||||||
asn: new AS(asn),
|
|
||||||
group: 'default'
|
|
||||||
}, monitoredPrefixesFile.options.monitorASns[asn]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const monitoredPrefixes = Object
|
|
||||||
.keys(monitoredPrefixesFile)
|
|
||||||
.filter(i => i !== "options")
|
|
||||||
.map(i => {
|
|
||||||
if (uniquePrefixes[i]) {
|
|
||||||
throw new Error("Duplicate entry for " + i);
|
|
||||||
}
|
|
||||||
uniquePrefixes[i] = true;
|
|
||||||
monitoredPrefixesFile[i].asn = new AS(monitoredPrefixesFile[i].asn);
|
|
||||||
|
|
||||||
return Object.assign({
|
|
||||||
prefix: i,
|
|
||||||
group: 'default',
|
|
||||||
ignore: false,
|
|
||||||
excludeMonitors: [],
|
|
||||||
includeMonitors: [],
|
|
||||||
}, monitoredPrefixesFile[i])
|
|
||||||
})
|
|
||||||
.filter(i => i !== null);
|
|
||||||
|
|
||||||
this.prefixes = this.prefixes.concat(monitoredPrefixes);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
fs.writeFileSync(prefixesFile, "");
|
|
||||||
throw new Error("The file " + prefixesFile + " cannot be loaded. An empty one has been created.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.prefixes = this.prefixes.sort((a, b) => {
|
|
||||||
return ipUtils.sortByPrefixLength(b.prefix, a.prefix);
|
|
||||||
});
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
validate = (fileContent) => {
|
validate = (fileContent) => {
|
||||||
@@ -211,6 +136,84 @@ export default class InputYml extends Input {
|
|||||||
return errors.length === 0;
|
return errors.length === 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
_update = (config) => {
|
||||||
|
this.prefixes = [];
|
||||||
|
this.asns = [];
|
||||||
|
|
||||||
|
if (!config.monitoredPrefixesFiles || config.monitoredPrefixesFiles.length === 0) {
|
||||||
|
throw new Error("The monitoredPrefixesFiles key is missing in the config file");
|
||||||
|
}
|
||||||
|
|
||||||
|
const uniquePrefixes = {};
|
||||||
|
const uniqueAsns = {};
|
||||||
|
for (let prefixesFile of config.monitoredPrefixesFiles) {
|
||||||
|
|
||||||
|
let monitoredPrefixesFile = {};
|
||||||
|
let fileContent;
|
||||||
|
|
||||||
|
if (fs.existsSync('./' + prefixesFile)) {
|
||||||
|
fileContent = fs.readFileSync('./' + prefixesFile, 'utf8');
|
||||||
|
try {
|
||||||
|
monitoredPrefixesFile = yaml.safeLoad(fileContent) || {};
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error("The file " + prefixesFile + " is not valid yml: " + error.message.split(":")[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(monitoredPrefixesFile).length === 0) {
|
||||||
|
throw new Error("No prefixes to monitor in " + prefixesFile + ". Please read https://github.com/nttgin/BGPalerter/blob/master/docs/prefixes.md");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.validate(monitoredPrefixesFile)) {
|
||||||
|
|
||||||
|
if (monitoredPrefixesFile.options && monitoredPrefixesFile.options.monitorASns) {
|
||||||
|
this.asns = Object
|
||||||
|
.keys(monitoredPrefixesFile.options.monitorASns)
|
||||||
|
.map(asn => {
|
||||||
|
if (uniqueAsns[asn]) {
|
||||||
|
throw new Error("Duplicate entry for monitored AS " + asn);
|
||||||
|
}
|
||||||
|
uniqueAsns[asn] = true;
|
||||||
|
return Object.assign({
|
||||||
|
asn: new AS(asn),
|
||||||
|
group: 'default'
|
||||||
|
}, monitoredPrefixesFile.options.monitorASns[asn]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const monitoredPrefixes = Object
|
||||||
|
.keys(monitoredPrefixesFile)
|
||||||
|
.filter(i => i !== "options")
|
||||||
|
.map(i => {
|
||||||
|
if (uniquePrefixes[i]) {
|
||||||
|
throw new Error("Duplicate entry for " + i);
|
||||||
|
}
|
||||||
|
uniquePrefixes[i] = true;
|
||||||
|
monitoredPrefixesFile[i].asn = new AS(monitoredPrefixesFile[i].asn);
|
||||||
|
|
||||||
|
return Object.assign({
|
||||||
|
prefix: i,
|
||||||
|
group: 'default',
|
||||||
|
ignore: false,
|
||||||
|
excludeMonitors: [],
|
||||||
|
includeMonitors: [],
|
||||||
|
}, monitoredPrefixesFile[i])
|
||||||
|
})
|
||||||
|
.filter(i => i !== null);
|
||||||
|
|
||||||
|
this.prefixes = this.prefixes.concat(monitoredPrefixes);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
fs.writeFileSync(prefixesFile, "");
|
||||||
|
throw new Error("The file " + prefixesFile + " cannot be loaded. An empty one has been created.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.prefixes = this.prefixes.sort((a, b) => {
|
||||||
|
return ipUtils.sortByPrefixLength(b.prefix, a.prefix);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
_validateRegex = (regex) => {
|
_validateRegex = (regex) => {
|
||||||
if (regex) {
|
if (regex) {
|
||||||
try {
|
try {
|
||||||
|
@@ -59,7 +59,6 @@ export default class ReportSyslog extends Report {
|
|||||||
if (this.connected) {
|
if (this.connected) {
|
||||||
resolve(true);
|
resolve(true);
|
||||||
} else {
|
} else {
|
||||||
console.log("connecting");
|
|
||||||
this.client = syslog.createClient(this.host, this.options);
|
this.client = syslog.createClient(this.host, this.options);
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
|
|
||||||
@@ -90,7 +89,6 @@ export default class ReportSyslog extends Report {
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
const message = this._getMessage(channel, content);
|
const message = this._getMessage(channel, content);
|
||||||
|
|
||||||
console.log(message);
|
|
||||||
this.client.log(message, {}, error => {
|
this.client.log(message, {}, error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
this.logger.log({
|
this.logger.log({
|
||||||
|
Reference in New Issue
Block a user