|
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
|
|
|
|
|
23
|
|
|
class TrapsController extends Controller |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var TrapModuleConfig $moduleConfig TrapModuleConfig instance */ |
|
26
|
|
|
protected $moduleConfig; |
|
27
|
|
|
/** @var TrapTableList $trapTableList (by date)*/ |
|
28
|
|
|
protected $trapTableList; |
|
29
|
|
|
/** @var TrapTableHostList $trapTableHostList TrapTableList (by hosts)*/ |
|
30
|
|
|
protected $trapTableHostList; |
|
31
|
|
|
/** @var HandlerTableList $handlerTableList HandlerTableList instance*/ |
|
32
|
|
|
protected $handlerTableList; |
|
33
|
|
|
/** @var ConfigObject $trapDB Trap database */ |
|
34
|
|
|
protected $trapDB; |
|
35
|
|
|
/** @var ConfigObject $icingaDB Icinga IDO database */ |
|
36
|
|
|
protected $icingaDB; |
|
37
|
|
|
/** @var MIBLoader $MIBData MIBLoader class */ |
|
38
|
|
|
protected $MIBData; |
|
39
|
|
|
/** @var Trap $trapClass Trap class for bin/trap_class.php */ |
|
40
|
|
|
protected $trapClass; |
|
41
|
|
|
/** @var UIDatabase $UIDatabase */ |
|
42
|
|
|
protected $UIDatabase; |
|
43
|
|
|
/** @var Icinga2Api $IcingaAPI */ |
|
44
|
|
|
protected $icingaAPI = NULL; |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** Get instance of TrapModuleConfig class |
|
49
|
|
|
* @return TrapModuleConfig |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getModuleConfig() |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->moduleConfig == Null) |
|
54
|
|
|
{ |
|
55
|
|
|
$db_prefix=$this->Config()->get('config', 'database_prefix'); |
|
56
|
|
|
if ($db_prefix === null) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->redirectNow('trapdirector/settings?message=No database prefix'); |
|
59
|
|
|
} |
|
60
|
|
|
$this->moduleConfig = new TrapModuleConfig($db_prefix); |
|
61
|
|
|
} |
|
62
|
|
|
return $this->moduleConfig; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get instance of TrapTableList |
|
67
|
|
|
* @return \Icinga\Module\Trapdirector\Tables\TrapTableList |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getTrapListTable() { |
|
70
|
|
|
if ($this->trapTableList == Null) { |
|
71
|
|
|
$this->trapTableList = new TrapTableList(); |
|
72
|
|
|
$this->trapTableList->setConfig($this->getModuleConfig()); |
|
73
|
|
|
} |
|
74
|
|
|
return $this->trapTableList; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return \Icinga\Module\Trapdirector\Tables\TrapTableHostList |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getTrapHostListTable() |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->trapTableHostList == Null) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->trapTableHostList = new TrapTableHostList(); |
|
85
|
|
|
$this->trapTableHostList->setConfig($this->getModuleConfig()); |
|
86
|
|
|
} |
|
87
|
|
|
return $this->trapTableHostList; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return \Icinga\Module\Trapdirector\Tables\HandlerTableList |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getHandlerListTable() |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->handlerTableList == Null) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->handlerTableList = new HandlerTableList(); |
|
98
|
|
|
$this->handlerTableList->setConfig($this->getModuleConfig()); |
|
99
|
|
|
} |
|
100
|
|
|
return $this->handlerTableList; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return UIDatabase |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getUIDatabase() |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->UIDatabase == Null) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->UIDatabase = new UIDatabase($this); |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
return $this->UIDatabase; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get Ido connection on API (first) or Database as fallback |
|
118
|
|
|
* @return \Icinga\Module\Trapdirector\TrapsActions\UIDatabase|\Icinga\Module\Trapdirector\Icinga2API |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getIdoConn() |
|
121
|
|
|
{ |
|
122
|
|
|
if ($this->icingaAPI === NULL) |
|
123
|
|
|
{ |
|
124
|
|
|
$host = $this->Config()->get('config', 'icingaAPI_host'); |
|
125
|
|
|
$port = $this->Config()->get('config', 'icingaAPI_port'); |
|
126
|
|
|
$user = $this->Config()->get('config', 'icingaAPI_user'); |
|
127
|
|
|
$pass = $this->Config()->get('config', 'icingaAPI_password'); |
|
128
|
|
|
$this->icingaAPI = new Icinga2API($host,$port); |
|
129
|
|
|
$this->icingaAPI->setCredentials($user, $pass); |
|
130
|
|
|
list($ret,$message) = $this->icingaAPI->test(NULL); |
|
|
|
|
|
|
131
|
|
|
if ($ret === TRUE) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
return $this->getUIDatabase(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
return $this->icingaAPI; |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
142
|
|
|
{ |
|
143
|
|
|
$limit = $this->params->get('limit', $limit); |
|
144
|
|
|
$page = $this->params->get('page', $offset); |
|
145
|
|
|
|
|
146
|
|
|
$paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
|
147
|
|
|
|
|
148
|
|
|
return $paginatable; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function displayExitError($source,$message) |
|
152
|
|
|
{ // TODO : check better ways to transmit data (with POST ?) |
|
153
|
|
|
$this->redirectNow('trapdirector/error?source='.$source.'&message='.$message); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
protected function checkReadPermission() |
|
157
|
|
|
{ |
|
158
|
|
|
if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
159
|
|
|
$this->displayExitError('Permissions','No permission fo view content'); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
protected function checkConfigPermission() |
|
164
|
|
|
{ |
|
165
|
|
|
if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
166
|
|
|
$this->displayExitError('Permissions','No permission fo configure'); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Check if user has write permission |
|
172
|
|
|
* @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
|
173
|
|
|
* @return boolean : user has permission |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function checkModuleConfigPermission($check=0) |
|
176
|
|
|
{ |
|
177
|
|
|
if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
178
|
|
|
if ($check == 0) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->displayExitError('Permissions','No permission fo configure module'); |
|
181
|
|
|
} |
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
return true; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/************************* Trap class get **********************/ |
|
188
|
|
|
public function getTrapClass() |
|
189
|
|
|
{ // TODO : try/catch here ? or within caller |
|
190
|
|
|
if ($this->trapClass == null) |
|
191
|
|
|
{ |
|
192
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
193
|
|
|
$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
194
|
|
|
//$debug_level=4; |
|
195
|
|
|
$this->trapClass = new Trap($icingaweb2_etc); |
|
196
|
|
|
//$Trap->setLogging($debug_level,'syslog'); |
|
197
|
|
|
} |
|
198
|
|
|
return $this->trapClass; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/************************** MIB related **************************/ |
|
202
|
|
|
|
|
203
|
|
|
/** Get MIBLoader class |
|
204
|
|
|
* @return MIBLoader class |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function getMIB() |
|
207
|
|
|
{ |
|
208
|
|
|
if ($this->MIBData == null) |
|
209
|
|
|
{ |
|
210
|
|
|
$dbConn = $this->getUIDatabase()->getDbConn(); |
|
211
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
212
|
|
|
$this->MIBData=new MIBLoader( |
|
213
|
|
|
$this->Config()->get('config', 'snmptranslate'), |
|
214
|
|
|
$this->Config()->get('config', 'snmptranslate_dirs'), |
|
215
|
|
|
$dbConn, |
|
216
|
|
|
$this->getModuleConfig() |
|
217
|
|
|
); |
|
218
|
|
|
} |
|
219
|
|
|
return $this->MIBData; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/************************** Database queries *******************/ |
|
223
|
|
|
|
|
224
|
|
|
/** Check if director is installed |
|
225
|
|
|
* @return bool true/false |
|
226
|
|
|
*/ |
|
227
|
|
|
protected function isDirectorInstalled() |
|
228
|
|
|
{ |
|
229
|
|
|
$output=array(); |
|
230
|
|
|
exec('icingacli module list',$output); |
|
231
|
|
|
foreach ($output as $line) |
|
232
|
|
|
{ |
|
233
|
|
|
if (preg_match('/^director .*enabled/',$line)) |
|
234
|
|
|
{ |
|
235
|
|
|
return true; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
return false; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
|