#!/usr/bin/perl

use strict;
use warnings;

# 20060206
# Do logcheck for each host logged in /var/log/internal.foo by syslog-ng

# Will /var/log/logcheck need to be pruned now?
# Do we need to check if hour == 0 and pass it yesterday and today's logfiles?

$logbase = 'internal.foo';
my @hosts = qw/ faith rebecca sarah nebula /;
my ($cmd,$ret);

# Hack
my $YY = `date '+%Y' -d '1 hour ago'`;
my $mm = `date '+%m' -d '1 hour ago'`;
my $dd = `date '+%d' -d '1 hour ago'`;
chomp( $YY );
chomp( $mm );
chomp( $dd );

foreach my $host (@hosts) {

	my $path = "/var/log/$logbase/$host/$YY/$mm/$dd";

	my @files;
	opendir( D, $path );
	foreach (readdir(D)) {
		my $filename;
		next if m/^\.\.?$/;
		if( -f "$path/$_" ) {
			push( @files, "$path/$_" );
		}
	}
	close( D );

	my $list = join( "\n", @files ) . "\n";

	my $listfiles;

	# Hack and hack to be owned by logcheck
	$cmd = 'mktemp /tmp/logreports.XXXXXX';
	if( $< == 0 ) {
		$listfiles = `su -s /bin/bash -c '$cmd' logcheck`;
	}
	else {
		$listfiles = `$cmd`;
	}

	chomp( $listfiles );
	open( F, ">$listfiles" );
	print F $list;
	close( F );

	# What if we run as non root?  Must be root or logcheck!
	$cmd = "/usr/sbin/logcheck -H $host -L $listfiles";
	if( $< == 0 ) {
		system( "su -s /bin/bash -c '$cmd' logcheck" );
	}
	else {
		system( "$cmd" );
	}
	# Should be 0
	$ret = $? >> 8;

	unlink( $listfiles ) or die $!;
}


