@@ -17,10 +17,10 @@ |
||
| 17 | 17 | class StatusCommand extends Command |
| 18 | 18 | { |
| 19 | 19 | /** |
| 20 | - * Get database counts |
|
| 21 | - * |
|
| 22 | - * Get number of traps, rules |
|
| 23 | - */ |
|
| 20 | + * Get database counts |
|
| 21 | + * |
|
| 22 | + * Get number of traps, rules |
|
| 23 | + */ |
|
| 24 | 24 | public function dbAction() |
| 25 | 25 | { |
| 26 | 26 | $db_prefix=$this->Config()->get('config', 'database_prefix'); |
@@ -17,16 +17,16 @@ discard block |
||
| 17 | 17 | class TrapsCommand extends Command |
| 18 | 18 | { |
| 19 | 19 | /** |
| 20 | - * Delete old traps |
|
| 21 | - * |
|
| 22 | - * USAGE |
|
| 23 | - * |
|
| 24 | - * icingali trapdirector traps rotate [options] |
|
| 25 | - * |
|
| 26 | - * OPTIONS |
|
| 27 | - * |
|
| 28 | - * --days remove traps older than <n> days |
|
| 29 | - */ |
|
| 20 | + * Delete old traps |
|
| 21 | + * |
|
| 22 | + * USAGE |
|
| 23 | + * |
|
| 24 | + * icingali trapdirector traps rotate [options] |
|
| 25 | + * |
|
| 26 | + * OPTIONS |
|
| 27 | + * |
|
| 28 | + * --days remove traps older than <n> days |
|
| 29 | + */ |
|
| 30 | 30 | public function rotateAction() |
| 31 | 31 | { |
| 32 | 32 | $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
@@ -56,13 +56,13 @@ discard block |
||
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | - * Reset services to OK state if timeout is passed |
|
| 60 | - * |
|
| 61 | - * USAGE |
|
| 62 | - * |
|
| 63 | - * icingali trapdirector traps reset_services |
|
| 64 | - * |
|
| 65 | - */ |
|
| 59 | + * Reset services to OK state if timeout is passed |
|
| 60 | + * |
|
| 61 | + * USAGE |
|
| 62 | + * |
|
| 63 | + * icingali trapdirector traps reset_services |
|
| 64 | + * |
|
| 65 | + */ |
|
| 66 | 66 | public function resetOKAction() |
| 67 | 67 | { |
| 68 | 68 | $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
@@ -21,13 +21,13 @@ discard block |
||
| 21 | 21 | protected $lastHost; |
| 22 | 22 | // Filters |
| 23 | 23 | |
| 24 | - protected $filter; |
|
| 25 | - protected $enforcedFilters = array(); |
|
| 26 | - protected $searchColumns = array(); |
|
| 24 | + protected $filter; |
|
| 25 | + protected $enforcedFilters = array(); |
|
| 26 | + protected $searchColumns = array(); |
|
| 27 | 27 | |
| 28 | 28 | protected function getTitles() { |
| 29 | 29 | // TODO : check moduleconfig is set |
| 30 | - return $this->moduleConfig->getTrapHostListTitles(); |
|
| 30 | + return $this->moduleConfig->getTrapHostListTitles(); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | // ****************** Render table in html |
@@ -35,45 +35,45 @@ discard block |
||
| 35 | 35 | // Host grouping |
| 36 | 36 | protected function renderHostIfNew($IP,$hostname) |
| 37 | 37 | { |
| 38 | - $view = $this->getView(); |
|
| 38 | + $view = $this->getView(); |
|
| 39 | 39 | |
| 40 | - if ($this->lastHost === $IP) { |
|
| 41 | - return; |
|
| 42 | - } |
|
| 40 | + if ($this->lastHost === $IP) { |
|
| 41 | + return; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - if ($this->lastHost === null) |
|
| 45 | - { |
|
| 46 | - $htm = "<thead>\n <tr>\n"; |
|
| 47 | - } else { |
|
| 48 | - $htm = "</tbody>\n<thead>\n <tr>\n"; |
|
| 49 | - } |
|
| 44 | + if ($this->lastHost === null) |
|
| 45 | + { |
|
| 46 | + $htm = "<thead>\n <tr>\n"; |
|
| 47 | + } else { |
|
| 48 | + $htm = "</tbody>\n<thead>\n <tr>\n"; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - if ($this->columnCount === null) |
|
| 52 | - { |
|
| 53 | - $this->columnCount = count($this->getTitles()); |
|
| 54 | - } |
|
| 51 | + if ($this->columnCount === null) |
|
| 52 | + { |
|
| 53 | + $this->columnCount = count($this->getTitles()); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - $htm .= '<th colspan="' . $this->columnCount . '">' . $view->escape($IP); |
|
| 57 | - if ($hostname != null) |
|
| 58 | - { |
|
| 59 | - $htm .= ' ('.$hostname.')'; |
|
| 60 | - } |
|
| 61 | - $htm .= '</th>' . "\n"; |
|
| 62 | - if ($this->lastHost === null) { |
|
| 63 | - $htm .= " </tr>\n"; |
|
| 64 | - } else { |
|
| 65 | - $htm .= " </tr>\n</thead>\n"; |
|
| 66 | - } |
|
| 56 | + $htm .= '<th colspan="' . $this->columnCount . '">' . $view->escape($IP); |
|
| 57 | + if ($hostname != null) |
|
| 58 | + { |
|
| 59 | + $htm .= ' ('.$hostname.')'; |
|
| 60 | + } |
|
| 61 | + $htm .= '</th>' . "\n"; |
|
| 62 | + if ($this->lastHost === null) { |
|
| 63 | + $htm .= " </tr>\n"; |
|
| 64 | + } else { |
|
| 65 | + $htm .= " </tr>\n</thead>\n"; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - $this->lastHost = $IP; |
|
| 68 | + $this->lastHost = $IP; |
|
| 69 | 69 | |
| 70 | - return $htm . "<tbody>\n"; |
|
| 70 | + return $htm . "<tbody>\n"; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - public function __toString() |
|
| 74 | - { |
|
| 75 | - return $this->render(); |
|
| 76 | - } |
|
| 73 | + public function __toString() |
|
| 74 | + { |
|
| 75 | + return $this->render(); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | 78 | public function render() |
| 79 | 79 | { |
@@ -139,9 +139,9 @@ discard block |
||
| 139 | 139 | |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - public function count() |
|
| 143 | - { |
|
| 144 | - $db=$this->db(); |
|
| 142 | + public function count() |
|
| 143 | + { |
|
| 144 | + $db=$this->db(); |
|
| 145 | 145 | |
| 146 | 146 | $query = $this->getBaseQuery(); |
| 147 | 147 | $this->applyFiltersToQuery($query); |
@@ -149,16 +149,16 @@ discard block |
||
| 149 | 149 | |
| 150 | 150 | return count($values); |
| 151 | 151 | |
| 152 | - //return $db->fetchOne($query); |
|
| 153 | - } |
|
| 152 | + //return $db->fetchOne($query); |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - public function getPaginator() |
|
| 156 | - { |
|
| 157 | - $paginator = new Paginator(); |
|
| 158 | - $paginator->setQuery($this); |
|
| 155 | + public function getPaginator() |
|
| 156 | + { |
|
| 157 | + $paginator = new Paginator(); |
|
| 158 | + $paginator->setQuery($this); |
|
| 159 | 159 | |
| 160 | - return $paginator; |
|
| 161 | - } |
|
| 160 | + return $paginator; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | 163 | // ****************** DB connection and query |
| 164 | 164 | |
@@ -168,24 +168,24 @@ discard block |
||
| 168 | 168 | |
| 169 | 169 | $query = $this->getBaseQuery(); |
| 170 | 170 | $this->applyFiltersToQuery($query); |
| 171 | - if ($this->hasLimit() || $this->hasOffset()) { |
|
| 172 | - $query->limit($this->getLimit(), $this->getOffset()); |
|
| 173 | - } |
|
| 171 | + if ($this->hasLimit() || $this->hasOffset()) { |
|
| 172 | + $query->limit($this->getLimit(), $this->getOffset()); |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | 175 | return $db->fetchAll($query); |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | - public function getBaseQuery() |
|
| 179 | - { |
|
| 178 | + public function getBaseQuery() |
|
| 179 | + { |
|
| 180 | 180 | $db=$this->db(); |
| 181 | 181 | $query = $db->select()->from( |
| 182 | - $this->moduleConfig->getTrapTableName(), |
|
| 183 | - $this->moduleConfig->getTrapHostListDisplayColumns() |
|
| 184 | - )->group(array('t.source_ip','t.source_name','t.trap_oid')) |
|
| 185 | - ->order('t.source_ip'); |
|
| 182 | + $this->moduleConfig->getTrapTableName(), |
|
| 183 | + $this->moduleConfig->getTrapHostListDisplayColumns() |
|
| 184 | + )->group(array('t.source_ip','t.source_name','t.trap_oid')) |
|
| 185 | + ->order('t.source_ip'); |
|
| 186 | 186 | |
| 187 | - return $query; |
|
| 188 | - } |
|
| 187 | + return $query; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | // ****************** Filters |
| 191 | 191 | |
@@ -215,39 +215,39 @@ discard block |
||
| 215 | 215 | $this->filter_done=(isset($filter['done']))?$this->filter_done=$filter['done']:0; |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | - protected function getSearchColumns() |
|
| 219 | - { |
|
| 220 | - return $this->getColumns(); |
|
| 221 | - } |
|
| 218 | + protected function getSearchColumns() |
|
| 219 | + { |
|
| 220 | + return $this->getColumns(); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | 223 | public function getColumns() |
| 224 | 224 | { |
| 225 | 225 | return $this->moduleConfig->getTrapListDisplayColumns(); |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | - public function setFilter($filter) |
|
| 229 | - { |
|
| 230 | - $this->filter = $filter; |
|
| 231 | - return $this; |
|
| 232 | - } |
|
| 228 | + public function setFilter($filter) |
|
| 229 | + { |
|
| 230 | + $this->filter = $filter; |
|
| 231 | + return $this; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | 234 | public function getFilterEditor(Request $request) |
| 235 | - { |
|
| 236 | - $filterEditor = Widget::create('filterEditor') |
|
| 237 | - ->setColumns(array_keys($this->getColumns())) |
|
| 238 | - ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 239 | - ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 240 | - ->ignoreParams('page') |
|
| 241 | - ->handleRequest($request); |
|
| 235 | + { |
|
| 236 | + $filterEditor = Widget::create('filterEditor') |
|
| 237 | + ->setColumns(array_keys($this->getColumns())) |
|
| 238 | + ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 239 | + ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 240 | + ->ignoreParams('page') |
|
| 241 | + ->handleRequest($request); |
|
| 242 | 242 | |
| 243 | - $filter = $filterEditor->getFilter(); |
|
| 244 | - $this->setFilter($filter); |
|
| 243 | + $filter = $filterEditor->getFilter(); |
|
| 244 | + $this->setFilter($filter); |
|
| 245 | 245 | |
| 246 | - return $filterEditor; |
|
| 247 | - } |
|
| 246 | + return $filterEditor; |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - protected function applyFiltersToQuery($query) |
|
| 250 | - { |
|
| 249 | + protected function applyFiltersToQuery($query) |
|
| 250 | + { |
|
| 251 | 251 | |
| 252 | 252 | $sql=''; |
| 253 | 253 | if ($this->filter_query != '') |
@@ -268,8 +268,8 @@ discard block |
||
| 268 | 268 | $sql.="(status != 'done')"; |
| 269 | 269 | } |
| 270 | 270 | if ($sql != '') $query->where($sql); |
| 271 | - return $query; |
|
| 272 | - } |
|
| 271 | + return $query; |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | 274 | } |
| 275 | 275 | |
@@ -14,40 +14,40 @@ |
||
| 14 | 14 | |
| 15 | 15 | if (!array_key_exists('v',$options) || !array_key_exists('c',$options) || !array_key_exists('b',$options)|| !array_key_exists('a',$options)) |
| 16 | 16 | { |
| 17 | - printf("Need version -v, path -a, database -b (mysql,pgsql) command -c (create/update)\n"); |
|
| 18 | - exit(1); |
|
| 17 | + printf("Need version -v, path -a, database -b (mysql,pgsql) command -c (create/update)\n"); |
|
| 18 | + exit(1); |
|
| 19 | 19 | } |
| 20 | 20 | $command=$options['c']; |
| 21 | 21 | $path=$options['a']; |
| 22 | 22 | try { |
| 23 | - switch($command) |
|
| 24 | - { |
|
| 25 | - case 'create': |
|
| 26 | - $schema=($options['b']=='mysql')?'schema_v'.$options['v'].'.sql':'schema_v'.$options['v'].'.pgsql'; |
|
| 27 | - $schema=$path.'/SQL/'.$schema; |
|
| 28 | - $trap->trapsDB->create_schema($schema, 'traps_'); |
|
| 29 | - break; |
|
| 30 | - case 'update': |
|
| 31 | - $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_',true); |
|
| 32 | - printf("Update message : %s\n",$message); |
|
| 33 | - if ($message == 'ERROR') |
|
| 34 | - { |
|
| 35 | - exit(1); |
|
| 36 | - } |
|
| 37 | - printf("Messages DONE, updating : \n"); |
|
| 38 | - $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_'); |
|
| 39 | - if ($message == 'ERROR') |
|
| 40 | - { |
|
| 41 | - exit(1); |
|
| 42 | - } |
|
| 43 | - break; |
|
| 44 | - default: |
|
| 45 | - printf("Unknown command\n"); |
|
| 46 | - exit(1); |
|
| 47 | - } |
|
| 23 | + switch($command) |
|
| 24 | + { |
|
| 25 | + case 'create': |
|
| 26 | + $schema=($options['b']=='mysql')?'schema_v'.$options['v'].'.sql':'schema_v'.$options['v'].'.pgsql'; |
|
| 27 | + $schema=$path.'/SQL/'.$schema; |
|
| 28 | + $trap->trapsDB->create_schema($schema, 'traps_'); |
|
| 29 | + break; |
|
| 30 | + case 'update': |
|
| 31 | + $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_',true); |
|
| 32 | + printf("Update message : %s\n",$message); |
|
| 33 | + if ($message == 'ERROR') |
|
| 34 | + { |
|
| 35 | + exit(1); |
|
| 36 | + } |
|
| 37 | + printf("Messages DONE, updating : \n"); |
|
| 38 | + $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_'); |
|
| 39 | + if ($message == 'ERROR') |
|
| 40 | + { |
|
| 41 | + exit(1); |
|
| 42 | + } |
|
| 43 | + break; |
|
| 44 | + default: |
|
| 45 | + printf("Unknown command\n"); |
|
| 46 | + exit(1); |
|
| 47 | + } |
|
| 48 | 48 | } catch (Exception $e) { |
| 49 | - printf("Caught Exception %s\n",$e->getMessage()); |
|
| 50 | - exit (1); |
|
| 49 | + printf("Caught Exception %s\n",$e->getMessage()); |
|
| 50 | + exit (1); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | exit(0); |
@@ -31,9 +31,9 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | // Filters |
| 33 | 33 | |
| 34 | - protected $filter; |
|
| 35 | - protected $enforcedFilters = array(); |
|
| 36 | - protected $searchColumns = array(); |
|
| 34 | + protected $filter; |
|
| 35 | + protected $enforcedFilters = array(); |
|
| 36 | + protected $searchColumns = array(); |
|
| 37 | 37 | |
| 38 | 38 | protected function getTitles() { |
| 39 | 39 | return $this->moduleConfig->getHandlerListTitles(); |
@@ -45,10 +45,10 @@ discard block |
||
| 45 | 45 | $this->doTranslate=true; |
| 46 | 46 | } |
| 47 | 47 | // ****************** Render table in html |
| 48 | - public function __toString() |
|
| 49 | - { |
|
| 50 | - return $this->render(); |
|
| 51 | - } |
|
| 48 | + public function __toString() |
|
| 49 | + { |
|
| 50 | + return $this->render(); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | 53 | public function render() |
| 54 | 54 | { |
@@ -153,30 +153,30 @@ discard block |
||
| 153 | 153 | |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - public function count() |
|
| 157 | - { |
|
| 158 | - //$db = $this->connection()->getConnection(); |
|
| 159 | - $query = clone($this->getBaseQuery()); |
|
| 160 | - $query->reset('order')->columns(array('COUNT(*)')); |
|
| 161 | - $this->applyFiltersToQuery($query); |
|
| 156 | + public function count() |
|
| 157 | + { |
|
| 158 | + //$db = $this->connection()->getConnection(); |
|
| 159 | + $query = clone($this->getBaseQuery()); |
|
| 160 | + $query->reset('order')->columns(array('COUNT(*)')); |
|
| 161 | + $this->applyFiltersToQuery($query); |
|
| 162 | 162 | |
| 163 | 163 | $db=$this->db(); |
| 164 | 164 | |
| 165 | 165 | $query = $db->select()->from( |
| 166 | - $this->moduleConfig->getTrapRuleName(), |
|
| 167 | - array('COUNT(*)') |
|
| 168 | - ); |
|
| 166 | + $this->moduleConfig->getTrapRuleName(), |
|
| 167 | + array('COUNT(*)') |
|
| 168 | + ); |
|
| 169 | 169 | |
| 170 | - return $db->fetchOne($query); |
|
| 171 | - } |
|
| 170 | + return $db->fetchOne($query); |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - public function getPaginator() |
|
| 174 | - { |
|
| 175 | - $paginator = new Paginator(); |
|
| 176 | - $paginator->setQuery($this); |
|
| 173 | + public function getPaginator() |
|
| 174 | + { |
|
| 175 | + $paginator = new Paginator(); |
|
| 176 | + $paginator->setQuery($this); |
|
| 177 | 177 | |
| 178 | - return $paginator; |
|
| 179 | - } |
|
| 178 | + return $paginator; |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | 181 | // ****************** DB connection and query |
| 182 | 182 | |
@@ -186,63 +186,63 @@ discard block |
||
| 186 | 186 | |
| 187 | 187 | $query = $this->getBaseQuery(); |
| 188 | 188 | |
| 189 | - if ($this->hasLimit() || $this->hasOffset()) { |
|
| 190 | - $query->limit($this->getLimit(), $this->getOffset()); |
|
| 191 | - } |
|
| 189 | + if ($this->hasLimit() || $this->hasOffset()) { |
|
| 190 | + $query->limit($this->getLimit(), $this->getOffset()); |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | return $db->fetchAll($query); |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - public function getBaseQuery() |
|
| 197 | - { |
|
| 196 | + public function getBaseQuery() |
|
| 197 | + { |
|
| 198 | 198 | $db=$this->db(); |
| 199 | 199 | |
| 200 | 200 | $query = $db->select()->from( |
| 201 | - $this->moduleConfig->getTrapRuleName(), |
|
| 202 | - $this->moduleConfig->getHandlerListDisplayColumns() |
|
| 203 | - )->order('host_name DESC,trap_oid DESC'); |
|
| 201 | + $this->moduleConfig->getTrapRuleName(), |
|
| 202 | + $this->moduleConfig->getHandlerListDisplayColumns() |
|
| 203 | + )->order('host_name DESC,trap_oid DESC'); |
|
| 204 | 204 | |
| 205 | - return $query; |
|
| 206 | - } |
|
| 205 | + return $query; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | 208 | // ****************** Filters |
| 209 | 209 | |
| 210 | - protected function getSearchColumns() |
|
| 211 | - { |
|
| 212 | - return $this->getColumns(); |
|
| 213 | - } |
|
| 210 | + protected function getSearchColumns() |
|
| 211 | + { |
|
| 212 | + return $this->getColumns(); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | 215 | public function getColumns() |
| 216 | 216 | { |
| 217 | 217 | return $this->moduleConfig->getHandlerListDisplayColumns(); |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | - public function setFilter($filter) |
|
| 221 | - { |
|
| 222 | - $this->filter = $filter; |
|
| 223 | - return $this; |
|
| 224 | - } |
|
| 220 | + public function setFilter($filter) |
|
| 221 | + { |
|
| 222 | + $this->filter = $filter; |
|
| 223 | + return $this; |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | 226 | public function getFilterEditor(Request $request) |
| 227 | - { |
|
| 228 | - $filterEditor = Widget::create('filterEditor') |
|
| 229 | - ->setColumns(array_keys($this->getColumns())) |
|
| 230 | - ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 231 | - ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 232 | - ->ignoreParams('page') |
|
| 233 | - ->handleRequest($request); |
|
| 227 | + { |
|
| 228 | + $filterEditor = Widget::create('filterEditor') |
|
| 229 | + ->setColumns(array_keys($this->getColumns())) |
|
| 230 | + ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 231 | + ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 232 | + ->ignoreParams('page') |
|
| 233 | + ->handleRequest($request); |
|
| 234 | 234 | |
| 235 | - $filter = $filterEditor->getFilter(); |
|
| 236 | - $this->setFilter($filter); |
|
| 235 | + $filter = $filterEditor->getFilter(); |
|
| 236 | + $this->setFilter($filter); |
|
| 237 | 237 | |
| 238 | - return $filterEditor; |
|
| 239 | - } |
|
| 238 | + return $filterEditor; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - protected function applyFiltersToQuery($query) |
|
| 242 | - { |
|
| 241 | + protected function applyFiltersToQuery($query) |
|
| 242 | + { |
|
| 243 | 243 | // TODO : implement |
| 244 | 244 | |
| 245 | - /*$filter = null; |
|
| 245 | + /*$filter = null; |
|
| 246 | 246 | $enforced = $this->enforcedFilters; |
| 247 | 247 | if ($this->filter && ! $this->filter->isEmpty()) { |
| 248 | 248 | $filter = $this->filter; |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | } |
| 258 | 258 | */ |
| 259 | 259 | |
| 260 | - return $query; |
|
| 261 | - } |
|
| 260 | + return $query; |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | 263 | } |
@@ -9,113 +9,113 @@ |
||
| 9 | 9 | class Logging |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - //**** Options from config database |
|
| 13 | - // Default values |
|
| 14 | - public $debugLevel=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
| 15 | - public $outputMode='syslog'; // alert type : file, syslog, display |
|
| 16 | - public $outputFile="/tmp/trapdebug.txt"; |
|
| 17 | - protected $logLevels=array("","Error","Warning","Info","Debug"); |
|
| 18 | - protected $outputList=array('file', 'syslog', 'display'); |
|
| 12 | + //**** Options from config database |
|
| 13 | + // Default values |
|
| 14 | + public $debugLevel=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
| 15 | + public $outputMode='syslog'; // alert type : file, syslog, display |
|
| 16 | + public $outputFile="/tmp/trapdebug.txt"; |
|
| 17 | + protected $logLevels=array("","Error","Warning","Info","Debug"); |
|
| 18 | + protected $outputList=array('file', 'syslog', 'display'); |
|
| 19 | 19 | |
| 20 | - /** Send log. Throws exception on critical error |
|
| 21 | - * @param string $message Message to log |
|
| 22 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 23 | - * @param string $destination file/syslog/display |
|
| 24 | - * @return void |
|
| 25 | - * @throws Exception |
|
| 26 | - **/ |
|
| 27 | - public function log( $message, $level, $destination ='') |
|
| 28 | - { |
|
| 29 | - if ($this->debugLevel >= $level) |
|
| 30 | - { |
|
| 31 | - $date = '['. date("Y/m/d H:i:s") . '] '; // no date in syslog as already there |
|
| 32 | - $message = '[TrapDirector] ['.$this->logLevels[$level].']: ' .$message . "\n"; |
|
| 20 | + /** Send log. Throws exception on critical error |
|
| 21 | + * @param string $message Message to log |
|
| 22 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 23 | + * @param string $destination file/syslog/display |
|
| 24 | + * @return void |
|
| 25 | + * @throws Exception |
|
| 26 | + **/ |
|
| 27 | + public function log( $message, $level, $destination ='') |
|
| 28 | + { |
|
| 29 | + if ($this->debugLevel >= $level) |
|
| 30 | + { |
|
| 31 | + $date = '['. date("Y/m/d H:i:s") . '] '; // no date in syslog as already there |
|
| 32 | + $message = '[TrapDirector] ['.$this->logLevels[$level].']: ' .$message . "\n"; |
|
| 33 | 33 | |
| 34 | - $output = ( $destination != '' ) ? $destination : $this->outputMode; |
|
| 35 | - switch ($output) |
|
| 36 | - { |
|
| 37 | - case 'file': |
|
| 38 | - file_put_contents ($this->outputFile, $date.$message , FILE_APPEND); |
|
| 39 | - break; |
|
| 40 | - case 'syslog': |
|
| 41 | - switch($level) |
|
| 42 | - { |
|
| 43 | - case 1 : $prio = LOG_ERR;break; |
|
| 44 | - case 2 : $prio = LOG_WARNING;break; |
|
| 45 | - case 3 : $prio = LOG_INFO;break; |
|
| 46 | - case 4 : $prio = LOG_INFO;break; // LOG_DEBUG isn't always displayed in syslog |
|
| 47 | - default: $prio = LOG_ERR; |
|
| 48 | - } |
|
| 49 | - syslog($prio,$message); |
|
| 50 | - break; |
|
| 51 | - case 'display': |
|
| 52 | - echo $date.$message; |
|
| 53 | - break; |
|
| 54 | - default : // nothing we can do at this point |
|
| 55 | - throw new Exception($date.$message); |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - if ($level == 1) |
|
| 59 | - { |
|
| 60 | - throw new Exception($message); |
|
| 61 | - } |
|
| 62 | - } |
|
| 34 | + $output = ( $destination != '' ) ? $destination : $this->outputMode; |
|
| 35 | + switch ($output) |
|
| 36 | + { |
|
| 37 | + case 'file': |
|
| 38 | + file_put_contents ($this->outputFile, $date.$message , FILE_APPEND); |
|
| 39 | + break; |
|
| 40 | + case 'syslog': |
|
| 41 | + switch($level) |
|
| 42 | + { |
|
| 43 | + case 1 : $prio = LOG_ERR;break; |
|
| 44 | + case 2 : $prio = LOG_WARNING;break; |
|
| 45 | + case 3 : $prio = LOG_INFO;break; |
|
| 46 | + case 4 : $prio = LOG_INFO;break; // LOG_DEBUG isn't always displayed in syslog |
|
| 47 | + default: $prio = LOG_ERR; |
|
| 48 | + } |
|
| 49 | + syslog($prio,$message); |
|
| 50 | + break; |
|
| 51 | + case 'display': |
|
| 52 | + echo $date.$message; |
|
| 53 | + break; |
|
| 54 | + default : // nothing we can do at this point |
|
| 55 | + throw new Exception($date.$message); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + if ($level == 1) |
|
| 59 | + { |
|
| 60 | + throw new Exception($message); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | |
| 65 | - public function setLogging($debugLvl,$outputType,$outputFile=null) |
|
| 66 | - { |
|
| 67 | - $this->setLevel($debugLvl); |
|
| 68 | - switch ($outputType) |
|
| 69 | - { |
|
| 70 | - case 'file': |
|
| 71 | - if ($outputFile == null) throw new Exception("File logging without file !"); |
|
| 72 | - $this->setFile($outputFile); |
|
| 73 | - $this->setDestination('file'); |
|
| 74 | - break; |
|
| 75 | - default: |
|
| 76 | - $this->setDestination($outputType); |
|
| 77 | - } |
|
| 78 | - } |
|
| 65 | + public function setLogging($debugLvl,$outputType,$outputFile=null) |
|
| 66 | + { |
|
| 67 | + $this->setLevel($debugLvl); |
|
| 68 | + switch ($outputType) |
|
| 69 | + { |
|
| 70 | + case 'file': |
|
| 71 | + if ($outputFile == null) throw new Exception("File logging without file !"); |
|
| 72 | + $this->setFile($outputFile); |
|
| 73 | + $this->setDestination('file'); |
|
| 74 | + break; |
|
| 75 | + default: |
|
| 76 | + $this->setDestination($outputType); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Set logging level |
|
| 82 | - * @param integer $level |
|
| 83 | - * @throws Exception |
|
| 84 | - */ |
|
| 85 | - public function setLevel($level) |
|
| 86 | - { |
|
| 87 | - if (!is_integer($level) || $level < 0 || $level > 10) |
|
| 88 | - { |
|
| 89 | - throw new Exception('Invalid log level'); |
|
| 90 | - } |
|
| 91 | - $this->debugLevel=$level; |
|
| 92 | - } |
|
| 80 | + /** |
|
| 81 | + * Set logging level |
|
| 82 | + * @param integer $level |
|
| 83 | + * @throws Exception |
|
| 84 | + */ |
|
| 85 | + public function setLevel($level) |
|
| 86 | + { |
|
| 87 | + if (!is_integer($level) || $level < 0 || $level > 10) |
|
| 88 | + { |
|
| 89 | + throw new Exception('Invalid log level'); |
|
| 90 | + } |
|
| 91 | + $this->debugLevel=$level; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Set logging destination |
|
| 96 | - * @param string $destination |
|
| 97 | - * @throws Exception |
|
| 98 | - */ |
|
| 99 | - public function setDestination($destination) |
|
| 100 | - { |
|
| 101 | - if (!is_string($destination) || ! in_array($destination, $this->outputList)) |
|
| 102 | - { |
|
| 103 | - throw new Exception('Invalid log destination'); |
|
| 104 | - } |
|
| 105 | - $this->outputMode=$destination; |
|
| 106 | - } |
|
| 107 | - /** |
|
| 108 | - * Set file destination |
|
| 109 | - * @param string $file |
|
| 110 | - * @throws Exception |
|
| 111 | - */ |
|
| 112 | - public function setFile($file) |
|
| 113 | - { |
|
| 114 | - if (!is_string($file)) |
|
| 115 | - { |
|
| 116 | - throw new Exception('Invalid log file'); |
|
| 117 | - } |
|
| 118 | - $this->outputFile=$file; |
|
| 119 | - } |
|
| 94 | + /** |
|
| 95 | + * Set logging destination |
|
| 96 | + * @param string $destination |
|
| 97 | + * @throws Exception |
|
| 98 | + */ |
|
| 99 | + public function setDestination($destination) |
|
| 100 | + { |
|
| 101 | + if (!is_string($destination) || ! in_array($destination, $this->outputList)) |
|
| 102 | + { |
|
| 103 | + throw new Exception('Invalid log destination'); |
|
| 104 | + } |
|
| 105 | + $this->outputMode=$destination; |
|
| 106 | + } |
|
| 107 | + /** |
|
| 108 | + * Set file destination |
|
| 109 | + * @param string $file |
|
| 110 | + * @throws Exception |
|
| 111 | + */ |
|
| 112 | + public function setFile($file) |
|
| 113 | + { |
|
| 114 | + if (!is_string($file)) |
|
| 115 | + { |
|
| 116 | + throw new Exception('Invalid log file'); |
|
| 117 | + } |
|
| 118 | + $this->outputFile=$file; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | } |
| 122 | 122 | \ No newline at end of file |
@@ -9,383 +9,383 @@ |
||
| 9 | 9 | class Database |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - // Databases |
|
| 13 | - /** @var \PDO $trapDB trap database */ |
|
| 14 | - protected $trapDB=null; |
|
| 15 | - protected $idoDB=null; //< ido database |
|
| 16 | - public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 17 | - public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 12 | + // Databases |
|
| 13 | + /** @var \PDO $trapDB trap database */ |
|
| 14 | + protected $trapDB=null; |
|
| 15 | + protected $idoDB=null; //< ido database |
|
| 16 | + public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 17 | + public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 18 | 18 | |
| 19 | - protected $trapDSN; //< trap database connection params |
|
| 20 | - protected $trapUsername; //< trap database connection params |
|
| 21 | - protected $trapPass; //< trap database connection params |
|
| 22 | - public $dbPrefix; //< database tables prefix |
|
| 19 | + protected $trapDSN; //< trap database connection params |
|
| 20 | + protected $trapUsername; //< trap database connection params |
|
| 21 | + protected $trapPass; //< trap database connection params |
|
| 22 | + public $dbPrefix; //< database tables prefix |
|
| 23 | 23 | |
| 24 | - protected $idoSet; //< bool true is ido database set |
|
| 25 | - protected $idoDSN; //< trap database connection params |
|
| 26 | - protected $idoUsername; //< trap database connection params |
|
| 27 | - protected $idoPass; //< trap database connection params |
|
| 24 | + protected $idoSet; //< bool true is ido database set |
|
| 25 | + protected $idoDSN; //< trap database connection params |
|
| 26 | + protected $idoUsername; //< trap database connection params |
|
| 27 | + protected $idoPass; //< trap database connection params |
|
| 28 | 28 | |
| 29 | - // Logging function |
|
| 29 | + // Logging function |
|
| 30 | 30 | |
| 31 | - protected $logging; //< logging class |
|
| 31 | + protected $logging; //< logging class |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @param Logging $logClass : where to log |
|
| 35 | - * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 36 | - */ |
|
| 37 | - function __construct($logClass,$dbParam,$dbPrefix) |
|
| 38 | - { |
|
| 39 | - $this->logging=$logClass; |
|
| 40 | - $this->dbPrefix=$dbPrefix; |
|
| 33 | + /** |
|
| 34 | + * @param Logging $logClass : where to log |
|
| 35 | + * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 36 | + */ |
|
| 37 | + function __construct($logClass,$dbParam,$dbPrefix) |
|
| 38 | + { |
|
| 39 | + $this->logging=$logClass; |
|
| 40 | + $this->dbPrefix=$dbPrefix; |
|
| 41 | 41 | |
| 42 | - $this->trapDSN=$this->setupDSN($dbParam); |
|
| 43 | - $this->trapUsername = $dbParam['username']; |
|
| 44 | - $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 45 | - $this->trapDBType=$dbParam['db']; |
|
| 46 | - $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 47 | - $this->db_connect_trap(); |
|
| 42 | + $this->trapDSN=$this->setupDSN($dbParam); |
|
| 43 | + $this->trapUsername = $dbParam['username']; |
|
| 44 | + $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 45 | + $this->trapDBType=$dbParam['db']; |
|
| 46 | + $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 47 | + $this->db_connect_trap(); |
|
| 48 | 48 | |
| 49 | - } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Setup and connect to IDO database |
|
| 53 | - * @param array $dbParam : array of named params |
|
| 54 | - */ |
|
| 55 | - public function setupIDO($dbParam) |
|
| 56 | - { |
|
| 57 | - $this->idoDSN=$this->setupDSN($dbParam); |
|
| 58 | - $this->idoUsername = $dbParam['username']; |
|
| 59 | - $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 60 | - $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 61 | - $this->idoDBType=$dbParam['db']; |
|
| 62 | - $this->db_connect_ido(); |
|
| 63 | - } |
|
| 51 | + /** |
|
| 52 | + * Setup and connect to IDO database |
|
| 53 | + * @param array $dbParam : array of named params |
|
| 54 | + */ |
|
| 55 | + public function setupIDO($dbParam) |
|
| 56 | + { |
|
| 57 | + $this->idoDSN=$this->setupDSN($dbParam); |
|
| 58 | + $this->idoUsername = $dbParam['username']; |
|
| 59 | + $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 60 | + $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 61 | + $this->idoDBType=$dbParam['db']; |
|
| 62 | + $this->db_connect_ido(); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Connect to IDO database |
|
| 67 | - * @return \PDO |
|
| 68 | - */ |
|
| 69 | - public function db_connect_ido() |
|
| 70 | - { |
|
| 71 | - if ($this->idoDB != null) { |
|
| 72 | - // Check if connection is still alive |
|
| 73 | - try { |
|
| 74 | - $this->idoDB->query('select 1')->fetchColumn(); |
|
| 75 | - return $this->idoDB; |
|
| 76 | - } catch (Exception $e) { |
|
| 77 | - // select 1 failed, try to reconnect. |
|
| 78 | - $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - try { |
|
| 82 | - $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 83 | - } catch (PDOException $e) { |
|
| 84 | - $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 85 | - } |
|
| 86 | - return $this->idoDB; |
|
| 87 | - } |
|
| 65 | + /** |
|
| 66 | + * Connect to IDO database |
|
| 67 | + * @return \PDO |
|
| 68 | + */ |
|
| 69 | + public function db_connect_ido() |
|
| 70 | + { |
|
| 71 | + if ($this->idoDB != null) { |
|
| 72 | + // Check if connection is still alive |
|
| 73 | + try { |
|
| 74 | + $this->idoDB->query('select 1')->fetchColumn(); |
|
| 75 | + return $this->idoDB; |
|
| 76 | + } catch (Exception $e) { |
|
| 77 | + // select 1 failed, try to reconnect. |
|
| 78 | + $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + try { |
|
| 82 | + $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 83 | + } catch (PDOException $e) { |
|
| 84 | + $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 85 | + } |
|
| 86 | + return $this->idoDB; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Connect to Trap database |
|
| 91 | - * @return \PDO |
|
| 92 | - */ |
|
| 93 | - public function db_connect_trap() |
|
| 94 | - { |
|
| 95 | - if ($this->trapDB != null) { |
|
| 96 | - // Check if connection is still alive |
|
| 97 | - try { |
|
| 98 | - $this->trapDB->query('select 1')->fetchColumn(); |
|
| 99 | - return $this->trapDB; |
|
| 100 | - } catch (Exception $e) { |
|
| 101 | - // select 1 failed, try to reconnect. |
|
| 102 | - $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - try { |
|
| 106 | - $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 107 | - } catch (PDOException $e) { |
|
| 108 | - $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 109 | - } |
|
| 110 | - return $this->trapDB; |
|
| 111 | - } |
|
| 89 | + /** |
|
| 90 | + * Connect to Trap database |
|
| 91 | + * @return \PDO |
|
| 92 | + */ |
|
| 93 | + public function db_connect_trap() |
|
| 94 | + { |
|
| 95 | + if ($this->trapDB != null) { |
|
| 96 | + // Check if connection is still alive |
|
| 97 | + try { |
|
| 98 | + $this->trapDB->query('select 1')->fetchColumn(); |
|
| 99 | + return $this->trapDB; |
|
| 100 | + } catch (Exception $e) { |
|
| 101 | + // select 1 failed, try to reconnect. |
|
| 102 | + $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + try { |
|
| 106 | + $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 107 | + } catch (PDOException $e) { |
|
| 108 | + $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 109 | + } |
|
| 110 | + return $this->trapDB; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Setup dsn and check parameters |
|
| 115 | - * @param array $configElmt |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - protected function setupDSN($configElmt) |
|
| 119 | - { |
|
| 120 | - if (!array_key_exists('db',$configElmt) || |
|
| 121 | - !array_key_exists('host',$configElmt) || |
|
| 122 | - !array_key_exists('dbname',$configElmt) || |
|
| 123 | - !array_key_exists('username',$configElmt)) |
|
| 124 | - { |
|
| 125 | - $this->logging->log('Missing DB params',ERROR); |
|
| 126 | - return ''; |
|
| 127 | - } |
|
| 113 | + /** |
|
| 114 | + * Setup dsn and check parameters |
|
| 115 | + * @param array $configElmt |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + protected function setupDSN($configElmt) |
|
| 119 | + { |
|
| 120 | + if (!array_key_exists('db',$configElmt) || |
|
| 121 | + !array_key_exists('host',$configElmt) || |
|
| 122 | + !array_key_exists('dbname',$configElmt) || |
|
| 123 | + !array_key_exists('username',$configElmt)) |
|
| 124 | + { |
|
| 125 | + $this->logging->log('Missing DB params',ERROR); |
|
| 126 | + return ''; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 130 | - $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 129 | + // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 130 | + $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 131 | 131 | |
| 132 | - if (array_key_exists('port', $configElmt)) |
|
| 133 | - { |
|
| 134 | - $dsn .= ';port='.$configElmt['port']; |
|
| 135 | - } |
|
| 136 | - return $dsn; |
|
| 137 | - } |
|
| 132 | + if (array_key_exists('port', $configElmt)) |
|
| 133 | + { |
|
| 134 | + $dsn .= ';port='.$configElmt['port']; |
|
| 135 | + } |
|
| 136 | + return $dsn; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** Set name=element in database config table |
|
| 140 | - * @param string $name |
|
| 141 | - * @param string $element |
|
| 142 | - * @return boolean true on success, else false (error logged) |
|
| 143 | - */ |
|
| 144 | - public function setDBConfig($name,$element) |
|
| 145 | - { |
|
| 146 | - $db_conn=$this->db_connect_trap(); |
|
| 147 | - $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
|
| 148 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 149 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - $value=$ret_code->fetch(); |
|
| 153 | - if ($value != null && isset($value['id'])) |
|
| 154 | - { // Entry exists -> update |
|
| 155 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
|
| 156 | - } |
|
| 157 | - else |
|
| 158 | - { // Entry does no exists -> create |
|
| 159 | - $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
|
| 160 | - } |
|
| 161 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 162 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 163 | - return false; |
|
| 164 | - } |
|
| 165 | - $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
| 166 | - return true; |
|
| 167 | - } |
|
| 139 | + /** Set name=element in database config table |
|
| 140 | + * @param string $name |
|
| 141 | + * @param string $element |
|
| 142 | + * @return boolean true on success, else false (error logged) |
|
| 143 | + */ |
|
| 144 | + public function setDBConfig($name,$element) |
|
| 145 | + { |
|
| 146 | + $db_conn=$this->db_connect_trap(); |
|
| 147 | + $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
|
| 148 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 149 | + $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + $value=$ret_code->fetch(); |
|
| 153 | + if ($value != null && isset($value['id'])) |
|
| 154 | + { // Entry exists -> update |
|
| 155 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
|
| 156 | + } |
|
| 157 | + else |
|
| 158 | + { // Entry does no exists -> create |
|
| 159 | + $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
|
| 160 | + } |
|
| 161 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 162 | + $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 163 | + return false; |
|
| 164 | + } |
|
| 165 | + $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
| 166 | + return true; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Get data from db_config |
|
| 171 | - * @param $element string name of param |
|
| 172 | - * @return mixed : value (or null) |
|
| 173 | - */ |
|
| 174 | - public function getDBConfig($element) |
|
| 175 | - { |
|
| 176 | - $db_conn=$this->db_connect_trap(); |
|
| 177 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 178 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 179 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 180 | - return null; |
|
| 181 | - } |
|
| 182 | - $value=$ret_code->fetch(); |
|
| 183 | - if ($value != null && isset($value['value'])) |
|
| 184 | - { |
|
| 185 | - return $value['value']; |
|
| 186 | - } |
|
| 187 | - return null; |
|
| 188 | - } |
|
| 169 | + /** |
|
| 170 | + * Get data from db_config |
|
| 171 | + * @param $element string name of param |
|
| 172 | + * @return mixed : value (or null) |
|
| 173 | + */ |
|
| 174 | + public function getDBConfig($element) |
|
| 175 | + { |
|
| 176 | + $db_conn=$this->db_connect_trap(); |
|
| 177 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 178 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 179 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 180 | + return null; |
|
| 181 | + } |
|
| 182 | + $value=$ret_code->fetch(); |
|
| 183 | + if ($value != null && isset($value['value'])) |
|
| 184 | + { |
|
| 185 | + return $value['value']; |
|
| 186 | + } |
|
| 187 | + return null; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | |
| 191 | - //********* Schema Management *********************/ |
|
| 191 | + //********* Schema Management *********************/ |
|
| 192 | 192 | |
| 193 | - /** Create database schema |
|
| 194 | - * @param $schema_file string File to read schema from |
|
| 195 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 196 | - */ |
|
| 197 | - public function create_schema($schema_file,$table_prefix) |
|
| 198 | - { |
|
| 199 | - //Read data from snmptrapd from stdin |
|
| 200 | - $input_stream=fopen($schema_file, 'r'); |
|
| 193 | + /** Create database schema |
|
| 194 | + * @param $schema_file string File to read schema from |
|
| 195 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 196 | + */ |
|
| 197 | + public function create_schema($schema_file,$table_prefix) |
|
| 198 | + { |
|
| 199 | + //Read data from snmptrapd from stdin |
|
| 200 | + $input_stream=fopen($schema_file, 'r'); |
|
| 201 | 201 | |
| 202 | - if ($input_stream=== false) |
|
| 203 | - { |
|
| 204 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
| 205 | - return; |
|
| 206 | - } |
|
| 207 | - $newline=''; |
|
| 208 | - $cur_table=''; |
|
| 209 | - $cur_table_array=array(); |
|
| 210 | - $db_conn=$this->db_connect_trap(); |
|
| 202 | + if ($input_stream=== false) |
|
| 203 | + { |
|
| 204 | + $this->logging->log("Error reading schema !",ERROR,''); |
|
| 205 | + return; |
|
| 206 | + } |
|
| 207 | + $newline=''; |
|
| 208 | + $cur_table=''; |
|
| 209 | + $cur_table_array=array(); |
|
| 210 | + $db_conn=$this->db_connect_trap(); |
|
| 211 | 211 | |
| 212 | - while (($line=fgets($input_stream)) !== false) |
|
| 213 | - { |
|
| 214 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 215 | - if (preg_match('/; *$/', $newline)) |
|
| 216 | - { |
|
| 217 | - $sql= $newline; |
|
| 218 | - if ($db_conn->query($sql) === false) { |
|
| 219 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 220 | - return; |
|
| 221 | - } |
|
| 222 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 223 | - { |
|
| 224 | - $cur_table='table '.$cur_table_array[1]; |
|
| 225 | - } |
|
| 226 | - else |
|
| 227 | - { |
|
| 228 | - $cur_table='secret SQL stuff :-)'; |
|
| 229 | - } |
|
| 230 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 231 | - $newline=''; |
|
| 232 | - } |
|
| 233 | - } |
|
| 212 | + while (($line=fgets($input_stream)) !== false) |
|
| 213 | + { |
|
| 214 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 215 | + if (preg_match('/; *$/', $newline)) |
|
| 216 | + { |
|
| 217 | + $sql= $newline; |
|
| 218 | + if ($db_conn->query($sql) === false) { |
|
| 219 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 220 | + return; |
|
| 221 | + } |
|
| 222 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 223 | + { |
|
| 224 | + $cur_table='table '.$cur_table_array[1]; |
|
| 225 | + } |
|
| 226 | + else |
|
| 227 | + { |
|
| 228 | + $cur_table='secret SQL stuff :-)'; |
|
| 229 | + } |
|
| 230 | + $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 231 | + $newline=''; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - $sql= $newline; |
|
| 236 | - if ($sql != '' ) |
|
| 237 | - { |
|
| 238 | - if ($db_conn->query($sql) === false) { |
|
| 239 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 240 | - return; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - $this->logging->log('Schema created',INFO); |
|
| 244 | - } |
|
| 235 | + $sql= $newline; |
|
| 236 | + if ($sql != '' ) |
|
| 237 | + { |
|
| 238 | + if ($db_conn->query($sql) === false) { |
|
| 239 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 240 | + return; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + $this->logging->log('Schema created',INFO); |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * Update database schema from current (as set in db) to $target_version |
|
| 248 | - * @param $prefix string file prefix of sql update File |
|
| 249 | - * @param $target_version int target db version number |
|
| 250 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 251 | - * @param bool $getmsg : only get messages from version upgrades |
|
| 252 | - * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
| 253 | - */ |
|
| 254 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 255 | - { |
|
| 256 | - // Get current db number |
|
| 257 | - $db_conn=$this->db_connect_trap(); |
|
| 258 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 259 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 260 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 261 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 262 | - return 'ERROR'; |
|
| 263 | - } |
|
| 264 | - $version=$ret_code->fetchAll(); |
|
| 265 | - $cur_version=$version[0]['value']; |
|
| 246 | + /** |
|
| 247 | + * Update database schema from current (as set in db) to $target_version |
|
| 248 | + * @param $prefix string file prefix of sql update File |
|
| 249 | + * @param $target_version int target db version number |
|
| 250 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 251 | + * @param bool $getmsg : only get messages from version upgrades |
|
| 252 | + * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
| 253 | + */ |
|
| 254 | + public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 255 | + { |
|
| 256 | + // Get current db number |
|
| 257 | + $db_conn=$this->db_connect_trap(); |
|
| 258 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 259 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 260 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 261 | + $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 262 | + return 'ERROR'; |
|
| 263 | + } |
|
| 264 | + $version=$ret_code->fetchAll(); |
|
| 265 | + $cur_version=$version[0]['value']; |
|
| 266 | 266 | |
| 267 | - if ($this->trapDBType == 'pgsql') |
|
| 268 | - { |
|
| 269 | - $prefix .= 'update_pgsql/schema_'; |
|
| 270 | - } |
|
| 271 | - else |
|
| 272 | - { |
|
| 273 | - $prefix .= 'update_sql/schema_'; |
|
| 274 | - } |
|
| 275 | - //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 276 | - if ($getmsg === true) |
|
| 277 | - { |
|
| 278 | - return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
| 279 | - } |
|
| 267 | + if ($this->trapDBType == 'pgsql') |
|
| 268 | + { |
|
| 269 | + $prefix .= 'update_pgsql/schema_'; |
|
| 270 | + } |
|
| 271 | + else |
|
| 272 | + { |
|
| 273 | + $prefix .= 'update_sql/schema_'; |
|
| 274 | + } |
|
| 275 | + //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 276 | + if ($getmsg === true) |
|
| 277 | + { |
|
| 278 | + return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
| 282 | - { |
|
| 283 | - return 'ERROR'; |
|
| 284 | - } |
|
| 285 | - return ''; |
|
| 281 | + if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
| 282 | + { |
|
| 283 | + return 'ERROR'; |
|
| 284 | + } |
|
| 285 | + return ''; |
|
| 286 | 286 | |
| 287 | - } |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * Update database schema from current (as set in db) to $target_version |
|
| 291 | - * @param string $prefix file prefix of sql update File |
|
| 292 | - * @param int $cur_version current db version number |
|
| 293 | - * @param int $target_version target db version number |
|
| 294 | - * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
| 295 | - * @return bool : true on error |
|
| 296 | - */ |
|
| 297 | - public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
| 298 | - { |
|
| 299 | - while($cur_version<$target_version) |
|
| 300 | - { // TODO : execute pre & post scripts |
|
| 301 | - $cur_version++; |
|
| 302 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 303 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 304 | - $input_stream=fopen($updateFile, 'r'); |
|
| 305 | - if ($input_stream=== false) |
|
| 306 | - { |
|
| 307 | - $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
| 308 | - return true; |
|
| 309 | - } |
|
| 310 | - $newline=''; |
|
| 311 | - $db_conn=$this->db_connect_trap(); |
|
| 312 | - $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 313 | - while (($line=fgets($input_stream)) !== false) |
|
| 314 | - { |
|
| 315 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 316 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 317 | - if (preg_match('/; *$/', $newline)) |
|
| 318 | - { |
|
| 319 | - $sql_req=$db_conn->prepare($newline); |
|
| 320 | - if ($sql_req->execute() === false) { |
|
| 321 | - $this->logging->log('Error create schema : '.$newline,ERROR); |
|
| 322 | - return true; |
|
| 323 | - } |
|
| 324 | - $cur_table_array=array(); |
|
| 325 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 326 | - { |
|
| 327 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 328 | - } |
|
| 329 | - else |
|
| 330 | - { |
|
| 331 | - $cur_table='secret SQL stuff :-)'; |
|
| 332 | - //$cur_table=$newline; |
|
| 333 | - } |
|
| 334 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 289 | + /** |
|
| 290 | + * Update database schema from current (as set in db) to $target_version |
|
| 291 | + * @param string $prefix file prefix of sql update File |
|
| 292 | + * @param int $cur_version current db version number |
|
| 293 | + * @param int $target_version target db version number |
|
| 294 | + * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
| 295 | + * @return bool : true on error |
|
| 296 | + */ |
|
| 297 | + public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
| 298 | + { |
|
| 299 | + while($cur_version<$target_version) |
|
| 300 | + { // TODO : execute pre & post scripts |
|
| 301 | + $cur_version++; |
|
| 302 | + $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 303 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 304 | + $input_stream=fopen($updateFile, 'r'); |
|
| 305 | + if ($input_stream=== false) |
|
| 306 | + { |
|
| 307 | + $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
| 308 | + return true; |
|
| 309 | + } |
|
| 310 | + $newline=''; |
|
| 311 | + $db_conn=$this->db_connect_trap(); |
|
| 312 | + $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 313 | + while (($line=fgets($input_stream)) !== false) |
|
| 314 | + { |
|
| 315 | + if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 316 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 317 | + if (preg_match('/; *$/', $newline)) |
|
| 318 | + { |
|
| 319 | + $sql_req=$db_conn->prepare($newline); |
|
| 320 | + if ($sql_req->execute() === false) { |
|
| 321 | + $this->logging->log('Error create schema : '.$newline,ERROR); |
|
| 322 | + return true; |
|
| 323 | + } |
|
| 324 | + $cur_table_array=array(); |
|
| 325 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 326 | + { |
|
| 327 | + $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 328 | + } |
|
| 329 | + else |
|
| 330 | + { |
|
| 331 | + $cur_table='secret SQL stuff :-)'; |
|
| 332 | + //$cur_table=$newline; |
|
| 333 | + } |
|
| 334 | + $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 335 | 335 | |
| 336 | - $newline=''; |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - fclose($input_stream); |
|
| 336 | + $newline=''; |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + fclose($input_stream); |
|
| 340 | 340 | |
| 341 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
| 342 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 343 | - if ($db_conn->query($sql) === false) { |
|
| 344 | - $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
| 345 | - return true; |
|
| 346 | - } |
|
| 341 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
| 342 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 343 | + if ($db_conn->query($sql) === false) { |
|
| 344 | + $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
| 345 | + return true; |
|
| 346 | + } |
|
| 347 | 347 | |
| 348 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 349 | - } |
|
| 350 | - return false; |
|
| 351 | - } |
|
| 348 | + $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 349 | + } |
|
| 350 | + return false; |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | - /** |
|
| 354 | - * Get database message for update to $target_version |
|
| 355 | - * @param string $prefix file prefix of sql update File |
|
| 356 | - * @param int $cur_version current db version number |
|
| 357 | - * @param int $target_version target db version number |
|
| 358 | - * @return string : return messages or 'ERROR'. |
|
| 359 | - */ |
|
| 360 | - private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 361 | - { |
|
| 353 | + /** |
|
| 354 | + * Get database message for update to $target_version |
|
| 355 | + * @param string $prefix file prefix of sql update File |
|
| 356 | + * @param int $cur_version current db version number |
|
| 357 | + * @param int $target_version target db version number |
|
| 358 | + * @return string : return messages or 'ERROR'. |
|
| 359 | + */ |
|
| 360 | + private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 361 | + { |
|
| 362 | 362 | |
| 363 | - $message=''; |
|
| 364 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 365 | - while($cur_version<$target_version) |
|
| 366 | - { |
|
| 367 | - $cur_version++; |
|
| 368 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 369 | - $input_stream=fopen($updateFile, 'r'); |
|
| 370 | - if ($input_stream=== false) |
|
| 371 | - { |
|
| 372 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 373 | - return 'ERROR'; |
|
| 374 | - } |
|
| 375 | - do |
|
| 376 | - { |
|
| 377 | - $line=fgets($input_stream); |
|
| 378 | - } |
|
| 379 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 380 | - fclose($input_stream); |
|
| 381 | - if ($line === false) |
|
| 382 | - { |
|
| 383 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 384 | - return ''; |
|
| 385 | - } |
|
| 386 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 387 | - } |
|
| 388 | - return $message; |
|
| 389 | - } |
|
| 363 | + $message=''; |
|
| 364 | + $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 365 | + while($cur_version<$target_version) |
|
| 366 | + { |
|
| 367 | + $cur_version++; |
|
| 368 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 369 | + $input_stream=fopen($updateFile, 'r'); |
|
| 370 | + if ($input_stream=== false) |
|
| 371 | + { |
|
| 372 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 373 | + return 'ERROR'; |
|
| 374 | + } |
|
| 375 | + do |
|
| 376 | + { |
|
| 377 | + $line=fgets($input_stream); |
|
| 378 | + } |
|
| 379 | + while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 380 | + fclose($input_stream); |
|
| 381 | + if ($line === false) |
|
| 382 | + { |
|
| 383 | + $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 384 | + return ''; |
|
| 385 | + } |
|
| 386 | + $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 387 | + } |
|
| 388 | + return $message; |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | 391 | } |
| 392 | 392 | \ No newline at end of file |
@@ -21,104 +21,104 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class NetworkRule extends PluginTemplate |
| 23 | 23 | { |
| 24 | - /** @var string $description Description of plugin */ |
|
| 25 | - public $description='Network functions to use into rules |
|
| 24 | + /** @var string $description Description of plugin */ |
|
| 25 | + public $description='Network functions to use into rules |
|
| 26 | 26 | test test test'; |
| 27 | 27 | |
| 28 | - /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 29 | - * If no functions are declared, set to empty array |
|
| 30 | - * $functions[<name>]['function'] : Name of the function to be called in this class |
|
| 31 | - * $functions[<name>]['params'] : Description of input parameters of function. |
|
| 32 | - * $functions[<name>]['description'] : Description. Can be multiline. |
|
| 33 | - */ |
|
| 34 | - public $functions=array( |
|
| 35 | - 'inNetwork' => array( // The name of the function in rules |
|
| 36 | - 'function' => 'isInNetwork', // Name of the function |
|
| 37 | - 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 38 | - 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 28 | + /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 29 | + * If no functions are declared, set to empty array |
|
| 30 | + * $functions[<name>]['function'] : Name of the function to be called in this class |
|
| 31 | + * $functions[<name>]['params'] : Description of input parameters of function. |
|
| 32 | + * $functions[<name>]['description'] : Description. Can be multiline. |
|
| 33 | + */ |
|
| 34 | + public $functions=array( |
|
| 35 | + 'inNetwork' => array( // The name of the function in rules |
|
| 36 | + 'function' => 'isInNetwork', // Name of the function |
|
| 37 | + 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 38 | + 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 39 | 39 | Does not work with IPV6' // Description (can be multiline). |
| 40 | - ), |
|
| 41 | - 'test' => array( // The name of the function in rules |
|
| 42 | - 'function' => 'testParam', // Name of the function |
|
| 43 | - 'params' => '<boolean to return as string>', // parameters description |
|
| 44 | - 'description' => 'Returns value passed as argument' // Description (can be multiline). |
|
| 45 | - ) |
|
| 46 | - ); |
|
| 40 | + ), |
|
| 41 | + 'test' => array( // The name of the function in rules |
|
| 42 | + 'function' => 'testParam', // Name of the function |
|
| 43 | + 'params' => '<boolean to return as string>', // parameters description |
|
| 44 | + 'description' => 'Returns value passed as argument' // Description (can be multiline). |
|
| 45 | + ) |
|
| 46 | + ); |
|
| 47 | 47 | |
| 48 | - /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
| 49 | - public $catchAllTraps=false; |
|
| 48 | + /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
| 49 | + public $catchAllTraps=false; |
|
| 50 | 50 | |
| 51 | - /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
| 52 | - public $processTraps=false; |
|
| 51 | + /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
| 52 | + public $processTraps=false; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 56 | - * @throws \Exception |
|
| 57 | - * @return \Trapdirector\Plugins\NetworkRule |
|
| 58 | - */ |
|
| 59 | - function __construct() |
|
| 60 | - { |
|
| 61 | - $this->name=basename(__FILE__,'.php'); |
|
| 62 | - return $this; |
|
| 63 | - } |
|
| 54 | + /** |
|
| 55 | + * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 56 | + * @throws \Exception |
|
| 57 | + * @return \Trapdirector\Plugins\NetworkRule |
|
| 58 | + */ |
|
| 59 | + function __construct() |
|
| 60 | + { |
|
| 61 | + $this->name=basename(__FILE__,'.php'); |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Function called by trapdirector if found in rules |
|
| 67 | - * Parameters check has to be done in function. |
|
| 68 | - * @param array $params Function parameters |
|
| 69 | - * @throws Exception |
|
| 70 | - * @return bool Evaluation |
|
| 71 | - */ |
|
| 72 | - public function isInNetwork(array $params) : bool |
|
| 73 | - { |
|
| 74 | - // Check param numbers and thrown exception if not correct. |
|
| 75 | - if (count($params)!=3) |
|
| 76 | - { |
|
| 77 | - throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 78 | - } |
|
| 65 | + /** |
|
| 66 | + * Function called by trapdirector if found in rules |
|
| 67 | + * Parameters check has to be done in function. |
|
| 68 | + * @param array $params Function parameters |
|
| 69 | + * @throws Exception |
|
| 70 | + * @return bool Evaluation |
|
| 71 | + */ |
|
| 72 | + public function isInNetwork(array $params) : bool |
|
| 73 | + { |
|
| 74 | + // Check param numbers and thrown exception if not correct. |
|
| 75 | + if (count($params)!=3) |
|
| 76 | + { |
|
| 77 | + throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - $ip = $params[0]; |
|
| 81 | - $net = $params[1]; |
|
| 82 | - $masq = $params[2]; |
|
| 80 | + $ip = $params[0]; |
|
| 81 | + $net = $params[1]; |
|
| 82 | + $masq = $params[2]; |
|
| 83 | 83 | |
| 84 | 84 | |
| 85 | - $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 85 | + $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 86 | 86 | |
| 87 | - $ip2 = ip2long($ip); |
|
| 88 | - $net2 = ip2long($net); |
|
| 87 | + $ip2 = ip2long($ip); |
|
| 88 | + $net2 = ip2long($net); |
|
| 89 | 89 | |
| 90 | - if ($ip2 === false ) |
|
| 91 | - { |
|
| 92 | - $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 93 | - throw new Exception('Invalid IP'); |
|
| 94 | - } |
|
| 95 | - if ($net2 === false) |
|
| 96 | - { |
|
| 97 | - $this->log('Invalid network',WARN); |
|
| 98 | - throw new Exception('Invalid net'); |
|
| 99 | - } |
|
| 100 | - if ($masq<1 || $masq > 32) |
|
| 101 | - { |
|
| 102 | - $this->log('Invalid masq',WARN); |
|
| 103 | - throw new Exception('Invalid net masq'); |
|
| 104 | - } |
|
| 105 | - // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 90 | + if ($ip2 === false ) |
|
| 91 | + { |
|
| 92 | + $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 93 | + throw new Exception('Invalid IP'); |
|
| 94 | + } |
|
| 95 | + if ($net2 === false) |
|
| 96 | + { |
|
| 97 | + $this->log('Invalid network',WARN); |
|
| 98 | + throw new Exception('Invalid net'); |
|
| 99 | + } |
|
| 100 | + if ($masq<1 || $masq > 32) |
|
| 101 | + { |
|
| 102 | + $this->log('Invalid masq',WARN); |
|
| 103 | + throw new Exception('Invalid net masq'); |
|
| 104 | + } |
|
| 105 | + // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 106 | 106 | |
| 107 | - $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 108 | - $masq = ~ $masq; |
|
| 109 | - return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 107 | + $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 108 | + $masq = ~ $masq; |
|
| 109 | + return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 110 | 110 | |
| 111 | - } |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - public function testParam(array $param) |
|
| 114 | - { |
|
| 115 | - if (count($param)!=1) |
|
| 116 | - { |
|
| 117 | - throw new Exception('Invalid number of parameters : ' . count($param)); |
|
| 118 | - } |
|
| 119 | - if ($param[0] == 'true') return true; |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 113 | + public function testParam(array $param) |
|
| 114 | + { |
|
| 115 | + if (count($param)!=1) |
|
| 116 | + { |
|
| 117 | + throw new Exception('Invalid number of parameters : ' . count($param)); |
|
| 118 | + } |
|
| 119 | + if ($param[0] == 'true') return true; |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | // view limits |
| 28 | 28 | protected $limit; |
| 29 | - protected $offset; |
|
| 29 | + protected $offset; |
|
| 30 | 30 | |
| 31 | 31 | // Used for rendering days in list |
| 32 | 32 | protected $columnCount; |
@@ -34,55 +34,55 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | // Filters |
| 36 | 36 | |
| 37 | - protected $filter; |
|
| 38 | - protected $enforcedFilters = array(); |
|
| 39 | - protected $searchColumns = array(); |
|
| 37 | + protected $filter; |
|
| 38 | + protected $enforcedFilters = array(); |
|
| 39 | + protected $searchColumns = array(); |
|
| 40 | 40 | |
| 41 | 41 | |
| 42 | 42 | // Must return titles in array ( id => display_name ) |
| 43 | 43 | abstract protected function getTitles(); |
| 44 | 44 | // ****************** Day header |
| 45 | - protected function renderDayIfNew($timestamp) |
|
| 46 | - { |
|
| 47 | - $view = $this->getView(); |
|
| 45 | + protected function renderDayIfNew($timestamp) |
|
| 46 | + { |
|
| 47 | + $view = $this->getView(); |
|
| 48 | 48 | |
| 49 | 49 | // Check for date local format |
| 50 | - if (in_array(setlocale(LC_ALL, 0), array('en_US.UTF-8', 'C'))) { |
|
| 51 | - $day = date('l, jS F Y', (int) $timestamp); |
|
| 52 | - } else { |
|
| 53 | - $day = strftime('%A, %e. %B, %Y', (int) $timestamp); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - if ($this->lastDay === $day) { |
|
| 57 | - return; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - if ($this->lastDay === null) { |
|
| 61 | - $htm = "<thead>\n <tr>\n"; |
|
| 62 | - } else { |
|
| 63 | - $htm = "</tbody>\n<thead>\n <tr>\n"; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if ($this->columnCount === null) { |
|
| 67 | - $this->columnCount = count($this->getTitles()); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $htm .= '<th colspan="' . $this->columnCount . '">' . $view->escape($day) . '</th>' . "\n"; |
|
| 71 | - if ($this->lastDay === null) { |
|
| 72 | - $htm .= " </tr>\n"; |
|
| 73 | - } else { |
|
| 74 | - $htm .= " </tr>\n</thead>\n"; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $this->lastDay = $day; |
|
| 78 | - |
|
| 79 | - return $htm . "<tbody>\n"; |
|
| 80 | - } |
|
| 50 | + if (in_array(setlocale(LC_ALL, 0), array('en_US.UTF-8', 'C'))) { |
|
| 51 | + $day = date('l, jS F Y', (int) $timestamp); |
|
| 52 | + } else { |
|
| 53 | + $day = strftime('%A, %e. %B, %Y', (int) $timestamp); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + if ($this->lastDay === $day) { |
|
| 57 | + return; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + if ($this->lastDay === null) { |
|
| 61 | + $htm = "<thead>\n <tr>\n"; |
|
| 62 | + } else { |
|
| 63 | + $htm = "</tbody>\n<thead>\n <tr>\n"; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if ($this->columnCount === null) { |
|
| 67 | + $this->columnCount = count($this->getTitles()); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $htm .= '<th colspan="' . $this->columnCount . '">' . $view->escape($day) . '</th>' . "\n"; |
|
| 71 | + if ($this->lastDay === null) { |
|
| 72 | + $htm .= " </tr>\n"; |
|
| 73 | + } else { |
|
| 74 | + $htm .= " </tr>\n</thead>\n"; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $this->lastDay = $day; |
|
| 78 | + |
|
| 79 | + return $htm . "<tbody>\n"; |
|
| 80 | + } |
|
| 81 | 81 | // ****************** Render table in html |
| 82 | - public function __toString() |
|
| 83 | - { |
|
| 84 | - return $this->render(); |
|
| 85 | - } |
|
| 82 | + public function __toString() |
|
| 83 | + { |
|
| 84 | + return $this->render(); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | abstract public function render(); |
| 88 | 88 | |
@@ -92,76 +92,76 @@ discard block |
||
| 92 | 92 | $this->moduleConfig = $conf; |
| 93 | 93 | } |
| 94 | 94 | // ****************** View get/set |
| 95 | - protected function getView() |
|
| 96 | - { |
|
| 97 | - if ($this->view === null) { |
|
| 98 | - $this->view = Icinga::app()->getViewRenderer()->view; |
|
| 99 | - } |
|
| 100 | - return $this->view; |
|
| 101 | - } |
|
| 95 | + protected function getView() |
|
| 96 | + { |
|
| 97 | + if ($this->view === null) { |
|
| 98 | + $this->view = Icinga::app()->getViewRenderer()->view; |
|
| 99 | + } |
|
| 100 | + return $this->view; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - public function setView($view) |
|
| 104 | - { |
|
| 105 | - $this->view = $view; |
|
| 106 | - } |
|
| 103 | + public function setView($view) |
|
| 104 | + { |
|
| 105 | + $this->view = $view; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | 108 | // Limits |
| 109 | 109 | |
| 110 | - public function limit($count = null, $offset = null) |
|
| 111 | - { |
|
| 112 | - $this->limit = $count; |
|
| 113 | - $this->offset = $offset; |
|
| 110 | + public function limit($count = null, $offset = null) |
|
| 111 | + { |
|
| 112 | + $this->limit = $count; |
|
| 113 | + $this->offset = $offset; |
|
| 114 | 114 | |
| 115 | - return $this; |
|
| 116 | - } |
|
| 115 | + return $this; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - public function hasLimit() |
|
| 119 | - { |
|
| 120 | - return $this->limit !== null; |
|
| 121 | - } |
|
| 118 | + public function hasLimit() |
|
| 119 | + { |
|
| 120 | + return $this->limit !== null; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - public function getLimit() |
|
| 124 | - { |
|
| 125 | - return $this->limit; |
|
| 126 | - } |
|
| 123 | + public function getLimit() |
|
| 124 | + { |
|
| 125 | + return $this->limit; |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - public function hasOffset() |
|
| 129 | - { |
|
| 130 | - return $this->offset !== null; |
|
| 131 | - } |
|
| 128 | + public function hasOffset() |
|
| 129 | + { |
|
| 130 | + return $this->offset !== null; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - public function getOffset() |
|
| 134 | - { |
|
| 135 | - return $this->offset; |
|
| 136 | - } |
|
| 133 | + public function getOffset() |
|
| 134 | + { |
|
| 135 | + return $this->offset; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | abstract function count(); |
| 139 | 139 | |
| 140 | - public function getPaginator() |
|
| 141 | - { |
|
| 142 | - $paginator = new Paginator(); |
|
| 143 | - $paginator->setQuery($this); |
|
| 140 | + public function getPaginator() |
|
| 141 | + { |
|
| 142 | + $paginator = new Paginator(); |
|
| 143 | + $paginator->setQuery($this); |
|
| 144 | 144 | |
| 145 | - return $paginator; |
|
| 146 | - } |
|
| 145 | + return $paginator; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | // ****************** DB connection and query |
| 149 | 149 | |
| 150 | - public function setConnection(Selectable $connection) |
|
| 151 | - { |
|
| 152 | - $this->connection = $connection; |
|
| 153 | - return $this; |
|
| 154 | - } |
|
| 150 | + public function setConnection(Selectable $connection) |
|
| 151 | + { |
|
| 152 | + $this->connection = $connection; |
|
| 153 | + return $this; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - protected function connection() |
|
| 157 | - { |
|
| 158 | - return $this->connection; |
|
| 159 | - } |
|
| 156 | + protected function connection() |
|
| 157 | + { |
|
| 158 | + return $this->connection; |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - protected function db() |
|
| 162 | - { |
|
| 163 | - return $this->connection()->getDbAdapter(); |
|
| 164 | - } |
|
| 161 | + protected function db() |
|
| 162 | + { |
|
| 163 | + return $this->connection()->getDbAdapter(); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | 166 | protected function getTable() |
| 167 | 167 | { |
@@ -169,52 +169,52 @@ discard block |
||
| 169 | 169 | |
| 170 | 170 | $query = $this->getBaseQuery(); |
| 171 | 171 | |
| 172 | - if ($this->hasLimit() || $this->hasOffset()) { |
|
| 173 | - $query->limit($this->getLimit(), $this->getOffset()); |
|
| 174 | - } |
|
| 172 | + if ($this->hasLimit() || $this->hasOffset()) { |
|
| 173 | + $query->limit($this->getLimit(), $this->getOffset()); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | 176 | return $db->fetchAll($query); |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - abstract public function getBaseQuery(); |
|
| 179 | + abstract public function getBaseQuery(); |
|
| 180 | 180 | |
| 181 | 181 | // ****************** Filters |
| 182 | 182 | |
| 183 | - protected function getSearchColumns() |
|
| 184 | - { |
|
| 185 | - return $this->getColumns(); |
|
| 186 | - } |
|
| 183 | + protected function getSearchColumns() |
|
| 184 | + { |
|
| 185 | + return $this->getColumns(); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | 188 | abstract public function getColumns(); |
| 189 | 189 | |
| 190 | - public function setFilter($filter) |
|
| 191 | - { |
|
| 192 | - $this->filter = $filter; |
|
| 193 | - return $this; |
|
| 194 | - } |
|
| 190 | + public function setFilter($filter) |
|
| 191 | + { |
|
| 192 | + $this->filter = $filter; |
|
| 193 | + return $this; |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | 196 | public function getFilterEditor(Request $request) |
| 197 | - { |
|
| 198 | - $filterEditor = Widget::create('filterEditor') |
|
| 199 | - ->setColumns(array_keys($this->getColumns())) |
|
| 200 | - ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 201 | - ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 202 | - ->ignoreParams('page') |
|
| 203 | - ->handleRequest($request); |
|
| 197 | + { |
|
| 198 | + $filterEditor = Widget::create('filterEditor') |
|
| 199 | + ->setColumns(array_keys($this->getColumns())) |
|
| 200 | + ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 201 | + ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 202 | + ->ignoreParams('page') |
|
| 203 | + ->handleRequest($request); |
|
| 204 | 204 | |
| 205 | - $filter = $filterEditor->getFilter(); |
|
| 206 | - $this->setFilter($filter); |
|
| 205 | + $filter = $filterEditor->getFilter(); |
|
| 206 | + $this->setFilter($filter); |
|
| 207 | 207 | |
| 208 | - return $filterEditor; |
|
| 209 | - } |
|
| 208 | + return $filterEditor; |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | 211 | protected function renderFilter($filter) |
| 212 | 212 | { // TODO : create a better filter wher user can choose host/oid to filter |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - protected function applyFiltersToQuery($query) |
|
| 216 | - { |
|
| 217 | - /* |
|
| 215 | + protected function applyFiltersToQuery($query) |
|
| 216 | + { |
|
| 217 | + /* |
|
| 218 | 218 | $filter = null; |
| 219 | 219 | $enforced = $this->enforcedFilters; |
| 220 | 220 | if ($this->filter && ! $this->filter->isEmpty()) { |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | $query->where($this->renderFilter($filter)); |
| 231 | 231 | } |
| 232 | 232 | */ |
| 233 | - return $query; |
|
| 234 | - } |
|
| 233 | + return $query; |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | 236 | } |
| 237 | 237 | \ No newline at end of file |