Skip to content

Commit

Permalink
Merge pull request #914 from mountaindude/908
Browse files Browse the repository at this point in the history
908
  • Loading branch information
mountaindude authored Dec 12, 2023
2 parents 968567e + f6389dd commit 1e8dba3
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 163 deletions.
208 changes: 104 additions & 104 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"cron-job-manager": "^2.3.1",
"email-validator": "^2.0.4",
"enigma.js": "^2.12.0",
"esbuild": "^0.19.8",
"esbuild": "^0.19.9",
"eslint": "^8.55.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.1.0",
Expand Down Expand Up @@ -90,19 +90,19 @@
"posthog-node": "^3.2.0",
"promise": "^8.3.0",
"qrs-interact": "^6.3.1",
"rate-limiter-flexible": "^3.0.4",
"rate-limiter-flexible": "^3.0.6",
"serializeapp": "^3.0.0",
"systeminformation": "^5.21.20",
"upath": "^2.0.1",
"uuid": "^9.0.1",
"winston": "^3.11.0",
"winston-daily-rotate-file": "^4.7.1",
"ws": "^8.14.2",
"ws": "^8.15.0",
"xstate": "^4.38.3"
},
"devDependencies": {
"jest": "^29.7.0",
"prettier": "^3.1.0",
"prettier": "^3.1.1",
"snyk": "^1.1260.0"
},
"pkg": {
Expand Down
30 changes: 16 additions & 14 deletions src/butler.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,22 @@ const start = async () => {

// ------------------------------------
// Create MQTT client object and connect to MQTT broker, if MQTT is enabled
mqttInitHandlers();

// Sleep 5 seconds to allow MQTT to connect
globals.logger.info('MAIN: Sleeping 5 seconds to allow MQTT to connect.');
globals.logger.info('5...');
await globals.sleep(1000);
globals.logger.info('4...');
await globals.sleep(1000);
globals.logger.info('3...');
await globals.sleep(1000);
globals.logger.info('2...');
await globals.sleep(1000);
globals.logger.info('1...');
await globals.sleep(1000);
if (globals.config.get('Butler.mqttConfig.enable')) {
mqttInitHandlers();

// Sleep 5 seconds to allow MQTT to connect
globals.logger.info('MAIN: Sleeping 5 seconds to allow MQTT to connect.');
globals.logger.info('5...');
await globals.sleep(1000);
globals.logger.info('4...');
await globals.sleep(1000);
globals.logger.info('3...');
await globals.sleep(1000);
globals.logger.info('2...');
await globals.sleep(1000);
globals.logger.info('1...');
await globals.sleep(1000);
}

// Set up service monitoring, if enabled in the config file
if (globals.config.has('Butler.serviceMonitor.enable') && globals.config.get('Butler.serviceMonitor.enable') === true) {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/post_to_influxdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function postButlerMemoryUsageToInfluxdb(memory) {

// Add function to store windows service status to InfluxDB
function postWindowsServiceStatusToInfluxDB(serviceStatus) {
globals.logger.verbose(
`INFLUXDB WINDOWS SERVICE STATUS: Sending service status to InfluxDB: service="${serviceStatus.serviceFriendlyName}", status="${serviceStatus.serviceStatus}"`
);

// Create lookup table for Windows service state to numeric value, starting with 1 for stopped
const serviceStateLookup = {
STOPPED: 1,
Expand Down
27 changes: 22 additions & 5 deletions src/lib/service_monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ const serviceMonitorMqttSend2 = (config, logger, svc) => {
) {
logger.verbose(`"${svc.serviceName}"No MQTT topic defined in config entry "Butler.mqttConfig.serviceStatusTopic"`);
} else {
logger.verbose(`Sending service status to MQTT: service="${svc.serviceName}", status="${svc.serviceStatus}"`);
logger.verbose(
`MQTT WINDOWS SERVICE STATUS: Sending service status to MQTT: service="${svc.serviceDetails.displayName}", status="${svc.serviceStatus}"`
);

globals.mqttClient.publish(
`${config.get('Butler.mqttConfig.serviceStatusTopic')}/${svc.host}/${svc.serviceName}`,
JSON.stringify({
Expand Down Expand Up @@ -137,10 +140,8 @@ const verifyServicesExist = async (config, logger) => {
logger.verbose(`Checking status of Windows service ${service.name} (="${service.friendlyName}") on host ${host.host}`);
let serviceExists;
try {
console.log('a1')
// eslint-disable-next-line no-await-in-loop
serviceExists = await svcTools.exists(logger, service.name, host.host);
console.log('a2')
} catch (err) {
logger.error(`Error verifying existence and reachability of service ${service.name} on host ${host.host}: ${err}`);
result = false;
Expand Down Expand Up @@ -172,9 +173,25 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {
logger.verbose(`Checking status of Windows services on host ${host.host}`);
const servicesToCheck = host.services;

// Get status of all services on host
logger.verbose(`Getting status of all Windows services on host ${host.host}`);

const serviceStatusAll = await svcTools.statusAll(logger, host.host);

servicesToCheck.forEach(async (service) => {
logger.verbose(`Checking status of Windows service ${service.name} (="${service.friendlyName}") on host ${host.host}`);

// Does this service exist in the serviceStatusAll array?
const svcMonitored = serviceStatusAll.find((svc) => svc.name === service.name);

if (svcMonitored === undefined) {
logger.error(
`Service ${service.name} (="${service.friendlyName}") on host ${host.host} does not exist or cannot be reached.`
);
return;
}

// Get status of this service
const serviceStatus = await svcTools.status(logger, service.name, host.host);
logger.verbose(`Got reply: Service ${service.name} (="${service.friendlyName}") on host ${host.host} status: ${serviceStatus}`);

Expand Down Expand Up @@ -469,9 +486,9 @@ const checkServiceStatus = async (config, logger, isFirstCheck = false) => {

// InfluDB
if (
globals.config.has('Butler.emailNotification.enable') &&
globals.config.has('Butler.influxDb.enable') &&
globals.config.has('Butler.serviceMonitor.alertDestination.influxDb.enable') &&
globals.config.get('Butler.emailNotification.enable') === true &&
globals.config.get('Butler.influxDb.enable') === true &&
globals.config.get('Butler.serviceMonitor.alertDestination.influxDb.enable') === true
) {
const instanceTag = globals.config.has('Butler.influxDb.instanceTag')
Expand Down
Loading

0 comments on commit 1e8dba3

Please sign in to comment.