Subversion Repositories ALCASAR

Rev

Rev 2976 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2976 Rev 3037
Line 26... Line 26...
26
 * @link      http://phpsysinfo.sourceforge.net
26
 * @link      http://phpsysinfo.sourceforge.net
27
 */
27
 */
28
abstract class BSDCommon extends OS
28
abstract class BSDCommon extends OS
29
{
29
{
30
    /**
30
    /**
-
 
31
     * Assoc array of all CPUs loads.
-
 
32
     */
-
 
33
    private $_cpu_loads = null;
-
 
34
 
-
 
35
    /**
31
     * content of the syslog
36
     * content of the syslog
32
     *
37
     *
33
     * @var array
38
     * @var array
34
     */
39
     */
35
    private $_dmesg = null;
40
    private $_dmesg = null;
Line 208... Line 213...
208
     *
213
     *
209
     * @return void
214
     * @return void
210
     */
215
     */
211
    protected function hostname()
216
    protected function hostname()
212
    {
217
    {
213
        if (PSI_USE_VHOST === true) {
218
        if (PSI_USE_VHOST) {
214
            if (CommonFunctions::readenv('SERVER_NAME', $hnm)) $this->sys->setHostname($hnm);
219
            if (CommonFunctions::readenv('SERVER_NAME', $hnm)) $this->sys->setHostname($hnm);
215
        } else {
220
        } else {
216
            if (CommonFunctions::executeProgram('hostname', '', $buf, PSI_DEBUG)) {
221
            if (CommonFunctions::executeProgram('hostname', '', $buf, PSI_DEBUG)) {
217
                $this->sys->setHostname($buf);
222
                $this->sys->setHostname($buf);
218
            }
223
            }
Line 234... Line 239...
234
            $this->sys->setKernel($s);
239
            $this->sys->setKernel($s);
235
        }
240
        }
236
    }
241
    }
237
 
242
 
238
    /**
243
    /**
239
     * Processor Load
244
     * Virtualizer info
240
     * optionally create a loadbar
-
 
241
     *
245
     *
242
     * @return void
246
     * @return void
243
     */
247
     */
244
    protected function loadavg()
248
    private function virtualizer()
245
    {
249
    {
-
 
250
        if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
246
        $s = $this->grabkey('vm.loadavg');
251
            $testvirt = $this->sys->getVirtualizer();
-
 
252
            $novm = true;
-
 
253
            foreach ($testvirt as $virtkey=>$virtvalue) if ($virtvalue) {
247
        $s = preg_replace('/{ /', '', $s);
254
                $novm = false;
-
 
255
                break;
-
 
256
            }
248
        $s = preg_replace('/ }/', '', $s);
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"])) {
249
        $this->sys->setLoad($s);
264
                $this->sys->setVirtualizer('unknown');
-
 
265
            }
-
 
266
        }
-
 
267
    }
-
 
268
 
-
 
269
    /**
-
 
270
     * CPU usage
-
 
271
     *
-
 
272
     * @return void
-
 
273
     */
-
 
274
    protected function cpuusage()
-
 
275
    {
250
        if (PSI_LOAD_BAR) {
276
        if (($this->_cpu_loads === null)) {
-
 
277
            $this->_cpu_loads = array();
251
            if (PSI_OS != 'Darwin') {
278
            if (PSI_OS != 'Darwin') {
252
                if ($fd = $this->grabkey('kern.cp_time')) {
279
                if ($fd = $this->grabkey('kern.cp_time')) {
253
                    // Find out the CPU load
280
                    // Find out the CPU load
254
                    // user + sys = load
281
                    // user + sys = load
255
                    // total = total
282
                    // total = total
Line 260... Line 287...
260
                        sleep(1);
287
                        sleep(1);
261
                        $fd = $this->grabkey('kern.cp_time');
288
                        $fd = $this->grabkey('kern.cp_time');
262
                        if (preg_match($this->_CPURegExp2, $fd, $res) && (sizeof($res) > 4)) {
289
                        if (preg_match($this->_CPURegExp2, $fd, $res) && (sizeof($res) > 4)) {
263
                            $load2 = $res[2] + $res[3] + $res[4];
290
                            $load2 = $res[2] + $res[3] + $res[4];
264
                            $total2 = $res[2] + $res[3] + $res[4] + $res[5];
291
                            $total2 = $res[2] + $res[3] + $res[4] + $res[5];
-
 
292
                            if ($total2 != $total) {
265
                            $this->sys->setLoadPercent((100 * ($load2 - $load)) / ($total2 - $total));
293
                                $this->_cpu_loads['cpu'] = (100 * ($load2 - $load)) / ($total2 - $total);
-
 
294
                            } else {
-
 
295
                                $this->_cpu_loads['cpu'] = 0;
-
 
296
                            }
266
                        }
297
                        }
267
                    }
298
                    }
268
                }
299
                }
269
            } else {
300
            } else {
270
                $ncpu = $this->grabkey('hw.ncpu');
301
                $ncpu = $this->grabkey('hw.ncpu');
271
                if (!is_null($ncpu) && (trim($ncpu) != "") && ($ncpu >= 1) && CommonFunctions::executeProgram('ps', "-A -o %cpu", $pstable, false) && !empty($pstable)) {
302
                if (($ncpu !== "") && ($ncpu >= 1) && CommonFunctions::executeProgram('ps', "-A -o %cpu", $pstable, false) && !empty($pstable)) {
272
                    $pslines = preg_split("/\n/", $pstable, -1, PREG_SPLIT_NO_EMPTY);
303
                    $pslines = preg_split("/\n/", $pstable, -1, PREG_SPLIT_NO_EMPTY);
273
                    if (!empty($pslines) && (count($pslines)>1) && (trim($pslines[0])==="%CPU")) {
304
                    if (!empty($pslines) && (count($pslines)>1) && (trim($pslines[0])==="%CPU")) {
274
                        array_shift($pslines);
305
                        array_shift($pslines);
275
                        $sum = 0;
306
                        $sum = 0;
276
                        foreach ($pslines as $psline) {
307
                        foreach ($pslines as $psline) {
277
                            $sum+=trim($psline);
308
                            $sum+=trim($psline);
278
                        }
309
                        }
279
                        $this->sys->setLoadPercent(min($sum/$ncpu, 100));
310
                        $this->_cpu_loads['cpu'] = min($sum/$ncpu, 100);
280
                    }
311
                    }
281
                }
312
                }
282
            }
313
            }
283
        }
314
        }
-
 
315
 
-
 
316
        if (isset($this->_cpu_loads['cpu'])) {
-
 
317
            return $this->_cpu_loads['cpu'];
-
 
318
        } else {
-
 
319
            return null;
-
 
320
        }
-
 
321
    }
-
 
322
 
-
 
323
    /**
-
 
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
        }
284
    }
339
    }
285
 
340
 
286
    /**
341
    /**
287
     * CPU information
342
     * CPU information
288
     *
343
     *
289
     * @return void
344
     * @return void
290
     */
345
     */
291
    protected function cpuinfo()
346
    protected function cpuinfo()
292
    {
347
    {
293
        $dev = new CpuDevice();
348
        $dev = new CpuDevice();
294
 
-
 
295
        if (PSI_OS == 'NetBSD') {
-
 
296
            if ($model = $this->grabkey('machdep.cpu_brand')) {
349
        $cpumodel = $this->grabkey('hw.model');
297
               $dev->setModel($model);
350
        $dev->setModel($cpumodel);
298
            }
-
 
299
            if ($cpuspeed = $this->grabkey('machdep.tsc_freq')) {
351
        if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && preg_match('/^QEMU Virtual CPU version /', $cpumodel)) {
300
               $dev->setCpuSpeed(round($cpuspeed / 1000000));
352
            $this->sys->setVirtualizer("cpuid:QEMU", false);
301
            }
-
 
302
        }
353
        }
303
 
354
 
304
        if ($dev->getModel() === "") {
-
 
305
            $dev->setModel($this->grabkey('hw.model'));
-
 
306
        }
-
 
307
        $notwas = true;
355
        $notwas = true;
308
        foreach ($this->readdmesg() as $line) {
356
        foreach ($this->readdmesg() as $line) {
309
            if ($notwas) {
357
            if ($notwas) {
-
 
358
               $regexps = preg_split("/\n/", $this->_CPURegExp1, -1, PREG_SPLIT_NO_EMPTY); // multiple regexp separated by \n
-
 
359
               foreach ($regexps as $regexp) {
310
               if (preg_match($this->_CPURegExp1, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
360
                   if (preg_match($regexp, $line, $ar_buf) && (sizeof($ar_buf) > 2)) {
311
                    if ($dev->getCpuSpeed() === 0) {
361
                        if ($dev->getCpuSpeed() === 0) {
312
                        $dev->setCpuSpeed(round($ar_buf[2]));
362
                            $dev->setCpuSpeed(round($ar_buf[2]));
-
 
363
                        }
-
 
364
                        $notwas = false;
-
 
365
                        break;
313
                    }
366
                    }
314
                    $notwas = false;
-
 
315
                }
367
                }
316
            } else {
368
            } else {
317
                if (preg_match("/ Origin| Features/", $line, $ar_buf)) {
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]);
318
                    if (preg_match("/ Features2[ ]*=.*<(.*)>/", $line, $ar_buf)) {
372
                    } elseif (preg_match("/ Features2[ ]*=.*<(.+)>/", $line, $ar_buf)) {
319
                        $feats = preg_split("/,/", strtolower(trim($ar_buf[1])), -1, PREG_SPLIT_NO_EMPTY);
373
                        $feats = preg_split("/,/", strtolower(trim($ar_buf[1])), -1, PREG_SPLIT_NO_EMPTY);
320
                        foreach ($feats as $feat) {
374
                        foreach ($feats as $feat) {
321
                            if (($feat=="vmx") || ($feat=="svm")) {
375
                            if (($feat=="vmx") || ($feat=="svm")) {
322
                                $dev->setVirt($feat);
376
                                $dev->setVirt($feat);
-
 
377
                            } elseif ($feat=="hv") {
-
 
378
                                if ($dev->getVirt() === null) {
-
 
379
                                    $dev->setVirt('hypervisor');
323
                                break 2;
380
                                }
-
 
381
                                if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
-
 
382
                                    $this->sys->setVirtualizer("hypervisor", false);
-
 
383
                                }
324
                            }
384
                            }
325
                        }
385
                        }
326
                        break;
-
 
327
                    }
386
                    }
328
                } else break;
387
                } else break;
329
            }
388
            }
330
        }
389
        }
331
 
390
 
332
        $ncpu = $this->grabkey('hw.ncpu');
391
        $ncpu = $this->grabkey('hw.ncpu');
333
        if (is_null($ncpu) || (trim($ncpu) == "") || (!($ncpu >= 1)))
392
        if (($ncpu === "") || !($ncpu >= 1)) {
334
            $ncpu = 1;
393
            $ncpu = 1;
-
 
394
        }
-
 
395
        if (($ncpu == 1) && PSI_LOAD_BAR) {
-
 
396
            $dev->setLoad($this->cpuusage());
-
 
397
        }
335
        for ($ncpu ; $ncpu > 0 ; $ncpu--) {
398
        for ($ncpu ; $ncpu > 0 ; $ncpu--) {
336
            $this->sys->setCpus($dev);
399
            $this->sys->setCpus($dev);
337
        }
400
        }
338
    }
401
    }
339
 
402
 
340
    /**
403
    /**
-
 
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
    /**
341
     * SCSI devices
497
     * SCSI devices
342
     * get the scsi device information out of dmesg
498
     * get the scsi device information out of dmesg
343
     *
499
     *
344
     * @return void
500
     * @return void
345
     */
501
     */
Line 402... Line 558...
402
                }
558
                }
403
            }
559
            }
404
        }
560
        }
405
        /* cleaning */
561
        /* cleaning */
406
        foreach ($this->sys->getScsiDevices() as $finddev) {
562
        foreach ($this->sys->getScsiDevices() as $finddev) {
407
                    if (strpos($finddev->getName(), ': ') !== false)
563
            if (strpos($finddev->getName(), ': ') !== false)
408
                        $finddev->setName(substr(strstr($finddev->getName(), ': '), 2));
564
                $finddev->setName(substr(strstr($finddev->getName(), ': '), 2));
409
        }
565
        }
410
    }
566
    }
411
 
567
 
412
    /**
568
    /**
413
     * parsing the output of pciconf command
569
     * parsing the output of pciconf command
Line 656... Line 812...
656
            $this->sys->setDistribution($result);
812
            $this->sys->setDistribution($result);
657
        }
813
        }
658
    }
814
    }
659
 
815
 
660
    /**
816
    /**
-
 
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
    /**
661
     * get the information
840
     * get the information
662
     *
841
     *
663
     * @see PSI_Interface_OS::build()
842
     * @see PSI_Interface_OS::build()
664
     *
843
     *
665
     * @return Void
844
     * @return void
666
     */
845
     */
667
    public function build()
846
    public function build()
668
    {
847
    {
669
        if (!$this->blockname || $this->blockname==='vitals') {
848
        if (!$this->blockname || $this->blockname==='vitals') {
670
            $this->distro();
849
            $this->distro();
671
            $this->hostname();
850
            $this->hostname();
672
            $this->kernel();
851
            $this->kernel();
673
            $this->_users();
852
            $this->_users();
674
            $this->loadavg();
853
            $this->loadavg();
-
 
854
            $this->uptime();
675
        }
855
        }
676
        if (!$this->blockname || $this->blockname==='hardware') {
856
        if (!$this->blockname || $this->blockname==='hardware') {
-
 
857
            $this->machine();
677
            $this->cpuinfo();
858
            $this->cpuinfo();
-
 
859
            $this->virtualizer();
678
            $this->pci();
860
            $this->pci();
679
            $this->ide();
861
            $this->ide();
680
            $this->scsi();
862
            $this->scsi();
681
            $this->usb();
863
            $this->usb();
682
        }
864
        }