From 18d75537459ed37b5dff04cd77fcb9b5585276a0 Mon Sep 17 00:00:00 2001 From: Laurent Pinsivy Date: Thu, 10 Oct 2019 16:38:49 +0200 Subject: [PATCH] fix(downtimes): apply dt on resources linked to a poller (#7955) * fix(downtime): allow to apply dt on resources linked to a poller --- .../monitoring/downtime/AddDowntime.php | 97 ++++++++++--------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/www/include/monitoring/downtime/AddDowntime.php b/www/include/monitoring/downtime/AddDowntime.php index 0a3114e86c6..33fb7808cec 100644 --- a/www/include/monitoring/downtime/AddDowntime.php +++ b/www/include/monitoring/downtime/AddDowntime.php @@ -1,6 +1,6 @@ getMyGMTFromSession(session_id(), $pearDB); $hostStr = $centreon->user->access->getHostsString("ID", $pearDBO); -$host_acl_id = preg_split('/,/', str_replace("'", "", $hostStr)); +$hostAclId = preg_split('/,/', str_replace("'", "", $hostStr)); $hObj = new CentreonHost($pearDB); $serviceObj = new CentreonService($pearDB); -$resource_id = isset($resource_id) ? $resource_id : 0; +$resourceId = $resourceId ?? 0; if (!$centreon->user->access->checkAction("host_schedule_downtime") - && !$centreon->user->access->checkAction("service_schedule_downtime")) { + && !$centreon->user->access->checkAction("service_schedule_downtime") +) { require_once _CENTREON_PATH_ . "www/include/core/errors/alt_error.php"; } else { /* @@ -74,18 +75,18 @@ $redirect = $form->addElement('hidden', 'o'); $redirect->setValue($o); - if (isset($_GET["host_id"]) && - !isset($_GET["service_id"]) + if (isset($_GET["host_id"]) + && !isset($_GET["service_id"]) ) { $disabled = "disabled"; - $host_name = $hObj->getHostName($_GET['host_id']); - } elseif (isset($_GET["host_id"]) && - isset($_GET["service_id"]) + $hostName = $hObj->getHostName($_GET['host_id']); + } elseif (isset($_GET["host_id"]) + && isset($_GET["service_id"]) ) { $disabled = "disabled"; $serviceParameters = $serviceObj->getParameters($_GET['service_id'], array('service_description')); $serviceDisplayName = $serviceParameters['service_description']; - $host_name = $hObj->getHostName($_GET['host_id']); + $hostName = $hObj->getHostName($_GET['host_id']); } else { $disabled = " "; } @@ -194,15 +195,17 @@ ); /* Host Parents */ - if (0 != $resource_id) { + if (0 !== $resourceId) { $attrPoller1 = array_merge( $attrPoller, array( 'defaultDatasetRoute' => './api/internal.php?object=centreon_configuration_poller' . - '&action=defaultValues&target=resources&field=instance_id&id=' . $resource_id) + '&action=defaultValues&target=resources&field=instance_id&id=' . $resourceId) ); $form->addElement('select2', 'poller_id', _("Pollers"), array(), $attrPoller1); + } else { + $form->addElement('select2', 'poller_id', _("Pollers"), array(), $attrPoller); } $chbx = $form->addElement( @@ -300,9 +303,9 @@ } $form->setDefaults(array('duration_scale' => $defaultScale)); - $with_services[] = $form->createElement('radio', 'with_services', null, _("Yes"), '1'); - $with_services[] = $form->createElement('radio', 'with_services', null, _("No"), '0'); - $form->addGroup($with_services, 'with_services', _("Set downtime for hosts services"), ' '); + $withServices[] = $form->createElement('radio', 'with_services', null, _("Yes"), '1'); + $withServices[] = $form->createElement('radio', 'with_services', null, _("No"), '0'); + $form->addGroup($withServices, 'with_services', _("Set downtime for hosts services"), ' '); $form->addElement('textarea', 'comment', _("Comments"), $attrsTextarea); @@ -382,19 +385,19 @@ } } - if (isset($_POST['host_or_centreon_time']['host_or_centreon_time']) && - $_POST['host_or_centreon_time']['host_or_centreon_time'] + if (isset($_POST['host_or_centreon_time']['host_or_centreon_time']) + && $_POST['host_or_centreon_time']['host_or_centreon_time'] ) { - $host_or_centreon_time = $_POST['host_or_centreon_time']['host_or_centreon_time']; + $hostOrCentreonTime = $_POST['host_or_centreon_time']['host_or_centreon_time']; } else { - $host_or_centreon_time = "0"; + $hostOrCentreonTime = "0"; } - $dt_w_services = false; + $applyDtOnServices = false; if (isset($values['with_services']) && $values['with_services']['with_services'] == 1 ) { - $dt_w_services = true; + $applyDtOnServices = true; } /* concatenating the chosen dates before sending them to ext_cmd */ @@ -421,13 +424,13 @@ $concatenatedEnd, $_POST["persistant"], $duration, - $dt_w_services, - $host_or_centreon_time + $applyDtOnServices, + $hostOrCentreonTime ); } - } elseif ($values['downtimeType']['downtimeType'] == 0 && - isset($_POST['hostgroup_id']) && - is_array($_POST['hostgroup_id']) + } elseif ($values['downtimeType']['downtimeType'] == 0 + && isset($_POST['hostgroup_id']) + && is_array($_POST['hostgroup_id']) ) { /* * Set a downtime for hostgroup @@ -436,7 +439,7 @@ foreach ($_POST['hostgroup_id'] as $hg_id) { $hostlist = $hg->getHostGroupHosts($hg_id); foreach ($hostlist as $host_id) { - if ($centreon->user->access->admin || in_array($host_id, $host_acl_id)) { + if ($centreon->user->access->admin || in_array($host_id, $hostAclId)) { $ecObj->addHostDowntime( $host_id, $_POST["comment"], @@ -444,8 +447,8 @@ $concatenatedEnd, $_POST["persistant"], $duration, - $dt_w_services, - $host_or_centreon_time + $applyDtOnServices, + $hostOrCentreonTime ); } } @@ -463,7 +466,7 @@ foreach ($_POST["service_id"] as $value) { $info = explode('-', $value); if ($centreon->user->access->admin || - in_array($info[0], $host_acl_id) + in_array($info[0], $hostAclId) ) { $ecObj->addSvcDowntime( $info[0], @@ -473,7 +476,7 @@ $concatenatedEnd, $_POST["persistant"], $duration, - $host_or_centreon_time + $hostOrCentreonTime ); } } @@ -481,10 +484,11 @@ /* * Set a downtime for a service group list */ - foreach ($_POST["servicegroup_id"] as $sg_id) { - $query = "SELECT host_id, service_id FROM services_servicegroups WHERE servicegroup_id = :sgId"; - $stmt = $pearDBO->prepare($query); - $stmt->bindValue(':sgId', $sg_id, PDO::PARAM_INT); + foreach ($_POST["servicegroup_id"] as $sgId) { + $stmt = $pearDBO->prepare( + "SELECT host_id, service_id FROM services_servicegroups WHERE servicegroup_id = :sgId" + ); + $stmt->bindValue(':sgId', $sgId, PDO::PARAM_INT); $stmt->execute(); while ($row = $stmt->fetch()) { @@ -496,7 +500,7 @@ $concatenatedEnd, $_POST["persistant"], $duration, - $host_or_centreon_time + $hostOrCentreonTime ); } } @@ -504,12 +508,15 @@ /* * Set a downtime for poller */ - foreach ($_POST['poller_id'] as $poller_id) { - $DBRESULT = $pearDBO->query("SELECT host_id FROM hosts WHERE instance_id = " . - $poller_id . "AND enabled = 1"); - while ($row = $DBRESULT->fetch()) { + foreach ($_POST['poller_id'] as $pollerId) { + $stmt = $pearDBO->prepare( + "SELECT host_id FROM hosts WHERE instance_id = :poller_id AND enabled = 1" + ); + $stmt->bindValue(':poller_id', $pollerId, PDO::PARAM_INT); + $stmt->execute(); + while ($row = $stmt->fetch()) { if ($centreon->user->access->admin || - in_array($row['host_id'], $host_acl_id) + in_array($row['host_id'], $hostAclId) ) { $ecObj->addHostDowntime( $row['host_id'], @@ -518,8 +525,8 @@ $concatenatedEnd, $_POST["persistant"], $duration, - $dt_w_services, - $host_or_centreon_time + $applyDtOnServices, + $hostOrCentreonTime ); } } @@ -545,10 +552,10 @@ if (isset($_GET['service_id']) && isset($_GET['host_id']) ) { - $tpl->assign('host_name', $host_name); + $tpl->assign('host_name', $hostName); $tpl->assign('service_description', $serviceDisplayName); } elseif (isset($_GET['host_id'])) { - $tpl->assign('host_name', $host_name); + $tpl->assign('host_name', $hostName); } $tpl->assign('seconds', _("seconds"));