Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezsysinfo.php
Go to the documentation of this file.
1 <?php
3 //
4 // $Id: ezsysinfo.php 6231 2001-07-20 11:30:53Z jakobn $
5 //
6 // This class is based on code from
7 // http://phpsysinfo.sourceforge.net/
8 // Written by:
9 // Uriah Welcome (precision@valinux.com)
10 // Matthew Snelham (infinite@valinux.com)
11 //
12 // Created on: <21-Apr-2001 12:11:59 bf>
13 //
14 // This source file is part of Exponential Basic, publishing software.
15 //
16 // Copyright (C) 1999-2001 eZ Systems. All rights reserved.
17 //
18 // This program is free software; you can redistribute it and/or
19 // modify it under the terms of the GNU General Public License
20 // as published by the Free Software Foundation; either version 2
21 // of the License, or (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US
31 //
32
35
40 class eZSysinfo
41 {
47 static public function compat_array_keys( $arr )
48 {
49 $result=array();
50
51 foreach ($arr as $key => $val)
52 {
53 $result[] = $key;
54 }
55 return $result;
56 }
57
58
64 static public function compat_in_array( $value, $arr )
65 {
66
67 foreach ($arr as $val)
68 {
69 if ( $value == $val )
70 {
71 return true;
72 }
73 }
74 return false;
75 }
76
83 static public function format_bytesize( $kbytes, $dec_places = 2 )
84 {
85 if ( $kbytes > 1048576 )
86 {
87 $result = sprintf("%." . $dec_places . "f", $kbytes / 1048576 );
88 $result .= ' GB';
89 }
90 elseif ( $kbytes > 1024 )
91 {
92 $result = sprintf("%." . $dec_places . "f", $kbytes / 1024 );
93 $result .= ' MB';
94 }
95 else
96 {
97 $result = sprintf("%." . $dec_places . "f", $kbytes );
98 $result .= ' KB';
99 }
100
101 return $result;
102 }
103
104
109 static public function vhostname()
110 {
111 if ( !( $result = getenv( 'SERVER_NAME' ) ) )
112 {
113 $result = "N.A.";
114 }
115
116 return $result;
117 }
118
119
124 static public function chostname()
125 {
126 if ( $fp = fopen('/proc/sys/kernel/hostname','r') )
127 {
128 $result = trim( fgets( $fp, 4096 ) );
129 fclose( $fp );
130 $result = gethostbyaddr( gethostbyname( $result ) );
131 }
132 else
133 {
134 $result = "N.A.";
135 }
136 return $result;
137 }
138
143 static public function ip_addr()
144 {
145 if ( !( $result = getenv( 'SERVER_ADDR' ) ) )
146 {
147 $result = gethostbyname( eZSysinfo::chostname() );
148 }
149
150 return $result;
151 }
152
153
159 static public function pcibus()
160 {
161 $results = array();
162
163 if ( $fd = @fopen("/proc/pci", "r") )
164 {
165 while ( $buf = fgets( $fd, 4096 ) )
166 {
167 if ( preg_match( "/Bus/", $buf ) )
168 {
169 $device = 1;
170 continue;
171 }
172
173 if ( $device )
174 {
175 list($key, $value) = preg_split(": ", $buf, 2);
176
177 if ( !preg_match( "/bridge/i", $key ) && !preg_match( "/USB/i", $key ) )
178 {
179 $results[] = preg_replace("/\‍([^\‍)]+\‍)\.$/", "", trim( $value ) );
180 }
181 $device = 0;
182 }
183 }
184 }
185
186 return $results;
187 }
188
189
196 static public function idebus()
197 {
198 $results = array();
199
200 $handle = @opendir( "/proc/ide" );
201 if( $handle !== false )
202 {
203 while ( $file = readdir($handle) )
204 {
205 if ( preg_match( "/^hd/", $file ) )
206 {
207 $results["$file"] = array();
208
209 if ( file_exists( "/proc/ide/$file/model" ) )
210 if ( $fd = fopen("/proc/ide/$file/model", "r") )
211 {
212 $results["$file"]["model"] = trim( fgets($fd, 4096) );
213 fclose( $fd );
214 }
215 if ( file_exists( "/proc/ide/$file/capacity" ) )
216 if ( $fd = fopen("/proc/ide/$file/capacity", "r") )
217 {
218 $results["$file"]["capacity"] = trim( fgets($fd, 4096) );
219 fclose( $fd );
220 }
221 }
222 }
223 closedir($handle);
224 }
225
226 return $results;
227 }
228
229
230 /*
231 \static
232 Returns an array of all meaningful devices
233 on the SCSI bus.
234 */
235 static public function scsibus()
236 {
237 $results = array();
238 $dev_vendor = "";
239 $dev_model = "";
240 $dev_rev = "";
241 $dev_type = "";
242 $get_type = false;
243
244 if ( file_exists( "/proc/scsi/scsi" ) and $fd = fopen("/proc/scsi/scsi", "r") )
245 {
246 while ( $buf = fgets($fd, 4096))
247 {
248 if ( preg_match( "/Vendor/", $buf ) )
249 {
250 preg_match("/Vendor: (.*) Model: (.*) Rev: (.*)/i", $buf, $dev );
251 list($key, $value) = preg_split("/: /", $buf, 2);
252 $dev_str = $value;
253 $get_type = 1;
254 continue;
255 }
256
257 if ( $get_type )
258 {
259 preg_match("/Type:\s+(\S+)/i", $buf, $dev_type );
260 $results[] = "$dev[1] $dev[2] ( $dev_type[1] )";
261 $get_type = 0;
262 }
263 }
264 }
265
266 return $results;
267 }
268
274 static public function meminfo()
275 {
276 $results = array();
277 $results['ram'] = array();
278 $results['swap'] = array();
279
280 if ( $fd = fopen("/proc/meminfo", "r") )
281 {
282 while ( $buf = fgets( $fd, 4096 ) )
283 {
284 if ( preg_match("/Mem:\s+(.*)$/", $buf, $ar_buf ) )
285 {
286 $ar_buf = preg_split("/\s+/", $ar_buf[1], 6);
287
288 $results['ram'] = array();
289
290 @$results['ram']['total'] = $ar_buf[0] / 1024;
291 @$results['ram']['used'] = $ar_buf[1] / 1024;
292 @$results['ram']['free'] = $ar_buf[2] / 1024;
293 @$results['ram']['shared'] = $ar_buf[3] / 1024;
294 @$results['ram']['buffers'] = $ar_buf[4] / 1024;
295 @$results['ram']['cached'] = $ar_buf[5] / 1024;
296
297 @$results['ram']['t_used'] = $results['ram']['used'] - $results['ram']['cached'] - $results['ram']['buffers'];
298 @$results['ram']['t_free'] = $results['ram']['total'] - $results['ram']['t_used'];
299 @$results['ram']['percent'] = round( ($results['ram']['t_used'] * 100) / $results['ram']['total']);
300 }
301
302 if ( preg_match("/Swap:\s+(.*)$/", $buf, $ar_buf ) )
303 {
304 $ar_buf = preg_split("/\s+/", $ar_buf[1], 3);
305
306 $results['swap'] = array();
307
308 @$results['swap']['total'] = $ar_buf[0] / 1024;
309 @$results['swap']['used'] = $ar_buf[1] / 1024;
310 @$results['swap']['free'] = $ar_buf[2] / 1024;
311 @$results['swap']['percent'] = round( ($ar_buf[1] * 100) / $ar_buf[0] );
312
313 break;
314 }
315 }
316 fclose( $fd );
317
318 }
319 else
320 {
321 @$results['ram'] = array();
322 @$results['swap'] = array();
323 }
324
325 return $results;
326 }
327
328
334 static public function netdevs()
335 {
336 $results = array();
337
338 if ( $fd = fopen("/proc/net/dev", "r") )
339 {
340 while ( $buf = fgets( $fd, 4096 ) )
341 {
342 if ( preg_match( "/:/", $buf ) )
343 {
344 list( $dev_name, $stats_list ) = preg_split( "/:/", $buf, 2 );
345 $stats = preg_split( "/\s+/", trim($stats_list) );
346 $results[$dev_name] = array();
347
348 $results[$dev_name]['rx_bytes'] = $stats[0];
349 $results[$dev_name]['rx_packets'] = $stats[1];
350 $results[$dev_name]['rx_errs'] = $stats[2];
351 $results[$dev_name]['rx_drop'] = $stats[3];
352
353 $results[$dev_name]['tx_bytes'] = $stats[8];
354 $results[$dev_name]['tx_packets'] = $stats[9];
355 $results[$dev_name]['tx_errs'] = $stats[10];
356 $results[$dev_name]['tx_drop'] = $stats[11];
357
358 $results[$dev_name]['errs'] = $stats[2] + $stats[10];
359 $results[$dev_name]['drop'] = $stats[3] + $stats[11];
360 }
361 }
362 }
363
364 return $results;
365 }
366
371 static public function kernel()
372 {
373 if ( $fd = fopen("/proc/version", "r") )
374 {
375 $buf = fgets( $fd, 4096 );
376 fclose( $fd );
377
378 if ( preg_match( "/version (.*?) /", $buf, $ar_buf ) )
379 {
380 $result = $ar_buf[1];
381
382 if ( preg_match("/SMP/", $buf) )
383 {
384 $result .= " (SMP)";
385 }
386 }
387 else
388 {
389 $result = "N.A.";
390 }
391 }
392 else
393 {
394 $result = "N.A.";
395 }
396
397 return $result;
398 }
399
405 static public function loadavg()
406 {
407 if ( $fd = fopen("/proc/loadavg", "r") )
408 {
409 $results = preg_split( "/[|\s:]/", fgets( $fd, 4096 ) );
410 fclose( $fd );
411 }
412 else
413 {
414 $results = array("N.A.","N.A.","N.A.");
415 }
416
417 return $results;
418 }
419
420
426 static public function uptime()
427 {
428 $result = false;
429 $fd = fopen("/proc/uptime", "r");
430 $ar_buf = preg_split( "/[|\s:]/", fgets( $fd, 4096 ) );
431 fclose( $fd );
432
433 $sys_ticks = trim( $ar_buf[0] );
434
435 $min = (int) $sys_ticks / 60;
436 $hours = $min / 60;
437 $days = floor( $hours / 24 );
438 $hours = floor( $hours - ($days * 24) );
439 $min = floor( $min - ($days * 60 * 24) - ($hours * 60) );
440
441 if ( $days != 0 )
442 {
443 $result = "$days days, ";
444 }
445 if ( $hours != 0 )
446 {
447 $result .= "$hours hours, ";
448 }
449 $result .= "$min minutes";
450
451 return $result;
452 }
453
454
459 static public function users()
460 {
461 $result = trim( `who | wc -l` );
462
463 return $result;
464 }
465
466
472 static public function cpu()
473 {
474 $results = array();
475 $ar_buf = array();
476
477 if ( $fd = fopen("/proc/cpuinfo", "r") )
478 {
479 while ( $buf = fgets( $fd, 4096 ) )
480 {
481 list($key, $value) = array_pad( preg_split("/\s+:\s+/", trim($buf), 2), 2, null );
482
483
484 // All of the tags here are highly architecture dependant.
485 // the only way I could reconstruct them for machines I don't
486 // have is to browse the kernel source. So if your arch isn't
487 // supported, tell me you want it written in. <infinite@sigkill.com>
488 switch ( $key ) {
489 case "model name":
490 @$results['model'] = $value;
491 break;
492 case "cpu MHz":
493 @$results['mhz'] = sprintf("%.2f", $value );
494 break;
495 case "clock": // For PPC arch (damn borked POS)
496 @$results['mhz'] = sprintf("%.2f", $value );
497 break;
498 case "cpu": // For PPC arch (damn borked POS)
499 @$results['model'] = $value;
500 break;
501 case "revision": // For PPC arch (damn borked POS)
502 @$results['model'] .= " ( rev: " . $value . ")";
503 break;
504 case "cache size":
505 @$results['cache'] = $value;
506 break;
507 case "bogomips":
508 @$results['bogomips'] += $value;
509 break;
510 case "processor":
511 @$results['cpus'] += 1;
512 break;
513 }
514 }
515 fclose($fd);
516 }
517
518 $keys = eZSysinfo::compat_array_keys( $results );
519 $keys2be = array( "model", "mhz", "cache", "bogomips", "cpus" );
520
521 foreach ($keys2be as $key => $ar_buf)
522 {
523 if (! eZSysinfo::compat_in_array( $ar_buf[1], $keys ) ) $results[$ar_buf[1]] = 'N.A.';
524 }
525
526 return $results;
527 }
528
534 static public function fsinfo()
535 {
536 $results = array();
537 $df = `/bin/df -kP`;
538 $mounts = preg_split( "/(\n)/", $df );
539 $fstype = array();
540
541 if ( $fd = fopen("/proc/mounts", "r") )
542 {
543 while ( $buf = fgets( $fd, 4096 ) )
544 {
545 list($dev, $mpoint, $type ) = preg_split("/\s+/", trim($buf), 4);
546 $fstype[$mpoint] = $type;
547 $fsdev[$dev] = $type;
548 }
549 fclose($fd);
550 }
551
552 for ( $i = 1; $i < sizeof($mounts) - 1; $i++ )
553 {
554 $ar_buf = preg_split("/\s+/", $mounts[$i], 6);
555
556 $results[$i - 1] = array();
557
558 $results[$i - 1]['disk'] = $ar_buf[0];
559 $results[$i - 1]['size'] = $ar_buf[1];
560 $results[$i - 1]['used'] = $ar_buf[2];
561 $results[$i - 1]['free'] = $ar_buf[3];
562 $results[$i - 1]['percent'] = $ar_buf[4];
563 $results[$i - 1]['mount'] = $ar_buf[5];
564 ($fstype[$ar_buf[5]]) ? $results[$i - 1]['fstype'] = $fstype[$ar_buf[5]] : $results[$i - 1]['fstype'] = $fsdev[$ar_buf[0]];
565 }
566
567 return $results;
568 }
569
570 }
571
572 ?>
$i
$i
Definition: admin/adlist.php:91
eZSysinfo
Definition: ezsysinfo.php:41
eZSysinfo\compat_in_array
static compat_in_array( $value, $arr)
Definition: ezsysinfo.php:64
eZSysinfo\idebus
static idebus()
Definition: ezsysinfo.php:196
eZSysinfo\compat_array_keys
static compat_array_keys( $arr)
Definition: ezsysinfo.php:47
eZSysinfo\loadavg
static loadavg()
Definition: ezsysinfo.php:405
eZSysinfo\format_bytesize
static format_bytesize( $kbytes, $dec_places=2)
Definition: ezsysinfo.php:83
eZSysinfo\fsinfo
static fsinfo()
Definition: ezsysinfo.php:534
eZSysinfo\meminfo
static meminfo()
Definition: ezsysinfo.php:274
eZSysinfo\chostname
static chostname()
Definition: ezsysinfo.php:124
eZSysinfo\cpu
static cpu()
Definition: ezsysinfo.php:472
eZSysinfo\kernel
static kernel()
Definition: ezsysinfo.php:371
eZSysinfo\vhostname
static vhostname()
Definition: ezsysinfo.php:109
eZSysinfo\pcibus
static pcibus()
Definition: ezsysinfo.php:159
eZSysinfo\scsibus
static scsibus()
Definition: ezsysinfo.php:235
eZSysinfo\netdevs
static netdevs()
Definition: ezsysinfo.php:334
eZSysinfo\ip_addr
static ip_addr()
Definition: ezsysinfo.php:143
eZSysinfo\uptime
static uptime()
Definition: ezsysinfo.php:426
eZSysinfo\users
static users()
Definition: ezsysinfo.php:459
$type
$type
Definition: classes/admin/linkselect.php:220
$result
$result
Definition: companysearch.php:94
$results
$results
Definition: companysearch.php:81
file_exists
static file_exists( $inifilename)
Definition: eZPBINIFile.php:195
$fp
$fp
Definition: export.php:97
$file
$file
Definition: ezfilemanager/user/filedownload.php:35
$ar_buf
$ar_buf
Definition: hwinfo.php:55
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.