Web design and hosting, database, cloud and social media solutions that deliver real business results
  • Business Solutions
    • Robotic Process Automation
    • Bespoke Software
    • Database Services
      • Data Integration
      • Datawarehouse Services
      • Power BI
    • Web Services
      • Logo Design
      • Payment Gateways
      • Web Site Optimisation
      • Web Site Security
      • Technical Tools
    • Cloud Services
      • Amazon Web Services
      • Google Cloud Services
      • Microsoft Azure
    • Microsoft Office
    • Social Media Services
  • Academy
    • Our Test Environment
    • Learning Databases
      • The Basics
      • Get Open Query
      • SQL Server Data
      • SQL Server Maintenance
      • Using SQL Server Dates
      • Using SQL Server Functions
      • Using SQL Server Pivot-Unpivot
      • Technical Tools
    • Learning Web Design
      • Building Ousia Content Management System
      • Using ASP-NET
      • Using CSS
      • Using JavaScript
    • Learning Cloud and IT Services
      • Task Scheduler Error 2147943645
      • Requesting SSL and Generation of PFX file in OpenSSL Simple Steps
    • Using Social Media
      • Asking for a Google Review
      • Changing a Facebook account from personal to business
      • Choosing where to focus Social Media effort
      • Social Media Image Sizes
      • Using Meta Data to set Social Media Images
  • About
    • Blog
      • Building an entry level gaming machine
      • Google Core Update Jan 2020
      • Hot Chilli Internet Closure
      • How To Choose Content For Your Website Adverts Leaflets
      • Preventing Online Scam
      • Skimmers of the gig economy
      • The most annoying things about websites on the Internet
      • Top 5 websites for free Vector Graphics
    • Portfolio
    • Team
      • Adrian Anandan
      • Ali Al Amine
      • Ayse Hur
      • Chester Copperpot
      • Fernando Ancona Camara
      • Gavin Clayton
      • Sai Gangu
      • Suneel Kumar
      • Surya Mukkamala
      • Trevor Momanyi
AR - عربىCS - češtinaDE - DeutschES - EspañolFA - فارسیFR - FrançaisHI - हिंदीIT - italianoJA - 日本語PL - polskiPT - PortuguêsRU - русскийTR - TürkZH - 中国的

Disk space and database size email alerts

Create a stored procedure that will send out disk space and database alerts via email that you can attach to a job

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 Article
SQL 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]
GO
CREATE PROCEDURE [maint].[DiskFreeSpaceAlert]
@DriveCBenchmark int= 1024,
@OtherDataDriveBenchmark int= 10240,
@ProfileName sysname='DatabaseEmailProfileName',
@Recipients NVARCHAR(1000)='youremail@yourdomain'
AS BEGIN
 
IF EXISTS(SELECT * FROM tempdb..sysobjects
WHERE id =object_id(N'[tempdb]..[##disk_free_space]'))
DROP TABLE ##disk_free_space
CREATE TABLE ##disk_free_space(
     DriveLetter CHAR(1) NOT NULL,
     FreeMB INTEGER NOT NULL)
 
DECLARE @DiskFreeSpace INT
DECLARE @DriveLetter CHAR(1)
DECLARE @AlertMessage VARCHAR(500)
DECLARE @MailSubject VARCHAR(100)
 
INSERT INTO ##disk_free_space
EXEC 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 sysname
CREATE TABLE ##TempDBNames(
DatabaseName sysname,
DATABASE_SIZE int,
REMARKS varchar(254))
INSERT INTO ##TempDBNames
EXEC sp_databases
 
CREATE TABLE ##DBInfo(
dbName sysname,
name sysname,
filepath sysname,
dbType VARCHAR(10),
dbSize INT,
DataModified DATETIME
)
 
DECLARE dbs CURSOR FOR
SELECT DatabaseName FROM ##TempDBNames
 
open dbs
fetch next from dbs into @dbName
WHILE (@@FETCH_STATUS= 0)
Begin
EXEC ('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 @dbName
End
close dbs
deallocate 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 ##DBInfo
DROP TABLE ##TempDBNames
/* End get dbsizes */
 
IF @DiskFreeSpace < @DriveCBenchmark
Begin
SET @MailSubject ='Drive C free space is low on '+@@SERVERNAME
SET @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 = @AlertMessage
End
 
DECLARE DriveSpace CURSOR FAST_FORWARD FOR
select DriveLetter, FreeMB from ##disk_free_space where DriveLetter not in('C')
 
open DriveSpace
fetch next from DriveSpace into @DriveLetter,@DiskFreeSpace
 
WHILE (@@FETCH_STATUS= 0)
Begin
if @DiskFreeSpace <@OtherDataDriveBenchmark
Begin
set @MailSubject ='Drive '+ @DriveLetter +' free space is low on '+@@SERVERNAME
set @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 = @AlertMessage
End
fetch next from DriveSpace into @DriveLetter,@DiskFreeSpace
End
close DriveSpace
deallocate DriveSpace
DROP TABLE ##disk_free_space
END

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.

Author

Gavin Clayton
Gavin Clayton
I formed Claytabase in 2010 as a way of carrying on my work with SQL Server and ASP.NET. This has culminated in the Ousia Content Management System being taken from concept to one of the quickest CMS's on the market.

Rating

Please note, this commenting system is still in final testing.
Copyright Claytabase Ltd 2021, registered in England and Wales 08985867

Site Links

RSS Login ContactCookie PolicySitemap

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+442392064871info@claytabase.co.ukGround Floor, Building 1000, Lakeside North Harbour, Western Road, Portsmouth, Hampshire, United Kingdom, PO6 3EZ

Partnered With

Partners
The settings on this site are set to allow all cookies. These can be changed on our Cookie Policy & Settings page.
By continuing to use this site you agree to the use of cookies.
Ousia Logo
Ousia CMS Loader