From a72706e5900cf185baa8f7c6f65fa85a5f8e127a Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 18 Nov 2019 14:28:25 +0100 Subject: [PATCH 01/41] enh(metrics): add method to get custom metrics --- www/api/class/centreon_metric.class.php | 197 +++++++++++++++++++++++- www/class/centreonGraphNg.class.php | 124 ++++++++++++--- 2 files changed, 295 insertions(+), 26 deletions(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index af63c6d8c14..9d6aa81e20b 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -155,6 +155,26 @@ protected function getListByService() ); } + /** + * Get metrics datas for a services, metrics + * + * @return array + */ + public function getMetricsData() + { + if (false === isset($this->arguments['services']) && + false === isset($this->arguments['metrics']) + ) { + self::sendResult(array()); + } + + $result = array(); + $result[] = $this->metricsData( + isset($this->arguments['services']) ? $this->arguments['services'] : '', + isset($this->arguments['metrics']) ? $this->arguments['metrics'] : '' + ); + return $result; + } /** * Get metrics datas for a service @@ -214,6 +234,180 @@ public function getMetricsDataByMetric() return $result; } + /** + * Check acl for user + * + * @param int $hostId Host id to check + * @param int $serviceId Service id to check + * @param string $aclGroups String with user acl groups + * @param int $isAdmin User is admin or not + * @param int $exception Throw exception or return integer + * + * @return int + */ + private function checkAcl($hostId, $serviceId, $aclGroups, $isAdmin=1, $exception=null) + { + if (!$isAdmin) { + $query = 'SELECT service_id ' . + 'FROM centreon_acl ' . + 'WHERE host_id = :hostId ' . + 'AND service_id = :serviceId ' . + 'AND group_id IN (' . $aclGroups . ')'; + + $stmt = $this->pearDBMonitoring->prepare($query); + $stmt->bindParam(':hostId', $hostId, PDO::PARAM_INT); + $stmt->bindParam(':serviceId', $serviceId, PDO::PARAM_INT); + $dbResult = $stmt->execute(); + if (!$dbResult) { + if (!is_null($exception)) { + throw new \Exception("An error occured"); + } + return -1; + } + if (0 == $stmt->rowCount()) { + if (!is_null($exception)) { + throw new RestForbiddenException("Access denied"); + } + return -2; + } + } + + return 1; + } + + /** + * Get array builded from arguments + * + * @param string $services List of services (like hostId_serviceId,hostId2_serviceId2,...) + * @param string $metrics List of metrics (like hostId_serviceId_metricId,hostId2_serviceId2_metricId2,...) + * + * @return mixed + */ + private function manageMetricsDataArguments($services, $metrics) + { + global $centreon; + + $userId = $centreon->user->user_id; + $isAdmin = $centreon->user->admin; + $aclGroups = null; + + /* Get ACL if user is not admin */ + if (!$isAdmin) { + $acl = new CentreonACL($userId, $isAdmin); + $aclGroups = $acl->getAccessGroupsString(); + } + + if (false === isset($this->arguments['start']) || + false === is_numeric($this->arguments['start']) || + false === isset($this->arguments['end']) || + false === is_numeric($this->arguments['end']) + ) { + throw new RestBadRequestException("Bad parameters"); + } + + /* + * Format: + * { + * "hostId_serviceId": {}, # no metricsId means: all metrics + * "hostId2_serviceId2: { "metricId": 1, "metricId2": 1 } + * } + */ + $selectedMetrics = array(); + if (is_string($services) && strlen($services) !== 0) { + foreach (explode(',', $services) as $service) { + list($hostId, $serviceId) = explode('_', $service); + if (false === is_numeric($hostId) || + false === is_numeric($serviceId) + ) { + continue; + } + if (isset($selectedMetrics[$service])) { + continue; + } + + if ($this->checkAcl($hostId, $serviceId, $aclGroups, $isAdmin) <= 0) { + continue; + } + $selectedMetrics[$service] = array(); + } + } + + if (is_string($metrics) && strlen($metrics) !== 0) { + foreach (explode(',', $metrics) as $metric) { + list($hostId, $serviceId, $metricId) = explode('_', $metric); + if (false === is_numeric($hostId) || + false === is_numeric($serviceId) || + false === is_numeric($metricId) + ) { + continue; + } + + if (isset($selectedMetrics[$hostId . '_' . $serviceId]) && count($selectedMetrics[$hostId . '_' . $serviceId]) <= 0) { + continue; + } + if (!isset($selectedMetrics[$hostId . '_' . $serviceId])) { + if ($this->checkAcl($hostId, $serviceId, $aclGroups, $isAdmin) <= 0) { + continue; + } + $selectedMetrics[$hostId . '_' . $serviceId] = array(); + } + $selectedMetrics[$hostId . '_' . $serviceId][$metricId] = 1; + } + } + + return $selectedMetrics; + } + + /** + * Get data for metrics (by services and/or metrics) + * + * @param string $services List of services (like hostId_serviceId,hostId2_serviceId2,...) + * @param string $metrics List of metrics (like hostId_serviceId_metricId,hostId2_serviceId2_metricId2,...) + * + * @return array + * + * @throws Exception + * @throws RestBadRequestException + * @throws RestForbiddenException + * @throws RestNotFoundException + */ + protected function metricsData($services, $metrics) + { + global $centreon; + + $selectedMetrics = $this->manageMetricsDataArguments($services, $metrics); + $multipleServices = count(array_keys($selectedMetrics)) > 1 ? 1 : 0; + + /* Prepare graph */ + try { + $graph = new CentreonGraphNg($centreon->user->user_id); + $graph->setMultipleServices($multipleServices); + foreach ($selectedMetrics as $service => $metrics) { + list($hostId, $serviceId) = explode('_', $service); + if (count(array_keys($metrics)) <= 0) { + $graph->addServiceMetrics($hostId, $serviceId); + } else { + $graph->addServiceCustomMetrics($hostId, $serviceId, $metrics); + } + } + } catch (Exception $e) { + throw new RestNotFoundException("Graph not found"); + } + + $result = $graph->getGraph($this->arguments['start'], $this->arguments['end']); + + if ($multipleServices == 0 && count($selectedMetrics) > 0) { + /* Get extra information (downtime/acknowledgment) */ + $result['acknowledge'] = array(); + $result['downtime'] = array(); + list($hostId, $serviceId) = explode('_', array_key_first($selectedMetrics)); + $result['acknowledge'] = $this->getAcknowlegePeriods($hostId, $serviceId, $this->arguments['start'], $this->arguments['end']); + $result['downtime'] = $this->getDowntimePeriods($hostId, $serviceId, $this->arguments['start'], $this->arguments['end']); + } + + return $result; + } + /** * Get the status for a service * @@ -318,7 +512,6 @@ public function getStatusByService() 'FROM comments ' . 'WHERE host_id = :hostId ' . 'AND service_id = :serviceId ' . - 'AND type = 2 ' . 'AND entry_type = 1 ' . 'AND deletion_time IS NULL ' . 'AND :start < entry_time ' . @@ -712,7 +905,7 @@ protected function getDowntimePeriods($hostId, $serviceId, $start, $end) { $query = 'SELECT actual_start_time as start, actual_end_time as end ' . 'FROM downtimes ' . - 'WHERE type = 1 AND host_id = :hostId AND service_id = :serviceId ' . + 'WHERE host_id = :hostId AND service_id = :serviceId ' . 'AND (' . '(actual_start_time <= :end AND :end <= actual_end_time) ' . 'OR (actual_start_time <= :start AND :start <= actual_end_time) ' . diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index 44d4d8aaccd..8c31a004bbf 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -218,7 +218,9 @@ public function __construct($userId) $this->listMetricsId = array(); $this->metrics = array(); $this->vmetrics = array(); + $this->templateInformations = array(); $this->extraDatas = array(); + $this->multipleServices = 0; $stmt = $this->dbCs->prepare("SELECT RRDdatabase_path, RRDdatabase_status_path FROM config"); $stmt->execute(); @@ -244,7 +246,19 @@ public function __construct($userId) $this->rrdCachedOptions[$row['config_key']] = $row['config_value']; } } - + + /** + * Tell it will be a multiple services graph + * + * @param int $multiple set multiple value + * + * @return void + */ + public function setMultipleServices($multiple=1) + { + $this->multipleServices = $multiple; + } + /** * Get graph result * @@ -583,11 +597,12 @@ public function addServiceMetrics($hostId, $serviceId) $this->addRealMetric($metric); } - $stmt = $this->db->prepare("SELECT * - FROM virtual_metrics - WHERE index_id = :index_id - AND vmetric_activate = '1' - "); + $stmt = $this->db->prepare( + "SELECT * + FROM virtual_metrics + WHERE index_id = :index_id + AND vmetric_activate = '1'" + ); $stmt->bindParam(':index_id', $indexId, PDO::PARAM_INT); $stmt->execute(); $vmetrics = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -596,7 +611,58 @@ public function addServiceMetrics($hostId, $serviceId) $this->addVirtualMetric($vmetric); } } - + + /** + * Add metrics for a service + * + * @param int $hostId + * @param int $serviceId + * @param mixed $metrics + * + * @return void + */ + public function addServiceCustomMetrics($hostId, $serviceId, $metrics) + { + $indexId = null; + $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 i.host_id = :host_id + AND i.service_id = :service_id + AND i.id = m.index_id + AND m.hidden = '0'" + ); + $stmt->bindParam(':host_id', $hostId, PDO::PARAM_INT); + $stmt->bindParam(':service_id', $serviceId, PDO::PARAM_INT); + $stmt->execute(); + $metrics = $stmt->fetchAll(PDO::FETCH_ASSOC); + foreach ($metrics as $metric) { + $indexId = $metric['index_id']; + $this->addIndexId($metric['index_id']); + if (isset($metrics[$metric['metric_id']])) { + $this->addRealMetric($metric); + } else { + $this->addRealMetric($metric, 1); + } + } + + $stmt = $this->db->prepare( + "SELECT * + FROM virtual_metrics + WHERE index_id = :index_id + AND vmetric_activate = '1'" + ); + $stmt->bindParam(':index_id', $indexId, PDO::PARAM_INT); + $stmt->execute(); + $vmetrics = $stmt->fetchAll(PDO::FETCH_ASSOC); + + foreach ($vmetrics as $vmetric) { + $this->addVirtualMetric($vmetric); + } + } + /** * Add a metric * @@ -622,13 +688,13 @@ public function addMetric($metricId, $isVirtual = 0) if (is_null($metric)) { return; } - + $this->addIndexId($metric['index_id']); $this->addRealMetric($metric); - + return ; } - + $stmt = $this->db->prepare( "SELECT * FROM virtual_metrics @@ -641,10 +707,10 @@ public function addMetric($metricId, $isVirtual = 0) if (is_null($vmetric)) { return; } - + $this->addIndexId($vmetric['index_id']); $this->addVirtualMetric($vmetric); - + /** * Brutal: we get all vmetrics and metrics, with hidden */ @@ -677,9 +743,9 @@ private function initCurveList() $this->addArgument("DEF:v" . $metricId . "=" . $this->dbPath . $metricId . ".rrd:value:AVERAGE"); } } - + $this->manageMetrics(); - + foreach ($this->vmetricsOrder as $vmetricId) { $this->addArgument($this->vmetrics[$vmetricId]['def_type'] . ":vv" . $vmetricId . "=" . $this->vmetrics[$vmetricId]['rpn_function']); @@ -874,6 +940,9 @@ private function getServiceGraphID() */ private function getIndexData() { + if ($this->multipleServices) { + return ; + } /** * We take the first */ @@ -910,6 +979,10 @@ private function getIndexData() */ public function setTemplate($templateId = null) { + if ($this->multipleServices) { + return ; + } + if (!isset($templateId) || !$templateId) { if ($this->indexData["host_name"] != "_Module_Meta") { $this->getDefaultGraphTemplate(); @@ -1060,9 +1133,10 @@ public function getJsonStream() null, null ); + $this->extraDatas['multiple_services'] = $this->multipleServices; $this->graphData = array( 'global' => $this->extraDatas, - 'metrics' => array(), + 'metrics' => array() ); foreach ($this->metrics as $metric) { if ($metric['hidden'] == 1) { @@ -1125,19 +1199,20 @@ public function getOVDColor($indexId, $metricId) { if (is_null($this->colorCache)) { $this->colorCache = array(); - + } + if (!isset($this->colorCache[$indexId])) { $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); + $this->colorCache[$indexId] = $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[$indexId][$metricId]) + && preg_match("/^\#[a-f0-9]{6,6}/i", $this->colorCache[$indexId][$metricId]['rnd_color']) ) { - return $this->colorCache[$metricId]['rnd_color']; + return $this->colorCache[$indexId][$metricId]['rnd_color']; } $lRndcolor = $this->getRandomWebColor(); $stmt = $this->db->prepare( @@ -1147,7 +1222,7 @@ public function getOVDColor($indexId, $metricId) $stmt->bindParam(':index_id', $indexId, PDO::PARAM_INT); $stmt->bindParam(':metric_id', $metricId, PDO::PARAM_INT); $stmt->execute(); - return $l_rndcolor; + return $lRndcolor; } /** @@ -1192,8 +1267,9 @@ public function getRandomWebColor() '#ff33ff', '#ff6600', '#ff6633', '#ff6666', '#ff6699', '#ff66cc', '#ff66ff', '#ff9900', '#ff9933', '#ff9966', '#ff9999', '#ff99cc', '#ff99ff', '#ffcc00', '#ffcc33', '#ffcc66', '#ffcc99', '#ffcccc', - '#ffccff'); - return $webSafeColors[rand(0, sizeof($webSafeColors)-1)]; + '#ffccff' + ); + return $webSafeColors[rand(0, sizeof($webSafeColors)-1)]; } /** From 4be6decc408ff40fbddb23969868932f144398df Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 18 Nov 2019 14:36:08 +0100 Subject: [PATCH 02/41] enh(chart): remove dimension method result --- www/api/class/centreon_metric.class.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index 9d6aa81e20b..2cfcdd0b49e 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -168,12 +168,10 @@ public function getMetricsData() self::sendResult(array()); } - $result = array(); - $result[] = $this->metricsData( + return $this->metricsData( isset($this->arguments['services']) ? $this->arguments['services'] : '', isset($this->arguments['metrics']) ? $this->arguments['metrics'] : '' ); - return $result; } /** From bef99f0e8f3b2c6e247d13a65f32da31a5b3e39b Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 18 Nov 2019 15:22:07 +0100 Subject: [PATCH 03/41] enh(chart): add minimum_value and maximum_value --- www/class/centreonGraphNg.class.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index 8c31a004bbf..873cd371a5d 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -1077,10 +1077,23 @@ private function formatByMetrics($rrdData) $metric['prints'][] = array_values($rrdData['meta']['gprints'][$gprintsPos]); } } - + + $minimum_value = null; + $maximum_value = null; for ($j = 0; $j < $size; $j++) { $metric['data'][] = $rrdData['data'][$j][$i]; + if (!is_null($rrdData['data'][$j][$i]) && + (is_null($minimum_value) || $rrdData['data'][$j][$i] < $minimum_value)) { + $minimum_value = $rrdData['data'][$j][$i]; + } + if (!is_null($rrdData['data'][$j][$i]) && + (is_null($maximum_value) || $rrdData['data'][$j][$i] > $maximum_value)) { + $maximum_value = $rrdData['data'][$j][$i]; + } } + + $metric['minimum_value'] = $minimum_value; + $metric['maximum_value'] = $maximum_value; $i++; } } From df4bd28524218147ce22512911d582368918daee Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Tue, 19 Nov 2019 08:20:30 +0100 Subject: [PATCH 04/41] feat(api): add endpoint to get widget preferences (json format) --- www/api/class/centreon_customview.class.php | 152 ------------------ .../class/centreon_home_customview.class.php | 21 +++ 2 files changed, 21 insertions(+), 152 deletions(-) delete mode 100644 www/api/class/centreon_customview.class.php diff --git a/www/api/class/centreon_customview.class.php b/www/api/class/centreon_customview.class.php deleted file mode 100644 index 2c0cb4c7717..00000000000 --- a/www/api/class/centreon_customview.class.php +++ /dev/null @@ -1,152 +0,0 @@ -. - * - * Linking this program statically or dynamically with other modules is making a - * combined work based on this program. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this program give Centreon - * permission to link this program with independent modules to produce an executable, - * regardless of the license terms of these independent modules, and to copy and - * distribute the resulting executable under terms of Centreon choice, provided that - * Centreon also meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module which is not - * derived from this program. If you modify this program, you may extend this - * exception to your version of the program, but you are not obliged to do so. If you - * do not wish to do so, delete this exception statement from your version. - * - * For more information : contact@centreon.com - * - */ - -require_once dirname(__FILE__) . "/webService.class.php"; -require_once _CENTREON_PATH_ . 'www/class/centreonCustomView.class.php'; - -class CentreonHomeCustomview extends CentreonWebService -{ - /** - * CentreonHomeCustomview constructor. - */ - public function __construct() - { - parent::__construct(); - } - - /** - * @return array - */ - public function getListSharedViews() - { - global $centreon; - - $views = array(); - - $query = 'SELECT custom_view_id, name FROM (' . - 'SELECT cv.custom_view_id, cv.name FROM custom_views cv ' . - 'INNER JOIN custom_view_user_relation cvur ON cv.custom_view_id = cvur.custom_view_id ' . - 'WHERE (cvur.user_id = ' . $centreon->user->user_id . ' ' . - 'OR cvur.usergroup_id IN ( ' . - 'SELECT contactgroup_cg_id ' . - 'FROM contactgroup_contact_relation ' . - 'WHERE contact_contact_id = ' . $centreon->user->user_id . ' ' . - ') ' . - ') ' . - 'UNION ' . - 'SELECT cv2.custom_view_id, cv2.name FROM custom_views cv2 ' . - 'WHERE cv2.public = 1 ) as d ' . - 'WHERE d.custom_view_id NOT IN (' . - 'SELECT cvur2.custom_view_id FROM custom_view_user_relation cvur2 ' . - 'WHERE cvur2.user_id = ' . $centreon->user->user_id . ' ' . - 'AND cvur2.is_consumed = 1) '; - - $dbResult = $this->pearDB->query($query); - while ($row = $dbResult->fetch()) { - $views[] = array( - 'id' => $row['custom_view_id'], - 'text' => $row['name'] - ); - } - return array( - 'items' => $views, - 'total' => count($views) - ); - } - - /** - * @return array - */ - public function getLinkedUsers() - { - // Check for select2 'q' argument - if (false === isset($this->arguments['q'])) { - $customViewId = 0; - } else { - $customViewId = $this->arguments['q']; - } - - global $centreon; - $viewObj = new CentreonCustomView($centreon, $this->pearDB); - - return $viewObj->getUsersFromViewId($customViewId); - } - - /** - * @return array - */ - public function getLinkedUsergroups() - { - // Check for select2 'q' argument - if (false === isset($this->arguments['q'])) { - $customViewId = 0; - } else { - $customViewId = $this->arguments['q']; - } - - global $centreon; - $viewObj = new CentreonCustomView($centreon, $this->pearDB); - - return $viewObj->getUsergroupsFromViewId($customViewId); - } - - /** - * Get the list of views - * - * @return array - */ - public function getListViews() - { - global $centreon; - $viewObj = new CentreonCustomView($centreon, $this->pearDB); - - $tabs = array(); - $tabsDb = $viewObj->getCustomViews(); - foreach ($tabsDb as $key => $tab) { - $tabs[] = array( - 'default' => false, - 'name' => $tab['name'], - 'custom_view_id' => $tab['custom_view_id'], - 'public' => $tab['public'], - 'nbCols' => $tab['layout'] - ); - } - - return array( - 'current' => $viewObj->getCurrentView(), - 'tabs' => $tabs - ); - } -} diff --git a/www/api/class/centreon_home_customview.class.php b/www/api/class/centreon_home_customview.class.php index 077edc93fd0..c931ab30688 100644 --- a/www/api/class/centreon_home_customview.class.php +++ b/www/api/class/centreon_home_customview.class.php @@ -321,6 +321,27 @@ public function getPreferences() return $tpl->fetch("widgetParam.html"); } + /** + * Get preferences by widget id + * + * @return array The widget preferences + * @throws \Exception When missing argument + */ + public function getPreferencesByWidgetId() + { + global $centreon; + + if (!isset($this->arguments['widgetId'])) { + throw new \Exception('Missing argument : widgetId'); + } + $widgetId = $this->arguments['widgetId']; + $widgetObj = new CentreonWidget($centreon, $this->pearDB); + + $preferences = $widgetObj->getWidgetPreferences($widgetId); + + return $preferences; + } + /** * Authorize to access to the action * From a801ec921717c67374c932151f2676b55706ebb1 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 19 Nov 2019 11:00:27 +0100 Subject: [PATCH 05/41] enh(chart): add acks --- www/api/class/centreon_metric.class.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index 2cfcdd0b49e..af66e555c0d 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -399,7 +399,7 @@ protected function metricsData($services, $metrics) $result['acknowledge'] = array(); $result['downtime'] = array(); list($hostId, $serviceId) = explode('_', array_key_first($selectedMetrics)); - $result['acknowledge'] = $this->getAcknowlegePeriods($hostId, $serviceId, $this->arguments['start'], $this->arguments['end']); + $result['acknowledge'] = $this->getAcknowlegements($hostId, $serviceId, $this->arguments['start'], $this->arguments['end']); $result['downtime'] = $this->getDowntimePeriods($hostId, $serviceId, $this->arguments['start'], $this->arguments['end']); } @@ -864,6 +864,27 @@ protected function convertNaN($element) return $element; } + /** + * Get the list of a acknowlegments for a service during a period + * + * @return array The list of ack + */ + protected function getAcknowlegements($hostId, $serviceId, $start, $end) + { + $query = 'SELECT entry_time as start, deletion_time as end, author, comment_data ' . + 'FROM acknowledgements ' . + 'WHERE host_id = :hostId ' . + 'AND service_id = :serviceId ' . + 'AND (entry_time >= :start AND entry_time <= :end)'; + $stmt = $this->pearDBMonitoring->prepare($query); + $stmt->bindParam(':hostId', $hostId, PDO::PARAM_INT); + $stmt->bindParam(':serviceId', $serviceId, PDO::PARAM_INT); + $stmt->bindParam(':start', $start, PDO::PARAM_INT); + $stmt->bindParam(':end', $end, PDO::PARAM_INT); + $stmt->execute(); + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } + /** * Get the list of a acknowlegment for a service during a period * From 4ce52d10be9b8dc1b884824ba2ac9f91f26af64d Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 19 Nov 2019 11:50:24 +0100 Subject: [PATCH 06/41] fix(chart): downtimes sql request --- www/api/class/centreon_metric.class.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index af66e555c0d..d9c0d6e5562 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -925,12 +925,8 @@ protected function getDowntimePeriods($hostId, $serviceId, $start, $end) $query = 'SELECT actual_start_time as start, actual_end_time as end ' . 'FROM downtimes ' . 'WHERE host_id = :hostId AND service_id = :serviceId ' . - 'AND (' . - '(actual_start_time <= :end AND :end <= actual_end_time) ' . - 'OR (actual_start_time <= :start AND :start <= actual_end_time) ' . - 'OR (actual_start_time >= :start AND :end >= actual_end_time) ' . - 'OR (actual_start_time IS NOT NULL AND actual_end_time IS NULL) ' . - ')'; + 'AND ' . + 'actual_start_time <= :end AND (actual_end_time >= :start OR actual_end_time is NULL)'; $queryValues['hostId'] = (int)$hostId; $queryValues['serviceId'] = (int)$serviceId; $queryValues['end'] = (int)$end; @@ -950,7 +946,7 @@ protected function executeQueryPeriods($query, $start, $end, $queryValues) { $periods = array(); $stmt = $this->pearDBMonitoring->prepare($query); - foreach ($queryValues as $key => $value) { + foreach ($queryValues as $key => &$value) { $stmt->bindParam(':' . $key, $value, PDO::PARAM_INT); } $dbResult = $stmt->execute(); From 07d5286c72d3dcd1db63267a2388f5f8f04e1728 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 19 Nov 2019 13:39:13 +0100 Subject: [PATCH 07/41] enh(chart): change metrics get parameters format --- www/api/class/centreon_metric.class.php | 39 ++++++++++++++++--------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index d9c0d6e5562..aadeb9c1795 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -277,7 +277,7 @@ private function checkAcl($hostId, $serviceId, $aclGroups, $isAdmin=1, $exceptio * Get array builded from arguments * * @param string $services List of services (like hostId_serviceId,hostId2_serviceId2,...) - * @param string $metrics List of metrics (like hostId_serviceId_metricId,hostId2_serviceId2_metricId2,...) + * @param string $metrics List of metrics (like metricId,metricId2,...) * * @return mixed */ @@ -331,25 +331,38 @@ private function manageMetricsDataArguments($services, $metrics) } if (is_string($metrics) && strlen($metrics) !== 0) { - foreach (explode(',', $metrics) as $metric) { - list($hostId, $serviceId, $metricId) = explode('_', $metric); - if (false === is_numeric($hostId) || - false === is_numeric($serviceId) || - false === is_numeric($metricId) - ) { + $filter = ''; + $filter_append = ''; + foreach (explode(',', $metrics) as $metricId) { + if (false === is_numeric($metricId)) { continue; } + $filter .= $filter_append . $metricId; + $filter_append = ','; + } + + if ($filter === '') { + return $selectedMetrics; + } + + $results = $this->pearDBMonitoring->query( + 'SELECT + metric_id, host_id, service_id + FROM metrics, index_data + WHERE metrics.metric_id IN (' . $filter . ') AND metrics.index_id = index_data.id' + ); - if (isset($selectedMetrics[$hostId . '_' . $serviceId]) && count($selectedMetrics[$hostId . '_' . $serviceId]) <= 0) { + foreach ($results as $row) { + if (isset($selectedMetrics[$row['host_id'] . '_' . $row['service_id']]) && count($selectedMetrics[$row['host_id'] . '_' . $row['service_id']]) <= 0) { continue; } - if (!isset($selectedMetrics[$hostId . '_' . $serviceId])) { - if ($this->checkAcl($hostId, $serviceId, $aclGroups, $isAdmin) <= 0) { + if (!isset($selectedMetrics[$row['host_id'] . '_' . $row['service_id']])) { + if ($this->checkAcl($row['host_id'], $row['service_id'], $aclGroups, $isAdmin) <= 0) { continue; } - $selectedMetrics[$hostId . '_' . $serviceId] = array(); + $selectedMetrics[$row['host_id'] . '_' . $row['service_id']] = array(); } - $selectedMetrics[$hostId . '_' . $serviceId][$metricId] = 1; + $selectedMetrics[$row['host_id'] . '_' . $row['service_id']][$row['metric_id']] = 1; } } @@ -360,7 +373,7 @@ private function manageMetricsDataArguments($services, $metrics) * Get data for metrics (by services and/or metrics) * * @param string $services List of services (like hostId_serviceId,hostId2_serviceId2,...) - * @param string $metrics List of metrics (like hostId_serviceId_metricId,hostId2_serviceId2_metricId2,...) + * @param string $metrics List of metrics (like metricId,metricId2,...) * * @return array * From 3b5d470461d90ee904111af3aaa3a8d0f2280193 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 19 Nov 2019 16:34:43 +0100 Subject: [PATCH 08/41] enh(chart): add getLastMetricsData method --- www/api/class/centreon_metric.class.php | 113 ++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index aadeb9c1795..df465fa8d81 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -155,6 +155,26 @@ protected function getListByService() ); } + /** + * Get last metrics value + * + * @return array + */ + public function getLastMetricsData() + { + if (false === isset($this->arguments['services']) && + false === isset($this->arguments['metrics']) + ) { + self::sendResult(array()); + } + + return $this->lastMetricsData( + isset($this->arguments['services']) ? $this->arguments['services'] : '', + isset($this->arguments['metrics']) ? $this->arguments['metrics'] : '' + ); + } + + /** * Get metrics datas for a services, metrics * @@ -369,6 +389,99 @@ private function manageMetricsDataArguments($services, $metrics) return $selectedMetrics; } + /** + * Get last data for metrics (by services and/or metrics) + * + * @param string $services List of services (like hostId_serviceId,hostId2_serviceId2,...) + * @param string $metrics List of metrics (like metricId,metricId2,...) + * + * @return array + * + * @throws Exception + * @throws RestBadRequestException + * @throws RestForbiddenException + * @throws RestNotFoundException + */ + protected function lastMetricsData($services, $metrics) + { + global $centreon; + + $userId = $centreon->user->user_id; + $isAdmin = $centreon->user->admin; + $aclGroups = null; + + $query = ''; + if (is_string($services) && strlen($services) !== 0) { + $filterHostIds = ''; + $filterHostIdsAppend = ''; + $filterServiceIds = ''; + $filterServiceIdsAppend = ''; + foreach (explode(',', $services) as $service) { + list($hostId, $serviceId) = explode('_', $service); + if (false === is_numeric($hostId) || + false === is_numeric($serviceId) + ) { + continue; + } + + $filterHostIds .= $filterHostIdsAppend . $hostId; + $filterServiceIds .= $filterServiceIdsAppend . $serviceId; + $filterHostIdsAppend = ','; + $filterServiceIdsAppend = ','; + } + + if ($filterHostIds !== '') { + $query = ' + SELECT i.host_id, i.service_id, m.* + FROM index_data i, metrics m + WHERE i.host_id IN (' . $filterHostIds . ') AND i.service_id IN (' . $filterServiceIds . ') AND i.id = m.index_id'; + } + } + + if (is_string($metrics) && strlen($metrics) !== 0) { + $filterMetricIds = ''; + $filterMetricAppend = ''; + foreach (explode(',', $metrics) as $metricId) { + if (false === is_numeric($metricId)) { + continue; + } + $filterMetricIds .= $filterMetricAppend . $metricId; + $filterMetricAppend = ','; + } + + if ($filterMetricIds !== '') { + if ($query !== '') { + $query .= ' UNION '; + } + $query .= ' + SELECT i.host_id, i.service_id, m.* + FROM metrics m, index_data i + WHERE m.metric_id IN (' . $filterMetricIds . ') AND m.index_id = i.id'; + } + } + + if ($subquery === '') { + throw new \Exception("No metrics found"); + } + + /* Get ACL if user is not admin */ + if (!$isAdmin) { + $acl = new CentreonACL($userId, $isAdmin); + $aclGroups = $acl->getAccessGroupsString(); + $query = ' + SELECT ms.* FROM (' . $query . ') as ms, centreon_acl ca + WHERE EXISTS ( + SELECT 1 + FROM centreon_acl ca + WHERE ca.host_id = ms.host_id + AND ca.service_id = ms.service_id + AND ca.group_id IN (' . $aclGroups . '))'; + } + + $res = $this->pearDBMonitoring->query($query); + return $res->fetchAll(PDO::FETCH_ASSOC); + } + /** * Get data for metrics (by services and/or metrics) * From a3dfa920c98e306d472425be506a29abd61ee35d Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 20 Nov 2019 15:54:17 +0100 Subject: [PATCH 09/41] fix(chart): options --- www/class/centreonGraphNg.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index 873cd371a5d..e850fd7fef4 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -617,11 +617,11 @@ public function addServiceMetrics($hostId, $serviceId) * * @param int $hostId * @param int $serviceId - * @param mixed $metrics + * @param mixed $metricsSelected * * @return void */ - public function addServiceCustomMetrics($hostId, $serviceId, $metrics) + public function addServiceCustomMetrics($hostId, $serviceId, $metricsSelected) { $indexId = null; $stmt = $this->dbCs->prepare( @@ -641,7 +641,7 @@ public function addServiceCustomMetrics($hostId, $serviceId, $metrics) foreach ($metrics as $metric) { $indexId = $metric['index_id']; $this->addIndexId($metric['index_id']); - if (isset($metrics[$metric['metric_id']])) { + if (isset($metricsSelected[$metric['metric_id']])) { $this->addRealMetric($metric); } else { $this->addRealMetric($metric, 1); From 65e3764382956ed3446274c6fab1388aacf9a7c7 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Thu, 21 Nov 2019 10:25:52 +0100 Subject: [PATCH 10/41] fix metrics getter in select2 --- www/class/centreonMetrics.class.php | 38 +++++++++---------- .../Params/Connector/MetricMulti.class.php | 8 ++-- www/include/home/customViews/widgetParam.html | 2 +- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/www/class/centreonMetrics.class.php b/www/class/centreonMetrics.class.php index d185b47048c..fa42d4ad28f 100644 --- a/www/class/centreonMetrics.class.php +++ b/www/class/centreonMetrics.class.php @@ -42,31 +42,30 @@ public function __construct($db) * @param array $values * @return array */ - public function getObjectForSelect2($values = array()) + public function getObjectForSelect2($values = []) { - $metrics = array(); - $filters = ''; + $metrics = []; $listValues = ''; - $queryValues = array(); + $queryValues = []; if (!empty($values)) { - foreach ($values as $k => $v) { - $listValues .= ':metric' . $v . ','; - $queryValues['metric' . $v] = (int)$v; + foreach ($values as $v) { + $multiValues = explode(',', $v); + foreach ($multiValues as $item) { + $listValues .= ':metric' . $item . ','; + $queryValues['metric' . $item] = (int)$item; + } } $listValues = rtrim($listValues, ','); - $filters .= 'm.metric_id IN (' . $listValues . ') AND'; } else { - $filters .= '""'; + $listValues = '""'; } - $queryService = "SELECT SQL_CALC_FOUND_ROWS m.metric_id, CONCAT(h.name,' - ', s.description," - . "' - ', m.metric_name) AS fullname " - . "FROM metrics m, hosts h, services s, index_data i " - . "WHERE " - . $filters . " " - . "i.id = m.index_id AND " - . "h.host_id = i.host_id " - . "AND s.service_id = i.service_id " + + $queryService = "SELECT SQL_CALC_FOUND_ROWS m.metric_id, CONCAT(i.host_name,' - ', i.service_description," + . "' - ', m.metric_name) AS fullname " + . "FROM metrics m, index_data i " + . "WHERE m.metric_id IN (" . $listValues . ") " + . "AND i.id = m.index_id " . "ORDER BY fullname COLLATE utf8_general_ci"; $stmt = $this->dbo->prepare($queryService); @@ -78,11 +77,12 @@ public function getObjectForSelect2($values = array()) $stmt->execute(); while ($row = $stmt->fetch()) { - $metrics[] = array( + $metrics[] = [ 'id' => $row['metric_id'], 'text' => $row['fullname'] - ); + ]; } + return $metrics; } } diff --git a/www/class/centreonWidget/Params/Connector/MetricMulti.class.php b/www/class/centreonWidget/Params/Connector/MetricMulti.class.php index a782370ebbc..b62dc896be5 100644 --- a/www/class/centreonWidget/Params/Connector/MetricMulti.class.php +++ b/www/class/centreonWidget/Params/Connector/MetricMulti.class.php @@ -44,12 +44,12 @@ public function __construct($db, $quickform, $userId) public function getParameters() { - $path = './include/common/webServices/rest/internal.php?object=centreon_metric&action=listByService'; - return array( + $path = './api/internal.php?object=centreon_metric&action=listByService'; + return [ 'datasourceOrigin' => 'ajax', 'availableDatasetRoute' => $path, 'multiple' => true, - 'linkedObject' => 'centreonMetrics' - ); + 'linkedObject' => 'centreonMetrics', + ]; } } diff --git a/www/include/home/customViews/widgetParam.html b/www/include/home/customViews/widgetParam.html index 649c5bd7228..0bf8cb54354 100644 --- a/www/include/home/customViews/widgetParam.html +++ b/www/include/home/customViews/widgetParam.html @@ -1,7 +1,7 @@ {$form.javascript}{$javascript}
- +

{$form.header.title}

From 6f6a60920a7ccace4fc2e8088d222860e2c778f1 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Fri, 22 Nov 2019 15:14:38 +0100 Subject: [PATCH 11/41] enh(chart): manage min/max for invert ds --- www/class/centreonGraphNg.class.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index e850fd7fef4..ca811c3861f 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -489,7 +489,16 @@ private function addRealMetric($metric, $hidden = null) 'max' => $metric['max'], 'virtual' => 0, ); - + + if (isset($tm['ds_data']['ds_invert']) && $tm['ds_data']['ds_invert']) { + if (!is_null($this->metrics[$metric['metric_id']]['min']) && is_numeric($this->metrics[$metric['metric_id']]['min'])) { + $this->metrics[$metric['metric_id']]['min'] = $metric['min'] * -1; + } + if (!is_null($this->metrics[$metric['metric_id']]['max']) && is_numeric($this->metrics[$metric['metric_id']]['max'])) { + $this->metrics[$metric['metric_id']]['max'] = $metric['max'] * -1; + } + } + $this->cacheAllMetrics['r:' . $metric["metric_name"]] = $metric["metric_id"]; $dsData = $this->getCurveDsConfig($metric); From 4a031da2be69ee4f6dfebc0618aeac81edad0541 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Fri, 22 Nov 2019 15:15:06 +0100 Subject: [PATCH 12/41] retrieve proper zindex for select2 --- www/Themes/Centreon-2/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/Themes/Centreon-2/style.css b/www/Themes/Centreon-2/style.css index e0b65c1daf8..7bf1db18f55 100755 --- a/www/Themes/Centreon-2/style.css +++ b/www/Themes/Centreon-2/style.css @@ -1915,7 +1915,7 @@ span.state_badge { /* Style for popin */ .centreon-popin { position: absolute; - z-index: 5050; + z-index: 1065; background-color: white; padding: 1em 3em 1em 1em; border-radius: 4px; From a61133abc61a147e9d981da15bb56a525fd7ad65 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Fri, 22 Nov 2019 15:17:31 +0100 Subject: [PATCH 13/41] enh(chart): manage min/max for invert ds --- www/class/centreonGraphNg.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index ca811c3861f..d4c06e9a034 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -490,7 +490,7 @@ private function addRealMetric($metric, $hidden = null) 'virtual' => 0, ); - if (isset($tm['ds_data']['ds_invert']) && $tm['ds_data']['ds_invert']) { + if (isset($dsData['ds_invert']) && $dsData['ds_invert']) { if (!is_null($this->metrics[$metric['metric_id']]['min']) && is_numeric($this->metrics[$metric['metric_id']]['min'])) { $this->metrics[$metric['metric_id']]['min'] = $metric['min'] * -1; } From b90b0f9045b3caf4b32db187b8dd5b75e77f9044 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Fri, 22 Nov 2019 15:19:16 +0100 Subject: [PATCH 14/41] enh(chart): manage min/max for invert ds --- www/class/centreonGraphNg.class.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index d4c06e9a034..f2800d1cab4 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -490,15 +490,6 @@ private function addRealMetric($metric, $hidden = null) 'virtual' => 0, ); - if (isset($dsData['ds_invert']) && $dsData['ds_invert']) { - if (!is_null($this->metrics[$metric['metric_id']]['min']) && is_numeric($this->metrics[$metric['metric_id']]['min'])) { - $this->metrics[$metric['metric_id']]['min'] = $metric['min'] * -1; - } - if (!is_null($this->metrics[$metric['metric_id']]['max']) && is_numeric($this->metrics[$metric['metric_id']]['max'])) { - $this->metrics[$metric['metric_id']]['max'] = $metric['max'] * -1; - } - } - $this->cacheAllMetrics['r:' . $metric["metric_name"]] = $metric["metric_id"]; $dsData = $this->getCurveDsConfig($metric); @@ -524,6 +515,15 @@ private function addRealMetric($metric, $hidden = null) (isset($dsData["ds_order"]) && $dsData["ds_order"] ? $dsData["ds_order"] : 0); $this->metrics[$metric['metric_id']]['hidden'] = is_null($hidden) ? 0 : $hidden; + + if (isset($dsData['ds_invert']) && $dsData['ds_invert']) { + if (!is_null($this->metrics[$metric['metric_id']]['min']) && is_numeric($this->metrics[$metric['metric_id']]['min'])) { + $this->metrics[$metric['metric_id']]['min'] = $metric['min'] * -1; + } + if (!is_null($this->metrics[$metric['metric_id']]['max']) && is_numeric($this->metrics[$metric['metric_id']]['max'])) { + $this->metrics[$metric['metric_id']]['max'] = $metric['max'] * -1; + } + } } /** From 2cb2ff81d997ab1813ef6d437181284d92cd1de5 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Fri, 20 Dec 2019 08:53:54 +0100 Subject: [PATCH 15/41] Update centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index df465fa8d81..7c9f4fdd7d2 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -162,10 +162,8 @@ protected function getListByService() */ public function getLastMetricsData() { - if (false === isset($this->arguments['services']) && - false === isset($this->arguments['metrics']) - ) { - self::sendResult(array()); + if (!isset($this->arguments['services']) && !isset($this->arguments['metrics'])) { + self::sendResult([]); } return $this->lastMetricsData( @@ -182,10 +180,8 @@ public function getLastMetricsData() */ public function getMetricsData() { - if (false === isset($this->arguments['services']) && - false === isset($this->arguments['metrics']) - ) { - self::sendResult(array()); + if (!isset($this->arguments['services']) && !isset($this->arguments['metrics'])) { + self::sendResult([]); } return $this->metricsData( @@ -201,8 +197,8 @@ public function getMetricsData() */ public function getMetricsDataByService() { - if (false === isset($this->arguments['ids'])) { - self::sendResult(array()); + if (!isset($this->arguments['ids'])) { + self::sendResult([]); } /* Get the list of service ID */ From 9364857076854c0058a9a28b14d7f5b88176a483 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Fri, 20 Dec 2019 13:35:40 +0100 Subject: [PATCH 16/41] improve style --- www/api/class/centreon_metric.class.php | 168 ++++++++++++------------ 1 file changed, 84 insertions(+), 84 deletions(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index 7c9f4fdd7d2..b6a7b57104f 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -38,7 +38,7 @@ require_once _CENTREON_PATH_ . "/www/class/centreonGraphService.class.php"; require_once _CENTREON_PATH_ . "/www/class/centreonGraphPoller.class.php"; require_once _CENTREON_PATH_ . "/www/class/centreonGraphStatus.class.php"; -require_once dirname(__FILE__) . "/webService.class.php"; +require_once __DIR__ . "/webService.class.php"; class CentreonMetric extends CentreonWebService { @@ -156,7 +156,7 @@ protected function getListByService() } /** - * Get last metrics value + * Get last metrics value by services or/and metrics * * @return array */ @@ -174,7 +174,7 @@ public function getLastMetricsData() /** - * Get metrics datas for a services, metrics + * Get metrics data by service or/and metrics * * @return array */ @@ -203,7 +203,7 @@ public function getMetricsDataByService() /* Get the list of service ID */ $ids = explode(',', $this->arguments['ids']); - $result = array(); + $result = []; if (isset($this->arguments['type']) && $this->arguments['type'] === 'ng') { foreach ($ids as $id) { @@ -250,16 +250,15 @@ public function getMetricsDataByMetric() /** * Check acl for user - * + * * @param int $hostId Host id to check * @param int $serviceId Service id to check * @param string $aclGroups String with user acl groups * @param int $isAdmin User is admin or not - * @param int $exception Throw exception or return integer * - * @return int + * @return bool if the user is allowed to get service information */ - private function checkAcl($hostId, $serviceId, $aclGroups, $isAdmin=1, $exception=null) + private function checkAcl($hostId, $serviceId, $aclGroups, $isAdmin = true): bool { if (!$isAdmin) { $query = 'SELECT service_id ' . @@ -272,26 +271,17 @@ private function checkAcl($hostId, $serviceId, $aclGroups, $isAdmin=1, $exceptio $stmt->bindParam(':hostId', $hostId, PDO::PARAM_INT); $stmt->bindParam(':serviceId', $serviceId, PDO::PARAM_INT); $dbResult = $stmt->execute(); - if (!$dbResult) { - if (!is_null($exception)) { - throw new \Exception("An error occured"); - } - return -1; - } - if (0 == $stmt->rowCount()) { - if (!is_null($exception)) { - throw new RestForbiddenException("Access denied"); - } - return -2; + if (!$dbResult || $stmt->rowCount() === 0) { + return false; } } - return 1; + return true; } /** * Get array builded from arguments - * + * * @param string $services List of services (like hostId_serviceId,hostId2_serviceId2,...) * @param string $metrics List of metrics (like metricId,metricId2,...) * @@ -311,10 +301,8 @@ private function manageMetricsDataArguments($services, $metrics) $aclGroups = $acl->getAccessGroupsString(); } - if (false === isset($this->arguments['start']) || - false === is_numeric($this->arguments['start']) || - false === isset($this->arguments['end']) || - false === is_numeric($this->arguments['end']) + if (!isset($this->arguments['start']) || !is_numeric($this->arguments['start']) + || !isset($this->arguments['end']) || !is_numeric($this->arguments['end']) ) { throw new RestBadRequestException("Bad parameters"); } @@ -326,35 +314,33 @@ private function manageMetricsDataArguments($services, $metrics) * "hostId2_serviceId2: { "metricId": 1, "metricId2": 1 } * } */ - $selectedMetrics = array(); - if (is_string($services) && strlen($services) !== 0) { + $selectedMetrics = []; + if (is_string($services) && strlen($services) > 0) { foreach (explode(',', $services) as $service) { list($hostId, $serviceId) = explode('_', $service); - if (false === is_numeric($hostId) || - false === is_numeric($serviceId) - ) { + if (!is_numeric($hostId) || !is_numeric($serviceId)) { continue; } if (isset($selectedMetrics[$service])) { continue; } - if ($this->checkAcl($hostId, $serviceId, $aclGroups, $isAdmin) <= 0) { + if (!$this->checkAcl($hostId, $serviceId, $aclGroups, $isAdmin)) { continue; } - $selectedMetrics[$service] = array(); + $selectedMetrics[$service] = []; } } - if (is_string($metrics) && strlen($metrics) !== 0) { + if (is_string($metrics) && strlen($metrics) > 0) { $filter = ''; - $filter_append = ''; + $filterAppend = ''; foreach (explode(',', $metrics) as $metricId) { - if (false === is_numeric($metricId)) { + if (!is_numeric($metricId)) { continue; } - $filter .= $filter_append . $metricId; - $filter_append = ','; + $filter .= $filterAppend . $metricId; + $filterAppend = ','; } if ($filter === '') { @@ -362,18 +348,19 @@ private function manageMetricsDataArguments($services, $metrics) } $results = $this->pearDBMonitoring->query( - 'SELECT - metric_id, host_id, service_id + 'SELECT metric_id, host_id, service_id FROM metrics, index_data WHERE metrics.metric_id IN (' . $filter . ') AND metrics.index_id = index_data.id' ); foreach ($results as $row) { - if (isset($selectedMetrics[$row['host_id'] . '_' . $row['service_id']]) && count($selectedMetrics[$row['host_id'] . '_' . $row['service_id']]) <= 0) { + if (isset($selectedMetrics[$row['host_id'] . '_' . $row['service_id']]) + && count($selectedMetrics[$row['host_id'] . '_' . $row['service_id']]) <= 0 + ) { continue; } if (!isset($selectedMetrics[$row['host_id'] . '_' . $row['service_id']])) { - if ($this->checkAcl($row['host_id'], $row['service_id'], $aclGroups, $isAdmin) <= 0) { + if (!$this->checkAcl($row['host_id'], $row['service_id'], $aclGroups, $isAdmin)) { continue; } $selectedMetrics[$row['host_id'] . '_' . $row['service_id']] = array(); @@ -407,19 +394,18 @@ protected function lastMetricsData($services, $metrics) $aclGroups = null; $query = ''; - if (is_string($services) && strlen($services) !== 0) { + if (is_string($services) && strlen($services) > 0) { $filterHostIds = ''; $filterHostIdsAppend = ''; $filterServiceIds = ''; $filterServiceIdsAppend = ''; foreach (explode(',', $services) as $service) { list($hostId, $serviceId) = explode('_', $service); - if (false === is_numeric($hostId) || - false === is_numeric($serviceId) + if (!is_numeric($hostId) || !is_numeric($serviceId) ) { continue; } - + $filterHostIds .= $filterHostIdsAppend . $hostId; $filterServiceIds .= $filterServiceIdsAppend . $serviceId; $filterHostIdsAppend = ','; @@ -428,17 +414,17 @@ protected function lastMetricsData($services, $metrics) if ($filterHostIds !== '') { $query = ' - SELECT i.host_id, i.service_id, m.* - FROM index_data i, metrics m + SELECT i.host_id, i.service_id, m.* + FROM index_data i, metrics m WHERE i.host_id IN (' . $filterHostIds . ') AND i.service_id IN (' . $filterServiceIds . ') AND i.id = m.index_id'; } } - if (is_string($metrics) && strlen($metrics) !== 0) { + if (is_string($metrics) && strlen($metrics) > 0) { $filterMetricIds = ''; $filterMetricAppend = ''; foreach (explode(',', $metrics) as $metricId) { - if (false === is_numeric($metricId)) { + if (!is_numeric($metricId)) { continue; } $filterMetricIds .= $filterMetricAppend . $metricId; @@ -456,7 +442,7 @@ protected function lastMetricsData($services, $metrics) } } - if ($subquery === '') { + if ($query === '') { throw new \Exception("No metrics found"); } @@ -465,13 +451,13 @@ protected function lastMetricsData($services, $metrics) $acl = new CentreonACL($userId, $isAdmin); $aclGroups = $acl->getAccessGroupsString(); $query = ' - SELECT ms.* FROM (' . $query . ') as ms, centreon_acl ca + SELECT ms.* FROM (' . $query . ') as ms, centreon_acl ca WHERE EXISTS ( - SELECT 1 + SELECT 1 FROM centreon_acl ca WHERE ca.host_id = ms.host_id AND ca.service_id = ms.service_id - AND ca.group_id IN (' . $aclGroups . '))'; + AND ca.group_id IN (' . $aclGroups . '))'; } $res = $this->pearDBMonitoring->query($query); @@ -521,8 +507,18 @@ protected function metricsData($services, $metrics) $result['acknowledge'] = array(); $result['downtime'] = array(); list($hostId, $serviceId) = explode('_', array_key_first($selectedMetrics)); - $result['acknowledge'] = $this->getAcknowlegements($hostId, $serviceId, $this->arguments['start'], $this->arguments['end']); - $result['downtime'] = $this->getDowntimePeriods($hostId, $serviceId, $this->arguments['start'], $this->arguments['end']); + $result['acknowledge'] = $this->getAcknowledgements( + (int)$hostId, + (int)$serviceId, + (int)$this->arguments['start'], + (int)$this->arguments['end'] + ); + $result['downtime'] = $this->getDowntimePeriods( + (int)$hostId, + (int)$serviceId, + (int)$this->arguments['start'], + (int)$this->arguments['end'] + ); } return $result; @@ -551,10 +547,8 @@ public function getStatusByService() } /* Validate options */ - if (false === isset($this->arguments['start']) || - false === is_numeric($this->arguments['start']) || - false === isset($this->arguments['end']) || - false === is_numeric($this->arguments['end']) + if (!isset($this->arguments['start']) || !is_numeric($this->arguments['start']) + || !isset($this->arguments['end']) || !is_numeric($this->arguments['end']) ) { throw new RestBadRequestException("Bad parameters"); } @@ -606,7 +600,7 @@ public function getStatusByService() throw new \Exception("An error occured"); } - if (0 == $stmt->rowCount()) { + if ($stmt->rowCount() === 0) { throw new RestForbiddenException("Access denied"); } } @@ -627,11 +621,12 @@ public function getStatusByService() $query = 'SELECT `value` FROM `options` WHERE `key` = "display_comment_chart"'; $res = $this->pearDB->query($query); $row = $res->fetch(); - if (false === is_null($row) && $row['value'] === '1') { + if (!is_null($row) && $row['value'] === '1') { $queryComment = 'SELECT `entry_time`, `author`, `data` ' . 'FROM comments ' . 'WHERE host_id = :hostId ' . 'AND service_id = :serviceId ' . + 'AND type = 2 ' . 'AND entry_type = 1 ' . 'AND deletion_time IS NULL ' . 'AND :start < entry_time ' . @@ -692,10 +687,8 @@ protected function serviceDatasNg($id, $metric = null) $aclGroups = $acl->getAccessGroupsString(); } - if (false === isset($this->arguments['start']) || - false === is_numeric($this->arguments['start']) || - false === isset($this->arguments['end']) || - false === is_numeric($this->arguments['end']) + if (!isset($this->arguments['start']) || !is_numeric($this->arguments['start']) + || !isset($this->arguments['end']) || is_numeric($this->arguments['end']) ) { throw new RestBadRequestException("Bad parameters"); } @@ -725,7 +718,7 @@ protected function serviceDatasNg($id, $metric = null) if (!$dbResult) { throw new \Exception("An error occured"); } - if (0 == $stmt->rowCount()) { + if ($stmt->rowCount() === 0) { throw new RestForbiddenException("Access denied"); } } @@ -741,7 +734,7 @@ protected function serviceDatasNg($id, $metric = null) } catch (Exception $e) { throw new RestNotFoundException("Graph not found"); } - + $result = $graph->getGraph($this->arguments['start'], $this->arguments['end']); /* Get extra information (downtime/acknowledgment) */ @@ -750,9 +743,9 @@ protected function serviceDatasNg($id, $metric = null) $query = 'SELECT `value` FROM `options` WHERE `key` = "display_downtime_chart"'; $res = $this->pearDB->query($query); - + $row = $res->fetch(); - if (false === is_null($row) && $row['value'] === '1') { + if (!is_null($row) && $row['value'] === '1') { $result['acknowledge'] = $this->getAcknowlegePeriods($hostId, $serviceId, $start, $end); $result['downtime'] = $this->getDowntimePeriods($hostId, $serviceId, $start, $end); } @@ -831,7 +824,7 @@ protected function serviceDatas($id, $metric = null) if (!$dbResult) { throw new \Exception("An error occured"); } - if (0 == $stmt->rowCount()) { + if ($stmt->rowCount() === 0) { throw new RestForbiddenException("Access denied"); } } @@ -964,7 +957,7 @@ public function getMetricsDataByPoller() } catch (\Exception $e) { throw new RestNotFoundException("Graph not found"); } - + $result = $graphPollerObject->getGraph($start, $end); return array($result); @@ -989,9 +982,14 @@ protected function convertNaN($element) /** * Get the list of a acknowlegments for a service during a period * - * @return array The list of ack + * @param int $hostId the host id + * @param int $serviceId the service id + * @param int $start the start timestamp + * @param int $end the end timestamp + * + * @return array The list of acknowledgements */ - protected function getAcknowlegements($hostId, $serviceId, $start, $end) + protected function getAcknowledgements(int $hostId, int $serviceId, int $start, int $end) { $query = 'SELECT entry_time as start, deletion_time as end, author, comment_data ' . 'FROM acknowledgements ' . @@ -999,10 +997,10 @@ protected function getAcknowlegements($hostId, $serviceId, $start, $end) 'AND service_id = :serviceId ' . 'AND (entry_time >= :start AND entry_time <= :end)'; $stmt = $this->pearDBMonitoring->prepare($query); - $stmt->bindParam(':hostId', $hostId, PDO::PARAM_INT); - $stmt->bindParam(':serviceId', $serviceId, PDO::PARAM_INT); - $stmt->bindParam(':start', $start, PDO::PARAM_INT); - $stmt->bindParam(':end', $end, PDO::PARAM_INT); + $stmt->bindValue(':hostId', $hostId, PDO::PARAM_INT); + $stmt->bindValue(':serviceId', $serviceId, PDO::PARAM_INT); + $stmt->bindValue(':start', $start, PDO::PARAM_INT); + $stmt->bindValue(':end', $end, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } @@ -1035,10 +1033,12 @@ protected function getAcknowlegePeriods($hostId, $serviceId, $start, $end) } /** - * @param int $hostId - * @param int $serviceId - * @param int $start - * @param int $end + * Get list of downtime periods by service + * + * @param int $hostId the host id + * @param int $serviceId the service id + * @param int $start the start timestamp + * @param int $end the end timestamp * * @return array */ @@ -1068,15 +1068,15 @@ protected function executeQueryPeriods($query, $start, $end, $queryValues) { $periods = array(); $stmt = $this->pearDBMonitoring->prepare($query); - foreach ($queryValues as $key => &$value) { - $stmt->bindParam(':' . $key, $value, PDO::PARAM_INT); + foreach ($queryValues as $key => $value) { + $stmt->bindValue(':' . $key, $value, PDO::PARAM_INT); } $dbResult = $stmt->execute(); if (!$dbResult) { throw new \Exception("An error occured"); } - while ($row = $stmt->fetchRow()) { + while ($row = $stmt->fetch()) { $period = array( 'start' => $row['start'], 'end' => $row['end'] From 484004916b485b2682584c1c3b01b78f4250d227 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Fri, 20 Dec 2019 14:05:46 +0100 Subject: [PATCH 17/41] add prepared statements --- www/api/class/centreon_metric.class.php | 62 +++++++++++++++---------- www/class/centreonGraphNg.class.php | 52 ++++++++++----------- www/class/centreonMetrics.class.php | 4 +- 3 files changed, 66 insertions(+), 52 deletions(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index b6a7b57104f..cd1e771e143 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -335,11 +335,13 @@ private function manageMetricsDataArguments($services, $metrics) if (is_string($metrics) && strlen($metrics) > 0) { $filter = ''; $filterAppend = ''; + $queryValues = []; foreach (explode(',', $metrics) as $metricId) { if (!is_numeric($metricId)) { continue; } - $filter .= $filterAppend . $metricId; + $filter .= $filterAppend . ' :metric' . $metricId; + $queryValues[':metric' . $metricId] = $metricId; $filterAppend = ','; } @@ -347,13 +349,20 @@ private function manageMetricsDataArguments($services, $metrics) return $selectedMetrics; } - $results = $this->pearDBMonitoring->query( + + + $stmt = $this->pearDBMonitoring->prepare( 'SELECT metric_id, host_id, service_id FROM metrics, index_data - WHERE metrics.metric_id IN (' . $filter . ') AND metrics.index_id = index_data.id' + WHERE metrics.metric_id IN (' . $filter. ') + AND metrics.index_id = index_data.id' ); + foreach ($queryValues as $param => $value) { + $stmt->bindValue($param, $value, PDO::PARAM_INT); + } + $stmt->execute(); - foreach ($results as $row) { + while ($row = $stmt->fetch()) { if (isset($selectedMetrics[$row['host_id'] . '_' . $row['service_id']]) && count($selectedMetrics[$row['host_id'] . '_' . $row['service_id']]) <= 0 ) { @@ -394,51 +403,47 @@ protected function lastMetricsData($services, $metrics) $aclGroups = null; $query = ''; + $filterHostIds = []; + $filterServiceIds = []; + $filterMetricIds = []; + if (is_string($services) && strlen($services) > 0) { - $filterHostIds = ''; - $filterHostIdsAppend = ''; - $filterServiceIds = ''; - $filterServiceIdsAppend = ''; foreach (explode(',', $services) as $service) { list($hostId, $serviceId) = explode('_', $service); - if (!is_numeric($hostId) || !is_numeric($serviceId) - ) { + if (!is_numeric($hostId) || !is_numeric($serviceId)) { continue; } - - $filterHostIds .= $filterHostIdsAppend . $hostId; - $filterServiceIds .= $filterServiceIdsAppend . $serviceId; - $filterHostIdsAppend = ','; - $filterServiceIdsAppend = ','; + $filterHostIds[':host' . $hostId] = $hostId; + $filterServiceIds[':service' . $serviceId] = $serviceId; } - if ($filterHostIds !== '') { + if (!empty($filterHostIds) && !empty($filterServiceIds)) { $query = ' SELECT i.host_id, i.service_id, m.* FROM index_data i, metrics m - WHERE i.host_id IN (' . $filterHostIds . ') AND i.service_id IN (' . $filterServiceIds . ') AND i.id = m.index_id'; + WHERE i.host_id IN (' . implode(',', array_keys($filterHostIds)) . ') + AND i.service_id IN (' . implode(',', array_keys($filterServiceIds)) . ') + AND i.id = m.index_id'; } } if (is_string($metrics) && strlen($metrics) > 0) { - $filterMetricIds = ''; - $filterMetricAppend = ''; foreach (explode(',', $metrics) as $metricId) { if (!is_numeric($metricId)) { continue; } - $filterMetricIds .= $filterMetricAppend . $metricId; - $filterMetricAppend = ','; + $filterMetricIds[':metric' . $metricId] = $metricId; } - if ($filterMetricIds !== '') { + if (!empty($filterMetricIds)) { if ($query !== '') { $query .= ' UNION '; } $query .= ' SELECT i.host_id, i.service_id, m.* FROM metrics m, index_data i - WHERE m.metric_id IN (' . $filterMetricIds . ') AND m.index_id = i.id'; + WHERE m.metric_id IN (' . implode(',', array_keys($filterMetricIds)) . ') + AND m.index_id = i.id'; } } @@ -460,8 +465,15 @@ protected function lastMetricsData($services, $metrics) AND ca.group_id IN (' . $aclGroups . '))'; } - $res = $this->pearDBMonitoring->query($query); - return $res->fetchAll(PDO::FETCH_ASSOC); + $stmt = $this->pearDBMonitoring->prepare($query); + foreach ([$filterHostIds, $filterServiceIds, $filterMetricIds] as $filterParams) { + foreach ($filterParams as $param => $value) { + $stmt->bindValue($param, $value, PDO::PARAM_INT); + } + } + $stmt->execute(); + + return $stmt->fetchAll(PDO::FETCH_ASSOC); } /** diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index f2800d1cab4..9284b4e3e4c 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -48,7 +48,7 @@ class MetricUtils { private static $instance = null; - + /** * Constructor * @@ -57,7 +57,7 @@ class MetricUtils private function __construct() { } - + /** * Singleton create method * @@ -634,13 +634,13 @@ public function addServiceCustomMetrics($hostId, $serviceId, $metricsSelected) { $indexId = null; $stmt = $this->dbCs->prepare( - "SELECT - m.index_id, host_id, service_id, metric_id, metric_name, + "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 i.host_id = :host_id AND i.service_id = :service_id - AND i.id = m.index_id + AND i.id = m.index_id AND m.hidden = '0'" ); $stmt->bindParam(':host_id', $hostId, PDO::PARAM_INT); @@ -666,7 +666,7 @@ public function addServiceCustomMetrics($hostId, $serviceId, $metricsSelected) $stmt->bindParam(':index_id', $indexId, PDO::PARAM_INT); $stmt->execute(); $vmetrics = $stmt->fetchAll(PDO::FETCH_ASSOC); - + foreach ($vmetrics as $vmetric) { $this->addVirtualMetric($vmetric); } @@ -1043,13 +1043,13 @@ public function setRRDOption($name, $value = null) /** * Parse rrdtool result * - * @param mixed $rrdData + * @param array $rrdData * * @return void */ private function formatByMetrics($rrdData) { - $this->graphData['times'] = array(); + $this->graphData['times'] = []; $size = (is_array($rrdData['data']) || $rrdData['data'] instanceof \Countable) ? count($rrdData['data']) @@ -1058,17 +1058,17 @@ private function formatByMetrics($rrdData) $gprintsSize = (is_array($rrdData['meta']['gprints']) || $rrdData['meta']['gprints'] instanceof \Countable) ? count($rrdData['meta']['gprints']) : 0; - + for ($i = 0; $i < $size; $i++) { $this->graphData['times'][] = $rrdData['data'][$i][0]; } - - $i = 1; + + $metricIndex = 1; $gprintsPos = 0; foreach ($this->graphData['metrics'] as &$metric) { $metric['data'] = array(); $metric['prints'] = array(); - + $insert = 0; if ($metric['virtual'] == 0) { $metricFullname = 'v' . $metric['metric_id']; @@ -1087,23 +1087,23 @@ private function formatByMetrics($rrdData) } } - $minimum_value = null; - $maximum_value = null; - for ($j = 0; $j < $size; $j++) { - $metric['data'][] = $rrdData['data'][$j][$i]; - if (!is_null($rrdData['data'][$j][$i]) && - (is_null($minimum_value) || $rrdData['data'][$j][$i] < $minimum_value)) { - $minimum_value = $rrdData['data'][$j][$i]; + $minimumValue = null; + $maximumValue = null; + for ($dataIndex = 0; $dataIndex < $size; $dataIndex++) { + $metric['data'][] = $rrdData['data'][$dataIndex][$metricIndex]; + if (!is_null($rrdData['data'][$dataIndex][$metricIndex]) && + (is_null($minimumValue) || $rrdData['data'][$dataIndex][$metricIndex] < $minimumValue)) { + $minimumValue = $rrdData['data'][$dataIndex][$metricIndex]; } - if (!is_null($rrdData['data'][$j][$i]) && - (is_null($maximum_value) || $rrdData['data'][$j][$i] > $maximum_value)) { - $maximum_value = $rrdData['data'][$j][$i]; + if (!is_null($rrdData['data'][$dataIndex][$metricIndex]) && + (is_null($maximumValue) || $rrdData['data'][$dataIndex][$metricIndex] > $maximumValue)) { + $maximumValue = $rrdData['data'][$dataIndex][$metricIndex]; } } - - $metric['minimum_value'] = $minimum_value; - $metric['maximum_value'] = $maximum_value; - $i++; + + $metric['minimum_value'] = $minimumValue; + $metric['maximum_value'] = $maximumValue; + $metricIndex++; } } diff --git a/www/class/centreonMetrics.class.php b/www/class/centreonMetrics.class.php index fa42d4ad28f..b7b8ecd90bd 100644 --- a/www/class/centreonMetrics.class.php +++ b/www/class/centreonMetrics.class.php @@ -39,7 +39,9 @@ public function __construct($db) } /** - * @param array $values + * Get metrics information from ids to populat select2 + * + * @param array $values list of metric ids * @return array */ public function getObjectForSelect2($values = []) From 17ef3503d5e2882547539777dc7c600ba2def99c Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Fri, 20 Dec 2019 17:30:29 +0100 Subject: [PATCH 18/41] fix status webservice --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index cd1e771e143..a218e86f1e8 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -700,7 +700,7 @@ protected function serviceDatasNg($id, $metric = null) } if (!isset($this->arguments['start']) || !is_numeric($this->arguments['start']) - || !isset($this->arguments['end']) || is_numeric($this->arguments['end']) + || !isset($this->arguments['end']) || !is_numeric($this->arguments['end']) ) { throw new RestBadRequestException("Bad parameters"); } From b86b238fc592062e726c7d3c5f345441c31cfb8f Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:32:50 +0100 Subject: [PATCH 19/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index a218e86f1e8..0dcb5b675ac 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -167,7 +167,7 @@ public function getLastMetricsData() } return $this->lastMetricsData( - isset($this->arguments['services']) ? $this->arguments['services'] : '', + $this->arguments['services'] ?? '', isset($this->arguments['metrics']) ? $this->arguments['metrics'] : '' ); } From faed08c812dceadb03af34610cb918edcc1c46a3 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:33:05 +0100 Subject: [PATCH 20/41] Update www/class/centreonGraphNg.class.php --- www/class/centreonGraphNg.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index 9284b4e3e4c..735980f1bc0 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -1291,7 +1291,7 @@ public function getRandomWebColor() '#ff99ff', '#ffcc00', '#ffcc33', '#ffcc66', '#ffcc99', '#ffcccc', '#ffccff' ); - return $webSafeColors[rand(0, sizeof($webSafeColors)-1)]; + return $webSafeColors[rand(0, sizeof($webSafeColors) - 1)]; } /** From 3cd8c717d49e028b4a3f1c040563978b33a4f153 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:33:19 +0100 Subject: [PATCH 21/41] Update www/class/centreonGraphNg.class.php --- www/class/centreonGraphNg.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index 735980f1bc0..e9cfe76bac5 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -1220,7 +1220,7 @@ public function checkArgument($name, $tab, $defaultValue) public function getOVDColor($indexId, $metricId) { if (is_null($this->colorCache)) { - $this->colorCache = array(); + $this->colorCache = []; } if (!isset($this->colorCache[$indexId])) { $stmt = $this->db->prepare( From 0a585e0a6b49ad1f43ffbd5215ee5f0208ba6b3c Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:33:35 +0100 Subject: [PATCH 22/41] Update www/class/centreonGraphNg.class.php --- www/class/centreonGraphNg.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index e9cfe76bac5..fcfafd9593a 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -1158,7 +1158,7 @@ public function getJsonStream() $this->extraDatas['multiple_services'] = $this->multipleServices; $this->graphData = array( 'global' => $this->extraDatas, - 'metrics' => array() + 'metrics' => [] ); foreach ($this->metrics as $metric) { if ($metric['hidden'] == 1) { From a4a1c97b7ca0190974117bd2662ef0d26ee69067 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:33:44 +0100 Subject: [PATCH 23/41] Update www/class/centreonGraphNg.class.php --- www/class/centreonGraphNg.class.php | 1 + 1 file changed, 1 insertion(+) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index fcfafd9593a..eceac441f49 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -653,6 +653,7 @@ public function addServiceCustomMetrics($hostId, $serviceId, $metricsSelected) if (isset($metricsSelected[$metric['metric_id']])) { $this->addRealMetric($metric); } else { + // this metric will be hidden $this->addRealMetric($metric, 1); } } From fc99d5b41025467150d6039b7fde469c44ad0164 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:34:08 +0100 Subject: [PATCH 24/41] Update www/class/centreonGraphNg.class.php --- www/class/centreonGraphNg.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index eceac441f49..8a7a263b20f 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -630,7 +630,7 @@ public function addServiceMetrics($hostId, $serviceId) * * @return void */ - public function addServiceCustomMetrics($hostId, $serviceId, $metricsSelected) + public function addServiceCustomMetrics($hostId, $serviceId, $metricsSelected): void { $indexId = null; $stmt = $this->dbCs->prepare( From 05af1497f2f6ee4a13531778355401c76ae4ccca Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:34:21 +0100 Subject: [PATCH 25/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index 0dcb5b675ac..312a166c024 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -176,7 +176,7 @@ public function getLastMetricsData() /** * Get metrics data by service or/and metrics * - * @return array + * @return array | null if arguments are not set */ public function getMetricsData() { From c0681ae00d4fdffa594a72ac50005ba9114c19e5 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:34:32 +0100 Subject: [PATCH 26/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index 312a166c024..7d8228a29f0 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -186,7 +186,7 @@ public function getMetricsData() return $this->metricsData( isset($this->arguments['services']) ? $this->arguments['services'] : '', - isset($this->arguments['metrics']) ? $this->arguments['metrics'] : '' + $this->arguments['metrics'] ?? '' ); } From 82ed074dc08b54d266ea27fe44d329fad8ea3932 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:34:41 +0100 Subject: [PATCH 27/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index 7d8228a29f0..f3844b5941d 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -185,7 +185,7 @@ public function getMetricsData() } return $this->metricsData( - isset($this->arguments['services']) ? $this->arguments['services'] : '', + $this->arguments['services'] ?? '', $this->arguments['metrics'] ?? '' ); } From be0aeb78fa30048c1a9bb954c25c2cf2a33097d8 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:34:49 +0100 Subject: [PATCH 28/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index f3844b5941d..b0c8d86b71c 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -158,7 +158,7 @@ protected function getListByService() /** * Get last metrics value by services or/and metrics * - * @return array + * @return array | null if arguments are not set */ public function getLastMetricsData() { From ad25b18fefaa7175be1be4cc383cda5e84b04613 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:34:58 +0100 Subject: [PATCH 29/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index b0c8d86b71c..02649a19b10 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -168,7 +168,7 @@ public function getLastMetricsData() return $this->lastMetricsData( $this->arguments['services'] ?? '', - isset($this->arguments['metrics']) ? $this->arguments['metrics'] : '' + $this->arguments['metrics'] ?? '' ); } From 390714db470e095b75946a33347d814795b417d0 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:35:17 +0100 Subject: [PATCH 30/41] Update www/class/centreonGraphNg.class.php --- www/class/centreonGraphNg.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index 8a7a263b20f..baf281902ec 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -220,7 +220,7 @@ public function __construct($userId) $this->vmetrics = array(); $this->templateInformations = array(); $this->extraDatas = array(); - $this->multipleServices = 0; + $this->multipleServices = false; $stmt = $this->dbCs->prepare("SELECT RRDdatabase_path, RRDdatabase_status_path FROM config"); $stmt->execute(); From b64ec2b095d6ee3bfd32fc38260b4b89c8b425f4 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:35:28 +0100 Subject: [PATCH 31/41] Update www/class/centreonGraphNg.class.php --- www/class/centreonGraphNg.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index baf281902ec..6b466b19448 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -254,7 +254,7 @@ public function __construct($userId) * * @return void */ - public function setMultipleServices($multiple=1) + public function setMultipleServices($multiple) { $this->multipleServices = $multiple; } From 7038c2bc462cdc5eb32bbe7685d47288656b424a Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:35:54 +0100 Subject: [PATCH 32/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index 02649a19b10..25ed83dc652 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -494,7 +494,7 @@ protected function metricsData($services, $metrics) global $centreon; $selectedMetrics = $this->manageMetricsDataArguments($services, $metrics); - $multipleServices = count(array_keys($selectedMetrics)) > 1 ? 1 : 0; + $multipleServices = count(array_keys($selectedMetrics)) > 1 ? true : false; /* Prepare graph */ try { From b1b3c74e00f68f704c40774cfaf5950223bf29f2 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:36:21 +0100 Subject: [PATCH 33/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index 25ed83dc652..d5650c791ef 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -514,7 +514,7 @@ protected function metricsData($services, $metrics) $result = $graph->getGraph($this->arguments['start'], $this->arguments['end']); - if ($multipleServices == 0 && count($selectedMetrics) > 0) { + if (!$multipleServices && count($selectedMetrics) > 0) { /* Get extra information (downtime/acknowledgment) */ $result['acknowledge'] = array(); $result['downtime'] = array(); From e01b94f8d167c7df89af1e221302b95ada22a802 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:36:33 +0100 Subject: [PATCH 34/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index d5650c791ef..e01fa8a3faa 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -1001,7 +1001,7 @@ protected function convertNaN($element) * * @return array The list of acknowledgements */ - protected function getAcknowledgements(int $hostId, int $serviceId, int $start, int $end) + protected function getAcknowledgements(int $hostId, int $serviceId, int $start, int $end): array { $query = 'SELECT entry_time as start, deletion_time as end, author, comment_data ' . 'FROM acknowledgements ' . From 5a66f07fd93003aa0daa2142d2f56aab379636f9 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:36:46 +0100 Subject: [PATCH 35/41] Update www/api/class/centreon_metric.class.php --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index e01fa8a3faa..a39b02caf4a 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -1054,7 +1054,7 @@ protected function getAcknowlegePeriods($hostId, $serviceId, $start, $end) * * @return array */ - protected function getDowntimePeriods($hostId, $serviceId, $start, $end) + protected function getDowntimePeriods($hostId, $serviceId, $start, $end): array { $query = 'SELECT actual_start_time as start, actual_end_time as end ' . 'FROM downtimes ' . From 843f8693ab93634015dce89432f746c877ad9cc2 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:36:57 +0100 Subject: [PATCH 36/41] Update www/class/centreonGraphNg.class.php --- www/class/centreonGraphNg.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/class/centreonGraphNg.class.php b/www/class/centreonGraphNg.class.php index 6b466b19448..930ea63fe65 100644 --- a/www/class/centreonGraphNg.class.php +++ b/www/class/centreonGraphNg.class.php @@ -248,7 +248,7 @@ public function __construct($userId) } /** - * Tell it will be a multiple services graph + * Set if a graph has multiple services * * @param int $multiple set multiple value * From 291b79e7c372c9d73b6c7629f8d19458551bd236 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:37:22 +0100 Subject: [PATCH 37/41] Update www/api/class/centreon_home_customview.class.php Co-Authored-By: loiclau --- www/api/class/centreon_home_customview.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_home_customview.class.php b/www/api/class/centreon_home_customview.class.php index c931ab30688..26e628f1de7 100644 --- a/www/api/class/centreon_home_customview.class.php +++ b/www/api/class/centreon_home_customview.class.php @@ -337,7 +337,7 @@ public function getPreferencesByWidgetId() $widgetId = $this->arguments['widgetId']; $widgetObj = new CentreonWidget($centreon, $this->pearDB); - $preferences = $widgetObj->getWidgetPreferences($widgetId); + return $widgetObj->getWidgetPreferences($widgetId); return $preferences; } From 0dd8aeb7c7a8613e5f94c7e0c1532a87d12e16a8 Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:37:30 +0100 Subject: [PATCH 38/41] Update www/api/class/centreon_home_customview.class.php Co-Authored-By: loiclau --- www/api/class/centreon_home_customview.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/www/api/class/centreon_home_customview.class.php b/www/api/class/centreon_home_customview.class.php index 26e628f1de7..a6cda674a06 100644 --- a/www/api/class/centreon_home_customview.class.php +++ b/www/api/class/centreon_home_customview.class.php @@ -339,7 +339,6 @@ public function getPreferencesByWidgetId() return $widgetObj->getWidgetPreferences($widgetId); - return $preferences; } /** From 83345ff690719aad4d561b47787d99eae435ac1f Mon Sep 17 00:00:00 2001 From: sc979 <34628915+sc979@users.noreply.github.com> Date: Thu, 2 Jan 2020 11:37:37 +0100 Subject: [PATCH 39/41] Update www/api/class/centreon_home_customview.class.php Co-Authored-By: loiclau --- www/api/class/centreon_home_customview.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/www/api/class/centreon_home_customview.class.php b/www/api/class/centreon_home_customview.class.php index a6cda674a06..9e910509872 100644 --- a/www/api/class/centreon_home_customview.class.php +++ b/www/api/class/centreon_home_customview.class.php @@ -338,7 +338,6 @@ public function getPreferencesByWidgetId() $widgetObj = new CentreonWidget($centreon, $this->pearDB); return $widgetObj->getWidgetPreferences($widgetId); - } /** From 84030620f97c6377a055382864c475e968d7e1c9 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 14 Jan 2020 16:09:15 +0100 Subject: [PATCH 40/41] code review solves --- www/api/class/centreon_metric.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index a39b02caf4a..0822204cd05 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -258,7 +258,7 @@ public function getMetricsDataByMetric() * * @return bool if the user is allowed to get service information */ - private function checkAcl($hostId, $serviceId, $aclGroups, $isAdmin = true): bool + private function checkAcl($hostId, $serviceId, ?array $aclGroups, $isAdmin = true): bool { if (!$isAdmin) { $query = 'SELECT service_id ' . From 416b16df242475617c89ab27809ad1d6c8f0ae5c Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Mon, 20 Jan 2020 17:18:40 +0100 Subject: [PATCH 41/41] check sql result properly --- www/api/class/centreon_metric.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/www/api/class/centreon_metric.class.php b/www/api/class/centreon_metric.class.php index 0822204cd05..30553881ba6 100644 --- a/www/api/class/centreon_metric.class.php +++ b/www/api/class/centreon_metric.class.php @@ -633,7 +633,7 @@ public function getStatusByService() $query = 'SELECT `value` FROM `options` WHERE `key` = "display_comment_chart"'; $res = $this->pearDB->query($query); $row = $res->fetch(); - if (!is_null($row) && $row['value'] === '1') { + if ($row && $row['value'] === '1') { $queryComment = 'SELECT `entry_time`, `author`, `data` ' . 'FROM comments ' . 'WHERE host_id = :hostId ' . @@ -757,7 +757,7 @@ protected function serviceDatasNg($id, $metric = null) $res = $this->pearDB->query($query); $row = $res->fetch(); - if (!is_null($row) && $row['value'] === '1') { + if ($row && $row['value'] === '1') { $result['acknowledge'] = $this->getAcknowlegePeriods($hostId, $serviceId, $start, $end); $result['downtime'] = $this->getDowntimePeriods($hostId, $serviceId, $start, $end); }