#!/usr/bin/perl
#
# $Id: alert-syslog-default.pl,v 1.8 2002/03/15 16:11:29 fpmip Exp $
#

# Author : Tycho Fruru <tycho@fruru.com>

# Copyright (C) 2001 Tycho Fruru <tycho@fruru.com>
# Copyright (C) 2001 Conostix S.A. Luxembourg - Tycho Fruru <tycho@conostix.com>

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

# alerting subroutines take the following arguments
#	event			an IPFC::Event
#	alert_destinations	a ref to an array containing
#				URI's of the destinations of the alerts
#	alert_summary		a one-line description of the alert
#				(if one was generated before)
#	alert_text		a full-text description of the alert
#				(if one was generated before)
#
# the outputs are as follows :
#
#	status			0 = error , 1 = all OK
#	alert_destinations	see above
#	alert_summary		see above
#	alert_text		see above

package IPFC::alerter::alert_syslog_default;

sub main::alert_syslog_default($$$$) {

    my $event = shift;
    my $alert_destinations = shift;
    my $alert_summary = shift;
    my $alert_text = shift;
    my $alert_level = 0; # 0 is lowest, 10 is highest
    my $current_alert_level = 0;
    
    my $current_message = $event->{'message'}->[0];
    study $current_message;

    foreach my $pattern (keys %message) {
    	if ($current_message =~ /$pattern/) {
	
	    my ($s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8,$s9) = ($1,$2,$3,$4,$5,$6,$7,$8,$9);
	    
	    if (defined($message{$pattern}->{'level'})) {
	    	$current_alert_level = $message{$pattern}->{'level'};
	    } else {
	    	$current_alert_level = $default->{'level'};
	    }
	    
	    if ($current_alert_level > $alert_level) {
	        $alert_level = $current_alert_level;
	        if (defined($message{$pattern}->{'alert_destinations'})) {
		    $alert_destinations = $message{$pattern}->{'alert_destinations'};
		} else {
		    $alert_destinations = $default->{'alert_destinations'};
		}
		
		if (defined($message{$pattern}->{'alert_summary'})) {
		    $alert_summary = $message{$pattern}->{'alert_summary'};
		} else {
		    $alert_summary = $default->{'alert_summary'};
		}
		
		my $event_id = $event->{"event_id"}->[0];
		
		$alert_summary =~ s/\$EVENT_ID/$event_id/g;
		$alert_summary =~ s/\$MESSAGE/$current_message/g;
		$alert_summary =~ s/\$PATTERN/$pattern/g;
		$alert_summary =~ s/\$1/$s1/g;
		$alert_summary =~ s/\$2/$s2/g;
		$alert_summary =~ s/\$3/$s3/g;
		$alert_summary =~ s/\$4/$s4/g;
		$alert_summary =~ s/\$5/$s5/g;
		$alert_summary =~ s/\$6/$s6/g;
		$alert_summary =~ s/\$7/$s7/g;
		$alert_summary =~ s/\$8/$s8/g;
		$alert_summary =~ s/\$9/$s9/g;

		
		
		if (defined($message{$pattern}->{'alert_message'})) {
		    $alert_text = $message{$pattern}->{'alert_message'};
		} else {
		    $alert_text = $default->{'alert_message'};
		}

		$alert_text =~ s/\$EVENT_ID/$event_id/g;
		$alert_text =~ s/\$MESSAGE/$current_message/g;
		$alert_text =~ s/\$PATTERN/$pattern/g;
		$alert_text =~ s/\$1/$s1/g;
		$alert_text =~ s/\$2/$s2/g;
		$alert_text =~ s/\$3/$s3/g;
		$alert_text =~ s/\$4/$s4/g;
		$alert_text =~ s/\$5/$s5/g;
		$alert_text =~ s/\$6/$s6/g;
		$alert_text =~ s/\$7/$s7/g;
		$alert_text =~ s/\$8/$s8/g;
		$alert_text =~ s/\$9/$s9/g;

		


	    }
	}
    }
    
    return (1,$alert_destinations,$alert_summary,$alert_text);
}

require "alert-syslog-default-config.pl";

return 1;
