#!/usr/bin/perl # # simple script to read the wake.conf file and decide whether this host is in # a wake period or not before some other task may then be run use File::Basename; $wake_conf = dirname($0) . '/wake.conf'; $hostname = `hostname`; $hostname =~ s/^\s+|\s+$//g; $hostname =~ s/\.maths\.ox\.ac\.uk//; # read in the conf file open(WAKECONF, "$wake_conf") || die "Cannot open $wake_conf for reading: $!"; while ( ) { chomp; next if m/^\#/; ($host,$start_time,$end_time,$start_day,$end_day) = split /,/; $start{$host} = $start_time; $end{$host} = $end_time; $daystart{$host} = $start_day; $dayend{$host} = $end_day; } #get the current time hours and seconds and turn into a seconds count my @now = localtime(); my $now_in_seconds = $now[2]*60+$now[1]; my $now_day = $now[6]; $now_day = 7 if $now_day == 0; #assess if we are on weekday when machine should be awake $daystart{$hostname} = $daystart{'default'} if ( ! defined $daystart{$hostname} ); $dayend{$hostname} = $dayend{'default'} if ( ! defined $dayend{$hostname} ); #exit and proceed with further checks if not a wake day exit 0 if ! ( $daystart{$hostname} <= $now_day && $dayend{$hostname} >= $now_day ); #work out the start and end time of the wake period for this host if ( defined $start{$hostname} ) { #custom start time ($start_hour,$start_minute) = split(/:/,$start{$hostname}); } else { ($start_hour,$start_minute) = split(/:/,$start{'default'}); } $start_in_seconds = $start_hour*60+$start_minute; if ( defined $end{$hostname} ) { #custom end time ($end_hour,$end_minute) = split(/:/,$end{$hostname}); } else { ($end_hour,$end_minute) = split(/:/,$end{'default'}); } $end_in_seconds = $end_hour*60+$end_minute; if ( $start_in_seconds <= $now_in_seconds && $end_in_seconds >= $now_in_seconds ) { # not ok to proceed with further checks print "We are in the wake period for $hostname\n"; # do not proceed with further checks exit 1; } else { #ok to proceed with further checks print "We are not in a wake period for $hostname\n"; exit 0; }