@@ -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()->getConnection(); |
|
| 164 | - } |
|
| 161 | + protected function db() |
|
| 162 | + { |
|
| 163 | + return $this->connection()->getConnection(); |
|
| 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 |
@@ -20,9 +20,9 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | // Filters |
| 22 | 22 | |
| 23 | - protected $filter; |
|
| 24 | - protected $enforcedFilters = array(); |
|
| 25 | - protected $searchColumns = array(); |
|
| 23 | + protected $filter; |
|
| 24 | + protected $enforcedFilters = array(); |
|
| 25 | + protected $searchColumns = array(); |
|
| 26 | 26 | |
| 27 | 27 | protected function getTitles() { |
| 28 | 28 | // TODO : check moduleconfig is set |
@@ -30,10 +30,10 @@ discard block |
||
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | // ****************** Render table in html |
| 33 | - public function __toString() |
|
| 34 | - { |
|
| 35 | - return $this->render(); |
|
| 36 | - } |
|
| 33 | + public function __toString() |
|
| 34 | + { |
|
| 35 | + return $this->render(); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | 38 | public function render() |
| 39 | 39 | { |
@@ -97,26 +97,26 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - public function count() |
|
| 101 | - { |
|
| 102 | - $db=$this->db(); |
|
| 100 | + public function count() |
|
| 101 | + { |
|
| 102 | + $db=$this->db(); |
|
| 103 | 103 | |
| 104 | 104 | $query = $db->select()->from( |
| 105 | - $this->moduleConfig->getTrapTableName(), |
|
| 106 | - array('COUNT(*)') |
|
| 107 | - ); |
|
| 105 | + $this->moduleConfig->getTrapTableName(), |
|
| 106 | + array('COUNT(*)') |
|
| 107 | + ); |
|
| 108 | 108 | $this->applyFiltersToQuery($query); |
| 109 | 109 | |
| 110 | - return $db->fetchOne($query); |
|
| 111 | - } |
|
| 110 | + return $db->fetchOne($query); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - public function getPaginator() |
|
| 114 | - { |
|
| 115 | - $paginator = new Paginator(); |
|
| 116 | - $paginator->setQuery($this); |
|
| 113 | + public function getPaginator() |
|
| 114 | + { |
|
| 115 | + $paginator = new Paginator(); |
|
| 116 | + $paginator->setQuery($this); |
|
| 117 | 117 | |
| 118 | - return $paginator; |
|
| 119 | - } |
|
| 118 | + return $paginator; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | // ****************** DB connection and query |
| 122 | 122 | |
@@ -126,24 +126,24 @@ discard block |
||
| 126 | 126 | |
| 127 | 127 | $query = $this->getBaseQuery(); |
| 128 | 128 | $this->applyFiltersToQuery($query); |
| 129 | - if ($this->hasLimit() || $this->hasOffset()) { |
|
| 130 | - $query->limit($this->getLimit(), $this->getOffset()); |
|
| 131 | - } |
|
| 129 | + if ($this->hasLimit() || $this->hasOffset()) { |
|
| 130 | + $query->limit($this->getLimit(), $this->getOffset()); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | 133 | return $db->fetchAll($query); |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - public function getBaseQuery() |
|
| 137 | - { |
|
| 136 | + public function getBaseQuery() |
|
| 137 | + { |
|
| 138 | 138 | $db=$this->db(); |
| 139 | 139 | |
| 140 | 140 | $query = $db->select()->from( |
| 141 | - $this->moduleConfig->getTrapTableName(), |
|
| 142 | - $this->moduleConfig->getTrapListDisplayColumns() |
|
| 143 | - )->order('timestamp DESC'); |
|
| 141 | + $this->moduleConfig->getTrapTableName(), |
|
| 142 | + $this->moduleConfig->getTrapListDisplayColumns() |
|
| 143 | + )->order('timestamp DESC'); |
|
| 144 | 144 | |
| 145 | - return $query; |
|
| 146 | - } |
|
| 145 | + return $query; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | // ****************** Filters |
| 149 | 149 | |
@@ -173,39 +173,39 @@ discard block |
||
| 173 | 173 | $this->filter_done=(isset($filter['done']))?$this->filter_done=$filter['done']:0; |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | - protected function getSearchColumns() |
|
| 177 | - { |
|
| 178 | - return $this->getColumns(); |
|
| 179 | - } |
|
| 176 | + protected function getSearchColumns() |
|
| 177 | + { |
|
| 178 | + return $this->getColumns(); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | 181 | public function getColumns() |
| 182 | 182 | { |
| 183 | 183 | return $this->moduleConfig->getTrapListDisplayColumns(); |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | - public function setFilter($filter) |
|
| 187 | - { |
|
| 188 | - $this->filter = $filter; |
|
| 189 | - return $this; |
|
| 190 | - } |
|
| 186 | + public function setFilter($filter) |
|
| 187 | + { |
|
| 188 | + $this->filter = $filter; |
|
| 189 | + return $this; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | 192 | public function getFilterEditor(Request $request) |
| 193 | - { |
|
| 194 | - $filterEditor = Widget::create('filterEditor') |
|
| 195 | - ->setColumns(array_keys($this->getColumns())) |
|
| 196 | - ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 197 | - ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 198 | - ->ignoreParams('page') |
|
| 199 | - ->handleRequest($request); |
|
| 200 | - |
|
| 201 | - $filter = $filterEditor->getFilter(); |
|
| 202 | - $this->setFilter($filter); |
|
| 203 | - |
|
| 204 | - return $filterEditor; |
|
| 205 | - } |
|
| 193 | + { |
|
| 194 | + $filterEditor = Widget::create('filterEditor') |
|
| 195 | + ->setColumns(array_keys($this->getColumns())) |
|
| 196 | + ->setSearchColumns(array_keys($this->getSearchColumns())) |
|
| 197 | + ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
|
| 198 | + ->ignoreParams('page') |
|
| 199 | + ->handleRequest($request); |
|
| 200 | + |
|
| 201 | + $filter = $filterEditor->getFilter(); |
|
| 202 | + $this->setFilter($filter); |
|
| 203 | + |
|
| 204 | + return $filterEditor; |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - protected function applyFiltersToQuery($query) |
|
| 208 | - { |
|
| 207 | + protected function applyFiltersToQuery($query) |
|
| 208 | + { |
|
| 209 | 209 | |
| 210 | 210 | $sql=''; |
| 211 | 211 | if ($this->filter_query != '') |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | $sql.="(status != 'done')"; |
| 227 | 227 | } |
| 228 | 228 | if ($sql != '') $query->where($sql); |
| 229 | - return $query; |
|
| 230 | - } |
|
| 229 | + return $query; |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | 232 | } |
| 233 | 233 | \ No newline at end of file |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | class Trap |
| 19 | 19 | { |
| 20 | 20 | // Configuration files and dirs |
| 21 | - /** @var string Icinga etc path */ |
|
| 21 | + /** @var string Icinga etc path */ |
|
| 22 | 22 | protected $icingaweb2_etc; |
| 23 | 23 | protected $trap_module_config; //< config.ini of module |
| 24 | 24 | protected $icingaweb2_ressources; //< resources.ini of icingaweb2 |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
| 64 | 64 | { |
| 65 | - // Paths of ini files |
|
| 65 | + // Paths of ini files |
|
| 66 | 66 | $this->icingaweb2_etc=$etc_dir; |
| 67 | 67 | $this->trap_module_config=$this->icingaweb2_etc."/modules/trapdirector/config.ini"; |
| 68 | 68 | $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
@@ -71,23 +71,23 @@ discard block |
||
| 71 | 71 | $this->logging = new Logging(); |
| 72 | 72 | if ($baseLogLevel != null) |
| 73 | 73 | { |
| 74 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 75 | - $this->logSetup=true; |
|
| 74 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 75 | + $this->logSetup=true; |
|
| 76 | 76 | } |
| 77 | 77 | else |
| 78 | - $this->logSetup=false; |
|
| 78 | + $this->logSetup=false; |
|
| 79 | 79 | $this->logging->log('Loggin started', INFO); |
| 80 | 80 | |
| 81 | 81 | // Get options from ini files |
| 82 | 82 | if (! is_file($this->trap_module_config)) |
| 83 | 83 | { |
| 84 | - throw new Exception("Ini file ".$this->trap_module_config." does not exists"); |
|
| 84 | + throw new Exception("Ini file ".$this->trap_module_config." does not exists"); |
|
| 85 | 85 | } |
| 86 | 86 | $trapConfig=parse_ini_file($this->trap_module_config,true); |
| 87 | 87 | if ($trapConfig == false) |
| 88 | 88 | { |
| 89 | - $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 90 | - throw new Exception("Error reading ini file : ".$this->trap_module_config); |
|
| 89 | + $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 90 | + throw new Exception("Error reading ini file : ".$this->trap_module_config); |
|
| 91 | 91 | } |
| 92 | 92 | $this->getMainOptions($trapConfig); // Get main options from ini file |
| 93 | 93 | $this->setupDatabase($trapConfig); // Setup database class |
@@ -121,26 +121,26 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
| 123 | 123 | { |
| 124 | - if (!isset($option_array[$option_category][$option_name])) |
|
| 125 | - { |
|
| 126 | - if ($message === null) |
|
| 127 | - { |
|
| 128 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 129 | - } |
|
| 130 | - $this->logging->log($message,$log_level); |
|
| 131 | - return false; |
|
| 132 | - } |
|
| 133 | - else |
|
| 134 | - { |
|
| 135 | - $option_var=$option_array[$option_category][$option_name]; |
|
| 136 | - return true; |
|
| 137 | - } |
|
| 124 | + if (!isset($option_array[$option_category][$option_name])) |
|
| 125 | + { |
|
| 126 | + if ($message === null) |
|
| 127 | + { |
|
| 128 | + $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 129 | + } |
|
| 130 | + $this->logging->log($message,$log_level); |
|
| 131 | + return false; |
|
| 132 | + } |
|
| 133 | + else |
|
| 134 | + { |
|
| 135 | + $option_var=$option_array[$option_category][$option_name]; |
|
| 136 | + return true; |
|
| 137 | + } |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
| 141 | 141 | * Get options from ini file |
| 142 | 142 | * @param array $trap_config : ini file array |
| 143 | - */ |
|
| 143 | + */ |
|
| 144 | 144 | protected function getMainOptions($trapConfig) |
| 145 | 145 | { |
| 146 | 146 | |
@@ -159,10 +159,10 @@ discard block |
||
| 159 | 159 | // API options |
| 160 | 160 | if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
| 161 | 161 | { |
| 162 | - $this->api_use=true; |
|
| 163 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 164 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 165 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 162 | + $this->api_use=true; |
|
| 163 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 164 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 165 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 166 | 166 | } |
| 167 | 167 | } |
| 168 | 168 | |
@@ -172,45 +172,45 @@ discard block |
||
| 172 | 172 | */ |
| 173 | 173 | protected function setupDatabase($trapConfig) |
| 174 | 174 | { |
| 175 | - // Trap database |
|
| 176 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
| 177 | - { |
|
| 178 | - $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 179 | - return; |
|
| 180 | - } |
|
| 181 | - $dbTrapName=$trapConfig['config']['database']; |
|
| 182 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 175 | + // Trap database |
|
| 176 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
| 177 | + { |
|
| 178 | + $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 179 | + return; |
|
| 180 | + } |
|
| 181 | + $dbTrapName=$trapConfig['config']['database']; |
|
| 182 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 183 | 183 | |
| 184 | 184 | if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
| 185 | - { |
|
| 186 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 187 | - return; |
|
| 188 | - } |
|
| 189 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 190 | - { |
|
| 191 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 192 | - return; |
|
| 193 | - } |
|
| 185 | + { |
|
| 186 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 187 | + return; |
|
| 188 | + } |
|
| 189 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 190 | + { |
|
| 191 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 192 | + return; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 195 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 196 | 196 | |
| 197 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 197 | + if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 198 | 198 | |
| 199 | - // IDO Database |
|
| 200 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 201 | - { |
|
| 202 | - $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 203 | - } |
|
| 204 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 199 | + // IDO Database |
|
| 200 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 201 | + { |
|
| 202 | + $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 203 | + } |
|
| 204 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 205 | 205 | |
| 206 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 207 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 208 | - { |
|
| 209 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 210 | - return; |
|
| 211 | - } |
|
| 206 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 207 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 208 | + { |
|
| 209 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 210 | + return; |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 213 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | /** |
@@ -221,9 +221,9 @@ discard block |
||
| 221 | 221 | // Database options |
| 222 | 222 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
| 223 | 223 | { |
| 224 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 225 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 226 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 224 | + $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 225 | + $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 226 | + $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 227 | 227 | } |
| 228 | 228 | } |
| 229 | 229 | |
@@ -234,10 +234,10 @@ discard block |
||
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | /** |
| 237 | - * Get data from db_config |
|
| 238 | - * @param $element string name of param |
|
| 239 | - * @return mixed : value (or null) |
|
| 240 | - */ |
|
| 237 | + * Get data from db_config |
|
| 238 | + * @param $element string name of param |
|
| 239 | + * @return mixed : value (or null) |
|
| 240 | + */ |
|
| 241 | 241 | protected function getDBConfig($element) |
| 242 | 242 | { |
| 243 | 243 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -255,15 +255,15 @@ discard block |
||
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | /** OBSOLETE Send log. Throws exception on critical error |
| 258 | - * @param string $message Message to log |
|
| 259 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 260 | - * @param string $destination file/syslog/display |
|
| 261 | - * @return void |
|
| 262 | - **/ |
|
| 258 | + * @param string $message Message to log |
|
| 259 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 260 | + * @param string $destination file/syslog/display |
|
| 261 | + * @return void |
|
| 262 | + **/ |
|
| 263 | 263 | public function trapLog( $message, $level, $destination ='') // OBSOLETE |
| 264 | 264 | { |
| 265 | 265 | // TODO : replace ref with $this->logging->log |
| 266 | - $this->logging->log($message, $level, $destination); |
|
| 266 | + $this->logging->log($message, $level, $destination); |
|
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
@@ -273,19 +273,19 @@ discard block |
||
| 273 | 273 | |
| 274 | 274 | protected function getAPI() |
| 275 | 275 | { |
| 276 | - if ($this->icinga2api == null) |
|
| 277 | - { |
|
| 278 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 279 | - } |
|
| 280 | - return $this->icinga2api; |
|
| 276 | + if ($this->icinga2api == null) |
|
| 277 | + { |
|
| 278 | + $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 279 | + } |
|
| 280 | + return $this->icinga2api; |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | 283 | |
| 284 | 284 | /** |
| 285 | 285 | * read data from stream |
| 286 | - * @param $stream string input stream, defaults to "php://stdin" |
|
| 287 | - * @return mixed array trap data or exception with error |
|
| 288 | - */ |
|
| 286 | + * @param $stream string input stream, defaults to "php://stdin" |
|
| 287 | + * @return mixed array trap data or exception with error |
|
| 288 | + */ |
|
| 289 | 289 | public function read_trap($stream='php://stdin') |
| 290 | 290 | { |
| 291 | 291 | //Read data from snmptrapd from stdin |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | |
| 294 | 294 | if ($input_stream === false) |
| 295 | 295 | { |
| 296 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 296 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 297 | 297 | $this->logging->log("Error reading stdin !",ERROR,''); |
| 298 | 298 | return null; // note : exception thrown by logging |
| 299 | 299 | } |
@@ -302,21 +302,21 @@ discard block |
||
| 302 | 302 | $this->receivingHost=chop(fgets($input_stream)); |
| 303 | 303 | if ($this->receivingHost === false) |
| 304 | 304 | { |
| 305 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 305 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 306 | 306 | $this->logging->log("Error reading Host !",ERROR,''); |
| 307 | 307 | } |
| 308 | 308 | // line 2 IP:port=>IP:port |
| 309 | 309 | $IP=chop(fgets($input_stream)); |
| 310 | 310 | if ($IP === false) |
| 311 | 311 | { |
| 312 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 312 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 313 | 313 | $this->logging->log("Error reading IP !",ERROR,''); |
| 314 | 314 | } |
| 315 | 315 | $matches=array(); |
| 316 | 316 | $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
| 317 | 317 | if ($ret_code===0 || $ret_code===false) |
| 318 | 318 | { |
| 319 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 319 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 320 | 320 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
| 321 | 321 | } |
| 322 | 322 | else |
@@ -337,7 +337,7 @@ discard block |
||
| 337 | 337 | } |
| 338 | 338 | else |
| 339 | 339 | { |
| 340 | - 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')) |
|
| 340 | + 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')) |
|
| 341 | 341 | { |
| 342 | 342 | $this->trap_data['trap_oid']=$matches[2]; |
| 343 | 343 | } |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | |
| 354 | 354 | if ($this->trap_data['trap_oid']=='unknown') |
| 355 | 355 | { |
| 356 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 356 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 357 | 357 | $this->logging->log('no trap oid found',ERROR,''); |
| 358 | 358 | } |
| 359 | 359 | |
@@ -383,9 +383,9 @@ discard block |
||
| 383 | 383 | |
| 384 | 384 | /** |
| 385 | 385 | * Translate oid into array(MIB,Name) |
| 386 | - * @param $oid string oid to translate |
|
| 387 | - * @return mixed : null if not found or array(MIB,Name) |
|
| 388 | - */ |
|
| 386 | + * @param $oid string oid to translate |
|
| 387 | + * @return mixed : null if not found or array(MIB,Name) |
|
| 388 | + */ |
|
| 389 | 389 | public function translateOID($oid) |
| 390 | 390 | { |
| 391 | 391 | // try from database |
@@ -418,7 +418,7 @@ discard block |
||
| 418 | 418 | |
| 419 | 419 | // Try to get oid name from snmptranslate |
| 420 | 420 | $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
| 421 | - ' '.$oid); |
|
| 421 | + ' '.$oid); |
|
| 422 | 422 | $matches=array(); |
| 423 | 423 | $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
| 424 | 424 | if ($ret_code===0 || $ret_code === false) { |
@@ -431,9 +431,9 @@ discard block |
||
| 431 | 431 | |
| 432 | 432 | /** |
| 433 | 433 | * Erase old trap records |
| 434 | - * @param integer $days : erase traps when more than $days old |
|
| 435 | - * @return integer : number of lines deleted |
|
| 436 | - **/ |
|
| 434 | + * @param integer $days : erase traps when more than $days old |
|
| 435 | + * @return integer : number of lines deleted |
|
| 436 | + **/ |
|
| 437 | 437 | public function eraseOldTraps($days=0) |
| 438 | 438 | { |
| 439 | 439 | if ($days==0) |
@@ -458,72 +458,72 @@ discard block |
||
| 458 | 458 | public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
| 459 | 459 | { |
| 460 | 460 | |
| 461 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 461 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 462 | 462 | |
| 463 | - // add date time |
|
| 464 | - $insert_col ='date_received,status'; |
|
| 465 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 463 | + // add date time |
|
| 464 | + $insert_col ='date_received,status'; |
|
| 465 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 466 | 466 | |
| 467 | - if ($sourceIP !=null) |
|
| 468 | - { |
|
| 469 | - $insert_col .=',source_ip'; |
|
| 470 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 471 | - } |
|
| 472 | - if ($trapoid !=null) |
|
| 473 | - { |
|
| 474 | - $insert_col .=',trap_oid'; |
|
| 475 | - $insert_val .=",'". $trapoid ."'"; |
|
| 476 | - } |
|
| 477 | - $insert_col .=',status_detail'; |
|
| 478 | - $insert_val .=",'". $message ."'"; |
|
| 467 | + if ($sourceIP !=null) |
|
| 468 | + { |
|
| 469 | + $insert_col .=',source_ip'; |
|
| 470 | + $insert_val .=",'". $sourceIP ."'"; |
|
| 471 | + } |
|
| 472 | + if ($trapoid !=null) |
|
| 473 | + { |
|
| 474 | + $insert_col .=',trap_oid'; |
|
| 475 | + $insert_val .=",'". $trapoid ."'"; |
|
| 476 | + } |
|
| 477 | + $insert_col .=',status_detail'; |
|
| 478 | + $insert_val .=",'". $message ."'"; |
|
| 479 | 479 | |
| 480 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 480 | + $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 481 | 481 | |
| 482 | - switch ($this->trapsDB->trapDBType) |
|
| 483 | - { |
|
| 484 | - case 'pgsql': |
|
| 485 | - $sql .= ' RETURNING id;'; |
|
| 486 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 487 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 488 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 489 | - } |
|
| 490 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 491 | - // Get last id to insert oid/values in secondary table |
|
| 492 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 482 | + switch ($this->trapsDB->trapDBType) |
|
| 483 | + { |
|
| 484 | + case 'pgsql': |
|
| 485 | + $sql .= ' RETURNING id;'; |
|
| 486 | + $this->logging->log('sql : '.$sql,INFO); |
|
| 487 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 488 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 489 | + } |
|
| 490 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 491 | + // Get last id to insert oid/values in secondary table |
|
| 492 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 493 | 493 | |
| 494 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 495 | - } |
|
| 496 | - if (! isset($inserted_id_ret['id'])) { |
|
| 497 | - $this->logging->log('Error getting id',1,''); |
|
| 498 | - } |
|
| 499 | - $this->trap_id=$inserted_id_ret['id']; |
|
| 500 | - break; |
|
| 501 | - case 'mysql': |
|
| 502 | - $sql .= ';'; |
|
| 503 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 504 | - if ($db_conn->query($sql) === false) { |
|
| 505 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 506 | - } |
|
| 507 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 508 | - // Get last id to insert oid/values in secondary table |
|
| 509 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 510 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 511 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 512 | - } |
|
| 494 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 495 | + } |
|
| 496 | + if (! isset($inserted_id_ret['id'])) { |
|
| 497 | + $this->logging->log('Error getting id',1,''); |
|
| 498 | + } |
|
| 499 | + $this->trap_id=$inserted_id_ret['id']; |
|
| 500 | + break; |
|
| 501 | + case 'mysql': |
|
| 502 | + $sql .= ';'; |
|
| 503 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 504 | + if ($db_conn->query($sql) === false) { |
|
| 505 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 506 | + } |
|
| 507 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 508 | + // Get last id to insert oid/values in secondary table |
|
| 509 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 510 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 511 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 512 | + } |
|
| 513 | 513 | |
| 514 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 515 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 516 | - $this->trap_id=$inserted_id; |
|
| 517 | - break; |
|
| 518 | - default: |
|
| 519 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 520 | - } |
|
| 514 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 515 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 516 | + $this->trap_id=$inserted_id; |
|
| 517 | + break; |
|
| 518 | + default: |
|
| 519 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 520 | + } |
|
| 521 | 521 | |
| 522 | - $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 522 | + $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 523 | 523 | } |
| 524 | 524 | |
| 525 | 525 | /** Write trap data to trap database |
| 526 | - */ |
|
| 526 | + */ |
|
| 527 | 527 | public function writeTrapToDB() |
| 528 | 528 | { |
| 529 | 529 | |
@@ -621,10 +621,10 @@ discard block |
||
| 621 | 621 | } |
| 622 | 622 | |
| 623 | 623 | /** Get rules from rule database with ip and oid |
| 624 | - * @param $ip string ipv4 or ipv6 |
|
| 625 | - * @param $oid string oid in numeric |
|
| 626 | - * @return mixed|boolean : PDO object or false |
|
| 627 | - */ |
|
| 624 | + * @param $ip string ipv4 or ipv6 |
|
| 625 | + * @param $oid string oid in numeric |
|
| 626 | + * @return mixed|boolean : PDO object or false |
|
| 627 | + */ |
|
| 628 | 628 | protected function getRules($ip,$oid) |
| 629 | 629 | { |
| 630 | 630 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -683,9 +683,9 @@ discard block |
||
| 683 | 683 | } |
| 684 | 684 | |
| 685 | 685 | /** Add rule match to rule |
| 686 | - * @param id int : rule id |
|
| 687 | - * @param set int : value to set |
|
| 688 | - */ |
|
| 686 | + * @param id int : rule id |
|
| 687 | + * @param set int : value to set |
|
| 688 | + */ |
|
| 689 | 689 | protected function add_rule_match($id, $set) |
| 690 | 690 | { |
| 691 | 691 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -702,65 +702,65 @@ discard block |
||
| 702 | 702 | * @param integer $state numerical staus |
| 703 | 703 | * @param string $display |
| 704 | 704 | * @returnn bool true is service check was sent without error |
| 705 | - */ |
|
| 705 | + */ |
|
| 706 | 706 | public function serviceCheckResult($host,$service,$state,$display) |
| 707 | 707 | { |
| 708 | - if ($this->api_use === false) |
|
| 709 | - { |
|
| 710 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 711 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 712 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 708 | + if ($this->api_use === false) |
|
| 709 | + { |
|
| 710 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 711 | + $host.';' .$service .';' . $state . ';'.$display; |
|
| 712 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 713 | 713 | |
| 714 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 715 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 716 | - return true; |
|
| 717 | - } |
|
| 718 | - else |
|
| 719 | - { |
|
| 720 | - // Get perfdata if found |
|
| 721 | - $matches=array(); |
|
| 722 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 723 | - { |
|
| 724 | - $display=$matches[1]; |
|
| 725 | - $perfdata=$matches[2]; |
|
| 726 | - } |
|
| 727 | - else |
|
| 728 | - { |
|
| 729 | - $perfdata=''; |
|
| 730 | - } |
|
| 714 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 715 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 716 | + return true; |
|
| 717 | + } |
|
| 718 | + else |
|
| 719 | + { |
|
| 720 | + // Get perfdata if found |
|
| 721 | + $matches=array(); |
|
| 722 | + if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 723 | + { |
|
| 724 | + $display=$matches[1]; |
|
| 725 | + $perfdata=$matches[2]; |
|
| 726 | + } |
|
| 727 | + else |
|
| 728 | + { |
|
| 729 | + $perfdata=''; |
|
| 730 | + } |
|
| 731 | 731 | |
| 732 | - $api = $this->getAPI(); |
|
| 733 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 734 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 735 | - if ($retcode == false) |
|
| 736 | - { |
|
| 737 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 738 | - return false; |
|
| 739 | - } |
|
| 740 | - else |
|
| 741 | - { |
|
| 742 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 743 | - return true; |
|
| 744 | - } |
|
| 745 | - } |
|
| 732 | + $api = $this->getAPI(); |
|
| 733 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 734 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 735 | + if ($retcode == false) |
|
| 736 | + { |
|
| 737 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 738 | + return false; |
|
| 739 | + } |
|
| 740 | + else |
|
| 741 | + { |
|
| 742 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 743 | + return true; |
|
| 744 | + } |
|
| 745 | + } |
|
| 746 | 746 | } |
| 747 | 747 | |
| 748 | 748 | public function getHostByIP($ip) |
| 749 | 749 | { |
| 750 | - $api = $this->getAPI(); |
|
| 751 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 752 | - return $api->getHostByIP($ip); |
|
| 750 | + $api = $this->getAPI(); |
|
| 751 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 752 | + return $api->getHostByIP($ip); |
|
| 753 | 753 | } |
| 754 | 754 | |
| 755 | 755 | /** Resolve display. |
| 756 | - * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 757 | - * @param $display string |
|
| 758 | - * @return string display |
|
| 759 | - */ |
|
| 756 | + * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 757 | + * @param $display string |
|
| 758 | + * @return string display |
|
| 759 | + */ |
|
| 760 | 760 | protected function applyDisplay($display) |
| 761 | 761 | { |
| 762 | - $matches=array(); |
|
| 763 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 762 | + $matches=array(); |
|
| 763 | + while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 764 | 764 | { |
| 765 | 765 | $oid=$matches[1]; |
| 766 | 766 | $found=0; |
@@ -794,7 +794,7 @@ discard block |
||
| 794 | 794 | } |
| 795 | 795 | |
| 796 | 796 | /** Match rules for current trap and do action |
| 797 | - */ |
|
| 797 | + */ |
|
| 798 | 798 | public function applyRules() |
| 799 | 799 | { |
| 800 | 800 | $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
@@ -832,12 +832,12 @@ discard block |
||
| 832 | 832 | { |
| 833 | 833 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 834 | 834 | { |
| 835 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 835 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 836 | 836 | } |
| 837 | 837 | else |
| 838 | 838 | { |
| 839 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 840 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 839 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 840 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 841 | 841 | } |
| 842 | 842 | } |
| 843 | 843 | else |
@@ -854,15 +854,15 @@ discard block |
||
| 854 | 854 | $this->logging->log('action NOK : '.$action,INFO ); |
| 855 | 855 | if ($action >= 0) |
| 856 | 856 | { |
| 857 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 858 | - { |
|
| 859 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 860 | - } |
|
| 861 | - else |
|
| 862 | - { |
|
| 863 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 864 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 865 | - } |
|
| 857 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 858 | + { |
|
| 859 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 860 | + } |
|
| 861 | + else |
|
| 862 | + { |
|
| 863 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 864 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 865 | + } |
|
| 866 | 866 | } |
| 867 | 867 | else |
| 868 | 868 | { |
@@ -885,9 +885,9 @@ discard block |
||
| 885 | 885 | } |
| 886 | 886 | catch (Exception $e) |
| 887 | 887 | { |
| 888 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 889 | - $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 890 | - $this->trap_data['status']='error'; |
|
| 888 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 889 | + $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 890 | + $this->trap_data['status']='error'; |
|
| 891 | 891 | } |
| 892 | 892 | |
| 893 | 893 | } |
@@ -902,8 +902,8 @@ discard block |
||
| 902 | 902 | } |
| 903 | 903 | |
| 904 | 904 | /** Add Time a action to rule |
| 905 | - * @param string $time : time to process to insert in SQL |
|
| 906 | - */ |
|
| 905 | + * @param string $time : time to process to insert in SQL |
|
| 906 | + */ |
|
| 907 | 907 | public function add_rule_final($time) |
| 908 | 908 | { |
| 909 | 909 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -920,9 +920,9 @@ discard block |
||
| 920 | 920 | /*********** UTILITIES *********************/ |
| 921 | 921 | |
| 922 | 922 | /** reset service to OK after time defined in rule |
| 923 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 924 | - * @return integer : not in use |
|
| 925 | - **/ |
|
| 923 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 924 | + * @return integer : not in use |
|
| 925 | + **/ |
|
| 926 | 926 | public function reset_services() |
| 927 | 927 | { |
| 928 | 928 | // Get all services not in 'ok' state |