Subversion Repositories ALCASAR

Rev

Rev 2976 | Go to most recent revision | 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');
235
        $a = preg_split('/:/', $s);
236
        if (isset($a[2])) {
237
            $this->sys->setKernel($a[0].$a[1].':'.$a[2]);
238
        } else {
239
            $this->sys->setKernel($s);
240
        }
241
    }
242
 
243
    /**
3037 rexy 244
     * Virtualizer info
2770 rexy 245
     *
246
     * @return void
247
     */
3037 rexy 248
    private function virtualizer()
2770 rexy 249
    {
3037 rexy 250
        if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
251
            $testvirt = $this->sys->getVirtualizer();
252
            $novm = true;
253
            foreach ($testvirt as $virtkey=>$virtvalue) if ($virtvalue) {
254
                $novm = false;
255
                break;
256
            }
257
            // Detect QEMU cpu
258
            if ($novm && isset($testvirt["cpuid:QEMU"])) {
259
                $this->sys->setVirtualizer('qemu'); // QEMU
260
                $novm = false;
261
            }
262
 
263
            if ($novm && isset($testvirt["hypervisor"])) {
264
                $this->sys->setVirtualizer('unknown');
265
            }
266
        }
267
    }
268
 
269
    /**
270
     * CPU usage
271
     *
272
     * @return void
273
     */
274
    protected function cpuusage()
275
    {
276
        if (($this->_cpu_loads === null)) {
277
            $this->_cpu_loads = array();
2976 rexy 278
            if (PSI_OS != 'Darwin') {
279
                if ($fd = $this->grabkey('kern.cp_time')) {
280
                    // Find out the CPU load
281
                    // user + sys = load
282
                    // total = total
283
                    if (preg_match($this->_CPURegExp2, $fd, $res) && (sizeof($res) > 4)) {
284
                        $load = $res[2] + $res[3] + $res[4]; // cpu.user + cpu.sys
285
                        $total = $res[2] + $res[3] + $res[4] + $res[5]; // cpu.total
286
                        // we need a second value, wait 1 second befor getting (< 1 second no good value will occour)
287
                        sleep(1);
288
                        $fd = $this->grabkey('kern.cp_time');
289
                        if (preg_match($this->_CPURegExp2, $fd, $res) && (sizeof($res) > 4)) {
290
                            $load2 = $res[2] + $res[3] + $res[4];
291
                            $total2 = $res[2] + $res[3] + $res[4] + $res[5];
3037 rexy 292
                            if ($total2 != $total) {
293
                                $this->_cpu_loads['cpu'] = (100 * ($load2 - $load)) / ($total2 - $total);
294
                            } else {
295
                                $this->_cpu_loads['cpu'] = 0;
296
                            }
2976 rexy 297
                        }
298
                    }
299
                }
300
            } else {
301
                $ncpu = $this->grabkey('hw.ncpu');
3037 rexy 302
                if (($ncpu !== "") && ($ncpu >= 1) && CommonFunctions::executeProgram('ps', "-A -o %cpu", $pstable, false) && !empty($pstable)) {
2976 rexy 303
                    $pslines = preg_split("/\n/", $pstable, -1, PREG_SPLIT_NO_EMPTY);
304
                    if (!empty($pslines) && (count($pslines)>1) && (trim($pslines[0])==="%CPU")) {
305
                        array_shift($pslines);
306
                        $sum = 0;
307
                        foreach ($pslines as $psline) {
308
                            $sum+=trim($psline);
309
                        }
3037 rexy 310
                        $this->_cpu_loads['cpu'] = min($sum/$ncpu, 100);
2976 rexy 311
                    }
312
                }
2770 rexy 313
            }
314
        }
3037 rexy 315
 
316
        if (isset($this->_cpu_loads['cpu'])) {
317
            return $this->_cpu_loads['cpu'];
318
        } else {
319
            return null;
320
        }
2770 rexy 321
    }
322
 
323
    /**
3037 rexy 324
     * Processor Load
325
     * optionally create a loadbar
326
     *
327
     * @return void
328
     */
329
    protected function loadavg()
330
    {
331
        $s = $this->grabkey('vm.loadavg');
332
        $s = preg_replace('/{ /', '', $s);
333
        $s = preg_replace('/ }/', '', $s);
334
        $this->sys->setLoad($s);
335
 
336
        if (PSI_LOAD_BAR) {
337
            $this->sys->setLoadPercent($this->cpuusage());
338
        }
339
    }
340
 
341
    /**
2770 rexy 342
     * CPU information
343
     *
344
     * @return void
345
     */
346
    protected function cpuinfo()
347
    {
348
        $dev = new CpuDevice();
3037 rexy 349
        $cpumodel = $this->grabkey('hw.model');
350
        $dev->setModel($cpumodel);
351
        if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && preg_match('/^QEMU Virtual CPU version /', $cpumodel)) {
352
            $this->sys->setVirtualizer("cpuid:QEMU", false);
2770 rexy 353
        }
354
 
355
        $notwas = true;
356
        foreach ($this->readdmesg() as $line) {
357
            if ($notwas) {
3037 rexy 358
               $regexps = preg_split("/\n/", $this->_CPURegExp1, -1, PREG_SPLIT_NO_EMPTY); // multiple regexp separated by \n
359
               foreach ($regexps as $regexp) {
360
                   if (preg_match($regexp, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
361
                        if ($dev->getCpuSpeed() === 0) {
362
                            $dev->setCpuSpeed(round($ar_buf[2]));
363
                        }
364
                        $notwas = false;
365
                        break;
2770 rexy 366
                    }
367
                }
368
            } else {
3037 rexy 369
                if (preg_match("/^\s+Origin| Features/", $line, $ar_buf)) {
370
                    if (preg_match("/^\s+Origin[ ]*=[ ]*\"(.+)\"/", $line, $ar_buf)) {
371
                        $dev->setVendorId($ar_buf[1]);
372
                    } elseif (preg_match("/ Features2[ ]*=.*<(.+)>/", $line, $ar_buf)) {
2770 rexy 373
                        $feats = preg_split("/,/", strtolower(trim($ar_buf[1])), -1, PREG_SPLIT_NO_EMPTY);
374
                        foreach ($feats as $feat) {
375
                            if (($feat=="vmx") || ($feat=="svm")) {
376
                                $dev->setVirt($feat);
3037 rexy 377
                            } elseif ($feat=="hv") {
378
                                if ($dev->getVirt() === null) {
379
                                    $dev->setVirt('hypervisor');
380
                                }
381
                                if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
382
                                    $this->sys->setVirtualizer("hypervisor", false);
383
                                }
2770 rexy 384
                            }
385
                        }
386
                    }
387
                } else break;
388
            }
389
        }
390
 
391
        $ncpu = $this->grabkey('hw.ncpu');
3037 rexy 392
        if (($ncpu === "") || !($ncpu >= 1)) {
2770 rexy 393
            $ncpu = 1;
3037 rexy 394
        }
395
        if (($ncpu == 1) && PSI_LOAD_BAR) {
396
            $dev->setLoad($this->cpuusage());
397
        }
2770 rexy 398
        for ($ncpu ; $ncpu > 0 ; $ncpu--) {
399
            $this->sys->setCpus($dev);
400
        }
401
    }
402
 
403
    /**
3037 rexy 404
     * Machine information
405
     *
406
     * @return void
407
     */
408
    private function machine()
409
    {
410
        if ((PSI_OS == 'NetBSD') || (PSI_OS == 'OpenBSD')) {
411
            $buffer = array();
412
            if (PSI_OS == 'NetBSD') { // NetBSD
413
                $buffer['Manufacturer'] = $this->grabkey('machdep.dmi.system-vendor');
414
                $buffer['Model'] = $this->grabkey('machdep.dmi.system-product');
415
                $buffer['Product'] = $this->grabkey('machdep.dmi.board-product');
416
                $buffer['SMBIOSBIOSVersion'] = $this->grabkey('machdep.dmi.bios-version');
417
                $buffer['ReleaseDate'] = $this->grabkey('machdep.dmi.bios-date');
418
            } else { // OpenBSD
419
                $buffer['Manufacturer'] = $this->grabkey('hw.vendor');
420
                $buffer['Model'] = $this->grabkey('hw.product');
421
                $buffer['Product'] = "";
422
                $buffer['SMBIOSBIOSVersion'] = "";
423
                $buffer['ReleaseDate'] = "";
424
            }
425
            if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
426
                $vendor_array = array();
427
                $vendor_array[] = $buffer['Model'];
428
                $vendor_array[] = trim($buffer['Manufacturer']." ".$buffer['Model']);
429
                if (PSI_OS == 'NetBSD') { // NetBSD
430
                    $vendor_array[] = $this->grabkey('machdep.dmi.board-vendor');
431
                    $vendor_array[] = $this->grabkey('machdep.dmi.bios-vendor');
432
                }
433
                $virt = CommonFunctions::decodevirtualizer($vendor_array);
434
                if ($virt !== null) {
435
                    $this->sys->setVirtualizer($virt);
436
                }
437
            }
438
 
439
            $buf = "";
440
            if (($buffer['Manufacturer'] !== "") && !preg_match("/^To be filled by O\.E\.M\.$|^System manufacturer$|^Not Specified$/i", $buf2=trim($buffer['Manufacturer'])) && ($buf2 !== "")) {
441
                $buf .= ' '.$buf2;
442
            }
443
 
444
            if (($buffer['Model'] !== "") && !preg_match("/^To be filled by O\.E\.M\.$|^System Product Name$|^Not Specified$/i", $buf2=trim($buffer['Model'])) && ($buf2 !== "")) {
445
                $model = $buf2;
446
                $buf .= ' '.$buf2;
447
            }
448
            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 !== "")) {
449
                if ($buf2 !== $model) {
450
                    $buf .= '/'.$buf2;
451
                } elseif (isset($buffer['SystemFamily']) && !preg_match("/^To be filled by O\.E\.M\.$|^System Family$|^Not Specified$/i", $buf2=trim($buffer['SystemFamily'])) && ($buf2 !== "")) {
452
                    $buf .= '/'.$buf2;
453
                }
454
            }
455
 
456
            $bver = "";
457
            $brel = "";
458
            if (($buf2=trim($buffer['SMBIOSBIOSVersion'])) !== "") {
459
                $bver .= ' '.$buf2;
460
            }
461
            if ($buffer['ReleaseDate'] !== "") {
462
                if (preg_match("/^(\d{4})(\d{2})(\d{2})$/", $buffer['ReleaseDate'], $dateout)) {
463
                    $brel .= ' '.$dateout[2].'/'.$dateout[3].'/'.$dateout[1];
464
                } elseif (preg_match("/^\d{2}\/\d{2}\/\d{4}$/", $buffer['ReleaseDate'])) {
465
                    $brel .= ' '.$buffer['ReleaseDate'];
466
                }
467
            }
468
            if ((trim($bver) !== "") || (trim($brel) !== "")) {
469
                $buf .= ', BIOS'.$bver.$brel;
470
            }
471
 
472
            if (trim($buf) !== "") {
473
                $this->sys->setMachine(trim($buf));
474
            }
475
        } elseif ((PSI_OS == 'FreeBSD') && defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
476
            $vendorid = $this->grabkey('hw.hv_vendor');
477
            if (trim($vendorid) === "") {
478
                foreach ($this->readdmesg() as $line) if (preg_match("/^Hypervisor: Origin = \"(.+)\"/", $line, $ar_buf)) {
479
                    if (trim($ar_buf[1]) !== "") {
480
                        $vendorid = $ar_buf[1];
481
                    }
482
                    break;
483
                }
484
            }
485
            if (trim($vendorid) !== "") {
486
                $virt = CommonFunctions::decodevirtualizer($vendorid);
487
                if ($virt !== null) {
488
                    $this->sys->setVirtualizer($virt);
489
                } else {
490
                    $this->sys->setVirtualizer('unknown');
491
                }
492
            }
493
        }
494
    }
495
 
496
    /**
2770 rexy 497
     * SCSI devices
498
     * get the scsi device information out of dmesg
499
     *
500
     * @return void
501
     */
502
    protected function scsi()
503
    {
504
        foreach ($this->readdmesg() as $line) {
2976 rexy 505
            if (preg_match($this->_SCSIRegExp1, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
2770 rexy 506
                $dev = new HWDevice();
507
                $dev->setName($ar_buf[1].": ".trim($ar_buf[2]));
508
                $this->sys->setScsiDevices($dev);
2976 rexy 509
            } elseif (preg_match($this->_SCSIRegExp2, $line, $ar_buf) && (sizeof($ar_buf) > 1)) {
2770 rexy 510
                /* duplication security */
511
                $notwas = true;
512
                foreach ($this->sys->getScsiDevices() as $finddev) {
513
                    if ($notwas && (substr($finddev->getName(), 0, strpos($finddev->getName(), ': ')) == $ar_buf[1])) {
514
                        if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
515
                            if (isset($ar_buf[3]) && ($ar_buf[3]==="G")) {
516
                                $finddev->setCapacity($ar_buf[2] * 1024 * 1024 * 1024);
2976 rexy 517
                            } elseif (isset($ar_buf[2])) {
2770 rexy 518
                                $finddev->setCapacity($ar_buf[2] * 1024 * 1024);
519
                            }
520
                        }
521
                        $notwas = false;
522
                        break;
523
                    }
524
                }
525
                if ($notwas) {
526
                    $dev = new HWDevice();
527
                    $dev->setName($ar_buf[1]);
528
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
529
                        if (isset($ar_buf[3]) && ($ar_buf[3]==="G")) {
530
                                $dev->setCapacity($ar_buf[2] * 1024 * 1024 * 1024);
2976 rexy 531
                            } elseif (isset($ar_buf[2])) {
2770 rexy 532
                                $dev->setCapacity($ar_buf[2] * 1024 * 1024);
533
                            }
534
                    }
535
                    $this->sys->setScsiDevices($dev);
536
                }
2976 rexy 537
            } elseif (preg_match($this->_SCSIRegExp3, $line, $ar_buf) && (sizeof($ar_buf) > 1)) {
2770 rexy 538
                /* duplication security */
539
                $notwas = true;
540
                foreach ($this->sys->getScsiDevices() as $finddev) {
541
                    if ($notwas && (substr($finddev->getName(), 0, strpos($finddev->getName(), ': ')) == $ar_buf[1])) {
542
                        if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
543
                           && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
2976 rexy 544
                            if (isset($ar_buf[2])) $finddev->setSerial(trim($ar_buf[2]));
2770 rexy 545
                        }
546
                        $notwas = false;
547
                        break;
548
                    }
549
                }
550
                if ($notwas) {
551
                    $dev = new HWDevice();
552
                    $dev->setName($ar_buf[1]);
553
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
554
                       && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
2976 rexy 555
                        if (isset($ar_buf[2])) $dev->setSerial(trim($ar_buf[2]));
2770 rexy 556
                    }
557
                    $this->sys->setScsiDevices($dev);
558
                }
559
            }
560
        }
561
        /* cleaning */
562
        foreach ($this->sys->getScsiDevices() as $finddev) {
3037 rexy 563
            if (strpos($finddev->getName(), ': ') !== false)
564
                $finddev->setName(substr(strstr($finddev->getName(), ': '), 2));
2770 rexy 565
        }
566
    }
567
 
568
    /**
569
     * parsing the output of pciconf command
570
     *
571
     * @return Array
572
     */
573
    protected function pciconf()
574
    {
575
        $arrResults = array();
576
        $intS = 0;
577
        if (CommonFunctions::executeProgram("pciconf", "-lv", $strBuf, PSI_DEBUG)) {
578
            $arrTemp = array();
579
            $arrBlocks = preg_split("/\n\S/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
580
            foreach ($arrBlocks as $strBlock) {
581
                $arrLines = preg_split("/\n/", $strBlock, -1, PREG_SPLIT_NO_EMPTY);
582
                $vend = null;
583
                foreach ($arrLines as $strLine) {
584
                    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)) {
585
                        $arrTemp[$intS] = 'Class '.$arrParts[1].': Device '.$arrParts[3].':'.$arrParts[2];
586
                        $vend = '';
587
                    } elseif (preg_match("/(.*) = '(.*)'/", $strLine, $arrParts)) {
588
                        if (trim($arrParts[1]) == "vendor") {
589
                            $vend = trim($arrParts[2]);
590
                        } elseif (trim($arrParts[1]) == "device") {
591
                            if (($vend !== null) && ($vend !== '')) {
592
                                $arrTemp[$intS] = $vend." - ".trim($arrParts[2]);
593
                            } else {
594
                                $arrTemp[$intS] = trim($arrParts[2]);
595
                                $vend = '';
596
                            }
597
                        }
598
                    }
599
                }
600
                if ($vend !== null) {
601
                    $intS++;
602
                }
603
            }
604
            foreach ($arrTemp as $name) {
605
                $dev = new HWDevice();
606
                $dev->setName($name);
607
                $arrResults[] = $dev;
608
            }
609
        }
610
 
611
        return $arrResults;
612
    }
613
 
614
    /**
615
     * PCI devices
616
     * get the pci device information out of dmesg
617
     *
618
     * @return void
619
     */
620
    protected function pci()
621
    {
622
        if ((!$results = Parser::lspci(false)) && (!$results = $this->pciconf())) {
623
            foreach ($this->readdmesg() as $line) {
2976 rexy 624
                if (preg_match($this->_PCIRegExp1, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
2770 rexy 625
                    $dev = new HWDevice();
626
                    $dev->setName($ar_buf[1].": ".$ar_buf[2]);
627
                    $results[] = $dev;
2976 rexy 628
                } elseif (preg_match($this->_PCIRegExp2, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
2770 rexy 629
                    $dev = new HWDevice();
630
                    $dev->setName($ar_buf[1].": ".$ar_buf[2]);
631
                    $results[] = $dev;
632
                }
633
            }
634
        }
635
        foreach ($results as $dev) {
636
            $this->sys->setPciDevices($dev);
637
        }
638
    }
639
 
640
    /**
641
     * IDE devices
642
     * get the ide device information out of dmesg
643
     *
644
     * @return void
645
     */
646
    protected function ide()
647
    {
648
        foreach ($this->readdmesg() as $line) {
649
            if (preg_match('/^(ad[0-9]+): (.*)MB <(.*)> (.*) (.*)/', $line, $ar_buf)) {
650
                $dev = new HWDevice();
651
                $dev->setName($ar_buf[1].": ".trim($ar_buf[3]));
652
                if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
653
                    $dev->setCapacity($ar_buf[2] * 1024 * 1024);
654
                }
655
                $this->sys->setIdeDevices($dev);
656
            } elseif (preg_match('/^(acd[0-9]+): (.*) <(.*)> (.*)/', $line, $ar_buf)) {
657
                $dev = new HWDevice();
658
                $dev->setName($ar_buf[1].": ".trim($ar_buf[3]));
659
                $this->sys->setIdeDevices($dev);
660
            } elseif (preg_match('/^(ada[0-9]+): <(.*)> (.*)/', $line, $ar_buf)) {
661
                $dev = new HWDevice();
662
                $dev->setName($ar_buf[1].": ".trim($ar_buf[2]));
663
                $this->sys->setIdeDevices($dev);
664
            } elseif (preg_match('/^(ada[0-9]+): (.*)MB \((.*)\)/', $line, $ar_buf)) {
665
                /* duplication security */
666
                $notwas = true;
667
                foreach ($this->sys->getIdeDevices() as $finddev) {
668
                    if ($notwas && (substr($finddev->getName(), 0, strpos($finddev->getName(), ': ')) == $ar_buf[1])) {
669
                        if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
670
                            $finddev->setCapacity($ar_buf[2] * 1024 * 1024);
671
                        }
672
                        $notwas = false;
673
                        break;
674
                    }
675
                }
676
                if ($notwas) {
677
                    $dev = new HWDevice();
678
                    $dev->setName($ar_buf[1]);
679
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
680
                        $dev->setCapacity($ar_buf[2] * 1024 * 1024);
681
                    }
682
                    $this->sys->setIdeDevices($dev);
683
                }
684
            } elseif (preg_match('/^(ada[0-9]+): Serial Number (.*)/', $line, $ar_buf)) {
685
                /* duplication security */
686
                $notwas = true;
687
                foreach ($this->sys->getIdeDevices() as $finddev) {
688
                    if ($notwas && (substr($finddev->getName(), 0, strpos($finddev->getName(), ': ')) == $ar_buf[1])) {
689
                        if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
690
                           && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
691
                            $finddev->setSerial(trim($ar_buf[2]));
692
                        }
693
                        $notwas = false;
694
                        break;
695
                    }
696
                }
697
                if ($notwas) {
698
                    $dev = new HWDevice();
699
                    $dev->setName($ar_buf[1]);
700
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
701
                       && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
702
                        $finddev->setSerial(trim($ar_buf[2]));
703
                    }
704
                    $this->sys->setIdeDevices($dev);
705
                }
706
            }
707
        }
708
        /* cleaning */
709
        foreach ($this->sys->getIdeDevices() as $finddev) {
710
                    if (strpos($finddev->getName(), ': ') !== false)
711
                        $finddev->setName(substr(strstr($finddev->getName(), ': '), 2));
712
        }
713
    }
714
 
715
    /**
716
     * Physical memory information and Swap Space information
717
     *
718
     * @return void
719
     */
720
    protected function memory()
721
    {
722
        if (PSI_OS == 'FreeBSD' || PSI_OS == 'OpenBSD') {
723
            // vmstat on fbsd 4.4 or greater outputs kbytes not hw.pagesize
724
            // I should probably add some version checking here, but for now
725
            // we only support fbsd 4.4
726
            $pagesize = 1024;
727
        } else {
728
            $pagesize = $this->grabkey('hw.pagesize');
729
        }
730
        if (CommonFunctions::executeProgram('vmstat', '', $vmstat, PSI_DEBUG)) {
731
            $lines = preg_split("/\n/", $vmstat, -1, PREG_SPLIT_NO_EMPTY);
732
            $ar_buf = preg_split("/\s+/", trim($lines[2]), 19);
733
            if (PSI_OS == 'NetBSD' || PSI_OS == 'DragonFly') {
734
                $this->sys->setMemFree($ar_buf[4] * 1024);
735
            } else {
736
                $this->sys->setMemFree($ar_buf[4] * $pagesize);
737
            }
738
            $this->sys->setMemTotal($this->grabkey('hw.physmem'));
739
            $this->sys->setMemUsed($this->sys->getMemTotal() - $this->sys->getMemFree());
740
 
741
            if (((PSI_OS == 'OpenBSD' || PSI_OS == 'NetBSD') && CommonFunctions::executeProgram('swapctl', '-l -k', $swapstat, PSI_DEBUG)) || CommonFunctions::executeProgram('swapinfo', '-k', $swapstat, PSI_DEBUG)) {
742
                $lines = preg_split("/\n/", $swapstat, -1, PREG_SPLIT_NO_EMPTY);
743
                foreach ($lines as $line) {
744
                    $ar_buf = preg_split("/\s+/", $line, 6);
745
                    if (($ar_buf[0] != 'Total') && ($ar_buf[0] != 'Device')) {
746
                        $dev = new DiskDevice();
747
                        $dev->setMountPoint($ar_buf[0]);
748
                        $dev->setName("SWAP");
749
                        $dev->setFsType('swap');
750
                        $dev->setTotal($ar_buf[1] * 1024);
751
                        $dev->setUsed($ar_buf[2] * 1024);
752
                        $dev->setFree($dev->getTotal() - $dev->getUsed());
753
                        $this->sys->setSwapDevices($dev);
754
                    }
755
                }
756
            }
757
        }
758
    }
759
 
760
    /**
761
     * USB devices
762
     * get the ide device information out of dmesg
763
     *
764
     * @return void
765
     */
766
    protected function usb()
767
    {
2976 rexy 768
        $notwas = true;
769
        if ((PSI_OS == 'FreeBSD') && CommonFunctions::executeProgram('usbconfig', '', $bufr, false)) {
770
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
771
            foreach ($lines as $line) {
772
                if (preg_match('/^(ugen[0-9]+\.[0-9]+): <([^,]*)(.*)> at (usbus[0-9]+)/', $line, $ar_buf)) {
773
                    $notwas = false;
774
                    $dev = new HWDevice();
775
                    $dev->setName($ar_buf[2]);
776
                    $this->sys->setUSBDevices($dev);
777
                }
778
            }
779
        }
780
        if ($notwas) foreach ($this->readdmesg() as $line) {
2770 rexy 781
//            if (preg_match('/^(ugen[0-9\.]+): <(.*)> (.*) (.*)/', $line, $ar_buf)) {
782
//                    $dev->setName($ar_buf[1].": ".$ar_buf[2]);
783
            if (preg_match('/^(u[a-z]+[0-9]+): <([^,]*)(.*)> on (usbus[0-9]+)/', $line, $ar_buf)) {
784
                    $dev = new HWDevice();
785
                    $dev->setName($ar_buf[2]);
786
                    $this->sys->setUSBDevices($dev);
787
            }
788
        }
789
    }
790
 
791
    /**
792
     * filesystem information
793
     *
794
     * @return void
795
     */
796
    protected function filesystems()
797
    {
798
        $arrResult = Parser::df();
799
        foreach ($arrResult as $dev) {
800
            $this->sys->setDiskDevices($dev);
801
        }
802
    }
803
 
804
    /**
805
     * Distribution
806
     *
807
     * @return void
808
     */
809
    protected function distro()
810
    {
811
        if (CommonFunctions::executeProgram('uname', '-s', $result, PSI_DEBUG)) {
812
            $this->sys->setDistribution($result);
813
        }
814
    }
815
 
816
    /**
3037 rexy 817
     * UpTime
818
     * time the system is running
819
     *
820
     * @return void
821
     */
822
    private function uptime()
823
    {
824
        if ($kb = $this->grabkey('kern.boottime')) {
825
            if (preg_match("/sec = ([0-9]+)/", $kb, $buf)) { // format like: { sec = 1096732600, usec = 885425 } Sat Oct 2 10:56:40 2004
826
                $this->sys->setUptime(time() - $buf[1]);
827
            } else {
828
                date_default_timezone_set('UTC');
829
                $kbt = strtotime($kb);
830
                if (($kbt !== false) && ($kbt != -1)) {
831
                    $this->sys->setUptime(time() - $kbt); // format like: Sat Oct 2 10:56:40 2004
832
                } else {
833
                    $this->sys->setUptime(time() - $kb); // format like: 1096732600
834
                }
835
            }
836
        }
837
    }
838
 
839
    /**
2770 rexy 840
     * get the information
841
     *
842
     * @see PSI_Interface_OS::build()
843
     *
3037 rexy 844
     * @return void
2770 rexy 845
     */
846
    public function build()
847
    {
848
        if (!$this->blockname || $this->blockname==='vitals') {
849
            $this->distro();
850
            $this->hostname();
851
            $this->kernel();
852
            $this->_users();
853
            $this->loadavg();
3037 rexy 854
            $this->uptime();
2770 rexy 855
        }
856
        if (!$this->blockname || $this->blockname==='hardware') {
3037 rexy 857
            $this->machine();
2770 rexy 858
            $this->cpuinfo();
3037 rexy 859
            $this->virtualizer();
2770 rexy 860
            $this->pci();
861
            $this->ide();
862
            $this->scsi();
863
            $this->usb();
864
        }
865
        if (!$this->blockname || $this->blockname==='memory') {
866
            $this->memory();
867
        }
868
        if (!$this->blockname || $this->blockname==='filesystem') {
869
            $this->filesystems();
870
        }
871
    }
872
}