|
From: Bob T. <bt...@us...> - 2004-02-10 07:34:20
|
Update of /cvsroot/benson/benson3/handlers In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9514/handlers Modified Files: A10email.pm Log Message: Added some basic xml support to A10email.pm, not quite functional, still testing. Index: A10email.pm =================================================================== RCS file: /cvsroot/benson/benson3/handlers/A10email.pm,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -r1.8 -r1.9 *** A10email.pm 27 Jan 2004 07:46:44 -0000 1.8 --- A10email.pm 10 Feb 2004 07:31:12 -0000 1.9 *************** *** 4,7 **** --- 4,8 ---- use Benson::Constants; use POSIX; + use XML::Simple; sub match_total_count { *************** *** 19,22 **** --- 20,53 ---- } + sub match_weekday_time { + my ($self, $start, $end, $weekday) = @_; + my $timestamp = time(); + my $current_time = POSIX::strftime("%H%M%S", localtime($timestamp)); + my $current_weekday = POSIX::strftime("%u", localtime($timestamp)); # 1-7 starting on Monday + my $current_weekday = $current_weekday - 1; # 0-6 starting on Monday + + my @days; + push @days, substr($weekday, 1, 1); # Monday + push @days, substr($weekday, 2, 1); # Tuesday + push @days, substr($weekday, 3, 1); # Wednesday + push @days, substr($weekday, 4, 1); # Thursday + push @days, substr($weekday, 5, 1); # Friday + push @days, substr($weekday, 6, 1); # Saturday + push @days, substr($weekday, 0, 1); # Sunday + + my $weekday_matched = 0; + if($days[$current_weekday] ne "-") { + $weekday_matched = 1; + } + my $time_matched = 0; + if(($start < $current_time) && ($end > $current_time)) { + $time_matched = 1; + } + if($weekday_matched && $time_matched) { + return 1; + } + return 0; + } + sub parse_legacy_alias { my ($self, $alias) = @_; *************** *** 28,31 **** --- 59,65 ---- $hash{protocol} = "legacy_file"; $hash{parameter} = $parameter; + } elsif($protocol eq "xml") { + $hash{protocol} = "xml_file"; + $hash{parameter} = $parameter; } elsif($protocol eq "email") { $hash{protocol} = "email"; *************** *** 38,41 **** --- 72,146 ---- } + sub parse_xml_rule_block { + my ($self, $rule_name, $rule_ref) = @_; + + my %hash; + $hash{line_count} = $rule_name; + $hash{count} = $rule_ref->{rule}->{$rule_name}->{count}; + $hash{starttime} = $rule_ref->{rule}->{$rule_name}->{time}->{start}; + $hash{endtime} = $rule_ref->{rule}->{$rule_name}->{time}->{end}; + $hash{weekday} = $rule_ref->{rule}->{$rule_name}->{time}->{weekday}; + + # + # Grab the agents + # + my $agents = $rule_ref->{rule}->{$rule_name}->{agent}; + if(ref($agents) eq "ARRAY") { ### for multiple values + foreach my $agent (@{$agents}) { + push @{$hash{agents}}, $agent->{value}; + } + } elsif(ref($agents) eq "HASH") { ### for single value + push @{$hash{agents}}, $agents->{value}; + } else { ### for no value + push @{$hash{agents}}, ".*"; + } + + # + # Grab the severities + # + my $severities = $rule_ref->{rule}->{$rule_name}->{severity}; + if(ref($severities) eq "ARRAY") { ### for multiple values + foreach my $severity (@{$severities}) { + push @{$hash{severity}}, $severity->{value}; + } + } elsif(ref($severities) eq "HASH") { ### for single value + push @{$hash{severity}}, $severities->{value}; + } else { ### for no value + push @{$hash{severity}}, ".*"; + } + + # + # Grab the subsystems + # + my $subsystems = $rule_ref->{rule}->{$rule_name}->{subsystem}; + if(ref($subsystems) eq "ARRAY") { ### for multiple values + foreach my $subsystem (@{$subsystems}) { + push @{$hash{subsystem}}, $subsystem->{value}; + } + } elsif(ref($subsystems) eq "HASH") { ### for single value + push @{$hash{subsystem}}, $subsystems->{value}; + } else { ### for no value + push @{$hash{subsystem}}, ".*"; + } + + # + # Grab the messages + # + my $messages = $rule_ref->{rule}->{$rule_name}->{message}; + if(ref($messages) eq "ARRAY") { ### for multiple values + foreach my $subsystem (@{$messages}) { + push @{$hash{message}}, $message->{value}; + } + } elsif(ref($messages) eq "HASH") { ### for single value + push @{$hash{message}}, $messages->{value}; + } else { ### for no value + push @{$hash{message}}, ".*"; + } + + $hash{count} = ".*"; + $hash{action} = $self->parse_legacy_alias("email:btribit\@newportal.com"); + return \%hash; + } + sub parse_legacy_line { my ($self, $line_count, $line) = @_; *************** *** 48,51 **** --- 153,159 ---- $hash{line_count} = $line_count; $hash{pass} = $pass; + $hash{starttime} = "000000"; # Start at the beginning of the day + $hash{endtime} = "235961"; # Finish at the end of the day + $hash{weekday} = "SMTWTFS"; # Go all week, by default push @{$hash{agents}}, $agent; push @{$hash{severity}}, $severity; *************** *** 58,61 **** --- 166,187 ---- } + sub parse_xml_file { + my ($self, $filename, $t) = @_; + my $s = $t->server; + my $identity = $t->identity; + my @rules; + my $xs = new XML::Simple; + + $s->trace("M ($identity) A10email opening xml $filename...", 3); + + my $ref = $xs->XMLin($filename); + + foreach my $rule_name (keys %{$ref->{rule}}) { + push @rules, $self->parse_xml_rule_block($rule_name, $ref); + } + + return \@rules; + } + sub parse_legacy_file { my ($self, $filename, $t) = @_; *************** *** 66,70 **** my $s = $t->server; ! $s->trace("M ($identity) A10email opening $filename...", 3); --- 192,196 ---- my $s = $t->server; ! $s->trace("M ($identity) A10email opening legacy $filename...", 3); *************** *** 114,118 **** # # If the rule is for a legacy_file read that. ! # if ($rule->{action}->{protocol} eq "legacy_file") { my $legacy_filename = $rule->{action}->{parameter}; --- 240,245 ---- # # If the rule is for a legacy_file read that. ! # Else if the rule is for an xml_file read that. ! # if ($rule->{action}->{protocol} eq "legacy_file") { my $legacy_filename = $rule->{action}->{parameter}; *************** *** 148,151 **** --- 275,296 ---- $rule_files->{$legacy_filename} = \%hash; $not_done = 1; + } elsif($rule->{action}->{protocol} eq "xml_file") { + my $xml_filename = $rule->{action}->{parameter}; + if($xml_filename eq $file_key) { + next; + } + if($rule_files->{$xml_filename}->{processed} != 0) { + next; + } + my %hash; + if(($rules = $self->parse_xml_file($xml_filename, $t)) == -1) { + $hash{processed} = -1; + $s->log("M ($identity) File $xml_filename not found", WARNING); + } else { + $hash{processed} = 0; + } + $hash{rules} = $rules; + $rule_files->{$xml_filename} = \%hash; + $not_done = 1; } } *************** *** 208,216 **** if($agent_found && $severity_found && $subsystem_found && $message_found && ! $self->match_total_count($total, $rule->{count})) { $s->trace("C ($identity) matched on $filename:$line_count", 4); $s->trace("C ($identity) action is ".$rule->{action}->{protocol}.":".$rule->{action}->{parameter}, 4); ! if($rule->{action}->{protocol} eq "legacy_file") { my $rc; if(($rc = $self->process_trap($t, $rule_files, $list, $rule->{action}->{parameter})) == -1) { --- 353,363 ---- if($agent_found && $severity_found && $subsystem_found && $message_found && ! $self->match_total_count($total, $rule->{count}) && ! $self->match_weekday_time($rule->{starttime}, $rule->{endtime}, $rule->{weekday})) { $s->trace("C ($identity) matched on $filename:$line_count", 4); $s->trace("C ($identity) action is ".$rule->{action}->{protocol}.":".$rule->{action}->{parameter}, 4); ! if(($rule->{action}->{protocol} eq "legacy_file") || ! ($rule->{action}->{protocol} eq "xml_file")) { my $rc; if(($rc = $self->process_trap($t, $rule_files, $list, $rule->{action}->{parameter})) == -1) { *************** *** 231,236 **** } else { my $total_matched = $agent_found + $severity_found + $subsystem_found + ! $message_found + $self->match_total_count($total, $rule->{count}); ! $s->trace("C ($identity) did not match ($total_matched/5) on $filename:$line_count", 4); } } --- 378,384 ---- } else { my $total_matched = $agent_found + $severity_found + $subsystem_found + ! $message_found + $self->match_total_count($total, $rule->{count}) + ! $self->match_weekday_time($rule->{starttime}, $rule->{endtime}, $rule->{weekday}); ! $s->trace("C ($identity) did not match ($total_matched/6) on $filename:$line_count", 4); } } |