Minor local-service cleanup

This commit is contained in:
iotux 2025-12-15 13:58:02 +01:00
parent d76ce4e28d
commit 96e8268986

View File

@ -9,40 +9,45 @@ class LocalServiceMonitorType extends MonitorType {
/** /**
* Check a local systemd service status. * Check a local systemd service status.
* Uses `systemctl is-running` to determine if the service is active. * Uses `systemctl is-running` to determine if the service is active.
* @param {object} monitor The monitor object containing serviceName. * @param {object} monitor The monitor object containing monitor.local_service_name.
* @param {object} heartbeat The heartbeat object to update. * @param {object} heartbeat The heartbeat object to update.
* @param {object} server The server object (unused in this specific check). * @param {object} server The server object (unused in this specific check).
* @returns {Promise<object>} A promise that resolves with the updated heartbeat. * @returns {Promise<object>} A promise that resolves with the updated heartbeat.
* @throws {Error} If the serviceName is invalid or the command execution fails. * @throws {Error} If the monitor.local_service_name is invalid or the command execution fails.
*/ */
async check(monitor, heartbeat, server) { async check(monitor, heartbeat, server) {
// This is the name of the service to check e.g. "nginx.service"
const serviceName = monitor.local_service_name;
// Basic sanitization to prevent argument injection. // Basic sanitization to prevent argument injection.
// This regex allows for standard service names, including those with instances like "sshd@.service". // This regex allows for standard service names, including those with instances like "sshd@.service".
if (!serviceName || !/^[a-zA-Z0-9._\-@]+$/.test(serviceName)) { if (!monitor.local_service_name || !/^[a-zA-Z0-9._\-@]+$/.test(monitor.local_service_name)) {
heartbeat.status = DOWN; heartbeat.status = DOWN;
heartbeat.msg = "Invalid service name provided."; heartbeat.msg = "Invalid service name provided.";
throw new Error(heartbeat.msg); throw new Error(heartbeat.msg);
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile("systemctl", [ "is-active", serviceName ], (error, stdout, stderr) => { execFile("systemctl", [ "is-active", monitor.local_service_name ], (error, stdout, stderr) => {
// systemctl is-active exits with 0 if the service is active, // systemctl is-active exits with 0 if the service is active,
// and a non-zero code if it is inactive, failed, or not found. // and a non-zero code if it is inactive, failed, or not found.
// 1. Capture the raw output (prioritize stderr for errors)
let output = (stderr || stdout || "").toString().trim();
// 2. Truncate if too long to ~200 chars
if (output.length > 200) {
output = output.substring(0, 200) + "...";
}
if (error) { if (error) {
heartbeat.status = DOWN;
// stderr often contains useful info like "service not found" // stderr often contains useful info like "service not found"
heartbeat.msg = stderr || stdout || `Service '${serviceName}' is not running.`; // Use the truncated output, or a default fallback if empty
heartbeat.msg = stderr || stdout || `Service '${monitor.local_service_name}' is not running.`;
reject(new Error(heartbeat.msg)); reject(new Error(heartbeat.msg));
return; return;
} }
// If there's no error, the service is running. // If there's no error, the service is running.
heartbeat.status = UP; heartbeat.status = UP;
heartbeat.msg = `Service '${serviceName}' is running.`; heartbeat.msg = `Service '${monitor.local_service_name}' is running.`;
resolve(heartbeat); resolve();
}); });
}); });
} }