-- Script to monitor available disk space -- -- Replace string with the operator name -- Replace string with the database name where the 'usp_dba_diskspace_monitor' -- stored procedure will be created -- Replace with the drive name. If not specified, all physical drives will be monitored against -- the same space limit specified by @spacelimit parameter -- Replace with the proper value. This value is used to check the available space left. -- It is passed to @spacelimit parameter. If the available disk space is less then @spacelimit the alert gets -- sent to an operator. -- Job schedule can be modified according to your requirements. -- -- add message EXEC sp_addmessage @msgnum = 60001, @severity = 16, @msgtext = N'Free space on drive %s is less then specified limit of %d MB. Available space is %d MB.', @lang = 'us_english' go -- create alert IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Free disk space reached specified limit')) ---- Delete the alert with the same name. EXECUTE msdb.dbo.sp_delete_alert @name = N'Free disk space reached specified limit' BEGIN EXECUTE msdb.dbo.sp_add_alert @name = N'Free disk space reached specified limit', @message_id = 60001, @severity = 0, @enabled = 1, @delay_between_responses = 0, @include_event_description_in = 5, @category_name = N'[Uncategorized]' EXECUTE msdb.dbo.sp_add_notification @alert_name = N'Free disk space reached specified limit', @operator_name = N'', @notification_method = 1 END go -- create stored procedure use go if exists (select * from sysobjects where id = object_id(N'[dbo].[usp_dba_diskspace_monitor]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[usp_dba_diskspace_monitor] GO create procedure usp_dba_diskspace_monitor @drivename varchar(5) = NULL, @spacelimit int as BEGIN DECLARE @drive varchar(5) DECLARE @MB_free int DECLARE @msg varchar(255) SET nocount on create table #diskspace (drive varchar(5) not null, MB_free int not null) insert into #diskspace exec master..xp_fixeddrives IF @drivename is NULL BEGIN DECLARE drive_curs CURSOR FOR select drive, MB_free from #diskspace OPEN drive_curs FETCH NEXT FROM drive_curs INTO @drive, @MB_free WHILE @@FETCH_STATUS = 0 BEGIN IF @MB_free < @spacelimit BEGIN RAISERROR(60001, 16, 1, @drive, @spacelimit, @MB_free) with log select @msg = 'Drive ' + RTRIM(@drive) + ': Free disk space reached specified limit' print ' ' print @msg print ' ' END ELSE BEGIN select @msg = 'Drive ' + RTRIM(@drive) + ': There is plenty of available disk space' print ' ' print @msg print ' ' END FETCH NEXT FROM drive_curs INTO @drive, @MB_free END close drive_curs deallocate drive_curs END ELSE -- drivename is not null BEGIN select @drive = drive, @MB_free = MB_free from #diskspace where drive = @drivename IF @@ROWCOUNT = 0 BEGIN select @msg = 'Could not find information for drive ' + @drivename return 1 END IF @MB_free < @spacelimit BEGIN RAISERROR(60001, 16, 1, @drive, @spacelimit, @MB_free) with log select @msg = 'Drive ' + RTRIM(@drive) + ': Free disk space reached specified limit' print ' ' print @msg print ' ' END ELSE BEGIN select @msg = 'Drive ' + RTRIM(@drive) + ': There is plenty of available disk space' print ' ' print @msg print ' ' END END drop table #diskspace END go -- create a job to execute the usp_dba_diskspace_monitor procedure BEGIN TRANSACTION DECLARE @JobID BINARY(16) DECLARE @ReturnCode INT SELECT @ReturnCode = 0 IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'[Uncategorized (Local)]') < 1 EXECUTE msdb.dbo.sp_add_category @name = N'[Uncategorized (Local)]' -- Delete the job with the same name (if it exists) SELECT @JobID = job_id FROM msdb.dbo.sysjobs WHERE (name = N'# DBA - Monitor Disk Space') IF (@JobID IS NOT NULL) BEGIN -- Check if the job is a multi-server job IF (EXISTS (SELECT * FROM msdb.dbo.sysjobservers WHERE (job_id = @JobID) AND (server_id <> 0))) BEGIN -- There is, so abort the script RAISERROR (N'Unable to import job ''# DBA - Monitor Disk Space'' since there is already a multi-server job with this name.', 16, 1) GOTO QuitWithRollback END ELSE -- Delete the [local] job EXECUTE msdb.dbo.sp_delete_job @job_name = N'# DBA - Monitor Disk Space' SELECT @JobID = NULL END BEGIN -- Add the job EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N'# DBA - Monitor Disk Space', @owner_login_name = N'sa', @description = N'No description available.', @category_name = N'[Uncategorized (Local)]', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback -- Add the job steps EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N'step1', @command = N'exec usp_dba_diskspace_monitor @drivename = '''', @spacelimit = ', @database_name = N'', @server = N'', @database_user_name = N'', @subsystem = N'TSQL', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback -- Add the job schedules EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N'Monitor disk space', @enabled = 1, @freq_type = 4, @active_start_date = 20000605, @active_start_time = 230000, @freq_interval = 1, @freq_subday_type = 1, @freq_subday_interval = 0, @freq_relative_interval = 0, @freq_recurrence_factor = 0, @active_end_date = 99991231, @active_end_time = 235959 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback -- Add the Target Servers EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N'(local)' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback END COMMIT TRANSACTION GOTO EndSave QuitWithRollback: IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION EndSave: go