Skip to content

Commit

Permalink
API calls instead of ido DB
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickpr committed Oct 22, 2020
1 parent f194438 commit d25d511
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 67 deletions.
6 changes: 4 additions & 2 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Migration path : from version > 1.0.0
Upgrade notes
=============

API is now the default for object queries. A new permission must be added for api user : 'objects/query/hostgroup'
API will soon be the default for object queries. A new permission must be added for api user : 'objects/query/hostgroup'
Either you create a new user (using "bin/installer -c api") or edit the /etc/icinga2/conf.d/api-users.conf file, find the user and change the 'permission=' line.

BUGS
====
Expand All @@ -18,7 +19,8 @@ ENHANCEMENTS
1) Added comments and categories for handlers, redesigned handler list table (issue # 43)
2) Added real host IP detection in forwarded traps using snmpTrapAddress (or custom one) (issue #41)
3) Added handler cloning (issue #44)
4) Added API



-------------------------------------------------------------------------------------------------
Version 1.0.4b/c
Expand Down
70 changes: 48 additions & 22 deletions application/controllers/HandlerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private function add_from_existing($trapid)
// Get host
try
{
$hosts=$this->getUIDatabase()->getHostByIP($hostfilter);
$hosts=$this->getIdoConn()->getHostByIP($hostfilter);
}
catch (Exception $e)
{
Expand Down Expand Up @@ -234,7 +234,7 @@ private function add_from_existing($trapid)
private function add_check_host_exists($ruleDetail)
{
// Check if hostname still exists
$host_get=$this->getUIDatabase()->getHostByName($this->view->hostname);
$host_get=$this->getIdoConn()->getHostByName($this->view->hostname);

if (count($host_get)==0)
{
Expand All @@ -246,7 +246,7 @@ private function add_check_host_exists($ruleDetail)
// Tell JS to get services when page is loaded
$this->view->serviceGet=true;
// get service id for form to set :
$serviceID=$this->getUIDatabase()->getServiceIDByName($this->view->hostname,$ruleDetail->service_name);
$serviceID=$this->getIdoConn()->getServiceIDByName($this->view->hostname,$ruleDetail->service_name);
if (count($serviceID) ==0)
{
$this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore';
Expand All @@ -265,15 +265,15 @@ private function add_check_host_exists($ruleDetail)
private function add_check_hostgroup_exists($ruleDetail)
{
// Check if groupe exists
$group_get=$this->getUIDatabase()->getHostGroupByName($this->view->hostgroupname);
$group_get=$this->getIdoConn()->getHostGroupByName($this->view->hostgroupname);
if (count($group_get)==0)
{
$this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore';
$this->view->serviceGroupGet=false;
}
else
{
$grpServices=$this->getUIDatabase()->getServicesByHostGroupid($group_get[0]->id);
$grpServices=$this->getIdoConn()->getServicesByHostGroupid($group_get[0]->id);
$foundGrpService=0;
foreach ($grpServices as $grpService)
{
Expand Down Expand Up @@ -473,7 +473,7 @@ protected function handlerformAction()
}
catch (Exception $e)
{
$this->_helper->json(array('status'=>$e->getMessage()));
$this->_helper->json(array('status'=>$e->getMessage(),'location'=>'Deleting Rule'));
return;
}
//$this->Module()->
Expand All @@ -500,13 +500,14 @@ protected function handlerformAction()
}
}
}

$this->getIdoConn(); //Set apiMode
try
{
$isHostGroup=($params['hostgroup']['val'] == 1)?true:false;
if (! $isHostGroup )
{ // checks if selection by host
$hostAddr=$this->getUIDatabase()->getHostInfoByID($params['hostid']['val']);
$hostAddr=$this->getIdoConn()->getHostInfoByID($params['hostid']['val']);
if ($hostAddr === NULL) throw new \Exception("No object found");
$params['ip4']['val']=$hostAddr->ip4;
$params['ip6']['val']=$hostAddr->ip6;
$checkHostName=$hostAddr->name;
Expand All @@ -515,26 +516,51 @@ protected function handlerformAction()
$this->_helper->json(array('status'=>"Invalid host id : Please re enter host name"));
return;
}
if (!is_numeric($params['serviceid']['val']))
if ($this->apiMode == TRUE)
{
$this->_helper->json(array('status'=>"Invalid service id ". $params['serviceid']['val']));
return;
$serviceName=$this->getIdoConn()->getServiceById($params['serviceid']['val']);
if (count($serviceName) == 0 )
{
$this->_helper->json(array('status'=>"Invalid service id : Please re enter service",'sent'=>$params['serviceid']['val'],'found'=>$serviceName[0]->__name));
return;
}
}
$serviceName=$this->getUIDatabase()->getObjectNameByid($params['serviceid']['val']);
if ($params['service_name']['val'] != $serviceName->name2)
else
{
$this->_helper->json(array('status'=>"Invalid service id : Please re enter service"));
return;
if (!is_numeric($params['serviceid']['val']))
{
$this->_helper->json(array('status'=>"Invalid service id ". $params['serviceid']['val']));
return;
}

$serviceName=$this->getUIDatabase()->getObjectNameByid($params['serviceid']['val']);
if ($params['service_name']['val'] != $serviceName->name2)
{
$this->_helper->json(array('status'=>"Invalid service id : Please re enter service"));
return;
}
}
}
else
{
$object=$this->getUIDatabase()->getObjectNameByid($params['hostid']['val']);
if ($params['host_name']['val'] != $object->name1)
{
$this->_helper->json(array('status'=>"Invalid object group id : Please re enter service"));
return;
}
if ($this->apiMode == TRUE)
{
$object=$this->getIdoConn()->getHostGroupById($params['hostid']['val']);
if (count($object) == 0 || $params['host_name']['val'] != $object->__name)
{
$this->_helper->json(array('status'=>"Invalid object group id : Please re enter service"));
return;
}
}
else
{
$object=$this->getUIDatabase()->getObjectNameByid($params['hostid']['val']);
if ($params['host_name']['val'] != $object->name1)
{
$this->_helper->json(array('status'=>"Invalid object group id : Please re enter service"));
return;
}
}
// Put param in correct column (group_name)
$params['host_group_name']['val'] = $params['host_name']['val'];
$params['host_name']['val']=null;
Expand All @@ -561,7 +587,7 @@ protected function handlerformAction()
}
catch (Exception $e)
{
$this->_helper->json(array('status'=>$e->getMessage()));
$this->_helper->json(array('status'=>$e->getMessage(),'location'=>'Add/update Rule','line'=>$e->getLine(),'file'=>$e->getFile()));
return;
}
$this->_helper->json(array('status'=>'OK', 'id' => $ruleID));
Expand Down
22 changes: 15 additions & 7 deletions application/controllers/HelperController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ public function gethostsAction()
$hostFilter = $this->checkPostVar($postData, 'hostFilter', '.*');

$retHosts=array('status'=>'OK','hosts' => array());

$hosts=$this->getUIDatabase()->getHostByIP($hostFilter);
$this->getIdoConn(); // set apiMode to correct val
if ($this->apiMode === TRUE)
{
$hosts=$this->getIdoConn()->getHostByNameOrIP($hostFilter);
$retHosts['test']=count($hosts);
}
else
{
$hosts=$this->getIdoConn()->getHostByIP($hostFilter);
}
foreach ($hosts as $val)
{
array_push($retHosts['hosts'],$val->name);
Expand All @@ -42,7 +50,7 @@ public function gethostgroupsAction()

$retHosts=array('status'=>'OK','hosts' => array());

$hosts=$this->getUIDatabase()->getHostGroupByName($hostFilter);
$hosts=$this->getIdoConn()->getHostGroupByName($hostFilter);
foreach ($hosts as $val)
{
array_push($retHosts['hosts'],$val->name);
Expand Down Expand Up @@ -72,7 +80,7 @@ public function getservicesAction()
return;
}

$hostArray=$this->getUIDatabase()->getHostByName($host);
$hostArray=$this->getIdoConn()->getHostByName($host);
if (count($hostArray) > 1)
{
$this->_helper->json(array('status'=>'More than one host matches','hostid' => -1));
Expand All @@ -83,7 +91,7 @@ public function getservicesAction()
$this->_helper->json(array('status'=>'No host matches','hostid' => -1));
return;
}
$services=$this->getUIDatabase()->getServicesByHostid($hostArray[0]->id);
$services=$this->getIdoConn()->getServicesByHostid($hostArray[0]->id);
if (count($services) < 1)
{
$this->_helper->json(array('status'=>'No services found for host','hostid' => $hostArray[0]->id));
Expand All @@ -109,7 +117,7 @@ public function gethostgroupservicesAction()

$host = $this->checkPostVar($postData, 'host', '.+');

$hostArray=$this->getUIDatabase()->getHostGroupByName($host);
$hostArray=$this->getIdoConn()->getHostGroupByName($host);
if (count($hostArray) > 1)
{
$this->_helper->json(array('status'=>'More than one hostgroup matches','hostid' => -1));
Expand All @@ -120,7 +128,7 @@ public function gethostgroupservicesAction()
$this->_helper->json(array('status'=>'No hostgroup matches','hostid' => -1));
return;
}
$services=$this->getUIDatabase()->getServicesByHostGroupid($hostArray[0]->id);
$services=$this->getIdoConn()->getServicesByHostGroupid($hostArray[0]->id);
if (count($services) < 1)
{
$this->_helper->json(array('status'=>'No services found for hostgroup','hostid' => $hostArray[0]->id));
Expand Down
2 changes: 1 addition & 1 deletion application/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Icinga\Module\Trapdirector\TrapsController;
use Icinga\Module\Trapdirector\Forms\TrapsConfigForm;
use Icinga\Module\Trapdirector\Icinga2Api;
use Icinga\Module\Trapdirector\Icinga2API;
use Icinga\Module\Trapdirector\TrapsActions\DBException;

use Trapdirector\Trap;
Expand Down
5 changes: 3 additions & 2 deletions application/views/scripts/handler/add.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,8 @@ function serviceGet(serviceSet=null)
{
$('#id_service').val(serviceSet);
}
}, function(data) { // status not OK
},
function(data) { // status not OK
html='<option value=-1 selected>'+data.status+'<option>'
$("#id_service").html(html);
trapDir.displayWarning ("Services : " + data.status);
Expand Down Expand Up @@ -981,7 +982,7 @@ $(function() {
if ($this->serviceGet == true) {
echo "serviceGet(";
if ($this->serviceSet != null) // add param to service get to set service after ajax call
{ echo $this->serviceSet;}
{ echo '"'.$this->serviceSet . '"'; }
echo ");\n" ;
}
?>
Expand Down
2 changes: 1 addition & 1 deletion bin/trap_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//No autoloader when running in CLI.

include (dirname(__DIR__).'/library/Trapdirector/IcingaApi/IcingaApiBase.php');
include (dirname(__DIR__).'/library/Trapdirector/Icinga2Api.php');
include (dirname(__DIR__).'/library/Trapdirector/Icinga2API.php');
include (dirname(__DIR__).'/library/Trapdirector/TrapsProcess/Logging.php');
include (dirname(__DIR__).'/library/Trapdirector/TrapsProcess/Database.php');

Expand Down

0 comments on commit d25d511

Please sign in to comment.