#!/usr/bin/perl # #Usage: check-tsm-schedule #e.g. check-tsm-schedule 8:00 # # Note maths desktops typically backup on Thursday nights, # starting somewhere between 18:30 and 23:45 # Logfile typically has an updated entry every 12 hours when machine is on use Time::Local; if ( ! defined $ARGV[0] ) { print "You need to provide a time for start of next wake period\n"; exit; } #check if there is a dsmc or dsmcad process otherwise exit cleanly if ( `ps ax | grep -v grep | grep dsmc` ) { print "TSM process present\n"; } else { print "No TSM process, exiting\n"; exit 0 } #TSM schedule backup log file $logfile='/var/log/dsmsched.log'; #amount of time to allow for a backup to start and then end too $backup_buffer=60*60; #time of next wakeup read in from command line ($next_wake_hour,$next_wake_minute) = split(/:/,$ARGV[0],2); #get the current time in two ways $now_in_secs=time(); @now = localtime(); $wake_secs_from_day_start=($next_wake_hour*60+$next_wake_minute)*60; $now_secs_from_day_start=($now[2]*60+$now[1])*60+$now[0]; $seconds_to_next_wakeup = $wake_secs_from_day_start - $now_secs_from_day_start; if ($wake_secs_from_day_start < $now_secs_from_day_start) { #then wake up is at least tomorrow so adjust $seconds_to_next_wakeup = 24*60*60 + $seconds_to_next_wakeup; } print "Seconds to next wake up >= $seconds_to_next_wakeup\n"; $logline=`grep "Server Window Start" $logfile | tail -1`; if ( $logline eq '' ) { print "TSM client is running but backup start time not defined\n"; exit 0; } $logline =~ /.*Start:\s*(\d*):(\d*):(\d*) on (\d*)-(\d*)-(\d*)/; $schedule_time_secs=timelocal($3,$2,$1,$4,$5-1,$6); $seconds_to_scheduled_backup=$schedule_time_secs-$now_in_secs; print "Seconds to next backup = $seconds_to_scheduled_backup\n"; if ($seconds_to_scheduled_backup < -$backup_buffer ) { #NOTE: don't strictly need this buffer step but it can be useful #backup should have started more than buffer period ago and hence finished so #can proceed with a shutdown print "Backup start time passed more than buffer $backup_buffer seconds ago\n"; exit 0; } elsif ( $seconds_to_scheduled_backup < 0 ) { #backup should have started but we are in the buffer period so #it may actually not have started (TSM has a 15 minute slot to #start in after scheduled start time) or still be running $logline=`grep "SCHEDULEREC OBJECT END" $logfile | tail -1`; $logline =~ /^(\d*)-(\d*)-(\d*) (\d*):(\d*):(\d*) --- /; $backup_end_time_secs=timelocal($6,$5,$4,$1,$2-1,$3); if ($backup_end_time_secs > $schedule_time_secs && $schedule_time_secs < $now) { print "Backup start time has passed, we are in the buffer period but backup confirmed completed\n"; exit 0; } else { print "Backup start time has passed, we are in the buffer period and backup not confirmed completed\n"; exit 1; } } elsif ( $seconds_to_scheduled_backup > $seconds_to_next_wakeup ) { #will wake up automatically before next backup so safe to go off now print "No backup scheduled within remainder of this shutdown period\n"; exit 0; } else { #backup has not occurred and will occur within this period so stay on for now print "Backup scheduled to start within remainder of this shutdown period\n"; exit 1; }