Commit b1861698 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1714100: Check the status of current elasticsearch index



Because the cluster can be giving a 'yellow' warning but the index
this particular site uses might be 'green' and therefore working as
expected

behatnotneeded

Change-Id: I9d38a4696cee777d4c20f368be917f22c3dde3d8
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent a3d904e1
......@@ -49,12 +49,14 @@ $string['hostdescription'] = 'Hostname of the Elasticsearch server. Default is 1
$string['indexingrunning'] = 'Indexing cron job is running. Please try again in a few minutes.';
$string['indexname'] = 'Index name';
$string['indexnamedescription'] = 'Name of the Elasticsearch index. Default is mahara.';
$string['indexstatusok'] = 'The current index "%s" has status green.';
$string['indexstatusbad'] = 'The current index "%s" has status "%s" and will need to be fixed up.';
$string['license'] = 'License';
$string['Media'] = 'Media';
$string['newindextype'] = 'A new index type "%s" has been added to your elasticsearch settings. For this to take effect you will need to reindex your site';
$string['newversion'] = 'A new Elasticsearch PHP version "%s" has been added to Mahara that is compatible with Elasticsearch server "%s" and above. For this to take effect you will need to reindex your site';
$string['none'] = 'none';
$string['noticeenabled'] = 'The Elasticsearch plugin is currently active. To deactivate it, deselect it in the <a href="%s">site options search settings</a>.';
$string['noticeenabled'] = 'The Elasticsearch plugin is currently active. To deactivate it, deselect it in the <a href="%s" class="elasticsearch-status">site options search settings</a>.';
$string['noticenotactive'] = 'The ElasticSearch Server is unreachable on host: %s and port %s. Please make sure it is running.';
$string['noticenotenabled'] = 'The Elasticsearch plugin is not currently enabled. To activate it, select it in the <a href="%s">site options in the search settings</a>.';
$string['owner'] = 'Owner';
......
......@@ -110,11 +110,12 @@ class PluginSearchElasticsearch extends PluginSearch {
/**
* This function returns elasticsearch server information at supplied host and port
* @param string $option An optional param to get status about a specific thing, eg cluster health
* @param string $option An optional param to get status about a specific status, eg cluster health
* @param string $index An optional param to get status about a specific status for a particular index, eg indices status
* @return array containing $canconnect bool - whether we can connect to elasticsearch at host/port
* $server object - information about the server request
*/
public static function elasticsearch_server($option = null) {
public static function elasticsearch_server($option=null, $index=null) {
$host = get_config_plugin('search', 'elasticsearch', 'host');
$port = get_config_plugin('search', 'elasticsearch', 'port');
$url = $host . ':' . $port;
......@@ -123,15 +124,27 @@ class PluginSearchElasticsearch extends PluginSearch {
case "clusterhealth":
$url .= '/_cluster/health';
break;
case "shardallocation":
$url .= '/_cat/shards';
case "indexhealth":
$url .= '/_cat/indices?format=json';
break;
}
}
$server = mahara_http_request(array(CURLOPT_URL => $url), true);
$canconnect = false;
if (!empty($server->data)) {
$server->data = json_decode($server->data);
if ($index && is_array($server->data)) {
// we need to find the data for particular index
$thisindex = null;
foreach ($server->data as $key => $data) {
if (isset($data->index) && $data->index == $index) {
$thisindex = $server->data[$key];
break;
}
}
$server->data = $thisindex;
}
if (!empty($server->data->version) && !empty($server->data->version->number)) {
if (version_compare($server->data->version->number, self::elasticsearch_version) !== -1) {
$canconnect = true;
......@@ -202,14 +215,10 @@ class PluginSearchElasticsearch extends PluginSearch {
public static function get_config_options() {
$enabledhtml = '';
if (get_config('searchplugin') == 'elasticsearch') {
$enabledhtml = self::get_formatted_notice(get_string('noticeenabled', 'search.elasticsearch', get_config('wwwroot') . 'admin/site/options.php?fs=searchsettings'), 'notice');
}
else {
$enabledhtml = self::get_formatted_notice(get_string('noticenotenabled', 'search.elasticsearch', get_config('wwwroot').'admin/site/options.php?fs=searchsettings'), 'warning');
}
$state = 'ok';
list($status, $server) = self::elasticsearch_server();
if (!$status) {
$state = 'notice';
$notice = get_string('noticenotactive', 'search.elasticsearch', get_config_plugin('search', 'elasticsearch', 'host'), get_config_plugin('search', 'elasticsearch', 'port'));
if (!empty($server->error)) {
$notice = $server->error;
......@@ -219,6 +228,19 @@ class PluginSearchElasticsearch extends PluginSearch {
list($status, $health) = self::elasticsearch_server('clusterhealth');
if (!empty($health->data) && $health->data->status != 'green') {
$enabledhtml .= self::get_formatted_notice(get_string('clusterstatus', 'search.elasticsearch', $health->data->status, $health->data->unassigned_shards), 'warning');
$state = 'notice';
}
$index = get_config_plugin('search', 'elasticsearch', 'indexname');
list($status, $health) = self::elasticsearch_server('indexhealth', $index);
if (!empty($health->data) && $health->data->health != 'green') {
$enabledhtml .= self::get_formatted_notice(get_string('indexstatusbad', 'search.elasticsearch', $index, $health->data->health), 'warning');
$state = 'notice';
}
if (get_config('searchplugin') == 'elasticsearch') {
$enabledhtml .= self::get_formatted_notice(get_string('noticeenabled', 'search.elasticsearch', get_config('wwwroot') . 'admin/site/options.php?fs=searchsettings'), $state);
}
else {
$enabledhtml .= self::get_formatted_notice(get_string('noticenotenabled', 'search.elasticsearch', get_config('wwwroot').'admin/site/options.php?fs=searchsettings'), 'warning');
}
$config = array(
......
<div class="admin-warning alert alert-success">
{if $header}<h3>{$header}</h3>{/if}
<p>{$notice|safe}</p>
</div>
\ No newline at end of file
......@@ -63,3 +63,8 @@
font-weight: bold;
}
}
.elasticsearch-status {
color: darken($link-color, 10%);
font-weight: bold;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment