Subversion Repositories ALCASAR

Rev

Rev 1764 | Go to most recent revision | Details | Last modification | View Log

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