Web design and hosting, database, cloud and social media solutions that deliver business results
  • Business Solutions
    • Robotic Process Automation
    • Software
    • Database Consultancy Services
      • Data Integration
      • Datawarehouse Services
      • Power BI
      • Server Upgrade and DBA Services
    • Web Site Design Services
      • Logo Design
      • Payment Gateways
      • Web Localisation and Translation
      • Web Site Optimisation
      • Web Site Security
      • Technical Tools
    • Cloud Services
      • Amazon Web Services
      • Google Cloud Services
      • Microsoft Azure
    • Microsoft Office
    • Social Media Management and Advice 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 Us
    • 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
    • Careers
      • Translator English-Portuguese
      • Translator English-Spanish
    • Portfolio
    • Team
      • Adrian Anandan
      • Ali Al Amine
      • Ayse Hur
      • Chester Copperpot
      • Deepika Bandaru
      • Gavin Clayton
      • Sai Gangu
      • Suneel Kumar
      • Surya Mukkamala
عربى (AR)čeština (CS)Deutsch (DE)English (EN-US)English (EN-GB)Español (ES)فارسی (FA)Français (FR)हिंदी (HI)italiano (IT)日本語 (JA)polski (PL)Português (PT)русский (RU)Türk (TR)中国的 (ZH)

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]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.

Author

Rating

Please note, this commenting system is still in final testing.
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

Site Links

RSSLoginLink Cookie PolicySitemap

Social Media

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

Get in Touch

+15125961417info@claytabase.comClaytabase USA, 501 Congress Avenue, Suite 150, Austin, Texas, 78701, United States

Partnered With

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
Logout
Ousia CMS Loader