1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Icinga\Module\Trapdirector\Controllers; |
4
|
|
|
|
5
|
|
|
//use Icinga\Web\Controller; |
6
|
|
|
//use Icinga\Web\Url; |
7
|
|
|
use Exception; |
8
|
|
|
use Icinga\Module\Trapdirector\TrapsController; |
9
|
|
|
use Trapdirector\Trap; |
10
|
|
|
|
11
|
|
|
class HelperController extends TrapsController |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** Get host list with filter (IP or name) : host=<filter> |
15
|
|
|
* returns in JSON : status=>OK/NOK hosts=>array of hosts |
16
|
|
|
*/ |
17
|
|
|
public function gethostsAction() |
18
|
|
|
{ |
19
|
|
|
$postData=$this->getRequest()->getPost(); |
20
|
|
|
|
21
|
|
|
$hostFilter = $this->checkPostVar($postData, 'hostFilter', '.*'); |
22
|
|
|
|
23
|
|
|
$retHosts=array('status'=>'OK','hosts' => array()); |
24
|
|
|
|
25
|
|
|
$hosts=$this->getUIDatabase()->getHostByIP($hostFilter); |
26
|
|
|
foreach ($hosts as $val) |
27
|
|
|
{ |
28
|
|
|
array_push($retHosts['hosts'],$val->name); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->_helper->json($retHosts); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** Get hostgroup list with filter (name) : hostgroup=<hostFilter> |
35
|
|
|
* returns in JSON : status=>OK/NOK hosts=>array of hosts |
36
|
|
|
*/ |
37
|
|
|
public function gethostgroupsAction() |
38
|
|
|
{ |
39
|
|
|
$postData=$this->getRequest()->getPost(); |
40
|
|
|
|
41
|
|
|
$hostFilter = $this->checkPostVar($postData, 'hostFilter', '.*'); |
42
|
|
|
|
43
|
|
|
$retHosts=array('status'=>'OK','hosts' => array()); |
44
|
|
|
|
45
|
|
|
$hosts=$this->getUIDatabase()->getHostGroupByName($hostFilter); |
46
|
|
|
foreach ($hosts as $val) |
47
|
|
|
{ |
48
|
|
|
array_push($retHosts['hosts'],$val->name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->_helper->json($retHosts); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** Get service list by host name ( host=<host> ) |
55
|
|
|
* returns in JSON : |
56
|
|
|
* status=>OK/No services found/More than one host matches |
57
|
|
|
* services=>array of services (name) |
58
|
|
|
* hostid = host object id or -1 if not found. |
59
|
|
|
*/ |
60
|
|
|
public function getservicesAction() |
61
|
|
|
{ |
62
|
|
|
$postData=$this->getRequest()->getPost(); |
63
|
|
|
|
64
|
|
|
$host=$this->checkPostVar($postData, 'host', '.*'); |
|
|
|
|
65
|
|
|
if (isset($postData['host'])) |
66
|
|
|
{ |
67
|
|
|
$host=$postData['host']; |
68
|
|
|
} |
69
|
|
|
else |
70
|
|
|
{ |
71
|
|
|
$this->_helper->json(array('status'=>'No Hosts','hostid' => -1)); |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$hostArray=$this->getUIDatabase()->getHostByName($host); |
76
|
|
|
if (count($hostArray) > 1) |
77
|
|
|
{ |
78
|
|
|
$this->_helper->json(array('status'=>'More than one host matches','hostid' => -1)); |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
else if (count($hostArray) == 0) |
82
|
|
|
{ |
83
|
|
|
$this->_helper->json(array('status'=>'No host matches','hostid' => -1)); |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
$services=$this->getUIDatabase()->getServicesByHostid($hostArray[0]->id); |
87
|
|
|
if (count($services) < 1) |
88
|
|
|
{ |
89
|
|
|
$this->_helper->json(array('status'=>'No services found for host','hostid' => $hostArray[0]->id)); |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
$retServices=array('status'=>'OK','services' => array(),'hostid' => $hostArray[0]->id); |
93
|
|
|
foreach ($services as $val) |
94
|
|
|
{ |
95
|
|
|
array_push($retServices['services'],array($val->id , $val->name)); |
96
|
|
|
} |
97
|
|
|
$this->_helper->json($retServices); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** Get service list by host group ( name=<host> ) |
101
|
|
|
* returns in JSON : |
102
|
|
|
* status=>OK/No services found/More than one host matches |
103
|
|
|
* services=>array of services (name) |
104
|
|
|
* groupid = group object id or -1 if not found. |
105
|
|
|
*/ |
106
|
|
|
public function gethostgroupservicesAction() |
107
|
|
|
{ |
108
|
|
|
$postData=$this->getRequest()->getPost(); |
109
|
|
|
|
110
|
|
|
$host = $this->checkPostVar($postData, 'host', '.+'); |
111
|
|
|
|
112
|
|
|
$hostArray=$this->getUIDatabase()->getHostGroupByName($host); |
113
|
|
|
if (count($hostArray) > 1) |
114
|
|
|
{ |
115
|
|
|
$this->_helper->json(array('status'=>'More than one hostgroup matches','hostid' => -1)); |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
else if (count($hostArray) == 0) |
119
|
|
|
{ |
120
|
|
|
$this->_helper->json(array('status'=>'No hostgroup matches','hostid' => -1)); |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
$services=$this->getUIDatabase()->getServicesByHostGroupid($hostArray[0]->id); |
124
|
|
|
if (count($services) < 1) |
125
|
|
|
{ |
126
|
|
|
$this->_helper->json(array('status'=>'No services found for hostgroup','hostid' => $hostArray[0]->id)); |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
$retServices=array('status'=>'OK','services' => $services,'hostid' => $hostArray[0]->id); |
130
|
|
|
|
131
|
|
|
$this->_helper->json($retServices); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** Get traps from mib : entry : mib=<mib> |
135
|
|
|
* returns in JSON : |
136
|
|
|
* status=>OK/No mib/Error getting mibs |
137
|
|
|
* traps=>array of array( oid -> name) |
138
|
|
|
*/ |
139
|
|
|
public function gettrapsAction() |
140
|
|
|
{ |
141
|
|
|
$postData=$this->getRequest()->getPost(); |
142
|
|
|
|
143
|
|
|
$mib = $this->checkPostVar($postData, 'mib', '.*'); |
144
|
|
|
|
145
|
|
|
try |
146
|
|
|
{ |
147
|
|
|
$traplist=$this->getMIB()->getTrapList($mib); |
148
|
|
|
$retTraps=array('status'=>'OK','traps' => $traplist); |
149
|
|
|
} |
150
|
|
|
catch (Exception $e) |
151
|
|
|
{ |
152
|
|
|
$retTraps=array('status' => 'Error getting mibs'); |
153
|
|
|
} |
154
|
|
|
$this->_helper->json($retTraps); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** Get trap objects from mib : entry : trap=<oid> |
158
|
|
|
* returns in JSON : |
159
|
|
|
* status=>OK/no trap/not found |
160
|
|
|
* objects=>array of array( oid -> name, oid->mib) |
161
|
|
|
*/ |
162
|
|
|
public function gettrapobjectsAction() |
163
|
|
|
{ |
164
|
|
|
$postData=$this->getRequest()->getPost(); |
165
|
|
|
|
166
|
|
|
$trap = $this->checkPostVar($postData, 'trap', '.*'); |
167
|
|
|
|
168
|
|
|
try |
169
|
|
|
{ |
170
|
|
|
$objectlist=$this->getMIB()->getObjectList($trap); |
171
|
|
|
$retObjects=array('status'=>'OK','objects' => $objectlist); |
172
|
|
|
} |
173
|
|
|
catch (Exception $e) |
174
|
|
|
{ |
175
|
|
|
$retObjects=array('status' => 'not found'); |
176
|
|
|
} |
177
|
|
|
$this->_helper->json($retObjects); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** Get list of all loaded mibs : entry : none |
181
|
|
|
* return : array of strings. |
182
|
|
|
*/ |
183
|
|
|
public function getmiblistAction() |
184
|
|
|
{ |
185
|
|
|
try |
186
|
|
|
{ |
187
|
|
|
$miblist=$this->getMIB()->getMIBList(); |
188
|
|
|
} |
189
|
|
|
catch (Exception $e) |
190
|
|
|
{ |
191
|
|
|
$miblist=array('Error getting mibs'); |
192
|
|
|
} |
193
|
|
|
$this->_helper->json($miblist); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** Get MIB::Name from OID : entry : oid |
197
|
|
|
* status=>OK/No oid/not found |
198
|
|
|
* mib=>string |
199
|
|
|
* name=>string |
200
|
|
|
*/ |
201
|
|
|
public function translateoidAction() |
202
|
|
|
{ |
203
|
|
|
$postData=$this->getRequest()->getPost(); |
204
|
|
|
|
205
|
|
|
$oid = $this->checkPostVar($postData, 'oid', '.*'); |
206
|
|
|
|
207
|
|
|
// Try to get oid name from snmptranslate |
208
|
|
|
if (($object=$this->getMIB()->translateOID($oid)) == null) |
209
|
|
|
{ |
210
|
|
|
$this->_helper->json(array('status'=>'Not found')); |
211
|
|
|
return; |
212
|
|
|
} |
213
|
|
|
else |
214
|
|
|
{ |
215
|
|
|
$this->_helper->json( |
216
|
|
|
array('status'=>'OK', |
217
|
|
|
'mib' => $object['mib'], |
218
|
|
|
'name' => $object['name'], |
219
|
|
|
'type' => $object['type'], |
220
|
|
|
'type_enum' => $object['type_enum'], |
221
|
|
|
'description' => $object['description'] |
222
|
|
|
) |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** Save or execute database purge of <n> days |
229
|
|
|
* days=>int |
230
|
|
|
* action=>save/execute |
231
|
|
|
* return : status=>OK/Message error |
232
|
|
|
*/ |
233
|
|
|
public function dbmaintenanceAction() |
234
|
|
|
{ |
235
|
|
|
|
236
|
|
|
$postData=$this->getRequest()->getPost(); |
237
|
|
|
|
238
|
|
|
$days = $this->checkPostVar($postData, 'days', '^[0-9]+$'); |
239
|
|
|
$days=intval($days); |
240
|
|
|
|
241
|
|
|
$action = $this->checkPostVar($postData, 'action', 'save|execute'); |
242
|
|
|
|
243
|
|
|
if ($action == 'save') |
244
|
|
|
{ |
245
|
|
|
try |
246
|
|
|
{ |
247
|
|
|
$this->getUIDatabase()->setDBConfigValue('db_remove_days',$days); |
248
|
|
|
} |
249
|
|
|
catch (Exception $e) |
250
|
|
|
{ |
251
|
|
|
$this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
252
|
|
|
return; |
253
|
|
|
} |
254
|
|
|
$this->_helper->json(array('status'=>'OK')); |
255
|
|
|
return; |
256
|
|
|
} |
257
|
|
|
if ($action == 'execute') |
258
|
|
|
{ |
259
|
|
|
try |
260
|
|
|
{ |
261
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
262
|
|
|
$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
263
|
|
|
$debug_level=4; |
264
|
|
|
$trap = new Trap($icingaweb2_etc); |
265
|
|
|
$trap->setLogging($debug_level,'syslog'); |
266
|
|
|
$trap->eraseOldTraps($days); |
267
|
|
|
} |
268
|
|
|
catch (Exception $e) |
269
|
|
|
{ |
270
|
|
|
$this->_helper->json(array('status'=>'execute error : '.$e->getMessage() )); |
271
|
|
|
return; |
272
|
|
|
} |
273
|
|
|
$this->_helper->json(array('status'=>'OK')); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** Save snmp configuration to db |
279
|
|
|
* use=>1 (true) or 0 |
280
|
|
|
* oid=>specific oid or '' for default |
281
|
|
|
* return : status=>OK/Message error |
282
|
|
|
*/ |
283
|
|
|
public function snmpconfigAction() |
284
|
|
|
{ |
285
|
|
|
$postData=$this->getRequest()->getPost(); |
286
|
|
|
|
287
|
|
|
$snmpUse = $this->checkPostVar($postData, 'useTrapAddr', '0|1'); |
288
|
|
|
|
289
|
|
|
$snmpOID = $this->checkPostVar($postData, 'trapAddrOID', '^[\.0-9]+$'); |
290
|
|
|
|
291
|
|
|
try |
292
|
|
|
{ |
293
|
|
|
$this->getUIDatabase()->setDBConfigValue('use_SnmpTrapAddess',$snmpUse); |
294
|
|
|
$this->getUIDatabase()->setDBConfigValue('SnmpTrapAddess_oid',$snmpOID); |
295
|
|
|
} |
296
|
|
|
catch (Exception $e) |
297
|
|
|
{ |
298
|
|
|
$this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
299
|
|
|
return; |
300
|
|
|
} |
301
|
|
|
$this->_helper->json(array('status'=>'OK')); |
302
|
|
|
return; |
303
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** Save log output to db |
307
|
|
|
* destination=>log destination |
308
|
|
|
* file=>file name |
309
|
|
|
* level => int |
310
|
|
|
* return : status=>OK/Message error |
311
|
|
|
*/ |
312
|
|
|
public function logdestinationAction() |
313
|
|
|
{ |
314
|
|
|
$postData=$this->getRequest()->getPost(); |
315
|
|
|
|
316
|
|
|
$destination = $this->checkPostVar($postData, 'destination', '.*'); |
317
|
|
|
$logDest=$this->getModuleConfig()->getLogDestinations(); |
318
|
|
|
if (!isset($logDest[$destination])) |
319
|
|
|
{ |
320
|
|
|
$this->_helper->json(array('status'=>'invalid destination : '.$destination)); |
321
|
|
|
return; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
if (isset($postData['file'])) |
325
|
|
|
{ |
326
|
|
|
$file=$postData['file']; |
327
|
|
|
$fileHandler=@fopen($file,'w'); |
328
|
|
|
if ($fileHandler == false) |
329
|
|
|
{ // File os note writabe / cannot create |
330
|
|
|
$this->_helper->json(array('status'=>'File not writable : '.$file)); |
331
|
|
|
return; |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
else |
335
|
|
|
{ |
336
|
|
|
if ($destination != 'file') |
337
|
|
|
{ |
338
|
|
|
$file=null; |
339
|
|
|
} |
340
|
|
|
else |
341
|
|
|
{ |
342
|
|
|
$this->_helper->json(array('status'=>'No file')); |
343
|
|
|
return; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$level = $this->checkPostVar($postData, 'level', '[0-9]'); |
348
|
|
|
|
349
|
|
|
try |
350
|
|
|
{ |
351
|
|
|
$this->getUIDatabase()->setDBConfigValue('log_destination',$destination); |
352
|
|
|
$this->getUIDatabase()->setDBConfigValue('log_file',$file); |
353
|
|
|
$this->getUIDatabase()->setDBConfigValue('log_level',$level); |
354
|
|
|
} |
355
|
|
|
catch (Exception $e) |
356
|
|
|
{ |
357
|
|
|
$this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
358
|
|
|
return; |
359
|
|
|
} |
360
|
|
|
$this->_helper->json(array('status'=>'OK')); |
361
|
|
|
return; |
362
|
|
|
|
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** Test a rule evaluation |
366
|
|
|
* rule=>rule to evaluate |
367
|
|
|
* action=>'evaluate' |
368
|
|
|
* return : status=>OK/Message error & message : return of evaluation |
369
|
|
|
*/ |
370
|
|
|
public function testruleAction() |
371
|
|
|
{ |
372
|
|
|
|
373
|
|
|
$postData=$this->getRequest()->getPost(); |
374
|
|
|
|
375
|
|
|
$rule = $this->checkPostVar($postData, 'rule', '.*'); |
376
|
|
|
|
377
|
|
|
$action = $this->checkPostVar($postData, 'action', 'evaluate'); |
378
|
|
|
|
379
|
|
|
if ($action == 'evaluate') |
380
|
|
|
{ |
381
|
|
|
try |
382
|
|
|
{ |
383
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
384
|
|
|
$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
385
|
|
|
$trap = new Trap($icingaweb2_etc); |
386
|
|
|
// Cleanup spaces before eval |
387
|
|
|
$rule=$trap->ruleClass->eval_cleanup($rule); |
388
|
|
|
// Eval |
389
|
|
|
$item=0; |
390
|
|
|
$rule=$trap->ruleClass->evaluation($rule,$item); |
391
|
|
|
} |
392
|
|
|
catch (Exception $e) |
393
|
|
|
{ |
394
|
|
|
$this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage() )); |
395
|
|
|
return; |
396
|
|
|
} |
397
|
|
|
$return=($rule==true)?'true':'false'; |
398
|
|
|
$this->_helper->json(array('status'=>'OK', 'message' => $return)); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** Test a rule evaluation |
404
|
|
|
* name => name of plugin |
405
|
|
|
* action => enable | disable |
406
|
|
|
* return : status=>OK/Message error |
407
|
|
|
*/ |
408
|
|
|
public function pluginAction() |
409
|
|
|
{ |
410
|
|
|
$postData=$this->getRequest()->getPost(); |
411
|
|
|
|
412
|
|
|
$pluginName = $this->checkPostVar($postData, 'name', '.*'); |
413
|
|
|
|
414
|
|
|
$action = $this->checkPostVar($postData, 'action', 'enable|disable'); |
415
|
|
|
|
416
|
|
|
try |
417
|
|
|
{ |
418
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
419
|
|
|
$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
420
|
|
|
$trap = new Trap($icingaweb2_etc); |
421
|
|
|
// Enable plugin. |
422
|
|
|
$action=($action == 'enable') ? true : false; |
423
|
|
|
$retVal=$trap->pluginClass->enablePlugin($pluginName, $action); |
424
|
|
|
|
425
|
|
|
} |
426
|
|
|
catch (Exception $e) |
427
|
|
|
{ |
428
|
|
|
$this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
429
|
|
|
return; |
430
|
|
|
} |
431
|
|
|
if ($retVal === true) |
432
|
|
|
{ |
433
|
|
|
$this->_helper->json(array('status'=>'OK')); |
434
|
|
|
} |
435
|
|
|
else |
436
|
|
|
{ |
437
|
|
|
$this->_helper->json(array('status'=>'Error, see logs')); |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** Function evaluation |
442
|
|
|
* function => name of function |
443
|
|
|
* action => evaluate |
444
|
|
|
* return : status=>OK/Message error & message : return of evaluation ('true' or 'false' ) |
445
|
|
|
*/ |
446
|
|
|
public function functionAction() |
447
|
|
|
{ |
448
|
|
|
$postData=$this->getRequest()->getPost(); |
449
|
|
|
|
450
|
|
|
$functionString = $this->checkPostVar($postData, 'function', '.*'); |
451
|
|
|
|
452
|
|
|
$this->checkPostVar($postData, 'action', 'evaluate'); |
453
|
|
|
|
454
|
|
|
// Only one action possible for now, no tests on action. |
455
|
|
|
try |
456
|
|
|
{ |
457
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
458
|
|
|
$icingaweb2Etc=$this->Config()->get('config', 'icingaweb2_etc'); |
459
|
|
|
$trap = new Trap($icingaweb2Etc); |
460
|
|
|
// load all plugins in case tested function is not enabled. |
461
|
|
|
$trap->pluginClass->registerAllPlugins(false); |
462
|
|
|
// Clean all spaces |
463
|
|
|
$functionString = $trap->ruleClass->eval_cleanup($functionString); |
464
|
|
|
// Eval functions |
465
|
|
|
$result = $trap->pluginClass->evaluateFunctionString($functionString); |
466
|
|
|
} |
467
|
|
|
catch (Exception $e) |
468
|
|
|
{ |
469
|
|
|
$this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
470
|
|
|
return; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
$result = ($result === true)?'True':'False'; |
474
|
|
|
$this->_helper->json(array('status'=>'OK','message' => $result)); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/************** Utilities **********************/ |
478
|
|
|
|
479
|
|
|
private function checkPostVar(array $postData,string $postVar, string $validRegexp) : string |
480
|
|
|
{ |
481
|
|
|
if (!isset ($postData[$postVar])) |
482
|
|
|
{ |
483
|
|
|
$this->_helper->json(array('status'=>'No ' . $postVar)); |
484
|
|
|
return ''; |
485
|
|
|
} |
486
|
|
|
if (preg_match('/'.$validRegexp.'/', $postData[$postVar]) != 1) |
487
|
|
|
{ |
488
|
|
|
$this->_helper->json(array('status'=>'Unknown ' . $postVar . ' value '.$postData[$postVar])); |
489
|
|
|
return ''; |
490
|
|
|
} |
491
|
|
|
return $postData[$postVar]; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|