Skip to content

Instantly share code, notes, and snippets.

@dermoth
Created April 13, 2013 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dermoth/61ca7124c8de89cbdbed to your computer and use it in GitHub Desktop.
Save dermoth/61ca7124c8de89cbdbed to your computer and use it in GitHub Desktop.
Template php nagios check
<?php
# Template php nagios check
#
# Meant to work along with nagios's check_http. In addition to the
# returned string which is a standard nagios result, the script returns
# 403 and 503 errors on WARNING and CRITICAL respectively to make
# check_http return an equivalent status. The HTTP status printed by
# check_http is also the script result.
#
# Author: Thomas Guyot-Sionnest <tguyot@gmail.com>
#
ini_set('display_errors', '0');
### Config
# Default thresholds
$warn = 60;
$crit = 300;
# Things to check
$things = array(
'foo' => 0,
'bar' => 1,
'baz' => 2,
);
###
// Exit wrapper function
function nagios_exit($code, $message, $perf='') {
switch ($code) {
case 0:
$err = 'OK';
header('HTTP/1.1 200 '."$err: $message\n");
break;
case 1:
$err = 'WARNING';
header('HTTP/1.1 403 '."$err: $message\n");
break;
case 2:
$err = 'CRITICAL';
header('HTTP/1.1 503 '."$err: $message\n");
break;
default:
header('HTTP/1.1 400 Bad Request');
$err = 'UNKNOWN';
}
header('Content-Type: text/plain');
echo "$err: $message |$perf\n";
exit();
}
if (isset($_REQUEST['thing']) && isset($things[$_REQUEST['thing']])) {
$type = $_REQUEST['thing'];
$thing = $things[$type];
} else {
nagios_exit(3, 'Missing or invalid `thing\' parameter');
}
if (isset($_REQUEST['value'])) {
if (is_numeric($_REQUEST['value'])) {
$value = $_REQUEST['value'];
} else {
nagios_exit(3, 'Invalid `value\' parameter');
}
} else {
nagios_exit(3, 'Missing `value\' parameter');
}
if (isset($_REQUEST['warn'])) {
if (is_numeric($_REQUEST['warn'])) {
$warn = $_REQUEST['warn'];
} else {
nagios_exit(3, 'Invalid `warn\' parameter');
}
}
if (isset($_REQUEST['crit'])) {
if (is_numeric($_REQUEST['crit'])) {
$crit = $_REQUEST['crit'];
} else {
nagios_exit(3, 'Invalid `crit\' parameter');
}
}
try {
// Do real stuff here...
$res = $thing * $value;
} catch (Exception $e) {
nagios_exit(3, 'Shit happens: '.$e);
}
$ret = 3;
if ($res >= $crit) {
$ret = 2;
} else if ($res >= $warn) {
$ret = 1;
} else if ($res >= 0) {
$ret = 0;
}
nagios_exit($ret, $type.'value is '.$res, 'thing='.$res.'s;'.$warn.';'.$crit.';0;');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment