2013-07-24 Ramon Novoa <rnovoa@artica.es>

* lib/PandoraFMS/Core.pm: Search for sub-strings when matching an OID
	  within an SNMP alert. Regular expressions should not be necessary and
	  this avoids having to escape dots inside the OID. Added a couple of new
	  SNMP macros.

	* lib/PandoraFMS/Tools.pm: Added a function to check for valid regular expressions.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@8568 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Ramon Novoa 2013-07-24 14:09:19 +00:00
parent 52bf1fb57c
commit f58b112698
3 changed files with 50 additions and 17 deletions

View File

@ -1,3 +1,12 @@
2013-07-24 Ramon Novoa <rnovoa@artica.es>
* lib/PandoraFMS/Core.pm: Search for sub-strings when matching an OID
within an SNMP alert. Regular expressions should not be necessary and
this avoids having to escape dots inside the OID. Added a couple of new
SNMP macros.
* lib/PandoraFMS/Tools.pm: Added a function to check for valid regular expressions.
2013-07-23 Juan Manuel Ramón Vigo <juanmanuel.ramon@artica.es> 2013-07-23 Juan Manuel Ramón Vigo <juanmanuel.ramon@artica.es>
* util/pandora_revent_create.pl: Pandora revent only for create events. * util/pandora_revent_create.pl: Pandora revent only for create events.

View File

@ -417,11 +417,7 @@ sub pandora_evaluate_alert ($$$$$$$;$$$) {
if ($alert->{'type'} eq "regex") { if ($alert->{'type'} eq "regex") {
# Make sure the regexp is valid # Make sure the regexp is valid
eval { if (valid_regex ($alert->{'value'}) == 0) {
local $SIG{'__DIE__'};
$data =~ m/$alert->{'value'}/i;
};
if ($@) {
logger ($pa_config, "Error evaluating alert '" . logger ($pa_config, "Error evaluating alert '" .
safe_output($alert->{'name'}) . "' for agent '" . safe_output($alert->{'name'}) . "' for agent '" .
safe_output($agent->{'nombre'}) . "': '" . $alert->{'value'} . "' is not a valid regular expression.", 10); safe_output($agent->{'nombre'}) . "': '" . $alert->{'value'} . "' is not a valid regular expression.", 10);
@ -429,10 +425,10 @@ sub pandora_evaluate_alert ($$$$$$$;$$$) {
} }
if ($alert->{'matches_value'} == 1) { if ($alert->{'matches_value'} == 1) {
return $status if ($data !~ m/$alert->{'value'}/i); return $status if (valid_regex ($alert->{'value'}) == 1 && $data !~ m/$alert->{'value'}/i);
} }
else { else {
return $status if ($data =~ m/$alert->{'value'}/i); return $status if (valid_regex ($alert->{'value'}) == 1 && $data =~ m/$alert->{'value'}/i);
} }
} }
@ -2711,7 +2707,6 @@ sub pandora_evaluate_snmp_alerts ($$$$$$$$$) {
my ($pa_config, $trap_id, $trap_agent, $trap_oid, $trap_type, my ($pa_config, $trap_id, $trap_agent, $trap_oid, $trap_type,
$trap_oid_text, $trap_value, $trap_custom_oid, $dbh) = @_; $trap_oid_text, $trap_value, $trap_custom_oid, $dbh) = @_;
# Get all SNMP alerts # Get all SNMP alerts
my @snmp_alerts = get_db_rows ($dbh, 'SELECT * FROM talert_snmp ORDER BY position ASC'); my @snmp_alerts = get_db_rows ($dbh, 'SELECT * FROM talert_snmp ORDER BY position ASC');
@ -2727,7 +2722,7 @@ sub pandora_evaluate_snmp_alerts ($$$$$$$$$) {
$alert->{'oid'} = decode_entities($alert->{'oid'}); $alert->{'oid'} = decode_entities($alert->{'oid'});
my $oid = $alert->{'oid'}; my $oid = $alert->{'oid'};
if ($oid ne '') { if ($oid ne '') {
next if ($trap_oid !~ m/^$oid$/i && $trap_oid_text !~ m/^$oid$/i); next if (index ($trap_oid, $oid) == -1 && index ($trap_oid_text, $oid) == -1);
$alert_data .= "OID: $oid "; $alert_data .= "OID: $oid ";
} }
@ -2746,14 +2741,18 @@ sub pandora_evaluate_snmp_alerts ($$$$$$$$$) {
# Trap value # Trap value
my $single_value = decode_entities($alert->{'single_value'}); my $single_value = decode_entities($alert->{'single_value'});
if ($single_value ne '') { if ($single_value ne '') {
next if ($trap_value !~ m/^$single_value$/i);
# No match
next if (valid_regex ($single_value) == 0 || $trap_value !~ m/^$single_value$/i);
$alert_data .= "Value: $trap_value "; $alert_data .= "Value: $trap_value ";
} }
# Agent IP # Agent IP
my $agent = decode_entities($alert->{'agent'}); my $agent = decode_entities($alert->{'agent'});
if ($agent ne '') { if ($agent ne '') {
next if ($trap_agent !~ m/^$agent$/i );
# No match
next if (valid_regex ($agent) == 0 || $trap_agent !~ m/^$agent$/i );
$alert_data .= "Agent: $agent"; $alert_data .= "Agent: $agent";
} }
@ -2768,18 +2767,17 @@ sub pandora_evaluate_snmp_alerts ($$$$$$$$$) {
if ($custom_oid ne '') { if ($custom_oid ne '') {
# No match # No match
next if ($trap_custom_oid !~ m/^$custom_oid$/i); next if (valid_regex ($custom_oid) == 0 || $trap_custom_oid !~ m/^$custom_oid$/i);
$alert_data .= " Custom: $trap_custom_oid"; $alert_data .= " Custom: $trap_custom_oid";
} }
# Assign default values to the _snmp_fx_ macros from variable bindings # Assign default values to the _snmp_fx_ macros from variable bindings
my $count;
my @custom_values = split ("\t", $trap_custom_oid); my @custom_values = split ("\t", $trap_custom_oid);
for (my $i = 1; defined ($custom_values[$i-1]); $i++) { for ($count = 1; defined ($custom_values[$count-1]); $count++) {
my $macro_name = '_snmp_f' . $i . '_'; my $macro_name = '_snmp_f' . $count . '_';
if ($custom_values[$i-1] =~ m/= \S+: (.*)/) { if ($custom_values[$count-1] =~ m/= \S+: (.*)/) {
my $value = $1; my $value = $1;
# Strip leading and trailing double quotes # Strip leading and trailing double quotes
@ -2789,6 +2787,13 @@ sub pandora_evaluate_snmp_alerts ($$$$$$$$$) {
$macros{$macro_name} = $value; $macros{$macro_name} = $value;
} }
} }
$count--;
# Number of variables
$macros{'_snmp_argc_'} = $count;
# All variables
$macros{'_snmp_argv_'} = $trap_custom_oid;
# Evaluate _snmp_fx_ filters # Evaluate _snmp_fx_ filters
my $filter_match = 1; my $filter_match = 1;

View File

@ -72,6 +72,7 @@ our @EXPORT = qw(
safe_output safe_output
month_have_days month_have_days
translate_obj translate_obj
valid_regex
); );
######################################################################## ########################################################################
@ -1179,6 +1180,24 @@ sub resolve_hostname ($) {
return inet_ntoa($resolved_hostname); return inet_ntoa($resolved_hostname);
} }
###############################################################################
# Returns 1 if the given regular expression is valid, 0 otherwise.
###############################################################################
sub valid_regex ($) {
my $regex = shift;
eval {
local $SIG{'__DIE__'};
qr/$regex/
};
# Invalid regex
return 0 if ($@);
# Valid regex
return 1;
}
# End of function declaration # End of function declaration
# End of defined Code # End of defined Code