Created
September 15, 2012 06:21
-
-
Save n8v/3726576 to your computer and use it in GitHub Desktop.
Example Nagios plugin using Node.js
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
#!/usr/bin/env node | |
/* -*- js2 -*- */ | |
/* ************************************************************* */ | |
// Set up classes to support Range objects. Ported from | |
// Nagios::Plugin::Range. See named functions below. | |
// TODO: Modularize properly/idiomatically. | |
/* | |
* Represents a Nagios plugin range object. | |
* @constructor | |
*/ | |
function Range(start, end, alert_on_outside) { | |
this.start = start; | |
this.end = end; | |
this.alert_on_outside = alert_on_outside; | |
} | |
Range.prototype = { | |
toString: RangeToString, | |
check: RangeCheck | |
}; | |
/* ************************************************************* */ | |
var plugin_name = 'CHECK_STUFF'; | |
// Set up command line args and usage etc using commander.js. | |
var cli = require('commander'); | |
cli | |
.version('0.0.1') | |
.option('-c, --critical <critical threshold>', 'Critical threshold using standard format', parseRangeString) | |
.option('-w, --warning <warning threshold>', 'Warning threshold using standard format', parseRangeString) | |
.option('-r, --result <Number4>', 'Use supplied value, not random', parseFloat) | |
.parse(process.argv); | |
var val = cli.result; | |
if (val == undefined) { | |
val = Math.floor((Math.random() * 20) + 1); | |
} | |
var message = ' Sample result was ' + val.toString(); | |
var perfdata = "'Val'="+val + ';' + cli.warning + ';' + cli.critical + ';'; | |
if (cli.critical && cli.critical.check(val)) { | |
nagios_exit(plugin_name, "CRITICAL", message, perfdata); | |
} | |
else if (cli.warning && cli.warning.check(val)) { | |
nagios_exit(plugin_name, "WARNING", message, perfdata); | |
} | |
else { | |
nagios_exit(plugin_name, "OK", message, perfdata); | |
} | |
/* ************************************************************* */ | |
/* | |
* Standard stringification a la | |
* http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT | |
* | |
* E.g. @10:~ means "outside the range 10 to Infinity" | |
*/ | |
function RangeToString() { | |
return ( this.alert_on_outside ? '' : '') + | |
(this.start == Number.NEGATIVE_INFINITY ? '~' : this.start) + | |
':' + | |
(this.end == Infinity ? '' : this.end) ; | |
}; | |
/* | |
* Returns true if an alert should be raised (the value is outside | |
* of the range).a | |
* | |
* @param v Number The value to be checked. | |
* @return boolean | |
*/ | |
function RangeCheck (v) { | |
var alerty = false || ! this.alert_on_outside; | |
if (this.start <= v && v <= this.end) { | |
return alerty; | |
} | |
else { | |
return ! alerty; | |
} | |
} | |
/* | |
* See | |
* http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT | |
* | |
* @param t String The string with a range definition. | |
*/ | |
function parseRangeString(t) { | |
var start = 0; | |
var end = Number.POSITIVE_INFINITY; | |
var alert_on_outside = true; | |
var valid = false; | |
// console.log('parsing Range %j', t); | |
// Strip whitespace. | |
t = t.toString().replace(/\s/g, ''); | |
// Validate | |
if ( ! /[\d~]/.test(t) || | |
! /^\@?(-?[\d.]+|~)?(:(-?[\d.]+)?)?$/.test(t) | |
) { | |
console.error('Invalid range definition %j', t); | |
process.exit(3); | |
} | |
if ( /^@/.test(t) ) { | |
alert_on_outside = false; | |
t = t.substring(1); | |
} | |
if ( /^~:/.test(t) ) { // '~:x' | |
start = Number.NEGATIVE_INFINITY; | |
t = t.substring(2); | |
valid = true; | |
} | |
var m = t.match(/^([\d\.-]+)?:/); // '10:' | |
if (m != null ) { | |
start = m[1]; | |
t = t.replace(/^([-\d\.]+)?:/, ''); | |
valid = true; | |
} | |
m = t.match(/^([-\d\.]+)$/); // 'x:10' or '10' | |
if (m != null ) { | |
end = m[1]; | |
valid = true; | |
} | |
if (valid && start <= end) { | |
return new Range(start, end, alert_on_outside); | |
} | |
else { | |
console.error('Invalid range definition %j', t); | |
process.exit(3); | |
} | |
return null; | |
} | |
// End Range stuff | |
/* | |
* Exit the plugin with Nagios specified output. See | |
* http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOUTPUT | |
* | |
* @param plugin_name String The name of the plugin e.g. 'CHECK_STUFF'. | |
* @param state String "OK", "WARNING", "CRITICAL" or defaults to "UNKNOWN". | |
* @param message String Human-readable message. | |
* @param perfdata String Nagios formatted perfomance data string. | |
*/ | |
function nagios_exit (plugin_name, state, message, perfdata) { | |
var exit_code, valid_state; | |
switch (state.toUpperCase()) { | |
case 'CRITICAL': | |
valid_state = state; | |
exit_code = 2; | |
break; | |
case 'WARNING': | |
valid_state = state; | |
exit_code = 1; | |
break; | |
case 'OK': | |
valid_state = state; | |
exit_code = 0; | |
break; | |
default: | |
valid_state = 'UNKNOWN'; | |
exit_code = 3; | |
} | |
console.log(plugin_name + ' ' + valid_state + ':' + message + '|' + perfdata); | |
process.exit(exit_code); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment