@@ -71,9 +71,9 @@ discard block |
||
| 71 | 71 | { |
| 72 | 72 | $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
| 73 | 73 | $this->logSetup=true; |
| 74 | + } else { |
|
| 75 | + $this->logSetup=false; |
|
| 74 | 76 | } |
| 75 | - else |
|
| 76 | - $this->logSetup=false; |
|
| 77 | 77 | $this->logging->log('Loggin started', INFO); |
| 78 | 78 | |
| 79 | 79 | // Get options from ini files |
@@ -86,7 +86,10 @@ discard block |
||
| 86 | 86 | $this->setupDatabase($trapConfig); // Setup database class |
| 87 | 87 | |
| 88 | 88 | $this->getDatabaseOptions(); // Get options in database |
| 89 | - if ($this->api_use === true) $this->getAPI(); // Setup API |
|
| 89 | + if ($this->api_use === true) { |
|
| 90 | + $this->getAPI(); |
|
| 91 | + } |
|
| 92 | + // Setup API |
|
| 90 | 93 | |
| 91 | 94 | $this->trap_data=array( |
| 92 | 95 | 'source_ip' => 'unknown', |
@@ -118,8 +121,7 @@ discard block |
||
| 118 | 121 | } |
| 119 | 122 | $this->logging->log($message,$log_level,'syslog'); |
| 120 | 123 | return false; |
| 121 | - } |
|
| 122 | - else |
|
| 124 | + } else |
|
| 123 | 125 | { |
| 124 | 126 | $option_var=$option_array[$option_category][$option_name]; |
| 125 | 127 | return true; |
@@ -185,7 +187,10 @@ discard block |
||
| 185 | 187 | |
| 186 | 188 | $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName]); |
| 187 | 189 | |
| 188 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 190 | + if ($this->api_use === true) { |
|
| 191 | + return; |
|
| 192 | + } |
|
| 193 | + // In case of API use, no IDO is necessary |
|
| 189 | 194 | |
| 190 | 195 | // IDO Database |
| 191 | 196 | if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
@@ -210,9 +215,11 @@ discard block |
||
| 210 | 215 | protected function getDatabaseOptions() |
| 211 | 216 | { |
| 212 | 217 | // Database options |
| 213 | - if ($this->logSetup == false) // Only if logging was no setup in constructor |
|
| 218 | + if ($this->logSetup == false) { |
|
| 219 | + // Only if logging was no setup in constructor |
|
| 214 | 220 | { |
| 215 | 221 | $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
| 222 | + } |
|
| 216 | 223 | $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
| 217 | 224 | $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
| 218 | 225 | } |
@@ -221,7 +228,9 @@ discard block |
||
| 221 | 228 | protected function getDBConfigIfSet($element,&$variable) |
| 222 | 229 | { |
| 223 | 230 | $value=$this->getDBConfig($element); |
| 224 | - if ($value != 'null') $variable=$value; |
|
| 231 | + if ($value != 'null') { |
|
| 232 | + $variable=$value; |
|
| 233 | + } |
|
| 225 | 234 | } |
| 226 | 235 | |
| 227 | 236 | /** |
@@ -309,8 +318,7 @@ discard block |
||
| 309 | 318 | { |
| 310 | 319 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
| 311 | 320 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
| 312 | - } |
|
| 313 | - else |
|
| 321 | + } else |
|
| 314 | 322 | { |
| 315 | 323 | $this->trap_data['source_ip']=$matches[1]; |
| 316 | 324 | $this->trap_data['destination_ip']=$matches[3]; |
@@ -325,14 +333,12 @@ discard block |
||
| 325 | 333 | if ($ret_code===0 || $ret_code===false) |
| 326 | 334 | { |
| 327 | 335 | $this->logging->log('No match on trap data : '.$vars,WARN,''); |
| 328 | - } |
|
| 329 | - else |
|
| 336 | + } else |
|
| 330 | 337 | { |
| 331 | 338 | 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')) |
| 332 | 339 | { |
| 333 | 340 | $this->trap_data['trap_oid']=$matches[2]; |
| 334 | - } |
|
| 335 | - else |
|
| 341 | + } else |
|
| 336 | 342 | { |
| 337 | 343 | $object= new stdClass; |
| 338 | 344 | $object->oid =$matches[1]; |
@@ -503,7 +509,9 @@ discard block |
||
| 503 | 509 | } |
| 504 | 510 | |
| 505 | 511 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 506 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 512 | + if ($inserted_id==false) { |
|
| 513 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 514 | + } |
|
| 507 | 515 | $this->trap_id=$inserted_id; |
| 508 | 516 | break; |
| 509 | 517 | default: |
@@ -519,7 +527,9 @@ discard block |
||
| 519 | 527 | { |
| 520 | 528 | |
| 521 | 529 | // If action is ignore -> don't send t DB |
| 522 | - if ($this->trap_to_db === false) return; |
|
| 530 | + if ($this->trap_to_db === false) { |
|
| 531 | + return; |
|
| 532 | + } |
|
| 523 | 533 | |
| 524 | 534 | |
| 525 | 535 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -576,7 +586,9 @@ discard block |
||
| 576 | 586 | } |
| 577 | 587 | |
| 578 | 588 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 579 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 589 | + if ($inserted_id==false) { |
|
| 590 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 591 | + } |
|
| 580 | 592 | $this->trap_id=$inserted_id; |
| 581 | 593 | break; |
| 582 | 594 | default: |
@@ -705,8 +717,7 @@ discard block |
||
| 705 | 717 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
| 706 | 718 | exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
| 707 | 719 | return true; |
| 708 | - } |
|
| 709 | - else |
|
| 720 | + } else |
|
| 710 | 721 | { |
| 711 | 722 | $api = $this->getAPI(); |
| 712 | 723 | $api->setCredentials($this->api_username, $this->api_password); |
@@ -715,8 +726,7 @@ discard block |
||
| 715 | 726 | { |
| 716 | 727 | $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
| 717 | 728 | return false; |
| 718 | - } |
|
| 719 | - else |
|
| 729 | + } else |
|
| 720 | 730 | { |
| 721 | 731 | $this->logging->log( "Sent result : " .$retmessage,INFO ); |
| 722 | 732 | return true; |
@@ -776,7 +786,9 @@ discard block |
||
| 776 | 786 | /***************** Eval & tokenizer functions ****************/ |
| 777 | 787 | protected function eval_getElement($rule,&$item) |
| 778 | 788 | { |
| 779 | - while ($rule[$item]==' ') $item++; |
|
| 789 | + while ($rule[$item]==' ') { |
|
| 790 | + $item++; |
|
| 791 | + } |
|
| 780 | 792 | if (preg_match('/[0-9\.]/',$rule[$item])) |
| 781 | 793 | { // number |
| 782 | 794 | |
@@ -803,14 +815,15 @@ discard block |
||
| 803 | 815 | $start=$item; |
| 804 | 816 | $parenthesis_count=0; |
| 805 | 817 | while (($item < strlen($rule)) // Not end of string AND |
| 806 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 818 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) { |
|
| 819 | + // Closing ')' or embeded () |
|
| 807 | 820 | { |
| 808 | 821 | if ($rule[$item] == '"' ) |
| 809 | 822 | { // pass through string |
| 810 | 823 | $item++; |
| 824 | + } |
|
| 811 | 825 | $item=$this->eval_getNext($rule,$item,'"'); |
| 812 | - } |
|
| 813 | - else{ |
|
| 826 | + } else{ |
|
| 814 | 827 | if ($rule[$item] == '(') |
| 815 | 828 | { |
| 816 | 829 | $parenthesis_count++; |
@@ -838,13 +851,17 @@ discard block |
||
| 838 | 851 | protected function eval_getNext($rule,$item,$tok) |
| 839 | 852 | { |
| 840 | 853 | while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;} |
| 841 | - if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 854 | + if ($item==strlen($rule)) { |
|
| 855 | + throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 856 | + } |
|
| 842 | 857 | return $item+1; |
| 843 | 858 | } |
| 844 | 859 | |
| 845 | 860 | protected function eval_getOper($rule,&$item) |
| 846 | 861 | { |
| 847 | - while ($rule[$item]==' ') $item++; |
|
| 862 | + while ($rule[$item]==' ') { |
|
| 863 | + $item++; |
|
| 864 | + } |
|
| 848 | 865 | switch ($rule[$item]) |
| 849 | 866 | { |
| 850 | 867 | case '<': |
@@ -880,12 +897,13 @@ discard block |
||
| 880 | 897 | public function evaluation($rule,&$item) |
| 881 | 898 | { |
| 882 | 899 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
| 883 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 900 | + if ( $rule[$item] == '!') { |
|
| 901 | + // If '!' found, negate next expression. |
|
| 884 | 902 | { |
| 885 | 903 | $negate=true; |
| 886 | - $item++; |
|
| 887 | 904 | } |
| 888 | - else |
|
| 905 | + $item++; |
|
| 906 | + } else |
|
| 889 | 907 | { |
| 890 | 908 | $negate=false; |
| 891 | 909 | } |
@@ -893,10 +911,14 @@ discard block |
||
| 893 | 911 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
| 894 | 912 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
| 895 | 913 | |
| 896 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 914 | + if ($item==strlen($rule)) { |
|
| 915 | + // If only element, return value, but only boolean |
|
| 897 | 916 | { |
| 898 | 917 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
| 899 | - if ($negate === true) $val1= ! $val1; |
|
| 918 | + } |
|
| 919 | + if ($negate === true) { |
|
| 920 | + $val1= ! $val1; |
|
| 921 | + } |
|
| 900 | 922 | return $val1; |
| 901 | 923 | } |
| 902 | 924 | |
@@ -905,27 +927,34 @@ discard block |
||
| 905 | 927 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
| 906 | 928 | |
| 907 | 929 | // Third element : number, string or () |
| 908 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 930 | + if ( $rule[$item] == '!') { |
|
| 931 | + // starts with a ! so evaluate whats next |
|
| 909 | 932 | { |
| 910 | 933 | $item++; |
| 911 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 934 | + } |
|
| 935 | + if ($typec != 1) { |
|
| 936 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 937 | + } |
|
| 912 | 938 | $val2= ! $this->evaluation($rule,$item); |
| 913 | 939 | $type2=2; // result is a boolean |
| 914 | - } |
|
| 915 | - else |
|
| 940 | + } else |
|
| 916 | 941 | { |
| 917 | 942 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
| 918 | 943 | } |
| 919 | 944 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 920 | 945 | |
| 921 | - if ($type1!=$type2) // cannot compare different types |
|
| 946 | + if ($type1!=$type2) { |
|
| 947 | + // cannot compare different types |
|
| 922 | 948 | { |
| 923 | 949 | throw new Exception("Cannot compare string & number : ".$rule); |
| 924 | 950 | } |
| 925 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 951 | + } |
|
| 952 | + if ($typec==1 && $type1 !=2) { |
|
| 953 | + // cannot use & or | with string/number |
|
| 926 | 954 | { |
| 927 | 955 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
| 928 | 956 | } |
| 957 | + } |
|
| 929 | 958 | |
| 930 | 959 | switch ($comp){ |
| 931 | 960 | case '<': $retVal= ($val1 < $val2); break; |
@@ -939,9 +968,15 @@ discard block |
||
| 939 | 968 | case '&': $retVal= ($val1 && $val2); break; |
| 940 | 969 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
| 941 | 970 | } |
| 942 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 971 | + if ($negate === true) { |
|
| 972 | + $retVal = ! $retVal; |
|
| 973 | + } |
|
| 974 | + // Inverse result if negate before expression |
|
| 943 | 975 | |
| 944 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 976 | + if ($item==strlen($rule)) { |
|
| 977 | + return $retVal; |
|
| 978 | + } |
|
| 979 | + // End of string : return evaluation |
|
| 945 | 980 | // check for logical operator : |
| 946 | 981 | switch ($rule[$item]) |
| 947 | 982 | { |
@@ -968,7 +1003,9 @@ discard block |
||
| 968 | 1003 | $rule2.=$rule[$item]; |
| 969 | 1004 | $item++; |
| 970 | 1005 | } |
| 971 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 1006 | + if ($item == strlen ($rule)) { |
|
| 1007 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 1008 | + } |
|
| 972 | 1009 | $rule2.=$rule[$item]; |
| 973 | 1010 | $item++; |
| 974 | 1011 | continue; |
@@ -988,10 +1025,12 @@ discard block |
||
| 988 | 1025 | |
| 989 | 1026 | protected function eval_rule($rule) |
| 990 | 1027 | { |
| 991 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 1028 | + if ($rule==null || $rule == '') { |
|
| 1029 | + // Empty rule is always true |
|
| 992 | 1030 | { |
| 993 | 1031 | return true; |
| 994 | 1032 | } |
| 1033 | + } |
|
| 995 | 1034 | $matches=array(); |
| 996 | 1035 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
| 997 | 1036 | { |
@@ -1078,20 +1117,17 @@ discard block |
||
| 1078 | 1117 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 1079 | 1118 | { |
| 1080 | 1119 | $this->trap_action.='Error sending status : check cmd/API'; |
| 1081 | - } |
|
| 1082 | - else |
|
| 1120 | + } else |
|
| 1083 | 1121 | { |
| 1084 | 1122 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 1085 | 1123 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 1086 | 1124 | } |
| 1087 | - } |
|
| 1088 | - else |
|
| 1125 | + } else |
|
| 1089 | 1126 | { |
| 1090 | 1127 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 1091 | 1128 | } |
| 1092 | 1129 | $this->trap_to_db=($action==-2)?false:true; |
| 1093 | - } |
|
| 1094 | - else |
|
| 1130 | + } else |
|
| 1095 | 1131 | { |
| 1096 | 1132 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
| 1097 | 1133 | |
@@ -1102,14 +1138,12 @@ discard block |
||
| 1102 | 1138 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
| 1103 | 1139 | { |
| 1104 | 1140 | $this->trap_action.='Error sending status : check cmd/API'; |
| 1105 | - } |
|
| 1106 | - else |
|
| 1141 | + } else |
|
| 1107 | 1142 | { |
| 1108 | 1143 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 1109 | 1144 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 1110 | 1145 | } |
| 1111 | - } |
|
| 1112 | - else |
|
| 1146 | + } else |
|
| 1113 | 1147 | { |
| 1114 | 1148 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 1115 | 1149 | } |
@@ -1119,16 +1153,14 @@ discard block |
||
| 1119 | 1153 | if (!isset($this->trap_data['source_name'])) |
| 1120 | 1154 | { |
| 1121 | 1155 | $this->trap_data['source_name']=$rule['host_name']; |
| 1122 | - } |
|
| 1123 | - else |
|
| 1156 | + } else |
|
| 1124 | 1157 | { |
| 1125 | 1158 | if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
| 1126 | 1159 | { // only add if not present |
| 1127 | 1160 | $this->trap_data['source_name'].=','.$rule['host_name']; |
| 1128 | 1161 | } |
| 1129 | 1162 | } |
| 1130 | - } |
|
| 1131 | - catch (Exception $e) |
|
| 1163 | + } catch (Exception $e) |
|
| 1132 | 1164 | { |
| 1133 | 1165 | $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
| 1134 | 1166 | $this->trap_action.=' ERR : '.$e->getMessage(); |
@@ -1139,8 +1171,7 @@ discard block |
||
| 1139 | 1171 | if ($this->trap_data['status']=='error') |
| 1140 | 1172 | { |
| 1141 | 1173 | $this->trap_to_db=true; // Always put errors in DB for the use can see |
| 1142 | - } |
|
| 1143 | - else |
|
| 1174 | + } else |
|
| 1144 | 1175 | { |
| 1145 | 1176 | $this->trap_data['status']='done'; |
| 1146 | 1177 | } |
@@ -1195,8 +1226,7 @@ discard block |
||
| 1195 | 1226 | if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
| 1196 | 1227 | { |
| 1197 | 1228 | $cur_table='table '.$cur_table_array[1]; |
| 1198 | - } |
|
| 1199 | - else |
|
| 1229 | + } else |
|
| 1200 | 1230 | { |
| 1201 | 1231 | $cur_table='secret SQL stuff :-)'; |
| 1202 | 1232 | } |
@@ -1240,8 +1270,7 @@ discard block |
||
| 1240 | 1270 | if ($this->trapsDB->trapDBType == 'pgsql') |
| 1241 | 1271 | { |
| 1242 | 1272 | $prefix .= 'update_pgsql/schema_'; |
| 1243 | - } |
|
| 1244 | - else |
|
| 1273 | + } else |
|
| 1245 | 1274 | { |
| 1246 | 1275 | $prefix .= 'update_sql/schema_'; |
| 1247 | 1276 | } |
@@ -1287,7 +1316,10 @@ discard block |
||
| 1287 | 1316 | $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 1288 | 1317 | while (($line=fgets($input_stream)) !== false) |
| 1289 | 1318 | { |
| 1290 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 1319 | + if (preg_match('/^#/', $line)) { |
|
| 1320 | + continue; |
|
| 1321 | + } |
|
| 1322 | + // ignore comment lines |
|
| 1291 | 1323 | $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
| 1292 | 1324 | if (preg_match('/; *$/', $newline)) |
| 1293 | 1325 | { |
@@ -1299,8 +1331,7 @@ discard block |
||
| 1299 | 1331 | if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
| 1300 | 1332 | { |
| 1301 | 1333 | $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
| 1302 | - } |
|
| 1303 | - else |
|
| 1334 | + } else |
|
| 1304 | 1335 | { |
| 1305 | 1336 | $cur_table='secret SQL stuff :-)'; |
| 1306 | 1337 | //$cur_table=$newline; |
@@ -1438,8 +1469,7 @@ discard block |
||
| 1438 | 1469 | } |
| 1439 | 1470 | $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
| 1440 | 1471 | return 1; |
| 1441 | - } |
|
| 1442 | - else |
|
| 1472 | + } else |
|
| 1443 | 1473 | { |
| 1444 | 1474 | $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
| 1445 | 1475 | return 0; |
@@ -1455,7 +1485,9 @@ discard block |
||
| 1455 | 1485 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 1456 | 1486 | ', :syntax, :type_enum, :description )'; |
| 1457 | 1487 | |
| 1458 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 1488 | + if ($this->trapsDB->trapDBType == 'pgsql') { |
|
| 1489 | + $sql .= 'RETURNING id'; |
|
| 1490 | + } |
|
| 1459 | 1491 | |
| 1460 | 1492 | $sqlQuery=$db_conn->prepare($sql); |
| 1461 | 1493 | |
@@ -1495,7 +1527,9 @@ discard block |
||
| 1495 | 1527 | } |
| 1496 | 1528 | |
| 1497 | 1529 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 1498 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1530 | + if ($inserted_id==false) { |
|
| 1531 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1532 | + } |
|
| 1499 | 1533 | $this->dbOidIndex[$oid]['id']=$inserted_id; |
| 1500 | 1534 | break; |
| 1501 | 1535 | default: |
@@ -1748,12 +1782,14 @@ discard block |
||
| 1748 | 1782 | $name=$match[1]; // Name |
| 1749 | 1783 | $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
| 1750 | 1784 | |
| 1751 | - if ($type==0) // object type=0 : check if v1 trap |
|
| 1785 | + if ($type==0) { |
|
| 1786 | + // object type=0 : check if v1 trap |
|
| 1752 | 1787 | { |
| 1753 | 1788 | // Check if next is suboid -> in that case is cannot be a trap |
| 1754 | 1789 | if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
| 1755 | 1790 | { |
| 1756 | 1791 | $time_check2 += microtime(true) - $time_1; |
| 1792 | + } |
|
| 1757 | 1793 | $time_check2N++; |
| 1758 | 1794 | continue; |
| 1759 | 1795 | } |
@@ -1770,9 +1806,11 @@ discard block |
||
| 1770 | 1806 | // Force as trap. |
| 1771 | 1807 | $type=21; |
| 1772 | 1808 | } |
| 1773 | - if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
| 1809 | + if ($onlyTraps===true && $type!=21) { |
|
| 1810 | + // if only traps and not a trap, continue |
|
| 1774 | 1811 | { |
| 1775 | 1812 | $time_check3 += microtime(true) - $time_1; |
| 1813 | + } |
|
| 1776 | 1814 | $time_check3N++; |
| 1777 | 1815 | continue; |
| 1778 | 1816 | } |
@@ -1780,7 +1818,10 @@ discard block |
||
| 1780 | 1818 | $time_num_traps++; |
| 1781 | 1819 | |
| 1782 | 1820 | $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
| 1783 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
| 1821 | + if ($display_progress) { |
|
| 1822 | + echo '#'; |
|
| 1823 | + } |
|
| 1824 | + // echo a # when trap found |
|
| 1784 | 1825 | |
| 1785 | 1826 | // get trap objects & source MIB |
| 1786 | 1827 | unset($snmptrans); |
@@ -1798,7 +1839,9 @@ discard block |
||
| 1798 | 1839 | $trapMib=$match[1]; |
| 1799 | 1840 | |
| 1800 | 1841 | $numLine=1;$trapDesc=''; |
| 1801 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 1842 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) { |
|
| 1843 | + $numLine++; |
|
| 1844 | + } |
|
| 1802 | 1845 | if (isset($snmptrans[$numLine])) |
| 1803 | 1846 | { |
| 1804 | 1847 | $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
@@ -1820,7 +1863,9 @@ discard block |
||
| 1820 | 1863 | if (($update==0) && ($check_change===false)) |
| 1821 | 1864 | { // Trapd didn't change & force check disabled |
| 1822 | 1865 | $time_objects += microtime(true) - $time_1; |
| 1823 | - if ($display_progress) echo "C"; |
|
| 1866 | + if ($display_progress) { |
|
| 1867 | + echo "C"; |
|
| 1868 | + } |
|
| 1824 | 1869 | continue; |
| 1825 | 1870 | } |
| 1826 | 1871 | |
@@ -30,15 +30,13 @@ |
||
| 30 | 30 | |
| 31 | 31 | $Trap->add_rule_final(microtime(true) - $time1); |
| 32 | 32 | |
| 33 | -} |
|
| 34 | -catch (Exception $e) |
|
| 33 | +} catch (Exception $e) |
|
| 35 | 34 | { |
| 36 | 35 | if ($Trap == null) |
| 37 | 36 | { // Exception in trap creation : log in display & syslog |
| 38 | 37 | $logging = new Logging(); |
| 39 | 38 | $logging->log("Caught exception creating Trap class",2); |
| 40 | - } |
|
| 41 | - else |
|
| 39 | + } else |
|
| 42 | 40 | { |
| 43 | 41 | $Trap->trapLog("Exception : ". $e->getMessage(),2,0); |
| 44 | 42 | } |
@@ -87,10 +87,11 @@ discard block |
||
| 87 | 87 | try |
| 88 | 88 | { |
| 89 | 89 | $dbconn = IcingaDbConnection::fromResourceName($DBname); |
| 90 | - } |
|
| 91 | - catch (Exception $e) |
|
| 90 | + } catch (Exception $e) |
|
| 92 | 91 | { |
| 93 | - if ($test) return array(2,$DBname); |
|
| 92 | + if ($test) { |
|
| 93 | + return array(2,$DBname); |
|
| 94 | + } |
|
| 94 | 95 | $this->redirectNow('trapdirector/settings?dberror=2'); |
| 95 | 96 | return null; |
| 96 | 97 | } |
@@ -98,10 +99,11 @@ discard block |
||
| 98 | 99 | try |
| 99 | 100 | { |
| 100 | 101 | $db=$dbconn->getConnection(); |
| 101 | - } |
|
| 102 | - catch (Exception $e) |
|
| 102 | + } catch (Exception $e) |
|
| 103 | 103 | { |
| 104 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
| 104 | + if ($test) { |
|
| 105 | + return array(3,$DBname,$e->getMessage()); |
|
| 106 | + } |
|
| 105 | 107 | $this->redirectNow('trapdirector/settings?dberror=3'); |
| 106 | 108 | return null; |
| 107 | 109 | } |
@@ -113,25 +115,32 @@ discard block |
||
| 113 | 115 | $version=$db->fetchRow($query); |
| 114 | 116 | if ( ($version == null) || ! property_exists($version,'value') ) |
| 115 | 117 | { |
| 116 | - if ($test) return array(4,$DBname); |
|
| 118 | + if ($test) { |
|
| 119 | + return array(4,$DBname); |
|
| 120 | + } |
|
| 117 | 121 | $this->redirectNow('trapdirector/settings?dberror=4'); |
| 118 | 122 | return null; |
| 119 | 123 | } |
| 120 | 124 | if ($version->value < $this->getModuleConfig()->getDbMinVersion()) |
| 121 | 125 | { |
| 122 | - if ($test) return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion()); |
|
| 126 | + if ($test) { |
|
| 127 | + return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion()); |
|
| 128 | + } |
|
| 123 | 129 | $this->redirectNow('trapdirector/settings?dberror=5'); |
| 124 | 130 | return null; |
| 125 | 131 | } |
| 126 | - } |
|
| 127 | - catch (Exception $e) |
|
| 132 | + } catch (Exception $e) |
|
| 128 | 133 | { |
| 129 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
| 134 | + if ($test) { |
|
| 135 | + return array(3,$DBname,$e->getMessage()); |
|
| 136 | + } |
|
| 130 | 137 | $this->redirectNow('trapdirector/settings?dberror=4'); |
| 131 | 138 | return null; |
| 132 | 139 | } |
| 133 | 140 | } |
| 134 | - if ($test) return array(0,''); |
|
| 141 | + if ($test) { |
|
| 142 | + return array(0,''); |
|
| 143 | + } |
|
| 135 | 144 | return $dbconn; |
| 136 | 145 | } |
| 137 | 146 | |
@@ -142,31 +151,41 @@ discard block |
||
| 142 | 151 | */ |
| 143 | 152 | public function getDb($test=false) |
| 144 | 153 | { |
| 145 | - if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
| 154 | + if ($this->trapDB != null && $test = false) { |
|
| 155 | + return $this->trapDB; |
|
| 156 | + } |
|
| 146 | 157 | |
| 147 | 158 | $dbresource=$this->Config()->get('config', 'database'); |
| 148 | 159 | |
| 149 | 160 | if ( ! $dbresource ) |
| 150 | 161 | { |
| 151 | - if ($test) return array(1,''); |
|
| 162 | + if ($test) { |
|
| 163 | + return array(1,''); |
|
| 164 | + } |
|
| 152 | 165 | $this->redirectNow('trapdirector/settings?dberror=1'); |
| 153 | 166 | return null; |
| 154 | 167 | } |
| 155 | 168 | $retDB=$this->getDbByName($dbresource,$test,true); |
| 156 | - if ($test == true) return $retDB; |
|
| 169 | + if ($test == true) { |
|
| 170 | + return $retDB; |
|
| 171 | + } |
|
| 157 | 172 | $this->trapDB=$retDB; |
| 158 | 173 | return $this->trapDB; |
| 159 | 174 | } |
| 160 | 175 | |
| 161 | 176 | public function getIdoDb($test=false) |
| 162 | 177 | { |
| 163 | - if ($this->icingaDB != null && $test = false) return $this->icingaDB; |
|
| 178 | + if ($this->icingaDB != null && $test = false) { |
|
| 179 | + return $this->icingaDB; |
|
| 180 | + } |
|
| 164 | 181 | // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
| 165 | 182 | $dbresource=$this->Config()->get('config', 'IDOdatabase');; |
| 166 | 183 | |
| 167 | 184 | if ( ! $dbresource ) |
| 168 | 185 | { |
| 169 | - if ($test) return array(1,'No database in config.ini'); |
|
| 186 | + if ($test) { |
|
| 187 | + return array(1,'No database in config.ini'); |
|
| 188 | + } |
|
| 170 | 189 | $this->redirectNow('trapdirector/settings?idodberror=1'); |
| 171 | 190 | return null; |
| 172 | 191 | } |
@@ -174,10 +193,11 @@ discard block |
||
| 174 | 193 | try |
| 175 | 194 | { |
| 176 | 195 | $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
| 177 | - } |
|
| 178 | - catch (Exception $e) |
|
| 196 | + } catch (Exception $e) |
|
| 179 | 197 | { |
| 180 | - if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
| 198 | + if ($test) { |
|
| 199 | + return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
| 200 | + } |
|
| 181 | 201 | $this->redirectNow('trapdirector/settings?idodberror=2'); |
| 182 | 202 | return null; |
| 183 | 203 | } |
@@ -197,8 +217,7 @@ discard block |
||
| 197 | 217 | { |
| 198 | 218 | return array(4,"$dbresource does not look like an IDO database"); |
| 199 | 219 | } |
| 200 | - } |
|
| 201 | - catch (Exception $e) |
|
| 220 | + } catch (Exception $e) |
|
| 202 | 221 | { |
| 203 | 222 | return array(3,"Error connecting to $dbresource : " . $e->getMessage()); |
| 204 | 223 | } |
@@ -446,8 +465,7 @@ discard block |
||
| 446 | 465 | if (isset($common_services[$service->name2]['num'])) |
| 447 | 466 | { |
| 448 | 467 | $common_services[$service->name2]['num'] +=1; |
| 449 | - } |
|
| 450 | - else |
|
| 468 | + } else |
|
| 451 | 469 | { |
| 452 | 470 | $common_services[$service->name2]['num']=1; |
| 453 | 471 | $common_services[$service->name2]['name']=$service->name; |
@@ -591,7 +609,9 @@ discard block |
||
| 591 | 609 | $condition=($condition===null)?'':$condition.' AND '; |
| 592 | 610 | $condition.="trap_oid='$oid'"; |
| 593 | 611 | } |
| 594 | - if($condition === null) return null; |
|
| 612 | + if($condition === null) { |
|
| 613 | + return null; |
|
| 614 | + } |
|
| 595 | 615 | $query=$db->delete( |
| 596 | 616 | $this->getModuleConfig()->getTrapTableName(), |
| 597 | 617 | $condition |
@@ -619,7 +639,9 @@ discard block |
||
| 619 | 639 | $condition=($condition===null)?'':$condition.' AND '; |
| 620 | 640 | $condition.="trap_oid='$oid'"; |
| 621 | 641 | } |
| 622 | - if($condition ==null) return 0; |
|
| 642 | + if($condition ==null) { |
|
| 643 | + return 0; |
|
| 644 | + } |
|
| 623 | 645 | $query=$db->select() |
| 624 | 646 | ->from( |
| 625 | 647 | $this->getModuleConfig()->getTrapTableName(), |
@@ -643,18 +665,28 @@ discard block |
||
| 643 | 665 | array('value'=>'value')) |
| 644 | 666 | ->where('name=?',$element); |
| 645 | 667 | $return_row=$db->fetchRow($query); |
| 646 | - if ($return_row==null) // value does not exists |
|
| 668 | + if ($return_row==null) { |
|
| 669 | + // value does not exists |
|
| 647 | 670 | { |
| 648 | 671 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
| 649 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
| 672 | + } |
|
| 673 | + if ( ! isset($default[$element])) { |
|
| 674 | + return null; |
|
| 675 | + } |
|
| 676 | + // no default and not value |
|
| 650 | 677 | |
| 651 | 678 | $this->addDBConfigValue($element,$default[$element]); |
| 652 | 679 | return $default[$element]; |
| 653 | 680 | } |
| 654 | - if ($return_row->value == null) // value id empty |
|
| 681 | + if ($return_row->value == null) { |
|
| 682 | + // value id empty |
|
| 655 | 683 | { |
| 656 | 684 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
| 657 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
| 685 | + } |
|
| 686 | + if ( ! isset($default[$element])) { |
|
| 687 | + return null; |
|
| 688 | + } |
|
| 689 | + // no default and not value |
|
| 658 | 690 | $this->setDBConfigValue($element,$default[$element]); |
| 659 | 691 | return $default[$element]; |
| 660 | 692 | } |
@@ -94,7 +94,9 @@ discard block |
||
| 94 | 94 | array('id' => 'id')) |
| 95 | 95 | ->where("oid = '".$trap."'") ; |
| 96 | 96 | $id=$dbconn->fetchRow($query); |
| 97 | - if ( ($id == null) || ! property_exists($id,'id') ) return null; |
|
| 97 | + if ( ($id == null) || ! property_exists($id,'id') ) { |
|
| 98 | + return null; |
|
| 99 | + } |
|
| 98 | 100 | |
| 99 | 101 | $query=$dbconn->select() |
| 100 | 102 | ->from( |
@@ -107,7 +109,9 @@ discard block |
||
| 107 | 109 | 'o.trap_id='.$id->id ) |
| 108 | 110 | ->where("o.object_id = c.id"); |
| 109 | 111 | $listObjects=$dbconn->fetchAll($query); |
| 110 | - if ( count($listObjects)==0 ) return null; |
|
| 112 | + if ( count($listObjects)==0 ) { |
|
| 113 | + return null; |
|
| 114 | + } |
|
| 111 | 115 | |
| 112 | 116 | foreach ($listObjects as $val) |
| 113 | 117 | { |
@@ -128,7 +132,10 @@ discard block |
||
| 128 | 132 | */ |
| 129 | 133 | public function translateOID($oid) |
| 130 | 134 | { |
| 131 | - if (!preg_match('/^\./',$oid)) $oid = '.' . $oid; // Add a leading '.' |
|
| 135 | + if (!preg_match('/^\./',$oid)) { |
|
| 136 | + $oid = '.' . $oid; |
|
| 137 | + } |
|
| 138 | + // Add a leading '.' |
|
| 132 | 139 | $retArray=array('oid' => $oid, 'mib' => null, 'name'=>null,'type'=>null); |
| 133 | 140 | $dbconn = $this->db->getConnection(); |
| 134 | 141 | |
@@ -166,8 +173,7 @@ discard block |
||
| 166 | 173 | { |
| 167 | 174 | $retArray['type']=$matches[1]; |
| 168 | 175 | $retArray['type_enum']=$matches[2]; |
| 169 | - } |
|
| 170 | - else |
|
| 176 | + } else |
|
| 171 | 177 | { |
| 172 | 178 | $retArray['type']=$translate; |
| 173 | 179 | $retArray['type_enum']=''; |
@@ -233,8 +239,7 @@ discard block |
||
| 233 | 239 | if ($oid===null) |
| 234 | 240 | { |
| 235 | 241 | $where="c.id = '$id'"; |
| 236 | - } |
|
| 237 | - else |
|
| 242 | + } else |
|
| 238 | 243 | { |
| 239 | 244 | $where="c.oid = '$oid'"; |
| 240 | 245 | } |
@@ -68,7 +68,9 @@ |
||
| 68 | 68 | switch ($outputType) |
| 69 | 69 | { |
| 70 | 70 | case 'file': |
| 71 | - if ($outputFile == null) throw new Exception("File logging without file !"); |
|
| 71 | + if ($outputFile == null) { |
|
| 72 | + throw new Exception("File logging without file !"); |
|
| 73 | + } |
|
| 72 | 74 | $this->setFile($outputFile); |
| 73 | 75 | $this->setDestination('file'); |
| 74 | 76 | break; |
@@ -76,9 +76,10 @@ discard block |
||
| 76 | 76 | ->from($this->moduleConfig->getTrapTableName(),$elmts) |
| 77 | 77 | ->where('id=?',$trapid); |
| 78 | 78 | $trapDetail=$db->fetchRow($query); |
| 79 | - if ( $trapDetail == null) throw new Exception('No traps was found with id = '.$trapid); |
|
| 80 | - } |
|
| 81 | - catch (Exception $e) |
|
| 79 | + if ( $trapDetail == null) { |
|
| 80 | + throw new Exception('No traps was found with id = '.$trapid); |
|
| 81 | + } |
|
| 82 | + } catch (Exception $e) |
|
| 82 | 83 | { |
| 83 | 84 | $this->displayExitError('Trap detail',$e->getMessage()); |
| 84 | 85 | return; |
@@ -109,8 +110,7 @@ discard block |
||
| 109 | 110 | ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
| 110 | 111 | ->where('trap_id=?',$trapid); |
| 111 | 112 | $trapDetail=$db->fetchAll($query); |
| 112 | - } |
|
| 113 | - catch (Exception $e) |
|
| 113 | + } catch (Exception $e) |
|
| 114 | 114 | { |
| 115 | 115 | $this->displayExitError('Trap detail',$e->getMessage()); |
| 116 | 116 | } |
@@ -118,8 +118,7 @@ discard block |
||
| 118 | 118 | if ($trapDetail == null ) |
| 119 | 119 | { |
| 120 | 120 | $this->view->data=false; |
| 121 | - } |
|
| 122 | - else |
|
| 121 | + } else |
|
| 123 | 122 | { |
| 124 | 123 | $this->view->data=true; |
| 125 | 124 | // Store result in array. |
@@ -205,8 +204,7 @@ discard block |
||
| 205 | 204 | $ip=$postData['IP']; |
| 206 | 205 | $oid=$postData['OID']; |
| 207 | 206 | $action=$postData['action']; |
| 208 | - } |
|
| 209 | - else |
|
| 207 | + } else |
|
| 210 | 208 | { |
| 211 | 209 | $this->_helper->json(array('status'=>'Missing variables')); |
| 212 | 210 | return; |