Skip to content

Instantly share code, notes, and snippets.

@n8v
Created September 15, 2012 07:18
Show Gist options
  • Save n8v/3726741 to your computer and use it in GitHub Desktop.
Save n8v/3726741 to your computer and use it in GitHub Desktop.
Nagios plugin example using Phantomjs headless webkit browser through the nice Casperjs API
#!/usr/local/bin/casperjs
/* js2 */
var casper = require('casper').create({
// iphone5 fullscreen portrait: 1136-by-640
viewportSize: {
width: 640,
height: 1136
}
});
// casper.echo("Starting up... at " + Date.now());
var last_timestamp, started_at;
last_timestamp = started_at = Date.now();
var perf_data = [];
var failures = [];
casper.on('starting', function() {
// this.echo("Browser ready and starting at " + Date.now());
elapsed("Browser ready");
});
casper.start('https://fairbanks.bannerhealth.com/cpoe_ordersets/');
casper.then(function() {
// this.echo("worksy");
timerize(elapsed("Home page loaded", true));
// TODO: Check the timestamps at bottom for recency.
this.fill('#searchform', {
q: "chest pain"
}, true);
});
casper.then(function() {
timerize(elapsed("Search results", true));
// The Tester methods produce unwanted output so we handle it manually.
var num_results = this.evaluate(function(){
return $('#search_results li').length;
});
if (num_results < 1) {
fail('At least one search result.')
}
else {
this.click('#search_results li a');
// this.waitUntilVisible('#cpoe-orders-page');
this.waitWhileVisible('#search_results');
}
});
casper.then(function() {
timerize(elapsed("View order set", true));
var num_checkboxes = this.evaluate(function(){
return $('input[type=checkbox]').length;
});
if (num_checkboxes < 10) {
fail('At least ten checkboxes (' + num_checkboxes + ') in the first chest pain order set.');
}
else {
this.clickLabel('Blank Careset PDF');
}
});
casper.then(function() {
timerize(elapsed("Blank Careset PDF", true));
});
casper.run(function() {
perf_data.unshift(elapsed("Total"));
// on completion of all the steps, exit "OK"
var result = "OK";
var message = "CPOE Ordersets UX";
nagios_exit(
"CHECK_SITE_UX",
result,
message,
perf_data.join(' ')
);
});
function elapsed(label, reset) {
var now = Date.now();
var elapsed_ms;
if (reset) {
elapsed_ms = now - last_timestamp;
last_timestamp = now;
}
else {
elapsed_ms = now - started_at;
}
// casper.echo(label + ': ' + elapsed_ms.toString() + " ms elapsed." );
return "'" + label + "'=" + elapsed_ms + 'ms';
}
function timerize(p) {
perf_data.push(p);
}
function fail(desc) {
casper.capture('fail.png');
nagios_exit(
"CHECK_SITE_UX",
'CRITICAL',
"CPOE Ordersets UX Failure: " + desc + ". Screenshot saved to 'fail.png'.",
perf_data.join(' ')
);
}
/*
* 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);
casper.exit(exit_code);
}
@forlornidealist
Copy link

I haven't been able to get this nor my own plugin I wrote before finding this page to work with Nagios. I get this error:
Fatal: [Errno 2] No such file or directory: did you install phantomjs?

I'm guessing it has something to do with ENV variables but I can't seem to figure it out. Any help greatly appreciated.

@stefreak
Copy link

did you install phantomjs?

@forlornidealist
Copy link

Yes I installed phantomjs. The test works fine if I run it manually from the shell.

@forlornidealist
Copy link

So to be clear on what I'm saying, I took your plugin line for line. If I run it in the shell (bash on CentOS 6.x), with casperjs and phantomjs installed and functional, it works fine. See output below. I then create a command and service check for this, just running the plugin with no arguments. It shows as a warning with this error: "Fatal: [Errno 2] No such file or directory: did you install phantomjs?". It is as if from the shell, nagios user knows how to find phantom from casper, but when run as an actual service check, it cannot seem to locate it.

Shell output:
[nagios@testbox libexec]$ ./caspertest
CHECK_SITE_UX CRITICAL: CPOE Ordersets UX Failure: At least one search result.. Screenshot saved to 'fail.png'.|'Home page loaded'=3084ms 'Search results'=20ms
[nagios@testbox libexec]$ echo $?
2

@randy-s
Copy link

randy-s commented Jul 17, 2014

That's because it's not finding phantomjs in the path. I'm not too familiar with javascript so I'm not sure how you'd set the path there but see here for an explanation of what's almost certainly happening:

http://www.nikola-breznjak.com/blog/casperjs/installing-casperjs-on-rhel-linux-distribution/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment