Completed
Pull Request — master (#123)
by Sascha
26:40 queued 15:43
created

HealthCheckCommand::execute()   D

Complexity

Conditions 9
Paths 29

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
c 8
b 0
f 1
dl 0
loc 42
rs 4.9091
cc 9
eloc 25
nc 29
nop 2
1
<?php
2
3
namespace Liip\MonitorBundle\Command;
4
5
use Liip\MonitorBundle\Helper\RunnerManager;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
11
12
class HealthCheckCommand extends ContainerAwareCommand
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('monitor:health')
18
            ->setDescription('Runs Health Checks')
19
            ->setDefinition(array(
20
                new InputArgument(
21
                    'checkName',
22
                    InputArgument::OPTIONAL,
23
                    'The name of the service to be used to perform the health check.'
24
                ),
25
                new InputOption(
26
                    'reporter',
27
                    null,
28
                    InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
29
                    'Additional reporters to run.',
30
                    array()
31
                ),
32
                new InputOption(
33
                    'nagios',
34
                    null,
35
                    InputOption::VALUE_NONE,
36
                    'Suitable for using as a nagios NRPE command.'
37
                ),
38
                new InputOption('group', 'g', InputOption::VALUE_REQUIRED, 'List checks for given group'),
39
            ));
40
    }
41
42
    /**
43
     * @param InputInterface  $input
44
     * @param OutputInterface $output
45
     *
46
     * @return int
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $checkName = $input->getArgument('checkName');
51
        $group = $input->getOption('group');
52
53
        /** @var RunnerManager $runnerManager */
54
        $runnerManager = $this->getContainer()->get('liip_monitor.helper.runner_manager');
55
        $runner = $runnerManager->getRunner($group);
56
57
        if (null === $runner) {
58
            $output->writeln('<error>No such group.</error>');
59
60
            return 1;
61
        }
62
63
        if ($input->getOption('nagios')) {
64
            $reporter = $this->getContainer()->get('liip_monitor.helper.raw_console_reporter');
65
        } else {
66
            $reporter = $this->getContainer()->get('liip_monitor.helper.console_reporter');
67
        }
68
69
        $runner->addReporter($reporter);
70
        $runner->useAdditionalReporters($input->getOption('reporter'));
71
72
        if (0 === count($runner->getChecks())) {
73
            $output->writeln('<error>No checks configured.</error>');
74
        }
75
76
        /** @var \ZendDiagnostics\Result\Collection $results */
77
        $results = $runner->run($checkName);
78
        if ($input->getOption('nagios')) {
79
            if ($results->getUnknownCount()) {
80
                return 3;
81
            } elseif ($results->getFailureCount()) {
82
                return 2;
83
            } elseif ($results->getWarningCount()) {
84
                return 1;
85
            }
86
        }
87
88
        return $results->getFailureCount() ? 1 : 0;
89
    }
90
}
91