1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Icinga\Module\Trapdirector\TrapsActions; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Zend_Db_Expr; |
7
|
|
|
use Zend_Db_Adapter_Abstract; |
8
|
|
|
use Zend_Db_Select; |
|
|
|
|
9
|
|
|
use Icinga\Module\Trapdirector\TrapsController; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Database queries for UI (on Trap database) |
13
|
|
|
* Calling class must implement : getTrapCtrl , getDbConn |
14
|
|
|
* @license GPL |
15
|
|
|
* @author Patrick Proy |
16
|
|
|
* @package trapdirector |
17
|
|
|
* @subpackage UI |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
trait TrapDBQuery |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** @return TrapsController */ |
24
|
|
|
abstract protected function getTrapCtrl(); |
25
|
|
|
|
26
|
|
|
/** @return Zend_Db_Adapter_Abstract : returns DB connexion or null on error */ |
27
|
|
|
abstract public function getDbConn(); |
28
|
|
|
|
29
|
|
|
/** Add handler rule in traps DB |
30
|
|
|
* @param array $params : array(<db item>=><value>) |
31
|
|
|
* @return int inserted id |
32
|
|
|
*/ |
33
|
|
|
public function addHandlerRule($params) |
34
|
|
|
{ |
35
|
|
|
// TODO Check for rule consistency |
36
|
|
|
|
37
|
|
|
$dbConn = $this->getDbConn(); |
38
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
39
|
|
|
// Add last modified date = creation date and username |
40
|
|
|
$params['created'] = new Zend_Db_Expr('NOW()'); |
41
|
|
|
$params['modified'] = new Zend_Db_Expr('NOW()'); |
42
|
|
|
$params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername(); |
43
|
|
|
|
44
|
|
|
$query=$dbConn->insert( |
45
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
46
|
|
|
$params |
47
|
|
|
); |
48
|
|
|
if($query==false) |
49
|
|
|
{ |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
return $dbConn->lastInsertId(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** Update handler rule in traps DB |
56
|
|
|
* @param array $params : (<db item>=><value>) |
57
|
|
|
* @param integer $ruleID : rule id in db |
58
|
|
|
* @return array affected rows |
59
|
|
|
*/ |
60
|
|
|
public function updateHandlerRule($params,$ruleID) |
61
|
|
|
{ |
62
|
|
|
// TODO Check for rule consistency |
63
|
|
|
$dbConn = $this->getDbConn(); |
64
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
65
|
|
|
// Add last modified date = creation date and username |
66
|
|
|
$params['modified'] = new Zend_Db_Expr('NOW()'); |
67
|
|
|
$params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername(); |
68
|
|
|
|
69
|
|
|
$numRows=$dbConn->update( |
70
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
71
|
|
|
$params, |
72
|
|
|
'id='.$ruleID |
73
|
|
|
); |
74
|
|
|
return $numRows; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* ON category removal, put back cat to 0 on handlers with this category. |
79
|
|
|
* @param int $catIndex |
80
|
|
|
* @throws \ErrorException |
81
|
|
|
* @return number |
82
|
|
|
*/ |
83
|
|
|
public function updateHandlersOnCategoryDelete($catIndex) |
84
|
|
|
{ |
85
|
|
|
// TODO Check for rule consistency |
86
|
|
|
$dbConn = $this->getDbConn(); |
87
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
88
|
|
|
|
89
|
|
|
$numRows=$dbConn->update( |
90
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
91
|
|
|
array('rule_type' => 0), |
92
|
|
|
'rule_type='.$catIndex |
93
|
|
|
); |
94
|
|
|
return $numRows; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** Delete rule by id |
98
|
|
|
* @param int $ruleID rule id |
99
|
|
|
*/ |
100
|
|
|
public function deleteRule($ruleID) |
101
|
|
|
{ |
102
|
|
|
if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
103
|
|
|
|
104
|
|
|
$dbConn = $this->getDbConn(); |
105
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
106
|
|
|
|
107
|
|
|
$query=$dbConn->delete( |
108
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
109
|
|
|
'id='.$ruleID |
110
|
|
|
); |
111
|
|
|
return $query; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get last trap rule table modification |
116
|
|
|
* @throws \ErrorException |
117
|
|
|
* @return Zend_Db_Select |
118
|
|
|
*/ |
119
|
|
|
public function lastModification() |
120
|
|
|
{ |
121
|
|
|
$dbConn = $this->getDbConn(); |
122
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
123
|
|
|
|
124
|
|
|
$query = $dbConn->select() |
125
|
|
|
->from( |
126
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
127
|
|
|
array('lastModified'=>'UNIX_TIMESTAMP(MAX(modified))')); |
128
|
|
|
$returnRow=$dbConn->fetchRow($query); |
129
|
|
|
return $returnRow->lastmodified; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** Delete trap by ip & oid |
133
|
|
|
* @param $ipAddr string source IP (v4 or v6) |
134
|
|
|
* @param $oid string oid |
135
|
|
|
*/ |
136
|
|
|
public function deleteTrap($ipAddr,$oid) |
137
|
|
|
{ |
138
|
|
|
|
139
|
|
|
$dbConn = $this->getDbConn(); |
140
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
141
|
|
|
$condition=null; |
142
|
|
|
if ($ipAddr != null) |
143
|
|
|
{ |
144
|
|
|
$condition="source_ip='$ipAddr'"; |
145
|
|
|
} |
146
|
|
|
if ($oid != null) |
147
|
|
|
{ |
148
|
|
|
$condition=($condition===null)?'':$condition.' AND '; |
149
|
|
|
$condition.="trap_oid='$oid'"; |
150
|
|
|
} |
151
|
|
|
if($condition === null) return null; |
152
|
|
|
$query=$dbConn->delete( |
153
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
154
|
|
|
$condition |
155
|
|
|
); |
156
|
|
|
// TODO test ret code etc... |
157
|
|
|
return $query; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** count trap by ip & oid |
162
|
|
|
* @param $ipAddr string source IP (v4 or v6) |
163
|
|
|
* @param $oid string oid |
164
|
|
|
*/ |
165
|
|
|
public function countTrap($ipAddr,$oid) |
166
|
|
|
{ |
167
|
|
|
|
168
|
|
|
$dbConn = $this->getDbConn(); |
169
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
170
|
|
|
|
171
|
|
|
$condition=null; |
172
|
|
|
if ($ipAddr != null) |
173
|
|
|
{ |
174
|
|
|
$condition="source_ip='$ipAddr'"; |
175
|
|
|
} |
176
|
|
|
if ($oid != null) |
177
|
|
|
{ |
178
|
|
|
$condition=($condition===null)?'':$condition.' AND '; |
179
|
|
|
$condition.="trap_oid='$oid'"; |
180
|
|
|
} |
181
|
|
|
if($condition === null) return 0; |
182
|
|
|
$query=$dbConn->select() |
183
|
|
|
->from( |
184
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
185
|
|
|
array('num'=>'count(*)')) |
186
|
|
|
->where($condition); |
187
|
|
|
$returnRow=$dbConn->fetchRow($query); |
188
|
|
|
return $returnRow->num; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** get configuration value |
192
|
|
|
* @param string $element : configuration name in db |
193
|
|
|
*/ |
194
|
|
|
public function getDBConfigValue($element) |
195
|
|
|
{ |
196
|
|
|
|
197
|
|
|
$dbConn = $this->getDbConn(); |
198
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
199
|
|
|
|
200
|
|
|
$query=$dbConn->select() |
201
|
|
|
->from( |
202
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
203
|
|
|
array('value'=>'value')) |
204
|
|
|
->where('name=?',$element); |
205
|
|
|
$returnRow=$dbConn->fetchRow($query); |
206
|
|
|
if ($returnRow==null) // value does not exists |
207
|
|
|
{ |
208
|
|
|
$default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
209
|
|
|
if ( ! isset($default[$element])) return null; // no default and not value |
210
|
|
|
|
211
|
|
|
$this->addDBConfigValue($element,$default[$element]); |
212
|
|
|
return $default[$element]; |
213
|
|
|
} |
214
|
|
|
if ($returnRow->value == null) // value id empty |
215
|
|
|
{ |
216
|
|
|
$default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
217
|
|
|
if ( ! isset($default[$element])) return null; // no default and not value |
218
|
|
|
$this->setDBConfigValue($element,$default[$element]); |
219
|
|
|
return $default[$element]; |
220
|
|
|
} |
221
|
|
|
return $returnRow->value; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** add configuration value |
225
|
|
|
* @param string $element : name of config element |
226
|
|
|
* @param string $value : value |
227
|
|
|
*/ |
228
|
|
|
|
229
|
|
|
public function addDBConfigValue($element,$value) |
230
|
|
|
{ |
231
|
|
|
|
232
|
|
|
$dbConn = $this->getDbConn(); |
233
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
234
|
|
|
|
235
|
|
|
$query=$dbConn->insert( |
236
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
237
|
|
|
array( |
238
|
|
|
'name' => $element, |
239
|
|
|
'value'=>$value |
240
|
|
|
) |
241
|
|
|
); |
242
|
|
|
return $query; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** set configuration value |
246
|
|
|
* @param string $element : name of config element |
247
|
|
|
* @param string $value : value |
248
|
|
|
*/ |
249
|
|
|
public function setDBConfigValue($element,$value) |
250
|
|
|
{ |
251
|
|
|
|
252
|
|
|
$dbConn = $this->getDbConn(); |
253
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
254
|
|
|
|
255
|
|
|
$query=$dbConn->update( |
256
|
|
|
$this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
257
|
|
|
array('value'=>$value), |
258
|
|
|
'name=\''.$element.'\'' |
259
|
|
|
); |
260
|
|
|
return $query; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths