Page 1 of 1

Creation Of Plugins - PHP

Posted: Mon Jul 31, 2017 1:51 am
by polariser
Hello!

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

?>



Re: Creation Of Plugins - PHP

Posted: Mon Jul 31, 2017 2:10 am
by polariser
I have just found these tutorials tough, i had to look a bit into it but i found some useful information.. So if anyone else needs it i'm posting these here:

https://assets.nagios.com/downloads/nag ... ios-XI.pdf
https://support.nagios.com/kb/article.php?id=52
https://support.nagios.com/kb/article/n ... nwc15.html
https://support.nagios.com/kb/article/n ... nwc15.html

I still need to know if i'm doing something wrong in my code :)

Re: Creation Of Plugins - PHP

Posted: Mon Jul 31, 2017 10:45 am
by lmiltchev
Writing (assisting with) custom plugins is out of scope for nagios support. I will leave the thread open in case some community members want to help/chime in. Your other option would contact our sales team at [email protected], and request a quote for custom development.

FYI, I tried running the plugin but I didn't get the "No output on stdout" output. I got the following error instead:

Code: Select all

PHP Parse error:  syntax error, unexpected 'usage' (T_STRING) in /usr/local/nagios/libexec/check_disk_custom.php on line 33

Re: Creation Of Plugins - PHP

Posted: Mon Jul 31, 2017 10:59 am
by mcapra
The nice (and sometimes terrible) thing about PHP is that it's a very relaxed language.

Nagios Core/XI aren't going to place restrictions on how you make your plugin. You can use just about any language that uses standard Unix exit codes and stdout. I get similar parsing issues as @lmiltchev:

Code: Select all

[root@nagios tmp]# php -q check_disk_custom.php
PHP Parse error:  syntax error, unexpected 'usage' (T_STRING) in /tmp/check_disk_custom.php on line 33
Which implies that there is an unexpected occurrence of something on line 33. It looks like a print command doesn't have a closing semi-colon ; on Line 32:

Code: Select all

 29 function plugin_error($errmessage)
 30 {
 31
 32     print("***ERROR***:\n\n{$errmessage}\n\n")
 33     usage();
 34     nagios_print("", STATUS_UNKNOWN);
 35
 36 }
Be sure to end all executions/definitions in PHP with a ; .
polariser wrote:when ever i run this in nagios with the right parameters i get error saying "No output on stdout"
This is most likely due to the status not going to stdout, but rather stderr due to the parsing error mentioned above.

I also noticed another error on Line 92 where you are not correctly concatenating your string literals with your variables:

Code: Select all

    
[root@nagios tmp]# php -q check_disk_custom.php
PHP Parse error:  syntax error, unexpected ''of free space'' (T_CONSTANT_ENCAPSED_STRING) in /tmp/check_disk_custom.php on line 92

...

     90   if ($uunit == '%')
     91   {
     92     nagios_print('You have '.(($freespace / $totalspace) * 100) .$uunit 'of free space', $status );
     93   }
     94   else
     95   {
     96     nagios_print('Total Space:\n'.$totalspace .$uunit '\nFree Space:\n'.$freespace .$uunit, $status);
     97   }
.$uunit 'of free space' needs a . between $uuinit and 'of free space'.

Re: Creation Of Plugins - PHP

Posted: Mon Jul 31, 2017 11:02 am
by lmiltchev
Thanks @mcapra!

Re: Creation Of Plugins - PHP

Posted: Tue Aug 01, 2017 1:26 am
by polariser
Thank you, really! I'm going to fix the code right now

Re: Creation Of Plugins - PHP

Posted: Tue Aug 01, 2017 11:37 am
by bolson
May we close this topic?