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