In the code below I started testing my options of allowing for a 
maintenance window for specific days/hours to be specified and then 
attempted to calculate if a specific day/time fell within that defined 
window.  I'm open to suggestions and would like to know if there is a 
better approach altogether for something like this.

#!/usr/bin/perl -w
use strict;
use warnings;

#maintenance time window specification should
#be dynamic by allowing the flexibility of
#specifying the start day of the week with a start
#time (24hour) and then a stop day of the week with
#a stop time
#
#so far by not using ~specific~ date/times, this requirement
#has kept me from using something like Date::Parse and
#doing some conversions to epoch which could allow for
#some easy math.
#
#Start: Sunday 2330 (11:30pm) and Stop: Tuesday's 0930 (9:30am)
my $timespec="6:2330-2:0930";

my @times=split '-', $timespec;

#my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);

my ($maintenance, $colpos, $maintstartday, $maintstarthour, 
$maintstartminute, $maintstopday, $maintstophour, $maintstopminute);

#parse window start time
$colpos=index($times[0],':');
$maintstartday=substr($times[0], 0, $colpos);
$maintstarthour=substr($times[0], $colpos+1, 2);
$maintstartminute=substr($times[0], $colpos+3, 2);

#parse window stop time
$colpos=index($times[1],':');
$maintstopday=substr($times[1], 0, $colpos);
$maintstophour=substr($times[1], $colpos+1, 2);
$maintstopminute=substr($times[1], $colpos+3, 2);

#Debug info
print "START DAY: [$maintstartday], START HOUR: [$maintstarthour], START 
MINUTE: [$maintstartminute]\n";
print "STOP DAY: [$maintstopday], STOP HOUR: [$maintstophour], STOP 
MINUTE: [$maintstopminute]\n";
#print "CUR DAY: [$wday], CUR HOUR: [$hour], CUR MINUTE: [$min]\n";

my $testday="0";
my $testhour="00";
my $testminute="00";
$maintenance=0;

#Debug info
print "TEST DAY: [$testday], TEST HOUR: [$testhour], TEST MINUTE: 
[$testminute]\n";

#this initial logic is all to hell and quickly showed that it was broken 
when I tested a maintenance
#window that spanned multiple days
#
if ($testday == $maintstartday or $testday == $maintstopday) {
    print "Test day falls on one of the maintenance window days\n";
    if (($testday == $maintstartday and $testhour >= $maintstarthour)
        or ($testday == $maintstopday and $testhour <= $maintstophour)
        or ($maintstartday == $maintstopday and $testhour >= 
$maintstarthour and $testhour <= $maintstophour)) {
        print "Test hour falls on or within the maintenance window hours\n";
        if ( ($testday == $maintstopday and $maintstopday != 
$maintstartday and $testminute <= $maintstopminute)
              or ($testday == $maintstartday and $maintstopday != 
$maintstartday and $testminute >= $maintstartminute)
              or ($maintstartday == $maintstopday and $maintstarthour == 
$maintstophour
            and $testminute >= $maintstartminute and $testminute <= 
$maintstopminute)
           ) {
            $maintenance=1;
        }
    }
}

if (!$maintenance) {
    print "maintenance window not active\n";
    exit;
} else {
    print "maintenance window is active!\n";
    exit;
}




_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to