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
|
|
|
protected function configure() |
14
|
|
|
{ |
15
|
|
|
$this |
16
|
|
|
->setName('monitor:health') |
17
|
|
|
->setDescription('Runs Health Checks') |
18
|
|
|
->setDefinition(array( |
19
|
|
|
new InputArgument( |
20
|
|
|
'checkName', |
21
|
|
|
InputArgument::OPTIONAL, |
22
|
|
|
'The name of the service to be used to perform the health check.' |
23
|
|
|
), |
24
|
|
|
new InputOption( |
25
|
|
|
'reporter', |
26
|
|
|
null, |
27
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
28
|
|
|
'Additional reporters to run.', |
29
|
|
|
array() |
30
|
|
|
), |
31
|
|
|
new InputOption( |
32
|
|
|
'nagios', |
33
|
|
|
null, |
34
|
|
|
InputOption::VALUE_NONE, |
35
|
|
|
'Suitable for using as a nagios NRPE command.' |
36
|
|
|
), |
37
|
|
|
new InputOption('group', 'g', InputOption::VALUE_REQUIRED, 'List checks for given group'), |
38
|
|
|
)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param InputInterface $input |
43
|
|
|
* @param OutputInterface $output |
44
|
|
|
* |
45
|
|
|
* @return int |
46
|
|
|
*/ |
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
48
|
|
|
{ |
49
|
|
|
$checkName = $input->getArgument('checkName'); |
50
|
|
|
$group = $input->getOption('group'); |
51
|
|
|
|
52
|
|
|
if (is_null($group)) { |
53
|
|
|
$group = $this->getContainer()->getParameter('liip_monitor.default_group'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$runnerServiceId = 'liip_monitor.runner_'.$group; |
57
|
|
|
|
58
|
|
|
if (!$this->getContainer()->has($runnerServiceId)) { |
59
|
|
|
$output->writeln('<error>No such group.</error>'); |
60
|
|
|
|
61
|
|
|
return 1; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$runner = $this->getContainer()->get('liip_monitor.runner_'.$group); |
65
|
|
|
|
66
|
|
|
if ($input->getOption('nagios')) { |
67
|
|
|
$reporter = $this->getContainer()->get('liip_monitor.helper.raw_console_reporter'); |
68
|
|
|
} else { |
69
|
|
|
$reporter = $this->getContainer()->get('liip_monitor.helper.console_reporter'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$runner->addReporter($reporter); |
73
|
|
|
$runner->useAdditionalReporters($input->getOption('reporter')); |
74
|
|
|
|
75
|
|
|
if (0 === count($runner->getChecks())) { |
76
|
|
|
$output->writeln('<error>No checks configured.</error>'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @var \ZendDiagnostics\Result\Collection $results */ |
80
|
|
|
$results = $runner->run($checkName); |
81
|
|
|
if ($input->getOption('nagios')) { |
82
|
|
|
if ($results->getUnknownCount()) { |
83
|
|
|
return 3; |
84
|
|
|
} elseif ($results->getFailureCount()) { |
85
|
|
|
return 2; |
86
|
|
|
} elseif ($results->getWarningCount()) { |
87
|
|
|
return 1; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $results->getFailureCount() ? 1 : 0; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|