1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\Server\SysInfo; |
6
|
|
|
|
7
|
|
|
use function array_combine; |
8
|
|
|
use function array_merge; |
9
|
|
|
use function file_get_contents; |
10
|
|
|
use function intval; |
11
|
|
|
use function is_array; |
12
|
|
|
use function is_readable; |
13
|
|
|
use function mb_strpos; |
14
|
|
|
use function mb_substr; |
15
|
|
|
use function preg_match_all; |
16
|
|
|
use function preg_split; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Linux based SysInfo class |
20
|
|
|
*/ |
21
|
|
|
class Linux extends Base |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The OS name |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $os = 'Linux'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets load information |
32
|
|
|
* |
33
|
|
|
* @return array<string, int> with load data |
34
|
|
|
*/ |
35
|
|
|
public function loadavg() |
36
|
|
|
{ |
37
|
|
|
$buf = file_get_contents('/proc/stat'); |
38
|
|
|
if ($buf === false) { |
39
|
|
|
$buf = ''; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$pos = mb_strpos($buf, "\n"); |
43
|
|
|
if ($pos === false) { |
44
|
|
|
$pos = 0; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$nums = preg_split( |
48
|
|
|
'/\s+/', |
49
|
|
|
mb_substr( |
50
|
|
|
$buf, |
51
|
|
|
0, |
52
|
|
|
$pos |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
if (! is_array($nums)) { |
|
|
|
|
57
|
|
|
return ['busy' => 0, 'idle' => 0]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return [ |
61
|
|
|
'busy' => (int) $nums[1] + (int) $nums[2] + (int) $nums[3], |
62
|
|
|
'idle' => (int) $nums[4], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks whether class is supported in this environment |
68
|
|
|
*/ |
69
|
|
|
public static function isSupported(): bool |
70
|
|
|
{ |
71
|
|
|
return @is_readable('/proc/meminfo') && @is_readable('/proc/stat'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Gets information about memory usage |
76
|
|
|
* |
77
|
|
|
* @return array with memory usage data |
78
|
|
|
*/ |
79
|
|
|
public function memory() |
80
|
|
|
{ |
81
|
|
|
$content = @file_get_contents('/proc/meminfo'); |
82
|
|
|
if ($content === false) { |
83
|
|
|
return []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
preg_match_all(SysInfo::MEMORY_REGEXP, $content, $matches); |
87
|
|
|
|
88
|
|
|
/** @var array<string, int>|false $mem */ |
89
|
|
|
$mem = array_combine($matches[1], $matches[2]); |
90
|
|
|
if ($mem === false) { |
91
|
|
|
return []; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$defaults = [ |
95
|
|
|
'MemTotal' => 0, |
96
|
|
|
'MemFree' => 0, |
97
|
|
|
'Cached' => 0, |
98
|
|
|
'Buffers' => 0, |
99
|
|
|
'SwapTotal' => 0, |
100
|
|
|
'SwapFree' => 0, |
101
|
|
|
'SwapCached' => 0, |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
$mem = array_merge($defaults, $mem); |
105
|
|
|
|
106
|
|
|
foreach ($mem as $idx => $value) { |
107
|
|
|
$mem[$idx] = intval($value); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'] - $mem['Cached'] - $mem['Buffers']; |
111
|
|
|
$mem['SwapUsed'] = $mem['SwapTotal'] - $mem['SwapFree'] - $mem['SwapCached']; |
112
|
|
|
|
113
|
|
|
return $mem; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|