I'm trying to write a plugin for nagios XI since, i have not found the one that suits me.. And i'm trying to check disk of a linux server,
Note that this code is not yet perfected but i would like to know if i did anything wrong, I have not seen any tutorials or documentation about creating this type of plugins (except official nagion documentation, but it really does not suit my needs), when ever i run this in nagios with the right parameters i get error saying "No output on stdout"
Code: Select all
<?php
//
//Basic Knowledge Of Script
define("PROGRAM","custom_disk_check");
define("VERSION", "0.0.1");
define("STATUS_OK", 0);
define("STATUS_WARNING", 1);
define("STATUS_CRITICAL", 2);
define("STATUS_UNKNOWN", 3);
//
//Specification Arguments
$host = "";
$port = "";
$user = "";
$password = "";
$path = "";
$warn = "";
$crit = "";
$mode = ""; // Warning: Make Sure That The Units You Use Here (In Mode), You Use In Warning & Critical.. You will get an error if you do not do so
//
//Nagios print out, status..
function nagios_print($stdout = '', $exitcode=0) {
print($stdout);
exit($exitcode);
}
//
// Prints out error message
function plugin_error($errmessage)
{
print("***ERROR***:\n\n{$errmessage}\n\n")
usage();
nagios_print("", STATUS_UNKNOWN);
}
//
//Selects Unit Mode Based Off Of Set Variable
function unitMode($unit)
{
global $mode;
$unitModes = array('precent', 'kb', 'mb', 'gb', 'tb');
switch($mode)
{
case 'precent':
$unit = '%';
break;
case 'kb':
$unit = 'KB';
break;
case 'gb':
$unit = 'GB';
break;
case 'tb':
$unit = 'TB';
break;
case 'mb':
$unit = 'MB';
break;
default:
$unit = 'MB';
}
return $unit;
}
//
//Normal disk stats in either kB, MB, GB, or TB
function disk_stat($path)
{
global $mode, $path;
//Gets disk space(s)
$freespace = disk_free_space($path);
$totalspace = disk_total_space($path);
$uunit = unitMode($mode);
$status = SetStatus($freespace);
if ($uunit == '%')
{
nagios_print('You have '.(($freespace / $totalspace) * 100) .$uunit 'of free space', $status );
}
else
{
nagios_print('Total Space:\n'.$totalspace .$uunit '\nFree Space:\n'.$freespace .$uunit, $status);
}
}
//
//If sentence to specify status of the nagios output
function SetStatus($freespace)
{
global $warn, $crit;
$status = STATUS_UNKNOWN;
if($freespace > $warn)
{
$status = STATUS_OK; //Ok Status
}
else if($freespace <= $warn && $freespace > $crit)
{
$status = STATUS_WARNING; // Warning Status
}
else if($freespace <= $crit)
{
$status = STATUS_CRITICAL; // Critical Status duh..
}
return $status;
}
//
//Prints out How To Use this script
function usage()
{
print(" \n
Usage: ".PROGRAM." -h | -d <domain> [-c <critical>] [-w <warning>] [-s <whoisServer>]
NOTE: host, port, path, need to be specified!
Options:
-host
Enter IP Of The Host
-port
Enter Port Of The Host
-user
Enter Username
-password
Enter Password
-path
Enter Path Of The Partition
-warn
Enter Warning Value To Get Warned Whenever The Disk Reaches That Value.
NOTE: The Value Should Be In The Unit You Are Going To Use In Mode Parameter.
-crit
Enter Critical Value To Get Warned Whenever The Disk Reaches That Value.
NOTE: The Value Should Be In The Unit You Are Going To Use In Mode Parameter.
-mode
Enter Mode To Use Specific Unit ['precent','kb','mb','gb','tb']
This plugin will use the PHP command to check disk space of certain server.
Example:
$./".PROGRAM." -host 127.0.0.1 -port 22 -user admin -password admin123 -path /root -warn 80 -crit 90 -mode precent
\n ");
}
function error()
{
}
function Connect()
{
global $host, $port, $user, $password;
$connection = ssh2_connect($host, $port);
ssh2_auth_password($connection, $user, $password);
$stream = ssh2_exec($connection, 'hostname');
echo "Output: " . stream_get_contents($stream);
}
function Main()
{
Connect();
disk_stat($path);
}
Main(); // Calling Main Function Of The Plugin
?>