diff --git a/README.md b/README.md index b412a67..92c626c 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,7 @@ You will find below the updated list of actions (**"API_function"**) possible in | `deleteServiceTemplate` | POST | [**templateName**] | "http_code": "200 OK", "result": ["code":returnCode,"description":"logs"] | Delete the given Service template | | `deleteCommand` | POST | [**commandName**] | "http_code": "200 OK", "result": ["code":returnCode,"description":"logs"] | Delete a command to Nagios. | | `deleteHost` | POST | [**hostName, exportConfiguration**] | "http_code": "200 OK", "result": ["code":returnCode,"description":"logs"] | Delete a nagios host. | +| `deleteHostById` | POST | [**id, exportConfiguration**] | "http_code": "200 OK", "result": ["code":returnCode,"description":"logs"] | Delete a nagios host by its ID. | | `deleteContactGroupToContact` | POST | [**contactName, contactGroupName, exportConfiguration**] | "http_code": "200 OK", "result": ["code":returnCode,"description":"logs"] | delete a contact group to a nagios contact. | | `deleteContactNotificationCommandToContact` | POST | [**contactName, commandName, exportConfiguration**] | "http_code": "200 OK", "result": ["code":returnCode,"description":"logs"] | delete a contact notification command to a nagios contact. | | `deleteServiceGroupToServiceInHost` | POST | [**serviceGroupName, serviceName, hostName, exportConfiguration = FALSE**] | "http_code": "200 OK", "result": ["code":returnCode,"description":"logs"] | delete a service group in the given service of the specified host. | diff --git a/html/api/index.php b/html/api/index.php index 9d50de9..1a09663 100644 --- a/html/api/index.php +++ b/html/api/index.php @@ -29,6 +29,7 @@ addRoute('post', '/getHost', 'getHost', 'operator'); +addRoute('post', '/getHostById', 'getHostById', 'operator'); addRoute('post', '/getContact', 'getContact', 'operator'); addRoute('post', '/getCommand', 'getCommand', 'operator'); addRoute('post', '/getHostGroup', 'getHostGroup', 'operator'); @@ -58,6 +59,7 @@ addRoute('post', '/createServiceToHostTemplate', 'createServiceToHostTemplate'); addRoute('post', '/addEventBroker', 'addEventBroker'); +addRoute('post', '/addParentToHost', 'addParentToHost'); addRoute('post', '/addContactToHost', 'addContactToHost'); addRoute('post', '/addHostGroupToHost', 'addHostGroupToHost'); addRoute('post', '/addContactGroupToHost', 'addContactGroupToHost'); @@ -103,6 +105,7 @@ addRoute('post', '/deleteHost', 'deleteHost'); +addRoute('post', '/deleteHostById', 'deleteHostById'); addRoute('post', '/deleteContact', 'deleteContact'); addRoute('post', '/deleteService', 'deleteService'); addRoute('post', '/deleteCommand', 'deleteCommand'); @@ -112,6 +115,7 @@ addRoute('post', '/deleteServiceGroup', 'deleteServiceGroup'); addRoute('post', '/deleteHostTemplate', 'deleteHostTemplate'); addRoute('post', '/deleteHostDowntime', 'deleteHostDowntime'); +addRoute('post', '/deleteParentToHost', 'deleteParentToHost'); addRoute('post', '/deleteContactToHost', 'deleteContactToHost'); addRoute('post', '/deleteServiceTemplate', 'deleteServiceTemplate'); addRoute('post', '/deleteHostGroupToHost', 'deleteHostGroupToHost'); @@ -151,6 +155,7 @@ addRoute('post', '/listNagiosStates', 'listNagiosStates', 'readonly'); addRoute('post', '/listNagiosObjects', 'listNagiosObjects', 'readonly'); addRoute('post', '/listNagiosBackends', 'listNagiosBackends', 'readonly'); +addRoute('post', '/listHosts', 'listHosts', 'readonly'); //============================================================================== //eonweb @@ -235,4 +240,4 @@ function addRoute($httpMethod, $routeName, $methodName, $right="admin"){ $app->run(); -?> \ No newline at end of file +?> diff --git a/include/ObjectManager.php b/include/ObjectManager.php index ee547b6..558e989 100644 --- a/include/ObjectManager.php +++ b/include/ObjectManager.php @@ -602,9 +602,7 @@ public function modifyNotifierMethod($method_name, $method_type,$new_method_name /* LILAC - List Hosts */ public function listHosts( $hostName = false, $hostTemplate = false ){ - - return true; - + return $this->listNagiosObjects("hosts")["default"]; } ########################################## GET @@ -631,6 +629,22 @@ public function getHost( $hostName){ return "Host named ".$hostName." doesn't exist."; } } + + /* LILAC - Get Host by ID */ + public function getHostById($id){ + $nhp = new NagiosHostPeer; + // Find host + $host = $nhp->getById($id); + if($host){ + return $host->toArray(); + }else{ + return "Host with ID ".$id." doesn't exist."; + } + } + + + + /* LILAC - Get Hosts by template name */ public function getHostsBytemplate( $templateHostName){ $nhtp = new NagiosHostTemplatePeer; @@ -944,6 +958,63 @@ public function getResources(){ } ########################################## CREATE + + /* LILAC - add kinship link */ + public function addParentToHost($parentName, $childName, $exportConfiguration=FALSE){ + + $error = ""; + $success = ""; + $code=0; + + try{ + + // Wants to add a parent + $nhp = new NagiosHostPeer; + // Find host + $parentHost = $nhp->getByName($parentName); + if(!$parentHost) { + $code=1; + $error .= "Parent Host $parentName does not exists\n"; + } + + $childHost = $nhp->getByName($childName); + if(!$childHost) { + $code=1; + $error .= "Child Host $childName does not exists\n"; + } + + if($code==0){ + $c = new Criteria(); + $c->add(NagiosHostParentPeer::CHILD_HOST , $childHost->getId()); + $c->add(NagiosHostParentPeer::PARENT_HOST, $parentHost->getId()); + $parentRelationship = NagiosHostParentPeer::doSelectOne($c); + if($parentRelationship) { + $code=1; + $error .= "That parent relationship already exist.\n"; + }else { + $tempParent = new NagiosHostParent(); + $tempParent->setChildHost($childHost->getId()); + $tempParent->setParentHost($parentHost->getId()); + $tempParent->save(); + $success .= "Parent added"; + } + + if( $exportConfiguration == TRUE ) + $this->exportConfigurationToNagios($error, $success); + + } + + }catch(Exception $e) { + $code=1; + $error .= $e->getMessage(); + } + + $logs = $this->getLogs($error, $success); + + return array("code"=>$code,"description"=>$logs); + } + + /* LILAC - create contact */ public function createContact($contactName, $contactAlias="description", $contactMail, $contactPager="", $contactGroup="",$serviceNotificationCommand="notify-by-email-service",$hostNotificationCommand="notify-by-email-host", $options=NULL, $exportConfiguration = FALSE ){ $error = ""; @@ -1198,7 +1269,7 @@ public function createHost( $templateHostName="GENERIC_HOST", $hostName, $hostIp $tempHost = new NagiosHost(); $tempHost->setName($hostName); $tempHost->setAlias($hostAlias); - $tempHost->setDisplayName($hostName); + // $tempHost->setDisplayName($hostName); $tempHost->setAddress($hostIp); $tempHost->save(); $success .= "Host $hostName added\n"; @@ -4171,6 +4242,57 @@ public function modifyNagiosMainConfiguration($requestConf=NULL, $exportConfigur } ########################################## DELETE +/* LILAC - delete kinship link */ +public function deleteParentToHost($parentName, $childName, $exportConfiguration=FALSE){ + + $error = ""; + $success = ""; + $code=0; + + try{ + + // Wants to add a parent + $nhp = new NagiosHostPeer; + // Find host + $parentHost = $nhp->getByName($parentName); + if(!$parentHost) { + $code=1; + $error .= "Parent Host $parentName does not exists\n"; + } + + $childHost = $nhp->getByName($childName); + if(!$childHost) { + $code=1; + $error .= "Child Host $childName does not exists\n"; + } + + if($code==0){ + $c = new Criteria(); + $c->add(NagiosHostParentPeer::CHILD_HOST , $childHost->getId()); + $c->add(NagiosHostParentPeer::PARENT_HOST, $parentHost->getId()); + $parentRelationship = NagiosHostParentPeer::doSelectOne($c); + if($parentRelationship) { + $parentRelationship->delete(); + $success .= "That parent relationship been deleted.\n"; + }else { + $code = 1; + $error.= "That parent relationship does not exist yet.\n"; + } + + if( $exportConfiguration == TRUE ) + $this->exportConfigurationToNagios($error, $success); + + } + + }catch(Exception $e) { + $code=1; + $error .= $e->getMessage(); + } + + $logs = $this->getLogs($error, $success); + + return array("code"=>$code,"description"=>$logs); +} /* LILAC - delete host Downtimes */ public function deleteHostDowntime($idDowntime){ $error = ""; @@ -5102,6 +5224,39 @@ public function deleteHost( $hostName, $exportConfiguration = FALSE ){ return array("code"=>$code,"description"=>$logs); } + + public function deleteHostById($id, $exportConfiguration = false) { + $error = ""; + $success = ""; + $code = 0; + try { + $nhp = new NagiosHostPeer; + $host = $nhp->getById($id); + if ($host) { + $host->delete(); + $success .= "Host with ID " . $id . " deleted\n"; + } + else { + $code = 1; + $error .= "Host with ID " . $id . " not found\n"; + } + + // Export + if ($exportConfiguration == true) $this->exportConfigurationToNagios(); + } + catch(Exception $e) { + $code = 1; + $error .= $e->getMessage() . "\n"; + } + + $logs = $this->getLogs($error, $success); + return array( + "code" => $code, + "description" => $logs + ); + } + + /* LILAC - Delete Templates Hosts */ public function deleteHostTemplate($templateHostName){ $error = "";