Subversion Repositories ALCASAR

Rev

Rev 3037 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * BSDCommon Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI BSDCommon OS class
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12
 * @version   SVN: $Id: class.BSDCommon.inc.php 621 2012-07-29 18:49:04Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * BSDCommon class
17
 * get all the required information for BSD Like systems
18
 * no need to implement in every class the same methods
19
 *
20
 * @category  PHP
21
 * @package   PSI BSDCommon OS class
22
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
23
 * @copyright 2009 phpSysInfo
24
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
25
 * @version   Release: 3.0
26
 * @link      http://phpsysinfo.sourceforge.net
27
 */
28
abstract class BSDCommon extends OS
29
{
30
    /**
3037 rexy 31
     * Assoc array of all CPUs loads.
32
     */
33
    private $_cpu_loads = null;
34
 
35
    /**
2770 rexy 36
     * content of the syslog
37
     *
38
     * @var array
39
     */
40
    private $_dmesg = null;
41
 
42
    /**
43
     * regexp1 for cpu information out of the syslog
44
     *
45
     * @var string
46
     */
47
    private $_CPURegExp1 = "//";
48
 
49
    /**
50
     * regexp2 for cpu information out of the syslog
51
     *
52
     * @var string
53
     */
54
    private $_CPURegExp2 = "//";
55
 
56
    /**
57
     * regexp1 for scsi information out of the syslog
58
     *
59
     * @var string
60
     */
61
    private $_SCSIRegExp1 = "//";
62
 
63
    /**
64
     * regexp2 for scsi information out of the syslog
65
     *
66
     * @var string
67
     */
68
    private $_SCSIRegExp2 = "//";
69
 
70
    /**
71
     * regexp3 for scsi information out of the syslog
72
     *
73
     * @var string
74
     */
75
    private $_SCSIRegExp3 = "//";
76
 
77
    /**
78
     * regexp1 for pci information out of the syslog
79
     *
80
     * @var string
81
     */
82
    private $_PCIRegExp1 = "//";
83
 
84
    /**
85
     * regexp1 for pci information out of the syslog
86
     *
87
     * @var string
88
     */
89
    private $_PCIRegExp2 = "//";
90
 
91
    /**
92
     * setter for cpuregexp1
93
     *
94
     * @param string $value value to set
95
     *
96
     * @return void
97
     */
98
    protected function setCPURegExp1($value)
99
    {
100
        $this->_CPURegExp1 = $value;
101
    }
102
 
103
    /**
104
     * setter for cpuregexp2
105
     *
106
     * @param string $value value to set
107
     *
108
     * @return void
109
     */
110
    protected function setCPURegExp2($value)
111
    {
112
        $this->_CPURegExp2 = $value;
113
    }
114
 
115
    /**
116
     * setter for scsiregexp1
117
     *
118
     * @param string $value value to set
119
     *
120
     * @return void
121
     */
122
    protected function setSCSIRegExp1($value)
123
    {
124
        $this->_SCSIRegExp1 = $value;
125
    }
126
 
127
    /**
128
     * setter for scsiregexp2
129
     *
130
     * @param string $value value to set
131
     *
132
     * @return void
133
     */
134
    protected function setSCSIRegExp2($value)
135
    {
136
        $this->_SCSIRegExp2 = $value;
137
    }
138
 
139
    /**
140
     * setter for scsiregexp3
141
     *
142
     * @param string $value value to set
143
     *
144
     * @return void
145
     */
146
    protected function setSCSIRegExp3($value)
147
    {
148
        $this->_SCSIRegExp3 = $value;
149
    }
150
 
151
    /**
152
     * setter for pciregexp1
153
     *
154
     * @param string $value value to set
155
     *
156
     * @return void
157
     */
158
    protected function setPCIRegExp1($value)
159
    {
160
        $this->_PCIRegExp1 = $value;
161
    }
162
 
163
    /**
164
     * setter for pciregexp2
165
     *
166
     * @param string $value value to set
167
     *
168
     * @return void
169
     */
170
    protected function setPCIRegExp2($value)
171
    {
172
        $this->_PCIRegExp2 = $value;
173
    }
174
 
175
    /**
176
     * read /var/run/dmesg.boot, but only if we haven't already
177
     *
178
     * @return array
179
     */
180
    protected function readdmesg()
181
    {
182
        if ($this->_dmesg === null) {
2976 rexy 183
            if ((PSI_OS != 'Darwin') && (CommonFunctions::rfts('/var/run/dmesg.boot', $buf, 0, 4096, false) || CommonFunctions::rfts('/var/log/dmesg.boot', $buf, 0, 4096, false) || CommonFunctions::rfts('/var/run/dmesg.boot', $buf))) {  // Once again but with debug
2770 rexy 184
                $parts = preg_split("/rebooting|Uptime/", $buf, -1, PREG_SPLIT_NO_EMPTY);
185
                $this->_dmesg = preg_split("/\n/", $parts[count($parts) - 1], -1, PREG_SPLIT_NO_EMPTY);
186
            } else {
187
                $this->_dmesg = array();
188
            }
189
        }
190
 
191
        return $this->_dmesg;
192
    }
193
 
194
    /**
195
     * get a value from sysctl command
196
     *
197
     * @param string $key key for the value to get
198
     *
199
     * @return string
200
     */
201
    protected function grabkey($key)
202
    {
203
        $buf = "";
204
        if (CommonFunctions::executeProgram('sysctl', "-n $key", $buf, PSI_DEBUG)) {
205
            return $buf;
206
        } else {
207
            return '';
208
        }
209
    }
210
 
211
    /**
212
     * Virtual Host Name
213
     *
214
     * @return void
215
     */
216
    protected function hostname()
217
    {
3037 rexy 218
        if (PSI_USE_VHOST) {
2770 rexy 219
            if (CommonFunctions::readenv('SERVER_NAME', $hnm)) $this->sys->setHostname($hnm);
220
        } else {
221
            if (CommonFunctions::executeProgram('hostname', '', $buf, PSI_DEBUG)) {
222
                $this->sys->setHostname($buf);
223
            }
224
        }
225
    }
226
 
227
    /**
228
     * Kernel Version
229
     *
230
     * @return void
231
     */
232
    protected function kernel()
233
    {
234
        $s = $this->grabkey('kern.version');
3100 rexy 235
        $a = preg_split('/:/', $s, 4);
236
        if (isset($a[3])) {
237
            if (preg_match('/^(\d{2} [A-Z]{3});/', $a[3], $abuf) // eg. 19:58 GMT;...
238
               || preg_match('/^(\d{2} [A-Z]{3} \d{4})/', $a[3], $abuf)) { // eg. 26:31 PDT 2019...
239
                $this->sys->setKernel($a[0].$a[1].':'.$a[2].':'.$abuf[1]);
240
            } else {
241
                $this->sys->setKernel($a[0].$a[1].':'.$a[2]);
242
            }
243
        } elseif (isset($a[2])) {
2770 rexy 244
            $this->sys->setKernel($a[0].$a[1].':'.$a[2]);
245
        } else {
246
            $this->sys->setKernel($s);
247
        }
248
    }
249
 
250
    /**
3037 rexy 251
     * Virtualizer info
2770 rexy 252
     *
253
     * @return void
254
     */
3037 rexy 255
    private function virtualizer()
2770 rexy 256
    {
3037 rexy 257
        if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
258
            $testvirt = $this->sys->getVirtualizer();
259
            $novm = true;
260
            foreach ($testvirt as $virtkey=>$virtvalue) if ($virtvalue) {
261
                $novm = false;
262
                break;
263
            }
264
            // Detect QEMU cpu
265
            if ($novm && isset($testvirt["cpuid:QEMU"])) {
266
                $this->sys->setVirtualizer('qemu'); // QEMU
267
                $novm = false;
268
            }
269
 
270
            if ($novm && isset($testvirt["hypervisor"])) {
271
                $this->sys->setVirtualizer('unknown');
272
            }
273
        }
274
    }
275
 
276
    /**
277
     * CPU usage
278
     *
279
     * @return void
280
     */
281
    protected function cpuusage()
282
    {
283
        if (($this->_cpu_loads === null)) {
284
            $this->_cpu_loads = array();
2976 rexy 285
            if (PSI_OS != 'Darwin') {
286
                if ($fd = $this->grabkey('kern.cp_time')) {
287
                    // Find out the CPU load
288
                    // user + sys = load
289
                    // total = total
290
                    if (preg_match($this->_CPURegExp2, $fd, $res) && (sizeof($res) > 4)) {
291
                        $load = $res[2] + $res[3] + $res[4]; // cpu.user + cpu.sys
292
                        $total = $res[2] + $res[3] + $res[4] + $res[5]; // cpu.total
293
                        // we need a second value, wait 1 second befor getting (< 1 second no good value will occour)
294
                        sleep(1);
295
                        $fd = $this->grabkey('kern.cp_time');
296
                        if (preg_match($this->_CPURegExp2, $fd, $res) && (sizeof($res) > 4)) {
297
                            $load2 = $res[2] + $res[3] + $res[4];
298
                            $total2 = $res[2] + $res[3] + $res[4] + $res[5];
3037 rexy 299
                            if ($total2 != $total) {
300
                                $this->_cpu_loads['cpu'] = (100 * ($load2 - $load)) / ($total2 - $total);
301
                            } else {
302
                                $this->_cpu_loads['cpu'] = 0;
303
                            }
2976 rexy 304
                        }
305
                    }
306
                }
307
            } else {
308
                $ncpu = $this->grabkey('hw.ncpu');
3037 rexy 309
                if (($ncpu !== "") && ($ncpu >= 1) && CommonFunctions::executeProgram('ps', "-A -o %cpu", $pstable, false) && !empty($pstable)) {
2976 rexy 310
                    $pslines = preg_split("/\n/", $pstable, -1, PREG_SPLIT_NO_EMPTY);
311
                    if (!empty($pslines) && (count($pslines)>1) && (trim($pslines[0])==="%CPU")) {
312
                        array_shift($pslines);
313
                        $sum = 0;
314
                        foreach ($pslines as $psline) {
3100 rexy 315
                            $sum+=str_replace(',', '.', trim($psline));
2976 rexy 316
                        }
3037 rexy 317
                        $this->_cpu_loads['cpu'] = min($sum/$ncpu, 100);
2976 rexy 318
                    }
319
                }
2770 rexy 320
            }
321
        }
3037 rexy 322
 
323
        if (isset($this->_cpu_loads['cpu'])) {
324
            return $this->_cpu_loads['cpu'];
325
        } else {
326
            return null;
327
        }
2770 rexy 328
    }
329
 
330
    /**
3037 rexy 331
     * Processor Load
332
     * optionally create a loadbar
333
     *
334
     * @return void
335
     */
336
    protected function loadavg()
337
    {
338
        $s = $this->grabkey('vm.loadavg');
339
        $s = preg_replace('/{ /', '', $s);
340
        $s = preg_replace('/ }/', '', $s);
3100 rexy 341
        $s = str_replace(',', '.', $s);
3037 rexy 342
        $this->sys->setLoad($s);
343
 
344
        if (PSI_LOAD_BAR) {
345
            $this->sys->setLoadPercent($this->cpuusage());
346
        }
347
    }
348
 
349
    /**
2770 rexy 350
     * CPU information
351
     *
352
     * @return void
353
     */
354
    protected function cpuinfo()
355
    {
356
        $dev = new CpuDevice();
3037 rexy 357
        $cpumodel = $this->grabkey('hw.model');
358
        $dev->setModel($cpumodel);
359
        if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && preg_match('/^QEMU Virtual CPU version /', $cpumodel)) {
360
            $this->sys->setVirtualizer("cpuid:QEMU", false);
2770 rexy 361
        }
362
 
363
        $notwas = true;
364
        foreach ($this->readdmesg() as $line) {
365
            if ($notwas) {
3037 rexy 366
               $regexps = preg_split("/\n/", $this->_CPURegExp1, -1, PREG_SPLIT_NO_EMPTY); // multiple regexp separated by \n
367
               foreach ($regexps as $regexp) {
368
                   if (preg_match($regexp, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
3100 rexy 369
                        if ($dev->getCpuSpeed() == 0) {
3037 rexy 370
                            $dev->setCpuSpeed(round($ar_buf[2]));
371
                        }
372
                        $notwas = false;
373
                        break;
2770 rexy 374
                    }
375
                }
376
            } else {
3037 rexy 377
                if (preg_match("/^\s+Origin| Features/", $line, $ar_buf)) {
378
                    if (preg_match("/^\s+Origin[ ]*=[ ]*\"(.+)\"/", $line, $ar_buf)) {
379
                        $dev->setVendorId($ar_buf[1]);
380
                    } elseif (preg_match("/ Features2[ ]*=.*<(.+)>/", $line, $ar_buf)) {
2770 rexy 381
                        $feats = preg_split("/,/", strtolower(trim($ar_buf[1])), -1, PREG_SPLIT_NO_EMPTY);
382
                        foreach ($feats as $feat) {
383
                            if (($feat=="vmx") || ($feat=="svm")) {
384
                                $dev->setVirt($feat);
3037 rexy 385
                            } elseif ($feat=="hv") {
386
                                if ($dev->getVirt() === null) {
387
                                    $dev->setVirt('hypervisor');
388
                                }
389
                                if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
390
                                    $this->sys->setVirtualizer("hypervisor", false);
391
                                }
2770 rexy 392
                            }
393
                        }
394
                    }
395
                } else break;
396
            }
397
        }
398
 
399
        $ncpu = $this->grabkey('hw.ncpu');
3037 rexy 400
        if (($ncpu === "") || !($ncpu >= 1)) {
2770 rexy 401
            $ncpu = 1;
3037 rexy 402
        }
403
        if (($ncpu == 1) && PSI_LOAD_BAR) {
404
            $dev->setLoad($this->cpuusage());
405
        }
2770 rexy 406
        for ($ncpu ; $ncpu > 0 ; $ncpu--) {
407
            $this->sys->setCpus($dev);
408
        }
409
    }
410
 
411
    /**
3037 rexy 412
     * Machine information
413
     *
414
     * @return void
415
     */
416
    private function machine()
417
    {
418
        if ((PSI_OS == 'NetBSD') || (PSI_OS == 'OpenBSD')) {
419
            $buffer = array();
420
            if (PSI_OS == 'NetBSD') { // NetBSD
421
                $buffer['Manufacturer'] = $this->grabkey('machdep.dmi.system-vendor');
422
                $buffer['Model'] = $this->grabkey('machdep.dmi.system-product');
423
                $buffer['Product'] = $this->grabkey('machdep.dmi.board-product');
424
                $buffer['SMBIOSBIOSVersion'] = $this->grabkey('machdep.dmi.bios-version');
425
                $buffer['ReleaseDate'] = $this->grabkey('machdep.dmi.bios-date');
426
            } else { // OpenBSD
427
                $buffer['Manufacturer'] = $this->grabkey('hw.vendor');
428
                $buffer['Model'] = $this->grabkey('hw.product');
429
                $buffer['Product'] = "";
430
                $buffer['SMBIOSBIOSVersion'] = "";
431
                $buffer['ReleaseDate'] = "";
432
            }
433
            if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
434
                $vendor_array = array();
435
                $vendor_array[] = $buffer['Model'];
436
                $vendor_array[] = trim($buffer['Manufacturer']." ".$buffer['Model']);
437
                if (PSI_OS == 'NetBSD') { // NetBSD
438
                    $vendor_array[] = $this->grabkey('machdep.dmi.board-vendor');
439
                    $vendor_array[] = $this->grabkey('machdep.dmi.bios-vendor');
440
                }
441
                $virt = CommonFunctions::decodevirtualizer($vendor_array);
442
                if ($virt !== null) {
443
                    $this->sys->setVirtualizer($virt);
444
                }
445
            }
446
 
447
            $buf = "";
448
            if (($buffer['Manufacturer'] !== "") && !preg_match("/^To be filled by O\.E\.M\.$|^System manufacturer$|^Not Specified$/i", $buf2=trim($buffer['Manufacturer'])) && ($buf2 !== "")) {
449
                $buf .= ' '.$buf2;
450
            }
451
 
452
            if (($buffer['Model'] !== "") && !preg_match("/^To be filled by O\.E\.M\.$|^System Product Name$|^Not Specified$/i", $buf2=trim($buffer['Model'])) && ($buf2 !== "")) {
453
                $model = $buf2;
454
                $buf .= ' '.$buf2;
455
            }
456
            if (($buffer['Product'] !== "") && !preg_match("/^To be filled by O\.E\.M\.$|^BaseBoard Product Name$|^Not Specified$|^Default string$/i", $buf2=trim($buffer['Product'])) && ($buf2 !== "")) {
457
                if ($buf2 !== $model) {
458
                    $buf .= '/'.$buf2;
459
                } elseif (isset($buffer['SystemFamily']) && !preg_match("/^To be filled by O\.E\.M\.$|^System Family$|^Not Specified$/i", $buf2=trim($buffer['SystemFamily'])) && ($buf2 !== "")) {
460
                    $buf .= '/'.$buf2;
461
                }
462
            }
463
 
464
            $bver = "";
465
            $brel = "";
466
            if (($buf2=trim($buffer['SMBIOSBIOSVersion'])) !== "") {
467
                $bver .= ' '.$buf2;
468
            }
469
            if ($buffer['ReleaseDate'] !== "") {
470
                if (preg_match("/^(\d{4})(\d{2})(\d{2})$/", $buffer['ReleaseDate'], $dateout)) {
471
                    $brel .= ' '.$dateout[2].'/'.$dateout[3].'/'.$dateout[1];
472
                } elseif (preg_match("/^\d{2}\/\d{2}\/\d{4}$/", $buffer['ReleaseDate'])) {
473
                    $brel .= ' '.$buffer['ReleaseDate'];
474
                }
475
            }
476
            if ((trim($bver) !== "") || (trim($brel) !== "")) {
477
                $buf .= ', BIOS'.$bver.$brel;
478
            }
479
 
480
            if (trim($buf) !== "") {
481
                $this->sys->setMachine(trim($buf));
482
            }
483
        } elseif ((PSI_OS == 'FreeBSD') && defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
484
            $vendorid = $this->grabkey('hw.hv_vendor');
485
            if (trim($vendorid) === "") {
486
                foreach ($this->readdmesg() as $line) if (preg_match("/^Hypervisor: Origin = \"(.+)\"/", $line, $ar_buf)) {
487
                    if (trim($ar_buf[1]) !== "") {
488
                        $vendorid = $ar_buf[1];
489
                    }
490
                    break;
491
                }
492
            }
493
            if (trim($vendorid) !== "") {
494
                $virt = CommonFunctions::decodevirtualizer($vendorid);
495
                if ($virt !== null) {
496
                    $this->sys->setVirtualizer($virt);
497
                } else {
498
                    $this->sys->setVirtualizer('unknown');
499
                }
500
            }
501
        }
502
    }
503
 
504
    /**
2770 rexy 505
     * SCSI devices
506
     * get the scsi device information out of dmesg
507
     *
508
     * @return void
509
     */
510
    protected function scsi()
511
    {
512
        foreach ($this->readdmesg() as $line) {
2976 rexy 513
            if (preg_match($this->_SCSIRegExp1, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
2770 rexy 514
                $dev = new HWDevice();
515
                $dev->setName($ar_buf[1].": ".trim($ar_buf[2]));
516
                $this->sys->setScsiDevices($dev);
2976 rexy 517
            } elseif (preg_match($this->_SCSIRegExp2, $line, $ar_buf) && (sizeof($ar_buf) > 1)) {
2770 rexy 518
                /* duplication security */
519
                $notwas = true;
520
                foreach ($this->sys->getScsiDevices() as $finddev) {
521
                    if ($notwas && (substr($finddev->getName(), 0, strpos($finddev->getName(), ': ')) == $ar_buf[1])) {
522
                        if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
523
                            if (isset($ar_buf[3]) && ($ar_buf[3]==="G")) {
524
                                $finddev->setCapacity($ar_buf[2] * 1024 * 1024 * 1024);
2976 rexy 525
                            } elseif (isset($ar_buf[2])) {
2770 rexy 526
                                $finddev->setCapacity($ar_buf[2] * 1024 * 1024);
527
                            }
528
                        }
529
                        $notwas = false;
530
                        break;
531
                    }
532
                }
533
                if ($notwas) {
534
                    $dev = new HWDevice();
535
                    $dev->setName($ar_buf[1]);
536
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
537
                        if (isset($ar_buf[3]) && ($ar_buf[3]==="G")) {
538
                                $dev->setCapacity($ar_buf[2] * 1024 * 1024 * 1024);
2976 rexy 539
                            } elseif (isset($ar_buf[2])) {
2770 rexy 540
                                $dev->setCapacity($ar_buf[2] * 1024 * 1024);
541
                            }
542
                    }
543
                    $this->sys->setScsiDevices($dev);
544
                }
2976 rexy 545
            } elseif (preg_match($this->_SCSIRegExp3, $line, $ar_buf) && (sizeof($ar_buf) > 1)) {
2770 rexy 546
                /* duplication security */
547
                $notwas = true;
548
                foreach ($this->sys->getScsiDevices() as $finddev) {
549
                    if ($notwas && (substr($finddev->getName(), 0, strpos($finddev->getName(), ': ')) == $ar_buf[1])) {
550
                        if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
551
                           && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
2976 rexy 552
                            if (isset($ar_buf[2])) $finddev->setSerial(trim($ar_buf[2]));
2770 rexy 553
                        }
554
                        $notwas = false;
555
                        break;
556
                    }
557
                }
558
                if ($notwas) {
559
                    $dev = new HWDevice();
560
                    $dev->setName($ar_buf[1]);
561
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
562
                       && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
2976 rexy 563
                        if (isset($ar_buf[2])) $dev->setSerial(trim($ar_buf[2]));
2770 rexy 564
                    }
565
                    $this->sys->setScsiDevices($dev);
566
                }
567
            }
568
        }
569
        /* cleaning */
570
        foreach ($this->sys->getScsiDevices() as $finddev) {
3037 rexy 571
            if (strpos($finddev->getName(), ': ') !== false)
572
                $finddev->setName(substr(strstr($finddev->getName(), ': '), 2));
2770 rexy 573
        }
574
    }
575
 
576
    /**
577
     * parsing the output of pciconf command
578
     *
579
     * @return Array
580
     */
581
    protected function pciconf()
582
    {
583
        $arrResults = array();
584
        $intS = 0;
585
        if (CommonFunctions::executeProgram("pciconf", "-lv", $strBuf, PSI_DEBUG)) {
586
            $arrTemp = array();
587
            $arrBlocks = preg_split("/\n\S/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
588
            foreach ($arrBlocks as $strBlock) {
589
                $arrLines = preg_split("/\n/", $strBlock, -1, PREG_SPLIT_NO_EMPTY);
590
                $vend = null;
591
                foreach ($arrLines as $strLine) {
592
                    if (preg_match("/\sclass=0x([a-fA-F0-9]{4})[a-fA-F0-9]{2}\s.*\schip=0x([a-fA-F0-9]{4})([a-fA-F0-9]{4})\s/", $strLine, $arrParts)) {
593
                        $arrTemp[$intS] = 'Class '.$arrParts[1].': Device '.$arrParts[3].':'.$arrParts[2];
594
                        $vend = '';
595
                    } elseif (preg_match("/(.*) = '(.*)'/", $strLine, $arrParts)) {
596
                        if (trim($arrParts[1]) == "vendor") {
597
                            $vend = trim($arrParts[2]);
598
                        } elseif (trim($arrParts[1]) == "device") {
599
                            if (($vend !== null) && ($vend !== '')) {
600
                                $arrTemp[$intS] = $vend." - ".trim($arrParts[2]);
601
                            } else {
602
                                $arrTemp[$intS] = trim($arrParts[2]);
603
                                $vend = '';
604
                            }
605
                        }
606
                    }
607
                }
608
                if ($vend !== null) {
609
                    $intS++;
610
                }
611
            }
612
            foreach ($arrTemp as $name) {
613
                $dev = new HWDevice();
614
                $dev->setName($name);
615
                $arrResults[] = $dev;
616
            }
617
        }
618
 
619
        return $arrResults;
620
    }
621
 
622
    /**
623
     * PCI devices
624
     * get the pci device information out of dmesg
625
     *
626
     * @return void
627
     */
628
    protected function pci()
629
    {
630
        if ((!$results = Parser::lspci(false)) && (!$results = $this->pciconf())) {
631
            foreach ($this->readdmesg() as $line) {
2976 rexy 632
                if (preg_match($this->_PCIRegExp1, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
2770 rexy 633
                    $dev = new HWDevice();
634
                    $dev->setName($ar_buf[1].": ".$ar_buf[2]);
635
                    $results[] = $dev;
2976 rexy 636
                } elseif (preg_match($this->_PCIRegExp2, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
2770 rexy 637
                    $dev = new HWDevice();
638
                    $dev->setName($ar_buf[1].": ".$ar_buf[2]);
639
                    $results[] = $dev;
640
                }
641
            }
642
        }
643
        foreach ($results as $dev) {
644
            $this->sys->setPciDevices($dev);
645
        }
646
    }
647
 
648
    /**
649
     * IDE devices
650
     * get the ide device information out of dmesg
651
     *
652
     * @return void
653
     */
654
    protected function ide()
655
    {
656
        foreach ($this->readdmesg() as $line) {
657
            if (preg_match('/^(ad[0-9]+): (.*)MB <(.*)> (.*) (.*)/', $line, $ar_buf)) {
658
                $dev = new HWDevice();
659
                $dev->setName($ar_buf[1].": ".trim($ar_buf[3]));
660
                if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
661
                    $dev->setCapacity($ar_buf[2] * 1024 * 1024);
662
                }
663
                $this->sys->setIdeDevices($dev);
664
            } elseif (preg_match('/^(acd[0-9]+): (.*) <(.*)> (.*)/', $line, $ar_buf)) {
665
                $dev = new HWDevice();
666
                $dev->setName($ar_buf[1].": ".trim($ar_buf[3]));
667
                $this->sys->setIdeDevices($dev);
668
            } elseif (preg_match('/^(ada[0-9]+): <(.*)> (.*)/', $line, $ar_buf)) {
669
                $dev = new HWDevice();
670
                $dev->setName($ar_buf[1].": ".trim($ar_buf[2]));
671
                $this->sys->setIdeDevices($dev);
672
            } elseif (preg_match('/^(ada[0-9]+): (.*)MB \((.*)\)/', $line, $ar_buf)) {
673
                /* duplication security */
674
                $notwas = true;
675
                foreach ($this->sys->getIdeDevices() as $finddev) {
676
                    if ($notwas && (substr($finddev->getName(), 0, strpos($finddev->getName(), ': ')) == $ar_buf[1])) {
677
                        if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
678
                            $finddev->setCapacity($ar_buf[2] * 1024 * 1024);
679
                        }
680
                        $notwas = false;
681
                        break;
682
                    }
683
                }
684
                if ($notwas) {
685
                    $dev = new HWDevice();
686
                    $dev->setName($ar_buf[1]);
687
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
688
                        $dev->setCapacity($ar_buf[2] * 1024 * 1024);
689
                    }
690
                    $this->sys->setIdeDevices($dev);
691
                }
692
            } elseif (preg_match('/^(ada[0-9]+): Serial Number (.*)/', $line, $ar_buf)) {
693
                /* duplication security */
694
                $notwas = true;
695
                foreach ($this->sys->getIdeDevices() as $finddev) {
696
                    if ($notwas && (substr($finddev->getName(), 0, strpos($finddev->getName(), ': ')) == $ar_buf[1])) {
697
                        if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
698
                           && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
699
                            $finddev->setSerial(trim($ar_buf[2]));
700
                        }
701
                        $notwas = false;
702
                        break;
703
                    }
704
                }
705
                if ($notwas) {
706
                    $dev = new HWDevice();
707
                    $dev->setName($ar_buf[1]);
708
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
709
                       && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
710
                        $finddev->setSerial(trim($ar_buf[2]));
711
                    }
712
                    $this->sys->setIdeDevices($dev);
713
                }
714
            }
715
        }
716
        /* cleaning */
717
        foreach ($this->sys->getIdeDevices() as $finddev) {
718
                    if (strpos($finddev->getName(), ': ') !== false)
719
                        $finddev->setName(substr(strstr($finddev->getName(), ': '), 2));
720
        }
721
    }
722
 
723
    /**
724
     * Physical memory information and Swap Space information
725
     *
726
     * @return void
727
     */
728
    protected function memory()
729
    {
730
        if (PSI_OS == 'FreeBSD' || PSI_OS == 'OpenBSD') {
731
            // vmstat on fbsd 4.4 or greater outputs kbytes not hw.pagesize
732
            // I should probably add some version checking here, but for now
733
            // we only support fbsd 4.4
734
            $pagesize = 1024;
735
        } else {
736
            $pagesize = $this->grabkey('hw.pagesize');
737
        }
738
        if (CommonFunctions::executeProgram('vmstat', '', $vmstat, PSI_DEBUG)) {
739
            $lines = preg_split("/\n/", $vmstat, -1, PREG_SPLIT_NO_EMPTY);
740
            $ar_buf = preg_split("/\s+/", trim($lines[2]), 19);
741
            if (PSI_OS == 'NetBSD' || PSI_OS == 'DragonFly') {
742
                $this->sys->setMemFree($ar_buf[4] * 1024);
743
            } else {
744
                $this->sys->setMemFree($ar_buf[4] * $pagesize);
745
            }
746
            $this->sys->setMemTotal($this->grabkey('hw.physmem'));
747
            $this->sys->setMemUsed($this->sys->getMemTotal() - $this->sys->getMemFree());
748
 
749
            if (((PSI_OS == 'OpenBSD' || PSI_OS == 'NetBSD') && CommonFunctions::executeProgram('swapctl', '-l -k', $swapstat, PSI_DEBUG)) || CommonFunctions::executeProgram('swapinfo', '-k', $swapstat, PSI_DEBUG)) {
750
                $lines = preg_split("/\n/", $swapstat, -1, PREG_SPLIT_NO_EMPTY);
751
                foreach ($lines as $line) {
752
                    $ar_buf = preg_split("/\s+/", $line, 6);
753
                    if (($ar_buf[0] != 'Total') && ($ar_buf[0] != 'Device')) {
754
                        $dev = new DiskDevice();
755
                        $dev->setMountPoint($ar_buf[0]);
756
                        $dev->setName("SWAP");
757
                        $dev->setFsType('swap');
758
                        $dev->setTotal($ar_buf[1] * 1024);
759
                        $dev->setUsed($ar_buf[2] * 1024);
760
                        $dev->setFree($dev->getTotal() - $dev->getUsed());
761
                        $this->sys->setSwapDevices($dev);
762
                    }
763
                }
764
            }
765
        }
766
    }
767
 
768
    /**
769
     * USB devices
770
     * get the ide device information out of dmesg
771
     *
772
     * @return void
773
     */
774
    protected function usb()
775
    {
2976 rexy 776
        $notwas = true;
777
        if ((PSI_OS == 'FreeBSD') && CommonFunctions::executeProgram('usbconfig', '', $bufr, false)) {
778
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
779
            foreach ($lines as $line) {
780
                if (preg_match('/^(ugen[0-9]+\.[0-9]+): <([^,]*)(.*)> at (usbus[0-9]+)/', $line, $ar_buf)) {
781
                    $notwas = false;
782
                    $dev = new HWDevice();
783
                    $dev->setName($ar_buf[2]);
784
                    $this->sys->setUSBDevices($dev);
785
                }
786
            }
787
        }
788
        if ($notwas) foreach ($this->readdmesg() as $line) {
2770 rexy 789
//            if (preg_match('/^(ugen[0-9\.]+): <(.*)> (.*) (.*)/', $line, $ar_buf)) {
790
//                    $dev->setName($ar_buf[1].": ".$ar_buf[2]);
791
            if (preg_match('/^(u[a-z]+[0-9]+): <([^,]*)(.*)> on (usbus[0-9]+)/', $line, $ar_buf)) {
792
                    $dev = new HWDevice();
793
                    $dev->setName($ar_buf[2]);
794
                    $this->sys->setUSBDevices($dev);
795
            }
796
        }
797
    }
798
 
799
    /**
800
     * filesystem information
801
     *
802
     * @return void
803
     */
804
    protected function filesystems()
805
    {
806
        $arrResult = Parser::df();
807
        foreach ($arrResult as $dev) {
808
            $this->sys->setDiskDevices($dev);
809
        }
810
    }
811
 
812
    /**
813
     * Distribution
814
     *
815
     * @return void
816
     */
817
    protected function distro()
818
    {
819
        if (CommonFunctions::executeProgram('uname', '-s', $result, PSI_DEBUG)) {
820
            $this->sys->setDistribution($result);
821
        }
822
    }
823
 
824
    /**
3037 rexy 825
     * UpTime
826
     * time the system is running
827
     *
828
     * @return void
829
     */
830
    private function uptime()
831
    {
832
        if ($kb = $this->grabkey('kern.boottime')) {
833
            if (preg_match("/sec = ([0-9]+)/", $kb, $buf)) { // format like: { sec = 1096732600, usec = 885425 } Sat Oct 2 10:56:40 2004
834
                $this->sys->setUptime(time() - $buf[1]);
835
            } else {
836
                date_default_timezone_set('UTC');
837
                $kbt = strtotime($kb);
838
                if (($kbt !== false) && ($kbt != -1)) {
839
                    $this->sys->setUptime(time() - $kbt); // format like: Sat Oct 2 10:56:40 2004
840
                } else {
841
                    $this->sys->setUptime(time() - $kb); // format like: 1096732600
842
                }
843
            }
844
        }
845
    }
846
 
847
    /**
2770 rexy 848
     * get the information
849
     *
850
     * @see PSI_Interface_OS::build()
851
     *
3037 rexy 852
     * @return void
2770 rexy 853
     */
854
    public function build()
855
    {
856
        if (!$this->blockname || $this->blockname==='vitals') {
857
            $this->distro();
858
            $this->hostname();
859
            $this->kernel();
860
            $this->_users();
861
            $this->loadavg();
3037 rexy 862
            $this->uptime();
2770 rexy 863
        }
864
        if (!$this->blockname || $this->blockname==='hardware') {
3037 rexy 865
            $this->machine();
2770 rexy 866
            $this->cpuinfo();
3037 rexy 867
            $this->virtualizer();
2770 rexy 868
            $this->pci();
869
            $this->ide();
870
            $this->scsi();
871
            $this->usb();
872
        }
873
        if (!$this->blockname || $this->blockname==='memory') {
874
            $this->memory();
875
        }
876
        if (!$this->blockname || $this->blockname==='filesystem') {
877
            $this->filesystems();
878
        }
879
    }
880
}