@@ -180,7 +180,9 @@ |
||
| 180 | 180 | if ($this->MIBData == null) |
| 181 | 181 | { |
| 182 | 182 | $dbConn = $this->getUIDatabase()->getDbConn(); |
| 183 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 183 | + if ($dbConn === null) { |
|
| 184 | + throw new \ErrorException('uncatched db error'); |
|
| 185 | + } |
|
| 184 | 186 | $this->MIBData=new MIBLoader( |
| 185 | 187 | $this->Config()->get('config', 'snmptranslate'), |
| 186 | 188 | $this->Config()->get('config', 'snmptranslate_dirs'), |
@@ -46,8 +46,8 @@ discard block |
||
| 46 | 46 | |
| 47 | 47 | |
| 48 | 48 | /** Get instance of TrapModuleConfig class |
| 49 | - * @return TrapModuleConfig |
|
| 50 | - */ |
|
| 49 | + * @return TrapModuleConfig |
|
| 50 | + */ |
|
| 51 | 51 | public function getModuleConfig() |
| 52 | 52 | { |
| 53 | 53 | if ($this->moduleConfig == Null) |
@@ -79,12 +79,12 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | public function getTrapHostListTable() |
| 81 | 81 | { |
| 82 | - if ($this->trapTableHostList == Null) |
|
| 82 | + if ($this->trapTableHostList == Null) |
|
| 83 | 83 | { |
| 84 | - $this->trapTableHostList = new TrapTableHostList(); |
|
| 85 | - $this->trapTableHostList->setConfig($this->getModuleConfig()); |
|
| 86 | - } |
|
| 87 | - return $this->trapTableHostList; |
|
| 84 | + $this->trapTableHostList = new TrapTableHostList(); |
|
| 85 | + $this->trapTableHostList->setConfig($this->getModuleConfig()); |
|
| 86 | + } |
|
| 87 | + return $this->trapTableHostList; |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /** |
@@ -105,12 +105,12 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | public function getUIDatabase() |
| 107 | 107 | { |
| 108 | - if ($this->UIDatabase == Null) |
|
| 109 | - { |
|
| 110 | - $this->UIDatabase = new UIDatabase($this); |
|
| 108 | + if ($this->UIDatabase == Null) |
|
| 109 | + { |
|
| 110 | + $this->UIDatabase = new UIDatabase($this); |
|
| 111 | 111 | |
| 112 | - } |
|
| 113 | - return $this->UIDatabase; |
|
| 112 | + } |
|
| 113 | + return $this->UIDatabase; |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | /** |
@@ -119,34 +119,34 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | public function getIdoConn() |
| 121 | 121 | { |
| 122 | - if ($this->icingaAPI === NULL) |
|
| 123 | - { |
|
| 124 | - $host = $this->Config()->get('config', 'icingaAPI_host'); |
|
| 125 | - $port = $this->Config()->get('config', 'icingaAPI_port'); |
|
| 126 | - $user = $this->Config()->get('config', 'icingaAPI_user'); |
|
| 127 | - $pass = $this->Config()->get('config', 'icingaAPI_password'); |
|
| 128 | - $this->icingaAPI = new Icinga2API($host,$port); |
|
| 129 | - $this->icingaAPI->setCredentials($user, $pass); |
|
| 130 | - list($ret,$message) = $this->icingaAPI->test(NULL); |
|
| 131 | - if ($ret === TRUE) |
|
| 132 | - { |
|
| 133 | - return $this->getUIDatabase(); |
|
| 134 | - } |
|
| 122 | + if ($this->icingaAPI === NULL) |
|
| 123 | + { |
|
| 124 | + $host = $this->Config()->get('config', 'icingaAPI_host'); |
|
| 125 | + $port = $this->Config()->get('config', 'icingaAPI_port'); |
|
| 126 | + $user = $this->Config()->get('config', 'icingaAPI_user'); |
|
| 127 | + $pass = $this->Config()->get('config', 'icingaAPI_password'); |
|
| 128 | + $this->icingaAPI = new Icinga2API($host,$port); |
|
| 129 | + $this->icingaAPI->setCredentials($user, $pass); |
|
| 130 | + list($ret,$message) = $this->icingaAPI->test(NULL); |
|
| 131 | + if ($ret === TRUE) |
|
| 132 | + { |
|
| 133 | + return $this->getUIDatabase(); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - } |
|
| 137 | - return $this->icingaAPI; |
|
| 136 | + } |
|
| 137 | + return $this->icingaAPI; |
|
| 138 | 138 | |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
| 142 | - { |
|
| 143 | - $limit = $this->params->get('limit', $limit); |
|
| 144 | - $page = $this->params->get('page', $offset); |
|
| 141 | + protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
| 142 | + { |
|
| 143 | + $limit = $this->params->get('limit', $limit); |
|
| 144 | + $page = $this->params->get('page', $offset); |
|
| 145 | 145 | |
| 146 | - $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
|
| 146 | + $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
|
| 147 | 147 | |
| 148 | - return $paginatable; |
|
| 149 | - } |
|
| 148 | + return $paginatable; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | 151 | public function displayExitError($source,$message) |
| 152 | 152 | { // TODO : check better ways to transmit data (with POST ?) |
@@ -155,33 +155,33 @@ discard block |
||
| 155 | 155 | |
| 156 | 156 | protected function checkReadPermission() |
| 157 | 157 | { |
| 158 | - if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
| 159 | - $this->displayExitError('Permissions','No permission fo view content'); |
|
| 160 | - } |
|
| 158 | + if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
| 159 | + $this->displayExitError('Permissions','No permission fo view content'); |
|
| 160 | + } |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | protected function checkConfigPermission() |
| 164 | 164 | { |
| 165 | - if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
| 166 | - $this->displayExitError('Permissions','No permission fo configure'); |
|
| 167 | - } |
|
| 165 | + if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
| 166 | + $this->displayExitError('Permissions','No permission fo configure'); |
|
| 167 | + } |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Check if user has write permission |
|
| 172 | - * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
|
| 173 | - * @return boolean : user has permission |
|
| 174 | - */ |
|
| 170 | + /** |
|
| 171 | + * Check if user has write permission |
|
| 172 | + * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
|
| 173 | + * @return boolean : user has permission |
|
| 174 | + */ |
|
| 175 | 175 | protected function checkModuleConfigPermission($check=0) |
| 176 | 176 | { |
| 177 | - if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
| 178 | - if ($check == 0) |
|
| 179 | - { |
|
| 180 | - $this->displayExitError('Permissions','No permission fo configure module'); |
|
| 181 | - } |
|
| 182 | - return false; |
|
| 183 | - } |
|
| 184 | - return true; |
|
| 177 | + if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
| 178 | + if ($check == 0) |
|
| 179 | + { |
|
| 180 | + $this->displayExitError('Permissions','No permission fo configure module'); |
|
| 181 | + } |
|
| 182 | + return false; |
|
| 183 | + } |
|
| 184 | + return true; |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | /************************* Trap class get **********************/ |
@@ -201,18 +201,18 @@ discard block |
||
| 201 | 201 | /************************** MIB related **************************/ |
| 202 | 202 | |
| 203 | 203 | /** Get MIBLoader class |
| 204 | - * @return MIBLoader class |
|
| 205 | - */ |
|
| 204 | + * @return MIBLoader class |
|
| 205 | + */ |
|
| 206 | 206 | protected function getMIB() |
| 207 | 207 | { |
| 208 | 208 | if ($this->MIBData == null) |
| 209 | 209 | { |
| 210 | - $dbConn = $this->getUIDatabase()->getDbConn(); |
|
| 211 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 210 | + $dbConn = $this->getUIDatabase()->getDbConn(); |
|
| 211 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 212 | 212 | $this->MIBData=new MIBLoader( |
| 213 | 213 | $this->Config()->get('config', 'snmptranslate'), |
| 214 | 214 | $this->Config()->get('config', 'snmptranslate_dirs'), |
| 215 | - $dbConn, |
|
| 215 | + $dbConn, |
|
| 216 | 216 | $this->getModuleConfig() |
| 217 | 217 | ); |
| 218 | 218 | } |
@@ -222,13 +222,13 @@ discard block |
||
| 222 | 222 | /************************** Database queries *******************/ |
| 223 | 223 | |
| 224 | 224 | /** Check if director is installed |
| 225 | - * @return bool true/false |
|
| 226 | - */ |
|
| 225 | + * @return bool true/false |
|
| 226 | + */ |
|
| 227 | 227 | protected function isDirectorInstalled() |
| 228 | 228 | { |
| 229 | - $output=array(); |
|
| 230 | - exec('icingacli module list',$output); |
|
| 231 | - foreach ($output as $line) |
|
| 229 | + $output=array(); |
|
| 230 | + exec('icingacli module list',$output); |
|
| 231 | + foreach ($output as $line) |
|
| 232 | 232 | { |
| 233 | 233 | if (preg_match('/^director .*enabled/',$line)) |
| 234 | 234 | { |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | /** @var UIDatabase $UIDatabase */ |
| 42 | 42 | protected $UIDatabase; |
| 43 | 43 | /** @var Icinga2Api $IcingaAPI */ |
| 44 | - protected $icingaAPI = NULL; |
|
| 44 | + protected $icingaAPI=NULL; |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | 47 | |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | { |
| 58 | 58 | $this->redirectNow('trapdirector/settings?message=No database prefix'); |
| 59 | 59 | } |
| 60 | - $this->moduleConfig = new TrapModuleConfig($db_prefix); |
|
| 60 | + $this->moduleConfig=new TrapModuleConfig($db_prefix); |
|
| 61 | 61 | } |
| 62 | 62 | return $this->moduleConfig; |
| 63 | 63 | } |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function getTrapListTable() { |
| 70 | 70 | if ($this->trapTableList == Null) { |
| 71 | - $this->trapTableList = new TrapTableList(); |
|
| 71 | + $this->trapTableList=new TrapTableList(); |
|
| 72 | 72 | $this->trapTableList->setConfig($this->getModuleConfig()); |
| 73 | 73 | } |
| 74 | 74 | return $this->trapTableList; |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | { |
| 82 | 82 | if ($this->trapTableHostList == Null) |
| 83 | 83 | { |
| 84 | - $this->trapTableHostList = new TrapTableHostList(); |
|
| 84 | + $this->trapTableHostList=new TrapTableHostList(); |
|
| 85 | 85 | $this->trapTableHostList->setConfig($this->getModuleConfig()); |
| 86 | 86 | } |
| 87 | 87 | return $this->trapTableHostList; |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | { |
| 95 | 95 | if ($this->handlerTableList == Null) |
| 96 | 96 | { |
| 97 | - $this->handlerTableList = new HandlerTableList(); |
|
| 97 | + $this->handlerTableList=new HandlerTableList(); |
|
| 98 | 98 | $this->handlerTableList->setConfig($this->getModuleConfig()); |
| 99 | 99 | } |
| 100 | 100 | return $this->handlerTableList; |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | { |
| 108 | 108 | if ($this->UIDatabase == Null) |
| 109 | 109 | { |
| 110 | - $this->UIDatabase = new UIDatabase($this); |
|
| 110 | + $this->UIDatabase=new UIDatabase($this); |
|
| 111 | 111 | |
| 112 | 112 | } |
| 113 | 113 | return $this->UIDatabase; |
@@ -121,13 +121,13 @@ discard block |
||
| 121 | 121 | { |
| 122 | 122 | if ($this->icingaAPI === NULL) |
| 123 | 123 | { |
| 124 | - $host = $this->Config()->get('config', 'icingaAPI_host'); |
|
| 125 | - $port = $this->Config()->get('config', 'icingaAPI_port'); |
|
| 126 | - $user = $this->Config()->get('config', 'icingaAPI_user'); |
|
| 127 | - $pass = $this->Config()->get('config', 'icingaAPI_password'); |
|
| 128 | - $this->icingaAPI = new Icinga2API($host,$port); |
|
| 124 | + $host=$this->Config()->get('config', 'icingaAPI_host'); |
|
| 125 | + $port=$this->Config()->get('config', 'icingaAPI_port'); |
|
| 126 | + $user=$this->Config()->get('config', 'icingaAPI_user'); |
|
| 127 | + $pass=$this->Config()->get('config', 'icingaAPI_password'); |
|
| 128 | + $this->icingaAPI=new Icinga2API($host, $port); |
|
| 129 | 129 | $this->icingaAPI->setCredentials($user, $pass); |
| 130 | - list($ret,$message) = $this->icingaAPI->test(NULL); |
|
| 130 | + list($ret, $message)=$this->icingaAPI->test(NULL); |
|
| 131 | 131 | if ($ret === TRUE) |
| 132 | 132 | { |
| 133 | 133 | return $this->getUIDatabase(); |
@@ -138,32 +138,32 @@ discard block |
||
| 138 | 138 | |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
| 141 | + protected function applyPaginationLimits(Paginatable $paginatable, $limit=25, $offset=null) |
|
| 142 | 142 | { |
| 143 | - $limit = $this->params->get('limit', $limit); |
|
| 144 | - $page = $this->params->get('page', $offset); |
|
| 143 | + $limit=$this->params->get('limit', $limit); |
|
| 144 | + $page=$this->params->get('page', $offset); |
|
| 145 | 145 | |
| 146 | 146 | $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
| 147 | 147 | |
| 148 | 148 | return $paginatable; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - public function displayExitError($source,$message) |
|
| 151 | + public function displayExitError($source, $message) |
|
| 152 | 152 | { // TODO : check better ways to transmit data (with POST ?) |
| 153 | 153 | $this->redirectNow('trapdirector/error?source='.$source.'&message='.$message); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | protected function checkReadPermission() |
| 157 | 157 | { |
| 158 | - if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
| 159 | - $this->displayExitError('Permissions','No permission fo view content'); |
|
| 158 | + if (!$this->Auth()->hasPermission('trapdirector/view')) { |
|
| 159 | + $this->displayExitError('Permissions', 'No permission fo view content'); |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | protected function checkConfigPermission() |
| 164 | 164 | { |
| 165 | - if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
| 166 | - $this->displayExitError('Permissions','No permission fo configure'); |
|
| 165 | + if (!$this->Auth()->hasPermission('trapdirector/config')) { |
|
| 166 | + $this->displayExitError('Permissions', 'No permission fo configure'); |
|
| 167 | 167 | } |
| 168 | 168 | } |
| 169 | 169 | |
@@ -174,10 +174,10 @@ discard block |
||
| 174 | 174 | */ |
| 175 | 175 | protected function checkModuleConfigPermission($check=0) |
| 176 | 176 | { |
| 177 | - if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
| 177 | + if (!$this->Auth()->hasPermission('trapdirector/module_config')) { |
|
| 178 | 178 | if ($check == 0) |
| 179 | 179 | { |
| 180 | - $this->displayExitError('Permissions','No permission fo configure module'); |
|
| 180 | + $this->displayExitError('Permissions', 'No permission fo configure module'); |
|
| 181 | 181 | } |
| 182 | 182 | return false; |
| 183 | 183 | } |
@@ -189,10 +189,10 @@ discard block |
||
| 189 | 189 | { // TODO : try/catch here ? or within caller |
| 190 | 190 | if ($this->trapClass == null) |
| 191 | 191 | { |
| 192 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 192 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
| 193 | 193 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 194 | 194 | //$debug_level=4; |
| 195 | - $this->trapClass = new Trap($icingaweb2_etc); |
|
| 195 | + $this->trapClass=new Trap($icingaweb2_etc); |
|
| 196 | 196 | //$Trap->setLogging($debug_level,'syslog'); |
| 197 | 197 | } |
| 198 | 198 | return $this->trapClass; |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | { |
| 208 | 208 | if ($this->MIBData == null) |
| 209 | 209 | { |
| 210 | - $dbConn = $this->getUIDatabase()->getDbConn(); |
|
| 210 | + $dbConn=$this->getUIDatabase()->getDbConn(); |
|
| 211 | 211 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 212 | 212 | $this->MIBData=new MIBLoader( |
| 213 | 213 | $this->Config()->get('config', 'snmptranslate'), |
@@ -227,10 +227,10 @@ discard block |
||
| 227 | 227 | protected function isDirectorInstalled() |
| 228 | 228 | { |
| 229 | 229 | $output=array(); |
| 230 | - exec('icingacli module list',$output); |
|
| 230 | + exec('icingacli module list', $output); |
|
| 231 | 231 | foreach ($output as $line) |
| 232 | 232 | { |
| 233 | - if (preg_match('/^director .*enabled/',$line)) |
|
| 233 | + if (preg_match('/^director .*enabled/', $line)) |
|
| 234 | 234 | { |
| 235 | 235 | return true; |
| 236 | 236 | } |
@@ -23,14 +23,14 @@ discard block |
||
| 23 | 23 | public function getHostByIP($ip) |
| 24 | 24 | { |
| 25 | 25 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 26 | - $dbConn = $this->getIdoDbConn(); |
|
| 26 | + $dbConn=$this->getIdoDbConn(); |
|
| 27 | 27 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 28 | 28 | |
| 29 | 29 | // TODO : check for SQL injections |
| 30 | 30 | $query=$dbConn->select() |
| 31 | 31 | ->from( |
| 32 | 32 | array('a' => 'icinga_objects'), |
| 33 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 33 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
| 34 | 34 | ->join( |
| 35 | 35 | array('b' => 'icinga_hosts'), |
| 36 | 36 | 'b.host_object_id=a.object_id', |
@@ -46,14 +46,14 @@ discard block |
||
| 46 | 46 | public function getHostByName($name) |
| 47 | 47 | { |
| 48 | 48 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 49 | - $dbConn = $this->getIdoDbConn(); |
|
| 49 | + $dbConn=$this->getIdoDbConn(); |
|
| 50 | 50 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 51 | 51 | |
| 52 | 52 | // TODO : check for SQL injections |
| 53 | 53 | $query=$dbConn->select() |
| 54 | 54 | ->from( |
| 55 | 55 | array('a' => 'icinga_objects'), |
| 56 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 56 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
| 57 | 57 | ->join( |
| 58 | 58 | array('b' => 'icinga_hosts'), |
| 59 | 59 | 'b.host_object_id=a.object_id', |
@@ -69,13 +69,13 @@ discard block |
||
| 69 | 69 | public function getHostGroupByName($ip) |
| 70 | 70 | { |
| 71 | 71 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 72 | - $dbConn = $this->getIdoDbConn(); |
|
| 72 | + $dbConn=$this->getIdoDbConn(); |
|
| 73 | 73 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 74 | 74 | // TODO : check for SQL injections |
| 75 | 75 | $query=$dbConn->select() |
| 76 | 76 | ->from( |
| 77 | 77 | array('a' => 'icinga_objects'), |
| 78 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 78 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
| 79 | 79 | ->join( |
| 80 | 80 | array('b' => 'icinga_hostgroups'), |
| 81 | 81 | 'b.hostgroup_object_id=a.object_id', |
@@ -91,8 +91,8 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function getHostInfoByID($id) |
| 93 | 93 | { |
| 94 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 95 | - $dbConn = $this->getIdoDbConn(); |
|
| 94 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 95 | + $dbConn=$this->getIdoDbConn(); |
|
| 96 | 96 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 97 | 97 | $query=$dbConn->select() |
| 98 | 98 | ->from( |
@@ -113,18 +113,18 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | public function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
| 115 | 115 | { |
| 116 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 117 | - $dbConn = $this->getIdoDbConn(); |
|
| 116 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 117 | + $dbConn=$this->getIdoDbConn(); |
|
| 118 | 118 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 119 | 119 | $query=$dbConn->select() |
| 120 | 120 | ->from( |
| 121 | 121 | array('a' => 'icinga_objects'), |
| 122 | - array('name' => 'a.name1','id' => 'a.object_id')) |
|
| 122 | + array('name' => 'a.name1', 'id' => 'a.object_id')) |
|
| 123 | 123 | ->join( |
| 124 | 124 | array('b' => 'icinga_hosts'), |
| 125 | 125 | 'b.host_object_id=a.object_id', |
| 126 | - array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
| 127 | - ->where('a.object_id = ?',$id); |
|
| 126 | + array('display_name' => 'b.display_name', 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
| 127 | + ->where('a.object_id = ?', $id); |
|
| 128 | 128 | return $dbConn->fetchRow($query); |
| 129 | 129 | } |
| 130 | 130 | |
@@ -136,17 +136,17 @@ discard block |
||
| 136 | 136 | public function getServicesByHostid($id) |
| 137 | 137 | { |
| 138 | 138 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 139 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 140 | - $dbConn = $this->getIdoDbConn(); |
|
| 139 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 140 | + $dbConn=$this->getIdoDbConn(); |
|
| 141 | 141 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 142 | 142 | $query=$dbConn->select() |
| 143 | 143 | ->from( |
| 144 | 144 | array('s' => 'icinga_services'), |
| 145 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 145 | + array('name' => 's.display_name', 'id' => 's.service_object_id')) |
|
| 146 | 146 | ->join( |
| 147 | 147 | array('a' => 'icinga_objects'), |
| 148 | 148 | 's.service_object_id=a.object_id', |
| 149 | - array('is_active'=>'a.is_active','name2'=>'a.name2')) |
|
| 149 | + array('is_active'=>'a.is_active', 'name2'=>'a.name2')) |
|
| 150 | 150 | ->where('s.host_object_id='.$id.' AND a.is_active = 1'); |
| 151 | 151 | return $dbConn->fetchAll($query); |
| 152 | 152 | } |
@@ -159,8 +159,8 @@ discard block |
||
| 159 | 159 | */ |
| 160 | 160 | public function getServicesByHostGroupid($id) |
| 161 | 161 | { |
| 162 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 163 | - $dbConn = $this->getIdoDbConn(); |
|
| 162 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 163 | + $dbConn=$this->getIdoDbConn(); |
|
| 164 | 164 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 165 | 165 | $query=$dbConn->select() |
| 166 | 166 | ->from( |
@@ -177,11 +177,11 @@ discard block |
||
| 177 | 177 | foreach ($hosts as $key => $host) |
| 178 | 178 | { // For each host, get all services and add in common_services if not found or add counter |
| 179 | 179 | $host_services=$this->getServicesByHostid($host->host_object_id); |
| 180 | - foreach($host_services as $service) |
|
| 180 | + foreach ($host_services as $service) |
|
| 181 | 181 | { |
| 182 | 182 | if (isset($common_services[$service->name2]['num'])) |
| 183 | 183 | { |
| 184 | - $common_services[$service->name2]['num'] +=1; |
|
| 184 | + $common_services[$service->name2]['num']+=1; |
|
| 185 | 185 | } |
| 186 | 186 | else |
| 187 | 187 | { |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | { |
| 198 | 198 | if ($common_services[$key]['num'] == $num_hosts) |
| 199 | 199 | { |
| 200 | - array_push($result,array($key,$common_services[$key]['name'])); |
|
| 200 | + array_push($result, array($key, $common_services[$key]['name'])); |
|
| 201 | 201 | } |
| 202 | 202 | } |
| 203 | 203 | |
@@ -210,9 +210,9 @@ discard block |
||
| 210 | 210 | * @param $name string service name |
| 211 | 211 | * @return array service id |
| 212 | 212 | */ |
| 213 | - public function getServiceIDByName($hostname,$name) |
|
| 213 | + public function getServiceIDByName($hostname, $name) |
|
| 214 | 214 | { |
| 215 | - $dbConn = $this->getIdoDbConn(); |
|
| 215 | + $dbConn=$this->getIdoDbConn(); |
|
| 216 | 216 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 217 | 217 | |
| 218 | 218 | if ($name == null) |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | $query=$dbConn->select() |
| 224 | 224 | ->from( |
| 225 | 225 | array('s' => 'icinga_services'), |
| 226 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 226 | + array('name' => 's.display_name', 'id' => 's.service_object_id')) |
|
| 227 | 227 | ->join( |
| 228 | 228 | array('a' => 'icinga_objects'), |
| 229 | 229 | 's.service_object_id=a.object_id', |
@@ -241,14 +241,14 @@ discard block |
||
| 241 | 241 | public function getObjectNameByid($id) |
| 242 | 242 | { |
| 243 | 243 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 244 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 245 | - $dbConn = $this->getIdoDbConn(); |
|
| 244 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
| 245 | + $dbConn=$this->getIdoDbConn(); |
|
| 246 | 246 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 247 | 247 | |
| 248 | 248 | $query=$dbConn->select() |
| 249 | 249 | ->from( |
| 250 | 250 | array('a' => 'icinga_objects'), |
| 251 | - array('name1' => 'a.name1','name2' => 'a.name2')) |
|
| 251 | + array('name1' => 'a.name1', 'name2' => 'a.name2')) |
|
| 252 | 252 | ->where('a.object_id='.$id.' AND a.is_active = 1'); |
| 253 | 253 | |
| 254 | 254 | return $dbConn->fetchRow($query); |
@@ -24,7 +24,9 @@ discard block |
||
| 24 | 24 | { |
| 25 | 25 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 26 | 26 | $dbConn = $this->getIdoDbConn(); |
| 27 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 27 | + if ($dbConn === null) { |
|
| 28 | + throw new \ErrorException('uncatched db error'); |
|
| 29 | + } |
|
| 28 | 30 | |
| 29 | 31 | // TODO : check for SQL injections |
| 30 | 32 | $query=$dbConn->select() |
@@ -47,7 +49,9 @@ discard block |
||
| 47 | 49 | { |
| 48 | 50 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 49 | 51 | $dbConn = $this->getIdoDbConn(); |
| 50 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 52 | + if ($dbConn === null) { |
|
| 53 | + throw new \ErrorException('uncatched db error'); |
|
| 54 | + } |
|
| 51 | 55 | |
| 52 | 56 | // TODO : check for SQL injections |
| 53 | 57 | $query=$dbConn->select() |
@@ -70,7 +74,9 @@ discard block |
||
| 70 | 74 | { |
| 71 | 75 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 72 | 76 | $dbConn = $this->getIdoDbConn(); |
| 73 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 77 | + if ($dbConn === null) { |
|
| 78 | + throw new \ErrorException('uncatched db error'); |
|
| 79 | + } |
|
| 74 | 80 | // TODO : check for SQL injections |
| 75 | 81 | $query=$dbConn->select() |
| 76 | 82 | ->from( |
@@ -93,7 +99,9 @@ discard block |
||
| 93 | 99 | { |
| 94 | 100 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 95 | 101 | $dbConn = $this->getIdoDbConn(); |
| 96 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 102 | + if ($dbConn === null) { |
|
| 103 | + throw new \ErrorException('uncatched db error'); |
|
| 104 | + } |
|
| 97 | 105 | $query=$dbConn->select() |
| 98 | 106 | ->from( |
| 99 | 107 | array('a' => 'icinga_objects'), |
@@ -115,7 +123,9 @@ discard block |
||
| 115 | 123 | { |
| 116 | 124 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 117 | 125 | $dbConn = $this->getIdoDbConn(); |
| 118 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 126 | + if ($dbConn === null) { |
|
| 127 | + throw new \ErrorException('uncatched db error'); |
|
| 128 | + } |
|
| 119 | 129 | $query=$dbConn->select() |
| 120 | 130 | ->from( |
| 121 | 131 | array('a' => 'icinga_objects'), |
@@ -138,7 +148,9 @@ discard block |
||
| 138 | 148 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 139 | 149 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 140 | 150 | $dbConn = $this->getIdoDbConn(); |
| 141 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 151 | + if ($dbConn === null) { |
|
| 152 | + throw new \ErrorException('uncatched db error'); |
|
| 153 | + } |
|
| 142 | 154 | $query=$dbConn->select() |
| 143 | 155 | ->from( |
| 144 | 156 | array('s' => 'icinga_services'), |
@@ -161,7 +173,9 @@ discard block |
||
| 161 | 173 | { |
| 162 | 174 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 163 | 175 | $dbConn = $this->getIdoDbConn(); |
| 164 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 176 | + if ($dbConn === null) { |
|
| 177 | + throw new \ErrorException('uncatched db error'); |
|
| 178 | + } |
|
| 165 | 179 | $query=$dbConn->select() |
| 166 | 180 | ->from( |
| 167 | 181 | array('s' => 'icinga_hostgroup_members'), |
@@ -182,8 +196,7 @@ discard block |
||
| 182 | 196 | if (isset($common_services[$service->name2]['num'])) |
| 183 | 197 | { |
| 184 | 198 | $common_services[$service->name2]['num'] +=1; |
| 185 | - } |
|
| 186 | - else |
|
| 199 | + } else |
|
| 187 | 200 | { |
| 188 | 201 | $common_services[$service->name2]['num']=1; |
| 189 | 202 | $common_services[$service->name2]['name']=$service->name; |
@@ -213,7 +226,9 @@ discard block |
||
| 213 | 226 | public function getServiceIDByName($hostname,$name) |
| 214 | 227 | { |
| 215 | 228 | $dbConn = $this->getIdoDbConn(); |
| 216 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 229 | + if ($dbConn === null) { |
|
| 230 | + throw new \ErrorException('uncatched db error'); |
|
| 231 | + } |
|
| 217 | 232 | |
| 218 | 233 | if ($name == null) |
| 219 | 234 | { |
@@ -243,7 +258,9 @@ discard block |
||
| 243 | 258 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
| 244 | 259 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
| 245 | 260 | $dbConn = $this->getIdoDbConn(); |
| 246 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 261 | + if ($dbConn === null) { |
|
| 262 | + throw new \ErrorException('uncatched db error'); |
|
| 263 | + } |
|
| 247 | 264 | |
| 248 | 265 | $query=$dbConn->select() |
| 249 | 266 | ->from( |
@@ -18,247 +18,247 @@ |
||
| 18 | 18 | trait IdoDBQuery |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** @return TrapsController */ |
|
| 22 | - abstract protected function getTrapCtrl(); |
|
| 21 | + /** @return TrapsController */ |
|
| 22 | + abstract protected function getTrapCtrl(); |
|
| 23 | 23 | |
| 24 | - /** @return Zend_Db_Adapter_Abstract : returns DB connexion or null on error */ |
|
| 25 | - abstract public function getIdoDbConn(); |
|
| 24 | + /** @return Zend_Db_Adapter_Abstract : returns DB connexion or null on error */ |
|
| 25 | + abstract public function getIdoDbConn(); |
|
| 26 | 26 | |
| 27 | - /** Get host(s) by IP (v4 or v6) or by name in IDO database |
|
| 28 | - * does not catch exceptions |
|
| 29 | - * @return array of objects ( name, id (object_id), display_name) |
|
| 30 | - */ |
|
| 31 | - public function getHostByIP($ip) |
|
| 32 | - { |
|
| 33 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 34 | - $dbConn = $this->getIdoDbConn(); |
|
| 35 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 27 | + /** Get host(s) by IP (v4 or v6) or by name in IDO database |
|
| 28 | + * does not catch exceptions |
|
| 29 | + * @return array of objects ( name, id (object_id), display_name) |
|
| 30 | + */ |
|
| 31 | + public function getHostByIP($ip) |
|
| 32 | + { |
|
| 33 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 34 | + $dbConn = $this->getIdoDbConn(); |
|
| 35 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 36 | 36 | |
| 37 | - // TODO : check for SQL injections |
|
| 38 | - $query=$dbConn->select() |
|
| 39 | - ->from( |
|
| 40 | - array('a' => 'icinga_objects'), |
|
| 41 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 42 | - ->join( |
|
| 43 | - array('b' => 'icinga_hosts'), |
|
| 44 | - 'b.host_object_id=a.object_id', |
|
| 45 | - array('display_name' => 'b.display_name')) |
|
| 46 | - ->where("(b.address LIKE '%".$ip."%' OR b.address6 LIKE '%".$ip."%' OR a.name1 LIKE '%".$ip."%' OR b.display_name LIKE '%".$ip."%') and a.is_active = 1"); |
|
| 47 | - return $dbConn->fetchAll($query); |
|
| 48 | - } |
|
| 37 | + // TODO : check for SQL injections |
|
| 38 | + $query=$dbConn->select() |
|
| 39 | + ->from( |
|
| 40 | + array('a' => 'icinga_objects'), |
|
| 41 | + array('name' => 'a.name1','id' => 'object_id')) |
|
| 42 | + ->join( |
|
| 43 | + array('b' => 'icinga_hosts'), |
|
| 44 | + 'b.host_object_id=a.object_id', |
|
| 45 | + array('display_name' => 'b.display_name')) |
|
| 46 | + ->where("(b.address LIKE '%".$ip."%' OR b.address6 LIKE '%".$ip."%' OR a.name1 LIKE '%".$ip."%' OR b.display_name LIKE '%".$ip."%') and a.is_active = 1"); |
|
| 47 | + return $dbConn->fetchAll($query); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** Get host(s) by name in IDO database |
|
| 51 | - * does not catch exceptions |
|
| 52 | - * @return array of objects ( name, id (object_id), display_name) |
|
| 53 | - */ |
|
| 54 | - public function getHostByName($name) |
|
| 55 | - { |
|
| 56 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 57 | - $dbConn = $this->getIdoDbConn(); |
|
| 58 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 50 | + /** Get host(s) by name in IDO database |
|
| 51 | + * does not catch exceptions |
|
| 52 | + * @return array of objects ( name, id (object_id), display_name) |
|
| 53 | + */ |
|
| 54 | + public function getHostByName($name) |
|
| 55 | + { |
|
| 56 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 57 | + $dbConn = $this->getIdoDbConn(); |
|
| 58 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 59 | 59 | |
| 60 | - // TODO : check for SQL injections |
|
| 61 | - $query=$dbConn->select() |
|
| 62 | - ->from( |
|
| 63 | - array('a' => 'icinga_objects'), |
|
| 64 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 65 | - ->join( |
|
| 66 | - array('b' => 'icinga_hosts'), |
|
| 67 | - 'b.host_object_id=a.object_id', |
|
| 68 | - array('display_name' => 'b.display_name')) |
|
| 69 | - ->where("a.name1 = '$name'"); |
|
| 70 | - return $dbConn->fetchAll($query); |
|
| 71 | - } |
|
| 60 | + // TODO : check for SQL injections |
|
| 61 | + $query=$dbConn->select() |
|
| 62 | + ->from( |
|
| 63 | + array('a' => 'icinga_objects'), |
|
| 64 | + array('name' => 'a.name1','id' => 'object_id')) |
|
| 65 | + ->join( |
|
| 66 | + array('b' => 'icinga_hosts'), |
|
| 67 | + 'b.host_object_id=a.object_id', |
|
| 68 | + array('display_name' => 'b.display_name')) |
|
| 69 | + ->where("a.name1 = '$name'"); |
|
| 70 | + return $dbConn->fetchAll($query); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** Get host groups by name in IDO database |
|
| 74 | - * does not catch exceptions |
|
| 75 | - * @return array of objects ( name, id (object_id), display_name) |
|
| 76 | - */ |
|
| 77 | - public function getHostGroupByName($ip) |
|
| 78 | - { |
|
| 79 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 80 | - $dbConn = $this->getIdoDbConn(); |
|
| 81 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 82 | - // TODO : check for SQL injections |
|
| 83 | - $query=$dbConn->select() |
|
| 84 | - ->from( |
|
| 85 | - array('a' => 'icinga_objects'), |
|
| 86 | - array('name' => 'a.name1','id' => 'object_id')) |
|
| 87 | - ->join( |
|
| 88 | - array('b' => 'icinga_hostgroups'), |
|
| 89 | - 'b.hostgroup_object_id=a.object_id', |
|
| 90 | - array('display_name' => 'b.alias')) |
|
| 91 | - ->where("(a.name1 LIKE '%".$ip."%' OR b.alias LIKE '%".$ip."%') and a.is_active = 1"); |
|
| 92 | - return $dbConn->fetchAll($query); |
|
| 93 | - } |
|
| 73 | + /** Get host groups by name in IDO database |
|
| 74 | + * does not catch exceptions |
|
| 75 | + * @return array of objects ( name, id (object_id), display_name) |
|
| 76 | + */ |
|
| 77 | + public function getHostGroupByName($ip) |
|
| 78 | + { |
|
| 79 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 80 | + $dbConn = $this->getIdoDbConn(); |
|
| 81 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 82 | + // TODO : check for SQL injections |
|
| 83 | + $query=$dbConn->select() |
|
| 84 | + ->from( |
|
| 85 | + array('a' => 'icinga_objects'), |
|
| 86 | + array('name' => 'a.name1','id' => 'object_id')) |
|
| 87 | + ->join( |
|
| 88 | + array('b' => 'icinga_hostgroups'), |
|
| 89 | + 'b.hostgroup_object_id=a.object_id', |
|
| 90 | + array('display_name' => 'b.alias')) |
|
| 91 | + ->where("(a.name1 LIKE '%".$ip."%' OR b.alias LIKE '%".$ip."%') and a.is_active = 1"); |
|
| 92 | + return $dbConn->fetchAll($query); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** Get host IP (v4 and v6) by name in IDO database |
|
| 96 | - * does not catch exceptions |
|
| 97 | - * @return array ( name, display_name, ip4, ip6) |
|
| 98 | - */ |
|
| 99 | - public function getHostInfoByID($id) |
|
| 100 | - { |
|
| 101 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 102 | - $dbConn = $this->getIdoDbConn(); |
|
| 103 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 104 | - $query=$dbConn->select() |
|
| 105 | - ->from( |
|
| 106 | - array('a' => 'icinga_objects'), |
|
| 107 | - array('name' => 'a.name1')) |
|
| 108 | - ->join( |
|
| 109 | - array('b' => 'icinga_hosts'), |
|
| 110 | - 'b.host_object_id=a.object_id', |
|
| 111 | - array('ip4' => 'b.address', 'ip6' => 'b.address6', 'display_name' => 'b.display_name')) |
|
| 112 | - ->where("a.object_id = '".$id."'"); |
|
| 113 | - return $dbConn->fetchRow($query); |
|
| 114 | - } |
|
| 95 | + /** Get host IP (v4 and v6) by name in IDO database |
|
| 96 | + * does not catch exceptions |
|
| 97 | + * @return array ( name, display_name, ip4, ip6) |
|
| 98 | + */ |
|
| 99 | + public function getHostInfoByID($id) |
|
| 100 | + { |
|
| 101 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 102 | + $dbConn = $this->getIdoDbConn(); |
|
| 103 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 104 | + $query=$dbConn->select() |
|
| 105 | + ->from( |
|
| 106 | + array('a' => 'icinga_objects'), |
|
| 107 | + array('name' => 'a.name1')) |
|
| 108 | + ->join( |
|
| 109 | + array('b' => 'icinga_hosts'), |
|
| 110 | + 'b.host_object_id=a.object_id', |
|
| 111 | + array('ip4' => 'b.address', 'ip6' => 'b.address6', 'display_name' => 'b.display_name')) |
|
| 112 | + ->where("a.object_id = '".$id."'"); |
|
| 113 | + return $dbConn->fetchRow($query); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** Get host by objectid in IDO database |
|
| 117 | - * does not catch exceptions |
|
| 118 | - * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
| 119 | - */ |
|
| 120 | - public function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
|
| 121 | - { |
|
| 122 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 123 | - $dbConn = $this->getIdoDbConn(); |
|
| 124 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 125 | - $query=$dbConn->select() |
|
| 126 | - ->from( |
|
| 127 | - array('a' => 'icinga_objects'), |
|
| 128 | - array('name' => 'a.name1','id' => 'a.object_id')) |
|
| 129 | - ->join( |
|
| 130 | - array('b' => 'icinga_hosts'), |
|
| 131 | - 'b.host_object_id=a.object_id', |
|
| 132 | - array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
| 133 | - ->where('a.object_id = ?',$id); |
|
| 134 | - return $dbConn->fetchRow($query); |
|
| 135 | - } |
|
| 116 | + /** Get host by objectid in IDO database |
|
| 117 | + * does not catch exceptions |
|
| 118 | + * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
| 119 | + */ |
|
| 120 | + public function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
|
| 121 | + { |
|
| 122 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 123 | + $dbConn = $this->getIdoDbConn(); |
|
| 124 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 125 | + $query=$dbConn->select() |
|
| 126 | + ->from( |
|
| 127 | + array('a' => 'icinga_objects'), |
|
| 128 | + array('name' => 'a.name1','id' => 'a.object_id')) |
|
| 129 | + ->join( |
|
| 130 | + array('b' => 'icinga_hosts'), |
|
| 131 | + 'b.host_object_id=a.object_id', |
|
| 132 | + array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
| 133 | + ->where('a.object_id = ?',$id); |
|
| 134 | + return $dbConn->fetchRow($query); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** Get services from object ( host_object_id) in IDO database |
|
| 138 | - * does not catch exceptions |
|
| 139 | - * @param $id int object_id |
|
| 140 | - * @return array display_name (of service), service_object_id |
|
| 141 | - */ |
|
| 142 | - public function getServicesByHostid($id) |
|
| 143 | - { |
|
| 144 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 145 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 146 | - $dbConn = $this->getIdoDbConn(); |
|
| 147 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 148 | - $query=$dbConn->select() |
|
| 149 | - ->from( |
|
| 150 | - array('s' => 'icinga_services'), |
|
| 151 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 152 | - ->join( |
|
| 153 | - array('a' => 'icinga_objects'), |
|
| 154 | - 's.service_object_id=a.object_id', |
|
| 155 | - array('is_active'=>'a.is_active','name2'=>'a.name2')) |
|
| 156 | - ->where('s.host_object_id='.$id.' AND a.is_active = 1'); |
|
| 157 | - return $dbConn->fetchAll($query); |
|
| 158 | - } |
|
| 137 | + /** Get services from object ( host_object_id) in IDO database |
|
| 138 | + * does not catch exceptions |
|
| 139 | + * @param $id int object_id |
|
| 140 | + * @return array display_name (of service), service_object_id |
|
| 141 | + */ |
|
| 142 | + public function getServicesByHostid($id) |
|
| 143 | + { |
|
| 144 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 145 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 146 | + $dbConn = $this->getIdoDbConn(); |
|
| 147 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 148 | + $query=$dbConn->select() |
|
| 149 | + ->from( |
|
| 150 | + array('s' => 'icinga_services'), |
|
| 151 | + array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 152 | + ->join( |
|
| 153 | + array('a' => 'icinga_objects'), |
|
| 154 | + 's.service_object_id=a.object_id', |
|
| 155 | + array('is_active'=>'a.is_active','name2'=>'a.name2')) |
|
| 156 | + ->where('s.host_object_id='.$id.' AND a.is_active = 1'); |
|
| 157 | + return $dbConn->fetchAll($query); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** Get services from hostgroup object id ( hostgroup_object_id) in IDO database |
|
| 161 | - * gets all hosts in hostgroup and return common services |
|
| 162 | - * does not catch exceptions |
|
| 163 | - * @param $id int object_id |
|
| 164 | - * @return array display_name (of service), service_object_id |
|
| 165 | - */ |
|
| 166 | - public function getServicesByHostGroupid($id) |
|
| 167 | - { |
|
| 168 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 169 | - $dbConn = $this->getIdoDbConn(); |
|
| 170 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 171 | - $query=$dbConn->select() |
|
| 172 | - ->from( |
|
| 173 | - array('s' => 'icinga_hostgroup_members'), |
|
| 174 | - array('host_object_id' => 's.host_object_id')) |
|
| 175 | - ->join( |
|
| 176 | - array('a' => 'icinga_hostgroups'), |
|
| 177 | - 's.hostgroup_id=a.hostgroup_id', |
|
| 178 | - 'hostgroup_object_id') |
|
| 179 | - ->where('a.hostgroup_object_id='.$id); |
|
| 180 | - $hosts=$dbConn->fetchAll($query); |
|
| 181 | - $common_services=array(); |
|
| 182 | - $num_hosts=count($hosts); |
|
| 183 | - foreach ($hosts as $key => $host) |
|
| 184 | - { // For each host, get all services and add in common_services if not found or add counter |
|
| 185 | - $host_services=$this->getServicesByHostid($host->host_object_id); |
|
| 186 | - foreach($host_services as $service) |
|
| 187 | - { |
|
| 188 | - if (isset($common_services[$service->name2]['num'])) |
|
| 189 | - { |
|
| 190 | - $common_services[$service->name2]['num'] +=1; |
|
| 191 | - } |
|
| 192 | - else |
|
| 193 | - { |
|
| 194 | - $common_services[$service->name2]['num']=1; |
|
| 195 | - $common_services[$service->name2]['name']=$service->name; |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - $result=array(); |
|
| 160 | + /** Get services from hostgroup object id ( hostgroup_object_id) in IDO database |
|
| 161 | + * gets all hosts in hostgroup and return common services |
|
| 162 | + * does not catch exceptions |
|
| 163 | + * @param $id int object_id |
|
| 164 | + * @return array display_name (of service), service_object_id |
|
| 165 | + */ |
|
| 166 | + public function getServicesByHostGroupid($id) |
|
| 167 | + { |
|
| 168 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 169 | + $dbConn = $this->getIdoDbConn(); |
|
| 170 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 171 | + $query=$dbConn->select() |
|
| 172 | + ->from( |
|
| 173 | + array('s' => 'icinga_hostgroup_members'), |
|
| 174 | + array('host_object_id' => 's.host_object_id')) |
|
| 175 | + ->join( |
|
| 176 | + array('a' => 'icinga_hostgroups'), |
|
| 177 | + 's.hostgroup_id=a.hostgroup_id', |
|
| 178 | + 'hostgroup_object_id') |
|
| 179 | + ->where('a.hostgroup_object_id='.$id); |
|
| 180 | + $hosts=$dbConn->fetchAll($query); |
|
| 181 | + $common_services=array(); |
|
| 182 | + $num_hosts=count($hosts); |
|
| 183 | + foreach ($hosts as $key => $host) |
|
| 184 | + { // For each host, get all services and add in common_services if not found or add counter |
|
| 185 | + $host_services=$this->getServicesByHostid($host->host_object_id); |
|
| 186 | + foreach($host_services as $service) |
|
| 187 | + { |
|
| 188 | + if (isset($common_services[$service->name2]['num'])) |
|
| 189 | + { |
|
| 190 | + $common_services[$service->name2]['num'] +=1; |
|
| 191 | + } |
|
| 192 | + else |
|
| 193 | + { |
|
| 194 | + $common_services[$service->name2]['num']=1; |
|
| 195 | + $common_services[$service->name2]['name']=$service->name; |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + $result=array(); |
|
| 200 | 200 | |
| 201 | - //print_r($common_services); |
|
| 202 | - foreach (array_keys($common_services) as $key) |
|
| 203 | - { |
|
| 204 | - if ($common_services[$key]['num'] == $num_hosts) |
|
| 205 | - { |
|
| 206 | - array_push($result,array($key,$common_services[$key]['name'])); |
|
| 207 | - } |
|
| 208 | - } |
|
| 201 | + //print_r($common_services); |
|
| 202 | + foreach (array_keys($common_services) as $key) |
|
| 203 | + { |
|
| 204 | + if ($common_services[$key]['num'] == $num_hosts) |
|
| 205 | + { |
|
| 206 | + array_push($result,array($key,$common_services[$key]['name'])); |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - return $result; |
|
| 211 | - } |
|
| 210 | + return $result; |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** Get services object id by host name / service name in IDO database |
|
| 214 | - * does not catch exceptions |
|
| 215 | - * @param $hostname string host name |
|
| 216 | - * @param $name string service name |
|
| 217 | - * @return array service id |
|
| 218 | - */ |
|
| 219 | - public function getServiceIDByName($hostname,$name) |
|
| 220 | - { |
|
| 221 | - $dbConn = $this->getIdoDbConn(); |
|
| 222 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 213 | + /** Get services object id by host name / service name in IDO database |
|
| 214 | + * does not catch exceptions |
|
| 215 | + * @param $hostname string host name |
|
| 216 | + * @param $name string service name |
|
| 217 | + * @return array service id |
|
| 218 | + */ |
|
| 219 | + public function getServiceIDByName($hostname,$name) |
|
| 220 | + { |
|
| 221 | + $dbConn = $this->getIdoDbConn(); |
|
| 222 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 223 | 223 | |
| 224 | - if ($name == null) |
|
| 225 | - { |
|
| 226 | - return array(); |
|
| 227 | - } |
|
| 224 | + if ($name == null) |
|
| 225 | + { |
|
| 226 | + return array(); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - $query=$dbConn->select() |
|
| 230 | - ->from( |
|
| 231 | - array('s' => 'icinga_services'), |
|
| 232 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 233 | - ->join( |
|
| 234 | - array('a' => 'icinga_objects'), |
|
| 235 | - 's.service_object_id=a.object_id', |
|
| 236 | - 'is_active') |
|
| 237 | - ->where('a.name2=\''.$name.'\' AND a.name1=\''.$hostname.'\' AND a.is_active = 1'); |
|
| 229 | + $query=$dbConn->select() |
|
| 230 | + ->from( |
|
| 231 | + array('s' => 'icinga_services'), |
|
| 232 | + array('name' => 's.display_name','id' => 's.service_object_id')) |
|
| 233 | + ->join( |
|
| 234 | + array('a' => 'icinga_objects'), |
|
| 235 | + 's.service_object_id=a.object_id', |
|
| 236 | + 'is_active') |
|
| 237 | + ->where('a.name2=\''.$name.'\' AND a.name1=\''.$hostname.'\' AND a.is_active = 1'); |
|
| 238 | 238 | |
| 239 | - return $dbConn->fetchAll($query); |
|
| 240 | - } |
|
| 239 | + return $dbConn->fetchAll($query); |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** Get object name from object_id in IDO database |
|
| 243 | - * does not catch exceptions |
|
| 244 | - * @param int $id object_id (default to null, used first if not null) |
|
| 245 | - * @return array name1 (host) name2 (service) |
|
| 246 | - */ |
|
| 247 | - public function getObjectNameByid($id) |
|
| 248 | - { |
|
| 249 | - // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 250 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 251 | - $dbConn = $this->getIdoDbConn(); |
|
| 252 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 242 | + /** Get object name from object_id in IDO database |
|
| 243 | + * does not catch exceptions |
|
| 244 | + * @param int $id object_id (default to null, used first if not null) |
|
| 245 | + * @return array name1 (host) name2 (service) |
|
| 246 | + */ |
|
| 247 | + public function getObjectNameByid($id) |
|
| 248 | + { |
|
| 249 | + // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
| 250 | + if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
| 251 | + $dbConn = $this->getIdoDbConn(); |
|
| 252 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 253 | 253 | |
| 254 | - $query=$dbConn->select() |
|
| 255 | - ->from( |
|
| 256 | - array('a' => 'icinga_objects'), |
|
| 257 | - array('name1' => 'a.name1','name2' => 'a.name2')) |
|
| 258 | - ->where('a.object_id='.$id.' AND a.is_active = 1'); |
|
| 254 | + $query=$dbConn->select() |
|
| 255 | + ->from( |
|
| 256 | + array('a' => 'icinga_objects'), |
|
| 257 | + array('name1' => 'a.name1','name2' => 'a.name2')) |
|
| 258 | + ->where('a.object_id='.$id.' AND a.is_active = 1'); |
|
| 259 | 259 | |
| 260 | - return $dbConn->fetchRow($query); |
|
| 261 | - } |
|
| 260 | + return $dbConn->fetchRow($query); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | 263 | |
| 264 | 264 | } |
| 265 | 265 | \ No newline at end of file |
@@ -74,8 +74,7 @@ discard block |
||
| 74 | 74 | } |
| 75 | 75 | $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
| 76 | 76 | return 1; |
| 77 | - } |
|
| 78 | - else |
|
| 77 | + } else |
|
| 79 | 78 | { |
| 80 | 79 | $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
| 81 | 80 | return 0; |
@@ -97,7 +96,9 @@ discard block |
||
| 97 | 96 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 98 | 97 | ', :syntax, :type_enum, :description )'; |
| 99 | 98 | |
| 100 | - if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 99 | + if ($this->getTrapsDB()->trapDBType == 'pgsql') { |
|
| 100 | + $sql .= 'RETURNING id'; |
|
| 101 | + } |
|
| 101 | 102 | |
| 102 | 103 | $sqlQuery=$db_conn->prepare($sql); |
| 103 | 104 | |
@@ -139,7 +140,9 @@ discard block |
||
| 139 | 140 | } |
| 140 | 141 | |
| 141 | 142 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 142 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 143 | + if ($inserted_id==false) { |
|
| 144 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 145 | + } |
|
| 143 | 146 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
| 144 | 147 | break; |
| 145 | 148 | default: |
@@ -12,183 +12,183 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | trait MibDatabase |
| 14 | 14 | { |
| 15 | - /** @var array $oidDesc MUST be overide by calling class */ |
|
| 16 | - //protected $oidDesc; |
|
| 15 | + /** @var array $oidDesc MUST be overide by calling class */ |
|
| 16 | + //protected $oidDesc; |
|
| 17 | 17 | |
| 18 | - /** @var array $dbOidIndex MUST be overide by calling class */ |
|
| 19 | - //protected $dbOidIndex; |
|
| 18 | + /** @var array $dbOidIndex MUST be overide by calling class */ |
|
| 19 | + //protected $dbOidIndex; |
|
| 20 | 20 | |
| 21 | - /** @return \Trapdirector\Logging */ |
|
| 22 | - abstract public function getLogging(); |
|
| 21 | + /** @return \Trapdirector\Logging */ |
|
| 22 | + abstract public function getLogging(); |
|
| 23 | 23 | |
| 24 | - /** @return \Trapdirector\Database */ |
|
| 25 | - abstract public function getTrapsDB(); |
|
| 24 | + /** @return \Trapdirector\Database */ |
|
| 25 | + abstract public function getTrapsDB(); |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 30 | - * and $this->oidDesc doe data |
|
| 31 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 32 | - */ |
|
| 33 | - public function update_oid() |
|
| 34 | - { |
|
| 35 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 36 | - // Quote description. |
|
| 37 | - $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
| 28 | + /** |
|
| 29 | + * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 30 | + * and $this->oidDesc doe data |
|
| 31 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 32 | + */ |
|
| 33 | + public function update_oid() |
|
| 34 | + { |
|
| 35 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 36 | + // Quote description. |
|
| 37 | + $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
| 38 | 38 | |
| 39 | - if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
| 40 | - { // oid exists in db, so update |
|
| 41 | - return $this->update_oid_update(); |
|
| 42 | - } |
|
| 43 | - // create new OID. |
|
| 44 | - return $this->update_oid_create(); |
|
| 39 | + if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
| 40 | + { // oid exists in db, so update |
|
| 41 | + return $this->update_oid_update(); |
|
| 42 | + } |
|
| 43 | + // create new OID. |
|
| 44 | + return $this->update_oid_create(); |
|
| 45 | 45 | |
| 46 | - } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Update object in DB with object in dbOidIndex if name/mib/type has changed. |
|
| 50 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 51 | - */ |
|
| 52 | - private function update_oid_update() |
|
| 53 | - { |
|
| 48 | + /** |
|
| 49 | + * Update object in DB with object in dbOidIndex if name/mib/type has changed. |
|
| 50 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 51 | + */ |
|
| 52 | + private function update_oid_update() |
|
| 53 | + { |
|
| 54 | 54 | |
| 55 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 55 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 56 | 56 | |
| 57 | - if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
| 58 | - { // newly created. |
|
| 59 | - return 0; |
|
| 60 | - } |
|
| 61 | - $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
| 62 | - $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
| 63 | - if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 64 | - $this->oidDesc['mib'] != $dbOid['mib'] || |
|
| 65 | - $this->oidDesc['type'] !=$dbOid['type'] |
|
| 66 | - ) |
|
| 67 | - { // Do update |
|
| 68 | - $sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '. |
|
| 69 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 70 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 71 | - ' WHERE id= :id'; |
|
| 72 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 57 | + if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
| 58 | + { // newly created. |
|
| 59 | + return 0; |
|
| 60 | + } |
|
| 61 | + $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
| 62 | + $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
| 63 | + if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 64 | + $this->oidDesc['mib'] != $dbOid['mib'] || |
|
| 65 | + $this->oidDesc['type'] !=$dbOid['type'] |
|
| 66 | + ) |
|
| 67 | + { // Do update |
|
| 68 | + $sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '. |
|
| 69 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 70 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 71 | + ' WHERE id= :id'; |
|
| 72 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 73 | 73 | |
| 74 | - $sqlParam=array( |
|
| 75 | - ':name' => $this->oidDesc['name'], |
|
| 76 | - ':type' => $this->oidDesc['type'], |
|
| 77 | - ':mib' => $this->oidDesc['mib'], |
|
| 78 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 79 | - ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 80 | - ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 81 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 82 | - ':description' => $this->oidDesc['description']??'null', |
|
| 83 | - ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
|
| 84 | - ); |
|
| 74 | + $sqlParam=array( |
|
| 75 | + ':name' => $this->oidDesc['name'], |
|
| 76 | + ':type' => $this->oidDesc['type'], |
|
| 77 | + ':mib' => $this->oidDesc['mib'], |
|
| 78 | + ':tc' => $this->oidDesc['textconv']??'null', |
|
| 79 | + ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 80 | + ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 81 | + ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 82 | + ':description' => $this->oidDesc['description']??'null', |
|
| 83 | + ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
|
| 84 | + ); |
|
| 85 | 85 | |
| 86 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 87 | - $this->getLogging()->log('Error in query : ' . $sql,ERROR,''); |
|
| 88 | - } |
|
| 89 | - $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 90 | - return 1; |
|
| 91 | - } |
|
| 92 | - else |
|
| 93 | - { |
|
| 94 | - $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 95 | - return 0; |
|
| 96 | - } |
|
| 97 | - } |
|
| 86 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 87 | + $this->getLogging()->log('Error in query : ' . $sql,ERROR,''); |
|
| 88 | + } |
|
| 89 | + $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 90 | + return 1; |
|
| 91 | + } |
|
| 92 | + else |
|
| 93 | + { |
|
| 94 | + $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 95 | + return 0; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Create object in DB with object in dbOidIndex |
|
| 101 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 102 | - */ |
|
| 103 | - private function update_oid_create() |
|
| 104 | - { |
|
| 105 | - // Insert data |
|
| 99 | + /** |
|
| 100 | + * Create object in DB with object in dbOidIndex |
|
| 101 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 102 | + */ |
|
| 103 | + private function update_oid_create() |
|
| 104 | + { |
|
| 105 | + // Insert data |
|
| 106 | 106 | |
| 107 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 108 | - $sql='INSERT INTO '.$this->getTrapsDB()->dbPrefix.'mib_cache '. |
|
| 109 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 110 | - ', syntax, type_enum , description ) ' . |
|
| 111 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 112 | - ', :syntax, :type_enum, :description )'; |
|
| 107 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 108 | + $sql='INSERT INTO '.$this->getTrapsDB()->dbPrefix.'mib_cache '. |
|
| 109 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 110 | + ', syntax, type_enum , description ) ' . |
|
| 111 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 112 | + ', :syntax, :type_enum, :description )'; |
|
| 113 | 113 | |
| 114 | - if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 114 | + if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 115 | 115 | |
| 116 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 116 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 117 | 117 | |
| 118 | - $sqlParam=array( |
|
| 119 | - ':oid' => $this->oidDesc['oid'], |
|
| 120 | - ':name' => $this->oidDesc['name'], |
|
| 121 | - ':type' => $this->oidDesc['type'], |
|
| 122 | - ':mib' => $this->oidDesc['mib'], |
|
| 123 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 124 | - ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
| 125 | - ':syntax' => $this->oidDesc['syntax']??'null', |
|
| 126 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 127 | - ':description' => $this->oidDesc['description']??'null' |
|
| 128 | - ); |
|
| 118 | + $sqlParam=array( |
|
| 119 | + ':oid' => $this->oidDesc['oid'], |
|
| 120 | + ':name' => $this->oidDesc['name'], |
|
| 121 | + ':type' => $this->oidDesc['type'], |
|
| 122 | + ':mib' => $this->oidDesc['mib'], |
|
| 123 | + ':tc' => $this->oidDesc['textconv']??'null', |
|
| 124 | + ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
| 125 | + ':syntax' => $this->oidDesc['syntax']??'null', |
|
| 126 | + ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 127 | + ':description' => $this->oidDesc['description']??'null' |
|
| 128 | + ); |
|
| 129 | 129 | |
| 130 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 131 | - $this->getLogging()->log('Error in query : ' . $sql,1,''); |
|
| 132 | - } |
|
| 130 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 131 | + $this->getLogging()->log('Error in query : ' . $sql,1,''); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - switch ($this->getTrapsDB()->trapDBType) |
|
| 135 | - { |
|
| 136 | - case 'pgsql': |
|
| 137 | - // Get last id to insert oid/values in secondary table |
|
| 138 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 139 | - $this->getLogging()->log('Error getting id - pgsql - ',1,''); |
|
| 140 | - } |
|
| 141 | - if (! isset($inserted_id_ret['id'])) { |
|
| 142 | - $this->getLogging()->log('Error getting id - pgsql - empty.',ERROR); |
|
| 143 | - return 0; |
|
| 144 | - } |
|
| 145 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
| 146 | - break; |
|
| 147 | - case 'mysql': |
|
| 148 | - // Get last id to insert oid/values in secondary table |
|
| 149 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 150 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 151 | - $this->getLogging()->log('Erreur getting id - mysql - ',ERROR); |
|
| 152 | - return 0; |
|
| 153 | - } |
|
| 134 | + switch ($this->getTrapsDB()->trapDBType) |
|
| 135 | + { |
|
| 136 | + case 'pgsql': |
|
| 137 | + // Get last id to insert oid/values in secondary table |
|
| 138 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 139 | + $this->getLogging()->log('Error getting id - pgsql - ',1,''); |
|
| 140 | + } |
|
| 141 | + if (! isset($inserted_id_ret['id'])) { |
|
| 142 | + $this->getLogging()->log('Error getting id - pgsql - empty.',ERROR); |
|
| 143 | + return 0; |
|
| 144 | + } |
|
| 145 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
| 146 | + break; |
|
| 147 | + case 'mysql': |
|
| 148 | + // Get last id to insert oid/values in secondary table |
|
| 149 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 150 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 151 | + $this->getLogging()->log('Erreur getting id - mysql - ',ERROR); |
|
| 152 | + return 0; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 156 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 157 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
| 158 | - break; |
|
| 159 | - default: |
|
| 160 | - $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType,ERROR); |
|
| 161 | - return 0; |
|
| 162 | - } |
|
| 155 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 156 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 157 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
| 158 | + break; |
|
| 159 | + default: |
|
| 160 | + $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType,ERROR); |
|
| 161 | + return 0; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - // Set as newly created. |
|
| 165 | - $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
| 166 | - return 2; |
|
| 167 | - } |
|
| 164 | + // Set as newly created. |
|
| 165 | + $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
| 166 | + return 2; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * get all objects for a trap. |
|
| 171 | - * @param integer $trapId |
|
| 172 | - * @return array : array of cached objects |
|
| 173 | - */ |
|
| 174 | - private function cache_db_objects($trapId) |
|
| 175 | - { |
|
| 176 | - $dbObjects=array(); // cache of objects for trap in db |
|
| 177 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 178 | - // Get all objects |
|
| 179 | - $sql='SELECT * FROM '.$this->getTrapsDB()->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 180 | - $this->getLogging()->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 181 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 182 | - $this->getLogging()->log('No result in query : ' . $sql,1,''); |
|
| 183 | - } |
|
| 184 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 169 | + /** |
|
| 170 | + * get all objects for a trap. |
|
| 171 | + * @param integer $trapId |
|
| 172 | + * @return array : array of cached objects |
|
| 173 | + */ |
|
| 174 | + private function cache_db_objects($trapId) |
|
| 175 | + { |
|
| 176 | + $dbObjects=array(); // cache of objects for trap in db |
|
| 177 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 178 | + // Get all objects |
|
| 179 | + $sql='SELECT * FROM '.$this->getTrapsDB()->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 180 | + $this->getLogging()->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 181 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 182 | + $this->getLogging()->log('No result in query : ' . $sql,1,''); |
|
| 183 | + } |
|
| 184 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 185 | 185 | |
| 186 | - foreach ($dbObjectsRaw as $val) |
|
| 187 | - { |
|
| 188 | - $dbObjects[$val['object_id']]=1; |
|
| 189 | - } |
|
| 190 | - return $dbObjects; |
|
| 191 | - } |
|
| 186 | + foreach ($dbObjectsRaw as $val) |
|
| 187 | + { |
|
| 188 | + $dbObjects[$val['object_id']]=1; |
|
| 189 | + } |
|
| 190 | + return $dbObjects; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | |
| 194 | 194 | } |
| 195 | 195 | \ No newline at end of file |
@@ -60,9 +60,9 @@ discard block |
||
| 60 | 60 | } |
| 61 | 61 | $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
| 62 | 62 | $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
| 63 | - if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 63 | + if ($this->oidDesc['name'] != $dbOid['name'] || |
|
| 64 | 64 | $this->oidDesc['mib'] != $dbOid['mib'] || |
| 65 | - $this->oidDesc['type'] !=$dbOid['type'] |
|
| 65 | + $this->oidDesc['type'] != $dbOid['type'] |
|
| 66 | 66 | ) |
| 67 | 67 | { // Do update |
| 68 | 68 | $sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '. |
@@ -75,23 +75,23 @@ discard block |
||
| 75 | 75 | ':name' => $this->oidDesc['name'], |
| 76 | 76 | ':type' => $this->oidDesc['type'], |
| 77 | 77 | ':mib' => $this->oidDesc['mib'], |
| 78 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 79 | - ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 80 | - ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 81 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 82 | - ':description' => $this->oidDesc['description']??'null', |
|
| 78 | + ':tc' => $this->oidDesc['textconv'] ?? 'null', |
|
| 79 | + ':display_hint' => $this->oidDesc['dispHint'] ?? 'null', |
|
| 80 | + ':syntax' => $this->oidDesc['syntax'] == null ?? 'null', |
|
| 81 | + ':type_enum' => $this->oidDesc['type_enum'] ?? 'null', |
|
| 82 | + ':description' => $this->oidDesc['description'] ?? 'null', |
|
| 83 | 83 | ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
| 84 | 84 | ); |
| 85 | 85 | |
| 86 | 86 | if ($sqlQuery->execute($sqlParam) === false) { |
| 87 | - $this->getLogging()->log('Error in query : ' . $sql,ERROR,''); |
|
| 87 | + $this->getLogging()->log('Error in query : '.$sql, ERROR, ''); |
|
| 88 | 88 | } |
| 89 | - $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 89 | + $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG); |
|
| 90 | 90 | return 1; |
| 91 | 91 | } |
| 92 | 92 | else |
| 93 | 93 | { |
| 94 | - $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 94 | + $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG); |
|
| 95 | 95 | return 0; |
| 96 | 96 | } |
| 97 | 97 | } |
@@ -107,11 +107,11 @@ discard block |
||
| 107 | 107 | $db_conn=$this->getTrapsDB()->db_connect_trap(); |
| 108 | 108 | $sql='INSERT INTO '.$this->getTrapsDB()->dbPrefix.'mib_cache '. |
| 109 | 109 | '(oid, name, type , mib, textual_convention, display_hint '. |
| 110 | - ', syntax, type_enum , description ) ' . |
|
| 110 | + ', syntax, type_enum , description ) '. |
|
| 111 | 111 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 112 | 112 | ', :syntax, :type_enum, :description )'; |
| 113 | 113 | |
| 114 | - if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 114 | + if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql.='RETURNING id'; |
|
| 115 | 115 | |
| 116 | 116 | $sqlQuery=$db_conn->prepare($sql); |
| 117 | 117 | |
@@ -120,15 +120,15 @@ discard block |
||
| 120 | 120 | ':name' => $this->oidDesc['name'], |
| 121 | 121 | ':type' => $this->oidDesc['type'], |
| 122 | 122 | ':mib' => $this->oidDesc['mib'], |
| 123 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 124 | - ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
| 125 | - ':syntax' => $this->oidDesc['syntax']??'null', |
|
| 126 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 127 | - ':description' => $this->oidDesc['description']??'null' |
|
| 123 | + ':tc' => $this->oidDesc['textconv'] ?? 'null', |
|
| 124 | + ':display_hint' => $this->oidDesc['dispHint'] ?? 'null', |
|
| 125 | + ':syntax' => $this->oidDesc['syntax'] ?? 'null', |
|
| 126 | + ':type_enum' => $this->oidDesc['type_enum'] ?? 'null', |
|
| 127 | + ':description' => $this->oidDesc['description'] ?? 'null' |
|
| 128 | 128 | ); |
| 129 | 129 | |
| 130 | 130 | if ($sqlQuery->execute($sqlParam) === false) { |
| 131 | - $this->getLogging()->log('Error in query : ' . $sql,1,''); |
|
| 131 | + $this->getLogging()->log('Error in query : '.$sql, 1, ''); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | switch ($this->getTrapsDB()->trapDBType) |
@@ -136,10 +136,10 @@ discard block |
||
| 136 | 136 | case 'pgsql': |
| 137 | 137 | // Get last id to insert oid/values in secondary table |
| 138 | 138 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
| 139 | - $this->getLogging()->log('Error getting id - pgsql - ',1,''); |
|
| 139 | + $this->getLogging()->log('Error getting id - pgsql - ', 1, ''); |
|
| 140 | 140 | } |
| 141 | - if (! isset($inserted_id_ret['id'])) { |
|
| 142 | - $this->getLogging()->log('Error getting id - pgsql - empty.',ERROR); |
|
| 141 | + if (!isset($inserted_id_ret['id'])) { |
|
| 142 | + $this->getLogging()->log('Error getting id - pgsql - empty.', ERROR); |
|
| 143 | 143 | return 0; |
| 144 | 144 | } |
| 145 | 145 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
@@ -148,16 +148,16 @@ discard block |
||
| 148 | 148 | // Get last id to insert oid/values in secondary table |
| 149 | 149 | $sql='SELECT LAST_INSERT_ID();'; |
| 150 | 150 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 151 | - $this->getLogging()->log('Erreur getting id - mysql - ',ERROR); |
|
| 151 | + $this->getLogging()->log('Erreur getting id - mysql - ', ERROR); |
|
| 152 | 152 | return 0; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 156 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 156 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 157 | 157 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
| 158 | 158 | break; |
| 159 | 159 | default: |
| 160 | - $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType,ERROR); |
|
| 160 | + $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType, ERROR); |
|
| 161 | 161 | return 0; |
| 162 | 162 | } |
| 163 | 163 | |
@@ -177,9 +177,9 @@ discard block |
||
| 177 | 177 | $db_conn=$this->getTrapsDB()->db_connect_trap(); |
| 178 | 178 | // Get all objects |
| 179 | 179 | $sql='SELECT * FROM '.$this->getTrapsDB()->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
| 180 | - $this->getLogging()->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 180 | + $this->getLogging()->log('SQL query get all traps: '.$sql, DEBUG); |
|
| 181 | 181 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 182 | - $this->getLogging()->log('No result in query : ' . $sql,1,''); |
|
| 182 | + $this->getLogging()->log('No result in query : '.$sql, 1, ''); |
|
| 183 | 183 | } |
| 184 | 184 | $dbObjectsRaw=$ret_code->fetchAll(); |
| 185 | 185 | |
@@ -14,57 +14,57 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | class Mib |
| 16 | 16 | { |
| 17 | - use \MibDatabase; |
|
| 17 | + use \MibDatabase; |
|
| 18 | 18 | |
| 19 | - /** @var Logging $logging logging class */ |
|
| 20 | - protected $logging; |
|
| 21 | - /** @var Database $trapsDB Database class */ |
|
| 22 | - protected $trapsDB; |
|
| 19 | + /** @var Logging $logging logging class */ |
|
| 20 | + protected $logging; |
|
| 21 | + /** @var Database $trapsDB Database class */ |
|
| 22 | + protected $trapsDB; |
|
| 23 | 23 | |
| 24 | - /** @var string $snmptranslate */ |
|
| 25 | - public $snmptranslate; |
|
| 26 | - /** @var string $snmptranslateDirs */ |
|
| 27 | - public $snmptranslateDirs; |
|
| 24 | + /** @var string $snmptranslate */ |
|
| 25 | + public $snmptranslate; |
|
| 26 | + /** @var string $snmptranslateDirs */ |
|
| 27 | + public $snmptranslateDirs; |
|
| 28 | 28 | |
| 29 | - private $dbOidAll; //< All oid in database; |
|
| 30 | - private $dbOidIndex; //< Index of oid in dbOidAll |
|
| 31 | - private $objectsAll; //< output lines of snmptranslate list |
|
| 32 | - private $trapObjectsIndex; //< array of traps objects (as OID) |
|
| 29 | + private $dbOidAll; //< All oid in database; |
|
| 30 | + private $dbOidIndex; //< Index of oid in dbOidAll |
|
| 31 | + private $objectsAll; //< output lines of snmptranslate list |
|
| 32 | + private $trapObjectsIndex; //< array of traps objects (as OID) |
|
| 33 | 33 | |
| 34 | - private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
|
| 34 | + private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
|
| 35 | 35 | |
| 36 | - // Timing vars for update |
|
| 37 | - private $timing=array(); |
|
| 36 | + // Timing vars for update |
|
| 37 | + private $timing=array(); |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Setup Mib Class |
|
| 41 | - * @param Logging $logClass : where to log |
|
| 42 | - * @param Database $dbClass : Database |
|
| 43 | - */ |
|
| 44 | - function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
| 45 | - { |
|
| 46 | - $this->logging=$logClass; |
|
| 47 | - $this->trapsDB=$dbClass; |
|
| 48 | - $this->snmptranslate=$snmptrans; |
|
| 49 | - $this->snmptranslateDirs=$snmptransdir; |
|
| 39 | + /** |
|
| 40 | + * Setup Mib Class |
|
| 41 | + * @param Logging $logClass : where to log |
|
| 42 | + * @param Database $dbClass : Database |
|
| 43 | + */ |
|
| 44 | + function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
| 45 | + { |
|
| 46 | + $this->logging=$logClass; |
|
| 47 | + $this->trapsDB=$dbClass; |
|
| 48 | + $this->snmptranslate=$snmptrans; |
|
| 49 | + $this->snmptranslateDirs=$snmptransdir; |
|
| 50 | 50 | |
| 51 | - } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return \Trapdirector\Logging |
|
| 55 | - */ |
|
| 56 | - public function getLogging() |
|
| 57 | - { |
|
| 58 | - return $this->logging; |
|
| 59 | - } |
|
| 53 | + /** |
|
| 54 | + * @return \Trapdirector\Logging |
|
| 55 | + */ |
|
| 56 | + public function getLogging() |
|
| 57 | + { |
|
| 58 | + return $this->logging; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @return \Trapdirector\Database |
|
| 63 | - */ |
|
| 64 | - public function getTrapsDB() |
|
| 65 | - { |
|
| 66 | - return $this->trapsDB; |
|
| 67 | - } |
|
| 61 | + /** |
|
| 62 | + * @return \Trapdirector\Database |
|
| 63 | + */ |
|
| 64 | + public function getTrapsDB() |
|
| 65 | + { |
|
| 66 | + return $this->trapsDB; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * Get object details & mib , returns snmptranslate output |
@@ -72,478 +72,478 @@ discard block |
||
| 72 | 72 | * @param string $trapmib : mib of trap |
| 73 | 73 | * @return NULL|array : null if not found, or output of snmptranslate |
| 74 | 74 | */ |
| 75 | - private function get_object_details($object,$trapmib) |
|
| 76 | - { |
|
| 77 | - $match=$snmptrans=array(); |
|
| 78 | - $retVal=0; |
|
| 79 | - $this->oidDesc['mib']=$trapmib; |
|
| 80 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 81 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 82 | - if ($retVal!=0) |
|
| 83 | - { |
|
| 84 | - // Maybe not trap mib, search with IR |
|
| 85 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 86 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 87 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 88 | - { // Not found -> continue with warning |
|
| 89 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 90 | - return null; |
|
| 91 | - } |
|
| 92 | - $this->oidDesc['mib']=$match[1]; |
|
| 75 | + private function get_object_details($object,$trapmib) |
|
| 76 | + { |
|
| 77 | + $match=$snmptrans=array(); |
|
| 78 | + $retVal=0; |
|
| 79 | + $this->oidDesc['mib']=$trapmib; |
|
| 80 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 81 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 82 | + if ($retVal!=0) |
|
| 83 | + { |
|
| 84 | + // Maybe not trap mib, search with IR |
|
| 85 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 86 | + ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 87 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 88 | + { // Not found -> continue with warning |
|
| 89 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 90 | + return null; |
|
| 91 | + } |
|
| 92 | + $this->oidDesc['mib']=$match[1]; |
|
| 93 | 93 | |
| 94 | - // Do the snmptranslate again. |
|
| 95 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 96 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
|
| 97 | - if ($retVal!=0) { |
|
| 98 | - $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
|
| 99 | - return null; |
|
| 100 | - } |
|
| 94 | + // Do the snmptranslate again. |
|
| 95 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 96 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
|
| 97 | + if ($retVal!=0) { |
|
| 98 | + $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
|
| 99 | + return null; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - } |
|
| 103 | - return $snmptrans; |
|
| 104 | - } |
|
| 102 | + } |
|
| 103 | + return $snmptrans; |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * Parse snmptranslate output and set $this->oidDesc with elements |
| 108 | 108 | * @param array $snmptrans : multi line output of snmptrans |
| 109 | 109 | */ |
| 110 | - private function parse_object($snmptrans) |
|
| 111 | - { |
|
| 112 | - $tmpdesc=''; // For multiline description |
|
| 113 | - $indesc=false; // true if currently inside multiline description |
|
| 114 | - $match=array(); |
|
| 110 | + private function parse_object($snmptrans) |
|
| 111 | + { |
|
| 112 | + $tmpdesc=''; // For multiline description |
|
| 113 | + $indesc=false; // true if currently inside multiline description |
|
| 114 | + $match=array(); |
|
| 115 | 115 | |
| 116 | - foreach ($snmptrans as $line) |
|
| 117 | - { |
|
| 118 | - if ($indesc===true) |
|
| 119 | - { |
|
| 120 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 121 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 122 | - { |
|
| 123 | - $this->oidDesc['description'] = $tmpdesc . $match[1]; |
|
| 124 | - $indesc=false; |
|
| 125 | - } |
|
| 126 | - $tmpdesc.=$line; |
|
| 127 | - continue; |
|
| 128 | - } |
|
| 129 | - if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 130 | - { |
|
| 131 | - $this->oidDesc['oid']=$line; |
|
| 132 | - continue; |
|
| 133 | - } |
|
| 134 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 135 | - { |
|
| 136 | - $this->oidDesc['syntax']=$match[1]; |
|
| 137 | - $this->oidDesc['type_enum']=$match[2]; |
|
| 138 | - continue; |
|
| 139 | - } |
|
| 140 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 141 | - { |
|
| 142 | - $this->oidDesc['syntax']=$match[1]; |
|
| 143 | - continue; |
|
| 144 | - } |
|
| 145 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 146 | - { |
|
| 147 | - $this->oidDesc['dispHint']=$match[1]; |
|
| 148 | - continue; |
|
| 149 | - } |
|
| 150 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 151 | - { |
|
| 152 | - $this->oidDesc['description']=$match[1]; |
|
| 153 | - continue; |
|
| 154 | - } |
|
| 155 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 156 | - { |
|
| 157 | - $tmpdesc=$match[1]; |
|
| 158 | - $indesc=true; |
|
| 159 | - continue; |
|
| 160 | - } |
|
| 161 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 162 | - { |
|
| 163 | - $this->oidDesc['textconv']=$match[1]; |
|
| 164 | - continue; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - } |
|
| 116 | + foreach ($snmptrans as $line) |
|
| 117 | + { |
|
| 118 | + if ($indesc===true) |
|
| 119 | + { |
|
| 120 | + $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 121 | + if (preg_match('/(.*)"$/', $line,$match)) |
|
| 122 | + { |
|
| 123 | + $this->oidDesc['description'] = $tmpdesc . $match[1]; |
|
| 124 | + $indesc=false; |
|
| 125 | + } |
|
| 126 | + $tmpdesc.=$line; |
|
| 127 | + continue; |
|
| 128 | + } |
|
| 129 | + if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 130 | + { |
|
| 131 | + $this->oidDesc['oid']=$line; |
|
| 132 | + continue; |
|
| 133 | + } |
|
| 134 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 135 | + { |
|
| 136 | + $this->oidDesc['syntax']=$match[1]; |
|
| 137 | + $this->oidDesc['type_enum']=$match[2]; |
|
| 138 | + continue; |
|
| 139 | + } |
|
| 140 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 141 | + { |
|
| 142 | + $this->oidDesc['syntax']=$match[1]; |
|
| 143 | + continue; |
|
| 144 | + } |
|
| 145 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 146 | + { |
|
| 147 | + $this->oidDesc['dispHint']=$match[1]; |
|
| 148 | + continue; |
|
| 149 | + } |
|
| 150 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 151 | + { |
|
| 152 | + $this->oidDesc['description']=$match[1]; |
|
| 153 | + continue; |
|
| 154 | + } |
|
| 155 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 156 | + { |
|
| 157 | + $tmpdesc=$match[1]; |
|
| 158 | + $indesc=true; |
|
| 159 | + continue; |
|
| 160 | + } |
|
| 161 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 162 | + { |
|
| 163 | + $this->oidDesc['textconv']=$match[1]; |
|
| 164 | + continue; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * create or update (with check_existing = true) objects of trap |
|
| 171 | - * @param string $trapOID : trap oid |
|
| 172 | - * @param string $trapmib : mib of trap |
|
| 173 | - * @param array $objects : array of objects name (without MIB) |
|
| 174 | - * @param bool $check_existing : check instead of create |
|
| 175 | - */ |
|
| 176 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 177 | - { |
|
| 178 | - $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
| 169 | + /** |
|
| 170 | + * create or update (with check_existing = true) objects of trap |
|
| 171 | + * @param string $trapOID : trap oid |
|
| 172 | + * @param string $trapmib : mib of trap |
|
| 173 | + * @param array $objects : array of objects name (without MIB) |
|
| 174 | + * @param bool $check_existing : check instead of create |
|
| 175 | + */ |
|
| 176 | + public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 177 | + { |
|
| 178 | + $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
| 179 | 179 | |
| 180 | - if ($check_existing === true) |
|
| 181 | - { |
|
| 182 | - $dbObjects=$this->cache_db_objects($trapId); |
|
| 183 | - } |
|
| 180 | + if ($check_existing === true) |
|
| 181 | + { |
|
| 182 | + $dbObjects=$this->cache_db_objects($trapId); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - foreach ($objects as $object) |
|
| 186 | - { |
|
| 185 | + foreach ($objects as $object) |
|
| 186 | + { |
|
| 187 | 187 | |
| 188 | - $this->reset_oidDesc(); |
|
| 188 | + $this->reset_oidDesc(); |
|
| 189 | 189 | |
| 190 | - $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
|
| 191 | - if ($snmptrans === null) continue; // object not found |
|
| 190 | + $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
|
| 191 | + if ($snmptrans === null) continue; // object not found |
|
| 192 | 192 | |
| 193 | - $this->parse_object($snmptrans); |
|
| 193 | + $this->parse_object($snmptrans); |
|
| 194 | 194 | |
| 195 | - $this->oidDesc['name'] = $object; |
|
| 195 | + $this->oidDesc['name'] = $object; |
|
| 196 | 196 | |
| 197 | - $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
|
| 197 | + $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
|
| 198 | 198 | |
| 199 | - // Update |
|
| 200 | - $this->update_oid(); |
|
| 199 | + // Update |
|
| 200 | + $this->update_oid(); |
|
| 201 | 201 | |
| 202 | - if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
|
| 203 | - { // if link exists, continue |
|
| 204 | - $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
|
| 205 | - continue; |
|
| 206 | - } |
|
| 207 | - if ($check_existing === true) |
|
| 208 | - { |
|
| 209 | - // TODO : check link trap - objects exists, mark them. |
|
| 210 | - } |
|
| 211 | - // Associate in object table |
|
| 212 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 213 | - $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 214 | - 'values (:trap_id, :object_id)'; |
|
| 215 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 216 | - $sqlParam=array( |
|
| 217 | - ':trap_id' => $trapId, |
|
| 218 | - ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
|
| 219 | - ); |
|
| 202 | + if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
|
| 203 | + { // if link exists, continue |
|
| 204 | + $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
|
| 205 | + continue; |
|
| 206 | + } |
|
| 207 | + if ($check_existing === true) |
|
| 208 | + { |
|
| 209 | + // TODO : check link trap - objects exists, mark them. |
|
| 210 | + } |
|
| 211 | + // Associate in object table |
|
| 212 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 213 | + $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 214 | + 'values (:trap_id, :object_id)'; |
|
| 215 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 216 | + $sqlParam=array( |
|
| 217 | + ':trap_id' => $trapId, |
|
| 218 | + ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
|
| 219 | + ); |
|
| 220 | 220 | |
| 221 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 222 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - if ($check_existing === true) |
|
| 226 | - { |
|
| 227 | - // TODO : remove link trap - objects that wasn't marked. |
|
| 228 | - } |
|
| 221 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 222 | + $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + if ($check_existing === true) |
|
| 226 | + { |
|
| 227 | + // TODO : remove link trap - objects that wasn't marked. |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - } |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - private function reset_oidDesc() |
|
| 233 | - { |
|
| 234 | - $this->oidDesc['oid']=null; |
|
| 235 | - $this->oidDesc['name']=null; |
|
| 236 | - $this->oidDesc['type']=null; |
|
| 237 | - $this->oidDesc['mib']=null; |
|
| 238 | - $this->oidDesc['textconv']=null; |
|
| 239 | - $this->oidDesc['dispHint'] =null; |
|
| 240 | - $this->oidDesc['syntax']=null; |
|
| 241 | - $this->oidDesc['type_enum']=null; |
|
| 242 | - $this->oidDesc['description']=null; |
|
| 243 | - } |
|
| 232 | + private function reset_oidDesc() |
|
| 233 | + { |
|
| 234 | + $this->oidDesc['oid']=null; |
|
| 235 | + $this->oidDesc['name']=null; |
|
| 236 | + $this->oidDesc['type']=null; |
|
| 237 | + $this->oidDesc['mib']=null; |
|
| 238 | + $this->oidDesc['textconv']=null; |
|
| 239 | + $this->oidDesc['dispHint'] =null; |
|
| 240 | + $this->oidDesc['syntax']=null; |
|
| 241 | + $this->oidDesc['type_enum']=null; |
|
| 242 | + $this->oidDesc['description']=null; |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * Fills $this->objectsAll with all mibs from snmptranslate |
|
| 247 | - * @return integer : number of elements |
|
| 248 | - */ |
|
| 249 | - private function load_mibs_snmptranslate() |
|
| 250 | - { |
|
| 251 | - $retVal=0; |
|
| 252 | - // Get all mib objects from all mibs |
|
| 253 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
| 254 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 255 | - unset($this->objectsAll); |
|
| 256 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 257 | - if ($retVal!=0) |
|
| 258 | - { |
|
| 259 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 260 | - } |
|
| 261 | - // Count elements to show progress |
|
| 262 | - $numElements=count($this->objectsAll); |
|
| 263 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 264 | - return $numElements; |
|
| 265 | - } |
|
| 245 | + /** |
|
| 246 | + * Fills $this->objectsAll with all mibs from snmptranslate |
|
| 247 | + * @return integer : number of elements |
|
| 248 | + */ |
|
| 249 | + private function load_mibs_snmptranslate() |
|
| 250 | + { |
|
| 251 | + $retVal=0; |
|
| 252 | + // Get all mib objects from all mibs |
|
| 253 | + $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
| 254 | + $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 255 | + unset($this->objectsAll); |
|
| 256 | + exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 257 | + if ($retVal!=0) |
|
| 258 | + { |
|
| 259 | + $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 260 | + } |
|
| 261 | + // Count elements to show progress |
|
| 262 | + $numElements=count($this->objectsAll); |
|
| 263 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 264 | + return $numElements; |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - /** |
|
| 268 | - * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
|
| 269 | - */ |
|
| 270 | - private function load_mibs_from_db() |
|
| 271 | - { |
|
| 272 | - // Get all mibs from databse to have a memory index |
|
| 267 | + /** |
|
| 268 | + * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
|
| 269 | + */ |
|
| 270 | + private function load_mibs_from_db() |
|
| 271 | + { |
|
| 272 | + // Get all mibs from databse to have a memory index |
|
| 273 | 273 | |
| 274 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 274 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 275 | 275 | |
| 276 | - $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
| 277 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 278 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 279 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 280 | - } |
|
| 281 | - $this->dbOidAll=$ret_code->fetchAll(); |
|
| 282 | - $this->dbOidIndex=array(); |
|
| 283 | - // Create the index for db; |
|
| 284 | - foreach($this->dbOidAll as $key=>$val) |
|
| 285 | - { |
|
| 286 | - $this->dbOidIndex[$val['oid']]['key']=$key; |
|
| 287 | - $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
| 288 | - } |
|
| 289 | - } |
|
| 276 | + $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
| 277 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 278 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 279 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 280 | + } |
|
| 281 | + $this->dbOidAll=$ret_code->fetchAll(); |
|
| 282 | + $this->dbOidIndex=array(); |
|
| 283 | + // Create the index for db; |
|
| 284 | + foreach($this->dbOidAll as $key=>$val) |
|
| 285 | + { |
|
| 286 | + $this->dbOidIndex[$val['oid']]['key']=$key; |
|
| 287 | + $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - /** |
|
| 292 | - * Reset all update timers & count to zero |
|
| 293 | - */ |
|
| 294 | - private function reset_update_timers() |
|
| 295 | - { |
|
| 296 | - $this->timing['base_parse_time']=0; |
|
| 297 | - $this->timing['base_check_time']=0; |
|
| 298 | - $this->timing['type0_check_time']=0; |
|
| 299 | - $this->timing['nottrap_time']=0; |
|
| 300 | - $this->timing['update_time']=0; |
|
| 301 | - $this->timing['objects_time']=0; |
|
| 302 | - $this->timing['base_parse_num']=0; |
|
| 303 | - $this->timing['base_check_num']=0; |
|
| 304 | - $this->timing['type0_check_num']=0; |
|
| 305 | - $this->timing['nottrap_num']=0; |
|
| 306 | - $this->timing['update_num']=0; |
|
| 307 | - $this->timing['objects_num']=0; |
|
| 308 | - $this->timing['num_traps']=0; |
|
| 309 | - } |
|
| 291 | + /** |
|
| 292 | + * Reset all update timers & count to zero |
|
| 293 | + */ |
|
| 294 | + private function reset_update_timers() |
|
| 295 | + { |
|
| 296 | + $this->timing['base_parse_time']=0; |
|
| 297 | + $this->timing['base_check_time']=0; |
|
| 298 | + $this->timing['type0_check_time']=0; |
|
| 299 | + $this->timing['nottrap_time']=0; |
|
| 300 | + $this->timing['update_time']=0; |
|
| 301 | + $this->timing['objects_time']=0; |
|
| 302 | + $this->timing['base_parse_num']=0; |
|
| 303 | + $this->timing['base_check_num']=0; |
|
| 304 | + $this->timing['type0_check_num']=0; |
|
| 305 | + $this->timing['nottrap_num']=0; |
|
| 306 | + $this->timing['update_num']=0; |
|
| 307 | + $this->timing['objects_num']=0; |
|
| 308 | + $this->timing['num_traps']=0; |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - /** |
|
| 312 | - * Detect if $this->objectsAll[$curElement] is a trap |
|
| 313 | - * @param integer $curElement |
|
| 314 | - * @param bool $onlyTraps : set to false to get all and not only traps. |
|
| 315 | - * @return boolean : false if it's a trap , true if not |
|
| 316 | - */ |
|
| 317 | - private function detect_trap($curElement,$onlyTraps) |
|
| 318 | - { |
|
| 319 | - // Get oid or pass if not found |
|
| 320 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 321 | - { |
|
| 322 | - $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
|
| 323 | - $this->timing['base_parse_num'] ++; |
|
| 324 | - return true; |
|
| 325 | - } |
|
| 326 | - $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
|
| 311 | + /** |
|
| 312 | + * Detect if $this->objectsAll[$curElement] is a trap |
|
| 313 | + * @param integer $curElement |
|
| 314 | + * @param bool $onlyTraps : set to false to get all and not only traps. |
|
| 315 | + * @return boolean : false if it's a trap , true if not |
|
| 316 | + */ |
|
| 317 | + private function detect_trap($curElement,$onlyTraps) |
|
| 318 | + { |
|
| 319 | + // Get oid or pass if not found |
|
| 320 | + if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 321 | + { |
|
| 322 | + $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
|
| 323 | + $this->timing['base_parse_num'] ++; |
|
| 324 | + return true; |
|
| 325 | + } |
|
| 326 | + $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
|
| 327 | 327 | |
| 328 | - // get next line |
|
| 329 | - $curElement++; |
|
| 330 | - $match=$snmptrans=array(); |
|
| 331 | - if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
| 332 | - $this->objectsAll[$curElement],$match)) |
|
| 333 | - { |
|
| 334 | - $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 335 | - $this->timing['base_check_num']++; |
|
| 336 | - return true; |
|
| 337 | - } |
|
| 328 | + // get next line |
|
| 329 | + $curElement++; |
|
| 330 | + $match=$snmptrans=array(); |
|
| 331 | + if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
| 332 | + $this->objectsAll[$curElement],$match)) |
|
| 333 | + { |
|
| 334 | + $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 335 | + $this->timing['base_check_num']++; |
|
| 336 | + return true; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - $this->oidDesc['name']=$match[1]; // Name |
|
| 340 | - $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
| 339 | + $this->oidDesc['name']=$match[1]; // Name |
|
| 340 | + $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
| 341 | 341 | |
| 342 | - if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
| 343 | - { |
|
| 344 | - // Check if next is suboid -> in that case is cannot be a trap |
|
| 345 | - if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
|
| 346 | - { |
|
| 347 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 348 | - $this->timing['type0_check_num']++; |
|
| 349 | - return true; |
|
| 350 | - } |
|
| 351 | - unset($snmptrans); |
|
| 352 | - $retVal=0; |
|
| 353 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 354 | - ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 355 | - if ($retVal!=0) |
|
| 356 | - { |
|
| 357 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 358 | - $this->timing['type0_check_num']++; |
|
| 359 | - return true; |
|
| 360 | - } |
|
| 361 | - //echo "\n v1 trap found : $this->oidDesc['oid'] \n"; |
|
| 362 | - // Force as trap. |
|
| 363 | - $this->oidDesc['type']=21; |
|
| 364 | - } |
|
| 365 | - if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
| 366 | - { |
|
| 367 | - $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
|
| 368 | - $this->timing['nottrap_num']++; |
|
| 369 | - return true; |
|
| 370 | - } |
|
| 371 | - return false; |
|
| 372 | - } |
|
| 342 | + if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
| 343 | + { |
|
| 344 | + // Check if next is suboid -> in that case is cannot be a trap |
|
| 345 | + if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
|
| 346 | + { |
|
| 347 | + $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 348 | + $this->timing['type0_check_num']++; |
|
| 349 | + return true; |
|
| 350 | + } |
|
| 351 | + unset($snmptrans); |
|
| 352 | + $retVal=0; |
|
| 353 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 354 | + ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 355 | + if ($retVal!=0) |
|
| 356 | + { |
|
| 357 | + $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 358 | + $this->timing['type0_check_num']++; |
|
| 359 | + return true; |
|
| 360 | + } |
|
| 361 | + //echo "\n v1 trap found : $this->oidDesc['oid'] \n"; |
|
| 362 | + // Force as trap. |
|
| 363 | + $this->oidDesc['type']=21; |
|
| 364 | + } |
|
| 365 | + if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
| 366 | + { |
|
| 367 | + $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
|
| 368 | + $this->timing['nottrap_num']++; |
|
| 369 | + return true; |
|
| 370 | + } |
|
| 371 | + return false; |
|
| 372 | + } |
|
| 373 | 373 | |
| 374 | - /** |
|
| 375 | - * get_trap_mib_description |
|
| 376 | - * @return array|null : array of snmptranslate output or null on error |
|
| 377 | - **/ |
|
| 378 | - private function get_trap_mib_description() |
|
| 379 | - { |
|
| 380 | - $retVal=0; |
|
| 381 | - $match=$snmptrans=array(); |
|
| 382 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 383 | - ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal); |
|
| 384 | - if ($retVal!=0) |
|
| 385 | - { |
|
| 386 | - $this->logging->log('error executing snmptranslate',ERROR); |
|
| 387 | - return $snmptrans; |
|
| 388 | - } |
|
| 374 | + /** |
|
| 375 | + * get_trap_mib_description |
|
| 376 | + * @return array|null : array of snmptranslate output or null on error |
|
| 377 | + **/ |
|
| 378 | + private function get_trap_mib_description() |
|
| 379 | + { |
|
| 380 | + $retVal=0; |
|
| 381 | + $match=$snmptrans=array(); |
|
| 382 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 383 | + ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal); |
|
| 384 | + if ($retVal!=0) |
|
| 385 | + { |
|
| 386 | + $this->logging->log('error executing snmptranslate',ERROR); |
|
| 387 | + return $snmptrans; |
|
| 388 | + } |
|
| 389 | 389 | |
| 390 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 391 | - { |
|
| 392 | - $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR); |
|
| 393 | - return $snmptrans; |
|
| 394 | - } |
|
| 395 | - $this->oidDesc['mib']=$match[1]; |
|
| 390 | + if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 391 | + { |
|
| 392 | + $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR); |
|
| 393 | + return $snmptrans; |
|
| 394 | + } |
|
| 395 | + $this->oidDesc['mib']=$match[1]; |
|
| 396 | 396 | |
| 397 | - $numLine=1; |
|
| 398 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 399 | - if (isset($snmptrans[$numLine])) |
|
| 400 | - { |
|
| 401 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 397 | + $numLine=1; |
|
| 398 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 399 | + if (isset($snmptrans[$numLine])) |
|
| 400 | + { |
|
| 401 | + $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 402 | 402 | |
| 403 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 404 | - { |
|
| 405 | - $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 406 | - $numLine++; |
|
| 407 | - } |
|
| 408 | - if (isset($snmptrans[$numLine])) { |
|
| 409 | - $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 410 | - $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']); |
|
| 411 | - } |
|
| 403 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 404 | + { |
|
| 405 | + $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 406 | + $numLine++; |
|
| 407 | + } |
|
| 408 | + if (isset($snmptrans[$numLine])) { |
|
| 409 | + $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 410 | + $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']); |
|
| 411 | + } |
|
| 412 | 412 | |
| 413 | - } |
|
| 414 | - return $snmptrans; |
|
| 415 | - } |
|
| 413 | + } |
|
| 414 | + return $snmptrans; |
|
| 415 | + } |
|
| 416 | 416 | |
| 417 | - /** |
|
| 418 | - * Get trap objects |
|
| 419 | - * @param array $snmptrans : output of snmptranslate for TrapModuleConfig |
|
| 420 | - * @return array|null : array of objects or null if not found |
|
| 421 | - **/ |
|
| 422 | - private function get_trap_objects($snmptrans) |
|
| 423 | - { |
|
| 424 | - $objectName=null; |
|
| 425 | - $match=array(); |
|
| 426 | - foreach ($snmptrans as $line) |
|
| 427 | - { |
|
| 428 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 429 | - { |
|
| 430 | - $objectName=$match[1]; |
|
| 431 | - } |
|
| 432 | - } |
|
| 433 | - if ($objectName == null) |
|
| 434 | - { |
|
| 435 | - $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG); |
|
| 436 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 437 | - return null; |
|
| 438 | - } |
|
| 417 | + /** |
|
| 418 | + * Get trap objects |
|
| 419 | + * @param array $snmptrans : output of snmptranslate for TrapModuleConfig |
|
| 420 | + * @return array|null : array of objects or null if not found |
|
| 421 | + **/ |
|
| 422 | + private function get_trap_objects($snmptrans) |
|
| 423 | + { |
|
| 424 | + $objectName=null; |
|
| 425 | + $match=array(); |
|
| 426 | + foreach ($snmptrans as $line) |
|
| 427 | + { |
|
| 428 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 429 | + { |
|
| 430 | + $objectName=$match[1]; |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | + if ($objectName == null) |
|
| 434 | + { |
|
| 435 | + $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG); |
|
| 436 | + $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 437 | + return null; |
|
| 438 | + } |
|
| 439 | 439 | |
| 440 | - $trapObjects=array(); |
|
| 441 | - while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match)) |
|
| 442 | - { |
|
| 443 | - array_push($trapObjects,$match[1]); |
|
| 444 | - $objectName=preg_replace('/'.$match[0].'/','',$objectName); |
|
| 445 | - } |
|
| 446 | - return $trapObjects; |
|
| 447 | - } |
|
| 440 | + $trapObjects=array(); |
|
| 441 | + while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match)) |
|
| 442 | + { |
|
| 443 | + array_push($trapObjects,$match[1]); |
|
| 444 | + $objectName=preg_replace('/'.$match[0].'/','',$objectName); |
|
| 445 | + } |
|
| 446 | + return $trapObjects; |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | - /** |
|
| 450 | - * Cache mib in database |
|
| 451 | - * @param boolean $display_progress : Display progress on standard output |
|
| 452 | - * @param boolean $check_change : Force check of trap params & objects |
|
| 453 | - * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
| 454 | - * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
| 455 | - */ |
|
| 456 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 457 | - { |
|
| 458 | - // Global Timing |
|
| 459 | - $timeTaken = microtime(true); |
|
| 449 | + /** |
|
| 450 | + * Cache mib in database |
|
| 451 | + * @param boolean $display_progress : Display progress on standard output |
|
| 452 | + * @param boolean $check_change : Force check of trap params & objects |
|
| 453 | + * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
| 454 | + * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
| 455 | + */ |
|
| 456 | + public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 457 | + { |
|
| 458 | + // Global Timing |
|
| 459 | + $timeTaken = microtime(true); |
|
| 460 | 460 | |
| 461 | - $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll |
|
| 461 | + $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll |
|
| 462 | 462 | |
| 463 | - $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex |
|
| 463 | + $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex |
|
| 464 | 464 | |
| 465 | - $step=$basestep=$numElements/10; // output display of % done |
|
| 466 | - $num_step=0; |
|
| 467 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 465 | + $step=$basestep=$numElements/10; // output display of % done |
|
| 466 | + $num_step=0; |
|
| 467 | + $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 468 | 468 | |
| 469 | - // Create index for trap objects |
|
| 470 | - $this->trapObjectsIndex=array(); |
|
| 469 | + // Create index for trap objects |
|
| 470 | + $this->trapObjectsIndex=array(); |
|
| 471 | 471 | |
| 472 | - // detailed timing (time_* vars) |
|
| 473 | - $this->reset_update_timers(); |
|
| 472 | + // detailed timing (time_* vars) |
|
| 473 | + $this->reset_update_timers(); |
|
| 474 | 474 | |
| 475 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 476 | - { |
|
| 477 | - $this->timing['base_time']= microtime(true); |
|
| 478 | - if ($display_progress) |
|
| 479 | - { |
|
| 480 | - if ((microtime(true)-$timeFiveSec) > 2) |
|
| 481 | - { // echo a . every 2 sec |
|
| 482 | - echo '.'; |
|
| 483 | - $timeFiveSec = microtime(true); |
|
| 484 | - } |
|
| 485 | - if ($curElement>$step) |
|
| 486 | - { // display progress |
|
| 487 | - $num_step++; |
|
| 488 | - $step+=$basestep; |
|
| 489 | - echo "\n" . ($num_step*10). '% : '; |
|
| 490 | - } |
|
| 491 | - } |
|
| 475 | + for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 476 | + { |
|
| 477 | + $this->timing['base_time']= microtime(true); |
|
| 478 | + if ($display_progress) |
|
| 479 | + { |
|
| 480 | + if ((microtime(true)-$timeFiveSec) > 2) |
|
| 481 | + { // echo a . every 2 sec |
|
| 482 | + echo '.'; |
|
| 483 | + $timeFiveSec = microtime(true); |
|
| 484 | + } |
|
| 485 | + if ($curElement>$step) |
|
| 486 | + { // display progress |
|
| 487 | + $num_step++; |
|
| 488 | + $step+=$basestep; |
|
| 489 | + echo "\n" . ($num_step*10). '% : '; |
|
| 490 | + } |
|
| 491 | + } |
|
| 492 | 492 | |
| 493 | - $this->reset_oidDesc(); |
|
| 494 | - if ($this->detect_trap($curElement,$onlyTraps)===true) |
|
| 495 | - { |
|
| 496 | - continue; |
|
| 497 | - } |
|
| 493 | + $this->reset_oidDesc(); |
|
| 494 | + if ($this->detect_trap($curElement,$onlyTraps)===true) |
|
| 495 | + { |
|
| 496 | + continue; |
|
| 497 | + } |
|
| 498 | 498 | |
| 499 | - $this->timing['num_traps']++; |
|
| 499 | + $this->timing['num_traps']++; |
|
| 500 | 500 | |
| 501 | - $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
|
| 502 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
| 501 | + $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
|
| 502 | + if ($display_progress) echo '#'; // echo a # when trap found |
|
| 503 | 503 | |
| 504 | - // get trap objects & source MIB |
|
| 504 | + // get trap objects & source MIB |
|
| 505 | 505 | |
| 506 | - $snmptrans=$this->get_trap_mib_description(); // get MIB & description |
|
| 506 | + $snmptrans=$this->get_trap_mib_description(); // get MIB & description |
|
| 507 | 507 | |
| 508 | 508 | |
| 509 | - $update=$this->update_oid(); // Do update of trap. |
|
| 509 | + $update=$this->update_oid(); // Do update of trap. |
|
| 510 | 510 | |
| 511 | - $this->timing['update_time'] += microtime(true) - $this->timing['base_time']; |
|
| 512 | - $this->timing['update_num']++; |
|
| 511 | + $this->timing['update_time'] += microtime(true) - $this->timing['base_time']; |
|
| 512 | + $this->timing['update_num']++; |
|
| 513 | 513 | |
| 514 | - $this->timing['base_time']= microtime(true); // Reset to check object time |
|
| 514 | + $this->timing['base_time']= microtime(true); // Reset to check object time |
|
| 515 | 515 | |
| 516 | - if (($update==0) && ($check_change===false)) |
|
| 517 | - { // Trapd didn't change & force check disabled |
|
| 518 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 519 | - if ($display_progress) echo "C"; |
|
| 520 | - continue; |
|
| 521 | - } |
|
| 516 | + if (($update==0) && ($check_change===false)) |
|
| 517 | + { // Trapd didn't change & force check disabled |
|
| 518 | + $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 519 | + if ($display_progress) echo "C"; |
|
| 520 | + continue; |
|
| 521 | + } |
|
| 522 | 522 | |
| 523 | - $trapObjects=$this->get_trap_objects($snmptrans); // Get trap objects from snmptranslate output |
|
| 524 | - if ($trapObjects == null) |
|
| 525 | - { |
|
| 526 | - continue; |
|
| 527 | - } |
|
| 523 | + $trapObjects=$this->get_trap_objects($snmptrans); // Get trap objects from snmptranslate output |
|
| 524 | + if ($trapObjects == null) |
|
| 525 | + { |
|
| 526 | + continue; |
|
| 527 | + } |
|
| 528 | 528 | |
| 529 | - $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false); |
|
| 529 | + $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false); |
|
| 530 | 530 | |
| 531 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 532 | - $this->timing['objects_num']++; |
|
| 533 | - } |
|
| 531 | + $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 532 | + $this->timing['objects_num']++; |
|
| 533 | + } |
|
| 534 | 534 | |
| 535 | - if ($display_progress) |
|
| 536 | - { |
|
| 537 | - echo "\nNumber of processed traps : ". $this->timing['num_traps'] ."\n"; |
|
| 538 | - echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num']) . " occurences\n"; |
|
| 539 | - echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n"; |
|
| 540 | - echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , "; |
|
| 541 | - echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n"; |
|
| 535 | + if ($display_progress) |
|
| 536 | + { |
|
| 537 | + echo "\nNumber of processed traps : ". $this->timing['num_traps'] ."\n"; |
|
| 538 | + echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num']) . " occurences\n"; |
|
| 539 | + echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n"; |
|
| 540 | + echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , "; |
|
| 541 | + echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n"; |
|
| 542 | 542 | |
| 543 | - $timeTaken=microtime(true) - $timeTaken; |
|
| 544 | - echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 545 | - } |
|
| 546 | - } |
|
| 543 | + $timeTaken=microtime(true) - $timeTaken; |
|
| 544 | + echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | 547 | |
| 548 | 548 | |
| 549 | 549 | } |
| 550 | 550 | \ No newline at end of file |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | * @param Logging $logClass : where to log |
| 42 | 42 | * @param Database $dbClass : Database |
| 43 | 43 | */ |
| 44 | - function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
| 44 | + function __construct($logClass, $dbClass, $snmptrans, $snmptransdir) |
|
| 45 | 45 | { |
| 46 | 46 | $this->logging=$logClass; |
| 47 | 47 | $this->trapsDB=$dbClass; |
@@ -72,30 +72,30 @@ discard block |
||
| 72 | 72 | * @param string $trapmib : mib of trap |
| 73 | 73 | * @return NULL|array : null if not found, or output of snmptranslate |
| 74 | 74 | */ |
| 75 | - private function get_object_details($object,$trapmib) |
|
| 75 | + private function get_object_details($object, $trapmib) |
|
| 76 | 76 | { |
| 77 | 77 | $match=$snmptrans=array(); |
| 78 | 78 | $retVal=0; |
| 79 | 79 | $this->oidDesc['mib']=$trapmib; |
| 80 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 81 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 82 | - if ($retVal!=0) |
|
| 80 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 81 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
| 82 | + if ($retVal != 0) |
|
| 83 | 83 | { |
| 84 | 84 | // Maybe not trap mib, search with IR |
| 85 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 86 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 87 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 85 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 86 | + ' -IR '.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
| 87 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/', $snmptrans[0], $match)) |
|
| 88 | 88 | { // Not found -> continue with warning |
| 89 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 89 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object, 2, ''); |
|
| 90 | 90 | return null; |
| 91 | 91 | } |
| 92 | 92 | $this->oidDesc['mib']=$match[1]; |
| 93 | 93 | |
| 94 | 94 | // Do the snmptranslate again. |
| 95 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 96 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
|
| 97 | - if ($retVal!=0) { |
|
| 98 | - $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
|
| 95 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 96 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object, $snmptrans, $retVal); |
|
| 97 | + if ($retVal != 0) { |
|
| 98 | + $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object, 2, ''); |
|
| 99 | 99 | return null; |
| 100 | 100 | } |
| 101 | 101 | |
@@ -115,12 +115,12 @@ discard block |
||
| 115 | 115 | |
| 116 | 116 | foreach ($snmptrans as $line) |
| 117 | 117 | { |
| 118 | - if ($indesc===true) |
|
| 118 | + if ($indesc === true) |
|
| 119 | 119 | { |
| 120 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 121 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 120 | + $line=preg_replace('/[\t ]+/', ' ', $line); |
|
| 121 | + if (preg_match('/(.*)"$/', $line, $match)) |
|
| 122 | 122 | { |
| 123 | - $this->oidDesc['description'] = $tmpdesc . $match[1]; |
|
| 123 | + $this->oidDesc['description']=$tmpdesc.$match[1]; |
|
| 124 | 124 | $indesc=false; |
| 125 | 125 | } |
| 126 | 126 | $tmpdesc.=$line; |
@@ -131,34 +131,34 @@ discard block |
||
| 131 | 131 | $this->oidDesc['oid']=$line; |
| 132 | 132 | continue; |
| 133 | 133 | } |
| 134 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 134 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/', $line, $match)) |
|
| 135 | 135 | { |
| 136 | 136 | $this->oidDesc['syntax']=$match[1]; |
| 137 | 137 | $this->oidDesc['type_enum']=$match[2]; |
| 138 | 138 | continue; |
| 139 | 139 | } |
| 140 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 140 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/', $line, $match)) |
|
| 141 | 141 | { |
| 142 | 142 | $this->oidDesc['syntax']=$match[1]; |
| 143 | 143 | continue; |
| 144 | 144 | } |
| 145 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 145 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/', $line, $match)) |
|
| 146 | 146 | { |
| 147 | 147 | $this->oidDesc['dispHint']=$match[1]; |
| 148 | 148 | continue; |
| 149 | 149 | } |
| 150 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 150 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/', $line, $match)) |
|
| 151 | 151 | { |
| 152 | 152 | $this->oidDesc['description']=$match[1]; |
| 153 | 153 | continue; |
| 154 | 154 | } |
| 155 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 155 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $line, $match)) |
|
| 156 | 156 | { |
| 157 | 157 | $tmpdesc=$match[1]; |
| 158 | 158 | $indesc=true; |
| 159 | 159 | continue; |
| 160 | 160 | } |
| 161 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 161 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/', $line, $match)) |
|
| 162 | 162 | { |
| 163 | 163 | $this->oidDesc['textconv']=$match[1]; |
| 164 | 164 | continue; |
@@ -173,9 +173,9 @@ discard block |
||
| 173 | 173 | * @param array $objects : array of objects name (without MIB) |
| 174 | 174 | * @param bool $check_existing : check instead of create |
| 175 | 175 | */ |
| 176 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 176 | + public function trap_objects($trapOID, $trapmib, $objects, $check_existing) |
|
| 177 | 177 | { |
| 178 | - $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
| 178 | + $trapId=$this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
| 179 | 179 | |
| 180 | 180 | if ($check_existing === true) |
| 181 | 181 | { |
@@ -192,9 +192,9 @@ discard block |
||
| 192 | 192 | |
| 193 | 193 | $this->parse_object($snmptrans); |
| 194 | 194 | |
| 195 | - $this->oidDesc['name'] = $object; |
|
| 195 | + $this->oidDesc['name']=$object; |
|
| 196 | 196 | |
| 197 | - $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
|
| 197 | + $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'], DEBUG); |
|
| 198 | 198 | |
| 199 | 199 | // Update |
| 200 | 200 | $this->update_oid(); |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | ); |
| 220 | 220 | |
| 221 | 221 | if ($sqlQuery->execute($sqlParam) === false) { |
| 222 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
|
| 222 | + $this->logging->log('Error adding trap object : '.$sql.' / '.$trapId.'/'.$this->dbOidIndex[$this->oidDesc['oid']]['id'], 1, ''); |
|
| 223 | 223 | } |
| 224 | 224 | } |
| 225 | 225 | if ($check_existing === true) |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | $this->oidDesc['type']=null; |
| 237 | 237 | $this->oidDesc['mib']=null; |
| 238 | 238 | $this->oidDesc['textconv']=null; |
| 239 | - $this->oidDesc['dispHint'] =null; |
|
| 239 | + $this->oidDesc['dispHint']=null; |
|
| 240 | 240 | $this->oidDesc['syntax']=null; |
| 241 | 241 | $this->oidDesc['type_enum']=null; |
| 242 | 242 | $this->oidDesc['description']=null; |
@@ -250,17 +250,17 @@ discard block |
||
| 250 | 250 | { |
| 251 | 251 | $retVal=0; |
| 252 | 252 | // Get all mib objects from all mibs |
| 253 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
| 254 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 253 | + $snmpCommand=$this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
| 254 | + $this->logging->log('Getting all traps : '.$snmpCommand, DEBUG); |
|
| 255 | 255 | unset($this->objectsAll); |
| 256 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 257 | - if ($retVal!=0) |
|
| 256 | + exec($snmpCommand, $this->objectsAll, $retVal); |
|
| 257 | + if ($retVal != 0) |
|
| 258 | 258 | { |
| 259 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 259 | + $this->logging->log('error executing snmptranslate', ERROR, ''); |
|
| 260 | 260 | } |
| 261 | 261 | // Count elements to show progress |
| 262 | 262 | $numElements=count($this->objectsAll); |
| 263 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 263 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements, INFO); |
|
| 264 | 264 | return $numElements; |
| 265 | 265 | } |
| 266 | 266 | |
@@ -274,14 +274,14 @@ discard block |
||
| 274 | 274 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 275 | 275 | |
| 276 | 276 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
| 277 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 277 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 278 | 278 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 279 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 279 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 280 | 280 | } |
| 281 | 281 | $this->dbOidAll=$ret_code->fetchAll(); |
| 282 | 282 | $this->dbOidIndex=array(); |
| 283 | 283 | // Create the index for db; |
| 284 | - foreach($this->dbOidAll as $key=>$val) |
|
| 284 | + foreach ($this->dbOidAll as $key=>$val) |
|
| 285 | 285 | { |
| 286 | 286 | $this->dbOidIndex[$val['oid']]['key']=$key; |
| 287 | 287 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
@@ -314,13 +314,13 @@ discard block |
||
| 314 | 314 | * @param bool $onlyTraps : set to false to get all and not only traps. |
| 315 | 315 | * @return boolean : false if it's a trap , true if not |
| 316 | 316 | */ |
| 317 | - private function detect_trap($curElement,$onlyTraps) |
|
| 317 | + private function detect_trap($curElement, $onlyTraps) |
|
| 318 | 318 | { |
| 319 | 319 | // Get oid or pass if not found |
| 320 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 320 | + if (!preg_match('/^\.[0-9\.]+$/', $this->objectsAll[$curElement])) |
|
| 321 | 321 | { |
| 322 | - $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
|
| 323 | - $this->timing['base_parse_num'] ++; |
|
| 322 | + $this->timing['base_parse_time']+=microtime(true) - $this->timing['base_time']; |
|
| 323 | + $this->timing['base_parse_num']++; |
|
| 324 | 324 | return true; |
| 325 | 325 | } |
| 326 | 326 | $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
@@ -329,9 +329,9 @@ discard block |
||
| 329 | 329 | $curElement++; |
| 330 | 330 | $match=$snmptrans=array(); |
| 331 | 331 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
| 332 | - $this->objectsAll[$curElement],$match)) |
|
| 332 | + $this->objectsAll[$curElement], $match)) |
|
| 333 | 333 | { |
| 334 | - $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 334 | + $this->timing['base_check_time']+=microtime(true) - $this->timing['base_time']; |
|
| 335 | 335 | $this->timing['base_check_num']++; |
| 336 | 336 | return true; |
| 337 | 337 | } |
@@ -339,22 +339,22 @@ discard block |
||
| 339 | 339 | $this->oidDesc['name']=$match[1]; // Name |
| 340 | 340 | $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
| 341 | 341 | |
| 342 | - if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
| 342 | + if ($this->oidDesc['type'] == 0) // object type=0 : check if v1 trap |
|
| 343 | 343 | { |
| 344 | 344 | // Check if next is suboid -> in that case is cannot be a trap |
| 345 | - if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
|
| 345 | + if (preg_match("/^".$this->oidDesc['oid']."/", $this->objectsAll[$curElement + 1])) |
|
| 346 | 346 | { |
| 347 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 347 | + $this->timing['type0_check_time']+=microtime(true) - $this->timing['base_time']; |
|
| 348 | 348 | $this->timing['type0_check_num']++; |
| 349 | 349 | return true; |
| 350 | 350 | } |
| 351 | 351 | unset($snmptrans); |
| 352 | 352 | $retVal=0; |
| 353 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 354 | - ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 355 | - if ($retVal!=0) |
|
| 353 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 354 | + ' -Td '.$this->oidDesc['oid'].' | grep OBJECTS ', $snmptrans, $retVal); |
|
| 355 | + if ($retVal != 0) |
|
| 356 | 356 | { |
| 357 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 357 | + $this->timing['type0_check_time']+=microtime(true) - $this->timing['base_time']; |
|
| 358 | 358 | $this->timing['type0_check_num']++; |
| 359 | 359 | return true; |
| 360 | 360 | } |
@@ -362,9 +362,9 @@ discard block |
||
| 362 | 362 | // Force as trap. |
| 363 | 363 | $this->oidDesc['type']=21; |
| 364 | 364 | } |
| 365 | - if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
| 365 | + if ($onlyTraps === true && $this->oidDesc['type'] != 21) // if only traps and not a trap, continue |
|
| 366 | 366 | { |
| 367 | - $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
|
| 367 | + $this->timing['nottrap_time']+=microtime(true) - $this->timing['base_time']; |
|
| 368 | 368 | $this->timing['nottrap_num']++; |
| 369 | 369 | return true; |
| 370 | 370 | } |
@@ -379,35 +379,35 @@ discard block |
||
| 379 | 379 | { |
| 380 | 380 | $retVal=0; |
| 381 | 381 | $match=$snmptrans=array(); |
| 382 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 383 | - ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal); |
|
| 384 | - if ($retVal!=0) |
|
| 382 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 383 | + ' -Td '.$this->oidDesc['oid'], $snmptrans, $retVal); |
|
| 384 | + if ($retVal != 0) |
|
| 385 | 385 | { |
| 386 | - $this->logging->log('error executing snmptranslate',ERROR); |
|
| 386 | + $this->logging->log('error executing snmptranslate', ERROR); |
|
| 387 | 387 | return $snmptrans; |
| 388 | 388 | } |
| 389 | 389 | |
| 390 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 390 | + if (!preg_match('/^(.*)::/', $snmptrans[0], $match)) |
|
| 391 | 391 | { |
| 392 | - $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR); |
|
| 392 | + $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : '.$snmptrans[0], ERROR); |
|
| 393 | 393 | return $snmptrans; |
| 394 | 394 | } |
| 395 | 395 | $this->oidDesc['mib']=$match[1]; |
| 396 | 396 | |
| 397 | 397 | $numLine=1; |
| 398 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 398 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $snmptrans[$numLine], $match)) $numLine++; |
|
| 399 | 399 | if (isset($snmptrans[$numLine])) |
| 400 | 400 | { |
| 401 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 401 | + $snmptrans[$numLine]=preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/', '', $snmptrans[$numLine]); |
|
| 402 | 402 | |
| 403 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 403 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/', $snmptrans[$numLine])) |
|
| 404 | 404 | { |
| 405 | - $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 405 | + $this->oidDesc['description'].=preg_replace('/[\t ]+/', ' ', $snmptrans[$numLine]); |
|
| 406 | 406 | $numLine++; |
| 407 | 407 | } |
| 408 | 408 | if (isset($snmptrans[$numLine])) { |
| 409 | - $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 410 | - $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']); |
|
| 409 | + $this->oidDesc['description'].=preg_replace('/".*/', '', $snmptrans[$numLine]); |
|
| 410 | + $this->oidDesc['description']=preg_replace('/[\t ]+/', ' ', $this->oidDesc['description']); |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | } |
@@ -425,23 +425,23 @@ discard block |
||
| 425 | 425 | $match=array(); |
| 426 | 426 | foreach ($snmptrans as $line) |
| 427 | 427 | { |
| 428 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 428 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/', $line, $match)) |
|
| 429 | 429 | { |
| 430 | 430 | $objectName=$match[1]; |
| 431 | 431 | } |
| 432 | 432 | } |
| 433 | 433 | if ($objectName == null) |
| 434 | 434 | { |
| 435 | - $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG); |
|
| 436 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 435 | + $this->logging->log('No objects for '.$this->oidDesc['oid'], DEBUG); |
|
| 436 | + $this->timing['objects_time']+=microtime(true) - $this->timing['base_time']; |
|
| 437 | 437 | return null; |
| 438 | 438 | } |
| 439 | 439 | |
| 440 | 440 | $trapObjects=array(); |
| 441 | - while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match)) |
|
| 441 | + while (preg_match('/ *([^ ,]+) *,* */', $objectName, $match)) |
|
| 442 | 442 | { |
| 443 | - array_push($trapObjects,$match[1]); |
|
| 444 | - $objectName=preg_replace('/'.$match[0].'/','',$objectName); |
|
| 443 | + array_push($trapObjects, $match[1]); |
|
| 444 | + $objectName=preg_replace('/'.$match[0].'/', '', $objectName); |
|
| 445 | 445 | } |
| 446 | 446 | return $trapObjects; |
| 447 | 447 | } |
@@ -453,18 +453,18 @@ discard block |
||
| 453 | 453 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
| 454 | 454 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
| 455 | 455 | */ |
| 456 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 456 | + public function update_mib_database($display_progress=false, $check_change=false, $onlyTraps=true, $startOID='.1') |
|
| 457 | 457 | { |
| 458 | 458 | // Global Timing |
| 459 | - $timeTaken = microtime(true); |
|
| 459 | + $timeTaken=microtime(true); |
|
| 460 | 460 | |
| 461 | 461 | $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll |
| 462 | 462 | |
| 463 | 463 | $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex |
| 464 | 464 | |
| 465 | - $step=$basestep=$numElements/10; // output display of % done |
|
| 465 | + $step=$basestep=$numElements / 10; // output display of % done |
|
| 466 | 466 | $num_step=0; |
| 467 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 467 | + $timeFiveSec=microtime(true); // Used for display a '.' every <n> seconds |
|
| 468 | 468 | |
| 469 | 469 | // Create index for trap objects |
| 470 | 470 | $this->trapObjectsIndex=array(); |
@@ -472,33 +472,33 @@ discard block |
||
| 472 | 472 | // detailed timing (time_* vars) |
| 473 | 473 | $this->reset_update_timers(); |
| 474 | 474 | |
| 475 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 475 | + for ($curElement=0; $curElement < $numElements; $curElement++) |
|
| 476 | 476 | { |
| 477 | - $this->timing['base_time']= microtime(true); |
|
| 477 | + $this->timing['base_time']=microtime(true); |
|
| 478 | 478 | if ($display_progress) |
| 479 | 479 | { |
| 480 | - if ((microtime(true)-$timeFiveSec) > 2) |
|
| 480 | + if ((microtime(true) - $timeFiveSec) > 2) |
|
| 481 | 481 | { // echo a . every 2 sec |
| 482 | 482 | echo '.'; |
| 483 | - $timeFiveSec = microtime(true); |
|
| 483 | + $timeFiveSec=microtime(true); |
|
| 484 | 484 | } |
| 485 | - if ($curElement>$step) |
|
| 485 | + if ($curElement > $step) |
|
| 486 | 486 | { // display progress |
| 487 | 487 | $num_step++; |
| 488 | 488 | $step+=$basestep; |
| 489 | - echo "\n" . ($num_step*10). '% : '; |
|
| 489 | + echo "\n".($num_step * 10).'% : '; |
|
| 490 | 490 | } |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | $this->reset_oidDesc(); |
| 494 | - if ($this->detect_trap($curElement,$onlyTraps)===true) |
|
| 494 | + if ($this->detect_trap($curElement, $onlyTraps) === true) |
|
| 495 | 495 | { |
| 496 | 496 | continue; |
| 497 | 497 | } |
| 498 | 498 | |
| 499 | 499 | $this->timing['num_traps']++; |
| 500 | 500 | |
| 501 | - $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
|
| 501 | + $this->logging->log('Found trap : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], INFO); |
|
| 502 | 502 | if ($display_progress) echo '#'; // echo a # when trap found |
| 503 | 503 | |
| 504 | 504 | // get trap objects & source MIB |
@@ -508,14 +508,14 @@ discard block |
||
| 508 | 508 | |
| 509 | 509 | $update=$this->update_oid(); // Do update of trap. |
| 510 | 510 | |
| 511 | - $this->timing['update_time'] += microtime(true) - $this->timing['base_time']; |
|
| 511 | + $this->timing['update_time']+=microtime(true) - $this->timing['base_time']; |
|
| 512 | 512 | $this->timing['update_num']++; |
| 513 | 513 | |
| 514 | - $this->timing['base_time']= microtime(true); // Reset to check object time |
|
| 514 | + $this->timing['base_time']=microtime(true); // Reset to check object time |
|
| 515 | 515 | |
| 516 | - if (($update==0) && ($check_change===false)) |
|
| 516 | + if (($update == 0) && ($check_change === false)) |
|
| 517 | 517 | { // Trapd didn't change & force check disabled |
| 518 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 518 | + $this->timing['objects_time']+=microtime(true) - $this->timing['base_time']; |
|
| 519 | 519 | if ($display_progress) echo "C"; |
| 520 | 520 | continue; |
| 521 | 521 | } |
@@ -528,17 +528,17 @@ discard block |
||
| 528 | 528 | |
| 529 | 529 | $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false); |
| 530 | 530 | |
| 531 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 531 | + $this->timing['objects_time']+=microtime(true) - $this->timing['base_time']; |
|
| 532 | 532 | $this->timing['objects_num']++; |
| 533 | 533 | } |
| 534 | 534 | |
| 535 | 535 | if ($display_progress) |
| 536 | 536 | { |
| 537 | - echo "\nNumber of processed traps : ". $this->timing['num_traps'] ."\n"; |
|
| 538 | - echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num']) . " occurences\n"; |
|
| 539 | - echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n"; |
|
| 540 | - echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , "; |
|
| 541 | - echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n"; |
|
| 537 | + echo "\nNumber of processed traps : ".$this->timing['num_traps']."\n"; |
|
| 538 | + echo "\nParsing : ".number_format($this->timing['base_parse_time'] + $this->timing['base_check_time'], 1)." sec / ".($this->timing['base_parse_num'] + $this->timing['base_check_num'])." occurences\n"; |
|
| 539 | + echo "Detecting traps : ".number_format($this->timing['type0_check_time'] + $this->timing['nottrap_time'], 1)." sec / ".($this->timing['type0_check_num'] + $this->timing['nottrap_num'])." occurences\n"; |
|
| 540 | + echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'], 1)." sec , "; |
|
| 541 | + echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'], 1)." sec \n"; |
|
| 542 | 542 | |
| 543 | 543 | $timeTaken=microtime(true) - $timeTaken; |
| 544 | 544 | echo "Global time : ".round($timeTaken)." seconds\n"; |
@@ -188,7 +188,10 @@ discard block |
||
| 188 | 188 | $this->reset_oidDesc(); |
| 189 | 189 | |
| 190 | 190 | $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
| 191 | - if ($snmptrans === null) continue; // object not found |
|
| 191 | + if ($snmptrans === null) { |
|
| 192 | + continue; |
|
| 193 | + } |
|
| 194 | + // object not found |
|
| 192 | 195 | |
| 193 | 196 | $this->parse_object($snmptrans); |
| 194 | 197 | |
@@ -339,12 +342,14 @@ discard block |
||
| 339 | 342 | $this->oidDesc['name']=$match[1]; // Name |
| 340 | 343 | $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
| 341 | 344 | |
| 342 | - if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
| 345 | + if ($this->oidDesc['type']==0) { |
|
| 346 | + // object type=0 : check if v1 trap |
|
| 343 | 347 | { |
| 344 | 348 | // Check if next is suboid -> in that case is cannot be a trap |
| 345 | 349 | if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
| 346 | 350 | { |
| 347 | 351 | $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
| 352 | + } |
|
| 348 | 353 | $this->timing['type0_check_num']++; |
| 349 | 354 | return true; |
| 350 | 355 | } |
@@ -362,9 +367,11 @@ discard block |
||
| 362 | 367 | // Force as trap. |
| 363 | 368 | $this->oidDesc['type']=21; |
| 364 | 369 | } |
| 365 | - if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
| 370 | + if ($onlyTraps===true && $this->oidDesc['type']!=21) { |
|
| 371 | + // if only traps and not a trap, continue |
|
| 366 | 372 | { |
| 367 | 373 | $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
| 374 | + } |
|
| 368 | 375 | $this->timing['nottrap_num']++; |
| 369 | 376 | return true; |
| 370 | 377 | } |
@@ -395,7 +402,9 @@ discard block |
||
| 395 | 402 | $this->oidDesc['mib']=$match[1]; |
| 396 | 403 | |
| 397 | 404 | $numLine=1; |
| 398 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 405 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) { |
|
| 406 | + $numLine++; |
|
| 407 | + } |
|
| 399 | 408 | if (isset($snmptrans[$numLine])) |
| 400 | 409 | { |
| 401 | 410 | $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
@@ -499,7 +508,10 @@ discard block |
||
| 499 | 508 | $this->timing['num_traps']++; |
| 500 | 509 | |
| 501 | 510 | $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
| 502 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
| 511 | + if ($display_progress) { |
|
| 512 | + echo '#'; |
|
| 513 | + } |
|
| 514 | + // echo a # when trap found |
|
| 503 | 515 | |
| 504 | 516 | // get trap objects & source MIB |
| 505 | 517 | |
@@ -516,7 +528,9 @@ discard block |
||
| 516 | 528 | if (($update==0) && ($check_change===false)) |
| 517 | 529 | { // Trapd didn't change & force check disabled |
| 518 | 530 | $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
| 519 | - if ($display_progress) echo "C"; |
|
| 531 | + if ($display_progress) { |
|
| 532 | + echo "C"; |
|
| 533 | + } |
|
| 520 | 534 | continue; |
| 521 | 535 | } |
| 522 | 536 | |
@@ -11,13 +11,12 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | //use Icinga\Web\Form as Form; |
| 13 | 13 | /** Rules management |
| 14 | - |
|
| 15 | -*/ |
|
| 14 | + */ |
|
| 16 | 15 | class HandlerController extends TrapsController |
| 17 | 16 | { |
| 18 | 17 | |
| 19 | 18 | /** index : list existing rules |
| 20 | - */ |
|
| 19 | + */ |
|
| 21 | 20 | public function indexAction() |
| 22 | 21 | { |
| 23 | 22 | $this->checkReadPermission(); |
@@ -41,22 +40,22 @@ discard block |
||
| 41 | 40 | */ |
| 42 | 41 | public function testruleAction() |
| 43 | 42 | { |
| 44 | - $this->checkReadPermission(); |
|
| 45 | - $this->getTabs()->add('get',array( |
|
| 46 | - 'active' => true, |
|
| 47 | - 'label' => $this->translate('Test Rule'), |
|
| 48 | - 'url' => Url::fromRequest() |
|
| 49 | - )); |
|
| 43 | + $this->checkReadPermission(); |
|
| 44 | + $this->getTabs()->add('get',array( |
|
| 45 | + 'active' => true, |
|
| 46 | + 'label' => $this->translate('Test Rule'), |
|
| 47 | + 'url' => Url::fromRequest() |
|
| 48 | + )); |
|
| 50 | 49 | |
| 51 | 50 | |
| 52 | - if ($this->params->get('rule') !== null) |
|
| 53 | - { |
|
| 54 | - $this->view->rule= $this->params->get('rule'); |
|
| 55 | - } |
|
| 56 | - else |
|
| 57 | - { |
|
| 58 | - $this->view->rule=''; |
|
| 59 | - } |
|
| 51 | + if ($this->params->get('rule') !== null) |
|
| 52 | + { |
|
| 53 | + $this->view->rule= $this->params->get('rule'); |
|
| 54 | + } |
|
| 55 | + else |
|
| 56 | + { |
|
| 57 | + $this->view->rule=''; |
|
| 58 | + } |
|
| 60 | 59 | } |
| 61 | 60 | |
| 62 | 61 | /** |
@@ -64,31 +63,31 @@ discard block |
||
| 64 | 63 | */ |
| 65 | 64 | private function add_setup_vars() |
| 66 | 65 | { |
| 67 | - // variables to send to view |
|
| 68 | - $this->view->hostlist=array(); // host list to input datalist |
|
| 69 | - $this->view->hostname=''; // Host name in input text |
|
| 70 | - $this->view->serviceGet=false; // Set to true to get list of service if only one host set |
|
| 71 | - $this->view->serviceSet=null; // Select service in services select (must have serviceGet=true). |
|
| 72 | - $this->view->mainoid=''; // Trap OID |
|
| 73 | - $this->view->mib=''; // Trap mib |
|
| 74 | - $this->view->name=''; // Trap name |
|
| 75 | - $this->view->trapListForMIB=array(); // Trap list if mib exists for trap |
|
| 76 | - $this->view->objectList=array(); // objects sent with trap |
|
| 77 | - $this->view->display=''; // Initial display |
|
| 78 | - $this->view->rule=''; // rule display |
|
| 79 | - $this->view->revertOK=''; // revert OK in seconds |
|
| 80 | - $this->view->hostid=-1; // normally set by javascript serviceGet() |
|
| 81 | - $this->view->ruleid=-1; // Rule id in DB for update & delete |
|
| 82 | - $this->view->setToUpdate=false; // set form as update form |
|
| 83 | - $this->view->setRuleMatch=-1; // set action on rule match (default nothing) |
|
| 84 | - $this->view->setRuleNoMatch=-1; // set action on rule no match (default nothing) |
|
| 66 | + // variables to send to view |
|
| 67 | + $this->view->hostlist=array(); // host list to input datalist |
|
| 68 | + $this->view->hostname=''; // Host name in input text |
|
| 69 | + $this->view->serviceGet=false; // Set to true to get list of service if only one host set |
|
| 70 | + $this->view->serviceSet=null; // Select service in services select (must have serviceGet=true). |
|
| 71 | + $this->view->mainoid=''; // Trap OID |
|
| 72 | + $this->view->mib=''; // Trap mib |
|
| 73 | + $this->view->name=''; // Trap name |
|
| 74 | + $this->view->trapListForMIB=array(); // Trap list if mib exists for trap |
|
| 75 | + $this->view->objectList=array(); // objects sent with trap |
|
| 76 | + $this->view->display=''; // Initial display |
|
| 77 | + $this->view->rule=''; // rule display |
|
| 78 | + $this->view->revertOK=''; // revert OK in seconds |
|
| 79 | + $this->view->hostid=-1; // normally set by javascript serviceGet() |
|
| 80 | + $this->view->ruleid=-1; // Rule id in DB for update & delete |
|
| 81 | + $this->view->setToUpdate=false; // set form as update form |
|
| 82 | + $this->view->setRuleMatch=-1; // set action on rule match (default nothing) |
|
| 83 | + $this->view->setRuleNoMatch=-1; // set action on rule no match (default nothing) |
|
| 85 | 84 | |
| 86 | - $this->view->selectGroup=false; // Select by group if true |
|
| 87 | - $this->view->hostgroupid=-1; // host group id |
|
| 88 | - $this->view->serviceGroupGet=false; // Get list of service for group (set serviceSet to select one) |
|
| 85 | + $this->view->selectGroup=false; // Select by group if true |
|
| 86 | + $this->view->hostgroupid=-1; // host group id |
|
| 87 | + $this->view->serviceGroupGet=false; // Get list of service for group (set serviceSet to select one) |
|
| 89 | 88 | |
| 90 | - $this->view->modifier=null; |
|
| 91 | - $this->view->modified=null; |
|
| 89 | + $this->view->modifier=null; |
|
| 90 | + $this->view->modified=null; |
|
| 92 | 91 | } |
| 93 | 92 | |
| 94 | 93 | /** |
@@ -97,102 +96,102 @@ discard block |
||
| 97 | 96 | */ |
| 98 | 97 | private function add_from_existing($trapid) |
| 99 | 98 | { |
| 100 | - /********** Setup from existing trap ***************/ |
|
| 101 | - // Get the full trap info |
|
| 102 | - $trapDetail=$this->getTrapDetail($trapid); |
|
| 99 | + /********** Setup from existing trap ***************/ |
|
| 100 | + // Get the full trap info |
|
| 101 | + $trapDetail=$this->getTrapDetail($trapid); |
|
| 103 | 102 | |
| 104 | - $hostfilter=$trapDetail->source_ip; |
|
| 103 | + $hostfilter=$trapDetail->source_ip; |
|
| 105 | 104 | |
| 106 | - // Get host |
|
| 107 | - try |
|
| 108 | - { |
|
| 109 | - $hosts=$this->getUIDatabase()->getHostByIP($hostfilter); |
|
| 110 | - } |
|
| 111 | - catch (Exception $e) |
|
| 112 | - { |
|
| 113 | - $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
|
| 114 | - } |
|
| 105 | + // Get host |
|
| 106 | + try |
|
| 107 | + { |
|
| 108 | + $hosts=$this->getUIDatabase()->getHostByIP($hostfilter); |
|
| 109 | + } |
|
| 110 | + catch (Exception $e) |
|
| 111 | + { |
|
| 112 | + $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
|
| 113 | + } |
|
| 115 | 114 | |
| 116 | 115 | |
| 117 | - // if one unique host found -> put id text input |
|
| 118 | - if (count($hosts)==1) { |
|
| 119 | - $this->view->hostname=$hosts[0]->name; |
|
| 120 | - //$hostid=$hosts[0]->id; |
|
| 121 | - // Tell JS to get services when page is loaded |
|
| 122 | - $this->view->serviceGet=true; |
|
| 116 | + // if one unique host found -> put id text input |
|
| 117 | + if (count($hosts)==1) { |
|
| 118 | + $this->view->hostname=$hosts[0]->name; |
|
| 119 | + //$hostid=$hosts[0]->id; |
|
| 120 | + // Tell JS to get services when page is loaded |
|
| 121 | + $this->view->serviceGet=true; |
|
| 123 | 122 | |
| 124 | - } |
|
| 125 | - else |
|
| 126 | - { |
|
| 127 | - foreach($hosts as $key=>$val) |
|
| 128 | - { |
|
| 129 | - array_push($this->view->hostlist,$hosts[$key]->name); |
|
| 130 | - } |
|
| 131 | - } |
|
| 123 | + } |
|
| 124 | + else |
|
| 125 | + { |
|
| 126 | + foreach($hosts as $key=>$val) |
|
| 127 | + { |
|
| 128 | + array_push($this->view->hostlist,$hosts[$key]->name); |
|
| 129 | + } |
|
| 130 | + } |
|
| 132 | 131 | |
| 133 | - // set up trap oid and objects received by the trap |
|
| 132 | + // set up trap oid and objects received by the trap |
|
| 134 | 133 | |
| 135 | - $this->view->mainoid=$trapDetail->trap_oid; |
|
| 136 | - if ($trapDetail->trap_name_mib != null) |
|
| 137 | - { |
|
| 138 | - $this->view->mib=$trapDetail->trap_name_mib; |
|
| 139 | - $this->view->name=$trapDetail->trap_name; |
|
| 140 | - $this->view->trapListForMIB=$this->getMIB() |
|
| 141 | - ->getTrapList($trapDetail->trap_name_mib); |
|
| 142 | - } |
|
| 134 | + $this->view->mainoid=$trapDetail->trap_oid; |
|
| 135 | + if ($trapDetail->trap_name_mib != null) |
|
| 136 | + { |
|
| 137 | + $this->view->mib=$trapDetail->trap_name_mib; |
|
| 138 | + $this->view->name=$trapDetail->trap_name; |
|
| 139 | + $this->view->trapListForMIB=$this->getMIB() |
|
| 140 | + ->getTrapList($trapDetail->trap_name_mib); |
|
| 141 | + } |
|
| 143 | 142 | |
| 144 | - // Get all objects that can be in trap from MIB |
|
| 145 | - $allObjects=$this->getMIB()->getObjectList($trapDetail->trap_oid); |
|
| 146 | - // Get all objects in current Trap |
|
| 147 | - $currentTrapObjects=$this->getTrapobjects($trapid); |
|
| 148 | - $oid_index=1; |
|
| 149 | - foreach ($currentTrapObjects as $key => $val) |
|
| 150 | - { |
|
| 151 | - $currentObjectType='Unknown'; |
|
| 152 | - $currentObjectTypeEnum='Unknown'; |
|
| 153 | - if (isset($allObjects[$val->oid]['type'])) |
|
| 154 | - { |
|
| 155 | - $currentObjectType=$allObjects[$val->oid]['type']; |
|
| 156 | - $currentObjectTypeEnum=$allObjects[$val->oid]['type_enum']; |
|
| 157 | - } |
|
| 158 | - $currentObject=array( |
|
| 159 | - $oid_index, |
|
| 160 | - $val->oid, |
|
| 161 | - $val->oid_name_mib, |
|
| 162 | - $val->oid_name, |
|
| 163 | - $val->value, |
|
| 164 | - $currentObjectType, |
|
| 165 | - $currentObjectTypeEnum |
|
| 166 | - ); |
|
| 167 | - $oid_index++; |
|
| 168 | - array_push($this->view->objectList,$currentObject); |
|
| 169 | - // set currrent object to null in allObjects |
|
| 170 | - if (isset($allObjects[$val->oid])) |
|
| 171 | - { |
|
| 172 | - $allObjects[$val->oid]=null; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
|
| 176 | - { |
|
| 177 | - foreach ($allObjects as $key => $val) |
|
| 178 | - { |
|
| 179 | - if ($val==null) { continue; } |
|
| 180 | - array_push($this->view->objectList, array( |
|
| 181 | - $oid_index, |
|
| 182 | - $key, |
|
| 183 | - $allObjects[$key]['mib'], |
|
| 184 | - $allObjects[$key]['name'], |
|
| 185 | - '', |
|
| 186 | - $allObjects[$key]['type'], |
|
| 187 | - $allObjects[$key]['type_enum'] |
|
| 188 | - )); |
|
| 189 | - $oid_index++; |
|
| 190 | - } |
|
| 191 | - } |
|
| 143 | + // Get all objects that can be in trap from MIB |
|
| 144 | + $allObjects=$this->getMIB()->getObjectList($trapDetail->trap_oid); |
|
| 145 | + // Get all objects in current Trap |
|
| 146 | + $currentTrapObjects=$this->getTrapobjects($trapid); |
|
| 147 | + $oid_index=1; |
|
| 148 | + foreach ($currentTrapObjects as $key => $val) |
|
| 149 | + { |
|
| 150 | + $currentObjectType='Unknown'; |
|
| 151 | + $currentObjectTypeEnum='Unknown'; |
|
| 152 | + if (isset($allObjects[$val->oid]['type'])) |
|
| 153 | + { |
|
| 154 | + $currentObjectType=$allObjects[$val->oid]['type']; |
|
| 155 | + $currentObjectTypeEnum=$allObjects[$val->oid]['type_enum']; |
|
| 156 | + } |
|
| 157 | + $currentObject=array( |
|
| 158 | + $oid_index, |
|
| 159 | + $val->oid, |
|
| 160 | + $val->oid_name_mib, |
|
| 161 | + $val->oid_name, |
|
| 162 | + $val->value, |
|
| 163 | + $currentObjectType, |
|
| 164 | + $currentObjectTypeEnum |
|
| 165 | + ); |
|
| 166 | + $oid_index++; |
|
| 167 | + array_push($this->view->objectList,$currentObject); |
|
| 168 | + // set currrent object to null in allObjects |
|
| 169 | + if (isset($allObjects[$val->oid])) |
|
| 170 | + { |
|
| 171 | + $allObjects[$val->oid]=null; |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
|
| 175 | + { |
|
| 176 | + foreach ($allObjects as $key => $val) |
|
| 177 | + { |
|
| 178 | + if ($val==null) { continue; } |
|
| 179 | + array_push($this->view->objectList, array( |
|
| 180 | + $oid_index, |
|
| 181 | + $key, |
|
| 182 | + $allObjects[$key]['mib'], |
|
| 183 | + $allObjects[$key]['name'], |
|
| 184 | + '', |
|
| 185 | + $allObjects[$key]['type'], |
|
| 186 | + $allObjects[$key]['type_enum'] |
|
| 187 | + )); |
|
| 188 | + $oid_index++; |
|
| 189 | + } |
|
| 190 | + } |
|
| 192 | 191 | |
| 193 | - // Add a simple display |
|
| 194 | - $this->view->display='Trap '.$trapDetail->trap_name.' received'; |
|
| 195 | - $this->view->create_basic_rule=true; |
|
| 192 | + // Add a simple display |
|
| 193 | + $this->view->display='Trap '.$trapDetail->trap_name.' received'; |
|
| 194 | + $this->view->create_basic_rule=true; |
|
| 196 | 195 | } |
| 197 | 196 | |
| 198 | 197 | /** |
@@ -201,29 +200,29 @@ discard block |
||
| 201 | 200 | */ |
| 202 | 201 | private function add_check_host_exists($ruleDetail) |
| 203 | 202 | { |
| 204 | - // Check if hostname still exists |
|
| 205 | - $host_get=$this->getUIDatabase()->getHostByName($this->view->hostname); |
|
| 203 | + // Check if hostname still exists |
|
| 204 | + $host_get=$this->getUIDatabase()->getHostByName($this->view->hostname); |
|
| 206 | 205 | |
| 207 | - if (count($host_get)==0) |
|
| 208 | - { |
|
| 209 | - $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
|
| 210 | - $this->view->serviceGet=false; |
|
| 211 | - } |
|
| 212 | - else |
|
| 213 | - { |
|
| 214 | - // Tell JS to get services when page is loaded |
|
| 215 | - $this->view->serviceGet=true; |
|
| 216 | - // get service id for form to set : |
|
| 217 | - $serviceID=$this->getUIDatabase()->getServiceIDByName($this->view->hostname,$ruleDetail->service_name); |
|
| 218 | - if (count($serviceID) ==0) |
|
| 219 | - { |
|
| 220 | - $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
| 221 | - } |
|
| 222 | - else |
|
| 223 | - { |
|
| 224 | - $this->view->serviceSet=$serviceID[0]->id; |
|
| 225 | - } |
|
| 226 | - } |
|
| 206 | + if (count($host_get)==0) |
|
| 207 | + { |
|
| 208 | + $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
|
| 209 | + $this->view->serviceGet=false; |
|
| 210 | + } |
|
| 211 | + else |
|
| 212 | + { |
|
| 213 | + // Tell JS to get services when page is loaded |
|
| 214 | + $this->view->serviceGet=true; |
|
| 215 | + // get service id for form to set : |
|
| 216 | + $serviceID=$this->getUIDatabase()->getServiceIDByName($this->view->hostname,$ruleDetail->service_name); |
|
| 217 | + if (count($serviceID) ==0) |
|
| 218 | + { |
|
| 219 | + $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
| 220 | + } |
|
| 221 | + else |
|
| 222 | + { |
|
| 223 | + $this->view->serviceSet=$serviceID[0]->id; |
|
| 224 | + } |
|
| 225 | + } |
|
| 227 | 226 | } |
| 228 | 227 | |
| 229 | 228 | /** |
@@ -232,33 +231,33 @@ discard block |
||
| 232 | 231 | */ |
| 233 | 232 | private function add_check_hostgroup_exists($ruleDetail) |
| 234 | 233 | { |
| 235 | - // Check if groupe exists |
|
| 236 | - $group_get=$this->getUIDatabase()->getHostGroupByName($this->view->hostgroupname); |
|
| 237 | - if (count($group_get)==0) |
|
| 238 | - { |
|
| 239 | - $this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore'; |
|
| 240 | - $this->view->serviceGroupGet=false; |
|
| 241 | - } |
|
| 242 | - else |
|
| 243 | - { |
|
| 244 | - $grpServices=$this->getUIDatabase()->getServicesByHostGroupid($group_get[0]->id); |
|
| 245 | - $foundGrpService=0; |
|
| 246 | - foreach ($grpServices as $grpService) |
|
| 247 | - { |
|
| 248 | - if ($grpService[0] == $ruleDetail->service_name) |
|
| 249 | - { |
|
| 250 | - $foundGrpService=1; |
|
| 251 | - $this->view->serviceSet=$ruleDetail->service_name; |
|
| 252 | - } |
|
| 253 | - } |
|
| 234 | + // Check if groupe exists |
|
| 235 | + $group_get=$this->getUIDatabase()->getHostGroupByName($this->view->hostgroupname); |
|
| 236 | + if (count($group_get)==0) |
|
| 237 | + { |
|
| 238 | + $this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore'; |
|
| 239 | + $this->view->serviceGroupGet=false; |
|
| 240 | + } |
|
| 241 | + else |
|
| 242 | + { |
|
| 243 | + $grpServices=$this->getUIDatabase()->getServicesByHostGroupid($group_get[0]->id); |
|
| 244 | + $foundGrpService=0; |
|
| 245 | + foreach ($grpServices as $grpService) |
|
| 246 | + { |
|
| 247 | + if ($grpService[0] == $ruleDetail->service_name) |
|
| 248 | + { |
|
| 249 | + $foundGrpService=1; |
|
| 250 | + $this->view->serviceSet=$ruleDetail->service_name; |
|
| 251 | + } |
|
| 252 | + } |
|
| 254 | 253 | |
| 255 | - // Tell JS to get services when page is loaded |
|
| 256 | - $this->view->serviceGroupGet=true; |
|
| 257 | - if ($foundGrpService==0) |
|
| 258 | - { |
|
| 259 | - $this->view->warning_message.=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
| 260 | - } |
|
| 261 | - } |
|
| 254 | + // Tell JS to get services when page is loaded |
|
| 255 | + $this->view->serviceGroupGet=true; |
|
| 256 | + if ($foundGrpService==0) |
|
| 257 | + { |
|
| 258 | + $this->view->warning_message.=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
| 259 | + } |
|
| 260 | + } |
|
| 262 | 261 | } |
| 263 | 262 | |
| 264 | 263 | /** |
@@ -270,52 +269,52 @@ discard block |
||
| 270 | 269 | */ |
| 271 | 270 | private function add_create_trap_object_list(&$display, &$rule) |
| 272 | 271 | { |
| 273 | - $curObjectList=array(); |
|
| 274 | - $index=1; |
|
| 275 | - // check in display & rule for : OID(<oid>) |
|
| 276 | - $matches=array(); |
|
| 277 | - while ( preg_match('/_OID\(([\.0-9\*]+)\)/',$display,$matches) || |
|
| 278 | - preg_match('/_OID\(([\.0-9\*]+)\)/',$rule,$matches)) |
|
| 279 | - { |
|
| 280 | - $curOid=$matches[1]; |
|
| 272 | + $curObjectList=array(); |
|
| 273 | + $index=1; |
|
| 274 | + // check in display & rule for : OID(<oid>) |
|
| 275 | + $matches=array(); |
|
| 276 | + while ( preg_match('/_OID\(([\.0-9\*]+)\)/',$display,$matches) || |
|
| 277 | + preg_match('/_OID\(([\.0-9\*]+)\)/',$rule,$matches)) |
|
| 278 | + { |
|
| 279 | + $curOid=$matches[1]; |
|
| 281 | 280 | |
| 282 | - if ( (preg_match('/\*/',$curOid) == 0 ) |
|
| 283 | - && ($object=$this->getMIB()->translateOID($curOid)) != null) |
|
| 284 | - { |
|
| 285 | - array_push($curObjectList, array( |
|
| 286 | - $index, |
|
| 287 | - $curOid, |
|
| 288 | - $object['mib'], |
|
| 289 | - $object['name'], |
|
| 290 | - '', |
|
| 291 | - $object['type'], |
|
| 292 | - $object['type_enum'] |
|
| 293 | - )); |
|
| 294 | - } |
|
| 295 | - else |
|
| 296 | - { |
|
| 297 | - array_push($curObjectList, array( |
|
| 298 | - $index, |
|
| 299 | - $curOid, |
|
| 300 | - 'not found', |
|
| 301 | - 'not found', |
|
| 302 | - '', |
|
| 303 | - 'not found', |
|
| 304 | - 'not found' |
|
| 305 | - )); |
|
| 306 | - } |
|
| 307 | - $curOid = preg_replace('/\*/','\*',$curOid); |
|
| 308 | - $display=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$display); |
|
| 309 | - $rule=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$rule); |
|
| 310 | - $index++; |
|
| 311 | - } |
|
| 312 | - return $curObjectList; |
|
| 281 | + if ( (preg_match('/\*/',$curOid) == 0 ) |
|
| 282 | + && ($object=$this->getMIB()->translateOID($curOid)) != null) |
|
| 283 | + { |
|
| 284 | + array_push($curObjectList, array( |
|
| 285 | + $index, |
|
| 286 | + $curOid, |
|
| 287 | + $object['mib'], |
|
| 288 | + $object['name'], |
|
| 289 | + '', |
|
| 290 | + $object['type'], |
|
| 291 | + $object['type_enum'] |
|
| 292 | + )); |
|
| 293 | + } |
|
| 294 | + else |
|
| 295 | + { |
|
| 296 | + array_push($curObjectList, array( |
|
| 297 | + $index, |
|
| 298 | + $curOid, |
|
| 299 | + 'not found', |
|
| 300 | + 'not found', |
|
| 301 | + '', |
|
| 302 | + 'not found', |
|
| 303 | + 'not found' |
|
| 304 | + )); |
|
| 305 | + } |
|
| 306 | + $curOid = preg_replace('/\*/','\*',$curOid); |
|
| 307 | + $display=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$display); |
|
| 308 | + $rule=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$rule); |
|
| 309 | + $index++; |
|
| 310 | + } |
|
| 311 | + return $curObjectList; |
|
| 313 | 312 | } |
| 314 | 313 | |
| 315 | 314 | /** Add a handler |
| 316 | - * Get params fromid : setup from existing trap (id of trap table) |
|
| 317 | - * Get param ruleid : edit from existing handler (id of rule table) |
|
| 318 | - */ |
|
| 315 | + * Get params fromid : setup from existing trap (id of trap table) |
|
| 316 | + * Get param ruleid : edit from existing handler (id of rule table) |
|
| 317 | + */ |
|
| 319 | 318 | public function addAction() |
| 320 | 319 | { |
| 321 | 320 | $this->checkConfigPermission(); |
@@ -334,8 +333,8 @@ discard block |
||
| 334 | 333 | //$this->view->trapvalues=false; // Set to true to display 'value' colum in objects |
| 335 | 334 | |
| 336 | 335 | if (($trapid = $this->params->get('fromid')) !== null) { |
| 337 | - /********** Setup from existing trap ***************/ |
|
| 338 | - $this->add_from_existing($trapid); |
|
| 336 | + /********** Setup from existing trap ***************/ |
|
| 337 | + $this->add_from_existing($trapid); |
|
| 339 | 338 | return; |
| 340 | 339 | } |
| 341 | 340 | |
@@ -360,14 +359,14 @@ discard block |
||
| 360 | 359 | $this->view->warning_message=''; |
| 361 | 360 | if ($this->view->hostname != null) |
| 362 | 361 | { |
| 363 | - $this->view->selectGroup=false; |
|
| 364 | - // Check if hostname still exists |
|
| 365 | - $this->add_check_host_exists($ruleDetail); |
|
| 362 | + $this->view->selectGroup=false; |
|
| 363 | + // Check if hostname still exists |
|
| 364 | + $this->add_check_host_exists($ruleDetail); |
|
| 366 | 365 | } |
| 367 | 366 | else |
| 368 | 367 | { |
| 369 | - $this->view->selectGroup=true; |
|
| 370 | - $this->add_check_hostgroup_exists($ruleDetail); // Check if groupe exists |
|
| 368 | + $this->view->selectGroup=true; |
|
| 369 | + $this->add_check_hostgroup_exists($ruleDetail); // Check if groupe exists |
|
| 371 | 370 | } |
| 372 | 371 | |
| 373 | 372 | $this->view->mainoid=$ruleDetail->trap_oid; |
@@ -395,9 +394,9 @@ discard block |
||
| 395 | 394 | } |
| 396 | 395 | |
| 397 | 396 | /** Validate form and output message to user |
| 398 | - * @param in postdata |
|
| 399 | - * @return string status : OK / <Message> |
|
| 400 | - **/ |
|
| 397 | + * @param in postdata |
|
| 398 | + * @return string status : OK / <Message> |
|
| 399 | + **/ |
|
| 401 | 400 | protected function handlerformAction() |
| 402 | 401 | { |
| 403 | 402 | $postData=$this->getRequest()->getPost(); |
@@ -428,7 +427,7 @@ discard block |
||
| 428 | 427 | { |
| 429 | 428 | try |
| 430 | 429 | { |
| 431 | - $this->getUIDatabase()->deleteRule($postData[$params['db_rule']['post']]); |
|
| 430 | + $this->getUIDatabase()->deleteRule($postData[$params['db_rule']['post']]); |
|
| 432 | 431 | } |
| 433 | 432 | catch (Exception $e) |
| 434 | 433 | { |
@@ -438,7 +437,7 @@ discard block |
||
| 438 | 437 | //$this->Module()-> |
| 439 | 438 | $this->_helper->json(array( |
| 440 | 439 | 'status'=>'OK', |
| 441 | - 'redirect'=>'trapdirector/handler' |
|
| 440 | + 'redirect'=>'trapdirector/handler' |
|
| 442 | 441 | |
| 443 | 442 | )); |
| 444 | 443 | } |
@@ -465,7 +464,7 @@ discard block |
||
| 465 | 464 | $isHostGroup=($params['hostgroup']['val'] == 1)?true:false; |
| 466 | 465 | if (! $isHostGroup ) |
| 467 | 466 | { // checks if selection by host |
| 468 | - $hostAddr=$this->getUIDatabase()->getHostInfoByID($params['hostid']['val']); |
|
| 467 | + $hostAddr=$this->getUIDatabase()->getHostInfoByID($params['hostid']['val']); |
|
| 469 | 468 | $params['ip4']['val']=$hostAddr->ip4; |
| 470 | 469 | $params['ip6']['val']=$hostAddr->ip6; |
| 471 | 470 | $checkHostName=$hostAddr->name; |
@@ -476,8 +475,8 @@ discard block |
||
| 476 | 475 | } |
| 477 | 476 | if (!is_numeric($params['serviceid']['val'])) |
| 478 | 477 | { |
| 479 | - $this->_helper->json(array('status'=>"Invalid service id ". $params['serviceid']['val'])); |
|
| 480 | - return; |
|
| 478 | + $this->_helper->json(array('status'=>"Invalid service id ". $params['serviceid']['val'])); |
|
| 479 | + return; |
|
| 481 | 480 | } |
| 482 | 481 | $serviceName=$this->getUIDatabase()->getObjectNameByid($params['serviceid']['val']); |
| 483 | 482 | if ($params['service_name']['val'] != $serviceName->name2) |
@@ -488,7 +487,7 @@ discard block |
||
| 488 | 487 | } |
| 489 | 488 | else |
| 490 | 489 | { |
| 491 | - $object=$this->getUIDatabase()->getObjectNameByid($params['hostid']['val']); |
|
| 490 | + $object=$this->getUIDatabase()->getObjectNameByid($params['hostid']['val']); |
|
| 492 | 491 | if ($params['host_name']['val'] != $object->name1) |
| 493 | 492 | { |
| 494 | 493 | $this->_helper->json(array('status'=>"Invalid object group id : Please re enter service")); |
@@ -509,11 +508,11 @@ discard block |
||
| 509 | 508 | // echo '<br>'; print_r($dbparams);echo '<br>'; |
| 510 | 509 | if ($params['db_rule']['val'] == -1 ) |
| 511 | 510 | { |
| 512 | - $ruleID=$this->getUIDatabase()->addHandlerRule($dbparams); |
|
| 511 | + $ruleID=$this->getUIDatabase()->addHandlerRule($dbparams); |
|
| 513 | 512 | } |
| 514 | 513 | else |
| 515 | 514 | { |
| 516 | - $this->getUIDatabase()->updateHandlerRule($dbparams,$params['db_rule']['val']); |
|
| 515 | + $this->getUIDatabase()->updateHandlerRule($dbparams,$params['db_rule']['val']); |
|
| 517 | 516 | $ruleID=$params['db_rule']['val']; |
| 518 | 517 | } |
| 519 | 518 | } |
@@ -527,9 +526,9 @@ discard block |
||
| 527 | 526 | } |
| 528 | 527 | |
| 529 | 528 | /** Get trap detail by trapid. |
| 530 | - * @param integer $trapid : id of trap in received table |
|
| 531 | - * @return array (objects) |
|
| 532 | - */ |
|
| 529 | + * @param integer $trapid : id of trap in received table |
|
| 530 | + * @return array (objects) |
|
| 531 | + */ |
|
| 533 | 532 | protected function getTrapDetail($trapid) |
| 534 | 533 | { |
| 535 | 534 | if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
@@ -545,14 +544,14 @@ discard block |
||
| 545 | 544 | } |
| 546 | 545 | try |
| 547 | 546 | { |
| 548 | - $query = $dbConn->select() |
|
| 547 | + $query = $dbConn->select() |
|
| 549 | 548 | ->from($this->getModuleConfig()->getTrapTableName(),$elmts) |
| 550 | 549 | ->where('id=?',$trapid); |
| 551 | 550 | $trapDetail=$dbConn->fetchRow($query); |
| 552 | 551 | if ( $trapDetail == null ) |
| 553 | 552 | { |
| 554 | - $trapDetail = 'NULL'; |
|
| 555 | - throw new Exception('No traps was found with id = '.$trapid); |
|
| 553 | + $trapDetail = 'NULL'; |
|
| 554 | + throw new Exception('No traps was found with id = '.$trapid); |
|
| 556 | 555 | } |
| 557 | 556 | } |
| 558 | 557 | catch (Exception $e) |
@@ -566,9 +565,9 @@ discard block |
||
| 566 | 565 | } |
| 567 | 566 | |
| 568 | 567 | /** Get trap objects |
| 569 | - * @param integer $trapid : trap id |
|
| 570 | - * @return array : full column in db of trap id |
|
| 571 | - */ |
|
| 568 | + * @param integer $trapid : trap id |
|
| 569 | + * @return array : full column in db of trap id |
|
| 570 | + */ |
|
| 572 | 571 | protected function getTrapobjects($trapid) |
| 573 | 572 | { |
| 574 | 573 | if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
@@ -584,7 +583,7 @@ discard block |
||
| 584 | 583 | } |
| 585 | 584 | try |
| 586 | 585 | { |
| 587 | - $query = $dbConn->select() |
|
| 586 | + $query = $dbConn->select() |
|
| 588 | 587 | ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
| 589 | 588 | ->where('trap_id=?',$trapid); |
| 590 | 589 | $trapDetail=$dbConn->fetchAll($query); |
@@ -600,10 +599,10 @@ discard block |
||
| 600 | 599 | } |
| 601 | 600 | |
| 602 | 601 | /** Get rule detail by ruleid. |
| 603 | - * @param integer $ruleid int id of rule in rule table |
|
| 604 | - * @return object|array : column objects in db |
|
| 605 | - * |
|
| 606 | - */ |
|
| 602 | + * @param integer $ruleid int id of rule in rule table |
|
| 603 | + * @return object|array : column objects in db |
|
| 604 | + * |
|
| 605 | + */ |
|
| 607 | 606 | protected function getRuleDetail($ruleid) |
| 608 | 607 | { |
| 609 | 608 | if (!preg_match('/^[0-9]+$/',$ruleid)) { throw new Exception('Invalid id'); } |
@@ -614,7 +613,7 @@ discard block |
||
| 614 | 613 | // *************** Get main data |
| 615 | 614 | try |
| 616 | 615 | { |
| 617 | - $query = $dbConn->select() |
|
| 616 | + $query = $dbConn->select() |
|
| 618 | 617 | ->from($this->getModuleConfig()->getTrapRuleName(),$queryArray) |
| 619 | 618 | ->where('id=?',$ruleid); |
| 620 | 619 | $ruleDetail=$dbConn->fetchRow($query); |
@@ -631,7 +630,7 @@ discard block |
||
| 631 | 630 | } |
| 632 | 631 | |
| 633 | 632 | /** Setup tabs for rules |
| 634 | - */ |
|
| 633 | + */ |
|
| 635 | 634 | protected function prepareTabs() |
| 636 | 635 | { |
| 637 | 636 | return $this->getTabs()->add('status', array( |
@@ -23,16 +23,16 @@ discard block |
||
| 23 | 23 | $this->checkReadPermission(); |
| 24 | 24 | $this->prepareTabs()->activate('status'); |
| 25 | 25 | |
| 26 | - $dbConn = $this->getUIDatabase()->getDb(); |
|
| 26 | + $dbConn=$this->getUIDatabase()->getDb(); |
|
| 27 | 27 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 28 | 28 | |
| 29 | 29 | $this->getHandlerListTable()->setConnection($dbConn); |
| 30 | 30 | $this->getHandlerListTable()->setMibloader($this->getMIB()); |
| 31 | 31 | // Apply pagination limits |
| 32 | - $this->view->table=$this->applyPaginationLimits($this->getHandlerListTable(),$this->getModuleConfig()->itemListDisplay()); |
|
| 32 | + $this->view->table=$this->applyPaginationLimits($this->getHandlerListTable(), $this->getModuleConfig()->itemListDisplay()); |
|
| 33 | 33 | |
| 34 | 34 | // Set Filter |
| 35 | - $this->view->filterEditor = $this->getHandlerListTable()->getFilterEditor($this->getRequest()); |
|
| 35 | + $this->view->filterEditor=$this->getHandlerListTable()->getFilterEditor($this->getRequest()); |
|
| 36 | 36 | |
| 37 | 37 | //$this->displayExitError('Handler/indexAction','Not implemented'); |
| 38 | 38 | } |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | public function testruleAction() |
| 43 | 43 | { |
| 44 | 44 | $this->checkReadPermission(); |
| 45 | - $this->getTabs()->add('get',array( |
|
| 45 | + $this->getTabs()->add('get', array( |
|
| 46 | 46 | 'active' => true, |
| 47 | 47 | 'label' => $this->translate('Test Rule'), |
| 48 | 48 | 'url' => Url::fromRequest() |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | |
| 52 | 52 | if ($this->params->get('rule') !== null) |
| 53 | 53 | { |
| 54 | - $this->view->rule= $this->params->get('rule'); |
|
| 54 | + $this->view->rule=$this->params->get('rule'); |
|
| 55 | 55 | } |
| 56 | 56 | else |
| 57 | 57 | { |
@@ -110,12 +110,12 @@ discard block |
||
| 110 | 110 | } |
| 111 | 111 | catch (Exception $e) |
| 112 | 112 | { |
| 113 | - $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
|
| 113 | + $this->displayExitError('Add handler : get host by IP/Name ', $e->getMessage()); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | |
| 117 | 117 | // if one unique host found -> put id text input |
| 118 | - if (count($hosts)==1) { |
|
| 118 | + if (count($hosts) == 1) { |
|
| 119 | 119 | $this->view->hostname=$hosts[0]->name; |
| 120 | 120 | //$hostid=$hosts[0]->id; |
| 121 | 121 | // Tell JS to get services when page is loaded |
@@ -124,9 +124,9 @@ discard block |
||
| 124 | 124 | } |
| 125 | 125 | else |
| 126 | 126 | { |
| 127 | - foreach($hosts as $key=>$val) |
|
| 127 | + foreach ($hosts as $key=>$val) |
|
| 128 | 128 | { |
| 129 | - array_push($this->view->hostlist,$hosts[$key]->name); |
|
| 129 | + array_push($this->view->hostlist, $hosts[$key]->name); |
|
| 130 | 130 | } |
| 131 | 131 | } |
| 132 | 132 | |
@@ -165,18 +165,18 @@ discard block |
||
| 165 | 165 | $currentObjectTypeEnum |
| 166 | 166 | ); |
| 167 | 167 | $oid_index++; |
| 168 | - array_push($this->view->objectList,$currentObject); |
|
| 168 | + array_push($this->view->objectList, $currentObject); |
|
| 169 | 169 | // set currrent object to null in allObjects |
| 170 | 170 | if (isset($allObjects[$val->oid])) |
| 171 | 171 | { |
| 172 | 172 | $allObjects[$val->oid]=null; |
| 173 | 173 | } |
| 174 | 174 | } |
| 175 | - if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
|
| 175 | + if ($allObjects != null) // in case trap doesn't have objects or is not resolved |
|
| 176 | 176 | { |
| 177 | 177 | foreach ($allObjects as $key => $val) |
| 178 | 178 | { |
| 179 | - if ($val==null) { continue; } |
|
| 179 | + if ($val == null) { continue; } |
|
| 180 | 180 | array_push($this->view->objectList, array( |
| 181 | 181 | $oid_index, |
| 182 | 182 | $key, |
@@ -204,9 +204,9 @@ discard block |
||
| 204 | 204 | // Check if hostname still exists |
| 205 | 205 | $host_get=$this->getUIDatabase()->getHostByName($this->view->hostname); |
| 206 | 206 | |
| 207 | - if (count($host_get)==0) |
|
| 207 | + if (count($host_get) == 0) |
|
| 208 | 208 | { |
| 209 | - $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
|
| 209 | + $this->view->warning_message='Host '.$this->view->hostname.' doesn\'t exists anymore'; |
|
| 210 | 210 | $this->view->serviceGet=false; |
| 211 | 211 | } |
| 212 | 212 | else |
@@ -214,10 +214,10 @@ discard block |
||
| 214 | 214 | // Tell JS to get services when page is loaded |
| 215 | 215 | $this->view->serviceGet=true; |
| 216 | 216 | // get service id for form to set : |
| 217 | - $serviceID=$this->getUIDatabase()->getServiceIDByName($this->view->hostname,$ruleDetail->service_name); |
|
| 218 | - if (count($serviceID) ==0) |
|
| 217 | + $serviceID=$this->getUIDatabase()->getServiceIDByName($this->view->hostname, $ruleDetail->service_name); |
|
| 218 | + if (count($serviceID) == 0) |
|
| 219 | 219 | { |
| 220 | - $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
| 220 | + $this->view->warning_message=' Service '.$ruleDetail->service_name.' doesn\'t exists anymore'; |
|
| 221 | 221 | } |
| 222 | 222 | else |
| 223 | 223 | { |
@@ -234,9 +234,9 @@ discard block |
||
| 234 | 234 | { |
| 235 | 235 | // Check if groupe exists |
| 236 | 236 | $group_get=$this->getUIDatabase()->getHostGroupByName($this->view->hostgroupname); |
| 237 | - if (count($group_get)==0) |
|
| 237 | + if (count($group_get) == 0) |
|
| 238 | 238 | { |
| 239 | - $this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore'; |
|
| 239 | + $this->view->warning_message='HostGroup '.$this->view->hostgroupname.' doesn\'t exists anymore'; |
|
| 240 | 240 | $this->view->serviceGroupGet=false; |
| 241 | 241 | } |
| 242 | 242 | else |
@@ -254,9 +254,9 @@ discard block |
||
| 254 | 254 | |
| 255 | 255 | // Tell JS to get services when page is loaded |
| 256 | 256 | $this->view->serviceGroupGet=true; |
| 257 | - if ($foundGrpService==0) |
|
| 257 | + if ($foundGrpService == 0) |
|
| 258 | 258 | { |
| 259 | - $this->view->warning_message.=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
| 259 | + $this->view->warning_message.=' Service '.$ruleDetail->service_name.' doesn\'t exists anymore'; |
|
| 260 | 260 | } |
| 261 | 261 | } |
| 262 | 262 | } |
@@ -274,12 +274,12 @@ discard block |
||
| 274 | 274 | $index=1; |
| 275 | 275 | // check in display & rule for : OID(<oid>) |
| 276 | 276 | $matches=array(); |
| 277 | - while ( preg_match('/_OID\(([\.0-9\*]+)\)/',$display,$matches) || |
|
| 278 | - preg_match('/_OID\(([\.0-9\*]+)\)/',$rule,$matches)) |
|
| 277 | + while (preg_match('/_OID\(([\.0-9\*]+)\)/', $display, $matches) || |
|
| 278 | + preg_match('/_OID\(([\.0-9\*]+)\)/', $rule, $matches)) |
|
| 279 | 279 | { |
| 280 | 280 | $curOid=$matches[1]; |
| 281 | 281 | |
| 282 | - if ( (preg_match('/\*/',$curOid) == 0 ) |
|
| 282 | + if ((preg_match('/\*/', $curOid) == 0) |
|
| 283 | 283 | && ($object=$this->getMIB()->translateOID($curOid)) != null) |
| 284 | 284 | { |
| 285 | 285 | array_push($curObjectList, array( |
@@ -304,9 +304,9 @@ discard block |
||
| 304 | 304 | 'not found' |
| 305 | 305 | )); |
| 306 | 306 | } |
| 307 | - $curOid = preg_replace('/\*/','\*',$curOid); |
|
| 308 | - $display=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$display); |
|
| 309 | - $rule=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$rule); |
|
| 307 | + $curOid=preg_replace('/\*/', '\*', $curOid); |
|
| 308 | + $display=preg_replace('/_OID\('.$curOid.'\)/', '\$'.$index.'\$', $display); |
|
| 309 | + $rule=preg_replace('/_OID\('.$curOid.'\)/', '\$'.$index.'\$', $rule); |
|
| 310 | 310 | $index++; |
| 311 | 311 | } |
| 312 | 312 | return $curObjectList; |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | { |
| 321 | 321 | $this->checkConfigPermission(); |
| 322 | 322 | // set up tab |
| 323 | - $this->getTabs()->add('get',array( |
|
| 323 | + $this->getTabs()->add('get', array( |
|
| 324 | 324 | 'active' => true, |
| 325 | 325 | 'label' => $this->translate('Add handler'), |
| 326 | 326 | 'url' => Url::fromRequest() |
@@ -333,7 +333,7 @@ discard block |
||
| 333 | 333 | |
| 334 | 334 | //$this->view->trapvalues=false; // Set to true to display 'value' colum in objects |
| 335 | 335 | |
| 336 | - if (($trapid = $this->params->get('fromid')) !== null) { |
|
| 336 | + if (($trapid=$this->params->get('fromid')) !== null) { |
|
| 337 | 337 | /********** Setup from existing trap ***************/ |
| 338 | 338 | $this->add_from_existing($trapid); |
| 339 | 339 | return; |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | $this->view->setRuleMatch=$ruleDetail->action_match; |
| 354 | 354 | $this->view->setRuleNoMatch=$ruleDetail->action_nomatch; |
| 355 | 355 | $this->view->hostgroupname=$ruleDetail->host_group_name; |
| 356 | - $this->view->modified=gmdate("Y-m-d\TH:i:s\Z",$ruleDetail->modified); |
|
| 356 | + $this->view->modified=gmdate("Y-m-d\TH:i:s\Z", $ruleDetail->modified); |
|
| 357 | 357 | $this->view->modifier=$ruleDetail->modifier; |
| 358 | 358 | |
| 359 | 359 | // Warning message if host/service don't exists anymore |
@@ -405,26 +405,26 @@ discard block |
||
| 405 | 405 | |
| 406 | 406 | $params=array( |
| 407 | 407 | // id (also db) => array('post' => post id, 'val' => default val, 'db' => send to table) |
| 408 | - 'hostgroup' => array('post' => 'hostgroup','db'=>false), |
|
| 409 | - 'db_rule' => array('post' => 'db_rule','db'=>false), |
|
| 410 | - 'hostid' => array('post' => 'hostid','db'=>false), |
|
| 411 | - 'host_name' => array('post' => 'hostname','val' => null,'db'=>true), |
|
| 412 | - 'host_group_name'=> array('post' => null,'val' => null,'db'=>true), |
|
| 413 | - 'serviceid' => array('post' => 'serviceid','db'=>false), |
|
| 414 | - 'service_name' => array('post' => 'serviceName','db'=>true), |
|
| 415 | - 'trap_oid' => array('post' => 'oid','db'=>true), |
|
| 416 | - 'revert_ok' => array('post' => 'revertOK','val' => 0,'db'=>true), |
|
| 417 | - 'display' => array('post' => 'display','val' => '','db'=>true), |
|
| 418 | - 'rule' => array('post' => 'rule','val' => '','db'=>true), |
|
| 419 | - 'action_match' => array('post' => 'ruleMatch','val' => -1,'db'=>true), |
|
| 420 | - 'action_nomatch'=> array('post' => 'ruleNoMatch','val' => -1,'db'=>true), |
|
| 421 | - 'ip4' => array('post' => null,'val' => null,'db'=>true), |
|
| 422 | - 'ip6' => array('post' => null,'val' => null,'db'=>true), |
|
| 423 | - 'action_form' => array('post' => 'action_form','db'=>false) |
|
| 408 | + 'hostgroup' => array('post' => 'hostgroup', 'db'=>false), |
|
| 409 | + 'db_rule' => array('post' => 'db_rule', 'db'=>false), |
|
| 410 | + 'hostid' => array('post' => 'hostid', 'db'=>false), |
|
| 411 | + 'host_name' => array('post' => 'hostname', 'val' => null, 'db'=>true), |
|
| 412 | + 'host_group_name'=> array('post' => null, 'val' => null, 'db'=>true), |
|
| 413 | + 'serviceid' => array('post' => 'serviceid', 'db'=>false), |
|
| 414 | + 'service_name' => array('post' => 'serviceName', 'db'=>true), |
|
| 415 | + 'trap_oid' => array('post' => 'oid', 'db'=>true), |
|
| 416 | + 'revert_ok' => array('post' => 'revertOK', 'val' => 0, 'db'=>true), |
|
| 417 | + 'display' => array('post' => 'display', 'val' => '', 'db'=>true), |
|
| 418 | + 'rule' => array('post' => 'rule', 'val' => '', 'db'=>true), |
|
| 419 | + 'action_match' => array('post' => 'ruleMatch', 'val' => -1, 'db'=>true), |
|
| 420 | + 'action_nomatch'=> array('post' => 'ruleNoMatch', 'val' => -1, 'db'=>true), |
|
| 421 | + 'ip4' => array('post' => null, 'val' => null, 'db'=>true), |
|
| 422 | + 'ip6' => array('post' => null, 'val' => null, 'db'=>true), |
|
| 423 | + 'action_form' => array('post' => 'action_form', 'db'=>false) |
|
| 424 | 424 | ); |
| 425 | 425 | |
| 426 | 426 | if (isset($postData[$params['action_form']['post']]) |
| 427 | - && $postData[$params['action_form']['post']] == 'delete' ) |
|
| 427 | + && $postData[$params['action_form']['post']] == 'delete') |
|
| 428 | 428 | { |
| 429 | 429 | try |
| 430 | 430 | { |
@@ -444,16 +444,16 @@ discard block |
||
| 444 | 444 | } |
| 445 | 445 | foreach (array_keys($params) as $key) |
| 446 | 446 | { |
| 447 | - if ($params[$key]['post']==null) continue; // data not sent in post vars |
|
| 448 | - if (! isset($postData[$params[$key]['post']])) |
|
| 447 | + if ($params[$key]['post'] == null) continue; // data not sent in post vars |
|
| 448 | + if (!isset($postData[$params[$key]['post']])) |
|
| 449 | 449 | { |
| 450 | 450 | // should not happen as the js checks data |
| 451 | - $this->_helper->json(array('status'=>'No ' . $key)); |
|
| 451 | + $this->_helper->json(array('status'=>'No '.$key)); |
|
| 452 | 452 | } |
| 453 | 453 | else |
| 454 | 454 | { |
| 455 | 455 | $data=$postData[$params[$key]['post']]; |
| 456 | - if ($data!=null && $data !="") |
|
| 456 | + if ($data != null && $data != "") |
|
| 457 | 457 | { |
| 458 | 458 | $params[$key]['val']=$postData[$params[$key]['post']]; |
| 459 | 459 | } |
@@ -462,8 +462,8 @@ discard block |
||
| 462 | 462 | |
| 463 | 463 | try |
| 464 | 464 | { |
| 465 | - $isHostGroup=($params['hostgroup']['val'] == 1)?true:false; |
|
| 466 | - if (! $isHostGroup ) |
|
| 465 | + $isHostGroup=($params['hostgroup']['val'] == 1) ?true:false; |
|
| 466 | + if (!$isHostGroup) |
|
| 467 | 467 | { // checks if selection by host |
| 468 | 468 | $hostAddr=$this->getUIDatabase()->getHostInfoByID($params['hostid']['val']); |
| 469 | 469 | $params['ip4']['val']=$hostAddr->ip4; |
@@ -476,7 +476,7 @@ discard block |
||
| 476 | 476 | } |
| 477 | 477 | if (!is_numeric($params['serviceid']['val'])) |
| 478 | 478 | { |
| 479 | - $this->_helper->json(array('status'=>"Invalid service id ". $params['serviceid']['val'])); |
|
| 479 | + $this->_helper->json(array('status'=>"Invalid service id ".$params['serviceid']['val'])); |
|
| 480 | 480 | return; |
| 481 | 481 | } |
| 482 | 482 | $serviceName=$this->getUIDatabase()->getObjectNameByid($params['serviceid']['val']); |
@@ -495,25 +495,25 @@ discard block |
||
| 495 | 495 | return; |
| 496 | 496 | } |
| 497 | 497 | // Put param in correct column (group_name) |
| 498 | - $params['host_group_name']['val'] = $params['host_name']['val']; |
|
| 498 | + $params['host_group_name']['val']=$params['host_name']['val']; |
|
| 499 | 499 | $params['host_name']['val']=null; |
| 500 | 500 | } |
| 501 | 501 | $dbparams=array(); |
| 502 | 502 | foreach ($params as $key=>$val) |
| 503 | 503 | { |
| 504 | - if ($val['db']==true ) |
|
| 504 | + if ($val['db'] == true) |
|
| 505 | 505 | { |
| 506 | - $dbparams[$key] = $val['val']; |
|
| 506 | + $dbparams[$key]=$val['val']; |
|
| 507 | 507 | } |
| 508 | 508 | } |
| 509 | 509 | // echo '<br>'; print_r($dbparams);echo '<br>'; |
| 510 | - if ($params['db_rule']['val'] == -1 ) |
|
| 510 | + if ($params['db_rule']['val'] == -1) |
|
| 511 | 511 | { |
| 512 | 512 | $ruleID=$this->getUIDatabase()->addHandlerRule($dbparams); |
| 513 | 513 | } |
| 514 | 514 | else |
| 515 | 515 | { |
| 516 | - $this->getUIDatabase()->updateHandlerRule($dbparams,$params['db_rule']['val']); |
|
| 516 | + $this->getUIDatabase()->updateHandlerRule($dbparams, $params['db_rule']['val']); |
|
| 517 | 517 | $ruleID=$params['db_rule']['val']; |
| 518 | 518 | } |
| 519 | 519 | } |
@@ -532,10 +532,10 @@ discard block |
||
| 532 | 532 | */ |
| 533 | 533 | protected function getTrapDetail($trapid) |
| 534 | 534 | { |
| 535 | - if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
|
| 535 | + if (!preg_match('/^[0-9]+$/', $trapid)) { throw new Exception('Invalid id'); } |
|
| 536 | 536 | $queryArray=$this->getModuleConfig()->trapDetailQuery(); |
| 537 | 537 | |
| 538 | - $dbConn = $this->getUIDatabase()->getDbConn(); |
|
| 538 | + $dbConn=$this->getUIDatabase()->getDbConn(); |
|
| 539 | 539 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 540 | 540 | // *************** Get main data |
| 541 | 541 | // extract columns and titles; |
@@ -545,19 +545,19 @@ discard block |
||
| 545 | 545 | } |
| 546 | 546 | try |
| 547 | 547 | { |
| 548 | - $query = $dbConn->select() |
|
| 549 | - ->from($this->getModuleConfig()->getTrapTableName(),$elmts) |
|
| 550 | - ->where('id=?',$trapid); |
|
| 548 | + $query=$dbConn->select() |
|
| 549 | + ->from($this->getModuleConfig()->getTrapTableName(), $elmts) |
|
| 550 | + ->where('id=?', $trapid); |
|
| 551 | 551 | $trapDetail=$dbConn->fetchRow($query); |
| 552 | - if ( $trapDetail == null ) |
|
| 552 | + if ($trapDetail == null) |
|
| 553 | 553 | { |
| 554 | - $trapDetail = 'NULL'; |
|
| 554 | + $trapDetail='NULL'; |
|
| 555 | 555 | throw new Exception('No traps was found with id = '.$trapid); |
| 556 | 556 | } |
| 557 | 557 | } |
| 558 | 558 | catch (Exception $e) |
| 559 | 559 | { |
| 560 | - $this->displayExitError('Add handler : get trap detail returning : '.print_r($trapDetail,true),$e->getMessage()); |
|
| 560 | + $this->displayExitError('Add handler : get trap detail returning : '.print_r($trapDetail, true), $e->getMessage()); |
|
| 561 | 561 | return; |
| 562 | 562 | } |
| 563 | 563 | |
@@ -571,10 +571,10 @@ discard block |
||
| 571 | 571 | */ |
| 572 | 572 | protected function getTrapobjects($trapid) |
| 573 | 573 | { |
| 574 | - if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
|
| 574 | + if (!preg_match('/^[0-9]+$/', $trapid)) { throw new Exception('Invalid id'); } |
|
| 575 | 575 | $queryArrayData=$this->getModuleConfig()->trapDataDetailQuery(); |
| 576 | 576 | |
| 577 | - $dbConn = $this->getUIDatabase()->getDbConn(); |
|
| 577 | + $dbConn=$this->getUIDatabase()->getDbConn(); |
|
| 578 | 578 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 579 | 579 | // *************** Get object data |
| 580 | 580 | // extract columns and titles; |
@@ -584,15 +584,15 @@ discard block |
||
| 584 | 584 | } |
| 585 | 585 | try |
| 586 | 586 | { |
| 587 | - $query = $dbConn->select() |
|
| 588 | - ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
|
| 589 | - ->where('trap_id=?',$trapid); |
|
| 587 | + $query=$dbConn->select() |
|
| 588 | + ->from($this->moduleConfig->getTrapDataTableName(), $data_elmts) |
|
| 589 | + ->where('trap_id=?', $trapid); |
|
| 590 | 590 | $trapDetail=$dbConn->fetchAll($query); |
| 591 | 591 | // if ( $trapDetail == null ) throw new Exception('No traps was found with id = '.$trapid); |
| 592 | 592 | } |
| 593 | 593 | catch (Exception $e) |
| 594 | 594 | { |
| 595 | - $this->displayExitError('Add handler : get trap data detail : ',$e->getMessage()); |
|
| 595 | + $this->displayExitError('Add handler : get trap data detail : ', $e->getMessage()); |
|
| 596 | 596 | return array(); |
| 597 | 597 | } |
| 598 | 598 | |
@@ -606,24 +606,24 @@ discard block |
||
| 606 | 606 | */ |
| 607 | 607 | protected function getRuleDetail($ruleid) |
| 608 | 608 | { |
| 609 | - if (!preg_match('/^[0-9]+$/',$ruleid)) { throw new Exception('Invalid id'); } |
|
| 609 | + if (!preg_match('/^[0-9]+$/', $ruleid)) { throw new Exception('Invalid id'); } |
|
| 610 | 610 | $queryArray=$this->getModuleConfig()->ruleDetailQuery(); |
| 611 | 611 | |
| 612 | - $dbConn = $this->getUIDatabase()->getDbConn(); |
|
| 612 | + $dbConn=$this->getUIDatabase()->getDbConn(); |
|
| 613 | 613 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
| 614 | 614 | // *************** Get main data |
| 615 | 615 | try |
| 616 | 616 | { |
| 617 | - $query = $dbConn->select() |
|
| 618 | - ->from($this->getModuleConfig()->getTrapRuleName(),$queryArray) |
|
| 619 | - ->where('id=?',$ruleid); |
|
| 617 | + $query=$dbConn->select() |
|
| 618 | + ->from($this->getModuleConfig()->getTrapRuleName(), $queryArray) |
|
| 619 | + ->where('id=?', $ruleid); |
|
| 620 | 620 | $ruleDetail=$dbConn->fetchRow($query); |
| 621 | - if ( $ruleDetail == null ) throw new Exception('No rule was found with id = '.$ruleid); |
|
| 621 | + if ($ruleDetail == null) throw new Exception('No rule was found with id = '.$ruleid); |
|
| 622 | 622 | } |
| 623 | 623 | catch (Exception $e) |
| 624 | 624 | { |
| 625 | - $this->displayExitError('Update handler : get rule detail',$e->getMessage()); |
|
| 626 | - throw new Exception('Error : ' . $e->getMessage()); |
|
| 625 | + $this->displayExitError('Update handler : get rule detail', $e->getMessage()); |
|
| 626 | + throw new Exception('Error : '.$e->getMessage()); |
|
| 627 | 627 | } |
| 628 | 628 | |
| 629 | 629 | return $ruleDetail; |
@@ -636,7 +636,7 @@ discard block |
||
| 636 | 636 | { |
| 637 | 637 | return $this->getTabs()->add('status', array( |
| 638 | 638 | 'label' => $this->translate('Traps'), |
| 639 | - 'url' => $this->getModuleConfig()->urlPath() . '/handler') |
|
| 639 | + 'url' => $this->getModuleConfig()->urlPath().'/handler') |
|
| 640 | 640 | ); |
| 641 | 641 | } |
| 642 | 642 | |
@@ -24,7 +24,9 @@ discard block |
||
| 24 | 24 | $this->prepareTabs()->activate('status'); |
| 25 | 25 | |
| 26 | 26 | $dbConn = $this->getUIDatabase()->getDb(); |
| 27 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 27 | + if ($dbConn === null) { |
|
| 28 | + throw new \ErrorException('uncatched db error'); |
|
| 29 | + } |
|
| 28 | 30 | |
| 29 | 31 | $this->getHandlerListTable()->setConnection($dbConn); |
| 30 | 32 | $this->getHandlerListTable()->setMibloader($this->getMIB()); |
@@ -52,8 +54,7 @@ discard block |
||
| 52 | 54 | if ($this->params->get('rule') !== null) |
| 53 | 55 | { |
| 54 | 56 | $this->view->rule= $this->params->get('rule'); |
| 55 | - } |
|
| 56 | - else |
|
| 57 | + } else |
|
| 57 | 58 | { |
| 58 | 59 | $this->view->rule=''; |
| 59 | 60 | } |
@@ -107,8 +108,7 @@ discard block |
||
| 107 | 108 | try |
| 108 | 109 | { |
| 109 | 110 | $hosts=$this->getUIDatabase()->getHostByIP($hostfilter); |
| 110 | - } |
|
| 111 | - catch (Exception $e) |
|
| 111 | + } catch (Exception $e) |
|
| 112 | 112 | { |
| 113 | 113 | $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
| 114 | 114 | } |
@@ -121,8 +121,7 @@ discard block |
||
| 121 | 121 | // Tell JS to get services when page is loaded |
| 122 | 122 | $this->view->serviceGet=true; |
| 123 | 123 | |
| 124 | - } |
|
| 125 | - else |
|
| 124 | + } else |
|
| 126 | 125 | { |
| 127 | 126 | foreach($hosts as $key=>$val) |
| 128 | 127 | { |
@@ -172,11 +171,14 @@ discard block |
||
| 172 | 171 | $allObjects[$val->oid]=null; |
| 173 | 172 | } |
| 174 | 173 | } |
| 175 | - if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
|
| 174 | + if ($allObjects!=null) { |
|
| 175 | + // in case trap doesn't have objects or is not resolved |
|
| 176 | 176 | { |
| 177 | 177 | foreach ($allObjects as $key => $val) |
| 178 | 178 | { |
| 179 | - if ($val==null) { continue; } |
|
| 179 | + if ($val==null) { continue; |
|
| 180 | + } |
|
| 181 | + } |
|
| 180 | 182 | array_push($this->view->objectList, array( |
| 181 | 183 | $oid_index, |
| 182 | 184 | $key, |
@@ -208,8 +210,7 @@ discard block |
||
| 208 | 210 | { |
| 209 | 211 | $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
| 210 | 212 | $this->view->serviceGet=false; |
| 211 | - } |
|
| 212 | - else |
|
| 213 | + } else |
|
| 213 | 214 | { |
| 214 | 215 | // Tell JS to get services when page is loaded |
| 215 | 216 | $this->view->serviceGet=true; |
@@ -218,8 +219,7 @@ discard block |
||
| 218 | 219 | if (count($serviceID) ==0) |
| 219 | 220 | { |
| 220 | 221 | $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
| 221 | - } |
|
| 222 | - else |
|
| 222 | + } else |
|
| 223 | 223 | { |
| 224 | 224 | $this->view->serviceSet=$serviceID[0]->id; |
| 225 | 225 | } |
@@ -238,8 +238,7 @@ discard block |
||
| 238 | 238 | { |
| 239 | 239 | $this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore'; |
| 240 | 240 | $this->view->serviceGroupGet=false; |
| 241 | - } |
|
| 242 | - else |
|
| 241 | + } else |
|
| 243 | 242 | { |
| 244 | 243 | $grpServices=$this->getUIDatabase()->getServicesByHostGroupid($group_get[0]->id); |
| 245 | 244 | $foundGrpService=0; |
@@ -291,8 +290,7 @@ discard block |
||
| 291 | 290 | $object['type'], |
| 292 | 291 | $object['type_enum'] |
| 293 | 292 | )); |
| 294 | - } |
|
| 295 | - else |
|
| 293 | + } else |
|
| 296 | 294 | { |
| 297 | 295 | array_push($curObjectList, array( |
| 298 | 296 | $index, |
@@ -363,8 +361,7 @@ discard block |
||
| 363 | 361 | $this->view->selectGroup=false; |
| 364 | 362 | // Check if hostname still exists |
| 365 | 363 | $this->add_check_host_exists($ruleDetail); |
| 366 | - } |
|
| 367 | - else |
|
| 364 | + } else |
|
| 368 | 365 | { |
| 369 | 366 | $this->view->selectGroup=true; |
| 370 | 367 | $this->add_check_hostgroup_exists($ruleDetail); // Check if groupe exists |
@@ -372,9 +369,11 @@ discard block |
||
| 372 | 369 | |
| 373 | 370 | $this->view->mainoid=$ruleDetail->trap_oid; |
| 374 | 371 | $oidName=$this->getMIB()->translateOID($ruleDetail->trap_oid); |
| 375 | - if ($oidName != null) // oid is found in mibs |
|
| 372 | + if ($oidName != null) { |
|
| 373 | + // oid is found in mibs |
|
| 376 | 374 | { |
| 377 | - $this->view->mib=$oidName['mib']; |
|
| 375 | + $this->view->mib=$oidName['mib']; |
|
| 376 | + } |
|
| 378 | 377 | $this->view->name=$oidName['name']; |
| 379 | 378 | $this->view->trapListForMIB=$this->getMIB() |
| 380 | 379 | ->getTrapList($oidName['mib']); |
@@ -429,8 +428,7 @@ discard block |
||
| 429 | 428 | try |
| 430 | 429 | { |
| 431 | 430 | $this->getUIDatabase()->deleteRule($postData[$params['db_rule']['post']]); |
| 432 | - } |
|
| 433 | - catch (Exception $e) |
|
| 431 | + } catch (Exception $e) |
|
| 434 | 432 | { |
| 435 | 433 | $this->_helper->json(array('status'=>$e->getMessage())); |
| 436 | 434 | return; |
@@ -444,13 +442,15 @@ discard block |
||
| 444 | 442 | } |
| 445 | 443 | foreach (array_keys($params) as $key) |
| 446 | 444 | { |
| 447 | - if ($params[$key]['post']==null) continue; // data not sent in post vars |
|
| 445 | + if ($params[$key]['post']==null) { |
|
| 446 | + continue; |
|
| 447 | + } |
|
| 448 | + // data not sent in post vars |
|
| 448 | 449 | if (! isset($postData[$params[$key]['post']])) |
| 449 | 450 | { |
| 450 | 451 | // should not happen as the js checks data |
| 451 | 452 | $this->_helper->json(array('status'=>'No ' . $key)); |
| 452 | - } |
|
| 453 | - else |
|
| 453 | + } else |
|
| 454 | 454 | { |
| 455 | 455 | $data=$postData[$params[$key]['post']]; |
| 456 | 456 | if ($data!=null && $data !="") |
@@ -485,8 +485,7 @@ discard block |
||
| 485 | 485 | $this->_helper->json(array('status'=>"Invalid service id : Please re enter service")); |
| 486 | 486 | return; |
| 487 | 487 | } |
| 488 | - } |
|
| 489 | - else |
|
| 488 | + } else |
|
| 490 | 489 | { |
| 491 | 490 | $object=$this->getUIDatabase()->getObjectNameByid($params['hostid']['val']); |
| 492 | 491 | if ($params['host_name']['val'] != $object->name1) |
@@ -510,14 +509,12 @@ discard block |
||
| 510 | 509 | if ($params['db_rule']['val'] == -1 ) |
| 511 | 510 | { |
| 512 | 511 | $ruleID=$this->getUIDatabase()->addHandlerRule($dbparams); |
| 513 | - } |
|
| 514 | - else |
|
| 512 | + } else |
|
| 515 | 513 | { |
| 516 | 514 | $this->getUIDatabase()->updateHandlerRule($dbparams,$params['db_rule']['val']); |
| 517 | 515 | $ruleID=$params['db_rule']['val']; |
| 518 | 516 | } |
| 519 | - } |
|
| 520 | - catch (Exception $e) |
|
| 517 | + } catch (Exception $e) |
|
| 521 | 518 | { |
| 522 | 519 | $this->_helper->json(array('status'=>$e->getMessage())); |
| 523 | 520 | return; |
@@ -536,7 +533,9 @@ discard block |
||
| 536 | 533 | $queryArray=$this->getModuleConfig()->trapDetailQuery(); |
| 537 | 534 | |
| 538 | 535 | $dbConn = $this->getUIDatabase()->getDbConn(); |
| 539 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 536 | + if ($dbConn === null) { |
|
| 537 | + throw new \ErrorException('uncatched db error'); |
|
| 538 | + } |
|
| 540 | 539 | // *************** Get main data |
| 541 | 540 | // extract columns and titles; |
| 542 | 541 | $elmts=NULL; |
@@ -554,8 +553,7 @@ discard block |
||
| 554 | 553 | $trapDetail = 'NULL'; |
| 555 | 554 | throw new Exception('No traps was found with id = '.$trapid); |
| 556 | 555 | } |
| 557 | - } |
|
| 558 | - catch (Exception $e) |
|
| 556 | + } catch (Exception $e) |
|
| 559 | 557 | { |
| 560 | 558 | $this->displayExitError('Add handler : get trap detail returning : '.print_r($trapDetail,true),$e->getMessage()); |
| 561 | 559 | return; |
@@ -575,7 +573,9 @@ discard block |
||
| 575 | 573 | $queryArrayData=$this->getModuleConfig()->trapDataDetailQuery(); |
| 576 | 574 | |
| 577 | 575 | $dbConn = $this->getUIDatabase()->getDbConn(); |
| 578 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 576 | + if ($dbConn === null) { |
|
| 577 | + throw new \ErrorException('uncatched db error'); |
|
| 578 | + } |
|
| 579 | 579 | // *************** Get object data |
| 580 | 580 | // extract columns and titles; |
| 581 | 581 | $data_elmts=NULL; |
@@ -589,8 +589,7 @@ discard block |
||
| 589 | 589 | ->where('trap_id=?',$trapid); |
| 590 | 590 | $trapDetail=$dbConn->fetchAll($query); |
| 591 | 591 | // if ( $trapDetail == null ) throw new Exception('No traps was found with id = '.$trapid); |
| 592 | - } |
|
| 593 | - catch (Exception $e) |
|
| 592 | + } catch (Exception $e) |
|
| 594 | 593 | { |
| 595 | 594 | $this->displayExitError('Add handler : get trap data detail : ',$e->getMessage()); |
| 596 | 595 | return array(); |
@@ -610,7 +609,9 @@ discard block |
||
| 610 | 609 | $queryArray=$this->getModuleConfig()->ruleDetailQuery(); |
| 611 | 610 | |
| 612 | 611 | $dbConn = $this->getUIDatabase()->getDbConn(); |
| 613 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
| 612 | + if ($dbConn === null) { |
|
| 613 | + throw new \ErrorException('uncatched db error'); |
|
| 614 | + } |
|
| 614 | 615 | // *************** Get main data |
| 615 | 616 | try |
| 616 | 617 | { |
@@ -618,9 +619,10 @@ discard block |
||
| 618 | 619 | ->from($this->getModuleConfig()->getTrapRuleName(),$queryArray) |
| 619 | 620 | ->where('id=?',$ruleid); |
| 620 | 621 | $ruleDetail=$dbConn->fetchRow($query); |
| 621 | - if ( $ruleDetail == null ) throw new Exception('No rule was found with id = '.$ruleid); |
|
| 622 | - } |
|
| 623 | - catch (Exception $e) |
|
| 622 | + if ( $ruleDetail == null ) { |
|
| 623 | + throw new Exception('No rule was found with id = '.$ruleid); |
|
| 624 | + } |
|
| 625 | + } catch (Exception $e) |
|
| 624 | 626 | { |
| 625 | 627 | $this->displayExitError('Update handler : get rule detail',$e->getMessage()); |
| 626 | 628 | throw new Exception('Error : ' . $e->getMessage()); |
@@ -20,7 +20,7 @@ |
||
| 20 | 20 | $inputStream=fopen('php://stdin', 'r'); |
| 21 | 21 | if ($inputStream === false) |
| 22 | 22 | { |
| 23 | - exit(1); |
|
| 23 | + exit(1); |
|
| 24 | 24 | } |
| 25 | 25 | $rule=chop(fgets($inputStream)); |
| 26 | 26 | } else |
@@ -5,18 +5,18 @@ discard block |
||
| 5 | 5 | |
| 6 | 6 | require_once 'bin/trap_class.php'; |
| 7 | 7 | |
| 8 | -$options = getopt("r:d:"); |
|
| 8 | +$options=getopt("r:d:"); |
|
| 9 | 9 | |
| 10 | -$icingaweb2Etc=(array_key_exists('d',$options))?$options['d']:"/etc/icingaweb2"; |
|
| 10 | +$icingaweb2Etc=(array_key_exists('d', $options)) ? $options['d'] : "/etc/icingaweb2"; |
|
| 11 | 11 | |
| 12 | -$debugLevel=4;// 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
| 12 | +$debugLevel=4; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
| 13 | 13 | |
| 14 | -$trap = new trap($icingaweb2Etc); |
|
| 15 | -$trap->setLogging($debugLevel,'display'); |
|
| 14 | +$trap=new trap($icingaweb2Etc); |
|
| 15 | +$trap->setLogging($debugLevel, 'display'); |
|
| 16 | 16 | |
| 17 | -$input=array_key_exists('r',$options); |
|
| 17 | +$input=array_key_exists('r', $options); |
|
| 18 | 18 | |
| 19 | -if (! $input) { |
|
| 19 | +if (!$input) { |
|
| 20 | 20 | $inputStream=fopen('php://stdin', 'r'); |
| 21 | 21 | if ($inputStream === false) |
| 22 | 22 | { |
@@ -31,10 +31,10 @@ discard block |
||
| 31 | 31 | $rule=$trap->ruleClass->eval_cleanup($rule); |
| 32 | 32 | //echo 'After cleanup : #'.$rule."#\n"; |
| 33 | 33 | $item=0; |
| 34 | - $val = $trap->ruleClass->evaluation($rule,$item); |
|
| 35 | - if ($val==true) { printf( "true"); } else { printf( "false");} |
|
| 34 | + $val=$trap->ruleClass->evaluation($rule, $item); |
|
| 35 | + if ($val == true) { printf("true"); } else { printf("false"); } |
|
| 36 | 36 | printf("\n"); |
| 37 | 37 | } |
| 38 | -catch (Exception $e) { printf("%s\n",$e->getMessage()); exit(1);} |
|
| 38 | +catch (Exception $e) { printf("%s\n", $e->getMessage()); exit(1); } |
|
| 39 | 39 | |
| 40 | 40 | exit(0); |
@@ -23,8 +23,9 @@ discard block |
||
| 23 | 23 | exit(1); |
| 24 | 24 | } |
| 25 | 25 | $rule=chop(fgets($inputStream)); |
| 26 | -} else |
|
| 26 | +} else { |
|
| 27 | 27 | $rule=$options['r']; |
| 28 | +} |
|
| 28 | 29 | |
| 29 | 30 | try |
| 30 | 31 | { |
@@ -34,7 +35,6 @@ discard block |
||
| 34 | 35 | $val = $trap->ruleClass->evaluation($rule,$item); |
| 35 | 36 | if ($val==true) { printf( "true"); } else { printf( "false");} |
| 36 | 37 | printf("\n"); |
| 37 | -} |
|
| 38 | -catch (Exception $e) { printf("%s\n",$e->getMessage()); exit(1);} |
|
| 38 | +} catch (Exception $e) { printf("%s\n",$e->getMessage()); exit(1);} |
|
| 39 | 39 | |
| 40 | 40 | exit(0); |
@@ -71,11 +71,13 @@ discard block |
||
| 71 | 71 | $start=$item; |
| 72 | 72 | $parenthesisCount=0; |
| 73 | 73 | while (($item < strlen($rule)) // Not end of string AND |
| 74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
| 74 | + && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) { |
|
| 75 | + // Closing ')' or embeded () |
|
| 75 | 76 | { |
| 76 | 77 | if ($rule[$item] == '"' ) |
| 77 | 78 | { // pass through string |
| 78 | 79 | $item++; |
| 80 | + } |
|
| 79 | 81 | $item=$this->eval_getNext($rule,$item,'"'); |
| 80 | 82 | continue; |
| 81 | 83 | } |
@@ -121,10 +123,12 @@ discard block |
||
| 121 | 123 | { |
| 122 | 124 | // function is : __function(param1,param2...) |
| 123 | 125 | $start=$item; |
| 124 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
| 126 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) { |
|
| 127 | + // Not end of string AND not opening '(' |
|
| 125 | 128 | { |
| 126 | 129 | $item++; |
| 127 | 130 | } |
| 131 | + } |
|
| 128 | 132 | if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
| 129 | 133 | |
| 130 | 134 | // get parameters between parenthesis |
@@ -167,12 +171,13 @@ discard block |
||
| 167 | 171 | */ |
| 168 | 172 | private function check_negate_first(string $rule,int &$item) |
| 169 | 173 | { |
| 170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 174 | + if ( $rule[$item] == '!') { |
|
| 175 | + // If '!' found, negate next expression. |
|
| 171 | 176 | { |
| 172 | 177 | $item++; |
| 173 | - return true; |
|
| 174 | 178 | } |
| 175 | - else |
|
| 179 | + return true; |
|
| 180 | + } else |
|
| 176 | 181 | { |
| 177 | 182 | return false; |
| 178 | 183 | } |
@@ -199,7 +204,9 @@ discard block |
||
| 199 | 204 | $rule2.=$rule[$item]; |
| 200 | 205 | $item++; |
| 201 | 206 | } |
| 202 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 207 | + if ($item == strlen ($rule)) { |
|
| 208 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 209 | + } |
|
| 203 | 210 | $rule2.=$rule[$item]; |
| 204 | 211 | $item++; |
| 205 | 212 | continue; |
@@ -12,204 +12,204 @@ |
||
| 12 | 12 | trait RuleUtils |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** rule eval starting from $token |
|
| 16 | - * @param string $rule |
|
| 17 | - * @param int $item |
|
| 18 | - */ |
|
| 19 | - abstract public function evaluation($rule,&$item); |
|
| 15 | + /** rule eval starting from $token |
|
| 16 | + * @param string $rule |
|
| 17 | + * @param int $item |
|
| 18 | + */ |
|
| 19 | + abstract public function evaluation($rule,&$item); |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Get full number |
|
| 23 | - * @param string $rule Rule as string |
|
| 24 | - * @param int $item current eval position |
|
| 25 | - * @return array<int,string> |
|
| 26 | - */ |
|
| 27 | - private function get_number(string $rule,int &$item) |
|
| 28 | - { |
|
| 29 | - $item2=$item+1; |
|
| 30 | - while ( |
|
| 31 | - ($item2!=strlen($rule)) |
|
| 32 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
| 33 | - { |
|
| 34 | - $item2++ ; |
|
| 35 | - } |
|
| 36 | - $val=substr($rule,$item,$item2-$item); |
|
| 37 | - $item=$item2; |
|
| 38 | - //echo "number ".$val."\n"; |
|
| 21 | + /** |
|
| 22 | + * Get full number |
|
| 23 | + * @param string $rule Rule as string |
|
| 24 | + * @param int $item current eval position |
|
| 25 | + * @return array<int,string> |
|
| 26 | + */ |
|
| 27 | + private function get_number(string $rule,int &$item) |
|
| 28 | + { |
|
| 29 | + $item2=$item+1; |
|
| 30 | + while ( |
|
| 31 | + ($item2!=strlen($rule)) |
|
| 32 | + && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
| 33 | + { |
|
| 34 | + $item2++ ; |
|
| 35 | + } |
|
| 36 | + $val=substr($rule,$item,$item2-$item); |
|
| 37 | + $item=$item2; |
|
| 38 | + //echo "number ".$val."\n"; |
|
| 39 | 39 | |
| 40 | - return array(0,$val); |
|
| 41 | - } |
|
| 40 | + return array(0,$val); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get a string (between ") |
|
| 45 | - * @param string $rule Rule as string |
|
| 46 | - * @param int $item current eval position |
|
| 47 | - * @return array<int,string> |
|
| 48 | - */ |
|
| 49 | - private function get_string(string $rule,int &$item) |
|
| 50 | - { |
|
| 51 | - $item++; |
|
| 52 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
| 53 | - $val=substr($rule,$item,$item2-$item-1); |
|
| 54 | - $item=$item2; |
|
| 55 | - //echo "string : ".$val."\n"; |
|
| 56 | - return array(1,$val); |
|
| 43 | + /** |
|
| 44 | + * Get a string (between ") |
|
| 45 | + * @param string $rule Rule as string |
|
| 46 | + * @param int $item current eval position |
|
| 47 | + * @return array<int,string> |
|
| 48 | + */ |
|
| 49 | + private function get_string(string $rule,int &$item) |
|
| 50 | + { |
|
| 51 | + $item++; |
|
| 52 | + $item2=$this->eval_getNext($rule,$item,'"'); |
|
| 53 | + $val=substr($rule,$item,$item2-$item-1); |
|
| 54 | + $item=$item2; |
|
| 55 | + //echo "string : ".$val."\n"; |
|
| 56 | + return array(1,$val); |
|
| 57 | 57 | |
| 58 | - } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Parse elements inside () : jumps over "" and count parenthesis. |
|
| 62 | - * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
| 63 | - * @param string $rule : the current rule |
|
| 64 | - * @param int $item : actual position in rule |
|
| 65 | - * @throws Exception |
|
| 66 | - * @return string : everything inside parenthesis |
|
| 67 | - */ |
|
| 68 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
| 69 | - { |
|
| 70 | - $item++; |
|
| 71 | - $start=$item; |
|
| 72 | - $parenthesisCount=0; |
|
| 73 | - while (($item < strlen($rule)) // Not end of string AND |
|
| 74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
| 75 | - { |
|
| 76 | - if ($rule[$item] == '"' ) |
|
| 77 | - { // pass through string |
|
| 78 | - $item++; |
|
| 79 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
| 80 | - continue; |
|
| 81 | - } |
|
| 82 | - if ($rule[$item] == '(') |
|
| 83 | - { |
|
| 84 | - $parenthesisCount++; |
|
| 85 | - } |
|
| 86 | - if ($rule[$item] == ')') |
|
| 87 | - { |
|
| 88 | - $parenthesisCount--; |
|
| 89 | - } |
|
| 90 | - $item++; |
|
| 91 | - } |
|
| 60 | + /** |
|
| 61 | + * Parse elements inside () : jumps over "" and count parenthesis. |
|
| 62 | + * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
| 63 | + * @param string $rule : the current rule |
|
| 64 | + * @param int $item : actual position in rule |
|
| 65 | + * @throws Exception |
|
| 66 | + * @return string : everything inside parenthesis |
|
| 67 | + */ |
|
| 68 | + private function parse_parenthesis(string $rule,int &$item) : string |
|
| 69 | + { |
|
| 70 | + $item++; |
|
| 71 | + $start=$item; |
|
| 72 | + $parenthesisCount=0; |
|
| 73 | + while (($item < strlen($rule)) // Not end of string AND |
|
| 74 | + && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
| 75 | + { |
|
| 76 | + if ($rule[$item] == '"' ) |
|
| 77 | + { // pass through string |
|
| 78 | + $item++; |
|
| 79 | + $item=$this->eval_getNext($rule,$item,'"'); |
|
| 80 | + continue; |
|
| 81 | + } |
|
| 82 | + if ($rule[$item] == '(') |
|
| 83 | + { |
|
| 84 | + $parenthesisCount++; |
|
| 85 | + } |
|
| 86 | + if ($rule[$item] == ')') |
|
| 87 | + { |
|
| 88 | + $parenthesisCount--; |
|
| 89 | + } |
|
| 90 | + $item++; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
| 94 | - $val=substr($rule,$start,$item-$start); |
|
| 95 | - $item++; |
|
| 96 | - return $val; |
|
| 97 | - } |
|
| 93 | + if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
| 94 | + $val=substr($rule,$start,$item-$start); |
|
| 95 | + $item++; |
|
| 96 | + return $val; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Get and eval a grouped condition - ex : (1==1) |
|
| 101 | - * @param string $rule |
|
| 102 | - * @param int $item |
|
| 103 | - * @return array<int,string> |
|
| 104 | - */ |
|
| 105 | - private function get_group(string $rule,int &$item) : array |
|
| 106 | - { |
|
| 107 | - // gets eveything inside parenthesis |
|
| 108 | - $val=$this->parse_parenthesis($rule, $item); |
|
| 109 | - // Returns boolean with evaluation of all inside parenthesis |
|
| 110 | - $start=0; |
|
| 111 | - return array(2,$this->evaluation($val,$start)); |
|
| 112 | - } |
|
| 99 | + /** |
|
| 100 | + * Get and eval a grouped condition - ex : (1==1) |
|
| 101 | + * @param string $rule |
|
| 102 | + * @param int $item |
|
| 103 | + * @return array<int,string> |
|
| 104 | + */ |
|
| 105 | + private function get_group(string $rule,int &$item) : array |
|
| 106 | + { |
|
| 107 | + // gets eveything inside parenthesis |
|
| 108 | + $val=$this->parse_parenthesis($rule, $item); |
|
| 109 | + // Returns boolean with evaluation of all inside parenthesis |
|
| 110 | + $start=0; |
|
| 111 | + return array(2,$this->evaluation($val,$start)); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * @param string $rule |
|
| 116 | - * @param int $item |
|
| 117 | - * @throws Exception |
|
| 118 | - * @return array<int,string> |
|
| 119 | - */ |
|
| 120 | - private function get_function(string $rule,int &$item) : array |
|
| 121 | - { |
|
| 122 | - // function is : __function(param1,param2...) |
|
| 123 | - $start=$item; |
|
| 124 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
| 125 | - { |
|
| 126 | - $item++; |
|
| 127 | - } |
|
| 128 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
| 114 | + /** |
|
| 115 | + * @param string $rule |
|
| 116 | + * @param int $item |
|
| 117 | + * @throws Exception |
|
| 118 | + * @return array<int,string> |
|
| 119 | + */ |
|
| 120 | + private function get_function(string $rule,int &$item) : array |
|
| 121 | + { |
|
| 122 | + // function is : __function(param1,param2...) |
|
| 123 | + $start=$item; |
|
| 124 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
| 125 | + { |
|
| 126 | + $item++; |
|
| 127 | + } |
|
| 128 | + if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
| 129 | 129 | |
| 130 | - // get parameters between parenthesis |
|
| 130 | + // get parameters between parenthesis |
|
| 131 | 131 | |
| 132 | - $this->parse_parenthesis($rule, $item); |
|
| 132 | + $this->parse_parenthesis($rule, $item); |
|
| 133 | 133 | |
| 134 | - $val=substr($rule,$start,$item-$start); |
|
| 134 | + $val=substr($rule,$start,$item-$start); |
|
| 135 | 135 | |
| 136 | - $this->logging->log('got function ' . $val,DEBUG); |
|
| 136 | + $this->logging->log('got function ' . $val,DEBUG); |
|
| 137 | 137 | |
| 138 | - return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
| 138 | + return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
| 139 | 139 | |
| 140 | - } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** Find next token $tok in $rule starting at $item |
|
| 143 | - * @param string $rule |
|
| 144 | - * @param int $item |
|
| 145 | - * @param string $tok : token to search for |
|
| 146 | - * @throws Exception |
|
| 147 | - * @return int Position after next token |
|
| 148 | - */ |
|
| 149 | - protected function eval_getNext(string $rule,int $item,string $tok) |
|
| 150 | - { |
|
| 151 | - while ( |
|
| 152 | - ($rule[$item] != $tok ) |
|
| 153 | - && ($item < strlen($rule))) |
|
| 154 | - { |
|
| 155 | - $item++; |
|
| 156 | - } |
|
| 157 | - if ($item==strlen($rule)) { |
|
| 158 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 159 | - } |
|
| 160 | - return $item+1; |
|
| 161 | - } |
|
| 142 | + /** Find next token $tok in $rule starting at $item |
|
| 143 | + * @param string $rule |
|
| 144 | + * @param int $item |
|
| 145 | + * @param string $tok : token to search for |
|
| 146 | + * @throws Exception |
|
| 147 | + * @return int Position after next token |
|
| 148 | + */ |
|
| 149 | + protected function eval_getNext(string $rule,int $item,string $tok) |
|
| 150 | + { |
|
| 151 | + while ( |
|
| 152 | + ($rule[$item] != $tok ) |
|
| 153 | + && ($item < strlen($rule))) |
|
| 154 | + { |
|
| 155 | + $item++; |
|
| 156 | + } |
|
| 157 | + if ($item==strlen($rule)) { |
|
| 158 | + throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 159 | + } |
|
| 160 | + return $item+1; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** get negate (!) and return true if found - and pass it with item++ - |
|
| 164 | - * @param string $rule |
|
| 165 | - * @param int $item |
|
| 166 | - * @return boolean |
|
| 167 | - */ |
|
| 168 | - private function check_negate_first(string $rule,int &$item) |
|
| 169 | - { |
|
| 170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 171 | - { |
|
| 172 | - $item++; |
|
| 173 | - return true; |
|
| 174 | - } |
|
| 175 | - else |
|
| 176 | - { |
|
| 177 | - return false; |
|
| 178 | - } |
|
| 179 | - } |
|
| 163 | + /** get negate (!) and return true if found - and pass it with item++ - |
|
| 164 | + * @param string $rule |
|
| 165 | + * @param int $item |
|
| 166 | + * @return boolean |
|
| 167 | + */ |
|
| 168 | + private function check_negate_first(string $rule,int &$item) |
|
| 169 | + { |
|
| 170 | + if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 171 | + { |
|
| 172 | + $item++; |
|
| 173 | + return true; |
|
| 174 | + } |
|
| 175 | + else |
|
| 176 | + { |
|
| 177 | + return false; |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** Remove all whitespaces (when not quoted) |
|
| 182 | - * @param string $rule |
|
| 183 | - * @throws Exception |
|
| 184 | - * @return string |
|
| 185 | - */ |
|
| 186 | - public function eval_cleanup($rule) |
|
| 187 | - { |
|
| 188 | - $item=0; |
|
| 189 | - $rule2=''; |
|
| 190 | - while ($item < strlen($rule)) |
|
| 191 | - { |
|
| 192 | - if ($rule[$item]==' ') { $item++; continue; } |
|
| 193 | - if ($rule[$item]=='"') |
|
| 194 | - { |
|
| 195 | - $rule2.=$rule[$item]; |
|
| 196 | - $item++; |
|
| 197 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
| 198 | - { |
|
| 199 | - $rule2.=$rule[$item]; |
|
| 200 | - $item++; |
|
| 201 | - } |
|
| 202 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 203 | - $rule2.=$rule[$item]; |
|
| 204 | - $item++; |
|
| 205 | - continue; |
|
| 206 | - } |
|
| 181 | + /** Remove all whitespaces (when not quoted) |
|
| 182 | + * @param string $rule |
|
| 183 | + * @throws Exception |
|
| 184 | + * @return string |
|
| 185 | + */ |
|
| 186 | + public function eval_cleanup($rule) |
|
| 187 | + { |
|
| 188 | + $item=0; |
|
| 189 | + $rule2=''; |
|
| 190 | + while ($item < strlen($rule)) |
|
| 191 | + { |
|
| 192 | + if ($rule[$item]==' ') { $item++; continue; } |
|
| 193 | + if ($rule[$item]=='"') |
|
| 194 | + { |
|
| 195 | + $rule2.=$rule[$item]; |
|
| 196 | + $item++; |
|
| 197 | + while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
| 198 | + { |
|
| 199 | + $rule2.=$rule[$item]; |
|
| 200 | + $item++; |
|
| 201 | + } |
|
| 202 | + if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 203 | + $rule2.=$rule[$item]; |
|
| 204 | + $item++; |
|
| 205 | + continue; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - $rule2.=$rule[$item]; |
|
| 209 | - $item++; |
|
| 210 | - } |
|
| 208 | + $rule2.=$rule[$item]; |
|
| 209 | + $item++; |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - return $rule2; |
|
| 213 | - } |
|
| 212 | + return $rule2; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | 215 | } |
| 216 | 216 | \ No newline at end of file |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @param string $rule |
| 17 | 17 | * @param int $item |
| 18 | 18 | */ |
| 19 | - abstract public function evaluation($rule,&$item); |
|
| 19 | + abstract public function evaluation($rule, &$item); |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * Get full number |
@@ -24,20 +24,20 @@ discard block |
||
| 24 | 24 | * @param int $item current eval position |
| 25 | 25 | * @return array<int,string> |
| 26 | 26 | */ |
| 27 | - private function get_number(string $rule,int &$item) |
|
| 27 | + private function get_number(string $rule, int &$item) |
|
| 28 | 28 | { |
| 29 | - $item2=$item+1; |
|
| 29 | + $item2=$item + 1; |
|
| 30 | 30 | while ( |
| 31 | - ($item2!=strlen($rule)) |
|
| 32 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
| 31 | + ($item2 != strlen($rule)) |
|
| 32 | + && (preg_match('/[\-0-9\.]/', $rule[$item2]))) |
|
| 33 | 33 | { |
| 34 | - $item2++ ; |
|
| 34 | + $item2++; |
|
| 35 | 35 | } |
| 36 | - $val=substr($rule,$item,$item2-$item); |
|
| 36 | + $val=substr($rule, $item, $item2 - $item); |
|
| 37 | 37 | $item=$item2; |
| 38 | 38 | //echo "number ".$val."\n"; |
| 39 | 39 | |
| 40 | - return array(0,$val); |
|
| 40 | + return array(0, $val); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -46,14 +46,14 @@ discard block |
||
| 46 | 46 | * @param int $item current eval position |
| 47 | 47 | * @return array<int,string> |
| 48 | 48 | */ |
| 49 | - private function get_string(string $rule,int &$item) |
|
| 49 | + private function get_string(string $rule, int &$item) |
|
| 50 | 50 | { |
| 51 | 51 | $item++; |
| 52 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
| 53 | - $val=substr($rule,$item,$item2-$item-1); |
|
| 52 | + $item2=$this->eval_getNext($rule, $item, '"'); |
|
| 53 | + $val=substr($rule, $item, $item2 - $item - 1); |
|
| 54 | 54 | $item=$item2; |
| 55 | 55 | //echo "string : ".$val."\n"; |
| 56 | - return array(1,$val); |
|
| 56 | + return array(1, $val); |
|
| 57 | 57 | |
| 58 | 58 | } |
| 59 | 59 | |
@@ -65,18 +65,18 @@ discard block |
||
| 65 | 65 | * @throws Exception |
| 66 | 66 | * @return string : everything inside parenthesis |
| 67 | 67 | */ |
| 68 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
| 68 | + private function parse_parenthesis(string $rule, int &$item) : string |
|
| 69 | 69 | { |
| 70 | 70 | $item++; |
| 71 | 71 | $start=$item; |
| 72 | 72 | $parenthesisCount=0; |
| 73 | 73 | while (($item < strlen($rule)) // Not end of string AND |
| 74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
| 74 | + && (($rule[$item] != ')') || $parenthesisCount > 0)) // Closing ')' or embeded () |
|
| 75 | 75 | { |
| 76 | - if ($rule[$item] == '"' ) |
|
| 76 | + if ($rule[$item] == '"') |
|
| 77 | 77 | { // pass through string |
| 78 | 78 | $item++; |
| 79 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
| 79 | + $item=$this->eval_getNext($rule, $item, '"'); |
|
| 80 | 80 | continue; |
| 81 | 81 | } |
| 82 | 82 | if ($rule[$item] == '(') |
@@ -90,8 +90,8 @@ discard block |
||
| 90 | 90 | $item++; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
| 94 | - $val=substr($rule,$start,$item-$start); |
|
| 93 | + if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); } |
|
| 94 | + $val=substr($rule, $start, $item - $start); |
|
| 95 | 95 | $item++; |
| 96 | 96 | return $val; |
| 97 | 97 | } |
@@ -102,13 +102,13 @@ discard block |
||
| 102 | 102 | * @param int $item |
| 103 | 103 | * @return array<int,string> |
| 104 | 104 | */ |
| 105 | - private function get_group(string $rule,int &$item) : array |
|
| 105 | + private function get_group(string $rule, int &$item) : array |
|
| 106 | 106 | { |
| 107 | 107 | // gets eveything inside parenthesis |
| 108 | 108 | $val=$this->parse_parenthesis($rule, $item); |
| 109 | 109 | // Returns boolean with evaluation of all inside parenthesis |
| 110 | 110 | $start=0; |
| 111 | - return array(2,$this->evaluation($val,$start)); |
|
| 111 | + return array(2, $this->evaluation($val, $start)); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | /** |
@@ -117,25 +117,25 @@ discard block |
||
| 117 | 117 | * @throws Exception |
| 118 | 118 | * @return array<int,string> |
| 119 | 119 | */ |
| 120 | - private function get_function(string $rule,int &$item) : array |
|
| 120 | + private function get_function(string $rule, int &$item) : array |
|
| 121 | 121 | { |
| 122 | 122 | // function is : __function(param1,param2...) |
| 123 | 123 | $start=$item; |
| 124 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
| 124 | + while (($item < strlen($rule)) && ($rule[$item] != '(')) // Not end of string AND not opening '(' |
|
| 125 | 125 | { |
| 126 | 126 | $item++; |
| 127 | 127 | } |
| 128 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
| 128 | + if ($item == strlen($rule)) {throw new Exception("no opening () for function in ".$rule." at ".$item); } |
|
| 129 | 129 | |
| 130 | 130 | // get parameters between parenthesis |
| 131 | 131 | |
| 132 | 132 | $this->parse_parenthesis($rule, $item); |
| 133 | 133 | |
| 134 | - $val=substr($rule,$start,$item-$start); |
|
| 134 | + $val=substr($rule, $start, $item - $start); |
|
| 135 | 135 | |
| 136 | - $this->logging->log('got function ' . $val,DEBUG); |
|
| 136 | + $this->logging->log('got function '.$val, DEBUG); |
|
| 137 | 137 | |
| 138 | - return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
| 138 | + return array(2, $this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
| 139 | 139 | |
| 140 | 140 | } |
| 141 | 141 | |
@@ -146,18 +146,18 @@ discard block |
||
| 146 | 146 | * @throws Exception |
| 147 | 147 | * @return int Position after next token |
| 148 | 148 | */ |
| 149 | - protected function eval_getNext(string $rule,int $item,string $tok) |
|
| 149 | + protected function eval_getNext(string $rule, int $item, string $tok) |
|
| 150 | 150 | { |
| 151 | 151 | while ( |
| 152 | - ($rule[$item] != $tok ) |
|
| 152 | + ($rule[$item] != $tok) |
|
| 153 | 153 | && ($item < strlen($rule))) |
| 154 | 154 | { |
| 155 | 155 | $item++; |
| 156 | 156 | } |
| 157 | - if ($item==strlen($rule)) { |
|
| 158 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 157 | + if ($item == strlen($rule)) { |
|
| 158 | + throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item); |
|
| 159 | 159 | } |
| 160 | - return $item+1; |
|
| 160 | + return $item + 1; |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** get negate (!) and return true if found - and pass it with item++ - |
@@ -165,9 +165,9 @@ discard block |
||
| 165 | 165 | * @param int $item |
| 166 | 166 | * @return boolean |
| 167 | 167 | */ |
| 168 | - private function check_negate_first(string $rule,int &$item) |
|
| 168 | + private function check_negate_first(string $rule, int &$item) |
|
| 169 | 169 | { |
| 170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 170 | + if ($rule[$item] == '!') // If '!' found, negate next expression. |
|
| 171 | 171 | { |
| 172 | 172 | $item++; |
| 173 | 173 | return true; |
@@ -189,17 +189,17 @@ discard block |
||
| 189 | 189 | $rule2=''; |
| 190 | 190 | while ($item < strlen($rule)) |
| 191 | 191 | { |
| 192 | - if ($rule[$item]==' ') { $item++; continue; } |
|
| 193 | - if ($rule[$item]=='"') |
|
| 192 | + if ($rule[$item] == ' ') { $item++; continue; } |
|
| 193 | + if ($rule[$item] == '"') |
|
| 194 | 194 | { |
| 195 | 195 | $rule2.=$rule[$item]; |
| 196 | 196 | $item++; |
| 197 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
| 197 | + while (($item < strlen($rule)) && ($rule[$item] != '"')) |
|
| 198 | 198 | { |
| 199 | 199 | $rule2.=$rule[$item]; |
| 200 | 200 | $item++; |
| 201 | 201 | } |
| 202 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 202 | + if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item); |
|
| 203 | 203 | $rule2.=$rule[$item]; |
| 204 | 204 | $item++; |
| 205 | 205 | continue; |
@@ -6,233 +6,233 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Rule |
| 8 | 8 | { |
| 9 | - use \RuleUtils; |
|
| 9 | + use \RuleUtils; |
|
| 10 | 10 | |
| 11 | - /** @var Logging $logging logging class*/ |
|
| 12 | - protected $logging; |
|
| 11 | + /** @var Logging $logging logging class*/ |
|
| 12 | + protected $logging; |
|
| 13 | 13 | |
| 14 | - /** @var Trap $trapClass */ |
|
| 15 | - protected $trapClass; |
|
| 14 | + /** @var Trap $trapClass */ |
|
| 15 | + protected $trapClass; |
|
| 16 | 16 | |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Setup Rule Class |
|
| 20 | - * @param Trap $trapClass : To get logging class & plugin class |
|
| 21 | - */ |
|
| 22 | - function __construct($trapClass) |
|
| 23 | - { |
|
| 24 | - $this->trapClass=$trapClass; |
|
| 25 | - $this->logging=$trapClass->logging; |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Setup Rule Class |
|
| 20 | + * @param Trap $trapClass : To get logging class & plugin class |
|
| 21 | + */ |
|
| 22 | + function __construct($trapClass) |
|
| 23 | + { |
|
| 24 | + $this->trapClass=$trapClass; |
|
| 25 | + $this->logging=$trapClass->logging; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | |
| 29 | - protected function eval_getElement($rule,&$item) |
|
| 30 | - { |
|
| 31 | - if ($item >= strlen($rule)) |
|
| 32 | - { |
|
| 33 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
| 34 | - } |
|
| 35 | - while ($rule[$item]==' ') $item++; |
|
| 36 | - if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
| 37 | - { // number |
|
| 38 | - return $this->get_number($rule, $item); |
|
| 39 | - } |
|
| 40 | - if ($rule[$item] == '"') |
|
| 41 | - { // string |
|
| 42 | - return $this->get_string($rule, $item); |
|
| 43 | - } |
|
| 29 | + protected function eval_getElement($rule,&$item) |
|
| 30 | + { |
|
| 31 | + if ($item >= strlen($rule)) |
|
| 32 | + { |
|
| 33 | + throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
| 34 | + } |
|
| 35 | + while ($rule[$item]==' ') $item++; |
|
| 36 | + if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
| 37 | + { // number |
|
| 38 | + return $this->get_number($rule, $item); |
|
| 39 | + } |
|
| 40 | + if ($rule[$item] == '"') |
|
| 41 | + { // string |
|
| 42 | + return $this->get_string($rule, $item); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - if ($rule[$item] == '(') |
|
| 46 | - { // grouping |
|
| 47 | - return $this->get_group($rule, $item); |
|
| 48 | - } |
|
| 49 | - if ($rule[$item] == '_') |
|
| 50 | - { // function |
|
| 51 | - return $this->get_function($rule, $item); |
|
| 52 | - } |
|
| 53 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
| 45 | + if ($rule[$item] == '(') |
|
| 46 | + { // grouping |
|
| 47 | + return $this->get_group($rule, $item); |
|
| 48 | + } |
|
| 49 | + if ($rule[$item] == '_') |
|
| 50 | + { // function |
|
| 51 | + return $this->get_function($rule, $item); |
|
| 52 | + } |
|
| 53 | + throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
| 54 | 54 | |
| 55 | - } |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - protected function eval_getOper($rule,&$item) |
|
| 58 | - { |
|
| 59 | - while ($rule[$item]==' ') $item++; |
|
| 60 | - switch ($rule[$item]) |
|
| 61 | - { |
|
| 62 | - case '<': |
|
| 63 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
| 64 | - $item++; return array(0,"<"); |
|
| 65 | - case '>': |
|
| 66 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
| 67 | - $item++; return array(0,">"); |
|
| 68 | - case '=': |
|
| 69 | - $item++; return array(0,"="); |
|
| 70 | - case '!': |
|
| 71 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
| 72 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
| 73 | - case '~': |
|
| 74 | - $item++; return array(0,"~"); |
|
| 75 | - case '|': |
|
| 76 | - $item++; return array(1,"|"); |
|
| 77 | - case '&': |
|
| 78 | - $item++; return array(1,"&"); |
|
| 79 | - default : |
|
| 80 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
| 81 | - } |
|
| 82 | - } |
|
| 57 | + protected function eval_getOper($rule,&$item) |
|
| 58 | + { |
|
| 59 | + while ($rule[$item]==' ') $item++; |
|
| 60 | + switch ($rule[$item]) |
|
| 61 | + { |
|
| 62 | + case '<': |
|
| 63 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
| 64 | + $item++; return array(0,"<"); |
|
| 65 | + case '>': |
|
| 66 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
| 67 | + $item++; return array(0,">"); |
|
| 68 | + case '=': |
|
| 69 | + $item++; return array(0,"="); |
|
| 70 | + case '!': |
|
| 71 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
| 72 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
| 73 | + case '~': |
|
| 74 | + $item++; return array(0,"~"); |
|
| 75 | + case '|': |
|
| 76 | + $item++; return array(1,"|"); |
|
| 77 | + case '&': |
|
| 78 | + $item++; return array(1,"&"); |
|
| 79 | + default : |
|
| 80 | + throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - private function do_compare($val1,$val2,$comp,$negate) |
|
| 85 | - { |
|
| 86 | - switch ($comp){ |
|
| 87 | - case '<': $retVal= ($val1 < $val2); break; |
|
| 88 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
| 89 | - case '>': $retVal= ($val1 > $val2); break; |
|
| 90 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
| 91 | - case '=': $retVal= ($val1 == $val2); break; |
|
| 92 | - case '!=': $retVal= ($val1 != $val2); break; |
|
| 93 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
| 94 | - case '|': $retVal= ($val1 || $val2); break; |
|
| 95 | - case '&': $retVal= ($val1 && $val2); break; |
|
| 96 | - default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
| 97 | - } |
|
| 98 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 84 | + private function do_compare($val1,$val2,$comp,$negate) |
|
| 85 | + { |
|
| 86 | + switch ($comp){ |
|
| 87 | + case '<': $retVal= ($val1 < $val2); break; |
|
| 88 | + case '<=': $retVal= ($val1 <= $val2); break; |
|
| 89 | + case '>': $retVal= ($val1 > $val2); break; |
|
| 90 | + case '>=': $retVal= ($val1 >= $val2); break; |
|
| 91 | + case '=': $retVal= ($val1 == $val2); break; |
|
| 92 | + case '!=': $retVal= ($val1 != $val2); break; |
|
| 93 | + case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
| 94 | + case '|': $retVal= ($val1 || $val2); break; |
|
| 95 | + case '&': $retVal= ($val1 && $val2); break; |
|
| 96 | + default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
| 97 | + } |
|
| 98 | + if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 99 | 99 | |
| 100 | - return $retVal; |
|
| 101 | - } |
|
| 100 | + return $retVal; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** Evaluation : makes token and evaluate. |
|
| 104 | - * Public function for expressions testing |
|
| 105 | - * accepts : < > = <= >= != (typec = 0) |
|
| 106 | - * operators : & | (typec=1) |
|
| 107 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 108 | - * comparison int vs strings will return null (error) |
|
| 109 | - * return : bool or null on error |
|
| 110 | - */ |
|
| 111 | - public function evaluation($rule,&$item) |
|
| 112 | - { |
|
| 113 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 114 | - $negate=$this->check_negate_first($rule, $item); |
|
| 115 | - // First element : number, string or () |
|
| 116 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
| 117 | - //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
| 103 | + /** Evaluation : makes token and evaluate. |
|
| 104 | + * Public function for expressions testing |
|
| 105 | + * accepts : < > = <= >= != (typec = 0) |
|
| 106 | + * operators : & | (typec=1) |
|
| 107 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 108 | + * comparison int vs strings will return null (error) |
|
| 109 | + * return : bool or null on error |
|
| 110 | + */ |
|
| 111 | + public function evaluation($rule,&$item) |
|
| 112 | + { |
|
| 113 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 114 | + $negate=$this->check_negate_first($rule, $item); |
|
| 115 | + // First element : number, string or () |
|
| 116 | + list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
| 117 | + //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
| 118 | 118 | |
| 119 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 120 | - { |
|
| 121 | - if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
| 122 | - if ($negate === true) $val1= ! $val1; |
|
| 123 | - return $val1; |
|
| 124 | - } |
|
| 119 | + if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 120 | + { |
|
| 121 | + if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
| 122 | + if ($negate === true) $val1= ! $val1; |
|
| 123 | + return $val1; |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - // Second element : operator |
|
| 127 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
| 128 | - //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
| 126 | + // Second element : operator |
|
| 127 | + list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
| 128 | + //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
| 129 | 129 | |
| 130 | - // Third element : number, string or () |
|
| 131 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 132 | - { |
|
| 133 | - $item++; |
|
| 134 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 135 | - $val2= ! $this->evaluation($rule,$item); |
|
| 136 | - $type2=2; // result is a boolean |
|
| 137 | - } |
|
| 138 | - else |
|
| 139 | - { |
|
| 140 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 141 | - } |
|
| 142 | - //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
| 130 | + // Third element : number, string or () |
|
| 131 | + if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 132 | + { |
|
| 133 | + $item++; |
|
| 134 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 135 | + $val2= ! $this->evaluation($rule,$item); |
|
| 136 | + $type2=2; // result is a boolean |
|
| 137 | + } |
|
| 138 | + else |
|
| 139 | + { |
|
| 140 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 141 | + } |
|
| 142 | + //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
| 143 | 143 | |
| 144 | - if ($type1!=$type2) // cannot compare different types |
|
| 145 | - { |
|
| 146 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
| 147 | - } |
|
| 148 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 149 | - { |
|
| 150 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 151 | - } |
|
| 144 | + if ($type1!=$type2) // cannot compare different types |
|
| 145 | + { |
|
| 146 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
| 147 | + } |
|
| 148 | + if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 149 | + { |
|
| 150 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
| 153 | + $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
| 154 | 154 | |
| 155 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 156 | - // check for logical operator : |
|
| 157 | - switch ($rule[$item]) |
|
| 158 | - { |
|
| 159 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
| 160 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
| 155 | + if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 156 | + // check for logical operator : |
|
| 157 | + switch ($rule[$item]) |
|
| 158 | + { |
|
| 159 | + case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
| 160 | + case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
| 161 | 161 | |
| 162 | - default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
| 163 | - } |
|
| 164 | - } |
|
| 162 | + default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * Get '*' or '**' and transform in [0-9]+ or .* in return string |
|
| 168 | - * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
|
| 169 | - * @return string correct regexp format |
|
| 170 | - */ |
|
| 171 | - public function regexp_eval(string &$oid) |
|
| 172 | - { |
|
| 173 | - // ** replaced by .* |
|
| 174 | - $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
| 175 | - // * replaced by [0-9]+ |
|
| 176 | - $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
| 166 | + /** |
|
| 167 | + * Get '*' or '**' and transform in [0-9]+ or .* in return string |
|
| 168 | + * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
|
| 169 | + * @return string correct regexp format |
|
| 170 | + */ |
|
| 171 | + public function regexp_eval(string &$oid) |
|
| 172 | + { |
|
| 173 | + // ** replaced by .* |
|
| 174 | + $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
| 175 | + // * replaced by [0-9]+ |
|
| 176 | + $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
| 177 | 177 | |
| 178 | - // replace * with \* in oid for preg_replace |
|
| 179 | - $oid=preg_replace('/\*/', '\*', $oid); |
|
| 178 | + // replace * with \* in oid for preg_replace |
|
| 179 | + $oid=preg_replace('/\*/', '\*', $oid); |
|
| 180 | 180 | |
| 181 | - $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
| 181 | + $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
| 182 | 182 | |
| 183 | - return $oidR; |
|
| 184 | - } |
|
| 183 | + return $oidR; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | 186 | |
| 187 | - /** Evaluation rule (uses eval_* functions recursively) |
|
| 188 | - * @param string $rule : rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
| 189 | - * @param array $oidList : OIDs values to sustitute. |
|
| 190 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 191 | - */ |
|
| 192 | - public function eval_rule($rule,$oidList) |
|
| 193 | - { |
|
| 194 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 195 | - { |
|
| 196 | - return true; |
|
| 197 | - } |
|
| 198 | - $matches=array(); |
|
| 199 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
| 200 | - { |
|
| 201 | - $oid=$matches[1]; |
|
| 202 | - $found=0; |
|
| 203 | - // Test and transform regexp |
|
| 204 | - $oidR = $this->regexp_eval($oid); |
|
| 187 | + /** Evaluation rule (uses eval_* functions recursively) |
|
| 188 | + * @param string $rule : rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
| 189 | + * @param array $oidList : OIDs values to sustitute. |
|
| 190 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 191 | + */ |
|
| 192 | + public function eval_rule($rule,$oidList) |
|
| 193 | + { |
|
| 194 | + if ($rule==null || $rule == '') // Empty rule is always true |
|
| 195 | + { |
|
| 196 | + return true; |
|
| 197 | + } |
|
| 198 | + $matches=array(); |
|
| 199 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
| 200 | + { |
|
| 201 | + $oid=$matches[1]; |
|
| 202 | + $found=0; |
|
| 203 | + // Test and transform regexp |
|
| 204 | + $oidR = $this->regexp_eval($oid); |
|
| 205 | 205 | |
| 206 | - foreach($oidList as $val) |
|
| 207 | - { |
|
| 208 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 209 | - { |
|
| 210 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
| 211 | - { // If not a number, change " to ' and put " around it |
|
| 212 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
| 213 | - $val->value='"'.$val->value.'"'; |
|
| 214 | - } |
|
| 215 | - $rep=0; |
|
| 216 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
| 217 | - if ($rep==0) |
|
| 218 | - { |
|
| 219 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
| 220 | - return false; |
|
| 221 | - } |
|
| 222 | - $found=1; |
|
| 223 | - break; |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - if ($found==0) |
|
| 227 | - { // OID not found : throw error |
|
| 228 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - $item=0; |
|
| 232 | - $rule=$this->eval_cleanup($rule); |
|
| 233 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
| 206 | + foreach($oidList as $val) |
|
| 207 | + { |
|
| 208 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 209 | + { |
|
| 210 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
| 211 | + { // If not a number, change " to ' and put " around it |
|
| 212 | + $val->value=preg_replace('/"/',"'",$val->value); |
|
| 213 | + $val->value='"'.$val->value.'"'; |
|
| 214 | + } |
|
| 215 | + $rep=0; |
|
| 216 | + $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
| 217 | + if ($rep==0) |
|
| 218 | + { |
|
| 219 | + $this->logging->log("Error in rule_eval",WARN,''); |
|
| 220 | + return false; |
|
| 221 | + } |
|
| 222 | + $found=1; |
|
| 223 | + break; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + if ($found==0) |
|
| 227 | + { // OID not found : throw error |
|
| 228 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + $item=0; |
|
| 232 | + $rule=$this->eval_cleanup($rule); |
|
| 233 | + $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
| 234 | 234 | |
| 235 | - return $this->evaluation($rule,$item); |
|
| 236 | - } |
|
| 235 | + return $this->evaluation($rule,$item); |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | 238 | } |
| 239 | 239 | \ No newline at end of file |
@@ -26,14 +26,14 @@ discard block |
||
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | |
| 29 | - protected function eval_getElement($rule,&$item) |
|
| 29 | + protected function eval_getElement($rule, &$item) |
|
| 30 | 30 | { |
| 31 | 31 | if ($item >= strlen($rule)) |
| 32 | 32 | { |
| 33 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
| 33 | + throw new Exception("Early end of string ".$rule." at ".$item); |
|
| 34 | 34 | } |
| 35 | - while ($rule[$item]==' ') $item++; |
|
| 36 | - if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
| 35 | + while ($rule[$item] == ' ') $item++; |
|
| 36 | + if (preg_match('/[\-0-9\.]/', $rule[$item])) |
|
| 37 | 37 | { // number |
| 38 | 38 | return $this->get_number($rule, $item); |
| 39 | 39 | } |
@@ -50,52 +50,52 @@ discard block |
||
| 50 | 50 | { // function |
| 51 | 51 | return $this->get_function($rule, $item); |
| 52 | 52 | } |
| 53 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
| 53 | + throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]); |
|
| 54 | 54 | |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - protected function eval_getOper($rule,&$item) |
|
| 57 | + protected function eval_getOper($rule, &$item) |
|
| 58 | 58 | { |
| 59 | - while ($rule[$item]==' ') $item++; |
|
| 59 | + while ($rule[$item] == ' ') $item++; |
|
| 60 | 60 | switch ($rule[$item]) |
| 61 | 61 | { |
| 62 | 62 | case '<': |
| 63 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
| 64 | - $item++; return array(0,"<"); |
|
| 63 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); } |
|
| 64 | + $item++; return array(0, "<"); |
|
| 65 | 65 | case '>': |
| 66 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
| 67 | - $item++; return array(0,">"); |
|
| 66 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); } |
|
| 67 | + $item++; return array(0, ">"); |
|
| 68 | 68 | case '=': |
| 69 | - $item++; return array(0,"="); |
|
| 69 | + $item++; return array(0, "="); |
|
| 70 | 70 | case '!': |
| 71 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
| 72 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
| 71 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); } |
|
| 72 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule." at ".$item); |
|
| 73 | 73 | case '~': |
| 74 | - $item++; return array(0,"~"); |
|
| 74 | + $item++; return array(0, "~"); |
|
| 75 | 75 | case '|': |
| 76 | - $item++; return array(1,"|"); |
|
| 76 | + $item++; return array(1, "|"); |
|
| 77 | 77 | case '&': |
| 78 | - $item++; return array(1,"&"); |
|
| 78 | + $item++; return array(1, "&"); |
|
| 79 | 79 | default : |
| 80 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
| 80 | + throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item); |
|
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - private function do_compare($val1,$val2,$comp,$negate) |
|
| 84 | + private function do_compare($val1, $val2, $comp, $negate) |
|
| 85 | 85 | { |
| 86 | - switch ($comp){ |
|
| 87 | - case '<': $retVal= ($val1 < $val2); break; |
|
| 88 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
| 89 | - case '>': $retVal= ($val1 > $val2); break; |
|
| 90 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
| 91 | - case '=': $retVal= ($val1 == $val2); break; |
|
| 92 | - case '!=': $retVal= ($val1 != $val2); break; |
|
| 93 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
| 94 | - case '|': $retVal= ($val1 || $val2); break; |
|
| 95 | - case '&': $retVal= ($val1 && $val2); break; |
|
| 86 | + switch ($comp) { |
|
| 87 | + case '<': $retVal=($val1 < $val2); break; |
|
| 88 | + case '<=': $retVal=($val1 <= $val2); break; |
|
| 89 | + case '>': $retVal=($val1 > $val2); break; |
|
| 90 | + case '>=': $retVal=($val1 >= $val2); break; |
|
| 91 | + case '=': $retVal=($val1 == $val2); break; |
|
| 92 | + case '!=': $retVal=($val1 != $val2); break; |
|
| 93 | + case '~': $retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break; |
|
| 94 | + case '|': $retVal=($val1 || $val2); break; |
|
| 95 | + case '&': $retVal=($val1 && $val2); break; |
|
| 96 | 96 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
| 97 | 97 | } |
| 98 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 98 | + if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression |
|
| 99 | 99 | |
| 100 | 100 | return $retVal; |
| 101 | 101 | } |
@@ -108,56 +108,56 @@ discard block |
||
| 108 | 108 | * comparison int vs strings will return null (error) |
| 109 | 109 | * return : bool or null on error |
| 110 | 110 | */ |
| 111 | - public function evaluation($rule,&$item) |
|
| 111 | + public function evaluation($rule, &$item) |
|
| 112 | 112 | { |
| 113 | 113 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
| 114 | 114 | $negate=$this->check_negate_first($rule, $item); |
| 115 | 115 | // First element : number, string or () |
| 116 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
| 116 | + list($type1, $val1)=$this->eval_getElement($rule, $item); |
|
| 117 | 117 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
| 118 | 118 | |
| 119 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 119 | + if ($item == strlen($rule)) // If only element, return value, but only boolean |
|
| 120 | 120 | { |
| 121 | 121 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
| 122 | - if ($negate === true) $val1= ! $val1; |
|
| 122 | + if ($negate === true) $val1=!$val1; |
|
| 123 | 123 | return $val1; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | // Second element : operator |
| 127 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
| 127 | + list($typec, $comp)=$this->eval_getOper($rule, $item); |
|
| 128 | 128 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
| 129 | 129 | |
| 130 | 130 | // Third element : number, string or () |
| 131 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 131 | + if ($rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 132 | 132 | { |
| 133 | 133 | $item++; |
| 134 | 134 | if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
| 135 | - $val2= ! $this->evaluation($rule,$item); |
|
| 135 | + $val2=!$this->evaluation($rule, $item); |
|
| 136 | 136 | $type2=2; // result is a boolean |
| 137 | 137 | } |
| 138 | 138 | else |
| 139 | 139 | { |
| 140 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 140 | + list($type2, $val2)=$this->eval_getElement($rule, $item); |
|
| 141 | 141 | } |
| 142 | 142 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 143 | 143 | |
| 144 | - if ($type1!=$type2) // cannot compare different types |
|
| 144 | + if ($type1 != $type2) // cannot compare different types |
|
| 145 | 145 | { |
| 146 | 146 | throw new Exception("Cannot compare string & number : ".$rule); |
| 147 | 147 | } |
| 148 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 148 | + if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number |
|
| 149 | 149 | { |
| 150 | 150 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
| 153 | + $retVal=$this->do_compare($val1, $val2, $comp, $negate); |
|
| 154 | 154 | |
| 155 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 155 | + if ($item == strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 156 | 156 | // check for logical operator : |
| 157 | 157 | switch ($rule[$item]) |
| 158 | 158 | { |
| 159 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
| 160 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
| 159 | + case '|': $item++; return ($retVal || $this->evaluation($rule, $item)); |
|
| 160 | + case '&': $item++; return ($retVal && $this->evaluation($rule, $item)); |
|
| 161 | 161 | |
| 162 | 162 | default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
| 163 | 163 | } |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | // replace * with \* in oid for preg_replace |
| 179 | 179 | $oid=preg_replace('/\*/', '\*', $oid); |
| 180 | 180 | |
| 181 | - $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
| 181 | + $this->logging->log('Regexp eval : '.$oid.' / '.$oidR, DEBUG); |
|
| 182 | 182 | |
| 183 | 183 | return $oidR; |
| 184 | 184 | } |
@@ -189,50 +189,50 @@ discard block |
||
| 189 | 189 | * @param array $oidList : OIDs values to sustitute. |
| 190 | 190 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
| 191 | 191 | */ |
| 192 | - public function eval_rule($rule,$oidList) |
|
| 192 | + public function eval_rule($rule, $oidList) |
|
| 193 | 193 | { |
| 194 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 194 | + if ($rule == null || $rule == '') // Empty rule is always true |
|
| 195 | 195 | { |
| 196 | 196 | return true; |
| 197 | 197 | } |
| 198 | 198 | $matches=array(); |
| 199 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
| 199 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1) |
|
| 200 | 200 | { |
| 201 | 201 | $oid=$matches[1]; |
| 202 | 202 | $found=0; |
| 203 | 203 | // Test and transform regexp |
| 204 | - $oidR = $this->regexp_eval($oid); |
|
| 204 | + $oidR=$this->regexp_eval($oid); |
|
| 205 | 205 | |
| 206 | - foreach($oidList as $val) |
|
| 206 | + foreach ($oidList as $val) |
|
| 207 | 207 | { |
| 208 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 208 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
| 209 | 209 | { |
| 210 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
| 210 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/', $val->value)) |
|
| 211 | 211 | { // If not a number, change " to ' and put " around it |
| 212 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
| 212 | + $val->value=preg_replace('/"/', "'", $val->value); |
|
| 213 | 213 | $val->value='"'.$val->value.'"'; |
| 214 | 214 | } |
| 215 | 215 | $rep=0; |
| 216 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
| 217 | - if ($rep==0) |
|
| 216 | + $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep); |
|
| 217 | + if ($rep == 0) |
|
| 218 | 218 | { |
| 219 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
| 219 | + $this->logging->log("Error in rule_eval", WARN, ''); |
|
| 220 | 220 | return false; |
| 221 | 221 | } |
| 222 | 222 | $found=1; |
| 223 | 223 | break; |
| 224 | 224 | } |
| 225 | 225 | } |
| 226 | - if ($found==0) |
|
| 226 | + if ($found == 0) |
|
| 227 | 227 | { // OID not found : throw error |
| 228 | 228 | throw new Exception('OID '.$oid.' not found in trap'); |
| 229 | 229 | } |
| 230 | 230 | } |
| 231 | 231 | $item=0; |
| 232 | 232 | $rule=$this->eval_cleanup($rule); |
| 233 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
| 233 | + $this->logging->log('Rule after clenup: '.$rule, INFO); |
|
| 234 | 234 | |
| 235 | - return $this->evaluation($rule,$item); |
|
| 235 | + return $this->evaluation($rule, $item); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | } |
| 239 | 239 | \ No newline at end of file |
@@ -32,7 +32,9 @@ discard block |
||
| 32 | 32 | { |
| 33 | 33 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
| 34 | 34 | } |
| 35 | - while ($rule[$item]==' ') $item++; |
|
| 35 | + while ($rule[$item]==' ') { |
|
| 36 | + $item++; |
|
| 37 | + } |
|
| 36 | 38 | if (preg_match('/[\-0-9\.]/',$rule[$item])) |
| 37 | 39 | { // number |
| 38 | 40 | return $this->get_number($rule, $item); |
@@ -56,7 +58,9 @@ discard block |
||
| 56 | 58 | |
| 57 | 59 | protected function eval_getOper($rule,&$item) |
| 58 | 60 | { |
| 59 | - while ($rule[$item]==' ') $item++; |
|
| 61 | + while ($rule[$item]==' ') { |
|
| 62 | + $item++; |
|
| 63 | + } |
|
| 60 | 64 | switch ($rule[$item]) |
| 61 | 65 | { |
| 62 | 66 | case '<': |
@@ -95,7 +99,10 @@ discard block |
||
| 95 | 99 | case '&': $retVal= ($val1 && $val2); break; |
| 96 | 100 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
| 97 | 101 | } |
| 98 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 102 | + if ($negate === true) { |
|
| 103 | + $retVal = ! $retVal; |
|
| 104 | + } |
|
| 105 | + // Inverse result if negate before expression |
|
| 99 | 106 | |
| 100 | 107 | return $retVal; |
| 101 | 108 | } |
@@ -116,10 +123,14 @@ discard block |
||
| 116 | 123 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
| 117 | 124 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
| 118 | 125 | |
| 119 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 126 | + if ($item==strlen($rule)) { |
|
| 127 | + // If only element, return value, but only boolean |
|
| 120 | 128 | { |
| 121 | 129 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
| 122 | - if ($negate === true) $val1= ! $val1; |
|
| 130 | + } |
|
| 131 | + if ($negate === true) { |
|
| 132 | + $val1= ! $val1; |
|
| 133 | + } |
|
| 123 | 134 | return $val1; |
| 124 | 135 | } |
| 125 | 136 | |
@@ -128,31 +139,41 @@ discard block |
||
| 128 | 139 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
| 129 | 140 | |
| 130 | 141 | // Third element : number, string or () |
| 131 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 142 | + if ( $rule[$item] == '!') { |
|
| 143 | + // starts with a ! so evaluate whats next |
|
| 132 | 144 | { |
| 133 | 145 | $item++; |
| 134 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 146 | + } |
|
| 147 | + if ($typec != 1) { |
|
| 148 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 149 | + } |
|
| 135 | 150 | $val2= ! $this->evaluation($rule,$item); |
| 136 | 151 | $type2=2; // result is a boolean |
| 137 | - } |
|
| 138 | - else |
|
| 152 | + } else |
|
| 139 | 153 | { |
| 140 | 154 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
| 141 | 155 | } |
| 142 | 156 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 143 | 157 | |
| 144 | - if ($type1!=$type2) // cannot compare different types |
|
| 158 | + if ($type1!=$type2) { |
|
| 159 | + // cannot compare different types |
|
| 145 | 160 | { |
| 146 | 161 | throw new Exception("Cannot compare string & number : ".$rule); |
| 147 | 162 | } |
| 148 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 163 | + } |
|
| 164 | + if ($typec==1 && $type1 !=2) { |
|
| 165 | + // cannot use & or | with string/number |
|
| 149 | 166 | { |
| 150 | 167 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
| 151 | 168 | } |
| 169 | + } |
|
| 152 | 170 | |
| 153 | 171 | $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
| 154 | 172 | |
| 155 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 173 | + if ($item==strlen($rule)) { |
|
| 174 | + return $retVal; |
|
| 175 | + } |
|
| 176 | + // End of string : return evaluation |
|
| 156 | 177 | // check for logical operator : |
| 157 | 178 | switch ($rule[$item]) |
| 158 | 179 | { |
@@ -191,10 +212,12 @@ discard block |
||
| 191 | 212 | */ |
| 192 | 213 | public function eval_rule($rule,$oidList) |
| 193 | 214 | { |
| 194 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 215 | + if ($rule==null || $rule == '') { |
|
| 216 | + // Empty rule is always true |
|
| 195 | 217 | { |
| 196 | 218 | return true; |
| 197 | 219 | } |
| 220 | + } |
|
| 198 | 221 | $matches=array(); |
| 199 | 222 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
| 200 | 223 | { |
@@ -95,8 +95,7 @@ discard block |
||
| 95 | 95 | { |
| 96 | 96 | $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
| 97 | 97 | $this->logSetup=true; |
| 98 | - } |
|
| 99 | - else |
|
| 98 | + } else |
|
| 100 | 99 | { |
| 101 | 100 | $this->logSetup=false; |
| 102 | 101 | } |
@@ -121,7 +120,10 @@ discard block |
||
| 121 | 120 | $this->getDatabaseOptions(); // Get options in database |
| 122 | 121 | |
| 123 | 122 | //*************** Setup API |
| 124 | - if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
| 123 | + if ($this->apiUse === true) { |
|
| 124 | + $this->getAPI(); |
|
| 125 | + } |
|
| 126 | + // Setup API |
|
| 125 | 127 | |
| 126 | 128 | //*************** Setup MIB |
| 127 | 129 | $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
@@ -224,8 +226,7 @@ discard block |
||
| 224 | 226 | { |
| 225 | 227 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
| 226 | 228 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
| 227 | - } |
|
| 228 | - else |
|
| 229 | + } else |
|
| 229 | 230 | { |
| 230 | 231 | $this->trapData['source_ip']=$matches[1]; |
| 231 | 232 | $this->trapData['destination_ip']=$matches[3]; |
@@ -240,14 +241,12 @@ discard block |
||
| 240 | 241 | if ($ret_code===0 || $ret_code===false) |
| 241 | 242 | { |
| 242 | 243 | $this->logging->log('No match on trap data : '.$vars,WARN,''); |
| 243 | - } |
|
| 244 | - else |
|
| 244 | + } else |
|
| 245 | 245 | { |
| 246 | 246 | if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
| 247 | 247 | { |
| 248 | 248 | $this->trapData['trap_oid']=$matches[2]; |
| 249 | - } |
|
| 250 | - else |
|
| 249 | + } else |
|
| 251 | 250 | { |
| 252 | 251 | $object= new stdClass; |
| 253 | 252 | $object->oid =$matches[1]; |
@@ -418,7 +417,9 @@ discard block |
||
| 418 | 417 | } |
| 419 | 418 | |
| 420 | 419 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 421 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 420 | + if ($inserted_id==false) { |
|
| 421 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 422 | + } |
|
| 422 | 423 | $this->trapId=$inserted_id; |
| 423 | 424 | break; |
| 424 | 425 | default: |
@@ -434,7 +435,9 @@ discard block |
||
| 434 | 435 | { |
| 435 | 436 | |
| 436 | 437 | // If action is ignore -> don't send t DB |
| 437 | - if ($this->trapToDb === false) return; |
|
| 438 | + if ($this->trapToDb === false) { |
|
| 439 | + return; |
|
| 440 | + } |
|
| 438 | 441 | |
| 439 | 442 | |
| 440 | 443 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -491,7 +494,9 @@ discard block |
||
| 491 | 494 | } |
| 492 | 495 | |
| 493 | 496 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 494 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 497 | + if ($inserted_id==false) { |
|
| 498 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 499 | + } |
|
| 495 | 500 | $this->trapId=$inserted_id; |
| 496 | 501 | break; |
| 497 | 502 | default: |
@@ -620,8 +625,7 @@ discard block |
||
| 620 | 625 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
| 621 | 626 | exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
| 622 | 627 | return true; |
| 623 | - } |
|
| 624 | - else |
|
| 628 | + } else |
|
| 625 | 629 | { |
| 626 | 630 | // Get perfdata if found |
| 627 | 631 | $matches=array(); |
@@ -629,8 +633,7 @@ discard block |
||
| 629 | 633 | { |
| 630 | 634 | $display=$matches[1]; |
| 631 | 635 | $perfdata=$matches[2]; |
| 632 | - } |
|
| 633 | - else |
|
| 636 | + } else |
|
| 634 | 637 | { |
| 635 | 638 | $perfdata=''; |
| 636 | 639 | } |
@@ -642,8 +645,7 @@ discard block |
||
| 642 | 645 | { |
| 643 | 646 | $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
| 644 | 647 | return false; |
| 645 | - } |
|
| 646 | - else |
|
| 648 | + } else |
|
| 647 | 649 | { |
| 648 | 650 | $this->logging->log( "Sent result : " .$retmessage,INFO ); |
| 649 | 651 | return true; |
@@ -742,20 +744,17 @@ discard block |
||
| 742 | 744 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 743 | 745 | { |
| 744 | 746 | $this->trapAction.='Error sending status : check cmd/API'; |
| 745 | - } |
|
| 746 | - else |
|
| 747 | + } else |
|
| 747 | 748 | { |
| 748 | 749 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 749 | 750 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 750 | 751 | } |
| 751 | - } |
|
| 752 | - else |
|
| 752 | + } else |
|
| 753 | 753 | { |
| 754 | 754 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 755 | 755 | } |
| 756 | 756 | $this->trapToDb=($action==-2)?false:true; |
| 757 | - } |
|
| 758 | - else |
|
| 757 | + } else |
|
| 759 | 758 | { |
| 760 | 759 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
| 761 | 760 | |
@@ -766,14 +765,12 @@ discard block |
||
| 766 | 765 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
| 767 | 766 | { |
| 768 | 767 | $this->trapAction.='Error sending status : check cmd/API'; |
| 769 | - } |
|
| 770 | - else |
|
| 768 | + } else |
|
| 771 | 769 | { |
| 772 | 770 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 773 | 771 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 774 | 772 | } |
| 775 | - } |
|
| 776 | - else |
|
| 773 | + } else |
|
| 777 | 774 | { |
| 778 | 775 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 779 | 776 | } |
@@ -783,16 +780,14 @@ discard block |
||
| 783 | 780 | if (!isset($this->trapData['source_name'])) |
| 784 | 781 | { |
| 785 | 782 | $this->trapData['source_name']=$rule['host_name']; |
| 786 | - } |
|
| 787 | - else |
|
| 783 | + } else |
|
| 788 | 784 | { |
| 789 | 785 | if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
| 790 | 786 | { // only add if not present |
| 791 | 787 | $this->trapData['source_name'].=','.$rule['host_name']; |
| 792 | 788 | } |
| 793 | 789 | } |
| 794 | - } |
|
| 795 | - catch (Exception $e) |
|
| 790 | + } catch (Exception $e) |
|
| 796 | 791 | { |
| 797 | 792 | $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
| 798 | 793 | $this->trapAction.=' ERR : '.$e->getMessage(); |
@@ -803,8 +798,7 @@ discard block |
||
| 803 | 798 | if ($this->trapData['status']=='error') |
| 804 | 799 | { |
| 805 | 800 | $this->trapToDb=true; // Always put errors in DB for the use can see |
| 806 | - } |
|
| 807 | - else |
|
| 801 | + } else |
|
| 808 | 802 | { |
| 809 | 803 | $this->trapData['status']='done'; |
| 810 | 804 | } |
@@ -17,839 +17,839 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Trap |
| 19 | 19 | { |
| 20 | - use TrapConfig; |
|
| 20 | + use TrapConfig; |
|
| 21 | 21 | |
| 22 | - // Configuration files and dirs |
|
| 23 | - /** @var string Icinga etc path */ |
|
| 24 | - protected $icingaweb2Etc; |
|
| 25 | - /** @var string $trapModuleConfig config.ini of module */ |
|
| 26 | - protected $trapModuleConfig; |
|
| 27 | - /** @var string $icingaweb2Ressources resources.ini of icingaweb2 */ |
|
| 28 | - protected $icingaweb2Ressources; |
|
| 29 | - // Options from config.ini (default values) |
|
| 30 | - /** @var string $snmptranslate */ |
|
| 31 | - protected $snmptranslate='/usr/bin/snmptranslate'; |
|
| 32 | - /** @var string $snmptranslate_dirs */ |
|
| 33 | - protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
| 34 | - /** @var string $icinga2cmd */ |
|
| 35 | - protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
| 36 | - /** @var string $dbPrefix */ |
|
| 37 | - protected $dbPrefix='traps_'; |
|
| 22 | + // Configuration files and dirs |
|
| 23 | + /** @var string Icinga etc path */ |
|
| 24 | + protected $icingaweb2Etc; |
|
| 25 | + /** @var string $trapModuleConfig config.ini of module */ |
|
| 26 | + protected $trapModuleConfig; |
|
| 27 | + /** @var string $icingaweb2Ressources resources.ini of icingaweb2 */ |
|
| 28 | + protected $icingaweb2Ressources; |
|
| 29 | + // Options from config.ini (default values) |
|
| 30 | + /** @var string $snmptranslate */ |
|
| 31 | + protected $snmptranslate='/usr/bin/snmptranslate'; |
|
| 32 | + /** @var string $snmptranslate_dirs */ |
|
| 33 | + protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
| 34 | + /** @var string $icinga2cmd */ |
|
| 35 | + protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
| 36 | + /** @var string $dbPrefix */ |
|
| 37 | + protected $dbPrefix='traps_'; |
|
| 38 | 38 | |
| 39 | - // API |
|
| 40 | - /** @var boolean $apiUse */ |
|
| 41 | - protected $apiUse=false; |
|
| 42 | - /** @var Icinga2API $icinga2api */ |
|
| 43 | - protected $icinga2api=null; |
|
| 44 | - /** @var string $apiHostname */ |
|
| 45 | - protected $apiHostname=''; |
|
| 46 | - /** @var integer $apiPort */ |
|
| 47 | - protected $apiPort=0; |
|
| 48 | - /** @var string $apiUsername */ |
|
| 49 | - protected $apiUsername=''; |
|
| 50 | - /** @var string $apiPassword */ |
|
| 51 | - protected $apiPassword=''; |
|
| 39 | + // API |
|
| 40 | + /** @var boolean $apiUse */ |
|
| 41 | + protected $apiUse=false; |
|
| 42 | + /** @var Icinga2API $icinga2api */ |
|
| 43 | + protected $icinga2api=null; |
|
| 44 | + /** @var string $apiHostname */ |
|
| 45 | + protected $apiHostname=''; |
|
| 46 | + /** @var integer $apiPort */ |
|
| 47 | + protected $apiPort=0; |
|
| 48 | + /** @var string $apiUsername */ |
|
| 49 | + protected $apiUsername=''; |
|
| 50 | + /** @var string $apiPassword */ |
|
| 51 | + protected $apiPassword=''; |
|
| 52 | 52 | |
| 53 | - // Logs |
|
| 54 | - /** @var Logging Logging class. */ |
|
| 55 | - public $logging; //< Logging class. |
|
| 56 | - /** @var bool true if log was setup in constructor */ |
|
| 57 | - protected $logSetup; //< bool true if log was setup in constructor |
|
| 53 | + // Logs |
|
| 54 | + /** @var Logging Logging class. */ |
|
| 55 | + public $logging; //< Logging class. |
|
| 56 | + /** @var bool true if log was setup in constructor */ |
|
| 57 | + protected $logSetup; //< bool true if log was setup in constructor |
|
| 58 | 58 | |
| 59 | - // Databases |
|
| 60 | - /** @var Database $trapsDB Database class*/ |
|
| 61 | - public $trapsDB = null; |
|
| 59 | + // Databases |
|
| 60 | + /** @var Database $trapsDB Database class*/ |
|
| 61 | + public $trapsDB = null; |
|
| 62 | 62 | |
| 63 | - // Trap received data |
|
| 64 | - protected $receivingHost; |
|
| 65 | - /** @var array Main trap data (oid, source...) */ |
|
| 66 | - public $trapData=array(); |
|
| 67 | - /** @var array $trapDataExt Additional trap data objects (oid/value).*/ |
|
| 68 | - public $trapDataExt=array(); |
|
| 69 | - /** @var int $trapId trap_id after sql insert*/ |
|
| 70 | - public $trapId=null; |
|
| 71 | - /** @var string $trapAction trap action for final write*/ |
|
| 72 | - public $trapAction=null; |
|
| 73 | - /** @var boolean $trapToDb log trap to DB */ |
|
| 74 | - protected $trapToDb=true; |
|
| 63 | + // Trap received data |
|
| 64 | + protected $receivingHost; |
|
| 65 | + /** @var array Main trap data (oid, source...) */ |
|
| 66 | + public $trapData=array(); |
|
| 67 | + /** @var array $trapDataExt Additional trap data objects (oid/value).*/ |
|
| 68 | + public $trapDataExt=array(); |
|
| 69 | + /** @var int $trapId trap_id after sql insert*/ |
|
| 70 | + public $trapId=null; |
|
| 71 | + /** @var string $trapAction trap action for final write*/ |
|
| 72 | + public $trapAction=null; |
|
| 73 | + /** @var boolean $trapToDb log trap to DB */ |
|
| 74 | + protected $trapToDb=true; |
|
| 75 | 75 | |
| 76 | - /** @var Mib mib class */ |
|
| 77 | - public $mibClass = null; |
|
| 76 | + /** @var Mib mib class */ |
|
| 77 | + public $mibClass = null; |
|
| 78 | 78 | |
| 79 | - /** @var Rule rule class */ |
|
| 80 | - public $ruleClass = null; |
|
| 79 | + /** @var Rule rule class */ |
|
| 80 | + public $ruleClass = null; |
|
| 81 | 81 | |
| 82 | - /** @var Plugins plugins manager **/ |
|
| 83 | - public $pluginClass = null; |
|
| 82 | + /** @var Plugins plugins manager **/ |
|
| 83 | + public $pluginClass = null; |
|
| 84 | 84 | |
| 85 | - /** @var TrapApi $trapApiClass */ |
|
| 86 | - public $trapApiClass = null; |
|
| 85 | + /** @var TrapApi $trapApiClass */ |
|
| 86 | + public $trapApiClass = null; |
|
| 87 | 87 | |
| 88 | - function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
| 89 | - { |
|
| 90 | - // Paths of ini files |
|
| 91 | - $this->icingaweb2Etc=$etcDir; |
|
| 92 | - $this->trapModuleConfig=$this->icingaweb2Etc."/modules/trapdirector/config.ini"; |
|
| 93 | - $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
|
| 88 | + function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
| 89 | + { |
|
| 90 | + // Paths of ini files |
|
| 91 | + $this->icingaweb2Etc=$etcDir; |
|
| 92 | + $this->trapModuleConfig=$this->icingaweb2Etc."/modules/trapdirector/config.ini"; |
|
| 93 | + $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
|
| 94 | 94 | |
| 95 | - //************* Setup logging |
|
| 96 | - $this->logging = new Logging(); |
|
| 97 | - if ($baseLogLevel != null) |
|
| 98 | - { |
|
| 99 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 100 | - $this->logSetup=true; |
|
| 101 | - } |
|
| 102 | - else |
|
| 103 | - { |
|
| 104 | - $this->logSetup=false; |
|
| 105 | - } |
|
| 106 | - $this->logging->log('Loggin started', INFO); |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - // Create distributed API object |
|
| 110 | - |
|
| 111 | - $this->trapApiClass = new TrapApi($this->logging); |
|
| 112 | - |
|
| 113 | - //*************** Get options from ini files |
|
| 114 | - if (! is_file($this->trapModuleConfig)) |
|
| 115 | - { |
|
| 116 | - throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
|
| 117 | - } |
|
| 118 | - $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
| 119 | - if ($trapConfig == false) |
|
| 120 | - { |
|
| 121 | - $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
| 122 | - throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
|
| 123 | - } |
|
| 124 | - $this->getMainOptions($trapConfig); // Get main options from ini file |
|
| 125 | - |
|
| 126 | - //*************** Setup database class & get options |
|
| 127 | - $this->setupDatabase($trapConfig); |
|
| 128 | - |
|
| 129 | - $this->getDatabaseOptions(); // Get options in database |
|
| 130 | - |
|
| 131 | - //*************** Setup API |
|
| 132 | - if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
| 133 | - |
|
| 134 | - //*************** Setup MIB |
|
| 135 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
| 136 | - |
|
| 137 | - //*************** Setup Rule |
|
| 138 | - $this->ruleClass = new Rule($this); //< Create Rule class |
|
| 139 | - |
|
| 140 | - $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
| 141 | - 'source_ip' => 'unknown', |
|
| 142 | - 'source_port' => 'unknown', |
|
| 143 | - 'destination_ip' => 'unknown', |
|
| 144 | - 'destination_port' => 'unknown', |
|
| 145 | - 'trap_oid' => 'unknown' |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - //*************** Setup Plugins |
|
| 149 | - //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
| 150 | - $this->pluginClass = new Plugins($this); |
|
| 95 | + //************* Setup logging |
|
| 96 | + $this->logging = new Logging(); |
|
| 97 | + if ($baseLogLevel != null) |
|
| 98 | + { |
|
| 99 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 100 | + $this->logSetup=true; |
|
| 101 | + } |
|
| 102 | + else |
|
| 103 | + { |
|
| 104 | + $this->logSetup=false; |
|
| 105 | + } |
|
| 106 | + $this->logging->log('Loggin started', INFO); |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + // Create distributed API object |
|
| 110 | + |
|
| 111 | + $this->trapApiClass = new TrapApi($this->logging); |
|
| 112 | + |
|
| 113 | + //*************** Get options from ini files |
|
| 114 | + if (! is_file($this->trapModuleConfig)) |
|
| 115 | + { |
|
| 116 | + throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
|
| 117 | + } |
|
| 118 | + $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
| 119 | + if ($trapConfig == false) |
|
| 120 | + { |
|
| 121 | + $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
| 122 | + throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
|
| 123 | + } |
|
| 124 | + $this->getMainOptions($trapConfig); // Get main options from ini file |
|
| 125 | + |
|
| 126 | + //*************** Setup database class & get options |
|
| 127 | + $this->setupDatabase($trapConfig); |
|
| 128 | + |
|
| 129 | + $this->getDatabaseOptions(); // Get options in database |
|
| 130 | + |
|
| 131 | + //*************** Setup API |
|
| 132 | + if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
| 133 | + |
|
| 134 | + //*************** Setup MIB |
|
| 135 | + $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
| 136 | + |
|
| 137 | + //*************** Setup Rule |
|
| 138 | + $this->ruleClass = new Rule($this); //< Create Rule class |
|
| 139 | + |
|
| 140 | + $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
| 141 | + 'source_ip' => 'unknown', |
|
| 142 | + 'source_port' => 'unknown', |
|
| 143 | + 'destination_ip' => 'unknown', |
|
| 144 | + 'destination_port' => 'unknown', |
|
| 145 | + 'trap_oid' => 'unknown' |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + //*************** Setup Plugins |
|
| 149 | + //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
| 150 | + $this->pluginClass = new Plugins($this); |
|
| 151 | 151 | |
| 152 | 152 | |
| 153 | - } |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** @return \Trapdirector\Logging */ |
|
| 156 | - public function getLogging() |
|
| 157 | - { |
|
| 158 | - return $this->logging; |
|
| 159 | - } |
|
| 155 | + /** @return \Trapdirector\Logging */ |
|
| 156 | + public function getLogging() |
|
| 157 | + { |
|
| 158 | + return $this->logging; |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** @return \Trapdirector\TrapApi */ |
|
| 162 | - public function getTrapApi() |
|
| 163 | - { |
|
| 164 | - return $this->trapApiClass; |
|
| 165 | - } |
|
| 161 | + /** @return \Trapdirector\TrapApi */ |
|
| 162 | + public function getTrapApi() |
|
| 163 | + { |
|
| 164 | + return $this->trapApiClass; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** @return \Trapdirector\Database */ |
|
| 168 | - public function getTrapsDB() |
|
| 169 | - { |
|
| 170 | - return $this->trapsDB; |
|
| 171 | - } |
|
| 167 | + /** @return \Trapdirector\Database */ |
|
| 168 | + public function getTrapsDB() |
|
| 169 | + { |
|
| 170 | + return $this->trapsDB; |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - /** OBSOLETE Send log. Throws exception on critical error |
|
| 174 | - * @param string $message Message to log |
|
| 175 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 176 | - * @param string $destination file/syslog/display |
|
| 177 | - * @return void |
|
| 178 | - **/ |
|
| 179 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
| 180 | - { |
|
| 181 | - // TODO : replace ref with $this->logging->log |
|
| 182 | - $this->logging->log($message, $level, $destination); |
|
| 183 | - } |
|
| 173 | + /** OBSOLETE Send log. Throws exception on critical error |
|
| 174 | + * @param string $message Message to log |
|
| 175 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 176 | + * @param string $destination file/syslog/display |
|
| 177 | + * @return void |
|
| 178 | + **/ |
|
| 179 | + public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
| 180 | + { |
|
| 181 | + // TODO : replace ref with $this->logging->log |
|
| 182 | + $this->logging->log($message, $level, $destination); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
| 186 | - { |
|
| 187 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
| 188 | - } |
|
| 185 | + public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
| 186 | + { |
|
| 187 | + $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * Returns or create new IcingaAPI object |
|
| 192 | - * @return \Icinga\Module\Trapdirector\Icinga2API |
|
| 193 | - */ |
|
| 194 | - protected function getAPI() |
|
| 195 | - { |
|
| 196 | - if ($this->icinga2api == null) |
|
| 197 | - { |
|
| 198 | - $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
| 199 | - } |
|
| 200 | - return $this->icinga2api; |
|
| 201 | - } |
|
| 190 | + /** |
|
| 191 | + * Returns or create new IcingaAPI object |
|
| 192 | + * @return \Icinga\Module\Trapdirector\Icinga2API |
|
| 193 | + */ |
|
| 194 | + protected function getAPI() |
|
| 195 | + { |
|
| 196 | + if ($this->icinga2api == null) |
|
| 197 | + { |
|
| 198 | + $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
| 199 | + } |
|
| 200 | + return $this->icinga2api; |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | 203 | |
| 204 | - /** |
|
| 205 | - * read data from stream |
|
| 206 | - * @param $stream string input stream, defaults to "php://stdin" |
|
| 207 | - * @return mixed array trap data or exception with error |
|
| 208 | - */ |
|
| 209 | - public function read_trap($stream='php://stdin') |
|
| 210 | - { |
|
| 211 | - //Read data from snmptrapd from stdin |
|
| 212 | - $input_stream=fopen($stream, 'r'); |
|
| 213 | - |
|
| 214 | - if ($input_stream === false) |
|
| 215 | - { |
|
| 216 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 217 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
| 218 | - return null; // note : exception thrown by logging |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // line 1 : host |
|
| 222 | - $this->receivingHost=chop(fgets($input_stream)); |
|
| 223 | - if ($this->receivingHost === false) |
|
| 224 | - { |
|
| 225 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 226 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
| 227 | - } |
|
| 228 | - // line 2 IP:port=>IP:port |
|
| 229 | - $IP=chop(fgets($input_stream)); |
|
| 230 | - if ($IP === false) |
|
| 231 | - { |
|
| 232 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 233 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
| 234 | - } |
|
| 235 | - $matches=array(); |
|
| 236 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
| 237 | - if ($ret_code===0 || $ret_code===false) |
|
| 238 | - { |
|
| 239 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 240 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
| 241 | - } |
|
| 242 | - else |
|
| 243 | - { |
|
| 244 | - $this->trapData['source_ip']=$matches[1]; |
|
| 245 | - $this->trapData['destination_ip']=$matches[3]; |
|
| 246 | - $this->trapData['source_port']=$matches[2]; |
|
| 247 | - $this->trapData['destination_port']=$matches[4]; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - while (($vars=fgets($input_stream)) !==false) |
|
| 251 | - { |
|
| 252 | - $vars=chop($vars); |
|
| 253 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
| 254 | - if ($ret_code===0 || $ret_code===false) |
|
| 255 | - { |
|
| 256 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
| 257 | - } |
|
| 258 | - else |
|
| 259 | - { |
|
| 260 | - if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
| 261 | - { |
|
| 262 | - $this->trapData['trap_oid']=$matches[2]; |
|
| 263 | - } |
|
| 264 | - else |
|
| 265 | - { |
|
| 266 | - $object= new stdClass; |
|
| 267 | - $object->oid =$matches[1]; |
|
| 268 | - $object->value = $matches[2]; |
|
| 269 | - array_push($this->trapDataExt,$object); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - if ($this->trapData['trap_oid']=='unknown') |
|
| 275 | - { |
|
| 276 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
| 277 | - $this->logging->log('no trap oid found',ERROR,''); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - // Translate oids. |
|
| 281 | - |
|
| 282 | - $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
| 283 | - if ($retArray != null) |
|
| 284 | - { |
|
| 285 | - $this->trapData['trap_name']=$retArray['trap_name']; |
|
| 286 | - $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
| 287 | - } |
|
| 288 | - foreach ($this->trapDataExt as $key => $val) |
|
| 289 | - { |
|
| 290 | - $retArray=$this->translateOID($val->oid); |
|
| 291 | - if ($retArray != null) |
|
| 292 | - { |
|
| 293 | - $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
| 294 | - $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - |
|
| 299 | - $this->trapData['status']= 'waiting'; |
|
| 300 | - |
|
| 301 | - return $this->trapData; |
|
| 302 | - } |
|
| 204 | + /** |
|
| 205 | + * read data from stream |
|
| 206 | + * @param $stream string input stream, defaults to "php://stdin" |
|
| 207 | + * @return mixed array trap data or exception with error |
|
| 208 | + */ |
|
| 209 | + public function read_trap($stream='php://stdin') |
|
| 210 | + { |
|
| 211 | + //Read data from snmptrapd from stdin |
|
| 212 | + $input_stream=fopen($stream, 'r'); |
|
| 213 | + |
|
| 214 | + if ($input_stream === false) |
|
| 215 | + { |
|
| 216 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 217 | + $this->logging->log("Error reading stdin !",ERROR,''); |
|
| 218 | + return null; // note : exception thrown by logging |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // line 1 : host |
|
| 222 | + $this->receivingHost=chop(fgets($input_stream)); |
|
| 223 | + if ($this->receivingHost === false) |
|
| 224 | + { |
|
| 225 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 226 | + $this->logging->log("Error reading Host !",ERROR,''); |
|
| 227 | + } |
|
| 228 | + // line 2 IP:port=>IP:port |
|
| 229 | + $IP=chop(fgets($input_stream)); |
|
| 230 | + if ($IP === false) |
|
| 231 | + { |
|
| 232 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 233 | + $this->logging->log("Error reading IP !",ERROR,''); |
|
| 234 | + } |
|
| 235 | + $matches=array(); |
|
| 236 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
| 237 | + if ($ret_code===0 || $ret_code===false) |
|
| 238 | + { |
|
| 239 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 240 | + $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
| 241 | + } |
|
| 242 | + else |
|
| 243 | + { |
|
| 244 | + $this->trapData['source_ip']=$matches[1]; |
|
| 245 | + $this->trapData['destination_ip']=$matches[3]; |
|
| 246 | + $this->trapData['source_port']=$matches[2]; |
|
| 247 | + $this->trapData['destination_port']=$matches[4]; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + while (($vars=fgets($input_stream)) !==false) |
|
| 251 | + { |
|
| 252 | + $vars=chop($vars); |
|
| 253 | + $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
| 254 | + if ($ret_code===0 || $ret_code===false) |
|
| 255 | + { |
|
| 256 | + $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
| 257 | + } |
|
| 258 | + else |
|
| 259 | + { |
|
| 260 | + if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
| 261 | + { |
|
| 262 | + $this->trapData['trap_oid']=$matches[2]; |
|
| 263 | + } |
|
| 264 | + else |
|
| 265 | + { |
|
| 266 | + $object= new stdClass; |
|
| 267 | + $object->oid =$matches[1]; |
|
| 268 | + $object->value = $matches[2]; |
|
| 269 | + array_push($this->trapDataExt,$object); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + if ($this->trapData['trap_oid']=='unknown') |
|
| 275 | + { |
|
| 276 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
| 277 | + $this->logging->log('no trap oid found',ERROR,''); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + // Translate oids. |
|
| 281 | + |
|
| 282 | + $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
| 283 | + if ($retArray != null) |
|
| 284 | + { |
|
| 285 | + $this->trapData['trap_name']=$retArray['trap_name']; |
|
| 286 | + $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
| 287 | + } |
|
| 288 | + foreach ($this->trapDataExt as $key => $val) |
|
| 289 | + { |
|
| 290 | + $retArray=$this->translateOID($val->oid); |
|
| 291 | + if ($retArray != null) |
|
| 292 | + { |
|
| 293 | + $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
| 294 | + $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + |
|
| 299 | + $this->trapData['status']= 'waiting'; |
|
| 300 | + |
|
| 301 | + return $this->trapData; |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | - /** |
|
| 305 | - * Translate oid into array(MIB,Name) |
|
| 306 | - * @param $oid string oid to translate |
|
| 307 | - * @return mixed : null if not found or array(MIB,Name) |
|
| 308 | - */ |
|
| 309 | - public function translateOID($oid) |
|
| 310 | - { |
|
| 311 | - // try from database |
|
| 312 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 313 | - |
|
| 314 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
| 315 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 316 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 317 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 318 | - } |
|
| 319 | - $name=$ret_code->fetch(); |
|
| 320 | - if ($name['name'] != null) |
|
| 321 | - { |
|
| 322 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - // Also check if it is an instance of OID |
|
| 326 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
| 327 | - |
|
| 328 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
| 329 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 330 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 331 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 332 | - } |
|
| 333 | - $name=$ret_code->fetch(); |
|
| 334 | - if ($name['name'] != null) |
|
| 335 | - { |
|
| 336 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - // Try to get oid name from snmptranslate |
|
| 340 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 341 | - ' '.$oid); |
|
| 342 | - $matches=array(); |
|
| 343 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 344 | - if ($ret_code===0 || $ret_code === false) { |
|
| 345 | - return NULL; |
|
| 346 | - } else { |
|
| 347 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
| 348 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
| 349 | - } |
|
| 350 | - } |
|
| 304 | + /** |
|
| 305 | + * Translate oid into array(MIB,Name) |
|
| 306 | + * @param $oid string oid to translate |
|
| 307 | + * @return mixed : null if not found or array(MIB,Name) |
|
| 308 | + */ |
|
| 309 | + public function translateOID($oid) |
|
| 310 | + { |
|
| 311 | + // try from database |
|
| 312 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 313 | + |
|
| 314 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
| 315 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 316 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 317 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 318 | + } |
|
| 319 | + $name=$ret_code->fetch(); |
|
| 320 | + if ($name['name'] != null) |
|
| 321 | + { |
|
| 322 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + // Also check if it is an instance of OID |
|
| 326 | + $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
| 327 | + |
|
| 328 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
| 329 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 330 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 331 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 332 | + } |
|
| 333 | + $name=$ret_code->fetch(); |
|
| 334 | + if ($name['name'] != null) |
|
| 335 | + { |
|
| 336 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + // Try to get oid name from snmptranslate |
|
| 340 | + $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 341 | + ' '.$oid); |
|
| 342 | + $matches=array(); |
|
| 343 | + $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 344 | + if ($ret_code===0 || $ret_code === false) { |
|
| 345 | + return NULL; |
|
| 346 | + } else { |
|
| 347 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
| 348 | + return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | 351 | |
| 352 | - /** |
|
| 353 | - * Erase old trap records |
|
| 354 | - * @param integer $days : erase traps when more than $days old |
|
| 355 | - * @return integer : number of lines deleted |
|
| 356 | - **/ |
|
| 357 | - public function eraseOldTraps($days=0) |
|
| 358 | - { |
|
| 359 | - if ($days==0) |
|
| 360 | - { |
|
| 361 | - if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
| 362 | - { |
|
| 363 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
| 364 | - return; |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 368 | - $daysago = strtotime("-".$days." day"); |
|
| 369 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
| 370 | - if ($db_conn->query($sql) === false) { |
|
| 371 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
| 372 | - } |
|
| 373 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
| 374 | - } |
|
| 352 | + /** |
|
| 353 | + * Erase old trap records |
|
| 354 | + * @param integer $days : erase traps when more than $days old |
|
| 355 | + * @return integer : number of lines deleted |
|
| 356 | + **/ |
|
| 357 | + public function eraseOldTraps($days=0) |
|
| 358 | + { |
|
| 359 | + if ($days==0) |
|
| 360 | + { |
|
| 361 | + if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
| 362 | + { |
|
| 363 | + $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
| 364 | + return; |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 368 | + $daysago = strtotime("-".$days." day"); |
|
| 369 | + $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
| 370 | + if ($db_conn->query($sql) === false) { |
|
| 371 | + $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
| 372 | + } |
|
| 373 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
| 374 | + } |
|
| 375 | 375 | |
| 376 | - /** Write error to received trap database |
|
| 377 | - */ |
|
| 378 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
| 379 | - { |
|
| 380 | - |
|
| 381 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 382 | - |
|
| 383 | - // add date time |
|
| 384 | - $insert_col ='date_received,status'; |
|
| 385 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 386 | - |
|
| 387 | - if ($sourceIP !=null) |
|
| 388 | - { |
|
| 389 | - $insert_col .=',source_ip'; |
|
| 390 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 391 | - } |
|
| 392 | - if ($trapoid !=null) |
|
| 393 | - { |
|
| 394 | - $insert_col .=',trap_oid'; |
|
| 395 | - $insert_val .=",'". $trapoid ."'"; |
|
| 396 | - } |
|
| 397 | - $insert_col .=',status_detail'; |
|
| 398 | - $insert_val .=",'". $message ."'"; |
|
| 399 | - |
|
| 400 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 401 | - |
|
| 402 | - switch ($this->trapsDB->trapDBType) |
|
| 403 | - { |
|
| 404 | - case 'pgsql': |
|
| 405 | - $sql .= ' RETURNING id;'; |
|
| 406 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 407 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 408 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 409 | - } |
|
| 410 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 411 | - // Get last id to insert oid/values in secondary table |
|
| 412 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 376 | + /** Write error to received trap database |
|
| 377 | + */ |
|
| 378 | + public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
| 379 | + { |
|
| 380 | + |
|
| 381 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 382 | + |
|
| 383 | + // add date time |
|
| 384 | + $insert_col ='date_received,status'; |
|
| 385 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 386 | + |
|
| 387 | + if ($sourceIP !=null) |
|
| 388 | + { |
|
| 389 | + $insert_col .=',source_ip'; |
|
| 390 | + $insert_val .=",'". $sourceIP ."'"; |
|
| 391 | + } |
|
| 392 | + if ($trapoid !=null) |
|
| 393 | + { |
|
| 394 | + $insert_col .=',trap_oid'; |
|
| 395 | + $insert_val .=",'". $trapoid ."'"; |
|
| 396 | + } |
|
| 397 | + $insert_col .=',status_detail'; |
|
| 398 | + $insert_val .=",'". $message ."'"; |
|
| 399 | + |
|
| 400 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 401 | + |
|
| 402 | + switch ($this->trapsDB->trapDBType) |
|
| 403 | + { |
|
| 404 | + case 'pgsql': |
|
| 405 | + $sql .= ' RETURNING id;'; |
|
| 406 | + $this->logging->log('sql : '.$sql,INFO); |
|
| 407 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 408 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 409 | + } |
|
| 410 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 411 | + // Get last id to insert oid/values in secondary table |
|
| 412 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 413 | 413 | |
| 414 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 415 | - } |
|
| 416 | - if (! isset($inserted_id_ret['id'])) { |
|
| 417 | - $this->logging->log('Error getting id',1,''); |
|
| 418 | - } |
|
| 419 | - $this->trapId=$inserted_id_ret['id']; |
|
| 420 | - break; |
|
| 421 | - case 'mysql': |
|
| 422 | - $sql .= ';'; |
|
| 423 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 424 | - if ($db_conn->query($sql) === false) { |
|
| 425 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 426 | - } |
|
| 427 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 428 | - // Get last id to insert oid/values in secondary table |
|
| 429 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 430 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 431 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 432 | - } |
|
| 414 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 415 | + } |
|
| 416 | + if (! isset($inserted_id_ret['id'])) { |
|
| 417 | + $this->logging->log('Error getting id',1,''); |
|
| 418 | + } |
|
| 419 | + $this->trapId=$inserted_id_ret['id']; |
|
| 420 | + break; |
|
| 421 | + case 'mysql': |
|
| 422 | + $sql .= ';'; |
|
| 423 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 424 | + if ($db_conn->query($sql) === false) { |
|
| 425 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 426 | + } |
|
| 427 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 428 | + // Get last id to insert oid/values in secondary table |
|
| 429 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 430 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 431 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 432 | + } |
|
| 433 | 433 | |
| 434 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 435 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 436 | - $this->trapId=$inserted_id; |
|
| 437 | - break; |
|
| 438 | - default: |
|
| 439 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
| 443 | - } |
|
| 434 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 435 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 436 | + $this->trapId=$inserted_id; |
|
| 437 | + break; |
|
| 438 | + default: |
|
| 439 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + $this->logging->log('id found: '. $this->trapId,INFO ); |
|
| 443 | + } |
|
| 444 | 444 | |
| 445 | - /** Write trap data to trap database |
|
| 446 | - */ |
|
| 447 | - public function writeTrapToDB() |
|
| 448 | - { |
|
| 449 | - |
|
| 450 | - // If action is ignore -> don't send t DB |
|
| 451 | - if ($this->trapToDb === false) return; |
|
| 452 | - |
|
| 453 | - |
|
| 454 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 455 | - |
|
| 456 | - $insert_col=''; |
|
| 457 | - $insert_val=''; |
|
| 458 | - // add date time |
|
| 459 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
| 460 | - |
|
| 461 | - $firstcol=1; |
|
| 462 | - foreach ($this->trapData as $col => $val) |
|
| 463 | - { |
|
| 464 | - if ($firstcol==0) |
|
| 465 | - { |
|
| 466 | - $insert_col .=','; |
|
| 467 | - $insert_val .=','; |
|
| 468 | - } |
|
| 469 | - $insert_col .= $col ; |
|
| 470 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 471 | - $firstcol=0; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 475 | - switch ($this->trapsDB->trapDBType) |
|
| 476 | - { |
|
| 477 | - case 'pgsql': |
|
| 478 | - $sql .= ' RETURNING id;'; |
|
| 479 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 480 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 481 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 482 | - } |
|
| 483 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 484 | - // Get last id to insert oid/values in secondary table |
|
| 485 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 445 | + /** Write trap data to trap database |
|
| 446 | + */ |
|
| 447 | + public function writeTrapToDB() |
|
| 448 | + { |
|
| 449 | + |
|
| 450 | + // If action is ignore -> don't send t DB |
|
| 451 | + if ($this->trapToDb === false) return; |
|
| 452 | + |
|
| 453 | + |
|
| 454 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 455 | + |
|
| 456 | + $insert_col=''; |
|
| 457 | + $insert_val=''; |
|
| 458 | + // add date time |
|
| 459 | + $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
| 460 | + |
|
| 461 | + $firstcol=1; |
|
| 462 | + foreach ($this->trapData as $col => $val) |
|
| 463 | + { |
|
| 464 | + if ($firstcol==0) |
|
| 465 | + { |
|
| 466 | + $insert_col .=','; |
|
| 467 | + $insert_val .=','; |
|
| 468 | + } |
|
| 469 | + $insert_col .= $col ; |
|
| 470 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 471 | + $firstcol=0; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 475 | + switch ($this->trapsDB->trapDBType) |
|
| 476 | + { |
|
| 477 | + case 'pgsql': |
|
| 478 | + $sql .= ' RETURNING id;'; |
|
| 479 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 480 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 481 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 482 | + } |
|
| 483 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 484 | + // Get last id to insert oid/values in secondary table |
|
| 485 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 486 | 486 | |
| 487 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 488 | - } |
|
| 489 | - if (! isset($inserted_id_ret['id'])) { |
|
| 490 | - $this->logging->log('Error getting id',ERROR,''); |
|
| 491 | - } |
|
| 492 | - $this->trapId=$inserted_id_ret['id']; |
|
| 493 | - break; |
|
| 494 | - case 'mysql': |
|
| 495 | - $sql .= ';'; |
|
| 496 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 497 | - if ($db_conn->query($sql) === false) { |
|
| 498 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 499 | - } |
|
| 500 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 501 | - // Get last id to insert oid/values in secondary table |
|
| 502 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 503 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 504 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 505 | - } |
|
| 487 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 488 | + } |
|
| 489 | + if (! isset($inserted_id_ret['id'])) { |
|
| 490 | + $this->logging->log('Error getting id',ERROR,''); |
|
| 491 | + } |
|
| 492 | + $this->trapId=$inserted_id_ret['id']; |
|
| 493 | + break; |
|
| 494 | + case 'mysql': |
|
| 495 | + $sql .= ';'; |
|
| 496 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 497 | + if ($db_conn->query($sql) === false) { |
|
| 498 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 499 | + } |
|
| 500 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 501 | + // Get last id to insert oid/values in secondary table |
|
| 502 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 503 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 504 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 505 | + } |
|
| 506 | 506 | |
| 507 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 508 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 509 | - $this->trapId=$inserted_id; |
|
| 510 | - break; |
|
| 511 | - default: |
|
| 512 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
| 513 | - } |
|
| 514 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
| 515 | - |
|
| 516 | - // Fill trap extended data table |
|
| 517 | - foreach ($this->trapDataExt as $value) { |
|
| 518 | - // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
| 519 | - $firstcol=1; |
|
| 520 | - $value->trap_id = $this->trapId; |
|
| 521 | - $insert_col=''; |
|
| 522 | - $insert_val=''; |
|
| 523 | - foreach ($value as $col => $val) |
|
| 524 | - { |
|
| 525 | - if ($firstcol==0) |
|
| 526 | - { |
|
| 527 | - $insert_col .=','; |
|
| 528 | - $insert_val .=','; |
|
| 529 | - } |
|
| 530 | - $insert_col .= $col; |
|
| 531 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 532 | - $firstcol=0; |
|
| 533 | - } |
|
| 507 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 508 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 509 | + $this->trapId=$inserted_id; |
|
| 510 | + break; |
|
| 511 | + default: |
|
| 512 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
| 513 | + } |
|
| 514 | + $this->logging->log('id found: '.$this->trapId,INFO ); |
|
| 515 | + |
|
| 516 | + // Fill trap extended data table |
|
| 517 | + foreach ($this->trapDataExt as $value) { |
|
| 518 | + // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
| 519 | + $firstcol=1; |
|
| 520 | + $value->trap_id = $this->trapId; |
|
| 521 | + $insert_col=''; |
|
| 522 | + $insert_val=''; |
|
| 523 | + foreach ($value as $col => $val) |
|
| 524 | + { |
|
| 525 | + if ($firstcol==0) |
|
| 526 | + { |
|
| 527 | + $insert_col .=','; |
|
| 528 | + $insert_val .=','; |
|
| 529 | + } |
|
| 530 | + $insert_col .= $col; |
|
| 531 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 532 | + $firstcol=0; |
|
| 533 | + } |
|
| 534 | 534 | |
| 535 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
| 535 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
| 536 | 536 | |
| 537 | - if ($db_conn->query($sql) === false) { |
|
| 538 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - } |
|
| 537 | + if ($db_conn->query($sql) === false) { |
|
| 538 | + $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | 542 | |
| 543 | - /** Get rules from rule database with ip and oid |
|
| 544 | - * @param $ip string ipv4 or ipv6 |
|
| 545 | - * @param $oid string oid in numeric |
|
| 546 | - * @return mixed|boolean : PDO object or false |
|
| 547 | - */ |
|
| 548 | - protected function getRules($ip,$oid) |
|
| 549 | - { |
|
| 550 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 551 | - // fetch rules based on IP in rule and OID |
|
| 552 | - $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
| 553 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 554 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 555 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 556 | - return false; |
|
| 557 | - } |
|
| 558 | - $rules_all=$ret_code->fetchAll(); |
|
| 559 | - //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
| 560 | - $rules_ret=array(); |
|
| 561 | - $rule_ret_key=0; |
|
| 562 | - foreach ($rules_all as $key => $rule) |
|
| 563 | - { |
|
| 564 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
| 565 | - { |
|
| 566 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 567 | - //TODO : get host name by API (and check if correct in rule). |
|
| 568 | - $rule_ret_key++; |
|
| 569 | - continue; |
|
| 570 | - } |
|
| 571 | - // TODO : get hosts IP by API |
|
| 572 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
| 573 | - { // get ips of group members by oid |
|
| 574 | - $db_conn2=$this->trapsDB->db_connect_ido(); |
|
| 575 | - $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
| 543 | + /** Get rules from rule database with ip and oid |
|
| 544 | + * @param $ip string ipv4 or ipv6 |
|
| 545 | + * @param $oid string oid in numeric |
|
| 546 | + * @return mixed|boolean : PDO object or false |
|
| 547 | + */ |
|
| 548 | + protected function getRules($ip,$oid) |
|
| 549 | + { |
|
| 550 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 551 | + // fetch rules based on IP in rule and OID |
|
| 552 | + $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
| 553 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 554 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 555 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 556 | + return false; |
|
| 557 | + } |
|
| 558 | + $rules_all=$ret_code->fetchAll(); |
|
| 559 | + //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
| 560 | + $rules_ret=array(); |
|
| 561 | + $rule_ret_key=0; |
|
| 562 | + foreach ($rules_all as $key => $rule) |
|
| 563 | + { |
|
| 564 | + if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
| 565 | + { |
|
| 566 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 567 | + //TODO : get host name by API (and check if correct in rule). |
|
| 568 | + $rule_ret_key++; |
|
| 569 | + continue; |
|
| 570 | + } |
|
| 571 | + // TODO : get hosts IP by API |
|
| 572 | + if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
| 573 | + { // get ips of group members by oid |
|
| 574 | + $db_conn2=$this->trapsDB->db_connect_ido(); |
|
| 575 | + $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
| 576 | 576 | FROM icinga_objects as o |
| 577 | 577 | LEFT JOIN icinga_hostgroups as h ON o.object_id=h.hostgroup_object_id |
| 578 | 578 | LEFT JOIN icinga_hostgroup_members as m ON h.hostgroup_id=m.hostgroup_id |
| 579 | 579 | LEFT JOIN icinga_hosts as a ON a.host_object_id = m.host_object_id |
| 580 | 580 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
| 581 | 581 | WHERE o.name1='".$rule['host_group_name']."';"; |
| 582 | - if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
| 583 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 584 | - continue; |
|
| 585 | - } |
|
| 586 | - $grouphosts=$ret_code2->fetchAll(); |
|
| 587 | - //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
| 588 | - foreach ( $grouphosts as $host) |
|
| 589 | - { |
|
| 590 | - //echo $host['ip4']."\n"; |
|
| 591 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
| 592 | - { |
|
| 593 | - //echo "Rule added \n"; |
|
| 594 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 595 | - $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
| 596 | - $rule_ret_key++; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - } |
|
| 600 | - } |
|
| 601 | - //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
| 602 | - return $rules_ret; |
|
| 603 | - } |
|
| 582 | + if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
| 583 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 584 | + continue; |
|
| 585 | + } |
|
| 586 | + $grouphosts=$ret_code2->fetchAll(); |
|
| 587 | + //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
| 588 | + foreach ( $grouphosts as $host) |
|
| 589 | + { |
|
| 590 | + //echo $host['ip4']."\n"; |
|
| 591 | + if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
| 592 | + { |
|
| 593 | + //echo "Rule added \n"; |
|
| 594 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 595 | + $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
| 596 | + $rule_ret_key++; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + } |
|
| 600 | + } |
|
| 601 | + //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
| 602 | + return $rules_ret; |
|
| 603 | + } |
|
| 604 | 604 | |
| 605 | - /** Add rule match to rule |
|
| 606 | - * @param id int : rule id |
|
| 607 | - * @param set int : value to set |
|
| 608 | - */ |
|
| 609 | - protected function add_rule_match($id, $set) |
|
| 610 | - { |
|
| 611 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 612 | - $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
| 613 | - if ($db_conn->query($sql) === false) { |
|
| 614 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 615 | - } |
|
| 616 | - } |
|
| 605 | + /** Add rule match to rule |
|
| 606 | + * @param id int : rule id |
|
| 607 | + * @param set int : value to set |
|
| 608 | + */ |
|
| 609 | + protected function add_rule_match($id, $set) |
|
| 610 | + { |
|
| 611 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 612 | + $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
| 613 | + if ($db_conn->query($sql) === false) { |
|
| 614 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 615 | + } |
|
| 616 | + } |
|
| 617 | 617 | |
| 618 | - /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
| 619 | - * |
|
| 620 | - * @param string $host |
|
| 621 | - * @param string $service |
|
| 622 | - * @param integer $state numerical staus |
|
| 623 | - * @param string $display |
|
| 624 | - * @returnn bool true is service check was sent without error |
|
| 625 | - */ |
|
| 626 | - public function serviceCheckResult($host,$service,$state,$display) |
|
| 627 | - { |
|
| 628 | - if ($this->apiUse === false) |
|
| 629 | - { |
|
| 630 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 631 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 632 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 618 | + /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
| 619 | + * |
|
| 620 | + * @param string $host |
|
| 621 | + * @param string $service |
|
| 622 | + * @param integer $state numerical staus |
|
| 623 | + * @param string $display |
|
| 624 | + * @returnn bool true is service check was sent without error |
|
| 625 | + */ |
|
| 626 | + public function serviceCheckResult($host,$service,$state,$display) |
|
| 627 | + { |
|
| 628 | + if ($this->apiUse === false) |
|
| 629 | + { |
|
| 630 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 631 | + $host.';' .$service .';' . $state . ';'.$display; |
|
| 632 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 633 | 633 | |
| 634 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 635 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 636 | - return true; |
|
| 637 | - } |
|
| 638 | - else |
|
| 639 | - { |
|
| 640 | - // Get perfdata if found |
|
| 641 | - $matches=array(); |
|
| 642 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 643 | - { |
|
| 644 | - $display=$matches[1]; |
|
| 645 | - $perfdata=$matches[2]; |
|
| 646 | - } |
|
| 647 | - else |
|
| 648 | - { |
|
| 649 | - $perfdata=''; |
|
| 650 | - } |
|
| 634 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 635 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 636 | + return true; |
|
| 637 | + } |
|
| 638 | + else |
|
| 639 | + { |
|
| 640 | + // Get perfdata if found |
|
| 641 | + $matches=array(); |
|
| 642 | + if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 643 | + { |
|
| 644 | + $display=$matches[1]; |
|
| 645 | + $perfdata=$matches[2]; |
|
| 646 | + } |
|
| 647 | + else |
|
| 648 | + { |
|
| 649 | + $perfdata=''; |
|
| 650 | + } |
|
| 651 | 651 | |
| 652 | - $api = $this->getAPI(); |
|
| 653 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
| 654 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 655 | - if ($retcode == false) |
|
| 656 | - { |
|
| 657 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 658 | - return false; |
|
| 659 | - } |
|
| 660 | - else |
|
| 661 | - { |
|
| 662 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 663 | - return true; |
|
| 664 | - } |
|
| 665 | - } |
|
| 666 | - } |
|
| 652 | + $api = $this->getAPI(); |
|
| 653 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
| 654 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 655 | + if ($retcode == false) |
|
| 656 | + { |
|
| 657 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 658 | + return false; |
|
| 659 | + } |
|
| 660 | + else |
|
| 661 | + { |
|
| 662 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 663 | + return true; |
|
| 664 | + } |
|
| 665 | + } |
|
| 666 | + } |
|
| 667 | 667 | |
| 668 | - public function getHostByIP($ip) |
|
| 669 | - { |
|
| 670 | - $api = $this->getAPI(); |
|
| 671 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
| 672 | - return $api->getHostByIP($ip); |
|
| 673 | - } |
|
| 668 | + public function getHostByIP($ip) |
|
| 669 | + { |
|
| 670 | + $api = $this->getAPI(); |
|
| 671 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
| 672 | + return $api->getHostByIP($ip); |
|
| 673 | + } |
|
| 674 | 674 | |
| 675 | - /** Resolve display. |
|
| 676 | - * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 677 | - * @param $display string |
|
| 678 | - * @return string display |
|
| 679 | - */ |
|
| 680 | - protected function applyDisplay($display) |
|
| 681 | - { |
|
| 682 | - $matches=array(); |
|
| 683 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
| 684 | - { |
|
| 685 | - $oid=$matches[1]; |
|
| 686 | - $found=0; |
|
| 687 | - // Test and transform regexp |
|
| 688 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
| 675 | + /** Resolve display. |
|
| 676 | + * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 677 | + * @param $display string |
|
| 678 | + * @return string display |
|
| 679 | + */ |
|
| 680 | + protected function applyDisplay($display) |
|
| 681 | + { |
|
| 682 | + $matches=array(); |
|
| 683 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
| 684 | + { |
|
| 685 | + $oid=$matches[1]; |
|
| 686 | + $found=0; |
|
| 687 | + // Test and transform regexp |
|
| 688 | + $oidR = $this->ruleClass->regexp_eval($oid); |
|
| 689 | 689 | |
| 690 | - foreach($this->trapDataExt as $val) |
|
| 691 | - { |
|
| 692 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 693 | - { |
|
| 694 | - $val->value=preg_replace('/"/','',$val->value); |
|
| 695 | - $rep=0; |
|
| 696 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
| 697 | - if ($rep==0) |
|
| 698 | - { |
|
| 699 | - $this->logging->log("Error in display",WARN,''); |
|
| 700 | - return $display; |
|
| 701 | - } |
|
| 702 | - $found=1; |
|
| 703 | - break; |
|
| 704 | - } |
|
| 705 | - } |
|
| 706 | - if ($found==0) |
|
| 707 | - { |
|
| 708 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
| 709 | - if ($rep==0) |
|
| 710 | - { |
|
| 711 | - $this->logging->log("Error in display",WARN,''); |
|
| 712 | - return $display; |
|
| 713 | - } |
|
| 714 | - } |
|
| 715 | - } |
|
| 716 | - return $display; |
|
| 717 | - } |
|
| 690 | + foreach($this->trapDataExt as $val) |
|
| 691 | + { |
|
| 692 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 693 | + { |
|
| 694 | + $val->value=preg_replace('/"/','',$val->value); |
|
| 695 | + $rep=0; |
|
| 696 | + $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
| 697 | + if ($rep==0) |
|
| 698 | + { |
|
| 699 | + $this->logging->log("Error in display",WARN,''); |
|
| 700 | + return $display; |
|
| 701 | + } |
|
| 702 | + $found=1; |
|
| 703 | + break; |
|
| 704 | + } |
|
| 705 | + } |
|
| 706 | + if ($found==0) |
|
| 707 | + { |
|
| 708 | + $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
| 709 | + if ($rep==0) |
|
| 710 | + { |
|
| 711 | + $this->logging->log("Error in display",WARN,''); |
|
| 712 | + return $display; |
|
| 713 | + } |
|
| 714 | + } |
|
| 715 | + } |
|
| 716 | + return $display; |
|
| 717 | + } |
|
| 718 | 718 | |
| 719 | - /** Match rules for current trap and do action |
|
| 720 | - */ |
|
| 721 | - public function applyRules() |
|
| 722 | - { |
|
| 723 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
| 724 | - |
|
| 725 | - if ($rules===false || count($rules)==0) |
|
| 726 | - { |
|
| 727 | - $this->logging->log('No rules found for this trap',INFO ); |
|
| 728 | - $this->trapData['status']='unknown'; |
|
| 729 | - $this->trapToDb=true; |
|
| 730 | - return; |
|
| 731 | - } |
|
| 732 | - //print_r($rules); |
|
| 733 | - // Evaluate all rules in sequence |
|
| 734 | - $this->trapAction=null; |
|
| 735 | - foreach ($rules as $rule) |
|
| 736 | - { |
|
| 719 | + /** Match rules for current trap and do action |
|
| 720 | + */ |
|
| 721 | + public function applyRules() |
|
| 722 | + { |
|
| 723 | + $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
| 724 | + |
|
| 725 | + if ($rules===false || count($rules)==0) |
|
| 726 | + { |
|
| 727 | + $this->logging->log('No rules found for this trap',INFO ); |
|
| 728 | + $this->trapData['status']='unknown'; |
|
| 729 | + $this->trapToDb=true; |
|
| 730 | + return; |
|
| 731 | + } |
|
| 732 | + //print_r($rules); |
|
| 733 | + // Evaluate all rules in sequence |
|
| 734 | + $this->trapAction=null; |
|
| 735 | + foreach ($rules as $rule) |
|
| 736 | + { |
|
| 737 | 737 | |
| 738 | - $host_name=$rule['host_name']; |
|
| 739 | - $service_name=$rule['service_name']; |
|
| 738 | + $host_name=$rule['host_name']; |
|
| 739 | + $service_name=$rule['service_name']; |
|
| 740 | 740 | |
| 741 | - $display=$this->applyDisplay($rule['display']); |
|
| 742 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
| 743 | - try |
|
| 744 | - { |
|
| 745 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
| 746 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
| 747 | - //->eval_rule($rule['rule']); |
|
| 741 | + $display=$this->applyDisplay($rule['display']); |
|
| 742 | + $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
| 743 | + try |
|
| 744 | + { |
|
| 745 | + $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
| 746 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
| 747 | + //->eval_rule($rule['rule']); |
|
| 748 | 748 | |
| 749 | - if ($evalr == true) |
|
| 750 | - { |
|
| 751 | - //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
| 752 | - $action=$rule['action_match']; |
|
| 753 | - $this->logging->log('action OK : '.$action,INFO ); |
|
| 754 | - if ($action >= 0) |
|
| 755 | - { |
|
| 756 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
| 757 | - { |
|
| 758 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
| 759 | - } |
|
| 760 | - else |
|
| 761 | - { |
|
| 762 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 763 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 764 | - } |
|
| 765 | - } |
|
| 766 | - else |
|
| 767 | - { |
|
| 768 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 769 | - } |
|
| 770 | - $this->trapToDb=($action==-2)?false:true; |
|
| 771 | - } |
|
| 772 | - else |
|
| 773 | - { |
|
| 774 | - //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
| 749 | + if ($evalr == true) |
|
| 750 | + { |
|
| 751 | + //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
| 752 | + $action=$rule['action_match']; |
|
| 753 | + $this->logging->log('action OK : '.$action,INFO ); |
|
| 754 | + if ($action >= 0) |
|
| 755 | + { |
|
| 756 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
| 757 | + { |
|
| 758 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
| 759 | + } |
|
| 760 | + else |
|
| 761 | + { |
|
| 762 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 763 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 764 | + } |
|
| 765 | + } |
|
| 766 | + else |
|
| 767 | + { |
|
| 768 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 769 | + } |
|
| 770 | + $this->trapToDb=($action==-2)?false:true; |
|
| 771 | + } |
|
| 772 | + else |
|
| 773 | + { |
|
| 774 | + //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
| 775 | 775 | |
| 776 | - $action=$rule['action_nomatch']; |
|
| 777 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
| 778 | - if ($action >= 0) |
|
| 779 | - { |
|
| 780 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 781 | - { |
|
| 782 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
| 783 | - } |
|
| 784 | - else |
|
| 785 | - { |
|
| 786 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 787 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 788 | - } |
|
| 789 | - } |
|
| 790 | - else |
|
| 791 | - { |
|
| 792 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 793 | - } |
|
| 794 | - $this->trapToDb=($action==-2)?false:true; |
|
| 795 | - } |
|
| 796 | - // Put name in source_name |
|
| 797 | - if (!isset($this->trapData['source_name'])) |
|
| 798 | - { |
|
| 799 | - $this->trapData['source_name']=$rule['host_name']; |
|
| 800 | - } |
|
| 801 | - else |
|
| 802 | - { |
|
| 803 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
| 804 | - { // only add if not present |
|
| 805 | - $this->trapData['source_name'].=','.$rule['host_name']; |
|
| 806 | - } |
|
| 807 | - } |
|
| 808 | - } |
|
| 809 | - catch (Exception $e) |
|
| 810 | - { |
|
| 811 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 812 | - $this->trapAction.=' ERR : '.$e->getMessage(); |
|
| 813 | - $this->trapData['status']='error'; |
|
| 814 | - } |
|
| 776 | + $action=$rule['action_nomatch']; |
|
| 777 | + $this->logging->log('action NOK : '.$action,INFO ); |
|
| 778 | + if ($action >= 0) |
|
| 779 | + { |
|
| 780 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 781 | + { |
|
| 782 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
| 783 | + } |
|
| 784 | + else |
|
| 785 | + { |
|
| 786 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 787 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 788 | + } |
|
| 789 | + } |
|
| 790 | + else |
|
| 791 | + { |
|
| 792 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 793 | + } |
|
| 794 | + $this->trapToDb=($action==-2)?false:true; |
|
| 795 | + } |
|
| 796 | + // Put name in source_name |
|
| 797 | + if (!isset($this->trapData['source_name'])) |
|
| 798 | + { |
|
| 799 | + $this->trapData['source_name']=$rule['host_name']; |
|
| 800 | + } |
|
| 801 | + else |
|
| 802 | + { |
|
| 803 | + if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
| 804 | + { // only add if not present |
|
| 805 | + $this->trapData['source_name'].=','.$rule['host_name']; |
|
| 806 | + } |
|
| 807 | + } |
|
| 808 | + } |
|
| 809 | + catch (Exception $e) |
|
| 810 | + { |
|
| 811 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 812 | + $this->trapAction.=' ERR : '.$e->getMessage(); |
|
| 813 | + $this->trapData['status']='error'; |
|
| 814 | + } |
|
| 815 | 815 | |
| 816 | - } |
|
| 817 | - if ($this->trapData['status']=='error') |
|
| 818 | - { |
|
| 819 | - $this->trapToDb=true; // Always put errors in DB for the use can see |
|
| 820 | - } |
|
| 821 | - else |
|
| 822 | - { |
|
| 823 | - $this->trapData['status']='done'; |
|
| 824 | - } |
|
| 825 | - } |
|
| 816 | + } |
|
| 817 | + if ($this->trapData['status']=='error') |
|
| 818 | + { |
|
| 819 | + $this->trapToDb=true; // Always put errors in DB for the use can see |
|
| 820 | + } |
|
| 821 | + else |
|
| 822 | + { |
|
| 823 | + $this->trapData['status']='done'; |
|
| 824 | + } |
|
| 825 | + } |
|
| 826 | 826 | |
| 827 | - /** Add Time a action to rule |
|
| 828 | - * @param string $time : time to process to insert in SQL |
|
| 829 | - */ |
|
| 830 | - public function add_rule_final($time) |
|
| 831 | - { |
|
| 832 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 833 | - if ($this->trapAction==null) |
|
| 834 | - { |
|
| 835 | - $this->trapAction='No action'; |
|
| 836 | - } |
|
| 837 | - $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
| 838 | - if ($db_conn->query($sql) === false) { |
|
| 839 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 840 | - } |
|
| 841 | - } |
|
| 827 | + /** Add Time a action to rule |
|
| 828 | + * @param string $time : time to process to insert in SQL |
|
| 829 | + */ |
|
| 830 | + public function add_rule_final($time) |
|
| 831 | + { |
|
| 832 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 833 | + if ($this->trapAction==null) |
|
| 834 | + { |
|
| 835 | + $this->trapAction='No action'; |
|
| 836 | + } |
|
| 837 | + $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
| 838 | + if ($db_conn->query($sql) === false) { |
|
| 839 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 840 | + } |
|
| 841 | + } |
|
| 842 | 842 | |
| 843 | - /*********** UTILITIES *********************/ |
|
| 843 | + /*********** UTILITIES *********************/ |
|
| 844 | 844 | |
| 845 | - /** reset service to OK after time defined in rule |
|
| 846 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 847 | - * @return integer : not in use |
|
| 848 | - **/ |
|
| 849 | - public function reset_services() |
|
| 850 | - { |
|
| 851 | - // Get all services not in 'ok' state |
|
| 852 | - $sql_query="SELECT s.service_object_id, |
|
| 845 | + /** reset service to OK after time defined in rule |
|
| 846 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 847 | + * @return integer : not in use |
|
| 848 | + **/ |
|
| 849 | + public function reset_services() |
|
| 850 | + { |
|
| 851 | + // Get all services not in 'ok' state |
|
| 852 | + $sql_query="SELECT s.service_object_id, |
|
| 853 | 853 | UNIX_TIMESTAMP(s.last_check) AS last_check, |
| 854 | 854 | s.current_state as state, |
| 855 | 855 | v.name1 as host_name, |
@@ -857,43 +857,43 @@ discard block |
||
| 857 | 857 | FROM icinga_servicestatus AS s |
| 858 | 858 | LEFT JOIN icinga_objects as v ON s.service_object_id=v.object_id |
| 859 | 859 | WHERE s.current_state != 0;"; |
| 860 | - $db_conn=$this->trapsDB->db_connect_ido(); |
|
| 861 | - if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
| 862 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 863 | - return 0; |
|
| 864 | - } |
|
| 865 | - $services=$services_db->fetchAll(); |
|
| 866 | - |
|
| 867 | - // Get all rules |
|
| 868 | - $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
| 869 | - $db_conn2=$this->trapsDB->db_connect_trap(); |
|
| 870 | - if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
| 871 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 872 | - return 0; |
|
| 873 | - } |
|
| 874 | - $rules=$rules_db->fetchAll(); |
|
| 875 | - |
|
| 876 | - $now=date('U'); |
|
| 877 | - |
|
| 878 | - $numreset=0; |
|
| 879 | - foreach ($rules as $rule) |
|
| 880 | - { |
|
| 881 | - foreach ($services as $service) |
|
| 882 | - { |
|
| 883 | - if ($service['service_name'] == $rule['service_name'] && |
|
| 884 | - $service['host_name'] == $rule['host_name'] && |
|
| 885 | - ($service['last_check'] + $rule['revert_ok']) < $now) |
|
| 886 | - { |
|
| 887 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 888 | - $numreset++; |
|
| 889 | - } |
|
| 890 | - } |
|
| 891 | - } |
|
| 892 | - echo "\n"; |
|
| 893 | - echo $numreset . " service(s) reset to OK\n"; |
|
| 894 | - return 0; |
|
| 895 | - |
|
| 896 | - } |
|
| 860 | + $db_conn=$this->trapsDB->db_connect_ido(); |
|
| 861 | + if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
| 862 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 863 | + return 0; |
|
| 864 | + } |
|
| 865 | + $services=$services_db->fetchAll(); |
|
| 866 | + |
|
| 867 | + // Get all rules |
|
| 868 | + $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
| 869 | + $db_conn2=$this->trapsDB->db_connect_trap(); |
|
| 870 | + if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
| 871 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 872 | + return 0; |
|
| 873 | + } |
|
| 874 | + $rules=$rules_db->fetchAll(); |
|
| 875 | + |
|
| 876 | + $now=date('U'); |
|
| 877 | + |
|
| 878 | + $numreset=0; |
|
| 879 | + foreach ($rules as $rule) |
|
| 880 | + { |
|
| 881 | + foreach ($services as $service) |
|
| 882 | + { |
|
| 883 | + if ($service['service_name'] == $rule['service_name'] && |
|
| 884 | + $service['host_name'] == $rule['host_name'] && |
|
| 885 | + ($service['last_check'] + $rule['revert_ok']) < $now) |
|
| 886 | + { |
|
| 887 | + $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 888 | + $numreset++; |
|
| 889 | + } |
|
| 890 | + } |
|
| 891 | + } |
|
| 892 | + echo "\n"; |
|
| 893 | + echo $numreset . " service(s) reset to OK\n"; |
|
| 894 | + return 0; |
|
| 895 | + |
|
| 896 | + } |
|
| 897 | 897 | |
| 898 | 898 | |
| 899 | 899 | } |
| 900 | 900 | \ No newline at end of file |
@@ -52,13 +52,13 @@ discard block |
||
| 52 | 52 | |
| 53 | 53 | // Logs |
| 54 | 54 | /** @var Logging Logging class. */ |
| 55 | - public $logging; //< Logging class. |
|
| 55 | + public $logging; //< Logging class. |
|
| 56 | 56 | /** @var bool true if log was setup in constructor */ |
| 57 | - protected $logSetup; //< bool true if log was setup in constructor |
|
| 57 | + protected $logSetup; //< bool true if log was setup in constructor |
|
| 58 | 58 | |
| 59 | 59 | // Databases |
| 60 | 60 | /** @var Database $trapsDB Database class*/ |
| 61 | - public $trapsDB = null; |
|
| 61 | + public $trapsDB=null; |
|
| 62 | 62 | |
| 63 | 63 | // Trap received data |
| 64 | 64 | protected $receivingHost; |
@@ -74,18 +74,18 @@ discard block |
||
| 74 | 74 | protected $trapToDb=true; |
| 75 | 75 | |
| 76 | 76 | /** @var Mib mib class */ |
| 77 | - public $mibClass = null; |
|
| 77 | + public $mibClass=null; |
|
| 78 | 78 | |
| 79 | 79 | /** @var Rule rule class */ |
| 80 | - public $ruleClass = null; |
|
| 80 | + public $ruleClass=null; |
|
| 81 | 81 | |
| 82 | 82 | /** @var Plugins plugins manager **/ |
| 83 | - public $pluginClass = null; |
|
| 83 | + public $pluginClass=null; |
|
| 84 | 84 | |
| 85 | 85 | /** @var TrapApi $trapApiClass */ |
| 86 | - public $trapApiClass = null; |
|
| 86 | + public $trapApiClass=null; |
|
| 87 | 87 | |
| 88 | - function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
| 88 | + function __construct($etcDir='/etc/icingaweb2', $baseLogLevel=null, $baseLogMode='syslog', $baseLogFile='') |
|
| 89 | 89 | { |
| 90 | 90 | // Paths of ini files |
| 91 | 91 | $this->icingaweb2Etc=$etcDir; |
@@ -93,10 +93,10 @@ discard block |
||
| 93 | 93 | $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
| 94 | 94 | |
| 95 | 95 | //************* Setup logging |
| 96 | - $this->logging = new Logging(); |
|
| 96 | + $this->logging=new Logging(); |
|
| 97 | 97 | if ($baseLogLevel != null) |
| 98 | 98 | { |
| 99 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 99 | + $this->logging->setLogging($baseLogLevel, $baseLogMode, $baseLogFile); |
|
| 100 | 100 | $this->logSetup=true; |
| 101 | 101 | } |
| 102 | 102 | else |
@@ -108,17 +108,17 @@ discard block |
||
| 108 | 108 | |
| 109 | 109 | // Create distributed API object |
| 110 | 110 | |
| 111 | - $this->trapApiClass = new TrapApi($this->logging); |
|
| 111 | + $this->trapApiClass=new TrapApi($this->logging); |
|
| 112 | 112 | |
| 113 | 113 | //*************** Get options from ini files |
| 114 | - if (! is_file($this->trapModuleConfig)) |
|
| 114 | + if (!is_file($this->trapModuleConfig)) |
|
| 115 | 115 | { |
| 116 | 116 | throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
| 117 | 117 | } |
| 118 | - $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
| 118 | + $trapConfig=parse_ini_file($this->trapModuleConfig, true); |
|
| 119 | 119 | if ($trapConfig == false) |
| 120 | 120 | { |
| 121 | - $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
| 121 | + $this->logging->log("Error reading ini file : ".$this->trapModuleConfig, ERROR, 'syslog'); |
|
| 122 | 122 | throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
| 123 | 123 | } |
| 124 | 124 | $this->getMainOptions($trapConfig); // Get main options from ini file |
@@ -132,10 +132,10 @@ discard block |
||
| 132 | 132 | if ($this->apiUse === true) $this->getAPI(); // Setup API |
| 133 | 133 | |
| 134 | 134 | //*************** Setup MIB |
| 135 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
| 135 | + $this->mibClass=new Mib($this->logging, $this->trapsDB, $this->snmptranslate, $this->snmptranslate_dirs); // Create Mib class |
|
| 136 | 136 | |
| 137 | 137 | //*************** Setup Rule |
| 138 | - $this->ruleClass = new Rule($this); //< Create Rule class |
|
| 138 | + $this->ruleClass=new Rule($this); //< Create Rule class |
|
| 139 | 139 | |
| 140 | 140 | $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
| 141 | 141 | 'source_ip' => 'unknown', |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | //*************** Setup Plugins |
| 149 | 149 | //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
| 150 | - $this->pluginClass = new Plugins($this); |
|
| 150 | + $this->pluginClass=new Plugins($this); |
|
| 151 | 151 | |
| 152 | 152 | |
| 153 | 153 | } |
@@ -176,15 +176,15 @@ discard block |
||
| 176 | 176 | * @param string $destination file/syslog/display |
| 177 | 177 | * @return void |
| 178 | 178 | **/ |
| 179 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
| 179 | + public function trapLog($message, $level, $destination='') // OBSOLETE |
|
| 180 | 180 | { |
| 181 | 181 | // TODO : replace ref with $this->logging->log |
| 182 | 182 | $this->logging->log($message, $level, $destination); |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
| 185 | + public function setLogging($debugLvl, $outputType, $outputOption=null) // OBSOLETE |
|
| 186 | 186 | { |
| 187 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
| 187 | + $this->logging->setLogging($debugLvl, $outputType, $outputOption); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | { |
| 196 | 196 | if ($this->icinga2api == null) |
| 197 | 197 | { |
| 198 | - $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
| 198 | + $this->icinga2api=new Icinga2API($this->apiHostname, $this->apiPort); |
|
| 199 | 199 | } |
| 200 | 200 | return $this->icinga2api; |
| 201 | 201 | } |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | if ($input_stream === false) |
| 215 | 215 | { |
| 216 | 216 | $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
| 217 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
| 217 | + $this->logging->log("Error reading stdin !", ERROR, ''); |
|
| 218 | 218 | return null; // note : exception thrown by logging |
| 219 | 219 | } |
| 220 | 220 | |
@@ -223,21 +223,21 @@ discard block |
||
| 223 | 223 | if ($this->receivingHost === false) |
| 224 | 224 | { |
| 225 | 225 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
| 226 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
| 226 | + $this->logging->log("Error reading Host !", ERROR, ''); |
|
| 227 | 227 | } |
| 228 | 228 | // line 2 IP:port=>IP:port |
| 229 | 229 | $IP=chop(fgets($input_stream)); |
| 230 | 230 | if ($IP === false) |
| 231 | 231 | { |
| 232 | 232 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
| 233 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
| 233 | + $this->logging->log("Error reading IP !", ERROR, ''); |
|
| 234 | 234 | } |
| 235 | 235 | $matches=array(); |
| 236 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
| 237 | - if ($ret_code===0 || $ret_code===false) |
|
| 236 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches); |
|
| 237 | + if ($ret_code === 0 || $ret_code === false) |
|
| 238 | 238 | { |
| 239 | 239 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
| 240 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
| 240 | + $this->logging->log('Error parsing IP : '.$IP, ERROR, ''); |
|
| 241 | 241 | } |
| 242 | 242 | else |
| 243 | 243 | { |
@@ -247,34 +247,34 @@ discard block |
||
| 247 | 247 | $this->trapData['destination_port']=$matches[4]; |
| 248 | 248 | } |
| 249 | 249 | |
| 250 | - while (($vars=fgets($input_stream)) !==false) |
|
| 250 | + while (($vars=fgets($input_stream)) !== false) |
|
| 251 | 251 | { |
| 252 | 252 | $vars=chop($vars); |
| 253 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
| 254 | - if ($ret_code===0 || $ret_code===false) |
|
| 253 | + $ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches); |
|
| 254 | + if ($ret_code === 0 || $ret_code === false) |
|
| 255 | 255 | { |
| 256 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
| 256 | + $this->logging->log('No match on trap data : '.$vars, WARN, ''); |
|
| 257 | 257 | } |
| 258 | 258 | else |
| 259 | 259 | { |
| 260 | - if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
| 260 | + if (($matches[1] == '.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1] == '.1.3.6.1.6.3.1.1.4.1')) |
|
| 261 | 261 | { |
| 262 | 262 | $this->trapData['trap_oid']=$matches[2]; |
| 263 | 263 | } |
| 264 | 264 | else |
| 265 | 265 | { |
| 266 | - $object= new stdClass; |
|
| 267 | - $object->oid =$matches[1]; |
|
| 268 | - $object->value = $matches[2]; |
|
| 269 | - array_push($this->trapDataExt,$object); |
|
| 266 | + $object=new stdClass; |
|
| 267 | + $object->oid=$matches[1]; |
|
| 268 | + $object->value=$matches[2]; |
|
| 269 | + array_push($this->trapDataExt, $object); |
|
| 270 | 270 | } |
| 271 | 271 | } |
| 272 | 272 | } |
| 273 | 273 | |
| 274 | - if ($this->trapData['trap_oid']=='unknown') |
|
| 274 | + if ($this->trapData['trap_oid'] == 'unknown') |
|
| 275 | 275 | { |
| 276 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
| 277 | - $this->logging->log('no trap oid found',ERROR,''); |
|
| 276 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trapData['source_ip']); |
|
| 277 | + $this->logging->log('no trap oid found', ERROR, ''); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | // Translate oids. |
@@ -296,7 +296,7 @@ discard block |
||
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | |
| 299 | - $this->trapData['status']= 'waiting'; |
|
| 299 | + $this->trapData['status']='waiting'; |
|
| 300 | 300 | |
| 301 | 301 | return $this->trapData; |
| 302 | 302 | } |
@@ -312,40 +312,40 @@ discard block |
||
| 312 | 312 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 313 | 313 | |
| 314 | 314 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
| 315 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 315 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 316 | 316 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 317 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 317 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 318 | 318 | } |
| 319 | 319 | $name=$ret_code->fetch(); |
| 320 | 320 | if ($name['name'] != null) |
| 321 | 321 | { |
| 322 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 322 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | // Also check if it is an instance of OID |
| 326 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
| 326 | + $oid_instance=preg_replace('/\.[0-9]+$/', '', $oid); |
|
| 327 | 327 | |
| 328 | 328 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
| 329 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 329 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 330 | 330 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 331 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 331 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 332 | 332 | } |
| 333 | 333 | $name=$ret_code->fetch(); |
| 334 | 334 | if ($name['name'] != null) |
| 335 | 335 | { |
| 336 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 336 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | // Try to get oid name from snmptranslate |
| 340 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 340 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 341 | 341 | ' '.$oid); |
| 342 | 342 | $matches=array(); |
| 343 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 344 | - if ($ret_code===0 || $ret_code === false) { |
|
| 343 | + $ret_code=preg_match('/(.*)::(.*)/', $translate, $matches); |
|
| 344 | + if ($ret_code === 0 || $ret_code === false) { |
|
| 345 | 345 | return NULL; |
| 346 | 346 | } else { |
| 347 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
| 348 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
| 347 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO); |
|
| 348 | + return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]); |
|
| 349 | 349 | } |
| 350 | 350 | } |
| 351 | 351 | |
@@ -356,90 +356,90 @@ discard block |
||
| 356 | 356 | **/ |
| 357 | 357 | public function eraseOldTraps($days=0) |
| 358 | 358 | { |
| 359 | - if ($days==0) |
|
| 359 | + if ($days == 0) |
|
| 360 | 360 | { |
| 361 | 361 | if (($days=$this->getDBConfig('db_remove_days')) == null) |
| 362 | 362 | { |
| 363 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
| 363 | + $this->logging->log('No days specified & no db value : no tap erase', WARN, ''); |
|
| 364 | 364 | return; |
| 365 | 365 | } |
| 366 | 366 | } |
| 367 | 367 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 368 | - $daysago = strtotime("-".$days." day"); |
|
| 369 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
| 368 | + $daysago=strtotime("-".$days." day"); |
|
| 369 | + $sql='delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';'; |
|
| 370 | 370 | if ($db_conn->query($sql) === false) { |
| 371 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
| 371 | + $this->logging->log('Error erasing traps : '.$sql, ERROR, ''); |
|
| 372 | 372 | } |
| 373 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
| 373 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO); |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | /** Write error to received trap database |
| 377 | 377 | */ |
| 378 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
| 378 | + public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null) |
|
| 379 | 379 | { |
| 380 | 380 | |
| 381 | 381 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 382 | 382 | |
| 383 | 383 | // add date time |
| 384 | - $insert_col ='date_received,status'; |
|
| 385 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 384 | + $insert_col='date_received,status'; |
|
| 385 | + $insert_val="'".date("Y-m-d H:i:s")."','error'"; |
|
| 386 | 386 | |
| 387 | - if ($sourceIP !=null) |
|
| 387 | + if ($sourceIP != null) |
|
| 388 | 388 | { |
| 389 | - $insert_col .=',source_ip'; |
|
| 390 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 389 | + $insert_col.=',source_ip'; |
|
| 390 | + $insert_val.=",'".$sourceIP."'"; |
|
| 391 | 391 | } |
| 392 | - if ($trapoid !=null) |
|
| 392 | + if ($trapoid != null) |
|
| 393 | 393 | { |
| 394 | - $insert_col .=',trap_oid'; |
|
| 395 | - $insert_val .=",'". $trapoid ."'"; |
|
| 394 | + $insert_col.=',trap_oid'; |
|
| 395 | + $insert_val.=",'".$trapoid."'"; |
|
| 396 | 396 | } |
| 397 | - $insert_col .=',status_detail'; |
|
| 398 | - $insert_val .=",'". $message ."'"; |
|
| 397 | + $insert_col.=',status_detail'; |
|
| 398 | + $insert_val.=",'".$message."'"; |
|
| 399 | 399 | |
| 400 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 400 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
| 401 | 401 | |
| 402 | 402 | switch ($this->trapsDB->trapDBType) |
| 403 | 403 | { |
| 404 | 404 | case 'pgsql': |
| 405 | - $sql .= ' RETURNING id;'; |
|
| 406 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 405 | + $sql.=' RETURNING id;'; |
|
| 406 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 407 | 407 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 408 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 408 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
| 409 | 409 | } |
| 410 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 410 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 411 | 411 | // Get last id to insert oid/values in secondary table |
| 412 | 412 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
| 413 | 413 | |
| 414 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 414 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
| 415 | 415 | } |
| 416 | - if (! isset($inserted_id_ret['id'])) { |
|
| 417 | - $this->logging->log('Error getting id',1,''); |
|
| 416 | + if (!isset($inserted_id_ret['id'])) { |
|
| 417 | + $this->logging->log('Error getting id', 1, ''); |
|
| 418 | 418 | } |
| 419 | 419 | $this->trapId=$inserted_id_ret['id']; |
| 420 | 420 | break; |
| 421 | 421 | case 'mysql': |
| 422 | - $sql .= ';'; |
|
| 423 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 422 | + $sql.=';'; |
|
| 423 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 424 | 424 | if ($db_conn->query($sql) === false) { |
| 425 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 425 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
| 426 | 426 | } |
| 427 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 427 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 428 | 428 | // Get last id to insert oid/values in secondary table |
| 429 | 429 | $sql='SELECT LAST_INSERT_ID();'; |
| 430 | 430 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 431 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 431 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
| 432 | 432 | } |
| 433 | 433 | |
| 434 | 434 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 435 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 435 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 436 | 436 | $this->trapId=$inserted_id; |
| 437 | 437 | break; |
| 438 | 438 | default: |
| 439 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 439 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
| 442 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | /** Write trap data to trap database |
@@ -456,86 +456,86 @@ discard block |
||
| 456 | 456 | $insert_col=''; |
| 457 | 457 | $insert_val=''; |
| 458 | 458 | // add date time |
| 459 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
| 459 | + $this->trapData['date_received']=date("Y-m-d H:i:s"); |
|
| 460 | 460 | |
| 461 | 461 | $firstcol=1; |
| 462 | 462 | foreach ($this->trapData as $col => $val) |
| 463 | 463 | { |
| 464 | - if ($firstcol==0) |
|
| 464 | + if ($firstcol == 0) |
|
| 465 | 465 | { |
| 466 | - $insert_col .=','; |
|
| 467 | - $insert_val .=','; |
|
| 466 | + $insert_col.=','; |
|
| 467 | + $insert_val.=','; |
|
| 468 | 468 | } |
| 469 | - $insert_col .= $col ; |
|
| 470 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 469 | + $insert_col.=$col; |
|
| 470 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
| 471 | 471 | $firstcol=0; |
| 472 | 472 | } |
| 473 | 473 | |
| 474 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 474 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
| 475 | 475 | switch ($this->trapsDB->trapDBType) |
| 476 | 476 | { |
| 477 | 477 | case 'pgsql': |
| 478 | - $sql .= ' RETURNING id;'; |
|
| 479 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 478 | + $sql.=' RETURNING id;'; |
|
| 479 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 480 | 480 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 481 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 481 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
| 482 | 482 | } |
| 483 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 483 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 484 | 484 | // Get last id to insert oid/values in secondary table |
| 485 | 485 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
| 486 | 486 | |
| 487 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 487 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
| 488 | 488 | } |
| 489 | - if (! isset($inserted_id_ret['id'])) { |
|
| 490 | - $this->logging->log('Error getting id',ERROR,''); |
|
| 489 | + if (!isset($inserted_id_ret['id'])) { |
|
| 490 | + $this->logging->log('Error getting id', ERROR, ''); |
|
| 491 | 491 | } |
| 492 | 492 | $this->trapId=$inserted_id_ret['id']; |
| 493 | 493 | break; |
| 494 | 494 | case 'mysql': |
| 495 | - $sql .= ';'; |
|
| 496 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 495 | + $sql.=';'; |
|
| 496 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 497 | 497 | if ($db_conn->query($sql) === false) { |
| 498 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 498 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
| 499 | 499 | } |
| 500 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 500 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 501 | 501 | // Get last id to insert oid/values in secondary table |
| 502 | 502 | $sql='SELECT LAST_INSERT_ID();'; |
| 503 | 503 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 504 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 504 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 508 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 508 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 509 | 509 | $this->trapId=$inserted_id; |
| 510 | 510 | break; |
| 511 | 511 | default: |
| 512 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
| 512 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, ''); |
|
| 513 | 513 | } |
| 514 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
| 514 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
| 515 | 515 | |
| 516 | 516 | // Fill trap extended data table |
| 517 | 517 | foreach ($this->trapDataExt as $value) { |
| 518 | 518 | // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
| 519 | 519 | $firstcol=1; |
| 520 | - $value->trap_id = $this->trapId; |
|
| 520 | + $value->trap_id=$this->trapId; |
|
| 521 | 521 | $insert_col=''; |
| 522 | 522 | $insert_val=''; |
| 523 | 523 | foreach ($value as $col => $val) |
| 524 | 524 | { |
| 525 | - if ($firstcol==0) |
|
| 525 | + if ($firstcol == 0) |
|
| 526 | 526 | { |
| 527 | - $insert_col .=','; |
|
| 528 | - $insert_val .=','; |
|
| 527 | + $insert_col.=','; |
|
| 528 | + $insert_val.=','; |
|
| 529 | 529 | } |
| 530 | - $insert_col .= $col; |
|
| 531 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 530 | + $insert_col.=$col; |
|
| 531 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
| 532 | 532 | $firstcol=0; |
| 533 | 533 | } |
| 534 | 534 | |
| 535 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
| 535 | + $sql='INSERT INTO '.$this->dbPrefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');'; |
|
| 536 | 536 | |
| 537 | 537 | if ($db_conn->query($sql) === false) { |
| 538 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
| 538 | + $this->logging->log('Erreur insertion data : '.$sql, WARN, ''); |
|
| 539 | 539 | } |
| 540 | 540 | } |
| 541 | 541 | } |
@@ -545,14 +545,14 @@ discard block |
||
| 545 | 545 | * @param $oid string oid in numeric |
| 546 | 546 | * @return mixed|boolean : PDO object or false |
| 547 | 547 | */ |
| 548 | - protected function getRules($ip,$oid) |
|
| 548 | + protected function getRules($ip, $oid) |
|
| 549 | 549 | { |
| 550 | 550 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 551 | 551 | // fetch rules based on IP in rule and OID |
| 552 | 552 | $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
| 553 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 553 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 554 | 554 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 555 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 555 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
| 556 | 556 | return false; |
| 557 | 557 | } |
| 558 | 558 | $rules_all=$ret_code->fetchAll(); |
@@ -561,7 +561,7 @@ discard block |
||
| 561 | 561 | $rule_ret_key=0; |
| 562 | 562 | foreach ($rules_all as $key => $rule) |
| 563 | 563 | { |
| 564 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
| 564 | + if ($rule['ip4'] == $ip || $rule['ip6'] == $ip) |
|
| 565 | 565 | { |
| 566 | 566 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
| 567 | 567 | //TODO : get host name by API (and check if correct in rule). |
@@ -569,7 +569,7 @@ discard block |
||
| 569 | 569 | continue; |
| 570 | 570 | } |
| 571 | 571 | // TODO : get hosts IP by API |
| 572 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
| 572 | + if (isset($rule['host_group_name']) && $rule['host_group_name'] != null) |
|
| 573 | 573 | { // get ips of group members by oid |
| 574 | 574 | $db_conn2=$this->trapsDB->db_connect_ido(); |
| 575 | 575 | $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
@@ -580,15 +580,15 @@ discard block |
||
| 580 | 580 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
| 581 | 581 | WHERE o.name1='".$rule['host_group_name']."';"; |
| 582 | 582 | if (($ret_code2=$db_conn2->query($sql)) === false) { |
| 583 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 583 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
| 584 | 584 | continue; |
| 585 | 585 | } |
| 586 | 586 | $grouphosts=$ret_code2->fetchAll(); |
| 587 | 587 | //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
| 588 | - foreach ( $grouphosts as $host) |
|
| 588 | + foreach ($grouphosts as $host) |
|
| 589 | 589 | { |
| 590 | 590 | //echo $host['ip4']."\n"; |
| 591 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
| 591 | + if ($host['ip4'] == $ip || $host['ip6'] == $ip) |
|
| 592 | 592 | { |
| 593 | 593 | //echo "Rule added \n"; |
| 594 | 594 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
@@ -611,7 +611,7 @@ discard block |
||
| 611 | 611 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 612 | 612 | $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
| 613 | 613 | if ($db_conn->query($sql) === false) { |
| 614 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 614 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
| 615 | 615 | } |
| 616 | 616 | } |
| 617 | 617 | |
@@ -623,23 +623,23 @@ discard block |
||
| 623 | 623 | * @param string $display |
| 624 | 624 | * @returnn bool true is service check was sent without error |
| 625 | 625 | */ |
| 626 | - public function serviceCheckResult($host,$service,$state,$display) |
|
| 626 | + public function serviceCheckResult($host, $service, $state, $display) |
|
| 627 | 627 | { |
| 628 | 628 | if ($this->apiUse === false) |
| 629 | 629 | { |
| 630 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 631 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 632 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 630 | + $send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'. |
|
| 631 | + $host.';'.$service.';'.$state.';'.$display; |
|
| 632 | + $this->logging->log($send." : to : ".$this->icinga2cmd, INFO); |
|
| 633 | 633 | |
| 634 | 634 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
| 635 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 635 | + exec('echo "'.$send.'" > '.$this->icinga2cmd); |
|
| 636 | 636 | return true; |
| 637 | 637 | } |
| 638 | 638 | else |
| 639 | 639 | { |
| 640 | 640 | // Get perfdata if found |
| 641 | 641 | $matches=array(); |
| 642 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 642 | + if (preg_match('/(.*)\|(.*)/', $display, $matches) == 1) |
|
| 643 | 643 | { |
| 644 | 644 | $display=$matches[1]; |
| 645 | 645 | $perfdata=$matches[2]; |
@@ -649,17 +649,17 @@ discard block |
||
| 649 | 649 | $perfdata=''; |
| 650 | 650 | } |
| 651 | 651 | |
| 652 | - $api = $this->getAPI(); |
|
| 652 | + $api=$this->getAPI(); |
|
| 653 | 653 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
| 654 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 654 | + list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display, $perfdata); |
|
| 655 | 655 | if ($retcode == false) |
| 656 | 656 | { |
| 657 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 657 | + $this->logging->log("Error sending result : ".$retmessage, WARN, ''); |
|
| 658 | 658 | return false; |
| 659 | 659 | } |
| 660 | 660 | else |
| 661 | 661 | { |
| 662 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 662 | + $this->logging->log("Sent result : ".$retmessage, INFO); |
|
| 663 | 663 | return true; |
| 664 | 664 | } |
| 665 | 665 | } |
@@ -667,7 +667,7 @@ discard block |
||
| 667 | 667 | |
| 668 | 668 | public function getHostByIP($ip) |
| 669 | 669 | { |
| 670 | - $api = $this->getAPI(); |
|
| 670 | + $api=$this->getAPI(); |
|
| 671 | 671 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
| 672 | 672 | return $api->getHostByIP($ip); |
| 673 | 673 | } |
@@ -680,35 +680,35 @@ discard block |
||
| 680 | 680 | protected function applyDisplay($display) |
| 681 | 681 | { |
| 682 | 682 | $matches=array(); |
| 683 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
| 683 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $display, $matches) == 1) |
|
| 684 | 684 | { |
| 685 | 685 | $oid=$matches[1]; |
| 686 | 686 | $found=0; |
| 687 | 687 | // Test and transform regexp |
| 688 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
| 688 | + $oidR=$this->ruleClass->regexp_eval($oid); |
|
| 689 | 689 | |
| 690 | - foreach($this->trapDataExt as $val) |
|
| 690 | + foreach ($this->trapDataExt as $val) |
|
| 691 | 691 | { |
| 692 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 692 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
| 693 | 693 | { |
| 694 | - $val->value=preg_replace('/"/','',$val->value); |
|
| 694 | + $val->value=preg_replace('/"/', '', $val->value); |
|
| 695 | 695 | $rep=0; |
| 696 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
| 697 | - if ($rep==0) |
|
| 696 | + $display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep); |
|
| 697 | + if ($rep == 0) |
|
| 698 | 698 | { |
| 699 | - $this->logging->log("Error in display",WARN,''); |
|
| 699 | + $this->logging->log("Error in display", WARN, ''); |
|
| 700 | 700 | return $display; |
| 701 | 701 | } |
| 702 | 702 | $found=1; |
| 703 | 703 | break; |
| 704 | 704 | } |
| 705 | 705 | } |
| 706 | - if ($found==0) |
|
| 706 | + if ($found == 0) |
|
| 707 | 707 | { |
| 708 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
| 709 | - if ($rep==0) |
|
| 708 | + $display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep); |
|
| 709 | + if ($rep == 0) |
|
| 710 | 710 | { |
| 711 | - $this->logging->log("Error in display",WARN,''); |
|
| 711 | + $this->logging->log("Error in display", WARN, ''); |
|
| 712 | 712 | return $display; |
| 713 | 713 | } |
| 714 | 714 | } |
@@ -720,11 +720,11 @@ discard block |
||
| 720 | 720 | */ |
| 721 | 721 | public function applyRules() |
| 722 | 722 | { |
| 723 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
| 723 | + $rules=$this->getRules($this->trapData['source_ip'], $this->trapData['trap_oid']); |
|
| 724 | 724 | |
| 725 | - if ($rules===false || count($rules)==0) |
|
| 725 | + if ($rules === false || count($rules) == 0) |
|
| 726 | 726 | { |
| 727 | - $this->logging->log('No rules found for this trap',INFO ); |
|
| 727 | + $this->logging->log('No rules found for this trap', INFO); |
|
| 728 | 728 | $this->trapData['status']='unknown'; |
| 729 | 729 | $this->trapToDb=true; |
| 730 | 730 | return; |
@@ -739,59 +739,59 @@ discard block |
||
| 739 | 739 | $service_name=$rule['service_name']; |
| 740 | 740 | |
| 741 | 741 | $display=$this->applyDisplay($rule['display']); |
| 742 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
| 742 | + $this->trapAction=($this->trapAction == null) ? '' : $this->trapAction.', '; |
|
| 743 | 743 | try |
| 744 | 744 | { |
| 745 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
| 746 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
| 745 | + $this->logging->log('Rule to eval : '.$rule['rule'], INFO); |
|
| 746 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt); |
|
| 747 | 747 | //->eval_rule($rule['rule']); |
| 748 | 748 | |
| 749 | 749 | if ($evalr == true) |
| 750 | 750 | { |
| 751 | 751 | //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
| 752 | 752 | $action=$rule['action_match']; |
| 753 | - $this->logging->log('action OK : '.$action,INFO ); |
|
| 753 | + $this->logging->log('action OK : '.$action, INFO); |
|
| 754 | 754 | if ($action >= 0) |
| 755 | 755 | { |
| 756 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
| 756 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
| 757 | 757 | { |
| 758 | 758 | $this->trapAction.='Error sending status : check cmd/API'; |
| 759 | 759 | } |
| 760 | 760 | else |
| 761 | 761 | { |
| 762 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 762 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 763 | 763 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 764 | 764 | } |
| 765 | 765 | } |
| 766 | 766 | else |
| 767 | 767 | { |
| 768 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 768 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 769 | 769 | } |
| 770 | - $this->trapToDb=($action==-2)?false:true; |
|
| 770 | + $this->trapToDb=($action == -2) ?false:true; |
|
| 771 | 771 | } |
| 772 | 772 | else |
| 773 | 773 | { |
| 774 | 774 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
| 775 | 775 | |
| 776 | 776 | $action=$rule['action_nomatch']; |
| 777 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
| 777 | + $this->logging->log('action NOK : '.$action, INFO); |
|
| 778 | 778 | if ($action >= 0) |
| 779 | 779 | { |
| 780 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 780 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
| 781 | 781 | { |
| 782 | 782 | $this->trapAction.='Error sending status : check cmd/API'; |
| 783 | 783 | } |
| 784 | 784 | else |
| 785 | 785 | { |
| 786 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 786 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 787 | 787 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 788 | 788 | } |
| 789 | 789 | } |
| 790 | 790 | else |
| 791 | 791 | { |
| 792 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 792 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 793 | 793 | } |
| 794 | - $this->trapToDb=($action==-2)?false:true; |
|
| 794 | + $this->trapToDb=($action == -2) ?false:true; |
|
| 795 | 795 | } |
| 796 | 796 | // Put name in source_name |
| 797 | 797 | if (!isset($this->trapData['source_name'])) |
@@ -800,7 +800,7 @@ discard block |
||
| 800 | 800 | } |
| 801 | 801 | else |
| 802 | 802 | { |
| 803 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
| 803 | + if (!preg_match('/'.$rule['host_name'].'/', $this->trapData['source_name'])) |
|
| 804 | 804 | { // only add if not present |
| 805 | 805 | $this->trapData['source_name'].=','.$rule['host_name']; |
| 806 | 806 | } |
@@ -808,13 +808,13 @@ discard block |
||
| 808 | 808 | } |
| 809 | 809 | catch (Exception $e) |
| 810 | 810 | { |
| 811 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 811 | + $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, ''); |
|
| 812 | 812 | $this->trapAction.=' ERR : '.$e->getMessage(); |
| 813 | 813 | $this->trapData['status']='error'; |
| 814 | 814 | } |
| 815 | 815 | |
| 816 | 816 | } |
| 817 | - if ($this->trapData['status']=='error') |
|
| 817 | + if ($this->trapData['status'] == 'error') |
|
| 818 | 818 | { |
| 819 | 819 | $this->trapToDb=true; // Always put errors in DB for the use can see |
| 820 | 820 | } |
@@ -830,13 +830,13 @@ discard block |
||
| 830 | 830 | public function add_rule_final($time) |
| 831 | 831 | { |
| 832 | 832 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 833 | - if ($this->trapAction==null) |
|
| 833 | + if ($this->trapAction == null) |
|
| 834 | 834 | { |
| 835 | 835 | $this->trapAction='No action'; |
| 836 | 836 | } |
| 837 | 837 | $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
| 838 | 838 | if ($db_conn->query($sql) === false) { |
| 839 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 839 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
| 840 | 840 | } |
| 841 | 841 | } |
| 842 | 842 | |
@@ -859,7 +859,7 @@ discard block |
||
| 859 | 859 | WHERE s.current_state != 0;"; |
| 860 | 860 | $db_conn=$this->trapsDB->db_connect_ido(); |
| 861 | 861 | if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
| 862 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 862 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
| 863 | 863 | return 0; |
| 864 | 864 | } |
| 865 | 865 | $services=$services_db->fetchAll(); |
@@ -868,7 +868,7 @@ discard block |
||
| 868 | 868 | $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
| 869 | 869 | $db_conn2=$this->trapsDB->db_connect_trap(); |
| 870 | 870 | if (($rules_db=$db_conn2->query($sql_query)) === false) { |
| 871 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 871 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
| 872 | 872 | return 0; |
| 873 | 873 | } |
| 874 | 874 | $rules=$rules_db->fetchAll(); |
@@ -884,13 +884,13 @@ discard block |
||
| 884 | 884 | $service['host_name'] == $rule['host_name'] && |
| 885 | 885 | ($service['last_check'] + $rule['revert_ok']) < $now) |
| 886 | 886 | { |
| 887 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 887 | + $this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 888 | 888 | $numreset++; |
| 889 | 889 | } |
| 890 | 890 | } |
| 891 | 891 | } |
| 892 | 892 | echo "\n"; |
| 893 | - echo $numreset . " service(s) reset to OK\n"; |
|
| 893 | + echo $numreset." service(s) reset to OK\n"; |
|
| 894 | 894 | return 0; |
| 895 | 895 | |
| 896 | 896 | } |