########################################################################
# 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 and is maintained by:
#    Brian Wood <brian.j.wood@intel.com>
#
# Please send all comments, suggestions, bug reports,
#    etc, to <brian.j.wood@intel.com>.
########################################################

# 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.)
$Storage_file = "/var/lib/logwatch/timestamp.txt";
$count = 0;
$Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;

if (-e $Storage_file) { 
	open(FD, "+<", "$Storage_file") or die $!;
	seek(FD, 0, 0);
	read(FD, $prev_time, 8);
}
else {
	open(FD, ">", "$Storage_file") or die $!;
	$prev_time = "";
}

while (defined($ThisLine = <STDIN>)) {
	#SAMPLE LOG DATA: Oct 15 01:14:33 dmraid-devhost dmeventd[24857]: Processing device "isw_febiihjha_Volume0" for events 
	# All of the elements of the 'split()' aren't used, but could be if custom formating is desired.
	($month, $day, $time, $hostname, $program, $message) = split(' ',$ThisLine, 6);
	chop($program); # Chop off the colon
	if ($prev_time eq "" || $time gt $prev_time) { # If this is the first run or the time is newer than that stored print log entry
		#print "$ThisLine";
		if($ThisLine =~ /Processing Raid|End of|Monitoring device|No longer/ ) {
			$entries{$count} = "$month $day $time:  $message";
		}
		else {
			$entries{$count} = "$month $day $time:    $message";
		}
		$count++; #Keep a count of the number of new logs 
	}
}

if ($count != 0) {
	print ("There were a total of $count new log entries\n\n");
	print ("Date             Message\n");
	print ("------------------------\n");
	$num = 0;
	while ($num < $count) {
		print ("$entries{$num}");
		$num++;
	}
}

seek(FD, 0, 0);
printf FD $time; 
close(FD);
exit(0);

# vi: shiftwidth=3 tabstop=3 syntax=perl et
