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