Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Zend diagnostics #33

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/composer.lock
/vendor/
19 changes: 8 additions & 11 deletions Check/CustomErrorPagesCheck.php → Check/CustomErrorPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace Liip\MonitorBundle\Check;

use Liip\Monitor\Check\Check;
use Liip\Monitor\Result\CheckResult;
use Liip\Monitor\Exception\CheckFailedException;

use Symfony\Bundle\TwigBundle\DependencyInjection\Configuration;

use ZendDiagnostics\Check\AbstractCheck;
use ZendDiagnostics\Result\Failure;
use ZendDiagnostics\Result\Success;

/**
* Checks if error pages have been customized.
*
* @author Cédric Girard <c.girard@lexik.fr>
*/
class CustomErrorPagesCheck extends Check
class CustomErrorPages extends AbstractCheck
{
/**
* @var string
Expand Down Expand Up @@ -74,17 +74,14 @@ public function check()
}

if (count($missingTemplate) > 0) {
throw new CheckFailedException(sprintf('No custom error page found for the following codes: %s', implode(', ', $missingTemplate)));
return new Failure(sprintf('No custom error page found for the following codes: %s', implode(', ', $missingTemplate)));
}
}

$result = $this->buildResult('OK', CheckResult::OK);

} catch (\Exception $e) {
$result = $this->buildResult($e->getMessage(), CheckResult::CRITICAL);
return new Failure($e->getMessage());
}

return $result;
return new Success();
}

/**
Expand Down
71 changes: 71 additions & 0 deletions Check/DepsEntries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Liip\MonitorBundle\Check;

use Symfony\Component\HttpKernel\Kernel;
use ZendDiagnostics\Check\AbstractCheck;
use ZendDiagnostics\Result\Failure;
use ZendDiagnostics\Result\Success;
use ZendDiagnostics\Result\Warning;

/**
* Checks all entries from deps are defined in deps.lock
*
* @author Cédric Girard <c.girard@lexik.fr>
*/
class DepsEntries extends AbstractCheck
{
/**
* @var string
*/
protected $kernelRootDir;

/**
* Construct.
*
* @param string $kernelRootDir
*/
public function __construct($kernelRootDir)
{
$this->kernelRootDir = $kernelRootDir;
}

/**
* @see Liip\Monitor\Check\CheckInterface::check()
*/
public function check()
{
$deps = parse_ini_file($this->kernelRootDir.'/../deps', true, INI_SCANNER_RAW);
$depsLock = array();
foreach (file($this->kernelRootDir.'/../deps.lock', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
$values = explode(' ', $line);
$depsLock[$values[0]] = isset($values[1]) ? $values[1] : '';
}

$unlocked = array();
foreach ($deps as $name => $data) {
if (!isset($depsLock[$name]) || $depsLock[$name] == '') {
$unlocked[] = $name;
}
}

if (count($unlocked) > 0) {
return new Failure(sprintf('The following entries are not defined in the deps.lock file: %s', implode(', ', $unlocked)));
}

$currentVersion = Kernel::VERSION;
if (version_compare($currentVersion, '2.1.0-dev') >= 0) {
return new Warning("Using 'deps.lock' file with Symfony 2.1+ is discouraged, use http://getcomposer.org instead.");
}

return new Success();
}

/**
* @see Liip\Monitor\Check\Check::getName()
*/
public function getName()
{
return 'Deps files entries check';
}
}
72 changes: 0 additions & 72 deletions Check/DepsEntriesCheck.php

This file was deleted.

46 changes: 46 additions & 0 deletions Check/DoctrineDbal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Liip\MonitorBundle\Check;

use Doctrine\Common\Persistence\ConnectionRegistry;
use Doctrine\DBAL\Connection;

use ZendDiagnostics\Check\AbstractCheck;
use ZendDiagnostics\Result\Failure;
use ZendDiagnostics\Result\Success;

class DoctrineDbal extends AbstractCheck
{
/**
* @var ConnectionRegistry
*/
protected $manager;

/**
* @var string
*/
protected $connectionName;

public function __construct(ConnectionRegistry $manager, $connectionName = 'default')
{
$this->manager = $manager;
$this->connectionName = $connectionName;
}

public function check()
{
try {
$connection = $this->manager->getConnection($this->connectionName);
$connection->fetchColumn('SELECT 1');
} catch (\Exception $e) {
return new Failure($e->getMessage());
}

return new Success();
}

public function getName()
{
return "Doctrine DBAL Connnection";
}
}
19 changes: 8 additions & 11 deletions Check/SymfonyVersionCheck.php → Check/SymfonyVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Liip\MonitorBundle\Check;

use Symfony\Component\HttpKernel\Kernel;
use Exception;
use Liip\Monitor\Check\Check;
use Liip\Monitor\Result\CheckResult;
use ZendDiagnostics\Check\AbstractCheck;
use ZendDiagnostics\Result\Success;
use ZendDiagnostics\Result\Warning;

/**
* Checks the version of this website against the latest stable release.
Expand All @@ -19,9 +19,8 @@
*
* @author Roderik van der Veer <roderik@vanderveer.be>
*/
class SymfonyVersionCheck extends Check
class SymfonyVersion extends AbstractCheck
{

/**
* {@inheritdoc}
*/
Expand All @@ -30,16 +29,14 @@ public function check()
try {
$latestRelease = $this->getLatestSymfonyVersion(); // eg. 2.0.12
$currentVersion = Kernel::VERSION;
if (version_compare($currentVersion, $latestRelease) >= 0) {
$result = $this->buildResult('OK', CheckResult::OK);
} else {
$result = $this->buildResult('Update to ' . $latestRelease . ' from ' . $currentVersion, CheckResult::WARNING);
if (version_compare($currentVersion, $latestRelease) < 0) {
return new Warning('Update to ' . $latestRelease . ' from ' . $currentVersion);
}
} catch (\Exception $e) {
$result = $this->buildResult($e->getMessage(), CheckResult::UNKNOWN);
return new Warning($e->getMessage());
}

return $result;
return new Success();
}

private function getLatestSymfonyVersion()
Expand Down
50 changes: 10 additions & 40 deletions Command/HealthCheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Liip\Monitor\Result\CheckResult;

use Liip\MonitorBundle\Helper\Reporter;

class HealthCheckCommand extends ContainerAwareCommand
{
Expand All @@ -20,55 +21,24 @@ protected function configure()
new InputArgument(
'checkName',
InputArgument::OPTIONAL,
'The name of the service to be used to perform the health check.',
'all'
'The name of the service to be used to perform the health check.'
),
));
}

/**
* {@inheritdoc}
*
* @throws \RuntimeException in case there is no or an invalid feed url is given.
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$checkName = $input->getArgument('checkName');
$runner = $this->getContainer()->get('liip_monitor.check.runner');

if ($checkName == 'all') {
$results = $runner->runAllChecks();
} else {
$results = array($runner->runCheckById($checkName));
}

$error = false;
foreach ($results as $result) {
$msg = sprintf('%s: %s', $result->getCheckName(), $result->getMessage());

switch ($result->getStatus()) {
case CheckResult::OK:
$output->writeln(sprintf('<info>OK</info> %s', $msg));
break;

case CheckResult::WARNING:
$output->writeln(sprintf('<comment>WARNING</comment> %s', $msg));
break;

case CheckResult::CRITICAL:
$error = true;
$output->writeln(sprintf('<error>CRITICAL %s</error>', $msg));
break;

case CheckResult::UNKNOWN:
$output->writeln(sprintf('<error>UNKNOWN<error> %s', $msg));
break;
/** @var \ZendDiagnostics\Runner\Runner $runner */
$runner = $this->getContainer()->get('liip_monitor.check.runner');
foreach ($runner->getReporters() as $reporter) {
if ($reporter instanceof Reporter) {
$reporter->setOutput($output);
}
}

$output->writeln('done!');

// return a negative value if an error occurs
return $error ? 1 : 0;
$results = $runner->run($checkName);
return $results->getFailureCount() ? 1 : 0;
}
}
24 changes: 6 additions & 18 deletions Command/ListChecksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,18 @@ protected function configure()
{
$this
->setName('monitor:list')
->addOption('group', 'g', InputOption::VALUE_OPTIONAL, 'List the checks by group')
->setDescription('Lists Health Checks');
}

/**
* {@inheritdoc}
*
* @throws \RuntimeException in case there is no or an invalid feed url is given.
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$chain = $this->getContainer()->get('liip_monitor.check_chain');
if (!$input->getOption('group')) {
foreach ($chain->getGroups() as $group) {

$output->writeln(sprintf('Group <info>%s</info>', $group));
foreach ($chain->getChecksByGroup($group) as $service_id) {
$output->writeln(' - ' . $service_id);
}
}
} else {
foreach ($chain->getAvailableChecks() as $service_id) {
$output->writeln($service_id);
/** @var \ZendDiagnostics\Runner\Runner $runner */
$runner = $this->getContainer()->get('liip_monitor.check.runner');
foreach ($runner->getChecks() as $key => $check) {
if (is_string($key)) {
$output->write($key.': ');
}
$output->writeln($check->getName());
}
}
}
Loading