[ Index ]
 

Code source de eGroupWare 1.2.106-2

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

title

Body

[fermer]

/phpsysinfo/includes/os/ -> class.Linux.inc.php (source)

   1  <?php 
   2  // phpSysInfo - A PHP System Information Script
   3  // http://phpsysinfo.sourceforge.net/
   4  // This program is free software; you can redistribute it and/or
   5  // modify it under the terms of the GNU General Public License
   6  // as published by the Free Software Foundation; either version 2
   7  // of the License, or (at your option) any later version.
   8  // This program is distributed in the hope that it will be useful,
   9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11  // GNU General Public License for more details.
  12  // You should have received a copy of the GNU General Public License
  13  // along with this program; if not, write to the Free Software
  14  // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  15  // $Id: class.Linux.inc.php 21228 2006-04-06 13:52:08Z ralfbecker $
  16  class sysinfo {
  17    var $inifile = "distros.ini";
  18    var $icon = "unknown.png";
  19    var $distro = "unknown";
  20  
  21    // get the distro name and icon when create the sysinfo object
  22    function sysinfo() {
  23     $list = @parse_ini_file(APP_ROOT . "/" . $this->inifile, true);
  24     if (!$list) {
  25      return;
  26     }
  27     foreach ($list as $section => $distribution) {
  28      if (!isset($distribution["Files"])) {
  29       continue;
  30      } else {
  31       foreach (explode(";", $distribution["Files"]) as $filename) {
  32        if (file_exists($filename)) {
  33         $buf = rfts( $filename );
  34         $this->icon = isset($distribution["Image"]) ? $distribution["Image"] : $this->icon;
  35         $this->distro = isset($distribution["Name"]) ? $distribution["Name"] . " " . trim($buf) : trim($buf);
  36         break 2;
  37        }
  38       }
  39      }
  40     }
  41    }
  42  
  43    // get our apache SERVER_NAME or vhost
  44    function vhostname () {
  45      if (! ($result = getenv('SERVER_NAME'))) {
  46        $result = 'N.A.';
  47      } 
  48      return $result;
  49    } 
  50    // get our canonical hostname
  51    function chostname () {
  52      $result = rfts( '/proc/sys/kernel/hostname', 1 );
  53      if ( $result == "ERROR" ) {
  54        $result = "N.A.";
  55      } else {
  56        $result = gethostbyaddr( gethostbyname( trim( $result ) ) );
  57      } 
  58      return $result;
  59    } 
  60    // get the IP address of our canonical hostname
  61    function ip_addr () {
  62      if (!($result = getenv('SERVER_ADDR'))) {
  63        $result = gethostbyname($this->chostname());
  64      } 
  65      return $result;
  66    } 
  67  
  68    function kernel () {
  69      $buf = rfts( '/proc/version', 1 );
  70      if ( $buf == "ERROR" ) {
  71        $result = "N.A.";
  72      } else {
  73        if (preg_match('/version (.*?) /', $buf, $ar_buf)) {
  74          $result = $ar_buf[1];
  75  
  76          if (preg_match('/SMP/', $buf)) {
  77            $result .= ' (SMP)';
  78          } 
  79        } 
  80      } 
  81      return $result;
  82    } 
  83    
  84    function uptime () {
  85      $buf = rfts( '/proc/uptime', 1 );
  86      $ar_buf = split( ' ', $buf );
  87      $result = trim( $ar_buf[0] );
  88  
  89      return $result;
  90    } 
  91  
  92    function users () {
  93      $who = split('=', execute_program('who', '-q'));
  94      $result = $who[1];
  95      return $result;
  96    } 
  97  
  98    function loadavg ($bar = false) {
  99      $buf = rfts( '/proc/loadavg' );
 100      if( $buf == "ERROR" ) {
 101        $results['avg'] = array('N.A.', 'N.A.', 'N.A.');
 102      } else {
 103        $results['avg'] = preg_split("/\s/", $buf, 4);
 104        unset($results['avg'][3]);    // don't need the extra values, only first three
 105      } 
 106      if ($bar) {
 107        $buf = rfts( '/proc/stat', 1 );
 108        if( $buf != "ERROR" ) {
 109      sscanf($buf, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
 110      // Find out the CPU load
 111      // user + sys = load 
 112      // total = total
 113      $load = $ab + $ac + $ad;    // cpu.user + cpu.sys
 114      $total = $ab + $ac + $ad + $ae;    // cpu.total
 115  
 116      // we need a second value, wait 1 second befor getting (< 1 second no good value will occour)
 117      sleep(1);
 118      $buf = rfts( '/proc/stat', 1 );
 119      sscanf($buf, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
 120      $load2 = $ab + $ac + $ad;
 121      $total2 = $ab + $ac + $ad + $ae;
 122      $results['cpupercent'] = (100*($load2 - $load)) / ($total2 - $total);
 123        }
 124      }
 125      return $results;
 126    } 
 127  
 128    function cpu_info () {
 129      $bufr = rfts( '/proc/cpuinfo' );
 130  
 131      if ( $bufr != "ERROR" ) {
 132        $bufe = explode("\n", $bufr);
 133  
 134        $results = array('cpus' => 0, 'bogomips' => 0);
 135        $ar_buf = array();
 136        
 137        foreach( $bufe as $buf ) {
 138         if($buf != "\n") {
 139          list($key, $value) = preg_split('/\s+:\s+/', trim($buf), 2); 
 140          // All of the tags here are highly architecture dependant.
 141          // the only way I could reconstruct them for machines I don't
 142          // have is to browse the kernel source.  So if your arch isn't
 143          // supported, tell me you want it written in.
 144          switch ($key) {
 145            case 'model name':
 146              $results['model'] = $value;
 147              break;
 148            case 'cpu MHz':
 149              $results['cpuspeed'] = sprintf('%.2f', $value);
 150              break;
 151            case 'cycle frequency [Hz]': // For Alpha arch - 2.2.x
 152              $results['cpuspeed'] = sprintf('%.2f', $value / 1000000);
 153              break;
 154            case 'clock': // For PPC arch (damn borked POS)
 155              $results['cpuspeed'] = sprintf('%.2f', $value);
 156              break;
 157            case 'cpu': // For PPC arch (damn borked POS)
 158              $results['model'] = $value;
 159              break;
 160            case 'L2 cache': // More for PPC
 161              $results['cache'] = $value;
 162              break;
 163            case 'revision': // For PPC arch (damn borked POS)
 164              $results['model'] .= ' ( rev: ' . $value . ')';
 165              break;
 166            case 'cpu model': // For Alpha arch - 2.2.x
 167              $results['model'] .= ' (' . $value . ')';
 168              break;
 169            case 'cache size':
 170              $results['cache'] = $value;
 171              break;
 172            case 'bogomips':
 173              $results['bogomips'] += $value;
 174              break;
 175            case 'BogoMIPS': // For alpha arch - 2.2.x
 176              $results['bogomips'] += $value;
 177              break;
 178            case 'BogoMips': // For sparc arch
 179              $results['bogomips'] += $value;
 180              break;
 181            case 'cpus detected': // For Alpha arch - 2.2.x
 182              $results['cpus'] += $value;
 183              break;
 184            case 'system type': // Alpha arch - 2.2.x
 185              $results['model'] .= ', ' . $value . ' ';
 186              break;
 187            case 'platform string': // Alpha arch - 2.2.x
 188              $results['model'] .= ' (' . $value . ')';
 189              break;
 190            case 'processor':
 191              $results['cpus'] += 1;
 192              break;
 193            case 'Cpu0ClkTck': // Linux sparc64
 194              $results['cpuspeed'] = sprintf('%.2f', hexdec($value) / 1000000);
 195              break;
 196            case 'Cpu0Bogo': // Linux sparc64 & sparc32
 197              $results['bogomips'] = $value;
 198              break;
 199            case 'ncpus probed': // Linux sparc64 & sparc32
 200              $results['cpus'] = $value;
 201              break;
 202          } 
 203         }
 204        } 
 205      } 
 206  
 207      // sparc64 specific code follows
 208      // This adds the ability to display the cache that a CPU has
 209      // Originally made by Sven Blumenstein <bazik@gentoo.org> in 2004
 210      // Modified by Tom Weustink <freshy98@gmx.net> in 2004
 211      $sparclist = array('SUNW,UltraSPARC@0,0', 'SUNW,UltraSPARC-II@0,0', 'SUNW,UltraSPARC@1c,0', 'SUNW,UltraSPARC-IIi@1c,0', 'SUNW,UltraSPARC-II@1c,0');
 212      foreach ($sparclist as $name) {
 213        $buf = rfts( '/proc/openprom/' . $name . '/ecache-size',1 , 32, false );
 214        if( $buf != "ERROR" ) {
 215          $results['cache'] = base_convert($buf, 16, 10)/1024 . ' KB';
 216        }
 217      }
 218      // sparc64 specific code ends
 219  
 220      // XScale detection code
 221      if ( $results['cpus'] == 0 ) {
 222        foreach( $bufe as $buf ) {
 223         if($buf != "\n") {
 224           list($key, $value) = preg_split('/\s+:\s+/', trim($buf), 2);
 225       switch($key) {
 226         case 'Processor':
 227          $results['cpus'] += 1;
 228          $results['model'] = $value;
 229          break;
 230            case 'BogoMIPS': //BogoMIPS are not BogoMIPS on this CPU, it's the speed, no BogoMIPS available
 231              $results['cpuspeed'] = $value;
 232              break;
 233            case 'I size':
 234          $results['cache'] = $value;
 235          break;
 236        case 'D size':
 237          $results['cache'] += $value;
 238          break;
 239       }
 240         }
 241        }
 242        $results['cache'] = $results['cache'] / 1024 . " KB";
 243      }
 244  
 245      $keys = array_keys($results);
 246      $keys2be = array('model', 'cpuspeed', 'cache', 'bogomips', 'cpus');
 247  
 248      while ($ar_buf = each($keys2be)) {
 249        if (! in_array($ar_buf[1], $keys)) {
 250          $results[$ar_buf[1]] = 'N.A.';
 251        } 
 252      } 
 253      return $results;
 254    } 
 255  
 256    function pci () {
 257      $results = array();
 258  
 259      if ($_results = execute_program('lspci', '', false)) {
 260        $lines = split("\n", $_results);
 261        for ($i = 0, $max = sizeof($lines); $i < $max; $i++) {
 262          list($addr, $name) = explode(' ', trim($lines[$i]), 2);
 263  
 264          if (!preg_match('/bridge/i', $name) && !preg_match('/USB/i', $name)) {
 265            // remove all the version strings
 266            $name = preg_replace('/\(.*\)/', '', $name);
 267        // is addr really usefull for this??? i think it's not
 268            // $results[] = $addr . ' ' . $name;
 269        $results[] = $name;
 270          } 
 271        } 
 272      } else {
 273        $bufr = rfts( '/proc/pci' );
 274        foreach( $bufr as $buf ) {
 275          if (preg_match('/Bus/', $buf)) {
 276            $device = true;
 277            continue;
 278          } 
 279  
 280          if ($device) {
 281            list($key, $value) = split(': ', $buf, 2);
 282  
 283            if (!preg_match('/bridge/i', $key) && !preg_match('/USB/i', $key)) {
 284              $results[] = preg_replace('/\([^\)]+\)\.$/', '', trim($value));
 285            } 
 286            $device = false;
 287          } 
 288        } 
 289      } 
 290      asort($results);
 291      return $results;
 292    } 
 293  
 294    function ide () {
 295      $results = array();
 296      $bufd = gdc( '/proc/ide' );
 297  
 298      foreach( $bufd as $file ) {
 299        if (preg_match('/^hd/', $file)) {
 300          $results[$file] = array(); 
 301          // Check if device is CD-ROM (CD-ROM capacity shows as 1024 GB)
 302      $buf = rfts("/proc/ide/" . $file . "/media", 1 );
 303          if ( $buf != "ERROR" ) {
 304            $results[$file]['media'] = trim($buf);
 305            if ($results[$file]['media'] == 'disk') {
 306              $results[$file]['media'] = 'Hard Disk';
 307            } 
 308  
 309            if ($results[$file]['media'] == 'cdrom') {
 310              $results[$file]['media'] = 'CD-ROM';
 311            } 
 312          } 
 313  
 314      $buf = rfts( "/proc/ide/" . $file . "/model", 1 );
 315          if ( $buf != "ERROR" ) {
 316            $results[$file]['model'] = trim( $buf );
 317            if (preg_match('/WDC/', $results[$file]['model'])) {
 318              $results[$file]['manufacture'] = 'Western Digital';
 319            } elseif (preg_match('/IBM/', $results[$file]['model'])) {
 320              $results[$file]['manufacture'] = 'IBM';
 321            } elseif (preg_match('/FUJITSU/', $results[$file]['model'])) {
 322              $results[$file]['manufacture'] = 'Fujitsu';
 323            } else {
 324              $results[$file]['manufacture'] = 'Unknown';
 325            } 
 326          } 
 327      
 328      $buf = rfts( "/proc/ide/" . $file . "/capacity", 1, 4096, false);
 329      if( $buf == "ERROR" )
 330        $buf = rfts( "/sys/block/" . $file . "/size", 1, 4096, false);
 331  
 332          if ( $buf != "ERROR" ) {
 333            $results[$file]['capacity'] = trim( $buf );
 334            if ($results[$file]['media'] == 'CD-ROM') {
 335              unset($results[$file]['capacity']);
 336            } 
 337          } 
 338        } 
 339      } 
 340  
 341      asort($results);
 342      return $results;
 343    } 
 344  
 345    function scsi () {
 346      $results = array();
 347      $dev_vendor = '';
 348      $dev_model = '';
 349      $dev_rev = '';
 350      $dev_type = '';
 351      $s = 1;
 352      $get_type = 0;
 353  
 354      $bufr = rfts( '/proc/scsi/scsi' );
 355      if ( $bufr != "ERROR" ) {
 356        $bufe = explode("\n", $bufr);
 357        foreach( $bufe as $buf ) {
 358          if (preg_match('/Vendor/', $buf)) {
 359            preg_match('/Vendor: (.*) Model: (.*) Rev: (.*)/i', $buf, $dev);
 360            list($key, $value) = split(': ', $buf, 2);
 361            $dev_str = $value;
 362            $get_type = true;
 363            continue;
 364          } 
 365  
 366          if ($get_type) {
 367            preg_match('/Type:\s+(\S+)/i', $buf, $dev_type);
 368            $results[$s]['model'] = "$dev[1] $dev[2] ($dev_type[1])";
 369            $results[$s]['media'] = "Hard Disk";
 370            $s++;
 371            $get_type = false;
 372          } 
 373        } 
 374      } 
 375      asort($results);
 376      return $results;
 377    } 
 378  
 379    function usb () {
 380      $results = array();
 381      $devnum = -1;
 382  
 383      $bufr = rfts( '/proc/bus/usb/devices' );
 384      if ( $bufr != "ERROR" ) {
 385        $bufe = explode("\n", $bufr);
 386        foreach( $bufe as $buf ) {
 387          if (preg_match('/^T/', $buf)) {
 388            $devnum += 1;
 389        $results[$devnum] = "";
 390          } elseif (preg_match('/^S:/', $buf)) {
 391            list($key, $value) = split(': ', $buf, 2);
 392            list($key, $value2) = split('=', $value, 2);
 393        if (trim($key) != "SerialNumber") {
 394              $results[$devnum] .= " " . trim($value2);
 395              $devstring = 0;
 396        }
 397          } 
 398        }
 399      } 
 400      return $results;
 401    } 
 402  
 403    function sbus () {
 404      $results = array();
 405      $_results[0] = ""; 
 406      // TODO. Nothing here yet. Move along.
 407      $results = $_results;
 408      return $results;
 409    } 
 410  
 411    function network () {
 412      $results = array();
 413  
 414      $bufr = rfts( '/proc/net/dev' );
 415      if ( $bufr != "ERROR" ) {
 416        $bufe = explode("\n", $bufr);
 417        foreach( $bufe as $buf ) {
 418          if (preg_match('/:/', $buf)) {
 419            list($dev_name, $stats_list) = preg_split('/:/', $buf, 2);
 420            $stats = preg_split('/\s+/', trim($stats_list));
 421            $results[$dev_name] = array();
 422  
 423            $results[$dev_name]['rx_bytes'] = $stats[0];
 424            $results[$dev_name]['rx_packets'] = $stats[1];
 425            $results[$dev_name]['rx_errs'] = $stats[2];
 426            $results[$dev_name]['rx_drop'] = $stats[3];
 427  
 428            $results[$dev_name]['tx_bytes'] = $stats[8];
 429            $results[$dev_name]['tx_packets'] = $stats[9];
 430            $results[$dev_name]['tx_errs'] = $stats[10];
 431            $results[$dev_name]['tx_drop'] = $stats[11];
 432  
 433            $results[$dev_name]['errs'] = $stats[2] + $stats[10];
 434            $results[$dev_name]['drop'] = $stats[3] + $stats[11];
 435          } 
 436        }
 437      }
 438      return $results;
 439    } 
 440  
 441    function memory () {
 442      $results['ram'] = array();
 443      $results['swap'] = array();
 444      $results['devswap'] = array();
 445  
 446      $bufr = rfts( '/proc/meminfo' );
 447      if ( $bufr != "ERROR" ) {
 448        $bufe = explode("\n", $bufr);
 449        foreach( $bufe as $buf ) {
 450          if (preg_match('/^MemTotal:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
 451            $results['ram']['total'] = $ar_buf[1];
 452          } else if (preg_match('/^MemFree:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
 453            $results['ram']['t_free'] = $ar_buf[1];
 454          } else if (preg_match('/^Cached:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
 455            $results['ram']['cached'] = $ar_buf[1];
 456          } else if (preg_match('/^Buffers:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
 457            $results['ram']['buffers'] = $ar_buf[1];
 458          } else if (preg_match('/^SwapTotal:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
 459            $results['swap']['total'] = $ar_buf[1];
 460          } else if (preg_match('/^SwapFree:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
 461            $results['swap']['free'] = $ar_buf[1];
 462          } 
 463        } 
 464  
 465        $results['ram']['t_used'] = $results['ram']['total'] - $results['ram']['t_free'];
 466        $results['ram']['percent'] = round(($results['ram']['t_used'] * 100) / $results['ram']['total']);
 467        $results['swap']['used'] = $results['swap']['total'] - $results['swap']['free'];
 468        $results['swap']['percent'] = round(($results['swap']['used'] * 100) / $results['swap']['total']);
 469        
 470        // values for splitting memory usage
 471        if (isset($results['ram']['cached']) && isset($results['ram']['buffers'])) {
 472          $results['ram']['app'] = $results['ram']['t_used'] - $results['ram']['cached'] - $results['ram']['buffers'];
 473      $results['ram']['app_percent'] = round(($results['ram']['app'] * 100) / $results['ram']['total']);
 474      $results['ram']['buffers_percent'] = round(($results['ram']['buffers'] * 100) / $results['ram']['total']);
 475      $results['ram']['cached_percent'] = round(($results['ram']['cached'] * 100) / $results['ram']['total']);
 476        }
 477  
 478        $bufr = rfts( '/proc/swaps' );
 479        if ( $bufr != "ERROR" ) {
 480          $swaps = explode("\n", $bufr);
 481          for ($i = 1; $i < (sizeof($swaps)); $i++) {
 482        if( trim( $swaps[$i] ) != "" ) {
 483              $ar_buf = preg_split('/\s+/', $swaps[$i], 6);
 484              $results['devswap'][$i - 1] = array();
 485              $results['devswap'][$i - 1]['dev'] = $ar_buf[0];
 486              $results['devswap'][$i - 1]['total'] = $ar_buf[2];
 487              $results['devswap'][$i - 1]['used'] = $ar_buf[3];
 488              $results['devswap'][$i - 1]['free'] = ($results['devswap'][$i - 1]['total'] - $results['devswap'][$i - 1]['used']);
 489              $results['devswap'][$i - 1]['percent'] = round(($ar_buf[3] * 100) / $ar_buf[2]);
 490        }
 491          } 
 492        }
 493      }
 494      return $results;
 495    } 
 496  
 497    function filesystems () {
 498      global $show_bind;
 499      $fstype = array();
 500      $fsoptions = array();
 501  
 502      $df = execute_program('df', '-kP');
 503      $mounts = split("\n", $df);
 504  
 505      $buffer = execute_program("mount");
 506      $buffer = explode("\n", $buffer);
 507  
 508      $j = 0;
 509      foreach($buffer as $line) {
 510        preg_match("/(.*) on (.*) type (.*) \((.*)\)/", $line, $result);
 511        if (count($result) == 5) {
 512          $dev = $result[1]; $mpoint = $result[2]; $type = $result[3]; $options = $result[4];
 513          $fstype[$mpoint] = $type; $fsdev[$dev] = $type; $fsoptions[$mpoint] = $options;
 514  
 515          foreach ($mounts as $line2) {
 516            if (preg_match("#^" . str_replace("\$","\\$",$result[1]) . " #", $line2)) {
 517              $line2 = preg_replace("#^" . str_replace("\$","\\$",$result[1]) . " #", "", $line2);
 518              $ar_buf = preg_split("/(\s+)/", $line2, 6);
 519              $ar_buf[0] = $result[1];
 520  
 521              if (hide_mount($ar_buf[5]) || $ar_buf[0] == "") {
 522                continue;
 523              }
 524  
 525              if ($show_bind || !stristr($fsoptions[$ar_buf[5]], "bind")) {
 526                $results[$j] = array();
 527                $results[$j]['disk'] = $ar_buf[0];
 528                $results[$j]['size'] = $ar_buf[1];
 529                $results[$j]['used'] = $ar_buf[2];
 530                $results[$j]['free'] = $ar_buf[3];
 531                $results[$j]['percent'] = round(($results[$j]['used'] * 100) / $results[$j]['size']);
 532                $results[$j]['mount'] = $ar_buf[5];
 533                ($fstype[$ar_buf[5]]) ? $results[$j]['fstype'] = $fstype[$ar_buf[5]] : $results[$j]['fstype'] = $fsdev[$ar_buf[0]];
 534                $results[$j]['options'] = $fsoptions[$ar_buf[5]];
 535                $j++;
 536              }
 537            }
 538      }
 539        }
 540      }
 541      return $results;
 542    } 
 543  
 544    function distro () {
 545     return $this->distro;
 546    }
 547  
 548    function distroicon () {   
 549     return $this->icon;
 550    }
 551  
 552  } 
 553  
 554  ?>


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