Passed
Push — master ( 850997...a2456a )
by Maurício
08:55
created

SunOs::isSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Server\SysInfo;
6
7
use function explode;
8
use function is_readable;
9
use function shell_exec;
10
use function trim;
11
12
/**
13
 * SunOS based SysInfo class
14
 */
15
class SunOs extends Base
16
{
17
    /**
18
     * The OS name
19
     *
20
     * @var string
21
     */
22
    public $os = 'SunOS';
23
24
    /**
25
     * Read value from kstat
26
     *
27
     * @param string $key Key to read
28
     *
29
     * @return string with value
30
     */
31
    private function kstat($key)
32
    {
33
        /** @psalm-suppress ForbiddenCode */
34
        $m = shell_exec('kstat -p d ' . $key);
35
36
        if ($m) {
37
            [, $value] = explode("\t", trim($m), 2);
38
39
            return $value;
40
        }
41
42
        return '';
43
    }
44
45
    /**
46
     * Gets load information
47
     *
48
     * @return array with load data
49
     */
50
    public function loadavg()
51
    {
52
        $load1 = $this->kstat('unix:0:system_misc:avenrun_1min');
53
54
        return ['loadavg' => $load1];
55
    }
56
57
    /**
58
     * Checks whether class is supported in this environment
59
     */
60
    public static function isSupported(): bool
61
    {
62
        return @is_readable('/proc/meminfo');
63
    }
64
65
    /**
66
     * Gets information about memory usage
67
     *
68
     * @return array with memory usage data
69
     */
70
    public function memory()
71
    {
72
        $pagesize = (int) $this->kstat('unix:0:seg_cache:slab_size');
73
        $mem = [];
74
        $mem['MemTotal'] = (int) $this->kstat('unix:0:system_pages:pagestotal') * $pagesize;
75
        $mem['MemUsed'] = (int) $this->kstat('unix:0:system_pages:pageslocked') * $pagesize;
76
        $mem['MemFree'] = (int) $this->kstat('unix:0:system_pages:pagesfree') * $pagesize;
77
        $mem['SwapTotal'] = (int) $this->kstat('unix:0:vminfo:swap_avail') / 1024;
78
        $mem['SwapUsed'] = (int) $this->kstat('unix:0:vminfo:swap_alloc') / 1024;
79
        $mem['SwapFree'] = (int) $this->kstat('unix:0:vminfo:swap_free') / 1024;
80
81
        return $mem;
82
    }
83
}
84