Completed
Pull Request — master (#115)
by Sascha
02:17
created

HealthCheckCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 10
Bugs 3 Features 1
Metric Value
wmc 11
lcom 1
cbo 8
dl 0
loc 99
rs 10
c 10
b 3
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B configure() 0 33 1
D execute() 0 41 9
1
<?php
2
3
namespace Liip\MonitorBundle\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
11
class HealthCheckCommand extends ContainerAwareCommand
12
{
13
    /**
14
     * @var string
15
     */
16
    private $defaultGroup;
17
18
    /**
19
     * @param string $defaultGroup
20
     * @param null   $name
21
     */
22
    public function __construct($defaultGroup, $name = null)
23
    {
24
        $this->defaultGroup = $defaultGroup;
25
        parent::__construct($name);
26
    }
27
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('monitor:health')
32
            ->setDescription('Runs Health Checks')
33
            ->setDefinition(array(
34
                new InputArgument(
35
                    'checkName',
36
                    InputArgument::OPTIONAL,
37
                    'The name of the service to be used to perform the health check.'
38
                ),
39
                new InputOption(
40
                    'reporter',
41
                    null,
42
                    InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
43
                    'Additional reporters to run.',
44
                    array()
45
                ),
46
                new InputOption(
47
                    'nagios',
48
                    null,
49
                    InputOption::VALUE_NONE,
50
                    'Suitable for using as a nagios NRPE command.'
51
                ),
52
                new InputOption(
53
                    'group',
54
                    'g',
55
                    InputOption::VALUE_REQUIRED,
56
                    'List checks for given group',
57
                    $this->defaultGroup
58
                ),
59
            ));
60
    }
61
62
    /**
63
     * @param InputInterface  $input
64
     * @param OutputInterface $output
65
     *
66
     * @return int
67
     */
68
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70
        $checkName = $input->getArgument('checkName');
71
        $group = $input->getOption('group');
72
        $runnerServiceId = 'liip_monitor.runner_'.$group;
73
74
        if (!$this->getContainer()->has($runnerServiceId)) {
75
            $output->writeln('<error>No such group.</error>');
76
77
            return 1;
78
        }
79
80
        $runner = $this->getContainer()->get('liip_monitor.runner_'.$group);
81
82
        if ($input->getOption('nagios')) {
83
            $reporter = $this->getContainer()->get('liip_monitor.helper.raw_console_reporter');
84
        } else {
85
            $reporter = $this->getContainer()->get('liip_monitor.helper.console_reporter');
86
        }
87
88
        $runner->addReporter($reporter);
89
        $runner->useAdditionalReporters($input->getOption('reporter'));
90
91
        if (0 === count($runner->getChecks())) {
92
            $output->writeln('<error>No checks configured.</error>');
93
        }
94
95
        /** @var \ZendDiagnostics\Result\Collection $results */
96
        $results = $runner->run($checkName);
97
        if ($input->getOption('nagios')) {
98
            if ($results->getUnknownCount()) {
99
                return 3;
100
            } elseif ($results->getFailureCount()) {
101
                return 2;
102
            } elseif ($results->getWarningCount()) {
103
                return 1;
104
            }
105
        }
106
107
        return $results->getFailureCount() ? 1 : 0;
108
    }
109
}
110