Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 11th, 2012  |  syntax: None  |  size: 5.62 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. class server_status {
  3.    
  4.     public $servers = array();
  5.     public $server;
  6.     public $settings = array();
  7.    
  8.     /**
  9.     * Starts the Process of display the server status
  10.     *
  11.     * Gets the parameters passed and assignes them to variables
  12.     *
  13.     * @param void
  14.     * @return NULL
  15.     */
  16.     public function start()
  17.     {
  18.         $_GET['server'] = strtolower($_GET['server']);
  19.         $this->server = array_key_exists($_GET['server'], $this->servers) ? $this->servers[$_GET['server']] : die('SERVER NOT FOUND');
  20.         $this->server['name'] = $_GET['server']; //Just so it's a little easier giving it a name :)
  21.         if($_GET['text']) $this->settings['text'] = $_GET['text'];
  22.        
  23.         $this->display();
  24.     }
  25.    
  26.     /**
  27.     * Makes a connection to the server to see if it is online and responding
  28.     *
  29.     * @param void
  30.     * @return boolean 1 or 0
  31.     */
  32.     public function checkServer()
  33.     {
  34.         if(!$this->server['offline']){
  35.             $check = @fsockopen($this->server['ip'], $this->server['port'], $errnum, $errstr, 2) ? 1 : 0;
  36.         }else{
  37.             $check = 0;
  38.         }
  39.         return $check;
  40.     }
  41.    
  42.     /**
  43.     * Gets the status of the server from the cache, or if expired re-makes connection and cache's again
  44.     *
  45.     * @param void
  46.     * @return boolean 1 or 0
  47.     */
  48.     private function getStatus()
  49.     {
  50.         $fullpath = $this->settings['fullpath'];
  51.         $cachefile = $this->server['offline'] == FALSE ? $fullpath."cache/".$this->server['name'].".txt" : $fullpath.'cache/offline.txt';
  52.         $cachetime = 5 * 60; // 5 minutes
  53.         // Serve from the cache if it is younger than $cachetime
  54.         if ((file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) or $this->server['offline'])
  55.         {
  56.             $this->logger('Loading from cache');
  57.             $this->logger('Loading File', $cachefile);
  58.             $file = fopen($cachefile, 'r');
  59.             $contents = fread($file, filesize($cachefile));
  60.             $this->logger('File contents', $contents);
  61.             $json = json_decode($contents, 1);
  62.             $online = $json['online'] ? 1 : 0;
  63.  
  64.             $this->logger('JSON Array', print_r($json, true));
  65.             $this->logger('Online Status', $online);
  66.             $this->logger("Cached",date('jS F Y H:i', filemtime($cachefile)));
  67.         }else{
  68.             $this->logger("Writing Cache File");
  69.  
  70.             $arr = array(); //ARRRGH PIRATES
  71.  
  72.             $check = $this->checkServer();
  73.             $online = $arr['online'] = $check;
  74.            
  75.             $this->logger('Online Status', $online);
  76.             $this->logger("Array List", print_r($arr, true));
  77.             $this->logger("Json encoded", json_encode($arr));
  78.            
  79.             if (is_writable($cachefile) or true) {
  80.                 if (!$handle = fopen($cachefile, 'w+')) {
  81.                     $this->logger("Cannot open file ($cachefile)");
  82.                     exit;
  83.                 }
  84.                 if (fwrite($handle, json_encode($arr)) === FALSE) {
  85.                     $this->logger("Cannot write to file ($cachefile)");
  86.                     exit;
  87.                 }
  88.                 $this->logger("Success, wrote ($cachefile) to file ($cachefile)");
  89.                 fclose($handle);
  90.             } else {
  91.                 $this->logger("The file $cachefile is not writable");
  92.             }
  93.         }
  94.         return $online;
  95.     }
  96.    
  97.     /**
  98.     * Displays the image
  99.     *
  100.     * $param void
  101.     * @output image png or text
  102.     * @return NULL
  103.     */
  104.     private function display()
  105.     {
  106.         $online = $this->getStatus();
  107.        
  108.         $this->logger('Display Online status', $online);
  109.        
  110.         //OUTPUT TIME BABY!!
  111.         header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  112.         $time = date('r', time() + 300); //Time in 5 minutes from now
  113.         header("Expires: $time");
  114.         //header("Expires: Tue, 23 Jul 1994 08:00:00 GMT"); // Date in the past
  115.         if(!$this->settings['text']){
  116.             header("Content-type: image/png");
  117.             if($online){
  118.                 readfile($this->settings['fullpath'].$this->server['name'].'-on.png');  
  119.             }else{
  120.                 readfile($this->settings['fullpath'].$this->server['name'].'-off.png');
  121.             }
  122.         }else{
  123.             if($online){
  124.                 $color = "green";  
  125.             }else{
  126.                 $color = "red";
  127.             }
  128.             $text = (!$online) ? "OFFLINE": "ONLINE";
  129.             echo "<font style='color:$color'>$text</font>";
  130.         }
  131.     }
  132.    
  133.     /**
  134.     * When the debug parameter is true when loading the &text=true variable, it will output extra information
  135.     *
  136.     * @param string $item
  137.     * @param string $text
  138.     * @return NULL
  139.     */
  140.     private function logger($item, $text = '')
  141.     {
  142.         if($this->settings['debug'] == TRUE)
  143.         {
  144.             echo "$item : $text <br /><br />\n\n";
  145.         }
  146.     }
  147. }
  148.  
  149.     $status = new server_status();
  150.     $status->settings['debug']      = FALSE; //Returns extra informations only viewable if &text=true
  151.     $status->settings['fullpath']   = ""; //full path to this script including /
  152.    
  153.     /**
  154.     * List servers in an array
  155.     *
  156.     * Format is: 'SERVER NAME' => array('ip' => '', 'port' => ''),
  157.     * There is an additional " 'offline' => true " if you would like to be able to display a server that is not online or you want to force it to show offline
  158.     *
  159.     * NOTE: you will need images corresponding to <server_name>-on.png and <server_name>-off.png in the script path.
  160.     */
  161.     $status->servers = array(
  162.         'server_name'   => array('ip' => '', 'port' => ''),
  163.         'server_2'   => array('ip' => '', 'port' => '')
  164.     );
  165.    
  166.     $status->start();
  167. ?>