Subversion Repositories ALCASAR

Rev

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