Quantcast
Channel: Thoughts, codes, occasional rants, many others. » SQL Server
Viewing all articles
Browse latest Browse all 11

Drive Space Alert

$
0
0

I am not sure whether there is already a mechanism in SQL Server (without any third party product) to send out alerts when your drive space is running low (below a certain threshold). Not much familiar with writing WMI, so I decided to put together a script that does this automatically – the idea is to send out an alert every hour in my email so that I know the alerting mechanism is working (have seen this break before), and to send out an alert if the drive space is running below certain threshold. In my experience, drive spaces run low when:
1) When too many autogrows happen.
2) When you have too many backups that you have not archived.
3) When the Windows Paging file grows (unless you have set up max size for this).

Please feel free to copy and modify the script to your needs and any comment/feedback will be most welcome. Please be sure to create a group email that sends alerts to every DBA or at least those who are on call.

Script as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 
USE [DBATasks]
GO
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[dba_DiskFreeSpaceAlert]
 
	@DriveCBenchMark int = 13000,
	@DriveKBenchMark int = 38044,
	@DriveLBenchMark int = 35780,
	@DriveWBenchMark int = 85811
 
 
AS
/**********************************************************************************
Author: Subhash Pant
 
**********************************************************************************/
SET NOCOUNT ON;
 
DECLARE @DiskFreeSpaceC INT
DECLARE @DiskFreeSpaceK  int
DECLARE @DiskFreeSpaceL INT
DECLARE @DiskFreeSpaceW INT
DECLARE @DriveLetter CHAR(1)
DECLARE @AlertMessage VARCHAR(500)
DECLARE @MailSubject VARCHAR(100)
 
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)
 
/* Populate #disk_free_space with data */
INSERT INTO #disk_free_space
	EXEC master..xp_fixeddrives
--SELECT * FROM #disk_free_space
 
SET @DiskFreeSpaceC = (SELECT FreeMB FROM #disk_free_space where DriveLetter = 'C')
SET @DiskFreeSpaceK = (SELECT FreeMB FROM #disk_free_space where DriveLetter = 'K')
SET @DiskFreeSpaceL = (SELECT FreeMB FROM #disk_free_space where DriveLetter = 'L')
SET @DiskFreeSpaceW = (SELECT FreeMB FROM #disk_free_space where DriveLetter = 'W')
 
--SELECT @DiskFreeSpaceC as DiskFreeSpaceC, @DiskFreeSpaceK as DiskFreeSpaceK, @DiskFreeSpaceL as DiskFreeSpaceL, @DiskFreeSpaceW as DiskFreeSpaceW
 
IF (@DiskFreeSpaceC < @DriveCBenchMark)
BEGIN
	SET @MailSubject = 'Drive C free space is low on ' + @@SERVERNAME
	SET @AlertMessage = 'Drive C on ' + @@SERVERNAME + ' has only ' +  CAST(@DiskFreeSpaceC/1024 AS VARCHAR) + ' GB left.'
 
	-- Send out email
	EXEC msdb.dbo.sp_send_dbmail 
		@profile_name='DBA',
	@recipients='dbagroup@companyname.com',
		@subject= @MailSubject,
		@body= @AlertMessage
END
 
--SELECT @DiskFreeSpaceK = FreeMB FROM #disk_free_space where DriveLetter = 'K'
 
IF (@DiskFreeSpaceK < @DriveKBenchMark)
BEGIN
	SET @MailSubject = 'Drive K free space is low on ' + @@SERVERNAME
	SET @AlertMessage = 'Drive K on ' + @@SERVERNAME + ' has only ' +  CAST(@DiskFreeSpaceK /1024 AS VARCHAR) + ' GB left.'
	-- Send out email
	EXEC msdb.dbo.sp_send_dbmail 
		@profile_name='DBA',
	@recipients='dbagroup@companyname.com',
		@subject= @MailSubject,
		@body= @AlertMessage
END
 
--SELECT @DiskFreeSpaceL = FreeMB FROM #disk_free_space where DriveLetter = 'L'
 
IF (@DiskFreeSpaceL < @DriveLBenchMark)
BEGIN
	SET @MailSubject = 'Drive L free space is low on ' + @@SERVERNAME
	SET @AlertMessage = 'Drive L on ' + @@SERVERNAME + ' has only ' +  CAST(@DiskFreeSpaceL /1024 AS VARCHAR) + ' GB left.'
	-- Send out email
	EXEC msdb.dbo.sp_send_dbmail 
		@profile_name='DBA',
	@recipients='dbagroup@companyname.com',
		@subject= @MailSubject,
		@body= @AlertMessage
END
 
--SELECT @DiskFreeSpaceW = FreeMB FROM #disk_free_space where DriveLetter = 'W'
 
IF (@DiskFreeSpaceW < @DriveWBenchMark)
BEGIN
	SET @MailSubject = 'Drive W free space is low on ' + @@SERVERNAME
	SET @AlertMessage = 'Drive W on ' + @@SERVERNAME + ' has only ' +  CAST(@DiskFreeSpaceW /1024 AS VARCHAR) + ' GB left.'
	-- Send out email
	EXEC msdb.dbo.sp_send_dbmail 
		@profile_name='DBA',
	@recipients='dbagroup@companyname.com',
		@subject= @MailSubject,
		@body= @AlertMessage
END
 
DROP TABLE #disk_free_space
 
 
EXEC msdb.dbo.sp_send_dbmail 
	@profile_name='DBA',
	@recipients='dbagroup@companyname.com',
	@subject= 'Drive space alert on [Database Name]',
	@body= 'I successfully ran. I alert you if the drive space is running low on C, K, L, W. If you did not receive other emails, you are probably fine. This email ensures that alerts are working.'

Viewing all articles
Browse latest Browse all 11

Trending Articles