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

C2 API - Add and List downtimes #5623

Closed
wants to merge 16 commits into from
61 changes: 53 additions & 8 deletions lib/Centreon/Object/Downtime/RtDowntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
*/

require_once "Centreon/Object/Object.php";

/**
* Used for interacting with downtime objects
*
Expand All @@ -43,6 +42,7 @@
class Centreon_Object_RtDowntime extends Centreon_Object
{
protected $table = "downtimes";
protected $name = "downtime_name";
protected $primaryKey = "downtime_id";
protected $uniqueLabelField = "comment_data";

Expand All @@ -52,15 +52,60 @@ public function __construct()
$this->db = Centreon_Db_Manager::factory('storage');
}

public function getRunningDowntimes()
/**
* @param array $hostList
* @return array
*/
public function getHostDowntimes($hostList = array())
{
$hostFilter = '';

// Remplace les pipe par des virgules pour la requêtes SQL
if (!empty($hostList)) {
$hostFilter = "AND h.name IN ('" . implode("','", $hostList) . "') ";
}

$query = "SELECT name, author, actual_start_time , end_time, comment_data, duration, fixed " .
"FROM downtimes d, hosts h " .
"WHERE d.host_id = h.host_id " .
"AND d.cancelled = 0 " .
"AND d.actual_end_time IS NULL " .
"AND service_id IS NULL " .
$hostFilter .
"ORDER BY actual_start_time";

return $this->getResult($query, array(), "fetchAll");
}

/**
* @param array $svcList
* @return array
*/
public function getSvcDowntimes($svcList = array())
{
$query = "SELECT author, actual_start_time , end_time, comment_data, duration, fixed " .
"FROM downtimes " .
"WHERE service_id IS NULL " .
"AND cancelled = 0 " .
"AND started = 1 " .
"AND actual_end_time IS NULL " .
$serviceFilter = '';

if (!empty($svcList)) {
$serviceFilter = 'AND (';
$filterTab = array();
for ($i = 0; $i < count($svcList); $i+=2) {
$hostname = $svcList[$i];
$serviceDescription = $svcList[$i + 1];
$filterTab[] = '(h.name = "' . $hostname . '" AND s.description = "' . $serviceDescription . '")';
}
$serviceFilter .= implode(' AND ', $filterTab) . ') ';
}

$query = "SELECT h.name, s.description, d.service_id, author, actual_start_time , end_time, comment_data, duration, fixed " .
"FROM downtimes d, hosts h, services s " .
"WHERE d.service_id = s.service_id " .
"AND d.host_id = s.host_id " .
"AND s.host_id = h.host_id " .
"AND d.cancelled = 0 " .
$serviceFilter .
"AND d.actual_end_time IS NULL " .
"ORDER BY actual_start_time";

return $this->getResult($query, array(), "fetchAll");
}
}
187 changes: 119 additions & 68 deletions www/class/centreon-clapi/centreonRtDowntime.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
require_once "Centreon/Object/Relation/Downtime/Host.php";
require_once "Centreon/Object/Relation/Downtime/Hostgroup.php";
require_once "Centreon/Object/Relation/Downtime/Servicegroup.php";
require_once realpath(dirname(__FILE__) . '/../centreonExternalCommand.class.php');
require_once realpath(dirname(__FILE__) . '/../centreonDB.class.php');
require_once realpath(dirname(__FILE__) . '/../centreonUser.class.php');
require_once realpath(dirname(__FILE__) . '/../centreonGMT.class.php');
require_once realpath(dirname(__FILE__) . '/../centreonHostgroups.class.php');
require_once realpath(dirname(__FILE__) . '/../centreonServicegroups.class.php');
require_once realpath(dirname(__FILE__) . '/../centreonInstance.class.php');
require_once realpath(dirname(__FILE__).'/../centreonExternalCommand.class.php');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style, add space

require_once realpath(dirname(__FILE__).'/../centreonDB.class.php');
require_once realpath(dirname(__FILE__).'/../centreonUser.class.php');
require_once realpath(dirname(__FILE__).'/../centreonGMT.class.php');
require_once realpath(dirname(__FILE__).'/../centreonHostgroups.class.php');
require_once realpath(dirname(__FILE__).'/../centreonServicegroups.class.php');
require_once realpath(dirname(__FILE__).'/../centreonInstance.class.php');

class CentreonRtDowntime extends CentreonObject
{
Expand All @@ -63,53 +63,26 @@ class CentreonRtDowntime extends CentreonObject
'SVC',
'HG',
'SG',
'INSTANCE'
'INSTANCE',
);

/**
* @var
*/
protected $pearDBMonitoring;

/**
* CentreonRtDowntime constructor.
*/
public function __construct()
{
parent::__construct();
$this->object = new \Centreon_Object_RtDowntime();
$db = new \CentreonDB('centreon');
$this->hgObject = new \CentreonHostgroups($db);
$this->sgObject = new \CentreonServiceGroups($db);
$this->instanceObject = new \CentreonInstance($db);
$this->db = new \CentreonDB('centreon');
$this->hgObject = new \CentreonHostgroups($this->db);
$this->sgObject = new \CentreonServiceGroups($this->db);
$this->instanceObject = new \CentreonInstance($this->db);
$this->externalCmdObj = new \CentreonExternalCommand();
$this->action = "RTDOWNTIME";
$this->externalCmdObj->setUserAlias(CentreonUtils::getUserName());
$this->externalCmdObj->setUserId(CentreonUtils::getUserId());
}

/**
* Display downtimes
*
* @param null $parameters
*/
public function show($parameters = null)
{
$params = array(
'author',
'actual_start_time',
'end_time',
'comment_data',
'duration',
'fixed'
);
echo str_replace("_", " ", implode($this->delim, $params)) . "\n";
$elements = $this->object->getRunningDowntimes();
foreach ($elements as $tab) {
echo implode($this->delim, array_values($tab)) . "\n";
}
}

/**
* @param $parameters
* @return array
Expand All @@ -126,7 +99,7 @@ private function parseParameters($action, $parameters)
}

// Check date format
$checkStart = \DateTime::createFromFormat('Y/m/d H:i',$start);
$checkStart = \DateTime::createFromFormat('Y/m/d H:i', $start);
$checkEnd = \DateTime::createFromFormat('Y/m/d H:i', $end);
if (!$checkStart || !$checkEnd) {
throw new CentreonClapiException('Wrong date format, expected : YYYY/MM/DD HH:mm');
Expand Down Expand Up @@ -162,10 +135,101 @@ private function parseParameters($action, $parameters)
'fixed' => $fixed,
'duration' => $duration,
'withServices' => $withServices,
'comment' => $comment
'comment' => $comment,
);
}

/**
* @param $parameters
* @return array
*/
private function parseShowParameters($parameters)
{
list($type, $resource) = explode(';', $parameters);

return array(
'type' => $type,
'resource' => $resource,
);
}

/**
* @param null $parameters
*/
public function show($parameters = null)
{
$parsedParameters = $this->parseShowparameters($parameters);

$method = 'show'.ucfirst($parsedParameters['type']);

$this->$method($parsedParameters['resource']);
}

/**
* @param $hostList
*/
public function showHost($hostList)
{
$fields = array(
'host_name',
'author',
'actual_start_time',
'end_time',
'comment_data',
'duration',
'fixed',
);

echo implode($this->delim, $fields)."\n";

// Résultat des la recherche dans la base
$hostList = array_filter(explode('|', $hostList));
$hostList = array_map(
function ($element) {
return $this->db->escape($element);
},
$hostList
);
$hostDowntimesList = $this->object->getHostDowntimes($hostList);

foreach ($hostDowntimesList as $hostDowntimes) {
echo implode($this->delim, array_values($hostDowntimes))."\n";
}
}

/**
* @param $svcList
*/
public function showSvc($svcList)
{
$fields = array(
'host_name',
'service_name',
'author',
'actual_start_time',
'end_time',
'comment_data',
'duration',
'fixed',
);

echo implode($this->delim, $fields)."\n";
$svcList = array_filter(explode('|', $svcList));
$svcList = array_map(
function ($arrayElem) {
return $this->db->escape($arrayElem);
},
$svcList
);

// Résultat des la recherche dans la base
$serviceDowntimesList = $this->object->getSvcDowntimes($svcList);

foreach ($serviceDowntimesList as $hostDowntimes) {
echo implode($this->delim, array_values($hostDowntimes))."\n";
}
}

/**
* @param null $parameters
*/
Expand All @@ -174,7 +238,7 @@ public function add($parameters = null)
$parsedParameters = $this->parseParameters($this->action, $parameters);

// Permet que $method prenne le bon nom de methode (addHostDowntime, addSvcDowntime etc.)
$method = 'add' . ucfirst($parsedParameters['type']) . 'Downtime';
$method = 'add'.ucfirst($parsedParameters['type']).'Downtime';
$this->$method(
$parsedParameters['resource'],
$parsedParameters['start'],
Expand All @@ -201,31 +265,18 @@ private function addHostDowntime(
$end,
$fixed,
$duration,
$comment,
$withServices
$withServices,
$comment
) {
// Vérification de l'ajout des services avec les hosts
if ($withServices === true) {
$this->externalCmdObj->addHostDowntime(
$resource,
$comment,
$start,
$end,
$fixed,
$duration,
true
);
} else {
$this->externalCmdObj->addHostDowntime(
$resource,
$comment,
$start,
$end,
$fixed,
$duration,
$withServices
);
}
$this->externalCmdObj->addHostDowntime(
$resource,
$comment,
$start,
$end,
$fixed,
$duration,
$withServices
);
}

/**
Expand All @@ -244,8 +295,8 @@ private function addSvcDowntime(
$end,
$fixed,
$duration,
$comment,
$withServices
$withServices,
$comment
) {
// Check if a pipe is present
if (preg_match('/^(.+)\|(.+)$/', $resource, $matches)) {
Expand Down