-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f852a6
commit 08592ea
Showing
10 changed files
with
107 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = require('./src/middleware-wrapper'); | ||
module.exports = require('./src/Uptime'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict'; | ||
|
||
const EventEmitter = require('eventemitter3'), | ||
request = require('request'); | ||
|
||
class Uptime extends EventEmitter{ | ||
|
||
constructor(options) { | ||
super(); | ||
if (!options || (options && (!options.SLACK_WEBHOOK_URL))) { | ||
throw new Error("You need to specify an SLACK_WEBHOOK_URL"); | ||
} | ||
this.SLACK_WEBHOOK_URL = options.SLACK_WEBHOOK_URL; | ||
this.pingInterval = 1*1000*60 | ||
this.serviceStatus = {} | ||
} | ||
|
||
pingService(url, cb){ | ||
|
||
console.log("PING START"); | ||
request({ | ||
method: 'GET', | ||
uri: url, | ||
time: true | ||
}, (err, res, body) => { | ||
if (!err && res.statusCode == 200) { | ||
// we'll use the time from the point we try to establish a connection with | ||
// the service until the first byte is received | ||
cb(res.timingPhases.firstByte) | ||
} else { | ||
cb('OUTAGE') | ||
} | ||
}) | ||
} | ||
|
||
postToSlack(serviceUrl){ | ||
|
||
console.log("POST TO SLACK START"); | ||
var message = ""; | ||
if (this.serviceStatus[serviceUrl].status == 'DEGRADED'){ | ||
message = "`СИСТЕМИЙН АЖИЛЛАГАА УНАСАН !!!` :skull: "; | ||
} else if (this.serviceStatus[serviceUrl].status == 'OPERATIONAL') { | ||
message = "СИСТЕМИЙН АЖИЛЛАГАА ХЭВИЙН БАЙНА :robot_face:"; | ||
} else if (this.serviceStatus[serviceUrl].status == 'OUTAGE') { | ||
message = "СИСТЕМИЙН АЖИЛЛАГАА ДОГОЛДЛОО :zzz:"; | ||
} | ||
let slackPayload = { | ||
text: `*${message}*\n_${serviceUrl}_` | ||
} | ||
|
||
request({ | ||
method: 'POST', | ||
uri: this.SLACK_WEBHOOK_URL, | ||
body: slackPayload, | ||
json: true | ||
}, (err, res, body) => { | ||
if (err) console.log(`Error posting to Slack: ${err}`) | ||
}) | ||
} | ||
|
||
monitor(websites){ | ||
console.log("MONITOR START"); | ||
websites.forEach(service => { | ||
this.serviceStatus[service.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 | ||
} | ||
|
||
setInterval(() => { | ||
this.pingService(service.url, (serviceResponse) => { | ||
if (serviceResponse === 'OUTAGE' && this.serviceStatus[service.url].status !== 'OUTAGE') { | ||
// only update and post to Slack on state change | ||
this.serviceStatus[service.url].status = 'OUTAGE' | ||
this.postToSlack(service.url) | ||
} else { | ||
let responseTimes = this.serviceStatus[service.url].responseTimes | ||
responseTimes.push(serviceResponse) | ||
|
||
// check degraded performance if we have 3 responses so we can average them | ||
if (responseTimes.length > 3) { | ||
// remove the oldest response time (beginning of array) | ||
responseTimes.shift() | ||
|
||
// compute average of last 3 response times | ||
let avgResTime = responseTimes.reduce((a, b) => a + b, 0) / responseTimes.length | ||
let currService = this.serviceStatus[service.url] | ||
|
||
if (avgResTime > currService.timeout && currService.status !== 'DEGRADED') { | ||
currService.status = 'DEGRADED' | ||
this.postToSlack(service.url) | ||
} else if (avgResTime < currService.timeout && currService.status !== 'OPERATIONAL') { | ||
currService.status = 'OPERATIONAL' | ||
this.ostToSlack(service.url) | ||
} | ||
} | ||
|
||
} | ||
}) | ||
}, this.pingInterval) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = Uptime; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.