#!/usr/bin/perl -w

=head1 NAME

octo_scheduler - Octopussy Scheduler program

=head1 SYNOPSIS

octo_scheduler

=head1 DESCRIPTION

octo_scheduler is the program used by the Octopussy Project 
to launch Report that have been scheduled 
and generate Syslog Activity RRD Graphs 

=cut

use strict;
use warnings;
use Readonly;

use Date::Manip;
use POSIX qw( strftime );

use AAT::Utils;
use Octopussy;
use Octopussy::Report;
use Octopussy::RRDTool;
use Octopussy::Schedule;

Readonly my $PROG_NAME => 'octo_scheduler';


=head1 FUNCTIONS

=head2 Launch_Report($sched)

Launches Report

=cut

sub Launch_Report
{
    my $sched       = shift;
    my $start_day   = $sched->{start_day};
    my $start_hour  = $sched->{start_hour};
    my $finish_day  = $sched->{finish_day};
    my $finish_hour = $sched->{finish_hour};
    $start_day   =~ s/Day-(\d+)/$1 days/i;
    $start_hour  =~ s/Hour-(\d+)/$1 hours/i;
    $finish_day  =~ s/Day-(\d+)/$1 days/i;
    $finish_hour =~ s/Hour-(\d+)/$1 hours/i;

    my $start =
        Date::Manip::UnixDate(
        Date::Manip::DateCalc('now', "- $start_day $start_hour"), '%Y%m%d%H%M');
    my $finish =
        Date::Manip::UnixDate(
        Date::Manip::DateCalc('now', "- $finish_day $finish_hour"),
        '%Y%m%d%H%M');
    print "$start - $finish\n";

    my $report = Octopussy::Report::Configuration($sched->{report});
    print "Launch Report: $sched->{device} $sched->{service}\n";
    print "               $start $finish\n";

    Octopussy::Report::CmdLine(
        $sched->{device}, $sched->{service},
        ($report->{loglevel} || '-ANY-'), ($report->{taxonomy} || '-ANY-'),
        $report,           $start,
        $finish,           'sched_' . $report->{name} . "_$$",
        $sched->{mail}[0], $sched->{ftp}[0],
        $sched->{scp}[0],  'EN'
    );

    return (1);
}


#
# MAIN
#
my $counter = 0;

exit if (!Octopussy::Valid_User($PROG_NAME));

while (1)
{
    my ($year, $mon, $mday, $hour, $min) = AAT::Utils::Now();
    my $wday = strftime("%w", 0, 0, 0, $mday, $mon - 1, $year - 1900);
    $wday = ($wday == 0 ? $wday = 7 : $wday); # on sunday we want 7 instead of 0
    my %dt = (
        year     => $year,
        month    => $mon,
        day      => $mday,
        wday     => $wday,
        hour     => $hour,
        min      => $min
    );
    my @schedules = Octopussy::Schedule::Configurations('title');

    foreach my $sched (@schedules)
    {
        Launch_Report($sched)	if (Octopussy::Schedule::Match($sched, \%dt));
    }
    Octopussy::RRDTool::Syslog_By_DeviceType_Hourly_Graph();
    if ($counter % 10 == 0)
    {
        Octopussy::RRDTool::Syslog_By_DeviceType_Daily_Graph();
    }
    if ($counter % 30 == 0)
    {
        Octopussy::RRDTool::Syslog_By_DeviceType_Weekly_Graph();
    }
    if ($counter % 60 == 0)
    {
        Octopussy::RRDTool::Syslog_By_DeviceType_Monthly_Graph();
    }
    if ($counter % 720 == 0)
    {
        Octopussy::RRDTool::Syslog_By_DeviceType_Yearly_Graph();
        $counter = 1;
    }
    $counter++;
    sleep 60;
}

=head1 AUTHOR

Sebastien Thebert <octo.devel@gmail.com>

=head1 SEE ALSO

octo_dispatcher, octo_extractor, octo_parser, octo_uparser, octo_reporter

=cut
