1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Petrica |
5
|
|
|
* Date: 5/28/2016 |
6
|
|
|
* Time: 23:53 |
7
|
|
|
*/ |
8
|
|
|
namespace Petrica\StatsdSystem\Gauge; |
9
|
|
|
|
10
|
|
|
use Petrica\StatsdSystem\Collection\ValuesCollection; |
11
|
|
|
use Petrica\StatsdSystem\Model\Process\Process; |
12
|
|
|
use Petrica\StatsdSystem\Model\Process\TopProcessParser; |
13
|
|
|
use Petrica\StatsdSystem\Model\TopCommand; |
14
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
15
|
|
|
|
16
|
|
|
class ProcessesGauge implements GaugeInterface |
17
|
|
|
{ |
18
|
|
|
protected $cpuAbove; |
19
|
|
|
|
20
|
|
|
protected $memoryAbove; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var TopCommand |
24
|
|
|
*/ |
25
|
|
|
protected $command; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This is the cache namespace |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $namespace; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ProcessesGauge constructor. |
36
|
|
|
* |
37
|
|
|
* Track CPU and memory of processes |
38
|
|
|
* |
39
|
|
|
* @param float $cpuAbove |
40
|
|
|
* @param float $memoryAbove |
41
|
|
|
*/ |
42
|
3 |
|
public function __construct($cpuAbove = 5.0, $memoryAbove = 1.0) |
43
|
|
|
{ |
44
|
3 |
|
$this->cpuAbove = $cpuAbove; |
45
|
3 |
|
$this->memoryAbove = $memoryAbove; |
46
|
|
|
|
47
|
3 |
|
$this->command = new TopCommand(); |
48
|
|
|
|
49
|
3 |
|
$this->namespace = 'statsd.localhost'; |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function getSamplingPeriod() |
56
|
|
|
{ |
57
|
|
|
return 10; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
1 |
|
public function getCollection() |
64
|
|
|
{ |
65
|
1 |
|
$processes = $this->getProcesses(); |
66
|
|
|
|
67
|
1 |
|
$cpu = $this->aggregateCpu($processes); |
68
|
1 |
|
$cpu = $this->filterAbove($cpu, $this->cpuAbove); |
69
|
|
|
|
70
|
1 |
|
$memory = $this->aggregateMemory($processes); |
71
|
1 |
|
$memory = $this->filterAbove($memory, $this->memoryAbove); |
72
|
|
|
|
73
|
|
|
// Get previous values saved |
74
|
1 |
|
$collection = $this->retrieveCollection(); |
75
|
1 |
|
$this->resetValues($collection); |
|
|
|
|
76
|
|
|
|
77
|
1 |
|
foreach ($cpu as $name => $value) { |
78
|
1 |
|
$collection->add($name . '.cpu.value', $value); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
foreach ($memory as $name => $value) { |
82
|
1 |
|
$collection->add($name . '.memory.value', $value); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
// Persist collection to a temporary storage |
86
|
1 |
|
$copyCollection = new ValuesCollection($collection->getValues()); |
87
|
1 |
|
$this->removeEmpty($copyCollection); |
|
|
|
|
88
|
1 |
|
$this->persistCollection($copyCollection); |
89
|
|
|
|
90
|
1 |
|
return $collection; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return parsed data for processes |
95
|
|
|
*/ |
96
|
1 |
|
protected function getProcesses() |
97
|
|
|
{ |
98
|
1 |
|
$data = $this->getCommand()->run(); |
99
|
|
|
|
100
|
1 |
|
$parser = new TopProcessParser($data); |
101
|
1 |
|
$parser->parse(); |
102
|
|
|
|
103
|
1 |
|
return $parser->getProcesses(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return only those processes above certail value |
108
|
|
|
* |
109
|
|
|
* @param $processes Process[] |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
protected function filterAbove($processes, $gate) |
113
|
|
|
{ |
114
|
1 |
|
return array_filter($processes, function ($item) use ($gate) { |
115
|
1 |
|
return $item >= $gate; |
116
|
1 |
|
}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Aggregate CPU for processes with the same name |
121
|
|
|
* |
122
|
|
|
* @param $processes Process[] |
123
|
|
|
*/ |
124
|
1 |
View Code Duplication |
protected function aggregateCpu($processes) |
|
|
|
|
125
|
|
|
{ |
126
|
1 |
|
$cpus = array(); |
127
|
|
|
|
128
|
1 |
|
foreach ($processes as $process) { |
129
|
1 |
|
if (isset($cpus[$process->getName()])) { |
130
|
1 |
|
$cpus[$process->getName()] += $process->getCpu(); |
131
|
1 |
|
} |
132
|
|
|
else { |
133
|
1 |
|
$cpus[$process->getName()] = $process->getCpu(); |
134
|
|
|
} |
135
|
1 |
|
} |
136
|
|
|
|
137
|
1 |
|
return $cpus; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Aggregate memory for processes with the same name |
142
|
|
|
* |
143
|
|
|
* @param $processes Process[] |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
1 |
View Code Duplication |
protected function aggregateMemory($processes) |
|
|
|
|
147
|
|
|
{ |
148
|
1 |
|
$memory = array(); |
149
|
|
|
|
150
|
1 |
|
foreach ($processes as $process) { |
151
|
1 |
|
if (isset($memory[$process->getName()])) { |
152
|
1 |
|
$memory[$process->getName()] += $process->getMemory(); |
153
|
1 |
|
} |
154
|
|
|
else { |
155
|
1 |
|
$memory[$process->getName()] = $process->getMemory(); |
156
|
|
|
} |
157
|
1 |
|
} |
158
|
|
|
|
159
|
1 |
|
return $memory; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return command object for top utility |
164
|
|
|
* |
165
|
|
|
* @return TopCommand |
166
|
|
|
*/ |
167
|
|
|
protected function getCommand() |
168
|
|
|
{ |
169
|
|
|
return $this->command; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Persist collection values to a temporary storage |
174
|
|
|
* key is the command string value |
175
|
|
|
* |
176
|
|
|
* @param $collection |
177
|
|
|
*/ |
178
|
3 |
|
protected function persistCollection($collection) |
179
|
|
|
{ |
180
|
3 |
|
$cache = new FilesystemAdapter($this->namespace); |
181
|
3 |
|
$item = $cache->getItem('collection'); |
182
|
3 |
|
$item->set($collection); |
183
|
3 |
|
$cache->save($item); |
184
|
3 |
|
} |
185
|
|
|
|
186
|
|
|
/**\ |
187
|
|
|
* Retrieve a new collection of a previous collection |
188
|
|
|
* |
189
|
|
|
* @return mixed|ValuesCollection |
190
|
|
|
*/ |
191
|
3 |
|
protected function retrieveCollection() |
192
|
|
|
{ |
193
|
3 |
|
$cache = new FilesystemAdapter($this->namespace); |
194
|
3 |
|
$item = $cache->getItem('collection'); |
195
|
|
|
|
196
|
3 |
|
$collection = null; |
197
|
3 |
|
if ($item->isHit()) { |
198
|
3 |
|
$collection = $item->get('collection'); |
|
|
|
|
199
|
3 |
|
} |
200
|
|
|
|
201
|
3 |
|
if (empty($collection) || !$collection instanceof ValuesCollection) { |
202
|
|
|
$collection = new ValuesCollection(); |
203
|
|
|
} |
204
|
|
|
|
205
|
3 |
|
return $collection; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Reset collection values to 0 |
210
|
|
|
* |
211
|
|
|
* These are in fact previous collection values that needs to be sent to statsd as 0 |
212
|
|
|
* |
213
|
|
|
* @param $collection |
214
|
|
|
*/ |
215
|
1 |
|
protected function resetValues($collection) { |
216
|
1 |
|
foreach ($collection as $key => $value) { |
217
|
1 |
|
$collection[$key] = 0; |
218
|
1 |
|
} |
219
|
|
|
|
220
|
1 |
|
return $collection; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Remove empty values from collection |
225
|
|
|
* |
226
|
|
|
* @param $collection |
227
|
|
|
*/ |
228
|
1 |
|
protected function removeEmpty($collection) { |
229
|
1 |
|
foreach ($collection as $key => $value) { |
230
|
1 |
|
if (empty($value)) { |
231
|
1 |
|
unset($collection[$key]); |
232
|
1 |
|
} |
233
|
1 |
|
} |
234
|
|
|
|
235
|
1 |
|
return $collection; |
236
|
|
|
} |
237
|
|
|
} |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: