mirror of
https://github.com/silverstripe/silverstripe-environmentcheck
synced 2024-10-22 17:05:40 +02:00
Optionally log check failures
Gives more granular control over monitoring failures
This commit is contained in:
parent
b9bfacf223
commit
81c7f2ba27
@ -173,6 +173,13 @@ class EnvironmentCheckSuite extends Object {
|
||||
if(!is_array($names)) $names = array($names);
|
||||
foreach($names as $name) self::inst($name)->push($check, $title);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters all checks.
|
||||
*/
|
||||
static function reset() {
|
||||
self::$instances = array();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,6 +210,7 @@ class EnvironmentCheckSuiteResult extends ViewableData {
|
||||
$this->details->push(new ArrayData(array(
|
||||
'Check' => $checkIdentifier,
|
||||
'Status' => $this->statusText($status),
|
||||
'StatusCode' => $status,
|
||||
'Message' => $message,
|
||||
)));
|
||||
|
||||
|
@ -41,6 +41,26 @@ class EnvironmentChecker extends RequestHandler {
|
||||
*/
|
||||
public static $email_results = false;
|
||||
|
||||
/**
|
||||
* @var bool Log results via {@link SS_Log}
|
||||
*/
|
||||
private static $log_results_warning = false;
|
||||
|
||||
/**
|
||||
* @var int Maps to {@link Zend_Log} levels. Defaults to Zend_Log::WARN
|
||||
*/
|
||||
private static $log_results_warning_level = 4;
|
||||
|
||||
/**
|
||||
* @var bool Log results via {@link SS_Log}
|
||||
*/
|
||||
private static $log_results_error = false;
|
||||
|
||||
/**
|
||||
* @var int Maps to {@link Zend_Log} levels. Defaults to Zend_Log::ALERT
|
||||
*/
|
||||
private static $log_results_error_level = 1;
|
||||
|
||||
/**
|
||||
* @param string $checkSuiteName
|
||||
* @param string $title
|
||||
@ -151,6 +171,21 @@ class EnvironmentChecker extends RequestHandler {
|
||||
$email->send();
|
||||
}
|
||||
|
||||
// Optionally log errors and warnings individually
|
||||
foreach($result->Details() as $detail) {
|
||||
if($this->config()->log_results_warning && $detail->StatusCode == EnvironmentCheck::WARNING) {
|
||||
$this->log(
|
||||
sprintf('EnvironmentChecker warning at "%s" check. Message: %s', $detail->Check, $detail->Message),
|
||||
$this->config()->log_results_warning_level
|
||||
);
|
||||
} elseif($this->config()->log_results_error && $detail->StatusCode == EnvironmentCheck::ERROR) {
|
||||
$this->log(
|
||||
sprintf('EnvironmentChecker error at "%s" check. Message: %s', $detail->Check, $detail->Message),
|
||||
$this->config()->log_results_error_level
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// output the result as JSON if requested
|
||||
if(
|
||||
$this->getRequest()->getExtension() == 'json'
|
||||
@ -166,6 +201,14 @@ class EnvironmentChecker extends RequestHandler {
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param int $level
|
||||
*/
|
||||
public function log($message, $level) {
|
||||
SS_Log::log($message, $level);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the HTTP status code that should be returned when there's an error.
|
||||
*
|
||||
|
@ -17,6 +17,10 @@
|
||||
{
|
||||
"silverstripe/framework": "~3.1"
|
||||
},
|
||||
"require-dev":
|
||||
{
|
||||
"hafriedlander/phockito": "dev-master"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.1.x-dev"
|
||||
|
24
readme.md
24
readme.md
@ -83,6 +83,30 @@ EnvironmentCheckSuite:
|
||||
* `SMTPConnectCheck`: Checks if the SMTP connection configured through PHP.ini works as expected.
|
||||
* `SolrIndexCheck`: Checks if the Solr cores of given class are available.
|
||||
|
||||
## Monitoring Checks
|
||||
|
||||
Checks will return an appropriate HTTP status code, so are easy to hook into common uptime montoring
|
||||
solutions like pingdom.com.
|
||||
|
||||
You can also have the environment checker email results with the following configuration:
|
||||
|
||||
```yml
|
||||
EnvironmentChecker:
|
||||
email_results: true
|
||||
to_email_address: support@test.com
|
||||
from_email_address: admin@test.com
|
||||
```
|
||||
|
||||
Errors can be logged via the standard SilverStripe logging. Each check will cause an individual log entry.
|
||||
You can choose to enable logging separately for warnings and errors,
|
||||
identified through the result of `EnvironmentCheck->check()`.
|
||||
|
||||
```yml
|
||||
EnvironmentChecker:
|
||||
log_results_warning: true
|
||||
log_results_error: true
|
||||
```
|
||||
|
||||
## Authentication
|
||||
|
||||
By default, accessing the `dev/check` URL will not require authentication on CLI and dev environments, but if you're
|
||||
|
89
tests/EnvironmentCheckerTest.php
Normal file
89
tests/EnvironmentCheckerTest.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
class EnvironmentCheckerTest extends SapphireTest {
|
||||
|
||||
public function setUpOnce() {
|
||||
parent::setUpOnce();
|
||||
|
||||
Phockito::include_hamcrest();
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
Config::nest();
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
Config::unnest();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testOnlyLogsWithErrors() {
|
||||
Config::inst()->update('EnvironmentChecker', 'log_results_warning', true);
|
||||
Config::inst()->update('EnvironmentChecker', 'log_results_error', true);
|
||||
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckNoErrors());
|
||||
$checker = Phockito::spy(
|
||||
'EnvironmentChecker',
|
||||
'test suite',
|
||||
'test'
|
||||
);
|
||||
|
||||
$response = $checker->index();
|
||||
Phockito::verify($checker, 0)->log(anything(), anything());
|
||||
EnvironmentCheckSuite::reset();
|
||||
}
|
||||
|
||||
public function testLogsWithWarnings() {
|
||||
Config::inst()->update('EnvironmentChecker', 'log_results_warning', true);
|
||||
Config::inst()->update('EnvironmentChecker', 'log_results_error', false);
|
||||
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckWarnings());
|
||||
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckErrors());
|
||||
$checker = Phockito::spy(
|
||||
'EnvironmentChecker',
|
||||
'test suite',
|
||||
'test'
|
||||
);
|
||||
|
||||
$response = $checker->index();
|
||||
Phockito::verify($checker, 1)->log(containsString('warning'), anything());
|
||||
Phockito::verify($checker, 0)->log(containsString('error'), anything());
|
||||
EnvironmentCheckSuite::reset();
|
||||
}
|
||||
|
||||
public function testLogsWithErrors() {
|
||||
Config::inst()->update('EnvironmentChecker', 'log_results_error', false);
|
||||
Config::inst()->update('EnvironmentChecker', 'log_results_error', true);
|
||||
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckWarnings());
|
||||
EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckErrors());
|
||||
$checker = Phockito::spy(
|
||||
'EnvironmentChecker',
|
||||
'test suite',
|
||||
'test'
|
||||
);
|
||||
|
||||
$response = $checker->index();
|
||||
Phockito::verify($checker, 0)->log(containsString('warning'), anything());
|
||||
Phockito::verify($checker, 1)->log(containsString('error'), anything());
|
||||
EnvironmentCheckSuite::reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class EnvironmentCheckerTest_CheckNoErrors implements EnvironmentCheck, TestOnly{
|
||||
public function check() {
|
||||
return array(EnvironmentCheck::OK, '');
|
||||
}
|
||||
}
|
||||
|
||||
class EnvironmentCheckerTest_CheckWarnings implements EnvironmentCheck, TestOnly{
|
||||
public function check() {
|
||||
return array(EnvironmentCheck::WARNING, "test warning");
|
||||
}
|
||||
}
|
||||
|
||||
class EnvironmentCheckerTest_CheckErrors implements EnvironmentCheck, TestOnly{
|
||||
public function check() {
|
||||
return array(EnvironmentCheck::ERROR, "test error");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user