|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Server\Status\Server\Disk; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Server\Status\{ |
|
7
|
|
|
Server\Disk, |
|
8
|
|
|
Server\Disk\Volume\MountPoint, |
|
9
|
|
|
Server\Disk\Volume\Usage, |
|
10
|
|
|
Server\Memory\Bytes, |
|
11
|
|
|
Exception\DiskUsageNotAccessible, |
|
12
|
|
|
}; |
|
13
|
|
|
use Innmind\Immutable\{ |
|
14
|
|
|
Str, |
|
15
|
|
|
Sequence, |
|
16
|
|
|
Map, |
|
17
|
|
|
}; |
|
18
|
|
|
use Symfony\Component\Process\Process; |
|
19
|
|
|
|
|
20
|
|
|
final class UnixDisk implements Disk |
|
21
|
|
|
{ |
|
22
|
|
|
private static array $columns = [ |
|
23
|
|
|
'Size' => 'size', |
|
24
|
|
|
'Used' => 'used', |
|
25
|
|
|
'Avail' => 'available', |
|
26
|
|
|
'Use%' => 'usage', |
|
27
|
|
|
'Capacity' => 'usage', |
|
28
|
|
|
'Mounted' => 'mountPoint', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function volumes(): Map |
|
35
|
|
|
{ |
|
36
|
6 |
|
return $this->parse( |
|
37
|
|
|
$this->run('df -lh'), |
|
38
|
6 |
|
); |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function get(MountPoint $point): Volume |
|
42
|
|
|
{ |
|
43
|
3 |
|
return $this |
|
44
|
|
|
->volumes() |
|
45
|
|
|
->get($point->toString()); |
|
46
|
3 |
|
} |
|
47
|
3 |
|
|
|
48
|
|
|
private function run(string $command): Str |
|
49
|
|
|
{ |
|
50
|
6 |
|
$process = Process::fromShellCommandline($command); |
|
51
|
|
|
$process->run(); |
|
52
|
6 |
|
|
|
53
|
6 |
|
if (!$process->isSuccessful()) { |
|
54
|
|
|
throw new DiskUsageNotAccessible; |
|
55
|
6 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
return Str::of($process->getOutput()); |
|
58
|
|
|
} |
|
59
|
6 |
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return Map<string, Volume> |
|
62
|
|
|
*/ |
|
63
|
|
|
private function parse(Str $output): Map |
|
64
|
|
|
{ |
|
65
|
6 |
|
$lines = $output |
|
66
|
|
|
->trim() |
|
67
|
|
|
->split("\n"); |
|
68
|
6 |
|
$columns = $lines |
|
69
|
6 |
|
->first() |
|
70
|
|
|
->pregSplit('~ +~') |
|
71
|
6 |
|
->reduce( |
|
72
|
6 |
|
Sequence::strings(), |
|
73
|
6 |
|
static function(Sequence $columns, Str $column): Sequence { |
|
74
|
6 |
|
$column = $column->toString(); |
|
75
|
|
|
|
|
76
|
6 |
|
return ($columns)(self::$columns[$column] ?? $column); |
|
77
|
|
|
}, |
|
78
|
6 |
|
); |
|
79
|
6 |
|
|
|
80
|
|
|
/** @var Sequence<Sequence<Str>> */ |
|
81
|
|
|
$partsByLine = $lines |
|
82
|
|
|
->drop(1) |
|
83
|
6 |
|
->reduce( |
|
84
|
6 |
|
Sequence::of(Sequence::class), |
|
85
|
6 |
|
static function(Sequence $lines, Str $line) use ($columns): Sequence { |
|
86
|
|
|
return ($lines)( |
|
87
|
6 |
|
$line->pregSplit('~ +~', $columns->size()), |
|
88
|
6 |
|
); |
|
89
|
|
|
}, |
|
90
|
6 |
|
); |
|
91
|
|
|
$volumes = $partsByLine->mapTo( |
|
92
|
|
|
Volume::class, |
|
93
|
6 |
|
function(Sequence $parts) use ($columns): Volume { |
|
94
|
6 |
|
return new Volume( |
|
95
|
6 |
|
new MountPoint( |
|
96
|
|
|
$parts->get($columns->indexOf('mountPoint'))->toString(), |
|
97
|
6 |
|
), |
|
98
|
6 |
|
Bytes::of( |
|
99
|
|
|
$parts->get($columns->indexOf('size'))->toString(), |
|
100
|
6 |
|
), |
|
101
|
6 |
|
Bytes::of( |
|
102
|
|
|
$parts->get($columns->indexOf('available'))->toString(), |
|
103
|
6 |
|
), |
|
104
|
6 |
|
Bytes::of( |
|
105
|
|
|
$parts->get($columns->indexOf('used'))->toString(), |
|
106
|
6 |
|
), |
|
107
|
6 |
|
new Usage( |
|
108
|
|
|
(float) $parts->get($columns->indexOf('usage'))->toString(), |
|
109
|
|
|
), |
|
110
|
6 |
|
); |
|
111
|
6 |
|
}, |
|
112
|
6 |
|
); |
|
113
|
|
|
|
|
114
|
6 |
|
/** @var Map<string, Volume> */ |
|
115
|
6 |
|
return $volumes->reduce( |
|
116
|
4 |
|
Map::of('string', Volume::class), |
|
117
|
|
|
static function(Map $volumes, Volume $volume): Map { |
|
118
|
6 |
|
return ($volumes)( |
|
119
|
|
|
$volume->mountPoint()->toString(), |
|
120
|
|
|
$volume, |
|
121
|
|
|
); |
|
122
|
|
|
}, |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|