home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / sysinfo.class.inc < prev    next >
Text File  |  2004-03-08  |  15KB  |  395 lines

  1. <?php
  2. //
  3. // phpSysInfo - A PHP System Information Script
  4. // http://phpsysinfo.sourceforge.net/
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. //
  20. // $Id: sysinfo.class.inc,v 1.1.1.1 2003/07/03 15:03:27 mschering Exp $
  21. //
  22.  
  23. class sysinfo {
  24.     // get our apache SERVER_NAME or vhost
  25.     function vhostname () {
  26.         if (! ($result = getenv('SERVER_NAME'))) {
  27.             $result = 'N.A.';
  28.         }
  29.         return $result;
  30.     }
  31.  
  32.     // get our canonical hostname
  33.     function chostname () {
  34.         if ($fp = fopen('/proc/sys/kernel/hostname','r')) {
  35.             $result = trim(fgets($fp, 4096));
  36.             fclose($fp);
  37.             $result = gethostbyaddr(gethostbyname($result));
  38.         } else {
  39.             $result = 'N.A.';
  40.         }
  41.         return $result;
  42.     }
  43.  
  44.     // get the IP address of our canonical hostname
  45.     function ip_addr () {
  46.         if (!($result = getenv('SERVER_ADDR'))) {
  47.             $result = gethostbyname($this->chostname());
  48.         }
  49.         return $result;
  50.     }
  51.  
  52.     function kernel () {
  53.         if ($fd = fopen('/proc/version', 'r')) {
  54.             $buf = fgets($fd, 4096);
  55.             fclose($fd);
  56.  
  57.             if (preg_match('/version (.*?) /', $buf, $ar_buf)) {
  58.                 $result = $ar_buf[1];
  59.  
  60.                 if (preg_match('/SMP/', $buf)) {
  61.                     $result .= ' (SMP)';
  62.                 }
  63.             } else {
  64.                 $result = 'N.A.';
  65.             }
  66.         } else {
  67.             $result = 'N.A.';
  68.         }
  69.         return $result;
  70.     }
  71.  
  72.     function uptime () {
  73.         global $text;
  74.         $fd = fopen('/proc/uptime', 'r');
  75.         $ar_buf = split(' ', fgets($fd, 4096));
  76.         fclose($fd);
  77.  
  78.         return trim($ar_buf[0]);
  79.     }
  80.  
  81.     function users () {
  82.         $who = split('=', execute_program('who', '-q'));
  83.         $result = $who[1];
  84.         return $result;
  85.     }
  86.  
  87.     function loadavg () {
  88.         if ($fd = fopen('/proc/loadavg', 'r')) {
  89.             $results = split(' ', fgets($fd, 4096));
  90.             fclose($fd);
  91.         } else {
  92.             $results = array('N.A.','N.A.','N.A.');
  93.         }
  94.         return $results;
  95.     }
  96.  
  97.     function cpu_info () {
  98.         $results = array();
  99.         $results['cpus'] = 0;
  100.         $results['mhz'] = 0;
  101.         $results['bogomips'] = 0;
  102.         $ar_buf = array();
  103.  
  104.         if ($fd = fopen('/proc/cpuinfo', 'r')) {
  105.             while ($buf = fgets($fd, 4096)) {
  106.                 @list($key, $value) = preg_split('/\s+:\s+/', trim($buf), 2);
  107.  
  108.                 // All of the tags here are highly architecture dependant.
  109.                 // the only way I could reconstruct them for machines I don't
  110.                 // have is to browse the kernel source.  So if your arch isn't
  111.                 // supported, tell me you want it written in.
  112.                 switch ($key) {
  113.                     case 'model name':
  114.                         $results['model'] = $value;
  115.                         break;
  116.                     case 'cpu MHz':
  117.                         $results['mhz'] = sprintf('%.2f', $value);
  118.                         break;
  119.                     case 'cycle frequency [Hz]': // For Alpha arch - 2.2.x
  120.                         $results['mhz'] = sprintf('%.2f', $value/1000000);
  121.                         break;
  122.                     case 'clock': // For PPC arch (damn borked POS)
  123.                         $results['mhz'] = sprintf('%.2f', $value);
  124.                         break;
  125.                     case 'cpu': // For PPC arch (damn borked POS)
  126.                         $results['model'] = $value;
  127.                         break;
  128.                     case 'revision': // For PPC arch (damn borked POS)
  129.                         $results['model'] .= ' ( rev: ' . $value . ')';
  130.                         break;
  131.                     case 'cpu model': // For Alpha arch - 2.2.x
  132.                         $results['model'] .= ' (' . $value . ')';
  133.                         break;
  134.                     case 'cache size':
  135.                         $results['cache'] = $value;
  136.                         break;
  137.                     case 'bogomips':
  138.                         $results['bogomips'] += $value;
  139.                         break;
  140.                     case 'BogoMIPS': // For alpha arch - 2.2.x
  141.                         $results['bogomips'] += $value;
  142.                         break;
  143.                     case 'BogoMips': // For sparc arch
  144.                         $results['bogomips'] += $value;
  145.                         break;
  146.                     case 'cpus detected': // For Alpha arch - 2.2.x
  147.                         $results['cpus'] += 1;
  148.                         break;
  149.                     case 'system type': // Alpha arch - 2.2.x
  150.                         $results['model'] .= ', ' . $value . ' ';
  151.                         break;
  152.                     case 'platform string': // Alpha arch - 2.2.x
  153.                         $results['model'] .= ' (' . $value . ')';
  154.                         break;
  155.                     case 'processor':
  156.                         $results['cpus'] += 1;
  157.                         break;
  158.                 }
  159.             }
  160.             fclose($fd);
  161.         }
  162.  
  163.         $keys = array_keys($results);
  164.         $keys2be = array('model', 'mhz', 'cache', 'bogomips', 'cpus');
  165.  
  166.         while ($ar_buf = each($keys2be)) {
  167.             if (! in_array($ar_buf[1], $keys)) {
  168.                 $results[$ar_buf[1]] = 'N.A.';
  169.             }
  170.         }
  171.         return $results;
  172.  
  173.     }
  174.  
  175.     function pci () {
  176.         $results = array();
  177.         $device = 0;
  178.         if ($fd = fopen('/proc/pci', 'r')) {
  179.             while ($buf = fgets($fd, 4096)) {
  180.                 if (preg_match('/Bus/', $buf)) {
  181.                     $device = 1;
  182.                     continue;
  183.                 }
  184.  
  185.                 if ($device) {
  186.                     list($key, $value) = split(': ', $buf, 2);
  187.  
  188.                     if (!preg_match('/bridge/i', $key) && !preg_match('/USB/i', $key)) {
  189.                         $results[] = preg_replace('/\([^\)]+\)\.$/', '', trim($value));
  190.                     }
  191.                     $device = 0;
  192.                 }
  193.             }
  194.         }
  195.         return $results;
  196.     }
  197.  
  198.     function ide () {
  199.         $results = array();
  200.  
  201.         $handle = opendir('/proc/ide');
  202.  
  203.         while ($file = readdir($handle)) {
  204.             if (preg_match('/^hd/', $file)) {
  205.                 $results[$file] = array();
  206.  
  207.                 // Check if device is CD-ROM (CD-ROM capacity shows as 1024 GB)
  208.                 if ($fd = fopen("/proc/ide/$file/media", 'r')) {
  209.                     $results[$file]['media'] = trim(fgets($fd, 4096));
  210.                     if ($results[$file]['media'] == 'disk') {
  211.                         $results[$file]['media'] = 'Hard Disk';
  212.                     }
  213.  
  214.                     if ($results[$file]['media'] == 'cdrom') {
  215.                         $results[$file]['media'] = 'CD-ROM';
  216.                     }
  217.                     fclose($fd);
  218.                 }
  219.  
  220.                 if ($fd = fopen("/proc/ide/$file/model", 'r')) {
  221.                     $results[$file]['model'] = trim(fgets($fd, 4096));
  222.                     if (preg_match('/WDC/', $results[$file]['model'])) {
  223.                         $results[$file]['manufacture'] = 'Western Digital';
  224.  
  225.                     } elseif (preg_match('/IBM/', $results[$file]['model'])) {
  226.                         $results[$file]['manufacture'] = 'IBM';
  227.  
  228.                     } elseif (preg_match('/FUJITSU/', $results[$file]['model'])) {
  229.                         $results[$file]['manufacture'] = 'Fujitsu';
  230.  
  231.                     } else {
  232.                         $results[$file]['manufacture'] = 'Unknown';
  233.                     }
  234.  
  235.                     fclose($fd);
  236.                 }
  237.  
  238.                 if ($fd = @fopen("/proc/ide/$file/capacity", 'r')) {
  239.                     $results[$file]['capacity'] = trim(fgets($fd, 4096));
  240.                     if ($results[$file]['media'] == 'CD-ROM') {
  241.                         unset($results[$file]['capacity']);
  242.                     }
  243.                     fclose($fd);
  244.                 }
  245.             }
  246.         }
  247.         closedir($handle);
  248.  
  249.         return $results;
  250.     }
  251.  
  252.     function scsi () {
  253.         $results    = array();
  254.         $dev_vendor = '';
  255.         $dev_model  = '';
  256.         $dev_rev    = '';
  257.         $dev_type   = '';
  258.         $get_type = false;
  259.         if ($fd = @fopen('/proc/scsi/scsi', 'r')) {
  260.             while ($buf = fgets($fd, 4096)) {
  261.                 if (preg_match('/Vendor/', $buf)) {
  262.                     preg_match('/Vendor: (.*) Model: (.*) Rev: (.*)/i', $buf, $dev);
  263.                     list($key, $value) = split(': ', $buf, 2);
  264.                     $dev_str  = $value;
  265.                     $get_type = 1;
  266.                     continue;
  267.                 }
  268.  
  269.                 if ($get_type) {
  270.                     preg_match('/Type:\s+(\S+)/i', $buf, $dev_type);
  271.                     $results[] = "$dev[1] $dev[2] ( $dev_type[1] )";
  272.                     $get_type = 0;
  273.                 }
  274.             }
  275.         }
  276.         return $results;
  277.     }
  278.  
  279.     function network () {
  280.         $results = array();
  281.  
  282.         if ($fd = fopen('/proc/net/dev', 'r')) {
  283.             while ($buf = fgets($fd, 4096)) {
  284.                 if (preg_match('/:/', $buf)) {
  285.                     list($dev_name, $stats_list) = preg_split('/:/', $buf, 2);
  286.                     $stats = preg_split('/\s+/', trim($stats_list));
  287.                     $results[$dev_name] = array();
  288.  
  289.                     $results[$dev_name]['rx_bytes']   = $stats[0];
  290.                     $results[$dev_name]['rx_packets'] = $stats[1];
  291.                     $results[$dev_name]['rx_errs']    = $stats[2];
  292.                     $results[$dev_name]['rx_drop']    = $stats[3];
  293.  
  294.                     $results[$dev_name]['tx_bytes']   = $stats[8];
  295.                     $results[$dev_name]['tx_packets'] = $stats[9];
  296.                     $results[$dev_name]['tx_errs']    = $stats[10];
  297.                     $results[$dev_name]['tx_drop']    = $stats[11];
  298.  
  299.                     $results[$dev_name]['errs']       = $stats[2] + $stats[10];
  300.                     $results[$dev_name]['drop']       = $stats[3] + $stats[11];
  301.                 }
  302.             }
  303.         }
  304.         return $results;
  305.     }
  306.  
  307.     function memory () {
  308.         if ($fd = fopen('/proc/meminfo', 'r')) {
  309.             while ($buf = fgets($fd, 4096)) {
  310.                 if (preg_match('/Mem:\s+(.*)$/', $buf, $ar_buf)) {
  311.                     $ar_buf = preg_split('/\s+/', $ar_buf[1], 6);
  312.  
  313.                     $results['ram'] = array();
  314.  
  315.                     $results['ram']['total']   = $ar_buf[0] / 1024;
  316.                     $results['ram']['used']    = $ar_buf[1] / 1024;
  317.                     $results['ram']['free']    = $ar_buf[2] / 1024;
  318.                     $results['ram']['shared']  = $ar_buf[3] / 1024;
  319.                     $results['ram']['buffers'] = $ar_buf[4] / 1024;
  320.                     $results['ram']['cached']  = $ar_buf[5] / 1024;
  321.  
  322.                     // I don't like this since buffers and cache really aren't
  323.                     // 'used' per say, but I get too many emails about it.
  324.                     $results['ram']['t_used']  = $results['ram']['used'];
  325.                     $results['ram']['t_free']  = $results['ram']['total'] - $results['ram']['t_used'];
  326.                     $results['ram']['percent'] = round(($results['ram']['t_used'] * 100) / $results['ram']['total']);
  327.                 }
  328.  
  329.                 if (preg_match('/Swap:\s+(.*)$/', $buf, $ar_buf)) {
  330.                     $ar_buf = preg_split('/\s+/', $ar_buf[1], 3);
  331.  
  332.                     $results['swap'] = array();
  333.  
  334.                     $results['swap']['total']   = $ar_buf[0] / 1024;
  335.                     $results['swap']['used']    = $ar_buf[1] / 1024;
  336.                     $results['swap']['free']    = $ar_buf[2] / 1024;
  337.                     $results['swap']['percent'] = round(($ar_buf[1] * 100) / $ar_buf[0]);
  338.  
  339.                     // Get info on individual swap files
  340.                     $swaps = @strval(file ('/proc/swaps'));
  341.                     $swapdevs = split("\n", $swaps);
  342.  
  343.                     for ($i = 1; $i < (sizeof($swapdevs) - 1); $i++) {
  344.                         $ar_buf = preg_split('/\s+/', $swapdevs[$i], 6);
  345.  
  346.                         $results['devswap'][$i - 1] = array();
  347.                         $results['devswap'][$i - 1]['dev']     = $ar_buf[0];
  348.                         $results['devswap'][$i - 1]['total']   = $ar_buf[2];
  349.                         $results['devswap'][$i - 1]['used']    = $ar_buf[3];
  350.                         $results['devswap'][$i - 1]['free']    = ($results['devswap'][$i - 1]['total'] - $results['devswap'][$i - 1]['used']);
  351.                         $results['devswap'][$i - 1]['percent'] = round(($ar_buf[3] * 100) / $ar_buf[2]);
  352.                     }
  353.                     break;
  354.                 }
  355.             }
  356.             fclose($fd);
  357.         } else {
  358.             $results['ram'] = array();
  359.             $results['swap'] = array();
  360.             $results['devswap'] = array();
  361.         }
  362.         return $results;
  363.     }
  364.  
  365.     function filesystems () {
  366.         $df = execute_program('df', '-kP');
  367.         $mounts = split("\n", $df);
  368.         $fstype = array();
  369.  
  370.         if ($fd = fopen('/proc/mounts', 'r')) {
  371.             while ($buf = fgets($fd, 4096)) {
  372.                 list($dev, $mpoint, $type) = preg_split('/\s+/', trim($buf), 4);
  373.                 $fstype[$mpoint] = $type;
  374.                 $fsdev[$dev] = $type;
  375.             }
  376.             fclose($fd);
  377.         }
  378.  
  379.         for ($i = 1; $i < sizeof($mounts); $i++) {
  380.             $ar_buf = preg_split('/\s+/', $mounts[$i], 6);
  381.  
  382.             $results[$i - 1] = array();
  383.  
  384.             $results[$i - 1]['disk'] = $ar_buf[0];
  385.             $results[$i - 1]['size'] = $ar_buf[1];
  386.             $results[$i - 1]['used'] = $ar_buf[2];
  387.             $results[$i - 1]['free'] = $ar_buf[3];
  388.             $results[$i - 1]['percent'] = round(($results[$i - 1]['used'] * 100) / $results[$i - 1]['size']) . '%';
  389.             $results[$i - 1]['mount'] = $ar_buf[5];
  390.             ($fstype[$ar_buf[5]]) ? $results[$i - 1]['fstype'] = $fstype[$ar_buf[5]] : $results[$i - 1]['fstype'] = $fsdev[$ar_buf[0]];
  391.         }
  392.         return $results;
  393.     }
  394. }
  395.