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