[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpsysinfo/includes/ -> system_functions.php (source)

   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: system_functions.php 17475 2004-11-28 19:01:45Z  $
  21  
  22  
  23  // So that stupid warnings do not appear when we stats files that do not exist.
  24  error_reporting(5);
  25  
  26  
  27  // print out the bar graph
  28  function create_bargraph ($percent, $a, $b)
  29  {
  30      if ($percent == 0) { 
  31          return '<img height="16" src="templates/' . TEMPLATE_SET . '/images/bar_left.gif" alt="">'
  32                . '<img src="templates/' . TEMPLATE_SET . '/images/bar_middle.gif" height="16" width="1" alt="">'
  33                . '<img src="templates/' . TEMPLATE_SET . '/images/bar_right.gif" height="16" alt="">';
  34      } else if ($percent < 90) {
  35          return '<img height="16" src="templates/' . TEMPLATE_SET . '/images/bar_left.gif" alt="">'
  36                . '<img src="templates/' . TEMPLATE_SET . '/images/bar_middle.gif" height="16" width="' . ($a * $b) . '" alt="">'
  37                . '<img height="16" src="templates/' . TEMPLATE_SET . '/images/bar_right.gif" alt="">';
  38      } else {
  39          return '<img height="16" src="templates/' . TEMPLATE_SET . '/images/redbar_left.gif" alt="">'
  40               . '<img src="templates/' . TEMPLATE_SET . '/images/redbar_middle.gif" height="16" width="' . ($a * $b) . '" alt="">'
  41               . '<img height="16" src="templates/' . TEMPLATE_SET . '/images/redbar_right.gif" alt="">';
  42      }
  43  }
  44  
  45  
  46  // Execute a system function.  Do path checking
  47  // return a trim()'d result.
  48  function execute_program ($program, $args)
  49  {
  50      $path = array( '/bin/', '/sbin/', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
  51      $buffer = '';
  52  
  53      while ($this_path = current($path)) {
  54          if (is_executable("$this_path/$program")) {
  55              if ($fp = popen("$this_path/$program $args", 'r')) {
  56                  while (!feof($fp)) {
  57                      $buffer .= fgets($fp, 4096);
  58                  }
  59                  return trim($buffer);
  60              }
  61          }
  62          next($path);
  63      }
  64  }
  65  
  66  
  67  // function that emulate the compat_array_keys function of PHP4
  68  // for PHP3 compatability
  69  function compat_array_keys ($arr)
  70  {
  71          $result = array();
  72  
  73          while (list($key, $val) = each($arr)) {
  74              $result[] = $key;
  75          }
  76          return $result;
  77  }
  78  
  79  
  80  // function that emulates the compat_in_array function of PHP4
  81  // for PHP3 compatability
  82  function compat_in_array ($value, $arr)
  83  {
  84      while (list($key,$val) = each($arr)) {
  85          if ($value == $val) {
  86              return true;
  87          }
  88      }
  89      return false;
  90  }
  91  
  92  
  93  // A helper function, when passed a number representing KB,
  94  // and optionally the number of decimal places required,
  95  // it returns a formated number string, with unit identifier.
  96  function format_bytesize ($kbytes, $dec_places = 2)
  97  {
  98      global $text;
  99      if ($kbytes > 1048576) {
 100          $result  = sprintf('%.' . $dec_places . 'f', $kbytes / 1048576);
 101          $result .= '&nbsp;'.$text['gb'];
 102      } elseif ($kbytes > 1024) {
 103          $result  = sprintf('%.' . $dec_places . 'f', $kbytes / 1024);
 104          $result .= '&nbsp;'.$text['mb'];
 105      } else {
 106          $result  = sprintf('%.' . $dec_places . 'f', $kbytes);
 107          $result .= '&nbsp;'.$text['kb'];
 108      }  
 109      return $result;
 110  }
 111  
 112  
 113  // Returns the virtual hostname accessed.
 114  function sys_vhostname ()
 115  {
 116      if (!($result = getenv('SERVER_NAME'))) {
 117          $result = 'N.A.';
 118      }
 119      return $result;
 120  }
 121  
 122  
 123  // Returns the Cannonical machine hostname.
 124  function sys_chostname ()
 125  {
 126      if ($fp = fopen('/proc/sys/kernel/hostname','r')) {
 127          $result = trim(fgets($fp, 4096));
 128          fclose($fp);
 129          $result = gethostbyaddr(gethostbyname($result));
 130      } else {
 131          $result = 'N.A.';
 132      }
 133      return $result;
 134  }
 135  
 136  
 137  // Returns the IP address that the request was made on.
 138  function sys_ip_addr ()
 139  {
 140      if (!($result = getenv('SERVER_ADDR'))) {
 141          $result = gethostbyname(sys_chostname());
 142      }
 143      return $result;
 144  } 
 145  
 146  
 147  // Returns an array of all meaningful devices 
 148  // on the PCI bus.
 149  function sys_pcibus ()
 150  {
 151      $results = array();
 152  
 153      if ($fd = fopen('/proc/pci', 'r')) {
 154          while ($buf = fgets($fd, 4096)) {
 155              if (preg_match('/Bus/', $buf)) {
 156                  $device = 1;
 157                  continue;
 158              } 
 159  
 160              if ($device) { 
 161                  list($key, $value) = split(': ', $buf, 2);
 162  
 163                  if (!preg_match('/bridge/i', $key) && !preg_match('/USB/i', $key)) {
 164                      $results[] = preg_replace('/\([^\)]+\)\.$/', '', trim($value));
 165                  }
 166                  $device = 0;
 167              }
 168          }
 169      } 
 170      return $results;
 171  }
 172  
 173  
 174  // Returns an array of all ide devices attached
 175  // to the system, as determined by the aliased
 176  // shortcuts in /proc/ide
 177  function sys_idebus ()
 178  {
 179      $results = array();
 180  
 181      $handle = opendir('/proc/ide');
 182  
 183      while ($file = readdir($handle)) {
 184          if (preg_match('/^hd/', $file)) { 
 185              $results[$file] = array();
 186              
 187              // Check if device is CD-ROM (CD-ROM capacity shows as 1024 GB)
 188              if ($fd = fopen("/proc/ide/$file/media", 'r')) {
 189                  $results[$file]['media'] = trim(fgets($fd, 4096));
 190                  if ($results[$file]['media'] == 'disk') { $results[$file]['media'] = 'Hard Disk'; }
 191                  if ($results[$file]['media'] == 'cdrom') { $results[$file]['media'] = 'CD-ROM'; }
 192                  fclose($fd);
 193              }
 194  
 195              if ($fd = fopen("/proc/ide/$file/model", 'r')) {
 196                  $results[$file]['model'] = trim(fgets($fd, 4096));
 197                  if (preg_match('/WDC/', $results[$file]['model'])){
 198                      $results[$file]['manufacture'] = 'Western Digital';
 199                  } elseif (preg_match('/IBM/', $results[$file]['model'])){
 200                      $results[$file]['manufacture'] = 'IBM';
 201                  } elseif (preg_match('/FUJITSU/', $results[$file]['model'])){
 202                      $results[$file]['manufacture'] = 'Fujitsu';
 203                  } else {
 204                      $results[$file]['manufacture'] = 'Unknown';
 205                  }
 206  
 207                  fclose($fd);
 208              }
 209              if ($fd = fopen("/proc/ide/$file/capacity", 'r')) {
 210                  $results[$file]['capacity'] = trim(fgets($fd, 4096));
 211                  if ($results[$file]['media'] == 'CD-ROM') {
 212                      unset($results[$file]['capacity']);
 213                  }
 214                  fclose($fd);
 215              }
 216          }
 217      }
 218      closedir($handle); 
 219      
 220      return $results;
 221  }
 222  
 223  
 224  // Returns an array of all meaningful devices 
 225  // on the SCSI bus.
 226  function sys_scsibus ()
 227  {
 228      $results = array();
 229      $dev_vendor = '';
 230      $dev_model = '';
 231      $dev_rev = '';
 232      $dev_type = '';
 233      
 234      if ($fd = fopen('/proc/scsi/scsi', 'r')) {
 235          while ($buf = fgets($fd, 4096)) {
 236              if (preg_match('/Vendor/', $buf)) {
 237                  preg_match('/Vendor: (.*) Model: (.*) Rev: (.*)/i', $buf, $dev);
 238                  list($key, $value) = split(': ', $buf, 2);
 239                  $dev_str = $value;
 240                  $get_type = 1;
 241                  continue;
 242              } 
 243      
 244              if ($get_type) { 
 245                  preg_match('/Type:\s+(\S+)/i', $buf, $dev_type);
 246                  $results[] = "$dev[1] $dev[2] ( $dev_type[1] )";
 247                  $get_type = 0;
 248              }
 249          }
 250      } 
 251      return $results;
 252  }
 253  
 254  
 255  // Returns an associative array of two associative
 256  // arrays, containg the memory statistics for RAM and swap
 257  function sys_meminfo ()
 258  {
 259      if ($fd = fopen('/proc/meminfo', 'r')) {
 260          while ($buf = fgets($fd, 4096)) {
 261              if (preg_match('/Mem:\s+(.*)$/', $buf, $ar_buf)) {
 262                  $ar_buf = preg_split('/\s+/', $ar_buf[1], 6);
 263      
 264                  $results['ram'] = array();
 265      
 266                  $results['ram']['total'] = $ar_buf[0] / 1024;
 267                  $results['ram']['used'] = $ar_buf[1] / 1024;
 268                  $results['ram']['free'] = $ar_buf[2] / 1024;
 269                  $results['ram']['shared'] = $ar_buf[3] / 1024;
 270                  $results['ram']['buffers'] = $ar_buf[4] / 1024;
 271                  $results['ram']['cached'] = $ar_buf[5] / 1024;
 272      
 273                  $results['ram']['t_used'] = $results['ram']['used'] - $results['ram']['cached'] - $results['ram']['buffers'];
 274                  $results['ram']['t_free'] = $results['ram']['total'] - $results['ram']['t_used'];
 275                  $results['ram']['percent'] = round(($results['ram']['t_used'] * 100) / $results['ram']['total']);
 276              }
 277      
 278              if (preg_match('/Swap:\s+(.*)$/', $buf, $ar_buf)) {
 279                  $ar_buf = preg_split('/\s+/', $ar_buf[1], 3);
 280      
 281                  $results['swap'] = array();
 282      
 283                  $results['swap']['total'] = $ar_buf[0] / 1024;
 284                  $results['swap']['used'] = $ar_buf[1] / 1024;
 285                  $results['swap']['free'] = $ar_buf[2] / 1024;
 286                  $results['swap']['percent'] = round(($ar_buf[1] * 100) / $ar_buf[0]);
 287                  
 288                  // Get info on individual swap files
 289                  $swaps = file ('/proc/swaps');
 290                  $swapdevs = split("\n", $swaps);
 291                      
 292                  for ($i = 1; $i < (sizeof($swapdevs) - 1); $i++) {
 293                      $ar_buf = preg_split('/\s+/', $swapdevs[$i], 6);
 294      
 295                      $results['devswap'][$i - 1] = array();
 296                      $results['devswap'][$i - 1]['dev']     = $ar_buf[0];
 297                      $results['devswap'][$i - 1]['total']   = $ar_buf[2];
 298                      $results['devswap'][$i - 1]['used']    = $ar_buf[3];
 299                      $results['devswap'][$i - 1]['free']    = ($results['devswap'][$i - 1]['total'] - $results['devswap'][$i - 1]['used']);
 300                      $results['devswap'][$i - 1]['percent'] = round(($ar_buf[3] * 100) / $ar_buf[2]);
 301                  }
 302                  break;
 303              }
 304          }
 305          fclose($fd);
 306      }
 307      else {
 308          $results['ram'] = array();
 309          $results['swap'] = array();
 310          $results['devswap'] = array();
 311      }
 312      return $results;
 313  }
 314  
 315  
 316  // Returns an array of all network devices 
 317  // and their tx/rx stats. 
 318  function sys_netdevs ()
 319  {
 320      $results = array();
 321      
 322      if ($fd = fopen('/proc/net/dev', 'r')) {
 323          while ($buf = fgets($fd, 4096)) {
 324              if (preg_match('/:/', $buf)) {
 325                  list($dev_name, $stats_list) = preg_split('/:/', $buf, 2);
 326                  $stats = preg_split('/\s+/', trim($stats_list));
 327                  $results[$dev_name] = array();
 328      
 329                  $results[$dev_name]['rx_bytes'] = $stats[0];
 330                  $results[$dev_name]['rx_packets'] = $stats[1];
 331                  $results[$dev_name]['rx_errs'] = $stats[2];
 332                  $results[$dev_name]['rx_drop'] = $stats[3];
 333      
 334                  $results[$dev_name]['tx_bytes'] = $stats[8];
 335                  $results[$dev_name]['tx_packets'] = $stats[9];
 336                  $results[$dev_name]['tx_errs'] = $stats[10];
 337                  $results[$dev_name]['tx_drop'] = $stats[11];
 338      
 339                  $results[$dev_name]['errs'] = $stats[2] + $stats[10];
 340                  $results[$dev_name]['drop'] = $stats[3] + $stats[11];
 341              }
 342          }
 343      } 
 344      return $results;
 345  }
 346  
 347  
 348  // Returns a string equivilant to `uname --release`)
 349  function sys_kernel ()
 350  {
 351      if ($fd = fopen('/proc/version', 'r')) {
 352          $buf = fgets($fd, 4096);
 353          fclose($fd);
 354      
 355          if (preg_match('/version (.*?) /', $buf, $ar_buf)) {
 356              $result = $ar_buf[1];
 357      
 358              if (preg_match('/SMP/', $buf)) {
 359                  $result .= ' (SMP)';
 360              }
 361          } else {
 362              $result = 'N.A.';
 363          }
 364      } else {
 365          $result = 'N.A.';
 366      }
 367      return $result;
 368  }
 369  
 370  
 371  // Returns a 1x3 array of load avg's in
 372  // standard order and format.
 373  function sys_loadavg ()
 374  {
 375      if ($fd = fopen('/proc/loadavg', 'r')) {
 376          $results = split(' ', fgets($fd, 4096));
 377          fclose($fd);
 378      } else {
 379          $results = array('N.A.','N.A.','N.A.');
 380      }
 381      
 382      return $results;
 383  }
 384  
 385  
 386  // Returns a formatted english string,
 387  // enumerating the uptime verbosely.
 388  function sys_uptime ()
 389  {
 390      global $text;
 391      $fd = fopen('/proc/uptime', 'r');
 392      $ar_buf = split(' ', fgets($fd, 4096));
 393      fclose($fd);
 394      
 395      $sys_ticks = trim($ar_buf[0]);
 396      
 397      $min   = $sys_ticks / 60;
 398      $hours = $min / 60;
 399      $days  = floor($hours / 24);
 400      $hours = floor($hours - ($days * 24));
 401      $min   = floor($min - ($days * 60 * 24) - ($hours * 60));
 402          
 403      if ($days != 0) {
 404          $result = "$days ".$text['days']." ";
 405      }  
 406      if ($hours != 0) {
 407          $result .= "$hours ".$text['hours']." ";
 408      }  
 409      $result .= "$min ".$text['minutes'];
 410          
 411      return $result;
 412  }
 413  
 414  
 415  // Returns the number of users currently logged in.
 416  function sys_users ()
 417  {
 418      $who = split('=', execute_program('who', '-q'));
 419      $result = $who[1];
 420      return $result;
 421  }
 422  
 423  
 424  // Returns an associative array containing all
 425  // relevant info about the processors in the system.
 426  function sys_cpu ()
 427  {
 428      $results = array();
 429      $ar_buf = array();
 430      
 431      if ($fd = fopen('/proc/cpuinfo', 'r')) {
 432          while ($buf = fgets($fd, 4096)) {
 433              list($key, $value) = preg_split('/\s+:\s+/', trim($buf), 2);
 434      
 435              // All of the tags here are highly architecture dependant.
 436              // the only way I could reconstruct them for machines I don't
 437              // have is to browse the kernel source.  So if your arch isn't
 438              // supported, tell me you want it written in. <infinite@sigkill.com>
 439              switch ($key) {
 440                  case 'model name':
 441                      $results['model'] = $value;
 442                      break;
 443                  case 'cpu MHz':
 444                      $results['mhz'] = sprintf('%.2f', $value);
 445                      break;
 446                  case 'clock': // For PPC arch (damn borked POS)
 447                      $results['mhz'] = sprintf('%.2f', $value);
 448                      break;
 449                  case 'cpu': // For PPC arch (damn borked POS)
 450                      $results['model'] = $value;
 451                      break;
 452                  case 'revision': // For PPC arch (damn borked POS)
 453                      $results['model'] .= ' ( rev: ' . $value . ')';
 454                      break;
 455                  case 'cache size':
 456                      $results['cache'] = $value;
 457                      break;
 458                  case 'bogomips':
 459                      $results['bogomips'] += $value;
 460                      break;
 461                  case 'processor':
 462                      $results['cpus'] += 1;
 463                      break;
 464              }    
 465          }
 466          fclose($fd);
 467      }
 468  
 469      $keys = compat_array_keys($results);
 470      $keys2be = array('model', 'mhz', 'cache', 'bogomips', 'cpus');
 471  
 472      while ($ar_buf = each($keys2be)) {
 473          if (! compat_in_array($ar_buf[1], $keys)) $results[$ar_buf[1]] = 'N.A.';
 474      }
 475  
 476      return $results;
 477  }
 478  
 479  
 480  // Returns an array of associative arrays
 481  // containing information on every mounted partition.
 482  function sys_fsinfo ()
 483  {
 484      $df = execute_program('df', '-kP');
 485      $mounts = split("\n", $df);
 486      $fstype = array();
 487      
 488      if ($fd = fopen('/proc/mounts', 'r')) {
 489          while ($buf = fgets($fd, 4096)) {
 490              list($dev, $mpoint, $type) = preg_split('/\s+/', trim($buf), 4);
 491              $fstype[$mpoint] = $type;
 492              $fsdev[$dev] = $type;
 493          }
 494          fclose($fd);
 495      }
 496      
 497      for ($i = 1; $i < sizeof($mounts); $i++) {
 498          $ar_buf = preg_split('/\s+/', $mounts[$i], 6);
 499      
 500          $results[$i - 1] = array();
 501  
 502          $results[$i - 1]['disk'] = $ar_buf[0];
 503          $results[$i - 1]['size'] = $ar_buf[1];
 504          $results[$i - 1]['used'] = $ar_buf[2];
 505          $results[$i - 1]['free'] = $ar_buf[3];
 506          $results[$i - 1]['percent'] = round(($results[$i - 1]['used'] * 100) / $results[$i - 1]['size']) . '%';
 507          $results[$i - 1]['mount'] = $ar_buf[5];
 508          ($fstype[$ar_buf[5]]) ? $results[$i - 1]['fstype'] = $fstype[$ar_buf[5]] : $results[$i - 1]['fstype'] = $fsdev[$ar_buf[0]];
 509      }
 510      
 511      return $results;
 512  }
 513  
 514  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7