new: [internal] Redis diagnostic

pull/5198/head
Jakub Onderka 2019-09-20 22:15:15 +02:00
parent dfbb94efba
commit 84d100e982
4 changed files with 64 additions and 7 deletions

View File

@ -1053,6 +1053,8 @@ class ServersController extends AppController
// get the DB diagnostics
$dbDiagnostics = $this->Server->dbSpaceUsage();
$redisInfo = $this->Server->redisInfo();
$moduleTypes = array('Enrichment', 'Import', 'Export', 'Cortex');
foreach ($moduleTypes as $type) {
$moduleStatus[$type] = $this->Server->moduleDiagnostics($diagnostic_errors, $type);
@ -1063,7 +1065,7 @@ class ServersController extends AppController
$sessionStatus = $this->Server->sessionDiagnostics($diagnostic_errors, $sessionCount);
$this->set('sessionCount', $sessionCount);
$additionalViewVars = array('gpgStatus', 'sessionErrors', 'proxyStatus', 'sessionStatus', 'zmqStatus', 'stixVersion', 'cyboxVersion', 'mixboxVersion', 'maecVersion', 'stix2Version', 'pymispVersion', 'moduleStatus', 'yaraStatus', 'gpgErrors', 'proxyErrors', 'zmqErrors', 'stixOperational', 'stix', 'moduleErrors', 'moduleTypes', 'dbDiagnostics');
$additionalViewVars = array('gpgStatus', 'sessionErrors', 'proxyStatus', 'sessionStatus', 'zmqStatus', 'stixVersion', 'cyboxVersion', 'mixboxVersion', 'maecVersion', 'stix2Version', 'pymispVersion', 'moduleStatus', 'yaraStatus', 'gpgErrors', 'proxyErrors', 'zmqErrors', 'stixOperational', 'stix', 'moduleErrors', 'moduleTypes', 'dbDiagnostics', 'redisInfo');
}
// check whether the files are writeable
$writeableDirs = $this->Server->writeableDirsDiagnostics($diagnostic_errors);

View File

@ -36,7 +36,7 @@ class AppModel extends Model
public $inserted_ids = array();
private $__redisConnection = false;
private $__redisConnection = null;
private $__profiler = array();
@ -1824,14 +1824,19 @@ class AppModel extends Model
return preg_match('@^([a-z0-9_.]+[a-z0-9_.\- ]*[a-z0-9_.\-]|[a-z0-9_.])+$@i', $filename);
}
public function setupRedis()
/**
* Similar method as `setupRedis`, but this method throw exception if Redis cannot be reached.
* @return Redis
* @throws Exception
*/
public function setupRedisWithException()
{
if ($this->__redisConnection) {
return $this->__redisConnection;
}
if (!class_exists('Redis')) {
return false;
throw new Exception("Class Redis doesn't exists.");
}
$host = Configure::read('MISP.redis_host') ?: '127.0.0.1';
@ -1841,16 +1846,36 @@ class AppModel extends Model
$redis = new Redis();
if (!$redis->connect($host, $port)) {
return false;
throw new Exception("Could not connect to Redis: {$redis->getLastError()}");
}
if (!empty($pass)) {
$redis->auth($pass);
if (!$redis->auth($pass)) {
throw new Exception("Could not authenticate to Redis: {$redis->getLastError()}");
}
}
$redis->select($database);
if (!$redis->select($database)) {
throw new Exception("Could not select Redis database $database: {$redis->getLastError()}");
}
$this->__redisConnection = $redis;
return $redis;
}
/**
* Method for backward compatibility.
* @deprecated
* @see AppModel::setupRedisWithException
* @return bool|Redis
*/
public function setupRedis()
{
try {
return $this->setupRedisWithException();
} catch (Exception $e) {
return false;
}
}
public function getKafkaPubTool()
{
if (!$this->loadedKafkaPubTool) {

View File

@ -4233,7 +4233,24 @@ class Server extends AppModel
}
return $result;
}
}
public function redisInfo()
{
$output = array(
'extensionVersion' => phpversion('redis'),
'connection' => false,
);
try {
$redis = $this->setupRedisWithException();
$output['connection'] = true;
$output = array_merge($output, $redis->info());
} catch (Exception $e) {
$output['connection_error'] = $e->getMessage();
}
return $output;
}
public function writeableDirsDiagnostics(&$diagnostic_errors)

View File

@ -225,6 +225,19 @@
));
echo '</div>';
?>
<h3><?= __("Redis info") ?></h3>
<div style="background-color:#f7f7f9;width:400px;">
<b><?= __('PHP extension version') ?>:</b> <?= $redisInfo['extensionVersion'] ?: ('<span class="red bold">' . __('Not installed.') . '</span>') ?><br>
<?php if ($redisInfo['connection']): ?>
<b><?= __('Redis version') ?>:</b> <?= $redisInfo['redis_version'] ?><br>
<b><?= __('Memory allocator') ?>:</b> <?= $redisInfo['mem_allocator'] ?><br>
<b><?= __('Memory usage') ?>:</b> <?= $redisInfo['used_memory_human'] ?>B<br>
<b><?= __('Peak memory usage') ?>:</b> <?= $redisInfo['used_memory_peak_human'] ?>B<br>
<b><?= __('Total system memory') ?>:</b> <?= $redisInfo['total_system_memory_human'] ?>B
<?php elseif ($redisInfo['extensionVersion']): ?>
<span class="red bold">Redis is not available. <?= $redisInfo['connection_error'] ?></span>
<?php endif; ?>
</div>
<h3><?php echo __('Advanced attachment handler');?></h3>
<?php echo __('The advanced attachment tools are used by the add attachment functionality to extract additional data about the uploaded sample.');?>
<div style="background-color:#f7f7f9;width:400px;">