Skip to content

Instantly share code, notes, and snippets.

@belohnung
Created July 27, 2023 21:25
Show Gist options
  • Save belohnung/17e8be1e71e2bfa82bf1439ce61f499e to your computer and use it in GitHub Desktop.
Save belohnung/17e8be1e71e2bfa82bf1439ce61f499e to your computer and use it in GitHub Desktop.
Smokeping Target config parser in Javascript
function parseSmokepingConfig(filePath) {
let fileContent = fs.readFileSync(filePath, 'utf8');
fileContent = fileContent.replace("\r\n", "\n");
const lines = fileContent.split("\n");
const targets = [];
let currentTarget = null;
let isInHeader = true;
let headerLines = "";
for (const line of lines) {
const trimmedLine = line.trim();
if (trimmedLine.startsWith('+')) {
isInHeader = false;
const depth = line.match(/^\++/)[0].length;
const targetInfo = trimmedLine.substring(depth).trim();
let id = targetInfo;
const newTarget = {};
let currentLine = targetInfo;
let nextLine = lines[lines.indexOf(line) + 1];
let offset = 1;
while (nextLine && !nextLine.trim().startsWith('+')) {
currentLine += ' ' + nextLine.trim();
nextLine = lines[lines.indexOf(line) + offset];
offset++;
}
const menuMatch = currentLine.match(/menu\s*=\s*(.*?)(?=\s+\w+\s*=|\s*$)/);
const titleMatch = currentLine.match(/title\s*=\s*(.*?)(?=\s+\w+\s*=|\s*$)/);
const hostMatch = currentLine.match(/host\s*=\s*(.*?)(?=\s+\w+\s*=|\s*$)/);
const probeMatch = currentLine.match(/probe\s*=\s*(.*?)(?=\s+\w+\s*=|\s*$)/);
const slaveMatch = currentLine.match(/slaves\s*=\s*(.*?)(?=\s+\w+\s*=|\s*$)/);
const alertsMatch = currentLine.match(/alerts\s*=\s*(.*?)(?=\s+\w+\s*=|\s*$)/);
const remarksMatch = currentLine.match(/remark\s*=\s*(.*?)(?=\s+\w+\s*=|\s*$)/);
newTarget.id = id;
if (probeMatch) { // We could parse the parent element(s) probe and put it here (to aid in using the config in a js project), but that would not produce the same result if reserialized, so I decided against it
newTarget.probe = probeMatch[1].trim();
}
newTarget.menu = menuMatch ? menuMatch[1].trim() : '';
newTarget.title = titleMatch ? titleMatch[1].trim() : '';
if (alertsMatch) {
newTarget.alerts = alertsMatch[1].trim();
}
if (slaveMatch) {
newTarget.slaves = slaveMatch[1].trim();
}
if (hostMatch) {
newTarget.host = hostMatch[1].trim().split(" ")[0];
}
if (remarksMatch) {
newTarget.remark = remarksMatch[1].trim();
}
newTarget.children = [];
if (depth === 1) {
targets.push(newTarget);
currentTarget = newTarget;
} else {
let parent = currentTarget;
while (parent.depth >= depth) {
parent = parent.parent;
}
parent.children.push(newTarget);
newTarget.parent = parent;
newTarget.depth = depth;
currentTarget = newTarget;
}
}
if (isInHeader && trimmedLine !== '') {
headerLines += trimmedLine + "\n";
}
}
return {header: headerLines, targets};
}
function serializeSmokepingConfig(config) {
let configString = config.header + "\n";
function serializeTarget(target, depth) {
const indent = '+'.repeat(depth);
const idPart = target.id ? ` ${target.id}` : '';
configString += `${indent}${idPart}\n`;
if (target.probe) {
configString += `probe = ${target.probe}\n`;
}
configString += `menu = ${target.menu}\n`;
configString += `title = ${target.title}\n`;
if (target.host) {
configString += `host = ${target.host}\n`;
}
if (target.alerts) {
configString += `alerts = ${target.alerts}\n`;
}
if (target.slaves) {
configString += `slaves = ${target.slaves}\n`;
}
if (target.remark) {
configString += `remark = ${target.remark}\n`;
}
configString += "\n";
for (const child of target.children) {
serializeTarget(child, depth + 1);
}
}
for (const target of config.targets) {
serializeTarget(target, 1);
}
return configString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment