@@ -71,11 +71,13 @@ discard block |
||
71 | 71 | $start=$item; |
72 | 72 | $parenthesisCount=0; |
73 | 73 | while (($item < strlen($rule)) // Not end of string AND |
74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
74 | + && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) { |
|
75 | + // Closing ')' or embeded () |
|
75 | 76 | { |
76 | 77 | if ($rule[$item] == '"' ) |
77 | 78 | { // pass through string |
78 | 79 | $item++; |
80 | + } |
|
79 | 81 | $item=$this->eval_getNext($rule,$item,'"'); |
80 | 82 | continue; |
81 | 83 | } |
@@ -121,10 +123,12 @@ discard block |
||
121 | 123 | { |
122 | 124 | // function is : __function(param1,param2...) |
123 | 125 | $start=$item; |
124 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
126 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) { |
|
127 | + // Not end of string AND not opening '(' |
|
125 | 128 | { |
126 | 129 | $item++; |
127 | 130 | } |
131 | + } |
|
128 | 132 | if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
129 | 133 | |
130 | 134 | // get parameters between parenthesis |
@@ -167,12 +171,13 @@ discard block |
||
167 | 171 | */ |
168 | 172 | private function check_negate_first(string $rule,int &$item) |
169 | 173 | { |
170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
174 | + if ( $rule[$item] == '!') { |
|
175 | + // If '!' found, negate next expression. |
|
171 | 176 | { |
172 | 177 | $item++; |
173 | - return true; |
|
174 | 178 | } |
175 | - else |
|
179 | + return true; |
|
180 | + } else |
|
176 | 181 | { |
177 | 182 | return false; |
178 | 183 | } |
@@ -199,7 +204,9 @@ discard block |
||
199 | 204 | $rule2.=$rule[$item]; |
200 | 205 | $item++; |
201 | 206 | } |
202 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
207 | + if ($item == strlen ($rule)) { |
|
208 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
209 | + } |
|
203 | 210 | $rule2.=$rule[$item]; |
204 | 211 | $item++; |
205 | 212 | continue; |
@@ -12,204 +12,204 @@ |
||
12 | 12 | trait RuleUtils |
13 | 13 | { |
14 | 14 | |
15 | - /** rule eval starting from $token |
|
16 | - * @param string $rule |
|
17 | - * @param int $item |
|
18 | - */ |
|
19 | - abstract public function evaluation($rule,&$item); |
|
15 | + /** rule eval starting from $token |
|
16 | + * @param string $rule |
|
17 | + * @param int $item |
|
18 | + */ |
|
19 | + abstract public function evaluation($rule,&$item); |
|
20 | 20 | |
21 | - /** |
|
22 | - * Get full number |
|
23 | - * @param string $rule Rule as string |
|
24 | - * @param int $item current eval position |
|
25 | - * @return array<int,string> |
|
26 | - */ |
|
27 | - private function get_number(string $rule,int &$item) |
|
28 | - { |
|
29 | - $item2=$item+1; |
|
30 | - while ( |
|
31 | - ($item2!=strlen($rule)) |
|
32 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
33 | - { |
|
34 | - $item2++ ; |
|
35 | - } |
|
36 | - $val=substr($rule,$item,$item2-$item); |
|
37 | - $item=$item2; |
|
38 | - //echo "number ".$val."\n"; |
|
21 | + /** |
|
22 | + * Get full number |
|
23 | + * @param string $rule Rule as string |
|
24 | + * @param int $item current eval position |
|
25 | + * @return array<int,string> |
|
26 | + */ |
|
27 | + private function get_number(string $rule,int &$item) |
|
28 | + { |
|
29 | + $item2=$item+1; |
|
30 | + while ( |
|
31 | + ($item2!=strlen($rule)) |
|
32 | + && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
33 | + { |
|
34 | + $item2++ ; |
|
35 | + } |
|
36 | + $val=substr($rule,$item,$item2-$item); |
|
37 | + $item=$item2; |
|
38 | + //echo "number ".$val."\n"; |
|
39 | 39 | |
40 | - return array(0,$val); |
|
41 | - } |
|
40 | + return array(0,$val); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get a string (between ") |
|
45 | - * @param string $rule Rule as string |
|
46 | - * @param int $item current eval position |
|
47 | - * @return array<int,string> |
|
48 | - */ |
|
49 | - private function get_string(string $rule,int &$item) |
|
50 | - { |
|
51 | - $item++; |
|
52 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
53 | - $val=substr($rule,$item,$item2-$item-1); |
|
54 | - $item=$item2; |
|
55 | - //echo "string : ".$val."\n"; |
|
56 | - return array(1,$val); |
|
43 | + /** |
|
44 | + * Get a string (between ") |
|
45 | + * @param string $rule Rule as string |
|
46 | + * @param int $item current eval position |
|
47 | + * @return array<int,string> |
|
48 | + */ |
|
49 | + private function get_string(string $rule,int &$item) |
|
50 | + { |
|
51 | + $item++; |
|
52 | + $item2=$this->eval_getNext($rule,$item,'"'); |
|
53 | + $val=substr($rule,$item,$item2-$item-1); |
|
54 | + $item=$item2; |
|
55 | + //echo "string : ".$val."\n"; |
|
56 | + return array(1,$val); |
|
57 | 57 | |
58 | - } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Parse elements inside () : jumps over "" and count parenthesis. |
|
62 | - * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
63 | - * @param string $rule : the current rule |
|
64 | - * @param int $item : actual position in rule |
|
65 | - * @throws Exception |
|
66 | - * @return string : everything inside parenthesis |
|
67 | - */ |
|
68 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
69 | - { |
|
70 | - $item++; |
|
71 | - $start=$item; |
|
72 | - $parenthesisCount=0; |
|
73 | - while (($item < strlen($rule)) // Not end of string AND |
|
74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
75 | - { |
|
76 | - if ($rule[$item] == '"' ) |
|
77 | - { // pass through string |
|
78 | - $item++; |
|
79 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
80 | - continue; |
|
81 | - } |
|
82 | - if ($rule[$item] == '(') |
|
83 | - { |
|
84 | - $parenthesisCount++; |
|
85 | - } |
|
86 | - if ($rule[$item] == ')') |
|
87 | - { |
|
88 | - $parenthesisCount--; |
|
89 | - } |
|
90 | - $item++; |
|
91 | - } |
|
60 | + /** |
|
61 | + * Parse elements inside () : jumps over "" and count parenthesis. |
|
62 | + * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
63 | + * @param string $rule : the current rule |
|
64 | + * @param int $item : actual position in rule |
|
65 | + * @throws Exception |
|
66 | + * @return string : everything inside parenthesis |
|
67 | + */ |
|
68 | + private function parse_parenthesis(string $rule,int &$item) : string |
|
69 | + { |
|
70 | + $item++; |
|
71 | + $start=$item; |
|
72 | + $parenthesisCount=0; |
|
73 | + while (($item < strlen($rule)) // Not end of string AND |
|
74 | + && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
75 | + { |
|
76 | + if ($rule[$item] == '"' ) |
|
77 | + { // pass through string |
|
78 | + $item++; |
|
79 | + $item=$this->eval_getNext($rule,$item,'"'); |
|
80 | + continue; |
|
81 | + } |
|
82 | + if ($rule[$item] == '(') |
|
83 | + { |
|
84 | + $parenthesisCount++; |
|
85 | + } |
|
86 | + if ($rule[$item] == ')') |
|
87 | + { |
|
88 | + $parenthesisCount--; |
|
89 | + } |
|
90 | + $item++; |
|
91 | + } |
|
92 | 92 | |
93 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
94 | - $val=substr($rule,$start,$item-$start); |
|
95 | - $item++; |
|
96 | - return $val; |
|
97 | - } |
|
93 | + if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
94 | + $val=substr($rule,$start,$item-$start); |
|
95 | + $item++; |
|
96 | + return $val; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get and eval a grouped condition - ex : (1==1) |
|
101 | - * @param string $rule |
|
102 | - * @param int $item |
|
103 | - * @return array<int,string> |
|
104 | - */ |
|
105 | - private function get_group(string $rule,int &$item) : array |
|
106 | - { |
|
107 | - // gets eveything inside parenthesis |
|
108 | - $val=$this->parse_parenthesis($rule, $item); |
|
109 | - // Returns boolean with evaluation of all inside parenthesis |
|
110 | - $start=0; |
|
111 | - return array(2,$this->evaluation($val,$start)); |
|
112 | - } |
|
99 | + /** |
|
100 | + * Get and eval a grouped condition - ex : (1==1) |
|
101 | + * @param string $rule |
|
102 | + * @param int $item |
|
103 | + * @return array<int,string> |
|
104 | + */ |
|
105 | + private function get_group(string $rule,int &$item) : array |
|
106 | + { |
|
107 | + // gets eveything inside parenthesis |
|
108 | + $val=$this->parse_parenthesis($rule, $item); |
|
109 | + // Returns boolean with evaluation of all inside parenthesis |
|
110 | + $start=0; |
|
111 | + return array(2,$this->evaluation($val,$start)); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * @param string $rule |
|
116 | - * @param int $item |
|
117 | - * @throws Exception |
|
118 | - * @return array<int,string> |
|
119 | - */ |
|
120 | - private function get_function(string $rule,int &$item) : array |
|
121 | - { |
|
122 | - // function is : __function(param1,param2...) |
|
123 | - $start=$item; |
|
124 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
125 | - { |
|
126 | - $item++; |
|
127 | - } |
|
128 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
114 | + /** |
|
115 | + * @param string $rule |
|
116 | + * @param int $item |
|
117 | + * @throws Exception |
|
118 | + * @return array<int,string> |
|
119 | + */ |
|
120 | + private function get_function(string $rule,int &$item) : array |
|
121 | + { |
|
122 | + // function is : __function(param1,param2...) |
|
123 | + $start=$item; |
|
124 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
125 | + { |
|
126 | + $item++; |
|
127 | + } |
|
128 | + if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
129 | 129 | |
130 | - // get parameters between parenthesis |
|
130 | + // get parameters between parenthesis |
|
131 | 131 | |
132 | - $this->parse_parenthesis($rule, $item); |
|
132 | + $this->parse_parenthesis($rule, $item); |
|
133 | 133 | |
134 | - $val=substr($rule,$start,$item-$start); |
|
134 | + $val=substr($rule,$start,$item-$start); |
|
135 | 135 | |
136 | - $this->logging->log('got function ' . $val,DEBUG); |
|
136 | + $this->logging->log('got function ' . $val,DEBUG); |
|
137 | 137 | |
138 | - return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
138 | + return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
139 | 139 | |
140 | - } |
|
140 | + } |
|
141 | 141 | |
142 | - /** Find next token $tok in $rule starting at $item |
|
143 | - * @param string $rule |
|
144 | - * @param int $item |
|
145 | - * @param string $tok : token to search for |
|
146 | - * @throws Exception |
|
147 | - * @return int Position after next token |
|
148 | - */ |
|
149 | - protected function eval_getNext(string $rule,int $item,string $tok) |
|
150 | - { |
|
151 | - while ( |
|
152 | - ($rule[$item] != $tok ) |
|
153 | - && ($item < strlen($rule))) |
|
154 | - { |
|
155 | - $item++; |
|
156 | - } |
|
157 | - if ($item==strlen($rule)) { |
|
158 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
159 | - } |
|
160 | - return $item+1; |
|
161 | - } |
|
142 | + /** Find next token $tok in $rule starting at $item |
|
143 | + * @param string $rule |
|
144 | + * @param int $item |
|
145 | + * @param string $tok : token to search for |
|
146 | + * @throws Exception |
|
147 | + * @return int Position after next token |
|
148 | + */ |
|
149 | + protected function eval_getNext(string $rule,int $item,string $tok) |
|
150 | + { |
|
151 | + while ( |
|
152 | + ($rule[$item] != $tok ) |
|
153 | + && ($item < strlen($rule))) |
|
154 | + { |
|
155 | + $item++; |
|
156 | + } |
|
157 | + if ($item==strlen($rule)) { |
|
158 | + throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
159 | + } |
|
160 | + return $item+1; |
|
161 | + } |
|
162 | 162 | |
163 | - /** get negate (!) and return true if found - and pass it with item++ - |
|
164 | - * @param string $rule |
|
165 | - * @param int $item |
|
166 | - * @return boolean |
|
167 | - */ |
|
168 | - private function check_negate_first(string $rule,int &$item) |
|
169 | - { |
|
170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
171 | - { |
|
172 | - $item++; |
|
173 | - return true; |
|
174 | - } |
|
175 | - else |
|
176 | - { |
|
177 | - return false; |
|
178 | - } |
|
179 | - } |
|
163 | + /** get negate (!) and return true if found - and pass it with item++ - |
|
164 | + * @param string $rule |
|
165 | + * @param int $item |
|
166 | + * @return boolean |
|
167 | + */ |
|
168 | + private function check_negate_first(string $rule,int &$item) |
|
169 | + { |
|
170 | + if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
171 | + { |
|
172 | + $item++; |
|
173 | + return true; |
|
174 | + } |
|
175 | + else |
|
176 | + { |
|
177 | + return false; |
|
178 | + } |
|
179 | + } |
|
180 | 180 | |
181 | - /** Remove all whitespaces (when not quoted) |
|
182 | - * @param string $rule |
|
183 | - * @throws Exception |
|
184 | - * @return string |
|
185 | - */ |
|
186 | - public function eval_cleanup($rule) |
|
187 | - { |
|
188 | - $item=0; |
|
189 | - $rule2=''; |
|
190 | - while ($item < strlen($rule)) |
|
191 | - { |
|
192 | - if ($rule[$item]==' ') { $item++; continue; } |
|
193 | - if ($rule[$item]=='"') |
|
194 | - { |
|
195 | - $rule2.=$rule[$item]; |
|
196 | - $item++; |
|
197 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
198 | - { |
|
199 | - $rule2.=$rule[$item]; |
|
200 | - $item++; |
|
201 | - } |
|
202 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
203 | - $rule2.=$rule[$item]; |
|
204 | - $item++; |
|
205 | - continue; |
|
206 | - } |
|
181 | + /** Remove all whitespaces (when not quoted) |
|
182 | + * @param string $rule |
|
183 | + * @throws Exception |
|
184 | + * @return string |
|
185 | + */ |
|
186 | + public function eval_cleanup($rule) |
|
187 | + { |
|
188 | + $item=0; |
|
189 | + $rule2=''; |
|
190 | + while ($item < strlen($rule)) |
|
191 | + { |
|
192 | + if ($rule[$item]==' ') { $item++; continue; } |
|
193 | + if ($rule[$item]=='"') |
|
194 | + { |
|
195 | + $rule2.=$rule[$item]; |
|
196 | + $item++; |
|
197 | + while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
198 | + { |
|
199 | + $rule2.=$rule[$item]; |
|
200 | + $item++; |
|
201 | + } |
|
202 | + if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
203 | + $rule2.=$rule[$item]; |
|
204 | + $item++; |
|
205 | + continue; |
|
206 | + } |
|
207 | 207 | |
208 | - $rule2.=$rule[$item]; |
|
209 | - $item++; |
|
210 | - } |
|
208 | + $rule2.=$rule[$item]; |
|
209 | + $item++; |
|
210 | + } |
|
211 | 211 | |
212 | - return $rule2; |
|
213 | - } |
|
212 | + return $rule2; |
|
213 | + } |
|
214 | 214 | |
215 | 215 | } |
216 | 216 | \ No newline at end of file |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * @param string $rule |
17 | 17 | * @param int $item |
18 | 18 | */ |
19 | - abstract public function evaluation($rule,&$item); |
|
19 | + abstract public function evaluation($rule, &$item); |
|
20 | 20 | |
21 | 21 | /** |
22 | 22 | * Get full number |
@@ -24,20 +24,20 @@ discard block |
||
24 | 24 | * @param int $item current eval position |
25 | 25 | * @return array<int,string> |
26 | 26 | */ |
27 | - private function get_number(string $rule,int &$item) |
|
27 | + private function get_number(string $rule, int &$item) |
|
28 | 28 | { |
29 | - $item2=$item+1; |
|
29 | + $item2=$item + 1; |
|
30 | 30 | while ( |
31 | - ($item2!=strlen($rule)) |
|
32 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
31 | + ($item2 != strlen($rule)) |
|
32 | + && (preg_match('/[\-0-9\.]/', $rule[$item2]))) |
|
33 | 33 | { |
34 | - $item2++ ; |
|
34 | + $item2++; |
|
35 | 35 | } |
36 | - $val=substr($rule,$item,$item2-$item); |
|
36 | + $val=substr($rule, $item, $item2 - $item); |
|
37 | 37 | $item=$item2; |
38 | 38 | //echo "number ".$val."\n"; |
39 | 39 | |
40 | - return array(0,$val); |
|
40 | + return array(0, $val); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -46,14 +46,14 @@ discard block |
||
46 | 46 | * @param int $item current eval position |
47 | 47 | * @return array<int,string> |
48 | 48 | */ |
49 | - private function get_string(string $rule,int &$item) |
|
49 | + private function get_string(string $rule, int &$item) |
|
50 | 50 | { |
51 | 51 | $item++; |
52 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
53 | - $val=substr($rule,$item,$item2-$item-1); |
|
52 | + $item2=$this->eval_getNext($rule, $item, '"'); |
|
53 | + $val=substr($rule, $item, $item2 - $item - 1); |
|
54 | 54 | $item=$item2; |
55 | 55 | //echo "string : ".$val."\n"; |
56 | - return array(1,$val); |
|
56 | + return array(1, $val); |
|
57 | 57 | |
58 | 58 | } |
59 | 59 | |
@@ -65,18 +65,18 @@ discard block |
||
65 | 65 | * @throws Exception |
66 | 66 | * @return string : everything inside parenthesis |
67 | 67 | */ |
68 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
68 | + private function parse_parenthesis(string $rule, int &$item) : string |
|
69 | 69 | { |
70 | 70 | $item++; |
71 | 71 | $start=$item; |
72 | 72 | $parenthesisCount=0; |
73 | 73 | while (($item < strlen($rule)) // Not end of string AND |
74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
74 | + && (($rule[$item] != ')') || $parenthesisCount > 0)) // Closing ')' or embeded () |
|
75 | 75 | { |
76 | - if ($rule[$item] == '"' ) |
|
76 | + if ($rule[$item] == '"') |
|
77 | 77 | { // pass through string |
78 | 78 | $item++; |
79 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
79 | + $item=$this->eval_getNext($rule, $item, '"'); |
|
80 | 80 | continue; |
81 | 81 | } |
82 | 82 | if ($rule[$item] == '(') |
@@ -90,8 +90,8 @@ discard block |
||
90 | 90 | $item++; |
91 | 91 | } |
92 | 92 | |
93 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
94 | - $val=substr($rule,$start,$item-$start); |
|
93 | + if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); } |
|
94 | + $val=substr($rule, $start, $item - $start); |
|
95 | 95 | $item++; |
96 | 96 | return $val; |
97 | 97 | } |
@@ -102,13 +102,13 @@ discard block |
||
102 | 102 | * @param int $item |
103 | 103 | * @return array<int,string> |
104 | 104 | */ |
105 | - private function get_group(string $rule,int &$item) : array |
|
105 | + private function get_group(string $rule, int &$item) : array |
|
106 | 106 | { |
107 | 107 | // gets eveything inside parenthesis |
108 | 108 | $val=$this->parse_parenthesis($rule, $item); |
109 | 109 | // Returns boolean with evaluation of all inside parenthesis |
110 | 110 | $start=0; |
111 | - return array(2,$this->evaluation($val,$start)); |
|
111 | + return array(2, $this->evaluation($val, $start)); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | /** |
@@ -117,25 +117,25 @@ discard block |
||
117 | 117 | * @throws Exception |
118 | 118 | * @return array<int,string> |
119 | 119 | */ |
120 | - private function get_function(string $rule,int &$item) : array |
|
120 | + private function get_function(string $rule, int &$item) : array |
|
121 | 121 | { |
122 | 122 | // function is : __function(param1,param2...) |
123 | 123 | $start=$item; |
124 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
124 | + while (($item < strlen($rule)) && ($rule[$item] != '(')) // Not end of string AND not opening '(' |
|
125 | 125 | { |
126 | 126 | $item++; |
127 | 127 | } |
128 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
128 | + if ($item == strlen($rule)) {throw new Exception("no opening () for function in ".$rule." at ".$item); } |
|
129 | 129 | |
130 | 130 | // get parameters between parenthesis |
131 | 131 | |
132 | 132 | $this->parse_parenthesis($rule, $item); |
133 | 133 | |
134 | - $val=substr($rule,$start,$item-$start); |
|
134 | + $val=substr($rule, $start, $item - $start); |
|
135 | 135 | |
136 | - $this->logging->log('got function ' . $val,DEBUG); |
|
136 | + $this->logging->log('got function '.$val, DEBUG); |
|
137 | 137 | |
138 | - return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
138 | + return array(2, $this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
139 | 139 | |
140 | 140 | } |
141 | 141 | |
@@ -146,18 +146,18 @@ discard block |
||
146 | 146 | * @throws Exception |
147 | 147 | * @return int Position after next token |
148 | 148 | */ |
149 | - protected function eval_getNext(string $rule,int $item,string $tok) |
|
149 | + protected function eval_getNext(string $rule, int $item, string $tok) |
|
150 | 150 | { |
151 | 151 | while ( |
152 | - ($rule[$item] != $tok ) |
|
152 | + ($rule[$item] != $tok) |
|
153 | 153 | && ($item < strlen($rule))) |
154 | 154 | { |
155 | 155 | $item++; |
156 | 156 | } |
157 | - if ($item==strlen($rule)) { |
|
158 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
157 | + if ($item == strlen($rule)) { |
|
158 | + throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item); |
|
159 | 159 | } |
160 | - return $item+1; |
|
160 | + return $item + 1; |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | /** get negate (!) and return true if found - and pass it with item++ - |
@@ -165,9 +165,9 @@ discard block |
||
165 | 165 | * @param int $item |
166 | 166 | * @return boolean |
167 | 167 | */ |
168 | - private function check_negate_first(string $rule,int &$item) |
|
168 | + private function check_negate_first(string $rule, int &$item) |
|
169 | 169 | { |
170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
170 | + if ($rule[$item] == '!') // If '!' found, negate next expression. |
|
171 | 171 | { |
172 | 172 | $item++; |
173 | 173 | return true; |
@@ -189,17 +189,17 @@ discard block |
||
189 | 189 | $rule2=''; |
190 | 190 | while ($item < strlen($rule)) |
191 | 191 | { |
192 | - if ($rule[$item]==' ') { $item++; continue; } |
|
193 | - if ($rule[$item]=='"') |
|
192 | + if ($rule[$item] == ' ') { $item++; continue; } |
|
193 | + if ($rule[$item] == '"') |
|
194 | 194 | { |
195 | 195 | $rule2.=$rule[$item]; |
196 | 196 | $item++; |
197 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
197 | + while (($item < strlen($rule)) && ($rule[$item] != '"')) |
|
198 | 198 | { |
199 | 199 | $rule2.=$rule[$item]; |
200 | 200 | $item++; |
201 | 201 | } |
202 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
202 | + if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item); |
|
203 | 203 | $rule2.=$rule[$item]; |
204 | 204 | $item++; |
205 | 205 | continue; |
@@ -6,233 +6,233 @@ |
||
6 | 6 | |
7 | 7 | class Rule |
8 | 8 | { |
9 | - use \RuleUtils; |
|
9 | + use \RuleUtils; |
|
10 | 10 | |
11 | - /** @var Logging $logging logging class*/ |
|
12 | - protected $logging; |
|
11 | + /** @var Logging $logging logging class*/ |
|
12 | + protected $logging; |
|
13 | 13 | |
14 | - /** @var Trap $trapClass */ |
|
15 | - protected $trapClass; |
|
14 | + /** @var Trap $trapClass */ |
|
15 | + protected $trapClass; |
|
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Setup Rule Class |
|
20 | - * @param Trap $trapClass : To get logging class & plugin class |
|
21 | - */ |
|
22 | - function __construct($trapClass) |
|
23 | - { |
|
24 | - $this->trapClass=$trapClass; |
|
25 | - $this->logging=$trapClass->logging; |
|
26 | - } |
|
18 | + /** |
|
19 | + * Setup Rule Class |
|
20 | + * @param Trap $trapClass : To get logging class & plugin class |
|
21 | + */ |
|
22 | + function __construct($trapClass) |
|
23 | + { |
|
24 | + $this->trapClass=$trapClass; |
|
25 | + $this->logging=$trapClass->logging; |
|
26 | + } |
|
27 | 27 | |
28 | 28 | |
29 | - protected function eval_getElement($rule,&$item) |
|
30 | - { |
|
31 | - if ($item >= strlen($rule)) |
|
32 | - { |
|
33 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
34 | - } |
|
35 | - while ($rule[$item]==' ') $item++; |
|
36 | - if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
37 | - { // number |
|
38 | - return $this->get_number($rule, $item); |
|
39 | - } |
|
40 | - if ($rule[$item] == '"') |
|
41 | - { // string |
|
42 | - return $this->get_string($rule, $item); |
|
43 | - } |
|
29 | + protected function eval_getElement($rule,&$item) |
|
30 | + { |
|
31 | + if ($item >= strlen($rule)) |
|
32 | + { |
|
33 | + throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
34 | + } |
|
35 | + while ($rule[$item]==' ') $item++; |
|
36 | + if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
37 | + { // number |
|
38 | + return $this->get_number($rule, $item); |
|
39 | + } |
|
40 | + if ($rule[$item] == '"') |
|
41 | + { // string |
|
42 | + return $this->get_string($rule, $item); |
|
43 | + } |
|
44 | 44 | |
45 | - if ($rule[$item] == '(') |
|
46 | - { // grouping |
|
47 | - return $this->get_group($rule, $item); |
|
48 | - } |
|
49 | - if ($rule[$item] == '_') |
|
50 | - { // function |
|
51 | - return $this->get_function($rule, $item); |
|
52 | - } |
|
53 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
45 | + if ($rule[$item] == '(') |
|
46 | + { // grouping |
|
47 | + return $this->get_group($rule, $item); |
|
48 | + } |
|
49 | + if ($rule[$item] == '_') |
|
50 | + { // function |
|
51 | + return $this->get_function($rule, $item); |
|
52 | + } |
|
53 | + throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
54 | 54 | |
55 | - } |
|
55 | + } |
|
56 | 56 | |
57 | - protected function eval_getOper($rule,&$item) |
|
58 | - { |
|
59 | - while ($rule[$item]==' ') $item++; |
|
60 | - switch ($rule[$item]) |
|
61 | - { |
|
62 | - case '<': |
|
63 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
64 | - $item++; return array(0,"<"); |
|
65 | - case '>': |
|
66 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
67 | - $item++; return array(0,">"); |
|
68 | - case '=': |
|
69 | - $item++; return array(0,"="); |
|
70 | - case '!': |
|
71 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
72 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
73 | - case '~': |
|
74 | - $item++; return array(0,"~"); |
|
75 | - case '|': |
|
76 | - $item++; return array(1,"|"); |
|
77 | - case '&': |
|
78 | - $item++; return array(1,"&"); |
|
79 | - default : |
|
80 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
81 | - } |
|
82 | - } |
|
57 | + protected function eval_getOper($rule,&$item) |
|
58 | + { |
|
59 | + while ($rule[$item]==' ') $item++; |
|
60 | + switch ($rule[$item]) |
|
61 | + { |
|
62 | + case '<': |
|
63 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
64 | + $item++; return array(0,"<"); |
|
65 | + case '>': |
|
66 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
67 | + $item++; return array(0,">"); |
|
68 | + case '=': |
|
69 | + $item++; return array(0,"="); |
|
70 | + case '!': |
|
71 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
72 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
73 | + case '~': |
|
74 | + $item++; return array(0,"~"); |
|
75 | + case '|': |
|
76 | + $item++; return array(1,"|"); |
|
77 | + case '&': |
|
78 | + $item++; return array(1,"&"); |
|
79 | + default : |
|
80 | + throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
81 | + } |
|
82 | + } |
|
83 | 83 | |
84 | - private function do_compare($val1,$val2,$comp,$negate) |
|
85 | - { |
|
86 | - switch ($comp){ |
|
87 | - case '<': $retVal= ($val1 < $val2); break; |
|
88 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
89 | - case '>': $retVal= ($val1 > $val2); break; |
|
90 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
91 | - case '=': $retVal= ($val1 == $val2); break; |
|
92 | - case '!=': $retVal= ($val1 != $val2); break; |
|
93 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
94 | - case '|': $retVal= ($val1 || $val2); break; |
|
95 | - case '&': $retVal= ($val1 && $val2); break; |
|
96 | - default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
97 | - } |
|
98 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
84 | + private function do_compare($val1,$val2,$comp,$negate) |
|
85 | + { |
|
86 | + switch ($comp){ |
|
87 | + case '<': $retVal= ($val1 < $val2); break; |
|
88 | + case '<=': $retVal= ($val1 <= $val2); break; |
|
89 | + case '>': $retVal= ($val1 > $val2); break; |
|
90 | + case '>=': $retVal= ($val1 >= $val2); break; |
|
91 | + case '=': $retVal= ($val1 == $val2); break; |
|
92 | + case '!=': $retVal= ($val1 != $val2); break; |
|
93 | + case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
94 | + case '|': $retVal= ($val1 || $val2); break; |
|
95 | + case '&': $retVal= ($val1 && $val2); break; |
|
96 | + default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
97 | + } |
|
98 | + if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
99 | 99 | |
100 | - return $retVal; |
|
101 | - } |
|
100 | + return $retVal; |
|
101 | + } |
|
102 | 102 | |
103 | - /** Evaluation : makes token and evaluate. |
|
104 | - * Public function for expressions testing |
|
105 | - * accepts : < > = <= >= != (typec = 0) |
|
106 | - * operators : & | (typec=1) |
|
107 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
108 | - * comparison int vs strings will return null (error) |
|
109 | - * return : bool or null on error |
|
110 | - */ |
|
111 | - public function evaluation($rule,&$item) |
|
112 | - { |
|
113 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
114 | - $negate=$this->check_negate_first($rule, $item); |
|
115 | - // First element : number, string or () |
|
116 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
117 | - //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
103 | + /** Evaluation : makes token and evaluate. |
|
104 | + * Public function for expressions testing |
|
105 | + * accepts : < > = <= >= != (typec = 0) |
|
106 | + * operators : & | (typec=1) |
|
107 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
108 | + * comparison int vs strings will return null (error) |
|
109 | + * return : bool or null on error |
|
110 | + */ |
|
111 | + public function evaluation($rule,&$item) |
|
112 | + { |
|
113 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
114 | + $negate=$this->check_negate_first($rule, $item); |
|
115 | + // First element : number, string or () |
|
116 | + list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
117 | + //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
118 | 118 | |
119 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
120 | - { |
|
121 | - if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
122 | - if ($negate === true) $val1= ! $val1; |
|
123 | - return $val1; |
|
124 | - } |
|
119 | + if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
120 | + { |
|
121 | + if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
122 | + if ($negate === true) $val1= ! $val1; |
|
123 | + return $val1; |
|
124 | + } |
|
125 | 125 | |
126 | - // Second element : operator |
|
127 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
128 | - //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
126 | + // Second element : operator |
|
127 | + list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
128 | + //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
129 | 129 | |
130 | - // Third element : number, string or () |
|
131 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
132 | - { |
|
133 | - $item++; |
|
134 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
135 | - $val2= ! $this->evaluation($rule,$item); |
|
136 | - $type2=2; // result is a boolean |
|
137 | - } |
|
138 | - else |
|
139 | - { |
|
140 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
141 | - } |
|
142 | - //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
130 | + // Third element : number, string or () |
|
131 | + if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
132 | + { |
|
133 | + $item++; |
|
134 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
135 | + $val2= ! $this->evaluation($rule,$item); |
|
136 | + $type2=2; // result is a boolean |
|
137 | + } |
|
138 | + else |
|
139 | + { |
|
140 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
141 | + } |
|
142 | + //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
143 | 143 | |
144 | - if ($type1!=$type2) // cannot compare different types |
|
145 | - { |
|
146 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
147 | - } |
|
148 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
149 | - { |
|
150 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
151 | - } |
|
144 | + if ($type1!=$type2) // cannot compare different types |
|
145 | + { |
|
146 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
147 | + } |
|
148 | + if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
149 | + { |
|
150 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
151 | + } |
|
152 | 152 | |
153 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
153 | + $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
154 | 154 | |
155 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
156 | - // check for logical operator : |
|
157 | - switch ($rule[$item]) |
|
158 | - { |
|
159 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
160 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
155 | + if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
156 | + // check for logical operator : |
|
157 | + switch ($rule[$item]) |
|
158 | + { |
|
159 | + case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
160 | + case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
161 | 161 | |
162 | - default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
163 | - } |
|
164 | - } |
|
162 | + default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
163 | + } |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * Get '*' or '**' and transform in [0-9]+ or .* in return string |
|
168 | - * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
|
169 | - * @return string correct regexp format |
|
170 | - */ |
|
171 | - public function regexp_eval(string &$oid) |
|
172 | - { |
|
173 | - // ** replaced by .* |
|
174 | - $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
175 | - // * replaced by [0-9]+ |
|
176 | - $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
166 | + /** |
|
167 | + * Get '*' or '**' and transform in [0-9]+ or .* in return string |
|
168 | + * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
|
169 | + * @return string correct regexp format |
|
170 | + */ |
|
171 | + public function regexp_eval(string &$oid) |
|
172 | + { |
|
173 | + // ** replaced by .* |
|
174 | + $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
175 | + // * replaced by [0-9]+ |
|
176 | + $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
177 | 177 | |
178 | - // replace * with \* in oid for preg_replace |
|
179 | - $oid=preg_replace('/\*/', '\*', $oid); |
|
178 | + // replace * with \* in oid for preg_replace |
|
179 | + $oid=preg_replace('/\*/', '\*', $oid); |
|
180 | 180 | |
181 | - $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
181 | + $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
182 | 182 | |
183 | - return $oidR; |
|
184 | - } |
|
183 | + return $oidR; |
|
184 | + } |
|
185 | 185 | |
186 | 186 | |
187 | - /** Evaluation rule (uses eval_* functions recursively) |
|
188 | - * @param string $rule : rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
189 | - * @param array $oidList : OIDs values to sustitute. |
|
190 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
191 | - */ |
|
192 | - public function eval_rule($rule,$oidList) |
|
193 | - { |
|
194 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
195 | - { |
|
196 | - return true; |
|
197 | - } |
|
198 | - $matches=array(); |
|
199 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
200 | - { |
|
201 | - $oid=$matches[1]; |
|
202 | - $found=0; |
|
203 | - // Test and transform regexp |
|
204 | - $oidR = $this->regexp_eval($oid); |
|
187 | + /** Evaluation rule (uses eval_* functions recursively) |
|
188 | + * @param string $rule : rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
189 | + * @param array $oidList : OIDs values to sustitute. |
|
190 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
191 | + */ |
|
192 | + public function eval_rule($rule,$oidList) |
|
193 | + { |
|
194 | + if ($rule==null || $rule == '') // Empty rule is always true |
|
195 | + { |
|
196 | + return true; |
|
197 | + } |
|
198 | + $matches=array(); |
|
199 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
200 | + { |
|
201 | + $oid=$matches[1]; |
|
202 | + $found=0; |
|
203 | + // Test and transform regexp |
|
204 | + $oidR = $this->regexp_eval($oid); |
|
205 | 205 | |
206 | - foreach($oidList as $val) |
|
207 | - { |
|
208 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
209 | - { |
|
210 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
211 | - { // If not a number, change " to ' and put " around it |
|
212 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
213 | - $val->value='"'.$val->value.'"'; |
|
214 | - } |
|
215 | - $rep=0; |
|
216 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
217 | - if ($rep==0) |
|
218 | - { |
|
219 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
220 | - return false; |
|
221 | - } |
|
222 | - $found=1; |
|
223 | - break; |
|
224 | - } |
|
225 | - } |
|
226 | - if ($found==0) |
|
227 | - { // OID not found : throw error |
|
228 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
229 | - } |
|
230 | - } |
|
231 | - $item=0; |
|
232 | - $rule=$this->eval_cleanup($rule); |
|
233 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
206 | + foreach($oidList as $val) |
|
207 | + { |
|
208 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
209 | + { |
|
210 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
211 | + { // If not a number, change " to ' and put " around it |
|
212 | + $val->value=preg_replace('/"/',"'",$val->value); |
|
213 | + $val->value='"'.$val->value.'"'; |
|
214 | + } |
|
215 | + $rep=0; |
|
216 | + $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
217 | + if ($rep==0) |
|
218 | + { |
|
219 | + $this->logging->log("Error in rule_eval",WARN,''); |
|
220 | + return false; |
|
221 | + } |
|
222 | + $found=1; |
|
223 | + break; |
|
224 | + } |
|
225 | + } |
|
226 | + if ($found==0) |
|
227 | + { // OID not found : throw error |
|
228 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
229 | + } |
|
230 | + } |
|
231 | + $item=0; |
|
232 | + $rule=$this->eval_cleanup($rule); |
|
233 | + $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
234 | 234 | |
235 | - return $this->evaluation($rule,$item); |
|
236 | - } |
|
235 | + return $this->evaluation($rule,$item); |
|
236 | + } |
|
237 | 237 | |
238 | 238 | } |
239 | 239 | \ No newline at end of file |
@@ -26,14 +26,14 @@ discard block |
||
26 | 26 | } |
27 | 27 | |
28 | 28 | |
29 | - protected function eval_getElement($rule,&$item) |
|
29 | + protected function eval_getElement($rule, &$item) |
|
30 | 30 | { |
31 | 31 | if ($item >= strlen($rule)) |
32 | 32 | { |
33 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
33 | + throw new Exception("Early end of string ".$rule." at ".$item); |
|
34 | 34 | } |
35 | - while ($rule[$item]==' ') $item++; |
|
36 | - if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
35 | + while ($rule[$item] == ' ') $item++; |
|
36 | + if (preg_match('/[\-0-9\.]/', $rule[$item])) |
|
37 | 37 | { // number |
38 | 38 | return $this->get_number($rule, $item); |
39 | 39 | } |
@@ -50,52 +50,52 @@ discard block |
||
50 | 50 | { // function |
51 | 51 | return $this->get_function($rule, $item); |
52 | 52 | } |
53 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
53 | + throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]); |
|
54 | 54 | |
55 | 55 | } |
56 | 56 | |
57 | - protected function eval_getOper($rule,&$item) |
|
57 | + protected function eval_getOper($rule, &$item) |
|
58 | 58 | { |
59 | - while ($rule[$item]==' ') $item++; |
|
59 | + while ($rule[$item] == ' ') $item++; |
|
60 | 60 | switch ($rule[$item]) |
61 | 61 | { |
62 | 62 | case '<': |
63 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
64 | - $item++; return array(0,"<"); |
|
63 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); } |
|
64 | + $item++; return array(0, "<"); |
|
65 | 65 | case '>': |
66 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
67 | - $item++; return array(0,">"); |
|
66 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); } |
|
67 | + $item++; return array(0, ">"); |
|
68 | 68 | case '=': |
69 | - $item++; return array(0,"="); |
|
69 | + $item++; return array(0, "="); |
|
70 | 70 | case '!': |
71 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
72 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
71 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); } |
|
72 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule." at ".$item); |
|
73 | 73 | case '~': |
74 | - $item++; return array(0,"~"); |
|
74 | + $item++; return array(0, "~"); |
|
75 | 75 | case '|': |
76 | - $item++; return array(1,"|"); |
|
76 | + $item++; return array(1, "|"); |
|
77 | 77 | case '&': |
78 | - $item++; return array(1,"&"); |
|
78 | + $item++; return array(1, "&"); |
|
79 | 79 | default : |
80 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
80 | + throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item); |
|
81 | 81 | } |
82 | 82 | } |
83 | 83 | |
84 | - private function do_compare($val1,$val2,$comp,$negate) |
|
84 | + private function do_compare($val1, $val2, $comp, $negate) |
|
85 | 85 | { |
86 | - switch ($comp){ |
|
87 | - case '<': $retVal= ($val1 < $val2); break; |
|
88 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
89 | - case '>': $retVal= ($val1 > $val2); break; |
|
90 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
91 | - case '=': $retVal= ($val1 == $val2); break; |
|
92 | - case '!=': $retVal= ($val1 != $val2); break; |
|
93 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
94 | - case '|': $retVal= ($val1 || $val2); break; |
|
95 | - case '&': $retVal= ($val1 && $val2); break; |
|
86 | + switch ($comp) { |
|
87 | + case '<': $retVal=($val1 < $val2); break; |
|
88 | + case '<=': $retVal=($val1 <= $val2); break; |
|
89 | + case '>': $retVal=($val1 > $val2); break; |
|
90 | + case '>=': $retVal=($val1 >= $val2); break; |
|
91 | + case '=': $retVal=($val1 == $val2); break; |
|
92 | + case '!=': $retVal=($val1 != $val2); break; |
|
93 | + case '~': $retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break; |
|
94 | + case '|': $retVal=($val1 || $val2); break; |
|
95 | + case '&': $retVal=($val1 && $val2); break; |
|
96 | 96 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
97 | 97 | } |
98 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
98 | + if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression |
|
99 | 99 | |
100 | 100 | return $retVal; |
101 | 101 | } |
@@ -108,56 +108,56 @@ discard block |
||
108 | 108 | * comparison int vs strings will return null (error) |
109 | 109 | * return : bool or null on error |
110 | 110 | */ |
111 | - public function evaluation($rule,&$item) |
|
111 | + public function evaluation($rule, &$item) |
|
112 | 112 | { |
113 | 113 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
114 | 114 | $negate=$this->check_negate_first($rule, $item); |
115 | 115 | // First element : number, string or () |
116 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
116 | + list($type1, $val1)=$this->eval_getElement($rule, $item); |
|
117 | 117 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
118 | 118 | |
119 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
119 | + if ($item == strlen($rule)) // If only element, return value, but only boolean |
|
120 | 120 | { |
121 | 121 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
122 | - if ($negate === true) $val1= ! $val1; |
|
122 | + if ($negate === true) $val1=!$val1; |
|
123 | 123 | return $val1; |
124 | 124 | } |
125 | 125 | |
126 | 126 | // Second element : operator |
127 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
127 | + list($typec, $comp)=$this->eval_getOper($rule, $item); |
|
128 | 128 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
129 | 129 | |
130 | 130 | // Third element : number, string or () |
131 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
131 | + if ($rule[$item] == '!') // starts with a ! so evaluate whats next |
|
132 | 132 | { |
133 | 133 | $item++; |
134 | 134 | if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
135 | - $val2= ! $this->evaluation($rule,$item); |
|
135 | + $val2=!$this->evaluation($rule, $item); |
|
136 | 136 | $type2=2; // result is a boolean |
137 | 137 | } |
138 | 138 | else |
139 | 139 | { |
140 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
140 | + list($type2, $val2)=$this->eval_getElement($rule, $item); |
|
141 | 141 | } |
142 | 142 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
143 | 143 | |
144 | - if ($type1!=$type2) // cannot compare different types |
|
144 | + if ($type1 != $type2) // cannot compare different types |
|
145 | 145 | { |
146 | 146 | throw new Exception("Cannot compare string & number : ".$rule); |
147 | 147 | } |
148 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
148 | + if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number |
|
149 | 149 | { |
150 | 150 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
151 | 151 | } |
152 | 152 | |
153 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
153 | + $retVal=$this->do_compare($val1, $val2, $comp, $negate); |
|
154 | 154 | |
155 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
155 | + if ($item == strlen($rule)) return $retVal; // End of string : return evaluation |
|
156 | 156 | // check for logical operator : |
157 | 157 | switch ($rule[$item]) |
158 | 158 | { |
159 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
160 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
159 | + case '|': $item++; return ($retVal || $this->evaluation($rule, $item)); |
|
160 | + case '&': $item++; return ($retVal && $this->evaluation($rule, $item)); |
|
161 | 161 | |
162 | 162 | default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
163 | 163 | } |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | // replace * with \* in oid for preg_replace |
179 | 179 | $oid=preg_replace('/\*/', '\*', $oid); |
180 | 180 | |
181 | - $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
181 | + $this->logging->log('Regexp eval : '.$oid.' / '.$oidR, DEBUG); |
|
182 | 182 | |
183 | 183 | return $oidR; |
184 | 184 | } |
@@ -189,50 +189,50 @@ discard block |
||
189 | 189 | * @param array $oidList : OIDs values to sustitute. |
190 | 190 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
191 | 191 | */ |
192 | - public function eval_rule($rule,$oidList) |
|
192 | + public function eval_rule($rule, $oidList) |
|
193 | 193 | { |
194 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
194 | + if ($rule == null || $rule == '') // Empty rule is always true |
|
195 | 195 | { |
196 | 196 | return true; |
197 | 197 | } |
198 | 198 | $matches=array(); |
199 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
199 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1) |
|
200 | 200 | { |
201 | 201 | $oid=$matches[1]; |
202 | 202 | $found=0; |
203 | 203 | // Test and transform regexp |
204 | - $oidR = $this->regexp_eval($oid); |
|
204 | + $oidR=$this->regexp_eval($oid); |
|
205 | 205 | |
206 | - foreach($oidList as $val) |
|
206 | + foreach ($oidList as $val) |
|
207 | 207 | { |
208 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
208 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
209 | 209 | { |
210 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
210 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/', $val->value)) |
|
211 | 211 | { // If not a number, change " to ' and put " around it |
212 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
212 | + $val->value=preg_replace('/"/', "'", $val->value); |
|
213 | 213 | $val->value='"'.$val->value.'"'; |
214 | 214 | } |
215 | 215 | $rep=0; |
216 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
217 | - if ($rep==0) |
|
216 | + $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep); |
|
217 | + if ($rep == 0) |
|
218 | 218 | { |
219 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
219 | + $this->logging->log("Error in rule_eval", WARN, ''); |
|
220 | 220 | return false; |
221 | 221 | } |
222 | 222 | $found=1; |
223 | 223 | break; |
224 | 224 | } |
225 | 225 | } |
226 | - if ($found==0) |
|
226 | + if ($found == 0) |
|
227 | 227 | { // OID not found : throw error |
228 | 228 | throw new Exception('OID '.$oid.' not found in trap'); |
229 | 229 | } |
230 | 230 | } |
231 | 231 | $item=0; |
232 | 232 | $rule=$this->eval_cleanup($rule); |
233 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
233 | + $this->logging->log('Rule after clenup: '.$rule, INFO); |
|
234 | 234 | |
235 | - return $this->evaluation($rule,$item); |
|
235 | + return $this->evaluation($rule, $item); |
|
236 | 236 | } |
237 | 237 | |
238 | 238 | } |
239 | 239 | \ No newline at end of file |
@@ -32,7 +32,9 @@ discard block |
||
32 | 32 | { |
33 | 33 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
34 | 34 | } |
35 | - while ($rule[$item]==' ') $item++; |
|
35 | + while ($rule[$item]==' ') { |
|
36 | + $item++; |
|
37 | + } |
|
36 | 38 | if (preg_match('/[\-0-9\.]/',$rule[$item])) |
37 | 39 | { // number |
38 | 40 | return $this->get_number($rule, $item); |
@@ -56,7 +58,9 @@ discard block |
||
56 | 58 | |
57 | 59 | protected function eval_getOper($rule,&$item) |
58 | 60 | { |
59 | - while ($rule[$item]==' ') $item++; |
|
61 | + while ($rule[$item]==' ') { |
|
62 | + $item++; |
|
63 | + } |
|
60 | 64 | switch ($rule[$item]) |
61 | 65 | { |
62 | 66 | case '<': |
@@ -95,7 +99,10 @@ discard block |
||
95 | 99 | case '&': $retVal= ($val1 && $val2); break; |
96 | 100 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
97 | 101 | } |
98 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
102 | + if ($negate === true) { |
|
103 | + $retVal = ! $retVal; |
|
104 | + } |
|
105 | + // Inverse result if negate before expression |
|
99 | 106 | |
100 | 107 | return $retVal; |
101 | 108 | } |
@@ -116,10 +123,14 @@ discard block |
||
116 | 123 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
117 | 124 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
118 | 125 | |
119 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
126 | + if ($item==strlen($rule)) { |
|
127 | + // If only element, return value, but only boolean |
|
120 | 128 | { |
121 | 129 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
122 | - if ($negate === true) $val1= ! $val1; |
|
130 | + } |
|
131 | + if ($negate === true) { |
|
132 | + $val1= ! $val1; |
|
133 | + } |
|
123 | 134 | return $val1; |
124 | 135 | } |
125 | 136 | |
@@ -128,31 +139,41 @@ discard block |
||
128 | 139 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
129 | 140 | |
130 | 141 | // Third element : number, string or () |
131 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
142 | + if ( $rule[$item] == '!') { |
|
143 | + // starts with a ! so evaluate whats next |
|
132 | 144 | { |
133 | 145 | $item++; |
134 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
146 | + } |
|
147 | + if ($typec != 1) { |
|
148 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
149 | + } |
|
135 | 150 | $val2= ! $this->evaluation($rule,$item); |
136 | 151 | $type2=2; // result is a boolean |
137 | - } |
|
138 | - else |
|
152 | + } else |
|
139 | 153 | { |
140 | 154 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
141 | 155 | } |
142 | 156 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
143 | 157 | |
144 | - if ($type1!=$type2) // cannot compare different types |
|
158 | + if ($type1!=$type2) { |
|
159 | + // cannot compare different types |
|
145 | 160 | { |
146 | 161 | throw new Exception("Cannot compare string & number : ".$rule); |
147 | 162 | } |
148 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
163 | + } |
|
164 | + if ($typec==1 && $type1 !=2) { |
|
165 | + // cannot use & or | with string/number |
|
149 | 166 | { |
150 | 167 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
151 | 168 | } |
169 | + } |
|
152 | 170 | |
153 | 171 | $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
154 | 172 | |
155 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
173 | + if ($item==strlen($rule)) { |
|
174 | + return $retVal; |
|
175 | + } |
|
176 | + // End of string : return evaluation |
|
156 | 177 | // check for logical operator : |
157 | 178 | switch ($rule[$item]) |
158 | 179 | { |
@@ -191,10 +212,12 @@ discard block |
||
191 | 212 | */ |
192 | 213 | public function eval_rule($rule,$oidList) |
193 | 214 | { |
194 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
215 | + if ($rule==null || $rule == '') { |
|
216 | + // Empty rule is always true |
|
195 | 217 | { |
196 | 218 | return true; |
197 | 219 | } |
220 | + } |
|
198 | 221 | $matches=array(); |
199 | 222 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
200 | 223 | { |
@@ -33,15 +33,13 @@ |
||
33 | 33 | |
34 | 34 | $trap->add_rule_final(microtime(true) - $time1); |
35 | 35 | |
36 | -} |
|
37 | -catch (Exception $e) |
|
36 | +} catch (Exception $e) |
|
38 | 37 | { |
39 | 38 | if ($trap == null) |
40 | 39 | { // Exception in trap creation : log in display & syslog |
41 | 40 | $logging = new Logging(); |
42 | 41 | $logging->log("Caught exception creating Trap class",2); |
43 | - } |
|
44 | - else |
|
42 | + } else |
|
45 | 43 | { |
46 | 44 | $trap->logging->log("Exception : ". $e->getMessage(),2,0); |
47 | 45 | } |
@@ -17,13 +17,13 @@ discard block |
||
17 | 17 | try |
18 | 18 | { |
19 | 19 | |
20 | - $trap = new Trap($icingaweb2Etc); |
|
21 | - //$trap = new Trap($icingaweb2Etc,4,'display'); // For debug |
|
22 | - //$trap = new Trap($icingaweb2Etc,4,'syslog'); // For debug |
|
23 | - //$trap->setLogging(4,'syslog'); |
|
20 | + $trap = new Trap($icingaweb2Etc); |
|
21 | + //$trap = new Trap($icingaweb2Etc,4,'display'); // For debug |
|
22 | + //$trap = new Trap($icingaweb2Etc,4,'syslog'); // For debug |
|
23 | + //$trap->setLogging(4,'syslog'); |
|
24 | 24 | |
25 | - // TODO : tranfer this to reset_trap cli command |
|
26 | - $trap->eraseOldTraps(); |
|
25 | + // TODO : tranfer this to reset_trap cli command |
|
26 | + $trap->eraseOldTraps(); |
|
27 | 27 | |
28 | 28 | $trap->read_trap('php://stdin'); |
29 | 29 | |
@@ -36,15 +36,15 @@ discard block |
||
36 | 36 | } |
37 | 37 | catch (Exception $e) |
38 | 38 | { |
39 | - if ($trap == null) |
|
40 | - { // Exception in trap creation : log in display & syslog |
|
41 | - $logging = new Logging(); |
|
42 | - $logging->log("Caught exception creating Trap class",2); |
|
43 | - } |
|
44 | - else |
|
45 | - { |
|
39 | + if ($trap == null) |
|
40 | + { // Exception in trap creation : log in display & syslog |
|
41 | + $logging = new Logging(); |
|
42 | + $logging->log("Caught exception creating Trap class",2); |
|
43 | + } |
|
44 | + else |
|
45 | + { |
|
46 | 46 | $trap->logging->log("Exception : ". $e->getMessage(),2,0); |
47 | - } |
|
47 | + } |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | //end |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | use Trapdirector\Trap; |
5 | 5 | |
6 | 6 | // start |
7 | -$time1 = microtime(true); |
|
7 | +$time1=microtime(true); |
|
8 | 8 | |
9 | 9 | require_once ('trap_class.php'); |
10 | 10 | |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | try |
18 | 18 | { |
19 | 19 | |
20 | - $trap = new Trap($icingaweb2Etc); |
|
20 | + $trap=new Trap($icingaweb2Etc); |
|
21 | 21 | //$trap = new Trap($icingaweb2Etc,4,'display'); // For debug |
22 | 22 | //$trap = new Trap($icingaweb2Etc,4,'syslog'); // For debug |
23 | 23 | //$trap->setLogging(4,'syslog'); |
@@ -38,12 +38,12 @@ discard block |
||
38 | 38 | { |
39 | 39 | if ($trap == null) |
40 | 40 | { // Exception in trap creation : log in display & syslog |
41 | - $logging = new Logging(); |
|
42 | - $logging->log("Caught exception creating Trap class",2); |
|
41 | + $logging=new Logging(); |
|
42 | + $logging->log("Caught exception creating Trap class", 2); |
|
43 | 43 | } |
44 | 44 | else |
45 | 45 | { |
46 | - $trap->logging->log("Exception : ". $e->getMessage(),2,0); |
|
46 | + $trap->logging->log("Exception : ".$e->getMessage(), 2, 0); |
|
47 | 47 | } |
48 | 48 | } |
49 | 49 |
@@ -22,77 +22,77 @@ discard block |
||
22 | 22 | class MibCommand extends Command |
23 | 23 | { |
24 | 24 | /** |
25 | - * Update mib database |
|
26 | - * |
|
27 | - * USAGE |
|
28 | - * |
|
29 | - * icingli trapdirector mib update |
|
30 | - * |
|
31 | - * OPTIONS |
|
32 | - * |
|
33 | - * --pid <file> : run in background with pid in <file> |
|
34 | - * |
|
35 | - * --verb : Set output log to verbose |
|
36 | - * |
|
37 | - * --force-check : force check of all traps & objects for change. (NOT IMPLEMENTED) |
|
38 | - */ |
|
25 | + * Update mib database |
|
26 | + * |
|
27 | + * USAGE |
|
28 | + * |
|
29 | + * icingli trapdirector mib update |
|
30 | + * |
|
31 | + * OPTIONS |
|
32 | + * |
|
33 | + * --pid <file> : run in background with pid in <file> |
|
34 | + * |
|
35 | + * --verb : Set output log to verbose |
|
36 | + * |
|
37 | + * --force-check : force check of all traps & objects for change. (NOT IMPLEMENTED) |
|
38 | + */ |
|
39 | 39 | public function updateAction() |
40 | 40 | { |
41 | - $background = $this->params->get('pid', null); |
|
42 | - $logLevel= $this->params->has('verb') ? 4 : 2; |
|
43 | - if ($this->params->has('force-check')) { echo "Not implemented"; return;} |
|
44 | - $forceCheck=$this->params->has('force-check')?True:False; |
|
45 | - $pid=1; |
|
46 | - if ($background != null) |
|
47 | - { |
|
48 | - $file=@fopen($background,'w'); |
|
49 | - if ($file == false) |
|
50 | - { |
|
51 | - echo 'Error : cannot open pid file '.$background; |
|
52 | - return 1; |
|
53 | - } |
|
54 | - $pid = pcntl_fork(); |
|
55 | - if ($pid == -1) { |
|
56 | - echo 'Error : Cannot fork process'; |
|
57 | - return 1; |
|
58 | - } |
|
59 | - } |
|
60 | - $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
|
41 | + $background = $this->params->get('pid', null); |
|
42 | + $logLevel= $this->params->has('verb') ? 4 : 2; |
|
43 | + if ($this->params->has('force-check')) { echo "Not implemented"; return;} |
|
44 | + $forceCheck=$this->params->has('force-check')?True:False; |
|
45 | + $pid=1; |
|
46 | + if ($background != null) |
|
47 | + { |
|
48 | + $file=@fopen($background,'w'); |
|
49 | + if ($file == false) |
|
50 | + { |
|
51 | + echo 'Error : cannot open pid file '.$background; |
|
52 | + return 1; |
|
53 | + } |
|
54 | + $pid = pcntl_fork(); |
|
55 | + if ($pid == -1) { |
|
56 | + echo 'Error : Cannot fork process'; |
|
57 | + return 1; |
|
58 | + } |
|
59 | + } |
|
60 | + $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
|
61 | 61 | require_once($module->getBaseDir() .'/bin/trap_class.php'); |
62 | 62 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
63 | 63 | $trap = new Trap($icingaweb2_etc); |
64 | 64 | if ($pid == 1) |
65 | 65 | { |
66 | - $trap->setLogging($logLevel,'display'); |
|
66 | + $trap->setLogging($logLevel,'display'); |
|
67 | 67 | } |
68 | 68 | else |
69 | 69 | { // use default display TODO : if default is 'display' son process will be killed at first output.... |
70 | - if ($pid != 0) |
|
71 | - { |
|
72 | - // father process |
|
73 | - fwrite($file,$pid); |
|
74 | - fclose($file); |
|
75 | - echo "OK : process $pid in bckground"; |
|
76 | - return 0; |
|
77 | - } |
|
78 | - else |
|
79 | - { // son process : close all file descriptors and go to a new session |
|
80 | - fclose($file); |
|
70 | + if ($pid != 0) |
|
71 | + { |
|
72 | + // father process |
|
73 | + fwrite($file,$pid); |
|
74 | + fclose($file); |
|
75 | + echo "OK : process $pid in bckground"; |
|
76 | + return 0; |
|
77 | + } |
|
78 | + else |
|
79 | + { // son process : close all file descriptors and go to a new session |
|
80 | + fclose($file); |
|
81 | 81 | // $sid = posix_setsid(); |
82 | - fclose(STDIN); |
|
83 | - fclose(STDOUT); |
|
84 | - fclose(STDERR); |
|
85 | - try |
|
86 | - { |
|
87 | - $trap->mibClass->update_mib_database(false,$forceCheck); |
|
88 | - } |
|
89 | - catch (Exception $e) |
|
90 | - { |
|
91 | - $trap->logging->log('Error in updating : ' . $e->getMessage(),2); |
|
92 | - } |
|
93 | - unlink($background); |
|
94 | - return 0; |
|
95 | - } |
|
82 | + fclose(STDIN); |
|
83 | + fclose(STDOUT); |
|
84 | + fclose(STDERR); |
|
85 | + try |
|
86 | + { |
|
87 | + $trap->mibClass->update_mib_database(false,$forceCheck); |
|
88 | + } |
|
89 | + catch (Exception $e) |
|
90 | + { |
|
91 | + $trap->logging->log('Error in updating : ' . $e->getMessage(),2); |
|
92 | + } |
|
93 | + unlink($background); |
|
94 | + return 0; |
|
95 | + } |
|
96 | 96 | |
97 | 97 | } |
98 | 98 | |
@@ -110,28 +110,28 @@ discard block |
||
110 | 110 | } |
111 | 111 | if ($pid != 1) |
112 | 112 | { |
113 | - unlink($background); |
|
113 | + unlink($background); |
|
114 | 114 | } |
115 | 115 | } |
116 | 116 | /** |
117 | - * purge all mib database NOT IMPLEMENTED |
|
118 | - * |
|
119 | - * USAGE |
|
120 | - * |
|
121 | - * icingli trapdirector mib purge --confirm |
|
122 | - * |
|
123 | - * OPTIONS |
|
124 | - * |
|
125 | - * --confirm : needed to execute purge |
|
126 | - */ |
|
117 | + * purge all mib database NOT IMPLEMENTED |
|
118 | + * |
|
119 | + * USAGE |
|
120 | + * |
|
121 | + * icingli trapdirector mib purge --confirm |
|
122 | + * |
|
123 | + * OPTIONS |
|
124 | + * |
|
125 | + * --confirm : needed to execute purge |
|
126 | + */ |
|
127 | 127 | public function purgeAction() |
128 | 128 | { |
129 | 129 | $db_prefix=$this->Config()->get('config', 'database_prefix'); |
130 | 130 | |
131 | 131 | if (!$this->params->has('confirm')) |
132 | 132 | { |
133 | - echo "This needs confirmation with '--confirm'\n"; |
|
134 | - return; |
|
133 | + echo "This needs confirmation with '--confirm'\n"; |
|
134 | + return; |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | $Config = new TrapModuleConfig($db_prefix); |
@@ -144,9 +144,9 @@ discard block |
||
144 | 144 | $db = IcingaDbConnection::fromResourceName($dbresource)->getConnection(); |
145 | 145 | |
146 | 146 | $query = $db->delete( |
147 | - $Config->getMIBCacheTableName(), |
|
148 | - 'id>0'); |
|
149 | - echo 'Deleted '. $query . " traps and objects\n"; |
|
147 | + $Config->getMIBCacheTableName(), |
|
148 | + 'id>0'); |
|
149 | + echo 'Deleted '. $query . " traps and objects\n"; |
|
150 | 150 | } |
151 | 151 | catch (Exception $e) |
152 | 152 | { |
@@ -38,39 +38,39 @@ discard block |
||
38 | 38 | */ |
39 | 39 | public function updateAction() |
40 | 40 | { |
41 | - $background = $this->params->get('pid', null); |
|
42 | - $logLevel= $this->params->has('verb') ? 4 : 2; |
|
43 | - if ($this->params->has('force-check')) { echo "Not implemented"; return;} |
|
44 | - $forceCheck=$this->params->has('force-check')?True:False; |
|
41 | + $background=$this->params->get('pid', null); |
|
42 | + $logLevel=$this->params->has('verb') ? 4 : 2; |
|
43 | + if ($this->params->has('force-check')) { echo "Not implemented"; return; } |
|
44 | + $forceCheck=$this->params->has('force-check') ?True:False; |
|
45 | 45 | $pid=1; |
46 | 46 | if ($background != null) |
47 | 47 | { |
48 | - $file=@fopen($background,'w'); |
|
48 | + $file=@fopen($background, 'w'); |
|
49 | 49 | if ($file == false) |
50 | 50 | { |
51 | 51 | echo 'Error : cannot open pid file '.$background; |
52 | 52 | return 1; |
53 | 53 | } |
54 | - $pid = pcntl_fork(); |
|
54 | + $pid=pcntl_fork(); |
|
55 | 55 | if ($pid == -1) { |
56 | 56 | echo 'Error : Cannot fork process'; |
57 | 57 | return 1; |
58 | 58 | } |
59 | 59 | } |
60 | 60 | $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
61 | - require_once($module->getBaseDir() .'/bin/trap_class.php'); |
|
61 | + require_once($module->getBaseDir().'/bin/trap_class.php'); |
|
62 | 62 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
63 | - $trap = new Trap($icingaweb2_etc); |
|
63 | + $trap=new Trap($icingaweb2_etc); |
|
64 | 64 | if ($pid == 1) |
65 | 65 | { |
66 | - $trap->setLogging($logLevel,'display'); |
|
66 | + $trap->setLogging($logLevel, 'display'); |
|
67 | 67 | } |
68 | 68 | else |
69 | 69 | { // use default display TODO : if default is 'display' son process will be killed at first output.... |
70 | 70 | if ($pid != 0) |
71 | 71 | { |
72 | 72 | // father process |
73 | - fwrite($file,$pid); |
|
73 | + fwrite($file, $pid); |
|
74 | 74 | fclose($file); |
75 | 75 | echo "OK : process $pid in bckground"; |
76 | 76 | return 0; |
@@ -84,11 +84,11 @@ discard block |
||
84 | 84 | fclose(STDERR); |
85 | 85 | try |
86 | 86 | { |
87 | - $trap->mibClass->update_mib_database(false,$forceCheck); |
|
87 | + $trap->mibClass->update_mib_database(false, $forceCheck); |
|
88 | 88 | } |
89 | 89 | catch (Exception $e) |
90 | 90 | { |
91 | - $trap->logging->log('Error in updating : ' . $e->getMessage(),2); |
|
91 | + $trap->logging->log('Error in updating : '.$e->getMessage(), 2); |
|
92 | 92 | } |
93 | 93 | unlink($background); |
94 | 94 | return 0; |
@@ -100,13 +100,13 @@ discard block |
||
100 | 100 | { |
101 | 101 | echo "Update main mib database : \n"; |
102 | 102 | echo "# (trap found) C (trap already processed) . (every 2 seconds) : \n"; |
103 | - $trap->mibClass->update_mib_database(true,$forceCheck); |
|
103 | + $trap->mibClass->update_mib_database(true, $forceCheck); |
|
104 | 104 | echo "Done\n"; |
105 | 105 | |
106 | 106 | } |
107 | 107 | catch (Exception $e) |
108 | 108 | { |
109 | - echo 'Error in updating : ' . $e->getMessage(); |
|
109 | + echo 'Error in updating : '.$e->getMessage(); |
|
110 | 110 | } |
111 | 111 | if ($pid != 1) |
112 | 112 | { |
@@ -134,23 +134,23 @@ discard block |
||
134 | 134 | return; |
135 | 135 | } |
136 | 136 | |
137 | - $Config = new TrapModuleConfig($db_prefix); |
|
137 | + $Config=new TrapModuleConfig($db_prefix); |
|
138 | 138 | |
139 | 139 | try |
140 | 140 | { |
141 | 141 | |
142 | 142 | $dbresource=$this->Config()->get('config', 'database'); |
143 | 143 | echo "DB name : $dbresource\n"; |
144 | - $db = IcingaDbConnection::fromResourceName($dbresource)->getConnection(); |
|
144 | + $db=IcingaDbConnection::fromResourceName($dbresource)->getConnection(); |
|
145 | 145 | |
146 | - $query = $db->delete( |
|
146 | + $query=$db->delete( |
|
147 | 147 | $Config->getMIBCacheTableName(), |
148 | 148 | 'id>0'); |
149 | - echo 'Deleted '. $query . " traps and objects\n"; |
|
149 | + echo 'Deleted '.$query." traps and objects\n"; |
|
150 | 150 | } |
151 | 151 | catch (Exception $e) |
152 | 152 | { |
153 | - echo 'Error in DB : ' . $e->getMessage(); |
|
153 | + echo 'Error in DB : '.$e->getMessage(); |
|
154 | 154 | } |
155 | 155 | } |
156 | 156 |
@@ -64,8 +64,7 @@ discard block |
||
64 | 64 | if ($pid == 1) |
65 | 65 | { |
66 | 66 | $trap->setLogging($logLevel,'display'); |
67 | - } |
|
68 | - else |
|
67 | + } else |
|
69 | 68 | { // use default display TODO : if default is 'display' son process will be killed at first output.... |
70 | 69 | if ($pid != 0) |
71 | 70 | { |
@@ -74,8 +73,7 @@ discard block |
||
74 | 73 | fclose($file); |
75 | 74 | echo "OK : process $pid in bckground"; |
76 | 75 | return 0; |
77 | - } |
|
78 | - else |
|
76 | + } else |
|
79 | 77 | { // son process : close all file descriptors and go to a new session |
80 | 78 | fclose($file); |
81 | 79 | // $sid = posix_setsid(); |
@@ -85,8 +83,7 @@ discard block |
||
85 | 83 | try |
86 | 84 | { |
87 | 85 | $trap->mibClass->update_mib_database(false,$forceCheck); |
88 | - } |
|
89 | - catch (Exception $e) |
|
86 | + } catch (Exception $e) |
|
90 | 87 | { |
91 | 88 | $trap->logging->log('Error in updating : ' . $e->getMessage(),2); |
92 | 89 | } |
@@ -103,8 +100,7 @@ discard block |
||
103 | 100 | $trap->mibClass->update_mib_database(true,$forceCheck); |
104 | 101 | echo "Done\n"; |
105 | 102 | |
106 | - } |
|
107 | - catch (Exception $e) |
|
103 | + } catch (Exception $e) |
|
108 | 104 | { |
109 | 105 | echo 'Error in updating : ' . $e->getMessage(); |
110 | 106 | } |
@@ -147,8 +143,7 @@ discard block |
||
147 | 143 | $Config->getMIBCacheTableName(), |
148 | 144 | 'id>0'); |
149 | 145 | echo 'Deleted '. $query . " traps and objects\n"; |
150 | - } |
|
151 | - catch (Exception $e) |
|
146 | + } catch (Exception $e) |
|
152 | 147 | { |
153 | 148 | echo 'Error in DB : ' . $e->getMessage(); |
154 | 149 | } |
@@ -52,8 +52,7 @@ discard block |
||
52 | 52 | $this->Config()->saveIni(); |
53 | 53 | $this->view->configErrorDetected='Configuration is empty : you can run install script with parameters (see Automatic installation below)'; |
54 | 54 | //$emptyConfig=1; |
55 | - } |
|
56 | - catch (Exception $e) |
|
55 | + } catch (Exception $e) |
|
57 | 56 | { |
58 | 57 | $this->view->configErrorDetected=$e->getMessage(); |
59 | 58 | } |
@@ -141,8 +140,7 @@ discard block |
||
141 | 140 | $this->view->apimessage='API config : ' . $e->getMessage(); |
142 | 141 | $this->view->apimessageError=true; |
143 | 142 | } |
144 | - } |
|
145 | - else |
|
143 | + } else |
|
146 | 144 | { |
147 | 145 | $this->view->apimessage='API parameters not configured'; |
148 | 146 | $this->view->apimessageError=true; |
@@ -208,7 +206,9 @@ discard block |
||
208 | 206 | $input="154865134987aaaa"; |
209 | 207 | exec("$phpBin -r \"echo '$input';\"",$output,$retCode); |
210 | 208 | |
211 | - if (! isset($output[0])) $output[0]="NO OUT"; |
|
209 | + if (! isset($output[0])) { |
|
210 | + $output[0]="NO OUT"; |
|
211 | + } |
|
212 | 212 | |
213 | 213 | if ($retCode == 0 && preg_match("/$input/",$output[0]) == 1) |
214 | 214 | { |
@@ -310,8 +310,7 @@ discard block |
||
310 | 310 | $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
311 | 311 | printf('Schema already exists'); |
312 | 312 | |
313 | - } |
|
314 | - catch (DBException $e) |
|
313 | + } catch (DBException $e) |
|
315 | 314 | { |
316 | 315 | |
317 | 316 | printf('Creating schema : <br>'); |
@@ -367,8 +366,7 @@ discard block |
||
367 | 366 | $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
368 | 367 | echo 'Schema already exists and is up to date<br>'; |
369 | 368 | return; |
370 | - } |
|
371 | - catch (DBException $e) |
|
369 | + } catch (DBException $e) |
|
372 | 370 | { |
373 | 371 | $dberror=$e->getArray(); |
374 | 372 | } |
@@ -452,16 +450,18 @@ discard block |
||
452 | 450 | } |
453 | 451 | // Assume there is only one line... TODO : see if there is a better way to do this |
454 | 452 | $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
455 | - if (!preg_match('/-n/',$line)) |
|
456 | - return array(1,'snmptrapd has no -n option : '.$line); |
|
457 | - if (!preg_match('/-O[^ ]*n/',$line)) |
|
458 | - return array(1,'snmptrapd has no -On option : '.$line); |
|
459 | - if (!preg_match('/-O[^ ]*e/',$line)) |
|
460 | - return array(1,'snmptrapd has no -Oe option : '.$line); |
|
453 | + if (!preg_match('/-n/',$line)) { |
|
454 | + return array(1,'snmptrapd has no -n option : '.$line); |
|
455 | + } |
|
456 | + if (!preg_match('/-O[^ ]*n/',$line)) { |
|
457 | + return array(1,'snmptrapd has no -On option : '.$line); |
|
458 | + } |
|
459 | + if (!preg_match('/-O[^ ]*e/',$line)) { |
|
460 | + return array(1,'snmptrapd has no -Oe option : '.$line); |
|
461 | + } |
|
461 | 462 | |
462 | 463 | return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
463 | - } |
|
464 | - else |
|
464 | + } else |
|
465 | 465 | { |
466 | 466 | return array(0,'A daemon (hidden by SELinux) is listening on UDP/162'); |
467 | 467 | } |
@@ -25,16 +25,16 @@ discard block |
||
25 | 25 | */ |
26 | 26 | private function get_param() |
27 | 27 | { |
28 | - $dberrorMsg=$this->params->get('dberror'); |
|
29 | - if ($dberrorMsg != '') |
|
30 | - { |
|
31 | - $this->view->errorDetected=$dberrorMsg; |
|
32 | - } |
|
33 | - $dberrorMsg=$this->params->get('idodberror'); |
|
34 | - if ($dberrorMsg != '') |
|
35 | - { |
|
36 | - $this->view->errorDetected=$dberrorMsg; |
|
37 | - } |
|
28 | + $dberrorMsg=$this->params->get('dberror'); |
|
29 | + if ($dberrorMsg != '') |
|
30 | + { |
|
31 | + $this->view->errorDetected=$dberrorMsg; |
|
32 | + } |
|
33 | + $dberrorMsg=$this->params->get('idodberror'); |
|
34 | + if ($dberrorMsg != '') |
|
35 | + { |
|
36 | + $this->view->errorDetected=$dberrorMsg; |
|
37 | + } |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -43,22 +43,22 @@ discard block |
||
43 | 43 | */ |
44 | 44 | private function check_empty_config() |
45 | 45 | { |
46 | - $this->view->configErrorDetected == NULL; // Displayed error on various conifugration errors. |
|
47 | - if ($this->Config()->isEmpty() == true) |
|
48 | - { |
|
49 | - $this->Config()->setSection('config'); // Set base config section. |
|
50 | - try |
|
51 | - { |
|
52 | - $this->Config()->saveIni(); |
|
53 | - $this->view->configErrorDetected='Configuration is empty : you can run install script with parameters (see Automatic installation below)'; |
|
54 | - //$emptyConfig=1; |
|
55 | - } |
|
56 | - catch (Exception $e) |
|
57 | - { |
|
58 | - $this->view->configErrorDetected=$e->getMessage(); |
|
59 | - } |
|
46 | + $this->view->configErrorDetected == NULL; // Displayed error on various conifugration errors. |
|
47 | + if ($this->Config()->isEmpty() == true) |
|
48 | + { |
|
49 | + $this->Config()->setSection('config'); // Set base config section. |
|
50 | + try |
|
51 | + { |
|
52 | + $this->Config()->saveIni(); |
|
53 | + $this->view->configErrorDetected='Configuration is empty : you can run install script with parameters (see Automatic installation below)'; |
|
54 | + //$emptyConfig=1; |
|
55 | + } |
|
56 | + catch (Exception $e) |
|
57 | + { |
|
58 | + $this->view->configErrorDetected=$e->getMessage(); |
|
59 | + } |
|
60 | 60 | |
61 | - } |
|
61 | + } |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | /** |
@@ -71,56 +71,56 @@ discard block |
||
71 | 71 | */ |
72 | 72 | private function check_db() |
73 | 73 | { |
74 | - $db_message=array( // index => ( message OK, message NOK, optional link if NOK ) |
|
75 | - 0 => array('Database configuration OK','',''), |
|
76 | - 1 => array('Database set in config.ini','No database in config.ini',''), |
|
77 | - 2 => array('Database exists in Icingaweb2 config','Database does not exist in Icingaweb2 : ', |
|
78 | - Url::fromPath('config/resource')), |
|
79 | - 3 => array('Database credentials OK','Database does not exist/invalid credentials/no schema : ', |
|
80 | - Url::fromPath('trapdirector/settings/createschema')), |
|
81 | - 4 => array('Schema is set','Schema is not set for ', |
|
82 | - Url::fromPath('trapdirector/settings/createschema')), |
|
83 | - 5 => array('Schema is up to date','Schema is outdated :', |
|
84 | - Url::fromPath('trapdirector/settings/updateschema')), |
|
85 | - ); |
|
74 | + $db_message=array( // index => ( message OK, message NOK, optional link if NOK ) |
|
75 | + 0 => array('Database configuration OK','',''), |
|
76 | + 1 => array('Database set in config.ini','No database in config.ini',''), |
|
77 | + 2 => array('Database exists in Icingaweb2 config','Database does not exist in Icingaweb2 : ', |
|
78 | + Url::fromPath('config/resource')), |
|
79 | + 3 => array('Database credentials OK','Database does not exist/invalid credentials/no schema : ', |
|
80 | + Url::fromPath('trapdirector/settings/createschema')), |
|
81 | + 4 => array('Schema is set','Schema is not set for ', |
|
82 | + Url::fromPath('trapdirector/settings/createschema')), |
|
83 | + 5 => array('Schema is up to date','Schema is outdated :', |
|
84 | + Url::fromPath('trapdirector/settings/updateschema')), |
|
85 | + ); |
|
86 | 86 | |
87 | - try { |
|
88 | - $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
89 | - $dberror=array(0,''); |
|
90 | - } catch (DBException $e) { |
|
91 | - $dberror = $e->getArray(); |
|
92 | - } |
|
87 | + try { |
|
88 | + $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
89 | + $dberror=array(0,''); |
|
90 | + } catch (DBException $e) { |
|
91 | + $dberror = $e->getArray(); |
|
92 | + } |
|
93 | 93 | |
94 | - $this->view->db_error=$dberror[0]; |
|
95 | - switch ($dberror[0]) |
|
96 | - { |
|
97 | - case 2: |
|
98 | - case 4: |
|
99 | - $db_message[$dberror[0]][1] .= $dberror[1]; |
|
100 | - break; |
|
101 | - case 3: |
|
102 | - $db_message[$dberror[0]][1] .= $dberror[1] . ', Message : ' . $dberror[2]; |
|
103 | - break; |
|
104 | - case 5: |
|
105 | - $db_message[$dberror[0]][1] .= ' version '. $dberror[1] . ', version needed : ' .$dberror[2]; |
|
106 | - break; |
|
107 | - case 0: |
|
108 | - case 1: |
|
109 | - break; |
|
110 | - default: |
|
111 | - new ProgrammingError('Out of bond result from database test'); |
|
112 | - } |
|
113 | - $this->view->message=$db_message; |
|
94 | + $this->view->db_error=$dberror[0]; |
|
95 | + switch ($dberror[0]) |
|
96 | + { |
|
97 | + case 2: |
|
98 | + case 4: |
|
99 | + $db_message[$dberror[0]][1] .= $dberror[1]; |
|
100 | + break; |
|
101 | + case 3: |
|
102 | + $db_message[$dberror[0]][1] .= $dberror[1] . ', Message : ' . $dberror[2]; |
|
103 | + break; |
|
104 | + case 5: |
|
105 | + $db_message[$dberror[0]][1] .= ' version '. $dberror[1] . ', version needed : ' .$dberror[2]; |
|
106 | + break; |
|
107 | + case 0: |
|
108 | + case 1: |
|
109 | + break; |
|
110 | + default: |
|
111 | + new ProgrammingError('Out of bond result from database test'); |
|
112 | + } |
|
113 | + $this->view->message=$db_message; |
|
114 | 114 | |
115 | - try { |
|
116 | - $this->getUIDatabase()->testGetIdoDb(); // Get DB in test mode |
|
117 | - $dberror=array(0,''); |
|
118 | - } catch (DBException $e) { |
|
119 | - $dberror = $e->getArray(); |
|
120 | - } |
|
115 | + try { |
|
116 | + $this->getUIDatabase()->testGetIdoDb(); // Get DB in test mode |
|
117 | + $dberror=array(0,''); |
|
118 | + } catch (DBException $e) { |
|
119 | + $dberror = $e->getArray(); |
|
120 | + } |
|
121 | 121 | |
122 | - $this->view->ido_db_error=$dberror[0]; |
|
123 | - $this->view->ido_message='IDO Database : ' . $dberror[1]; |
|
122 | + $this->view->ido_db_error=$dberror[0]; |
|
123 | + $this->view->ido_message='IDO Database : ' . $dberror[1]; |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
@@ -130,23 +130,23 @@ discard block |
||
130 | 130 | */ |
131 | 131 | private function check_api() |
132 | 132 | { |
133 | - if ($this->Config()->get('config', 'icingaAPI_host') != '') |
|
134 | - { |
|
135 | - $apitest=new Icinga2API($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
|
136 | - $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
|
137 | - try { |
|
138 | - list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
139 | - //$this->view->apimessageError=false; |
|
140 | - } catch (RuntimeException $e) { |
|
141 | - $this->view->apimessage='API config : ' . $e->getMessage(); |
|
142 | - $this->view->apimessageError=true; |
|
143 | - } |
|
144 | - } |
|
145 | - else |
|
146 | - { |
|
147 | - $this->view->apimessage='API parameters not configured'; |
|
148 | - $this->view->apimessageError=true; |
|
149 | - } |
|
133 | + if ($this->Config()->get('config', 'icingaAPI_host') != '') |
|
134 | + { |
|
135 | + $apitest=new Icinga2API($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
|
136 | + $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
|
137 | + try { |
|
138 | + list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
139 | + //$this->view->apimessageError=false; |
|
140 | + } catch (RuntimeException $e) { |
|
141 | + $this->view->apimessage='API config : ' . $e->getMessage(); |
|
142 | + $this->view->apimessageError=true; |
|
143 | + } |
|
144 | + } |
|
145 | + else |
|
146 | + { |
|
147 | + $this->view->apimessage='API parameters not configured'; |
|
148 | + $this->view->apimessageError=true; |
|
149 | + } |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | /** |
@@ -157,21 +157,21 @@ discard block |
||
157 | 157 | */ |
158 | 158 | private function check_icingaweb_path() |
159 | 159 | { |
160 | - $this->view->icingaEtcWarn=0; |
|
161 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
162 | - if ($icingaweb2_etc != "/etc/icingaweb2/" && $icingaweb2_etc != '') |
|
163 | - { |
|
164 | - $output=array(); |
|
160 | + $this->view->icingaEtcWarn=0; |
|
161 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
162 | + if ($icingaweb2_etc != "/etc/icingaweb2/" && $icingaweb2_etc != '') |
|
163 | + { |
|
164 | + $output=array(); |
|
165 | 165 | |
166 | - exec('cat ' . $this->module->getBaseDir() .'/bin/trap_in.php | grep "\$icingaweb2Etc=" ',$output); |
|
166 | + exec('cat ' . $this->module->getBaseDir() .'/bin/trap_in.php | grep "\$icingaweb2Etc=" ',$output); |
|
167 | 167 | |
168 | 168 | |
169 | - if (! isset($output[0]) || ! preg_match('#"'. $icingaweb2_etc .'"#',$output[0])) |
|
170 | - { |
|
171 | - $this->view->icingaEtcWarn=1; |
|
172 | - $this->view->icingaweb2_etc=$icingaweb2_etc; |
|
173 | - } |
|
174 | - } |
|
169 | + if (! isset($output[0]) || ! preg_match('#"'. $icingaweb2_etc .'"#',$output[0])) |
|
170 | + { |
|
171 | + $this->view->icingaEtcWarn=1; |
|
172 | + $this->view->icingaweb2_etc=$icingaweb2_etc; |
|
173 | + } |
|
174 | + } |
|
175 | 175 | |
176 | 176 | } |
177 | 177 | |
@@ -182,15 +182,15 @@ discard block |
||
182 | 182 | */ |
183 | 183 | private function get_db_list($allowed) |
184 | 184 | { |
185 | - $resources = array(); |
|
186 | - foreach (ResourceFactory::getResourceConfigs() as $name => $resource) |
|
187 | - { |
|
188 | - if ($resource->get('type') === 'db' && in_array($resource->get('db'), $allowed)) |
|
189 | - { |
|
190 | - $resources[$name] = $name; |
|
191 | - } |
|
192 | - } |
|
193 | - return $resources; |
|
185 | + $resources = array(); |
|
186 | + foreach (ResourceFactory::getResourceConfigs() as $name => $resource) |
|
187 | + { |
|
188 | + if ($resource->get('type') === 'db' && in_array($resource->get('db'), $allowed)) |
|
189 | + { |
|
190 | + $resources[$name] = $name; |
|
191 | + } |
|
192 | + } |
|
193 | + return $resources; |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | /** |
@@ -199,23 +199,23 @@ discard block |
||
199 | 199 | */ |
200 | 200 | private function get_php_binary() |
201 | 201 | { |
202 | - $phpBinary= array( PHP_BINARY, PHP_BINDIR . "/php", '/usr/bin/php'); |
|
202 | + $phpBinary= array( PHP_BINARY, PHP_BINDIR . "/php", '/usr/bin/php'); |
|
203 | 203 | |
204 | - foreach ($phpBinary as $phpBin ) |
|
205 | - { |
|
206 | - $output=array(); |
|
207 | - $retCode=255; |
|
208 | - $input="154865134987aaaa"; |
|
209 | - exec("$phpBin -r \"echo '$input';\"",$output,$retCode); |
|
204 | + foreach ($phpBinary as $phpBin ) |
|
205 | + { |
|
206 | + $output=array(); |
|
207 | + $retCode=255; |
|
208 | + $input="154865134987aaaa"; |
|
209 | + exec("$phpBin -r \"echo '$input';\"",$output,$retCode); |
|
210 | 210 | |
211 | - if (! isset($output[0])) $output[0]="NO OUT"; |
|
211 | + if (! isset($output[0])) $output[0]="NO OUT"; |
|
212 | 212 | |
213 | - if ($retCode == 0 && preg_match("/$input/",$output[0]) == 1) |
|
214 | - { |
|
215 | - return $phpBin; |
|
216 | - } |
|
217 | - } |
|
218 | - return NULL; |
|
213 | + if ($retCode == 0 && preg_match("/$input/",$output[0]) == 1) |
|
214 | + { |
|
215 | + return $phpBin; |
|
216 | + } |
|
217 | + } |
|
218 | + return NULL; |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | /** |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | public function indexAction() |
235 | 235 | { |
236 | 236 | |
237 | - // CHeck permissions : display tests in any case, but no configuration. |
|
237 | + // CHeck permissions : display tests in any case, but no configuration. |
|
238 | 238 | $this->view->configPermission=$this->checkModuleConfigPermission(1); |
239 | 239 | // But check read permission |
240 | 240 | $this->checkReadPermission(); |
@@ -242,16 +242,16 @@ discard block |
||
242 | 242 | $this->view->tabs = $this->Module()->getConfigTabs()->activate('config'); |
243 | 243 | |
244 | 244 | // Get message : sent on configuration problems detected by controllers |
245 | - $this->get_param(); |
|
245 | + $this->get_param(); |
|
246 | 246 | |
247 | - // Test if configuration exists, if not create for installer script |
|
247 | + // Test if configuration exists, if not create for installer script |
|
248 | 248 | $this->check_empty_config(); |
249 | 249 | |
250 | 250 | // Test Database |
251 | - $this->check_db(); |
|
251 | + $this->check_db(); |
|
252 | 252 | |
253 | 253 | //********* Test API |
254 | - $this->check_api(); |
|
254 | + $this->check_api(); |
|
255 | 255 | |
256 | 256 | //*********** Test snmptrapd alive and options |
257 | 257 | list ($this->view->snmptrapdError, $this->view->snmptrapdMessage) = $this->checkSnmpTrapd(); |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | $phpBinary = $this->get_php_binary(); |
266 | 266 | if ($phpBinary === null) |
267 | 267 | { |
268 | - $phpBinary = ' PHP BINARY NOT FOUND '; |
|
268 | + $phpBinary = ' PHP BINARY NOT FOUND '; |
|
269 | 269 | |
270 | 270 | } |
271 | 271 | |
@@ -273,11 +273,11 @@ discard block |
||
273 | 273 | $this->view->traps_in_config= $phpBinary . ' ' . $this->Module()->getBaseDir() . '/bin/trap_in.php'; |
274 | 274 | |
275 | 275 | $this->view->installer= $this->Module()->getBaseDir() . '/bin/installer.sh ' |
276 | - . ' -c all ' |
|
277 | - . ' -d ' . $this->Module()->getBaseDir() |
|
278 | - . ' -p ' . $phpBinary |
|
279 | - . ' -a ' . exec('whoami') |
|
280 | - . ' -w ' . Icinga::app()->getConfigDir(); |
|
276 | + . ' -c all ' |
|
277 | + . ' -d ' . $this->Module()->getBaseDir() |
|
278 | + . ' -p ' . $phpBinary |
|
279 | + . ' -a ' . exec('whoami') |
|
280 | + . ' -w ' . Icinga::app()->getConfigDir(); |
|
281 | 281 | |
282 | 282 | // ******************* configuration form setup******************* |
283 | 283 | $this->view->form = $form = new TrapsConfigForm(); |
@@ -311,15 +311,15 @@ discard block |
||
311 | 311 | public function satelliteAction() |
312 | 312 | { |
313 | 313 | |
314 | - // CHeck permissions |
|
315 | - $this->view->configPermission=$this->checkModuleConfigPermission(); |
|
314 | + // CHeck permissions |
|
315 | + $this->view->configPermission=$this->checkModuleConfigPermission(); |
|
316 | 316 | |
317 | - // Setup tabs |
|
318 | - $this->view->tabs = $this->Module()->getConfigTabs()->activate('satellite'); |
|
317 | + // Setup tabs |
|
318 | + $this->view->tabs = $this->Module()->getConfigTabs()->activate('satellite'); |
|
319 | 319 | |
320 | - $this->view->masterHASet = FALSE; |
|
320 | + $this->view->masterHASet = FALSE; |
|
321 | 321 | |
322 | - $this->view->masterHAConf = array ('name' => 'masterHA1', 'icingaweb2 user'=>'User1'); |
|
322 | + $this->view->masterHAConf = array ('name' => 'masterHA1', 'icingaweb2 user'=>'User1'); |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | |
@@ -335,8 +335,8 @@ discard block |
||
335 | 335 | |
336 | 336 | try |
337 | 337 | { |
338 | - $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
339 | - printf('Schema already exists'); |
|
338 | + $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
339 | + printf('Schema already exists'); |
|
340 | 340 | |
341 | 341 | } |
342 | 342 | catch (DBException $e) |
@@ -347,19 +347,19 @@ discard block |
||
347 | 347 | // Get module database name |
348 | 348 | $dbName=$this->Config()->get('config', 'database'); |
349 | 349 | |
350 | - $dbResource = ResourceFactory::getResourceConfig($dbName); |
|
351 | - $dbType=$dbResource->get('db'); |
|
352 | - switch ($dbType) { |
|
353 | - case 'mysql': |
|
354 | - $dbFileExt='sql'; |
|
355 | - break; |
|
356 | - case 'pgsql': |
|
357 | - $dbFileExt='pgsql'; |
|
358 | - break; |
|
359 | - default: |
|
360 | - printf("Database configuration error : Unsuported DB"); |
|
361 | - return; |
|
362 | - } |
|
350 | + $dbResource = ResourceFactory::getResourceConfig($dbName); |
|
351 | + $dbType=$dbResource->get('db'); |
|
352 | + switch ($dbType) { |
|
353 | + case 'mysql': |
|
354 | + $dbFileExt='sql'; |
|
355 | + break; |
|
356 | + case 'pgsql': |
|
357 | + $dbFileExt='pgsql'; |
|
358 | + break; |
|
359 | + default: |
|
360 | + printf("Database configuration error : Unsuported DB"); |
|
361 | + return; |
|
362 | + } |
|
363 | 363 | |
364 | 364 | printf('<pre>'); |
365 | 365 | require_once $this->Module()->getBaseDir() .'/bin/trap_class.php'; |
@@ -383,32 +383,32 @@ discard block |
||
383 | 383 | public function updateschemaAction() |
384 | 384 | { |
385 | 385 | $this->checkModuleConfigPermission(); |
386 | - $this->getTabs()->add('get',array( |
|
387 | - 'active' => true, |
|
388 | - 'label' => $this->translate('Update Schema'), |
|
389 | - 'url' => Url::fromRequest() |
|
390 | - )); |
|
386 | + $this->getTabs()->add('get',array( |
|
387 | + 'active' => true, |
|
388 | + 'label' => $this->translate('Update Schema'), |
|
389 | + 'url' => Url::fromRequest() |
|
390 | + )); |
|
391 | 391 | // check if needed |
392 | 392 | $dberror=array(); |
393 | - try |
|
394 | - { |
|
395 | - $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
396 | - echo 'Schema already exists and is up to date<br>'; |
|
397 | - return; |
|
398 | - } |
|
399 | - catch (DBException $e) |
|
400 | - { |
|
401 | - $dberror=$e->getArray(); |
|
402 | - } |
|
393 | + try |
|
394 | + { |
|
395 | + $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
396 | + echo 'Schema already exists and is up to date<br>'; |
|
397 | + return; |
|
398 | + } |
|
399 | + catch (DBException $e) |
|
400 | + { |
|
401 | + $dberror=$e->getArray(); |
|
402 | + } |
|
403 | 403 | |
404 | 404 | echo 'Return to <a href="' . Url::fromPath('trapdirector/settings') .'" class="link-button icon-wrench"> settings page </a><br><br>'; |
405 | 405 | |
406 | 406 | if ($dberror[0] != 5) |
407 | 407 | { |
408 | - echo 'Database does not exists or is not setup correctly<br>'; |
|
409 | - return; |
|
408 | + echo 'Database does not exists or is not setup correctly<br>'; |
|
409 | + return; |
|
410 | 410 | } |
411 | - // setup |
|
411 | + // setup |
|
412 | 412 | require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
413 | 413 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
414 | 414 | $debug_level=4; |
@@ -421,20 +421,20 @@ discard block |
||
421 | 421 | $target_version=$dberror[2]; |
422 | 422 | |
423 | 423 | if ($this->params->get('msgok') == null) { |
424 | - // Check for messages and display if any |
|
425 | - echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
|
426 | - $Trap->setLogging(2,'syslog'); |
|
427 | - $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
|
428 | - if ($message != '') |
|
429 | - { |
|
430 | - echo 'Note :<br><pre>'; |
|
431 | - echo $message; |
|
432 | - echo '</pre>'; |
|
433 | - echo '<br>'; |
|
434 | - echo '<a class="link-button" style="font-size:large;font-weight:bold" href="' . Url::fromPath('trapdirector/settings/updateschema') .'?msgok=1">Click here to update</a>'; |
|
435 | - echo '<br>'; |
|
436 | - return; |
|
437 | - } |
|
424 | + // Check for messages and display if any |
|
425 | + echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
|
426 | + $Trap->setLogging(2,'syslog'); |
|
427 | + $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
|
428 | + if ($message != '') |
|
429 | + { |
|
430 | + echo 'Note :<br><pre>'; |
|
431 | + echo $message; |
|
432 | + echo '</pre>'; |
|
433 | + echo '<br>'; |
|
434 | + echo '<a class="link-button" style="font-size:large;font-weight:bold" href="' . Url::fromPath('trapdirector/settings/updateschema') .'?msgok=1">Click here to update</a>'; |
|
435 | + echo '<br>'; |
|
436 | + return; |
|
437 | + } |
|
438 | 438 | } |
439 | 439 | |
440 | 440 | $Trap->setLogging($debug_level,'display'); |
@@ -449,50 +449,50 @@ discard block |
||
449 | 449 | |
450 | 450 | private function checkSnmpTrapd() |
451 | 451 | { |
452 | - $psOutput=array(); |
|
453 | - // First check is someone is listening to port 162. As not root, we can't have pid... |
|
454 | - $sspath = exec('which ss 2>/dev/null'); |
|
455 | - if(empty($sspath)) |
|
456 | - { |
|
457 | - // RHEL based systems |
|
458 | - $sspath = '/usr/sbin/ss'; |
|
459 | - } |
|
460 | - if(!is_executable("$sspath")) |
|
461 | - { |
|
462 | - return array(1,"Can not execute $sspath"); |
|
463 | - } |
|
464 | - exec("$sspath -lun | grep ':162 '",$psOutput); |
|
465 | - if (count($psOutput) == 0) |
|
466 | - { |
|
467 | - return array(1,'Port UDP/162 is not open : is snmptrapd running?'); |
|
468 | - } |
|
469 | - $psOutput=array(); |
|
470 | - $selinux_state = ''; |
|
471 | - if(is_executable('/usr/sbin/getenforce')) |
|
472 | - { |
|
473 | - $selinux_state = exec('/usr/sbin/getenforce 2>/dev/null'); |
|
474 | - } |
|
475 | - if($selinux_state !== 'Enforcing') |
|
476 | - { |
|
477 | - exec('ps --no-headers -o command -C snmptrapd',$psOutput); |
|
478 | - if (count($psOutput) == 0) |
|
479 | - { |
|
480 | - return array(1,"UDP/162 : OK, but no snmptrapd process (?)"); |
|
481 | - } |
|
482 | - // Assume there is only one line... TODO : see if there is a better way to do this |
|
483 | - $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
|
484 | - if (!preg_match('/-n/',$line)) |
|
485 | - return array(1,'snmptrapd has no -n option : '.$line); |
|
486 | - if (!preg_match('/-O[^ ]*n/',$line)) |
|
487 | - return array(1,'snmptrapd has no -On option : '.$line); |
|
488 | - if (!preg_match('/-O[^ ]*e/',$line)) |
|
489 | - return array(1,'snmptrapd has no -Oe option : '.$line); |
|
452 | + $psOutput=array(); |
|
453 | + // First check is someone is listening to port 162. As not root, we can't have pid... |
|
454 | + $sspath = exec('which ss 2>/dev/null'); |
|
455 | + if(empty($sspath)) |
|
456 | + { |
|
457 | + // RHEL based systems |
|
458 | + $sspath = '/usr/sbin/ss'; |
|
459 | + } |
|
460 | + if(!is_executable("$sspath")) |
|
461 | + { |
|
462 | + return array(1,"Can not execute $sspath"); |
|
463 | + } |
|
464 | + exec("$sspath -lun | grep ':162 '",$psOutput); |
|
465 | + if (count($psOutput) == 0) |
|
466 | + { |
|
467 | + return array(1,'Port UDP/162 is not open : is snmptrapd running?'); |
|
468 | + } |
|
469 | + $psOutput=array(); |
|
470 | + $selinux_state = ''; |
|
471 | + if(is_executable('/usr/sbin/getenforce')) |
|
472 | + { |
|
473 | + $selinux_state = exec('/usr/sbin/getenforce 2>/dev/null'); |
|
474 | + } |
|
475 | + if($selinux_state !== 'Enforcing') |
|
476 | + { |
|
477 | + exec('ps --no-headers -o command -C snmptrapd',$psOutput); |
|
478 | + if (count($psOutput) == 0) |
|
479 | + { |
|
480 | + return array(1,"UDP/162 : OK, but no snmptrapd process (?)"); |
|
481 | + } |
|
482 | + // Assume there is only one line... TODO : see if there is a better way to do this |
|
483 | + $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
|
484 | + if (!preg_match('/-n/',$line)) |
|
485 | + return array(1,'snmptrapd has no -n option : '.$line); |
|
486 | + if (!preg_match('/-O[^ ]*n/',$line)) |
|
487 | + return array(1,'snmptrapd has no -On option : '.$line); |
|
488 | + if (!preg_match('/-O[^ ]*e/',$line)) |
|
489 | + return array(1,'snmptrapd has no -Oe option : '.$line); |
|
490 | 490 | |
491 | - return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
|
492 | - } |
|
493 | - else |
|
494 | - { |
|
495 | - return array(0,'A daemon (hidden by SELinux) is listening on UDP/162'); |
|
496 | - } |
|
491 | + return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
|
492 | + } |
|
493 | + else |
|
494 | + { |
|
495 | + return array(0,'A daemon (hidden by SELinux) is listening on UDP/162'); |
|
496 | + } |
|
497 | 497 | } |
498 | 498 | } |
@@ -72,23 +72,23 @@ discard block |
||
72 | 72 | private function check_db() |
73 | 73 | { |
74 | 74 | $db_message=array( // index => ( message OK, message NOK, optional link if NOK ) |
75 | - 0 => array('Database configuration OK','',''), |
|
76 | - 1 => array('Database set in config.ini','No database in config.ini',''), |
|
77 | - 2 => array('Database exists in Icingaweb2 config','Database does not exist in Icingaweb2 : ', |
|
75 | + 0 => array('Database configuration OK', '', ''), |
|
76 | + 1 => array('Database set in config.ini', 'No database in config.ini', ''), |
|
77 | + 2 => array('Database exists in Icingaweb2 config', 'Database does not exist in Icingaweb2 : ', |
|
78 | 78 | Url::fromPath('config/resource')), |
79 | - 3 => array('Database credentials OK','Database does not exist/invalid credentials/no schema : ', |
|
79 | + 3 => array('Database credentials OK', 'Database does not exist/invalid credentials/no schema : ', |
|
80 | 80 | Url::fromPath('trapdirector/settings/createschema')), |
81 | - 4 => array('Schema is set','Schema is not set for ', |
|
81 | + 4 => array('Schema is set', 'Schema is not set for ', |
|
82 | 82 | Url::fromPath('trapdirector/settings/createschema')), |
83 | - 5 => array('Schema is up to date','Schema is outdated :', |
|
83 | + 5 => array('Schema is up to date', 'Schema is outdated :', |
|
84 | 84 | Url::fromPath('trapdirector/settings/updateschema')), |
85 | 85 | ); |
86 | 86 | |
87 | 87 | try { |
88 | 88 | $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
89 | - $dberror=array(0,''); |
|
89 | + $dberror=array(0, ''); |
|
90 | 90 | } catch (DBException $e) { |
91 | - $dberror = $e->getArray(); |
|
91 | + $dberror=$e->getArray(); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | $this->view->db_error=$dberror[0]; |
@@ -96,13 +96,13 @@ discard block |
||
96 | 96 | { |
97 | 97 | case 2: |
98 | 98 | case 4: |
99 | - $db_message[$dberror[0]][1] .= $dberror[1]; |
|
99 | + $db_message[$dberror[0]][1].=$dberror[1]; |
|
100 | 100 | break; |
101 | 101 | case 3: |
102 | - $db_message[$dberror[0]][1] .= $dberror[1] . ', Message : ' . $dberror[2]; |
|
102 | + $db_message[$dberror[0]][1].=$dberror[1].', Message : '.$dberror[2]; |
|
103 | 103 | break; |
104 | 104 | case 5: |
105 | - $db_message[$dberror[0]][1] .= ' version '. $dberror[1] . ', version needed : ' .$dberror[2]; |
|
105 | + $db_message[$dberror[0]][1].=' version '.$dberror[1].', version needed : '.$dberror[2]; |
|
106 | 106 | break; |
107 | 107 | case 0: |
108 | 108 | case 1: |
@@ -114,13 +114,13 @@ discard block |
||
114 | 114 | |
115 | 115 | try { |
116 | 116 | $this->getUIDatabase()->testGetIdoDb(); // Get DB in test mode |
117 | - $dberror=array(0,''); |
|
117 | + $dberror=array(0, ''); |
|
118 | 118 | } catch (DBException $e) { |
119 | - $dberror = $e->getArray(); |
|
119 | + $dberror=$e->getArray(); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | $this->view->ido_db_error=$dberror[0]; |
123 | - $this->view->ido_message='IDO Database : ' . $dberror[1]; |
|
123 | + $this->view->ido_message='IDO Database : '.$dberror[1]; |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
@@ -132,13 +132,13 @@ discard block |
||
132 | 132 | { |
133 | 133 | if ($this->Config()->get('config', 'icingaAPI_host') != '') |
134 | 134 | { |
135 | - $apitest=new Icinga2API($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
|
135 | + $apitest=new Icinga2API($this->Config()->get('config', 'icingaAPI_host'), $this->Config()->get('config', 'icingaAPI_port')); |
|
136 | 136 | $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
137 | 137 | try { |
138 | - list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
138 | + list($this->view->apimessageError, $this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
139 | 139 | //$this->view->apimessageError=false; |
140 | 140 | } catch (RuntimeException $e) { |
141 | - $this->view->apimessage='API config : ' . $e->getMessage(); |
|
141 | + $this->view->apimessage='API config : '.$e->getMessage(); |
|
142 | 142 | $this->view->apimessageError=true; |
143 | 143 | } |
144 | 144 | } |
@@ -163,10 +163,10 @@ discard block |
||
163 | 163 | { |
164 | 164 | $output=array(); |
165 | 165 | |
166 | - exec('cat ' . $this->module->getBaseDir() .'/bin/trap_in.php | grep "\$icingaweb2Etc=" ',$output); |
|
166 | + exec('cat '.$this->module->getBaseDir().'/bin/trap_in.php | grep "\$icingaweb2Etc=" ', $output); |
|
167 | 167 | |
168 | 168 | |
169 | - if (! isset($output[0]) || ! preg_match('#"'. $icingaweb2_etc .'"#',$output[0])) |
|
169 | + if (!isset($output[0]) || !preg_match('#"'.$icingaweb2_etc.'"#', $output[0])) |
|
170 | 170 | { |
171 | 171 | $this->view->icingaEtcWarn=1; |
172 | 172 | $this->view->icingaweb2_etc=$icingaweb2_etc; |
@@ -182,12 +182,12 @@ discard block |
||
182 | 182 | */ |
183 | 183 | private function get_db_list($allowed) |
184 | 184 | { |
185 | - $resources = array(); |
|
185 | + $resources=array(); |
|
186 | 186 | foreach (ResourceFactory::getResourceConfigs() as $name => $resource) |
187 | 187 | { |
188 | 188 | if ($resource->get('type') === 'db' && in_array($resource->get('db'), $allowed)) |
189 | 189 | { |
190 | - $resources[$name] = $name; |
|
190 | + $resources[$name]=$name; |
|
191 | 191 | } |
192 | 192 | } |
193 | 193 | return $resources; |
@@ -199,18 +199,18 @@ discard block |
||
199 | 199 | */ |
200 | 200 | private function get_php_binary() |
201 | 201 | { |
202 | - $phpBinary= array( PHP_BINARY, PHP_BINDIR . "/php", '/usr/bin/php'); |
|
202 | + $phpBinary=array(PHP_BINARY, PHP_BINDIR."/php", '/usr/bin/php'); |
|
203 | 203 | |
204 | - foreach ($phpBinary as $phpBin ) |
|
204 | + foreach ($phpBinary as $phpBin) |
|
205 | 205 | { |
206 | 206 | $output=array(); |
207 | 207 | $retCode=255; |
208 | 208 | $input="154865134987aaaa"; |
209 | - exec("$phpBin -r \"echo '$input';\"",$output,$retCode); |
|
209 | + exec("$phpBin -r \"echo '$input';\"", $output, $retCode); |
|
210 | 210 | |
211 | - if (! isset($output[0])) $output[0]="NO OUT"; |
|
211 | + if (!isset($output[0])) $output[0]="NO OUT"; |
|
212 | 212 | |
213 | - if ($retCode == 0 && preg_match("/$input/",$output[0]) == 1) |
|
213 | + if ($retCode == 0 && preg_match("/$input/", $output[0]) == 1) |
|
214 | 214 | { |
215 | 215 | return $phpBin; |
216 | 216 | } |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | // But check read permission |
240 | 240 | $this->checkReadPermission(); |
241 | 241 | |
242 | - $this->view->tabs = $this->Module()->getConfigTabs()->activate('config'); |
|
242 | + $this->view->tabs=$this->Module()->getConfigTabs()->activate('config'); |
|
243 | 243 | |
244 | 244 | // Get message : sent on configuration problems detected by controllers |
245 | 245 | $this->get_param(); |
@@ -254,39 +254,39 @@ discard block |
||
254 | 254 | $this->check_api(); |
255 | 255 | |
256 | 256 | //*********** Test snmptrapd alive and options |
257 | - list ($this->view->snmptrapdError, $this->view->snmptrapdMessage) = $this->checkSnmpTrapd(); |
|
257 | + list ($this->view->snmptrapdError, $this->view->snmptrapdMessage)=$this->checkSnmpTrapd(); |
|
258 | 258 | |
259 | 259 | // List DB in $ressources |
260 | - $resources = $this->get_db_list(array('mysql', 'pgsql')); |
|
260 | + $resources=$this->get_db_list(array('mysql', 'pgsql')); |
|
261 | 261 | |
262 | 262 | // Check standard Icingaweb2 path |
263 | 263 | $this->check_icingaweb_path(); |
264 | 264 | |
265 | - $phpBinary = $this->get_php_binary(); |
|
265 | + $phpBinary=$this->get_php_binary(); |
|
266 | 266 | if ($phpBinary === null) |
267 | 267 | { |
268 | - $phpBinary = ' PHP BINARY NOT FOUND '; |
|
268 | + $phpBinary=' PHP BINARY NOT FOUND '; |
|
269 | 269 | |
270 | 270 | } |
271 | 271 | |
272 | 272 | // Setup path for mini documentation |
273 | - $this->view->traps_in_config= $phpBinary . ' ' . $this->Module()->getBaseDir() . '/bin/trap_in.php'; |
|
273 | + $this->view->traps_in_config=$phpBinary.' '.$this->Module()->getBaseDir().'/bin/trap_in.php'; |
|
274 | 274 | |
275 | - $this->view->installer= $this->Module()->getBaseDir() . '/bin/installer.sh ' |
|
275 | + $this->view->installer=$this->Module()->getBaseDir().'/bin/installer.sh ' |
|
276 | 276 | . ' -c all ' |
277 | - . ' -d ' . $this->Module()->getBaseDir() |
|
278 | - . ' -p ' . $phpBinary |
|
279 | - . ' -a ' . exec('whoami') |
|
280 | - . ' -w ' . Icinga::app()->getConfigDir(); |
|
277 | + . ' -d '.$this->Module()->getBaseDir() |
|
278 | + . ' -p '.$phpBinary |
|
279 | + . ' -a '.exec('whoami') |
|
280 | + . ' -w '.Icinga::app()->getConfigDir(); |
|
281 | 281 | |
282 | 282 | // ******************* configuration form setup******************* |
283 | - $this->view->form = $form = new TrapsConfigForm(); |
|
283 | + $this->view->form=$form=new TrapsConfigForm(); |
|
284 | 284 | |
285 | 285 | // set default paths; |
286 | - $this->view->form->setPaths($this->Module()->getBaseDir(),Icinga::app()->getConfigDir()); |
|
286 | + $this->view->form->setPaths($this->Module()->getBaseDir(), Icinga::app()->getConfigDir()); |
|
287 | 287 | |
288 | 288 | // set default ido database |
289 | - $this->view->form->setDefaultIDODB($this->Config()->module('monitoring','backends')->get('icinga','resource')); |
|
289 | + $this->view->form->setDefaultIDODB($this->Config()->module('monitoring', 'backends')->get('icinga', 'resource')); |
|
290 | 290 | |
291 | 291 | // Make form handle request. |
292 | 292 | $form->setIniConfig($this->Config()) |
@@ -315,18 +315,18 @@ discard block |
||
315 | 315 | $this->view->configPermission=$this->checkModuleConfigPermission(); |
316 | 316 | |
317 | 317 | // Setup tabs |
318 | - $this->view->tabs = $this->Module()->getConfigTabs()->activate('satellite'); |
|
318 | + $this->view->tabs=$this->Module()->getConfigTabs()->activate('satellite'); |
|
319 | 319 | |
320 | - $this->view->masterHASet = FALSE; |
|
320 | + $this->view->masterHASet=FALSE; |
|
321 | 321 | |
322 | - $this->view->masterHAConf = array ('name' => 'masterHA1', 'icingaweb2 user'=>'User1'); |
|
322 | + $this->view->masterHAConf=array('name' => 'masterHA1', 'icingaweb2 user'=>'User1'); |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | |
326 | 326 | public function createschemaAction() |
327 | 327 | { |
328 | 328 | $this->checkModuleConfigPermission(); |
329 | - $this->getTabs()->add('create_schema',array( |
|
329 | + $this->getTabs()->add('create_schema', array( |
|
330 | 330 | 'active' => true, |
331 | 331 | 'label' => $this->translate('Create Schema'), |
332 | 332 | 'url' => Url::fromRequest() |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | // Get module database name |
348 | 348 | $dbName=$this->Config()->get('config', 'database'); |
349 | 349 | |
350 | - $dbResource = ResourceFactory::getResourceConfig($dbName); |
|
350 | + $dbResource=ResourceFactory::getResourceConfig($dbName); |
|
351 | 351 | $dbType=$dbResource->get('db'); |
352 | 352 | switch ($dbType) { |
353 | 353 | case 'mysql': |
@@ -362,28 +362,28 @@ discard block |
||
362 | 362 | } |
363 | 363 | |
364 | 364 | printf('<pre>'); |
365 | - require_once $this->Module()->getBaseDir() .'/bin/trap_class.php'; |
|
365 | + require_once $this->Module()->getBaseDir().'/bin/trap_class.php'; |
|
366 | 366 | |
367 | 367 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
368 | 368 | $debug_level=4; |
369 | - $Trap = new Trap($icingaweb2_etc); |
|
370 | - $Trap->setLogging($debug_level,'display'); |
|
369 | + $Trap=new Trap($icingaweb2_etc); |
|
370 | + $Trap->setLogging($debug_level, 'display'); |
|
371 | 371 | |
372 | 372 | $prefix=$this->Config()->get('config', 'database_prefix'); |
373 | 373 | // schema file : <path>/SQL/schema_v<verion>.<dbtype> |
374 | - $schema=$this->Module()->getBaseDir() . |
|
375 | - '/SQL/schema_v'. $this->getModuleConfig()->getDbCurVersion() . '.' . $dbFileExt; |
|
374 | + $schema=$this->Module()->getBaseDir(). |
|
375 | + '/SQL/schema_v'.$this->getModuleConfig()->getDbCurVersion().'.'.$dbFileExt; |
|
376 | 376 | |
377 | - $Trap->trapsDB->create_schema($schema,$prefix); |
|
377 | + $Trap->trapsDB->create_schema($schema, $prefix); |
|
378 | 378 | echo '</pre>'; |
379 | 379 | } |
380 | - echo '<br><br>Return to <a href="' . Url::fromPath('trapdirector/settings') .'" class="link-button icon-wrench"> settings page </a>'; |
|
380 | + echo '<br><br>Return to <a href="'.Url::fromPath('trapdirector/settings').'" class="link-button icon-wrench"> settings page </a>'; |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | public function updateschemaAction() |
384 | 384 | { |
385 | 385 | $this->checkModuleConfigPermission(); |
386 | - $this->getTabs()->add('get',array( |
|
386 | + $this->getTabs()->add('get', array( |
|
387 | 387 | 'active' => true, |
388 | 388 | 'label' => $this->translate('Update Schema'), |
389 | 389 | 'url' => Url::fromRequest() |
@@ -401,7 +401,7 @@ discard block |
||
401 | 401 | $dberror=$e->getArray(); |
402 | 402 | } |
403 | 403 | |
404 | - echo 'Return to <a href="' . Url::fromPath('trapdirector/settings') .'" class="link-button icon-wrench"> settings page </a><br><br>'; |
|
404 | + echo 'Return to <a href="'.Url::fromPath('trapdirector/settings').'" class="link-button icon-wrench"> settings page </a><br><br>'; |
|
405 | 405 | |
406 | 406 | if ($dberror[0] != 5) |
407 | 407 | { |
@@ -409,40 +409,40 @@ discard block |
||
409 | 409 | return; |
410 | 410 | } |
411 | 411 | // setup |
412 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
412 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
413 | 413 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
414 | 414 | $debug_level=4; |
415 | - $Trap = new Trap($icingaweb2_etc); |
|
415 | + $Trap=new Trap($icingaweb2_etc); |
|
416 | 416 | |
417 | 417 | |
418 | 418 | $prefix=$this->Config()->get('config', 'database_prefix'); |
419 | - $updateSchema=$this->Module()->getBaseDir() . '/SQL/'; |
|
419 | + $updateSchema=$this->Module()->getBaseDir().'/SQL/'; |
|
420 | 420 | |
421 | 421 | $target_version=$dberror[2]; |
422 | 422 | |
423 | 423 | if ($this->params->get('msgok') == null) { |
424 | 424 | // Check for messages and display if any |
425 | 425 | echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
426 | - $Trap->setLogging(2,'syslog'); |
|
427 | - $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
|
426 | + $Trap->setLogging(2, 'syslog'); |
|
427 | + $message=$Trap->trapsDB->update_schema($updateSchema, $target_version, $prefix, true); |
|
428 | 428 | if ($message != '') |
429 | 429 | { |
430 | 430 | echo 'Note :<br><pre>'; |
431 | 431 | echo $message; |
432 | 432 | echo '</pre>'; |
433 | 433 | echo '<br>'; |
434 | - echo '<a class="link-button" style="font-size:large;font-weight:bold" href="' . Url::fromPath('trapdirector/settings/updateschema') .'?msgok=1">Click here to update</a>'; |
|
434 | + echo '<a class="link-button" style="font-size:large;font-weight:bold" href="'.Url::fromPath('trapdirector/settings/updateschema').'?msgok=1">Click here to update</a>'; |
|
435 | 435 | echo '<br>'; |
436 | 436 | return; |
437 | 437 | } |
438 | 438 | } |
439 | 439 | |
440 | - $Trap->setLogging($debug_level,'display'); |
|
440 | + $Trap->setLogging($debug_level, 'display'); |
|
441 | 441 | |
442 | - echo 'Updating schema to '. $target_version . ': <br>'; |
|
442 | + echo 'Updating schema to '.$target_version.': <br>'; |
|
443 | 443 | echo '<pre>'; |
444 | 444 | |
445 | - $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix); |
|
445 | + $Trap->trapsDB->update_schema($updateSchema, $target_version, $prefix); |
|
446 | 446 | echo '</pre>'; |
447 | 447 | } |
448 | 448 | |
@@ -451,48 +451,48 @@ discard block |
||
451 | 451 | { |
452 | 452 | $psOutput=array(); |
453 | 453 | // First check is someone is listening to port 162. As not root, we can't have pid... |
454 | - $sspath = exec('which ss 2>/dev/null'); |
|
455 | - if(empty($sspath)) |
|
454 | + $sspath=exec('which ss 2>/dev/null'); |
|
455 | + if (empty($sspath)) |
|
456 | 456 | { |
457 | 457 | // RHEL based systems |
458 | - $sspath = '/usr/sbin/ss'; |
|
458 | + $sspath='/usr/sbin/ss'; |
|
459 | 459 | } |
460 | - if(!is_executable("$sspath")) |
|
460 | + if (!is_executable("$sspath")) |
|
461 | 461 | { |
462 | - return array(1,"Can not execute $sspath"); |
|
462 | + return array(1, "Can not execute $sspath"); |
|
463 | 463 | } |
464 | - exec("$sspath -lun | grep ':162 '",$psOutput); |
|
464 | + exec("$sspath -lun | grep ':162 '", $psOutput); |
|
465 | 465 | if (count($psOutput) == 0) |
466 | 466 | { |
467 | - return array(1,'Port UDP/162 is not open : is snmptrapd running?'); |
|
467 | + return array(1, 'Port UDP/162 is not open : is snmptrapd running?'); |
|
468 | 468 | } |
469 | 469 | $psOutput=array(); |
470 | - $selinux_state = ''; |
|
471 | - if(is_executable('/usr/sbin/getenforce')) |
|
470 | + $selinux_state=''; |
|
471 | + if (is_executable('/usr/sbin/getenforce')) |
|
472 | 472 | { |
473 | - $selinux_state = exec('/usr/sbin/getenforce 2>/dev/null'); |
|
473 | + $selinux_state=exec('/usr/sbin/getenforce 2>/dev/null'); |
|
474 | 474 | } |
475 | - if($selinux_state !== 'Enforcing') |
|
475 | + if ($selinux_state !== 'Enforcing') |
|
476 | 476 | { |
477 | - exec('ps --no-headers -o command -C snmptrapd',$psOutput); |
|
477 | + exec('ps --no-headers -o command -C snmptrapd', $psOutput); |
|
478 | 478 | if (count($psOutput) == 0) |
479 | 479 | { |
480 | - return array(1,"UDP/162 : OK, but no snmptrapd process (?)"); |
|
480 | + return array(1, "UDP/162 : OK, but no snmptrapd process (?)"); |
|
481 | 481 | } |
482 | 482 | // Assume there is only one line... TODO : see if there is a better way to do this |
483 | - $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
|
484 | - if (!preg_match('/-n/',$line)) |
|
485 | - return array(1,'snmptrapd has no -n option : '.$line); |
|
486 | - if (!preg_match('/-O[^ ]*n/',$line)) |
|
487 | - return array(1,'snmptrapd has no -On option : '.$line); |
|
488 | - if (!preg_match('/-O[^ ]*e/',$line)) |
|
489 | - return array(1,'snmptrapd has no -Oe option : '.$line); |
|
483 | + $line=preg_replace('/^.*snmptrapd /', '', $psOutput[0]); |
|
484 | + if (!preg_match('/-n/', $line)) |
|
485 | + return array(1, 'snmptrapd has no -n option : '.$line); |
|
486 | + if (!preg_match('/-O[^ ]*n/', $line)) |
|
487 | + return array(1, 'snmptrapd has no -On option : '.$line); |
|
488 | + if (!preg_match('/-O[^ ]*e/', $line)) |
|
489 | + return array(1, 'snmptrapd has no -Oe option : '.$line); |
|
490 | 490 | |
491 | - return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
|
491 | + return array(0, 'snmptrapd listening to UDP/162, options : '.$line); |
|
492 | 492 | } |
493 | 493 | else |
494 | 494 | { |
495 | - return array(0,'A daemon (hidden by SELinux) is listening on UDP/162'); |
|
495 | + return array(0, 'A daemon (hidden by SELinux) is listening on UDP/162'); |
|
496 | 496 | } |
497 | 497 | } |
498 | 498 | } |
@@ -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 | } |
@@ -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 | } |
@@ -17,846 +17,846 @@ 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 | - continue; |
|
258 | - } |
|
259 | - 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 | - { |
|
261 | - $this->trapData['trap_oid']=$matches[2]; |
|
262 | - continue; |
|
263 | - } |
|
264 | - if ( $this->useSnmpTrapAddess === TRUE && preg_match('/'.$this->snmpTrapAddressOID.'/', $matches[1]) == 1) |
|
265 | - { |
|
266 | - $this->logging->log('Found relayed trap from ' . $matches[2] . ' relayed by ' .$this->trapData['source_ip'],DEBUG); |
|
267 | - if (preg_match('/^[0-9\.]+$/',$matches[2]) == 0 && preg_match('/^[0-9a-fA-F:]+$/',$matches[2]) == 0) |
|
268 | - { |
|
269 | - $this->logging->log('Value of SnmpTrapAddess ('.$this->snmpTrapAddressOID.') is not IP : ' .$matches[2],WARN,''); |
|
270 | - continue; |
|
271 | - } |
|
272 | - $this->trapData['source_ip'] = $matches[2]; |
|
273 | - continue; |
|
274 | - } |
|
275 | - $object= new stdClass; |
|
276 | - $object->oid =$matches[1]; |
|
277 | - $object->value = $matches[2]; |
|
278 | - array_push($this->trapDataExt,$object); |
|
279 | - } |
|
280 | - |
|
281 | - if ($this->trapData['trap_oid']=='unknown') |
|
282 | - { |
|
283 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
284 | - $this->logging->log('no trap oid found',ERROR,''); |
|
285 | - } |
|
286 | - |
|
287 | - // Translate oids. |
|
288 | - |
|
289 | - $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
290 | - if ($retArray != null) |
|
291 | - { |
|
292 | - $this->trapData['trap_name']=$retArray['trap_name']; |
|
293 | - $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
294 | - } |
|
295 | - foreach ($this->trapDataExt as $key => $val) |
|
296 | - { |
|
297 | - $retArray=$this->translateOID($val->oid); |
|
298 | - if ($retArray != null) |
|
299 | - { |
|
300 | - $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
301 | - $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
302 | - } |
|
303 | - } |
|
304 | - |
|
305 | - |
|
306 | - $this->trapData['status']= 'waiting'; |
|
307 | - |
|
308 | - return $this->trapData; |
|
309 | - } |
|
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 | + continue; |
|
258 | + } |
|
259 | + 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 | + { |
|
261 | + $this->trapData['trap_oid']=$matches[2]; |
|
262 | + continue; |
|
263 | + } |
|
264 | + if ( $this->useSnmpTrapAddess === TRUE && preg_match('/'.$this->snmpTrapAddressOID.'/', $matches[1]) == 1) |
|
265 | + { |
|
266 | + $this->logging->log('Found relayed trap from ' . $matches[2] . ' relayed by ' .$this->trapData['source_ip'],DEBUG); |
|
267 | + if (preg_match('/^[0-9\.]+$/',$matches[2]) == 0 && preg_match('/^[0-9a-fA-F:]+$/',$matches[2]) == 0) |
|
268 | + { |
|
269 | + $this->logging->log('Value of SnmpTrapAddess ('.$this->snmpTrapAddressOID.') is not IP : ' .$matches[2],WARN,''); |
|
270 | + continue; |
|
271 | + } |
|
272 | + $this->trapData['source_ip'] = $matches[2]; |
|
273 | + continue; |
|
274 | + } |
|
275 | + $object= new stdClass; |
|
276 | + $object->oid =$matches[1]; |
|
277 | + $object->value = $matches[2]; |
|
278 | + array_push($this->trapDataExt,$object); |
|
279 | + } |
|
280 | + |
|
281 | + if ($this->trapData['trap_oid']=='unknown') |
|
282 | + { |
|
283 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
284 | + $this->logging->log('no trap oid found',ERROR,''); |
|
285 | + } |
|
286 | + |
|
287 | + // Translate oids. |
|
288 | + |
|
289 | + $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
290 | + if ($retArray != null) |
|
291 | + { |
|
292 | + $this->trapData['trap_name']=$retArray['trap_name']; |
|
293 | + $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
294 | + } |
|
295 | + foreach ($this->trapDataExt as $key => $val) |
|
296 | + { |
|
297 | + $retArray=$this->translateOID($val->oid); |
|
298 | + if ($retArray != null) |
|
299 | + { |
|
300 | + $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
301 | + $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
302 | + } |
|
303 | + } |
|
304 | + |
|
305 | + |
|
306 | + $this->trapData['status']= 'waiting'; |
|
307 | + |
|
308 | + return $this->trapData; |
|
309 | + } |
|
310 | 310 | |
311 | - /** |
|
312 | - * Translate oid into array(MIB,Name) |
|
313 | - * @param $oid string oid to translate |
|
314 | - * @return mixed : null if not found or array(MIB,Name) |
|
315 | - */ |
|
316 | - public function translateOID($oid) |
|
317 | - { |
|
318 | - // try from database |
|
319 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
320 | - |
|
321 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
322 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
323 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
324 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
325 | - } |
|
326 | - $name=$ret_code->fetch(); |
|
327 | - if ($name['name'] != null) |
|
328 | - { |
|
329 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
330 | - } |
|
331 | - |
|
332 | - // Also check if it is an instance of OID |
|
333 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
334 | - |
|
335 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
336 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
337 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
338 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
339 | - } |
|
340 | - $name=$ret_code->fetch(); |
|
341 | - if ($name['name'] != null) |
|
342 | - { |
|
343 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
344 | - } |
|
345 | - |
|
346 | - // Try to get oid name from snmptranslate |
|
347 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
348 | - ' '.$oid); |
|
349 | - $matches=array(); |
|
350 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
351 | - if ($ret_code===0 || $ret_code === false) { |
|
352 | - return NULL; |
|
353 | - } else { |
|
354 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
355 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
356 | - } |
|
357 | - } |
|
311 | + /** |
|
312 | + * Translate oid into array(MIB,Name) |
|
313 | + * @param $oid string oid to translate |
|
314 | + * @return mixed : null if not found or array(MIB,Name) |
|
315 | + */ |
|
316 | + public function translateOID($oid) |
|
317 | + { |
|
318 | + // try from database |
|
319 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
320 | + |
|
321 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
322 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
323 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
324 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
325 | + } |
|
326 | + $name=$ret_code->fetch(); |
|
327 | + if ($name['name'] != null) |
|
328 | + { |
|
329 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
330 | + } |
|
331 | + |
|
332 | + // Also check if it is an instance of OID |
|
333 | + $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
334 | + |
|
335 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
336 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
337 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
338 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
339 | + } |
|
340 | + $name=$ret_code->fetch(); |
|
341 | + if ($name['name'] != null) |
|
342 | + { |
|
343 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
344 | + } |
|
345 | + |
|
346 | + // Try to get oid name from snmptranslate |
|
347 | + $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
348 | + ' '.$oid); |
|
349 | + $matches=array(); |
|
350 | + $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
351 | + if ($ret_code===0 || $ret_code === false) { |
|
352 | + return NULL; |
|
353 | + } else { |
|
354 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
355 | + return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
356 | + } |
|
357 | + } |
|
358 | 358 | |
359 | - /** |
|
360 | - * Erase old trap records |
|
361 | - * @param integer $days : erase traps when more than $days old |
|
362 | - * @return integer : number of lines deleted |
|
363 | - **/ |
|
364 | - public function eraseOldTraps($days=0) |
|
365 | - { |
|
366 | - if ($days==0) |
|
367 | - { |
|
368 | - if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
369 | - { |
|
370 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
371 | - return; |
|
372 | - } |
|
373 | - } |
|
374 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
375 | - $daysago = strtotime("-".$days." day"); |
|
376 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
377 | - if ($db_conn->query($sql) === false) { |
|
378 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
379 | - } |
|
380 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
381 | - } |
|
359 | + /** |
|
360 | + * Erase old trap records |
|
361 | + * @param integer $days : erase traps when more than $days old |
|
362 | + * @return integer : number of lines deleted |
|
363 | + **/ |
|
364 | + public function eraseOldTraps($days=0) |
|
365 | + { |
|
366 | + if ($days==0) |
|
367 | + { |
|
368 | + if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
369 | + { |
|
370 | + $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
371 | + return; |
|
372 | + } |
|
373 | + } |
|
374 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
375 | + $daysago = strtotime("-".$days." day"); |
|
376 | + $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
377 | + if ($db_conn->query($sql) === false) { |
|
378 | + $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
379 | + } |
|
380 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
381 | + } |
|
382 | 382 | |
383 | - /** Write error to received trap database |
|
384 | - */ |
|
385 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
386 | - { |
|
387 | - |
|
388 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
389 | - |
|
390 | - // add date time |
|
391 | - $insert_col ='date_received,status'; |
|
392 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
393 | - |
|
394 | - if ($sourceIP !=null) |
|
395 | - { |
|
396 | - $insert_col .=',source_ip'; |
|
397 | - $insert_val .=",'". $sourceIP ."'"; |
|
398 | - } |
|
399 | - if ($trapoid !=null) |
|
400 | - { |
|
401 | - $insert_col .=',trap_oid'; |
|
402 | - $insert_val .=",'". $trapoid ."'"; |
|
403 | - } |
|
404 | - $insert_col .=',status_detail'; |
|
405 | - $insert_val .=",'". $message ."'"; |
|
406 | - |
|
407 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
408 | - |
|
409 | - switch ($this->trapsDB->trapDBType) |
|
410 | - { |
|
411 | - case 'pgsql': |
|
412 | - $sql .= ' RETURNING id;'; |
|
413 | - $this->logging->log('sql : '.$sql,INFO); |
|
414 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
415 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
416 | - } |
|
417 | - $this->logging->log('SQL insertion OK',INFO ); |
|
418 | - // Get last id to insert oid/values in secondary table |
|
419 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
383 | + /** Write error to received trap database |
|
384 | + */ |
|
385 | + public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
386 | + { |
|
387 | + |
|
388 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
389 | + |
|
390 | + // add date time |
|
391 | + $insert_col ='date_received,status'; |
|
392 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
393 | + |
|
394 | + if ($sourceIP !=null) |
|
395 | + { |
|
396 | + $insert_col .=',source_ip'; |
|
397 | + $insert_val .=",'". $sourceIP ."'"; |
|
398 | + } |
|
399 | + if ($trapoid !=null) |
|
400 | + { |
|
401 | + $insert_col .=',trap_oid'; |
|
402 | + $insert_val .=",'". $trapoid ."'"; |
|
403 | + } |
|
404 | + $insert_col .=',status_detail'; |
|
405 | + $insert_val .=",'". $message ."'"; |
|
406 | + |
|
407 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
408 | + |
|
409 | + switch ($this->trapsDB->trapDBType) |
|
410 | + { |
|
411 | + case 'pgsql': |
|
412 | + $sql .= ' RETURNING id;'; |
|
413 | + $this->logging->log('sql : '.$sql,INFO); |
|
414 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
415 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
416 | + } |
|
417 | + $this->logging->log('SQL insertion OK',INFO ); |
|
418 | + // Get last id to insert oid/values in secondary table |
|
419 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
420 | 420 | |
421 | - $this->logging->log('Erreur recuperation id',1,''); |
|
422 | - } |
|
423 | - if (! isset($inserted_id_ret['id'])) { |
|
424 | - $this->logging->log('Error getting id',1,''); |
|
425 | - } |
|
426 | - $this->trapId=$inserted_id_ret['id']; |
|
427 | - break; |
|
428 | - case 'mysql': |
|
429 | - $sql .= ';'; |
|
430 | - $this->logging->log('sql : '.$sql,INFO ); |
|
431 | - if ($db_conn->query($sql) === false) { |
|
432 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
433 | - } |
|
434 | - $this->logging->log('SQL insertion OK',INFO ); |
|
435 | - // Get last id to insert oid/values in secondary table |
|
436 | - $sql='SELECT LAST_INSERT_ID();'; |
|
437 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
438 | - $this->logging->log('Erreur recuperation id',1,''); |
|
439 | - } |
|
421 | + $this->logging->log('Erreur recuperation id',1,''); |
|
422 | + } |
|
423 | + if (! isset($inserted_id_ret['id'])) { |
|
424 | + $this->logging->log('Error getting id',1,''); |
|
425 | + } |
|
426 | + $this->trapId=$inserted_id_ret['id']; |
|
427 | + break; |
|
428 | + case 'mysql': |
|
429 | + $sql .= ';'; |
|
430 | + $this->logging->log('sql : '.$sql,INFO ); |
|
431 | + if ($db_conn->query($sql) === false) { |
|
432 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
433 | + } |
|
434 | + $this->logging->log('SQL insertion OK',INFO ); |
|
435 | + // Get last id to insert oid/values in secondary table |
|
436 | + $sql='SELECT LAST_INSERT_ID();'; |
|
437 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
438 | + $this->logging->log('Erreur recuperation id',1,''); |
|
439 | + } |
|
440 | 440 | |
441 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
442 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
443 | - $this->trapId=$inserted_id; |
|
444 | - break; |
|
445 | - default: |
|
446 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
447 | - } |
|
448 | - |
|
449 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
450 | - } |
|
441 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
442 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
443 | + $this->trapId=$inserted_id; |
|
444 | + break; |
|
445 | + default: |
|
446 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
447 | + } |
|
448 | + |
|
449 | + $this->logging->log('id found: '. $this->trapId,INFO ); |
|
450 | + } |
|
451 | 451 | |
452 | - /** Write trap data to trap database |
|
453 | - */ |
|
454 | - public function writeTrapToDB() |
|
455 | - { |
|
456 | - |
|
457 | - // If action is ignore -> don't send t DB |
|
458 | - if ($this->trapToDb === false) return; |
|
459 | - |
|
460 | - |
|
461 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
462 | - |
|
463 | - $insert_col=''; |
|
464 | - $insert_val=''; |
|
465 | - // add date time |
|
466 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
467 | - |
|
468 | - $firstcol=1; |
|
469 | - foreach ($this->trapData as $col => $val) |
|
470 | - { |
|
471 | - if ($firstcol==0) |
|
472 | - { |
|
473 | - $insert_col .=','; |
|
474 | - $insert_val .=','; |
|
475 | - } |
|
476 | - $insert_col .= $col ; |
|
477 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
478 | - $firstcol=0; |
|
479 | - } |
|
480 | - |
|
481 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
482 | - switch ($this->trapsDB->trapDBType) |
|
483 | - { |
|
484 | - case 'pgsql': |
|
485 | - $sql .= ' RETURNING id;'; |
|
486 | - $this->logging->log('sql : '.$sql,INFO ); |
|
487 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
488 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
489 | - } |
|
490 | - $this->logging->log('SQL insertion OK',INFO ); |
|
491 | - // Get last id to insert oid/values in secondary table |
|
492 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
452 | + /** Write trap data to trap database |
|
453 | + */ |
|
454 | + public function writeTrapToDB() |
|
455 | + { |
|
456 | + |
|
457 | + // If action is ignore -> don't send t DB |
|
458 | + if ($this->trapToDb === false) return; |
|
459 | + |
|
460 | + |
|
461 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
462 | + |
|
463 | + $insert_col=''; |
|
464 | + $insert_val=''; |
|
465 | + // add date time |
|
466 | + $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
467 | + |
|
468 | + $firstcol=1; |
|
469 | + foreach ($this->trapData as $col => $val) |
|
470 | + { |
|
471 | + if ($firstcol==0) |
|
472 | + { |
|
473 | + $insert_col .=','; |
|
474 | + $insert_val .=','; |
|
475 | + } |
|
476 | + $insert_col .= $col ; |
|
477 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
478 | + $firstcol=0; |
|
479 | + } |
|
480 | + |
|
481 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
482 | + switch ($this->trapsDB->trapDBType) |
|
483 | + { |
|
484 | + case 'pgsql': |
|
485 | + $sql .= ' RETURNING id;'; |
|
486 | + $this->logging->log('sql : '.$sql,INFO ); |
|
487 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
488 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
489 | + } |
|
490 | + $this->logging->log('SQL insertion OK',INFO ); |
|
491 | + // Get last id to insert oid/values in secondary table |
|
492 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
493 | 493 | |
494 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
495 | - } |
|
496 | - if (! isset($inserted_id_ret['id'])) { |
|
497 | - $this->logging->log('Error getting id',ERROR,''); |
|
498 | - } |
|
499 | - $this->trapId=$inserted_id_ret['id']; |
|
500 | - break; |
|
501 | - case 'mysql': |
|
502 | - $sql .= ';'; |
|
503 | - $this->logging->log('sql : '.$sql,INFO ); |
|
504 | - if ($db_conn->query($sql) === false) { |
|
505 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
506 | - } |
|
507 | - $this->logging->log('SQL insertion OK',INFO ); |
|
508 | - // Get last id to insert oid/values in secondary table |
|
509 | - $sql='SELECT LAST_INSERT_ID();'; |
|
510 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
511 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
512 | - } |
|
494 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
495 | + } |
|
496 | + if (! isset($inserted_id_ret['id'])) { |
|
497 | + $this->logging->log('Error getting id',ERROR,''); |
|
498 | + } |
|
499 | + $this->trapId=$inserted_id_ret['id']; |
|
500 | + break; |
|
501 | + case 'mysql': |
|
502 | + $sql .= ';'; |
|
503 | + $this->logging->log('sql : '.$sql,INFO ); |
|
504 | + if ($db_conn->query($sql) === false) { |
|
505 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
506 | + } |
|
507 | + $this->logging->log('SQL insertion OK',INFO ); |
|
508 | + // Get last id to insert oid/values in secondary table |
|
509 | + $sql='SELECT LAST_INSERT_ID();'; |
|
510 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
511 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
512 | + } |
|
513 | 513 | |
514 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
515 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
516 | - $this->trapId=$inserted_id; |
|
517 | - break; |
|
518 | - default: |
|
519 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
520 | - } |
|
521 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
522 | - |
|
523 | - // Fill trap extended data table |
|
524 | - foreach ($this->trapDataExt as $value) { |
|
525 | - // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
526 | - $firstcol=1; |
|
527 | - $value->trap_id = $this->trapId; |
|
528 | - $insert_col=''; |
|
529 | - $insert_val=''; |
|
530 | - foreach ($value as $col => $val) |
|
531 | - { |
|
532 | - if ($firstcol==0) |
|
533 | - { |
|
534 | - $insert_col .=','; |
|
535 | - $insert_val .=','; |
|
536 | - } |
|
537 | - $insert_col .= $col; |
|
538 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
539 | - $firstcol=0; |
|
540 | - } |
|
514 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
515 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
516 | + $this->trapId=$inserted_id; |
|
517 | + break; |
|
518 | + default: |
|
519 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
520 | + } |
|
521 | + $this->logging->log('id found: '.$this->trapId,INFO ); |
|
522 | + |
|
523 | + // Fill trap extended data table |
|
524 | + foreach ($this->trapDataExt as $value) { |
|
525 | + // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
526 | + $firstcol=1; |
|
527 | + $value->trap_id = $this->trapId; |
|
528 | + $insert_col=''; |
|
529 | + $insert_val=''; |
|
530 | + foreach ($value as $col => $val) |
|
531 | + { |
|
532 | + if ($firstcol==0) |
|
533 | + { |
|
534 | + $insert_col .=','; |
|
535 | + $insert_val .=','; |
|
536 | + } |
|
537 | + $insert_col .= $col; |
|
538 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
539 | + $firstcol=0; |
|
540 | + } |
|
541 | 541 | |
542 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
542 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
543 | 543 | |
544 | - if ($db_conn->query($sql) === false) { |
|
545 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
546 | - } |
|
547 | - } |
|
548 | - } |
|
544 | + if ($db_conn->query($sql) === false) { |
|
545 | + $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
546 | + } |
|
547 | + } |
|
548 | + } |
|
549 | 549 | |
550 | - /** Get rules from rule database with ip and oid |
|
551 | - * @param $ip string ipv4 or ipv6 |
|
552 | - * @param $oid string oid in numeric |
|
553 | - * @return mixed|boolean : PDO object or false |
|
554 | - */ |
|
555 | - protected function getRules($ip,$oid) |
|
556 | - { |
|
557 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
558 | - // fetch rules based on IP in rule and OID |
|
559 | - $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
560 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
561 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
562 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
563 | - return false; |
|
564 | - } |
|
565 | - $rules_all=$ret_code->fetchAll(); |
|
566 | - //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
567 | - $rules_ret=array(); |
|
568 | - $rule_ret_key=0; |
|
569 | - foreach ($rules_all as $key => $rule) |
|
570 | - { |
|
571 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
572 | - { |
|
573 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
574 | - //TODO : get host name by API (and check if correct in rule). |
|
575 | - $rule_ret_key++; |
|
576 | - continue; |
|
577 | - } |
|
578 | - // TODO : get hosts IP by API |
|
579 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
580 | - { // get ips of group members by oid |
|
581 | - $db_conn2=$this->trapsDB->db_connect_ido(); |
|
582 | - $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
550 | + /** Get rules from rule database with ip and oid |
|
551 | + * @param $ip string ipv4 or ipv6 |
|
552 | + * @param $oid string oid in numeric |
|
553 | + * @return mixed|boolean : PDO object or false |
|
554 | + */ |
|
555 | + protected function getRules($ip,$oid) |
|
556 | + { |
|
557 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
558 | + // fetch rules based on IP in rule and OID |
|
559 | + $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
560 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
561 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
562 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
563 | + return false; |
|
564 | + } |
|
565 | + $rules_all=$ret_code->fetchAll(); |
|
566 | + //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
567 | + $rules_ret=array(); |
|
568 | + $rule_ret_key=0; |
|
569 | + foreach ($rules_all as $key => $rule) |
|
570 | + { |
|
571 | + if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
572 | + { |
|
573 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
574 | + //TODO : get host name by API (and check if correct in rule). |
|
575 | + $rule_ret_key++; |
|
576 | + continue; |
|
577 | + } |
|
578 | + // TODO : get hosts IP by API |
|
579 | + if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
580 | + { // get ips of group members by oid |
|
581 | + $db_conn2=$this->trapsDB->db_connect_ido(); |
|
582 | + $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
583 | 583 | FROM icinga_objects as o |
584 | 584 | LEFT JOIN icinga_hostgroups as h ON o.object_id=h.hostgroup_object_id |
585 | 585 | LEFT JOIN icinga_hostgroup_members as m ON h.hostgroup_id=m.hostgroup_id |
586 | 586 | LEFT JOIN icinga_hosts as a ON a.host_object_id = m.host_object_id |
587 | 587 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
588 | 588 | WHERE o.name1='".$rule['host_group_name']."';"; |
589 | - if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
590 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
591 | - continue; |
|
592 | - } |
|
593 | - $grouphosts=$ret_code2->fetchAll(); |
|
594 | - //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
595 | - foreach ( $grouphosts as $host) |
|
596 | - { |
|
597 | - //echo $host['ip4']."\n"; |
|
598 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
599 | - { |
|
600 | - //echo "Rule added \n"; |
|
601 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
602 | - $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
603 | - $rule_ret_key++; |
|
604 | - } |
|
605 | - } |
|
606 | - } |
|
607 | - } |
|
608 | - //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
609 | - return $rules_ret; |
|
610 | - } |
|
589 | + if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
590 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
591 | + continue; |
|
592 | + } |
|
593 | + $grouphosts=$ret_code2->fetchAll(); |
|
594 | + //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
595 | + foreach ( $grouphosts as $host) |
|
596 | + { |
|
597 | + //echo $host['ip4']."\n"; |
|
598 | + if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
599 | + { |
|
600 | + //echo "Rule added \n"; |
|
601 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
602 | + $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
603 | + $rule_ret_key++; |
|
604 | + } |
|
605 | + } |
|
606 | + } |
|
607 | + } |
|
608 | + //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
609 | + return $rules_ret; |
|
610 | + } |
|
611 | 611 | |
612 | - /** Add rule match to rule |
|
613 | - * @param id int : rule id |
|
614 | - * @param set int : value to set |
|
615 | - */ |
|
616 | - protected function add_rule_match($id, $set) |
|
617 | - { |
|
618 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
619 | - $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
620 | - if ($db_conn->query($sql) === false) { |
|
621 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
622 | - } |
|
623 | - } |
|
612 | + /** Add rule match to rule |
|
613 | + * @param id int : rule id |
|
614 | + * @param set int : value to set |
|
615 | + */ |
|
616 | + protected function add_rule_match($id, $set) |
|
617 | + { |
|
618 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
619 | + $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
620 | + if ($db_conn->query($sql) === false) { |
|
621 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
622 | + } |
|
623 | + } |
|
624 | 624 | |
625 | - /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
626 | - * |
|
627 | - * @param string $host |
|
628 | - * @param string $service |
|
629 | - * @param integer $state numerical staus |
|
630 | - * @param string $display |
|
631 | - * @returnn bool true is service check was sent without error |
|
632 | - */ |
|
633 | - public function serviceCheckResult($host,$service,$state,$display) |
|
634 | - { |
|
635 | - if ($this->apiUse === false) |
|
636 | - { |
|
637 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
638 | - $host.';' .$service .';' . $state . ';'.$display; |
|
639 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
625 | + /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
626 | + * |
|
627 | + * @param string $host |
|
628 | + * @param string $service |
|
629 | + * @param integer $state numerical staus |
|
630 | + * @param string $display |
|
631 | + * @returnn bool true is service check was sent without error |
|
632 | + */ |
|
633 | + public function serviceCheckResult($host,$service,$state,$display) |
|
634 | + { |
|
635 | + if ($this->apiUse === false) |
|
636 | + { |
|
637 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
638 | + $host.';' .$service .';' . $state . ';'.$display; |
|
639 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
640 | 640 | |
641 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
642 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
643 | - return true; |
|
644 | - } |
|
645 | - else |
|
646 | - { |
|
647 | - // Get perfdata if found |
|
648 | - $matches=array(); |
|
649 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
650 | - { |
|
651 | - $display=$matches[1]; |
|
652 | - $perfdata=$matches[2]; |
|
653 | - } |
|
654 | - else |
|
655 | - { |
|
656 | - $perfdata=''; |
|
657 | - } |
|
641 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
642 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
643 | + return true; |
|
644 | + } |
|
645 | + else |
|
646 | + { |
|
647 | + // Get perfdata if found |
|
648 | + $matches=array(); |
|
649 | + if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
650 | + { |
|
651 | + $display=$matches[1]; |
|
652 | + $perfdata=$matches[2]; |
|
653 | + } |
|
654 | + else |
|
655 | + { |
|
656 | + $perfdata=''; |
|
657 | + } |
|
658 | 658 | |
659 | - $api = $this->getAPI(); |
|
660 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
661 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
662 | - if ($retcode == false) |
|
663 | - { |
|
664 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
665 | - return false; |
|
666 | - } |
|
667 | - else |
|
668 | - { |
|
669 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
670 | - return true; |
|
671 | - } |
|
672 | - } |
|
673 | - } |
|
659 | + $api = $this->getAPI(); |
|
660 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
661 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
662 | + if ($retcode == false) |
|
663 | + { |
|
664 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
665 | + return false; |
|
666 | + } |
|
667 | + else |
|
668 | + { |
|
669 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
670 | + return true; |
|
671 | + } |
|
672 | + } |
|
673 | + } |
|
674 | 674 | |
675 | - public function getHostByIP($ip) |
|
676 | - { |
|
677 | - $api = $this->getAPI(); |
|
678 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
679 | - return $api->getHostByIP($ip); |
|
680 | - } |
|
675 | + public function getHostByIP($ip) |
|
676 | + { |
|
677 | + $api = $this->getAPI(); |
|
678 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
679 | + return $api->getHostByIP($ip); |
|
680 | + } |
|
681 | 681 | |
682 | - /** Resolve display. |
|
683 | - * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
684 | - * @param $display string |
|
685 | - * @return string display |
|
686 | - */ |
|
687 | - protected function applyDisplay($display) |
|
688 | - { |
|
689 | - $matches=array(); |
|
690 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
691 | - { |
|
692 | - $oid=$matches[1]; |
|
693 | - $found=0; |
|
694 | - // Test and transform regexp |
|
695 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
682 | + /** Resolve display. |
|
683 | + * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
684 | + * @param $display string |
|
685 | + * @return string display |
|
686 | + */ |
|
687 | + protected function applyDisplay($display) |
|
688 | + { |
|
689 | + $matches=array(); |
|
690 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
691 | + { |
|
692 | + $oid=$matches[1]; |
|
693 | + $found=0; |
|
694 | + // Test and transform regexp |
|
695 | + $oidR = $this->ruleClass->regexp_eval($oid); |
|
696 | 696 | |
697 | - foreach($this->trapDataExt as $val) |
|
698 | - { |
|
699 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
700 | - { |
|
701 | - $val->value=preg_replace('/"/','',$val->value); |
|
702 | - $rep=0; |
|
703 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
704 | - if ($rep==0) |
|
705 | - { |
|
706 | - $this->logging->log("Error in display",WARN,''); |
|
707 | - return $display; |
|
708 | - } |
|
709 | - $found=1; |
|
710 | - break; |
|
711 | - } |
|
712 | - } |
|
713 | - if ($found==0) |
|
714 | - { |
|
715 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
716 | - if ($rep==0) |
|
717 | - { |
|
718 | - $this->logging->log("Error in display",WARN,''); |
|
719 | - return $display; |
|
720 | - } |
|
721 | - } |
|
722 | - } |
|
723 | - return $display; |
|
724 | - } |
|
697 | + foreach($this->trapDataExt as $val) |
|
698 | + { |
|
699 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
700 | + { |
|
701 | + $val->value=preg_replace('/"/','',$val->value); |
|
702 | + $rep=0; |
|
703 | + $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
704 | + if ($rep==0) |
|
705 | + { |
|
706 | + $this->logging->log("Error in display",WARN,''); |
|
707 | + return $display; |
|
708 | + } |
|
709 | + $found=1; |
|
710 | + break; |
|
711 | + } |
|
712 | + } |
|
713 | + if ($found==0) |
|
714 | + { |
|
715 | + $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
716 | + if ($rep==0) |
|
717 | + { |
|
718 | + $this->logging->log("Error in display",WARN,''); |
|
719 | + return $display; |
|
720 | + } |
|
721 | + } |
|
722 | + } |
|
723 | + return $display; |
|
724 | + } |
|
725 | 725 | |
726 | - /** Match rules for current trap and do action |
|
727 | - */ |
|
728 | - public function applyRules() |
|
729 | - { |
|
730 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
731 | - |
|
732 | - if ($rules===false || count($rules)==0) |
|
733 | - { |
|
734 | - $this->logging->log('No rules found for this trap',INFO ); |
|
735 | - $this->trapData['status']='unknown'; |
|
736 | - $this->trapToDb=true; |
|
737 | - return; |
|
738 | - } |
|
739 | - //print_r($rules); |
|
740 | - // Evaluate all rules in sequence |
|
741 | - $this->trapAction=null; |
|
742 | - foreach ($rules as $rule) |
|
743 | - { |
|
726 | + /** Match rules for current trap and do action |
|
727 | + */ |
|
728 | + public function applyRules() |
|
729 | + { |
|
730 | + $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
731 | + |
|
732 | + if ($rules===false || count($rules)==0) |
|
733 | + { |
|
734 | + $this->logging->log('No rules found for this trap',INFO ); |
|
735 | + $this->trapData['status']='unknown'; |
|
736 | + $this->trapToDb=true; |
|
737 | + return; |
|
738 | + } |
|
739 | + //print_r($rules); |
|
740 | + // Evaluate all rules in sequence |
|
741 | + $this->trapAction=null; |
|
742 | + foreach ($rules as $rule) |
|
743 | + { |
|
744 | 744 | |
745 | - $host_name=$rule['host_name']; |
|
746 | - $service_name=$rule['service_name']; |
|
745 | + $host_name=$rule['host_name']; |
|
746 | + $service_name=$rule['service_name']; |
|
747 | 747 | |
748 | - $display=$this->applyDisplay($rule['display']); |
|
749 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
750 | - try |
|
751 | - { |
|
752 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
753 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
754 | - //->eval_rule($rule['rule']); |
|
748 | + $display=$this->applyDisplay($rule['display']); |
|
749 | + $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
750 | + try |
|
751 | + { |
|
752 | + $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
753 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
754 | + //->eval_rule($rule['rule']); |
|
755 | 755 | |
756 | - if ($evalr == true) |
|
757 | - { |
|
758 | - //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
759 | - $action=$rule['action_match']; |
|
760 | - $this->logging->log('action OK : '.$action,INFO ); |
|
761 | - if ($action >= 0) |
|
762 | - { |
|
763 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
764 | - { |
|
765 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
766 | - } |
|
767 | - else |
|
768 | - { |
|
769 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
770 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
771 | - } |
|
772 | - } |
|
773 | - else |
|
774 | - { |
|
775 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
776 | - } |
|
777 | - $this->trapToDb=($action==-2)?false:true; |
|
778 | - } |
|
779 | - else |
|
780 | - { |
|
781 | - //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
756 | + if ($evalr == true) |
|
757 | + { |
|
758 | + //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
759 | + $action=$rule['action_match']; |
|
760 | + $this->logging->log('action OK : '.$action,INFO ); |
|
761 | + if ($action >= 0) |
|
762 | + { |
|
763 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
764 | + { |
|
765 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
766 | + } |
|
767 | + else |
|
768 | + { |
|
769 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
770 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
771 | + } |
|
772 | + } |
|
773 | + else |
|
774 | + { |
|
775 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
776 | + } |
|
777 | + $this->trapToDb=($action==-2)?false:true; |
|
778 | + } |
|
779 | + else |
|
780 | + { |
|
781 | + //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
782 | 782 | |
783 | - $action=$rule['action_nomatch']; |
|
784 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
785 | - if ($action >= 0) |
|
786 | - { |
|
787 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
788 | - { |
|
789 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
790 | - } |
|
791 | - else |
|
792 | - { |
|
793 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
794 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
795 | - } |
|
796 | - } |
|
797 | - else |
|
798 | - { |
|
799 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
800 | - } |
|
801 | - $this->trapToDb=($action==-2)?false:true; |
|
802 | - } |
|
803 | - // Put name in source_name |
|
804 | - if (!isset($this->trapData['source_name'])) |
|
805 | - { |
|
806 | - $this->trapData['source_name']=$rule['host_name']; |
|
807 | - } |
|
808 | - else |
|
809 | - { |
|
810 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
811 | - { // only add if not present |
|
812 | - $this->trapData['source_name'].=','.$rule['host_name']; |
|
813 | - } |
|
814 | - } |
|
815 | - } |
|
816 | - catch (Exception $e) |
|
817 | - { |
|
818 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
819 | - $this->trapAction.=' ERR : '.$e->getMessage(); |
|
820 | - $this->trapData['status']='error'; |
|
821 | - } |
|
783 | + $action=$rule['action_nomatch']; |
|
784 | + $this->logging->log('action NOK : '.$action,INFO ); |
|
785 | + if ($action >= 0) |
|
786 | + { |
|
787 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
788 | + { |
|
789 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
790 | + } |
|
791 | + else |
|
792 | + { |
|
793 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
794 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
795 | + } |
|
796 | + } |
|
797 | + else |
|
798 | + { |
|
799 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
800 | + } |
|
801 | + $this->trapToDb=($action==-2)?false:true; |
|
802 | + } |
|
803 | + // Put name in source_name |
|
804 | + if (!isset($this->trapData['source_name'])) |
|
805 | + { |
|
806 | + $this->trapData['source_name']=$rule['host_name']; |
|
807 | + } |
|
808 | + else |
|
809 | + { |
|
810 | + if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
811 | + { // only add if not present |
|
812 | + $this->trapData['source_name'].=','.$rule['host_name']; |
|
813 | + } |
|
814 | + } |
|
815 | + } |
|
816 | + catch (Exception $e) |
|
817 | + { |
|
818 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
819 | + $this->trapAction.=' ERR : '.$e->getMessage(); |
|
820 | + $this->trapData['status']='error'; |
|
821 | + } |
|
822 | 822 | |
823 | - } |
|
824 | - if ($this->trapData['status']=='error') |
|
825 | - { |
|
826 | - $this->trapToDb=true; // Always put errors in DB for the use can see |
|
827 | - } |
|
828 | - else |
|
829 | - { |
|
830 | - $this->trapData['status']='done'; |
|
831 | - } |
|
832 | - } |
|
823 | + } |
|
824 | + if ($this->trapData['status']=='error') |
|
825 | + { |
|
826 | + $this->trapToDb=true; // Always put errors in DB for the use can see |
|
827 | + } |
|
828 | + else |
|
829 | + { |
|
830 | + $this->trapData['status']='done'; |
|
831 | + } |
|
832 | + } |
|
833 | 833 | |
834 | - /** Add Time a action to rule |
|
835 | - * @param string $time : time to process to insert in SQL |
|
836 | - */ |
|
837 | - public function add_rule_final($time) |
|
838 | - { |
|
839 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
840 | - if ($this->trapAction==null) |
|
841 | - { |
|
842 | - $this->trapAction='No action'; |
|
843 | - } |
|
844 | - $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
845 | - if ($db_conn->query($sql) === false) { |
|
846 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
847 | - } |
|
848 | - } |
|
834 | + /** Add Time a action to rule |
|
835 | + * @param string $time : time to process to insert in SQL |
|
836 | + */ |
|
837 | + public function add_rule_final($time) |
|
838 | + { |
|
839 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
840 | + if ($this->trapAction==null) |
|
841 | + { |
|
842 | + $this->trapAction='No action'; |
|
843 | + } |
|
844 | + $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
845 | + if ($db_conn->query($sql) === false) { |
|
846 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
847 | + } |
|
848 | + } |
|
849 | 849 | |
850 | - /*********** UTILITIES *********************/ |
|
850 | + /*********** UTILITIES *********************/ |
|
851 | 851 | |
852 | - /** reset service to OK after time defined in rule |
|
853 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
854 | - * @return integer : not in use |
|
855 | - **/ |
|
856 | - public function reset_services() |
|
857 | - { |
|
858 | - // Get all services not in 'ok' state |
|
859 | - $sql_query="SELECT s.service_object_id, |
|
852 | + /** reset service to OK after time defined in rule |
|
853 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
854 | + * @return integer : not in use |
|
855 | + **/ |
|
856 | + public function reset_services() |
|
857 | + { |
|
858 | + // Get all services not in 'ok' state |
|
859 | + $sql_query="SELECT s.service_object_id, |
|
860 | 860 | UNIX_TIMESTAMP(s.last_check) AS last_check, |
861 | 861 | s.current_state as state, |
862 | 862 | v.name1 as host_name, |
@@ -864,43 +864,43 @@ discard block |
||
864 | 864 | FROM icinga_servicestatus AS s |
865 | 865 | LEFT JOIN icinga_objects as v ON s.service_object_id=v.object_id |
866 | 866 | WHERE s.current_state != 0;"; |
867 | - $db_conn=$this->trapsDB->db_connect_ido(); |
|
868 | - if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
869 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
870 | - return 0; |
|
871 | - } |
|
872 | - $services=$services_db->fetchAll(); |
|
873 | - |
|
874 | - // Get all rules |
|
875 | - $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
876 | - $db_conn2=$this->trapsDB->db_connect_trap(); |
|
877 | - if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
878 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
879 | - return 0; |
|
880 | - } |
|
881 | - $rules=$rules_db->fetchAll(); |
|
882 | - |
|
883 | - $now=date('U'); |
|
884 | - |
|
885 | - $numreset=0; |
|
886 | - foreach ($rules as $rule) |
|
887 | - { |
|
888 | - foreach ($services as $service) |
|
889 | - { |
|
890 | - if ($service['service_name'] == $rule['service_name'] && |
|
891 | - $service['host_name'] == $rule['host_name'] && |
|
892 | - ($service['last_check'] + $rule['revert_ok']) < $now) |
|
893 | - { |
|
894 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
895 | - $numreset++; |
|
896 | - } |
|
897 | - } |
|
898 | - } |
|
899 | - echo "\n"; |
|
900 | - echo $numreset . " service(s) reset to OK\n"; |
|
901 | - return 0; |
|
902 | - |
|
903 | - } |
|
867 | + $db_conn=$this->trapsDB->db_connect_ido(); |
|
868 | + if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
869 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
870 | + return 0; |
|
871 | + } |
|
872 | + $services=$services_db->fetchAll(); |
|
873 | + |
|
874 | + // Get all rules |
|
875 | + $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
876 | + $db_conn2=$this->trapsDB->db_connect_trap(); |
|
877 | + if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
878 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
879 | + return 0; |
|
880 | + } |
|
881 | + $rules=$rules_db->fetchAll(); |
|
882 | + |
|
883 | + $now=date('U'); |
|
884 | + |
|
885 | + $numreset=0; |
|
886 | + foreach ($rules as $rule) |
|
887 | + { |
|
888 | + foreach ($services as $service) |
|
889 | + { |
|
890 | + if ($service['service_name'] == $rule['service_name'] && |
|
891 | + $service['host_name'] == $rule['host_name'] && |
|
892 | + ($service['last_check'] + $rule['revert_ok']) < $now) |
|
893 | + { |
|
894 | + $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
895 | + $numreset++; |
|
896 | + } |
|
897 | + } |
|
898 | + } |
|
899 | + echo "\n"; |
|
900 | + echo $numreset . " service(s) reset to OK\n"; |
|
901 | + return 0; |
|
902 | + |
|
903 | + } |
|
904 | 904 | |
905 | 905 | |
906 | 906 | } |
907 | 907 | \ 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,41 +247,41 @@ 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 | continue; |
258 | 258 | } |
259 | - 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')) |
|
259 | + 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 | 260 | { |
261 | 261 | $this->trapData['trap_oid']=$matches[2]; |
262 | 262 | continue; |
263 | 263 | } |
264 | - if ( $this->useSnmpTrapAddess === TRUE && preg_match('/'.$this->snmpTrapAddressOID.'/', $matches[1]) == 1) |
|
264 | + if ($this->useSnmpTrapAddess === TRUE && preg_match('/'.$this->snmpTrapAddressOID.'/', $matches[1]) == 1) |
|
265 | 265 | { |
266 | - $this->logging->log('Found relayed trap from ' . $matches[2] . ' relayed by ' .$this->trapData['source_ip'],DEBUG); |
|
267 | - if (preg_match('/^[0-9\.]+$/',$matches[2]) == 0 && preg_match('/^[0-9a-fA-F:]+$/',$matches[2]) == 0) |
|
266 | + $this->logging->log('Found relayed trap from '.$matches[2].' relayed by '.$this->trapData['source_ip'], DEBUG); |
|
267 | + if (preg_match('/^[0-9\.]+$/', $matches[2]) == 0 && preg_match('/^[0-9a-fA-F:]+$/', $matches[2]) == 0) |
|
268 | 268 | { |
269 | - $this->logging->log('Value of SnmpTrapAddess ('.$this->snmpTrapAddressOID.') is not IP : ' .$matches[2],WARN,''); |
|
269 | + $this->logging->log('Value of SnmpTrapAddess ('.$this->snmpTrapAddressOID.') is not IP : '.$matches[2], WARN, ''); |
|
270 | 270 | continue; |
271 | 271 | } |
272 | - $this->trapData['source_ip'] = $matches[2]; |
|
272 | + $this->trapData['source_ip']=$matches[2]; |
|
273 | 273 | continue; |
274 | 274 | } |
275 | - $object= new stdClass; |
|
276 | - $object->oid =$matches[1]; |
|
277 | - $object->value = $matches[2]; |
|
278 | - array_push($this->trapDataExt,$object); |
|
275 | + $object=new stdClass; |
|
276 | + $object->oid=$matches[1]; |
|
277 | + $object->value=$matches[2]; |
|
278 | + array_push($this->trapDataExt, $object); |
|
279 | 279 | } |
280 | 280 | |
281 | - if ($this->trapData['trap_oid']=='unknown') |
|
281 | + if ($this->trapData['trap_oid'] == 'unknown') |
|
282 | 282 | { |
283 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
284 | - $this->logging->log('no trap oid found',ERROR,''); |
|
283 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trapData['source_ip']); |
|
284 | + $this->logging->log('no trap oid found', ERROR, ''); |
|
285 | 285 | } |
286 | 286 | |
287 | 287 | // Translate oids. |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | } |
304 | 304 | |
305 | 305 | |
306 | - $this->trapData['status']= 'waiting'; |
|
306 | + $this->trapData['status']='waiting'; |
|
307 | 307 | |
308 | 308 | return $this->trapData; |
309 | 309 | } |
@@ -319,40 +319,40 @@ discard block |
||
319 | 319 | $db_conn=$this->trapsDB->db_connect_trap(); |
320 | 320 | |
321 | 321 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
322 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
322 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
323 | 323 | if (($ret_code=$db_conn->query($sql)) === false) { |
324 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
324 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
325 | 325 | } |
326 | 326 | $name=$ret_code->fetch(); |
327 | 327 | if ($name['name'] != null) |
328 | 328 | { |
329 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
329 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | // Also check if it is an instance of OID |
333 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
333 | + $oid_instance=preg_replace('/\.[0-9]+$/', '', $oid); |
|
334 | 334 | |
335 | 335 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
336 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
336 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
337 | 337 | if (($ret_code=$db_conn->query($sql)) === false) { |
338 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
338 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
339 | 339 | } |
340 | 340 | $name=$ret_code->fetch(); |
341 | 341 | if ($name['name'] != null) |
342 | 342 | { |
343 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
343 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
344 | 344 | } |
345 | 345 | |
346 | 346 | // Try to get oid name from snmptranslate |
347 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
347 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
348 | 348 | ' '.$oid); |
349 | 349 | $matches=array(); |
350 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
351 | - if ($ret_code===0 || $ret_code === false) { |
|
350 | + $ret_code=preg_match('/(.*)::(.*)/', $translate, $matches); |
|
351 | + if ($ret_code === 0 || $ret_code === false) { |
|
352 | 352 | return NULL; |
353 | 353 | } else { |
354 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
355 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
354 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO); |
|
355 | + return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]); |
|
356 | 356 | } |
357 | 357 | } |
358 | 358 | |
@@ -363,90 +363,90 @@ discard block |
||
363 | 363 | **/ |
364 | 364 | public function eraseOldTraps($days=0) |
365 | 365 | { |
366 | - if ($days==0) |
|
366 | + if ($days == 0) |
|
367 | 367 | { |
368 | 368 | if (($days=$this->getDBConfig('db_remove_days')) == null) |
369 | 369 | { |
370 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
370 | + $this->logging->log('No days specified & no db value : no tap erase', WARN, ''); |
|
371 | 371 | return; |
372 | 372 | } |
373 | 373 | } |
374 | 374 | $db_conn=$this->trapsDB->db_connect_trap(); |
375 | - $daysago = strtotime("-".$days." day"); |
|
376 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
375 | + $daysago=strtotime("-".$days." day"); |
|
376 | + $sql='delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';'; |
|
377 | 377 | if ($db_conn->query($sql) === false) { |
378 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
378 | + $this->logging->log('Error erasing traps : '.$sql, ERROR, ''); |
|
379 | 379 | } |
380 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
380 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | /** Write error to received trap database |
384 | 384 | */ |
385 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
385 | + public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null) |
|
386 | 386 | { |
387 | 387 | |
388 | 388 | $db_conn=$this->trapsDB->db_connect_trap(); |
389 | 389 | |
390 | 390 | // add date time |
391 | - $insert_col ='date_received,status'; |
|
392 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
391 | + $insert_col='date_received,status'; |
|
392 | + $insert_val="'".date("Y-m-d H:i:s")."','error'"; |
|
393 | 393 | |
394 | - if ($sourceIP !=null) |
|
394 | + if ($sourceIP != null) |
|
395 | 395 | { |
396 | - $insert_col .=',source_ip'; |
|
397 | - $insert_val .=",'". $sourceIP ."'"; |
|
396 | + $insert_col.=',source_ip'; |
|
397 | + $insert_val.=",'".$sourceIP."'"; |
|
398 | 398 | } |
399 | - if ($trapoid !=null) |
|
399 | + if ($trapoid != null) |
|
400 | 400 | { |
401 | - $insert_col .=',trap_oid'; |
|
402 | - $insert_val .=",'". $trapoid ."'"; |
|
401 | + $insert_col.=',trap_oid'; |
|
402 | + $insert_val.=",'".$trapoid."'"; |
|
403 | 403 | } |
404 | - $insert_col .=',status_detail'; |
|
405 | - $insert_val .=",'". $message ."'"; |
|
404 | + $insert_col.=',status_detail'; |
|
405 | + $insert_val.=",'".$message."'"; |
|
406 | 406 | |
407 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
407 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
408 | 408 | |
409 | 409 | switch ($this->trapsDB->trapDBType) |
410 | 410 | { |
411 | 411 | case 'pgsql': |
412 | - $sql .= ' RETURNING id;'; |
|
413 | - $this->logging->log('sql : '.$sql,INFO); |
|
412 | + $sql.=' RETURNING id;'; |
|
413 | + $this->logging->log('sql : '.$sql, INFO); |
|
414 | 414 | if (($ret_code=$db_conn->query($sql)) === false) { |
415 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
415 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
416 | 416 | } |
417 | - $this->logging->log('SQL insertion OK',INFO ); |
|
417 | + $this->logging->log('SQL insertion OK', INFO); |
|
418 | 418 | // Get last id to insert oid/values in secondary table |
419 | 419 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
420 | 420 | |
421 | - $this->logging->log('Erreur recuperation id',1,''); |
|
421 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
422 | 422 | } |
423 | - if (! isset($inserted_id_ret['id'])) { |
|
424 | - $this->logging->log('Error getting id',1,''); |
|
423 | + if (!isset($inserted_id_ret['id'])) { |
|
424 | + $this->logging->log('Error getting id', 1, ''); |
|
425 | 425 | } |
426 | 426 | $this->trapId=$inserted_id_ret['id']; |
427 | 427 | break; |
428 | 428 | case 'mysql': |
429 | - $sql .= ';'; |
|
430 | - $this->logging->log('sql : '.$sql,INFO ); |
|
429 | + $sql.=';'; |
|
430 | + $this->logging->log('sql : '.$sql, INFO); |
|
431 | 431 | if ($db_conn->query($sql) === false) { |
432 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
432 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
433 | 433 | } |
434 | - $this->logging->log('SQL insertion OK',INFO ); |
|
434 | + $this->logging->log('SQL insertion OK', INFO); |
|
435 | 435 | // Get last id to insert oid/values in secondary table |
436 | 436 | $sql='SELECT LAST_INSERT_ID();'; |
437 | 437 | if (($ret_code=$db_conn->query($sql)) === false) { |
438 | - $this->logging->log('Erreur recuperation id',1,''); |
|
438 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
439 | 439 | } |
440 | 440 | |
441 | 441 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
442 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
442 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
443 | 443 | $this->trapId=$inserted_id; |
444 | 444 | break; |
445 | 445 | default: |
446 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
446 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
447 | 447 | } |
448 | 448 | |
449 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
449 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
450 | 450 | } |
451 | 451 | |
452 | 452 | /** Write trap data to trap database |
@@ -463,86 +463,86 @@ discard block |
||
463 | 463 | $insert_col=''; |
464 | 464 | $insert_val=''; |
465 | 465 | // add date time |
466 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
466 | + $this->trapData['date_received']=date("Y-m-d H:i:s"); |
|
467 | 467 | |
468 | 468 | $firstcol=1; |
469 | 469 | foreach ($this->trapData as $col => $val) |
470 | 470 | { |
471 | - if ($firstcol==0) |
|
471 | + if ($firstcol == 0) |
|
472 | 472 | { |
473 | - $insert_col .=','; |
|
474 | - $insert_val .=','; |
|
473 | + $insert_col.=','; |
|
474 | + $insert_val.=','; |
|
475 | 475 | } |
476 | - $insert_col .= $col ; |
|
477 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
476 | + $insert_col.=$col; |
|
477 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
478 | 478 | $firstcol=0; |
479 | 479 | } |
480 | 480 | |
481 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
481 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
482 | 482 | switch ($this->trapsDB->trapDBType) |
483 | 483 | { |
484 | 484 | case 'pgsql': |
485 | - $sql .= ' RETURNING id;'; |
|
486 | - $this->logging->log('sql : '.$sql,INFO ); |
|
485 | + $sql.=' RETURNING id;'; |
|
486 | + $this->logging->log('sql : '.$sql, INFO); |
|
487 | 487 | if (($ret_code=$db_conn->query($sql)) === false) { |
488 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
488 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
489 | 489 | } |
490 | - $this->logging->log('SQL insertion OK',INFO ); |
|
490 | + $this->logging->log('SQL insertion OK', INFO); |
|
491 | 491 | // Get last id to insert oid/values in secondary table |
492 | 492 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
493 | 493 | |
494 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
494 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
495 | 495 | } |
496 | - if (! isset($inserted_id_ret['id'])) { |
|
497 | - $this->logging->log('Error getting id',ERROR,''); |
|
496 | + if (!isset($inserted_id_ret['id'])) { |
|
497 | + $this->logging->log('Error getting id', ERROR, ''); |
|
498 | 498 | } |
499 | 499 | $this->trapId=$inserted_id_ret['id']; |
500 | 500 | break; |
501 | 501 | case 'mysql': |
502 | - $sql .= ';'; |
|
503 | - $this->logging->log('sql : '.$sql,INFO ); |
|
502 | + $sql.=';'; |
|
503 | + $this->logging->log('sql : '.$sql, INFO); |
|
504 | 504 | if ($db_conn->query($sql) === false) { |
505 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
505 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
506 | 506 | } |
507 | - $this->logging->log('SQL insertion OK',INFO ); |
|
507 | + $this->logging->log('SQL insertion OK', INFO); |
|
508 | 508 | // Get last id to insert oid/values in secondary table |
509 | 509 | $sql='SELECT LAST_INSERT_ID();'; |
510 | 510 | if (($ret_code=$db_conn->query($sql)) === false) { |
511 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
511 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
512 | 512 | } |
513 | 513 | |
514 | 514 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
515 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
515 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
516 | 516 | $this->trapId=$inserted_id; |
517 | 517 | break; |
518 | 518 | default: |
519 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
519 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, ''); |
|
520 | 520 | } |
521 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
521 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
522 | 522 | |
523 | 523 | // Fill trap extended data table |
524 | 524 | foreach ($this->trapDataExt as $value) { |
525 | 525 | // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
526 | 526 | $firstcol=1; |
527 | - $value->trap_id = $this->trapId; |
|
527 | + $value->trap_id=$this->trapId; |
|
528 | 528 | $insert_col=''; |
529 | 529 | $insert_val=''; |
530 | 530 | foreach ($value as $col => $val) |
531 | 531 | { |
532 | - if ($firstcol==0) |
|
532 | + if ($firstcol == 0) |
|
533 | 533 | { |
534 | - $insert_col .=','; |
|
535 | - $insert_val .=','; |
|
534 | + $insert_col.=','; |
|
535 | + $insert_val.=','; |
|
536 | 536 | } |
537 | - $insert_col .= $col; |
|
538 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
537 | + $insert_col.=$col; |
|
538 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
539 | 539 | $firstcol=0; |
540 | 540 | } |
541 | 541 | |
542 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
542 | + $sql='INSERT INTO '.$this->dbPrefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');'; |
|
543 | 543 | |
544 | 544 | if ($db_conn->query($sql) === false) { |
545 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
545 | + $this->logging->log('Erreur insertion data : '.$sql, WARN, ''); |
|
546 | 546 | } |
547 | 547 | } |
548 | 548 | } |
@@ -552,14 +552,14 @@ discard block |
||
552 | 552 | * @param $oid string oid in numeric |
553 | 553 | * @return mixed|boolean : PDO object or false |
554 | 554 | */ |
555 | - protected function getRules($ip,$oid) |
|
555 | + protected function getRules($ip, $oid) |
|
556 | 556 | { |
557 | 557 | $db_conn=$this->trapsDB->db_connect_trap(); |
558 | 558 | // fetch rules based on IP in rule and OID |
559 | 559 | $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
560 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
560 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
561 | 561 | if (($ret_code=$db_conn->query($sql)) === false) { |
562 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
562 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
563 | 563 | return false; |
564 | 564 | } |
565 | 565 | $rules_all=$ret_code->fetchAll(); |
@@ -568,7 +568,7 @@ discard block |
||
568 | 568 | $rule_ret_key=0; |
569 | 569 | foreach ($rules_all as $key => $rule) |
570 | 570 | { |
571 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
571 | + if ($rule['ip4'] == $ip || $rule['ip6'] == $ip) |
|
572 | 572 | { |
573 | 573 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
574 | 574 | //TODO : get host name by API (and check if correct in rule). |
@@ -576,7 +576,7 @@ discard block |
||
576 | 576 | continue; |
577 | 577 | } |
578 | 578 | // TODO : get hosts IP by API |
579 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
579 | + if (isset($rule['host_group_name']) && $rule['host_group_name'] != null) |
|
580 | 580 | { // get ips of group members by oid |
581 | 581 | $db_conn2=$this->trapsDB->db_connect_ido(); |
582 | 582 | $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
@@ -587,15 +587,15 @@ discard block |
||
587 | 587 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
588 | 588 | WHERE o.name1='".$rule['host_group_name']."';"; |
589 | 589 | if (($ret_code2=$db_conn2->query($sql)) === false) { |
590 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
590 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
591 | 591 | continue; |
592 | 592 | } |
593 | 593 | $grouphosts=$ret_code2->fetchAll(); |
594 | 594 | //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
595 | - foreach ( $grouphosts as $host) |
|
595 | + foreach ($grouphosts as $host) |
|
596 | 596 | { |
597 | 597 | //echo $host['ip4']."\n"; |
598 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
598 | + if ($host['ip4'] == $ip || $host['ip6'] == $ip) |
|
599 | 599 | { |
600 | 600 | //echo "Rule added \n"; |
601 | 601 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
@@ -618,7 +618,7 @@ discard block |
||
618 | 618 | $db_conn=$this->trapsDB->db_connect_trap(); |
619 | 619 | $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
620 | 620 | if ($db_conn->query($sql) === false) { |
621 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
621 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
622 | 622 | } |
623 | 623 | } |
624 | 624 | |
@@ -630,23 +630,23 @@ discard block |
||
630 | 630 | * @param string $display |
631 | 631 | * @returnn bool true is service check was sent without error |
632 | 632 | */ |
633 | - public function serviceCheckResult($host,$service,$state,$display) |
|
633 | + public function serviceCheckResult($host, $service, $state, $display) |
|
634 | 634 | { |
635 | 635 | if ($this->apiUse === false) |
636 | 636 | { |
637 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
638 | - $host.';' .$service .';' . $state . ';'.$display; |
|
639 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
637 | + $send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'. |
|
638 | + $host.';'.$service.';'.$state.';'.$display; |
|
639 | + $this->logging->log($send." : to : ".$this->icinga2cmd, INFO); |
|
640 | 640 | |
641 | 641 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
642 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
642 | + exec('echo "'.$send.'" > '.$this->icinga2cmd); |
|
643 | 643 | return true; |
644 | 644 | } |
645 | 645 | else |
646 | 646 | { |
647 | 647 | // Get perfdata if found |
648 | 648 | $matches=array(); |
649 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
649 | + if (preg_match('/(.*)\|(.*)/', $display, $matches) == 1) |
|
650 | 650 | { |
651 | 651 | $display=$matches[1]; |
652 | 652 | $perfdata=$matches[2]; |
@@ -656,17 +656,17 @@ discard block |
||
656 | 656 | $perfdata=''; |
657 | 657 | } |
658 | 658 | |
659 | - $api = $this->getAPI(); |
|
659 | + $api=$this->getAPI(); |
|
660 | 660 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
661 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
661 | + list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display, $perfdata); |
|
662 | 662 | if ($retcode == false) |
663 | 663 | { |
664 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
664 | + $this->logging->log("Error sending result : ".$retmessage, WARN, ''); |
|
665 | 665 | return false; |
666 | 666 | } |
667 | 667 | else |
668 | 668 | { |
669 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
669 | + $this->logging->log("Sent result : ".$retmessage, INFO); |
|
670 | 670 | return true; |
671 | 671 | } |
672 | 672 | } |
@@ -674,7 +674,7 @@ discard block |
||
674 | 674 | |
675 | 675 | public function getHostByIP($ip) |
676 | 676 | { |
677 | - $api = $this->getAPI(); |
|
677 | + $api=$this->getAPI(); |
|
678 | 678 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
679 | 679 | return $api->getHostByIP($ip); |
680 | 680 | } |
@@ -687,35 +687,35 @@ discard block |
||
687 | 687 | protected function applyDisplay($display) |
688 | 688 | { |
689 | 689 | $matches=array(); |
690 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
690 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $display, $matches) == 1) |
|
691 | 691 | { |
692 | 692 | $oid=$matches[1]; |
693 | 693 | $found=0; |
694 | 694 | // Test and transform regexp |
695 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
695 | + $oidR=$this->ruleClass->regexp_eval($oid); |
|
696 | 696 | |
697 | - foreach($this->trapDataExt as $val) |
|
697 | + foreach ($this->trapDataExt as $val) |
|
698 | 698 | { |
699 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
699 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
700 | 700 | { |
701 | - $val->value=preg_replace('/"/','',$val->value); |
|
701 | + $val->value=preg_replace('/"/', '', $val->value); |
|
702 | 702 | $rep=0; |
703 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
704 | - if ($rep==0) |
|
703 | + $display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep); |
|
704 | + if ($rep == 0) |
|
705 | 705 | { |
706 | - $this->logging->log("Error in display",WARN,''); |
|
706 | + $this->logging->log("Error in display", WARN, ''); |
|
707 | 707 | return $display; |
708 | 708 | } |
709 | 709 | $found=1; |
710 | 710 | break; |
711 | 711 | } |
712 | 712 | } |
713 | - if ($found==0) |
|
713 | + if ($found == 0) |
|
714 | 714 | { |
715 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
716 | - if ($rep==0) |
|
715 | + $display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep); |
|
716 | + if ($rep == 0) |
|
717 | 717 | { |
718 | - $this->logging->log("Error in display",WARN,''); |
|
718 | + $this->logging->log("Error in display", WARN, ''); |
|
719 | 719 | return $display; |
720 | 720 | } |
721 | 721 | } |
@@ -727,11 +727,11 @@ discard block |
||
727 | 727 | */ |
728 | 728 | public function applyRules() |
729 | 729 | { |
730 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
730 | + $rules=$this->getRules($this->trapData['source_ip'], $this->trapData['trap_oid']); |
|
731 | 731 | |
732 | - if ($rules===false || count($rules)==0) |
|
732 | + if ($rules === false || count($rules) == 0) |
|
733 | 733 | { |
734 | - $this->logging->log('No rules found for this trap',INFO ); |
|
734 | + $this->logging->log('No rules found for this trap', INFO); |
|
735 | 735 | $this->trapData['status']='unknown'; |
736 | 736 | $this->trapToDb=true; |
737 | 737 | return; |
@@ -746,59 +746,59 @@ discard block |
||
746 | 746 | $service_name=$rule['service_name']; |
747 | 747 | |
748 | 748 | $display=$this->applyDisplay($rule['display']); |
749 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
749 | + $this->trapAction=($this->trapAction == null) ? '' : $this->trapAction.', '; |
|
750 | 750 | try |
751 | 751 | { |
752 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
753 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
752 | + $this->logging->log('Rule to eval : '.$rule['rule'], INFO); |
|
753 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt); |
|
754 | 754 | //->eval_rule($rule['rule']); |
755 | 755 | |
756 | 756 | if ($evalr == true) |
757 | 757 | { |
758 | 758 | //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
759 | 759 | $action=$rule['action_match']; |
760 | - $this->logging->log('action OK : '.$action,INFO ); |
|
760 | + $this->logging->log('action OK : '.$action, INFO); |
|
761 | 761 | if ($action >= 0) |
762 | 762 | { |
763 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
763 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
764 | 764 | { |
765 | 765 | $this->trapAction.='Error sending status : check cmd/API'; |
766 | 766 | } |
767 | 767 | else |
768 | 768 | { |
769 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
769 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
770 | 770 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
771 | 771 | } |
772 | 772 | } |
773 | 773 | else |
774 | 774 | { |
775 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
775 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
776 | 776 | } |
777 | - $this->trapToDb=($action==-2)?false:true; |
|
777 | + $this->trapToDb=($action == -2) ?false:true; |
|
778 | 778 | } |
779 | 779 | else |
780 | 780 | { |
781 | 781 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
782 | 782 | |
783 | 783 | $action=$rule['action_nomatch']; |
784 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
784 | + $this->logging->log('action NOK : '.$action, INFO); |
|
785 | 785 | if ($action >= 0) |
786 | 786 | { |
787 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
787 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
788 | 788 | { |
789 | 789 | $this->trapAction.='Error sending status : check cmd/API'; |
790 | 790 | } |
791 | 791 | else |
792 | 792 | { |
793 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
793 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
794 | 794 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
795 | 795 | } |
796 | 796 | } |
797 | 797 | else |
798 | 798 | { |
799 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
799 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
800 | 800 | } |
801 | - $this->trapToDb=($action==-2)?false:true; |
|
801 | + $this->trapToDb=($action == -2) ?false:true; |
|
802 | 802 | } |
803 | 803 | // Put name in source_name |
804 | 804 | if (!isset($this->trapData['source_name'])) |
@@ -807,7 +807,7 @@ discard block |
||
807 | 807 | } |
808 | 808 | else |
809 | 809 | { |
810 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
810 | + if (!preg_match('/'.$rule['host_name'].'/', $this->trapData['source_name'])) |
|
811 | 811 | { // only add if not present |
812 | 812 | $this->trapData['source_name'].=','.$rule['host_name']; |
813 | 813 | } |
@@ -815,13 +815,13 @@ discard block |
||
815 | 815 | } |
816 | 816 | catch (Exception $e) |
817 | 817 | { |
818 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
818 | + $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, ''); |
|
819 | 819 | $this->trapAction.=' ERR : '.$e->getMessage(); |
820 | 820 | $this->trapData['status']='error'; |
821 | 821 | } |
822 | 822 | |
823 | 823 | } |
824 | - if ($this->trapData['status']=='error') |
|
824 | + if ($this->trapData['status'] == 'error') |
|
825 | 825 | { |
826 | 826 | $this->trapToDb=true; // Always put errors in DB for the use can see |
827 | 827 | } |
@@ -837,13 +837,13 @@ discard block |
||
837 | 837 | public function add_rule_final($time) |
838 | 838 | { |
839 | 839 | $db_conn=$this->trapsDB->db_connect_trap(); |
840 | - if ($this->trapAction==null) |
|
840 | + if ($this->trapAction == null) |
|
841 | 841 | { |
842 | 842 | $this->trapAction='No action'; |
843 | 843 | } |
844 | 844 | $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
845 | 845 | if ($db_conn->query($sql) === false) { |
846 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
846 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
847 | 847 | } |
848 | 848 | } |
849 | 849 | |
@@ -866,7 +866,7 @@ discard block |
||
866 | 866 | WHERE s.current_state != 0;"; |
867 | 867 | $db_conn=$this->trapsDB->db_connect_ido(); |
868 | 868 | if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
869 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
869 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
870 | 870 | return 0; |
871 | 871 | } |
872 | 872 | $services=$services_db->fetchAll(); |
@@ -875,7 +875,7 @@ discard block |
||
875 | 875 | $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
876 | 876 | $db_conn2=$this->trapsDB->db_connect_trap(); |
877 | 877 | if (($rules_db=$db_conn2->query($sql_query)) === false) { |
878 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
878 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
879 | 879 | return 0; |
880 | 880 | } |
881 | 881 | $rules=$rules_db->fetchAll(); |
@@ -891,13 +891,13 @@ discard block |
||
891 | 891 | $service['host_name'] == $rule['host_name'] && |
892 | 892 | ($service['last_check'] + $rule['revert_ok']) < $now) |
893 | 893 | { |
894 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
894 | + $this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
895 | 895 | $numreset++; |
896 | 896 | } |
897 | 897 | } |
898 | 898 | } |
899 | 899 | echo "\n"; |
900 | - echo $numreset . " service(s) reset to OK\n"; |
|
900 | + echo $numreset." service(s) reset to OK\n"; |
|
901 | 901 | return 0; |
902 | 902 | |
903 | 903 | } |
@@ -98,8 +98,7 @@ discard block |
||
98 | 98 | { |
99 | 99 | $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
100 | 100 | $this->logSetup=true; |
101 | - } |
|
102 | - else |
|
101 | + } else |
|
103 | 102 | { |
104 | 103 | $this->logSetup=false; |
105 | 104 | } |
@@ -129,7 +128,10 @@ discard block |
||
129 | 128 | $this->getDatabaseOptions(); // Get options in database |
130 | 129 | |
131 | 130 | //*************** Setup API |
132 | - if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
131 | + if ($this->apiUse === true) { |
|
132 | + $this->getAPI(); |
|
133 | + } |
|
134 | + // Setup API |
|
133 | 135 | |
134 | 136 | //*************** Setup MIB |
135 | 137 | $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
@@ -238,8 +240,7 @@ discard block |
||
238 | 240 | { |
239 | 241 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
240 | 242 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
241 | - } |
|
242 | - else |
|
243 | + } else |
|
243 | 244 | { |
244 | 245 | $this->trapData['source_ip']=$matches[1]; |
245 | 246 | $this->trapData['destination_ip']=$matches[3]; |
@@ -439,7 +440,9 @@ discard block |
||
439 | 440 | } |
440 | 441 | |
441 | 442 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
442 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
443 | + if ($inserted_id==false) { |
|
444 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
445 | + } |
|
443 | 446 | $this->trapId=$inserted_id; |
444 | 447 | break; |
445 | 448 | default: |
@@ -455,7 +458,9 @@ discard block |
||
455 | 458 | { |
456 | 459 | |
457 | 460 | // If action is ignore -> don't send t DB |
458 | - if ($this->trapToDb === false) return; |
|
461 | + if ($this->trapToDb === false) { |
|
462 | + return; |
|
463 | + } |
|
459 | 464 | |
460 | 465 | |
461 | 466 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -512,7 +517,9 @@ discard block |
||
512 | 517 | } |
513 | 518 | |
514 | 519 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
515 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
520 | + if ($inserted_id==false) { |
|
521 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
522 | + } |
|
516 | 523 | $this->trapId=$inserted_id; |
517 | 524 | break; |
518 | 525 | default: |
@@ -641,8 +648,7 @@ discard block |
||
641 | 648 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
642 | 649 | exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
643 | 650 | return true; |
644 | - } |
|
645 | - else |
|
651 | + } else |
|
646 | 652 | { |
647 | 653 | // Get perfdata if found |
648 | 654 | $matches=array(); |
@@ -650,8 +656,7 @@ discard block |
||
650 | 656 | { |
651 | 657 | $display=$matches[1]; |
652 | 658 | $perfdata=$matches[2]; |
653 | - } |
|
654 | - else |
|
659 | + } else |
|
655 | 660 | { |
656 | 661 | $perfdata=''; |
657 | 662 | } |
@@ -663,8 +668,7 @@ discard block |
||
663 | 668 | { |
664 | 669 | $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
665 | 670 | return false; |
666 | - } |
|
667 | - else |
|
671 | + } else |
|
668 | 672 | { |
669 | 673 | $this->logging->log( "Sent result : " .$retmessage,INFO ); |
670 | 674 | return true; |
@@ -763,20 +767,17 @@ discard block |
||
763 | 767 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
764 | 768 | { |
765 | 769 | $this->trapAction.='Error sending status : check cmd/API'; |
766 | - } |
|
767 | - else |
|
770 | + } else |
|
768 | 771 | { |
769 | 772 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
770 | 773 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
771 | 774 | } |
772 | - } |
|
773 | - else |
|
775 | + } else |
|
774 | 776 | { |
775 | 777 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
776 | 778 | } |
777 | 779 | $this->trapToDb=($action==-2)?false:true; |
778 | - } |
|
779 | - else |
|
780 | + } else |
|
780 | 781 | { |
781 | 782 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
782 | 783 | |
@@ -787,14 +788,12 @@ discard block |
||
787 | 788 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
788 | 789 | { |
789 | 790 | $this->trapAction.='Error sending status : check cmd/API'; |
790 | - } |
|
791 | - else |
|
791 | + } else |
|
792 | 792 | { |
793 | 793 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
794 | 794 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
795 | 795 | } |
796 | - } |
|
797 | - else |
|
796 | + } else |
|
798 | 797 | { |
799 | 798 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
800 | 799 | } |
@@ -804,16 +803,14 @@ discard block |
||
804 | 803 | if (!isset($this->trapData['source_name'])) |
805 | 804 | { |
806 | 805 | $this->trapData['source_name']=$rule['host_name']; |
807 | - } |
|
808 | - else |
|
806 | + } else |
|
809 | 807 | { |
810 | 808 | if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
811 | 809 | { // only add if not present |
812 | 810 | $this->trapData['source_name'].=','.$rule['host_name']; |
813 | 811 | } |
814 | 812 | } |
815 | - } |
|
816 | - catch (Exception $e) |
|
813 | + } catch (Exception $e) |
|
817 | 814 | { |
818 | 815 | $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
819 | 816 | $this->trapAction.=' ERR : '.$e->getMessage(); |
@@ -824,8 +821,7 @@ discard block |
||
824 | 821 | if ($this->trapData['status']=='error') |
825 | 822 | { |
826 | 823 | $this->trapToDb=true; // Always put errors in DB for the use can see |
827 | - } |
|
828 | - else |
|
824 | + } else |
|
829 | 825 | { |
830 | 826 | $this->trapData['status']='done'; |
831 | 827 | } |