How to monitor disk space and jobs in MS SQL server 2005

Following stored procedure is a simple way to check free disk space and jobs in MS SQL Server 2005.

USE [<DB_NAME>]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
— =============================================
— Author:        Duminda Gunasekara
— Create date: 2009-10-20
— Description:    This display the free disk space of drivers and alert if
—                free space drop below certain amount and send email alert
—                if a job failed to run
— =============================================
ALTER PROCEDURE [dbo].[sp_WW_AlertLowDiskSpace]
AS
–local variables
DECLARE        @total_space int
DECLARE        @drive_letter nchar(10)
DECLARE        @low_water_mark int
DECLARE        @current_percentage float
DECLARE        @jobId uniqueidentifier
DECLARE        @endId int, @startId int

BEGIN
SET NOCOUNT ON;

–drive letter and the total disk space hard corded here
select @total_space = 186368; — in MB
select @drive_letter = ‘D’;
select @low_water_mark = 10; — the % value

–tempory table to keep free disk space info
create table #freespace (
drive    nchar(10),
free    int
)

insert into #freespace EXEC xp_fixeddrives

–calculate the free space as a %
select @current_percentage = cast((select free from #freespace where drive = rtrim(@drive_letter)) as float) / cast(@total_space as float) * 100

if @current_percentage < @low_water_mark
begin
–send an email if free space is below the low water mark level
exec msdb..sp_send_dbmail
@profile_name = ‘Test Account’,
@subject = ‘Disk space alert’,
@body = ‘Your free disk space is less than 10%’,
@recipients = ‘<youremail here>’
end

drop table #freespace

–check the log backup schedules
select @jobId = job_id from msdb.dbo.sysjobs where name in (
‘Transactionlog – Backup.Subplan_1’
)

–check the last step that run
select @endId = COALESCE(max(instance_id), 0) from msdb.dbo.sysjobhistory where job_id = @jobId
select @startId = COALESCE(max(instance_id), 0) from msdb.dbo.sysjobhistory where job_id = @jobId and instance_id < @endId

–0 = Failed, 2 = Retry, 3 = Canceled
if exists (select * from msdb.dbo.sysjobhistory where job_id = @jobId and run_status in(0,2,3))
begin
–send an email if any job failed to run
exec msdb..sp_send_dbmail
@profile_name = ‘Test Account’,
@subject = ‘Job execution alert’,
@body = ‘One or more scheduled jobs not executed’,
@recipients = ‘<youremail here>’
end

END;

You can create a new job and execute this SP periodically.