Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
fix(chart): curves in graph not synchronized on display (#8039)
Browse files Browse the repository at this point in the history
Refs: MON-4344
  • Loading branch information
lpinsivy authored and kduret committed Oct 31, 2019
1 parent bf79161 commit 6597ef4
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 75 deletions.
44 changes: 27 additions & 17 deletions www/class/centreonGraph.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class CentreonGraph
protected $vname;
protected $metrics;
protected $longer;
protected $rrdCachedOptions;
public $onecurve;
public $checkcurve;

Expand Down Expand Up @@ -206,6 +207,19 @@ public function __construct($user_id, $index = null, $debug = 0, $compress = nul
$DBRESULT->closeCursor();
unset($opt);

/* Get RRDCacheD options */
$result = $this->DB->query(
"SELECT config_key, config_value
FROM cfg_centreonbroker_info AS cbi
INNER JOIN cfg_centreonbroker AS cb ON (cb.config_id = cbi.config_id)
INNER JOIN nagios_server AS ns ON (ns.id = cb.ns_nagios_server)
WHERE ns.localhost = '1'
AND cbi.config_key IN ('rrd_cached_option', 'rrd_cached')"
);
while ($row = $result->fetch()) {
$this->rrdCachedOptions[$row['config_key']] = $row['config_value'];
}

if (isset($index)) {
$DBRESULT = $this->DB->query("SELECT `metric_id`
FROM `ods_view_details`
Expand Down Expand Up @@ -1504,9 +1518,11 @@ public function getOVDColor($metricId)
if (is_null($this->colorCache)) {
$this->colorCache = array();

$DBRESULT = $this->DB->query("SELECT metric_id, rnd_color FROM `ods_view_details` WHERE `index_id` = '" . $this->index . "'");
$DBRESULT = $this->DB->query(
"SELECT metric_id, rnd_color FROM `ods_view_details` WHERE `index_id` = '" . $this->index . "'"
);
while (($row = $DBRESULT->fetchRow())) {
$this->colorCache[$row['metric_id']] = $row['rnd_color'];
$this->colorCache[$row['metric_id']] = $row['rnd_color'];
}
}

Expand All @@ -1519,7 +1535,7 @@ public function getOVDColor($metricId)
'INSERT INTO `ods_view_details` (rnd_color, index_id, metric_id) '
. 'VALUES ("' . $lRndcolor . '", ' . $this->index . ', ' . $metricId . ')'
);
}
}
return $lRndcolor;
}

Expand Down Expand Up @@ -1565,7 +1581,7 @@ public function getRandomWebColor()
'#ff66ff', '#ff9900', '#ff9933', '#ff9966', '#ff9999', '#ff99cc',
'#ff99ff', '#ffcc00', '#ffcc33', '#ffcc66', '#ffcc99', '#ffcccc',
'#ffccff');
return $webSafeColors[rand(0,sizeof($webSafeColors)-1)];
return $webSafeColors[rand(0, sizeof($webSafeColors)-1)];
}

/**
Expand Down Expand Up @@ -1780,8 +1796,8 @@ private function checkDBAvailability($metric_id)
*/
protected function flushRrdcached($metricsId)
{
if (!isset($this->generalOpt['rrdcached_enable'])
|| $this->generalOpt['rrdcached_enable'] == 0
if (!isset($this->rrdCachedOptions['rrd_cached_option'])
|| !in_array($this->rrdCachedOptions['rrd_cached_option'], ['unix', 'tcp'])
) {
return true;
}
Expand All @@ -1791,20 +1807,14 @@ protected function flushRrdcached($metricsId)
*/
$errno = 0;
$errstr = '';
if (isset($this->generalOpt['rrdcached_port'])
&& trim($this->generalOpt['rrdcached_port']) != ''
) {
$sock = @fsockopen('127.0.0.1', trim($this->generalOpt['rrdcached_port']), $errno, $errstr);
if ($sock === false) {
return false;
}
} elseif (isset($this->generalOpt['rrdcached_unix_path'])
&& trim($this->generalOpt['rrdcached_unix_path']) != ''
) {
$sock = @fsockopen('unix://' . trim($this->generalOpt['rrdcached_unix_path']), $errno, $errstr);
if ($this->rrdCachedOptions['rrd_cached_option'] === 'tcp') {
$sock = fsockopen('127.0.0.1', trim($this->rrdCachedOptions['rrd_cached']), $errno, $errstr);
} elseif ($this->rrdCachedOptions['rrd_cached_option'] === 'unix') {
$sock = fsockopen('unix://' . trim($this->rrdCachedOptions['rrd_cached']), $errno, $errstr);
} else {
return false;
}

if (false === $sock) {
// @todo log the error
return false;
Expand Down
76 changes: 53 additions & 23 deletions www/class/centreonGraphNg.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class CentreonGraphNg
protected $vnodesDependencies;
protected $vmetricsOrder;
protected $graphData;
protected $rrdCachedOptions;

/**
* Connect to databases
Expand Down Expand Up @@ -228,6 +229,20 @@ public function __construct($userId)
$stmt = $this->db->prepare("SELECT `key`, `value` FROM options");
$stmt->execute();
$this->generalOpt = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_ASSOC);

/* Get RRDCacheD options */
$stmt = $this->db->query(
"SELECT config_key, config_value
FROM cfg_centreonbroker_info AS cbi
INNER JOIN cfg_centreonbroker AS cb ON (cb.config_id = cbi.config_id)
INNER JOIN nagios_server AS ns ON (ns.id = cb.ns_nagios_server)
WHERE ns.localhost = '1'
AND cbi.config_key IN ('rrd_cached_option', 'rrd_cached')"
);

while ($row = $stmt->fetch()) {
$this->rrdCachedOptions[$row['config_key']] = $row['config_value'];
}
}

/**
Expand Down Expand Up @@ -332,8 +347,8 @@ protected function getCurveDsConfig($metric)
break;
}

if (is_null($dsDataRegular) &&
preg_match('/^' . preg_quote($dsVal['ds_name'], '/') . '$/i', $metric['metric_name'])
if (is_null($dsDataRegular)
&& preg_match('/^' . preg_quote($dsVal['ds_name'], '/') . '$/i', $metric['metric_name'])
) {
$dsDataRegular = $dsVal;
}
Expand Down Expand Up @@ -594,11 +609,12 @@ public function addMetric($metricId, $isVirtual = 0)
{
if ($isVirtual == 0) {
$stmt = $this->dbCs->prepare(
"SELECT m.index_id, host_id, service_id, metric_id, metric_name, unit_name, min, max, warn, warn_low, crit, crit_low
FROM metrics AS m, index_data AS i
WHERE m.metric_id = :metric_id
AND m.hidden = '0'
AND m.index_id = i.id"
"SELECT m.index_id, host_id, service_id, metric_id, metric_name,
unit_name, min, max, warn, warn_low, crit, crit_low
FROM metrics AS m, index_data AS i
WHERE m.metric_id = :metric_id
AND m.hidden = '0'
AND m.index_id = i.id"
);
$stmt->bindParam(':metric_id', $metricId, PDO::PARAM_INT);
$stmt->execute();
Expand Down Expand Up @@ -655,7 +671,8 @@ private function initCurveList()

foreach ($this->metrics as $metricId => &$tm) {
if (isset($tm['ds_data']['ds_invert']) && $tm['ds_data']['ds_invert']) {
$this->addArgument("DEF:vi" . $metricId . "=" . $this->dbPath . $metricId . ".rrd:value:AVERAGE CDEF:v" . $metricId . "=vi" . $metricId . ",-1,*");
$this->addArgument("DEF:vi" . $metricId . "=" . $this->dbPath . $metricId . ".rrd:value:AVERAGE CDEF:v"
. $metricId . "=vi" . $metricId . ",-1,*");
} else {
$this->addArgument("DEF:v" . $metricId . "=" . $this->dbPath . $metricId . ".rrd:value:AVERAGE");
}
Expand All @@ -664,7 +681,8 @@ private function initCurveList()
$this->manageMetrics();

foreach ($this->vmetricsOrder as $vmetricId) {
$this->addArgument($this->vmetrics[$vmetricId]['def_type'] . ":vv" . $vmetricId . "=" . $this->vmetrics[$vmetricId]['rpn_function']);
$this->addArgument($this->vmetrics[$vmetricId]['def_type'] . ":vv" . $vmetricId . "="
. $this->vmetrics[$vmetricId]['rpn_function']);
}
}

Expand Down Expand Up @@ -876,7 +894,8 @@ private function getIndexData()
}

if ($this->indexData["host_name"] != "_Module_Meta") {
$this->extraDatas['title'] = $this->indexData['service_description'] . " " . _("graph on") . " " . $this->indexData['host_name'];
$this->extraDatas['title'] = $this->indexData['service_description'] . " " . _("graph on") . " "
. $this->indexData['host_name'];
} else {
$this->extraDatas['title'] = _("Graph") . " " . $this->indexData["service_description"];
}
Expand Down Expand Up @@ -1034,7 +1053,13 @@ public function getJsonStream()
2 => $stderr
);

$process = proc_open($this->generalOpt['rrdtool_path_bin']['value'] . " - ", $descriptorspec, $pipes, null, null);
$process = proc_open(
$this->generalOpt['rrdtool_path_bin']['value'] . " - ",
$descriptorspec,
$pipes,
null,
null
);
$this->graphData = array(
'global' => $this->extraDatas,
'metrics' => array(),
Expand Down Expand Up @@ -1101,17 +1126,23 @@ public function getOVDColor($indexId, $metricId)
if (is_null($this->colorCache)) {
$this->colorCache = array();

$stmt = $this->db->prepare("SELECT metric_id, rnd_color FROM `ods_view_details` WHERE `index_id` = :index_id");
$stmt = $this->db->prepare(
"SELECT metric_id, rnd_color FROM `ods_view_details` WHERE `index_id` = :index_id"
);
$stmt->bindParam(':index_id', $indexId, PDO::PARAM_INT);
$stmt->execute();
$this->colorCache = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_ASSOC);
}

if (isset($this->colorCache[$metricId]) && preg_match("/^\#[a-f0-9]{6,6}/i", $this->colorCache[$metricId]['rnd_color'])) {
if (isset($this->colorCache[$metricId])
&& preg_match("/^\#[a-f0-9]{6,6}/i", $this->colorCache[$metricId]['rnd_color'])
) {
return $this->colorCache[$metricId]['rnd_color'];
}
$lRndcolor = $this->getRandomWebColor();
$stmt = $this->db->prepare("INSERT INTO `ods_view_details` (rnd_color, index_id, metric_id) VALUES (:rnd_color, :index_id, :metric_id)");
$stmt = $this->db->prepare(
"INSERT INTO `ods_view_details` (rnd_color, index_id, metric_id) VALUES (:rnd_color, :index_id, :metric_id)"
);
$stmt->bindParam(':rnd_color', $lRndcolor, PDO::PARAM_STR);
$stmt->bindParam(':index_id', $indexId, PDO::PARAM_INT);
$stmt->bindParam(':metric_id', $metricId, PDO::PARAM_INT);
Expand Down Expand Up @@ -1231,23 +1262,22 @@ private function checkDBAvailability($metricId)
*/
protected function flushRrdcached($metricsId)
{
if (!isset($this->generalOpt['rrdcached_enable']['value'])
|| $this->generalOpt['rrdcached_enable']['value'] == 0) {
if (!isset($this->rrdCachedOptions['rrd_cached_option'])
|| !in_array($this->rrdCachedOptions['rrd_cached_option'], ['unix', 'tcp'])
) {
return true;
}

$errno = 0;
$errstr = '';
if (isset($this->generalOpt['rrdcached_port']['value'])
&& trim($this->generalOpt['rrdcached_port']['value']) != '') {
$sock = fsockopen('127.0.0.1', trim($this->generalOpt['rrdcached_port']['value']), $errno, $errstr);
} elseif (isset($this->generalOpt['rrdcached_unix_path']['value'])
&& trim($this->generalOpt['rrdcached_unix_path']['value']) != '') {
$sock = fsockopen('unix://' . trim($this->generalOpt['rrdcached_unix_path']['value']), $errno, $errstr);
if ($this->rrdCachedOptions['rrd_cached_option'] === 'tcp') {
$sock = fsockopen('127.0.0.1', trim($this->rrdCachedOptions['rrd_cached']), $errno, $errstr);
} elseif ($this->rrdCachedOptions['rrd_cached_option'] === 'unix') {
$sock = fsockopen('unix://' . trim($this->rrdCachedOptions['rrd_cached']), $errno, $errstr);
} else {
return false;
}

if (false === $sock) {
$this->log("socket connection: " . $errstr);
return false;
Expand Down
45 changes: 32 additions & 13 deletions www/class/centreonGraphStatus.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct($index, $start, $end)
$this->endTime = $end;
$this->statusPath = $this->getStatusPath();
$this->generalOpt = $this->getOptions();
$this->rrdCachedOptions = $this->getRrdCachedOptions();
}

/**
Expand Down Expand Up @@ -133,28 +134,22 @@ public function getData()
*/
public function flushRrdCached($indexData)
{
if (false === isset($this->generalOpt['rrdcached_enabled']) ||
$this->generalOpt['rrdcached_enabled'] == 0
if (!isset($this->rrdCachedOptions['rrd_cached_option'])
|| !in_array($this->rrdCachedOptions['rrd_cached_option'], ['unix', 'tcp'])
) {
return true;
}

$errno = 0;
$errstr = '';
if (isset($this->general_opt['rrdcached_port'])
&& trim($this->general_opt['rrdcached_port']) != ''
) {
$sock = @fsockopen('127.0.0.1', trim($this->general_opt['rrdcached_port']), $errno, $errstr);
if ($sock === false) {
return false;
}
} elseif (isset($this->general_opt['rrdcached_unix_path'])
&& trim($this->general_opt['rrdcached_unix_path']) != ''
) {
$sock = @fsockopen('unix://' . trim($this->general_opt['rrdcached_unix_path']), $errno, $errstr);
if ($this->rrdCachedOptions['rrd_cached_option'] === 'tcp') {
$sock = fsockopen('127.0.0.1', trim($this->rrdCachedOptions['rrd_cached']), $errno, $errstr);
} elseif ($this->rrdCachedOptions['rrd_cached_option'] === 'unix') {
$sock = fsockopen('unix://' . trim($this->rrdCachedOptions['rrd_cached']), $errno, $errstr);
} else {
return false;
}

if (false === $sock) {
return false;
}
Expand Down Expand Up @@ -209,6 +204,30 @@ protected function getOptions()
}
return $result;
}

/**
* Get the RRDCacheD options of local RRD Broker
*
* @return array of RRDCacheD options
*/
protected function getRrdCachedOptions()
{
$result = $this->pearDB->query(
"SELECT config_key, config_value
FROM cfg_centreonbroker_info AS cbi
INNER JOIN cfg_centreonbroker AS cb ON (cb.config_id = cbi.config_id)
INNER JOIN nagios_server AS ns ON (ns.id = cb.ns_nagios_server)
WHERE ns.localhost = '1'
AND cbi.config_key IN ('rrd_cached_option', 'rrd_cached')"
);

$rrdCachedOptions = [];
while ($row = $result->fetch()) {
$this->rrdCachedOptions[$row['config_key']] = $row['config_value'];
}

return $rrdCachedOptions;
}

/**
* Get the status RRD path
Expand Down
22 changes: 0 additions & 22 deletions www/include/Administration/parameters/rrdtool/form.ihtml
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,4 @@
{/if}
{$form.hidden}
</form>
<script type="text/javascript">
{literal}
function toggleRrdcached(radio) {
var elPort = document.getElementsByName('rrdcached_port')[0];
var elUnixPath = document.getElementsByName('rrdcached_unix_path')[0];
if (radio.value == 0) {
elPort.value = '';
elUnixPath.value = '';
elPort.disabled = true;
elUnixPath.disabled = true;
} else {
elPort.disabled = false;
elUnixPath.disabled = false;
}
}

jQuery(document).ready(function () {
var checkedEl = jQuery('input:checked[type="radio"][name="rrdcached_enable[rrdcached_enable]"]')[0];
toggleRrdcached(checkedEl);
});
{/literal}
</script>
{$helptext}

0 comments on commit 6597ef4

Please sign in to comment.