npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nagios-plugin

v0.1.2

Published

Facilitates writing Nagios plugins

Downloads

20

Readme

nagios-plugin

Toolkit facilitates writing Nagios plugins in Node.js. It is a mimic of Perl Nagios::Plugin module, barring command line parsing functions in favor of many existing npm modules such as node-getopt or commander.js.

Annotated Working Example

Following script implements a plugin to check web sites using external program wget. This plugin addresses some defects in Nagios built-in plugin check_http, for instance unable to failover to next IP when a web site is mapped to multiple IPs and attempts to connect to currently chosen IP failed at TCP socket layer.

wget-http.js:

'use strict';
// add a command line parser;
// you can substitute with your own favorite npm module
var getOpt = require('node-getopt')
.create([ [ 'm', 'match=<STRING>', 'String response body must match' ]
        , [ 'w', 'warning=<STRING>', 'Warning threshold' ]
        , [ 'c', 'critical=<STRING>', 'Critical threshold' ]
        , [ 'h', 'help', 'display this help' ] ])
.bindHelp();
getOpt.setHelp('Usage: node wget-http.js [Options] -- '
		+ '<arguments passed to wget>\nOptions:\n[[OPTIONS]]');
var args = getOpt.parseSystem();
// validate mandatory arguments
if (args.argv.length == 0) {
	console.log('missing arguments passed to wget');
	getOpt.showHelp();
	process.exit(3);
}

var Plugin = require('nagios-plugin');
// create a new plugin object with optional initialization parameters
var o = new Plugin({
	// shortName is used in output
	shortName : 'wget_http'
});
// set monitor thresholds
o.setThresholds({
	'critical' : args.options.critical || 2,
	'warning' : args.options.warning || 0.2
});

// run the check - replace with your own business logic
var exec = require('child_process').exec;
var before = new Date().getTime();
exec('wget -qO- ' + args.argv.join(' '), function(error, stdout, stderr) {
	var after = new Date().getTime();
	var diff = (after - before) / 1000;

	// check actual data against predefined threshold
	// and returns state: OK, WARNING or CRITICAL
	var state = o.checkThreshold(diff);
	// Add message for later output. Multiple messages
	// in the same state are concatenated at output
	o.addMessage(state, stdout.length + ' bytes in ' + diff
			+ ' seconds response time.');
	if (args.options.match && stdout.indexOf(args.options.match) === -1) {
		o.addMessage(o.states.CRITICAL, args.options.match + ' not found');
	}
	// Add performance data
	o.addPerfData({
		label : "time",
		value : diff,
		uom : "s",
		threshold : o.threshold,
		min : 0
	});
	o.addPerfData({
		label : "size",
		value : stdout.length,
		uom : "B",
		min : 0
	});
	// check messages added earlier and return the most severe set:
	// CRITICAL; otherwise WARNING; otherwise OK
	var messageObj = o.checkMessages();
	// output the short name, state, message and perf data
	// exit the program with state as return code
	o.nagiosExit(messageObj.state, messageObj.message);
});

Outputs

$ node wget-http.js -- http://www.google.com
WGET_HTTP WARNING - 11815 bytes in 0.571 seconds response time.|time=0.571s;0.2;2;0; size=11815B;;;0;
$ node wget-http.js --match=unicorn -- http://www.google.com
WGET_HTTP CRITICAL - unicorn not found|time=0.467s;0.2;2;0; size=11763B;;;0;
$ node wget-http.js --help 
Usage: node wget-http.js [Options] -- <arguments passed to wget>
Options:
  -m, --match=<STRING>     String response body must match
  -w, --warning=<STRING>   Warning threshold
  -c, --critical=<STRING>  Critical threshold
  -h, --help               display this help
$ node wget-http.js
missing arguments passed to wget
Usage: ... <same as --help>

API

constructor

var o = new Plugin({
	shortName : 'wget_http'
});
  • shortName is used in output, if omitted by default it is set to JavaScript file name launched by program

properties

  • states

    enum

    o.states = {
    	OK : 0,
    	WARNING : 1,
    	CRITICAL : 2,
    	UNKNOWN : 3
    }

methods

  • setThresholds

    o.setThresholds({
    	'critical' : ...,
    	'warning' : ...
    });

    See Nagios Plugin Development Guidelines for valid threshold formats

    • There is only one critical and warning threshold set. If setThresholds is called multiple times, the result is concatenated and latter settings overwrites previous ones.
  • checkThreshold

    var state = o.checkThreshold(actualData);

    check actualData against predefined threshold. Returns the matching state.

  • addMessage

    o.addMessage(state, 'msg');

    Add message to the corresponding state.

  • checkMessages

    var messageObj = o.checkMessages();

    checkMessages concatenates messages added with addMessage by state and returns the {state, message} pair of the most severe state with message populated in this precedence: critical, warning or OK.

  • addPerfData

    o.addPerfData({
    	label : "time",
    	value : diff,
    	uom : "s",
    	threshold : o.threshold,
    	min : 0,
    	max : 100
    })	

    See Nagios Plugin Development Guidelines for valid UOMs.

  • nagiosExit

    o.nagiosExit(state, message);

    Generates one line output conforming to Nagios expected format and exit the program with state as return code. State and message should come from checkMessages output.

  • getReturnMessage

    var msg = o.getReturnMessage(state, message);

    If you want the output string but don't want to quit the program by calling nagiosExit, you can call getReturnMessage with same parameters as nagiosExit. Internally nagiosExit calls getReturnMessage.

Install

npm install nagios-plugin