Advertisement
Acer364

C.D.C. - NagiosXI Plugin - Unfinished

Jul 27th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. //Custom Disk Check
  2. <?php
  3. //
  4. //Basic Knowledge Of Script
  5. define("PROGRAM","custom_disk_check");
  6. define("VERSION", "0.0.1");
  7. define("STATUS_OK", 0);
  8. define("STATUS_WARNING", 1);
  9. define("STATUS_CRITICAL", 2);
  10. define("STATUS_UNKNOWN", 3);
  11. //
  12. //Specification Arguments
  13. $host = "";
  14. $port = "";
  15. $user = "";
  16. $password = "";
  17. $path = "";
  18. $warn = "";
  19. $crit = "";
  20. $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
  21. //
  22. //Nagios print out, status..
  23. function nagios_print($stdout = '', $exitcode=0) {
  24.     print($stdout);
  25.     exit($exitcode);
  26. }
  27. //
  28. //Let's hope this never happens but..
  29. //In case it does, it must be here.. Prints out error message
  30. function plugin_error($errmessage)
  31. {
  32.  
  33.     print("***ERROR***:\n\n{$errmessage}\n\n")
  34.     usage();
  35.     nagios_print("", STATUS_UNKNOWN);
  36.  
  37. }
  38. //
  39. //Selects Unit Mode Based Off Of Set Variable
  40. function unitMode($unit)
  41. {
  42.  
  43.   global $mode;
  44.   $unitModes = array('precent', 'kb', 'mb', 'gb', 'tb');
  45.  
  46.   switch($mode)
  47.   {
  48.  
  49.   case 'precent':
  50.      $unit = '%';
  51.       break;
  52.  
  53.   case 'kb':
  54.      $unit = 'KB';
  55.       break;
  56.  
  57.   case 'gb':
  58.      $unit = 'GB';
  59.       break;
  60.  
  61.   case 'tb':
  62.      $unit = 'TB';
  63.       break;
  64.  
  65.    case 'mb':
  66.      $unit = 'MB';
  67.       break;
  68.  
  69.    default:
  70.      $unit = 'MB';
  71.  
  72.    }
  73.  
  74.    return $unit;
  75.  
  76. }
  77. //
  78. //Normal disk stats in either kB, MB, GB, or TB
  79. function disk_stat($path)
  80. {
  81.  
  82.   global $mode, $path;
  83.  
  84.   //Gets disk space(s)
  85.   $freespace = disk_free_space($path);
  86.   $totalspace = disk_total_space($path);
  87.  
  88.   $uunit = unitMode($mode);
  89.   $status = SetStatus($freespace);
  90.  
  91.   if ($uunit == '%')
  92.   {
  93.     nagios_print('You have '.(($freespace / $totalspace) * 100) .$uunit 'of free space', $status );
  94.   }
  95.   else
  96.   {
  97.     nagios_print('Total Space:\n'.$totalspace .$uunit '\nFree Space:\n'.$freespace .$uunit, $status);
  98.   }
  99.  
  100. }
  101. //
  102. //If sentence to specify status of the nagios output
  103. function SetStatus($freespace)
  104. {
  105.  
  106.   global $warn, $crit;
  107.  
  108.   $status = STATUS_UNKNOWN;
  109.  
  110.   if($freespace > $warn)
  111.   {
  112.         $status = STATUS_OK; //Ok Status
  113.   }
  114.   else if($freespace <= $warn && $freespace > $crit)
  115.   {
  116.         $status = STATUS_WARNING; // Warning Status
  117.   }
  118.   else if($freespace <= $crit)
  119.   {
  120.         $status = STATUS_CRITICAL; // Critical Status duh..
  121.   }
  122.  
  123.   return $status;
  124.  
  125. }
  126. //
  127. //Prints out HowToUse this script
  128. function usage()
  129. {
  130.  
  131.   print(" \n
  132.  Usage: ".PROGRAM." -h | -d <domain> [-c <critical>] [-w <warning>] [-s <whoisServer>]
  133.  NOTE: host, port, path, need to be specified!
  134.  
  135.  Options:
  136.  -host
  137.       Enter IP Of The Host
  138.  
  139.  -port
  140.       Enter Port Of The Host
  141.  
  142.  -user
  143.       Enter Username
  144.  
  145.  -password
  146.       Enter Password
  147.  
  148.  -path
  149.       Enter Path Of The Partition
  150.  
  151.  -warn
  152.       Enter Warning Value To Get Warned Whenever The Disk Reaches That Value.
  153.       NOTE: The Value Should Be In The Unit You Are Going To Use In Mode Parameter.
  154.  
  155.  -crit
  156.       Enter Critical Value To Get Warned Whenever The Disk Reaches That Value.
  157.       NOTE: The Value Should Be In The Unit You Are Going To Use In Mode Parameter.
  158.  
  159.  -mode
  160.       Enter Mode To Use Specific Unit ['precent','kb','mb','gb','tb']
  161.  
  162.  This plugin will use the PHP command to check disk space of certain server.
  163.  Example:
  164.       $./".PROGRAM." -host 127.0.0.1 -port 22 -user admin -password admin123 -path /root -warn 80 -crit 90 -mode precent
  165.  
  166.        \n ");
  167.  
  168. }
  169.  
  170. function error()
  171. {
  172.  
  173. }
  174.  
  175. function Connect()
  176. {
  177.   global $host, $port, $user, $password;
  178.  
  179.   $connection = ssh2_connect($host, $port);
  180.   ssh2_auth_password($connection, $user, $password);
  181.   $stream = ssh2_exec($connection, 'hostname');
  182.   echo "Output: " . stream_get_contents($stream);
  183.  
  184. }
  185.  
  186. function Main()
  187. {
  188.  
  189.   Connect();
  190.    
  191.     disk_stat($path);
  192.  
  193. }
  194.  
  195.         Main(); //Should work shouln't it?
  196.  
  197. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement