@@ -11,28 +11,28 @@ discard block |
||
11 | 11 | class ApiController extends TrapsController |
12 | 12 | { |
13 | 13 | |
14 | - private $json_options=JSON_PRETTY_PRINT; |
|
14 | + private $json_options=JSON_PRETTY_PRINT; |
|
15 | 15 | |
16 | - protected function send_json($object) |
|
17 | - { |
|
18 | - if (isset($object['Error'])) |
|
19 | - { |
|
20 | - $this->send_json_error($object); |
|
21 | - return; |
|
22 | - } |
|
23 | - $this->_helper->layout()->disableLayout(); |
|
24 | - $this->getResponse()->setHeader('Content-Type', 'application/json', true); |
|
25 | - $this->getResponse()->sendHeaders(); |
|
26 | - echo json_encode($object, $this->json_options) . "\n"; |
|
27 | - } |
|
16 | + protected function send_json($object) |
|
17 | + { |
|
18 | + if (isset($object['Error'])) |
|
19 | + { |
|
20 | + $this->send_json_error($object); |
|
21 | + return; |
|
22 | + } |
|
23 | + $this->_helper->layout()->disableLayout(); |
|
24 | + $this->getResponse()->setHeader('Content-Type', 'application/json', true); |
|
25 | + $this->getResponse()->sendHeaders(); |
|
26 | + echo json_encode($object, $this->json_options) . "\n"; |
|
27 | + } |
|
28 | 28 | |
29 | - protected function send_json_error($object) |
|
30 | - { |
|
31 | - $this->_helper->layout()->disableLayout(); |
|
32 | - $this->getResponse()->setHeader('Content-Type', 'application/json', true); |
|
33 | - $this->getResponse()->sendHeaders(); |
|
34 | - echo json_encode($object, $this->json_options) . "\n"; |
|
35 | - } |
|
29 | + protected function send_json_error($object) |
|
30 | + { |
|
31 | + $this->_helper->layout()->disableLayout(); |
|
32 | + $this->getResponse()->setHeader('Content-Type', 'application/json', true); |
|
33 | + $this->getResponse()->sendHeaders(); |
|
34 | + echo json_encode($object, $this->json_options) . "\n"; |
|
35 | + } |
|
36 | 36 | |
37 | 37 | public function indexAction() |
38 | 38 | { |
@@ -47,18 +47,18 @@ discard block |
||
47 | 47 | |
48 | 48 | public function dboptionActions() |
49 | 49 | { |
50 | - $this->checkReadPermission(); |
|
51 | - $apiObj= new RestAPI($this); |
|
50 | + $this->checkReadPermission(); |
|
51 | + $apiObj= new RestAPI($this); |
|
52 | 52 | |
53 | - $params = $this->getRequest()->getParams(); |
|
54 | - if (isset($params['name'])) |
|
55 | - { |
|
53 | + $params = $this->getRequest()->getParams(); |
|
54 | + if (isset($params['name'])) |
|
55 | + { |
|
56 | 56 | |
57 | - } |
|
58 | - else |
|
59 | - { |
|
57 | + } |
|
58 | + else |
|
59 | + { |
|
60 | 60 | |
61 | - } |
|
61 | + } |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | } |
65 | 65 | \ No newline at end of file |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | $this->_helper->layout()->disableLayout(); |
24 | 24 | $this->getResponse()->setHeader('Content-Type', 'application/json', true); |
25 | 25 | $this->getResponse()->sendHeaders(); |
26 | - echo json_encode($object, $this->json_options) . "\n"; |
|
26 | + echo json_encode($object, $this->json_options)."\n"; |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | protected function send_json_error($object) |
@@ -31,15 +31,15 @@ discard block |
||
31 | 31 | $this->_helper->layout()->disableLayout(); |
32 | 32 | $this->getResponse()->setHeader('Content-Type', 'application/json', true); |
33 | 33 | $this->getResponse()->sendHeaders(); |
34 | - echo json_encode($object, $this->json_options) . "\n"; |
|
34 | + echo json_encode($object, $this->json_options)."\n"; |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | public function indexAction() |
38 | 38 | { |
39 | 39 | $this->checkReadPermission(); |
40 | - $apiObj= new RestAPI($this); |
|
40 | + $apiObj=new RestAPI($this); |
|
41 | 41 | |
42 | - $modif = $apiObj->last_modified(); |
|
42 | + $modif=$apiObj->last_modified(); |
|
43 | 43 | $this->send_json($modif); |
44 | 44 | //print_r($modif); |
45 | 45 | return; |
@@ -48,9 +48,9 @@ discard block |
||
48 | 48 | public function dboptionActions() |
49 | 49 | { |
50 | 50 | $this->checkReadPermission(); |
51 | - $apiObj= new RestAPI($this); |
|
51 | + $apiObj=new RestAPI($this); |
|
52 | 52 | |
53 | - $params = $this->getRequest()->getParams(); |
|
53 | + $params=$this->getRequest()->getParams(); |
|
54 | 54 | if (isset($params['name'])) |
55 | 55 | { |
56 | 56 |
@@ -54,8 +54,7 @@ |
||
54 | 54 | if (isset($params['name'])) |
55 | 55 | { |
56 | 56 | |
57 | - } |
|
58 | - else |
|
57 | + } else |
|
59 | 58 | { |
60 | 59 | |
61 | 60 | } |
@@ -20,225 +20,225 @@ |
||
20 | 20 | trait TrapDBQuery |
21 | 21 | { |
22 | 22 | |
23 | - /** @return TrapsController */ |
|
24 | - abstract protected function getTrapCtrl(); |
|
23 | + /** @return TrapsController */ |
|
24 | + abstract protected function getTrapCtrl(); |
|
25 | 25 | |
26 | - /** @return Zend_Db_Adapter_Abstract : returns DB connexion or null on error */ |
|
27 | - abstract public function getDbConn(); |
|
26 | + /** @return Zend_Db_Adapter_Abstract : returns DB connexion or null on error */ |
|
27 | + abstract public function getDbConn(); |
|
28 | 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 |
|
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 | 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(); |
|
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 | 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 | - } |
|
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 | 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(); |
|
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 | 68 | |
69 | - $numRows=$dbConn->update( |
|
70 | - $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
|
71 | - $params, |
|
72 | - 'id='.$ruleID |
|
73 | - ); |
|
74 | - return $numRows; |
|
75 | - } |
|
69 | + $numRows=$dbConn->update( |
|
70 | + $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
|
71 | + $params, |
|
72 | + 'id='.$ruleID |
|
73 | + ); |
|
74 | + return $numRows; |
|
75 | + } |
|
76 | 76 | |
77 | - /** Delete rule by id |
|
78 | - * @param int $ruleID rule id |
|
79 | - */ |
|
80 | - public function deleteRule($ruleID) |
|
81 | - { |
|
82 | - if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
|
77 | + /** Delete rule by id |
|
78 | + * @param int $ruleID rule id |
|
79 | + */ |
|
80 | + public function deleteRule($ruleID) |
|
81 | + { |
|
82 | + if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
|
83 | 83 | |
84 | - $dbConn = $this->getDbConn(); |
|
85 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
84 | + $dbConn = $this->getDbConn(); |
|
85 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
86 | 86 | |
87 | - $query=$dbConn->delete( |
|
88 | - $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
|
89 | - 'id='.$ruleID |
|
90 | - ); |
|
91 | - return $query; |
|
92 | - } |
|
87 | + $query=$dbConn->delete( |
|
88 | + $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
|
89 | + 'id='.$ruleID |
|
90 | + ); |
|
91 | + return $query; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Get last trap rule table modification |
|
96 | - * @throws \ErrorException |
|
97 | - * @return Zend_Db_Select |
|
98 | - */ |
|
99 | - public function lastModification() |
|
100 | - { |
|
101 | - $dbConn = $this->getDbConn(); |
|
102 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
94 | + /** |
|
95 | + * Get last trap rule table modification |
|
96 | + * @throws \ErrorException |
|
97 | + * @return Zend_Db_Select |
|
98 | + */ |
|
99 | + public function lastModification() |
|
100 | + { |
|
101 | + $dbConn = $this->getDbConn(); |
|
102 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
103 | 103 | |
104 | - $query = $dbConn->select() |
|
105 | - ->from( |
|
106 | - $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
|
107 | - array('lastModified'=>'UNIX_TIMESTAMP(MAX(modified))')); |
|
108 | - $returnRow=$dbConn->fetchRow($query); |
|
109 | - return $returnRow->lastmodified; |
|
110 | - } |
|
104 | + $query = $dbConn->select() |
|
105 | + ->from( |
|
106 | + $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
|
107 | + array('lastModified'=>'UNIX_TIMESTAMP(MAX(modified))')); |
|
108 | + $returnRow=$dbConn->fetchRow($query); |
|
109 | + return $returnRow->lastmodified; |
|
110 | + } |
|
111 | 111 | |
112 | - /** Delete trap by ip & oid |
|
113 | - * @param $ipAddr string source IP (v4 or v6) |
|
114 | - * @param $oid string oid |
|
115 | - */ |
|
116 | - public function deleteTrap($ipAddr,$oid) |
|
117 | - { |
|
112 | + /** Delete trap by ip & oid |
|
113 | + * @param $ipAddr string source IP (v4 or v6) |
|
114 | + * @param $oid string oid |
|
115 | + */ |
|
116 | + public function deleteTrap($ipAddr,$oid) |
|
117 | + { |
|
118 | 118 | |
119 | - $dbConn = $this->getDbConn(); |
|
120 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
121 | - $condition=null; |
|
122 | - if ($ipAddr != null) |
|
123 | - { |
|
124 | - $condition="source_ip='$ipAddr'"; |
|
125 | - } |
|
126 | - if ($oid != null) |
|
127 | - { |
|
128 | - $condition=($condition===null)?'':$condition.' AND '; |
|
129 | - $condition.="trap_oid='$oid'"; |
|
130 | - } |
|
131 | - if($condition === null) return null; |
|
132 | - $query=$dbConn->delete( |
|
133 | - $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
|
134 | - $condition |
|
135 | - ); |
|
136 | - // TODO test ret code etc... |
|
137 | - return $query; |
|
138 | - } |
|
119 | + $dbConn = $this->getDbConn(); |
|
120 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
121 | + $condition=null; |
|
122 | + if ($ipAddr != null) |
|
123 | + { |
|
124 | + $condition="source_ip='$ipAddr'"; |
|
125 | + } |
|
126 | + if ($oid != null) |
|
127 | + { |
|
128 | + $condition=($condition===null)?'':$condition.' AND '; |
|
129 | + $condition.="trap_oid='$oid'"; |
|
130 | + } |
|
131 | + if($condition === null) return null; |
|
132 | + $query=$dbConn->delete( |
|
133 | + $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
|
134 | + $condition |
|
135 | + ); |
|
136 | + // TODO test ret code etc... |
|
137 | + return $query; |
|
138 | + } |
|
139 | 139 | |
140 | 140 | |
141 | - /** count trap by ip & oid |
|
142 | - * @param $ipAddr string source IP (v4 or v6) |
|
143 | - * @param $oid string oid |
|
144 | - */ |
|
145 | - public function countTrap($ipAddr,$oid) |
|
146 | - { |
|
141 | + /** count trap by ip & oid |
|
142 | + * @param $ipAddr string source IP (v4 or v6) |
|
143 | + * @param $oid string oid |
|
144 | + */ |
|
145 | + public function countTrap($ipAddr,$oid) |
|
146 | + { |
|
147 | 147 | |
148 | - $dbConn = $this->getDbConn(); |
|
149 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
148 | + $dbConn = $this->getDbConn(); |
|
149 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
150 | 150 | |
151 | - $condition=null; |
|
152 | - if ($ipAddr != null) |
|
153 | - { |
|
154 | - $condition="source_ip='$ipAddr'"; |
|
155 | - } |
|
156 | - if ($oid != null) |
|
157 | - { |
|
158 | - $condition=($condition===null)?'':$condition.' AND '; |
|
159 | - $condition.="trap_oid='$oid'"; |
|
160 | - } |
|
161 | - if($condition === null) return 0; |
|
162 | - $query=$dbConn->select() |
|
163 | - ->from( |
|
164 | - $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
|
165 | - array('num'=>'count(*)')) |
|
166 | - ->where($condition); |
|
167 | - $returnRow=$dbConn->fetchRow($query); |
|
168 | - return $returnRow->num; |
|
169 | - } |
|
151 | + $condition=null; |
|
152 | + if ($ipAddr != null) |
|
153 | + { |
|
154 | + $condition="source_ip='$ipAddr'"; |
|
155 | + } |
|
156 | + if ($oid != null) |
|
157 | + { |
|
158 | + $condition=($condition===null)?'':$condition.' AND '; |
|
159 | + $condition.="trap_oid='$oid'"; |
|
160 | + } |
|
161 | + if($condition === null) return 0; |
|
162 | + $query=$dbConn->select() |
|
163 | + ->from( |
|
164 | + $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
|
165 | + array('num'=>'count(*)')) |
|
166 | + ->where($condition); |
|
167 | + $returnRow=$dbConn->fetchRow($query); |
|
168 | + return $returnRow->num; |
|
169 | + } |
|
170 | 170 | |
171 | - /** get configuration value |
|
172 | - * @param string $element : configuration name in db |
|
173 | - */ |
|
174 | - public function getDBConfigValue($element) |
|
175 | - { |
|
171 | + /** get configuration value |
|
172 | + * @param string $element : configuration name in db |
|
173 | + */ |
|
174 | + public function getDBConfigValue($element) |
|
175 | + { |
|
176 | 176 | |
177 | - $dbConn = $this->getDbConn(); |
|
178 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
177 | + $dbConn = $this->getDbConn(); |
|
178 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
179 | 179 | |
180 | - $query=$dbConn->select() |
|
181 | - ->from( |
|
182 | - $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
|
183 | - array('value'=>'value')) |
|
184 | - ->where('name=?',$element); |
|
185 | - $returnRow=$dbConn->fetchRow($query); |
|
186 | - if ($returnRow==null) // value does not exists |
|
187 | - { |
|
188 | - $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
|
189 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
180 | + $query=$dbConn->select() |
|
181 | + ->from( |
|
182 | + $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
|
183 | + array('value'=>'value')) |
|
184 | + ->where('name=?',$element); |
|
185 | + $returnRow=$dbConn->fetchRow($query); |
|
186 | + if ($returnRow==null) // value does not exists |
|
187 | + { |
|
188 | + $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
|
189 | + if ( ! isset($default[$element])) return null; // no default and not value |
|
190 | 190 | |
191 | - $this->addDBConfigValue($element,$default[$element]); |
|
192 | - return $default[$element]; |
|
193 | - } |
|
194 | - if ($returnRow->value == null) // value id empty |
|
195 | - { |
|
196 | - $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
|
197 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
198 | - $this->setDBConfigValue($element,$default[$element]); |
|
199 | - return $default[$element]; |
|
200 | - } |
|
201 | - return $returnRow->value; |
|
202 | - } |
|
191 | + $this->addDBConfigValue($element,$default[$element]); |
|
192 | + return $default[$element]; |
|
193 | + } |
|
194 | + if ($returnRow->value == null) // value id empty |
|
195 | + { |
|
196 | + $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
|
197 | + if ( ! isset($default[$element])) return null; // no default and not value |
|
198 | + $this->setDBConfigValue($element,$default[$element]); |
|
199 | + return $default[$element]; |
|
200 | + } |
|
201 | + return $returnRow->value; |
|
202 | + } |
|
203 | 203 | |
204 | - /** add configuration value |
|
205 | - * @param string $element : name of config element |
|
206 | - * @param string $value : value |
|
207 | - */ |
|
204 | + /** add configuration value |
|
205 | + * @param string $element : name of config element |
|
206 | + * @param string $value : value |
|
207 | + */ |
|
208 | 208 | |
209 | - public function addDBConfigValue($element,$value) |
|
210 | - { |
|
209 | + public function addDBConfigValue($element,$value) |
|
210 | + { |
|
211 | 211 | |
212 | - $dbConn = $this->getDbConn(); |
|
213 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
212 | + $dbConn = $this->getDbConn(); |
|
213 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
214 | 214 | |
215 | - $query=$dbConn->insert( |
|
216 | - $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
|
217 | - array( |
|
218 | - 'name' => $element, |
|
219 | - 'value'=>$value |
|
220 | - ) |
|
221 | - ); |
|
222 | - return $query; |
|
223 | - } |
|
215 | + $query=$dbConn->insert( |
|
216 | + $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
|
217 | + array( |
|
218 | + 'name' => $element, |
|
219 | + 'value'=>$value |
|
220 | + ) |
|
221 | + ); |
|
222 | + return $query; |
|
223 | + } |
|
224 | 224 | |
225 | - /** set configuration value |
|
226 | - * @param string $element : name of config element |
|
227 | - * @param string $value : value |
|
228 | - */ |
|
229 | - public function setDBConfigValue($element,$value) |
|
230 | - { |
|
225 | + /** set configuration value |
|
226 | + * @param string $element : name of config element |
|
227 | + * @param string $value : value |
|
228 | + */ |
|
229 | + public function setDBConfigValue($element,$value) |
|
230 | + { |
|
231 | 231 | |
232 | - $dbConn = $this->getDbConn(); |
|
233 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
232 | + $dbConn = $this->getDbConn(); |
|
233 | + if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
234 | 234 | |
235 | - $query=$dbConn->update( |
|
236 | - $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
|
237 | - array('value'=>$value), |
|
238 | - 'name=\''.$element.'\'' |
|
239 | - ); |
|
240 | - return $query; |
|
241 | - } |
|
235 | + $query=$dbConn->update( |
|
236 | + $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
|
237 | + array('value'=>$value), |
|
238 | + 'name=\''.$element.'\'' |
|
239 | + ); |
|
240 | + return $query; |
|
241 | + } |
|
242 | 242 | |
243 | 243 | |
244 | 244 | } |
245 | 245 | \ No newline at end of file |
@@ -34,18 +34,18 @@ discard block |
||
34 | 34 | { |
35 | 35 | // TODO Check for rule consistency |
36 | 36 | |
37 | - $dbConn = $this->getDbConn(); |
|
37 | + $dbConn=$this->getDbConn(); |
|
38 | 38 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
39 | 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(); |
|
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 | 43 | |
44 | 44 | $query=$dbConn->insert( |
45 | 45 | $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
46 | 46 | $params |
47 | 47 | ); |
48 | - if($query==false) |
|
48 | + if ($query == false) |
|
49 | 49 | { |
50 | 50 | return null; |
51 | 51 | } |
@@ -57,14 +57,14 @@ discard block |
||
57 | 57 | * @param integer $ruleID : rule id in db |
58 | 58 | * @return array affected rows |
59 | 59 | */ |
60 | - public function updateHandlerRule($params,$ruleID) |
|
60 | + public function updateHandlerRule($params, $ruleID) |
|
61 | 61 | { |
62 | 62 | // TODO Check for rule consistency |
63 | - $dbConn = $this->getDbConn(); |
|
63 | + $dbConn=$this->getDbConn(); |
|
64 | 64 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
65 | 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(); |
|
66 | + $params['modified']=new Zend_Db_Expr('NOW()'); |
|
67 | + $params['modifier']=$this->getTrapCtrl()->Auth()->getUser()->getUsername(); |
|
68 | 68 | |
69 | 69 | $numRows=$dbConn->update( |
70 | 70 | $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
@@ -79,9 +79,9 @@ discard block |
||
79 | 79 | */ |
80 | 80 | public function deleteRule($ruleID) |
81 | 81 | { |
82 | - if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
|
82 | + if (!preg_match('/^[0-9]+$/', $ruleID)) { throw new Exception('Invalid id'); } |
|
83 | 83 | |
84 | - $dbConn = $this->getDbConn(); |
|
84 | + $dbConn=$this->getDbConn(); |
|
85 | 85 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
86 | 86 | |
87 | 87 | $query=$dbConn->delete( |
@@ -98,10 +98,10 @@ discard block |
||
98 | 98 | */ |
99 | 99 | public function lastModification() |
100 | 100 | { |
101 | - $dbConn = $this->getDbConn(); |
|
101 | + $dbConn=$this->getDbConn(); |
|
102 | 102 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
103 | 103 | |
104 | - $query = $dbConn->select() |
|
104 | + $query=$dbConn->select() |
|
105 | 105 | ->from( |
106 | 106 | $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
107 | 107 | array('lastModified'=>'UNIX_TIMESTAMP(MAX(modified))')); |
@@ -113,10 +113,10 @@ discard block |
||
113 | 113 | * @param $ipAddr string source IP (v4 or v6) |
114 | 114 | * @param $oid string oid |
115 | 115 | */ |
116 | - public function deleteTrap($ipAddr,$oid) |
|
116 | + public function deleteTrap($ipAddr, $oid) |
|
117 | 117 | { |
118 | 118 | |
119 | - $dbConn = $this->getDbConn(); |
|
119 | + $dbConn=$this->getDbConn(); |
|
120 | 120 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
121 | 121 | $condition=null; |
122 | 122 | if ($ipAddr != null) |
@@ -125,10 +125,10 @@ discard block |
||
125 | 125 | } |
126 | 126 | if ($oid != null) |
127 | 127 | { |
128 | - $condition=($condition===null)?'':$condition.' AND '; |
|
128 | + $condition=($condition === null) ? '' : $condition.' AND '; |
|
129 | 129 | $condition.="trap_oid='$oid'"; |
130 | 130 | } |
131 | - if($condition === null) return null; |
|
131 | + if ($condition === null) return null; |
|
132 | 132 | $query=$dbConn->delete( |
133 | 133 | $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
134 | 134 | $condition |
@@ -142,10 +142,10 @@ discard block |
||
142 | 142 | * @param $ipAddr string source IP (v4 or v6) |
143 | 143 | * @param $oid string oid |
144 | 144 | */ |
145 | - public function countTrap($ipAddr,$oid) |
|
145 | + public function countTrap($ipAddr, $oid) |
|
146 | 146 | { |
147 | 147 | |
148 | - $dbConn = $this->getDbConn(); |
|
148 | + $dbConn=$this->getDbConn(); |
|
149 | 149 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
150 | 150 | |
151 | 151 | $condition=null; |
@@ -155,10 +155,10 @@ discard block |
||
155 | 155 | } |
156 | 156 | if ($oid != null) |
157 | 157 | { |
158 | - $condition=($condition===null)?'':$condition.' AND '; |
|
158 | + $condition=($condition === null) ? '' : $condition.' AND '; |
|
159 | 159 | $condition.="trap_oid='$oid'"; |
160 | 160 | } |
161 | - if($condition === null) return 0; |
|
161 | + if ($condition === null) return 0; |
|
162 | 162 | $query=$dbConn->select() |
163 | 163 | ->from( |
164 | 164 | $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
@@ -174,28 +174,28 @@ discard block |
||
174 | 174 | public function getDBConfigValue($element) |
175 | 175 | { |
176 | 176 | |
177 | - $dbConn = $this->getDbConn(); |
|
177 | + $dbConn=$this->getDbConn(); |
|
178 | 178 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
179 | 179 | |
180 | 180 | $query=$dbConn->select() |
181 | 181 | ->from( |
182 | 182 | $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
183 | 183 | array('value'=>'value')) |
184 | - ->where('name=?',$element); |
|
184 | + ->where('name=?', $element); |
|
185 | 185 | $returnRow=$dbConn->fetchRow($query); |
186 | - if ($returnRow==null) // value does not exists |
|
186 | + if ($returnRow == null) // value does not exists |
|
187 | 187 | { |
188 | 188 | $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
189 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
189 | + if (!isset($default[$element])) return null; // no default and not value |
|
190 | 190 | |
191 | - $this->addDBConfigValue($element,$default[$element]); |
|
191 | + $this->addDBConfigValue($element, $default[$element]); |
|
192 | 192 | return $default[$element]; |
193 | 193 | } |
194 | 194 | if ($returnRow->value == null) // value id empty |
195 | 195 | { |
196 | 196 | $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
197 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
198 | - $this->setDBConfigValue($element,$default[$element]); |
|
197 | + if (!isset($default[$element])) return null; // no default and not value |
|
198 | + $this->setDBConfigValue($element, $default[$element]); |
|
199 | 199 | return $default[$element]; |
200 | 200 | } |
201 | 201 | return $returnRow->value; |
@@ -206,10 +206,10 @@ discard block |
||
206 | 206 | * @param string $value : value |
207 | 207 | */ |
208 | 208 | |
209 | - public function addDBConfigValue($element,$value) |
|
209 | + public function addDBConfigValue($element, $value) |
|
210 | 210 | { |
211 | 211 | |
212 | - $dbConn = $this->getDbConn(); |
|
212 | + $dbConn=$this->getDbConn(); |
|
213 | 213 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
214 | 214 | |
215 | 215 | $query=$dbConn->insert( |
@@ -226,10 +226,10 @@ discard block |
||
226 | 226 | * @param string $element : name of config element |
227 | 227 | * @param string $value : value |
228 | 228 | */ |
229 | - public function setDBConfigValue($element,$value) |
|
229 | + public function setDBConfigValue($element, $value) |
|
230 | 230 | { |
231 | 231 | |
232 | - $dbConn = $this->getDbConn(); |
|
232 | + $dbConn=$this->getDbConn(); |
|
233 | 233 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
234 | 234 | |
235 | 235 | $query=$dbConn->update( |
@@ -35,7 +35,9 @@ discard block |
||
35 | 35 | // TODO Check for rule consistency |
36 | 36 | |
37 | 37 | $dbConn = $this->getDbConn(); |
38 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
38 | + if ($dbConn === null) { |
|
39 | + throw new \ErrorException('uncatched db error'); |
|
40 | + } |
|
39 | 41 | // Add last modified date = creation date and username |
40 | 42 | $params['created'] = new Zend_Db_Expr('NOW()'); |
41 | 43 | $params['modified'] = new Zend_Db_Expr('NOW()'); |
@@ -61,7 +63,9 @@ discard block |
||
61 | 63 | { |
62 | 64 | // TODO Check for rule consistency |
63 | 65 | $dbConn = $this->getDbConn(); |
64 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
66 | + if ($dbConn === null) { |
|
67 | + throw new \ErrorException('uncatched db error'); |
|
68 | + } |
|
65 | 69 | // Add last modified date = creation date and username |
66 | 70 | $params['modified'] = new Zend_Db_Expr('NOW()'); |
67 | 71 | $params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername(); |
@@ -82,7 +86,9 @@ discard block |
||
82 | 86 | if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
83 | 87 | |
84 | 88 | $dbConn = $this->getDbConn(); |
85 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
89 | + if ($dbConn === null) { |
|
90 | + throw new \ErrorException('uncatched db error'); |
|
91 | + } |
|
86 | 92 | |
87 | 93 | $query=$dbConn->delete( |
88 | 94 | $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(), |
@@ -99,7 +105,9 @@ discard block |
||
99 | 105 | public function lastModification() |
100 | 106 | { |
101 | 107 | $dbConn = $this->getDbConn(); |
102 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
108 | + if ($dbConn === null) { |
|
109 | + throw new \ErrorException('uncatched db error'); |
|
110 | + } |
|
103 | 111 | |
104 | 112 | $query = $dbConn->select() |
105 | 113 | ->from( |
@@ -117,7 +125,9 @@ discard block |
||
117 | 125 | { |
118 | 126 | |
119 | 127 | $dbConn = $this->getDbConn(); |
120 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
128 | + if ($dbConn === null) { |
|
129 | + throw new \ErrorException('uncatched db error'); |
|
130 | + } |
|
121 | 131 | $condition=null; |
122 | 132 | if ($ipAddr != null) |
123 | 133 | { |
@@ -128,7 +138,9 @@ discard block |
||
128 | 138 | $condition=($condition===null)?'':$condition.' AND '; |
129 | 139 | $condition.="trap_oid='$oid'"; |
130 | 140 | } |
131 | - if($condition === null) return null; |
|
141 | + if($condition === null) { |
|
142 | + return null; |
|
143 | + } |
|
132 | 144 | $query=$dbConn->delete( |
133 | 145 | $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
134 | 146 | $condition |
@@ -146,7 +158,9 @@ discard block |
||
146 | 158 | { |
147 | 159 | |
148 | 160 | $dbConn = $this->getDbConn(); |
149 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
161 | + if ($dbConn === null) { |
|
162 | + throw new \ErrorException('uncatched db error'); |
|
163 | + } |
|
150 | 164 | |
151 | 165 | $condition=null; |
152 | 166 | if ($ipAddr != null) |
@@ -158,7 +172,9 @@ discard block |
||
158 | 172 | $condition=($condition===null)?'':$condition.' AND '; |
159 | 173 | $condition.="trap_oid='$oid'"; |
160 | 174 | } |
161 | - if($condition === null) return 0; |
|
175 | + if($condition === null) { |
|
176 | + return 0; |
|
177 | + } |
|
162 | 178 | $query=$dbConn->select() |
163 | 179 | ->from( |
164 | 180 | $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(), |
@@ -175,7 +191,9 @@ discard block |
||
175 | 191 | { |
176 | 192 | |
177 | 193 | $dbConn = $this->getDbConn(); |
178 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
194 | + if ($dbConn === null) { |
|
195 | + throw new \ErrorException('uncatched db error'); |
|
196 | + } |
|
179 | 197 | |
180 | 198 | $query=$dbConn->select() |
181 | 199 | ->from( |
@@ -183,18 +201,28 @@ discard block |
||
183 | 201 | array('value'=>'value')) |
184 | 202 | ->where('name=?',$element); |
185 | 203 | $returnRow=$dbConn->fetchRow($query); |
186 | - if ($returnRow==null) // value does not exists |
|
204 | + if ($returnRow==null) { |
|
205 | + // value does not exists |
|
187 | 206 | { |
188 | 207 | $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
189 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
208 | + } |
|
209 | + if ( ! isset($default[$element])) { |
|
210 | + return null; |
|
211 | + } |
|
212 | + // no default and not value |
|
190 | 213 | |
191 | 214 | $this->addDBConfigValue($element,$default[$element]); |
192 | 215 | return $default[$element]; |
193 | 216 | } |
194 | - if ($returnRow->value == null) // value id empty |
|
217 | + if ($returnRow->value == null) { |
|
218 | + // value id empty |
|
195 | 219 | { |
196 | 220 | $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults(); |
197 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
221 | + } |
|
222 | + if ( ! isset($default[$element])) { |
|
223 | + return null; |
|
224 | + } |
|
225 | + // no default and not value |
|
198 | 226 | $this->setDBConfigValue($element,$default[$element]); |
199 | 227 | return $default[$element]; |
200 | 228 | } |
@@ -210,7 +238,9 @@ discard block |
||
210 | 238 | { |
211 | 239 | |
212 | 240 | $dbConn = $this->getDbConn(); |
213 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
241 | + if ($dbConn === null) { |
|
242 | + throw new \ErrorException('uncatched db error'); |
|
243 | + } |
|
214 | 244 | |
215 | 245 | $query=$dbConn->insert( |
216 | 246 | $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
@@ -230,7 +260,9 @@ discard block |
||
230 | 260 | { |
231 | 261 | |
232 | 262 | $dbConn = $this->getDbConn(); |
233 | - if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
263 | + if ($dbConn === null) { |
|
264 | + throw new \ErrorException('uncatched db error'); |
|
265 | + } |
|
234 | 266 | |
235 | 267 | $query=$dbConn->update( |
236 | 268 | $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(), |
@@ -17,839 +17,839 @@ discard block |
||
17 | 17 | */ |
18 | 18 | class Trap |
19 | 19 | { |
20 | - use TrapConfig; |
|
20 | + use TrapConfig; |
|
21 | 21 | |
22 | - // Configuration files and dirs |
|
23 | - /** @var string Icinga etc path */ |
|
24 | - protected $icingaweb2Etc; |
|
25 | - /** @var string $trapModuleConfig config.ini of module */ |
|
26 | - protected $trapModuleConfig; |
|
27 | - /** @var string $icingaweb2Ressources resources.ini of icingaweb2 */ |
|
28 | - protected $icingaweb2Ressources; |
|
29 | - // Options from config.ini (default values) |
|
30 | - /** @var string $snmptranslate */ |
|
31 | - protected $snmptranslate='/usr/bin/snmptranslate'; |
|
32 | - /** @var string $snmptranslate_dirs */ |
|
33 | - protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
34 | - /** @var string $icinga2cmd */ |
|
35 | - protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
36 | - /** @var string $dbPrefix */ |
|
37 | - protected $dbPrefix='traps_'; |
|
22 | + // Configuration files and dirs |
|
23 | + /** @var string Icinga etc path */ |
|
24 | + protected $icingaweb2Etc; |
|
25 | + /** @var string $trapModuleConfig config.ini of module */ |
|
26 | + protected $trapModuleConfig; |
|
27 | + /** @var string $icingaweb2Ressources resources.ini of icingaweb2 */ |
|
28 | + protected $icingaweb2Ressources; |
|
29 | + // Options from config.ini (default values) |
|
30 | + /** @var string $snmptranslate */ |
|
31 | + protected $snmptranslate='/usr/bin/snmptranslate'; |
|
32 | + /** @var string $snmptranslate_dirs */ |
|
33 | + protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
34 | + /** @var string $icinga2cmd */ |
|
35 | + protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
36 | + /** @var string $dbPrefix */ |
|
37 | + protected $dbPrefix='traps_'; |
|
38 | 38 | |
39 | - // API |
|
40 | - /** @var boolean $apiUse */ |
|
41 | - protected $apiUse=false; |
|
42 | - /** @var Icinga2API $icinga2api */ |
|
43 | - protected $icinga2api=null; |
|
44 | - /** @var string $apiHostname */ |
|
45 | - protected $apiHostname=''; |
|
46 | - /** @var integer $apiPort */ |
|
47 | - protected $apiPort=0; |
|
48 | - /** @var string $apiUsername */ |
|
49 | - protected $apiUsername=''; |
|
50 | - /** @var string $apiPassword */ |
|
51 | - protected $apiPassword=''; |
|
39 | + // API |
|
40 | + /** @var boolean $apiUse */ |
|
41 | + protected $apiUse=false; |
|
42 | + /** @var Icinga2API $icinga2api */ |
|
43 | + protected $icinga2api=null; |
|
44 | + /** @var string $apiHostname */ |
|
45 | + protected $apiHostname=''; |
|
46 | + /** @var integer $apiPort */ |
|
47 | + protected $apiPort=0; |
|
48 | + /** @var string $apiUsername */ |
|
49 | + protected $apiUsername=''; |
|
50 | + /** @var string $apiPassword */ |
|
51 | + protected $apiPassword=''; |
|
52 | 52 | |
53 | - // Logs |
|
54 | - /** @var Logging Logging class. */ |
|
55 | - public $logging; //< Logging class. |
|
56 | - /** @var bool true if log was setup in constructor */ |
|
57 | - protected $logSetup; //< bool true if log was setup in constructor |
|
53 | + // Logs |
|
54 | + /** @var Logging Logging class. */ |
|
55 | + public $logging; //< Logging class. |
|
56 | + /** @var bool true if log was setup in constructor */ |
|
57 | + protected $logSetup; //< bool true if log was setup in constructor |
|
58 | 58 | |
59 | - // Databases |
|
60 | - /** @var Database $trapsDB Database class*/ |
|
61 | - public $trapsDB = null; |
|
59 | + // Databases |
|
60 | + /** @var Database $trapsDB Database class*/ |
|
61 | + public $trapsDB = null; |
|
62 | 62 | |
63 | - // Trap received data |
|
64 | - protected $receivingHost; |
|
65 | - /** @var array Main trap data (oid, source...) */ |
|
66 | - public $trapData=array(); |
|
67 | - /** @var array $trapDataExt Additional trap data objects (oid/value).*/ |
|
68 | - public $trapDataExt=array(); |
|
69 | - /** @var int $trapId trap_id after sql insert*/ |
|
70 | - public $trapId=null; |
|
71 | - /** @var string $trapAction trap action for final write*/ |
|
72 | - public $trapAction=null; |
|
73 | - /** @var boolean $trapToDb log trap to DB */ |
|
74 | - protected $trapToDb=true; |
|
63 | + // Trap received data |
|
64 | + protected $receivingHost; |
|
65 | + /** @var array Main trap data (oid, source...) */ |
|
66 | + public $trapData=array(); |
|
67 | + /** @var array $trapDataExt Additional trap data objects (oid/value).*/ |
|
68 | + public $trapDataExt=array(); |
|
69 | + /** @var int $trapId trap_id after sql insert*/ |
|
70 | + public $trapId=null; |
|
71 | + /** @var string $trapAction trap action for final write*/ |
|
72 | + public $trapAction=null; |
|
73 | + /** @var boolean $trapToDb log trap to DB */ |
|
74 | + protected $trapToDb=true; |
|
75 | 75 | |
76 | - /** @var Mib mib class */ |
|
77 | - public $mibClass = null; |
|
76 | + /** @var Mib mib class */ |
|
77 | + public $mibClass = null; |
|
78 | 78 | |
79 | - /** @var Rule rule class */ |
|
80 | - public $ruleClass = null; |
|
79 | + /** @var Rule rule class */ |
|
80 | + public $ruleClass = null; |
|
81 | 81 | |
82 | - /** @var Plugins plugins manager **/ |
|
83 | - public $pluginClass = null; |
|
82 | + /** @var Plugins plugins manager **/ |
|
83 | + public $pluginClass = null; |
|
84 | 84 | |
85 | - /** @var TrapApi $trapApiClass */ |
|
86 | - public $trapApiClass = null; |
|
85 | + /** @var TrapApi $trapApiClass */ |
|
86 | + public $trapApiClass = null; |
|
87 | 87 | |
88 | - function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
89 | - { |
|
90 | - // Paths of ini files |
|
91 | - $this->icingaweb2Etc=$etcDir; |
|
92 | - $this->trapModuleConfig=$this->icingaweb2Etc."/modules/trapdirector/config.ini"; |
|
93 | - $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
|
88 | + function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
89 | + { |
|
90 | + // Paths of ini files |
|
91 | + $this->icingaweb2Etc=$etcDir; |
|
92 | + $this->trapModuleConfig=$this->icingaweb2Etc."/modules/trapdirector/config.ini"; |
|
93 | + $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
|
94 | 94 | |
95 | - //************* Setup logging |
|
96 | - $this->logging = new Logging(); |
|
97 | - if ($baseLogLevel != null) |
|
98 | - { |
|
99 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
100 | - $this->logSetup=true; |
|
101 | - } |
|
102 | - else |
|
103 | - { |
|
104 | - $this->logSetup=false; |
|
105 | - } |
|
106 | - $this->logging->log('Loggin started', INFO); |
|
107 | - |
|
108 | - |
|
109 | - // Create distributed API object |
|
110 | - |
|
111 | - $this->trapApiClass = new TrapApi($this->logging); |
|
112 | - |
|
113 | - //*************** Get options from ini files |
|
114 | - if (! is_file($this->trapModuleConfig)) |
|
115 | - { |
|
116 | - throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
|
117 | - } |
|
118 | - $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
119 | - if ($trapConfig == false) |
|
120 | - { |
|
121 | - $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
122 | - throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
|
123 | - } |
|
124 | - $this->getMainOptions($trapConfig); // Get main options from ini file |
|
125 | - |
|
126 | - //*************** Setup database class & get options |
|
127 | - $this->setupDatabase($trapConfig); |
|
128 | - |
|
129 | - $this->getDatabaseOptions(); // Get options in database |
|
130 | - |
|
131 | - //*************** Setup API |
|
132 | - if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
133 | - |
|
134 | - //*************** Setup MIB |
|
135 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
136 | - |
|
137 | - //*************** Setup Rule |
|
138 | - $this->ruleClass = new Rule($this); //< Create Rule class |
|
139 | - |
|
140 | - $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
141 | - 'source_ip' => 'unknown', |
|
142 | - 'source_port' => 'unknown', |
|
143 | - 'destination_ip' => 'unknown', |
|
144 | - 'destination_port' => 'unknown', |
|
145 | - 'trap_oid' => 'unknown' |
|
146 | - ); |
|
147 | - |
|
148 | - //*************** Setup Plugins |
|
149 | - //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
150 | - $this->pluginClass = new Plugins($this); |
|
95 | + //************* Setup logging |
|
96 | + $this->logging = new Logging(); |
|
97 | + if ($baseLogLevel != null) |
|
98 | + { |
|
99 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
100 | + $this->logSetup=true; |
|
101 | + } |
|
102 | + else |
|
103 | + { |
|
104 | + $this->logSetup=false; |
|
105 | + } |
|
106 | + $this->logging->log('Loggin started', INFO); |
|
107 | + |
|
108 | + |
|
109 | + // Create distributed API object |
|
110 | + |
|
111 | + $this->trapApiClass = new TrapApi($this->logging); |
|
112 | + |
|
113 | + //*************** Get options from ini files |
|
114 | + if (! is_file($this->trapModuleConfig)) |
|
115 | + { |
|
116 | + throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
|
117 | + } |
|
118 | + $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
119 | + if ($trapConfig == false) |
|
120 | + { |
|
121 | + $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
122 | + throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
|
123 | + } |
|
124 | + $this->getMainOptions($trapConfig); // Get main options from ini file |
|
125 | + |
|
126 | + //*************** Setup database class & get options |
|
127 | + $this->setupDatabase($trapConfig); |
|
128 | + |
|
129 | + $this->getDatabaseOptions(); // Get options in database |
|
130 | + |
|
131 | + //*************** Setup API |
|
132 | + if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
133 | + |
|
134 | + //*************** Setup MIB |
|
135 | + $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
136 | + |
|
137 | + //*************** Setup Rule |
|
138 | + $this->ruleClass = new Rule($this); //< Create Rule class |
|
139 | + |
|
140 | + $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
141 | + 'source_ip' => 'unknown', |
|
142 | + 'source_port' => 'unknown', |
|
143 | + 'destination_ip' => 'unknown', |
|
144 | + 'destination_port' => 'unknown', |
|
145 | + 'trap_oid' => 'unknown' |
|
146 | + ); |
|
147 | + |
|
148 | + //*************** Setup Plugins |
|
149 | + //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
150 | + $this->pluginClass = new Plugins($this); |
|
151 | 151 | |
152 | 152 | |
153 | - } |
|
153 | + } |
|
154 | 154 | |
155 | - /** @return \Trapdirector\Logging */ |
|
156 | - public function getLogging() |
|
157 | - { |
|
158 | - return $this->logging; |
|
159 | - } |
|
155 | + /** @return \Trapdirector\Logging */ |
|
156 | + public function getLogging() |
|
157 | + { |
|
158 | + return $this->logging; |
|
159 | + } |
|
160 | 160 | |
161 | - /** @return \Trapdirector\TrapApi */ |
|
162 | - public function getTrapApi() |
|
163 | - { |
|
164 | - return $this->trapApiClass; |
|
165 | - } |
|
161 | + /** @return \Trapdirector\TrapApi */ |
|
162 | + public function getTrapApi() |
|
163 | + { |
|
164 | + return $this->trapApiClass; |
|
165 | + } |
|
166 | 166 | |
167 | - /** @return \Trapdirector\Database */ |
|
168 | - public function getTrapsDB() |
|
169 | - { |
|
170 | - return $this->trapsDB; |
|
171 | - } |
|
167 | + /** @return \Trapdirector\Database */ |
|
168 | + public function getTrapsDB() |
|
169 | + { |
|
170 | + return $this->trapsDB; |
|
171 | + } |
|
172 | 172 | |
173 | - /** OBSOLETE Send log. Throws exception on critical error |
|
174 | - * @param string $message Message to log |
|
175 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
176 | - * @param string $destination file/syslog/display |
|
177 | - * @return void |
|
178 | - **/ |
|
179 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
180 | - { |
|
181 | - // TODO : replace ref with $this->logging->log |
|
182 | - $this->logging->log($message, $level, $destination); |
|
183 | - } |
|
173 | + /** OBSOLETE Send log. Throws exception on critical error |
|
174 | + * @param string $message Message to log |
|
175 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
176 | + * @param string $destination file/syslog/display |
|
177 | + * @return void |
|
178 | + **/ |
|
179 | + public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
180 | + { |
|
181 | + // TODO : replace ref with $this->logging->log |
|
182 | + $this->logging->log($message, $level, $destination); |
|
183 | + } |
|
184 | 184 | |
185 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
186 | - { |
|
187 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
188 | - } |
|
185 | + public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
186 | + { |
|
187 | + $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Returns or create new IcingaAPI object |
|
192 | - * @return \Icinga\Module\Trapdirector\Icinga2API |
|
193 | - */ |
|
194 | - protected function getAPI() |
|
195 | - { |
|
196 | - if ($this->icinga2api == null) |
|
197 | - { |
|
198 | - $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
199 | - } |
|
200 | - return $this->icinga2api; |
|
201 | - } |
|
190 | + /** |
|
191 | + * Returns or create new IcingaAPI object |
|
192 | + * @return \Icinga\Module\Trapdirector\Icinga2API |
|
193 | + */ |
|
194 | + protected function getAPI() |
|
195 | + { |
|
196 | + if ($this->icinga2api == null) |
|
197 | + { |
|
198 | + $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
199 | + } |
|
200 | + return $this->icinga2api; |
|
201 | + } |
|
202 | 202 | |
203 | 203 | |
204 | - /** |
|
205 | - * read data from stream |
|
206 | - * @param $stream string input stream, defaults to "php://stdin" |
|
207 | - * @return mixed array trap data or exception with error |
|
208 | - */ |
|
209 | - public function read_trap($stream='php://stdin') |
|
210 | - { |
|
211 | - //Read data from snmptrapd from stdin |
|
212 | - $input_stream=fopen($stream, 'r'); |
|
213 | - |
|
214 | - if ($input_stream === false) |
|
215 | - { |
|
216 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
217 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
218 | - return null; // note : exception thrown by logging |
|
219 | - } |
|
220 | - |
|
221 | - // line 1 : host |
|
222 | - $this->receivingHost=chop(fgets($input_stream)); |
|
223 | - if ($this->receivingHost === false) |
|
224 | - { |
|
225 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
226 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
227 | - } |
|
228 | - // line 2 IP:port=>IP:port |
|
229 | - $IP=chop(fgets($input_stream)); |
|
230 | - if ($IP === false) |
|
231 | - { |
|
232 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
233 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
234 | - } |
|
235 | - $matches=array(); |
|
236 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
237 | - if ($ret_code===0 || $ret_code===false) |
|
238 | - { |
|
239 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
240 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
241 | - } |
|
242 | - else |
|
243 | - { |
|
244 | - $this->trapData['source_ip']=$matches[1]; |
|
245 | - $this->trapData['destination_ip']=$matches[3]; |
|
246 | - $this->trapData['source_port']=$matches[2]; |
|
247 | - $this->trapData['destination_port']=$matches[4]; |
|
248 | - } |
|
249 | - |
|
250 | - while (($vars=fgets($input_stream)) !==false) |
|
251 | - { |
|
252 | - $vars=chop($vars); |
|
253 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
254 | - if ($ret_code===0 || $ret_code===false) |
|
255 | - { |
|
256 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
257 | - } |
|
258 | - else |
|
259 | - { |
|
260 | - if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
261 | - { |
|
262 | - $this->trapData['trap_oid']=$matches[2]; |
|
263 | - } |
|
264 | - else |
|
265 | - { |
|
266 | - $object= new stdClass; |
|
267 | - $object->oid =$matches[1]; |
|
268 | - $object->value = $matches[2]; |
|
269 | - array_push($this->trapDataExt,$object); |
|
270 | - } |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - if ($this->trapData['trap_oid']=='unknown') |
|
275 | - { |
|
276 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
277 | - $this->logging->log('no trap oid found',ERROR,''); |
|
278 | - } |
|
279 | - |
|
280 | - // Translate oids. |
|
281 | - |
|
282 | - $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
283 | - if ($retArray != null) |
|
284 | - { |
|
285 | - $this->trapData['trap_name']=$retArray['trap_name']; |
|
286 | - $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
287 | - } |
|
288 | - foreach ($this->trapDataExt as $key => $val) |
|
289 | - { |
|
290 | - $retArray=$this->translateOID($val->oid); |
|
291 | - if ($retArray != null) |
|
292 | - { |
|
293 | - $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
294 | - $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - $this->trapData['status']= 'waiting'; |
|
300 | - |
|
301 | - return $this->trapData; |
|
302 | - } |
|
204 | + /** |
|
205 | + * read data from stream |
|
206 | + * @param $stream string input stream, defaults to "php://stdin" |
|
207 | + * @return mixed array trap data or exception with error |
|
208 | + */ |
|
209 | + public function read_trap($stream='php://stdin') |
|
210 | + { |
|
211 | + //Read data from snmptrapd from stdin |
|
212 | + $input_stream=fopen($stream, 'r'); |
|
213 | + |
|
214 | + if ($input_stream === false) |
|
215 | + { |
|
216 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
217 | + $this->logging->log("Error reading stdin !",ERROR,''); |
|
218 | + return null; // note : exception thrown by logging |
|
219 | + } |
|
220 | + |
|
221 | + // line 1 : host |
|
222 | + $this->receivingHost=chop(fgets($input_stream)); |
|
223 | + if ($this->receivingHost === false) |
|
224 | + { |
|
225 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
226 | + $this->logging->log("Error reading Host !",ERROR,''); |
|
227 | + } |
|
228 | + // line 2 IP:port=>IP:port |
|
229 | + $IP=chop(fgets($input_stream)); |
|
230 | + if ($IP === false) |
|
231 | + { |
|
232 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
233 | + $this->logging->log("Error reading IP !",ERROR,''); |
|
234 | + } |
|
235 | + $matches=array(); |
|
236 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
237 | + if ($ret_code===0 || $ret_code===false) |
|
238 | + { |
|
239 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
240 | + $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
241 | + } |
|
242 | + else |
|
243 | + { |
|
244 | + $this->trapData['source_ip']=$matches[1]; |
|
245 | + $this->trapData['destination_ip']=$matches[3]; |
|
246 | + $this->trapData['source_port']=$matches[2]; |
|
247 | + $this->trapData['destination_port']=$matches[4]; |
|
248 | + } |
|
249 | + |
|
250 | + while (($vars=fgets($input_stream)) !==false) |
|
251 | + { |
|
252 | + $vars=chop($vars); |
|
253 | + $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
254 | + if ($ret_code===0 || $ret_code===false) |
|
255 | + { |
|
256 | + $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
257 | + } |
|
258 | + else |
|
259 | + { |
|
260 | + if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
261 | + { |
|
262 | + $this->trapData['trap_oid']=$matches[2]; |
|
263 | + } |
|
264 | + else |
|
265 | + { |
|
266 | + $object= new stdClass; |
|
267 | + $object->oid =$matches[1]; |
|
268 | + $object->value = $matches[2]; |
|
269 | + array_push($this->trapDataExt,$object); |
|
270 | + } |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + if ($this->trapData['trap_oid']=='unknown') |
|
275 | + { |
|
276 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
277 | + $this->logging->log('no trap oid found',ERROR,''); |
|
278 | + } |
|
279 | + |
|
280 | + // Translate oids. |
|
281 | + |
|
282 | + $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
283 | + if ($retArray != null) |
|
284 | + { |
|
285 | + $this->trapData['trap_name']=$retArray['trap_name']; |
|
286 | + $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
287 | + } |
|
288 | + foreach ($this->trapDataExt as $key => $val) |
|
289 | + { |
|
290 | + $retArray=$this->translateOID($val->oid); |
|
291 | + if ($retArray != null) |
|
292 | + { |
|
293 | + $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
294 | + $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + $this->trapData['status']= 'waiting'; |
|
300 | + |
|
301 | + return $this->trapData; |
|
302 | + } |
|
303 | 303 | |
304 | - /** |
|
305 | - * Translate oid into array(MIB,Name) |
|
306 | - * @param $oid string oid to translate |
|
307 | - * @return mixed : null if not found or array(MIB,Name) |
|
308 | - */ |
|
309 | - public function translateOID($oid) |
|
310 | - { |
|
311 | - // try from database |
|
312 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
313 | - |
|
314 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
315 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
316 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
317 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
318 | - } |
|
319 | - $name=$ret_code->fetch(); |
|
320 | - if ($name['name'] != null) |
|
321 | - { |
|
322 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
323 | - } |
|
324 | - |
|
325 | - // Also check if it is an instance of OID |
|
326 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
327 | - |
|
328 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
329 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
330 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
331 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
332 | - } |
|
333 | - $name=$ret_code->fetch(); |
|
334 | - if ($name['name'] != null) |
|
335 | - { |
|
336 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
337 | - } |
|
338 | - |
|
339 | - // Try to get oid name from snmptranslate |
|
340 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
341 | - ' '.$oid); |
|
342 | - $matches=array(); |
|
343 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
344 | - if ($ret_code===0 || $ret_code === false) { |
|
345 | - return NULL; |
|
346 | - } else { |
|
347 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
348 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
349 | - } |
|
350 | - } |
|
304 | + /** |
|
305 | + * Translate oid into array(MIB,Name) |
|
306 | + * @param $oid string oid to translate |
|
307 | + * @return mixed : null if not found or array(MIB,Name) |
|
308 | + */ |
|
309 | + public function translateOID($oid) |
|
310 | + { |
|
311 | + // try from database |
|
312 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
313 | + |
|
314 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
315 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
316 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
317 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
318 | + } |
|
319 | + $name=$ret_code->fetch(); |
|
320 | + if ($name['name'] != null) |
|
321 | + { |
|
322 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
323 | + } |
|
324 | + |
|
325 | + // Also check if it is an instance of OID |
|
326 | + $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
327 | + |
|
328 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
329 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
330 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
331 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
332 | + } |
|
333 | + $name=$ret_code->fetch(); |
|
334 | + if ($name['name'] != null) |
|
335 | + { |
|
336 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
337 | + } |
|
338 | + |
|
339 | + // Try to get oid name from snmptranslate |
|
340 | + $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
341 | + ' '.$oid); |
|
342 | + $matches=array(); |
|
343 | + $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
344 | + if ($ret_code===0 || $ret_code === false) { |
|
345 | + return NULL; |
|
346 | + } else { |
|
347 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
348 | + return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
349 | + } |
|
350 | + } |
|
351 | 351 | |
352 | - /** |
|
353 | - * Erase old trap records |
|
354 | - * @param integer $days : erase traps when more than $days old |
|
355 | - * @return integer : number of lines deleted |
|
356 | - **/ |
|
357 | - public function eraseOldTraps($days=0) |
|
358 | - { |
|
359 | - if ($days==0) |
|
360 | - { |
|
361 | - if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
362 | - { |
|
363 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
364 | - return; |
|
365 | - } |
|
366 | - } |
|
367 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
368 | - $daysago = strtotime("-".$days." day"); |
|
369 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
370 | - if ($db_conn->query($sql) === false) { |
|
371 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
372 | - } |
|
373 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
374 | - } |
|
352 | + /** |
|
353 | + * Erase old trap records |
|
354 | + * @param integer $days : erase traps when more than $days old |
|
355 | + * @return integer : number of lines deleted |
|
356 | + **/ |
|
357 | + public function eraseOldTraps($days=0) |
|
358 | + { |
|
359 | + if ($days==0) |
|
360 | + { |
|
361 | + if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
362 | + { |
|
363 | + $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
364 | + return; |
|
365 | + } |
|
366 | + } |
|
367 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
368 | + $daysago = strtotime("-".$days." day"); |
|
369 | + $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
370 | + if ($db_conn->query($sql) === false) { |
|
371 | + $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
372 | + } |
|
373 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
374 | + } |
|
375 | 375 | |
376 | - /** Write error to received trap database |
|
377 | - */ |
|
378 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
379 | - { |
|
380 | - |
|
381 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
382 | - |
|
383 | - // add date time |
|
384 | - $insert_col ='date_received,status'; |
|
385 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
386 | - |
|
387 | - if ($sourceIP !=null) |
|
388 | - { |
|
389 | - $insert_col .=',source_ip'; |
|
390 | - $insert_val .=",'". $sourceIP ."'"; |
|
391 | - } |
|
392 | - if ($trapoid !=null) |
|
393 | - { |
|
394 | - $insert_col .=',trap_oid'; |
|
395 | - $insert_val .=",'". $trapoid ."'"; |
|
396 | - } |
|
397 | - $insert_col .=',status_detail'; |
|
398 | - $insert_val .=",'". $message ."'"; |
|
399 | - |
|
400 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
401 | - |
|
402 | - switch ($this->trapsDB->trapDBType) |
|
403 | - { |
|
404 | - case 'pgsql': |
|
405 | - $sql .= ' RETURNING id;'; |
|
406 | - $this->logging->log('sql : '.$sql,INFO); |
|
407 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
408 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
409 | - } |
|
410 | - $this->logging->log('SQL insertion OK',INFO ); |
|
411 | - // Get last id to insert oid/values in secondary table |
|
412 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
376 | + /** Write error to received trap database |
|
377 | + */ |
|
378 | + public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
379 | + { |
|
380 | + |
|
381 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
382 | + |
|
383 | + // add date time |
|
384 | + $insert_col ='date_received,status'; |
|
385 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
386 | + |
|
387 | + if ($sourceIP !=null) |
|
388 | + { |
|
389 | + $insert_col .=',source_ip'; |
|
390 | + $insert_val .=",'". $sourceIP ."'"; |
|
391 | + } |
|
392 | + if ($trapoid !=null) |
|
393 | + { |
|
394 | + $insert_col .=',trap_oid'; |
|
395 | + $insert_val .=",'". $trapoid ."'"; |
|
396 | + } |
|
397 | + $insert_col .=',status_detail'; |
|
398 | + $insert_val .=",'". $message ."'"; |
|
399 | + |
|
400 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
401 | + |
|
402 | + switch ($this->trapsDB->trapDBType) |
|
403 | + { |
|
404 | + case 'pgsql': |
|
405 | + $sql .= ' RETURNING id;'; |
|
406 | + $this->logging->log('sql : '.$sql,INFO); |
|
407 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
408 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
409 | + } |
|
410 | + $this->logging->log('SQL insertion OK',INFO ); |
|
411 | + // Get last id to insert oid/values in secondary table |
|
412 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
413 | 413 | |
414 | - $this->logging->log('Erreur recuperation id',1,''); |
|
415 | - } |
|
416 | - if (! isset($inserted_id_ret['id'])) { |
|
417 | - $this->logging->log('Error getting id',1,''); |
|
418 | - } |
|
419 | - $this->trapId=$inserted_id_ret['id']; |
|
420 | - break; |
|
421 | - case 'mysql': |
|
422 | - $sql .= ';'; |
|
423 | - $this->logging->log('sql : '.$sql,INFO ); |
|
424 | - if ($db_conn->query($sql) === false) { |
|
425 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
426 | - } |
|
427 | - $this->logging->log('SQL insertion OK',INFO ); |
|
428 | - // Get last id to insert oid/values in secondary table |
|
429 | - $sql='SELECT LAST_INSERT_ID();'; |
|
430 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
431 | - $this->logging->log('Erreur recuperation id',1,''); |
|
432 | - } |
|
414 | + $this->logging->log('Erreur recuperation id',1,''); |
|
415 | + } |
|
416 | + if (! isset($inserted_id_ret['id'])) { |
|
417 | + $this->logging->log('Error getting id',1,''); |
|
418 | + } |
|
419 | + $this->trapId=$inserted_id_ret['id']; |
|
420 | + break; |
|
421 | + case 'mysql': |
|
422 | + $sql .= ';'; |
|
423 | + $this->logging->log('sql : '.$sql,INFO ); |
|
424 | + if ($db_conn->query($sql) === false) { |
|
425 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
426 | + } |
|
427 | + $this->logging->log('SQL insertion OK',INFO ); |
|
428 | + // Get last id to insert oid/values in secondary table |
|
429 | + $sql='SELECT LAST_INSERT_ID();'; |
|
430 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
431 | + $this->logging->log('Erreur recuperation id',1,''); |
|
432 | + } |
|
433 | 433 | |
434 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
435 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
436 | - $this->trapId=$inserted_id; |
|
437 | - break; |
|
438 | - default: |
|
439 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
440 | - } |
|
441 | - |
|
442 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
443 | - } |
|
434 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
435 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
436 | + $this->trapId=$inserted_id; |
|
437 | + break; |
|
438 | + default: |
|
439 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
440 | + } |
|
441 | + |
|
442 | + $this->logging->log('id found: '. $this->trapId,INFO ); |
|
443 | + } |
|
444 | 444 | |
445 | - /** Write trap data to trap database |
|
446 | - */ |
|
447 | - public function writeTrapToDB() |
|
448 | - { |
|
449 | - |
|
450 | - // If action is ignore -> don't send t DB |
|
451 | - if ($this->trapToDb === false) return; |
|
452 | - |
|
453 | - |
|
454 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
455 | - |
|
456 | - $insert_col=''; |
|
457 | - $insert_val=''; |
|
458 | - // add date time |
|
459 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
460 | - |
|
461 | - $firstcol=1; |
|
462 | - foreach ($this->trapData as $col => $val) |
|
463 | - { |
|
464 | - if ($firstcol==0) |
|
465 | - { |
|
466 | - $insert_col .=','; |
|
467 | - $insert_val .=','; |
|
468 | - } |
|
469 | - $insert_col .= $col ; |
|
470 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
471 | - $firstcol=0; |
|
472 | - } |
|
473 | - |
|
474 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
475 | - switch ($this->trapsDB->trapDBType) |
|
476 | - { |
|
477 | - case 'pgsql': |
|
478 | - $sql .= ' RETURNING id;'; |
|
479 | - $this->logging->log('sql : '.$sql,INFO ); |
|
480 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
481 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
482 | - } |
|
483 | - $this->logging->log('SQL insertion OK',INFO ); |
|
484 | - // Get last id to insert oid/values in secondary table |
|
485 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
445 | + /** Write trap data to trap database |
|
446 | + */ |
|
447 | + public function writeTrapToDB() |
|
448 | + { |
|
449 | + |
|
450 | + // If action is ignore -> don't send t DB |
|
451 | + if ($this->trapToDb === false) return; |
|
452 | + |
|
453 | + |
|
454 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
455 | + |
|
456 | + $insert_col=''; |
|
457 | + $insert_val=''; |
|
458 | + // add date time |
|
459 | + $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
460 | + |
|
461 | + $firstcol=1; |
|
462 | + foreach ($this->trapData as $col => $val) |
|
463 | + { |
|
464 | + if ($firstcol==0) |
|
465 | + { |
|
466 | + $insert_col .=','; |
|
467 | + $insert_val .=','; |
|
468 | + } |
|
469 | + $insert_col .= $col ; |
|
470 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
471 | + $firstcol=0; |
|
472 | + } |
|
473 | + |
|
474 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
475 | + switch ($this->trapsDB->trapDBType) |
|
476 | + { |
|
477 | + case 'pgsql': |
|
478 | + $sql .= ' RETURNING id;'; |
|
479 | + $this->logging->log('sql : '.$sql,INFO ); |
|
480 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
481 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
482 | + } |
|
483 | + $this->logging->log('SQL insertion OK',INFO ); |
|
484 | + // Get last id to insert oid/values in secondary table |
|
485 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
486 | 486 | |
487 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
488 | - } |
|
489 | - if (! isset($inserted_id_ret['id'])) { |
|
490 | - $this->logging->log('Error getting id',ERROR,''); |
|
491 | - } |
|
492 | - $this->trapId=$inserted_id_ret['id']; |
|
493 | - break; |
|
494 | - case 'mysql': |
|
495 | - $sql .= ';'; |
|
496 | - $this->logging->log('sql : '.$sql,INFO ); |
|
497 | - if ($db_conn->query($sql) === false) { |
|
498 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
499 | - } |
|
500 | - $this->logging->log('SQL insertion OK',INFO ); |
|
501 | - // Get last id to insert oid/values in secondary table |
|
502 | - $sql='SELECT LAST_INSERT_ID();'; |
|
503 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
504 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
505 | - } |
|
487 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
488 | + } |
|
489 | + if (! isset($inserted_id_ret['id'])) { |
|
490 | + $this->logging->log('Error getting id',ERROR,''); |
|
491 | + } |
|
492 | + $this->trapId=$inserted_id_ret['id']; |
|
493 | + break; |
|
494 | + case 'mysql': |
|
495 | + $sql .= ';'; |
|
496 | + $this->logging->log('sql : '.$sql,INFO ); |
|
497 | + if ($db_conn->query($sql) === false) { |
|
498 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
499 | + } |
|
500 | + $this->logging->log('SQL insertion OK',INFO ); |
|
501 | + // Get last id to insert oid/values in secondary table |
|
502 | + $sql='SELECT LAST_INSERT_ID();'; |
|
503 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
504 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
505 | + } |
|
506 | 506 | |
507 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
508 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
509 | - $this->trapId=$inserted_id; |
|
510 | - break; |
|
511 | - default: |
|
512 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
513 | - } |
|
514 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
515 | - |
|
516 | - // Fill trap extended data table |
|
517 | - foreach ($this->trapDataExt as $value) { |
|
518 | - // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
519 | - $firstcol=1; |
|
520 | - $value->trap_id = $this->trapId; |
|
521 | - $insert_col=''; |
|
522 | - $insert_val=''; |
|
523 | - foreach ($value as $col => $val) |
|
524 | - { |
|
525 | - if ($firstcol==0) |
|
526 | - { |
|
527 | - $insert_col .=','; |
|
528 | - $insert_val .=','; |
|
529 | - } |
|
530 | - $insert_col .= $col; |
|
531 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
532 | - $firstcol=0; |
|
533 | - } |
|
507 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
508 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
509 | + $this->trapId=$inserted_id; |
|
510 | + break; |
|
511 | + default: |
|
512 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
513 | + } |
|
514 | + $this->logging->log('id found: '.$this->trapId,INFO ); |
|
515 | + |
|
516 | + // Fill trap extended data table |
|
517 | + foreach ($this->trapDataExt as $value) { |
|
518 | + // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
519 | + $firstcol=1; |
|
520 | + $value->trap_id = $this->trapId; |
|
521 | + $insert_col=''; |
|
522 | + $insert_val=''; |
|
523 | + foreach ($value as $col => $val) |
|
524 | + { |
|
525 | + if ($firstcol==0) |
|
526 | + { |
|
527 | + $insert_col .=','; |
|
528 | + $insert_val .=','; |
|
529 | + } |
|
530 | + $insert_col .= $col; |
|
531 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
532 | + $firstcol=0; |
|
533 | + } |
|
534 | 534 | |
535 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
535 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
536 | 536 | |
537 | - if ($db_conn->query($sql) === false) { |
|
538 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
539 | - } |
|
540 | - } |
|
541 | - } |
|
537 | + if ($db_conn->query($sql) === false) { |
|
538 | + $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
539 | + } |
|
540 | + } |
|
541 | + } |
|
542 | 542 | |
543 | - /** Get rules from rule database with ip and oid |
|
544 | - * @param $ip string ipv4 or ipv6 |
|
545 | - * @param $oid string oid in numeric |
|
546 | - * @return mixed|boolean : PDO object or false |
|
547 | - */ |
|
548 | - protected function getRules($ip,$oid) |
|
549 | - { |
|
550 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
551 | - // fetch rules based on IP in rule and OID |
|
552 | - $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
553 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
554 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
555 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
556 | - return false; |
|
557 | - } |
|
558 | - $rules_all=$ret_code->fetchAll(); |
|
559 | - //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
560 | - $rules_ret=array(); |
|
561 | - $rule_ret_key=0; |
|
562 | - foreach ($rules_all as $key => $rule) |
|
563 | - { |
|
564 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
565 | - { |
|
566 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
567 | - //TODO : get host name by API (and check if correct in rule). |
|
568 | - $rule_ret_key++; |
|
569 | - continue; |
|
570 | - } |
|
571 | - // TODO : get hosts IP by API |
|
572 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
573 | - { // get ips of group members by oid |
|
574 | - $db_conn2=$this->trapsDB->db_connect_ido(); |
|
575 | - $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
543 | + /** Get rules from rule database with ip and oid |
|
544 | + * @param $ip string ipv4 or ipv6 |
|
545 | + * @param $oid string oid in numeric |
|
546 | + * @return mixed|boolean : PDO object or false |
|
547 | + */ |
|
548 | + protected function getRules($ip,$oid) |
|
549 | + { |
|
550 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
551 | + // fetch rules based on IP in rule and OID |
|
552 | + $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
553 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
554 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
555 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
556 | + return false; |
|
557 | + } |
|
558 | + $rules_all=$ret_code->fetchAll(); |
|
559 | + //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
560 | + $rules_ret=array(); |
|
561 | + $rule_ret_key=0; |
|
562 | + foreach ($rules_all as $key => $rule) |
|
563 | + { |
|
564 | + if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
565 | + { |
|
566 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
567 | + //TODO : get host name by API (and check if correct in rule). |
|
568 | + $rule_ret_key++; |
|
569 | + continue; |
|
570 | + } |
|
571 | + // TODO : get hosts IP by API |
|
572 | + if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
573 | + { // get ips of group members by oid |
|
574 | + $db_conn2=$this->trapsDB->db_connect_ido(); |
|
575 | + $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
576 | 576 | FROM icinga_objects as o |
577 | 577 | LEFT JOIN icinga_hostgroups as h ON o.object_id=h.hostgroup_object_id |
578 | 578 | LEFT JOIN icinga_hostgroup_members as m ON h.hostgroup_id=m.hostgroup_id |
579 | 579 | LEFT JOIN icinga_hosts as a ON a.host_object_id = m.host_object_id |
580 | 580 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
581 | 581 | WHERE o.name1='".$rule['host_group_name']."';"; |
582 | - if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
583 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
584 | - continue; |
|
585 | - } |
|
586 | - $grouphosts=$ret_code2->fetchAll(); |
|
587 | - //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
588 | - foreach ( $grouphosts as $host) |
|
589 | - { |
|
590 | - //echo $host['ip4']."\n"; |
|
591 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
592 | - { |
|
593 | - //echo "Rule added \n"; |
|
594 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
595 | - $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
596 | - $rule_ret_key++; |
|
597 | - } |
|
598 | - } |
|
599 | - } |
|
600 | - } |
|
601 | - //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
602 | - return $rules_ret; |
|
603 | - } |
|
582 | + if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
583 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
584 | + continue; |
|
585 | + } |
|
586 | + $grouphosts=$ret_code2->fetchAll(); |
|
587 | + //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
588 | + foreach ( $grouphosts as $host) |
|
589 | + { |
|
590 | + //echo $host['ip4']."\n"; |
|
591 | + if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
592 | + { |
|
593 | + //echo "Rule added \n"; |
|
594 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
595 | + $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
596 | + $rule_ret_key++; |
|
597 | + } |
|
598 | + } |
|
599 | + } |
|
600 | + } |
|
601 | + //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
602 | + return $rules_ret; |
|
603 | + } |
|
604 | 604 | |
605 | - /** Add rule match to rule |
|
606 | - * @param id int : rule id |
|
607 | - * @param set int : value to set |
|
608 | - */ |
|
609 | - protected function add_rule_match($id, $set) |
|
610 | - { |
|
611 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
612 | - $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
613 | - if ($db_conn->query($sql) === false) { |
|
614 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
615 | - } |
|
616 | - } |
|
605 | + /** Add rule match to rule |
|
606 | + * @param id int : rule id |
|
607 | + * @param set int : value to set |
|
608 | + */ |
|
609 | + protected function add_rule_match($id, $set) |
|
610 | + { |
|
611 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
612 | + $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
613 | + if ($db_conn->query($sql) === false) { |
|
614 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
615 | + } |
|
616 | + } |
|
617 | 617 | |
618 | - /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
619 | - * |
|
620 | - * @param string $host |
|
621 | - * @param string $service |
|
622 | - * @param integer $state numerical staus |
|
623 | - * @param string $display |
|
624 | - * @returnn bool true is service check was sent without error |
|
625 | - */ |
|
626 | - public function serviceCheckResult($host,$service,$state,$display) |
|
627 | - { |
|
628 | - if ($this->apiUse === false) |
|
629 | - { |
|
630 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
631 | - $host.';' .$service .';' . $state . ';'.$display; |
|
632 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
618 | + /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
619 | + * |
|
620 | + * @param string $host |
|
621 | + * @param string $service |
|
622 | + * @param integer $state numerical staus |
|
623 | + * @param string $display |
|
624 | + * @returnn bool true is service check was sent without error |
|
625 | + */ |
|
626 | + public function serviceCheckResult($host,$service,$state,$display) |
|
627 | + { |
|
628 | + if ($this->apiUse === false) |
|
629 | + { |
|
630 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
631 | + $host.';' .$service .';' . $state . ';'.$display; |
|
632 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
633 | 633 | |
634 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
635 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
636 | - return true; |
|
637 | - } |
|
638 | - else |
|
639 | - { |
|
640 | - // Get perfdata if found |
|
641 | - $matches=array(); |
|
642 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
643 | - { |
|
644 | - $display=$matches[1]; |
|
645 | - $perfdata=$matches[2]; |
|
646 | - } |
|
647 | - else |
|
648 | - { |
|
649 | - $perfdata=''; |
|
650 | - } |
|
634 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
635 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
636 | + return true; |
|
637 | + } |
|
638 | + else |
|
639 | + { |
|
640 | + // Get perfdata if found |
|
641 | + $matches=array(); |
|
642 | + if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
643 | + { |
|
644 | + $display=$matches[1]; |
|
645 | + $perfdata=$matches[2]; |
|
646 | + } |
|
647 | + else |
|
648 | + { |
|
649 | + $perfdata=''; |
|
650 | + } |
|
651 | 651 | |
652 | - $api = $this->getAPI(); |
|
653 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
654 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
655 | - if ($retcode == false) |
|
656 | - { |
|
657 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
658 | - return false; |
|
659 | - } |
|
660 | - else |
|
661 | - { |
|
662 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
663 | - return true; |
|
664 | - } |
|
665 | - } |
|
666 | - } |
|
652 | + $api = $this->getAPI(); |
|
653 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
654 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
655 | + if ($retcode == false) |
|
656 | + { |
|
657 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
658 | + return false; |
|
659 | + } |
|
660 | + else |
|
661 | + { |
|
662 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
663 | + return true; |
|
664 | + } |
|
665 | + } |
|
666 | + } |
|
667 | 667 | |
668 | - public function getHostByIP($ip) |
|
669 | - { |
|
670 | - $api = $this->getAPI(); |
|
671 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
672 | - return $api->getHostByIP($ip); |
|
673 | - } |
|
668 | + public function getHostByIP($ip) |
|
669 | + { |
|
670 | + $api = $this->getAPI(); |
|
671 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
672 | + return $api->getHostByIP($ip); |
|
673 | + } |
|
674 | 674 | |
675 | - /** Resolve display. |
|
676 | - * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
677 | - * @param $display string |
|
678 | - * @return string display |
|
679 | - */ |
|
680 | - protected function applyDisplay($display) |
|
681 | - { |
|
682 | - $matches=array(); |
|
683 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
684 | - { |
|
685 | - $oid=$matches[1]; |
|
686 | - $found=0; |
|
687 | - // Test and transform regexp |
|
688 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
675 | + /** Resolve display. |
|
676 | + * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
677 | + * @param $display string |
|
678 | + * @return string display |
|
679 | + */ |
|
680 | + protected function applyDisplay($display) |
|
681 | + { |
|
682 | + $matches=array(); |
|
683 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
684 | + { |
|
685 | + $oid=$matches[1]; |
|
686 | + $found=0; |
|
687 | + // Test and transform regexp |
|
688 | + $oidR = $this->ruleClass->regexp_eval($oid); |
|
689 | 689 | |
690 | - foreach($this->trapDataExt as $val) |
|
691 | - { |
|
692 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
693 | - { |
|
694 | - $val->value=preg_replace('/"/','',$val->value); |
|
695 | - $rep=0; |
|
696 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
697 | - if ($rep==0) |
|
698 | - { |
|
699 | - $this->logging->log("Error in display",WARN,''); |
|
700 | - return $display; |
|
701 | - } |
|
702 | - $found=1; |
|
703 | - break; |
|
704 | - } |
|
705 | - } |
|
706 | - if ($found==0) |
|
707 | - { |
|
708 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
709 | - if ($rep==0) |
|
710 | - { |
|
711 | - $this->logging->log("Error in display",WARN,''); |
|
712 | - return $display; |
|
713 | - } |
|
714 | - } |
|
715 | - } |
|
716 | - return $display; |
|
717 | - } |
|
690 | + foreach($this->trapDataExt as $val) |
|
691 | + { |
|
692 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
693 | + { |
|
694 | + $val->value=preg_replace('/"/','',$val->value); |
|
695 | + $rep=0; |
|
696 | + $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
697 | + if ($rep==0) |
|
698 | + { |
|
699 | + $this->logging->log("Error in display",WARN,''); |
|
700 | + return $display; |
|
701 | + } |
|
702 | + $found=1; |
|
703 | + break; |
|
704 | + } |
|
705 | + } |
|
706 | + if ($found==0) |
|
707 | + { |
|
708 | + $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
709 | + if ($rep==0) |
|
710 | + { |
|
711 | + $this->logging->log("Error in display",WARN,''); |
|
712 | + return $display; |
|
713 | + } |
|
714 | + } |
|
715 | + } |
|
716 | + return $display; |
|
717 | + } |
|
718 | 718 | |
719 | - /** Match rules for current trap and do action |
|
720 | - */ |
|
721 | - public function applyRules() |
|
722 | - { |
|
723 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
724 | - |
|
725 | - if ($rules===false || count($rules)==0) |
|
726 | - { |
|
727 | - $this->logging->log('No rules found for this trap',INFO ); |
|
728 | - $this->trapData['status']='unknown'; |
|
729 | - $this->trapToDb=true; |
|
730 | - return; |
|
731 | - } |
|
732 | - //print_r($rules); |
|
733 | - // Evaluate all rules in sequence |
|
734 | - $this->trapAction=null; |
|
735 | - foreach ($rules as $rule) |
|
736 | - { |
|
719 | + /** Match rules for current trap and do action |
|
720 | + */ |
|
721 | + public function applyRules() |
|
722 | + { |
|
723 | + $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
724 | + |
|
725 | + if ($rules===false || count($rules)==0) |
|
726 | + { |
|
727 | + $this->logging->log('No rules found for this trap',INFO ); |
|
728 | + $this->trapData['status']='unknown'; |
|
729 | + $this->trapToDb=true; |
|
730 | + return; |
|
731 | + } |
|
732 | + //print_r($rules); |
|
733 | + // Evaluate all rules in sequence |
|
734 | + $this->trapAction=null; |
|
735 | + foreach ($rules as $rule) |
|
736 | + { |
|
737 | 737 | |
738 | - $host_name=$rule['host_name']; |
|
739 | - $service_name=$rule['service_name']; |
|
738 | + $host_name=$rule['host_name']; |
|
739 | + $service_name=$rule['service_name']; |
|
740 | 740 | |
741 | - $display=$this->applyDisplay($rule['display']); |
|
742 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
743 | - try |
|
744 | - { |
|
745 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
746 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
747 | - //->eval_rule($rule['rule']); |
|
741 | + $display=$this->applyDisplay($rule['display']); |
|
742 | + $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
743 | + try |
|
744 | + { |
|
745 | + $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
746 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
747 | + //->eval_rule($rule['rule']); |
|
748 | 748 | |
749 | - if ($evalr == true) |
|
750 | - { |
|
751 | - //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
752 | - $action=$rule['action_match']; |
|
753 | - $this->logging->log('action OK : '.$action,INFO ); |
|
754 | - if ($action >= 0) |
|
755 | - { |
|
756 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
757 | - { |
|
758 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
759 | - } |
|
760 | - else |
|
761 | - { |
|
762 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
763 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
764 | - } |
|
765 | - } |
|
766 | - else |
|
767 | - { |
|
768 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
769 | - } |
|
770 | - $this->trapToDb=($action==-2)?false:true; |
|
771 | - } |
|
772 | - else |
|
773 | - { |
|
774 | - //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
749 | + if ($evalr == true) |
|
750 | + { |
|
751 | + //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
752 | + $action=$rule['action_match']; |
|
753 | + $this->logging->log('action OK : '.$action,INFO ); |
|
754 | + if ($action >= 0) |
|
755 | + { |
|
756 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
757 | + { |
|
758 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
759 | + } |
|
760 | + else |
|
761 | + { |
|
762 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
763 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
764 | + } |
|
765 | + } |
|
766 | + else |
|
767 | + { |
|
768 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
769 | + } |
|
770 | + $this->trapToDb=($action==-2)?false:true; |
|
771 | + } |
|
772 | + else |
|
773 | + { |
|
774 | + //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
775 | 775 | |
776 | - $action=$rule['action_nomatch']; |
|
777 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
778 | - if ($action >= 0) |
|
779 | - { |
|
780 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
781 | - { |
|
782 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
783 | - } |
|
784 | - else |
|
785 | - { |
|
786 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
787 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
788 | - } |
|
789 | - } |
|
790 | - else |
|
791 | - { |
|
792 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
793 | - } |
|
794 | - $this->trapToDb=($action==-2)?false:true; |
|
795 | - } |
|
796 | - // Put name in source_name |
|
797 | - if (!isset($this->trapData['source_name'])) |
|
798 | - { |
|
799 | - $this->trapData['source_name']=$rule['host_name']; |
|
800 | - } |
|
801 | - else |
|
802 | - { |
|
803 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
804 | - { // only add if not present |
|
805 | - $this->trapData['source_name'].=','.$rule['host_name']; |
|
806 | - } |
|
807 | - } |
|
808 | - } |
|
809 | - catch (Exception $e) |
|
810 | - { |
|
811 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
812 | - $this->trapAction.=' ERR : '.$e->getMessage(); |
|
813 | - $this->trapData['status']='error'; |
|
814 | - } |
|
776 | + $action=$rule['action_nomatch']; |
|
777 | + $this->logging->log('action NOK : '.$action,INFO ); |
|
778 | + if ($action >= 0) |
|
779 | + { |
|
780 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
781 | + { |
|
782 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
783 | + } |
|
784 | + else |
|
785 | + { |
|
786 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
787 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
788 | + } |
|
789 | + } |
|
790 | + else |
|
791 | + { |
|
792 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
793 | + } |
|
794 | + $this->trapToDb=($action==-2)?false:true; |
|
795 | + } |
|
796 | + // Put name in source_name |
|
797 | + if (!isset($this->trapData['source_name'])) |
|
798 | + { |
|
799 | + $this->trapData['source_name']=$rule['host_name']; |
|
800 | + } |
|
801 | + else |
|
802 | + { |
|
803 | + if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
804 | + { // only add if not present |
|
805 | + $this->trapData['source_name'].=','.$rule['host_name']; |
|
806 | + } |
|
807 | + } |
|
808 | + } |
|
809 | + catch (Exception $e) |
|
810 | + { |
|
811 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
812 | + $this->trapAction.=' ERR : '.$e->getMessage(); |
|
813 | + $this->trapData['status']='error'; |
|
814 | + } |
|
815 | 815 | |
816 | - } |
|
817 | - if ($this->trapData['status']=='error') |
|
818 | - { |
|
819 | - $this->trapToDb=true; // Always put errors in DB for the use can see |
|
820 | - } |
|
821 | - else |
|
822 | - { |
|
823 | - $this->trapData['status']='done'; |
|
824 | - } |
|
825 | - } |
|
816 | + } |
|
817 | + if ($this->trapData['status']=='error') |
|
818 | + { |
|
819 | + $this->trapToDb=true; // Always put errors in DB for the use can see |
|
820 | + } |
|
821 | + else |
|
822 | + { |
|
823 | + $this->trapData['status']='done'; |
|
824 | + } |
|
825 | + } |
|
826 | 826 | |
827 | - /** Add Time a action to rule |
|
828 | - * @param string $time : time to process to insert in SQL |
|
829 | - */ |
|
830 | - public function add_rule_final($time) |
|
831 | - { |
|
832 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
833 | - if ($this->trapAction==null) |
|
834 | - { |
|
835 | - $this->trapAction='No action'; |
|
836 | - } |
|
837 | - $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
838 | - if ($db_conn->query($sql) === false) { |
|
839 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
840 | - } |
|
841 | - } |
|
827 | + /** Add Time a action to rule |
|
828 | + * @param string $time : time to process to insert in SQL |
|
829 | + */ |
|
830 | + public function add_rule_final($time) |
|
831 | + { |
|
832 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
833 | + if ($this->trapAction==null) |
|
834 | + { |
|
835 | + $this->trapAction='No action'; |
|
836 | + } |
|
837 | + $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
838 | + if ($db_conn->query($sql) === false) { |
|
839 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
840 | + } |
|
841 | + } |
|
842 | 842 | |
843 | - /*********** UTILITIES *********************/ |
|
843 | + /*********** UTILITIES *********************/ |
|
844 | 844 | |
845 | - /** reset service to OK after time defined in rule |
|
846 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
847 | - * @return integer : not in use |
|
848 | - **/ |
|
849 | - public function reset_services() |
|
850 | - { |
|
851 | - // Get all services not in 'ok' state |
|
852 | - $sql_query="SELECT s.service_object_id, |
|
845 | + /** reset service to OK after time defined in rule |
|
846 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
847 | + * @return integer : not in use |
|
848 | + **/ |
|
849 | + public function reset_services() |
|
850 | + { |
|
851 | + // Get all services not in 'ok' state |
|
852 | + $sql_query="SELECT s.service_object_id, |
|
853 | 853 | UNIX_TIMESTAMP(s.last_check) AS last_check, |
854 | 854 | s.current_state as state, |
855 | 855 | v.name1 as host_name, |
@@ -857,43 +857,43 @@ discard block |
||
857 | 857 | FROM icinga_servicestatus AS s |
858 | 858 | LEFT JOIN icinga_objects as v ON s.service_object_id=v.object_id |
859 | 859 | WHERE s.current_state != 0;"; |
860 | - $db_conn=$this->trapsDB->db_connect_ido(); |
|
861 | - if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
862 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
863 | - return 0; |
|
864 | - } |
|
865 | - $services=$services_db->fetchAll(); |
|
866 | - |
|
867 | - // Get all rules |
|
868 | - $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
869 | - $db_conn2=$this->trapsDB->db_connect_trap(); |
|
870 | - if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
871 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
872 | - return 0; |
|
873 | - } |
|
874 | - $rules=$rules_db->fetchAll(); |
|
875 | - |
|
876 | - $now=date('U'); |
|
877 | - |
|
878 | - $numreset=0; |
|
879 | - foreach ($rules as $rule) |
|
880 | - { |
|
881 | - foreach ($services as $service) |
|
882 | - { |
|
883 | - if ($service['service_name'] == $rule['service_name'] && |
|
884 | - $service['host_name'] == $rule['host_name'] && |
|
885 | - ($service['last_check'] + $rule['revert_ok']) < $now) |
|
886 | - { |
|
887 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
888 | - $numreset++; |
|
889 | - } |
|
890 | - } |
|
891 | - } |
|
892 | - echo "\n"; |
|
893 | - echo $numreset . " service(s) reset to OK\n"; |
|
894 | - return 0; |
|
895 | - |
|
896 | - } |
|
860 | + $db_conn=$this->trapsDB->db_connect_ido(); |
|
861 | + if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
862 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
863 | + return 0; |
|
864 | + } |
|
865 | + $services=$services_db->fetchAll(); |
|
866 | + |
|
867 | + // Get all rules |
|
868 | + $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
869 | + $db_conn2=$this->trapsDB->db_connect_trap(); |
|
870 | + if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
871 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
872 | + return 0; |
|
873 | + } |
|
874 | + $rules=$rules_db->fetchAll(); |
|
875 | + |
|
876 | + $now=date('U'); |
|
877 | + |
|
878 | + $numreset=0; |
|
879 | + foreach ($rules as $rule) |
|
880 | + { |
|
881 | + foreach ($services as $service) |
|
882 | + { |
|
883 | + if ($service['service_name'] == $rule['service_name'] && |
|
884 | + $service['host_name'] == $rule['host_name'] && |
|
885 | + ($service['last_check'] + $rule['revert_ok']) < $now) |
|
886 | + { |
|
887 | + $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
888 | + $numreset++; |
|
889 | + } |
|
890 | + } |
|
891 | + } |
|
892 | + echo "\n"; |
|
893 | + echo $numreset . " service(s) reset to OK\n"; |
|
894 | + return 0; |
|
895 | + |
|
896 | + } |
|
897 | 897 | |
898 | 898 | |
899 | 899 | } |
900 | 900 | \ No newline at end of file |
@@ -52,13 +52,13 @@ discard block |
||
52 | 52 | |
53 | 53 | // Logs |
54 | 54 | /** @var Logging Logging class. */ |
55 | - public $logging; //< Logging class. |
|
55 | + public $logging; //< Logging class. |
|
56 | 56 | /** @var bool true if log was setup in constructor */ |
57 | - protected $logSetup; //< bool true if log was setup in constructor |
|
57 | + protected $logSetup; //< bool true if log was setup in constructor |
|
58 | 58 | |
59 | 59 | // Databases |
60 | 60 | /** @var Database $trapsDB Database class*/ |
61 | - public $trapsDB = null; |
|
61 | + public $trapsDB=null; |
|
62 | 62 | |
63 | 63 | // Trap received data |
64 | 64 | protected $receivingHost; |
@@ -74,18 +74,18 @@ discard block |
||
74 | 74 | protected $trapToDb=true; |
75 | 75 | |
76 | 76 | /** @var Mib mib class */ |
77 | - public $mibClass = null; |
|
77 | + public $mibClass=null; |
|
78 | 78 | |
79 | 79 | /** @var Rule rule class */ |
80 | - public $ruleClass = null; |
|
80 | + public $ruleClass=null; |
|
81 | 81 | |
82 | 82 | /** @var Plugins plugins manager **/ |
83 | - public $pluginClass = null; |
|
83 | + public $pluginClass=null; |
|
84 | 84 | |
85 | 85 | /** @var TrapApi $trapApiClass */ |
86 | - public $trapApiClass = null; |
|
86 | + public $trapApiClass=null; |
|
87 | 87 | |
88 | - function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
88 | + function __construct($etcDir='/etc/icingaweb2', $baseLogLevel=null, $baseLogMode='syslog', $baseLogFile='') |
|
89 | 89 | { |
90 | 90 | // Paths of ini files |
91 | 91 | $this->icingaweb2Etc=$etcDir; |
@@ -93,10 +93,10 @@ discard block |
||
93 | 93 | $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
94 | 94 | |
95 | 95 | //************* Setup logging |
96 | - $this->logging = new Logging(); |
|
96 | + $this->logging=new Logging(); |
|
97 | 97 | if ($baseLogLevel != null) |
98 | 98 | { |
99 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
99 | + $this->logging->setLogging($baseLogLevel, $baseLogMode, $baseLogFile); |
|
100 | 100 | $this->logSetup=true; |
101 | 101 | } |
102 | 102 | else |
@@ -108,17 +108,17 @@ discard block |
||
108 | 108 | |
109 | 109 | // Create distributed API object |
110 | 110 | |
111 | - $this->trapApiClass = new TrapApi($this->logging); |
|
111 | + $this->trapApiClass=new TrapApi($this->logging); |
|
112 | 112 | |
113 | 113 | //*************** Get options from ini files |
114 | - if (! is_file($this->trapModuleConfig)) |
|
114 | + if (!is_file($this->trapModuleConfig)) |
|
115 | 115 | { |
116 | 116 | throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
117 | 117 | } |
118 | - $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
118 | + $trapConfig=parse_ini_file($this->trapModuleConfig, true); |
|
119 | 119 | if ($trapConfig == false) |
120 | 120 | { |
121 | - $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
121 | + $this->logging->log("Error reading ini file : ".$this->trapModuleConfig, ERROR, 'syslog'); |
|
122 | 122 | throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
123 | 123 | } |
124 | 124 | $this->getMainOptions($trapConfig); // Get main options from ini file |
@@ -132,10 +132,10 @@ discard block |
||
132 | 132 | if ($this->apiUse === true) $this->getAPI(); // Setup API |
133 | 133 | |
134 | 134 | //*************** Setup MIB |
135 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
135 | + $this->mibClass=new Mib($this->logging, $this->trapsDB, $this->snmptranslate, $this->snmptranslate_dirs); // Create Mib class |
|
136 | 136 | |
137 | 137 | //*************** Setup Rule |
138 | - $this->ruleClass = new Rule($this); //< Create Rule class |
|
138 | + $this->ruleClass=new Rule($this); //< Create Rule class |
|
139 | 139 | |
140 | 140 | $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
141 | 141 | 'source_ip' => 'unknown', |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | |
148 | 148 | //*************** Setup Plugins |
149 | 149 | //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
150 | - $this->pluginClass = new Plugins($this); |
|
150 | + $this->pluginClass=new Plugins($this); |
|
151 | 151 | |
152 | 152 | |
153 | 153 | } |
@@ -176,15 +176,15 @@ discard block |
||
176 | 176 | * @param string $destination file/syslog/display |
177 | 177 | * @return void |
178 | 178 | **/ |
179 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
179 | + public function trapLog($message, $level, $destination='') // OBSOLETE |
|
180 | 180 | { |
181 | 181 | // TODO : replace ref with $this->logging->log |
182 | 182 | $this->logging->log($message, $level, $destination); |
183 | 183 | } |
184 | 184 | |
185 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
185 | + public function setLogging($debugLvl, $outputType, $outputOption=null) // OBSOLETE |
|
186 | 186 | { |
187 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
187 | + $this->logging->setLogging($debugLvl, $outputType, $outputOption); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | /** |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | { |
196 | 196 | if ($this->icinga2api == null) |
197 | 197 | { |
198 | - $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
198 | + $this->icinga2api=new Icinga2API($this->apiHostname, $this->apiPort); |
|
199 | 199 | } |
200 | 200 | return $this->icinga2api; |
201 | 201 | } |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | if ($input_stream === false) |
215 | 215 | { |
216 | 216 | $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
217 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
217 | + $this->logging->log("Error reading stdin !", ERROR, ''); |
|
218 | 218 | return null; // note : exception thrown by logging |
219 | 219 | } |
220 | 220 | |
@@ -223,21 +223,21 @@ discard block |
||
223 | 223 | if ($this->receivingHost === false) |
224 | 224 | { |
225 | 225 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
226 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
226 | + $this->logging->log("Error reading Host !", ERROR, ''); |
|
227 | 227 | } |
228 | 228 | // line 2 IP:port=>IP:port |
229 | 229 | $IP=chop(fgets($input_stream)); |
230 | 230 | if ($IP === false) |
231 | 231 | { |
232 | 232 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
233 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
233 | + $this->logging->log("Error reading IP !", ERROR, ''); |
|
234 | 234 | } |
235 | 235 | $matches=array(); |
236 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
237 | - if ($ret_code===0 || $ret_code===false) |
|
236 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches); |
|
237 | + if ($ret_code === 0 || $ret_code === false) |
|
238 | 238 | { |
239 | 239 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
240 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
240 | + $this->logging->log('Error parsing IP : '.$IP, ERROR, ''); |
|
241 | 241 | } |
242 | 242 | else |
243 | 243 | { |
@@ -247,34 +247,34 @@ discard block |
||
247 | 247 | $this->trapData['destination_port']=$matches[4]; |
248 | 248 | } |
249 | 249 | |
250 | - while (($vars=fgets($input_stream)) !==false) |
|
250 | + while (($vars=fgets($input_stream)) !== false) |
|
251 | 251 | { |
252 | 252 | $vars=chop($vars); |
253 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
254 | - if ($ret_code===0 || $ret_code===false) |
|
253 | + $ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches); |
|
254 | + if ($ret_code === 0 || $ret_code === false) |
|
255 | 255 | { |
256 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
256 | + $this->logging->log('No match on trap data : '.$vars, WARN, ''); |
|
257 | 257 | } |
258 | 258 | else |
259 | 259 | { |
260 | - if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
260 | + if (($matches[1] == '.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1] == '.1.3.6.1.6.3.1.1.4.1')) |
|
261 | 261 | { |
262 | 262 | $this->trapData['trap_oid']=$matches[2]; |
263 | 263 | } |
264 | 264 | else |
265 | 265 | { |
266 | - $object= new stdClass; |
|
267 | - $object->oid =$matches[1]; |
|
268 | - $object->value = $matches[2]; |
|
269 | - array_push($this->trapDataExt,$object); |
|
266 | + $object=new stdClass; |
|
267 | + $object->oid=$matches[1]; |
|
268 | + $object->value=$matches[2]; |
|
269 | + array_push($this->trapDataExt, $object); |
|
270 | 270 | } |
271 | 271 | } |
272 | 272 | } |
273 | 273 | |
274 | - if ($this->trapData['trap_oid']=='unknown') |
|
274 | + if ($this->trapData['trap_oid'] == 'unknown') |
|
275 | 275 | { |
276 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
277 | - $this->logging->log('no trap oid found',ERROR,''); |
|
276 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trapData['source_ip']); |
|
277 | + $this->logging->log('no trap oid found', ERROR, ''); |
|
278 | 278 | } |
279 | 279 | |
280 | 280 | // Translate oids. |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | } |
297 | 297 | |
298 | 298 | |
299 | - $this->trapData['status']= 'waiting'; |
|
299 | + $this->trapData['status']='waiting'; |
|
300 | 300 | |
301 | 301 | return $this->trapData; |
302 | 302 | } |
@@ -312,40 +312,40 @@ discard block |
||
312 | 312 | $db_conn=$this->trapsDB->db_connect_trap(); |
313 | 313 | |
314 | 314 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
315 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
315 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
316 | 316 | if (($ret_code=$db_conn->query($sql)) === false) { |
317 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
317 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
318 | 318 | } |
319 | 319 | $name=$ret_code->fetch(); |
320 | 320 | if ($name['name'] != null) |
321 | 321 | { |
322 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
322 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | // Also check if it is an instance of OID |
326 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
326 | + $oid_instance=preg_replace('/\.[0-9]+$/', '', $oid); |
|
327 | 327 | |
328 | 328 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
329 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
329 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
330 | 330 | if (($ret_code=$db_conn->query($sql)) === false) { |
331 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
331 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
332 | 332 | } |
333 | 333 | $name=$ret_code->fetch(); |
334 | 334 | if ($name['name'] != null) |
335 | 335 | { |
336 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
336 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
337 | 337 | } |
338 | 338 | |
339 | 339 | // Try to get oid name from snmptranslate |
340 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
340 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
341 | 341 | ' '.$oid); |
342 | 342 | $matches=array(); |
343 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
344 | - if ($ret_code===0 || $ret_code === false) { |
|
343 | + $ret_code=preg_match('/(.*)::(.*)/', $translate, $matches); |
|
344 | + if ($ret_code === 0 || $ret_code === false) { |
|
345 | 345 | return NULL; |
346 | 346 | } else { |
347 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
348 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
347 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO); |
|
348 | + return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]); |
|
349 | 349 | } |
350 | 350 | } |
351 | 351 | |
@@ -356,90 +356,90 @@ discard block |
||
356 | 356 | **/ |
357 | 357 | public function eraseOldTraps($days=0) |
358 | 358 | { |
359 | - if ($days==0) |
|
359 | + if ($days == 0) |
|
360 | 360 | { |
361 | 361 | if (($days=$this->getDBConfig('db_remove_days')) == null) |
362 | 362 | { |
363 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
363 | + $this->logging->log('No days specified & no db value : no tap erase', WARN, ''); |
|
364 | 364 | return; |
365 | 365 | } |
366 | 366 | } |
367 | 367 | $db_conn=$this->trapsDB->db_connect_trap(); |
368 | - $daysago = strtotime("-".$days." day"); |
|
369 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
368 | + $daysago=strtotime("-".$days." day"); |
|
369 | + $sql='delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';'; |
|
370 | 370 | if ($db_conn->query($sql) === false) { |
371 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
371 | + $this->logging->log('Error erasing traps : '.$sql, ERROR, ''); |
|
372 | 372 | } |
373 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
373 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO); |
|
374 | 374 | } |
375 | 375 | |
376 | 376 | /** Write error to received trap database |
377 | 377 | */ |
378 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
378 | + public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null) |
|
379 | 379 | { |
380 | 380 | |
381 | 381 | $db_conn=$this->trapsDB->db_connect_trap(); |
382 | 382 | |
383 | 383 | // add date time |
384 | - $insert_col ='date_received,status'; |
|
385 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
384 | + $insert_col='date_received,status'; |
|
385 | + $insert_val="'".date("Y-m-d H:i:s")."','error'"; |
|
386 | 386 | |
387 | - if ($sourceIP !=null) |
|
387 | + if ($sourceIP != null) |
|
388 | 388 | { |
389 | - $insert_col .=',source_ip'; |
|
390 | - $insert_val .=",'". $sourceIP ."'"; |
|
389 | + $insert_col.=',source_ip'; |
|
390 | + $insert_val.=",'".$sourceIP."'"; |
|
391 | 391 | } |
392 | - if ($trapoid !=null) |
|
392 | + if ($trapoid != null) |
|
393 | 393 | { |
394 | - $insert_col .=',trap_oid'; |
|
395 | - $insert_val .=",'". $trapoid ."'"; |
|
394 | + $insert_col.=',trap_oid'; |
|
395 | + $insert_val.=",'".$trapoid."'"; |
|
396 | 396 | } |
397 | - $insert_col .=',status_detail'; |
|
398 | - $insert_val .=",'". $message ."'"; |
|
397 | + $insert_col.=',status_detail'; |
|
398 | + $insert_val.=",'".$message."'"; |
|
399 | 399 | |
400 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
400 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
401 | 401 | |
402 | 402 | switch ($this->trapsDB->trapDBType) |
403 | 403 | { |
404 | 404 | case 'pgsql': |
405 | - $sql .= ' RETURNING id;'; |
|
406 | - $this->logging->log('sql : '.$sql,INFO); |
|
405 | + $sql.=' RETURNING id;'; |
|
406 | + $this->logging->log('sql : '.$sql, INFO); |
|
407 | 407 | if (($ret_code=$db_conn->query($sql)) === false) { |
408 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
408 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
409 | 409 | } |
410 | - $this->logging->log('SQL insertion OK',INFO ); |
|
410 | + $this->logging->log('SQL insertion OK', INFO); |
|
411 | 411 | // Get last id to insert oid/values in secondary table |
412 | 412 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
413 | 413 | |
414 | - $this->logging->log('Erreur recuperation id',1,''); |
|
414 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
415 | 415 | } |
416 | - if (! isset($inserted_id_ret['id'])) { |
|
417 | - $this->logging->log('Error getting id',1,''); |
|
416 | + if (!isset($inserted_id_ret['id'])) { |
|
417 | + $this->logging->log('Error getting id', 1, ''); |
|
418 | 418 | } |
419 | 419 | $this->trapId=$inserted_id_ret['id']; |
420 | 420 | break; |
421 | 421 | case 'mysql': |
422 | - $sql .= ';'; |
|
423 | - $this->logging->log('sql : '.$sql,INFO ); |
|
422 | + $sql.=';'; |
|
423 | + $this->logging->log('sql : '.$sql, INFO); |
|
424 | 424 | if ($db_conn->query($sql) === false) { |
425 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
425 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
426 | 426 | } |
427 | - $this->logging->log('SQL insertion OK',INFO ); |
|
427 | + $this->logging->log('SQL insertion OK', INFO); |
|
428 | 428 | // Get last id to insert oid/values in secondary table |
429 | 429 | $sql='SELECT LAST_INSERT_ID();'; |
430 | 430 | if (($ret_code=$db_conn->query($sql)) === false) { |
431 | - $this->logging->log('Erreur recuperation id',1,''); |
|
431 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
432 | 432 | } |
433 | 433 | |
434 | 434 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
435 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
435 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
436 | 436 | $this->trapId=$inserted_id; |
437 | 437 | break; |
438 | 438 | default: |
439 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
439 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
440 | 440 | } |
441 | 441 | |
442 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
442 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
443 | 443 | } |
444 | 444 | |
445 | 445 | /** Write trap data to trap database |
@@ -456,86 +456,86 @@ discard block |
||
456 | 456 | $insert_col=''; |
457 | 457 | $insert_val=''; |
458 | 458 | // add date time |
459 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
459 | + $this->trapData['date_received']=date("Y-m-d H:i:s"); |
|
460 | 460 | |
461 | 461 | $firstcol=1; |
462 | 462 | foreach ($this->trapData as $col => $val) |
463 | 463 | { |
464 | - if ($firstcol==0) |
|
464 | + if ($firstcol == 0) |
|
465 | 465 | { |
466 | - $insert_col .=','; |
|
467 | - $insert_val .=','; |
|
466 | + $insert_col.=','; |
|
467 | + $insert_val.=','; |
|
468 | 468 | } |
469 | - $insert_col .= $col ; |
|
470 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
469 | + $insert_col.=$col; |
|
470 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
471 | 471 | $firstcol=0; |
472 | 472 | } |
473 | 473 | |
474 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
474 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
475 | 475 | switch ($this->trapsDB->trapDBType) |
476 | 476 | { |
477 | 477 | case 'pgsql': |
478 | - $sql .= ' RETURNING id;'; |
|
479 | - $this->logging->log('sql : '.$sql,INFO ); |
|
478 | + $sql.=' RETURNING id;'; |
|
479 | + $this->logging->log('sql : '.$sql, INFO); |
|
480 | 480 | if (($ret_code=$db_conn->query($sql)) === false) { |
481 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
481 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
482 | 482 | } |
483 | - $this->logging->log('SQL insertion OK',INFO ); |
|
483 | + $this->logging->log('SQL insertion OK', INFO); |
|
484 | 484 | // Get last id to insert oid/values in secondary table |
485 | 485 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
486 | 486 | |
487 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
487 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
488 | 488 | } |
489 | - if (! isset($inserted_id_ret['id'])) { |
|
490 | - $this->logging->log('Error getting id',ERROR,''); |
|
489 | + if (!isset($inserted_id_ret['id'])) { |
|
490 | + $this->logging->log('Error getting id', ERROR, ''); |
|
491 | 491 | } |
492 | 492 | $this->trapId=$inserted_id_ret['id']; |
493 | 493 | break; |
494 | 494 | case 'mysql': |
495 | - $sql .= ';'; |
|
496 | - $this->logging->log('sql : '.$sql,INFO ); |
|
495 | + $sql.=';'; |
|
496 | + $this->logging->log('sql : '.$sql, INFO); |
|
497 | 497 | if ($db_conn->query($sql) === false) { |
498 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
498 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
499 | 499 | } |
500 | - $this->logging->log('SQL insertion OK',INFO ); |
|
500 | + $this->logging->log('SQL insertion OK', INFO); |
|
501 | 501 | // Get last id to insert oid/values in secondary table |
502 | 502 | $sql='SELECT LAST_INSERT_ID();'; |
503 | 503 | if (($ret_code=$db_conn->query($sql)) === false) { |
504 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
504 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
505 | 505 | } |
506 | 506 | |
507 | 507 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
508 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
508 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
509 | 509 | $this->trapId=$inserted_id; |
510 | 510 | break; |
511 | 511 | default: |
512 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
512 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, ''); |
|
513 | 513 | } |
514 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
514 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
515 | 515 | |
516 | 516 | // Fill trap extended data table |
517 | 517 | foreach ($this->trapDataExt as $value) { |
518 | 518 | // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
519 | 519 | $firstcol=1; |
520 | - $value->trap_id = $this->trapId; |
|
520 | + $value->trap_id=$this->trapId; |
|
521 | 521 | $insert_col=''; |
522 | 522 | $insert_val=''; |
523 | 523 | foreach ($value as $col => $val) |
524 | 524 | { |
525 | - if ($firstcol==0) |
|
525 | + if ($firstcol == 0) |
|
526 | 526 | { |
527 | - $insert_col .=','; |
|
528 | - $insert_val .=','; |
|
527 | + $insert_col.=','; |
|
528 | + $insert_val.=','; |
|
529 | 529 | } |
530 | - $insert_col .= $col; |
|
531 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
530 | + $insert_col.=$col; |
|
531 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
532 | 532 | $firstcol=0; |
533 | 533 | } |
534 | 534 | |
535 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
535 | + $sql='INSERT INTO '.$this->dbPrefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');'; |
|
536 | 536 | |
537 | 537 | if ($db_conn->query($sql) === false) { |
538 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
538 | + $this->logging->log('Erreur insertion data : '.$sql, WARN, ''); |
|
539 | 539 | } |
540 | 540 | } |
541 | 541 | } |
@@ -545,14 +545,14 @@ discard block |
||
545 | 545 | * @param $oid string oid in numeric |
546 | 546 | * @return mixed|boolean : PDO object or false |
547 | 547 | */ |
548 | - protected function getRules($ip,$oid) |
|
548 | + protected function getRules($ip, $oid) |
|
549 | 549 | { |
550 | 550 | $db_conn=$this->trapsDB->db_connect_trap(); |
551 | 551 | // fetch rules based on IP in rule and OID |
552 | 552 | $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
553 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
553 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
554 | 554 | if (($ret_code=$db_conn->query($sql)) === false) { |
555 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
555 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
556 | 556 | return false; |
557 | 557 | } |
558 | 558 | $rules_all=$ret_code->fetchAll(); |
@@ -561,7 +561,7 @@ discard block |
||
561 | 561 | $rule_ret_key=0; |
562 | 562 | foreach ($rules_all as $key => $rule) |
563 | 563 | { |
564 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
564 | + if ($rule['ip4'] == $ip || $rule['ip6'] == $ip) |
|
565 | 565 | { |
566 | 566 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
567 | 567 | //TODO : get host name by API (and check if correct in rule). |
@@ -569,7 +569,7 @@ discard block |
||
569 | 569 | continue; |
570 | 570 | } |
571 | 571 | // TODO : get hosts IP by API |
572 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
572 | + if (isset($rule['host_group_name']) && $rule['host_group_name'] != null) |
|
573 | 573 | { // get ips of group members by oid |
574 | 574 | $db_conn2=$this->trapsDB->db_connect_ido(); |
575 | 575 | $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
@@ -580,15 +580,15 @@ discard block |
||
580 | 580 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
581 | 581 | WHERE o.name1='".$rule['host_group_name']."';"; |
582 | 582 | if (($ret_code2=$db_conn2->query($sql)) === false) { |
583 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
583 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
584 | 584 | continue; |
585 | 585 | } |
586 | 586 | $grouphosts=$ret_code2->fetchAll(); |
587 | 587 | //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
588 | - foreach ( $grouphosts as $host) |
|
588 | + foreach ($grouphosts as $host) |
|
589 | 589 | { |
590 | 590 | //echo $host['ip4']."\n"; |
591 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
591 | + if ($host['ip4'] == $ip || $host['ip6'] == $ip) |
|
592 | 592 | { |
593 | 593 | //echo "Rule added \n"; |
594 | 594 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
@@ -611,7 +611,7 @@ discard block |
||
611 | 611 | $db_conn=$this->trapsDB->db_connect_trap(); |
612 | 612 | $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
613 | 613 | if ($db_conn->query($sql) === false) { |
614 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
614 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
615 | 615 | } |
616 | 616 | } |
617 | 617 | |
@@ -623,23 +623,23 @@ discard block |
||
623 | 623 | * @param string $display |
624 | 624 | * @returnn bool true is service check was sent without error |
625 | 625 | */ |
626 | - public function serviceCheckResult($host,$service,$state,$display) |
|
626 | + public function serviceCheckResult($host, $service, $state, $display) |
|
627 | 627 | { |
628 | 628 | if ($this->apiUse === false) |
629 | 629 | { |
630 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
631 | - $host.';' .$service .';' . $state . ';'.$display; |
|
632 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
630 | + $send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'. |
|
631 | + $host.';'.$service.';'.$state.';'.$display; |
|
632 | + $this->logging->log($send." : to : ".$this->icinga2cmd, INFO); |
|
633 | 633 | |
634 | 634 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
635 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
635 | + exec('echo "'.$send.'" > '.$this->icinga2cmd); |
|
636 | 636 | return true; |
637 | 637 | } |
638 | 638 | else |
639 | 639 | { |
640 | 640 | // Get perfdata if found |
641 | 641 | $matches=array(); |
642 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
642 | + if (preg_match('/(.*)\|(.*)/', $display, $matches) == 1) |
|
643 | 643 | { |
644 | 644 | $display=$matches[1]; |
645 | 645 | $perfdata=$matches[2]; |
@@ -649,17 +649,17 @@ discard block |
||
649 | 649 | $perfdata=''; |
650 | 650 | } |
651 | 651 | |
652 | - $api = $this->getAPI(); |
|
652 | + $api=$this->getAPI(); |
|
653 | 653 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
654 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
654 | + list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display, $perfdata); |
|
655 | 655 | if ($retcode == false) |
656 | 656 | { |
657 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
657 | + $this->logging->log("Error sending result : ".$retmessage, WARN, ''); |
|
658 | 658 | return false; |
659 | 659 | } |
660 | 660 | else |
661 | 661 | { |
662 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
662 | + $this->logging->log("Sent result : ".$retmessage, INFO); |
|
663 | 663 | return true; |
664 | 664 | } |
665 | 665 | } |
@@ -667,7 +667,7 @@ discard block |
||
667 | 667 | |
668 | 668 | public function getHostByIP($ip) |
669 | 669 | { |
670 | - $api = $this->getAPI(); |
|
670 | + $api=$this->getAPI(); |
|
671 | 671 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
672 | 672 | return $api->getHostByIP($ip); |
673 | 673 | } |
@@ -680,35 +680,35 @@ discard block |
||
680 | 680 | protected function applyDisplay($display) |
681 | 681 | { |
682 | 682 | $matches=array(); |
683 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
683 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $display, $matches) == 1) |
|
684 | 684 | { |
685 | 685 | $oid=$matches[1]; |
686 | 686 | $found=0; |
687 | 687 | // Test and transform regexp |
688 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
688 | + $oidR=$this->ruleClass->regexp_eval($oid); |
|
689 | 689 | |
690 | - foreach($this->trapDataExt as $val) |
|
690 | + foreach ($this->trapDataExt as $val) |
|
691 | 691 | { |
692 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
692 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
693 | 693 | { |
694 | - $val->value=preg_replace('/"/','',$val->value); |
|
694 | + $val->value=preg_replace('/"/', '', $val->value); |
|
695 | 695 | $rep=0; |
696 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
697 | - if ($rep==0) |
|
696 | + $display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep); |
|
697 | + if ($rep == 0) |
|
698 | 698 | { |
699 | - $this->logging->log("Error in display",WARN,''); |
|
699 | + $this->logging->log("Error in display", WARN, ''); |
|
700 | 700 | return $display; |
701 | 701 | } |
702 | 702 | $found=1; |
703 | 703 | break; |
704 | 704 | } |
705 | 705 | } |
706 | - if ($found==0) |
|
706 | + if ($found == 0) |
|
707 | 707 | { |
708 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
709 | - if ($rep==0) |
|
708 | + $display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep); |
|
709 | + if ($rep == 0) |
|
710 | 710 | { |
711 | - $this->logging->log("Error in display",WARN,''); |
|
711 | + $this->logging->log("Error in display", WARN, ''); |
|
712 | 712 | return $display; |
713 | 713 | } |
714 | 714 | } |
@@ -720,11 +720,11 @@ discard block |
||
720 | 720 | */ |
721 | 721 | public function applyRules() |
722 | 722 | { |
723 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
723 | + $rules=$this->getRules($this->trapData['source_ip'], $this->trapData['trap_oid']); |
|
724 | 724 | |
725 | - if ($rules===false || count($rules)==0) |
|
725 | + if ($rules === false || count($rules) == 0) |
|
726 | 726 | { |
727 | - $this->logging->log('No rules found for this trap',INFO ); |
|
727 | + $this->logging->log('No rules found for this trap', INFO); |
|
728 | 728 | $this->trapData['status']='unknown'; |
729 | 729 | $this->trapToDb=true; |
730 | 730 | return; |
@@ -739,59 +739,59 @@ discard block |
||
739 | 739 | $service_name=$rule['service_name']; |
740 | 740 | |
741 | 741 | $display=$this->applyDisplay($rule['display']); |
742 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
742 | + $this->trapAction=($this->trapAction == null) ? '' : $this->trapAction.', '; |
|
743 | 743 | try |
744 | 744 | { |
745 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
746 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
745 | + $this->logging->log('Rule to eval : '.$rule['rule'], INFO); |
|
746 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt); |
|
747 | 747 | //->eval_rule($rule['rule']); |
748 | 748 | |
749 | 749 | if ($evalr == true) |
750 | 750 | { |
751 | 751 | //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
752 | 752 | $action=$rule['action_match']; |
753 | - $this->logging->log('action OK : '.$action,INFO ); |
|
753 | + $this->logging->log('action OK : '.$action, INFO); |
|
754 | 754 | if ($action >= 0) |
755 | 755 | { |
756 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
756 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
757 | 757 | { |
758 | 758 | $this->trapAction.='Error sending status : check cmd/API'; |
759 | 759 | } |
760 | 760 | else |
761 | 761 | { |
762 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
762 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
763 | 763 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
764 | 764 | } |
765 | 765 | } |
766 | 766 | else |
767 | 767 | { |
768 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
768 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
769 | 769 | } |
770 | - $this->trapToDb=($action==-2)?false:true; |
|
770 | + $this->trapToDb=($action == -2) ?false:true; |
|
771 | 771 | } |
772 | 772 | else |
773 | 773 | { |
774 | 774 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
775 | 775 | |
776 | 776 | $action=$rule['action_nomatch']; |
777 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
777 | + $this->logging->log('action NOK : '.$action, INFO); |
|
778 | 778 | if ($action >= 0) |
779 | 779 | { |
780 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
780 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
781 | 781 | { |
782 | 782 | $this->trapAction.='Error sending status : check cmd/API'; |
783 | 783 | } |
784 | 784 | else |
785 | 785 | { |
786 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
786 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
787 | 787 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
788 | 788 | } |
789 | 789 | } |
790 | 790 | else |
791 | 791 | { |
792 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
792 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
793 | 793 | } |
794 | - $this->trapToDb=($action==-2)?false:true; |
|
794 | + $this->trapToDb=($action == -2) ?false:true; |
|
795 | 795 | } |
796 | 796 | // Put name in source_name |
797 | 797 | if (!isset($this->trapData['source_name'])) |
@@ -800,7 +800,7 @@ discard block |
||
800 | 800 | } |
801 | 801 | else |
802 | 802 | { |
803 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
803 | + if (!preg_match('/'.$rule['host_name'].'/', $this->trapData['source_name'])) |
|
804 | 804 | { // only add if not present |
805 | 805 | $this->trapData['source_name'].=','.$rule['host_name']; |
806 | 806 | } |
@@ -808,13 +808,13 @@ discard block |
||
808 | 808 | } |
809 | 809 | catch (Exception $e) |
810 | 810 | { |
811 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
811 | + $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, ''); |
|
812 | 812 | $this->trapAction.=' ERR : '.$e->getMessage(); |
813 | 813 | $this->trapData['status']='error'; |
814 | 814 | } |
815 | 815 | |
816 | 816 | } |
817 | - if ($this->trapData['status']=='error') |
|
817 | + if ($this->trapData['status'] == 'error') |
|
818 | 818 | { |
819 | 819 | $this->trapToDb=true; // Always put errors in DB for the use can see |
820 | 820 | } |
@@ -830,13 +830,13 @@ discard block |
||
830 | 830 | public function add_rule_final($time) |
831 | 831 | { |
832 | 832 | $db_conn=$this->trapsDB->db_connect_trap(); |
833 | - if ($this->trapAction==null) |
|
833 | + if ($this->trapAction == null) |
|
834 | 834 | { |
835 | 835 | $this->trapAction='No action'; |
836 | 836 | } |
837 | 837 | $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
838 | 838 | if ($db_conn->query($sql) === false) { |
839 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
839 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
840 | 840 | } |
841 | 841 | } |
842 | 842 | |
@@ -859,7 +859,7 @@ discard block |
||
859 | 859 | WHERE s.current_state != 0;"; |
860 | 860 | $db_conn=$this->trapsDB->db_connect_ido(); |
861 | 861 | if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
862 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
862 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
863 | 863 | return 0; |
864 | 864 | } |
865 | 865 | $services=$services_db->fetchAll(); |
@@ -868,7 +868,7 @@ discard block |
||
868 | 868 | $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
869 | 869 | $db_conn2=$this->trapsDB->db_connect_trap(); |
870 | 870 | if (($rules_db=$db_conn2->query($sql_query)) === false) { |
871 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
871 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
872 | 872 | return 0; |
873 | 873 | } |
874 | 874 | $rules=$rules_db->fetchAll(); |
@@ -884,13 +884,13 @@ discard block |
||
884 | 884 | $service['host_name'] == $rule['host_name'] && |
885 | 885 | ($service['last_check'] + $rule['revert_ok']) < $now) |
886 | 886 | { |
887 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
887 | + $this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
888 | 888 | $numreset++; |
889 | 889 | } |
890 | 890 | } |
891 | 891 | } |
892 | 892 | echo "\n"; |
893 | - echo $numreset . " service(s) reset to OK\n"; |
|
893 | + echo $numreset." service(s) reset to OK\n"; |
|
894 | 894 | return 0; |
895 | 895 | |
896 | 896 | } |
@@ -15,181 +15,181 @@ |
||
15 | 15 | trait TrapConfig |
16 | 16 | { |
17 | 17 | |
18 | - /** @return \Trapdirector\Logging */ |
|
19 | - abstract public function getLogging(); |
|
20 | - /** @return \Trapdirector\TrapApi */ |
|
21 | - abstract public function getTrapApi(); |
|
18 | + /** @return \Trapdirector\Logging */ |
|
19 | + abstract public function getLogging(); |
|
20 | + /** @return \Trapdirector\TrapApi */ |
|
21 | + abstract public function getTrapApi(); |
|
22 | 22 | |
23 | - /** |
|
24 | - * Get option from array of ini file, send message if empty |
|
25 | - * @param string $option_array Array of ini file |
|
26 | - * @param string $option_category category in ini file |
|
27 | - * @param string $option_name name of option in category |
|
28 | - * @param mixed $option_var variable to fill if found, left untouched if not found |
|
29 | - * @param integer $log_level default 2 (warning) |
|
30 | - * @param string $message warning message if not found |
|
31 | - * @return boolean true if found, or false |
|
32 | - */ |
|
33 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = WARN, $message = null) |
|
34 | - { |
|
35 | - if (!isset($option_array[$option_category][$option_name])) |
|
36 | - { |
|
37 | - if ($message === null) |
|
38 | - { |
|
39 | - $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
40 | - } |
|
41 | - $this->getLogging()->log($message,$log_level); |
|
42 | - return false; |
|
43 | - } |
|
44 | - else |
|
45 | - { |
|
46 | - $option_var=$option_array[$option_category][$option_name]; |
|
47 | - return true; |
|
48 | - } |
|
49 | - } |
|
23 | + /** |
|
24 | + * Get option from array of ini file, send message if empty |
|
25 | + * @param string $option_array Array of ini file |
|
26 | + * @param string $option_category category in ini file |
|
27 | + * @param string $option_name name of option in category |
|
28 | + * @param mixed $option_var variable to fill if found, left untouched if not found |
|
29 | + * @param integer $log_level default 2 (warning) |
|
30 | + * @param string $message warning message if not found |
|
31 | + * @return boolean true if found, or false |
|
32 | + */ |
|
33 | + protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = WARN, $message = null) |
|
34 | + { |
|
35 | + if (!isset($option_array[$option_category][$option_name])) |
|
36 | + { |
|
37 | + if ($message === null) |
|
38 | + { |
|
39 | + $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
40 | + } |
|
41 | + $this->getLogging()->log($message,$log_level); |
|
42 | + return false; |
|
43 | + } |
|
44 | + else |
|
45 | + { |
|
46 | + $option_var=$option_array[$option_category][$option_name]; |
|
47 | + return true; |
|
48 | + } |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Get options in database |
|
53 | - */ |
|
54 | - protected function getDatabaseOptions() |
|
55 | - { |
|
56 | - // Database options |
|
57 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
58 | - { |
|
59 | - $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
|
60 | - $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
|
61 | - $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
|
62 | - } |
|
63 | - } |
|
51 | + /** |
|
52 | + * Get options in database |
|
53 | + */ |
|
54 | + protected function getDatabaseOptions() |
|
55 | + { |
|
56 | + // Database options |
|
57 | + if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
58 | + { |
|
59 | + $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
|
60 | + $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
|
61 | + $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
|
62 | + } |
|
63 | + } |
|
64 | 64 | |
65 | - /** Set $variable to value if $element found in database config table |
|
66 | - * @param string $element |
|
67 | - * @param string $variable |
|
68 | - */ |
|
69 | - protected function getDBConfigIfSet($element,&$variable) |
|
70 | - { |
|
71 | - $value=$this->getDBConfig($element); |
|
72 | - if ($value != null) $variable=$value; |
|
73 | - } |
|
65 | + /** Set $variable to value if $element found in database config table |
|
66 | + * @param string $element |
|
67 | + * @param string $variable |
|
68 | + */ |
|
69 | + protected function getDBConfigIfSet($element,&$variable) |
|
70 | + { |
|
71 | + $value=$this->getDBConfig($element); |
|
72 | + if ($value != null) $variable=$value; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Get data from db_config |
|
77 | - * @param $element string name of param |
|
78 | - * @return mixed : value (or null) |
|
79 | - */ |
|
80 | - protected function getDBConfig($element) // TODO : put this in DB class |
|
81 | - { |
|
82 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
83 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
84 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
85 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
86 | - return null; |
|
87 | - } |
|
88 | - $value=$ret_code->fetch(); |
|
89 | - if ($value != null && isset($value['value'])) |
|
90 | - { |
|
91 | - return $value['value']; |
|
92 | - } |
|
93 | - return null; |
|
94 | - } |
|
75 | + /** |
|
76 | + * Get data from db_config |
|
77 | + * @param $element string name of param |
|
78 | + * @return mixed : value (or null) |
|
79 | + */ |
|
80 | + protected function getDBConfig($element) // TODO : put this in DB class |
|
81 | + { |
|
82 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
83 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
84 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
85 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
86 | + return null; |
|
87 | + } |
|
88 | + $value=$ret_code->fetch(); |
|
89 | + if ($value != null && isset($value['value'])) |
|
90 | + { |
|
91 | + return $value['value']; |
|
92 | + } |
|
93 | + return null; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Get options from ini file |
|
98 | - * @param array $trap_config : ini file array |
|
99 | - */ |
|
100 | - protected function getMainOptions($trapConfig) |
|
101 | - { |
|
96 | + /** |
|
97 | + * Get options from ini file |
|
98 | + * @param array $trap_config : ini file array |
|
99 | + */ |
|
100 | + protected function getMainOptions($trapConfig) |
|
101 | + { |
|
102 | 102 | |
103 | - $nodeStatus=''; |
|
104 | - $this->getOptionIfSet($trapConfig,'config','node', $nodeStatus); |
|
105 | - if ($this->getTrapApi()->setStatus($nodeStatus) === FALSE) |
|
106 | - { |
|
107 | - $this->getLogging()->log('Unknown node status '.$nodeStatus.' : setting to MASTER',WARN); |
|
108 | - $this->getTrapApi()->setStatusMaster(); |
|
109 | - } |
|
110 | - else |
|
111 | - { |
|
112 | - if ($this->getTrapApi()->getStatus() != TrapApi::MASTER) |
|
113 | - { |
|
114 | - // Get options to connect to API |
|
115 | - $IP = $port = $user = $pass = null; |
|
116 | - $this->getOptionIfSet($trapConfig,'config','masterIP', $IP, ERROR); |
|
117 | - $this->getOptionIfSet($trapConfig,'config','masterPort', $port, ERROR); |
|
118 | - $this->getOptionIfSet($trapConfig,'config','masterUser', $user, ERROR); |
|
119 | - $this->getOptionIfSet($trapConfig,'config','masterPass', $pass, ERROR); |
|
120 | - $this->getTrapApi()->setParams($IP, $port, $user, $pass); |
|
121 | - return; |
|
122 | - } |
|
123 | - } |
|
103 | + $nodeStatus=''; |
|
104 | + $this->getOptionIfSet($trapConfig,'config','node', $nodeStatus); |
|
105 | + if ($this->getTrapApi()->setStatus($nodeStatus) === FALSE) |
|
106 | + { |
|
107 | + $this->getLogging()->log('Unknown node status '.$nodeStatus.' : setting to MASTER',WARN); |
|
108 | + $this->getTrapApi()->setStatusMaster(); |
|
109 | + } |
|
110 | + else |
|
111 | + { |
|
112 | + if ($this->getTrapApi()->getStatus() != TrapApi::MASTER) |
|
113 | + { |
|
114 | + // Get options to connect to API |
|
115 | + $IP = $port = $user = $pass = null; |
|
116 | + $this->getOptionIfSet($trapConfig,'config','masterIP', $IP, ERROR); |
|
117 | + $this->getOptionIfSet($trapConfig,'config','masterPort', $port, ERROR); |
|
118 | + $this->getOptionIfSet($trapConfig,'config','masterUser', $user, ERROR); |
|
119 | + $this->getOptionIfSet($trapConfig,'config','masterPass', $pass, ERROR); |
|
120 | + $this->getTrapApi()->setParams($IP, $port, $user, $pass); |
|
121 | + return; |
|
122 | + } |
|
123 | + } |
|
124 | 124 | |
125 | - // Snmptranslate binary path |
|
126 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
125 | + // Snmptranslate binary path |
|
126 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
127 | 127 | |
128 | - // mibs path |
|
129 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
128 | + // mibs path |
|
129 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
130 | 130 | |
131 | - // icinga2cmd path |
|
132 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
131 | + // icinga2cmd path |
|
132 | + $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
133 | 133 | |
134 | - // table prefix |
|
135 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
134 | + // table prefix |
|
135 | + $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
136 | 136 | |
137 | - // API options |
|
138 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
139 | - { |
|
140 | - $this->apiUse=true; |
|
141 | - // Get API options or throw exception as not configured correctly |
|
142 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort,ERROR); |
|
143 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername,ERROR); |
|
144 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword,ERROR); |
|
145 | - } |
|
146 | - } |
|
137 | + // API options |
|
138 | + if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
139 | + { |
|
140 | + $this->apiUse=true; |
|
141 | + // Get API options or throw exception as not configured correctly |
|
142 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort,ERROR); |
|
143 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername,ERROR); |
|
144 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword,ERROR); |
|
145 | + } |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Create and setup database class for trap & ido (if no api) db |
|
150 | - * @param array $trap_config : ini file array |
|
151 | - */ |
|
152 | - protected function setupDatabase($trapConfig) |
|
153 | - { |
|
154 | - // Trap database |
|
155 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
156 | - { |
|
157 | - $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
158 | - return; |
|
159 | - } |
|
160 | - $dbTrapName=$trapConfig['config']['database']; |
|
161 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
148 | + /** |
|
149 | + * Create and setup database class for trap & ido (if no api) db |
|
150 | + * @param array $trap_config : ini file array |
|
151 | + */ |
|
152 | + protected function setupDatabase($trapConfig) |
|
153 | + { |
|
154 | + // Trap database |
|
155 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
156 | + { |
|
157 | + $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
158 | + return; |
|
159 | + } |
|
160 | + $dbTrapName=$trapConfig['config']['database']; |
|
161 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
162 | 162 | |
163 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
164 | - { |
|
165 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
166 | - return; |
|
167 | - } |
|
168 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
169 | - { |
|
170 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
171 | - return; |
|
172 | - } |
|
163 | + if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
164 | + { |
|
165 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
166 | + return; |
|
167 | + } |
|
168 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
169 | + { |
|
170 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
171 | + return; |
|
172 | + } |
|
173 | 173 | |
174 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
174 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
175 | 175 | |
176 | - if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
176 | + if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
177 | 177 | |
178 | - // IDO Database |
|
179 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
180 | - { |
|
181 | - $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
182 | - } |
|
183 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
178 | + // IDO Database |
|
179 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
180 | + { |
|
181 | + $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
182 | + } |
|
183 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
184 | 184 | |
185 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
186 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
187 | - { |
|
188 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
189 | - return; |
|
190 | - } |
|
185 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
186 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
187 | + { |
|
188 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
189 | + return; |
|
190 | + } |
|
191 | 191 | |
192 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
193 | - } |
|
192 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
193 | + } |
|
194 | 194 | |
195 | 195 | } |
196 | 196 | \ No newline at end of file |
@@ -30,15 +30,15 @@ discard block |
||
30 | 30 | * @param string $message warning message if not found |
31 | 31 | * @return boolean true if found, or false |
32 | 32 | */ |
33 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = WARN, $message = null) |
|
33 | + protected function getOptionIfSet($option_array, $option_category, $option_name, &$option_var, $log_level=WARN, $message=null) |
|
34 | 34 | { |
35 | 35 | if (!isset($option_array[$option_category][$option_name])) |
36 | 36 | { |
37 | 37 | if ($message === null) |
38 | 38 | { |
39 | - $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
39 | + $message='No '.$option_name.' in config file: '.$this->trapModuleConfig; |
|
40 | 40 | } |
41 | - $this->getLogging()->log($message,$log_level); |
|
41 | + $this->getLogging()->log($message, $log_level); |
|
42 | 42 | return false; |
43 | 43 | } |
44 | 44 | else |
@@ -56,9 +56,9 @@ discard block |
||
56 | 56 | // Database options |
57 | 57 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
58 | 58 | { |
59 | - $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
|
60 | - $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
|
61 | - $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
|
59 | + $this->getDBConfigIfSet('log_level', $this->getLogging()->debugLevel); |
|
60 | + $this->getDBConfigIfSet('log_destination', $this->getLogging()->outputMode); |
|
61 | + $this->getDBConfigIfSet('log_file', $this->getLogging()->outputFile); |
|
62 | 62 | } |
63 | 63 | } |
64 | 64 | |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | * @param string $element |
67 | 67 | * @param string $variable |
68 | 68 | */ |
69 | - protected function getDBConfigIfSet($element,&$variable) |
|
69 | + protected function getDBConfigIfSet($element, &$variable) |
|
70 | 70 | { |
71 | 71 | $value=$this->getDBConfig($element); |
72 | 72 | if ($value != null) $variable=$value; |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $db_conn=$this->trapsDB->db_connect_trap(); |
83 | 83 | $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
84 | 84 | if (($ret_code=$db_conn->query($sql)) === false) { |
85 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
85 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
86 | 86 | return null; |
87 | 87 | } |
88 | 88 | $value=$ret_code->fetch(); |
@@ -101,10 +101,10 @@ discard block |
||
101 | 101 | { |
102 | 102 | |
103 | 103 | $nodeStatus=''; |
104 | - $this->getOptionIfSet($trapConfig,'config','node', $nodeStatus); |
|
104 | + $this->getOptionIfSet($trapConfig, 'config', 'node', $nodeStatus); |
|
105 | 105 | if ($this->getTrapApi()->setStatus($nodeStatus) === FALSE) |
106 | 106 | { |
107 | - $this->getLogging()->log('Unknown node status '.$nodeStatus.' : setting to MASTER',WARN); |
|
107 | + $this->getLogging()->log('Unknown node status '.$nodeStatus.' : setting to MASTER', WARN); |
|
108 | 108 | $this->getTrapApi()->setStatusMaster(); |
109 | 109 | } |
110 | 110 | else |
@@ -112,36 +112,36 @@ discard block |
||
112 | 112 | if ($this->getTrapApi()->getStatus() != TrapApi::MASTER) |
113 | 113 | { |
114 | 114 | // Get options to connect to API |
115 | - $IP = $port = $user = $pass = null; |
|
116 | - $this->getOptionIfSet($trapConfig,'config','masterIP', $IP, ERROR); |
|
117 | - $this->getOptionIfSet($trapConfig,'config','masterPort', $port, ERROR); |
|
118 | - $this->getOptionIfSet($trapConfig,'config','masterUser', $user, ERROR); |
|
119 | - $this->getOptionIfSet($trapConfig,'config','masterPass', $pass, ERROR); |
|
115 | + $IP=$port=$user=$pass=null; |
|
116 | + $this->getOptionIfSet($trapConfig, 'config', 'masterIP', $IP, ERROR); |
|
117 | + $this->getOptionIfSet($trapConfig, 'config', 'masterPort', $port, ERROR); |
|
118 | + $this->getOptionIfSet($trapConfig, 'config', 'masterUser', $user, ERROR); |
|
119 | + $this->getOptionIfSet($trapConfig, 'config', 'masterPass', $pass, ERROR); |
|
120 | 120 | $this->getTrapApi()->setParams($IP, $port, $user, $pass); |
121 | 121 | return; |
122 | 122 | } |
123 | 123 | } |
124 | 124 | |
125 | 125 | // Snmptranslate binary path |
126 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
126 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate', $this->snmptranslate); |
|
127 | 127 | |
128 | 128 | // mibs path |
129 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
129 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate_dirs', $this->snmptranslate_dirs); |
|
130 | 130 | |
131 | 131 | // icinga2cmd path |
132 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
132 | + $this->getOptionIfSet($trapConfig, 'config', 'icingacmd', $this->icinga2cmd); |
|
133 | 133 | |
134 | 134 | // table prefix |
135 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
135 | + $this->getOptionIfSet($trapConfig, 'config', 'database_prefix', $this->dbPrefix); |
|
136 | 136 | |
137 | 137 | // API options |
138 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
138 | + if ($this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_host', $this->apiHostname)) |
|
139 | 139 | { |
140 | 140 | $this->apiUse=true; |
141 | 141 | // Get API options or throw exception as not configured correctly |
142 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort,ERROR); |
|
143 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername,ERROR); |
|
144 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword,ERROR); |
|
142 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_port', $this->apiPort, ERROR); |
|
143 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_user', $this->apiUsername, ERROR); |
|
144 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_password', $this->apiPassword, ERROR); |
|
145 | 145 | } |
146 | 146 | } |
147 | 147 | |
@@ -152,40 +152,40 @@ discard block |
||
152 | 152 | protected function setupDatabase($trapConfig) |
153 | 153 | { |
154 | 154 | // Trap database |
155 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
155 | + if (!array_key_exists('database', $trapConfig['config'])) |
|
156 | 156 | { |
157 | - $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
157 | + $this->logging->log("No database in config file: ".$this->trapModuleConfig, ERROR, ''); |
|
158 | 158 | return; |
159 | 159 | } |
160 | 160 | $dbTrapName=$trapConfig['config']['database']; |
161 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
161 | + $this->logging->log("Found database in config file: ".$dbTrapName, INFO); |
|
162 | 162 | |
163 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
163 | + if (($dbConfig=parse_ini_file($this->icingaweb2Ressources, true)) === false) |
|
164 | 164 | { |
165 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
165 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources, ERROR, ''); |
|
166 | 166 | return; |
167 | 167 | } |
168 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
168 | + if (!array_key_exists($dbTrapName, $dbConfig)) |
|
169 | 169 | { |
170 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
170 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources, ERROR, ''); |
|
171 | 171 | return; |
172 | 172 | } |
173 | 173 | |
174 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
174 | + $this->trapsDB=new Database($this->logging, $dbConfig[$dbTrapName], $this->dbPrefix); |
|
175 | 175 | |
176 | 176 | if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
177 | 177 | |
178 | 178 | // IDO Database |
179 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
179 | + if (!array_key_exists('IDOdatabase', $trapConfig['config'])) |
|
180 | 180 | { |
181 | - $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
181 | + $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig, ERROR, ''); |
|
182 | 182 | } |
183 | 183 | $dbIdoName=$trapConfig['config']['IDOdatabase']; |
184 | 184 | |
185 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
186 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
185 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName, INFO); |
|
186 | + if (!array_key_exists($dbIdoName, $dbConfig)) |
|
187 | 187 | { |
188 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
188 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources, ERROR, ''); |
|
189 | 189 | return; |
190 | 190 | } |
191 | 191 |
@@ -40,8 +40,7 @@ discard block |
||
40 | 40 | } |
41 | 41 | $this->getLogging()->log($message,$log_level); |
42 | 42 | return false; |
43 | - } |
|
44 | - else |
|
43 | + } else |
|
45 | 44 | { |
46 | 45 | $option_var=$option_array[$option_category][$option_name]; |
47 | 46 | return true; |
@@ -54,9 +53,11 @@ discard block |
||
54 | 53 | protected function getDatabaseOptions() |
55 | 54 | { |
56 | 55 | // Database options |
57 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
56 | + if ($this->logSetup === false) { |
|
57 | + // Only if logging was no setup in constructor |
|
58 | 58 | { |
59 | 59 | $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
60 | + } |
|
60 | 61 | $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
61 | 62 | $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
62 | 63 | } |
@@ -69,7 +70,9 @@ discard block |
||
69 | 70 | protected function getDBConfigIfSet($element,&$variable) |
70 | 71 | { |
71 | 72 | $value=$this->getDBConfig($element); |
72 | - if ($value != null) $variable=$value; |
|
73 | + if ($value != null) { |
|
74 | + $variable=$value; |
|
75 | + } |
|
73 | 76 | } |
74 | 77 | |
75 | 78 | /** |
@@ -106,8 +109,7 @@ discard block |
||
106 | 109 | { |
107 | 110 | $this->getLogging()->log('Unknown node status '.$nodeStatus.' : setting to MASTER',WARN); |
108 | 111 | $this->getTrapApi()->setStatusMaster(); |
109 | - } |
|
110 | - else |
|
112 | + } else |
|
111 | 113 | { |
112 | 114 | if ($this->getTrapApi()->getStatus() != TrapApi::MASTER) |
113 | 115 | { |
@@ -173,7 +175,10 @@ discard block |
||
173 | 175 | |
174 | 176 | $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
175 | 177 | |
176 | - if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
178 | + if ($this->apiUse === true) { |
|
179 | + return; |
|
180 | + } |
|
181 | + // In case of API use, no IDO is necessary |
|
177 | 182 | |
178 | 183 | // IDO Database |
179 | 184 | if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
@@ -17,93 +17,93 @@ |
||
17 | 17 | class TrapApi |
18 | 18 | { |
19 | 19 | |
20 | - // Constants |
|
21 | - public const MASTER=1; |
|
22 | - public const MASTERHA=2; |
|
23 | - public const SAT=3; |
|
24 | - public $stateArray = array('MASTER' => TrapApi::MASTER, 'MASTERHA' => TrapApi::MASTERHA , 'SAT' => TrapApi::SAT ); |
|
20 | + // Constants |
|
21 | + public const MASTER=1; |
|
22 | + public const MASTERHA=2; |
|
23 | + public const SAT=3; |
|
24 | + public $stateArray = array('MASTER' => TrapApi::MASTER, 'MASTERHA' => TrapApi::MASTERHA , 'SAT' => TrapApi::SAT ); |
|
25 | 25 | |
26 | - /** @var integer $whoami current server : MASTER MASTERHA or SAT */ |
|
27 | - public $whoami = TrapApi::MASTER; |
|
28 | - /** @var string $masterIP ip of master if MASTERHA or SAT */ |
|
29 | - public $masterIP=''; |
|
30 | - /** @var integer $masterPort port of master if MASTERHA or SAT */ |
|
31 | - public $masterPort=443; |
|
32 | - /** @var string $masterUser user to log in API */ |
|
33 | - public $masterUser=''; |
|
34 | - /** @var string $masterPass password */ |
|
35 | - public $masterPass=''; |
|
26 | + /** @var integer $whoami current server : MASTER MASTERHA or SAT */ |
|
27 | + public $whoami = TrapApi::MASTER; |
|
28 | + /** @var string $masterIP ip of master if MASTERHA or SAT */ |
|
29 | + public $masterIP=''; |
|
30 | + /** @var integer $masterPort port of master if MASTERHA or SAT */ |
|
31 | + public $masterPort=443; |
|
32 | + /** @var string $masterUser user to log in API */ |
|
33 | + public $masterUser=''; |
|
34 | + /** @var string $masterPass password */ |
|
35 | + public $masterPass=''; |
|
36 | 36 | |
37 | - /** @var Logging $logging logging class */ |
|
38 | - protected $logging; |
|
37 | + /** @var Logging $logging logging class */ |
|
38 | + protected $logging; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Create TrapApi class |
|
42 | - * @param Logging $logClass |
|
43 | - */ |
|
44 | - function __construct($logClass) |
|
45 | - { |
|
46 | - $this->logging=$logClass; |
|
47 | - } |
|
40 | + /** |
|
41 | + * Create TrapApi class |
|
42 | + * @param Logging $logClass |
|
43 | + */ |
|
44 | + function __construct($logClass) |
|
45 | + { |
|
46 | + $this->logging=$logClass; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Return true if ode is master. |
|
51 | - * @return boolean |
|
52 | - */ |
|
53 | - public function isMaster() |
|
54 | - { |
|
55 | - return ($this->whoami == MASTER); |
|
56 | - } |
|
49 | + /** |
|
50 | + * Return true if ode is master. |
|
51 | + * @return boolean |
|
52 | + */ |
|
53 | + public function isMaster() |
|
54 | + { |
|
55 | + return ($this->whoami == MASTER); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * return status of node |
|
60 | - * @return number |
|
61 | - */ |
|
62 | - public function getStatus() |
|
63 | - { |
|
64 | - return $this->whoami; |
|
65 | - } |
|
58 | + /** |
|
59 | + * return status of node |
|
60 | + * @return number |
|
61 | + */ |
|
62 | + public function getStatus() |
|
63 | + { |
|
64 | + return $this->whoami; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Set status os node to $status |
|
69 | - * @param string $status |
|
70 | - * @return boolean : true if $status is correct, or false. |
|
71 | - */ |
|
72 | - public function setStatus(string $status) |
|
73 | - { |
|
74 | - if (! isset($this->stateArray[$status])) |
|
75 | - { |
|
76 | - return FALSE; |
|
77 | - } |
|
67 | + /** |
|
68 | + * Set status os node to $status |
|
69 | + * @param string $status |
|
70 | + * @return boolean : true if $status is correct, or false. |
|
71 | + */ |
|
72 | + public function setStatus(string $status) |
|
73 | + { |
|
74 | + if (! isset($this->stateArray[$status])) |
|
75 | + { |
|
76 | + return FALSE; |
|
77 | + } |
|
78 | 78 | |
79 | - $this->logging->log('Setting status to : ' . $status, INFO); |
|
79 | + $this->logging->log('Setting status to : ' . $status, INFO); |
|
80 | 80 | |
81 | - $this->whoami = $this->stateArray[$status]; |
|
81 | + $this->whoami = $this->stateArray[$status]; |
|
82 | 82 | |
83 | - return TRUE; |
|
84 | - } |
|
83 | + return TRUE; |
|
84 | + } |
|
85 | 85 | |
86 | - public function setStatusMaster() |
|
87 | - { |
|
88 | - $this->whoami = TrapApi::MASTER; |
|
89 | - } |
|
86 | + public function setStatusMaster() |
|
87 | + { |
|
88 | + $this->whoami = TrapApi::MASTER; |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Set params for API connection |
|
93 | - * @param string $IP |
|
94 | - * @param int $port |
|
95 | - * @param string $user |
|
96 | - * @param string $pass |
|
97 | - * @return boolean true if params are OK |
|
98 | - */ |
|
99 | - public function setParams(string $IP, int $port, string $user, string $pass) |
|
100 | - { |
|
101 | - $this->masterIP = $IP; |
|
102 | - $this->masterPort = $port; |
|
103 | - $this->masterUser = $user; |
|
104 | - $this->masterPass = $pass; |
|
91 | + /** |
|
92 | + * Set params for API connection |
|
93 | + * @param string $IP |
|
94 | + * @param int $port |
|
95 | + * @param string $user |
|
96 | + * @param string $pass |
|
97 | + * @return boolean true if params are OK |
|
98 | + */ |
|
99 | + public function setParams(string $IP, int $port, string $user, string $pass) |
|
100 | + { |
|
101 | + $this->masterIP = $IP; |
|
102 | + $this->masterPort = $port; |
|
103 | + $this->masterUser = $user; |
|
104 | + $this->masterPass = $pass; |
|
105 | 105 | |
106 | - return true; |
|
107 | - } |
|
106 | + return true; |
|
107 | + } |
|
108 | 108 | |
109 | 109 | } |
110 | 110 | \ No newline at end of file |
@@ -21,10 +21,10 @@ discard block |
||
21 | 21 | public const MASTER=1; |
22 | 22 | public const MASTERHA=2; |
23 | 23 | public const SAT=3; |
24 | - public $stateArray = array('MASTER' => TrapApi::MASTER, 'MASTERHA' => TrapApi::MASTERHA , 'SAT' => TrapApi::SAT ); |
|
24 | + public $stateArray=array('MASTER' => TrapApi::MASTER, 'MASTERHA' => TrapApi::MASTERHA, 'SAT' => TrapApi::SAT); |
|
25 | 25 | |
26 | 26 | /** @var integer $whoami current server : MASTER MASTERHA or SAT */ |
27 | - public $whoami = TrapApi::MASTER; |
|
27 | + public $whoami=TrapApi::MASTER; |
|
28 | 28 | /** @var string $masterIP ip of master if MASTERHA or SAT */ |
29 | 29 | public $masterIP=''; |
30 | 30 | /** @var integer $masterPort port of master if MASTERHA or SAT */ |
@@ -71,21 +71,21 @@ discard block |
||
71 | 71 | */ |
72 | 72 | public function setStatus(string $status) |
73 | 73 | { |
74 | - if (! isset($this->stateArray[$status])) |
|
74 | + if (!isset($this->stateArray[$status])) |
|
75 | 75 | { |
76 | 76 | return FALSE; |
77 | 77 | } |
78 | 78 | |
79 | - $this->logging->log('Setting status to : ' . $status, INFO); |
|
79 | + $this->logging->log('Setting status to : '.$status, INFO); |
|
80 | 80 | |
81 | - $this->whoami = $this->stateArray[$status]; |
|
81 | + $this->whoami=$this->stateArray[$status]; |
|
82 | 82 | |
83 | 83 | return TRUE; |
84 | 84 | } |
85 | 85 | |
86 | 86 | public function setStatusMaster() |
87 | 87 | { |
88 | - $this->whoami = TrapApi::MASTER; |
|
88 | + $this->whoami=TrapApi::MASTER; |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | /** |
@@ -98,10 +98,10 @@ discard block |
||
98 | 98 | */ |
99 | 99 | public function setParams(string $IP, int $port, string $user, string $pass) |
100 | 100 | { |
101 | - $this->masterIP = $IP; |
|
102 | - $this->masterPort = $port; |
|
103 | - $this->masterUser = $user; |
|
104 | - $this->masterPass = $pass; |
|
101 | + $this->masterIP=$IP; |
|
102 | + $this->masterPort=$port; |
|
103 | + $this->masterUser=$user; |
|
104 | + $this->masterPass=$pass; |
|
105 | 105 | |
106 | 106 | return true; |
107 | 107 | } |
@@ -14,43 +14,43 @@ |
||
14 | 14 | |
15 | 15 | class RestAPI |
16 | 16 | { |
17 | - public $version=1; |
|
17 | + public $version=1; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @var TrapsController $trapController |
|
21 | - */ |
|
22 | - protected $trapController=null; |
|
19 | + /** |
|
20 | + * @var TrapsController $trapController |
|
21 | + */ |
|
22 | + protected $trapController=null; |
|
23 | 23 | |
24 | - public function __construct(TrapsController $trapCtrl) |
|
25 | - { |
|
26 | - $this->trapController=$trapCtrl; |
|
27 | - } |
|
24 | + public function __construct(TrapsController $trapCtrl) |
|
25 | + { |
|
26 | + $this->trapController=$trapCtrl; |
|
27 | + } |
|
28 | 28 | |
29 | - public function sendJson($object) |
|
30 | - { |
|
31 | - $this->trapController->getResponse()->setHeader('Content-Type', 'application/json', true); |
|
32 | - $this->trapController->getResponse()->sendHeaders(); |
|
33 | - $this->trapController->helper_ret(json_encode($object, JSON_PRETTY_PRINT)); |
|
34 | - //$this->trapController->_helper->json($object); |
|
35 | - //echo json_encode($object, JSON_PRETTY_PRINT) . "\n"; |
|
36 | - } |
|
29 | + public function sendJson($object) |
|
30 | + { |
|
31 | + $this->trapController->getResponse()->setHeader('Content-Type', 'application/json', true); |
|
32 | + $this->trapController->getResponse()->sendHeaders(); |
|
33 | + $this->trapController->helper_ret(json_encode($object, JSON_PRETTY_PRINT)); |
|
34 | + //$this->trapController->_helper->json($object); |
|
35 | + //echo json_encode($object, JSON_PRETTY_PRINT) . "\n"; |
|
36 | + } |
|
37 | 37 | |
38 | - protected function sendJsonError(string $error, int $retCode = 200) |
|
39 | - { |
|
40 | - //TODO |
|
41 | - $this->sendJson('{"Error":"'.$error.'"}'); |
|
42 | - } |
|
38 | + protected function sendJsonError(string $error, int $retCode = 200) |
|
39 | + { |
|
40 | + //TODO |
|
41 | + $this->sendJson('{"Error":"'.$error.'"}'); |
|
42 | + } |
|
43 | 43 | |
44 | - public function last_modified() |
|
45 | - { |
|
46 | - try |
|
47 | - { |
|
48 | - $query = $this->trapController->getUIDatabase()->lastModification(); |
|
49 | - return array('lastModified' => $query); |
|
50 | - } |
|
51 | - catch (\ErrorException $e) |
|
52 | - { |
|
53 | - return array('Error' => $e->getMessage()); |
|
54 | - } |
|
55 | - } |
|
44 | + public function last_modified() |
|
45 | + { |
|
46 | + try |
|
47 | + { |
|
48 | + $query = $this->trapController->getUIDatabase()->lastModification(); |
|
49 | + return array('lastModified' => $query); |
|
50 | + } |
|
51 | + catch (\ErrorException $e) |
|
52 | + { |
|
53 | + return array('Error' => $e->getMessage()); |
|
54 | + } |
|
55 | + } |
|
56 | 56 | } |
57 | 57 | \ No newline at end of file |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | //echo json_encode($object, JSON_PRETTY_PRINT) . "\n"; |
36 | 36 | } |
37 | 37 | |
38 | - protected function sendJsonError(string $error, int $retCode = 200) |
|
38 | + protected function sendJsonError(string $error, int $retCode=200) |
|
39 | 39 | { |
40 | 40 | //TODO |
41 | 41 | $this->sendJson('{"Error":"'.$error.'"}'); |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | { |
46 | 46 | try |
47 | 47 | { |
48 | - $query = $this->trapController->getUIDatabase()->lastModification(); |
|
48 | + $query=$this->trapController->getUIDatabase()->lastModification(); |
|
49 | 49 | return array('lastModified' => $query); |
50 | 50 | } |
51 | 51 | catch (\ErrorException $e) |
@@ -47,8 +47,7 @@ |
||
47 | 47 | { |
48 | 48 | $query = $this->trapController->getUIDatabase()->lastModification(); |
49 | 49 | return array('lastModified' => $query); |
50 | - } |
|
51 | - catch (\ErrorException $e) |
|
50 | + } catch (\ErrorException $e) |
|
52 | 51 | { |
53 | 52 | return array('Error' => $e->getMessage()); |
54 | 53 | } |