|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Petrica |
|
5
|
|
|
* Date: 3/22/2016 |
|
6
|
|
|
* Time: 22:45 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Petrica\StatsdSystem\Command; |
|
9
|
|
|
|
|
10
|
|
|
use Domnikl\Statsd\Client; |
|
11
|
|
|
use Domnikl\Statsd\Connection\UdpSocket; |
|
12
|
|
|
use Petrica\StatsdSystem\Gauge\CpuAverageGauge; |
|
13
|
|
|
use Petrica\StatsdSystem\Gauge\GaugeInterface; |
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
class NotifyCommand extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Client |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $statsd = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Instantiated gauges |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $gauges = null; |
|
33
|
|
|
|
|
34
|
|
|
protected function configure() |
|
35
|
|
|
{ |
|
36
|
|
|
$this |
|
37
|
|
|
->setName('statsd:notify') |
|
38
|
|
|
->setDescription('Collect and send defined gauges to statsd server') |
|
39
|
|
|
->addArgument( |
|
40
|
|
|
'gauges-class', |
|
41
|
|
|
InputArgument::REQUIRED, |
|
42
|
|
|
'PHP class name of gauges separate by comma eg: Petrica\StatsdSystem\Gauge\CpuAverageGauge,Petrica\StatsdSystem\Gauge\MemoryGauge' |
|
43
|
|
|
) |
|
44
|
|
|
->addOption( |
|
45
|
|
|
'statsd-host', |
|
46
|
|
|
null, |
|
47
|
|
|
InputOption::VALUE_OPTIONAL, |
|
48
|
|
|
'Statsd server hostname', |
|
49
|
|
|
'localhost' |
|
50
|
|
|
) |
|
51
|
|
|
->addOption( |
|
52
|
|
|
'statsd-port', |
|
53
|
|
|
null, |
|
54
|
|
|
InputOption::VALUE_OPTIONAL, |
|
55
|
|
|
'Statsd server port', |
|
56
|
|
|
'8125' |
|
57
|
|
|
) |
|
58
|
|
|
->addOption( |
|
59
|
|
|
'statsd-namespace', |
|
60
|
|
|
null, |
|
61
|
|
|
InputOption::VALUE_OPTIONAL, |
|
62
|
|
|
'Gauge namespace sent to statsd', |
|
63
|
|
|
'system' |
|
64
|
|
|
) |
|
65
|
|
|
->addOption( |
|
66
|
|
|
'iterations', |
|
67
|
|
|
null, |
|
68
|
|
|
InputOption::VALUE_OPTIONAL, |
|
69
|
|
|
'The number of times the job collects stats and sends them to statsd server before exiting.', |
|
70
|
|
|
3600 |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param InputInterface $input |
|
76
|
|
|
* @param OutputInterface $output |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
79
|
|
|
{ |
|
80
|
|
|
$gauges = $this->getGauges($input); |
|
81
|
|
|
|
|
82
|
|
|
$statsd = $this->getStatsd($input); |
|
83
|
|
|
|
|
84
|
|
|
$iterations = $input->getOption('iterations'); |
|
85
|
|
|
$count = 0; |
|
86
|
|
|
while($count < $iterations) |
|
87
|
|
|
{ |
|
88
|
|
|
foreach($gauges as $gauge) { |
|
89
|
|
|
// Sampling period attained for current gauge? |
|
90
|
|
|
if (fmod($count, $gauge->getSamplingPeriod()) == 0) { |
|
91
|
|
|
$value = $gauge->getValue(); |
|
92
|
|
|
if (null !== $value) { |
|
93
|
|
|
$statsd->gauge($gauge->getPath(), $value); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($input->getOption('verbose')) { |
|
97
|
|
|
$output->writeln(sprintf('%s: %s', $gauge->getPath(), $gauge->getValue())); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
sleep(1); |
|
103
|
|
|
$count ++; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Statically cache statsd client and return a statsd client |
|
109
|
|
|
* |
|
110
|
|
|
* @param InputInterface $input |
|
111
|
|
|
* @return Client |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getStatsd(InputInterface $input) |
|
114
|
|
|
{ |
|
115
|
|
|
if (null === $this->statsd) { |
|
116
|
|
|
$connection = new UdpSocket( |
|
117
|
|
|
$input->getOption('statsd-host'), |
|
118
|
|
|
$input->getOption('statsd-port') |
|
119
|
|
|
); |
|
120
|
|
|
$this->statsd = new Client( |
|
121
|
|
|
$connection, |
|
122
|
|
|
$input->getOption('statsd-namespace') |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $this->statsd; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Collect current metrics |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getMetrics() |
|
135
|
|
|
{ |
|
136
|
|
|
$cpuLoadGauge = new CpuAverageGauge(); |
|
137
|
|
|
|
|
138
|
|
|
$stats = array( |
|
139
|
|
|
'cpu.load.average' => $cpuLoadGauge->getGauge() |
|
|
|
|
|
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
return $stats; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Instantiate selected gauges |
|
147
|
|
|
* |
|
148
|
|
|
* @param InputInterface $input |
|
149
|
|
|
* @return GaugeInterface[] array |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getGauges(InputInterface $input) |
|
152
|
|
|
{ |
|
153
|
|
|
if (null === $this->gauges) { |
|
154
|
|
|
$this->gauges = array(); |
|
155
|
|
|
$proprietaryPrefix = 'Petrica\StatsdSystem\Gauge'; |
|
156
|
|
|
$classes = explode(',', $input->getArgument('gauges-class')); |
|
157
|
|
|
|
|
158
|
|
|
foreach ($classes as $class) { |
|
159
|
|
|
$class = trim($class); |
|
160
|
|
|
$proprietaryClass = $proprietaryPrefix . '\\' . $class; |
|
161
|
|
|
|
|
162
|
|
|
if (class_exists($proprietaryClass)) { |
|
163
|
|
|
$class = $proprietaryClass; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if (class_exists($class) && |
|
167
|
|
|
in_array('Petrica\StatsdSystem\Gauge\GaugeInterface', class_implements($class))) { |
|
168
|
|
|
|
|
169
|
|
|
$this->gauges[] = new $class(); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (empty($this->gauges)) { |
|
174
|
|
|
throw new \RuntimeException( |
|
175
|
|
|
sprintf('No gauges found for provided string %s', $input->getArgument('gauges-class')) |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $this->gauges; |
|
181
|
|
|
} |
|
182
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.