Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

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