Author: The_PHP_Jedi Language: php
Description: MOSMC-PHP v1.1.5 Timestamp: 2008-07-22 04:25:49 -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.5 ==
  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: Tuesday, July 22nd 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.                 == 1.1.5 ==
  28.                 = Fixed PHP error. Might be major bug if run in cron.
  29.                 \*** Changelog ***/
  30.  
  31.                 /*** To-Do ***\
  32.                 = Nothing right now, but I'm sure I can find new features I can add very soon ;)
  33.                 \*** To-Do ***/
  34.                
  35.                 /*** Comments ***\
  36.                 = MOSMC is pronounced as "moss-mick"
  37.                 = 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. :)
  38.                 \*** Comments ***/
  39.                
  40.                 /*** Bugs ***\
  41.                 = 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.
  42.                 \*** Bugs ***/
  43.                
  44.                 /*** Notes & Usage ***\
  45.                 == General ==
  46.                 = Status codes in ascending order of severity. Higher number, higher severity.
  47.                 = Site monitor links to URL only in case of failure. Otherwise, it doesn't link to the URL being tested.
  48.                 =
  49.                 == Local Cron Jobs ==
  50.                 = Use command 'php /path/to/monitor.php type password'
  51.                 = Example: 'php /path/to/monitor.php failure yourpass' if you wanted to send emails only on failure
  52.                 =
  53.                 == Remote Cron Jobs or On-Demand Reporting ==
  54.                 = 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)
  55.                 = Use monitor.php?cron=1&type=report&pass=yourpass if you want to send email with full report regardless of status
  56.                 = 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
  57.                 =
  58.                 == How MOSMC-PHP Tests ==
  59.                 = Services: Checks first if services are listening, then checks if it accepts internal connections, then checks if it accepts external connections.
  60.                 = Sites: Checks HTTP header response.
  61.                 =
  62.                 == Disclaimers ==
  63.                 = 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.
  64.                 = 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).
  65.                 \*** Notes & Usage ***/
  66.  
  67.         /*** Init ***/
  68.                 //For security purposes. You never know your own server configuration sometimes ;)
  69.                 //This script (as most others) would become a gaping security hole in your server if register_globals were enabled.
  70.                 ini_set('register_globals', 0);
  71.                 $version = '1.1.5';
  72.         /*** Init ***/
  73.  
  74.         /*** Configuration ***/
  75.                 //External IP of server to test
  76.                 $ip = '123.45.67.89';
  77.                 //If testing a box with a dynamic IP linked to a DNS record, comment the line above and uncomment line below
  78.                 //$ip = gethostbyname('sub.domain.tld');
  79.                
  80.                 //Name of your server
  81.                 $serverName = 'ServerName';
  82.                
  83.                 //E-mail to send Reports to
  84.                 $emailRecipient['report'] = 'reports@domain.tld';
  85.                 //E-mail to send Alerts to
  86.                 $emailRecipient['alert'] = 'alerts@domain.tld';
  87.                 //E-mail to send Reports from
  88.                 $emailSender['report'] = 'mosmc-reports@domain.tld';
  89.                 //E-mail to send Alerts from
  90.                 $emailSender['alert'] = 'mosmc-alerts@domain.tld';
  91.                 //Subject of Report e-mail
  92.                 $emailSubject['report'] = 'ServerName Report';
  93.                 //Subject of Alert e-mail
  94.                 $emailSubject['alert'] = 'ServerName Alert';
  95.                 //E-mail headers for Reports
  96.                 $emailHeaders['report'] = 'From: "MOSMC-PHP" <'.$emailSender['report'].">\n";
  97.                 //E-mail headers for Alerts
  98.                 $emailHeaders['alert'] = 'From: "MOSMC-PHP" <'.$emailSender['alert'].">\n";
  99.                 //Set minimum threshold level to send email. Values: 0 = Warn; 1 = Max/Alert
  100.                 $emailThreshold = 0;
  101.                 //Password for sending e-mails.
  102.                 //Use in order to prevent others from triggering e-mails. Leaving it blank is not recommended.
  103.                 $emailPassword = 'yourpass';
  104.                
  105.                 //What should be grep'd from 'netstat -l'
  106.                 $service[0] = 'www';
  107.                 $service[1] = 'ftp';
  108.                 $service[2] = 'mysql';
  109.                 $service[3] = 'ssh';
  110.                
  111.                 //Human readable service name
  112.                 $serviceName[0] = 'httpd';
  113.                 $serviceName[1] = 'proFTPd';
  114.                 $serviceName[2] = 'MySQL';
  115.                 $serviceName[3] = 'SSH';
  116.                
  117.                 //Type of connection
  118.                 //type 0 == 'Local service', type 1 == 'Public service'
  119.                 $serviceConnType[0] = 1;
  120.                 $serviceConnType[1] = 1;
  121.                 $serviceConnType[2] = 0;
  122.                 $serviceConnType[3] = 1;
  123.                
  124.                 //Port connection is running on
  125.                 $servicePort[0] = 80;
  126.                 $servicePort[1] = 21;
  127.                 $servicePort[2] = 3306;
  128.                 $servicePort[3] = 22;
  129.                
  130.                 //Statistics Warning and Maximum thresholds
  131.                 //Load average
  132.                 $loadWarn = '2.00';
  133.                 $loadMax = '4.00';
  134.                 //RAM
  135.                 $memWarn = 236;
  136.                 $memMax = 256;
  137.                 //Storage (File System)
  138.                 $fsWarn = 8*1024; //8GB (2.0 GB free)
  139.                 $fsMax = 9.5*1024; //9.5GB (0.5 GB free)
  140.                
  141.                 //Human readable website name
  142.                 $siteName[0] = 'Google';
  143.                 $siteName[1] = 'YouTube';
  144.                
  145.                 //URL of websites to check (You may use alternate ports. Ex: http://domain.tld:8080)
  146.                 $siteURL[0] = 'http://www.google.com';
  147.                 $siteURL[1] = 'http://youtube.com';
  148.         /*** Configuration ***/
  149.                
  150.         /*** Init ***/
  151.                 //Count number of services to test
  152.                 $serviceCount = count($serviceName);
  153.                 //Count number of services to test
  154.                 $siteCount = count($siteURL);
  155.                 //Check if running cron job and get configuration options if so
  156.                 if(isset($argv)) {
  157.                         $cron = 1;
  158.                         $type = strtolower($argv[1]);
  159.                         $password = $argv[2];
  160.                 } elseif(isset($_GET['cron'])) {
  161.                         $cron = $_GET['cron'];
  162.                         $type = strtolower($_GET['type']);
  163.                         $password = $_GET['pass'];
  164.                 }
  165.         /*** Init ***/
  166.                
  167.         /*** Populate Data Variables ***/
  168.                 /*** Services Status Checks ***/
  169.                 //Check if ports are open for each service
  170.                 for($i=0;$i<$serviceCount;$i++)
  171.                 {
  172.                         $listenStatus[$i] = -1;
  173.                         $intStatus[$i] = -1;
  174.                         $extStatus[$i] = -1;
  175.  
  176.                         //Check if services are listening
  177.                         $serviceListen = exec("netstat -l -n | grep $servicePort[$i] | wc -l");
  178.                         //If listening, win
  179.                         if($serviceListen > 0) {
  180.                                 $listenStatus[$i] = 0;
  181.                         //If not listening, fail
  182.                         } else {
  183.                                 $listenStatus[$i] = 1;
  184.                         }
  185.                        
  186.                         //Check internal connection if service is listening for connections
  187.                         if($listenStatus[$i] == 0)
  188.                         {
  189.                                 $fp = @fsockopen('localhost', $servicePort[$i], $errno, $errstr, 1);
  190.                                 //If internal connection is accepted, win
  191.                                 if($fp) {
  192.                                         $intStatus[$i] = 0;
  193.                                 //If internal connection is rejected, fail
  194.                                 } else {
  195.                                         $intStatus[$i] = 1;
  196.                                 }
  197.                         }
  198.                        
  199.                         //Check external connection if internal connection is available
  200.                         if($intStatus[$i] == 0 && $listenStatus[$i] == 0)
  201.                         {
  202.                                 $fp = @fsockopen($ip, $servicePort[$i], $errno, $errstr, 1);
  203.                                 //If external connection is accepted, and is supposed to be accepted, win
  204.                                 if($fp && $serviceConnType[$i] == 1) {
  205.                                         $extStatus[$i] = 0;
  206.                                 //If external connection is rejected, and not supposed to be accepted, win
  207.                                 } elseif(!$fp && $serviceConnType[$i] == 0) {
  208.                                         $extStatus[$i] = 1;
  209.                                 //If external connection is rejected, and is supposed to be accepted, fail
  210.                                 } elseif(!$fp && $serviceConnType[$i] == 1) {
  211.                                         $extStatus[$i] = 2;
  212.                                 //If external connection is accepted, but not supposed to be, fail + alert
  213.                                 } elseif($fp && $serviceConnType[$i] == 0) {
  214.                                         $extStatus[$i] = 3;
  215.                                 }
  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