Completed
Push — master ( b7faa8...c403ae )
by Petrică
02:58 queued 37s
created

NotifyCommand::getStatsd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
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
     * Instantiate selected gauges
131
     *
132
     * @param InputInterface $input
133
     * @return GaugeInterface[] array
134
     */
135
    public function getGauges(InputInterface $input)
136
    {
137
        if (null === $this->gauges) {
138
            $this->gauges = array();
139
            $proprietaryPrefix = 'Petrica\StatsdSystem\Gauge';
140
            $classes = explode(',', $input->getArgument('gauges-class'));
141
142
            foreach ($classes as $class) {
143
                $class = trim($class);
144
                $proprietaryClass = $proprietaryPrefix . '\\' . $class;
145
146
                if (class_exists($proprietaryClass)) {
147
                    $class = $proprietaryClass;
148
                }
149
150
                if (class_exists($class) &&
151
                    in_array('Petrica\StatsdSystem\Gauge\GaugeInterface', class_implements($class))) {
152
153
                    $this->gauges[] = new $class();
154
                }
155
            }
156
157
            if (empty($this->gauges)) {
158
                throw new \RuntimeException(
159
                    sprintf('No gauges found for provided string %s', $input->getArgument('gauges-class'))
160
                );
161
            }
162
        }
163
164
        return $this->gauges;
165
    }
166
}