1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Icinga\Module\Trapdirector; |
4
|
|
|
|
5
|
|
|
use Icinga\Web\Controller; |
6
|
|
|
|
7
|
|
|
use Icinga\Data\Paginatable; |
8
|
|
|
|
9
|
|
|
//use Exception; |
10
|
|
|
|
11
|
|
|
use Icinga\Module\Trapdirector\Config\TrapModuleConfig; |
12
|
|
|
use Icinga\Module\Trapdirector\Tables\TrapTableList; |
13
|
|
|
use Icinga\Module\Trapdirector\Tables\TrapTableHostList; |
14
|
|
|
use Icinga\Module\Trapdirector\Tables\HandlerTableList; |
15
|
|
|
use Icinga\Module\Trapdirector\Config\MIBLoader; |
16
|
|
|
use Icinga\Module\Trapdirector\TrapsActions\UIDatabase; |
17
|
|
|
use Icinga\Module\Trapdirector\Icinga2API; |
18
|
|
|
|
19
|
|
|
use Trapdirector\Trap; |
20
|
|
|
|
21
|
|
|
use Icinga\Data\ConfigObject; |
22
|
|
|
use Icinga\Exception\ProgrammingError; |
23
|
|
|
|
24
|
|
|
class TrapsController extends Controller |
25
|
|
|
{ |
26
|
|
|
/** @var TrapModuleConfig $moduleConfig TrapModuleConfig instance */ |
27
|
|
|
protected $moduleConfig; |
28
|
|
|
/** @var TrapTableList $trapTableList (by date)*/ |
29
|
|
|
protected $trapTableList; |
30
|
|
|
/** @var TrapTableHostList $trapTableHostList TrapTableList (by hosts)*/ |
31
|
|
|
protected $trapTableHostList; |
32
|
|
|
/** @var HandlerTableList $handlerTableList HandlerTableList instance*/ |
33
|
|
|
protected $handlerTableList; |
34
|
|
|
/** @var ConfigObject $trapDB Trap database */ |
35
|
|
|
protected $trapDB; |
36
|
|
|
/** @var ConfigObject $icingaDB Icinga IDO database */ |
37
|
|
|
protected $icingaDB; |
38
|
|
|
/** @var MIBLoader $MIBData MIBLoader class */ |
39
|
|
|
protected $MIBData; |
40
|
|
|
/** @var Trap $trapClass Trap class for bin/trap_class.php */ |
41
|
|
|
protected $trapClass; |
42
|
|
|
/** @var UIDatabase $UIDatabase */ |
43
|
|
|
protected $UIDatabase; |
44
|
|
|
/** @var Icinga2Api $IcingaAPI */ |
45
|
|
|
protected $icingaAPI = NULL; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** Get instance of TrapModuleConfig class |
50
|
|
|
* @return TrapModuleConfig |
51
|
|
|
*/ |
52
|
|
|
public function getModuleConfig() |
53
|
|
|
{ |
54
|
|
|
if ($this->moduleConfig == Null) |
55
|
|
|
{ |
56
|
|
|
$db_prefix=$this->Config()->get('config', 'database_prefix'); |
57
|
|
|
if ($db_prefix === null) |
58
|
|
|
{ |
59
|
|
|
$this->redirectNow('trapdirector/settings?message=No database prefix'); |
60
|
|
|
} |
61
|
|
|
$this->moduleConfig = new TrapModuleConfig($db_prefix); |
62
|
|
|
} |
63
|
|
|
return $this->moduleConfig; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get instance of TrapTableList |
68
|
|
|
* @return \Icinga\Module\Trapdirector\Tables\TrapTableList |
69
|
|
|
*/ |
70
|
|
|
public function getTrapListTable() { |
71
|
|
|
if ($this->trapTableList == Null) { |
72
|
|
|
$this->trapTableList = new TrapTableList(); |
73
|
|
|
$this->trapTableList->setConfig($this->getModuleConfig()); |
74
|
|
|
} |
75
|
|
|
return $this->trapTableList; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return \Icinga\Module\Trapdirector\Tables\TrapTableHostList |
80
|
|
|
*/ |
81
|
|
|
public function getTrapHostListTable() |
82
|
|
|
{ |
83
|
|
|
if ($this->trapTableHostList == Null) |
84
|
|
|
{ |
85
|
|
|
$this->trapTableHostList = new TrapTableHostList(); |
86
|
|
|
$this->trapTableHostList->setConfig($this->getModuleConfig()); |
87
|
|
|
} |
88
|
|
|
return $this->trapTableHostList; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return \Icinga\Module\Trapdirector\Tables\HandlerTableList |
93
|
|
|
*/ |
94
|
|
|
public function getHandlerListTable() |
95
|
|
|
{ |
96
|
|
|
if ($this->handlerTableList == Null) |
97
|
|
|
{ |
98
|
|
|
$this->handlerTableList = new HandlerTableList(); |
99
|
|
|
$this->handlerTableList->setConfig($this->getModuleConfig()); |
100
|
|
|
} |
101
|
|
|
return $this->handlerTableList; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return UIDatabase |
106
|
|
|
*/ |
107
|
|
|
public function getUIDatabase() |
108
|
|
|
{ |
109
|
|
|
if ($this->UIDatabase == Null) |
110
|
|
|
{ |
111
|
|
|
$this->UIDatabase = new UIDatabase($this); |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
return $this->UIDatabase; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get Ido connection on API (first) or Database as fallback |
119
|
|
|
* @return \Icinga\Module\Trapdirector\TrapsActions\UIDatabase|\Icinga\Module\Trapdirector\Icinga2API |
120
|
|
|
*/ |
121
|
|
|
public function getIdoConn() |
122
|
|
|
{ |
123
|
|
|
if ($this->icingaAPI === NULL) |
124
|
|
|
{ |
125
|
|
|
$host = $this->Config()->get('config', 'icingaAPI_host'); |
126
|
|
|
$port = $this->Config()->get('config', 'icingaAPI_port'); |
127
|
|
|
$user = $this->Config()->get('config', 'icingaAPI_user'); |
128
|
|
|
$pass = $this->Config()->get('config', 'icingaAPI_password'); |
129
|
|
|
$this->icingaAPI = new Icinga2API($host,$port); |
130
|
|
|
$this->icingaAPI->setCredentials($user, $pass); |
131
|
|
|
list($ret,$message) = $this->icingaAPI->test(NULL); |
|
|
|
|
132
|
|
|
if ($ret === TRUE) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
return $this->getUIDatabase(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
return $this->icingaAPI; |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
143
|
|
|
{ |
144
|
|
|
$limit = $this->params->get('limit', $limit); |
145
|
|
|
$page = $this->params->get('page', $offset); |
146
|
|
|
|
147
|
|
|
$paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
148
|
|
|
|
149
|
|
|
return $paginatable; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function displayExitError($source,$message) |
153
|
|
|
{ // TODO : check better ways to transmit data (with POST ?) |
154
|
|
|
$this->redirectNow('trapdirector/error?source='.$source.'&message='.$message); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
protected function checkReadPermission() |
158
|
|
|
{ |
159
|
|
|
if (! $this->Auth()->hasPermission('trapdirector/view')) { |
160
|
|
|
$this->displayExitError('Permissions','No permission fo view content'); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
protected function checkConfigPermission() |
165
|
|
|
{ |
166
|
|
|
if (! $this->Auth()->hasPermission('trapdirector/config')) { |
167
|
|
|
$this->displayExitError('Permissions','No permission fo configure'); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Check if user has write permission |
173
|
|
|
* @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
174
|
|
|
* @return boolean : user has permission |
175
|
|
|
*/ |
176
|
|
|
protected function checkModuleConfigPermission($check=0) |
177
|
|
|
{ |
178
|
|
|
if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
179
|
|
|
if ($check == 0) |
180
|
|
|
{ |
181
|
|
|
$this->displayExitError('Permissions','No permission fo configure module'); |
182
|
|
|
} |
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/************************* Trap class get **********************/ |
189
|
|
|
public function getTrapClass() |
190
|
|
|
{ // TODO : try/catch here ? or within caller |
191
|
|
|
if ($this->trapClass == null) |
192
|
|
|
{ |
193
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
194
|
|
|
$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
195
|
|
|
//$debug_level=4; |
196
|
|
|
$this->trapClass = new Trap($icingaweb2_etc); |
197
|
|
|
//$Trap->setLogging($debug_level,'syslog'); |
198
|
|
|
} |
199
|
|
|
return $this->trapClass; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/************************** MIB related **************************/ |
203
|
|
|
|
204
|
|
|
/** Get MIBLoader class |
205
|
|
|
* @return MIBLoader class |
206
|
|
|
*/ |
207
|
|
|
protected function getMIB() |
208
|
|
|
{ |
209
|
|
|
if ($this->MIBData == null) |
210
|
|
|
{ |
211
|
|
|
$dbConn = $this->getUIDatabase()->getDbConn(); |
212
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
213
|
|
|
$this->MIBData=new MIBLoader( |
214
|
|
|
$this->Config()->get('config', 'snmptranslate'), |
215
|
|
|
$this->Config()->get('config', 'snmptranslate_dirs'), |
216
|
|
|
$dbConn, |
217
|
|
|
$this->getModuleConfig() |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
return $this->MIBData; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/************************** Database queries *******************/ |
224
|
|
|
|
225
|
|
|
/** Check if director is installed |
226
|
|
|
* @return bool true/false |
227
|
|
|
*/ |
228
|
|
|
protected function isDirectorInstalled() |
229
|
|
|
{ |
230
|
|
|
$output=array(); |
231
|
|
|
exec('icingacli module list',$output); |
232
|
|
|
foreach ($output as $line) |
233
|
|
|
{ |
234
|
|
|
if (preg_match('/^director .*enabled/',$line)) |
235
|
|
|
{ |
236
|
|
|
return true; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
return false; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
/************************ UI elements **************************/ |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* get max rows to display before paging. |
247
|
|
|
* @return number |
248
|
|
|
*/ |
249
|
|
|
public function itemListDisplay() |
250
|
|
|
{ |
251
|
|
|
return $this->getUIDatabase()->getDBConfigValue('max_rows_in_list'); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function setitemListDisplay(int $maxRows) |
255
|
|
|
{ |
256
|
|
|
return $this->getUIDatabase()->setDBConfigValue('max_rows_in_list',$maxRows); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* get Handlers categories list (index => textvalue). |
261
|
|
|
* @return array |
262
|
|
|
*/ |
263
|
|
|
public function getHandlersCategory() |
264
|
|
|
{ |
265
|
|
|
//<index>:<name>!<index>:<name> |
266
|
|
|
$catList = $this->getUIDatabase()->getDBConfigValue('handler_categories'); |
267
|
|
|
$catListArray=explode('!',$catList); |
268
|
|
|
$retArray=array(); |
269
|
|
|
foreach ($catListArray as $category) |
270
|
|
|
{ |
271
|
|
|
$catArray=explode(':',$category); |
272
|
|
|
$retArray[$catArray[0]] = $catArray[1]; |
273
|
|
|
} |
274
|
|
|
return $retArray; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function setHandlerCategory(array $catArray) |
278
|
|
|
{ |
279
|
|
|
$catString=''; |
280
|
|
|
foreach ($catArray as $index => $value) |
281
|
|
|
{ |
282
|
|
|
if ($catString != '' ) $catString .= '!'; |
283
|
|
|
$catString .= $index . ':' . $value; |
284
|
|
|
} |
285
|
|
|
$this->getUIDatabase()->setDBConfigValue('handler_categories', $catString); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function addHandlersCategory(string $catName) |
289
|
|
|
{ |
290
|
|
|
$catArray = $this->getHandlersCategory(); |
291
|
|
|
$i=1; |
292
|
|
|
while (isset($catArray[$i]) && $i < 100) $i++; |
293
|
|
|
if ($i == 100) throw new ProgrammingError('Category array error'); |
294
|
|
|
$catArray[$i] = $catName; |
295
|
|
|
$this->setHandlerCategory($catArray); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
public function delHandlersCategory(int $catIndex) |
299
|
|
|
{ |
300
|
|
|
$catArray = $this->getHandlersCategory(); |
301
|
|
|
unset($catArray[$catIndex]); |
302
|
|
|
$this->setHandlerCategory($catArray); |
303
|
|
|
$this->getUIDatabase()->updateHandlersOnCategoryDelete($catIndex); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function renameHandlersCategory(int $catIndex, string $catName) |
307
|
|
|
{ |
308
|
|
|
$catArray = $this->getHandlersCategory(); |
309
|
|
|
$catArray[$catIndex] = $catName; |
310
|
|
|
$this->setHandlerCategory($catArray); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
|