Subversion Repositories ALCASAR

Rev

Rev 3037 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * Linux System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI Linux 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.Linux.inc.php 712 2012-12-05 14:09:18Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * Linux sysinfo class
17
 * get all the required information from Linux system
18
 *
19
 * @category  PHP
20
 * @package   PSI Linux OS class
21
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
22
 * @copyright 2009 phpSysInfo
23
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
24
 * @version   Release: 3.0
25
 * @link      http://phpsysinfo.sourceforge.net
26
 */
27
class Linux extends OS
28
{
29
    /**
3037 rexy 30
     * Uptime command result.
31
     */
32
    private $_uptime = null;
33
 
34
    /**
2770 rexy 35
     * Assoc array of all CPUs loads.
36
     */
3100 rexy 37
    protected $_cpu_loads = null;
325 richard 38
 
2770 rexy 39
    /**
3037 rexy 40
     * Version string.
2770 rexy 41
     */
3037 rexy 42
    private $_kernel_string = null;
43
 
44
    /**
45
     * Array of info from Bios.
46
     */
47
    private $_machine_info = null;
48
 
49
    /**
50
     * Array of info from dmesg.
51
     */
52
    private $_dmesg_info = null;
53
 
54
    /**
55
     * Result of systemd-detect-virt.
56
     */
57
    private $system_detect_virt = null;
58
 
59
     /**
60
      * Get info from dmesg
61
      *
62
      * @return array
63
      */
64
    private function _get_dmesg_info()
2770 rexy 65
    {
3037 rexy 66
        if ($this->_dmesg_info === null) {
67
            $this->_dmesg_info = array();
68
            if (CommonFunctions::rfts('/var/log/dmesg', $result, 0, 4096, false)) {
69
                if (preg_match('/^[\s\[\]\.\d]*DMI:\s*(.+)/m', $result, $ar_buf)) {
70
                    $this->_dmesg_info['dmi'] = trim($ar_buf[1]);
71
                }
72
                if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && ($this->system_detect_virt === null) && preg_match('/^[\s\[\]\.\d]*Hypervisor detected:\s*(.+)/m', $result, $ar_buf)) {
73
                    $this->_dmesg_info['hypervisor'] = trim($ar_buf[1]);
74
                }
2770 rexy 75
            }
3037 rexy 76
            if ((count($this->_dmesg_info) < ((defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && ($this->system_detect_virt === null))?2:1)) && CommonFunctions::executeProgram('dmesg', '', $result, false)) {
77
                if (!isset($this->_dmesg_info['dmi']) && preg_match('/^[\s\[\]\.\d]*DMI:\s*(.+)/m', $result, $ar_buf)) {
78
                    $this->_dmesg_info['dmi'] = trim($ar_buf[1]);
79
                }
80
                if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && ($this->system_detect_virt === null) && !isset($this->_dmesg_info['hypervisor']) && preg_match('/^[\s\[\]\.\d]*Hypervisor detected:\s*(.+)/m', $result, $ar_buf)) {
81
                    $this->_dmesg_info['hypervisor'] = trim($ar_buf[1]);
82
                }
2770 rexy 83
            }
3037 rexy 84
        }
85
 
86
        return $this->_dmesg_info;
87
    }
88
 
89
    /**
90
     * Get machine info
91
     *
92
     * @return string
93
     */
94
    private function _get_machine_info()
95
    {
96
        if ($this->_machine_info === null) {
97
            $this->_machine_info = array();
98
            if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
99
                if (CommonFunctions::executeProgram('systemd-detect-virt', '-v', $resultv, false)) {
100
                    $this->system_detect_virt = $resultv;
101
                }
2770 rexy 102
            }
3037 rexy 103
            $vendor_array = array();
104
            if ((($dmesg = $this->_get_dmesg_info()) !== null) && isset($dmesg['dmi'])) {
105
                $this->_machine_info['machine'] = $dmesg['dmi'];
106
                if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && ($this->system_detect_virt === null)) {
107
                    /* Test this before sys_vendor to detect KVM over QEMU */
108
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/product_name', $buf, 1, 4096, false) && (trim($buf)!="")) {
109
                        $vendor_array[] = $product_name = trim($buf);
110
                    } else {
111
                        $product_name = '';
112
                    }
113
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/sys_vendor', $buf, 1, 4096, false) && (trim($buf)!="")) {
114
                        $vendor_array[] = trim($buf);
115
                    }
116
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/board_vendor', $buf, 1, 4096, false) && (trim($buf)!="")) {
117
                        if ($product_name != "") {
118
                            $vendor_array[] = trim($buf)." ".$product_name;
119
                        } else {
120
                            $vendor_array[] = trim($buf);
121
                        }
122
                    } else {
123
                        $vendor_array[] = $dmesg['dmi'];
124
                    }
125
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/bios_vendor', $buf, 1, 4096, false) && (trim($buf)!="")) {
126
                        $vendor_array[] = trim($buf);
127
                    }
128
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/product_version', $buf, 1, 4096, false) && (trim($buf)!="")) {
129
                        $vendor_array[] = trim($buf);
130
                    }
131
                }
132
            } else { // 'machine' data from /sys/devices/virtual/dmi/id/
133
                $bios = "";
134
                if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && ($this->system_detect_virt === null)) {
135
                    // Test this before sys_vendor to detect KVM over QEMU
136
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/product_name', $buf, 1, 4096, false) && (trim($buf)!="")) {
137
                        $vendor_array[] = $product_name = trim($buf);
138
                    } else {
139
                        $product_name = '';
140
                    }
141
 
142
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/sys_vendor', $buf, 1, 4096, false) && (trim($buf)!="")) {
143
                        $vendor_array[] = trim($buf);
144
                    }
145
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/board_vendor', $buf, 1, 4096, false) && (trim($buf)!="")) {
146
                        if ($product_name != "") {
147
                            $this->_machine_info['machine'] = trim($buf)." ".$product_name;
148
                        } else {
149
                            $this->_machine_info['machine'] = trim($buf);
150
                        }
151
                        $vendor_array[] = $this->_machine_info["machine"];
152
                    } elseif ($product_name != "") {
153
                        $this->_machine_info['machine'] = $product_name;
154
                    }
155
 
156
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/bios_vendor', $buf, 1, 4096, false) && (trim($buf)!="")) {
157
                        $vendor_array[] = trim($buf);
158
                    }
159
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/product_version', $buf, 1, 4096, false) && (trim($buf)!="")) {
160
                        $vendor_array[] = trim($buf);
161
                    }
162
                } else {
163
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/board_vendor', $buf, 1, 4096, false) && (trim($buf)!="")) {
164
                        $this->_machine_info['machine'] = trim($buf);
165
                    }
166
                    if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/product_name', $buf, 1, 4096, false) && (trim($buf)!="")) {
167
                        if (isset($this->_machine_info['machine'])) {
168
                            $this->_machine_info['machine'] .= " ".trim($buf);
169
                        } else {
170
                            $this->_machine_info['machine'] = trim($buf);
171
                        }
172
                    }
173
                }
174
                if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/board_name', $buf, 1, 4096, false) && (trim($buf)!="")) {
175
                    if (isset($this->_machine_info['machine'])) {
176
                        $this->_machine_info['machine'] .= "/".trim($buf);
177
                    } else {
178
                        $this->_machine_info['machine'] = trim($buf);
179
                    }
180
                }
181
                if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/bios_version', $buf, 1, 4096, false) && (trim($buf)!="")) {
182
                    $bios = trim($buf);
183
                }
184
                if (CommonFunctions::rfts('/sys/devices/virtual/dmi/id/bios_date', $buf, 1, 4096, false) && (trim($buf)!="")) {
185
                    $bios = trim($bios." ".trim($buf));
186
                }
187
                if ($bios != "") {
188
                    if (isset($this->_machine_info['machine'])) {
189
                        $this->_machine_info['machine'] .= ", BIOS ".$bios;
190
                    } else {
191
                        $this->_machine_info['machine'] = "BIOS ".$bios;
192
                    }
193
                }
2770 rexy 194
            }
3037 rexy 195
            if (isset($this->_machine_info['machine'])) {
196
                $this->_machine_info['machine'] = trim(preg_replace("/^\/,?/", "", preg_replace("/ ?(To be filled by O\.E\.M\.|System manufacturer|System Product Name|Not Specified|Default string) ?/i", "", $this->_machine_info['machine'])));
2770 rexy 197
            }
3037 rexy 198
 
3100 rexy 199
            if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && ($this->system_detect_virt === null) && count($vendor_array) > 0) {
3037 rexy 200
                $virt = CommonFunctions::decodevirtualizer($vendor_array);
201
                if ($virt !== null) {
202
                    $this->_machine_info['hypervisor'] = $virt;
203
                }
2770 rexy 204
            }
3037 rexy 205
        }
206
 
207
        return $this->_machine_info;
208
    }
209
 
210
    /**
211
     * Get kernel string
212
     *
213
     * @return string
214
     */
215
    private function _get_kernel_string()
216
    {
217
        if ($this->_kernel_string === null) {
218
            $this->_kernel_string = "";
3100 rexy 219
            if ($this->sys->getOS() == 'SSH') {
220
                if (CommonFunctions::executeProgram('uname', '-s', $strBuf, false) && ($strBuf !== '')) {
221
                    $this->sys->setOS($strBuf);
222
                } else {
223
                    return $this->_kernel_string;
224
                }
225
            }
226
            if ((CommonFunctions::executeProgram($uname="uptrack-uname", '-r', $strBuf, false) && ($strBuf !== '')) || // show effective kernel if ksplice uptrack is installed
227
                (CommonFunctions::executeProgram($uname="uname", '-r', $strBuf, PSI_DEBUG) && ($strBuf !== ''))) {
3037 rexy 228
                $this->_kernel_string = $strBuf;
3100 rexy 229
                if (CommonFunctions::executeProgram($uname, '-v', $strBuf, PSI_DEBUG) && ($strBuf !== '')) {
3037 rexy 230
                    if (preg_match('/ SMP /', $strBuf)) {
231
                        $this->_kernel_string .= ' (SMP)';
232
                    }
233
                }
3100 rexy 234
                if (CommonFunctions::executeProgram($uname, '-m', $strBuf, PSI_DEBUG) && ($strBuf !== '')) {
3037 rexy 235
                    $this->_kernel_string .= ' '.$strBuf;
236
                }
237
            } elseif (CommonFunctions::rfts('/proc/version', $strBuf, 1)) {
3100 rexy 238
                if (preg_match('/\/Hurd-([^\)]+)/', $strBuf, $ar_buf)) {
3037 rexy 239
                    $this->_kernel_string = $ar_buf[1];
3100 rexy 240
                } elseif (preg_match('/version\s+(\S+)/', $strBuf, $ar_buf)) {
241
                    $this->_kernel_string = $ar_buf[1];
3037 rexy 242
                    if (preg_match('/ SMP /', $strBuf)) {
243
                        $this->_kernel_string .= ' (SMP)';
244
                    }
245
                }
2770 rexy 246
            }
247
        }
325 richard 248
 
3037 rexy 249
        return $this->_kernel_string;
250
    }
251
 
252
    /**
3100 rexy 253
     * check OS type
254
     */
255
    public function __construct($blockname = false)
256
    {
257
        parent::__construct($blockname);
258
        $this->_get_kernel_string();
259
    }
260
 
261
    /**
3037 rexy 262
     * Machine
263
     *
264
     * @return void
265
     */
3100 rexy 266
    protected function _machine()
3037 rexy 267
    {
268
        $machine_info = $this->_get_machine_info();
269
        if (isset($machine_info['machine'])) {
270
            $machine = $machine_info['machine'];
271
        } else {
272
            $machine = "";
2770 rexy 273
        }
325 richard 274
 
2770 rexy 275
        if (CommonFunctions::fileexists($filename="/etc/config/uLinux.conf") // QNAP detection
276
           && CommonFunctions::rfts($filename, $buf, 0, 4096, false)
277
           && preg_match("/^Rsync\sModel\s*=\s*QNAP/m", $buf)
278
           && CommonFunctions::fileexists($filename="/etc/platform.conf") // Platform detection
279
           && CommonFunctions::rfts($filename, $buf, 0, 4096, false)
280
           && preg_match("/^DISPLAY_NAME\s*=\s*(\S+)/m", $buf, $mach_buf) && ($mach_buf[1]!=="")) {
3037 rexy 281
            if ($machine !== "") {
2770 rexy 282
                $machine = "QNAP ".$mach_buf[1].' - '.$machine;
283
            } else {
284
                $machine = "QNAP ".$mach_buf[1];
285
            }
286
        }
287
 
3037 rexy 288
        if ($machine !== "") {
2770 rexy 289
            $this->sys->setMachine($machine);
290
        }
1764 richard 291
    }
325 richard 292
 
2770 rexy 293
    /**
294
     * Hostname
295
     *
296
     * @return void
297
     */
298
    protected function _hostname()
299
    {
3100 rexy 300
        if (PSI_USE_VHOST && !defined('PSI_EMU_PORT')) {
2770 rexy 301
            if (CommonFunctions::readenv('SERVER_NAME', $hnm)) $this->sys->setHostname($hnm);
302
        } else {
303
            if (CommonFunctions::rfts('/proc/sys/kernel/hostname', $result, 1, 4096, PSI_DEBUG && (PSI_OS != 'Android'))) {
304
                $result = trim($result);
305
                $ip = gethostbyname($result);
306
                if ($ip != $result) {
307
                    $this->sys->setHostname(gethostbyaddr($ip));
308
                }
309
            } elseif (CommonFunctions::executeProgram('hostname', '', $ret)) {
310
                $this->sys->setHostname($ret);
311
            }
312
        }
313
    }
325 richard 314
 
2770 rexy 315
    /**
316
     * Kernel Version
317
     *
318
     * @return void
319
     */
3100 rexy 320
    protected function _kernel()
2770 rexy 321
    {
3037 rexy 322
        if (($verBuf = $this->_get_kernel_string()) != "") {
323
            $this->sys->setKernel($verBuf);
324
        }
325
    }
326
 
327
    /**
328
     * Virtualizer info
329
     *
330
     * @return void
331
     */
332
    protected function _virtualizer()
333
    {
334
        if (!defined('PSI_SHOW_VIRTUALIZER_INFO') || !PSI_SHOW_VIRTUALIZER_INFO) {
335
            return;
336
        }
337
        if ($this->system_detect_virt !== null) {
338
            if (($this->system_detect_virt !== "") && ($this->system_detect_virt !== "none")) {
339
                $this->sys->setVirtualizer($this->system_detect_virt);
340
            }
341
            if (($verBuf = $this->_get_kernel_string()) !== "") {
342
                if (preg_match('/^[\d\.-]+-microsoft-standard/', $verBuf)) {
343
                    $this->sys->setVirtualizer('wsl2', 'wsl'); // Windows Subsystem for Linux 2
2770 rexy 344
                }
345
            }
3037 rexy 346
            if (CommonFunctions::executeProgram('systemd-detect-virt', '-c', $resultc, false) && ($resultc !== "") && ($resultc !== "none")) {
347
                $this->sys->setVirtualizer($resultc);
2770 rexy 348
            }
3037 rexy 349
        } else {
350
            $cpuvirt = $this->sys->getVirtualizer(); // previous info from _cpuinfo()
351
 
352
            $novm = true;
353
            // code based on src/basic/virt.c from systemd-detect-virt source code (https://github.com/systemd/systemd)
354
 
3100 rexy 355
            // First, try to detect Oracle Virtualbox, Amazon EC2 Nitro and Parallels, even if they use KVM,
356
            // as well as Xen even if it cloaks as Microsoft Hyper-V. Attempt to detect uml at this stage also
357
            // since it runs as a user-process nested inside other VMs. Also check for Xen now, because Xen PV
358
            // mode does not override CPUID when nested inside another hypervisor.
3037 rexy 359
            $machine_info = $this->_get_machine_info();
360
            if (isset($machine_info['hypervisor'])) {
361
                $hypervisor = $machine_info['hypervisor'];
3100 rexy 362
                if (($hypervisor === 'oracle') || ($hypervisor === 'amazon') || ($hypervisor === 'xen') || ($hypervisor === 'parallels')) {
3037 rexy 363
                    $this->sys->setVirtualizer($hypervisor);
364
                    $novm = false;
365
                }
2770 rexy 366
            }
3037 rexy 367
 
368
            // Detect UML
369
            if ($novm) {
370
                if (isset($cpuvirt["cpuid:UserModeLinux"])) {
371
                    $this->sys->setVirtualizer('uml'); // User-mode Linux
372
                    $novm = false;
373
                }
374
            }
375
 
376
            // Detect Xen
377
            if ($novm && is_dir('/proc/xen')) {
378
                // xen Dom0 is detected as XEN in hypervisor and maybe others.
379
                // In order to detect the Dom0 as not virtualization we need to
380
                // double-check it
381
                if (CommonFunctions::rfts('/sys/hypervisor/properties/features', $features, 1, 4096, false)) {
382
                    if ((hexdec($features) & 2048) == 0) { // XENFEAT_dom0 is not set
383
                        $this->sys->setVirtualizer('xen'); // Xen hypervisor (only domU, not dom0)
384
                        $novm = false;
385
                    }
386
                } elseif (CommonFunctions::rfts('/proc/xen/capabilities', $capabilities, 1, 4096, false) && !preg_match('/control_d/', $capabilities)) { // control_d not in capabilities
387
                    $this->sys->setVirtualizer('xen'); // Xen hypervisor (only domU, not dom0)
388
                    $novm = false;
389
                }
390
            }
391
 
392
            // Second, try to detect from CPUID, this will report KVM for whatever software is used even if info in DMI is overwritten.
393
            // Since the vendor_id in /proc/cpuinfo is overwritten on virtualization we use values from msr-cpuid.
394
            if ($novm && CommonFunctions::executeProgram('msr-cpuid', '', $bufr, false)
395
               && (preg_match('/^40000000 00000000:  [0-9a-f]{8} \S{4}  [0-9a-f]{8} ([A-Za-z0-9\.]{4})  [0-9a-f]{8} ([A-Za-z0-9\.]{4})  [0-9a-f]{8} ([A-Za-z0-9\.]{4})/m', $bufr, $cpuid))) {
396
                $virt = CommonFunctions::decodevirtualizer($cpuid[1].$cpuid[2].$cpuid[3]);
397
                if ($virt !== null) {
398
                    $this->sys->setVirtualizer($virt);
399
                }
400
            }
401
 
402
            // Third, try to detect from DMI.
403
            if ($novm && isset($hypervisor)) {
404
                $this->sys->setVirtualizer($hypervisor);
405
                $novm = false;
406
            }
407
 
408
            // Check high-level hypervisor sysfs file
409
            if ($novm && CommonFunctions::rfts('/sys/hypervisor/type', $type, 1, 4096, false) && ($type === "xen")) {
410
                $this->sys->setVirtualizer('xen'); // Xen hypervisor
411
                $novm = false;
412
            }
413
 
414
            if ($novm) {
415
                if (CommonFunctions::rfts('/proc/device-tree/hypervisor/compatible', $compatible, 1, 4096, false)) {
416
                    switch ($compatible) {
417
                    case 'linux,kvm':
418
                        $this->sys->setVirtualizer('kvm'); // KVM
419
                        $novm = false;
420
                        break;
421
                    case 'vmware':
422
                        $this->sys->setVirtualizer('vmware'); // VMware
423
                        $novm = false;
424
                        break;
425
                    case 'xen':
426
                        $this->sys->setVirtualizer('xen'); // Xen hypervisor
427
                        $novm = false;
428
                    }
429
                } else {
430
                    if (CommonFunctions::fileexists('/proc/device-tree/ibm,partition-name')
431
                       && CommonFunctions::fileexists('/proc/device-tree/hmc-managed?')
432
                       && CommonFunctions::fileexists('/proc/device-tree/chosen/qemu,graphic-width')) {
433
                        $this->sys->setVirtualizer('powervm'); // IBM PowerVM hypervisor
434
                        $novm = false;
435
                    } else {
436
                        $names = CommonFunctions::findglob('/proc/device-tree', GLOB_NOSORT);
437
                        if (is_array($names) && (($total = count($names)) > 0)) {
438
                            for ($i = 0; $i < $total; $i++) {
439
                                if (preg_match('/fw-cfg/', $names[$i])) {
440
                                    $this->sys->setVirtualizer('qemu'); // QEMU
441
                                    $novm = false;
442
                                    break;
443
                                }
444
                            }
445
                        }
446
                    }
447
                }
448
            }
449
 
450
            if ($novm && CommonFunctions::rfts('/proc/sysinfo', $sysinfo, 0, 4096, false) && preg_match('//VM00 Control Program:\s*(\S+)/m', $sysinfo, $vcp)) {
451
                if ($vcp[1] === 'z/VM') {
452
                    $this->sys->setVirtualizer('zvm'); // s390 z/VM
453
                } else {
454
                    $this->sys->setVirtualizer('kvm'); // KVM
455
                }
456
                $novm = false;
457
            }
458
 
459
            // Additional tests outside of the systemd-detect-virt source code
460
            if ($novm && (($dmesg = $this->_get_dmesg_info()) !== null) && isset($dmesg['hypervisor'])) {
461
                switch ($dmesg['hypervisor']) {
462
                case 'VMware':
463
                    $this->sys->setVirtualizer('vmware'); // VMware
464
                    $novm = false;
465
                    break;
466
                case 'KVM':
467
                    $this->sys->setVirtualizer('kvm'); // KVM
468
                    $novm = false;
469
                    break;
470
                case 'Microsoft HyperV':
471
                case 'Microsoft Hyper-V':
472
                    $this->sys->setVirtualizer('microsoft'); // Hyper-V
473
                    $novm = false;
474
                    break;
475
                case 'ACRN':
476
                    $this->sys->setVirtualizer('acrn'); // ACRN hypervisor
477
                    $novm = false;
478
                    break;
479
                case 'Jailhouse':
480
                    $this->sys->setVirtualizer('jailhouse'); // Jailhouse
481
                    $novm = false;
482
                    break;
483
                case 'Xen':
484
                case 'Xen PV':
485
                case 'Xen HVM':
486
                    // xen Dom0 is detected as XEN in hypervisor and maybe others.
487
                    // In order to detect the Dom0 as not virtualization we need to
488
                    // double-check it
489
                    if (CommonFunctions::rfts('/sys/hypervisor/properties/features', $features, 1, 4096, false)) {
490
                        if ((hexdec($features) & 2048) == 0) { // XENFEAT_dom0 is not set
491
                            $this->sys->setVirtualizer('xen'); // Xen hypervisor (only domU, not dom0)
492
                            $novm = false;
493
                        }
494
                    } elseif (CommonFunctions::rfts('/proc/xen/capabilities', $capabilities, 1, 4096, false) && !preg_match('/control_d/', $capabilities)) { // control_d not in capabilities
495
                        $this->sys->setVirtualizer('xen'); // Xen hypervisor (only domU, not dom0)
496
                        $novm = false;
497
                    }
498
                }
499
            }
500
 
501
            // Detect QEMU cpu
502
            if ($novm && isset($cpuvirt["cpuid:QEMU"])) {
503
                $this->sys->setVirtualizer('qemu'); // QEMU
504
                $novm = false;
505
            }
506
 
507
            if ($novm && isset($cpuvirt["hypervisor"])) {
508
                $this->sys->setVirtualizer('unknown');
509
            }
510
 
511
            if ((count(CommonFunctions::gdc('/proc/vz', false)) == 0) && (count(CommonFunctions::gdc('/proc/bc', false)) > 0)) {
512
                $this->sys->setVirtualizer('openvz'); // OpenVZ/Virtuozzo
513
            }
514
 
515
            if (($verBuf = $this->_get_kernel_string()) !== "") {
516
                if (preg_match('/^[\d\.-]+-Microsoft/', $verBuf)) {
517
                    $this->sys->setVirtualizer('wsl'); // Windows Subsystem for Linux
518
                } elseif (preg_match('/^[\d\.-]+-microsoft-standard/', $verBuf)) {
519
                    $this->sys->setVirtualizer('wsl2'); // Windows Subsystem for Linux 2
520
                }
521
            }
522
 
2770 rexy 523
            if (CommonFunctions::rfts('/proc/self/cgroup', $strBuf2, 0, 4096, false)) {
3037 rexy 524
               if (preg_match('/:\/lxc\//m', $strBuf2)) {
525
                    $this->sys->setVirtualizer('lxc'); // Linux container
2770 rexy 526
                } elseif (preg_match('/:\/docker\//m', $strBuf2)) {
3037 rexy 527
                    $this->sys->setVirtualizer('docker'); // Docker
2770 rexy 528
                } elseif (preg_match('/:\/system\.slice\/docker\-/m', $strBuf2)) {
3037 rexy 529
                    $this->sys->setVirtualizer('docker'); // Docker
2770 rexy 530
                }
531
            }
532
        }
533
    }
325 richard 534
 
2770 rexy 535
    /**
536
     * UpTime
537
     * time the system is running
538
     *
539
     * @return void
540
     */
3100 rexy 541
    protected function _uptime($bufu = null)
2770 rexy 542
    {
543
        if (CommonFunctions::rfts('/proc/uptime', $buf, 1, 4096, PSI_OS != 'Android')) {
544
            $ar_buf = preg_split('/ /', $buf);
545
            $this->sys->setUptime(trim($ar_buf[0]));
3100 rexy 546
        } elseif (($this->_uptime !== null) || ($bufu !== null) || CommonFunctions::executeProgram('uptime', '', $bufu)) {
547
            if (($this->_uptime === null) && ($bufu !== null)) {
548
                $this->_uptime = $bufu;
549
            }
3037 rexy 550
            if (preg_match("/up (\d+) day[s]?,[ ]+(\d+):(\d+),/", $this->_uptime, $ar_buf)) {
2770 rexy 551
                $min = $ar_buf[3];
552
                $hours = $ar_buf[2];
553
                $days = $ar_buf[1];
554
                $this->sys->setUptime($days * 86400 + $hours * 3600 + $min * 60);
3037 rexy 555
            } elseif (preg_match("/up (\d+) day[s]?,[ ]+(\d+) min,/", $this->_uptime, $ar_buf)) {
2770 rexy 556
                $min = $ar_buf[2];
557
                $days = $ar_buf[1];
558
                $this->sys->setUptime($days * 86400 + $min * 60);
3037 rexy 559
            } elseif (preg_match("/up[ ]+(\d+):(\d+),/", $this->_uptime, $ar_buf)) {
2770 rexy 560
                $min = $ar_buf[2];
561
                $hours = $ar_buf[1];
562
                $this->sys->setUptime($hours * 3600 + $min * 60);
3100 rexy 563
            } elseif (preg_match("/up[ ]+(\d+):(\d+):(\d+)/", $this->_uptime, $ar_buf)) {
564
                $sec = $ar_buf[3];
565
                $min = $ar_buf[2];
566
                $hours = $ar_buf[1];
567
                $this->sys->setUptime($hours * 3600 + $min * 60 + $sec);
3037 rexy 568
            } elseif (preg_match("/up[ ]+(\d+) min,/", $this->_uptime, $ar_buf)) {
2770 rexy 569
                $min = $ar_buf[1];
570
                $this->sys->setUptime($min * 60);
3100 rexy 571
            } elseif (preg_match("/up[ ]+(\d+) day[s]?,[ ]+(\d+) hour[s]?,[ ]+(\d+) minute[s]?/", $this->_uptime, $ar_buf)) {
572
                $min = $ar_buf[3];
573
                $hours = $ar_buf[2];
574
                $days = $ar_buf[1];
575
                $this->sys->setUptime($days * 86400 + $hours * 3600 + $min * 60);
2770 rexy 576
            }
577
        }
578
    }
325 richard 579
 
2770 rexy 580
    /**
581
     * Processor Load
582
     * optionally create a loadbar
583
     *
584
     * @return void
585
     */
3100 rexy 586
    protected function _loadavg($buf = null)
2770 rexy 587
    {
3100 rexy 588
        if ((($buf !== null) || CommonFunctions::rfts('/proc/loadavg', $buf, 1, 4096, PSI_OS != 'Android')) && preg_match("/^\d/", trim($buf))) {
2770 rexy 589
            $result = preg_split("/\s/", $buf, 4);
590
            // don't need the extra values, only first three
591
            unset($result[3]);
592
            $this->sys->setLoad(implode(' ', $result));
3100 rexy 593
        } elseif (($buf === null) && ((($this->_uptime !== null) || CommonFunctions::executeProgram('uptime', '', $this->_uptime)) && preg_match("/load average: (.*), (.*), (.*)$/", $this->_uptime, $ar_buf))) {
594
              $this->sys->setLoad($ar_buf[1].' '.$ar_buf[2].' '.$ar_buf[3]);
2770 rexy 595
        }
596
        if (PSI_LOAD_BAR) {
597
            $this->sys->setLoadPercent($this->_parseProcStat('cpu'));
598
        }
325 richard 599
    }
600
 
2770 rexy 601
    /**
602
     * fill the load for a individual cpu, through parsing /proc/stat for the specified cpu
603
     *
604
     * @param String $cpuline cpu for which load should be meassured
605
     *
3037 rexy 606
     * @return int
2770 rexy 607
     */
608
    protected function _parseProcStat($cpuline)
609
    {
3037 rexy 610
        if ($this->_cpu_loads === null) {
2770 rexy 611
            $this->_cpu_loads = array();
325 richard 612
 
2770 rexy 613
            $cpu_tmp = array();
2976 rexy 614
            if (CommonFunctions::rfts('/proc/stat', $buf, 0, 4096, PSI_DEBUG && (PSI_OS != 'Android'))) {
2770 rexy 615
                if (preg_match_all('/^(cpu[0-9]*) (.*)/m', $buf, $matches, PREG_SET_ORDER)) {
616
                    foreach ($matches as $line) {
617
                        $cpu = $line[1];
618
                        $buf2 = $line[2];
325 richard 619
 
2770 rexy 620
                        $cpu_tmp[$cpu] = array();
325 richard 621
 
2770 rexy 622
                        $ab = 0;
623
                        $ac = 0;
624
                        $ad = 0;
625
                        $ae = 0;
626
                        sscanf($buf2, "%Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
627
                        $cpu_tmp[$cpu]['load'] = $ab + $ac + $ad; // cpu.user + cpu.sys
628
                        $cpu_tmp[$cpu]['total'] = $ab + $ac + $ad + $ae; // cpu.total
629
                    }
630
                }
631
 
632
                // we need a second value, wait 1 second befor getting (< 1 second no good value will occour)
633
                sleep(1);
634
 
2976 rexy 635
                if (CommonFunctions::rfts('/proc/stat', $buf, 0, 4096, PSI_DEBUG)) {
2770 rexy 636
                    if (preg_match_all('/^(cpu[0-9]*) (.*)/m', $buf, $matches, PREG_SET_ORDER)) {
637
                        foreach ($matches as $line) {
638
                            $cpu = $line[1];
639
                            if (isset($cpu_tmp[$cpu])) {
640
                                $buf2 = $line[2];
641
 
642
                                $ab = 0;
643
                                $ac = 0;
644
                                $ad = 0;
645
                                $ae = 0;
646
                                sscanf($buf2, "%Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
647
                                $load2 = $ab + $ac + $ad; // cpu.user + cpu.sys
648
                                $total2 = $ab + $ac + $ad + $ae; // cpu.total
649
                                $total = $cpu_tmp[$cpu]['total'];
650
                                $load = $cpu_tmp[$cpu]['load'];
651
                                $this->_cpu_loads[$cpu] = 0;
652
                                if ($total > 0 && $total2 > 0 && $load > 0 && $load2 > 0 && $total2 != $total && $load2 != $load) {
653
                                    $this->_cpu_loads[$cpu] = (100 * ($load2 - $load)) / ($total2 - $total);
654
                                }
655
                            }
656
                        }
657
                    }
658
                }
659
            }
660
        }
661
 
662
        if (isset($this->_cpu_loads[$cpuline])) {
663
            return $this->_cpu_loads[$cpuline];
325 richard 664
        } else {
2976 rexy 665
            return null;
2770 rexy 666
        }
667
    }
325 richard 668
 
2770 rexy 669
    /**
670
     * CPU information
671
     * All of the tags here are highly architecture dependant.
672
     *
673
     * @return void
674
     */
3100 rexy 675
    protected function _cpuinfo($bufr = null)
2770 rexy 676
    {
3100 rexy 677
        if (($bufr !== null) || CommonFunctions::rfts('/proc/cpuinfo', $bufr)) {
2770 rexy 678
            $cpulist = null;
679
            $raslist = null;
325 richard 680
 
2770 rexy 681
            // sparc
682
            if (preg_match('/\nCpu(\d+)Bogo\s*:/i', $bufr)) {
683
                $bufr = preg_replace('/\nCpu(\d+)ClkTck\s*:/i', "\nCpu0ClkTck:", preg_replace('/\nCpu(\d+)Bogo\s*:/i', "\n\nprocessor: $1\nCpu0Bogo:", $bufr));
684
            } else {
685
                $bufr = preg_replace('/\nCpu(\d+)ClkTck\s*:/i', "\n\nprocessor: $1\nCpu0ClkTck:", $bufr);
686
            }
325 richard 687
 
2770 rexy 688
            if (preg_match('/\nprocessor\s*:\s*\d+\r?\nprocessor\s*:\s*\d+/', $bufr)) {
689
                $bufr = preg_replace('/^(processor\s*:\s*\d+)\r?$/m', "$1\n", $bufr);
690
            }
325 richard 691
 
2770 rexy 692
            // IBM/S390
693
            $bufr = preg_replace('/\ncpu number\s*:\s*(\d+)\r?\ncpu MHz dynamic\s*:\s*(\d+)/m', "\nprocessor:$1\nclock:$2", $bufr);
694
 
695
            $processors = preg_split('/\s?\n\s?\n/', trim($bufr));
696
 
697
            //first stage
698
            $_arch = null;
699
            $_impl = null;
700
            $_part = null;
3037 rexy 701
            $_vari = null;
2770 rexy 702
            $_hard = null;
703
            $_revi = null;
704
            $_cpus = null;
705
            $_buss = null;
706
            $_bogo = null;
707
            $_vend = null;
708
            $procname = null;
709
            foreach ($processors as $processor) if (!preg_match('/^\s*processor\s*:/mi', $processor)) {
710
                $details = preg_split("/\n/", $processor, -1, PREG_SPLIT_NO_EMPTY);
711
                foreach ($details as $detail) {
712
                    $arrBuff = preg_split('/\s*:\s*/', trim($detail));
713
                    if ((count($arrBuff) == 2) && (($arrBuff1 = trim($arrBuff[1])) !== '')) {
714
                        switch (strtolower($arrBuff[0])) {
715
                        case 'cpu architecture':
716
                            $_arch = $arrBuff1;
717
                            break;
718
                        case 'cpu implementer':
719
                            $_impl = $arrBuff1;
720
                            break;
721
                        case 'cpu part':
722
                            $_part = $arrBuff1;
723
                            break;
3037 rexy 724
                        case 'cpu variant':
725
                            $_vari = $arrBuff1;
726
                            break;
2770 rexy 727
                        case 'hardware':
728
                            $_hard = $arrBuff1;
729
                            break;
730
                        case 'revision':
731
                            $_revi = $arrBuff1;
732
                            break;
733
                        case 'cpu frequency':
734
                            if (preg_match('/^(\d+)\s+Hz/i', $arrBuff1, $bufr2)) {
735
                                $_cpus = round($bufr2[1]/1000000);
3100 rexy 736
                            } elseif (preg_match('/^(\d+)\s+MHz/i', $arrBuff1, $bufr2)) {
737
                                $_cpus = $bufr2[1];
2770 rexy 738
                            }
739
                            break;
740
                        case 'system bus frequency':
741
                            if (preg_match('/^(\d+)\s+Hz/i', $arrBuff1, $bufr2)) {
742
                                $_buss = round($bufr2[1]/1000000);
3100 rexy 743
                            } elseif (preg_match('/^(\d+)\s+MHz/i', $arrBuff1, $bufr2)) {
744
                                $_buss = $bufr2[1];
2770 rexy 745
                            }
746
                            break;
747
                        case 'bogomips per cpu':
748
                            $_bogo = round($arrBuff1);
749
                            break;
750
                        case 'vendor_id':
751
                            $_vend = $arrBuff1;
3037 rexy 752
                            break;
2770 rexy 753
                        case 'cpu':
754
                            $procname = $arrBuff1;
755
                        }
756
                    }
757
                }
758
            }
759
 
760
            //second stage
761
            $cpucount = 0;
762
            $speedset = false;
763
            foreach ($processors as $processor) if (preg_match('/^\s*processor\s*:/mi', $processor)) {
764
                $proc = null;
765
                $arch = null;
766
                $impl = null;
767
                $part = null;
3037 rexy 768
                $vari = null;
2770 rexy 769
                $dev = new CpuDevice();
770
                $details = preg_split("/\n/", $processor, -1, PREG_SPLIT_NO_EMPTY);
771
                foreach ($details as $detail) {
772
                    $arrBuff = preg_split('/\s*:\s*/', trim($detail));
773
                    if ((count($arrBuff) == 2) && (($arrBuff1 = trim($arrBuff[1])) !== '')) {
774
                        switch (strtolower($arrBuff[0])) {
775
                        case 'processor':
776
                            $proc = $arrBuff1;
777
                            if (is_numeric($proc)) {
3100 rexy 778
                                if (($procname !== null) && (strlen($procname) > 0)) {
2770 rexy 779
                                    $dev->setModel($procname);
780
                                }
781
                            } else {
782
                                $procname = $proc;
783
                                $dev->setModel($procname);
784
                            }
785
                            break;
786
                        case 'model name':
787
                        case 'cpu model':
788
                        case 'cpu type':
789
                        case 'cpu':
790
                            $dev->setModel($arrBuff1);
791
                            break;
3100 rexy 792
                        case 'cpu frequency':
793
                            if (preg_match('/^(\d+)\s+Hz/i', $arrBuff1, $bufr2)) {
794
                                if (($tmpsp = round($bufr2[1]/1000000)) > 0) {
795
                                    $dev->setCpuSpeed($tmpsp);
796
                                    $speedset = true;
797
                                }
798
                            } elseif (preg_match('/^(\d+)\s+MHz/i', $arrBuff1, $bufr2)) {
799
                                if ($bufr2[1] > 0) {
800
                                    $dev->setCpuSpeed($bufr2[1]);
801
                                    $speedset = true;
802
                                }
803
                            }
804
                            break;
2770 rexy 805
                        case 'cpu mhz':
806
                        case 'clock':
3100 rexy 807
                            if ($arrBuff1 > 0) {
2770 rexy 808
                                $dev->setCpuSpeed($arrBuff1);
809
                                $speedset = true;
810
                            }
811
                            break;
812
                        case 'cpu mhz static':
3100 rexy 813
                            $dev->setCpuSpeedMax($arrBuff1);
2770 rexy 814
                            break;
815
                        case 'cycle frequency [hz]':
3100 rexy 816
                            if (($tmpsp = round($arrBuff1/1000000)) > 0) {
817
                                $dev->setCpuSpeed($tmpsp);
818
                                $speedset = true;
819
                            }
2770 rexy 820
                            break;
3100 rexy 821
                        case 'cpu0clktck': // Linux sparc64
822
                            if (($tmpsp = round(hexdec($arrBuff1)/1000000)) > 0) {
823
                                $dev->setCpuSpeed($tmpsp);
824
                                $speedset = true;
825
                            }
2770 rexy 826
                            break;
827
                        case 'l3 cache':
828
                        case 'cache size':
829
                            $dev->setCache(trim(preg_replace("/[a-zA-Z]/", "", $arrBuff1)) * 1024);
830
                            break;
831
                        case 'initial bogomips':
832
                        case 'bogomips':
833
                        case 'cpu0bogo':
834
                            $dev->setBogomips(round($arrBuff1));
835
                            break;
836
                        case 'flags':
837
                            if (preg_match("/ vmx/", $arrBuff1)) {
838
                                $dev->setVirt("vmx");
839
                            } elseif (preg_match("/ svm/", $arrBuff1)) {
840
                                $dev->setVirt("svm");
841
                            }
3037 rexy 842
                            if (preg_match("/ hypervisor/", $arrBuff1)) {
843
                                if ($dev->getVirt() === null) {
844
                                    $dev->setVirt("hypervisor");
845
                                }
846
                                if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && ($this->system_detect_virt === null)) {
847
                                    $this->sys->setVirtualizer("hypervisor", false);
848
                                }
849
                            }
2770 rexy 850
                            break;
851
                        case 'i size':
852
                        case 'd size':
853
                            if ($dev->getCache() === null) {
854
                                $dev->setCache($arrBuff1 * 1024);
855
                            } else {
856
                                $dev->setCache($dev->getCache() + ($arrBuff1 * 1024));
857
                            }
858
                            break;
859
                        case 'cpu architecture':
860
                            $arch = $arrBuff1;
861
                            break;
862
                        case 'cpu implementer':
863
                            $impl = $arrBuff1;
864
                            break;
865
                        case 'cpu part':
866
                            $part = $arrBuff1;
867
                            break;
3037 rexy 868
                        case 'cpu variant':
869
                            $vari = $arrBuff1;
870
                            break;
2770 rexy 871
                        case 'vendor_id':
872
                            $dev->setVendorId($arrBuff1);
3037 rexy 873
                            if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && preg_match('/^User Mode Linux/', $arrBuff1)) {
874
                                $this->sys->setVirtualizer("cpuid:UserModeLinux", false);
875
                            }
2770 rexy 876
                        }
877
                    }
878
                }
879
                if ($arch === null) $arch = $_arch;
880
                if ($impl === null) $impl = $_impl;
881
                if ($part === null) $part = $_part;
3037 rexy 882
                if ($vari === null) $vari = $_vari;
2770 rexy 883
 
884
                // sparc64 specific code follows
885
                // This adds the ability to display the cache that a CPU has
886
                // Originally made by Sven Blumenstein <bazik@gentoo.org> in 2004
887
                // Modified by Tom Weustink <freshy98@gmx.net> in 2004
888
                $sparclist = array('SUNW,UltraSPARC@0,0', 'SUNW,UltraSPARC-II@0,0', 'SUNW,UltraSPARC@1c,0', 'SUNW,UltraSPARC-IIi@1c,0', 'SUNW,UltraSPARC-II@1c,0', 'SUNW,UltraSPARC-IIe@0,0');
889
                foreach ($sparclist as $name) {
890
                    if (CommonFunctions::rfts('/proc/openprom/'.$name.'/ecache-size', $buf, 1, 32, false)) {
891
                        $dev->setCache(base_convert(trim($buf), 16, 10));
892
                    }
893
                }
894
                // sparc64 specific code ends
895
 
896
                // XScale detection code
3100 rexy 897
                if (($arch === "5TE") && (($bogo = $dev->getBogomips()) !== null) && ($bogo > 0)) {
898
                    $dev->setCpuSpeed($bogo); // BogoMIPS are not BogoMIPS on this CPU, it's the speed
2770 rexy 899
                    $speedset = true;
900
                    $dev->setBogomips(null); // no BogoMIPS available, unset previously set BogoMIPS
901
                }
902
 
903
                if (($dev->getBusSpeed() == 0) && ($_buss !== null)) {
904
                    $dev->setBusSpeed($_buss);
905
                }
3100 rexy 906
                if (($dev->getCpuSpeed() == 0) && ($_cpus !== null) && ($_cpus > 0)) {
2770 rexy 907
                    $dev->setCpuSpeed($_cpus);
908
                    $speedset = true;
909
                }
910
                if (($dev->getBogomips() == 0) && ($_bogo !== null)) {
911
                    $dev->setBogomips($_bogo);
912
                }
913
                if (($dev->getVendorId() === null) && ($_vend !== null)) {
914
                    $dev->setVendorId($_vend);
3037 rexy 915
                     if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && preg_match('/^User Mode Linux/', $_vend)) {
916
                        $this->sys->setVirtualizer("cpuid:UserModeLinux", false);
917
                    }
2770 rexy 918
                }
919
 
3037 rexy 920
                if ($proc !== null) {
2770 rexy 921
                    if (!is_numeric($proc)) {
922
                        $proc = 0;
923
                    }
924
                    // variable speed processors specific code follows
3100 rexy 925
                    if (CommonFunctions::rfts('/sys/devices/system/cpu/cpu'.$proc.'/cpufreq/cpuinfo_cur_freq', $buf, 1, 4096, false)
926
                       || CommonFunctions::rfts('/sys/devices/system/cpu/cpu'.$proc.'/cpufreq/scaling_cur_freq', $buf, 1, 4096, false)) {
927
                        if (round(trim($buf)/1000) > 0) {
928
                            $dev->setCpuSpeed(round(trim($buf)/1000));
929
                            $speedset = true;
930
                        }
2770 rexy 931
                    }
3100 rexy 932
                    if (CommonFunctions::rfts('/sys/devices/system/cpu/cpu'.$proc.'/cpufreq/cpuinfo_max_freq', $buf, 1, 4096, false)
933
                       || CommonFunctions::rfts('/sys/devices/system/cpu/cpu'.$proc.'/cpufreq/scaling_max_freq', $buf, 1, 4096, false)) {
934
                        $dev->setCpuSpeedMax(round(trim($buf)/1000));
2770 rexy 935
                    }
3100 rexy 936
                    if (CommonFunctions::rfts('/sys/devices/system/cpu/cpu'.$proc.'/cpufreq/cpuinfo_min_freq', $buf, 1, 4096, false)
937
                       || CommonFunctions::rfts('/sys/devices/system/cpu/cpu'.$proc.'/cpufreq/scaling_min_freq', $buf, 1, 4096, false)) {
938
                        $dev->setCpuSpeedMin(round(trim($buf)/1000));
2770 rexy 939
                    }
940
                    // variable speed processors specific code ends
941
                    if (PSI_LOAD_BAR) {
942
                            $dev->setLoad($this->_parseProcStat('cpu'.$proc));
943
                    }
944
/*
945
                    if (CommonFunctions::rfts('/proc/acpi/thermal_zone/THRM/temperature', $buf, 1, 4096, false)
946
                       &&  preg_match("/(\S+)\sC$/", $buf, $value)) {
947
                        $dev->setTemp(value[1]);
948
                    }
949
*/
950
                    if (($arch !== null) && ($impl !== null) && ($part !== null)) {
951
                        if (($impl === '0x41')
952
                           && (($_hard === 'BCM2708') || ($_hard === 'BCM2835') || ($_hard === 'BCM2709') || ($_hard === 'BCM2836') || ($_hard === 'BCM2710') || ($_hard === 'BCM2837') || ($_hard === 'BCM2711') || ($_hard === 'BCM2838'))
953
                           && ($_revi !== null)) { // Raspberry Pi detection (instead of 'cat /proc/device-tree/model')
954
                            if ($raslist === null) $raslist = @parse_ini_file(PSI_APP_ROOT."/data/raspberry.ini", true);
3100 rexy 955
                            $oldmach = $this->sys->getMachine();
956
                            if (($oldmach !== '') && preg_match("/^raspberrypi rpi(,.+)/", $oldmach, $machbuf)) {
957
                                $oldmachend = $machbuf[1];
958
                            } else {
959
                                 $oldmachend = '';
960
                            }
2770 rexy 961
                            if ($raslist && !preg_match('/[^0-9a-f]/', $_revi)) {
962
                                if (($revidec = hexdec($_revi)) & 0x800000) {
3100 rexy 963
                                    if (($oldmach === '') || ($oldmachend !== '')) {
2770 rexy 964
                                        $manufacturer = ($revidec >> 16) & 15;
965
                                        if (isset($raslist['manufacturer'][$manufacturer])) {
966
                                            $manuf = ' '.$raslist['manufacturer'][$manufacturer];
967
                                        } else {
968
                                            $manuf = '';
969
                                        }
970
                                        $model = ($revidec >> 4) & 255;
971
                                        if (isset($raslist['model'][$model])) {
3100 rexy 972
                                            $this->sys->setMachine('Raspberry Pi '.$raslist['model'][$model].' (PCB 1.'.($revidec & 15).$manuf.')'.$oldmachend);
2770 rexy 973
                                        } else {
3100 rexy 974
                                            $this->sys->setMachine('Raspberry Pi (PCB 1.'.($revidec & 15).$manuf.')'.$oldmachend);
2770 rexy 975
                                        }
976
                                    }
977
                                } else {
3100 rexy 978
                                    if (($oldmach === '') || ($oldmachend !== '')) {
2770 rexy 979
                                        if (isset($raslist['old'][$revidec & 0x7fffff])) {
3100 rexy 980
                                            $this->sys->setMachine('Raspberry Pi '.$raslist['old'][$revidec & 0x7fffff].$oldmachend);
2770 rexy 981
                                        } else {
3100 rexy 982
                                            $this->sys->setMachine('Raspberry Pi'.$oldmachend);
2770 rexy 983
                                        }
984
                                    }
985
                                }
986
                            }
987
                        } elseif (($_hard !== null) && ($this->sys->getMachine() === '')) { // other ARM hardware
988
                            $this->sys->setMachine($_hard);
989
                        }
990
                        if ($cpulist === null) $cpulist = @parse_ini_file(PSI_APP_ROOT."/data/cpus.ini", true);
3037 rexy 991
                        if ($cpulist && (((($vari !== null) && isset($cpulist['cpu'][$cpufromlist = strtolower($impl.','.$part.','.$vari)]))
992
                           || isset($cpulist['cpu'][$cpufromlist = strtolower($impl.','.$part)])))) {
2770 rexy 993
                            if (($cpumodel = $dev->getModel()) !== '') {
3037 rexy 994
                                $dev->setModel($cpumodel.' - '.$cpulist['cpu'][$cpufromlist]);
2770 rexy 995
                            } else {
3037 rexy 996
                                $dev->setModel($cpulist['cpu'][$cpufromlist]);
2770 rexy 997
                            }
998
                        }
999
                    } elseif (($_hard !== null) && ($this->sys->getMachine() === '')) { // other hardware
1000
                        $this->sys->setMachine($_hard);
1001
                    }
1002
 
3037 rexy 1003
                    $cpumodel = $dev->getModel();
1004
                    if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO && ($this->system_detect_virt === null) && preg_match('/^QEMU Virtual CPU version /', $cpumodel)) {
1005
                        $this->sys->setVirtualizer("cpuid:QEMU", false);
2770 rexy 1006
                    }
3037 rexy 1007
                    if ($cpumodel === "") {
1008
                        if (($vendid = $dev->getVendorId()) !== "") {
1009
                            $dev->setModel($vendid);
1010
                        } else {
1011
                            $dev->setModel("unknown");
1012
                        }
1013
                    }
2770 rexy 1014
                    $cpucount++;
1015
                    $this->sys->setCpus($dev);
1016
                }
1017
            }
1018
 
3037 rexy 1019
            $cpudevices = CommonFunctions::findglob('/sys/devices/system/cpu/cpu*/uevent', GLOB_NOSORT);
2770 rexy 1020
            if (is_array($cpudevices) && (($cpustopped = count($cpudevices)-$cpucount) > 0)) {
1021
                for (; $cpustopped > 0; $cpustopped--) {
1022
                    $dev = new CpuDevice();
1023
                    $dev->setModel("stopped");
1024
                    if ($speedset) {
1025
                        $dev->setCpuSpeed(-1);
1026
                    }
1027
                    $this->sys->setCpus($dev);
1028
                }
1029
            }
1030
        }
325 richard 1031
    }
1032
 
2770 rexy 1033
    /**
1034
     * PCI devices
1035
     *
1036
     * @return void
1037
     */
1038
    private function _pci()
1039
    {
1040
        if ($arrResults = Parser::lspci()) {
1041
            foreach ($arrResults as $dev) {
1042
                $this->sys->setPciDevices($dev);
1043
            }
1044
        } elseif (CommonFunctions::rfts('/proc/pci', $strBuf, 0, 4096, false)) {
1045
            $booDevice = false;
1046
            $arrBuf = preg_split("/\n/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
1047
            foreach ($arrBuf as $strLine) {
1048
                if (preg_match('/^\s*Bus\s/', $strLine)) {
1049
                    $booDevice = true;
1050
                    continue;
1051
                }
1052
                if ($booDevice) {
1053
                    $dev = new HWDevice();
2976 rexy 1054
                    $dev->setName(preg_replace('/\(rev\s[^\)]+\)\.$/', '', trim($strLine)));
2770 rexy 1055
                    $this->sys->setPciDevices($dev);
1056
/*
1057
                    list($strKey, $strValue) = preg_split('/: /', $strLine, 2);
1058
                    if (!preg_match('/bridge/i', $strKey) && !preg_match('/USB/i ', $strKey)) {
1059
                        $dev = new HWDevice();
2976 rexy 1060
                        $dev->setName(preg_replace('/\(rev\s[^\)]+\)\.$/', '', trim($strValue)));
2770 rexy 1061
                        $this->sys->setPciDevices($dev);
1062
                    }
1063
*/
1064
                    $booDevice = false;
1065
                }
1066
            }
1067
        } else {
3037 rexy 1068
            $pcidevices = CommonFunctions::findglob('/sys/bus/pci/devices/*/uevent', GLOB_NOSORT);
2770 rexy 1069
            if (is_array($pcidevices) && (($total = count($pcidevices)) > 0)) {
1070
                $buf = "";
1071
                for ($i = 0; $i < $total; $i++) {
1072
                    if (CommonFunctions::rfts($pcidevices[$i], $buf, 0, 4096, false) && (trim($buf) != "")) {
1073
                        $pcibuf = "";
1074
                        if (preg_match("/^PCI_CLASS=(\S+)/m", trim($buf), $subbuf)) {
1075
                            $pcibuf = "Class ".$subbuf[1].":";
1076
                        }
1077
                        if (preg_match("/^PCI_ID=(\S+)/m", trim($buf), $subbuf)) {
1078
                            $pcibuf .= " Device ".$subbuf[1];
1079
                        }
1080
                        if (preg_match("/^DRIVER=(\S+)/m", trim($buf), $subbuf)) {
1081
                            $pcibuf .= " Driver ".$subbuf[1];
1082
                        }
1083
                        $dev = new HWDevice();
1084
                        if (trim($pcibuf) != "") {
1085
                            $dev->setName(trim($pcibuf));
1086
                        } else {
1087
                            $dev->setName("unknown");
1088
                        }
1089
                        $this->sys->setPciDevices($dev);
1090
                    }
1091
                }
1092
            }
1093
        }
1094
    }
325 richard 1095
 
2770 rexy 1096
    /**
1097
     * IDE devices
1098
     *
1099
     * @return void
1100
     */
1101
    private function _ide()
1102
    {
1103
        $bufd = CommonFunctions::gdc('/proc/ide', false);
1104
        foreach ($bufd as $file) {
1105
            if (preg_match('/^hd/', $file)) {
1106
                $dev = new HWDevice();
1107
                $dev->setName(trim($file));
1108
                if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS && CommonFunctions::rfts("/proc/ide/".$file."/media", $buf, 1)) {
1109
                    if (trim($buf) == 'disk') {
1110
                        if (CommonFunctions::rfts("/proc/ide/".$file."/capacity", $buf, 1, 4096, false) || CommonFunctions::rfts("/sys/block/".$file."/size", $buf, 1, 4096, false)) {
1111
                            $dev->setCapacity(trim($buf) * 512);
1112
                        }
1113
                    }
1114
                }
1115
                if (CommonFunctions::rfts("/proc/ide/".$file."/model", $buf, 1)) {
1116
                    $dev->setName($dev->getName().": ".trim($buf));
1117
                }
1118
                $this->sys->setIdeDevices($dev);
1119
            }
1120
        }
1121
    }
325 richard 1122
 
2770 rexy 1123
    /**
1124
     * SCSI devices
1125
     *
1126
     * @return void
1127
     */
1128
    private function _scsi()
1129
    {
1130
        $getline = 0;
1131
        $device = null;
1132
        $scsiid = null;
1133
        if (CommonFunctions::executeProgram('lsscsi', '-c', $bufr, PSI_DEBUG) || CommonFunctions::rfts('/proc/scsi/scsi', $bufr, 0, 4096, PSI_DEBUG)) {
1134
            $bufe = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
1135
            foreach ($bufe as $buf) {
1136
                if (preg_match('/Host: scsi(\d+) Channel: (\d+) Target: (\d+) Lun: (\d+)/i', $buf, $scsiids)
1137
                   || preg_match('/Host: scsi(\d+) Channel: (\d+) Id: (\d+) Lun: (\d+)/i', $buf, $scsiids)) {
1138
                    $scsiid = $scsiids;
1139
                    $getline = 1;
1140
                    continue;
1141
                }
1142
                if ($getline == 1) {
1143
                    preg_match('/Vendor: (.*) Model: (.*) Rev: (.*)/i', $buf, $devices);
1144
                    $getline = 2;
1145
                    $device = $devices;
1146
                    continue;
1147
                }
1148
                if ($getline == 2) {
1149
                    preg_match('/Type:\s+(\S+)/i', $buf, $dev_type);
1150
 
1151
                    $dev = new HWDevice();
1152
                    $dev->setName($device[1].' '.$device[2].' ('.$dev_type[1].')');
1153
 
1154
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
1155
                       && ($dev_type[1]==='Direct-Access')) {
3037 rexy 1156
                       $sizelist = CommonFunctions::findglob('/sys/bus/scsi/devices/'.intval($scsiid[1]).':'.intval($scsiid[2]).':'.intval($scsiid[3]).':'.intval($scsiid[4]).'/*/*/size', GLOB_NOSORT);
2770 rexy 1157
                       if (is_array($sizelist) && (($total = count($sizelist)) > 0)) {
1158
                           $buf = "";
1159
                           for ($i = 0; $i < $total; $i++) {
1160
                               if (CommonFunctions::rfts($sizelist[$i], $buf, 1, 4096, false) && (($buf=trim($buf)) != "") && ($buf > 0)) {
1161
                                   $dev->setCapacity($buf * 512);
1162
                                   break;
1163
                               }
1164
                           }
1165
                       }
1166
                    }
1167
                    $this->sys->setScsiDevices($dev);
1168
                    $getline = 0;
1169
                }
325 richard 1170
            }
2770 rexy 1171
        }
325 richard 1172
    }
1173
 
2770 rexy 1174
    /**
1175
     * USB devices
1176
     *
1177
     * @return void
1178
     */
3100 rexy 1179
    protected function _usb($bufu = null)
2770 rexy 1180
    {
1181
        $usbarray = array();
3100 rexy 1182
        if ($nobufu = ($bufu === null)) {
1183
            if (CommonFunctions::executeProgram('lsusb', (PSI_OS != 'Android')?'':'2>/dev/null', $bufr, PSI_DEBUG && (PSI_OS != 'Android'), 5) && ($bufr !== "")) {
1184
                $bufe = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
1185
                foreach ($bufe as $buf) {
1186
                    $device = preg_split("/ /", $buf, 7);
1187
                    if (((isset($device[6]) && trim($device[6]) != "")) ||
1188
                        ((isset($device[5]) && trim($device[5]) != ""))) {
1189
                        $usbid = intval($device[1]).'-'.intval(trim($device[3], ':')).' '.$device[5];
1190
                        if ((isset($device[6]) && trim($device[6]) != "")) {
1191
                            $usbarray[$usbid]['name'] = trim($device[6]);
1192
                        } else {
1193
                            $usbarray[$usbid]['name'] = 'unknown';
1194
                        }
2770 rexy 1195
                    }
1196
                }
1197
            }
325 richard 1198
 
3100 rexy 1199
            $usbdevices = CommonFunctions::findglob('/sys/bus/usb/devices/*/idProduct', GLOB_NOSORT);
1200
            if (is_array($usbdevices) && (($total = count($usbdevices)) > 0)) {
1201
                for ($i = 0; $i < $total; $i++) {
1202
                    if (CommonFunctions::rfts($usbdevices[$i], $idproduct, 1, 4096, false) && (($idproduct=trim($idproduct)) != "")) { // is readable
1203
                        $busnum = CommonFunctions::rolv($usbdevices[$i], '/\/idProduct$/', '/busnum');
1204
                        $devnum = CommonFunctions::rolv($usbdevices[$i], '/\/idProduct$/', '/devnum');
1205
                        $idvendor = CommonFunctions::rolv($usbdevices[$i], '/\/idProduct$/', '/idVendor');
1206
                        if (($busnum!==null) && ($devnum!==null) && ($idvendor!==null)) {
1207
                            $usbid = intval($busnum).'-'.intval($devnum).' '.$idvendor.':'.$idproduct;
1208
                            $manufacturer = CommonFunctions::rolv($usbdevices[$i], '/\/idProduct$/', '/manufacturer');
1209
                            if ($manufacturer!==null) {
1210
                                $usbarray[$usbid]['manufacturer'] = $manufacturer;
2770 rexy 1211
                            }
3100 rexy 1212
                            $product = CommonFunctions::rolv($usbdevices[$i], '/\/idProduct$/', '/product');
1213
                            if ($product!==null) {
1214
                                $usbarray[$usbid]['product'] = $product;
1215
                            }
1216
                            $speed = CommonFunctions::rolv($usbdevices[$i], '/\/idProduct$/', '/speed');
1217
                            if ($product!==null) {
1218
                                $usbarray[$usbid]['speed'] = $speed;
1219
                            }
1220
                            if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
1221
                               && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
1222
                                $serial = CommonFunctions::rolv($usbdevices[$i], '/\/idProduct$/', '/serial');
1223
                                if (($serial!==null) && !preg_match('/\W/', $serial)) {
1224
                                    $usbarray[$usbid]['serial'] = $serial;
1225
                                }
1226
                            }
2770 rexy 1227
                        }
1228
                    }
1229
                }
1230
            }
1231
        }
325 richard 1232
 
3100 rexy 1233
        if (!$nobufu || ((count($usbarray) == 0) && CommonFunctions::rfts('/proc/bus/usb/devices', $bufu, 0, 4096, false))) { // usb-devices
2770 rexy 1234
            $devnum = -1;
3100 rexy 1235
            $bufe = preg_split("/\n/", $bufu, -1, PREG_SPLIT_NO_EMPTY);
2770 rexy 1236
            foreach ($bufe as $buf) {
1237
                if (preg_match('/^T/', $buf)) {
1238
                    $devnum++;
2976 rexy 1239
                    if (preg_match('/\sSpd=([\d\.]+)/', $buf, $bufr)
1240
                       && isset($bufr[1]) && ($bufr[1]!=="")) {
1241
                        $usbarray[$devnum]['speed'] = $bufr[1];
1242
                    }
2770 rexy 1243
                } elseif (preg_match('/^S:/', $buf)) {
1244
                    list($key, $value) = preg_split('/: /', $buf, 2);
1245
                    list($key, $value2) = preg_split('/=/', $value, 2);
1246
                    switch (trim($key)) {
1247
                    case 'Manufacturer':
1248
                        $usbarray[$devnum]['manufacturer'] = trim($value2);
1249
                        break;
1250
                    case 'Product':
1251
                        $usbarray[$devnum]['product'] = trim($value2);
1252
                        break;
1253
                    case 'SerialNumber':
1254
                        if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
1255
                           && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL
1256
                           && !preg_match('/\W/', trim($value2))) {
1257
                            $usbarray[$devnum]['serial'] = trim($value2);
3037 rexy 1258
                        }
2770 rexy 1259
                    }
1260
                }
1261
            }
1262
        }
325 richard 1263
 
3100 rexy 1264
        if ($nobufu && (count($usbarray) == 0) && CommonFunctions::rfts('/proc/bus/input/devices', $bufr, 0, 4096, false)) {
2770 rexy 1265
            $devnam = "unknown";
1266
            $bufe = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
1267
            foreach ($bufe as $buf) {
1268
                if (preg_match('/^I:\s+(.+)/', $buf, $bufr)
1269
                   && isset($bufr[1]) && (trim($bufr[1])!=="")) {
1270
                    $devnam = trim($bufr[1]);
1271
                    $usbarray[$devnam]['phys'] = 'unknown';
1272
                } elseif (preg_match('/^N:\s+Name="([^"]+)"/', $buf, $bufr2)
1273
                   && isset($bufr2[1]) && (trim($bufr2[1])!=="")) {
1274
                    $usbarray[$devnam]['name'] = trim($bufr2[1]);
1275
                } elseif (preg_match('/^P:\s+Phys=(.*)/', $buf, $bufr2)
1276
                   && isset($bufr2[1]) && (trim($bufr2[1])!=="")) {
1277
                    $usbarray[$devnam]['phys'] = trim($bufr2[1]);
1278
                } elseif (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS
1279
                   && defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL
1280
                   && preg_match('/^U:\s+Uniq=(.+)/', $buf, $bufr2)
1281
                   && isset($bufr2[1]) && (trim($bufr2[1])!=="")) {
1282
                    $usbarray[$devnam]['serial'] = trim($bufr2[1]);
1283
                }
1284
            }
1285
        }
325 richard 1286
 
2770 rexy 1287
        foreach ($usbarray as $usbdev) if (!isset($usbdev['phys']) || preg_match('/^usb-/', $usbdev['phys'])) {
1288
            $dev = new HWDevice();
325 richard 1289
 
2770 rexy 1290
            if (isset($usbdev['manufacturer']) && (($manufacturer=$usbdev['manufacturer']) !== 'no manufacturer')) {
1291
                if (preg_match("/^linux\s/i", $manufacturer)) {
1292
                    $manufacturer = 'Linux Foundation';
1293
                }
1294
                if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
1295
                    $dev->setManufacturer($manufacturer);
1296
                }
1297
            } else {
1298
                $manufacturer = '';
1299
            }
1300
 
1301
            if (isset($usbdev['product'])) {
1302
                $product = $usbdev['product'];
1303
                if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
1304
                    $dev->setProduct($product);
1305
                }
1306
            } else {
1307
                $product = '';
1308
            }
1309
 
2976 rexy 1310
            if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
1311
                if (isset($usbdev['speed'])) {
1312
                    $dev->setSpeed($usbdev['speed']);
1313
                }
1314
                if (defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL
1315
                   && isset($usbdev['serial'])) {
1316
                    $dev->setSerial($usbdev['serial']);
1317
                }
2770 rexy 1318
            }
1319
 
1320
            if (isset($usbdev['name']) && (($name=$usbdev['name']) !== 'unknown')) {
1321
                $dev->setName($name);
1322
            } else {
1323
                if (($newname = trim($manufacturer.' '.$product)) !== '') {
1324
                    $dev->setName($newname);
1325
                } else {
1326
                    $dev->setName('unknown');
1327
                }
1328
            }
1329
 
1330
            $this->sys->setUsbDevices($dev);
1331
        }
325 richard 1332
    }
1333
 
2770 rexy 1334
    /**
1335
     * I2C devices
1336
     *
1337
     * @return void
1338
     */
1339
    protected function _i2c()
1340
    {
3037 rexy 1341
        $i2cdevices = CommonFunctions::findglob('/sys/bus/i2c/devices/*/name', GLOB_NOSORT);
2770 rexy 1342
        if (is_array($i2cdevices) && (($total = count($i2cdevices)) > 0)) {
1343
            $buf = "";
1344
            for ($i = 0; $i < $total; $i++) {
1345
                if (CommonFunctions::rfts($i2cdevices[$i], $buf, 1, 4096, false) && (trim($buf) != "")) {
1346
                    $dev = new HWDevice();
1347
                    $dev->setName(trim($buf));
1348
                    $this->sys->setI2cDevices($dev);
1349
                }
1350
            }
1351
        }
1352
    }
325 richard 1353
 
2770 rexy 1354
    /**
1355
     * NVMe devices
1356
     *
1357
     * @return void
1358
     */
1359
    protected function _nvme()
1360
    {
1361
        if (CommonFunctions::executeProgram('nvme', 'list', $bufr, PSI_DEBUG) && ($bufr!="")) {
1362
            $bufe = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
1363
            $count = 0;
1364
            $nlocate = array();
1365
            $nsize = array();
1366
            foreach ($bufe as $buf) {
1367
                if ($count == 1) {
1368
                    $locid = 0;
1369
                    $nlocate[0] = 0;
1370
                    $total = strlen($buf);
1371
                    $begin = true;
1372
                    for ($i = 0; $i < $total; $i++) {
1373
                        if ($begin) {
1374
                            if ($buf[$i] !== '-') {
1375
                                $nsize[$locid] = $i - $nlocate[$locid];
1376
                                $locid++;
1377
                                $begin = false;
1378
                            }
1379
                        } else {
1380
                            if ($buf[$i] === '-') {
1381
                                $nlocate[$locid] = $i;
1382
                                $begin = true;
1383
                            }
1384
                        }
1385
                    }
1386
                    if ($begin) {
1387
                        $nsize[$locid] = $i - $nlocate[$locid];
1388
                    }
1389
                } elseif ($count > 1) {
1390
                    if (isset($nlocate[2]) && isset($nsize[2])) {
1391
                        $dev = new HWDevice();
1392
                        $dev->setName(trim(substr($buf, $nlocate[2], $nsize[2])));
1393
                        if (defined('PSI_SHOW_DEVICES_INFOS') && (PSI_SHOW_DEVICES_INFOS)) {
1394
                            if (isset($nlocate[4]) && isset($nsize[4])) {
1395
                                if (preg_match('/\/\s*([0-9\.]+)\s*(B|KB|MB|GB|TB|PB)$/', str_replace(',', '.', trim(substr($buf, $nlocate[4], $nsize[4]))), $tmpbuf)) {
1396
                                    switch ($tmpbuf[2]) {
3037 rexy 1397
                                    case 'B':
1398
                                        $dev->setCapacity($tmpbuf[1]);
1399
                                        break;
1400
                                    case 'KB':
1401
                                        $dev->setCapacity(1000*$tmpbuf[1]);
1402
                                        break;
1403
                                    case 'MB':
1404
                                        $dev->setCapacity(1000*1000*$tmpbuf[1]);
1405
                                        break;
1406
                                    case 'GB':
1407
                                        $dev->setCapacity(1000*1000*1000*$tmpbuf[1]);
1408
                                        break;
1409
                                    case 'TB':
1410
                                        $dev->setCapacity(1000*1000*1000*1000*$tmpbuf[1]);
1411
                                        break;
1412
                                    case 'PB':
1413
                                        $dev->setCapacity(1000*1000*1000*1000*1000*$tmpbuf[1]);
2770 rexy 1414
                                    }
1415
                                }
1416
                            }
1417
                            if (defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL) {
1418
                                if (isset($nlocate[1]) && isset($nsize[1])) {
1419
                                    $dev->setSerial(trim(substr($buf, $nlocate[1], $nsize[1])));
1420
                                }
1421
                            }
1422
                        }
1423
                        $this->sys->setNvmeDevices($dev);
1424
                    }
1425
                }
1426
                $count++;
1427
            }
1428
        }
1429
    }
325 richard 1430
 
2770 rexy 1431
    /**
1432
     * Network devices
1433
     * includes also rx/tx bytes
1434
     *
1435
     * @return void
1436
     */
3100 rexy 1437
    protected function _network($bufr = null)
2770 rexy 1438
    {
3100 rexy 1439
        if (($bufr === null) && CommonFunctions::rfts('/proc/net/dev', $bufr, 0, 4096, PSI_DEBUG)) {
2770 rexy 1440
            $bufe = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
1441
            foreach ($bufe as $buf) {
1442
                if (preg_match('/:/', $buf)) {
1443
                    list($dev_name, $stats_list) = preg_split('/:/', $buf, 2);
1444
                    $stats = preg_split('/\s+/', trim($stats_list));
1445
                    $dev = new NetDevice();
1446
                    $dev->setName(trim($dev_name));
1447
                    $dev->setRxBytes($stats[0]);
1448
                    $dev->setTxBytes($stats[8]);
1449
                    $dev->setErrors($stats[2] + $stats[10]);
1450
                    $dev->setDrops($stats[3] + $stats[11]);
1451
                    if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
1452
                        $macaddr = "";
1453
                        if ((CommonFunctions::executeProgram('ip', 'addr show '.trim($dev_name), $bufr2, PSI_DEBUG) && ($bufr2!=""))
1454
                           || CommonFunctions::executeProgram('ifconfig', trim($dev_name).' 2>/dev/null', $bufr2, PSI_DEBUG)) {
1455
                            $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
1456
                            foreach ($bufe2 as $buf2) {
1457
//                                if (preg_match('/^'.trim($dev_name).'\s+Link\sencap:Ethernet\s+HWaddr\s(\S+)/i', $buf2, $ar_buf2)
1458
                                if (preg_match('/\s+encap:Ethernet\s+HWaddr\s(\S+)/i', $buf2, $ar_buf2)
1459
                                   || preg_match('/\s+encap:UNSPEC\s+HWaddr\s(\S+)-00-00-00-00-00-00-00-00-00-00\s*$/i', $buf2, $ar_buf2)
1460
                                   || preg_match('/^\s+ether\s+(\S+)\s+txqueuelen/i', $buf2, $ar_buf2)
2976 rexy 1461
                                   || preg_match('/^\s+link\/\S+\s+(\S+)\s+brd/i', $buf2, $ar_buf2)
1462
                                   || preg_match('/^\s+link\/\S+\s+(\S+)$/i', $buf2, $ar_buf2)) {
2770 rexy 1463
                                    if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) {
1464
                                        $macaddr = preg_replace('/:/', '-', strtoupper($ar_buf2[1]));
3037 rexy 1465
                                        if (($macaddr === '00-00-00-00-00-00') || ($macaddr === '00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00') || ($macaddr === '--') || ($macaddr === '0.0.0.0')) { // empty
2770 rexy 1466
                                            $macaddr = "";
1467
                                        }
1468
                                    }
1469
                                } elseif (preg_match('/^\s+inet\saddr:(\S+)\s+P-t-P:(\S+)/i', $buf2, $ar_buf2)
1470
                                       || preg_match('/^\s+inet\s+(\S+)\s+netmask.+destination\s+(\S+)/i', $buf2, $ar_buf2)
1471
                                       || preg_match('/^\s+inet\s+([^\/\s]+).*peer\s+([^\/\s]+).*\s+scope\s((global)|(host))/i', $buf2, $ar_buf2)) {
1472
                                    if ($ar_buf2[1] != $ar_buf2[2]) {
1473
                                        $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1].";:".$ar_buf2[2]);
1474
                                    } else {
1475
                                        $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
1476
                                    }
1477
                                } elseif ((preg_match('/^\s+inet\saddr:(\S+)/i', $buf2, $ar_buf2)
1478
                                   || preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $buf2, $ar_buf2)
1479
                                   || preg_match('/^'.trim($dev_name).':\s+ip\s+(\S+)\s+mask/i', $buf2, $ar_buf2)
1480
                                   || preg_match('/^\s+inet6\saddr:\s([^\/\s]+)(.+)\s+Scope:[GH]/i', $buf2, $ar_buf2)
1481
                                   || preg_match('/^\s+inet6\s+(\S+)\s+prefixlen(.+)((<global>)|(<host>))/i', $buf2, $ar_buf2)
3100 rexy 1482
                                   || preg_match('/^\s+inet6?\s+([^\/\s]+).*\s+scope\s((global)|(host))/i', $buf2, $ar_buf2)
1483
                                   || preg_match('/^\s+inet\saddr6:\s+(\S+)\s+prefixlen(.+)/i', $buf2, $ar_buf2))
2770 rexy 1484
                                   && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1])) {
1485
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
1486
                                }
1487
                            }
1488
                        }
1489
                        if ($macaddr != "") {
1490
                            $dev->setInfo($macaddr.($dev->getInfo()?';'.$dev->getInfo():''));
1491
                        }
2976 rexy 1492
                        if ((!CommonFunctions::rfts('/sys/class/net/'.trim($dev_name).'/operstate', $buf, 1, 4096, false) || (($down=strtolower(trim($buf)))=="") || ($down!=="down")) &&
1493
                           (CommonFunctions::rfts('/sys/class/net/'.trim($dev_name).'/speed', $buf, 1, 4096, false) && (($speed=trim($buf))!="") && ($buf > 0) && ($buf < 65535))) {
2770 rexy 1494
                            if ($speed > 1000) {
1495
                                $speed = $speed/1000;
1496
                                $unit = "G";
1497
                            } else {
1498
                                $unit = "M";
1499
                            }
1500
                            if (CommonFunctions::rfts('/sys/class/net/'.trim($dev_name).'/duplex', $buf, 1, 4096, false) && (($duplex=strtolower(trim($buf)))!="") && ($duplex!='unknown')) {
1501
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$speed.$unit.'b/s '.$duplex);
1502
                            } else {
1503
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$speed.$unit.'b/s');
1504
                            }
1505
                        }
1506
                    }
1507
                    $this->sys->setNetDevices($dev);
1508
                }
1509
            }
3100 rexy 1510
        } elseif (($bufr === null) && CommonFunctions::executeProgram('ip', 'addr show', $bufr, PSI_DEBUG) && ($bufr!="")) {
2770 rexy 1511
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
1512
            $was = false;
1513
            $macaddr = "";
1514
            $speedinfo = "";
1515
            $dev = null;
1516
            foreach ($lines as $line) {
1517
                if (preg_match("/^\d+:\s+([^\s:]+)/", $line, $ar_buf)) {
1518
                    if ($was) {
1519
                        if ($macaddr != "") {
1520
                            $dev->setInfo($macaddr.($dev->getInfo()?';'.$dev->getInfo():''));
1521
                        }
1522
                        if ($speedinfo != "") {
1523
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$speedinfo);
1524
                        }
1525
                        $this->sys->setNetDevices($dev);
1526
                    }
1527
                    $speedinfo = "";
1528
                    $macaddr = "";
1529
                    $dev = new NetDevice();
1530
                    $dev->setName($ar_buf[1]);
1531
                    if (CommonFunctions::executeProgram('ip', '-s link show '.$ar_buf[1], $bufr2, PSI_DEBUG) && ($bufr2!="")
1532
                       && preg_match("/\n\s+RX:\s[^\n]+\n\s+(\d+)\s+\d+\s+(\d+)\s+(\d+)[^\n]+\n\s+TX:\s[^\n]+\n\s+(\d+)\s+\d+\s+(\d+)\s+(\d+)/m", $bufr2, $ar_buf2)) {
1533
                        $dev->setRxBytes($ar_buf2[1]);
1534
                        $dev->setTxBytes($ar_buf2[4]);
1535
                        $dev->setErrors($ar_buf2[2]+$ar_buf2[5]);
1536
                        $dev->setDrops($ar_buf2[3]+$ar_buf2[6]);
1537
                    }
1538
                    $was = true;
1539
                    if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
2976 rexy 1540
                        if ((!CommonFunctions::rfts('/sys/class/net/'.$ar_buf[1].'/operstate', $buf, 1, 4096, false) || (($down=strtolower(trim($buf)))=="") || ($down!=="down")) &&
1541
                           (CommonFunctions::rfts('/sys/class/net/'.$ar_buf[1].'/speed', $buf, 1, 4096, false) && (($speed=trim($buf))!="") && ($buf > 0) && ($buf < 65535))) {
2770 rexy 1542
                            if ($speed > 1000) {
1543
                                $speed = $speed/1000;
1544
                                $unit = "G";
1545
                            } else {
1546
                                $unit = "M";
1547
                            }
2976 rexy 1548
                            if (CommonFunctions::rfts('/sys/class/net/'.$ar_buf[1].'/duplex', $buf, 1, 4096, false) && (($duplex=strtolower(trim($buf)))!="") && ($duplex!='unknown')) {
1549
                                $speedinfo = $speed.$unit.'b/s '.$duplex;
2770 rexy 1550
                            } else {
1551
                                $speedinfo = $speed.$unit.'b/s';
1552
                            }
1553
                        }
1554
                    }
1555
                } else {
1556
                    if ($was) {
1557
                        if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
2976 rexy 1558
                            if (preg_match('/^\s+link\/\S+\s+(\S+)\s+brd/i', $line, $ar_buf2)
1559
                               || preg_match('/^\s+link\/\S+\s+(\S+)$/i', $line, $ar_buf2)) {
3037 rexy 1560
                                if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) {
1561
                                    $macaddr = preg_replace('/:/', '-', strtoupper($ar_buf2[1]));
1562
                                    if (($macaddr === '00-00-00-00-00-00') || ($macaddr === '00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00') || ($macaddr === '--') || ($macaddr === '0.0.0.0')) { // empty
1563
                                        $macaddr = "";
1564
                                    }
1565
                                }
2770 rexy 1566
                            } elseif (preg_match('/^\s+inet\s+([^\/\s]+).*peer\s+([^\/\s]+).*\s+scope\s((global)|(host))/i', $line, $ar_buf2)) {
1567
                                if ($ar_buf2[1] != $ar_buf2[2]) {
1568
                                     $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1].";:".$ar_buf2[2]);
1569
                                } else {
1570
                                     $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
1571
                                }
1572
                            } elseif (preg_match('/^\s+inet6?\s+([^\/\s]+).*\s+scope\s((global)|(host))/i', $line, $ar_buf2)
1573
                                     && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1])) {
1574
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
1575
                            }
1576
                        }
1577
                    }
1578
                }
1579
            }
1580
            if ($was) {
1581
                if ($macaddr != "") {
1582
                    $dev->setInfo($macaddr.($dev->getInfo()?';'.$dev->getInfo():''));
1583
                }
1584
                if ($speedinfo != "") {
1585
                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$speedinfo);
1586
                }
1587
                $this->sys->setNetDevices($dev);
1588
            }
3100 rexy 1589
        } elseif (($bufr !== null) || CommonFunctions::executeProgram('ifconfig', '-a', $bufr, PSI_DEBUG)) {
2770 rexy 1590
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
1591
            $was = false;
1592
            $errors = 0;
1593
            $drops = 0;
1594
            $macaddr = "";
1595
            $speedinfo = "";
1596
            $dev = null;
1597
            foreach ($lines as $line) {
1598
                if (preg_match("/^([^\s:]+)/", $line, $ar_buf)) {
1599
                    if ($was) {
1600
                        $dev->setErrors($errors);
1601
                        $dev->setDrops($drops);
1602
                        if ($macaddr != "") {
1603
                            $dev->setInfo($macaddr.($dev->getInfo()?';'.$dev->getInfo():''));
1604
                        }
1605
                        if ($speedinfo != "") {
1606
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$speedinfo);
1607
                        }
1608
                        $this->sys->setNetDevices($dev);
1609
                    }
1610
                    $errors = 0;
1611
                    $drops = 0;
1612
                    $speedinfo = "";
1613
                    $macaddr = "";
1614
                    $dev = new NetDevice();
1615
                    $dev->setName($ar_buf[1]);
1616
                    $was = true;
1617
                    if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
2976 rexy 1618
                        if ((!CommonFunctions::rfts('/sys/class/net/'.$ar_buf[1].'/operstate', $buf, 1, 4096, false) || (($down=strtolower(trim($buf)))=="") || ($down!=="down")) &&
1619
                           (CommonFunctions::rfts('/sys/class/net/'.$ar_buf[1].'/speed', $buf, 1, 4096, false) && (($speed=trim($buf))!="") && ($buf > 0) && ($buf < 65535))) {
2770 rexy 1620
                            if ($speed > 1000) {
1621
                                $speed = $speed/1000;
1622
                                $unit = "G";
1623
                            } else {
1624
                                $unit = "M";
1625
                            }
2976 rexy 1626
                            if (CommonFunctions::rfts('/sys/class/net/'.$ar_buf[1].'/duplex', $buf, 1, 4096, false) && (($duplex=strtolower(trim($buf)))!="") && ($duplex!='unknown')) {
1627
                                $speedinfo = $speed.$unit.'b/s '.$duplex;
2770 rexy 1628
                            } else {
1629
                                $speedinfo = $speed.$unit.'b/s';
1630
                            }
1631
                        }
2976 rexy 1632
                        if (preg_match('/^'.$ar_buf[1].'\s+Link\sencap:Ethernet\s+HWaddr\s(\S+)/i', $line, $ar_buf2)
1633
                           || preg_match('/^'.$ar_buf[1].'\s+Link\s+encap:UNSPEC\s+HWaddr\s(\S+)-00-00-00-00-00-00-00-00-00-00\s*$/i', $line, $ar_buf2)) {
1634
                            if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) {
1635
                                $macaddr = preg_replace('/:/', '-', strtoupper($ar_buf2[1]));
1636
                                if ($macaddr === '00-00-00-00-00-00') { // empty
1637
                                    $macaddr = "";
1638
                                }
1639
                            }
1640
                        } elseif (preg_match('/^'.$ar_buf[1].':\s+ip\s+(\S+)\s+mask/i', $line, $ar_buf2))
2770 rexy 1641
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
1642
                    }
1643
                } else {
1644
                    if ($was) {
1645
                        if (preg_match('/\sRX bytes:(\d+)\s/i', $line, $ar_buf2)) {
1646
                            $dev->setRxBytes($ar_buf2[1]);
1647
                        }
1648
                        if (preg_match('/\sTX bytes:(\d+)\s/i', $line, $ar_buf2)) {
1649
                            $dev->setTxBytes($ar_buf2[1]);
1650
                        }
325 richard 1651
 
2770 rexy 1652
                        if (preg_match('/\sRX packets:\d+\serrors:(\d+)\sdropped:(\d+)/i', $line, $ar_buf2)) {
1653
                            $errors +=$ar_buf2[1];
1654
                            $drops +=$ar_buf2[2];
1655
                        } elseif (preg_match('/\sTX packets:\d+\serrors:(\d+)\sdropped:(\d+)/i', $line, $ar_buf2)) {
1656
                            $errors +=$ar_buf2[1];
1657
                            $drops +=$ar_buf2[2];
1658
                        }
1659
 
1660
                        if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
1661
                            if (preg_match('/\s+encap:Ethernet\s+HWaddr\s(\S+)/i', $line, $ar_buf2)
2976 rexy 1662
                             || preg_match('/\s+encap:UNSPEC\s+HWaddr\s(\S+)-00-00-00-00-00-00-00-00-00-00\s*$/i', $line, $ar_buf2)
2770 rexy 1663
                             || preg_match('/^\s+ether\s+(\S+)\s+txqueuelen/i', $line, $ar_buf2)) {
2976 rexy 1664
                                if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) {
1665
                                    $macaddr = preg_replace('/:/', '-', strtoupper($ar_buf2[1]));
1666
                                    if ($macaddr === '00-00-00-00-00-00') { // empty
1667
                                        $macaddr = "";
1668
                                    }
1669
                                }
2770 rexy 1670
                            } elseif (preg_match('/^\s+inet\saddr:(\S+)\s+P-t-P:(\S+)/i', $line, $ar_buf2)
1671
                                  || preg_match('/^\s+inet\s+(\S+)\s+netmask.+destination\s+(\S+)/i', $line, $ar_buf2)) {
1672
                                if ($ar_buf2[1] != $ar_buf2[2]) {
1673
                                     $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1].";:".$ar_buf2[2]);
1674
                                } else {
1675
                                     $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
1676
                                }
1677
                            } elseif ((preg_match('/^\s+inet\saddr:(\S+)/i', $line, $ar_buf2)
1678
                                  || preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $line, $ar_buf2)
1679
                                  || preg_match('/^\s+inet6\saddr:\s([^\/\s]+)(.+)\s+Scope:[GH]/i', $line, $ar_buf2)
3100 rexy 1680
                                  || preg_match('/^\s+inet6\s+(\S+)\s+prefixlen(.+)((<global>)|(<host>))/i', $line, $ar_buf2)
1681
                                  || preg_match('/^\s+inet\saddr6:\s+(\S+)\s+prefixlen(.+)/i', $line, $ar_buf2))
2770 rexy 1682
                                  && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1])) {
1683
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
1684
                            }
1685
                        }
1686
                    }
1687
                }
1688
            }
1689
            if ($was) {
1690
                $dev->setErrors($errors);
1691
                $dev->setDrops($drops);
1692
                if ($macaddr != "") {
1693
                    $dev->setInfo($macaddr.($dev->getInfo()?';'.$dev->getInfo():''));
1694
                }
1695
                if ($speedinfo != "") {
1696
                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$speedinfo);
1697
                }
1698
                $this->sys->setNetDevices($dev);
1699
            }
1700
        }
325 richard 1701
    }
1702
 
2770 rexy 1703
    /**
1704
     * Physical memory information and Swap Space information
1705
     *
1706
     * @return void
1707
     */
3100 rexy 1708
    protected function _memory($mbuf = null, $sbuf = null)
2770 rexy 1709
    {
3100 rexy 1710
        if (($mbuf !== null) || CommonFunctions::rfts('/proc/meminfo', $mbuf)) {
3037 rexy 1711
            $swaptotal = null;
1712
            $swapfree = null;
2770 rexy 1713
            $bufe = preg_split("/\n/", $mbuf, -1, PREG_SPLIT_NO_EMPTY);
1714
            foreach ($bufe as $buf) {
1715
                if (preg_match('/^MemTotal:\s+(\d+)\s*kB/i', $buf, $ar_buf)) {
1716
                    $this->sys->setMemTotal($ar_buf[1] * 1024);
1717
                } elseif (preg_match('/^MemFree:\s+(\d+)\s*kB/i', $buf, $ar_buf)) {
1718
                    $this->sys->setMemFree($ar_buf[1] * 1024);
1719
                } elseif (preg_match('/^Cached:\s+(\d+)\s*kB/i', $buf, $ar_buf)) {
1720
                    $this->sys->setMemCache($ar_buf[1] * 1024);
1721
                } elseif (preg_match('/^Buffers:\s+(\d+)\s*kB/i', $buf, $ar_buf)) {
1722
                    $this->sys->setMemBuffer($ar_buf[1] * 1024);
3037 rexy 1723
                } elseif (preg_match('/^SwapTotal:\s+(\d+)\s*kB/i', $buf, $ar_buf)) {
1724
                    $swaptotal = $ar_buf[1] * 1024;
1725
                } elseif (preg_match('/^SwapFree:\s+(\d+)\s*kB/i', $buf, $ar_buf)) {
1726
                    $swapfree = $ar_buf[1] * 1024;
2770 rexy 1727
                }
1728
            }
1729
            $this->sys->setMemUsed($this->sys->getMemTotal() - $this->sys->getMemFree());
1730
            // values for splitting memory usage
3037 rexy 1731
            if (($this->sys->getMemCache() !== null) && ($this->sys->getMemBuffer() !== null)) {
2770 rexy 1732
                $this->sys->setMemApplication($this->sys->getMemUsed() - $this->sys->getMemCache() - $this->sys->getMemBuffer());
1733
            }
3100 rexy 1734
            if (($sbuf !== null) || CommonFunctions::rfts('/proc/swaps', $sbuf, 0, 4096, false)) {
2770 rexy 1735
                $swaps = preg_split("/\n/", $sbuf, -1, PREG_SPLIT_NO_EMPTY);
1736
                unset($swaps[0]);
1737
                foreach ($swaps as $swap) {
1738
                    $ar_buf = preg_split('/\s+/', $swap, 5);
1739
                    $dev = new DiskDevice();
1740
                    $dev->setMountPoint($ar_buf[0]);
1741
                    $dev->setName("SWAP");
1742
                    $dev->setTotal($ar_buf[2] * 1024);
1743
                    $dev->setUsed($ar_buf[3] * 1024);
1744
                    $dev->setFree($dev->getTotal() - $dev->getUsed());
1745
                    $this->sys->setSwapDevices($dev);
1746
                }
3037 rexy 1747
            } elseif (($swaptotal !== null) && ($swapfree !== null) && ($swaptotal > 0)) {
1748
                    $dev = new DiskDevice();
1749
                    $dev->setName("SWAP");
1750
                    $dev->setTotal($swaptotal);
1751
                    $dev->setFree($swapfree);
1752
                    $dev->setUsed($dev->getTotal() - $dev->getFree());
1753
                    $this->sys->setSwapDevices($dev);
2770 rexy 1754
            }
1755
        }
1756
    }
325 richard 1757
 
2770 rexy 1758
    /**
1759
     * filesystem information
1760
     *
1761
     * @return void
1762
     */
3100 rexy 1763
    protected function _filesystems()
2770 rexy 1764
    {
1765
        $df_args = "";
1766
        $hideFstypes = array();
1767
        if (defined('PSI_HIDE_FS_TYPES') && is_string(PSI_HIDE_FS_TYPES)) {
1768
            if (preg_match(ARRAY_EXP, PSI_HIDE_FS_TYPES)) {
1769
                $hideFstypes = eval(PSI_HIDE_FS_TYPES);
1770
            } else {
1771
                $hideFstypes = array(PSI_HIDE_FS_TYPES);
1772
            }
1773
        }
1774
        foreach ($hideFstypes as $Fstype) {
1775
            $df_args .= "-x $Fstype ";
1776
        }
1777
        if ($df_args !== "") {
3037 rexy 1778
            $df_args = trim($df_args); // trim spaces
2770 rexy 1779
            $arrResult = Parser::df("-P $df_args 2>/dev/null");
1780
        } else {
1781
            $arrResult = Parser::df("-P 2>/dev/null");
1782
        }
1783
        foreach ($arrResult as $dev) {
1784
            $this->sys->setDiskDevices($dev);
1785
        }
1786
    }
325 richard 1787
 
2770 rexy 1788
    /**
1789
     * Distribution
1790
     *
1791
     * @return void
1792
     */
1793
    protected function _distro()
1794
    {
1795
        $this->sys->setDistribution("Linux");
1796
        $list = @parse_ini_file(PSI_APP_ROOT."/data/distros.ini", true);
1797
        if (!$list) {
1798
            return;
1799
        }
1800
        // We have the '2>/dev/null' because Ubuntu gives an error on this command which causes the distro to be unknown
3037 rexy 1801
        if (CommonFunctions::executeProgram('lsb_release', '-a 2>/dev/null', $distro_info, PSI_DEBUG) && strlen($distro_info) > 0) {
1802
            $distro_tmp = preg_split("/\r?\n/", $distro_info, -1, PREG_SPLIT_NO_EMPTY);
2770 rexy 1803
            foreach ($distro_tmp as $info) {
1804
                $info_tmp = preg_split('/:/', $info, 2);
3037 rexy 1805
                if (isset($distro_tmp[0]) && ($distro_tmp[0] !== null) && (trim($distro_tmp[0]) != "") &&
1806
                     isset($distro_tmp[1]) && ($distro_tmp[1] !== null) && (trim($distro_tmp[1]) != "")) {
2770 rexy 1807
                    $distro[trim($info_tmp[0])] = trim($info_tmp[1]);
1808
                }
1809
            }
1810
            if (!isset($distro['Distributor ID']) && !isset($distro['Description'])) { // Systems like StartOS
3037 rexy 1811
                if (isset($distro_tmp[0]) && ($distro_tmp[0] !== null) && (trim($distro_tmp[0]) != "")) {
2770 rexy 1812
                    $this->sys->setDistribution(trim($distro_tmp[0]));
1813
                    if (preg_match('/^(\S+)\s*/', $distro_tmp[0], $id_buf)
1814
                        && isset($list[trim($id_buf[1])]['Image'])) {
1815
                            $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
1816
                    }
1817
                }
1818
            } else {
3037 rexy 1819
                if (isset($distro['Description']) && ($distro['Description'] != "n/a") && isset($distro['Distributor ID']) && $distro['Distributor ID']=="Neon") { // Neon systems
1820
                    $distro_tmp = preg_split("/\s/", $distro['Description'], -1, PREG_SPLIT_NO_EMPTY);
1821
                    $distro['Distributor ID'] = $distro_tmp[0];
1822
                }
2770 rexy 1823
                if (isset($distro['Description'])
3037 rexy 1824
                   && preg_match('/^NAME=\s*"?([^"\r\n]+)"?\s*$/', $distro['Description'], $name_tmp)) {
1825
                   $distro['Description'] = trim($name_tmp[1]);
2770 rexy 1826
                }
1827
                if (isset($distro['Description'])
1828
                   && ($distro['Description'] != "n/a")
1829
                   && (!isset($distro['Distributor ID'])
1830
                   || (($distro['Distributor ID'] != "n/a")
1831
                   && ($distro['Description'] != $distro['Distributor ID'])))) {
1832
                    $this->sys->setDistribution($distro['Description']);
1833
                    if (isset($distro['Release']) && ($distro['Release'] != "n/a")
1834
                       && ($distro['Release'] != $distro['Description']) && strstr($distro['Release'], ".")){
1835
                        if (preg_match("/^(\d+)\.[0]+$/", $distro['Release'], $match_buf)) {
1836
                            $tofind = $match_buf[1];
1837
                        } else {
1838
                            $tofind = $distro['Release'];
1839
                        }
1840
                        if (!preg_match("/^".$tofind."[\s\.]|[\(\[]".$tofind."[\.\)\]]|\s".$tofind."$|\s".$tofind."[\s\.]/", $distro['Description'])) {
1841
                            $this->sys->setDistribution($this->sys->getDistribution()." ".$distro['Release']);
1842
                        }
1843
                    }
3037 rexy 1844
                } elseif (isset($distro['Distributor ID'])) {
1845
                    if ($distro['Distributor ID'] != "n/a") {
1846
                        $this->sys->setDistribution($distro['Distributor ID']);
1847
                        if (isset($distro['Release']) && ($distro['Release'] != "n/a")) {
1848
                            $this->sys->setDistribution($this->sys->getDistribution()." ".$distro['Release']);
1849
                        }
1850
                        if (isset($distro['Codename']) && ($distro['Codename'] != "n/a")) {
1851
                            $this->sys->setDistribution($this->sys->getDistribution()." (".$distro['Codename'].")");
1852
                        }
1853
                    } elseif (isset($distro['Description']) && ($distro['Description'] != "n/a")) {
1854
                        $this->sys->setDistribution($distro['Description']);
2770 rexy 1855
                    }
1856
                }
3037 rexy 1857
                if (isset($distro['Distributor ID'])) {
1858
                    $distrib = $distro['Distributor ID'];
1859
                    if (isset($distro['Description'])) {
1860
                        $distarr = preg_split("/\s/", $distro['Description'], -1, PREG_SPLIT_NO_EMPTY);
1861
                        if (isset($distarr[0])) {
1862
                            if ($distrib != "n/a") {
1863
                                $distrib .= ' '.$distarr[0];
1864
                            } else {
1865
                                $distrib = $distarr[0];
1866
                            }
1867
                        }
2770 rexy 1868
                    }
3037 rexy 1869
                    if (isset($list[$distrib]['Image'])) {
1870
                        $this->sys->setDistributionIcon($list[$distrib]['Image']);
1871
                    } elseif (($distro['Distributor ID'] != "n/a") && isset($list[$distro['Distributor ID']]['Image'])) {
1872
                        $this->sys->setDistributionIcon($list[$distro['Distributor ID']]['Image']);
1873
                    }
2770 rexy 1874
                }
1875
            }
1876
        } else {
1877
            /* default error handler */
1878
            if (function_exists('errorHandlerPsi')) {
1879
                restore_error_handler();
1880
            }
1881
            /* fatal errors only */
1882
            $old_err_rep = error_reporting();
1883
            error_reporting(E_ERROR);
325 richard 1884
 
2770 rexy 1885
            // Fall back in case 'lsb_release' does not exist but exist /etc/lsb-release
1886
            if (CommonFunctions::fileexists($filename="/etc/lsb-release")
1887
               && CommonFunctions::rfts($filename, $buf, 0, 4096, false)
3037 rexy 1888
               && preg_match('/^DISTRIB_ID="?([^"\r\n]+)/m', $buf, $id_buf)) {
1889
                if (preg_match('/^DISTRIB_DESCRIPTION="?([^"\r\n]+)/m', $buf, $desc_buf)
2770 rexy 1890
                   && (trim($desc_buf[1])!=trim($id_buf[1]))) {
1891
                    $this->sys->setDistribution(trim($desc_buf[1]));
3037 rexy 1892
                    if (preg_match('/^DISTRIB_RELEASE="?([^"\r\n]+)/m', $buf, $vers_buf)
2770 rexy 1893
                       && (trim($vers_buf[1])!=trim($desc_buf[1])) && strstr($vers_buf[1], ".")){
1894
                        if (preg_match("/^(\d+)\.[0]+$/", trim($vers_buf[1]), $match_buf)) {
1895
                            $tofind = $match_buf[1];
1896
                        } else {
1897
                            $tofind = trim($vers_buf[1]);
1898
                        }
1899
                        if (!preg_match("/^".$tofind."[\s\.]|[\(\[]".$tofind."[\.\)\]]|\s".$tofind."$|\s".$tofind."[\s\.]/", trim($desc_buf[1]))) {
1900
                            $this->sys->setDistribution($this->sys->getDistribution()." ".trim($vers_buf[1]));
1901
                        }
1902
                    }
1903
                } else {
1904
                    if (isset($list[trim($id_buf[1])]['Name'])) {
1905
                        $this->sys->setDistribution(trim($list[trim($id_buf[1])]['Name']));
1906
                    } else {
1907
                        $this->sys->setDistribution(trim($id_buf[1]));
1908
                    }
3037 rexy 1909
                    if (preg_match('/^DISTRIB_RELEASE="?([^"\r\n]+)/m', $buf, $vers_buf)) {
2770 rexy 1910
                        $this->sys->setDistribution($this->sys->getDistribution()." ".trim($vers_buf[1]));
1911
                    }
3037 rexy 1912
                    if (preg_match('/^DISTRIB_CODENAME="?([^"\r\n]+)/m', $buf, $vers_buf)) {
2770 rexy 1913
                        $this->sys->setDistribution($this->sys->getDistribution()." (".trim($vers_buf[1]).")");
1914
                    }
1915
                }
1916
                if (isset($list[trim($id_buf[1])]['Image'])) {
1917
                    $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
1918
                }
1919
            } else { // otherwise find files specific for distribution
1920
                foreach ($list as $section=>$distribution) {
1921
                    if (!isset($distribution['Files'])) {
1922
                        continue;
1923
                    } else {
1924
                        foreach (preg_split("/;/", $distribution['Files'], -1, PREG_SPLIT_NO_EMPTY) as $filename) {
1925
                            if (CommonFunctions::fileexists($filename)) {
1926
                                $distro = $distribution;
1927
                                if (isset($distribution['Mode'])&&(strtolower($distribution['Mode'])=="detection")) {
1928
                                    $buf = "";
1929
                                } elseif (isset($distribution['Mode'])&&(strtolower($distribution['Mode'])=="execute")) {
1930
                                    if (!CommonFunctions::executeProgram($filename, '2>/dev/null', $buf, PSI_DEBUG)) {
1931
                                        $buf = "";
1932
                                    }
1933
                                } else {
1934
                                    if (!CommonFunctions::rfts($filename, $buf, 1, 4096, false)) {
1935
                                        $buf = "";
1936
                                    } elseif (isset($distribution['Mode'])&&(strtolower($distribution['Mode'])=="analyse")) {
1937
                                        if (preg_match('/^(\S+)\s*/', preg_replace('/^Red\s+/', 'Red', $buf), $id_buf)
1938
                                           && isset($list[trim($id_buf[1])]['Image'])) {
1939
                                            $distro = $list[trim($id_buf[1])];
1940
                                        }
1941
                                    }
1942
                                }
1943
                                if (isset($distro['Image'])) {
1944
                                    $this->sys->setDistributionIcon($distro['Image']);
1945
                                }
1946
                                if (isset($distribution['Name'])) {
3037 rexy 1947
                                    if (($buf === null) || (trim($buf) == "")) {
2770 rexy 1948
                                        $this->sys->setDistribution($distribution['Name']);
1949
                                    } else {
1950
                                        $this->sys->setDistribution($distribution['Name']." ".trim($buf));
1951
                                    }
1952
                                } else {
3037 rexy 1953
                                    if (($buf === null) || (trim($buf) == "")) {
2770 rexy 1954
                                        $this->sys->setDistribution($section);
1955
                                    } else {
1956
                                        $this->sys->setDistribution(trim($buf));
1957
                                    }
1958
                                }
1959
                                if (isset($distribution['Files2'])) {
1960
                                    foreach (preg_split("/;/", $distribution['Files2'], -1, PREG_SPLIT_NO_EMPTY) as $filename2) {
1961
                                        if (CommonFunctions::fileexists($filename2) && CommonFunctions::rfts($filename2, $buf, 0, 4096, false)) {
3037 rexy 1962
                                            if (preg_match('/^majorversion="?([^"\r\n]+)/m', $buf, $maj_buf)
1963
                                               && preg_match('/^minorversion="?([^"\r\n]+)/m', $buf, $min_buf)) {
2770 rexy 1964
                                                $distr2=$maj_buf[1].'.'.$min_buf[1];
3037 rexy 1965
                                                if (preg_match('/^buildphase="?([^"\r\n]+)/m', $buf, $pha_buf) && ($pha_buf[1]!=="0")) {
2770 rexy 1966
                                                    $distr2.='.'.$pha_buf[1];
1967
                                                }
3037 rexy 1968
                                                if (preg_match('/^buildnumber="?([^"\r\n]+)/m', $buf, $num_buf)) {
2770 rexy 1969
                                                    $distr2.='-'.$num_buf[1];
1970
                                                }
3037 rexy 1971
                                                if (preg_match('/^builddate="?([^"\r\n]+)/m', $buf, $dat_buf)) {
2770 rexy 1972
                                                    $distr2.=' ('.$dat_buf[1].')';
1973
                                                }
1974
                                                $this->sys->setDistribution($this->sys->getDistribution()." ".$distr2);
1975
                                            } else {
1976
                                                $distr2=trim(substr($buf, 0, strpos($buf, "\n")));
3037 rexy 1977
                                                if (($distr2 !== null) && ($distr2 != "")) {
2770 rexy 1978
                                                    $this->sys->setDistribution($this->sys->getDistribution()." ".$distr2);
1979
                                                }
1980
                                            }
1981
                                            break;
1982
                                        }
1983
                                    }
1984
                                }
1985
                                break 2;
1986
                            }
1987
                        }
1988
                    }
1989
                }
1990
            }
1991
            // if the distribution is still unknown
1992
            if ($this->sys->getDistribution() == "Linux") {
1993
                if (CommonFunctions::fileexists($filename="/etc/DISTRO_SPECS")
1994
                   && CommonFunctions::rfts($filename, $buf, 0, 4096, false)
1995
                   && preg_match('/^DISTRO_NAME=\'(.+)\'/m', $buf, $id_buf)) {
1996
                    if (isset($list[trim($id_buf[1])]['Name'])) {
1997
                        $dist = trim($list[trim($id_buf[1])]['Name']);
1998
                    } else {
1999
                        $dist = trim($id_buf[1]);
2000
                    }
2001
                    if (preg_match('/^DISTRO_VERSION=([^#\n\r]+)/m', $buf, $vers_buf)) {
2002
                        $this->sys->setDistribution(trim($dist." ".trim($vers_buf[1])));
2003
                    } else {
2004
                        $this->sys->setDistribution($dist);
2005
                    }
2006
                    if (isset($list[trim($id_buf[1])]['Image'])) {
2007
                        $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
2008
                    } else {
2009
                        if (isset($list['Puppy']['Image'])) {
2010
                            $this->sys->setDistributionIcon($list['Puppy']['Image']);
2011
                        }
2012
                    }
2013
                } elseif ((CommonFunctions::fileexists($filename="/etc/distro-release")
2014
                        && CommonFunctions::rfts($filename, $buf, 1, 4096, false)
3037 rexy 2015
                        && ($buf !== null) && (trim($buf) != ""))
2770 rexy 2016
                    || (CommonFunctions::fileexists($filename="/etc/system-release")
2017
                        && CommonFunctions::rfts($filename, $buf, 1, 4096, false)
3037 rexy 2018
                        && ($buf !== null) && (trim($buf) != ""))) {
2770 rexy 2019
                    $this->sys->setDistribution(trim($buf));
2020
                    if (preg_match('/^(\S+)\s*/', preg_replace('/^Red\s+/', 'Red', $buf), $id_buf)
2021
                        && isset($list[trim($id_buf[1])]['Image'])) {
2022
                            $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
2023
                    }
2024
                } elseif (CommonFunctions::fileexists($filename="/etc/solydxk/info")
2025
                   && CommonFunctions::rfts($filename, $buf, 0, 4096, false)
3037 rexy 2026
                   && preg_match('/^DISTRIB_ID="?([^"\r\n]+)/m', $buf, $id_buf)) {
2027
                    if (preg_match('/^DESCRIPTION="?([^"\r\n]+)/m', $buf, $desc_buf)
2770 rexy 2028
                       && (trim($desc_buf[1])!=trim($id_buf[1]))) {
2029
                        $this->sys->setDistribution(trim($desc_buf[1]));
2030
                    } else {
2031
                        if (isset($list[trim($id_buf[1])]['Name'])) {
2032
                            $this->sys->setDistribution(trim($list[trim($id_buf[1])]['Name']));
2033
                        } else {
2034
                            $this->sys->setDistribution(trim($id_buf[1]));
2035
                        }
3037 rexy 2036
                        if (preg_match('/^RELEASE="?([^"\r\n]+)/m', $buf, $vers_buf)) {
2770 rexy 2037
                            $this->sys->setDistribution($this->sys->getDistribution()." ".trim($vers_buf[1]));
2038
                        }
3037 rexy 2039
                        if (preg_match('/^CODENAME="?([^"\r\n]+)/m', $buf, $vers_buf)) {
2770 rexy 2040
                            $this->sys->setDistribution($this->sys->getDistribution()." (".trim($vers_buf[1]).")");
2041
                        }
2042
                    }
2043
                    if (isset($list[trim($id_buf[1])]['Image'])) {
2044
                        $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
2045
                    } else {
2046
                        $this->sys->setDistributionIcon($list['SolydXK']['Image']);
2047
                    }
2048
                } elseif (CommonFunctions::fileexists($filename="/etc/os-release")
2049
                   && CommonFunctions::rfts($filename, $buf, 0, 4096, false)
3037 rexy 2050
                   && (preg_match('/^TAILS_VERSION_ID="?([^"\r\n]+)/m', $buf, $tid_buf)
2051
                   || preg_match('/^NAME=["\']?([^"\'\r\n]+)/m', $buf, $id_buf))) {
2052
                    if (preg_match('/^TAILS_VERSION_ID="?([^"\r\n]+)/m', $buf, $tid_buf)) {
2053
                        if (preg_match('/^TAILS_PRODUCT_NAME="?([^"\r\n]+)/m', $buf, $desc_buf)) {
2770 rexy 2054
                            $this->sys->setDistribution(trim($desc_buf[1])." ".trim($tid_buf[1]));
2055
                        } else {
2056
                            if (isset($list['Tails']['Name'])) {
2057
                                $this->sys->setDistribution(trim($list['Tails']['Name'])." ".trim($tid_buf[1]));
2058
                            } else {
2059
                                $this->sys->setDistribution('Tails'." ".trim($tid_buf[1]));
2060
                            }
2061
                        }
2062
                        $this->sys->setDistributionIcon($list['Tails']['Image']);
2063
                    } else {
3037 rexy 2064
                        if (preg_match('/^PRETTY_NAME=["\']?([^"\'\r\n]+)/m', $buf, $desc_buf)
2065
                           && !preg_match('/\$/', $desc_buf[1])) { // if is not defined by variable
2770 rexy 2066
                            $this->sys->setDistribution(trim($desc_buf[1]));
2067
                        } else {
2068
                            if (isset($list[trim($id_buf[1])]['Name'])) {
2069
                                $this->sys->setDistribution(trim($list[trim($id_buf[1])]['Name']));
2070
                            } else {
2071
                                $this->sys->setDistribution(trim($id_buf[1]));
2072
                            }
3037 rexy 2073
                            if (preg_match('/^VERSION=["\']?([^"\'\r\n]+)/m', $buf, $vers_buf)) {
2770 rexy 2074
                                $this->sys->setDistribution($this->sys->getDistribution()." ".trim($vers_buf[1]));
3037 rexy 2075
                            } elseif (preg_match('/^VERSION_ID=["\']?([^"\'\r\n]+)/m', $buf, $vers_buf)) {
2770 rexy 2076
                                $this->sys->setDistribution($this->sys->getDistribution()." ".trim($vers_buf[1]));
2077
                            }
2078
                        }
2079
                        if (isset($list[trim($id_buf[1])]['Image'])) {
2080
                            $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
2081
                        }
2082
                    }
2083
                } elseif (CommonFunctions::fileexists($filename="/etc/debian_version")) {
2084
                    if (!CommonFunctions::rfts($filename, $buf, 1, 4096, false)) {
2085
                        $buf = "";
2086
                    }
2087
                    if (isset($list['Debian']['Image'])) {
2088
                        $this->sys->setDistributionIcon($list['Debian']['Image']);
2089
                    }
2090
                    if (isset($list['Debian']['Name'])) {
3037 rexy 2091
                        if (($buf === null) || (trim($buf) == "")) {
2770 rexy 2092
                            $this->sys->setDistribution($list['Debian']['Name']);
2093
                        } else {
2094
                            $this->sys->setDistribution($list['Debian']['Name']." ".trim($buf));
2095
                        }
2096
                    } else {
3037 rexy 2097
                        if (($buf === null) || (trim($buf) == "")) {
2770 rexy 2098
                            $this->sys->setDistribution('Debian');
2099
                        } else {
2100
                            $this->sys->setDistribution(trim($buf));
2101
                        }
2102
                    }
3100 rexy 2103
                } elseif (CommonFunctions::fileexists($filename="/etc/slackware-version")) {
2104
                    if (!CommonFunctions::rfts($filename, $buf, 1, 4096, false)) {
2105
                        $buf = "";
2106
                    }
2107
                    if (isset($list['Slackware']['Image'])) {
2108
                        $this->sys->setDistributionIcon($list['Slackware']['Image']);
2109
                    }
2110
                    if (isset($list['Slackware']['Name'])) {
2111
                        if (($buf === null) || (trim($buf) == "")) {
2112
                            $this->sys->setDistribution($list['Slackware']['Name']);
2113
                        } else {
2114
                            $this->sys->setDistribution($list['Slackware']['Name']." ".trim($buf));
2115
                        }
2116
                    } else {
2117
                        if (($buf === null) || (trim($buf) == "")) {
2118
                            $this->sys->setDistribution('Slackware');
2119
                        } else {
2120
                            $this->sys->setDistribution(trim($buf));
2121
                        }
2122
                    }
2770 rexy 2123
                } elseif (CommonFunctions::fileexists($filename="/etc/config/uLinux.conf")
2124
                   && CommonFunctions::rfts($filename, $buf, 0, 4096, false)
2125
                   && preg_match("/^Rsync\sModel\s*=\s*QNAP/m", $buf)
2126
                   && preg_match("/^Version\s*=\s*([\d\.]+)\r?\nBuild\sNumber\s*=\s*(\S+)/m", $buf, $ver_buf)) {
2127
                    $buf = $ver_buf[1]."-".$ver_buf[2];
2128
                    if (isset($list['QTS']['Image'])) {
2129
                        $this->sys->setDistributionIcon($list['QTS']['Image']);
2130
                    }
2131
                    if (isset($list['QTS']['Name'])) {
2132
                        $this->sys->setDistribution($list['QTS']['Name']." ".trim($buf));
2133
                    } else {
2134
                        $this->sys->setDistribution(trim($buf));
2135
                    }
2136
                }
2137
            }
2138
            /* restore error level */
2139
            error_reporting($old_err_rep);
2140
            /* restore error handler */
2141
            if (function_exists('errorHandlerPsi')) {
2142
                set_error_handler('errorHandlerPsi');
2143
            }
2144
        }
2145
    }
2146
 
2147
    /**
2148
     * Processes
2149
     *
2150
     * @return void
2151
     */
2152
    protected function _processes()
2153
    {
3037 rexy 2154
        $process = CommonFunctions::findglob('/proc/*/status', GLOB_NOSORT);
2770 rexy 2155
        if (is_array($process) && (($total = count($process)) > 0)) {
2156
            $processes['*'] = 0;
2157
            $buf = "";
2158
            for ($i = 0; $i < $total; $i++) {
2159
                if (CommonFunctions::rfts($process[$i], $buf, 0, 4096, false)) {
3037 rexy 2160
                    $processes['*']++; // current total
2770 rexy 2161
                    if (preg_match('/^State:\s+(\w)/m', $buf, $state)) {
2162
                        if (isset($processes[$state[1]])) {
2163
                            $processes[$state[1]]++;
2164
                        } else {
2165
                            $processes[$state[1]] = 1;
2166
                        }
2167
                    }
2168
                }
2169
            }
2170
            if (!($processes['*'] > 0)) {
3037 rexy 2171
                $processes['*'] = $processes[' '] = $total; // all unknown
2770 rexy 2172
            }
2173
            $this->sys->setProcesses($processes);
2174
        }
2175
    }
2176
 
2177
    /**
2178
     * get the information
2179
     *
2180
     * @see PSI_Interface_OS::build()
2181
     *
3037 rexy 2182
     * @return void
2770 rexy 2183
     */
2184
    public function build()
2185
    {
2186
        if (!$this->blockname || $this->blockname==='vitals') {
2187
            $this->_distro();
2188
            $this->_hostname();
2189
            $this->_kernel();
2190
            $this->_uptime();
2191
            $this->_users();
2192
            $this->_loadavg();
2193
            $this->_processes();
2194
        }
2195
        if (!$this->blockname || $this->blockname==='hardware') {
2196
            $this->_machine();
2197
            $this->_cpuinfo();
3037 rexy 2198
            $this->_virtualizer();
2770 rexy 2199
            $this->_pci();
2200
            $this->_ide();
2201
            $this->_scsi();
2202
            $this->_nvme();
2203
            $this->_usb();
2204
            $this->_i2c();
2205
        }
2206
        if (!$this->blockname || $this->blockname==='memory') {
2207
            $this->_memory();
2208
        }
2209
        if (!$this->blockname || $this->blockname==='filesystem') {
2210
            $this->_filesystems();
2211
        }
3037 rexy 2212
        if (!$this->blockname || $this->blockname==='network') {
2213
            $this->_network();
2214
        }
2770 rexy 2215
    }
2216
}