Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tortuvshin committed Jan 18, 2018
1 parent 0f852a6 commit 08592ea
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 112 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./src/middleware-wrapper');
module.exports = require('./src/Uptime');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "techstar-uptime",
"version": "1.0.4",
"version": "1.0.5",
"description": "Uptime monitor",
"main": "index.js",
"scripts": {
Expand Down
Binary file added public/img/image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/image2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/image3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/img/image4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions src/Uptime.js
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;
6 changes: 0 additions & 6 deletions src/helpers/default-config.js

This file was deleted.

14 changes: 0 additions & 14 deletions src/helpers/validate.js

This file was deleted.

90 changes: 0 additions & 90 deletions src/middleware-wrapper.js

This file was deleted.

0 comments on commit 08592ea

Please sign in to comment.