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