Apache-Logmonster

 view release on metacpan or  search on metacpan

bin/logmonster.pl  view on Meta::CPAN

#!/usr/bin/perl

use strict;
use warnings;

use lib 'lib';

use English qw( -no_match_vars );
use Getopt::Long;
use Pod::Usage;
use Apache::Logmonster '3.07';
use Apache::Logmonster::Utility 5; 

my %command_line_options = (
    'bump:i'     => \my $bump,       # an optional time offset
    'clean!'     => \my $clean,      # ability to override conf file
    'interval:s' => \my $interval,   # hour/day/month
    'hourly'     => \my $hourly,
    'daily'      => \my $daily,
    'monthly'    => \my $monthly,
    'n'          => \my $dry_run,    # just show what we would do
    'verbose+'   => \my $verbose,    # incremental -v options
    'report'     => \my $report_mode,
);
if ( ! GetOptions (%command_line_options) ) {
    pod2usage;
};

# a generic utility object that provides many useful functions
my $lm = Apache::Logmonster->new( $verbose );
my $utility = $lm->get_util();
my $banner  = "\n\t\t Apache Log Monster \n\n";
my $config  = $lm->get_config( 'logmonster.conf' );

$config->{'clean'} = $clean if defined $clean; # allow CLI to override file
$config->{'time_offset'} = $bump if defined $bump;


# if this is not enabled, our report formatting will be jumbled
$OUTPUT_AUTOFLUSH++;

if ( $verbose && ! $report_mode ) {
  print $verbose == 1 ? "verbose mode (1).\n"
      : $verbose == 2 ? "very verbose mode (2).\n"
      : $verbose == 3 ? "screaming at you (3).\n"
      : "unknown verbosity\n";
};

print $banner if $verbose;

# run sanity tests
$lm->check_config();

# CLI backwards compatability with previous versions
$interval ||= $hourly  ? "hour"
            : $daily   ? "day"
            : $monthly ? "month"
            : q{};

my %valid_intervals = ( hour => 1, day => 1, month => 1 );

if ( ! defined $valid_intervals{$interval} ) {
    pod2usage;
};

# stuff a few settings into the $lm object so
# Apache::Logmonster functions can access them.

my @hosts = split(/ /, $config->{'hosts'});
my $host_count = @hosts;
$lm->{'host_count'} = $host_count;
$lm->{'rotation_interval'} = $interval;
$lm->{'dry_run'} = $dry_run || 0;

if ($report_mode) {
    # prints out the last intervals hit count and exit, useful for SNMP
    $lm->report_hits();
    exit 1;
};

# open a file to log our activities to
my $REPORT = $lm->report_open("Logmonster", $verbose);

# store the file handle in the $lm object for functions
$lm->{'report'} = $REPORT;

$lm->_progress($banner) if $verbose;
print $REPORT $banner;

# do the work
$lm->fetch_log_files();
my $domains_ref = $lm->split_logs_to_vhosts();
$lm->sort_vhost_logs      () if !$dry_run;
$lm->feed_the_machine     ($domains_ref);
$lm->report_close         ($REPORT);

exit 1;

__END__



( run in 0.738 second using v1.01-cache-2.11-cpan-13bb782fe5a )