#!/usr/bin/perl # # this script is run to wake up the desktop during the working day # # it runs as a cronjob from 7am to 6pm Monday to Friday # and also as a cronjob with TSM option at 5:50pm on Thursdays # # The options/logic in the script further control which machines # are actually woken/kept awake use lib '/mi/it/assets'; use assets; # read in the conf file use File::Basename; my $wake_conf = dirname($0) . '/wake.conf'; my %start; open(WAKECONF, "$wake_conf") || die "Cannot open $wake_conf for reading: $!"; while ( ) { chomp; next if m/^\#/; my ($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; } my $searchstring="disposed='0' and purpose like '%Desktop%' order by rand()"; #narrow the search to Linux machines if just waking TSM clients if ( $ARGV[0] eq 'TSM' ) { $tsm_wake = 'yes'; $searchstring = "os_type like 'Linux%' and " . $searchstring; } #construct relevant time/date info my @now = localtime(); my $now_in_seconds = $now[2]*60+$now[1]; my $monthday = $now[3]; my $month = $now[4]; my ($start_hour,$start_minute); #don't wake anything up between Christmas and New Year unless for TSM backups exit if ( $month == 11 && $monthday >= 24 && $tsm_wake ne 'yes'); exit if ( $month == 0 && $monthday == 1 && $tsm_wake ne 'yes'); my $db = new assets; $db->init(); my $hosts = $db->get_assets($searchstring); foreach my $i (@$hosts){ my $hostname = $i->{'hostname'}; my @macs= split(/;/, $i->{'mac'}); # check we are in this hosts wake period unless this is a TSM backup wake if ( $tsm_wake ne 'yes') { #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'}); } my $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'}); } my $end_in_seconds = $end_hour*60+$end_minute; # next if we are not within the wake period for this machine next if ! ( $start_in_seconds <= $now_in_seconds && $end_in_seconds >= $now_in_seconds ); } # if we got here then we should wake the machine foreach my $mac (@macs) { $count++; #print STDERR "Waking up $hostname\n"; system "/usr/bin/wakeonlan $mac > /dev/null"; #perl sleep only does integer seconds, #could use Time::HiRes but this `trick' is easier select(undef, undef, undef, 0.9); #without this delay each loop would take about 0.015 seconds } } #print STDERR "Woke $count machines\n";