Completed
Push — master ( 269eb1...62642d )
by Lukas Kahwe
01:25
created

HealthCheckCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace Liip\MonitorBundle\Command;
4
5
use Liip\MonitorBundle\Helper\ConsoleReporter;
6
use Liip\MonitorBundle\Helper\RawConsoleReporter;
7
use Liip\MonitorBundle\Helper\RunnerManager;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Input\InputArgument;
13
14
class HealthCheckCommand extends Command
15
{
16
    private $rawReporter;
17
    private $reporter;
18
    private $runnerManager;
19
20
    public function __construct(ConsoleReporter $reporter, RawConsoleReporter $rawReporter, RunnerManager $runnerManager, $name = null)
21
    {
22
        $this->rawReporter = $rawReporter;
23
        $this->reporter = $reporter;
24
        $this->runnerManager = $runnerManager;
25
26
        parent::__construct($name);
27
    }
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setDescription('Runs Health Checks')
33
            ->addArgument(
34
                'checkName',
35
                InputArgument::OPTIONAL,
36
                'The name of the service to be used to perform the health check.'
37
            )
38
            ->addOption(
39
                'reporter',
40
                null,
41
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
42
                'Additional reporters to run.'
43
            )
44
            ->addOption('nagios', null, InputOption::VALUE_NONE, 'Suitable for using as a nagios NRPE command.')
45
            ->addOption(
46
                'group',
47
                'g',
48
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
49
                'Run Health Checks for given group'
50
            )
51
            ->addOption('all', 'a', InputOption::VALUE_NONE, 'Run Health Checks of all groups')
52
        ;
53
    }
54
55
    /**
56
     * @param InputInterface  $input
57
     * @param OutputInterface $output
58
     *
59
     * @return int
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $failureCount = 0;
64
65
        $groups = $input->getOption('group') ?: array(null);
66
        $allGroups = $input->getOption('all');
67
        $checkName = $input->getArgument('checkName');
68
        $nagios = $input->getOption('nagios');
69
        $additionalReporters = $input->getOption('reporter');
70
71
        if ($nagios) {
72
            $reporter = $this->rawReporter;
73
        } else {
74
            $reporter = $this->reporter;
75
        }
76
77
        if ($allGroups) {
78
            $groups = $this->runnerManager->getGroups();
79
        }
80
81
        foreach ($groups as $group) {
82
            if (count($groups) > 1 || $allGroups) {
83
                $output->writeln(sprintf('<fg=yellow;options=bold>%s</>', $group));
84
            }
85
86
            $runner = $this->runnerManager->getRunner($group);
87
88
            if (null === $runner) {
89
                $output->writeln('<error>No such group.</error>');
90
91
                return 1;
92
            }
93
94
            $runner->addReporter($reporter);
95
            $runner->useAdditionalReporters($additionalReporters);
96
97
            if (0 === count($runner->getChecks())) {
98
                $output->writeln('<error>No checks configured.</error>');
99
            }
100
101
            $results = $runner->run($checkName);
102
103
            if ($nagios) {
104
                if ($results->getUnknownCount()) {
105
                    return 3;
106
                }
107
                if ($results->getFailureCount()) {
108
                    return 2;
109
                }
110
                if ($results->getWarningCount()) {
111
                    return 1;
112
                }
113
            }
114
115
            $failureCount += $results->getFailureCount();
116
        }
117
118
        return $failureCount > 0 ? 1 : 0;
119
    }
120
}
121