Skip to content

Commit

Permalink
Merge pull request #891 from mountaindude/870
Browse files Browse the repository at this point in the history
870
  • Loading branch information
mountaindude authored Dec 6, 2023
2 parents 2b188fb + 56a18de commit bafd91e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
28 changes: 21 additions & 7 deletions src/lib/service_monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ const serviceMonitorMqttSend2 = (config, logger, svc) => {
};

const verifyServicesExist = async (config, logger) => {
logger.info('Verifying that all Windows services specified in config file exist and can be reached.');

// Return false if one or more services do not exist or cannot be reached.
// Return true if all services are reachable.
let result = false;
let result = true;

const hostsToCheck = config.get('Butler.serviceMonitor.monitor');

Expand All @@ -132,13 +134,25 @@ const verifyServicesExist = async (config, logger) => {

// eslint-disable-next-line no-restricted-syntax
for (const service of servicesToCheck) {
// eslint-disable-next-line no-await-in-loop
const serviceExists = await svcTools.exists(service.name, host.host);
if (serviceExists) {
result = true;
let serviceExists;
try {
// eslint-disable-next-line no-await-in-loop
serviceExists = await svcTools.exists(service.name, host.host);
} catch (err) {
logger.error(`Error verifying existence and reachability of service ${service.name} on host ${host.host}: ${err}`);
result = false;
}

logger.verbose(`Windows service ${service.name} (="${service.friendlyName}") on host ${host.host} exists: ${serviceExists}`);
if (serviceExists) {
logger.verbose(
`Windows service ${service.name} (="${service.friendlyName}") on host ${host.host} exists: ${serviceExists}`
);
} else {
logger.error(
`Windows service ${service.name} (="${service.friendlyName}") on host ${host.host} does not exist or cannot be reached.`
);
result = false;
}
}
}

Expand Down Expand Up @@ -539,7 +553,7 @@ async function setupServiceMonitorTimer(config, logger) {
}
} else {
logger.error(
'At least one Windows service does not exist or could not be reached. Will not check any Windows services from here on.'
'At least one Windows service does not exist or could not be reached. Monitoring of Windows services is disabled.'
);
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/lib/winsvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function all(host = null) {
// Create promise
return new Promise((resolve, reject) => {
let command = '';

if (host === null) {
// Run command for get states of all services on local machine
command = 'sc.exe query state= all';
Expand All @@ -21,6 +22,9 @@ function all(host = null) {
exec(command, (err, stdout) => {
// On error, reject and exit
if (err) {
if (stdout) {
reject(stdout);
}
reject(err);
return;
}
Expand Down Expand Up @@ -55,6 +59,8 @@ function exists(serviceName, host = null) {
return;
}

// Is host reachable?

// Get all services
all(host).then(
// On success, check
Expand Down
30 changes: 17 additions & 13 deletions src/udp/udp_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,20 +942,24 @@ module.exports.udpInitTaskErrorServer = () => {
// Handler for UDP error event
// eslint-disable-next-line no-unused-vars
globals.udpServerReloadTaskSocket.on('error', (message, remote) => {
const address = globals.udpServerReloadTaskSocket.address();
globals.logger.error(`TASKFAILURE: UDP server error on ${address.address}:${address.port}`);

// Publish MQTT message that UDP server has reported an error
if (globals.config.has('Butler.mqttConfig.enable') && globals.config.get('Butler.mqttConfig.enable') === true) {
if (globals?.mqttClient?.connected) {
globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'error');
} else {
globals.logger.warn(
`MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskAbortedTopic'
)}`
);
try {
const address = globals.udpServerReloadTaskSocket.address();
globals.logger.error(`TASKFAILURE: UDP server error on ${address.address}:${address.port}`);

// Publish MQTT message that UDP server has reported an error
if (globals.config.has('Butler.mqttConfig.enable') && globals.config.get('Butler.mqttConfig.enable') === true) {
if (globals?.mqttClient?.connected) {
globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'error');
} else {
globals.logger.warn(
`MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskAbortedTopic'
)}`
);
}
}
} catch (err) {
globals.logger.error(`TASKFAILURE: Error in UDP error handler: ${err}`);
}
});

Expand Down

0 comments on commit bafd91e

Please sign in to comment.