[ 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.BSD.common.inc.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: class.BSD.common.inc.php 21228 2006-04-06 13:52:08Z ralfbecker $
  21  
  22  class bsd_common {
  23    var $dmesg; 
  24    // Our constructor
  25    // this function is run on the initialization of this class
  26    function bsd_common () {
  27      // initialize all the variables we need from our parent class
  28      $this->sysinfo();
  29    } 
  30    // read /var/run/dmesg.boot, but only if we haven't already.
  31    function read_dmesg () {
  32      if (! $this->dmesg) {
  33        $this->dmesg = rfts( '/var/run/dmesg.boot' );
  34      } 
  35      return $this->dmesg;
  36    } 
  37    // grabs a key from sysctl(8)
  38    function grab_key ($key) {
  39      return execute_program('sysctl', "-n $key");
  40    } 
  41    // get our apache SERVER_NAME or vhost
  42    function hostname () {
  43      if (!($result = getenv('SERVER_NAME'))) {
  44        $result = "N.A.";
  45      } 
  46      return $result;
  47    } 
  48    // get our canonical hostname
  49    function chostname () {
  50      return execute_program('hostname');
  51    } 
  52    // get the IP address of our canonical hostname
  53    function ip_addr () {
  54      if (!($result = getenv('SERVER_ADDR'))) {
  55        $result = gethostbyname($this->chostname());
  56      } 
  57      return $result;
  58    } 
  59  
  60    function kernel () {
  61      $s = $this->grab_key('kern.version');
  62      $a = explode(':', $s);
  63      return $a[0] . $a[1] . ':' . $a[2];
  64    } 
  65  
  66    function uptime () {
  67      $result = $this->get_sys_ticks();
  68  
  69      return $result;
  70    } 
  71  
  72    function users () {
  73      return execute_program('who', '| wc -l');
  74    } 
  75  
  76    function loadavg ($bar = false) {
  77      $s = $this->grab_key('vm.loadavg');
  78      $s = ereg_replace('{ ', '', $s);
  79      $s = ereg_replace(' }', '', $s);
  80      $results['avg'] = explode(' ', $s);
  81  
  82      if ($bar) {
  83        if ($fd = $this->grab_key('kern.cp_time')) {
  84          sscanf($fd, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
  85          // Find out the CPU load
  86          // user + sys = load
  87          // total = total
  88          $load = $ab + $ac + $ad;        // cpu.user + cpu.sys
  89          $total = $ab + $ac + $ad + $ae; // cpu.total
  90  
  91          // we need a second value, wait 1 second befor getting (< 1 second no good value will occour)
  92          sleep(1);
  93          $fd = $this->grab_key('kern.cp_time');
  94          sscanf($fd, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
  95          $load2 = $ab + $ac + $ad;
  96          $total2 = $ab + $ac + $ad + $ae;
  97          $results['cpupercent'] = (100*($load2 - $load)) / ($total2 - $total);
  98        }
  99      }
 100      return $results;
 101    } 
 102  
 103    function cpu_info () {
 104      $results = array();
 105      $ar_buf = array();
 106  
 107      $results['model'] = $this->grab_key('hw.model');
 108      $results['cpus'] = $this->grab_key('hw.ncpu');
 109  
 110      for ($i = 0, $max = count($this->read_dmesg()); $i < $max; $i++) {
 111        $buf = $this->dmesg[$i];
 112        if (preg_match("/$this->cpu_regexp/", $buf, $ar_buf)) {
 113          $results['cpuspeed'] = round($ar_buf[2]);
 114          break;
 115        } 
 116      } 
 117      return $results;
 118    } 
 119    // get the scsi device information out of dmesg
 120    function scsi () {
 121      $results = array();
 122      $ar_buf = array();
 123  
 124      for ($i = 0, $max = count($this->read_dmesg()); $i < $max; $i++) {
 125        $buf = $this->dmesg[$i];
 126  
 127        if (preg_match("/$this->scsi_regexp1/", $buf, $ar_buf)) {
 128          $s = $ar_buf[1];
 129          $results[$s]['model'] = $ar_buf[2];
 130          $results[$s]['media'] = 'Hard Disk';
 131        } elseif (preg_match("/$this->scsi_regexp2/", $buf, $ar_buf)) {
 132          $s = $ar_buf[1];
 133          $results[$s]['capacity'] = $ar_buf[2] * 2048 * 1.049;
 134        }
 135      } 
 136      // return array_values(array_unique($results));
 137      // 1. more useful to have device names
 138      // 2. php 4.1.1 array_unique() deletes non-unique values.
 139      asort($results);
 140      return $results;
 141    } 
 142  
 143    // get the pci device information out of dmesg
 144    function pci () {
 145      $results = array();
 146  
 147      if($buf = execute_program("pciconf", "-lv")) {
 148      $buf = explode("\n", $buf); $s = 0;
 149      foreach($buf as $line) {
 150          if (preg_match("/(.*) = '(.*)'/", $line, $strings)) {
 151          if (trim($strings[1]) == "vendor") {
 152              $results[$s] = trim($strings[2]);
 153          } elseif (trim($strings[1]) == "device") {
 154              $results[$s] .= " - " . trim($strings[2]);
 155              $s++;
 156          }
 157          }
 158      }
 159      } else {
 160      for ($i = 0, $s = 0; $i < count($this->read_dmesg()); $i++) {
 161          $buf = $this->dmesg[$i];
 162          if (preg_match('/(.*): <(.*)>(.*) pci[0-9]$/', $buf, $ar_buf)) {
 163          $results[$s++] = $ar_buf[1] . ": " . $ar_buf[2];
 164          } elseif (preg_match('/(.*): <(.*)>.* at [.0-9]+ irq/', $buf, $ar_buf)) {
 165          $results[$s++] = $ar_buf[1] . ": " . $ar_buf[2];
 166          }
 167      } 
 168      $results = array_unique($results);
 169      }
 170      asort($results);
 171      return $results;
 172    } 
 173  
 174    // get the ide device information out of dmesg
 175    function ide () {
 176      $results = array();
 177  
 178      $s = 0;
 179      for ($i = 0, $max = count($this->read_dmesg()); $i < $max; $i++) {
 180        $buf = $this->dmesg[$i];
 181  
 182        if (preg_match('/^(ad[0-9]+): (.*)MB <(.*)> (.*) (.*)/', $buf, $ar_buf)) {
 183          $s = $ar_buf[1];
 184          $results[$s]['model'] = $ar_buf[3];
 185          $results[$s]['media'] = 'Hard Disk';
 186          $results[$s]['capacity'] = $ar_buf[2] * 2048 * 1.049;
 187        } elseif (preg_match('/^(acd[0-9]+): (.*) <(.*)> (.*)/', $buf, $ar_buf)) {
 188          $s = $ar_buf[1];
 189          $results[$s]['model'] = $ar_buf[3];
 190          $results[$s]['media'] = 'CD-ROM';
 191        }
 192      } 
 193      // return array_values(array_unique($results));
 194      // 1. more useful to have device names
 195      // 2. php 4.1.1 array_unique() deletes non-unique values.
 196      asort($results);
 197      return $results;
 198    } 
 199  
 200    // place holder function until we add acual usb detection
 201    function usb () {
 202      return array();
 203    } 
 204  
 205    function sbus () {
 206      $results = array();
 207      $_results[0] = "";
 208      // TODO. Nothing here yet. Move along.
 209      $results = $_results;
 210      return $results;
 211    }
 212  
 213    function memory () {
 214      $s = $this->grab_key('hw.physmem');
 215  
 216      if (PHP_OS == 'FreeBSD' || PHP_OS == 'OpenBSD') {
 217        // vmstat on fbsd 4.4 or greater outputs kbytes not hw.pagesize
 218        // I should probably add some version checking here, but for now
 219        // we only support fbsd 4.4
 220        $pagesize = 1024;
 221      } else {
 222        $pagesize = $this->grab_key('hw.pagesize');
 223      } 
 224  
 225      $results['ram'] = array();
 226  
 227      $pstat = execute_program('vmstat');
 228      $lines = split("\n", $pstat);
 229      for ($i = 0, $max = sizeof($lines); $i < $max; $i++) {
 230        $ar_buf = preg_split("/\s+/", $lines[$i], 19);
 231  
 232        if ($i == 2) {
 233          $results['ram']['free'] = $ar_buf[5] * $pagesize / 1024;
 234        } 
 235      } 
 236  
 237      $results['ram']['total'] = $s / 1024;
 238      $results['ram']['shared'] = 0;
 239      $results['ram']['buffers'] = 0;
 240      $results['ram']['used'] = $results['ram']['total'] - $results['ram']['free'];
 241      $results['ram']['cached'] = 0;
 242      $results['ram']['t_used'] = $results['ram']['used'];
 243      $results['ram']['t_free'] = $results['ram']['free'];
 244  
 245      $results['ram']['percent'] = round(($results['ram']['used'] * 100) / $results['ram']['total']);
 246  
 247      if (PHP_OS == 'OpenBSD') {
 248        $pstat = execute_program('swapctl', '-l -k');
 249      } else {
 250        $pstat = execute_program('swapinfo', '-k');
 251      } 
 252  
 253      $lines = split("\n", $pstat);
 254  
 255      $results['swap']['total'] = 0;
 256      $results['swap']['used'] = 0;
 257      $results['swap']['free'] = 0;
 258  
 259      for ($i = 0, $max = sizeof($lines); $i < $max; $i++) {
 260        $ar_buf = preg_split("/\s+/", $lines[$i], 6);
 261  
 262        if ($ar_buf[0] != 'Total') {
 263          $results['swap']['total'] = $results['swap']['total'] + $ar_buf[1];
 264          $results['swap']['used'] = $results['swap']['used'] + $ar_buf[2];
 265          $results['swap']['free'] = $results['swap']['free'] + $ar_buf[3];
 266        } 
 267      } 
 268      $results['swap']['percent'] = round(($results['swap']['used'] * 100) / $results['swap']['total']);
 269  
 270      return $results;
 271    } 
 272  
 273    function filesystems () {
 274      global $show_bind;
 275      $fstype = array();
 276      $fsoptions = array();
 277  
 278      $df = execute_program('df', '-k');
 279      $mounts = split("\n", $df);
 280  
 281      $buffer = execute_program("mount");
 282      $buffer = explode("\n", $buffer);
 283  
 284      $j = 0;
 285      foreach($buffer as $line) {
 286        preg_match("/(.*) on (.*) \((.*)\)/", $line, $result);
 287        list($result[3], $result[4]) = preg_split("/,\s/", $result[3], 2);
 288        if (count($result) == 5) {
 289          $dev = $result[1]; $mpoint = $result[2]; $type = $result[3]; $options = $result[4];
 290          $fstype[$mpoint] = $type; $fsdev[$dev] = $type; $fsoptions[$mpoint] = $options;
 291  
 292         if ($dev == "devfs")
 293           continue;
 294          foreach ($mounts as $line2) {
 295            if (preg_match("#^" . str_replace("\$", "\\$", $result[1]) . "#", $line2)) {
 296              $line2 = preg_replace("#^" . str_replace("\$", "\\$", $result[1]) . "#", "", $line2);
 297              $ar_buf = preg_split("/(\s+)/", $line2, 6);
 298              $ar_buf[0] = $result[1];
 299  
 300              if (hide_mount($ar_buf[5]) || $ar_buf[0] == "") {
 301                continue;
 302              }
 303  
 304              if ($show_bind || !stristr($fsoptions[$ar_buf[5]], "bind")) {
 305                $results[$j] = array();
 306                $results[$j]['disk'] = $ar_buf[0];
 307                $results[$j]['size'] = $ar_buf[1];
 308                $results[$j]['used'] = $ar_buf[2];
 309                $results[$j]['free'] = $ar_buf[3];
 310                $results[$j]['percent'] = round(($results[$j]['used'] * 100) / $results[$j]['size']);
 311                $results[$j]['mount'] = $ar_buf[5];
 312                ($fstype[$ar_buf[5]]) ? $results[$j]['fstype'] = $fstype[$ar_buf[5]] : $results[$j]['fstype'] = $fsdev[$ar_buf[0]];
 313                $results[$j]['options'] = $fsoptions[$ar_buf[5]];
 314                $j++;
 315              }
 316            }
 317          }
 318        }
 319      }
 320      return $results;
 321    }
 322  
 323    function distro () { 
 324      $distro = execute_program('uname', '-s');                             
 325      $result = $distro;
 326      return($result);               
 327    }
 328  } 
 329  
 330  ?>


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