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){
|
||||
super(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);
|
||||
});
|
||||
|
||||
this._update(config);
|
||||
};
|
||||
|
||||
validate = (fileContent) => {
|
||||
@@ -211,6 +136,84 @@ export default class InputYml extends Input {
|
||||
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) => {
|
||||
if (regex) {
|
||||
try {
|
||||
|
Reference in New Issue
Block a user