Author: The_PHP_Jedi Language: php
Description: MOSMC v2.0.0 Timestamp: 2009-01-11 21:36:30 -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: 2.0.0 ==
  6. == Description: Multi-Output Server Monitoring Console (MOSMC) ==
  7. == Author: Alesandro Ortiz - The_PHP_Jedi <thephpjedi@thephpjedi.com> ==
  8. == Created: Wednesday, July 2nd 2008 ==
  9. == Modified: Sunday, January 11th 2009 ==
  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.                 =
  19.                 == 1.1.1 & 1.1.2 ==
  20.                 = Display fix: Fixed minor bugs regarding display of information.
  21.                 =
  22.                 == 1.1.3 ==
  23.                 = New Feature: Different e-mail subject, sender, and recipient for Reports and Alerts.
  24.                 = Bug fix: Site monitor would show incorrect information in most cases where more than one site was monitored.
  25.                 = Display fix: E-mail report would show 'MB' in RAM information in a new line.
  26.                 =
  27.                 == 1.1.4 ==
  28.                 = New Feature: Added User Agent "MOSMC-PHP" when checking sites.
  29.                 = Bux fix: Minor. Related to services checking. Did not affect functionality.
  30.                 =
  31.                 == 1.1.5 (07/22/2008) ==
  32.                 = Bug fix: PHP error. Might be major bug if running through cron.
  33.                 =
  34.                 == 2.0.0 (01/11/2009) ==
  35.                 = Update: Re-wrote MOSMC in OOP.
  36.                 = Update: Re-worded some of the comments to make clearer what MOSMC is doing.
  37.                 = Update: Documentation is now more specific, and has been re-worded to make it clearer.
  38.                 = New Feature: Added configuration option: hard drive partition to use for hard drive usage stats.
  39.                 = New Feature: Integrated HTTP authentication.
  40.                 = New Feature: Notification when new MOSMC release is available (optional; See configuration options).
  41.                 = New Feature: Test variables simulating various scenarios have been included. Search for "Testing Variables" in the source. Note: Email and Browser output testing variables are separated.
  42.                 = Bug fix: PHP mail() would not set the Return-Path header properly, often required for SPF. Now fixed, using sender email as return path address.
  43.                 = Bug fix: Service listening check now returns proper results. In limited cases, it would show a service as listening when it wasn't.
  44.                 = Bug fix: Only first word of HTTP status description would be shown.
  45.                 = Bug fix: Now handles DNS errors and cURL errors properly, and when the server hangs connections (an alert is triggered).
  46.                 = Bug fix: When testing sites, a 30 second timeout is used. Previously, MOSMC would seem to hang if a site was not responding.
  47.                 = Security fix: Vulnerability when register_globals is enabled in PHP configuration.
  48.                 = Display fix: Alert SMS output has updated formatting for improved readability.
  49.                 = Misc fix: Cleaned up HTML output formatting.
  50.                 = Misc fix: Cleaned up PHP source code formatting.
  51.                 \*** Changelog ***/
  52.  
  53.                 /*** Comments ***\
  54.                 = MOSMC is pronounced as "moss-em-see".
  55.                 = Feel free to improve upon it, but please let me know what you've done with it. :)
  56.                 \*** Comments ***/
  57.                
  58.                 /*** Notes & Usage ***\
  59.                 == How To Configure Initially ==
  60.                 = Go to the area delimited as "Configuration" (the Config class), and insert/modify values as necessary.
  61.                 = For configuration options with arrays (multiple values), such as services and sites, increase the array key (inside the square brackets) by one for each new service/site configured.
  62.                 = Then proceed to use MOSMC via a Web browser, by placing it in a PHP-enabled Web server directory.
  63.                 = For full utilization of MOSMC, it is recommended you set up cron jobs, as detailed later in this documentation. This enables alerts on component failure, as well as periodic reports.
  64.                 =
  65.                 == General ==
  66.                 = Status codes are in ascending order of severity. Higher number, higher severity.
  67.                 = Site monitor links to URL only in case of failure. Otherwise, it doesn't link to the URL being tested.
  68.                 = 'Component' refers to any item that is monitored by MOSMC (such as a specific website or service).
  69.                 =
  70.                 == Cron Jobs // Overview ==
  71.                 = Cron jobs can be run to either notify you on failure of a component or to send reports periodically.
  72.                 = There are two types of cron jobs: Those that trigger alerts only on failure (type = failure), and those who send a full report of all monitored components, regardless of their status (type = report)
  73.                 = Additionally, you can set up a cron job to check for updates periodically (optional, of course).
  74.                 =
  75.                 == Setting Up Local Cron Jobs ==
  76.                 = Use command 'php /path/to/monitor.php type password'
  77.                 = Example: 'php /path/to/monitor.php failure yourpass' if you wanted to send emails only on component failure
  78.                 =
  79.                 == Setting Up Remote Cron Jobs or On-Demand Reporting ==
  80.                 = Use monitor.php?cron=1&type=failure&pass=yourpass if you want to send an email formatted for SMS on component failure with the details of the failing component
  81.                 = Use monitor.php?cron=1&type=report&pass=yourpass if you want to send email with full report regardless of status (Not optimized for SMS)
  82.                 = To send an e-mail, you must 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
  83.                 = Note: HTTP authentication (username and password) is not required when using web cron jobs.
  84.                 =
  85.                 == Checking for Updates Automatically ==
  86.                 = This is optional, but recommended. There are two methods to check for updates:
  87.                 = Add a cron job, either locally ('php /path/to/monitor.php update password'), or remotely (monitor.php?cron=1&type=update&pass=yourpass)
  88.                 = It is recommended that you set your cron job to check once a week, preferably Monday or Tuesday (updates will probably be published on these days)
  89.                 = Note: An e-mail will be sent if an update is available each time you run the cron job.
  90.                 = Note: The Web console may check for updates each time you access it, depending on your configuration.
  91.                 = Note: MOSMC checks updates using cURL from the terminal, when using cron jobs. It checks using an AJAX request when using the Web console.
  92.                 = Note: No information is deliberately sent or stored, other than your current version number. Standard information recorded by Apache logs is saved, but will *NEVER* be used or analyzed whatsoever.
  93.                 =
  94.                 == How MOSMC-PHP Tests Components ==
  95.                 = Services: Checks first if services are listening through netstat, then checks if it accepts internal connections, and then checks if it accepts external connections (last two using fsockopen() ).
  96.                 = Sites: Checks HTTP header response using cURL (from the terminal, not the PHP library).
  97.                 =
  98.                 == Disclaimers ==
  99.                 =
  100.                 ****====IMPORTANT====****
  101.                 = MOSMC outputs certain information that can make it easier to target your system for vulnerabilities, such as ports where services are running, and the types of connections accepted by services.
  102.                 = It is highly recommended that your MOSMC script is *ALWAYS* in a location you only know, and that is not easily guessable.
  103.                 = It is highly recommended that your MOSMC script *ALWAYS* be protected by a strong username and password combination.
  104.                 = It is highly recommended that you have register_globals *DISABLED* in your PHP configuration (although MOSMC makes sure only required input is accepted).
  105.                 ****====IMPORTANT====****
  106.                 =
  107.                 = MOSMC 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).
  108.                 = MOSMC will probably *not* work in PHP 4, as it uses PHP 5 OOP and functions only supported by PHP 5.
  109.                 \*** Notes & Usage ***/
  110.  
  111. class Config {
  112.        
  113.         protected $server, $email, $web, $service, $load, $ram, $fs, $site;
  114.        
  115.         public function __construct()
  116.         {
  117.                 /*** Configuration ***/
  118.                 //External IP of server to test
  119.                 $this->server->ip = '1234.56.78.90';
  120.                 //If using MOSMC in a server with a dynamic IP linked to a DNS record, comment the line above and uncomment line below
  121.                 //$this->server->ip = gethostbyname('sub.domain.tld');
  122.                
  123.                 //Name of your server
  124.                 $this->server->name = 'Server Name';
  125.                
  126.                 //E-mail to send Reports to
  127.                 $this->email->recipient->report = 'reports@yourdomain.tld';
  128.                 //E-mail to send Alerts to
  129.                 $this->email->recipient->alert = 'alerts@yourdomain.tld';
  130.                 //E-mail to send Update notifications to (optional)
  131.                 $this->email->recipient->update = 'update@yourdomain.tld';
  132.                 //E-mail to send Reports from
  133.                 $this->email->sender->report = 'mosmc-report@yourdomain.tld';
  134.                 //E-mail to send Alerts from
  135.                 $this->email->sender->alert = 'mosmc-alert@yourdomain.tld';
  136.                 //E-mail to send Update notifcations from (optional)
  137.                 $this->email->sender->update = 'mosmc-update@yourdomain.tld';
  138.                 //Subject of Report e-mail (Recommended: Default)
  139.                 $this->email->subject->report = $this->server->name.' Report';
  140.                 //Subject of Alert e-mail (Recommended: Default)
  141.                 $this->email->subject->alert = $this->server->name.' Alert';
  142.                 //Subject of Update notification e-mail (Recommended: Default)
  143.                 $this->email->subject->update = 'MOSMC Update needed at '.$this->server->name;
  144.                 //E-mail headers for Reports (Recommended: Default)
  145.                 $this->email->header->report = 'From: "MOSMC" <'.$this->email->sender->report.">\r\n";
  146.                 //E-mail headers for Alerts (Recommended: Default)
  147.                 $this->email->header->alert = 'From: "MOSMC" <'.$this->email->sender->alert.">\r\n";
  148.                 //E-mail headers for Update notifications (Recommended: Default)
  149.                 $this->email->header->update = 'From: "MOSMC" <'.$this->email->sender->update.">\r\n";
  150.                 //Set minimum threshold level to send email. Values: 0 = Send on Warning and Max/Alert (recommended); 1 = Send only on Max/Alert
  151.                 $this->email->threshold = 0;
  152.                
  153.                 //Username used for viewing the Web console.
  154.                 $this->web->username = 'yourUsername';
  155.                 //Password for viewing the Web console, and sending e-mails.
  156.                 $this->web->password = 'yourVerySecurePassword';
  157.                 //Set if you want to check for updates automatically when visiting the Web console. 1 = yes (default), 0 = no
  158.                 $this->web->update = 1;
  159.                
  160.                 //Human readable service name
  161.                 $this->service[0]->name = 'httpd';
  162.                 $this->service[1]->name = 'MySQL';
  163.                
  164.                 //Type of connection accepted by service. Type 0 = 'Local service' (only internal connections), type 1 = 'Public service' (internal + external connections)
  165.                 $this->service[0]->type = 1;
  166.                 $this->service[1]->type = 0;
  167.                
  168.                 //Port service is running on
  169.                 $this->service[0]->port = 80;
  170.                 $this->service[1]->port = 3306;
  171.                
  172.                 //Statistics Warning and Maximum/Alert thresholds
  173.                 //Load average
  174.                 $this->load->warn = '4.00';
  175.                 $this->load->max = '8.00';
  176.                
  177.                 //RAM
  178.                 $this->ram->warn = 460;
  179.                 $this->ram->max = 512;
  180.                
  181.                 //Storage (File System)
  182.                 $this->fs->partition = 'sda1'; //As listed in 'df' after /dev/
  183.                 $this->fs->warn = 8*1024; //8GB (2.0 GB free)
  184.                 $this->fs->max = 10*1024; //10GB (0.0 GB free)
  185.                
  186.                 //Human readable website name
  187.                 $this->site[0]->name = 'Google';
  188.                 $this->site[1]->name = 'Slashdot';
  189.                
  190.                 //URL of websites to check (You may use alternate ports. Ex: http://domain.tld:8080)
  191.                 $this->site[0]->url = 'http://www.google.com';
  192.                 $this->site[1]->url = 'http://slashdot.org';
  193.                 /*** Configuration ***/
  194.         }
  195.        
  196. }
  197.  
  198. class Monitor extends Config {
  199.        
  200.         protected $version, $get, $services, $sites;
  201.         private $global, $key;
  202.        
  203.         public function __construct()
  204.         {
  205.                 $this->sanitizeGlobals();
  206.                 $this->getArguments();
  207.                 parent::__construct();
  208.                 $this->version = '2.0.0';
  209.                 $this->populateData();
  210.                
  211.         }
  212.        
  213.         private function sanitizeGlobals()
  214.         {
  215.                 //We don't need any of these
  216.                 foreach(array($_POST, $_COOKIE, $_FILES, $_ENV, (isset($_SESSION) && is_array($_SESSION)) ? $_SESSION : array()) as $global)
  217.                 {
  218.                         unset($global);
  219.                 }
  220.                
  221.                 //Lets only grab the input we need, and get rid of everything else
  222.                 foreach(array($_GET, $_SERVER) as $global)
  223.                 {
  224.                         foreach($global as $key => $value)
  225.                         {
  226.                                 if($key == 'argv')
  227.                                 {
  228.                                         $this->get->$key = $value;
  229.                                 }
  230.                                 elseif($key == 'PHP_AUTH_USER')
  231.                                 {
  232.                                         $this->get->http->user = $value;
  233.                                 }
  234.                                 elseif($key == 'PHP_AUTH_PW')
  235.                                 {
  236.                                         $this->get->http->pass = $value;
  237.                                 }
  238.                                 elseif($key == 'cron' || $key == 'type' || $key == 'pass')
  239.                                 {
  240.                                         $_GET[$key] = $value;
  241.                                 }
  242.                                 else
  243.                                 {
  244.                                         unset($_GET[$key], $_SERVER[$key]);
  245.                                 }
  246.                         }
  247.                 }
  248.         }
  249.  
  250.         private function getArguments()
  251.         {
  252.                 //Check if running cron job. If so, get configuration options from CLI.
  253.                 if(isset($this->get->argv))
  254.                 {
  255.                         $this->get->cron = 1;
  256.                         $this->get->type = strtolower($this->get->argv[1]);
  257.                         $this->get->pass = $this->get->argv[2];
  258.                 }
  259.                 else
  260.                 {
  261.                 //Already sanitized. Contains only necessary information.
  262.                         foreach($_GET as $key => $value)
  263.                         {
  264.                                 $this->get->$key = $value;
  265.                         }
  266.                 }
  267.         }
  268.        
  269.         private function populateData()
  270.         {
  271.                 /*** Populate Data Variables ***/
  272.                 $this->services->count = count($this->service);
  273.                 $this->sites->count = count($this->site);
  274.                 $this->checkServices();
  275.                 $this->grabStats();
  276.                 $this->checkSites();
  277.                 /*** Populate Data Variables ***/
  278.         }
  279.        
  280.         private function checkServices()
  281.         {
  282.                 //Check if ports are open for each service
  283.                 for($i=0; $i < $this->services->count; $i++)
  284.                 {
  285.                         $this->service[$i]->status->listen = -1;
  286.                         $this->service[$i]->status->internal = -1;
  287.                         $this->service[$i]->status->external = -1;
  288.  
  289.                         //Check if services are listening
  290.                         $listenCheck = exec('netstat -l -n | grep ":'.$this->service[$i]->port.' " | wc -l');
  291.                         //If listening, win
  292.                         if($listenCheck  > 0)
  293.                         {
  294.                                 $this->service[$i]->status->listen = 0;
  295.                         }
  296.                         //If not listening, fail
  297.                         else
  298.                         {
  299.                                 $this->service[$i]->status->listen = 1;
  300.                         }
  301.                        
  302.                         //Check internal connection if service is listening for connections
  303.                         if($this->service[$i]->status->listen == 0)
  304.                         {
  305.                                 $fp = @fsockopen('localhost', $this->service[$i]->port, $errno, $errstr, 1);
  306.                                 //If internal connection is accepted, win
  307.                                 if($fp)
  308.                                 {
  309.                                         $this->service[$i]->status->internal = 0;
  310.                                 }
  311.                                 //If internal connection is rejected, fail
  312.                                 else
  313.                                 {
  314.                                         $this->service[$i]->status->internal = 1;
  315.                                 }
  316.                         }
  317.                        
  318.                         //Check external connection if internal connection is available
  319.                         if($this->service[$i]->status->internal == 0 && $this->service[$i]->status->listen == 0)
  320.                         {
  321.                                 $fp = @fsockopen($this->server->ip, $this->service[$i]->port, $errno, $errstr, 1);
  322.                                 //If external connection is accepted, and is supposed to be accepted, win
  323.                                 if($fp && $this->service[$i]->type == 1)
  324.                                 {
  325.                                         $this->service[$i]->status->external = 0;
  326.                                 }
  327.                                 //If external connection is rejected, and not supposed to be accepted, win
  328.                                 elseif(!$fp && $this->service[$i]->type == 0)
  329.                                 {
  330.                                         $this->service[$i]->status->external = 1;
  331.                                 }
  332.                                 //If external connection is rejected, and is supposed to be accepted, fail
  333.                                 elseif(!$fp && $this->service[$i]->type == 1)
  334.                                 {
  335.                                         $this->service[$i]->status->external = 2;
  336.                                 }
  337.                                 //If external connection is accepted, but not supposed to be, fail + alert
  338.                                 elseif($fp && $this->service[$i]->type == 0)
  339.                                 {
  340.                                         $this->service[$i]->status->external = 3;
  341.                                 }
  342.                         }
  343.                 }
  344.         }
  345.        
  346.         private function grabStats()
  347.         {
  348.                 //Grab Uptime + Load Averages
  349.                 $this->uptime = @exec('uptime');
  350.                 //Extract load averages
  351.                 preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/", $this->uptime, $this->loads);
  352.                
  353.                 $this->load->status = -1;
  354.                
  355.                 if($this->load->warn > $this->loads[1] && $this->loads[1] < $this->load->max)
  356.                 {
  357.                         $this->load->status = 0;
  358.                 }
  359.                 elseif($this->load->warn <= $this->loads[1] && $this->loads[1] < $this->load->max)
  360.                 {
  361.                         $this->load->status = 1;
  362.                 }
  363.                 else
  364.                 {
  365.                         $this->load->status = 2;
  366.                 }
  367.                
  368.                 //Extract uptime
  369.                 $this->uptime = explode(' up ', $this->uptime);
  370.                 $this->uptime = explode(',', $this->uptime[1]);
  371.                 $this->uptime = $this->uptime[0].', '.trim($this->uptime[1]);
  372.                
  373.                 //Grab number of users logged in
  374.                 $this->users = trim(shell_exec('who | wc -l'));
  375.                
  376.                 //Grab RAM usage
  377.                 $this->ram->used = trim(shell_exec('free -m | grep "buffers/cache" | awk \'{print $3}\''));
  378.                 $this->ram->total = shell_exec('free -m | grep Mem | awk \'{print $2}\'');
  379.                
  380.                 $this->ram->status = -1;
  381.                
  382.                 if($this->ram->warn > $this->ram->used && $this->ram->used < $this->ram->max)
  383.                 {
  384.                         $this->ram->status = 0;
  385.                 }
  386.                 elseif($this->ram->warn <= $this->ram->used && $this->ram->used < $this->ram->max)
  387.                 {
  388.                         $this->ram->status = 1;
  389.                 }
  390.                 else
  391.                 {
  392.                         $this->ram->status = 2;
  393.                 }
  394.                
  395.                 //Grab HDD File System usage
  396.                 $this->fs->free = round(shell_exec('df | grep '.$this->fs->partition.' | awk \'{print $4}\'')/1024, 2);
  397.                 $this->fs->used = round(shell_exec('df | grep '.$this->fs->partition.' | awk \'{print $3}\'')/1024, 2);
  398.                 $this->fs->total = round(shell_exec('df | grep '.$this->fs->partition.' | awk \'{print $2}\'')/1024, 2);
  399.                
  400.                 $this->fs->status = -1;
  401.                
  402.                 if($this->fs->warn > $this->fs->used  && $this->fs->used < $this->fs->max)
  403.                 {
  404.                         $this->fs->status = 0;
  405.                 }
  406.                 elseif($this->fs->warn <= $this->fs->used && $this->fs->used < $this->fs->max)
  407.                 {
  408.                         $this->fs->status = 1;
  409.                 }
  410.                 else
  411.                 {
  412.                         $this->fs->status = 2;
  413.                 }
  414.                
  415.                 //Grab number of processes
  416.                 $this->processes = trim(shell_exec('ps aux | wc -l'));
  417.                 $this->processes = $this->processes--;
  418.                
  419.                 //Grab number of connections
  420.                 $this->conns->tcp = trim(shell_exec('netstat -t | grep tcp | wc -l'));
  421.                 $this->conns->udp = trim(shell_exec('netstat -u | grep udp | wc -l'));
  422.         }
  423.        
  424.         private function checkSites()
  425.         {
  426.                 //Check if sites are returning HTTP status 200 (OK) or HTTP status 302 (Found)
  427.                 for($i=0; $i < $this->sites->count; $i++)
  428.                 {
  429.                         $this->site[$i]->status = -1;
  430.  
  431.                         //Grab HTTP Status. The whitespace in the awk command is there as a workaround to a small bug when parsing data w/ awk.
  432.                         $this->site[$i]->http->response = trim(shell_exec('curl '.$this->site[$i]->url." -I -s -m 30 -A 'MOSMC-PHP' | grep HTTP | ".'awk \'{print "               "$2" "$3" "$4" "$5" "$6}\''));
  433.                         $this->site[$i]->http->status = substr($this->site[$i]->http->response, 0, 3);
  434.                         $this->site[$i]->http->desc = trim(substr($this->site[$i]->http->response, 3));
  435.                         //If headers return HTTP status 200 (OK) or HTTP status 302 (Found), win
  436.                         if($this->site[$i]->http->status == 200 || $this->site[$i]->http->status == 302)
  437.                         {
  438.                                 $this->site[$i]->status = 0;
  439.                         }
  440.                         //If DNS cannot be resolved, cURL isn't installed, or server hangs connections, fail
  441.                         elseif($this->site[$i]->http->response == '')
  442.                         {
  443.                                 $this->site[$i]->status = 1;
  444.                                 $this->site[$i]->http->desc = 'DNS/cURL error or server hangs conn.';
  445.                         }
  446.                         //If headers return HTTP status other than 200 (OK) or HTTP status 302 (Found), fail
  447.                         else
  448.                         {
  449.                                 $this->site[$i]->status = 1;
  450.                         }
  451.                 }
  452.         }
  453.        
  454. }
  455.  
  456. class BrowserOutput extends Monitor {
  457.        
  458.         public function __construct()
  459.         {
  460.                 parent::__construct();
  461.                 if($this->get->http->user == $this->web->username && $this->get->http->pass == $this->web->password)
  462.                 {
  463.                         //Do nothing
  464.                 }
  465.                 else
  466.                 {
  467.                         header('WWW-Authenticate: Basic realm="MOSMC"');
  468.                         exit('Not authorized.');
  469.                 }
  470.                
  471.                 /*** Testing Variables ***/
  472.                 //These testing variables only affect browser output, not email notifications.
  473.                 //Various service status scenarios
  474.                 //1-Service is not listening, probably because it's down
  475.                 /*$this->service[0]->status->listen = 1;
  476.                 $this->service[0]->status->internal = -1;
  477.                 $this->service[0]->status->external = -1;*/
  478.                 //2-Service is listening, but rejecting all connections
  479.                 /*$this->service[0]->status->listen = 0;
  480.                 $this->service[0]->status->internal = 1;
  481.                 $this->service[0]->status->external = -1;*/
  482.                 //3-Service is listening, and rejecting external connections, but supposed to accept them
  483.                 /*$this->service[0]->status->listen = 0;
  484.                 $this->service[0]->status->internal = 0;
  485.                 $this->service[0]->status->external = 2;*/
  486.                 //4-Service is listening, and accepting external connections, but supposed to reject them
  487.                 /*$this->service[0]->status->listen = 0;
  488.                 $this->service[0]->status->internal = 0;
  489.                 $this->service[0]->status->external = 3;*/
  490.                
  491.                 //Various system stats failure scenarios
  492.                 //Trigger warning
  493.                 /*$this->load->status = 1;
  494.                 $this->ram->status = 1;
  495.                 $this->fs->status = 1;*/
  496.                 //Trigger alert
  497.                 /*$this->load->status = 2;
  498.                 $this->ram->status = 2;
  499.                 $this->fs->status = 2;*/
  500.                
  501.                 //Site failure scenario
  502.                 /*$this->site[0]->status = 1;
  503.                 $this->site[0]->http->status = 500;
  504.                 $this->site[0]->http->desc = 'Internal Server Error';*/
  505.         }
  506.        
  507.         public function update()
  508.         {
  509.                 //Send a request to my update checking script
  510.                 $return = shell_exec('curl http://update.thephpjedi.com/mosmc/?version='.$this->version.' -s -m 30 -A "MOSMC Updater"');
  511.                 //If we get a response, display it
  512.                 if($return != '')
  513.                 {
  514.                         echo $return;
  515.                 }
  516.                 else
  517.                 {
  518.                         echo 'updates=>Error while checking for newer version.|';
  519.                 }
  520.         }
  521.        
  522.         public function render()
  523.         {
  524.         ?>
  525. <html>
  526.         <head>
  527.                 <title><?php echo $this->server->name; ?> Monitoring Console</title>
  528.                 <style type="text/css">
  529.                         body {
  530.                                 background-color:#f4ead3;
  531.                                 color:#746f63;
  532.                                 text-align:center;
  533.                         }
  534.                         h1 {
  535.                                 color:#838078;
  536.                         }
  537.                         a:link, a:active, a:visited {
  538.                                 color:#595751;
  539.                                 text-decoration:none;
  540.                         }
  541.                         a:hover {
  542.                                 color:#6e6c65;
  543.                         }
  544.                         table{
  545.                                 border: 1.5pt dotted #dacfb6;
  546.                                 font-size:0.8em;
  547.                         }
  548.                         .up {
  549.                                 background-color:#99cc66;
  550.                         }
  551.                         .down {
  552.                                 background-color:#cc3333;
  553.                                 color:#eee;
  554.                         }
  555.                         .warn {
  556.                                 background-color:#fcc15a;
  557.                         }
  558.                         #footer {
  559.                                 background-color:#e9ddc2;
  560.                                 border: 1pt dotted #dacfb6;
  561.                                 font-size:0.7em;
  562.                                 margin:auto;
  563.                                 margin-top:20px;
  564.                                 padding:5px;
  565.                                 width:350px;
  566.                         }
  567.                         #updates {
  568.                                 background-color:#dacfb6;
  569.                                 color:#746f63;
  570.                                 font-size:0.8em;
  571.                                 margin:auto;
  572.                                 margin-bottom:20px;
  573.                                 padding:5px;
  574.                                 width:350px;
  575.                         }
  576.                         #updates input {
  577.                                 background-color:#e9ddc2;
  578.                                 border:1px solid #eee;
  579.                                 color:#746f63;
  580.                         }
  581.                 </style>
  582.                
  583.                 <script type="text/javascript">
  584.                         //Very lightweight AJAX library. I love it! -TPJ
  585.                         //Created by Sean Kane (http://celtickane.com/programming/code/ajax.php)
  586.                         //Feather Ajax v1.0.1
  587.                        
  588.                         function AjaxObject101() {
  589.                                 this.createRequestObject = function() {
  590.                                         try {
  591.                                                 var ro = new XMLHttpRequest();
  592.                                         }
  593.                                         catch (e) {
  594.                                                 var ro = new ActiveXObject("Microsoft.XMLHTTP");
  595.                                         }
  596.                                         return ro;
  597.                                 }
  598.                                 this.sndReq = function(action, url, data) {
  599.                                         if (action.toUpperCase() == "POST") {
  600.                                                 this.http.open(action,url,true);
  601.                                                 this.http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  602.                                                 this.http.onreadystatechange = this.handleResponse;
  603.                                                 this.http.send(data);
  604.                                         }
  605.                                         else {
  606.                                                 this.http.open(action,url + '?' + data,true);
  607.                                                 this.http.onreadystatechange = this.handleResponse;
  608.                                                 this.http.send(null);
  609.                                         }
  610.                                 }
  611.                                 this.handleResponse = function() {
  612.                                         if ( me.http.readyState == 4) {
  613.                                                 if (typeof me.funcDone == 'function') { me.funcDone();}
  614.                                                 var rawdata = me.http.responseText.split("|");
  615.                                                 for ( var i = 0; i < rawdata.length; i++ ) {
  616.                                                         var item = (rawdata[i]).split("=>");
  617.                                                         if (item[0] != "") {
  618.                                                                 if (item[1].substr(0,3) == "%V%" ) {
  619.                                                                         document.getElementById(item[0]).value = item[1].substring(3);
  620.                                                                 }
  621.                                                                 else {
  622.                                                                         document.getElementById(item[0]).innerHTML = item[1];
  623.                                                                 }
  624.                                                         }
  625.                                                 }
  626.                                         }
  627.                                         if ((me.http.readyState == 1) && (typeof me.funcWait == 'function')) { me.funcWait(); }
  628.                                 }
  629.                                 var me = this;
  630.                                 this.http = this.createRequestObject();
  631.                                
  632.                                 var funcWait = null;
  633.                                 var funcDone = null;
  634.                         }
  635.                 </script>
  636.         </head>
  637.         <body>
  638.                 <h1><?php echo $this->server->name; ?> Monitoring Console</h1>
  639.                 <h2><?php echo $this->server->ip; ?></h2>
  640.                
  641.                 <div id="updates">
  642.                         <input type="button" onclick="checkUpdates();" value="Check for newer version" />
  643.                 </div>
  644.                
  645. <?
  646.                 $this->services();
  647.                 $this->stats();
  648.                 $this->sites();
  649. ?>
  650.                
  651.                 <div id="footer">
  652.                         MOSMC v<?php echo $this->version; ?> | Alesandro Ortiz<br />
  653.                         The source of this script is available at <a href="http://thephpjedi.com">ThePHPJedi.com</a>
  654.                         <div style="width:80%;margin:3px auto 3px auto;border-top:1px solid #746f63;"></div>
  655.                         Source of MOSMC is licensed under a     <a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/us/">Creative Commons Attribution-Noncommercial 3.0 United States License</a>.
  656.                 </div>
  657.                
  658.                 <script type="text/javascript">
  659.                         function waiting()
  660.                         {
  661.                            document.getElementById('updates').innerHTML = 'Checking for newer version...';
  662.                         }
  663.                         function checkUpdates()
  664.                         {
  665.                                 var ao = new AjaxObject101();
  666.                                 ao.funcWait = waiting;
  667.                                 ao.sndReq('get','','update=1');
  668.                         }
  669. <?php
  670.                         if($this->web->update)
  671.                         {
  672. ?>
  673.                                 checkUpdates();
  674. <?
  675.                         }
  676. ?>
  677.                 </script>
  678.                
  679.         </body>
  680. </html>
  681. <?
  682.         }
  683.  
  684.         private function services()
  685.         {
  686. ?>
  687.         <table width="600px" border="0" cellspacing="1" cellpadding="5" align="center">
  688.                 <tr style="background-color:#e9ddc2">
  689.                         <td style="min-width:75px;background-color:#dacfb6">Service</td>
  690.                         <td>Port</td>
  691.                         <td>Local Connection</td>
  692.                         <td>External Connection</td>
  693.                 </tr>
  694.                
  695. <?php
  696.                 for($i=0; $i < $this->services->count; $i++)
  697.                 {
  698. ?>
  699.                 <tr style="background-color:#dacfb6">
  700.                         <td style="background-color:#e9ddc2"><?php echo $this->service[$i]->name; ?></td>
  701.                         <?php
  702.                                 switch ($this->service[$i]->status->listen)
  703.                                 {
  704.                                         case 0:
  705.                                                 echo '<td class="up">';
  706.                                             echo 'Listening ('.$this->service[$i]->port.')';
  707.                                             break;
  708.                                         case 1:
  709.                                                 echo '<td class="down">';
  710.                                             echo '<b>Not listening</b> ('.$this->service[$i]->port.')';
  711.                                             break;
  712.                                         default:
  713.                                                 echo '<td class="warn">';
  714.                                                 echo 'N/A';
  715.                                                 break;
  716.                                 }
  717.                         ?></td>
  718.                         <?php
  719.                                 switch ($this->service[$i]->status->internal)
  720.                                 {
  721.                                         case 0:
  722.                                                 echo '<td class="up">';
  723.                                             echo 'Accepting';
  724.                                             break;
  725.                                         case 1:
  726.                                                 echo '<td class="down">';
  727.                                             echo '<b>Rejecting</b>';
  728.                                             break;
  729.                                         default:
  730.                                                 echo '<td class="warn">';
  731.                                                 echo 'N/A';
  732.                                                 break;
  733.                                 }
  734.                         ?></td>
  735.                         <?php
  736.                                 switch ($this->service[$i]->status->external)
  737.                                 {
  738.                                         case 0:
  739.                                                 echo '<td class="up">';
  740.                                             echo 'Accepting as configured';
  741.                                             break;
  742.                                         case 1:
  743.                                                 echo '<td class="up">';
  744.                                             echo 'Rejecting as configured';
  745.                                             break;
  746.                                         case 2:
  747.                                                 echo '<td class="down">';
  748.                                                 echo '<b>Rejecting; Misconfigured</b>';
  749.                                                 break;
  750.                                         case 3:
  751.                                                 echo '<td class="down">';
  752.                                                 echo '<b>Accepting; Misconfigured</b>';
  753.                                                 break;
  754.                                         default:
  755.                                                 echo '<td class="warn">';
  756.                                                 echo 'N/A';
  757.                                                 break;
  758.                                 }
  759.                         ?></td>
  760.                 </tr>
  761. <?php
  762.                 }
  763. ?>
  764.         </table>
  765.        
  766.         <br />
  767.        
  768. <?
  769.         }
  770.        
  771.         private function stats()
  772.         {
  773. ?>
  774.         <table width="600px" border="0" cellspacing="1" cellpadding="5" bordercolor="#333333" align="center">
  775.                 <tr style="background-color:#e9ddc2">
  776.                         <td style="width:125px;background-color:#dacfb6">Stat Type</td>
  777.                         <td style="width:150px;">Current Value</td>
  778.                         <td>Warn Threshold</td>
  779.                         <td>Alert Threshold</td>
  780.                 </tr>
  781.                
  782.                 <tr style="background-color:#dacfb6">
  783.                         <td style="background-color:#e9ddc2">Load Averages</td>
  784.                         <?php
  785.                         switch ($this->load->status)
  786.                         {
  787.                                 case 0:
  788.                                         echo '<td class="up">';
  789.                                         echo $this->loads[1].' '.$this->loads[2].' '.$this->loads[3];
  790.                                         break;
  791.                                 case 1:
  792.                                         echo '<td class="warn">';
  793.                                         echo '<b>'.$this->loads[1].' '.$this->loads[2].' '.$this->loads[3].'</b>';
  794.                                         break;
  795.                                 case 2:
  796.                                         echo '<td class="down">';
  797.                                         echo '<b>'.$this->loads[1].' '.$this->loads[2].' '.$this->loads[3].'</b>';
  798.                                         break;
  799.                                 default:
  800.                                         echo '<td class="warn">';
  801.                                         echo 'Error';
  802.                                         break;
  803.                         }
  804.                         ?></td>
  805.                         <td><?php echo $this->load->warn; ?> (1 min avg)</td>
  806.                         <td><?php echo $this->load->max; ?> (1 min avg)</td>
  807.                 </tr>
  808.  
  809.                 <tr style="background-color:#dacfb6">
  810.                         <td style="background-color:#e9ddc2">Used RAM</td>
  811.                         <?php
  812.                         switch ($this->ram->status)
  813.                         {
  814.                                 case 0:
  815.                                         echo '<td class="up">';
  816.                                         echo $this->ram->used;
  817.                                         break;
  818.                                 case 1:
  819.                                         echo '<td class="warn">';
  820.                                         echo '<b>'.$this->ram->used.'</b>';
  821.                                         break;
  822.                                 case 2:
  823.                                         echo '<td class="down">';
  824.                                         echo '<b>'.$this->ram->used.'</b>';
  825.                                         break;
  826.                                 default:
  827.                                         echo '<td class="warn">';
  828.                                         echo 'Error';
  829.                                         break;
  830.                         }
  831.                         ?> MB</td>
  832.                         <td><?php echo $this->ram->warn; ?> MB</td>
  833.                         <td><?php echo ($this->ram->max >= 1024) ? ($this->ram->max/1024).' GB' : $this->ram->max.' MB'; ?></td>
  834.                 </tr>
  835.  
  836.                 <tr style="background-color:#dacfb6">
  837.                         <td style="background-color:#e9ddc2">Used Storage</td>
  838.                         <?php
  839.                         switch ($this->fs->status)
  840.                         {
  841.                                 case 0:
  842.                                         echo '<td class="up">';
  843.                                         echo ($this->fs->used >= 1024) ? round($this->fs->used/1024, 2).' GB' : $this->fs->used.' MB';
  844.                                         break;
  845.                                 case 1:
  846.                                         echo '<td class="warn"><b>';
  847.                                         echo ($this->fs->used >= 1024) ? round($this->fs->used/1024, 2).' GB' : $this->fs->used.' MB';
  848.                                         echo '</b>';
  849.                                         break;
  850.                                 case 2:
  851.                                         echo '<td class="down"><b>';
  852.                                         echo ($this->fs->used >= 1024) ? round($this->fs->used/1024, 2).' GB' : $this->fs->used.' MB';
  853.                                         echo '</b>';
  854.                                         break;
  855.                                 default:
  856.                                         echo '<td class="warn">';
  857.                                         echo 'Error';
  858.                                         break;
  859.                         }
  860.                         ?></td>
  861.                         <td><?php echo ($this->fs->warn >= 1024) ? round($this->fs->warn/1024, 2).' GB' : $this->fs->warn.' MB'; ?></td>
  862.                         <td><?php echo ($this->fs->max >= 1024) ? round($this->fs->max/1024, 2).' GB' : $this->fs->max.' MB'; ?></td>
  863.                 </tr>
  864.  
  865.         </table>
  866.  
  867.         <br />
  868.                
  869.         <table width="600px" border="0" cellspacing="1" cellpadding="5" bordercolor="#333333" align="center">
  870.                 <tr style="background-color:#e9ddc2">
  871.                         <td style="width:150px;background-color:#dacfb6">Stat Type</td>
  872.                         <td style="width:150px;">Current Value</td>
  873.                         <td style="width:150px;background-color:#dacfb6">Stat Type</td>
  874.                         <td style="width:150px;">Current Value</td>
  875.                 </tr>
  876.  
  877.                 <tr style="background-color:#dacfb6">
  878.                         <td style="background-color:#e9ddc2">Processes</td>
  879.                         <td><?php echo $this->processes; ?></td>
  880.        
  881.                         <td style="background-color:#e9ddc2">Users Logged In</td>
  882.                         <td><?php echo $this->users; ?></td>
  883.                 </tr>
  884.        
  885.                 <tr style="background-color:#dacfb6">
  886.                         <td style="background-color:#e9ddc2">Uptime</td>
  887.                         <td><?php echo $this->uptime; ?></td>
  888.        
  889.                         <td style="background-color:#e9ddc2">TCP/UDP Conns</td>
  890.                         <td><?php echo $this->conns->tcp.'/'.$this->conns->udp; ?></td>
  891.                 </tr>
  892.         </table>
  893.                
  894.         <br />
  895. <?
  896.         }
  897.        
  898.         private function sites()
  899.         {
  900. ?>
  901.         <table width="600px" border="0" cellspacing="1" cellpadding="5" bordercolor="#333333" align="center">
  902.                 <tr style="background-color:#e9ddc2">
  903.                         <td style="width:125px;background-color:#dacfb6">Site</td>
  904.                         <td style="width:175px;">HTTP Status</td>
  905.                         <td style="width:300px;">URL</td>
  906.                 </tr>
  907.                
  908. <?php
  909.                 for($i=0; $i < $this->sites->count; $i++)
  910.                 {
  911. ?>
  912.                 <tr style="background-color:#dacfb6">
  913.                         <td style="background-color:#e9ddc2"><?php echo $this->site[$i]->name; ?></td>
  914.                         <?php
  915.                         switch ($this->site[$i]->status)
  916.                         {
  917.                                 case 0:
  918.                                         echo '<td class="up">';
  919.                                     echo $this->site[$i]->http->status.' ('.$this->site[$i]->http->desc.')';
  920.                                     break;
  921.                                 case 1:
  922.                                         echo '<td class="down">';
  923.                                     echo $this->site[$i]->http->status.' ('.$this->site[$i]->http->desc.')';
  924.                                     break;
  925.                                 default:
  926.                                         echo '<td class="warn">';
  927.                                         echo 'N/A';
  928.                                         break;
  929.                         }
  930.                         ?></td>
  931.                         <td><?php
  932.                                 //If site is not returning HTTP 200 (OK) or HTTP status 302 (Found), link. Otherwise, just show URL
  933.                                 echo ($this->site[$i]->status) ? '<a href="'.$this->site[$i]->url.'">'.$this->site[$i]->url.'</a>' : $this->site[$i]->url;
  934.                         ?></td>
  935.                 </tr>
  936. <?php
  937.                 }
  938. ?>
  939.         </table>
  940. <?
  941.         }
  942.        
  943. }
  944.  
  945. class EmailOutput extends Monitor {
  946.        
  947.         public function __construct()
  948.         {
  949.                 parent::__construct();
  950.                
  951.                 /*** Testing Variables ***/
  952.                 //These testing variables only affect email notifications, not browser output.
  953.                 //Various service status scenarios
  954.                 //1-Service is not listening, probably because it's down
  955.                 /*$this->service[0]->status->listen = 1;
  956.                 $this->service[0]->status->internal = -1;
  957.                 $this->service[0]->status->external = -1;*/
  958.                 //2-Service is listening, but rejecting all connections
  959.                 /*$this->service[0]->status->listen = 0;
  960.                 $this->service[0]->status->internal = 1;
  961.                 $this->service[0]->status->external = -1;*/
  962.                 //3-Service is listening, and rejecting external connections, but supposed to accept them
  963.                 /*$this->service[1]->status->listen = 0;
  964.                 $this->service[1]->status->internal = 0;
  965.                 $this->service[1]->status->external = 2;*/
  966.                 //4-Service is listening, and accepting external connections, but supposed to reject them
  967.                 /*$this->service[0]->status->listen = 0;
  968.                 $this->service[0]->status->internal = 0;
  969.                 $this->service[0]->status->external = 3;*/
  970.                
  971.                 //Various system stats failure scenarios
  972.                 //Trigger warning
  973.                 /*$this->load->status = 1;
  974.                 $this->ram->status = 1;
  975.                 $this->fs->status = 1;*/
  976.                 //Trigger alert
  977.                 /*$this->load->status = 2;
  978.                 $this->ram->status = 2;
  979.                 $this->fs->status = 2;*/
  980.                
  981.                 //Site failure scenario
  982.                 /*$this->site[0]->status = 1;
  983.                 $this->site[0]->http->status = 500;
  984.                 $this->site[0]->http->desc = 'Internal Server Error';*/
  985.                 /*** Testing Variables ***/
  986.         }
  987.        
  988.         public function update()
  989.         {
  990.                 //Send a request to my update checking script
  991.                 $return = shell_exec('curl "http://update.thephpjedi.com/mosmc/?version='.$this->version.'&method=cron" -s -m 30 -A "MOSMC Updater"');
  992.                 //If we get a response, send it
  993.                 if($return != '')
  994.                 {
  995.                         mail($this->email->recipient->update, $this->email->subject->update, $return, $this->email->header->update, '-f'.$this->email->sender->update);
  996.                 }
  997.         }
  998.        
  999.         public function send()
  1000.         {
  1001.                 if($this->web->password == $this->get->pass)
  1002.                 {
  1003.                         if($this->get->type == 'report')
  1004.                         {
  1005.                                 $this->generateReport();
  1006.                                 mail($this->email->recipient->report, $this->email->subject->report, $this->email->output, $this->email->header->report, '-f'.$this->email->sender->report);
  1007.                                 echo 'Cron job run successfully.';
  1008.                         }
  1009.                         elseif($this->get->type == 'failure')
  1010.                         {
  1011.                                 $this->generateAlert();
  1012.                                 //Send SMS containing alerts only if threshold includes alerts.
  1013.                                 if(isset($this->email->alert->trigger) && $this->email->alert->trigger)
  1014.                                 {
  1015.                                         mail($this->email->recipient->alert, $this->email->subject->alert, $this->email->output, $this->email->header->alert, '-f'.$this->email->sender->alert);
  1016.                                 }
  1017.                                 //Send SMS containing warnings only if threshold includes warnings.
  1018.                                 elseif((isset($this->email->warning->trigger) && $this->email->warning->trigger) && $this->email->threshold == 0)
  1019.                                 {
  1020.                                         mail($this->email->recipient->alert, $this->email->subject->alert, $this->email->output, $this->email->header->alert, '-f'.$this->email->sender->alert);
  1021.                                 }
  1022.                                 echo 'Cron job run successfully.';
  1023.                         }
  1024.                 }
  1025.         }
  1026.        
  1027.         private function generateReport()
  1028.         {
  1029.                 ob_start();
  1030.                         echo '=Services='."\n";
  1031.                         for($i=0; $i < $this->services->count; $i++)
  1032.                         {
  1033.                                 echo $this->service[$i]->name;
  1034.                                
  1035.                                 echo ' | Port: ';
  1036.                                 switch ($this->service[$i]->status->listen)
  1037.                                 {
  1038.                                         case 0:
  1039.                                                 echo 'Up, ';
  1040.                                             echo 'Listening ('.$this->service[$i]->port.')';
  1041.                                             break;
  1042.                                         case 1:
  1043.                                                 echo 'Down, ';
  1044.                                             echo 'Not listening ('.$this->service[$i]->port.')';
  1045.                                             break;
  1046.                                         default:
  1047.                                                 echo 'Warning, ';
  1048.                                                 echo 'N/A';
  1049.                                                 break;
  1050.                                 }
  1051.                                
  1052.                                 echo "\n".'Local Connection: ';
  1053.                                 switch ($this->service[$i]->status->internal)
  1054.                                 {
  1055.                                         case 0:
  1056.                                                 echo 'Up, ';
  1057.                                             echo 'Accepting';
  1058.                                             break;
  1059.                                         case 1:
  1060.                                                 echo 'Down, ';
  1061.                                             echo 'Rejecting';
  1062.                                             break;
  1063.                                         default:
  1064.                                                 echo 'Warning, ';
  1065.                                                 echo 'N/A';
  1066.                                                 break;
  1067.                                 }
  1068.                                
  1069.                                 echo "\n".'External Connection: ';
  1070.                                 switch ($this->service[$i]->status->external)
  1071.                                 {
  1072.                                         case 0:
  1073.                                                 echo 'Up, ';
  1074.                                             echo 'Accepting as configured';
  1075.                                             break;
  1076.                                         case 1:
  1077.                                                 echo 'Up, ';
  1078.                                             echo 'Rejecting as configured';
  1079.                                             break;
  1080.                                         case 2:
  1081.                                                 echo 'Down, ';
  1082.                                                 echo 'Rejecting; Misconfigured';
  1083.                                                 break;
  1084.                                         case 3:
  1085.                                                 echo 'Down, ';
  1086.                                                 echo 'Accepting; Misconfigured';
  1087.                                                 break;
  1088.                                         default:
  1089.                                                 echo 'Warning, ';
  1090.                                                 echo 'N/A';
  1091.                                                 break;
  1092.                                 }
  1093.                                 echo "\n\n";
  1094.                         }
  1095.                
  1096.                         echo '=System Stats=';
  1097.                        
  1098.                         echo "\n".'Loads: ';
  1099.                         switch ($this->load->status)
  1100.                         {
  1101.                                 case 0:
  1102.                                         echo 'Up, ';
  1103.                                         echo $this->loads[1].' '.$this->loads[2].' '.$this->loads[3];
  1104.                                         break;
  1105.                                 case 1:
  1106.                                         echo 'Warning, ';
  1107.                                         echo $this->loads[1].' '.$this->loads[2].' '.$this->loads[3];
  1108.                                         break;
  1109.                                 case 2:
  1110.                                         echo 'Down, ';
  1111.                                         echo $this->loads[1].' '.$this->loads[2].' '.$this->loads[3];
  1112.                                         break;
  1113.                                 default:
  1114.                                         echo 'Warning, ';
  1115.                                         echo 'Error';
  1116.                                         break;
  1117.                         }
  1118.                        
  1119.                        
  1120.                         echo "\n".'RAM: ';
  1121.                         switch ($this->ram->status)
  1122.                         {
  1123.                                 case 0:
  1124.                                         echo 'Up, ';
  1125.                                         echo $this->ram->used.' MB';
  1126.                                         break;
  1127.                                 case 1:
  1128.                                         echo 'Warning, ';
  1129.                                         echo $this->ram->used.' MB';
  1130.                                         break;
  1131.                                 case 2:
  1132.                                         echo 'Down, ';
  1133.                                         echo $this->ram->used.' MB';
  1134.                                         break;
  1135.                                 default:
  1136.                                         echo 'Warning, ';
  1137.                                         echo 'Error';
  1138.                                         break;
  1139.                         }
  1140.                        
  1141.                         echo "\n".'Storage: ';
  1142.                         switch ($this->fs->status)
  1143.                         {
  1144.                                 case 0:
  1145.                                         echo 'Up, ';
  1146.                                         echo ($this->fs->used >= 1024) ? round($this->fs->used/1024, 2).' GB' : $this->fs->used.' MB';
  1147.                                         break;
  1148.                                 case 1:
  1149.                                         echo 'Warning, ';
  1150.                                         echo ($this->fs->used >= 1024) ? round($this->fs->used/1024, 2).' GB' : $this->fs->used.' MB';
  1151.                                         break;
  1152.                                 case 2:
  1153.                                         echo 'Down, ';
  1154.                                         echo ($this->fs->used >= 1024) ? round($this->fs->used/1024, 2).' GB' : $this->fs->used.' MB';
  1155.                                         break;
  1156.                                 default:
  1157.                                         echo 'Warning, ';
  1158.                                         echo 'Error';
  1159.                                         break;
  1160.                         }
  1161.  
  1162.                         echo "\n".'Processes: '.$this->processes;
  1163.                         echo "\n".'Uptime: '.$this->uptime;
  1164.                         echo "\n".'Users Logged In: '.$this->users;
  1165.                         echo "\n".'TCP Connections: '.$this->conns->tcp;
  1166.                         echo "\n".'UDP Connections: '.$this->conns->udp;
  1167.                        
  1168.                         echo "\n\n".'=Sites=';
  1169.                         for($i=0; $i < $this->sites->count; $i++)
  1170.                         {
  1171.                                 echo "\n".$this->site[$i]->name.': ';
  1172.                                
  1173.                                 switch ($this->site[$i]->status)
  1174.                                 {
  1175.                                         case 0:
  1176.                                                 echo 'Up, ';
  1177.                                                 echo $this->site[$i]->http->status.' ('.$this->site[$i]->http->desc.')';
  1178.                                             break;
  1179.                                         case 1:
  1180.                                                 echo 'Down, ';
  1181.                                             echo $this->site[$i]->http->status.' ('.$this->site[$i]->http->desc.')';
  1182.                                             break;
  1183.                                         default:
  1184.                                                 echo 'Warning, ';
  1185.                                                 echo 'Error';
  1186.                                                 break;
  1187.                                 }
  1188.                         }
  1189.                 $this->email->output = ob_get_clean();
  1190.         }
  1191.        
  1192.         private function generateAlert()
  1193.         {      
  1194.                 ob_start();
  1195.                         for($i=0; $i < $this->services->count; $i++)
  1196.                         {
  1197.                                 echo ($this->service[$i]->status->listen >= 1 || $this->service[$i]->status->internal >= 1 || $this->service[$i]->status->external >= 2 || $this->service[$i]->status->listen == -1 || $this->service[$i]->status->internal == -1 || $this->service[$i]->status->external == -1) ? '>'.$this->service[$i]->name : null;
  1198.                                
  1199.                                 echo ($this->service[$i]->status->listen >= 1 || $this->service[$i]->status->listen == -1) ? '|Port:' : null;
  1200.                                 switch ($this->service[$i]->status->listen)
  1201.                                 {
  1202.                                         case 0:
  1203.                                             break;
  1204.                                         case 1:
  1205.                                                 echo 'Down:';
  1206.                                             echo 'NotListening ('.$this->service[$i]->port.');';
  1207.                                                 $this->email->alert->trigger = 1;
  1208.                                             break;
  1209.                                         default:
  1210.                                                 echo 'Warning:';
  1211.                                                 echo 'N/A;';
  1212.                                                 $this->email->warning->trigger = 1;
  1213.                                                 break;
  1214.                                 }
  1215.                                
  1216.                                 echo ($this->service[$i]->status->internal >= 1 || $this->service[$i]->status->internal == -1) ? '|LocalConn:' : null;
  1217.                                 switch ($this->service[$i]->status->internal)
  1218.                                 {
  1219.                                         case 0:
  1220.                                             break;
  1221.                                         case 1:
  1222.                                                 echo 'Down:';
  1223.                                             echo 'Rejecting;';
  1224.                                                 $this->email->alert->trigger = 1;
  1225.                                             break;
  1226.                                         default:
  1227.                                                 echo 'Warning:';
  1228.                                                 echo 'N/A;';
  1229.                                                 $this->email->warning->trigger = 1;
  1230.                                                 break;
  1231.                                 }
  1232.                                
  1233.                                 echo ($this->service[$i]->status->external >= 2 || $this->service[$i]->status->external == -1) ? '|ExtConn:' : null;
  1234.                                 switch ($this->service[$i]->status->external)
  1235.                                 {
  1236.                                         case 0:
  1237.                                             break;
  1238.                                         case 1:
  1239.                                             break;
  1240.                                         case 2:
  1241.                                                 echo 'Down:';
  1242.                                                 echo 'Rejecting,Misconfigured;';
  1243.                                                 $this->email->alert->trigger = 1;
  1244.                                                 break;
  1245.                                         case 3:
  1246.                                                 echo 'Down:';
  1247.                                                 echo 'Accepting,Misconfigured;';
  1248.                                                 $this->email->alert->trigger = 1;
  1249.                                                 break;
  1250.                                         default:
  1251.                                                 echo 'Warning:';
  1252.                                                 echo 'N/A;';
  1253.                                                 $this->email->warning->trigger = 1;
  1254.                                                 break;
  1255.                                 }
  1256.                                
  1257.                         }
  1258.                        
  1259.                         echo ($this->load->status >= 1 | $this->load->status == -1) ? '|Loads:' : null;
  1260.                         switch ($this->load->status)
  1261.                         {
  1262.                                 case 0:
  1263.                                         break;
  1264.                                 case 1:
  1265.                                         echo 'Warning:';
  1266.                                         echo $this->loads[1].' '.$this->loads[2].' '.$this->loads[3].';';
  1267.                                         $this->email->warning->trigger = 1;
  1268.                                         break;
  1269.                                 case 2:
  1270.                                         echo 'Down:';
  1271.                                         echo $this->loads[1].' '.$this->loads[2].' '.$this->loads[3].';';
  1272.                                         $this->email->alert->trigger = 1;
  1273.                                         break;
  1274.                                 default:
  1275.                                         echo 'Warning:';
  1276.                                         echo 'Error;';
  1277.                                         $this->email->warning->trigger = 1;
  1278.                                         break;
  1279.                         }
  1280.                        
  1281.                         echo ($this->ram->status >= 1 || $this->ram->status == -1) ? '|RAM:' : null;
  1282.                         switch ($this->ram->status)
  1283.                         {
  1284.                                 case 0:
  1285.                                         break;
  1286.                                 case 1:
  1287.                                         echo 'Warning:';
  1288.                                         echo $this->ram->used;
  1289.                                         $this->email->warning->trigger = 1;
  1290.                                         break;
  1291.                                 case 2:
  1292.                                         echo 'Down:';
  1293.                                         echo $this->ram->used;
  1294.                                         $this->email->alert->trigger = 1;
  1295.                                         break;
  1296.                                 default:
  1297.                                         echo 'Warning:';
  1298.                                         echo 'Error;';
  1299.                                         $this->email->warning->trigger = 1;
  1300.                                         break;
  1301.                         }
  1302.                         echo ($this->ram->status >= 1 || $this->ram->status == -1) ? 'MB;' : null;
  1303.                        
  1304.                         echo ($this->fs->status >= 1 || $this->fs->status == -1) ? '|FS:' : null;
  1305.                         switch ($this->fs->status)
  1306.                         {
  1307.                                 case 0:
  1308.                                         break;
  1309.                                 case 1:
  1310.                                         echo 'Warning:';
  1311.                                         echo ($this->fs->used >= 1024) ? round($this->fs->used/1024, 2).'GB;' : $this->fs->used.'MB;';
  1312.                                         $this->email->warning->trigger = 1;
  1313.                                         break;
  1314.                                 case 2:
  1315.                                         echo 'Down:';
  1316.                                         echo ($this->fs->used >= 1024) ? round($this->fs->used/1024, 2).'GB;' : $this->fs->used.'MB;';
  1317.                                         $this->email->alert->trigger = 1;
  1318.                                         break;
  1319.                                 default:
  1320.                                         echo 'Warning:';
  1321.                                         echo 'Error;';
  1322.                                         $this->email->warning->trigger = 1;
  1323.                                         break;
  1324.                         }
  1325.                        
  1326.                         for($i=0; $i < $this->sites->count; $i++)
  1327.                         {
  1328.                                 echo ($this->site[$i]->status >= 1 || $this->site[$i]->status == -1) ? '|'.$this->site[$i]->name.':' : null;
  1329.                                
  1330.                                 switch ($this->site[$i]->status)
  1331.                                 {
  1332.                                         case 0:
  1333.                                             break;
  1334.                                         case 1:
  1335.                                                 echo 'Down:';
  1336.                                             echo $this->site[$i]->http->status.'('.$this->site[$i]->http->desc.');';
  1337.                                                 $this->email->alert->trigger = 1;
  1338.                                             break;
  1339.                                         default:
  1340.                                                 echo 'Warning:';
  1341.                                                 echo 'Error;';
  1342.                                                 $this->email->warning->trigger = 1;
  1343.                                                 break;
  1344.                                 }
  1345.                         }
  1346.                 $this->email->output = ob_get_clean();
  1347.         }
  1348.  
  1349. }
  1350.  
  1351. //If running cron job (either through CLI or Web), and type isn't update, send email if needed
  1352. if((isset($argv) && $argv[1] != 'update') || (isset($_GET['cron']) && $_GET['cron']))
  1353. {
  1354.         $email = new EmailOutput();
  1355.         $email->send();
  1356. }
  1357. //If running cron job (through CLI), and type is update, check for updates, and send email if needed
  1358. elseif(isset($argv) && $argv[1] == 'update')
  1359. {
  1360.         $email = new EmailOutput();
  1361.         $email->update();
  1362. }
  1363. //If checking for updates through AJAX
  1364. elseif(isset($_GET['update']) && $_GET['update'])
  1365. {
  1366.         $browser = new BrowserOutput();
  1367.         $browser->update();
  1368. }
  1369. //If accessing MOSMC directly from browser
  1370. else
  1371. {
  1372.         $browser = new BrowserOutput();
  1373.         $browser->render();
  1374. }
  1375.  
  1376. ?>
View raw paste Parent paste by: The_PHP_Jedi - Child paste by: The_PHP_Jedi Reply