Monitor SQL Server Agent Jobs with Policy Based Management
I need to monitor all the SQL Agents to find out if a job failed in the last 24 hours. Could this be done with policy based management? If so, can you show me how to create the policy?
Yes, Policy-Based Management includes the functionality to execute a SQL script for a facet. We will leverage this functionality to run a script against the server facet to count failed jobs. If the count does not equal zero we will have the policy fail.
If you are new to Policy-Based Management please see the following tips before continuing.
- Using Policy-Based Management in SQL Server 2008
- Administering Servers by Using Policy-Based Management
Step 1: Right click on conditions and select new condition. This will allow us to build the condition we need to evaluate our policy we will create soon.
Step 2: Enter the title you would like for your condition. In this example we will use "SQL Agent Jobs executed in last 24 hours." We will also use the server facet so we can validate SQL Agent jobs against multiple servers.
Step 3: Next we will click on the ellipse next to the field and paste the following ExecuteSQL command below. The following script will count all the failed jobs within the last day. The majority of this script comes from this tip by Jeremy Kadlec.
ExecuteSql('Numeric', '-- Variable Declarations
DECLARE @PreviousDate datetime
DECLARE @Year VARCHAR(4)
DECLARE @Month VARCHAR(2)
DECLARE @MonthPre VARCHAR(2)
DECLARE @Day VARCHAR(2)
DECLARE @DayPre VARCHAR(2)
DECLARE @FinalDate INT
-- Initialize Variables
SET @PreviousDate = DATEADD(dd, -1, GETDATE()) -- Last day
SET @Year = DATEPART(yyyy, @PreviousDate)
SELECT @MonthPre = CONVERT(VARCHAR(2), DATEPART(mm, @PreviousDate))
SELECT @Month = RIGHT(CONVERT(VARCHAR, (@MonthPre + 1000000000)),2)
SELECT @DayPre = CONVERT(VARCHAR(2), DATEPART(dd, @PreviousDate))
SELECT @Day = RIGHT(CONVERT(VARCHAR, (@DayPre + 1000000000)),2)
SET @FinalDate = CAST(@Year + @Month + @Day AS INT)
-- Count failed jobs within last day
FROM msdb.dbo.sysjobhistory h
INNER JOIN msdb.dbo.sysjobs j
ON h.job_id = j.job_id
INNER JOIN msdb.dbo.sysjobsteps s
ON j.job_id = s.job_id
INNER JOIN ( SELECT job_id, max(instance_id) maxinstanceid
WHERE run_status <> 1
GROUP BY job_id) a ON h.job_id = a.job_id
AND h.instance_id = a.maxinstanceid
WHERE h.run_status = 0 -- Failure
AND h.run_date > @FinalDate')
Step 5: Now that we have created our new condition we will create a policy and add our new condition. Right click on Policies and select New Policy.
Step 6: Select the condition we just created "SQL Agent jobs succeeded in last 24 hours" in the Check condition drop down. We will also name the policy "All SQL Agent jobs succeeded in the last 24 hours." Let's click on the OK button to create our new policy.
Step 7: Now that we create our policy lets evaluate it. You should now see your new policy "All SQL Agent jobs succeeded in the last 24 hours" Right click on the policy and click on evaluate.
Step 8: Now you will see the results of your evaluation. In this case the policy passed which means there were no failures in the last day for this instance of SQL Server Agent. If it did fail, I would recommend looking at this tip to get more details towards your failed job executions against that instance of SQL Server.
- Review these additional tips:
Last Updated: 2010-12-27
About the author
View all my tips