diff --git a/package.json b/package.json index 1924a572..c6482c77 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "techstar-uptime", - "version": "1.0.1", + "version": "1.0.3", "description": "Uptime monitor", "main": "index.js", "scripts": { diff --git a/src/helpers/default-config.js b/src/helpers/default-config.js index 8776a57d..91ad10cd 100644 --- a/src/helpers/default-config.js +++ b/src/helpers/default-config.js @@ -1,7 +1,5 @@ module.exports = { - websites: { - url: 'https://www.techstar.cloud', // URL of service we'll be pining - timeout: 200 // threshold in milliseconds above which is considered degraded performance - }, + url: 'https://www.techstar.cloud', // URL of service we'll be pining + timeout: 200, // threshold in milliseconds above which is considered degraded performance SLACK_WEBHOOK_URL: "" }; \ No newline at end of file diff --git a/src/helpers/validate.js b/src/helpers/validate.js index 8f6c47f8..2d102f93 100644 --- a/src/helpers/validate.js +++ b/src/helpers/validate.js @@ -5,7 +5,9 @@ module.exports = config => { return defaultConfig; } - config.websites = (typeof config.websites === 'object') ? config.websites : defaultConfig.websites; + config.url = (typeof config.url === 'object') ? config.url : defaultConfig.url; + config.timeout = (typeof config.timeout === 'number') ? config.timeout : defaultConfig.timeout; + config.SLACK_WEBHOOK_URL = (typeof config.SLACK_WEBHOOK_URL === 'string') ? config.SLACK_WEBHOOK_URL : defaultConfig.SLACK_WEBHOOK_URL; return config; }; \ No newline at end of file diff --git a/src/middleware-wrapper.js b/src/middleware-wrapper.js index 0a501c1b..ec893820 100644 --- a/src/middleware-wrapper.js +++ b/src/middleware-wrapper.js @@ -23,21 +23,21 @@ const middlewareWrapper = config => { const pingInterval = 1*1000*60 // 1 minutes let serviceStatus = {} - validatedConfig.websites.forEach(service => { - serviceStatus[service.url] = { + monitor => { + serviceStatus[validatedConfig.url] = { status: 'OPERATIONAL', // initialize all services as operational when we start responseTimes: [], // array containing the responses times for last 3 pings - timeout: service.timeout // load up the timout from the config + timeout: validatedConfig.timeout // load up the timout from the config } setInterval(() => { - pingService(service.url, (serviceResponse) => { - if (serviceResponse === 'OUTAGE' && serviceStatus[service.url].status !== 'OUTAGE') { + pingService(validatedConfig.url, (serviceResponse) => { + if (serviceResponse === 'OUTAGE' && serviceStatus[validatedConfig.url].status !== 'OUTAGE') { // only update and post to Slack on state change - serviceStatus[service.url].status = 'OUTAGE' - postToSlack(service.url) + serviceStatus[validatedConfig.url].status = 'OUTAGE' + postToSlack(validatedConfig.url) } else { - let responseTimes = serviceStatus[service.url].responseTimes + let responseTimes = serviceStatus[validatedConfig.url].responseTimes responseTimes.push(serviceResponse) // check degraded performance if we have 3 responses so we can average them @@ -47,21 +47,21 @@ const middlewareWrapper = config => { // compute average of last 3 response times let avgResTime = responseTimes.reduce((a, b) => a + b, 0) / responseTimes.length - let currService = serviceStatus[service.url] + let currService = serviceStatus[validatedConfig.url] if (avgResTime > currService.timeout && currService.status !== 'DEGRADED') { currService.status = 'DEGRADED' postToSlack(service.url) } else if (avgResTime < currService.timeout && currService.status !== 'OPERATIONAL') { currService.status = 'OPERATIONAL' - postToSlack(service.url) + postToSlack(validatedConfig.url) } } } }) }, pingInterval) - }) + } const postToSlack = (serviceUrl) => { var message = "";