Subversion Repositories ALCASAR

Rev

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