Advertisement
Acer364

Nagios Plugin 2

Aug 14th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.16 KB | None | 0 0
  1. /*
  2. */
  3. <?php
  4.  
  5. define('OK', 0);
  6. define('WARNING', 1);
  7. define('CRITICAL', 2);
  8. define('UNKNOWN', 3);
  9. define('DEBUG', false);
  10.  
  11. $wArgL = array(
  12.                $warg1 = '30',
  13.                $warg5 = '25',
  14.                $warg15 = '20'
  15.               );
  16.  
  17. $cArgL = array(
  18.                $carg1 = '15',
  19.                $carg5 = '10',
  20.                $carg15 = '5'
  21.                );
  22.  
  23. $wArgD = '';
  24.  
  25. $cArgD = '';
  26.  
  27. $ConArg = array(
  28.                 $host = '127.0.0.1',
  29.                 $port = '22',
  30.                 $user = 'root',
  31.                 $pass = 'nagiosxi'
  32.                );
  33.  
  34. $mode = array(
  35.               'cpuload',
  36.               'diskcheck'
  37.              );
  38.  
  39. $path = '/';
  40.  
  41. $diskMode = array(
  42.                   'USpercent', //Used Space Percentage
  43.                   'FSpercent', // Free Space Percentage
  44.                   'kB',
  45.                   'MB',
  46.                   'KB',
  47.                   'GB',
  48.                   'TB'
  49.                  );
  50.  
  51. function Connect($ConArg)
  52. {
  53.     CheckIfEmpty($ConArg)
  54.  
  55.     $connection = ssh2_connect($host, $port);
  56.     ssh2_auth_password($connection, $user, $pass);
  57.  
  58.     return($connection);
  59. }
  60.  
  61. function NagOut($msg = '', $exitcode = 0)
  62. {
  63.     print($msg);
  64.     exit($exitcode);
  65. }
  66.  
  67. function ErrorMsg($errMsg = '', $exitcode = 3)
  68. {
  69.   print($errMsg);
  70.   exit($exitcode);
  71. }
  72.  
  73. function CheckIfEmpty($someArg)
  74. {
  75.   $argType = gettype($someArg);
  76.  
  77.  if($argType == "array")
  78.  {
  79.     $someNum = array_count_values($someArg);
  80.     for($Z = 0, $Z < $someNum, $Z++)
  81.     {
  82.       if(empty($someArg[$Z]) == True)
  83.       {
  84.         ErrorMsg('Some of the crucial array elements are not defined, please check the arguments /n');
  85.       }
  86.     }
  87.     elseif(empty($someArg) == True)
  88.   {
  89.     ErrorMsg('Some of the crucial elements are not defined, please check arguments /n');
  90.   }
  91.  }
  92. }
  93.  
  94. function ArrayComparison($L, $W, $C)
  95. {
  96.  
  97.   CheckIfEmpty($W);
  98.   CheckIfEmpty($C);
  99.  
  100.   for($x = 0, $x <= 2, $x++)
  101.   {
  102.     if($L[$x] > $W[$x] && $L[$x] < $C[$x])
  103.     {
  104.         $status = 1;
  105.     }
  106.     elseif($L[$x] > $C[$x])
  107.     {
  108.         $status = 2;
  109.     }
  110.     else
  111.     {
  112.         $status = 0;
  113.     }
  114.   }
  115.   return($status);
  116. }
  117.  
  118. function CPULoad()
  119. {
  120.   global $wArgL, $cArgL;
  121.  
  122.   Connect($ConArg);
  123.  
  124.   $load = sys_getloadavg();
  125.  
  126.   CheckIfEmpty($wArgL);
  127.   CheckIfEmpty($cArgL);
  128.  
  129.   ArrayComparison($load, $wArgL, $cArgL);
  130.  
  131.   NagOut('CPU Load: '.$load, $status);
  132. }
  133.  
  134. function DiskCheck()
  135. {
  136.   global $path, $diskMode, $wArgD, $cArgD;
  137.  
  138.   CheckIfEmpty($diskMode);
  139.   CheckIfEmpty($path);
  140.   CheckIfEmpty($wArgD);
  141.   CheckIfEmpty($cArgD);
  142.  
  143.   Connect();
  144.   $dfs = disk_free_space($path);
  145.   $dts = disk_total_space($path);
  146.  
  147.   if($fs >= $wArgD && $fs < $cArgD)
  148.   {
  149.     $statusD = 1;
  150.   }
  151.   elseif($fs >= $cArgD)
  152.     {
  153.       $statusD = 2;
  154.     }
  155.     else
  156.       {
  157.         $statusD = 0;
  158.       }
  159.       switch($diskMode)
  160.       {
  161.           case 'USpercent':
  162.             NagOut('Used Space[%]: '.(100-($dfs/$dts)*100) , $statusD);
  163.             break;
  164.  
  165.           case 'FSpercent':
  166.             NagOut('Free Space[%]: '.(($dfs/$dts)*100) , $statusD);
  167.             break;
  168.  
  169.           case 'kB':
  170.             NagOut(/'Total Space[kB]: '.($dts * 1024) 'Free Space[kB]: '/.($dfs * 1024), $statusD);
  171.             break;
  172.  
  173.           case 'MB':
  174.             NagOut(/'Total Space[MB]: '.($dts * 1024 * 1024) 'Free Space[MB]: '/.($dfs * 1024 * 1024), $statusD);
  175.             break;
  176.  
  177.           case 'GB':
  178.             NagOut(/'Total Space[GB]: '.($dts * 1024 * 1024 * 1024) 'Free Space[GB]: '/.($dfs * 1024 * 1024 * 1024), $statusD);
  179.             break;
  180.  
  181.           case 'TB':
  182.             NagOut(/'Total Space[TB]: '.($dts * 1024 * 1024 * 1024 * 1024) 'Free Space[TB]: '/.($dfs * 1024 * 1024 * 1024 * 1024), $statusD);
  183.  
  184.           default:
  185.             NagOut(/'Total Space[MB]: '.($dts * 1024 * 1024) 'Free Space[MB]: '/.($dfs * 1024 * 1024), $statusD);
  186.       }
  187. }
  188.  
  189. function ModeSelect()
  190. {
  191.   global $mode;
  192.  
  193.   switch($mode)
  194.   {
  195.       case 'cpuload':
  196.       CPULoad();
  197.           break;
  198.  
  199.       case 'diskcheck':
  200.       DiskCheck();
  201.           break;
  202.  
  203.       default:
  204.       DiskCheck();
  205.   }
  206. }
  207.  
  208. function Main()
  209. {
  210.   ModeSelect();
  211. }
  212.  
  213. Main();
  214.  
  215. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement