Skip to content

Commit

Permalink
Merge pull request #9 from pluginsGLPI/bugfix/servicesdowntimes
Browse files Browse the repository at this point in the history
Extend downtime cancelling to associated services
  • Loading branch information
Mary-Clb authored Feb 2, 2024
2 parents 6006bb7 + 8ec7ea7 commit 6f1e445
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
39 changes: 24 additions & 15 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public function connectionRequest(array $params = [])

return $data;
}

public function diagnostic()
{
$result = [];
Expand All @@ -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]);
Expand All @@ -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 = [
Expand All @@ -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 = [])
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');
Expand Down
46 changes: 37 additions & 9 deletions src/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ public function sendCheck(int $id)
}
public function setDowntime(int $id, array $params)
{

$params['author_id'] = filter_var($params['author_id'], FILTER_VALIDATE_INT);
$params['is_fixed'] = filter_var($params['is_fixed'], FILTER_VALIDATE_BOOLEAN);
$params['with_services'] = filter_var($params['with_services'], FILTER_VALIDATE_BOOLEAN);
$params['start_time'] = $this->convertDateToIso8601($params['start_time']);
Expand All @@ -241,6 +239,7 @@ public function setDowntime(int $id, array $params)
$params['duration'] = filter_var($params['duration'], FILTER_VALIDATE_INT);
}
unset($params['time_select']);
unset($params['author_id']);
$api = new ApiClient();
$res = $api->connectionRequest();
if (isset($res["security"]["token"])) {
Expand All @@ -256,7 +255,8 @@ public function setDowntime(int $id, array $params)

public function convertDateToIso8601($date)
{
$new_date = new \DateTime($date);
$timezone = new \DateTimeZone($_SESSION['glpi_tz'] ?? date_default_timezone_get());
$new_date = new \DateTime($date, $timezone);
$iso_date = $new_date->format(DATE_ATOM);
return $iso_date;
}
Expand All @@ -281,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();
Expand Down
13 changes: 7 additions & 6 deletions templates/host.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@
<script>
$(document).ready(function(){
$('#check').click(function(){
console.log('toto');
$('.toggle').toggleClass('d-none');
$.post(CFG_GLPI['root_doc'] + "/" + GLPI_PLUGINS_PATH.centreon + "/ajax/sendCheck.php?hostid=" + {{ hostid }}, function(response){
reloadTab('');
Expand All @@ -311,20 +310,22 @@
var params = $('#setdowntime').serializeArray();
var flatparams = {};
$.each(params, function() {
if (this.name.includes('time')) {
var date = new Date(this.value);
this.value = date.toISOString();
}
flatparams[this.name] = this.value;
});
console.log(params);
$.ajax({
"type": 'POST',
"url" : CFG_GLPI['root_doc'] + "/" + GLPI_PLUGINS_PATH.centreon + "/ajax/setDowntime.php",
"data": {
"hostid": {{ hostid }},
"params": flatparams
},
"success": function(data) {
reloadTab('');
"success": function(result) {
console.log('downtime envoyé');
console.log(data);
reloadTab('');
}
});
});
Expand Down Expand Up @@ -360,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('');
Expand Down

0 comments on commit 6f1e445

Please sign in to comment.