| @@ -12,204 +12,204 @@ | ||
| 12 | 12 | trait RuleUtils | 
| 13 | 13 |  { | 
| 14 | 14 | |
| 15 | - /** rule eval starting from $token | |
| 16 | - * @param string $rule | |
| 17 | - * @param int $item | |
| 18 | - */ | |
| 19 | - abstract public function evaluation($rule,&$item); | |
| 15 | + /** rule eval starting from $token | |
| 16 | + * @param string $rule | |
| 17 | + * @param int $item | |
| 18 | + */ | |
| 19 | + abstract public function evaluation($rule,&$item); | |
| 20 | 20 | |
| 21 | - /** | |
| 22 | - * Get full number | |
| 23 | - * @param string $rule Rule as string | |
| 24 | - * @param int $item current eval position | |
| 25 | - * @return array<int,string> | |
| 26 | - */ | |
| 27 | - private function get_number(string $rule,int &$item) | |
| 28 | -    { | |
| 29 | - $item2=$item+1; | |
| 30 | - while ( | |
| 31 | - ($item2!=strlen($rule)) | |
| 32 | -            && (preg_match('/[\-0-9\.]/',$rule[$item2]))) | |
| 33 | -        { | |
| 34 | - $item2++ ; | |
| 35 | - } | |
| 36 | - $val=substr($rule,$item,$item2-$item); | |
| 37 | - $item=$item2; | |
| 38 | - //echo "number ".$val."\n"; | |
| 21 | + /** | |
| 22 | + * Get full number | |
| 23 | + * @param string $rule Rule as string | |
| 24 | + * @param int $item current eval position | |
| 25 | + * @return array<int,string> | |
| 26 | + */ | |
| 27 | + private function get_number(string $rule,int &$item) | |
| 28 | +	{ | |
| 29 | + $item2=$item+1; | |
| 30 | + while ( | |
| 31 | + ($item2!=strlen($rule)) | |
| 32 | +			&& (preg_match('/[\-0-9\.]/',$rule[$item2]))) | |
| 33 | +		{ | |
| 34 | + $item2++ ; | |
| 35 | + } | |
| 36 | + $val=substr($rule,$item,$item2-$item); | |
| 37 | + $item=$item2; | |
| 38 | + //echo "number ".$val."\n"; | |
| 39 | 39 | |
| 40 | - return array(0,$val); | |
| 41 | - } | |
| 40 | + return array(0,$val); | |
| 41 | + } | |
| 42 | 42 | |
| 43 | - /** | |
| 44 | - * Get a string (between ") | |
| 45 | - * @param string $rule Rule as string | |
| 46 | - * @param int $item current eval position | |
| 47 | - * @return array<int,string> | |
| 48 | - */ | |
| 49 | - private function get_string(string $rule,int &$item) | |
| 50 | -    { | |
| 51 | - $item++; | |
| 52 | - $item2=$this->eval_getNext($rule,$item,'"'); | |
| 53 | - $val=substr($rule,$item,$item2-$item-1); | |
| 54 | - $item=$item2; | |
| 55 | - //echo "string : ".$val."\n"; | |
| 56 | - return array(1,$val); | |
| 43 | + /** | |
| 44 | + * Get a string (between ") | |
| 45 | + * @param string $rule Rule as string | |
| 46 | + * @param int $item current eval position | |
| 47 | + * @return array<int,string> | |
| 48 | + */ | |
| 49 | + private function get_string(string $rule,int &$item) | |
| 50 | +	{ | |
| 51 | + $item++; | |
| 52 | + $item2=$this->eval_getNext($rule,$item,'"'); | |
| 53 | + $val=substr($rule,$item,$item2-$item-1); | |
| 54 | + $item=$item2; | |
| 55 | + //echo "string : ".$val."\n"; | |
| 56 | + return array(1,$val); | |
| 57 | 57 | |
| 58 | - } | |
| 58 | + } | |
| 59 | 59 | |
| 60 | - /** | |
| 61 | - * Parse elements inside () : jumps over "" and count parenthesis. | |
| 62 | - * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) | |
| 63 | - * @param string $rule : the current rule | |
| 64 | - * @param int $item : actual position in rule | |
| 65 | - * @throws Exception | |
| 66 | - * @return string : everything inside parenthesis | |
| 67 | - */ | |
| 68 | - private function parse_parenthesis(string $rule,int &$item) : string | |
| 69 | -    { | |
| 70 | - $item++; | |
| 71 | - $start=$item; | |
| 72 | - $parenthesisCount=0; | |
| 73 | - while (($item < strlen($rule)) // Not end of string AND | |
| 74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () | |
| 75 | -        { | |
| 76 | - if ($rule[$item] == '"' ) | |
| 77 | -            { // pass through string | |
| 78 | - $item++; | |
| 79 | - $item=$this->eval_getNext($rule,$item,'"'); | |
| 80 | - continue; | |
| 81 | - } | |
| 82 | -            if ($rule[$item] == '(') | |
| 83 | -            { | |
| 84 | - $parenthesisCount++; | |
| 85 | - } | |
| 86 | - if ($rule[$item] == ')') | |
| 87 | -            { | |
| 88 | - $parenthesisCount--; | |
| 89 | - } | |
| 90 | - $item++; | |
| 91 | - } | |
| 60 | + /** | |
| 61 | + * Parse elements inside () : jumps over "" and count parenthesis. | |
| 62 | + * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) | |
| 63 | + * @param string $rule : the current rule | |
| 64 | + * @param int $item : actual position in rule | |
| 65 | + * @throws Exception | |
| 66 | + * @return string : everything inside parenthesis | |
| 67 | + */ | |
| 68 | + private function parse_parenthesis(string $rule,int &$item) : string | |
| 69 | +	{ | |
| 70 | + $item++; | |
| 71 | + $start=$item; | |
| 72 | + $parenthesisCount=0; | |
| 73 | + while (($item < strlen($rule)) // Not end of string AND | |
| 74 | + && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () | |
| 75 | +		{ | |
| 76 | + if ($rule[$item] == '"' ) | |
| 77 | +			{ // pass through string | |
| 78 | + $item++; | |
| 79 | + $item=$this->eval_getNext($rule,$item,'"'); | |
| 80 | + continue; | |
| 81 | + } | |
| 82 | +			if ($rule[$item] == '(') | |
| 83 | +			{ | |
| 84 | + $parenthesisCount++; | |
| 85 | + } | |
| 86 | + if ($rule[$item] == ')') | |
| 87 | +			{ | |
| 88 | + $parenthesisCount--; | |
| 89 | + } | |
| 90 | + $item++; | |
| 91 | + } | |
| 92 | 92 | |
| 93 | -        if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} | |
| 94 | - $val=substr($rule,$start,$item-$start); | |
| 95 | - $item++; | |
| 96 | - return $val; | |
| 97 | - } | |
| 93 | +		if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} | |
| 94 | + $val=substr($rule,$start,$item-$start); | |
| 95 | + $item++; | |
| 96 | + return $val; | |
| 97 | + } | |
| 98 | 98 | |
| 99 | - /** | |
| 100 | - * Get and eval a grouped condition - ex : (1==1) | |
| 101 | - * @param string $rule | |
| 102 | - * @param int $item | |
| 103 | - * @return array<int,string> | |
| 104 | - */ | |
| 105 | - private function get_group(string $rule,int &$item) : array | |
| 106 | -    { | |
| 107 | - // gets eveything inside parenthesis | |
| 108 | - $val=$this->parse_parenthesis($rule, $item); | |
| 109 | - // Returns boolean with evaluation of all inside parenthesis | |
| 110 | - $start=0; | |
| 111 | - return array(2,$this->evaluation($val,$start)); | |
| 112 | - } | |
| 99 | + /** | |
| 100 | + * Get and eval a grouped condition - ex : (1==1) | |
| 101 | + * @param string $rule | |
| 102 | + * @param int $item | |
| 103 | + * @return array<int,string> | |
| 104 | + */ | |
| 105 | + private function get_group(string $rule,int &$item) : array | |
| 106 | +	{ | |
| 107 | + // gets eveything inside parenthesis | |
| 108 | + $val=$this->parse_parenthesis($rule, $item); | |
| 109 | + // Returns boolean with evaluation of all inside parenthesis | |
| 110 | + $start=0; | |
| 111 | + return array(2,$this->evaluation($val,$start)); | |
| 112 | + } | |
| 113 | 113 | |
| 114 | - /** | |
| 115 | - * @param string $rule | |
| 116 | - * @param int $item | |
| 117 | - * @throws Exception | |
| 118 | - * @return array<int,string> | |
| 119 | - */ | |
| 120 | - private function get_function(string $rule,int &$item) : array | |
| 121 | -    { | |
| 122 | - // function is : __function(param1,param2...) | |
| 123 | - $start=$item; | |
| 124 | -        while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' | |
| 125 | -        { | |
| 126 | - $item++; | |
| 127 | - } | |
| 128 | -        if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} | |
| 114 | + /** | |
| 115 | + * @param string $rule | |
| 116 | + * @param int $item | |
| 117 | + * @throws Exception | |
| 118 | + * @return array<int,string> | |
| 119 | + */ | |
| 120 | + private function get_function(string $rule,int &$item) : array | |
| 121 | +	{ | |
| 122 | + // function is : __function(param1,param2...) | |
| 123 | + $start=$item; | |
| 124 | +		while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' | |
| 125 | +		{ | |
| 126 | + $item++; | |
| 127 | + } | |
| 128 | +		if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} | |
| 129 | 129 | |
| 130 | - // get parameters between parenthesis | |
| 130 | + // get parameters between parenthesis | |
| 131 | 131 | |
| 132 | - $this->parse_parenthesis($rule, $item); | |
| 132 | + $this->parse_parenthesis($rule, $item); | |
| 133 | 133 | |
| 134 | - $val=substr($rule,$start,$item-$start); | |
| 134 | + $val=substr($rule,$start,$item-$start); | |
| 135 | 135 | |
| 136 | -        $this->logging->log('got function ' . $val,DEBUG); | |
| 136 | +		$this->logging->log('got function ' . $val,DEBUG); | |
| 137 | 137 | |
| 138 | - return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); | |
| 138 | + return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); | |
| 139 | 139 | |
| 140 | - } | |
| 140 | + } | |
| 141 | 141 | |
| 142 | - /** Find next token $tok in $rule starting at $item | |
| 143 | - * @param string $rule | |
| 144 | - * @param int $item | |
| 145 | - * @param string $tok : token to search for | |
| 146 | - * @throws Exception | |
| 147 | - * @return int Position after next token | |
| 148 | - */ | |
| 149 | - protected function eval_getNext(string $rule,int $item,string $tok) | |
| 150 | -    { | |
| 151 | - while ( | |
| 152 | - ($rule[$item] != $tok ) | |
| 153 | - && ($item < strlen($rule))) | |
| 154 | -        { | |
| 155 | - $item++; | |
| 156 | - } | |
| 157 | -        if ($item==strlen($rule)) { | |
| 158 | -            throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); | |
| 159 | - } | |
| 160 | - return $item+1; | |
| 161 | - } | |
| 142 | + /** Find next token $tok in $rule starting at $item | |
| 143 | + * @param string $rule | |
| 144 | + * @param int $item | |
| 145 | + * @param string $tok : token to search for | |
| 146 | + * @throws Exception | |
| 147 | + * @return int Position after next token | |
| 148 | + */ | |
| 149 | + protected function eval_getNext(string $rule,int $item,string $tok) | |
| 150 | +	{ | |
| 151 | + while ( | |
| 152 | + ($rule[$item] != $tok ) | |
| 153 | + && ($item < strlen($rule))) | |
| 154 | +		{ | |
| 155 | + $item++; | |
| 156 | + } | |
| 157 | +		if ($item==strlen($rule)) { | |
| 158 | +			throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); | |
| 159 | + } | |
| 160 | + return $item+1; | |
| 161 | + } | |
| 162 | 162 | |
| 163 | - /** get negate (!) and return true if found - and pass it with item++ - | |
| 164 | - * @param string $rule | |
| 165 | - * @param int $item | |
| 166 | - * @return boolean | |
| 167 | - */ | |
| 168 | - private function check_negate_first(string $rule,int &$item) | |
| 169 | -    { | |
| 170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. | |
| 171 | -        { | |
| 172 | - $item++; | |
| 173 | - return true; | |
| 174 | - } | |
| 175 | - else | |
| 176 | -        { | |
| 177 | - return false; | |
| 178 | - } | |
| 179 | - } | |
| 163 | + /** get negate (!) and return true if found - and pass it with item++ - | |
| 164 | + * @param string $rule | |
| 165 | + * @param int $item | |
| 166 | + * @return boolean | |
| 167 | + */ | |
| 168 | + private function check_negate_first(string $rule,int &$item) | |
| 169 | +	{ | |
| 170 | + if ( $rule[$item] == '!') // If '!' found, negate next expression. | |
| 171 | +		{ | |
| 172 | + $item++; | |
| 173 | + return true; | |
| 174 | + } | |
| 175 | + else | |
| 176 | +		{ | |
| 177 | + return false; | |
| 178 | + } | |
| 179 | + } | |
| 180 | 180 | |
| 181 | - /** Remove all whitespaces (when not quoted) | |
| 182 | - * @param string $rule | |
| 183 | - * @throws Exception | |
| 184 | - * @return string | |
| 185 | - */ | |
| 186 | - public function eval_cleanup($rule) | |
| 187 | -    { | |
| 188 | - $item=0; | |
| 189 | - $rule2=''; | |
| 190 | - while ($item < strlen($rule)) | |
| 191 | -        { | |
| 192 | -            if ($rule[$item]==' ') { $item++; continue; } | |
| 193 | - if ($rule[$item]=='"') | |
| 194 | -            { | |
| 195 | - $rule2.=$rule[$item]; | |
| 196 | - $item++; | |
| 197 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) | |
| 198 | -                { | |
| 199 | - $rule2.=$rule[$item]; | |
| 200 | - $item++; | |
| 201 | - } | |
| 202 | -                if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); | |
| 203 | - $rule2.=$rule[$item]; | |
| 204 | - $item++; | |
| 205 | - continue; | |
| 206 | - } | |
| 181 | + /** Remove all whitespaces (when not quoted) | |
| 182 | + * @param string $rule | |
| 183 | + * @throws Exception | |
| 184 | + * @return string | |
| 185 | + */ | |
| 186 | + public function eval_cleanup($rule) | |
| 187 | +	{ | |
| 188 | + $item=0; | |
| 189 | + $rule2=''; | |
| 190 | + while ($item < strlen($rule)) | |
| 191 | +		{ | |
| 192 | +			if ($rule[$item]==' ') { $item++; continue; } | |
| 193 | + if ($rule[$item]=='"') | |
| 194 | +			{ | |
| 195 | + $rule2.=$rule[$item]; | |
| 196 | + $item++; | |
| 197 | + while (($item < strlen($rule)) && ($rule[$item]!='"') ) | |
| 198 | +				{ | |
| 199 | + $rule2.=$rule[$item]; | |
| 200 | + $item++; | |
| 201 | + } | |
| 202 | +				if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); | |
| 203 | + $rule2.=$rule[$item]; | |
| 204 | + $item++; | |
| 205 | + continue; | |
| 206 | + } | |
| 207 | 207 | |
| 208 | - $rule2.=$rule[$item]; | |
| 209 | - $item++; | |
| 210 | - } | |
| 208 | + $rule2.=$rule[$item]; | |
| 209 | + $item++; | |
| 210 | + } | |
| 211 | 211 | |
| 212 | - return $rule2; | |
| 213 | - } | |
| 212 | + return $rule2; | |
| 213 | + } | |
| 214 | 214 | |
| 215 | 215 | } | 
| 216 | 216 | \ No newline at end of file | 
| @@ -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 | 
| @@ -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 | 
| @@ -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 | 
| @@ -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 |  		{ |