/
ProcessesGauge.php
155 lines (129 loc) · 3.39 KB
/
ProcessesGauge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Created by PhpStorm.
* User: Petrica
* Date: 5/28/2016
* Time: 23:53
*/
namespace Petrica\StatsdSystem\Gauge;
use Petrica\StatsdSystem\Collection\ValuesCollection;
use Petrica\StatsdSystem\Model\Process\Process;
use Petrica\StatsdSystem\Model\Process\TopProcessParser;
use Petrica\StatsdSystem\Model\TopCommand;
class ProcessesGauge implements GaugeInterface
{
protected $cpuAbove;
protected $memoryAbove;
/**
* @var TopCommand
*/
protected $command;
/**
* ProcessesGauge constructor.
*
* Track CPU and memory of processes
*
* @param float $cpuAbove
* @param float $memoryAbove
*/
public function __construct($cpuAbove = 5.0, $memoryAbove = 1.0)
{
$this->cpuAbove = $cpuAbove;
$this->memoryAbove = $memoryAbove;
$this->command = new TopCommand();
}
/**
* {@inheritdoc}
*/
public function getSamplingPeriod()
{
return 10;
}
/**
* {@inheritdoc}
*/
public function getCollection()
{
$processes = $this->getProcesses();
$cpu = $this->aggregateCpu($processes);
$cpu = $this->filterAbove($cpu, $this->cpuAbove);
$memory = $this->aggregateMemory($processes);
$memory = $this->filterAbove($memory, $this->memoryAbove);
$collection = new ValuesCollection();
foreach ($cpu as $name => $value) {
$collection->add($name . '.cpu.value', $value);
}
foreach ($memory as $name => $value) {
$collection->add($name . '.memory.value', $value);
}
return $collection;
}
/**
* Return parsed data for processes
*/
protected function getProcesses()
{
$data = $this->getCommand()->run();
$parser = new TopProcessParser($data);
$parser->parse();
return $parser->getProcesses();
}
/**
* Return only those processes above certail value
*
* @param $processes Process[]
* @return array
*/
protected function filterAbove($processes, $gate)
{
return array_filter($processes, function ($item) use ($gate) {
return $item >= $gate;
});
}
/**
* Aggregate CPU for processes with the same name
*
* @param $processes Process[]
*/
protected function aggregateCpu($processes)
{
$cpus = array();
foreach ($processes as $process) {
if (isset($cpus[$process->getName()])) {
$cpus[$process->getName()] += $process->getCpu();
}
else {
$cpus[$process->getName()] = $process->getCpu();
}
}
return $cpus;
}
/**
* Aggregate memory for processes with the same name
*
* @param $processes Process[]
* @return array
*/
protected function aggregateMemory($processes)
{
$memory = array();
foreach ($processes as $process) {
if (isset($memory[$process->getName()])) {
$memory[$process->getName()] += $process->getMemory();
}
else {
$memory[$process->getName()] = $process->getMemory();
}
}
return $memory;
}
/**
* Return command object for top utility
*
* @return TopCommand
*/
protected function getCommand()
{
return $this->command;
}
}