Author: The_PHP_Jedi Language: php
Description: MOSMC-PHP v1.1.4 Timestamp: 2008-07-20 18:57:36 -0400
View raw paste Parent paste by: The_PHP_Jedi - Child paste by: The_PHP_Jedi Reply
  1. <?php
  2.  
  3. /***** File Info *****\
  4.  
  5. == Version: 1.1.4 ==
  6. == Description: Multi-Output Server Monitoring Console in PHP (MOSMC-PHP) ==
  7. == Author: Alesandro Ortiz - The_PHP_Jedi <thephpjedi@thephpjedi.com> ==
  8. == Created: Wednesday, July 2nd 2008 ==
  9. == Modified: Saturday, July 19th 2008 ==
  10. == License: Creative Commons Attribution-Noncommercial 3.0 United States ==
  11. == License URL: http://creativecommons.org/licenses/by-nc/3.0/us/ ==
  12.  
  13. \***** File Info *****/
  14.  
  15.                 /*** Changelog ***\
  16.                 == 1.1.0 ==
  17.                 = Public release
  18.                 == 1.1.1 & 1.1.2 ==
  19.                 = Fixed minor bugs regarding display of information
  20.                 == 1.1.3 ==
  21.                 = New Feature: Different e-mail subject, sender, and recipient for Reports and Alerts
  22.                 = Bug fix: Site monitor would show incorrect information in most cases where more than one site was monitored
  23.                 = Display fix: E-mail report would show 'MB' in RAM information in a new line
  24.                 == 1.1.4 ==
  25.                 = Added User Agent "MOSMC-PHP" when checking sites.
  26.                 = Minor bug fix related to services checking. Did not affect functionality.
  27.                 \*** Changelog ***/
  28.  
  29.                 /*** To-Do ***\
  30.                 = Nothing right now, but I'm sure I can find new features I can add very soon ;)
  31.                 \*** To-Do ***/
  32.                
  33.                 /*** Comments ***\
  34.                 = MOSMC is pronounced as "moss-mick"
  35.                 = I admit I could've used some OOP, or at least functions, but currently it's more scalable and configurable. Feel free to improve upon it, but please let me know what you've done with it. :)
  36.                 \*** Comments ***/
  37.                
  38.                 /*** Bugs ***\
  39.                 = Site Monitor, when testing site that it cannot connect to, displays HTTP 200 (OK) text in GUI and Alerts. Oddly enough, it triggers the GUI + Alert mechanism.
  40.                 \*** Bugs ***/
  41.                
  42.                 /*** Notes & Usage ***\
  43.                 == General ==
  44.                 = Status codes in ascending order of severity. Higher number, higher severity.
  45.                 = Site monitor links to URL only in case of failure. Otherwise, it doesn't link to the URL being tested.
  46.                 =
  47.                 == Local Cron Jobs ==
  48.                 = Use command 'php /path/to/monitor.php type password'
  49.                 = Example: 'php /path/to/monitor.php failure yourpass' if you wanted to send emails only on failure
  50.                 =
  51.                 == Remote Cron Jobs or On-Demand Reporting ==
  52.                 = Use monitor.php?cron=1&type=failure&pass=yourpass if you want to send an email formatted for SMS on failure with the details of the failing component (SMS formatting isn't applied with &report=1)
  53.                 = Use monitor.php?cron=1&type=report&pass=yourpass if you want to send email with full report regardless of status
  54.                 = To send an e-mail, you need to append the password you've set in the configuration to the URL. This prevents others from triggering e-mails. Example: monitor.php?cron=1&type=failure&pass=yourpass
  55.                 =
  56.                 == How MOSMC-PHP Tests ==
  57.                 = Services: Checks first if services are listening, then checks if it accepts internal connections, then checks if it accepts external connections.
  58.                 = Sites: Checks HTTP header response.
  59.                 =
  60.                 == Disclaimers ==
  61.                 = All user input that is accepted by this script is only used for loose comparisons. No input is directly used in any PHP function, command, or printed. This should make the script relatively secure.
  62.                 = Script has only been tested using PHP 5.2 on Debian 4.0 (Etch). No guarantee of functionality or reliability is given (although I've tested it extensively, and it seems very reliable).
  63.                 \*** Notes & Usage ***/
  64.  
  65.         /*** Init ***/
  66.                 //For security purposes. You never know your own server configuration sometimes ;)
  67.                 //This script (as most others) would become a gaping security hole in your server if register_globals were enabled.
  68.                 ini_set('register_globals', 0);
  69.                 $version = '1.1.4';
  70.         /*** Init ***/
  71.  
  72.         /*** Configuration ***/
  73.                 //External IP of server to test
  74.                 $ip = '123.45.67.89';
  75.                 //If testing a box with a dynamic IP linked to a DNS record, comment the line above and uncomment line below
  76.                 //$ip = gethostbyname('sub.domain.tld');
  77.                
  78.                 //Name of your server
  79.                 $serverName = 'ServerName';
  80.                
  81.                 //E-mail to send Reports to
  82.                 $emailRecipient['report'] = 'reports@domain.tld';
  83.                 //E-mail to send Alerts to
  84.                 $emailRecipient['alert'] = 'alerts@domain.tld';
  85.                 //E-mail to send Reports from
  86.                 $emailSender['report'] = 'mosmc-reports@domain.tld';
  87.                 //E-mail to send Alerts from
  88.                 $emailSender['alert'] = 'mosmc-alerts@domain.tld';
  89.                 //Subject of Report e-mail
  90.                 $emailSubject['report'] = 'ServerName Report';
  91.                 //Subject of Alert e-mail
  92.                 $emailSubject['alert'] = 'ServerName Alert';
  93.                 //E-mail headers for Reports
  94.                 $emailHeaders['report'] = 'From: "MOSMC-PHP" <'.$emailSender['report'].">\n";
  95.                 //E-mail headers for Alerts
  96.                 $emailHeaders['alert'] = 'From: "MOSMC-PHP" <'.$emailSender['alert'].">\n";
  97.                 //Set minimum threshold level to send email. Values: 0 = Warn; 1 = Max/Alert
  98.                 $emailThreshold = 0;
  99.                 //Password for sending e-mails.
  100.                 //Use in order to prevent others from triggering e-mails. Leaving it blank is not recommended.
  101.                 $emailPassword = 'yourpass';
  102.                
  103.                 //What should be grep'd from 'netstat -l'
  104.                 $service[0] = 'www';
  105.                 $service[1] = 'ftp';
  106.                 $service[2] = 'mysql';
  107.                 $service[3] = 'ssh';
  108.                
  109.                 //Human readable service name
  110.                 $serviceName[0] = 'httpd';
  111.                 $serviceName[1] = 'proFTPd';
  112.                 $serviceName[2] = 'MySQL';
  113.                 $serviceName[3] = 'SSH';
  114.                
  115.                 //Type of connection
  116.                 //type 0 == 'Local service', type 1 == 'Public service'
  117.                 $serviceConnType[0] = 1;
  118.                 $serviceConnType[1] = 1;
  119.                 $serviceConnType[2] = 0;
  120.                 $serviceConnType[3] = 1;
  121.                
  122.                 //Port connection is running on
  123.                 $servicePort[0] = 80;
  124.                 $servicePort[1] = 21;
  125.                 $servicePort[2] = 3306;
  126.                 $servicePort[3] = 22;
  127.                
  128.                 //Statistics Warning and Maximum thresholds
  129.                 //Load average
  130.                 $loadWarn = '2.00';
  131.                 $loadMax = '4.00';
  132.                 //RAM
  133.                 $memWarn = 236;
  134.                 $memMax = 256;
  135.                 //Storage (File System)
  136.                 $fsWarn = 8*1024; //8GB (2.0 GB free)
  137.                 $fsMax = 9.5*1024; //9.5GB (0.5 GB free)
  138.                
  139.                 //Human readable website name
  140.                 $siteName[0] = 'Google';
  141.                 $siteName[1] = 'YouTube';
  142.                
  143.                 //URL of websites to check (You may use alternate ports. Ex: http://domain.tld:8080)
  144.                 $siteURL[0] = 'http://www.google.com';
  145.                 $siteURL[1] = 'http://youtube.com';
  146.         /*** Configuration ***/
  147.                
  148.         /*** Init ***/
  149.                 //Count number of services to test
  150.                 $serviceCount = count($serviceName);
  151.                 //Count number of services to test
  152.                 $siteCount = count($siteURL);
  153.                 //Check if running cron job and get configuration options if so
  154.                 if(isset($argv)) {
  155.                         $cron = 1;
  156.                         $type = strtolower($argv[1]);
  157.                         $password = $argv[2];
  158.                 } elseif(isset($_GET['cron'])) {
  159.                         $cron = $_GET['cron'];
  160.                         $type = strtolower($_GET['type']);
  161.                         $password = $_GET['pass'];
  162.                 }
  163.         /*** Init ***/
  164.                
  165.         /*** Populate Data Variables ***/
  166.                 /*** Services Status Checks ***/
  167.                 //Check if ports are open for each service
  168.                 for($i=0;$i<$serviceCount;$i++)
  169.                 {
  170.                         $listenStatus[$i] = -1;
  171.                         $intStatus[$i] = -1;
  172.                         $extStatus[$i] = -1;
  173.  
  174.                         //Check if services are listening
  175.                         $serviceListen = exec("netstat -l -n | grep $servicePort[$i] | wc -l");
  176.                         //If listening, win
  177.                         if($serviceListen > 0) {
  178.                                 $listenStatus[$i] = 0;
  179.                         //If not listening, fail
  180.                         } else {
  181.                                 $listenStatus[$i] = 1;
  182.                         }
  183.                        
  184.                         //Check internal connection if service is listening for connections
  185.                         if($listenStatus[$i] == 0)
  186.                         {
  187.                                 $fp = @fsockopen('localhost', $servicePort[$i], $errno, $errstr, 1);
  188.                                 //If internal connection is accepted, win
  189.                                 if($fp) {
  190.                                         $intStatus[$i] = 0;
  191.                                 //If internal connection is rejected, fail
  192.                                 } else {
  193.                                         $intStatus[$i] = 1;
  194.                                 }
  195.                                 fclose($fp);
  196.                         }
  197.                        
  198.                         //Check external connection if internal connection is available
  199.                         if($intStatus[$i] == 0 && $listenStatus[$i] == 0)
  200.                         {
  201.                                 $fp = @fsockopen($ip, $servicePort[$i], $errno, $errstr, 1);
  202.                                 //If external connection is accepted, and is supposed to be accepted, win
  203.                                 if($fp && $serviceConnType[$i] == 1) {
  204.                                         $extStatus[$i] = 0;
  205.                                 //If external connection is rejected, and not supposed to be accepted, win
  206.                                 } elseif(!$fp && $serviceConnType[$i] == 0) {
  207.                                         $extStatus[$i] = 1;
  208.                                 //If external connection is rejected, and is supposed to be accepted, fail
  209.                                 } elseif(!$fp && $serviceConnType[$i] == 1) {
  210.                                         $extStatus[$i] = 2;
  211.                                 //If external connection is accepted, but not supposed to be, fail + alert
  212.                                 } elseif($fp && $serviceConnType[$i] == 0) {
  213.                                         $extStatus[$i] = 3;
  214.                                 }
  215.                                 fclose($fp);
  216.                         }
  217.                 }
  218.                 /*** Services Status Checks ***/
  219.                
  220.                 /*** Grab System Stats ***/
  221.                 //Grab Uptime + Load Averages
  222.                 $uptimeRaw = @exec('uptime');
  223.                 //Extract load averages
  224.                 preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/", $uptimeRaw, $loads);
  225.                 //Extract uptime
  226.                 $uptime = explode(' up ', $uptimeRaw);
  227.                 $uptime = explode(',', $uptime[1]);
  228.                 $uptime = $uptime[0].', '.$uptime[1];
  229.                
  230.                 //Grab number of users logged in
  231.                 $users = shell_exec('who | wc -l');
  232.                
  233.                 //Grab RAM usage
  234.                 $usedMem = trim(shell_exec('free -m | grep "buffers/cache" | awk \'{print $3}\''));
  235.                 $totalMem = shell_exec('free -m | grep Mem | awk \'{print $2}\'');
  236.                
  237.                 //Grab HDD File System usage
  238.                 $fsFree = round(shell_exec('df | grep sda1 | awk \'{print $4}\'')/1024, 2);
  239.                 $fsUsed = round(shell_exec('df | grep sda1 | awk \'{print $3}\'')/1024, 2);
  240.                 $fsTotal = round(shell_exec('df | grep sda1 | awk \'{print $2}\'')/1024, 2);
  241.                
  242.                 //Grab number of processes
  243.                 $procNumber = shell_exec('ps aux | wc -l');
  244.                 $procNumber = $procNumber--;
  245.                
  246.                 //Grab number of connections
  247.                 $tcpConn = trim(shell_exec('netstat -t | grep tcp | wc -l'));
  248.                 $udpConn = trim(shell_exec('netstat -u | grep udp | wc -l'));
  249.                 /*** Grab System Stats ***/
  250.                
  251.                 /*** Get Sites' Status ***/
  252.                         //Check if sites are returning HTTP status 200 (OK) or hTTP status 302 (Found)
  253.                         for($i=0;$i<$siteCount;$i++)
  254.                         {
  255.                                 $siteStatus[$i] = -1;
  256.  
  257.                                 //Grab HTTP Status
  258.                                 $rawResponse = @shell_exec("curl $siteURL[$i] -I -s -A 'MOSMC-PHP' | grep HTTP | awk '{print $2 $3}'");
  259.                                 $response[$i][0] = substr($rawResponse, 0, 3);
  260.                                 $response[$i][1] = trim(substr($rawResponse, 3));
  261.                                 //If headers return HTTP status 200 (OK) or hTTP status 302 (Found), win
  262.                                 if($response[$i][0] == 200 || $response[$i][0] == 302) {
  263.                                         $siteStatus[$i] = 0;
  264.                                 //If headers return HTTP status other than 200 (OK) or hTTP status 302 (Found), fail
  265.                                 } else {
  266.                                         $siteStatus[$i] = 1;
  267.                                 }
  268.                         }
  269.                 /*** Get Sites' Status ***/    
  270.                
  271.                 /*** Testing Values ***/
  272.                 //Various service status scenarios
  273.                 //1-Service is not listening, probably because it's down
  274.                 /*$listenStatus[0] = 1;
  275.                 $intStatus[0] = -1;
  276.                 $extStatus[0] = -1;*/
  277.                 //2-Service is listening, but rejecting all connections
  278.                 /*$listenStatus[0] = 0;
  279.                 $intStatus[0] = 1;
  280.                 $extStatus[0] = -1;*/
  281.                 //3-Service is listening, and rejecting external connections, but supposed to accept them
  282.                 /*$listenStatus[0] = 0;
  283.                 $intStatus[0] = 0;
  284.                 $extStatus[0] = 2;*/
  285.                 //4-Service is listening, and accepting external connections, but supposed to reject them
  286.                 /*$listenStatus[0] = 0;
  287.                 $intStatus[0] = 0;
  288.                 $extStatus[0] = 3;*/
  289.                 //$loads[1] = '4.00';
  290.                 //$usedMem = 350;
  291.                 //$fsUsed = 10*1024;
  292.                 /*$siteStatus[0] = 1;
  293.                 $response[0][0] = 403;
  294.                 $response[0][1] = 'Forbidden';*/
  295.                 /*** Testing Values ***/
  296.         /*** Populate Data Variables ***/
  297.        
  298.         /*** Print All Data ***/
  299.         //If this is a cron job, don't print data
  300.         if(!isset($cron))
  301.         {
  302. ?>
  303. <html>
  304.         <head>
  305.                 <title><?php echo $serverName; ?> Monitoring Console</title>
  306.                 <style type="text/css">
  307.                         body {
  308.                                 text-align:center;
  309.                                 background-color:#f4ead3;
  310.                                 color:#746f63;
  311.                         }
  312.                         h1 {
  313.                                 color:#838078;
  314.                         }
  315.                         a:link, a:active, a:visited {
  316.                                 color:#595751;
  317.                                 text-decoration:none;
  318.                         }
  319.                         a:hover {
  320.                                 color:#6e6c65;
  321.                         }
  322.                         table{
  323.                                 font-size:0.8em;
  324.                                 border: 1.5pt dotted #dacfb6;
  325.                         }
  326.                         .up {
  327.                                 background-color:#99cc66;
  328.                         }
  329.                         .down {
  330.                                 background-color:#cc3333;
  331.                                 color:#eee;
  332.                         }
  333.                         .warn {
  334.                                 background-color:#fcc15a;
  335.                         }
  336.                 </style>
  337.         </head>
  338.         <body>
  339.                 <h1><?php echo $serverName; ?> Monitoring Console</h1>
  340.                 <h2><?php echo $ip; ?></h2>            
  341.  
  342.                 <table width="600px" border="0" cellspacing="1" cellpadding="5" align="center">
  343.                         <tr style="background-color:#e9ddc2">
  344.                                 <td style="min-width:75px;background-color:#dacfb6">Service</td>
  345.                                 <td>Port</td>
  346.                                 <td>Local Connection</td>
  347.                                 <td>External Connection</td>
  348.                         </tr>
  349.                
  350.         <?php /**Printut Services Status ***/
  351.                         for($i=0;$i<$serviceCount;$i++)
  352.                         {
  353.                 ?>
  354.                 <tr style="background-color:#dacfb6">
  355.                         <td style="background-color:#e9ddc2">
  356.                                 <?php echo $serviceName[$i]; ?>
  357.                         </td>
  358.                         <?php
  359.                                 switch ($listenStatus[$i]) {
  360.                                 case 0:
  361.                                         echo '<td class="up">';
  362.                                     echo 'Listening ('.$servicePort[$i].')';
  363.                                     break;
  364.                                 case 1:
  365.                                         echo '<td class="down">';
  366.                                     echo '<b>Not listening</b> ('.$servicePort[$i].')';
  367.                                     break;
  368.                                 default:
  369.                                         echo '<td class="warn">';
  370.                                         echo 'N/A';
  371.                                         break;
  372.                                 }
  373.                         ?>
  374.                                 </td>
  375.                         <?php
  376.                                 switch ($intStatus[$i]) {
  377.                                 case 0:
  378.                                         echo '<td class="up">';
  379.                                     echo 'Accepting';
  380.                                     break;
  381.                                 case 1:
  382.                                         echo '<td class="down">';
  383.                                     echo '<b>Rejecting</b>';
  384.                                     break;
  385.                                 default:
  386.                                         echo '<td class="warn">';
  387.                                         echo 'N/A';
  388.                                         break;
  389.                                 }
  390.                         ?>
  391.                                 </td>
  392.                         <?php
  393.                                 switch ($extStatus[$i]) {
  394.                                 case 0:
  395.                                         echo '<td class="up">';
  396.                                     echo 'Accepting as configured';
  397.                                     break;
  398.                                 case 1:
  399.                                         echo '<td class="up">';
  400.                                     echo 'Rejecting as configured';
  401.                                     break;
  402.                                 case 2:
  403.                                         echo '<td class="down">';
  404.                                         echo '<b>Rejecting; Misconfigured</b>';
  405.                                         break;
  406.                                 case 3:
  407.                                         echo '<td class="down">';
  408.                                         echo '<b>Accepting; Misconfigured</b>';
  409.                                         break;
  410.                                 default:
  411.                                         echo '<td class="warn">';
  412.                                         echo 'N/A';
  413.                                         break;
  414.                                 }
  415.                         ?>
  416.                                 </td>
  417.                         </tr>
  418.                 <?php
  419.                         }
  420.                 ?>
  421.                 </table>
  422.         <?php /*** Print Services Status ***/ ?>
  423.                
  424.                 <br />
  425.                
  426.         <?php /*** Print System Stats ***/ ?>
  427.                 <table width="600px" border="0" cellspacing="1" cellpadding="5" bordercolor="#333333" align="center">
  428.                         <tr style="background-color:#e9ddc2">
  429.                                 <td style="width:125px;background-color:#dacfb6">Stat Type</td>
  430.                                 <td style="width:150px;">Current Value</td>
  431.                                 <td>Warn Threshold</td>
  432.                                 <td>Alert Threshold</td>
  433.                         </tr>
  434.                        
  435.                 <? /*** Print Load Averages ***/ ?>
  436.                         <tr style="background-color:#dacfb6">
  437.                                 <td style="background-color:#e9ddc2">
  438.                                         Load Averages
  439.                                 </td>
  440.                         <?php
  441.                                 if($loadWarn > $loads[1] && $loads[1] < $loadMax) {
  442.                                         echo '<td class="up">';
  443.                                         echo $loads[1].' '.$loads[2].' '.$loads[3];
  444.                                 } elseif($loadWarn <= $loads[1] && $loads[1] < $loadMax) {
  445.                                         echo '<td class="warn">';
  446.                                         echo '<b>'.$loads[1].' '.$loads[2].' '.$loads[3].'</b>';
  447.                                 } else {
  448.                                         echo '<td class="down">';
  449.                                         echo '<b>'.$loads[1].' '.$loads[2].' '.$loads[3].'</b>';
  450.                                 }
  451.                         ?>
  452.                                 </td>
  453.                                 <td>
  454.                                         <?php echo $loadWarn; ?> (1 min avg)
  455.                                 </td>
  456.                                 <td>
  457.                                         <?php echo $loadMax; ?> (1 min avg)
  458.                                 </td>
  459.                         </tr>
  460.                 <? /*** Print Load Averages ***/ ?>
  461.                
  462.                 <? /*** Print RAM Data ***/ ?>
  463.                         <tr style="background-color:#dacfb6">
  464.                                 <td style="background-color:#e9ddc2">
  465.                                         Used RAM
  466.                                 </td>
  467.                         <?php
  468.                                 if($memWarn > $usedMem && $usedMem < $memMax) {
  469.                                         echo '<td class="up">';
  470.                                         echo $usedMem;
  471.                                 } elseif($memWarn <= $usedMem && $usedMem < $memMax) {
  472.                                         echo '<td class="warn">';
  473.                                         echo '<b>'.$usedMem.'</b>';
  474.                                 } else {
  475.                                         echo '<td class="down">';
  476.                                         echo '<b>'.$usedMem.'</b>';
  477.                                 }
  478.                         ?> MB
  479.                                 </td>
  480.                                 <td>
  481.                                         <?php echo $memWarn; ?> MB
  482.                                 </td>
  483.                                 <td>
  484.                                         <?php echo ($memMax >= 1024) ? ($memMax/1024).' GB' : $memMax.' MB'; ?>
  485.                                 </td>
  486.                         </tr>
  487.                 <? /*** Print RAM Data ***/ ?>
  488.                
  489.                 <? /*** Print HDD Data ***/ ?>
  490.                         <tr style="background-color:#dacfb6">
  491.                                 <td style="background-color:#e9ddc2">
  492.                                         Used Storage
  493.                                 </td>
  494.                         <?php
  495.                                 if($fsWarn > $fsUsed  && $fsUsed < $fsMax) {
  496.                                         echo '<td class="up">';
  497.                                         echo ($fsUsed >= 1024) ? round($fsUsed/1024, 2).' GB' : $fsUsed.' MB';
  498.                                 } elseif($fsWarn <= $fsUsed && $fsUsed < $fsMax) {
  499.                                         echo '<td class="warn"><b>';
  500.                                         echo ($fsUsed >= 1024) ? round($fsUsed/1024, 2).' GB' : $fsUsed.' MB';
  501.                                         echo '</b>';
  502.                                 } else {
  503.                                         echo '<td class="down"><b>';
  504.                                         echo ($fsUsed >= 1024) ? round($fsUsed/1024, 2).' GB' : $fsUsed.' MB';
  505.                                         echo '</b>';
  506.                                 }
  507.                         ?>
  508.                                 </td>
  509.                                 <td>
  510.                                         <?php echo ($fsWarn >= 1024) ? round($fsWarn/1024, 2).' GB' : $fsWarn.' MB'; ?>
  511.                                 </td>
  512.                                 <td>
  513.                                         <?php echo ($fsMax >= 1024) ? round($fsMax/1024, 2).' GB' : $fsMax.' MB'; ?>
  514.                                 </td>
  515.                         </tr>
  516.                 <? /*** Print HDD Data ***/ ?>
  517.                 </table>
  518.         <?php /*** Print System Stats ***/ ?>
  519.  
  520.                 <br />
  521.  
  522.         <?php /*** Print System Values ***/ ?>
  523.                
  524.                 <table width="600px" border="0" cellspacing="1" cellpadding="5" bordercolor="#333333" align="center">
  525.                         <tr style="background-color:#e9ddc2">
  526.                                 <td style="width:150px;background-color:#dacfb6">Stat Type</td>
  527.                                 <td style="width:150px;">Current Value</td>
  528.                                 <td style="width:150px;background-color:#dacfb6">Stat Type</td>
  529.                                 <td style="width:150px;">Current Value</td>
  530.                         </tr>
  531.  
  532.                 <? /*** Print Number of Processes ***/ ?>
  533.                         <tr style="background-color:#dacfb6">
  534.                                 <td style="background-color:#e9ddc2">
  535.                                         Processes
  536.                                 </td>
  537.                                 <td>
  538.                                         <?php echo $procNumber; ?>
  539.                                 </td>
  540.                 <? /*** Print Number of Processes ***/ ?>
  541.                
  542.                 <? /*** Print Number of Users Logged In ***/ ?>
  543.                                 <td style="background-color:#e9ddc2">
  544.                                         Users Logged In
  545.                                 </td>
  546.                                 <td>
  547.                                         <?php echo $users; ?>
  548.                                 </td>
  549.                         </tr>
  550.                 <? /*** Print Number of TCP and UDP Connections ***/ ?>
  551.                
  552.                 <? /*** Print Uptime Data ***/ ?>
  553.                         <tr style="background-color:#dacfb6">
  554.                                 <td style="background-color:#e9ddc2">
  555.                                         Uptime
  556.                                 </td>
  557.                                 <td>
  558.                                         <?php echo $uptime; ?>
  559.                                 </td>
  560.                 <? /*** Print Uptime Data ***/ ?>
  561.                
  562.                 <? /*** Print Number of TCP and UDP Connections ***/ ?>
  563.                                 <td style="background-color:#e9ddc2">
  564.                                         TCP/UDP Conns
  565.                                 </td>
  566.                                 <td>
  567.                                         <?php echo $tcpConn.'/'.$udpConn; ?>
  568.                                 </td>
  569.                         </tr>
  570.                 <? /*** Print Number of TCP and UDP Connections ***/ ?>
  571.                 </table>
  572.         <?php /*** Print System Values ***/ ?>
  573.                
  574.                 <br />
  575.                
  576.                 <table width="600px" border="0" cellspacing="1" cellpadding="5" bordercolor="#333333" align="center">
  577.                         <tr style="background-color:#e9ddc2">
  578.                                 <td style="width:125px;background-color:#dacfb6">Site</td>
  579.                                 <td style="width:175px;">HTTP Status</td>
  580.                                 <td style="width:300px;">URL</td>
  581.                         </tr>
  582.                
  583.         <?php /*** Print Sites' Status ***/
  584.                         for($i=0;$i<$siteCount;$i++)
  585.                         {
  586.         ?>
  587.                         <tr style="background-color:#dacfb6">
  588.                                 <td style="background-color:#e9ddc2">
  589.                                         <?php echo $siteName[$i]; ?>
  590.                                 </td>
  591.                         <?php
  592.                                 switch ($siteStatus[$i]) {
  593.                                 case 0:
  594.                                         echo '<td class="up">';
  595.                                     echo $response[$i][0].' ('.$response[$i][1].')';
  596.                                     break;
  597.                                 case 1:
  598.                                         echo '<td class="down">';
  599.                                     echo $response[$i][0].' ('.$response[$i][1].')';
  600.                                     break;
  601.                                 default:
  602.                                         echo '<td class="warn">';
  603.                                         echo 'N/A';
  604.                                         break;
  605.                                 }
  606.                         ?>
  607.                                 </td>
  608.                                 <td>
  609.                                         <?php
  610.                                         //If site is not returning HTTP 200 (OK) or HTTP status 302 (Found), link. Otherwise, just show URL
  611.                                         echo ($siteStatus[$i]) ? '<a href="'.$siteURL[$i].'">'.$siteURL[$i].'</a>' : $siteURL[$i];
  612.                                         ?>
  613.                                 </td>
  614.                         </tr>
  615.                 <?php
  616.                         }
  617.                 ?>
  618.                 </table>
  619.         <?php /*** Print Sites' Status ***/
  620.        
  621.         /*** Print All Data ***/ ?>
  622.                
  623.                 <div style="background-color:#e9ddc2;padding:5px;width:250px;margin:auto;margin-top:20px;font-size:0.7em;border: 1pt dotted #dacfb6;">
  624.                         <a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/us/"><img alt="Creative Commons License" style="border-width:0;margin:2px;" src="http://i.creativecommons.org/l/by-nc/3.0/us/80x15.png"/></a>
  625.                         <br />
  626.                         MOSMC-PHP v<?php echo $version; ?> | Alesandro Ortiz<br />
  627.                         The source of this script is available at <a href="http://thephpjedi.com">ThePHPJedi.com</a>
  628.                 </div>
  629.                
  630.         </body>
  631. </html>
  632. <?
  633.         //If this is a cron job, send emails
  634.         } else {
  635.                 //If you want to send a full report...
  636.                 if($type == 'report')
  637.                 {
  638.                         ob_start();
  639.                         echo '=Services='."\n";
  640.                         for($i=0;$i<$serviceCount;$i++)
  641.                         {
  642.                                 echo $serviceName[$i];
  643.                                
  644.                                 echo ' | Port: ';
  645.                                 switch ($listenStatus[$i]) {
  646.                                 case 0:
  647.                                         echo 'Up, ';
  648.                                     echo 'Listening ('.$servicePort[$i].')';
  649.                                     break;
  650.                                 case 1:
  651.                                         echo 'Down, ';
  652.                                     echo 'Not listening ('.$servicePort[$i].')';
  653.                                     break;
  654.                                 default:
  655.                                         echo 'Warning, ';
  656.                                         echo 'N/A';
  657.                                         break;
  658.                                 }
  659.                                
  660.                                 echo "\n".'Local Connection: ';
  661.                                 switch ($intStatus[$i]) {
  662.                                 case 0:
  663.                                         echo 'Up, ';
  664.                                     echo 'Accepting';
  665.                                     break;
  666.                                 case 1:
  667.                                         echo 'Down, ';
  668.                                     echo 'Rejecting';
  669.                                     break;
  670.                                 default:
  671.                                         echo 'Warning, ';
  672.                                         echo 'N/A';
  673.                                         break;
  674.                                 }
  675.                                
  676.                                 echo "\n".'External Connection: ';
  677.                                 switch ($extStatus[$i]) {
  678.                                 case 0:
  679.                                         echo 'Up, ';
  680.                                     echo 'Accepting as configured';
  681.                                     break;
  682.                                 case 1:
  683.                                         echo 'Up, ';
  684.                                     echo 'Rejecting as configured';
  685.                                     break;
  686.                                 case 2:
  687.                                         echo 'Down, ';
  688.                                         echo 'Rejecting; Misconfigured';
  689.                                         break;
  690.                                 case 3:
  691.                                         echo 'Down, ';
  692.                                         echo 'Accepting; Misconfigured';
  693.                                         break;
  694.                                 default:
  695.                                         echo 'Warning, ';
  696.                                         echo 'N/A';
  697.                                         break;
  698.                                 }
  699.                                 echo "\n\n";
  700.                         }
  701.                
  702.                         echo '=System Stats=';
  703.                        
  704.                         echo "\n".'Loads: ';
  705.                         if($loadWarn > $loads[1] && $loads[1] < $loadMax) {
  706.                                 echo 'Up, ';
  707.                                 echo $loads[1].' '.$loads[2].' '.$loads[3];
  708.                         } elseif($loadWarn <= $loads[1] && $loads[1] < $loadMax) {
  709.                                 echo 'Warning, ';
  710.                                 echo $loads[1].' '.$loads[2].' '.$loads[3];
  711.                         } else {
  712.                                 echo 'Down, ';
  713.                                 echo $loads[1].' '.$loads[2].' '.$loads[3];
  714.                         }
  715.                        
  716.                         echo "\n".'RAM: ';
  717.                         if($memWarn > $usedMem && $usedMem < $memMax) {
  718.                                 echo 'Up, ';
  719.                                 echo $usedMem.' MB';
  720.                         } elseif($memWarn <= $usedMem && $usedMem < $memMax) {
  721.                                 echo 'Warning, ';
  722.                                 echo $usedMem.' MB';
  723.                         } else {
  724.                                 echo 'Down, ';
  725.                                 echo $usedMem.' MB';
  726.                         }
  727.                        
  728.                         echo "\n".'Storage: ';
  729.                         if($fsWarn > $fsUsed  && $fsUsed < $fsMax) {
  730.                                 echo 'Up, ';
  731.                                 echo ($fsUsed >= 1024) ? round($fsUsed/1024, 2).' GB' : $fsUsed.' MB';
  732.                         } elseif($fsWarn <= $fsUsed && $fsUsed < $fsMax) {
  733.                                 echo 'Warning, ';
  734.                                 echo ($fsUsed >= 1024) ? round($fsUsed/1024, 2).' GB' : $fsUsed.' MB';
  735.                         } else {
  736.                                 echo 'Down, ';
  737.                                 echo ($fsUsed >= 1024) ? round($fsUsed/1024, 2).' GB' : $fsUsed.' MB';
  738.                         }
  739.  
  740.                         echo "\n".'Processes: '.$procNumber;
  741.                         echo "\n".'Uptime: '.$uptime;
  742.                         echo "\n".'Users Logged In: '.$users;
  743.                         echo "\n".'TCP Connections: '.$tcpConn;
  744.                         echo "\n".'UDP Connections: '.$udpConn;
  745.                        
  746.                         echo "\n".'=Sites=';
  747.                         for($i=0;$i<$siteCount;$i++)
  748.                         {
  749.                                 echo "\n".$siteName[$i].': ';
  750.                                
  751.                                 switch ($siteStatus[$i]) {
  752.                                 case 0:
  753.                                         echo 'Up, ';
  754.                                         echo $response[$i][0].' ('.$response[$i][1].')';
  755.                                     break;
  756.                                 case 1:
  757.                                         echo 'Down, ';
  758.                                     echo $response[$i][0].' ('.$response[$i][1].')';
  759.                                     break;
  760.                                 default:
  761.                                         echo 'Warning, ';
  762.                                         echo 'N/A';
  763.                                         break;
  764.                                 }
  765.                         }
  766.                         $content = ob_get_clean();
  767.                
  768.                 //Send email with report
  769.                 if($content) {
  770.                         //Check password
  771.                         if($emailPassword == $password)
  772.                         {
  773.                                 mail($emailRecipient['report'],$emailSubject['report'],$content,$emailHeaders['report']);
  774.                         }
  775.                 }
  776.                        
  777.                 //If you want to send a report only on errors and warnings...
  778.                 } elseif($type == 'failure') {
  779.                         ob_start();
  780.                         for($i=0;$i<$serviceCount;$i++)
  781.                         {
  782.                                 echo ($listenStatus[$i] > 0 || $intStatus[$i] > 0 || $extStatus[$i] > 1) ? $serviceName[$i] : null;
  783.                                
  784.                                 echo ($listenStatus[$i] > 0) ? '|Port:' : null;
  785.                                 switch ($listenStatus[$i]) {
  786.                                 case 0:
  787.                                     break;
  788.                                 case 1:
  789.                                         echo 'Down,';
  790.                                     echo 'Not listening ('.$servicePort[$i].')';
  791.                                     break;
  792.                                 default:
  793.                                         echo 'Warning,';
  794.                                         echo 'N/A';
  795.                                         break;
  796.                                 }
  797.                                
  798.                                 echo ($intStatus[$i] > 0) ? '|Local Conn:' : null;
  799.                                 switch ($intStatus[$i]) {
  800.                                 case 0:
  801.                                     break;
  802.                                 case 1:
  803.                                         echo 'Down,';
  804.                                     echo 'Rejecting';
  805.                                     break;
  806.                                 default:
  807.                                         echo 'Warning,';
  808.                                         echo 'N/A';
  809.                                         break;
  810.                                 }
  811.                                
  812.                                 echo ($extStatus[$i] > 1) ? '|Ext Conn:' : null;
  813.                                 switch ($extStatus[$i]) {
  814.                                 case 0:
  815.                                     break;
  816.                                 case 1:
  817.                                     break;
  818.                                 case 2:
  819.                                         echo 'Down,';
  820.                                         echo 'Rejecting; Misconfigured';
  821.                                         echo "\n";
  822.                                         break;
  823.                                 case 3:
  824.                                         echo 'Down,';
  825.                                         echo 'Accepting; Misconfigured';
  826.                                         echo "\n";
  827.                                         break;
  828.                                 default:
  829.                                         echo 'Warning,';
  830.                                         echo 'N/A';
  831.                                         echo "\n";
  832.                                         break;
  833.                                 }
  834.                                
  835.                         }
  836.                        
  837.                         echo ($loadWarn < $loads[1]) ? 'Loads:' : null;
  838.                         if($loadWarn < $loads[1] && $loads[1] < $loadMax) {
  839.                                 echo 'Warning,';
  840.                                 echo $loads[1].' '.$loads[2].' '.$loads[3];
  841.                         } elseif($loadMax <= $loads[1]) {
  842.                                 echo 'Down,';
  843.                                 echo $loads[1].' '.$loads[2].' '.$loads[3];
  844.                         }
  845.                        
  846.                         echo ($memWarn < $usedMem) ? '|RAM:' : null;
  847.                         if($memWarn < $usedMem && $usedMem < $memMax) {
  848.                                 echo 'Warning,';
  849.                                 echo $usedMem;
  850.                         } elseif($memMax <= $usedMem) {
  851.                                 echo 'Down,';
  852.                                 echo $usedMem;
  853.                         }
  854.                         echo ($memWarn < $usedMem) ? 'MB' : null;
  855.                        
  856.                         echo ($fsWarn < $fsUsed) ? '|FS:' : null;
  857.                         if($fsWarn < $fsUsed && $fsUsed < $fsMax) {
  858.                                 echo 'Warning,';
  859.                                 echo ($fsUsed >= 1024) ? round($fsUsed/1024, 2).'GB' : $fsUsed.'MB';
  860.                         } elseif($fsMax <= $fsUsed) {
  861.                                 echo 'Down,';
  862.                                 echo ($fsUsed >= 1024) ? round($fsUsed/1024, 2).'GB' : $fsUsed.'MB';
  863.                         }
  864.                        
  865.                         for($i=0;$i<$siteCount;$i++)
  866.                         {
  867.                                 echo ($siteStatus[$i] > 0) ? '|'.$siteName[$i].':' : null;
  868.                                
  869.                                 switch ($siteStatus[$i]) {
  870.                                 case 0:
  871.                                     break;
  872.                                 case 1:
  873.                                         echo 'Down,';
  874.                                     echo $response[$i][0].'('.$response[$i][1].')';
  875.                                     break;
  876.                                 default:
  877.                                         echo 'Warning,';
  878.                                         echo 'N/A';
  879.                                         break;
  880.                                 }
  881.                         }
  882.                         $content = ob_get_clean();
  883.                        
  884.                         //Send email if any failure is detected
  885.                         if($content) {
  886.                                 //Check password
  887.                                 if($emailPassword == $password)
  888.                                 {
  889.                                         $searchType = strtolower($content);
  890.                                         //If threshold has been set to 'Alert/Max', send only when down
  891.                                         if($emailThreshold) {
  892.                                                 if(fnmatch('*down*', $searchType))
  893.                                                 mail($emailRecipient['alert'],$emailSubject['alert'],$content,$emailHeaders['alert']);
  894.                                         //If threshold has been set to 'Warn', send on warn or down
  895.                                         } else {
  896.                                                 if(fnmatch('*warn*', $searchType) || fnmatch('*down*', $searchType))
  897.                                                 mail($emailRecipient['alert'],$emailSubject['alert'],$content,$emailHeaders['alert']);
  898.                                         }
  899.                                 }
  900.                         }
  901.                        
  902.                 }
  903.         }
  904. ?>
View raw paste Parent paste by: The_PHP_Jedi - Child paste by: The_PHP_Jedi Reply