[ Index ] |
|
Code source de vtiger CRM 5.0.2 |
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,v 1.50 2006/04/22 14:35:57 bigmichi1 Exp $ 21 22 if (!defined('IN_PHPSYSINFO')) { 23 die("No Hacking"); 24 } 25 26 require_once(APP_ROOT . '/includes/os/class.parseProgs.inc.php'); 27 28 class bsd_common { 29 var $dmesg; 30 var $parser; 31 // Our constructor 32 // this function is run on the initialization of this class 33 function bsd_common () { 34 $this->parser = new Parser(); 35 $this->parser->df_param = ""; 36 } 37 38 // read /var/run/dmesg.boot, but only if we haven't already. 39 function read_dmesg () { 40 if (! $this->dmesg) { 41 $parts = explode("rebooting", rfts( '/var/run/dmesg.boot' ) ); 42 $this->dmesg = explode("\n", $parts[count($parts) - 1]); 43 } 44 return $this->dmesg; 45 } 46 47 // grabs a key from sysctl(8) 48 function grab_key ($key) { 49 return execute_program('sysctl', "-n $key"); 50 } 51 // get our apache SERVER_NAME or vhost 52 function hostname () { 53 if (!($result = getenv('SERVER_NAME'))) { 54 $result = "N.A."; 55 } 56 return $result; 57 } 58 // get our canonical hostname 59 function chostname () { 60 return execute_program('hostname'); 61 } 62 // get the IP address of our canonical hostname 63 function ip_addr () { 64 if (!($result = getenv('SERVER_ADDR'))) { 65 $result = gethostbyname($this->chostname()); 66 } 67 return $result; 68 } 69 70 function kernel () { 71 $s = $this->grab_key('kern.version'); 72 $a = explode(':', $s); 73 return $a[0] . $a[1] . ':' . $a[2]; 74 } 75 76 function uptime () { 77 $result = $this->get_sys_ticks(); 78 79 return $result; 80 } 81 82 function users () { 83 return execute_program('who', '| wc -l'); 84 } 85 86 function loadavg ($bar = false) { 87 $s = $this->grab_key('vm.loadavg'); 88 $s = ereg_replace('{ ', '', $s); 89 $s = ereg_replace(' }', '', $s); 90 $results['avg'] = explode(' ', $s); 91 92 if ($bar) { 93 if ($fd = $this->grab_key('kern.cp_time')) { 94 // Find out the CPU load 95 // user + sys = load 96 // total = total 97 preg_match($this->cpu_regexp2, $fd, $res ); 98 $load = $res[2] + $res[3] + $res[4]; // cpu.user + cpu.sys 99 $total = $res[2] + $res[3] + $res[4] + $res[5]; // cpu.total 100 101 // we need a second value, wait 1 second befor getting (< 1 second no good value will occour) 102 sleep(1); 103 $fd = $this->grab_key('kern.cp_time'); 104 preg_match($this->cpu_regexp2, $fd, $res ); 105 $load2 = $res[2] + $res[3] + $res[4]; 106 $total2 = $res[2] + $res[3] + $res[4] + $res[5]; 107 $results['cpupercent'] = (100*($load2 - $load)) / ($total2 - $total); 108 } 109 } 110 return $results; 111 } 112 113 function cpu_info () { 114 $results = array(); 115 $ar_buf = array(); 116 117 $results['model'] = $this->grab_key('hw.model'); 118 $results['cpus'] = $this->grab_key('hw.ncpu'); 119 120 for ($i = 0, $max = count($this->read_dmesg()); $i < $max; $i++) { 121 $buf = $this->dmesg[$i]; 122 if (preg_match("/$this->cpu_regexp/", $buf, $ar_buf)) { 123 $results['cpuspeed'] = round($ar_buf[2]); 124 break; 125 } 126 } 127 return $results; 128 } 129 // get the scsi device information out of dmesg 130 function scsi () { 131 $results = array(); 132 $ar_buf = array(); 133 134 for ($i = 0, $max = count($this->read_dmesg()); $i < $max; $i++) { 135 $buf = $this->dmesg[$i]; 136 137 if (preg_match("/$this->scsi_regexp1/", $buf, $ar_buf)) { 138 $s = $ar_buf[1]; 139 $results[$s]['model'] = $ar_buf[2]; 140 $results[$s]['media'] = 'Hard Disk'; 141 } elseif (preg_match("/$this->scsi_regexp2/", $buf, $ar_buf)) { 142 $s = $ar_buf[1]; 143 $results[$s]['capacity'] = $ar_buf[2] * 2048 * 1.049; 144 } 145 } 146 // return array_values(array_unique($results)); 147 // 1. more useful to have device names 148 // 2. php 4.1.1 array_unique() deletes non-unique values. 149 asort($results); 150 return $results; 151 } 152 153 // get the pci device information out of dmesg 154 function pci () { 155 $results = array(); 156 157 if( !( is_array($results = $this->parser->parse_lspci()) || is_array($results = $this->parser->parse_pciconf() ))) { 158 for ($i = 0, $s = 0; $i < count($this->read_dmesg()); $i++) { 159 $buf = $this->dmesg[$i]; 160 if(!isset($this->pci_regexp1) && !isset($this->pci_regexp2)) { 161 $this->pci_regexp1 = '/(.*): <(.*)>(.*) pci[0-9]$/'; 162 $this->pci_regexp2 = '/(.*): <(.*)>.* at [.0-9]+ irq/'; 163 } 164 if (preg_match($this->pci_regexp1, $buf, $ar_buf)) { 165 $results[$s++] = $ar_buf[1] . ": " . $ar_buf[2]; 166 } elseif (preg_match($this->pci_regexp2, $buf, $ar_buf)) { 167 $results[$s++] = $ar_buf[1] . ": " . $ar_buf[2]; 168 } 169 } 170 asort($results); 171 } 172 return $results; 173 } 174 175 // get the ide device information out of dmesg 176 function ide () { 177 $results = array(); 178 179 $s = 0; 180 for ($i = 0, $max = count($this->read_dmesg()); $i < $max; $i++) { 181 $buf = $this->dmesg[$i]; 182 183 if (preg_match('/^(ad[0-9]+): (.*)MB <(.*)> (.*) (.*)/', $buf, $ar_buf)) { 184 $s = $ar_buf[1]; 185 $results[$s]['model'] = $ar_buf[3]; 186 $results[$s]['media'] = 'Hard Disk'; 187 $results[$s]['capacity'] = $ar_buf[2] * 2048 * 1.049; 188 } elseif (preg_match('/^(acd[0-9]+): (.*) <(.*)> (.*)/', $buf, $ar_buf)) { 189 $s = $ar_buf[1]; 190 $results[$s]['model'] = $ar_buf[3]; 191 $results[$s]['media'] = 'CD-ROM'; 192 } 193 } 194 // return array_values(array_unique($results)); 195 // 1. more useful to have device names 196 // 2. php 4.1.1 array_unique() deletes non-unique values. 197 asort($results); 198 return $results; 199 } 200 201 // place holder function until we add acual usb detection 202 function usb () { 203 return array(); 204 } 205 206 function sbus () { 207 $results = array(); 208 $_results[0] = ""; 209 // TODO. Nothing here yet. Move along. 210 $results = $_results; 211 return $results; 212 } 213 214 function memory () { 215 $s = $this->grab_key('hw.physmem'); 216 217 if (PHP_OS == 'FreeBSD' || PHP_OS == 'OpenBSD') { 218 // vmstat on fbsd 4.4 or greater outputs kbytes not hw.pagesize 219 // I should probably add some version checking here, but for now 220 // we only support fbsd 4.4 221 $pagesize = 1024; 222 } else { 223 $pagesize = $this->grab_key('hw.pagesize'); 224 } 225 226 $results['ram'] = array(); 227 228 $pstat = execute_program('vmstat'); 229 $lines = split("\n", $pstat); 230 for ($i = 0, $max = sizeof($lines); $i < $max; $i++) { 231 $ar_buf = preg_split("/\s+/", $lines[$i], 19); 232 if ($i == 2) { 233 if(PHP_OS == 'NetBSD') { 234 $results['ram']['free'] = $ar_buf[5]; 235 } else { 236 $results['ram']['free'] = $ar_buf[5] * $pagesize / 1024; 237 } 238 } 239 } 240 241 $results['ram']['total'] = $s / 1024; 242 $results['ram']['shared'] = 0; 243 $results['ram']['buffers'] = 0; 244 $results['ram']['used'] = $results['ram']['total'] - $results['ram']['free']; 245 $results['ram']['cached'] = 0; 246 $results['ram']['t_used'] = $results['ram']['used']; 247 $results['ram']['t_free'] = $results['ram']['free']; 248 249 $results['ram']['percent'] = round(($results['ram']['used'] * 100) / $results['ram']['total']); 250 251 if (PHP_OS == 'OpenBSD' || PHP_OS == 'NetBSD') { 252 $pstat = execute_program('swapctl', '-l -k'); 253 } else { 254 $pstat = execute_program('swapinfo', '-k'); 255 } 256 257 $lines = split("\n", $pstat); 258 259 $results['swap']['total'] = 0; 260 $results['swap']['used'] = 0; 261 $results['swap']['free'] = 0; 262 263 for ($i = 1, $max = sizeof($lines); $i < $max; $i++) { 264 $ar_buf = preg_split("/\s+/", $lines[$i], 6); 265 266 if ($ar_buf[0] != 'Total') { 267 $results['swap']['total'] = $results['swap']['total'] + $ar_buf[1]; 268 $results['swap']['used'] = $results['swap']['used'] + $ar_buf[2]; 269 $results['swap']['free'] = $results['swap']['free'] + $ar_buf[3]; 270 271 $results['devswap'][$i - 1] = array(); 272 $results['devswap'][$i - 1]['dev'] = $ar_buf[0]; 273 $results['devswap'][$i - 1]['total'] = $ar_buf[1]; 274 $results['devswap'][$i - 1]['used'] = $ar_buf[2]; 275 $results['devswap'][$i - 1]['free'] = ($results['devswap'][$i - 1]['total'] - $results['devswap'][$i - 1]['used']); 276 $results['devswap'][$i - 1]['percent'] = $ar_buf[2] > 0 ? round(($ar_buf[2] * 100) / $ar_buf[1]) : 0; 277 } 278 } 279 $results['swap']['percent'] = round(($results['swap']['used'] * 100) / $results['swap']['total']); 280 281 if( is_callable( array( 'sysinfo', 'memory_additional' ) ) ) { 282 $results = $this->memory_additional( $results ); 283 } 284 return $results; 285 } 286 287 function filesystems () { 288 return $this->parser->parse_filesystems(); 289 } 290 291 function distro () { 292 $distro = execute_program('uname', '-s'); 293 $result = $distro; 294 return($result); 295 } 296 } 297 298 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 10:22:19 2007 | par Balluche grâce à PHPXref 0.7 |