Skip to content

Commit

Permalink
Merge 65ddc12 into b3a5422
Browse files Browse the repository at this point in the history
  • Loading branch information
DrSchimke committed Jan 5, 2016
2 parents b3a5422 + 65ddc12 commit b97f503
Show file tree
Hide file tree
Showing 54 changed files with 1,336 additions and 450 deletions.
14 changes: 13 additions & 1 deletion Command/HealthCheckCommand.php
Expand Up @@ -2,6 +2,7 @@

namespace Liip\MonitorBundle\Command;

use Liip\MonitorBundle\Helper\RunnerManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -34,6 +35,7 @@ protected function configure()
InputOption::VALUE_NONE,
'Suitable for using as a nagios NRPE command.'
),
new InputOption('group', 'g', InputOption::VALUE_REQUIRED, 'List checks for given group'),
));
}

Expand All @@ -46,7 +48,17 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$checkName = $input->getArgument('checkName');
$runner = $this->getContainer()->get('liip_monitor.runner');
$group = $input->getOption('group');

/** @var RunnerManager $runnerManager */
$runnerManager = $this->getContainer()->get('liip_monitor.helper.runner_manager');
$runner = $runnerManager->getRunner($group);

if (null === $runner) {
$output->writeln('<error>No such group.</error>');

return 1;
}

if ($input->getOption('nagios')) {
$reporter = $this->getContainer()->get('liip_monitor.helper.raw_console_reporter');
Expand Down
75 changes: 67 additions & 8 deletions Command/ListChecksCommand.php
Expand Up @@ -2,48 +2,80 @@

namespace Liip\MonitorBundle\Command;

use Liip\MonitorBundle\Helper\RunnerManager;
use Liip\MonitorBundle\Runner;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class ListChecksCommand extends ContainerAwareCommand
{
/** @var RunnerManager */
private $runnerManager;

protected function configure()
{
$this
->setName('monitor:list')
->setDescription('Lists Health Checks')
->addOption('all', 'a', InputOption::VALUE_NONE, 'Lists Health Checks of all groups')
->addOption('reporters', 'r', InputOption::VALUE_NONE, 'List registered additional reporters')
->addOption('group', 'g', InputOption::VALUE_REQUIRED, 'List checks for given group')
->addOption('groups', 'G', InputOption::VALUE_NONE, 'List all registered groups')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->runnerManager = $this->getContainer()->get('liip_monitor.helper.runner_manager');

switch (true) {
case $input->getOption('reporters'):
$this->listReporters($output);
break;
case $input->getOption('all'):
$this->listAllChecks($output);
break;
case $input->getOption('groups'):
$this->listGroups($output);
break;
default:
$this->listChecks($output);
$this->listChecks($input, $output);
break;
}
}

protected function listChecks(OutputInterface $output)
protected function listChecks(InputInterface $input, OutputInterface $output)
{
$runner = $this->getContainer()->get('liip_monitor.runner');
$checks = $runner->getChecks();
$group = $input->getOption('group');

if (0 === count($checks)) {
$output->writeln('<error>No checks configured.</error>');
$runner = $this->runnerManager->getRunner($group);

if (null === $runner) {
$output->writeln('<error>No such group.</error>');

return;
}

foreach ($runner->getChecks() as $alias => $check) {
$output->writeln(sprintf('<info>%s</info> %s', $alias, $check->getLabel()));
$this->doListChecks($output, $runner);
}

/**
* @param OutputInterface $output
*/
protected function listAllChecks(OutputInterface $output)
{
foreach ($this->runnerManager->getRunners() as $group => $runner) {
$output->writeln(sprintf('<fg=yellow;options=bold>%s</>', $group));

$this->doListChecks($output, $runner);
}
}

/**
* @param OutputInterface $output
*/
protected function listReporters(OutputInterface $output)
{
$reporters = $this->getContainer()->get('liip_monitor.runner')->getAdditionalReporters();
Expand All @@ -55,4 +87,31 @@ protected function listReporters(OutputInterface $output)
$output->writeln($reporter);
}
}

/**
* @param OutputInterface $output
*/
protected function listGroups(OutputInterface $output)
{
foreach ($this->runnerManager->getGroups() as $group) {
$output->writeln($group);
}
}

/**
* @param OutputInterface $output
* @param Runner $runner
*/
private function doListChecks(OutputInterface $output, Runner $runner)
{
$checks = $runner->getChecks();

if (0 === count($checks)) {
$output->writeln('<error>No checks configured.</error>');
}

foreach ($runner->getChecks() as $alias => $check) {
$output->writeln(sprintf('<info>%s</info> %s', $alias, $check->getLabel()));
}
}
}
89 changes: 77 additions & 12 deletions Controller/HealthCheckController.php
Expand Up @@ -3,6 +3,7 @@
namespace Liip\MonitorBundle\Controller;

use Liip\MonitorBundle\Helper\ArrayReporter;
use Liip\MonitorBundle\Helper\RunnerManager;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -11,17 +12,18 @@

class HealthCheckController
{
protected $runner;
protected $runnerManager;
protected $pathHelper;
protected $template;

/**
* @param Runner $runner
* @param PathHelper $pathHelper
* @param RunnerManager $runnerManager
* @param PathHelper $pathHelper
* @param $template
*/
public function __construct(Runner $runner, PathHelper $pathHelper, $template)
public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template)
{
$this->runner = $runner;
$this->runnerManager = $runnerManager;
$this->pathHelper = $pathHelper;
$this->template = $template;
}
Expand All @@ -33,9 +35,11 @@ public function __construct(Runner $runner, PathHelper $pathHelper, $template)
*/
public function indexAction(Request $request)
{
$group = $this->getGroup($request);

$urls = $this->pathHelper->getRoutesJs(array(
'liip_monitor_run_all_checks' => array(),
'liip_monitor_run_single_check' => array('checkId' => 'replaceme'),
'liip_monitor_run_all_checks' => array('group' => $group),
'liip_monitor_run_single_check' => array('checkId' => 'replaceme', 'group' => $group),
));

$css = $this->pathHelper->getStyleTags(array(
Expand All @@ -61,17 +65,45 @@ public function indexAction(Request $request)
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function listAction()
public function listAction(Request $request)
{
$ret = array();

foreach ($this->runner->getChecks() as $alias => $check) {
$runner = $this->getRunner($request);

foreach ($runner->getChecks() as $alias => $check) {
$ret[] = $alias;
}

return new JsonResponse($ret);
}

/**
* @return JsonResponse
*/
public function listAllAction()
{
$allChecks = array();

foreach ($this->runnerManager->getRunners() as $group => $runner) {
foreach ($runner->getChecks() as $alias => $check) {
$allChecks[$group][] = $alias;
}
}

return new JsonResponse($allChecks);
}

/**
* @return JsonResponse
*/
public function listGroupsAction()
{
$groups = $this->runnerManager->getGroups();

return new JsonResponse($groups);
}

/**
* @param Request $request
*
Expand Down Expand Up @@ -146,10 +178,43 @@ protected function runTests(Request $request, $checkId = null)
}

$reporter = new ArrayReporter();
$this->runner->addReporter($reporter);
$this->runner->useAdditionalReporters($reporters);
$this->runner->run($checkId);

$runner = $this->getRunner($request);

$runner->addReporter($reporter);
$runner->useAdditionalReporters($reporters);
$runner->run($checkId);

return $reporter;
}

/**
* @param Request $request
*
* @return Runner
*
* @throws \Exception
*/
private function getRunner(Request $request)
{
$group = $this->getGroup($request);

$runner = $this->runnerManager->getRunner($group);

if ($runner) {
return $runner;
}

throw new \RuntimeException(sprintf('Unknown check group "%s"', $group));
}

/**
* @param Request $request
*
* @return string
*/
private function getGroup(Request $request)
{
return $request->query->get('group') ?: $this->runnerManager->getDefaultGroup();
}
}
91 changes: 91 additions & 0 deletions DependencyInjection/Compiler/AddGroupsCompilerPass.php
@@ -0,0 +1,91 @@
<?php

namespace Liip\MonitorBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AddGroupsCompilerPass implements CompilerPassInterface
{
const SERVICE_ID_PREFIX = 'liip_monitor.check.';

/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasParameter('liip_monitor.checks')) {
return;
}

$checkConfig = $container->getParameter('liip_monitor.checks');

list($checks, $checkCollections) = $this->parseGroups($container, $checkConfig['groups']);

$this->addGroupTags($container, $checks, 'liip_monitor.check');
$this->addGroupTags($container, $checkCollections, 'liip_monitor.check_collection');
}

/**
* @param ContainerBuilder $container
* @param array $data
*
* @return array
*/
private function parseGroups(ContainerBuilder $container, array $data)
{
$checks = array();
$checkCollections = array();

foreach ($data as $group => $groupChecks) {
foreach (array_keys($groupChecks) as $checkName) {
$serviceId = self::SERVICE_ID_PREFIX.$checkName;
$checkDefinition = $container->getDefinition($serviceId);

if ($checkDefinition->hasTag('liip_monitor.check')) {
$checks[$checkName][] = $group;
} elseif ($checkDefinition->hasTag('liip_monitor.check_collection')) {
$checkCollections[$checkName][] = $group;
}
}
}

return array($checks, $checkCollections);
}

/**
* This Method completes the service definitions of each check for a configured group.
*
* For every configured check (per group) a parameter has been generated in LiipMonitorExtension::setParameters.
* So the finally generated parameters have to be injected into each check service definition.
* (see the preg_match part).
*
* @param ContainerBuilder $container
* @param array $checks
* @param string $tag
*/
private function addGroupTags(ContainerBuilder $container, array $checks, $tag)
{
foreach ($checks as $checkName => $groups) {
$serviceId = self::SERVICE_ID_PREFIX.$checkName;
$serviceDefinition = $container->getDefinition($serviceId);
$serviceDefinition->clearTag($tag);

foreach ($groups as $group) {
$tmpDefinition = clone $serviceDefinition;
$tmpDefinition->addTag($tag, array('group' => $group, 'alias' => $checkName));

foreach ($tmpDefinition->getArguments() as $argumentIndex => $argument) {
if (is_string($argument) && preg_match('/^%%(.*)%%$/', $argument, $matches)) {
$newArgument = $container->getParameter($matches[1].'.'.$group);
$tmpDefinition->replaceArgument($argumentIndex, $newArgument);
}
}

$container->setDefinition($serviceId.'.'.$group, $tmpDefinition);
}

$container->removeDefinition($serviceId);
}
}
}

0 comments on commit b97f503

Please sign in to comment.