Server uptime pinging tool that sends a message to Slack

Posted by Derek McAuley on May 17th, 2017

This is a stripped down version of a tool I built for work.
It will ping a website for a 200 response code every 7 seconds. If it doesn't get a 200 response code four times in a row it'll send a message to slack with the websites response code.
This is built use a node.js server.
Requirements:

var request = require("request")
var Hapi = require('hapi');
var Slack = require('slack-node');
Installing the requirements..
$ npm install time
$ npm install hapi
$ npm install slack-node
$ npm install request

The main function pings the server, if it's down four times in a row it sends text and the response code to the 'slackReviewBot' function.

function onlineBooking(){
request({
    url: "http://example.com",
    json: true
}, function (error, response, body) {

    if (!error && response.statusCode === 200) {
        
        // 'if/else' checks that it receives an up response four times in a row 
        // the variable e is increased by .25 for every 200(ok response) until e reaches 1
        // it will then send a message that the server is up
        if(e < 1  && response.statusCode === 200){
            setTimeout(function () {
           console.log(response.statusCode) // Print the response code
            e =e+0.25;
            }, 6000); // 6 seconds delay between each response 
        }
        
    else {
          while(h == 0){
        console.log(response.statusCode) // Print the response code
        console.log("********************")
        slackReviewBot("Website :robot_face: ", response.statusCode + " - OK", "http://example.com", "");
            h++;
            s = 0; 
          } 
        }// end of else
    }// end of if
    else {
        console.log(response.statusCode) // Print the response code
        e = 0;
        setTimeout(function () {  
        while(s == 0){
         
        console.log(response.statusCode) // Print the response code
        console.log("********************")
        slackReviewBot("Website :robot_face: ", response.statusCode, "http://example.com", "");
            s++;
            h=0;
        }}, 3000);
    } // end of else
    
})

}

Sets the timer to call the pinging function every 7 seconds.

// sets the loop for checking every 7 seconds

setInterval(function(){

    onlineBooking();
    
}, 7000); 

This function sends the message to slack.

//  this function sends server name, a message and url to slack
function slackReviewBot(servername, body, urls, bod) {
    
var time = require('time');
// Create a new Date instance
var now = new time.Date();
now.setTimezone("Europe/London");

    var bo = body; 
    var bod = bod;
    var urls = urls;
    var sname = servername; 
    // you'll need to replace the slack webhook below
    // you'll find info on webhooks here https://api.slack.com/incoming-webhooks
    var webhook_url = 'https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxxxx';
    slack = new Slack();
    slack.setWebhook(webhook_url);
    
        slack.webhook({
        channel: "#server-uptime",
        username: "Server:",
        icon_emoji: "http://4.bp.blogspot.com/-mYCTaPOu-60/VK98X5CJEyI/AAAAAAAAApM/0oplbclvnUY/s1600/unnamed.png",
        text: " " + "\n" + 
            "*" + sname + " * " + "\n" +
            "Status: " + bo + "\n" +
            now + "\n" +
            "Check the status here: " + urls + "\n" 
            
        }, function(err, response) {      
        console.log(response);
        }); 
}

Running the app:

Ruuning the app on linux

Below is an example of what the message in Slack looks like:

Message in slack Download code from github

If you want this program to run each time the server starts up then check out this blog post.



Why not leave a comment below.


Name:

Comments:




Comments: