Passed
Push — master ( 4d5cad...037a61 )
by Patrick
02:02
created
bin/trap_class.php 1 patch
Braces   +89 added lines, -58 removed lines patch added patch discarded remove patch
@@ -68,9 +68,9 @@  discard block
 block discarded – undo
68 68
 		{
69 69
 		    $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile);
70 70
 		    $this->logSetup=true;
71
+		} else {
72
+				    $this->logSetup=false;
71 73
 		}
72
-		else
73
-		    $this->logSetup=false;
74 74
 		$this->logging->log('Loggin started', INFO);
75 75
 
76 76
 		// Get options from ini files
@@ -83,7 +83,10 @@  discard block
 block discarded – undo
83 83
 		$this->setupDatabase($trapConfig); // Setup database class
84 84
 		
85 85
 		$this->getDatabaseOptions(); // Get options in database
86
-		if ($this->api_use === true) $this->getAPI(); // Setup API
86
+		if ($this->api_use === true) {
87
+			$this->getAPI();
88
+		}
89
+		// Setup API
87 90
 		
88 91
 		$this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class
89 92
 		
@@ -117,8 +120,7 @@  discard block
 block discarded – undo
117 120
 	        }
118 121
 	        $this->logging->log($message,$log_level,'syslog');
119 122
 	        return false;
120
-	    }
121
-	    else
123
+	    } else
122 124
 	    {
123 125
 	        $option_var=$option_array[$option_category][$option_name];
124 126
 	        return true;
@@ -182,7 +184,10 @@  discard block
 block discarded – undo
182 184
 	    
183 185
 	    $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix);
184 186
 	    
185
-	    if ($this->api_use === true) return; // In case of API use, no IDO is necessary
187
+	    if ($this->api_use === true) {
188
+	    	return;
189
+	    }
190
+	    // In case of API use, no IDO is necessary
186 191
         
187 192
 	    // IDO Database
188 193
 	    if (!array_key_exists('IDOdatabase',$trapConfig['config']))
@@ -207,9 +212,11 @@  discard block
 block discarded – undo
207 212
 	protected function getDatabaseOptions()
208 213
 	{
209 214
 		// Database options
210
-		if ($this->logSetup === false) // Only if logging was no setup in constructor
215
+		if ($this->logSetup === false) {
216
+			// Only if logging was no setup in constructor
211 217
 		{
212 218
     		$this->getDBConfigIfSet('log_level',$this->logging->debugLevel);
219
+		}
213 220
     		$this->getDBConfigIfSet('log_destination',$this->logging->outputMode);
214 221
     		$this->getDBConfigIfSet('log_file',$this->logging->outputFile);
215 222
 		}
@@ -218,7 +225,9 @@  discard block
 block discarded – undo
218 225
 	protected function getDBConfigIfSet($element,&$variable)
219 226
 	{
220 227
 		$value=$this->getDBConfig($element);
221
-		if ($value != 'null') $variable=$value;
228
+		if ($value != 'null') {
229
+			$variable=$value;
230
+		}
222 231
 	}
223 232
 	
224 233
 	/** 
@@ -306,8 +315,7 @@  discard block
 block discarded – undo
306 315
 		{
307 316
 		    $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)");
308 317
 			$this->logging->log('Error parsing IP : '.$IP,ERROR,'');
309
-		} 
310
-		else 
318
+		} else 
311 319
 		{		
312 320
 			$this->trap_data['source_ip']=$matches[1];
313 321
 			$this->trap_data['destination_ip']=$matches[3];
@@ -322,14 +330,12 @@  discard block
 block discarded – undo
322 330
 			if ($ret_code===0 || $ret_code===false) 
323 331
 			{
324 332
 				$this->logging->log('No match on trap data : '.$vars,WARN,'');
325
-			}
326
-			else 
333
+			} else 
327 334
 			{
328 335
 			    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'))
329 336
 				{
330 337
 					$this->trap_data['trap_oid']=$matches[2];				
331
-				}
332
-				else
338
+				} else
333 339
 				{
334 340
 					$object= new stdClass;
335 341
 					$object->oid =$matches[1];
@@ -500,7 +506,9 @@  discard block
 block discarded – undo
500 506
 	            }
501 507
 	            
502 508
 	            $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
503
-	            if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
509
+	            if ($inserted_id==false) {
510
+	            	throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
511
+	            }
504 512
 	            $this->trap_id=$inserted_id;
505 513
 	            break;
506 514
 	        default:
@@ -516,7 +524,9 @@  discard block
 block discarded – undo
516 524
 	{
517 525
 		
518 526
 		// If action is ignore -> don't send t DB
519
-		if ($this->trap_to_db === false) return;
527
+		if ($this->trap_to_db === false) {
528
+			return;
529
+		}
520 530
 		
521 531
 		
522 532
 		$db_conn=$this->trapsDB->db_connect_trap();
@@ -573,7 +583,9 @@  discard block
 block discarded – undo
573 583
 				}
574 584
 
575 585
 				$inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
576
-				if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
586
+				if ($inserted_id==false) {
587
+					throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
588
+				}
577 589
 				$this->trap_id=$inserted_id;
578 590
 			break;
579 591
 			default: 
@@ -702,8 +714,7 @@  discard block
 block discarded – undo
702 714
     		// TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default....
703 715
     		exec('echo "'.$send.'" > ' .$this->icinga2cmd);
704 716
     		return true;
705
-	    }
706
-	    else
717
+	    } else
707 718
 	    {
708 719
 	        $api = $this->getAPI();
709 720
 	        $api->setCredentials($this->api_username, $this->api_password);
@@ -712,8 +723,7 @@  discard block
 block discarded – undo
712 723
 	        {
713 724
 	            $this->logging->log( "Error sending result : " .$retmessage,WARN,'');
714 725
 	            return false;
715
-	        }
716
-	        else 
726
+	        } else 
717 727
 	        {
718 728
 	            $this->logging->log( "Sent result : " .$retmessage,INFO );
719 729
 	            return true;
@@ -773,7 +783,9 @@  discard block
 block discarded – undo
773 783
 	/***************** Eval & tokenizer functions ****************/
774 784
 	protected function eval_getElement($rule,&$item)
775 785
 	{
776
-		while ($rule[$item]==' ') $item++;
786
+		while ($rule[$item]==' ') {
787
+			$item++;
788
+		}
777 789
 		if (preg_match('/[0-9\.]/',$rule[$item]))
778 790
 		{ // number
779 791
 	
@@ -800,14 +812,15 @@  discard block
 block discarded – undo
800 812
 			$start=$item;
801 813
 			$parenthesis_count=0; 
802 814
 			while (($item < strlen($rule)) // Not end of string AND
803
-			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
815
+			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) {
816
+				// Closing ')' or embeded ()
804 817
 			{ 
805 818
 				if ($rule[$item] == '"' )
806 819
 				{ // pass through string
807 820
 					$item++;
821
+			}
808 822
 					$item=$this->eval_getNext($rule,$item,'"');
809
-				} 
810
-				else{
823
+				} else{
811 824
 				    if ($rule[$item] == '(')
812 825
 				    {
813 826
 				        $parenthesis_count++;
@@ -835,13 +848,17 @@  discard block
 block discarded – undo
835 848
 	protected function eval_getNext($rule,$item,$tok)
836 849
 	{
837 850
 		while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;}
838
-		if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
851
+		if ($item==strlen($rule)) {
852
+			throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
853
+		}
839 854
 		return $item+1;
840 855
 	}
841 856
 	
842 857
 	protected function eval_getOper($rule,&$item)
843 858
 	{
844
-		while ($rule[$item]==' ') $item++;
859
+		while ($rule[$item]==' ') {
860
+			$item++;
861
+		}
845 862
 		switch ($rule[$item])
846 863
 		{
847 864
 			case '<':
@@ -877,12 +894,13 @@  discard block
 block discarded – undo
877 894
 	public function evaluation($rule,&$item)
878 895
 	{
879 896
 	    //echo "Evaluation of ".substr($rule,$item)."\n";
880
-		if ( $rule[$item] == '!') // If '!' found, negate next expression.
897
+		if ( $rule[$item] == '!') {
898
+			// If '!' found, negate next expression.
881 899
 		{
882 900
 		    $negate=true;
883
-		    $item++;
884 901
 		}
885
-		else
902
+		    $item++;
903
+		} else
886 904
 		{
887 905
 		    $negate=false;
888 906
 		}
@@ -890,10 +908,14 @@  discard block
 block discarded – undo
890 908
 		list($type1,$val1) = $this->eval_getElement($rule,$item);
891 909
 		//echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n";
892 910
 		
893
-		if ($item==strlen($rule)) // If only element, return value, but only boolean
911
+		if ($item==strlen($rule)) {
912
+			// If only element, return value, but only boolean
894 913
 		{
895 914
 		  if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule);
896
-		  if ($negate === true) $val1= ! $val1;
915
+		}
916
+		  if ($negate === true) {
917
+		  	$val1= ! $val1;
918
+		  }
897 919
 		  return $val1;
898 920
 		}  
899 921
 		
@@ -902,27 +924,34 @@  discard block
 block discarded – undo
902 924
 		//echo "Comp : ".$comp." : ".substr($rule,$item)."\n";
903 925
         
904 926
 		// Third element : number, string or ()
905
-		if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
927
+		if ( $rule[$item] == '!') {
928
+			// starts with a ! so evaluate whats next
906 929
 		{
907 930
 		    $item++;
908
-		    if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
931
+		}
932
+		    if ($typec != 1) {
933
+		    	throw new Exception("Mixing boolean and comparison : ".$rule);
934
+		    }
909 935
 		    $val2= ! $this->evaluation($rule,$item);
910 936
 		    $type2=2; // result is a boolean 
911
-		}
912
-		else 
937
+		} else 
913 938
 		{
914 939
 		    list($type2,$val2) = $this->eval_getElement($rule,$item);
915 940
 		}
916 941
 		//echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
917 942
 		
918
-		if ($type1!=$type2)  // cannot compare different types
943
+		if ($type1!=$type2) {
944
+			// cannot compare different types
919 945
 		{ 
920 946
 		    throw new Exception("Cannot compare string & number : ".$rule);
921 947
 		}
922
-		if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
948
+		}
949
+		if ($typec==1 && $type1 !=2) {
950
+			// cannot use & or | with string/number
923 951
 		{
924 952
 		    throw new Exception("Cannot use boolean operators with string & number : ".$rule);
925 953
 		}
954
+		}
926 955
 		
927 956
 		switch ($comp){
928 957
 			case '<':	$retVal= ($val1 < $val2); break;
@@ -936,9 +965,15 @@  discard block
 block discarded – undo
936 965
 			case '&':	$retVal= ($val1 && $val2); break;
937 966
 			default:  throw new Exception("Error in expression - unknown comp : ".$comp);
938 967
 		}
939
-		if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression
968
+		if ($negate === true) {
969
+			$retVal = ! $retVal;
970
+		}
971
+		// Inverse result if negate before expression
940 972
 		
941
-		if ($item==strlen($rule)) return $retVal; // End of string : return evaluation
973
+		if ($item==strlen($rule)) {
974
+			return $retVal;
975
+		}
976
+		// End of string : return evaluation
942 977
 		// check for logical operator :
943 978
 		switch ($rule[$item])
944 979
 		{
@@ -965,7 +1000,9 @@  discard block
 block discarded – undo
965 1000
 					$rule2.=$rule[$item];
966 1001
 					$item++;
967 1002
 				}
968
-				if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
1003
+				if ($item == strlen ($rule)) {
1004
+					throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
1005
+				}
969 1006
 				$rule2.=$rule[$item];
970 1007
 				$item++;
971 1008
 				continue;
@@ -985,10 +1022,12 @@  discard block
 block discarded – undo
985 1022
 	
986 1023
 	protected function eval_rule($rule)
987 1024
 	{
988
-		if ($rule==null || $rule == '') // Empty rule is always true
1025
+		if ($rule==null || $rule == '') {
1026
+			// Empty rule is always true
989 1027
 		{
990 1028
 			return true;
991 1029
 		}
1030
+		}
992 1031
 		$matches=array();
993 1032
 		while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1)
994 1033
 		{
@@ -1075,20 +1114,17 @@  discard block
 block discarded – undo
1075 1114
 						if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false)
1076 1115
 						{
1077 1116
 						    $this->trap_action.='Error sending status : check cmd/API';
1078
-						}
1079
-						else
1117
+						} else
1080 1118
 						{
1081 1119
 						    $this->add_rule_match($rule['id'],$rule['num_match']+1);
1082 1120
 						    $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1083 1121
 						}
1084
-					}
1085
-					else
1122
+					} else
1086 1123
 					{
1087 1124
 						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1088 1125
 					}
1089 1126
 					$this->trap_to_db=($action==-2)?false:true;
1090
-				}
1091
-				else
1127
+				} else
1092 1128
 				{
1093 1129
 					//$this->logging->log('rules KOO : '.print_r($rule),INFO );
1094 1130
 					
@@ -1099,14 +1135,12 @@  discard block
 block discarded – undo
1099 1135
 					    if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false)
1100 1136
 					    {
1101 1137
 					        $this->trap_action.='Error sending status : check cmd/API';
1102
-					    }
1103
-					    else
1138
+					    } else
1104 1139
 					    {
1105 1140
     						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1106 1141
     						$this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1107 1142
 					    }
1108
-					}
1109
-					else
1143
+					} else
1110 1144
 					{
1111 1145
 						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1112 1146
 					}
@@ -1116,16 +1150,14 @@  discard block
 block discarded – undo
1116 1150
 				if (!isset($this->trap_data['source_name']))
1117 1151
 				{
1118 1152
 					$this->trap_data['source_name']=$rule['host_name'];
1119
-				}
1120
-				else
1153
+				} else
1121 1154
 				{
1122 1155
 					if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name']))
1123 1156
 					{ // only add if not present
1124 1157
 						$this->trap_data['source_name'].=','.$rule['host_name'];
1125 1158
 					}
1126 1159
 				}
1127
-			}
1128
-			catch (Exception $e) 
1160
+			} catch (Exception $e) 
1129 1161
 			{ 
1130 1162
 			    $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,'');
1131 1163
 			    $this->trap_action.=' ERR : '.$e->getMessage();
@@ -1136,8 +1168,7 @@  discard block
 block discarded – undo
1136 1168
 		if ($this->trap_data['status']=='error')
1137 1169
 		{
1138 1170
 		  $this->trap_to_db=true; // Always put errors in DB for the use can see
1139
-		}
1140
-		else
1171
+		} else
1141 1172
 		{
1142 1173
 		  $this->trap_data['status']='done';
1143 1174
 		}
Please login to merge, or discard this patch.
application/clicommands/MibCommand.php 1 patch
Braces   +5 added lines, -10 removed lines patch added patch discarded remove patch
@@ -62,8 +62,7 @@  discard block
 block discarded – undo
62 62
 		if ($pid == 1)
63 63
 		{
64 64
 		    $trap->setLogging($logLevel,'display');
65
-		}
66
-		else
65
+		} else
67 66
 		{  // use default display TODO : if default is 'display' son process will be killed at first output....
68 67
 		    if ($pid != 0)
69 68
 		    {
@@ -72,8 +71,7 @@  discard block
 block discarded – undo
72 71
 		        fclose($file);
73 72
 		        echo "OK : process $pid in bckground";
74 73
 		        return 0;
75
-		    }
76
-		    else
74
+		    } else
77 75
 		    {  // son process : close all file descriptors and go to a new session
78 76
 		        fclose($file);		        
79 77
 // 		        $sid = posix_setsid();
@@ -83,8 +81,7 @@  discard block
 block discarded – undo
83 81
                 try
84 82
                 {
85 83
                     $trap->mibClass->update_mib_database(false,$forceCheck);
86
-                }
87
-                catch (Exception $e)
84
+                } catch (Exception $e)
88 85
                 {
89 86
                     $trap->trapLog('Error in updating : ' . $e->getMessage(),2);
90 87
                 }
@@ -101,8 +98,7 @@  discard block
 block discarded – undo
101 98
 			$trap->mibClass->update_mib_database(true,$forceCheck);
102 99
 			echo "Done\n";
103 100
 			
104
-		}
105
-		catch (Exception $e)
101
+		} catch (Exception $e)
106 102
 		{
107 103
 			echo 'Error in updating : ' . $e->getMessage();
108 104
 		}
@@ -144,8 +140,7 @@  discard block
 block discarded – undo
144 140
 			$query = $db->select()->from($Config->getTrapRuleName(),array('COUNT(*)'));			
145 141
 			echo "Number of rules : " . $db->fetchOne($query) ."\n";		
146 142
 			
147
-		}
148
-		catch (Exception $e)
143
+		} catch (Exception $e)
149 144
 		{
150 145
 			echo 'Error in DB : ' . $e->getMessage();
151 146
 		}	   
Please login to merge, or discard this patch.