diff --git a/src/ApiClient.php b/src/ApiClient.php index 75375a4..2cff5df 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -74,7 +74,6 @@ public function connectionRequest(array $params = []) return $data; } - public function diagnostic() { $result = []; @@ -95,8 +94,6 @@ public function diagnostic() } return $result; } - - public function clientRequest(string $endpoint = '', array $params = [], string $method = 'GET') { $api_client = new Client(['base_uri' => $this->api_config["centreon-url"], 'verify' => false]); @@ -119,7 +116,6 @@ public function clientRequest(string $endpoint = '', array $params = [], string $data = json_decode($data_body, true); return $data; } - public function getHostsList(array $params = []) { $defaults = [ @@ -131,63 +127,76 @@ public function getHostsList(array $params = []) $data = $this->clientRequest('monitoring/hosts', $params); return $data; } - public function getOneHost(int $host_id, array $params = []): array { $data = $this->clientRequest('monitoring/hosts/' . $host_id, $params); return $data; } - public function getOneHostResources(int $host_id, array $params = []): array { $data = $this->clientRequest('monitoring/resources/hosts/' . $host_id, $params); return $data; } - public function getOneHostTimeline(int $host_id, array $params = []): array { $data = $this->clientRequest('monitoring/hosts/' . $host_id . '/timeline', $params); return $data; } - public function getServicesList(array $params = []): array { $data = $this->clientRequest('monitoring/services', $params); return $data; } - - public function getServicesListForOneHost(int $host_id, array $params = []): array + public function getServicesListForOneHost(int $host_id, array $params = []) { $params['query'] = ['limit' => 30]; $data = $this->clientRequest('monitoring/hosts/' . $host_id . '/services', $params); return $data; } - public function sendCheckToAnHost(int $host_id, array $params = []) { $params['json']['is_forced'] = true; $data = $this->clientRequest('monitoring/hosts/' . $host_id . '/check', $params['json'], 'POST'); return $data; } - public function setDowntimeOnAHost(int $host_id, array $params) { $data = $this->clientRequest('monitoring/hosts/' . $host_id . '/downtimes', $params, 'POST'); return $data; } - public function listDowntimes(int $host_id, array $params = []) { $data = $this->clientRequest('monitoring/hosts/' . $host_id . '/downtimes', $params); return $data; } + public function displayDowntime(int $downtime_id): array + { + $data = $this->clientRequest('monitoring/downtimes/' . $downtime_id); + return $data; + } + public function servicesDowntimesByHost(int $host_id, array $params = []) + { + $defaultParams = [ + 'query' => [ + 'search' => json_encode([ + 'host.id' => [ + '$eq' => $host_id + ] + ]) + ] + ]; + + $queryParams = array_merge($defaultParams, $params); + + $data = $this->clientRequest('monitoring/services/downtimes', $queryParams); + return $data; + } public function cancelDowntime(int $downtime_id, array $params = []) { $data = $this->clientRequest('monitoring/downtimes/' . $downtime_id, $params, 'DELETE'); return $data; } - public function acknowledgement(int $host_id, array $request = []) { $data = $this->clientRequest('monitoring/hosts/' . $host_id . 'acknowledgements', $request, 'POST'); diff --git a/src/Host.php b/src/Host.php index bc46efd..8074b51 100644 --- a/src/Host.php +++ b/src/Host.php @@ -240,13 +240,11 @@ public function setDowntime(int $id, array $params) } unset($params['time_select']); unset($params['author_id']); - \Toolbox::logDebug($params); $api = new ApiClient(); $res = $api->connectionRequest(); if (isset($res["security"]["token"])) { try { $res = $api->setDowntimeOnAHost($id, ['json' => $params]); - \Toolbox::logDebug($res); return $res; } catch (\Exception $e) { $error_msg = $e->getMessage(); @@ -283,21 +281,49 @@ public function convertToSeconds($option, $duration) return $new_duration; } - public function cancelActualDownTime(int $downtime_id) + public function cancelActualDownTime(int $downtime_id): array { $api = new ApiClient(); $res = $api->connectionRequest(); - if (isset($res['security']['token'])) { + $error = []; + + if (isset($res["security"]["token"])) { try { - $result = $api->cancelDowntime($downtime_id); - return $result; + $actualDowntime = $api->displayDowntime($downtime_id); + $host_id = $actualDowntime['host_id']; + $start_time = $actualDowntime['start_time']; + $end_time = $actualDowntime['end_time']; + + $servicesDowntimes = $api->servicesDowntimesByHost($host_id); + foreach ($servicesDowntimes['result'] as $serviceDowntime) { + if (isset($serviceDowntime['start_time']) && isset($serviceDowntime['end_time'])) { + if ($serviceDowntime['start_time'] == $start_time && $serviceDowntime['end_time'] == $end_time) { + $s_downtime_id = $serviceDowntime['id']; + $api->cancelDowntime($s_downtime_id); + } + } else { + $error[] = [ + 'service_id' => $serviceDowntime['id'], + 'message' => 'No downtime found for this service' + ]; + } + } + $api->cancelDowntime($downtime_id); } catch (\Exception $e) { - $error_msg = $e->getMessage(); - return $error_msg; + $error[] = [ + 'message' => $e->getMessage() + ]; } + } else { + $error[] = [ + 'message' => 'Error' + ]; } + + return $error; } + public function acknowledgement(int $host_id, array $request = []) { $api = new ApiClient(); diff --git a/templates/host.html.twig b/templates/host.html.twig index 55ff19d..3d2ec05 100644 --- a/templates/host.html.twig +++ b/templates/host.html.twig @@ -361,7 +361,7 @@ "type": 'POST', "url" : CFG_GLPI['root_doc'] + "/" + GLPI_PLUGINS_PATH.centreon + "/ajax/cancelDowntime.php", "data": { - "downtimeid": {{ lastdowntimeid ?? 0 }} + "downtimeid": {{ lastdowntimeid ?? 0 }}, }, "success": function(){ reloadTab('');