#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Pod::Usage; use Text::CSV_XS; # variables my ( $version, $revision, $help, %lastresult_of, $checknow, %exclusions, %lastresult_excl ); my %ERRORS = ( 'OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3, 'DEPENDENT' => 4, ); $version = '1.1'; Getopt::Long::Configure( "no_ignore_case", "bundling" ); GetOptions( 'c|checknow' => \$checknow, 'h|help|?' => \$help, 'V|version' => \$revision, 'e|exclude=s%' => sub{ push ( @{$exclusions{$_[1]}}, $_[2] ) }, ); # run schtaks, keep output in JOBS memory handle # switches for schtasks.exe: # /query: get the list of scheduled jobs # /fo csv: dump the list as in csv format # /v: verbose # open (JOBS, "schtasks /query /fo csv /v |") or die "couldn't exec schtasks: $!\n"; # create a Text::CSV_XS object my $csv = Text::CSV_XS->new(); # parse JOBS memory handle. The output is a csv file. The second column ($columns[1] is "Taskname", # the 7th $columns[6] is "Last Result". I only need the values of "Last Result" which are NOT 0 (0 is good, it means it ran well). # Because in windows 2008 the task scheduler has been revamped, there are a lot of new scheduled jobs that are not important, so I # filter them in the next if statements while ( my $line = ) { chomp $line; last if $line =~ /^INFO: There are no scheduled tasks.*$/; if ( $csv->parse($line) ) { my @columns = $csv->fields(); next if $columns[1] eq "TaskName"; # skip the header next if $columns[3] eq "Disabled"; # skip if 4th column is 'disabled' next if $columns[2] eq "Disabled"; # skip if next run time is 'disabled' next if $columns[3] eq "Running"; # skip if the task is running now next if $columns[18] eq "At logon time"; # skip if 19th colum is 'At logon time" next if $columns[5] eq "N/A"; # skip if last run time is empty # if last result is other than 0, save taskname and last result in the %lastresult_of if ( $columns[6] != 0 ) { $lastresult_of{ $columns[1] } = $columns[6]; } } } # if the %lastresult_of is empty, this will be zero if ( scalar keys %lastresult_of == 0 ) { print "0K: All scheduled tasks seem to have run fine\n"; exit $ERRORS{OK}; } else { while ( my ( $key, $value ) = each(%lastresult_of) ) { print "WARNING: scheduled task [$key] finished with error [$value]\n"; exit $ERRORS{WARNING}; } } __DATA__ "HostName","TaskName","Next Run Time","Status","Logon Mode","Last Run Time","Last Result","Creator","Schedule","Task To Run","Start In","Comment","Scheduled Task State","Scheduled Type","Start Time","Start Date","End Date","Days","Months","Run As User","Delete Task If Not Rescheduled","Stop Task If Runs X Hours and X Mins","Repeat: Every","Repeat: Until: Time","Repeat: Until: Duration","Repeat: Stop If Still Running","Idle Time","Power Management" "host","Defrag C","04:00:00, 09-10-2011","","Interactive/Background","04:00:00, 02-10-2011","0","SYSTEM","At 04:00 every Sun of every week, starting 26-05-2010","C:\WINDOWS\system32\defrag.exe c:","C:\WINDOWS\system32","N/A","Enabled","Weekly","04:00:00","26-05-2010","N/A","SUNDAY","N/A","domain\SVC.Scheduler","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode" "host","DkTknSrv","05:00:00, 07-10-2011","","Interactive/Background","05:00:00, 06-10-2011","0","SYSTEM","At 05:00 every day, starting 05-05-2011","d:\scripts\DkTknSrv\DkTknSrv.cmd ","d:\scripts\DkTknSrv","N/A","Enabled","Daily ","05:00:00","05-05-2011","N/A","Everyday","N/A","domain\SVC.Scheduler","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode" "host","do stuff then","15:32:00, 06-10-2011","","Interactive/Background","14:32:00, 06-10-2011","0","SYSTEM","Every 1 hour(s) from 21:32 for 24 hour(s) every day, starting 16-02-2011","d:\perl\bin\perl.exe d:\scripts\dostuff.pl","d:\perl\bin","N/A","Enabled","Hourly ","21:32:00","16-02-2011","N/A","Everyday","N/A","domain\SVC.Scheduler","Disabled","72:0","1 Hour(s)","None","24 Hour(s): 0 Minute(s)","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode" "host","Memory Optimization Schedule","Disabled","","Background only","Never","0","SYSTEM","Disabled","C:\Program Files\Citrix\Server Resource Management\Memory Optimization Management\Program\CtxBace.exe -optimize","N/A","N/A","Disabled","At system start up","At system start up","01-01-2001","N/A","N/A","N/A","NT AUTHORITY\SYSTEM","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode" "host","Memory Optimization Schedule","Disabled","","Background only","Never","0","SYSTEM","Disabled","C:\Program Files\Citrix\Server Resource Management\Memory Optimization Management\Program\CtxBace.exe -optimize","N/A","N/A","Disabled","Hourly ","03:00:00","01-01-1999","N/A","Everyday","N/A","NT AUTHORITY\SYSTEM","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode" "host","perfmon-srv","02:00:00, 07-10-2011","","Interactive/Background","02:00:09, 06-10-2011","0","SYSTEM","At 02:00 every day, starting 02-07-2010","d:\scripts\startup\termserv.cmd ","d:\scripts\startup","N/A","Enabled","Daily ","02:00:00","02-07-2010","N/A","Everyday","N/A","domain\SVC.Scheduler","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode" "host","blabla","At system start up","","Interactive/Background","01:36:17, 06-10-2011","0","administrator","Run at system startup","d:\scripts\bla\prog.vbs ","d:\scripts\bla","N/A","Enabled","At system start up","At system start up","02-02-2010","N/A","N/A","N/A","domain\SVC.Scheduler","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode" "host","robocopy dir","15:22:00, 06-10-2011","","Interactive/Background","14:22:00, 06-10-2011","3","SYSTEM","Every 1 hour(s) from 02:22 for 24 hour(s) every day, starting 06-09-2011","d:\scripts\robocopy.exe source d:\dir /copyall /mir /purge /r:2 /w:3 /xf program.exe /xd archive /np /log+:d:\scripts\logs\robocopy.log","d:\scripts","N/A","Enabled","Hourly ","02:22:00","06-09-2011","N/A","Everyday","N/A","domain\SVC.Scheduler","Disabled","72:0","1 Hour(s)","None","24 Hour(s): 0 Minute(s)","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode" "host","shutdown","01:30:00, 07-10-2011","","Interactive/Background","01:30:00, 06-10-2011","0","SYSTEM","At 01:30 every day, starting 04-12-2009","d:\scripts\shutdown\shutdown.cmd ","d:\scripts\shutdown","N/A","Enabled","Daily ","01:30:00","04-12-2009","N/A","Everyday","N/A","domain\SVC.Scheduler","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode" "host","startup","At system start up","","Interactive/Background","01:36:10, 06-10-2011","0","SYSTEM","Run at system startup","d:\Scripts\Startup\startup.cmd ","d:\Scripts\Startup","N/A","Enabled","At system start up","At system start up","04-12-2009","N/A","N/A","N/A","domain\SVC.Scheduler","Disabled","72:0","Disabled","Disabled","Disabled","Disabled","Disabled","No Start On Batteries, Stop On Battery Mode"