Disk space and database size email alerts
About
A previous client was struggling with disk space on one of their SQL Servers. We wanted to get early visibility of where potential errors could arise, and looked online for some samples which led us to these two articles;
MSDN ArticleSQL Server Central Article
Our adapted code will send you an alert for low space, a statement of drive space and a list of database sizes, we then scheduled it as part of a job to run at various times of the day.
You will need to set up an email account for your server to use, I will try and add an article for this myself, but in the mean time have a look at this article by Pinal Dave on SQL Authority.com or this MSDN Article.
SQL Code
USE [utilities]GOCREATE PROCEDURE [maint].[DiskFreeSpaceAlert]@DriveCBenchmark int= 1024,@OtherDataDriveBenchmark int= 10240,@ProfileName sysname='DatabaseEmailProfileName',@Recipients NVARCHAR(1000)='youremail@yourdomain'AS BEGIN IF EXISTS(SELECT * FROM tempdb..sysobjectsWHERE id =object_id(N'[tempdb]..[##disk_free_space]'))DROP TABLE ##disk_free_spaceCREATE TABLE ##disk_free_space( DriveLetter CHAR(1) NOT NULL, FreeMB INTEGER NOT NULL) DECLARE @DiskFreeSpace INTDECLARE @DriveLetter CHAR(1)DECLARE @AlertMessage VARCHAR(500)DECLARE @MailSubject VARCHAR(100) INSERT INTO ##disk_free_spaceEXEC master..xp_fixeddrives SELECT @DiskFreeSpace = FreeMB FROM ##disk_free_space where DriveLetter ='C' SET @MailSubject =''+@@SERVERNAME+' Disk Analysis'EXEC msdb..sp_send_dbmail @profile_name=@ProfileName,@recipients = @Recipients,@subject = @MailSubject,@query=N'SELECT DriveLetter,CAST(CAST(CAST(FreeMB ASDECIMAL(18,2))/CAST(1024 AS DECIMAL(18,2)) AS DECIMAL(18,2)) AS VARCHAR(20)) +'' GB '' SpaceAvailable FROM ##disk_free_space' /* Get db sizes */DECLARE @dbName sysnameCREATE TABLE ##TempDBNames(DatabaseName sysname,DATABASE_SIZE int,REMARKS varchar(254))INSERT INTO ##TempDBNamesEXEC sp_databases CREATE TABLE ##DBInfo(dbName sysname,name sysname,filepath sysname,dbType VARCHAR(10),dbSize INT,DataModified DATETIME) DECLARE dbs CURSOR FORSELECT DatabaseName FROM ##TempDBNames open dbsfetch next from dbs into @dbNameWHILE (@@FETCH_STATUS= 0)BeginEXEC ('USE '+ @dbName +' INSERT INTO ##DBInfo SELECT '''+ @dbName +''',name,physical_name,type_desc,size,(SELECTMAX(modify_date) FROM sys.tables) LastModified FROM sys.database_files')fetch next from dbs into @dbNameEndclose dbsdeallocate dbs SET @MailSubject =''+@@SERVERNAME+' Database Analysis'EXEC msdb..sp_send_dbmail @profile_name=@ProfileName,@recipients = @Recipients,@subject = @MailSubject,@query=N'SELECT dbName,dbType,(dbSize*8)/1024dbSize,DataModified FROM ##DBInfo' DROP TABLE ##DBInfoDROP TABLE ##TempDBNames/* End get dbsizes */ IF @DiskFreeSpace < @DriveCBenchmarkBeginSET @MailSubject ='Drive C free space is low on '+@@SERVERNAMESET @AlertMessage ='Drive C on '+@@SERVERNAME+' has only '+ CAST(@DiskFreeSpace AS VARCHAR)+' MB left. Please freeup space on this drive. C drive usually has OS installed on it. Lower space onC could slow down performance of the server' EXEC msdb..sp_send_dbmail @profile_name=@ProfileName,@recipients = @Recipients,@subject = @MailSubject,@body = @AlertMessageEnd DECLARE DriveSpace CURSOR FAST_FORWARD FORselect DriveLetter, FreeMB from ##disk_free_space where DriveLetter not in('C') open DriveSpacefetch next from DriveSpace into @DriveLetter,@DiskFreeSpace WHILE (@@FETCH_STATUS= 0)Beginif @DiskFreeSpace <@OtherDataDriveBenchmarkBeginset @MailSubject ='Drive '+ @DriveLetter +' free space is low on '+@@SERVERNAMEset @AlertMessage = @DriveLetter +' has only '+cast(@DiskFreeSpace as varchar)+' MB left. Please increase free space for thisdrive immediately to avoid production issues' EXEC msdb..sp_send_dbmail @profile_name=@ProfileName,@recipients = @Recipients,@subject = @MailSubject,@body = @AlertMessageEndfetch next from DriveSpace into @DriveLetter,@DiskFreeSpaceEndclose DriveSpacedeallocate DriveSpaceDROP TABLE ##disk_free_spaceEND
Please note!
We had to adapt our code above as someone copied the whole procedure in and sent us an email in Portuguese with their disk space details, so make sure you fill in the email address properly.