|
This script allows you to check whether any scheduled jobs have failed and email the errors to the administrator(s).
The script is perfect for SQL Server 2000 deployments which are clustered. This is because clustering does not support SQL Mail simply because applications like Outlook etc. are not cluster aware.
One of the main advantages of this script is that unlike normal SQL Server job error notifications, the failures are emailed in HTML and the error message is clearly displayed.

------------------------------------------------------------------------------
-- Check Jobs.sql
-- Written by John Buoro
-- This script checks the sysjobs table and reports and jobs which have failed.
-- This script is used on clustered SQL 2000 servers that are unable to properly use SQL Mail.
-- To use, place this script in a scheduled job which runs every 5 minutes.
-- This script requires xp_smtp_sendmail http://www.sqldev.net/xp/xpsmtp.htm to be installed.
-- The value of @MinutesAgo must be set to the same repeat interval as when the job runs.
-- You will need to strip out these comments before pasting into a job step otherwise the script -- will be truncated.
-- 26/06/2007 - John Buoro - Script created.
-- 08/10/2007 - John Buoro - Added %TEMP% environment variable.
DECLARE @MinutesAgo int
DECLARE @OutputFileName varchar(50)
DECLARE @MailAlertTo varchar(100)
DECLARE @SMTPServer varchar(30)
-- Configuration variables.
SET @MinutesAgo = 5 -- Number of minutes when this job will next run (ie. the repeat frequency of the job)
SET @OutputFileName = 'CheckJobs.html' -- Temporary output file (do not add path)
SET @MailAlertTo = 'DBAdmin@somewhere.com' -- Email address to send alerts to.
SET @SMTPServer = 'SMTPSERVER' -- SMTP server name.
-- Drop temp table.
IF OBJECT_ID('tempdb..##tmp1') IS NOT NULL
BEGIN
DROP TABLE ##tmp1
END
-- Drop temp table.
IF OBJECT_ID('tempdb..##tmp2') IS NOT NULL
BEGIN
DROP TABLE ##tmp2
END
-- Find all failed jobs.
SELECT server as 'Server', [name] as 'Job Name',
CONVERT(datetime,
SUBSTRING(CAST(run_date as varchar(8)),1,4) + '-' +
SUBSTRING(CAST(run_date as varchar(8)),5,2) + '-' +
SUBSTRING(CAST(run_date as varchar(8)),7,2) + ' ' +
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(8)),6),1,2) + ':' +
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(8)),6),3,2) + ':' +
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(8)),6),5,2) ) as RunDateTime,
message as 'Error Message'
INTO ##tmp1
FROM msdb..sysjobhistory jh join msdb..sysjobs j ON jh.job_id=j.job_id
WHERE run_status = 0 and step_id = 0
-- Find all failed jobs in the last @MinutesAgo.
SELECT * INTO ##tmp2 FROM ##tmp1
WHERE RunDateTime >= DATEADD(mi, -@MinutesAgo, GETDATE())
-- Any results found?
IF @@ROWCOUNT > 0
BEGIN
-- Get the %TEMP% environment variable.
DECLARE @tempvar varchar(1000)
CREATE TABLE #tempvartable(info VARCHAR(1000))
INSERT #tempvartable EXEC master..xp_cmdshell 'echo %temp%'
SET @tempvar = (SELECT top 1 info FROM #tempvartable)
IF RIGHT(@tempvar, 1) <> '\' SET @tempvar = @tempvar + '\'
DROP TABLE #tempvartable
DECLARE @sub varchar(100)
DECLARE @svr varchar(100)
DECLARE @rc int
SET @svr = (select @@servername) + '@tabcorp.com.au'
SET @sub = 'Scheduled job failure on SQL server ' + @@servername
SET @OutputFileName = @tempvar + @OutputFileName
-- Generate HTML results for query.
EXEC @rc = master..sp_makewebtask
@outputfile = @OutputFileName,
@query = 'select * from ##tmp2',
@lastupdated = 0,
@HTMLheader = 3,
@webpagetitle = @sub,
@resultstitle = @sub
-- Email the results.
-- xp_smtp_sendmail http://www.sqldev.net/xp/xpsmtp.htm
EXEC @rc = master.dbo.xp_smtp_sendmail
@FROM = @svr,
@FROM_NAME = @@servername,
@TO = @MailAlertTo,
@priority = 'HIGH',
@subject = @sub,
@type = 'text/html',
@messagefile = @OutputFileName,
@server = @SMTPServer
END
-- Drop temp tables.
DROP TABLE ##tmp1
DROP TABLE ##tmp2
------------------------------------------------------------------------------