• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Uptime Event Emitter

Uptime Event Emitter

An uptime event emitter for http and tcp servers.

Installation

npm install ping-monitor

Documentation

How to use

const Monitor = require('ping-monitor');

const myWebsite = new Monitor(options);

myWebsite.on(event, function(response, state) {
    // Do something with the response
});

Alternatively, you can subscribe to the Monitor's events through a notification channel. Click to see some demo nofitication channels.

const Monitor = require('ping-monitor');
const SlackChannel = require('@ping-monitor/slack');
const EmailChannel = require('@ping-monitor/email');

const myWebsite = new Monitor(options);
const slacker = new SlackChannel({...config});
const mailer = new EmailChannel({...config});

myWebsite.addNotificationChannel(slacker);
myWebsite.addNotificationChannel(mailer);

Methods

  • stop - stop an active monitor
  • restart - stop and start an active monitor
  • addNotificationChannel (or addChannel) - adds a notification channel that subscribes to the monitor's events

Options

  • address - Server address to be monitored
  • protocol - (defaults to http) request protocol (http/s, tcp, udp)
  • port - Server port (optional).
  • interval (defaults to 15) - time interval for polling requests.
  • httpOptions - allows you to define your http/s request with more control. A full list of the options can be found here: https://nodejs.org/api/http.html#http_http_request_url_options_callback
  • expect { statusCode , contentSearch } - allows you define what kind of a response you expect from your endpoint.
    • statusCode defines the expected http response status code.
    • contentSearch defines a substring to be expected from the response body.
  • config { intervalUnits } - configuration for your Monitor, currently supports one property, intervalUnits. intervalUnits specifies which to time unit you want your Monitor to use. There are 4 options, milliseconds, seconds, minutes (default), and hours.
  • ignoreSSL - ignore broken/expired certificates
  • Expect Object

    expect {
      statusCode: Integer, // http status codes
      contentSearch: String
    }

    Config Object

    config {
      intervalUnits: String
    }
    // http Get
    const myApi = new Monitor({
        address: 'https://api.ragingflame.co.za',
        title: 'Raging Flame',
        interval: 5,
        protocol: 'http', // http/s, tcp, udp
        config: {
          intervalUnits: 'minutes' // seconds, milliseconds, minutes {default}, hours
        },
    
        httpOptions: {
          path: '/users',
          method: 'get',
          query: {
            id: 3
          }
        },
        expect: {
          statusCode: 200
        }
    });
    
    // http Post
    const myApi = new Monitor({
        address: 'http://api.ragingflame.co.za',
        title: 'Raging Flame',
        interval: 10,
        protocol: 'http',
        config: {
          intervalUnits: 'minutes' // seconds, milliseconds, minutes {default}, hours
        },
    
        httpOptions: {
          path: '/users',
          method: 'post',
          query: {
            first_name: 'Que',
            last_name: 'Fire'
          },
          body: {content:'Hello World!'}
        },
        expect: {
          statusCode: 200
        }
    });

    Emitted Events

    • up - All is good server is up.
    • down - Not good, server is down.
    • stop - Fired when the monitor has stopped.
    • error - Fired when there's an error
    • timeout - Fired when the http request times out
    • restored - Fired server is up after being down

    Response object

    • object.website (deprecated) - website being monitored .
    • object.address - server address
    • object.port - server port
    • object.time - (aka responseTime) request response time
    • object.responseMessage - http response code message
    • object.responseTime - response time in milliseconds
    • object.httpResponse - native http/s response object

    State object

    • object.id null - monitor id, useful when persistence.
    • object.title null - monitor label for humans.
    • object.isUp true - flag to indicate if monitored server is up or down.
    • object.created_at <Date.now()> - monitor creation date.
    • object.isUp true - previous uptime status of the monitor.
    • object.port null - server port.
    • object.totalRequests 0 - total requests made.
    • object.totalDownTimes 0 - total number of downtimes.
    • object.lastDownTime <Date.now()> - time of last downtime.
    • object.lastRequest <Date.now()> - time of last request.
    • object.interval 5 - polling interval in minutes
    • object.website null - (deprecated) website being monitored.
    • object.address null - server address being monitored.
    • object.port null - server port.
    • object.paused false - monitor paused flag
    • object.httpOptions - monitor httpOptions options

      Website Example

      'use strict';
      
      const Monitor = require('ping-monitor');
      
      
      const myMonitor = new Monitor({
        address: 'http://www.ragingflame.co.za',
        title: 'Raging Flame',
        interval: 10 // minutes
        //protocol: 'http'
      });
      
      
      myMonitor.on('up', function (res, state) {
          console.log('Yay!! ' + state.address + ' is up.');
      });
      
      
      myMonitor.on('down', function (res, state) {
        console.log('Oh Snap!! ' + state.address + ' is down! ' + state.statusMessage);
      });
      
      
      myMonitor.on('restored', function (res, state) {
        console.log(state.address + ' has been restore');
      });
      
      
      myMonitor.on('stop', function (res, state) {
        console.log(state.address + ' monitor has stopped.');
      });
      
      
      myMonitor.on('timeout', function (error, res) {
        console.log(error);
      });
      
      
      myMonitor.on('error', function (error) {
        console.log(error);
      });

      TCP Example

      'use strict';
      
      const Monitor = require('ping-monitor');
      
      
      const myMonitor = new Monitor({
        address: '162.13.124.139',
        port: 8080,
        interval: 5, // minutes
        protocol: 'tcp'
      });
      
      
      myMonitor.on('up', function (res, state) {
        console.log('Yay!! ' + state.address + ':' + state.port + ' is up.');
      });
      
      
      myMonitor.on('down', function (res, state) {
        console.log('Oh Snap!! ' + state.address + ':' + state.port + ' is down! ');
      });
      
      
      myMonitor.on('restored', function (res, state) {
        console.log('Yay!! ' + state.address + ':' + state.port + ' has been restored! ');
      });
      
      
      myMonitor.on('stop', function (res, state) {
        console.log(state.address + ' monitor has stopped.');
      });
      
      
      myMonitor.on('error', function (error, res) {
        console.log(error);
      });
      
      
      myMonitor.on('timeout', function (error, res) {
        console.log(error);
      });

      UDP Example

      'use strict';
      
      const Monitor = require('ping-monitor');
      
      
      const myMonitor = new Monitor({
        address: '32.13.124.139',
        port: 8080,
        interval: 5,
        protocol: 'udp'
      });
      
      
      myMonitor.on('up', function (res, state) {
        console.log('Yay!! ' + state.address + ':' + state.port + ' is up.');
      });
      
      
      myMonitor.on('down', function (res, state) {
        console.log('Oh Snap!! ' + state.address + ':' + state.port + ' is down! ');
      });
      
      
      myMonitor.on('restored', function (res, state) {
        console.log('Yay!! ' + state.address + ':' + state.port + ' has been restored! ');
      });
      
      
      myMonitor.on('stop', function (res, state) {
        console.log(state.address + ' monitor has stopped.');
      });
      
      
      myMonitor.on('error', function (error, res) {
        console.log(error);
      });
      
      
      myMonitor.on('timeout', function (error, res) {
        console.log(error);
      });

      Change log

      v0.8.0

      Changes

      • Added protocol property to the Monitor Options object
      • Added support for UDP servers. To monitor a UDP server, set the protocol property to udp
      • Added the restored event which is emitted once when a server is up after beign down
      • Depracated website property on the Monitor Options object. Only use address
      • Refactored some code
        const ping = new Monitor({
          address: '34.22.237.1',
          port: 1234,
          interval: 10,
          protocol: 'udp',
        });
      
        ping.on('up', function (res, state) {
          console.log('Yay!! Service is up');
        });
      
        ping.on('down', function (res, state) {
          console.log(':( Service is down!');
        });
      
        ping.on('restored', function (res, state) {
          console.log('Yay!! Service has been restored');
        });
      
        ping.on('error', function (error, res) {
          console.error(error);
        });

      v0.7.0

      Changes

      • Dependencies update.
      • Added addNotificationChannel method to Monitor.
      • Added addChannel method to Monitor. This method is an alias of the addNotificationChannel method.
        /*** 
         * Channel class 
         * methods: up, down, stop, error, timeout 
         * properties: name
         ***/
        class Logger {
      
          constructor(config = {}) {
            // do something with the config
          }
      
          name = 'logger';
      
          up(res, state) {
            console.log(`#${this.name}: ${res.address} is up`);
          }
      
          down(res, state) {
            console.log(`#${this.name}: ${res.address} is down`);
          }
      
          stop(res, state) {
            console.log(`#${this.name}: ${res.address} monitor stopped`);
          }
      
          error(error, res, state) {
            console.log(`#${this.name}: ${res.address} monitor returned an error`);
          }
      
          timeout(error, res, state) {
            console.log(`#${this.name}: ${res.address} timed out`);
          }
      
          restored(error, res, state) {
            console.log(`#${this.name}: ${res.address} has been restored`);
          }
        }
      
      
        const ping = new Monitor({
          address: 'https://google.com',
          interval: 30,
          protocol: 'http',
          config: {
            intervalUnits: 'seconds',
          }
        });
      
        const logger = new Logger();
      
        ping.addNotificationChannel(logger);
      
        // you can multiple notification channels
        // ping.addNotificationChannel(mailer)
        // ping.addNotificationChannel(slack)

      v0.6.1

      Changes

      • Added auto id generation opt-out
        let ping = new Monitor({
          address: 'https://google.com',
          interval: 5,
          protocol: 'http',
          config: {
            intervalUnits: 'minutes',
            generateId: false // defaults is true
          }
        });
      
        ping.on('up', function (res, state) {
          //state.id === null
          console.log('Yay!! Google is up');
        });
      
      
        ping.on('error', function (error, res) {
          console.error(error);
        });

      v0.6.0

      Changes

      • Code refactoring
      • Removed active from props (redundant)
      • Removed host from props (not used)
      • Added ignoreSSL to support websites with expired certificates
        let ping = new Monitor({
          address: 'https://wrong.host.badssl.com',
          interval: 1,
          protocol: 'http',
          config: {
            intervalUnits: 'minutes' // seconds, milliseconds, minutes {default}, hours
          },
          ignoreSSL: true
        });
      
        ping.on('up', function (res, state) {
          console.log('Yay!! Service is up');
        });
      
      
        ping.on('error', function (error, res) {
          console.error(error);
        });

      v0.5.2

      Changes

      • Added support for configuring interval units
        let ping = new Monitor({
          address: 'https://webservice.com',
          interval: 1,
          protocol: 'http',
          config: {
            intervalUnits: 'minutes'
          }
        });
      
        ping.on('up', function (res, state) {
          console.log('Yay!! Service is up');
        });
      
      
        ping.on('error', function (error, res) {
          console.error(error);
        });

      v0.5.1

      Changes

      • Added Support for content search in HTTP/HTTPS - courtesy of @pbombnz
        let ping = new Monitor({
          address: 'https://ecommorce-shop.com/playstation5',
          interval: 1,
          protocol: 'http',
          expect: {
            contentSearch: 'In stock'
          }
        });
      
        ping.on('up', function (res, state) {
          console.log('Yay!! Content cantains the phrase "In stock"');
        });
      
      
        ping.on('error', function (error, res) {
          console.error(error);
        });

      v0.5.0

      Changes

      • Added timeout event to Monitor instance. This event is passed from the htt/s module.
        myMonitor.on('timeout', function (error, res) {
          console.log(error);
        });
      
        // also make sure that you are handling the error event
        myMonitor.on('error', function (error, res) {
          console.log(error);
        });
      • Dependencies update

      Please note: When the timeout event is fired, it is followed by the error event which is created when we manually abort the http request.

      v0.4.4

      Dependencies update

      v0.4.3

      Changes

      • Added the native http/s response object in the Monitor response object
      • Added Post support in your Monitor instances.

      You can now include a body in your httpOptions:

      // http Post
      const myApi = new Monitor({
          address: 'http://api.ragingflame.co.za',
          title: 'Raging Flame',
          interval: 10 // minutes
          protocol: 'http',
      
          // new options
          httpOptions: {
            path: '/users',
            method: 'post',
            query: {
              type: 'customer',
            },
            body: {
              name: 'Que',
              email: '[email protected]'  
            }
          },
          expect: {
            statusCode: 200
          }
      });
      
      myApi.on('up', function (res, state) {
        /*
          response {
            responseTime <Integer> milliseconds
            responseMessage <String> response code message
            address <String> url being monitored.
            address <String> server address being monitored
            port <Integer>
            httpResponse <Object> native http/s response object
          }
      
          state {
            created_at <Date.now()>
            isUp <Boolean>
            port: <Integer>
            totalRequests <Integer>
            lastDownTime <Date.now()>
            lastRequest <Date.now()>
            interval <Integer>
          }
        */
      });

      v0.4.2

      Changes

      Added some utility methods used when updating a monitor and added immediate ping on monitor creation.

      • Added pause method to Monitor.
      • Added unpause method to Monitor.

      Tip: See options section to learn how they work.

      v0.4.1

      Changes

      Changes in v0.4.1 give you more control to define your http requests and what response to expect.

      • Added httpOptions prop to Monitor instance options.
      • Added expect prop for naming your your monitor.

      Tip: See options section to learn how they work.

      v0.4.0

      Changes

      Most of the changes introduced in this version were introduced to support database persistence.

      • Added id prop, useful when you add database persistence.
      • Added title prop for naming your your monitor.
      • Added active prop to flag if monitoring is active.
      • Added totalDownTimes prop for keeping record of total downtimes.
      • Added isUp prop to indicate if monitored server is up or down.
      • Added website, address, totalDownTimes, active, active props to the emitted state object
      • Added eslinting (2015) and cleaned up the code a bit
      • *breaking change: * the stop event now takes a callback that accepts 2 arguments, response && state (same as the up and down events).

      v0.3.1

      New Feature

      • Added a state object in the response that returns useful monitoring data

      • State object

        const Monitor = require('ping-monitor');
      
        const myMonitor = new Monitor(options);
      
        myMonitor.on(event, function(response, state) {
          /*
            response {...}
            state {
              created_at <Date.now()>
              isUp <Boolean>
              port: <Integer>
              totalRequests <Integer>
              lastDownTime <Date.now()>
              lastRequest <Date.now()>
              interval <Integer>
            }
          */
        });

      Changes made

      • The event handler now accepts to arguments response and state, please see above examples.

      v0.3.0

      • Brought back error event - required for handling module usage related errors
      • Added responseTime to the response object
      • Added support for tcp servers

      v0.2.0

      Testing

      npm test
      

      License

      (MIT License)

      Copyright (c) 2013 - 2018 Qawelesizwe Mlilo [email protected]

      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

      The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

      THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

node-ping

Node uptime app
JavaScript
90
star
2

node-blogger

A static blog generator for Node.js.
JavaScript
51
star
3

nodetube

Download YouTube videos
JavaScript
49
star
4

node-mysql

Building Apps with Bookshelf.js, MySQL, and Express
JavaScript
38
star
5

nodebookmarks

A Single Page Web Application for managing browser bookmarks
JavaScript
31
star
6

node-streams

Using Node.js streams to join audio files
JavaScript
20
star
7

prompt

Simple Node.js commandline prompt
JavaScript
19
star
8

Bookmarks

A hacker's bookmarks
JavaScript
16
star
9

emitter

An event emitter - a fork of Backbone.js Events
JavaScript
15
star
10

image-processing

JavaScript
11
star
11

blog

Raging Flame Blog -
HTML
11
star
12

nodeza

A Node.js information portal and social platform for developers in South Africa.
JavaScript
10
star
13

crushit

CrushIt is a commandline tool for crawling web pages and compiling scripts.
JavaScript
9
star
14

uptime-emitter-desktop

Monitor your websites, APIs, and Microservices
Vue
9
star
15

pagination

Implementing Pagination in Bookshelf.js and Express applications
JavaScript
8
star
16

nodetube-desktop

Download and watch YouTube videos
JavaScript
6
star
17

rgb-to-grayscale

Convert images to Grayscale using HTML5 convas api
JavaScript
4
star
18

handling_migrations

JavaScript
3
star
19

winzip

CMD file archiver, created mainly for windows
JavaScript
3
star
20

widget-knex-schema

Knex generated tables using JS/JSON Object schemas
JavaScript
3
star
21

gatemaster

CSS
2
star
22

express-mysql

Express & MySQL boilerplate
CSS
2
star
23

ping

Roll out your own website uptime monitor with NodeJS
JavaScript
2
star
24

crushit-compiler

CrushIt Compiler crawls through a web page and crushes all scripts into a single file.
JavaScript
2
star
25

Donate-Button

A simple donate button for a payment gateway
1
star
26

ragingflame

Raging Flame Lab - My Node.js Website
JavaScript
1
star
27

ping-monitor-slack

JavaScript
1
star
28

ping-monitor-channels

JavaScript
1
star
29

uptime

JavaScript
1
star
30

com_mojovids

A Joomla! component for uploading image and video files, and making payment on Paypal
JavaScript
1
star
31

joomlajs

A Grunt-init template for creating Joomla! CMS components
JavaScript
1
star
32

draft

A real-time multi-player board game
JavaScript
1
star
33

ping-monitor-twitter

1
star
34

ping-monitor-email

JavaScript
1
star
35

com_clonard

A custom Joomla order form component
PHP
1
star
36

Templates

Joomla component templates
1
star
37

customized-swfupload

A customized swfupload
JavaScript
1
star
38

piflix

Desktop app for organising and watching movies
JavaScript
1
star
39

nodetube-website

CSS
1
star
40

qawemlilo.github.com

Qawelesizwe Mlilo: Github profile
1
star
41

posty

PHP
1
star
42

com_devotions

Devotions component for Pasters.
PHP
1
star
43

com_uploads

A custom Joomla component for mailtiple file uploads
1
star
44

saservice

Joomla! 2.5 Web App for business listings
PHP
1
star
45

Content-Slider

A jQuery plugin for displaying sliding html content
JavaScript
1
star