diff --git a/application/controllers/HandlerController.php b/application/controllers/HandlerController.php index f229ee8..0367611 100644 --- a/application/controllers/HandlerController.php +++ b/application/controllers/HandlerController.php @@ -16,7 +16,9 @@ */ class HandlerController extends TrapsController { - + /** @var array $curObjectList OID list in rules/display/perfdata */ + private $curObjectList=array(); + /** index : list existing rules */ public function indexAction() @@ -229,68 +231,63 @@ private function add_from_existing($trapid) /** * Check if host & service still exists or set warning message - * @param object $ruleDetail + * @param string $hostgroupName Name of hostgroup + * @param string $service Name of service + * @throws Exception + * @return array (service_id, service_name) */ - private function add_check_host_exists($ruleDetail) + private function add_check_host_exists(string $hostName, string $service) { // Check if hostname still exists - $host_get=$this->getIdoConn()->getHostByName($this->view->hostname); + $host_get=$this->getIdoConn()->getHostByName($hostName); if (count($host_get)==0) { - $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; - $this->view->serviceGet=false; - } - else - { - // Tell JS to get services when page is loaded - $this->view->serviceGet=true; - // get service id for form to set : - $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'; - } - else - { - $this->view->serviceSet=$serviceID[0]->id; - } + throw new Exception('Host '.$hostName. ' doesn\'t exists anymore'); + } + // get service id for form to set : + $serviceID=$this->getIdoConn()->getServiceIDByName($hostName,$service); + if (count($serviceID) ==0) + { + throw new Exception('Service '.$service. ' doesn\'t exists anymore'); + } + return $serviceID[0]->id; } - /** - * Check if hostgroup & service still exists or set warning message - * @param array $ruleDetail - */ - private function add_check_hostgroup_exists($ruleDetail) + /** + * Check if host / hostgroup & service still exists + * @param string $hostgroupName Name of hostgroup + * @param string $service Name of service + * @throws Exception + * @return string service_id + */ + private function add_check_hostgroup_exists(string $hostgroupName, string $service) { // Check if groupe exists - $group_get=$this->getIdoConn()->getHostGroupByName($this->view->hostgroupname); + $group_get=$this->getIdoConn()->getHostGroupByName($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->getIdoConn()->getServicesByHostGroupid($group_get[0]->id); - $foundGrpService=0; - foreach ($grpServices as $grpService) - { - if ($grpService[0] == $ruleDetail->service_name) - { - $foundGrpService=1; - $this->view->serviceSet=$ruleDetail->service_name; - } - } - - // Tell JS to get services when page is loaded - $this->view->serviceGroupGet=true; - if ($foundGrpService==0) - { - $this->view->warning_message.=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; - } + { + throw new Exception('HostGroup '.$hostgroupName. ' doesn\'t exists anymore.'); } + + $grpServices=$this->getIdoConn()->getServicesByHostGroupid($group_get[0]->id); + $foundGrpService=0; + foreach ($grpServices as $grpService) + { + if ($grpService[0] == $service) + { + $foundGrpService=1; + } + } + + // Tell JS to get services when page is loaded + $this->view->serviceGroupGet=true; + if ($foundGrpService==0) + { + throw new Exception(' Service '.$service. ' doesn\'t exists anymore.'); + } + return $service; } /** @@ -343,6 +340,62 @@ private function add_create_trap_object_list(&$display, &$rule) } return $curObjectList; } + + /** + * Update object list array with all OIDs in evalString + * Replace in evalString by $$ and store oid in $this->curObjectList + * @param string|null $evalString + */ + private function update_trap_object_list( &$evalString ) + { + if ($evalString === NULL) return; + // Replace existing elements + $index=1; + foreach ($this->curObjectList as $curOidObj) + { + if ($curOidObj[0] >= $index ) $index = $curOidObj[0]+1; + $curOid = $curOidObj[1]; + $curOid = preg_replace('/\*/','\*',$curOid); + $evalString=preg_replace('/_OID\('.$curOid.'\)/','\$'.$curOidObj[0].'\$',$evalString); + } + + // check in display & rule for : OID() + $matches=array(); + while ( preg_match('/_OID\(([\.0-9\*]+)\)/',$evalString,$matches) ) + { + $curOid=$matches[1]; + + if ( (preg_match('/\*/',$curOid) == 0 ) + && ($object=$this->getMIB()->translateOID($curOid)) != null) + { + array_push($this->curObjectList, array( + $index, + $curOid, + $object['mib'], + $object['name'], + '', + $object['type'], + $object['type_enum'] + )); + } + else + { + array_push($this->curObjectList, array( + $index, + $curOid, + 'not found', + 'not found', + '', + 'not found', + 'not found' + )); + } + $curOid = preg_replace('/\*/','\*',$curOid); + $evalString=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$evalString); + $index++; + } + return; + } /** Add a handler * Get params fromid : setup from existing trap (id of trap table) @@ -394,19 +447,38 @@ public function addAction() $this->view->comment = $ruleDetail->comment; $this->view->category = $ruleDetail->category; + $this->view->limit = $ruleDetail->limit; // Warning message if host/service don't exists anymore $this->view->warning_message=''; if ($this->view->hostname != null) { $this->view->selectGroup=false; + $baseHostName = $this->view->hostname; // Check if hostname still exists - $this->add_check_host_exists($ruleDetail); + try + { + $service_info = $this->add_check_host_exists( $ruleDetail->host_name , $ruleDetail->service_name); + $this->view->serviceSet = $service_info; + $this->view->serviceGet = true; + } catch (Exception $e) + { + $this->view->warning_message .= $e->getMessage(); + $this->view->serviceGet = false; + } } else { + $baseHostName = $ruleDetail->host_group_name; $this->view->selectGroup=true; - $this->add_check_hostgroup_exists($ruleDetail); // Check if groupe exists + try { + $service_info = $this->add_check_hostgroup_exists($ruleDetail->host_group_name, $ruleDetail->service_name); + $this->view->serviceSet = $service_info; + $this->view->serviceGroupGet = true; + } catch (Exception $e) { + $this->view->warning_message .= $e->getMessage(); + $this->view->serviceGroupGet = false; + } } $this->view->mainoid=$ruleDetail->trap_oid; @@ -421,15 +493,25 @@ public function addAction() // Create object list with : display & rules references (OID) and complete with all objects if found $display=$ruleDetail->display; $rule=$ruleDetail->rule; + $perfdata=$ruleDetail->perfdata; // Create object list array with all OIDs in rule & display - $curObjectList=$this->add_create_trap_object_list($display, $rule); + $this->update_trap_object_list($rule); + $this->update_trap_object_list($display); + $this->update_trap_object_list($perfdata); + //$curObjectList=$this->add_create_trap_object_list($display, $rule); // set display $this->view->display=$display; $this->view->rule=$rule; + $this->view->perfdata = $perfdata; - $this->view->objectList=$curObjectList; + // Rules attached + $ruleWarning=''; + $this->view->ruleList = $this->getRuleListDetail($ruleid,$baseHostName,$this->view->selectGroup,$ruleWarning); + $this->view->warning_message .= $ruleWarning; + + $this->view->objectList=$this->curObjectList; } } @@ -698,6 +780,95 @@ protected function getRuleDetail($ruleid) } + /** + * Get all rules linked to default rule by ruleid. + * @param int $ruleid id of rule in rule table + * @param string $baseHostName host name of base rule + * @param bool $isGroup if host in base rule is hostgroup + * @param string $warningMessage wrning(s) messages if any + * @throws Exception + * @return array column objects in db + */ + protected function getRuleListDetail(int $ruleid,string $baseHostName,bool $isGroup, string &$warningMessage) + { + if (!preg_match('/^[0-9]+$/',$ruleid)) { throw new Exception('Invalid id'); } + + $ruleListObjects=$this->getUIDatabase()->getRulesList($ruleid); + $index=1; + foreach ( $ruleListObjects as &$ruleObject ) + { + $ruleObject->id = $index; // Rewrite order if error in numbering (getRuleList sorts element) + $this->update_trap_object_list($ruleObject->rule); + $this->update_trap_object_list($ruleObject->display); + $this->update_trap_object_list($ruleObject->perfdata); + + $temp_host=FALSE; + $ruleObject->isGroup = $isGroup; + if ($ruleObject->service_name !== NULL && $ruleObject->host_name === NULL && $ruleObject->host_group_name == NULL) + { + $temp_host = TRUE; + if ( $isGroup === TRUE ) + { + $ruleObject->host_group_name = $baseHostName; + } + else + { + $ruleObject->host_name = $baseHostName; + } + } + if ($ruleObject->host_name !== NULL) + { + $ruleObject->isGroup = false; + if ($ruleObject->service_name === NULL) + { + $warningMessage .= 'Rule ' . $index . ' missing service for host ' . $ruleObject->host_name . ' '; + $ruleObject->service_id = NULL; + } + else + { + try + { + $ruleObject->service_id = $this->add_check_host_exists( $ruleObject->host_name , $ruleObject->service_name); + } catch (Exception $e) + { + $warningMessage .= 'Rule '. $index . ' : ' .$e->getMessage(); + $ruleObject->service_id = NULL; + } + } + } + else if ($ruleObject->host_group_name !== NULL) + { + $ruleObject->isGroup = true; + if ($ruleObject->service_name === NULL) + { + $warningMessage .= 'Rule ' + $index + ' missing service for host group ' + $ruleObject->host_group_name + ' '; + $ruleObject->service_id = NULL; + } + else + { + try + { + $ruleObject->service_id = $this->add_check_hostgroup_exists( $ruleObject->host_group_name , $ruleObject->service_name); + } catch (Exception $e) + { + $warningMessage .= 'Rule '. $index . ' : ' . $e->getMessage(); + $ruleObject->service_id = NULL; + } + } + } + else + { + $ruleObject->service_id = NULL; + } + + if ($temp_host === TRUE ) + { + $ruleObject->host_group_name = $ruleObject->host_name = NULL; + } + } + return $ruleListObjects; + } + /** Setup tabs for rules */ protected function prepareTabs() diff --git a/application/views/scripts/handler/add.phtml b/application/views/scripts/handler/add.phtml index 4e3af45..edc3931 100644 --- a/application/views/scripts/handler/add.phtml +++ b/application/views/scripts/handler/add.phtml @@ -238,7 +238,7 @@ use Icinga\Web\Url; -
Multiple rules list +
Multiple rules list @@ -248,16 +248,16 @@ use Icinga\Web\Url; - - - + + + - - - + + +
Default$1$ = 3Result : $1$result:$1$Display OK No No - - + -
@@ -405,7 +405,8 @@ count_id=1; trapDir=null; var actionList= { '-2' : 'Ignore' , '-1' : 'Nothing' , '0' : 'OK' , '1' : 'Warning' , '2' : 'Critical' , '3' : 'Unknown' }; -var ruleList = new CruleList('rule) ?>', '', 'display) ?>', setRuleMatch ?>, setRuleNoMatch ?>); +var ruleList = new CruleList('rule) ?>', 'perfdata) ?>', + 'display) ?>', setRuleMatch ?>, setRuleNoMatch ?>, limit ?>); // ************** Rule List *************** function CRuleElmt(isDefault=false, rule='', perfdata='', display='', actionOK=-1, actionNOK=-1, hostGroup=null,newHost='', newService=-1, newServiceText = '', newlimit = '0') @@ -504,27 +505,29 @@ function CruleList(rule='', perfdata='', display='', actionOK=-1, actionNOK=-1,l this.curRule=0; this.ruleListArray[0] = new CRuleElmt(true,rule,perfdata, display, actionOK, actionNOK); this.isDefault=true; + $("#fieldset_rules_list").toggle(false); this.editRulesetup = function(CurRuleElmt,isDefault) { $("#id_rules").val(CurRuleElmt.rule); $("#id_display").val(CurRuleElmt.display); $("#id_rule_action_true").val(CurRuleElmt.actionOK); - //$("#id_rule_action_false").prop( "disabled", ! isDefault ); $("#id_rule_action_false").val(CurRuleElmt.actionNOK); $("#id_rule_limit").val(CurRuleElmt.limit); $("#id_perfdata").val(CurRuleElmt.perfdata); $("#id_rule_host_or_hostgroup").prop('checked', CurRuleElmt.hostGroup); - //$("#id_rule_host_or_hostgroup").prop( "disabled", isDefault ); $("#id_host_reassign").val(CurRuleElmt.newHost); - //$("#id_host_reassign").prop( "disabled", isDefault ); $(".trapdir_rule_list_entries").toggle( ! isDefault ); $(".trapdir_rule_default_entries").toggle( isDefault ); this.ruleServiceGet(CurRuleElmt.newService); - //$("#id_service_reassign").prop( "disabled", isDefault ); this.setRuleDisplay(); }; this.setRuleDisplay = function () { $("#id_rule_number").html(this.curRule==0 ? 'Default' : this.curRule); } + this.addRule = function(rule) { + $("#fieldset_rules_list").toggle(true); // Display rules list + this.ruleListIndex++; + this.ruleListArray[this.ruleListIndex] = rule; + } this.render = function() { // render table htm = 'OrderRuleDisplayPerf. Dataon match'; htm += 'Host reassignService reassignMoveEditDelete'; @@ -538,6 +541,7 @@ function CruleList(rule='', perfdata='', display='', actionOK=-1, actionNOK=-1,l }; this.newRule = function() { // Save current and add new rule if (this.saveCurrentRule() == false) return; + $("#fieldset_rules_list").toggle(true); // Display rules list this.ruleListIndex++; this.ruleListArray[this.ruleListIndex] = new CRuleElmt(false); this.isDefault=false; @@ -1333,8 +1337,20 @@ $(function() { //var ruleList = new CruleList('rule) ?>', '', 'display) ?>', setRuleMatch ?>, setRuleNoMatch ?>); //ruleList.setDefaultRule(true); - ruleList.render(); ruleList.editRulesetup(ruleList.ruleListArray[0],true); + +ruleList as $curRule) + { + //(isDefault=false, rule='', perfdata='', display='', actionOK=-1, actionNOK=-1, hostGroup=null,newHost='', newService=-1, newServiceText = '', newlimit = '0') + if ($curRule->service_id === NULL) $curRule->service_id = -1; + echo 'rule = new CRuleElmt(false, '; + echo "'" . $curRule->rule . "', '" . $curRule->perfdata . "', '" . $curRule->display . "', " . $curRule->action_match . ", -1, "; + echo "'" . ($curRule->isGroup ? "true" : "false" ) . "','" . $curRule->host_name . "', '" . $curRule->service_id . "', '" . $curRule->service_name . "');\n"; + echo "ruleList.addRule(rule);\n"; + } +?> + ruleList.render(); }); diff --git a/library/Trapdirector/Config/TrapModuleConfig.php b/library/Trapdirector/Config/TrapModuleConfig.php index 5084f89..c763b74 100644 --- a/library/Trapdirector/Config/TrapModuleConfig.php +++ b/library/Trapdirector/Config/TrapModuleConfig.php @@ -62,6 +62,12 @@ public function getTrapRuleName() { return array('r' => $this->table_prefix . 'rules'); } + + // DB table name of rules_list : prefix 'l' + public function getTrapRuleListName() + { + return array('l' => $this->table_prefix . 'rules_list'); + } // DB table name of db config : prefix 'c' public function getDbConfigTableName() @@ -206,20 +212,39 @@ public function ruleDetailQuery() 'ip6' => "r.ip6", 'trap_oid' => 'r.trap_oid', 'host_name' => 'r.host_name', - 'host_group_name' => 'r.host_group_name', + 'host_group_name' => 'r.host_group_name', 'rule' => 'r.rule', 'action_match' => 'r.action_match', 'action_nomatch'=> 'r.action_nomatch', 'service_name' => 'r.service_name', 'revert_ok' => 'r.revert_ok', 'display' => 'r.display', + 'perfdata' => 'r.performance_data', 'modified' => 'UNIX_TIMESTAMP(r.modified)', 'modifier' => 'r.modifier', 'comment' => 'r.comment', - 'category' => 'r.rule_type' + 'category' => 'r.rule_type', + 'last_matched' => 'UNIX_TIMESTAMP(r.last_matched)', + 'limit' => 'r.limit' ); } - + + // rule list update ( => ) + public function ruleListDetailQuery() + { + return array( + 'id' => 'l.id', + 'rule' => 'l.rule', + 'rule_order' => 'l.evaluation_order', + 'display' => 'l.display', + 'action_match' => 'l.status', + 'service_name' => 'l.reassign_service', + 'host_name' => 'l.reassign_host', + 'host_group_name' => 'l.reassign_hostgroup', + 'perfdata' => 'l.performance_data' + ); + } + // Trap detail ( => <sql select>) public function trapDetailQuery() { diff --git a/library/Trapdirector/TrapsActions/TrapDBQuery.php b/library/Trapdirector/TrapsActions/TrapDBQuery.php index dc5ab93..fbb57bc 100644 --- a/library/Trapdirector/TrapsActions/TrapDBQuery.php +++ b/library/Trapdirector/TrapsActions/TrapDBQuery.php @@ -129,6 +129,20 @@ public function lastModification() return $returnRow->lastmodified; } + public function getRulesList(int $ruleID) + { + $dbConn = $this->getDbConn(); + if ($dbConn === null) throw new \ErrorException('uncatched db error'); + $query = $dbConn->select() + ->from( + $this->getTrapCtrl()->getModuleConfig()->getTrapRuleListName(), + $this->getTrapCtrl()->getModuleConfig()->ruleListDetailQuery()) + ->where('l.handler='.$ruleID) + ->order('l.evaluation_order ASC'); + $returnList=$dbConn->fetchall($query); + return $returnList; + } + /** Delete trap by ip & oid * @param $ipAddr string source IP (v4 or v6) * @param $oid string oid