@@ -56,14 +56,15 @@ discard block |
||
| 56 | 56 | $start=$item; |
| 57 | 57 | $parenthesis_count=0; |
| 58 | 58 | while (($item < strlen($rule)) // Not end of string AND |
| 59 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 59 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) { |
|
| 60 | + // Closing ')' or embeded () |
|
| 60 | 61 | { |
| 61 | 62 | if ($rule[$item] == '"' ) |
| 62 | 63 | { // pass through string |
| 63 | 64 | $item++; |
| 65 | + } |
|
| 64 | 66 | $item=$this->eval_getNext($rule,$item,'"'); |
| 65 | - } |
|
| 66 | - else{ |
|
| 67 | + } else{ |
|
| 67 | 68 | if ($rule[$item] == '(') |
| 68 | 69 | { |
| 69 | 70 | $parenthesis_count++; |
@@ -91,7 +92,9 @@ discard block |
||
| 91 | 92 | { |
| 92 | 93 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
| 93 | 94 | } |
| 94 | - while ($rule[$item]==' ') $item++; |
|
| 95 | + while ($rule[$item]==' ') { |
|
| 96 | + $item++; |
|
| 97 | + } |
|
| 95 | 98 | if (preg_match('/[\-0-9\.]/',$rule[$item])) |
| 96 | 99 | { // number |
| 97 | 100 | return $this->get_number($rule, $item); |
@@ -125,7 +128,9 @@ discard block |
||
| 125 | 128 | |
| 126 | 129 | protected function eval_getOper($rule,&$item) |
| 127 | 130 | { |
| 128 | - while ($rule[$item]==' ') $item++; |
|
| 131 | + while ($rule[$item]==' ') { |
|
| 132 | + $item++; |
|
| 133 | + } |
|
| 129 | 134 | switch ($rule[$item]) |
| 130 | 135 | { |
| 131 | 136 | case '<': |
@@ -152,12 +157,13 @@ discard block |
||
| 152 | 157 | |
| 153 | 158 | private function check_negate_first($rule,&$item) |
| 154 | 159 | { |
| 155 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 160 | + if ( $rule[$item] == '!') { |
|
| 161 | + // If '!' found, negate next expression. |
|
| 156 | 162 | { |
| 157 | 163 | $item++; |
| 158 | - return true; |
|
| 159 | 164 | } |
| 160 | - else |
|
| 165 | + return true; |
|
| 166 | + } else |
|
| 161 | 167 | { |
| 162 | 168 | return false; |
| 163 | 169 | } |
@@ -177,7 +183,10 @@ discard block |
||
| 177 | 183 | case '&': $retVal= ($val1 && $val2); break; |
| 178 | 184 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
| 179 | 185 | } |
| 180 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 186 | + if ($negate === true) { |
|
| 187 | + $retVal = ! $retVal; |
|
| 188 | + } |
|
| 189 | + // Inverse result if negate before expression |
|
| 181 | 190 | |
| 182 | 191 | return $retVal; |
| 183 | 192 | } |
@@ -198,10 +207,14 @@ discard block |
||
| 198 | 207 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
| 199 | 208 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
| 200 | 209 | |
| 201 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 210 | + if ($item==strlen($rule)) { |
|
| 211 | + // If only element, return value, but only boolean |
|
| 202 | 212 | { |
| 203 | 213 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
| 204 | - if ($negate === true) $val1= ! $val1; |
|
| 214 | + } |
|
| 215 | + if ($negate === true) { |
|
| 216 | + $val1= ! $val1; |
|
| 217 | + } |
|
| 205 | 218 | return $val1; |
| 206 | 219 | } |
| 207 | 220 | |
@@ -210,31 +223,41 @@ discard block |
||
| 210 | 223 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
| 211 | 224 | |
| 212 | 225 | // Third element : number, string or () |
| 213 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 226 | + if ( $rule[$item] == '!') { |
|
| 227 | + // starts with a ! so evaluate whats next |
|
| 214 | 228 | { |
| 215 | 229 | $item++; |
| 216 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 230 | + } |
|
| 231 | + if ($typec != 1) { |
|
| 232 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 233 | + } |
|
| 217 | 234 | $val2= ! $this->evaluation($rule,$item); |
| 218 | 235 | $type2=2; // result is a boolean |
| 219 | - } |
|
| 220 | - else |
|
| 236 | + } else |
|
| 221 | 237 | { |
| 222 | 238 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
| 223 | 239 | } |
| 224 | 240 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 225 | 241 | |
| 226 | - if ($type1!=$type2) // cannot compare different types |
|
| 242 | + if ($type1!=$type2) { |
|
| 243 | + // cannot compare different types |
|
| 227 | 244 | { |
| 228 | 245 | throw new Exception("Cannot compare string & number : ".$rule); |
| 229 | 246 | } |
| 230 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 247 | + } |
|
| 248 | + if ($typec==1 && $type1 !=2) { |
|
| 249 | + // cannot use & or | with string/number |
|
| 231 | 250 | { |
| 232 | 251 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
| 233 | 252 | } |
| 253 | + } |
|
| 234 | 254 | |
| 235 | 255 | $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
| 236 | 256 | |
| 237 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 257 | + if ($item==strlen($rule)) { |
|
| 258 | + return $retVal; |
|
| 259 | + } |
|
| 260 | + // End of string : return evaluation |
|
| 238 | 261 | // check for logical operator : |
| 239 | 262 | switch ($rule[$item]) |
| 240 | 263 | { |
@@ -262,7 +285,9 @@ discard block |
||
| 262 | 285 | $rule2.=$rule[$item]; |
| 263 | 286 | $item++; |
| 264 | 287 | } |
| 265 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 288 | + if ($item == strlen ($rule)) { |
|
| 289 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 290 | + } |
|
| 266 | 291 | $rule2.=$rule[$item]; |
| 267 | 292 | $item++; |
| 268 | 293 | continue; |
@@ -283,10 +308,12 @@ discard block |
||
| 283 | 308 | |
| 284 | 309 | public function eval_rule($rule,$oidList) |
| 285 | 310 | { |
| 286 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 311 | + if ($rule==null || $rule == '') { |
|
| 312 | + // Empty rule is always true |
|
| 287 | 313 | { |
| 288 | 314 | return true; |
| 289 | 315 | } |
| 316 | + } |
|
| 290 | 317 | $matches=array(); |
| 291 | 318 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
| 292 | 319 | { |
@@ -73,9 +73,9 @@ discard block |
||
| 73 | 73 | { |
| 74 | 74 | $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
| 75 | 75 | $this->logSetup=true; |
| 76 | + } else { |
|
| 77 | + $this->logSetup=false; |
|
| 76 | 78 | } |
| 77 | - else |
|
| 78 | - $this->logSetup=false; |
|
| 79 | 79 | $this->logging->log('Loggin started', INFO); |
| 80 | 80 | |
| 81 | 81 | // Get options from ini files |
@@ -93,7 +93,10 @@ discard block |
||
| 93 | 93 | $this->setupDatabase($trapConfig); // Setup database class |
| 94 | 94 | |
| 95 | 95 | $this->getDatabaseOptions(); // Get options in database |
| 96 | - if ($this->api_use === true) $this->getAPI(); // Setup API |
|
| 96 | + if ($this->api_use === true) { |
|
| 97 | + $this->getAPI(); |
|
| 98 | + } |
|
| 99 | + // Setup API |
|
| 97 | 100 | |
| 98 | 101 | $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
| 99 | 102 | |
@@ -129,8 +132,7 @@ discard block |
||
| 129 | 132 | } |
| 130 | 133 | $this->logging->log($message,$log_level); |
| 131 | 134 | return false; |
| 132 | - } |
|
| 133 | - else |
|
| 135 | + } else |
|
| 134 | 136 | { |
| 135 | 137 | $option_var=$option_array[$option_category][$option_name]; |
| 136 | 138 | return true; |
@@ -194,7 +196,10 @@ discard block |
||
| 194 | 196 | |
| 195 | 197 | $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
| 196 | 198 | |
| 197 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 199 | + if ($this->api_use === true) { |
|
| 200 | + return; |
|
| 201 | + } |
|
| 202 | + // In case of API use, no IDO is necessary |
|
| 198 | 203 | |
| 199 | 204 | // IDO Database |
| 200 | 205 | if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
@@ -219,9 +224,11 @@ discard block |
||
| 219 | 224 | protected function getDatabaseOptions() |
| 220 | 225 | { |
| 221 | 226 | // Database options |
| 222 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
| 227 | + if ($this->logSetup === false) { |
|
| 228 | + // Only if logging was no setup in constructor |
|
| 223 | 229 | { |
| 224 | 230 | $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
| 231 | + } |
|
| 225 | 232 | $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
| 226 | 233 | $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
| 227 | 234 | } |
@@ -230,7 +237,9 @@ discard block |
||
| 230 | 237 | protected function getDBConfigIfSet($element,&$variable) |
| 231 | 238 | { |
| 232 | 239 | $value=$this->getDBConfig($element); |
| 233 | - if ($value != 'null') $variable=$value; |
|
| 240 | + if ($value != 'null') { |
|
| 241 | + $variable=$value; |
|
| 242 | + } |
|
| 234 | 243 | } |
| 235 | 244 | |
| 236 | 245 | /** |
@@ -318,8 +327,7 @@ discard block |
||
| 318 | 327 | { |
| 319 | 328 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
| 320 | 329 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
| 321 | - } |
|
| 322 | - else |
|
| 330 | + } else |
|
| 323 | 331 | { |
| 324 | 332 | $this->trap_data['source_ip']=$matches[1]; |
| 325 | 333 | $this->trap_data['destination_ip']=$matches[3]; |
@@ -334,14 +342,12 @@ discard block |
||
| 334 | 342 | if ($ret_code===0 || $ret_code===false) |
| 335 | 343 | { |
| 336 | 344 | $this->logging->log('No match on trap data : '.$vars,WARN,''); |
| 337 | - } |
|
| 338 | - else |
|
| 345 | + } else |
|
| 339 | 346 | { |
| 340 | 347 | 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')) |
| 341 | 348 | { |
| 342 | 349 | $this->trap_data['trap_oid']=$matches[2]; |
| 343 | - } |
|
| 344 | - else |
|
| 350 | + } else |
|
| 345 | 351 | { |
| 346 | 352 | $object= new stdClass; |
| 347 | 353 | $object->oid =$matches[1]; |
@@ -512,7 +518,9 @@ discard block |
||
| 512 | 518 | } |
| 513 | 519 | |
| 514 | 520 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 515 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 521 | + if ($inserted_id==false) { |
|
| 522 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 523 | + } |
|
| 516 | 524 | $this->trap_id=$inserted_id; |
| 517 | 525 | break; |
| 518 | 526 | default: |
@@ -528,7 +536,9 @@ discard block |
||
| 528 | 536 | { |
| 529 | 537 | |
| 530 | 538 | // If action is ignore -> don't send t DB |
| 531 | - if ($this->trap_to_db === false) return; |
|
| 539 | + if ($this->trap_to_db === false) { |
|
| 540 | + return; |
|
| 541 | + } |
|
| 532 | 542 | |
| 533 | 543 | |
| 534 | 544 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -585,7 +595,9 @@ discard block |
||
| 585 | 595 | } |
| 586 | 596 | |
| 587 | 597 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 588 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 598 | + if ($inserted_id==false) { |
|
| 599 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 600 | + } |
|
| 589 | 601 | $this->trap_id=$inserted_id; |
| 590 | 602 | break; |
| 591 | 603 | default: |
@@ -714,8 +726,7 @@ discard block |
||
| 714 | 726 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
| 715 | 727 | exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
| 716 | 728 | return true; |
| 717 | - } |
|
| 718 | - else |
|
| 729 | + } else |
|
| 719 | 730 | { |
| 720 | 731 | // Get perfdata if found |
| 721 | 732 | $matches=array(); |
@@ -723,8 +734,7 @@ discard block |
||
| 723 | 734 | { |
| 724 | 735 | $display=$matches[1]; |
| 725 | 736 | $perfdata=$matches[2]; |
| 726 | - } |
|
| 727 | - else |
|
| 737 | + } else |
|
| 728 | 738 | { |
| 729 | 739 | $perfdata=''; |
| 730 | 740 | } |
@@ -736,8 +746,7 @@ discard block |
||
| 736 | 746 | { |
| 737 | 747 | $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
| 738 | 748 | return false; |
| 739 | - } |
|
| 740 | - else |
|
| 749 | + } else |
|
| 741 | 750 | { |
| 742 | 751 | $this->logging->log( "Sent result : " .$retmessage,INFO ); |
| 743 | 752 | return true; |
@@ -833,20 +842,17 @@ discard block |
||
| 833 | 842 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 834 | 843 | { |
| 835 | 844 | $this->trap_action.='Error sending status : check cmd/API'; |
| 836 | - } |
|
| 837 | - else |
|
| 845 | + } else |
|
| 838 | 846 | { |
| 839 | 847 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 840 | 848 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 841 | 849 | } |
| 842 | - } |
|
| 843 | - else |
|
| 850 | + } else |
|
| 844 | 851 | { |
| 845 | 852 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 846 | 853 | } |
| 847 | 854 | $this->trap_to_db=($action==-2)?false:true; |
| 848 | - } |
|
| 849 | - else |
|
| 855 | + } else |
|
| 850 | 856 | { |
| 851 | 857 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
| 852 | 858 | |
@@ -857,14 +863,12 @@ discard block |
||
| 857 | 863 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
| 858 | 864 | { |
| 859 | 865 | $this->trap_action.='Error sending status : check cmd/API'; |
| 860 | - } |
|
| 861 | - else |
|
| 866 | + } else |
|
| 862 | 867 | { |
| 863 | 868 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 864 | 869 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 865 | 870 | } |
| 866 | - } |
|
| 867 | - else |
|
| 871 | + } else |
|
| 868 | 872 | { |
| 869 | 873 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 870 | 874 | } |
@@ -874,16 +878,14 @@ discard block |
||
| 874 | 878 | if (!isset($this->trap_data['source_name'])) |
| 875 | 879 | { |
| 876 | 880 | $this->trap_data['source_name']=$rule['host_name']; |
| 877 | - } |
|
| 878 | - else |
|
| 881 | + } else |
|
| 879 | 882 | { |
| 880 | 883 | if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
| 881 | 884 | { // only add if not present |
| 882 | 885 | $this->trap_data['source_name'].=','.$rule['host_name']; |
| 883 | 886 | } |
| 884 | 887 | } |
| 885 | - } |
|
| 886 | - catch (Exception $e) |
|
| 888 | + } catch (Exception $e) |
|
| 887 | 889 | { |
| 888 | 890 | $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
| 889 | 891 | $this->trap_action.=' ERR : '.$e->getMessage(); |
@@ -894,8 +896,7 @@ discard block |
||
| 894 | 896 | if ($this->trap_data['status']=='error') |
| 895 | 897 | { |
| 896 | 898 | $this->trap_to_db=true; // Always put errors in DB for the use can see |
| 897 | - } |
|
| 898 | - else |
|
| 899 | + } else |
|
| 899 | 900 | { |
| 900 | 901 | $this->trap_data['status']='done'; |
| 901 | 902 | } |