1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Server\Status\Server\Processes; |
5
|
|
|
|
6
|
|
|
use Innmind\Server\Status\{ |
7
|
|
|
Server\Processes, |
8
|
|
|
Server\Process, |
9
|
|
|
Server\Process\Pid, |
10
|
|
|
Server\Process\User, |
11
|
|
|
Server\Process\Command, |
12
|
|
|
Server\Process\Memory, |
13
|
|
|
Server\Cpu\Percentage, |
14
|
|
|
Exception\InformationNotAccessible |
15
|
|
|
}; |
16
|
|
|
use Innmind\TimeContinuum\TimeContinuumInterface; |
17
|
|
|
use Innmind\Immutable\{ |
18
|
|
|
MapInterface, |
19
|
|
|
Str, |
20
|
|
|
StreamInterface, |
21
|
|
|
Sequence, |
22
|
|
|
Map |
23
|
|
|
}; |
24
|
|
|
use Symfony\Component\Process\Process as SfProcess; |
25
|
|
|
|
26
|
|
|
final class UnixProcesses implements Processes |
27
|
|
|
{ |
28
|
|
|
private $clock; |
29
|
|
|
|
30
|
24 |
|
public function __construct(TimeContinuumInterface $clock) |
31
|
|
|
{ |
32
|
24 |
|
$this->clock = $clock; |
33
|
24 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function all(): MapInterface |
39
|
|
|
{ |
40
|
|
|
return $this->parse( |
41
|
|
|
$this->run('ps aux') |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function get(Pid $pid): Process |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
2 |
|
$processes = $this->parse( |
49
|
2 |
|
$this->run(sprintf('ps ux -p %s', $pid)) |
50
|
|
|
); |
51
|
2 |
|
} catch (InformationNotAccessible $e) { |
52
|
2 |
|
$processes = $this->parse( |
53
|
2 |
|
$this->run(sprintf('ps ux -q %s', $pid)) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!$processes->contains($pid->toInt())) { |
58
|
|
|
throw new InformationNotAccessible; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $processes->get($pid->toInt()); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
private function run(string $command): Str |
65
|
|
|
{ |
66
|
2 |
|
$process = new SfProcess($command); |
67
|
2 |
|
$process->run(); |
68
|
|
|
|
69
|
2 |
|
if (!$process->isSuccessful()) { |
70
|
2 |
|
throw new InformationNotAccessible; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new Str($process->getOutput()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return MapInterface<int, Process> |
78
|
|
|
*/ |
79
|
|
|
private function parse(Str $output): MapInterface |
80
|
|
|
{ |
81
|
|
|
$lines = $output |
82
|
|
|
->trim() |
83
|
|
|
->split("\n"); |
84
|
|
|
$columns = $lines |
85
|
|
|
->first() |
86
|
|
|
->pregSplit('~ +~') |
87
|
|
|
->reduce( |
88
|
|
|
new Sequence, |
89
|
|
|
static function(Sequence $columns, Str $column): Sequence { |
90
|
|
|
return $columns->add((string) $column); |
91
|
|
|
} |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
return $lines |
95
|
|
|
->drop(1) |
96
|
|
|
->reduce( |
97
|
|
|
new Sequence, |
98
|
|
|
static function(Sequence $lines, Str $line) use ($columns): Sequence { |
99
|
|
|
return $lines->add( |
100
|
|
|
$line->pregSplit('~ +~', $columns->size()) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
) |
104
|
|
|
->map(function(StreamInterface $parts) use ($columns): Process { |
105
|
|
|
return new Process( |
106
|
|
|
new Pid((int) (string) $parts->get($columns->indexOf('PID'))), |
107
|
|
|
new User((string) $parts->get($columns->indexOf('USER'))), |
108
|
|
|
new Percentage((float) (string) $parts->get($columns->indexOf('%CPU'))), |
109
|
|
|
new Memory((float) (string) $parts->get($columns->indexOf('%MEM'))), |
110
|
|
|
$this->clock->at( |
111
|
|
|
(string) $parts->get( |
112
|
|
|
$columns->indexOf(PHP_OS === 'Linux' ? 'START' : 'STARTED') |
113
|
|
|
) |
114
|
|
|
), |
115
|
|
|
new Command((string) $parts->get($columns->indexOf('COMMAND'))) |
116
|
|
|
); |
117
|
|
|
}) |
118
|
|
|
->reduce( |
119
|
|
|
new Map('int', Process::class), |
120
|
|
|
static function(Map $processes, Process $process): Map { |
121
|
|
|
return $processes->put( |
122
|
|
|
$process->pid()->toInt(), |
123
|
|
|
$process |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|