#!/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;
no strict "refs";
use AAT;
use Octopussy;
use Date::Manip;

my %day = ("Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, 
	"Friday" => 5, "Saturday" => 6, "Sunday" => 7, "Every Day" => 0);

my %month = ("January" => 1, "February" => 2, "March" => 3, "April" => 4, 
	"May" => 5, "June" => 6, "July" => 7, "August" => 8, 
	"September" => 9, "October" => 10, "November" => 11, "December" => 12,
	"Every Month" => 0);

=head1 FUNCTIONS

=head2 Launch_Report($sched, $y, $m, $d, $hour, $min)

Launches Report

=cut
sub Launch_Report($$$$$$)
{
	my ($sched, $y, $m, $d, $hour, $min) = @_;
	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 = UnixDate(DateCalc("now", "- $start_day $start_hour"), 
		"%Y%m%d%H%M");
	my $finish = UnixDate(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->{taxonomy} || "-ANY-"), $report, $start, $finish, "sched_${report}_$$",
    $sched->{mail}[0], $sched->{ftp}[0], $sched->{scp}[0], "EN");
}

#
# MAIN
#
my $counter = 0;

while (1)
{
	my ($year, $mon, $mday, $hour, $min, $sec) = AAT::Datetime::Now();
	my $wday = AAT::Datetime::WeekDay($year, $mon, $mday);
	my @schedules = Octopussy::Schedule::Configurations("title");
	
	foreach my $sched (@schedules)
	{
		my ($sched_hour, $sched_min) = split(/:/, $sched->{start_time});
		if (($hour == $sched_hour) && ($min == $sched_min))
		{ # time matches
			my $match = 0;
			my $everyday = 0;

			my @dow = defined $sched->{dayofweek} ? @{$sched->{dayofweek}} : ();
			foreach my $dw (@dow)
				{ $match = 1  if ($day{$dw} == $wday); }
			foreach my $dw (@dow)
				{ $everyday = 1	if ($day{$dw} == 0); }
		
			if ($match || $everyday)
			{ # day matches
				$match = 0;
				my @dom = defined $sched->{dayofmonth} ? @{$sched->{dayofmonth}} : ();
				foreach my $dm (@dom)
					{ $match = 1	if ($dm == $mday); }
				$match = 1  if ($everyday);
				if ($match)
				{ # dayofmonth matches
					$match = 0;
					my @months = defined $sched->{month} ? @{$sched->{month}} : ();
					foreach my $m (@months)
						{ $match = 1  if ($month{$m} == ($mon + 1)); }
					foreach my $m (@months)
						{ $match = 1	if ($month{$m} == 0); }
					Launch_Report($sched, $year, $mon, $mday, $hour, $min)  if ($match);
				}
			}
		}
	}
	my @dconfs = Octopussy::Device::Configurations();
	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
