########################################################################
# Copyright (C) 2007-2008, Intel Corp. All rights reserved.
#
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
########################################################################

########################################################
# dmeventd
########################################################

########################################################
# This was written by:
#    Brian Wood <brian.j.wood@intel.com>
#
# Changes to get it to work:
#    Heinz Mauelshagen <heinzm@redhat.com>
#
#    Needs more tweking to deal with absolute timestamp timestamps properly
#    in order to avoid mismatches in the log file being displayed!!!
#
# 
########################################################

# Set the location of the folder to store last time stamp
# (This is used to record the last log sent out so repeats 
#  are not mailed in error.)
$syslogpattern_file =
        "/etc/logwatch/scripts/services/dmeventd_syslogpattern.txt";

if (-e $syslogpattern_file) {
	open(FD, "+<", $syslogpattern_file) or die $!;        
        $last_pattern = join('', <FD>);
} else {
	open(FD, ">", $syslogpattern_file) or die $!;
	$last_pattern = "";
}

# SAMPLE LOG DATA:
# Oct 15 01:14:33 dmraid-devhost dmeventd[24857]: Processing device \
# "isw_febiihjha_Volume0" for events
@entries = ();
@pattern = ();

while (<>) {
	($month, $day, $time, $message) = split(' ', $_, 4);
	($m, $d, $t, $mes) = split(' ', @pattern[0], 4);
	if ($time ne $t) {
		@pattern = ();
	} 

	# New pattern.
	push (@pattern, $_);
	push (@entries, $_);

	if (join('', @pattern) eq $last_pattern) {
                @entries = ();
                @pattern = ();
                $last_pattern = "";
	}
}

if ($#entries > -1) {
	print("There were a total of ", $#entries + 1, " new log entries\n\n");
	print("Date             Message\n");
	print("-" x 80, "\n");
	print @entries;

	# Now save current pattern.
        close(FD);
        open(FD, ">", $syslogpattern_file) or die $!;
	printf FD join('',@pattern);
}

close(FD);
exit(0);

# vi: shiftwidth=3 syntax=perl et
