1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Icinga\Module\TrapDirector\Tables; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Icinga\Web\Request; |
7
|
|
|
use Icinga\Web\Url; |
8
|
|
|
use Icinga\Web\Widget; |
9
|
|
|
use Icinga\Web\Widget\Paginator; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Icinga\Module\Trapdirector\Tables\TrapTable; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class TrapTableHostList extends TrapTable |
16
|
|
|
{ |
17
|
|
|
// Db connection : getConnection / setConnection |
18
|
|
|
protected $connection; |
19
|
|
|
|
20
|
|
|
// Host grouping |
21
|
|
|
protected $lastHost; |
22
|
|
|
// Filters |
23
|
|
|
|
24
|
|
|
protected $filter; |
25
|
|
|
protected $enforcedFilters = array(); |
26
|
|
|
protected $searchColumns = array(); |
27
|
|
|
|
28
|
|
|
protected function getTitles() { |
29
|
|
|
// TODO : check moduleconfig is set |
30
|
|
|
return $this->moduleConfig->getTrapHostListTitles(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// ****************** Render table in html |
34
|
|
|
|
35
|
|
|
// Host grouping |
36
|
|
|
protected function renderHostIfNew($IP,$hostname) |
37
|
|
|
{ |
38
|
|
|
$view = $this->getView(); |
39
|
|
|
|
40
|
|
|
if ($this->lastHost === $IP) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($this->lastHost === null) |
45
|
|
|
{ |
46
|
|
|
$htm = "<thead>\n <tr>\n"; |
47
|
|
|
} else { |
48
|
|
|
$htm = "</tbody>\n<thead>\n <tr>\n"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($this->columnCount === null) |
52
|
|
|
{ |
53
|
|
|
$this->columnCount = count($this->getTitles()); |
54
|
|
|
} |
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
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->lastHost = $IP; |
69
|
|
|
|
70
|
|
|
return $htm . "<tbody>\n"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function __toString() |
74
|
|
|
{ |
75
|
|
|
return $this->render(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function render() |
79
|
|
|
{ |
80
|
|
|
$data=$this->getTable(); |
81
|
|
|
$view = $this->getView(); |
82
|
|
|
$this->columnCount = count($this->getTitles()); |
83
|
|
|
$this->lastHost=null; |
84
|
|
|
// Table start |
85
|
|
|
$htm = '<table class="simple common-table table-row-selectable">'; |
86
|
|
|
|
87
|
|
|
// Titles |
88
|
|
|
$htm .= "<thead>\n <tr>\n"; |
89
|
|
|
$titles = $this->getTitles(); |
90
|
|
|
foreach ($titles as $title) |
91
|
|
|
{ |
92
|
|
|
$htm .= ' <th>' . $view->escape($view->translate($title)) . "</th>\n"; |
93
|
|
|
} |
94
|
|
|
$htm .= " </tr>\n</thead>\n"; |
95
|
|
|
|
96
|
|
|
// Rows |
97
|
|
|
$htm .= "<tbody>\n"; |
98
|
|
|
|
99
|
|
|
foreach ($data as $row) |
100
|
|
|
{ |
101
|
|
|
|
102
|
|
|
$firstCol = true; |
103
|
|
|
// Put host header |
104
|
|
|
$source_name=(property_exists($row,'source_name'))?$row->source_name:null; |
105
|
|
|
$htm .= $this->renderHostIfNew($row->source_ip,$source_name); |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
// Render row |
109
|
|
|
$htm .= '<tr '.' >'; |
110
|
|
|
foreach ( $titles as $rowkey => $title) |
111
|
|
|
{ |
112
|
|
|
// Check missing value |
113
|
|
|
if (property_exists($row, $rowkey)) |
114
|
|
|
{ |
115
|
|
|
$val = ($rowkey=='last_sent') ? strftime('%c',$row->$rowkey) : $row->$rowkey; |
116
|
|
|
} else { |
117
|
|
|
$val = '-'; |
118
|
|
|
} |
119
|
|
|
if ($firstCol === true) { // Put link in first column for trap detail. |
120
|
|
|
$htm .= '<td>' |
121
|
|
|
. $view->qlink( |
122
|
|
|
$view->escape($val), |
123
|
|
|
Url::fromPath( |
124
|
|
|
$this->moduleConfig->urlPath() . '/received', |
125
|
|
|
array('q' => $row->trap_oid) |
126
|
|
|
) |
127
|
|
|
) |
128
|
|
|
. '</td>'; |
129
|
|
|
} else { |
130
|
|
|
$htm .= '<td>' . $view->escape($val) . '</td>'; |
131
|
|
|
} |
132
|
|
|
$firstCol=false; |
133
|
|
|
} |
134
|
|
|
$htm .= "<tr>\n"; |
135
|
|
|
} |
136
|
|
|
$htm .= "</tbody></table>\n"; |
137
|
|
|
//$htm .= "Filter : " . $this->filter."<br>\n"; |
138
|
|
|
return $htm; |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function count() |
143
|
|
|
{ |
144
|
|
|
$db=$this->db(); |
145
|
|
|
|
146
|
|
|
$query = $this->getBaseQuery(); |
147
|
|
|
$this->applyFiltersToQuery($query); |
148
|
|
|
$values=$db->fetchAll($query); |
149
|
|
|
|
150
|
|
|
return count($values); |
151
|
|
|
|
152
|
|
|
//return $db->fetchOne($query); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getPaginator() |
156
|
|
|
{ |
157
|
|
|
$paginator = new Paginator(); |
158
|
|
|
$paginator->setQuery($this); |
159
|
|
|
|
160
|
|
|
return $paginator; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// ****************** DB connection and query |
164
|
|
|
|
165
|
|
|
protected function getTable() |
166
|
|
|
{ |
167
|
|
|
$db=$this->db(); |
168
|
|
|
|
169
|
|
|
$query = $this->getBaseQuery(); |
170
|
|
|
$this->applyFiltersToQuery($query); |
171
|
|
|
if ($this->hasLimit() || $this->hasOffset()) { |
172
|
|
|
$query->limit($this->getLimit(), $this->getOffset()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $db->fetchAll($query); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getBaseQuery() |
179
|
|
|
{ |
180
|
|
|
$db=$this->db(); |
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'); |
186
|
|
|
|
187
|
|
|
return $query; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// ****************** Filters |
191
|
|
|
|
192
|
|
|
protected $filter_Handler; |
193
|
|
|
protected $filter_query=''; |
194
|
|
|
protected $filter_done=''; |
195
|
|
|
protected $filter_query_list=array('q','done'); |
196
|
|
|
public function renderFilterHTML() |
197
|
|
|
{ |
198
|
|
|
$htm=' <form id="filter" name="mainFilter" |
199
|
|
|
enctype="application/x-www-form-urlencoded" |
200
|
|
|
action="'.$this->filter_Handler.'" |
201
|
|
|
method="get">'; |
202
|
|
|
$htm.='<input type="text" name="q" title="Search is simple! Try to combine multiple words" |
203
|
|
|
placeholder="Search..." class="search" value="'.$this->filter_query.'">'; |
204
|
|
|
$htm.='<input type="checkbox" id="checkbox_done" name="done" value="1" class="autosubmit" '; |
205
|
|
|
if ($this->filter_done == 1) { $htm.=' checked ';} |
206
|
|
|
$htm.='> <label for="checkbox_done">Hide processed traps</label>'; |
207
|
|
|
$htm.='</form>'; |
208
|
|
|
return $htm; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function updateFilter($handler,$filter) |
212
|
|
|
{ |
213
|
|
|
$this->filter_Handler=$handler->remove($this->filter_query_list)->__toString(); |
214
|
|
|
$this->filter_query=(isset($filter['q']))?$this->filter_query=$filter['q']:''; |
215
|
|
|
$this->filter_done=(isset($filter['done']))?$this->filter_done=$filter['done']:0; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
protected function getSearchColumns() |
219
|
|
|
{ |
220
|
|
|
return $this->getColumns(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function getColumns() |
224
|
|
|
{ |
225
|
|
|
return $this->moduleConfig->getTrapListDisplayColumns(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function setFilter($filter) |
229
|
|
|
{ |
230
|
|
|
$this->filter = $filter; |
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
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); |
242
|
|
|
|
243
|
|
|
$filter = $filterEditor->getFilter(); |
244
|
|
|
$this->setFilter($filter); |
245
|
|
|
|
246
|
|
|
return $filterEditor; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
protected function applyFiltersToQuery($query) |
250
|
|
|
{ |
251
|
|
|
|
252
|
|
|
$sql=''; |
253
|
|
|
if ($this->filter_query != '') |
254
|
|
|
{ |
255
|
|
|
$sql.='('; |
256
|
|
|
$first=1; |
257
|
|
|
foreach($this->moduleConfig->getTrapListSearchColumns() as $column) |
258
|
|
|
{ |
259
|
|
|
if ($first==0) $sql.=' OR '; |
260
|
|
|
$first=0; |
261
|
|
|
$sql.=" ".$column." LIKE '%".$this->filter_query."%' "; |
262
|
|
|
} |
263
|
|
|
$sql.=')'; |
264
|
|
|
} |
265
|
|
|
if ($this->filter_done == 1) |
266
|
|
|
{ |
267
|
|
|
if ($sql != '') $sql.=' AND '; |
268
|
|
|
$sql.="(status != 'done')"; |
269
|
|
|
} |
270
|
|
|
if ($sql != '') $query->where($sql); |
271
|
|
|
return $query; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
} |
275
|
|
|
|