|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Saturday, April 06, 2013 9:18 AM
Points: 57,
Visits: 76
|
|
I would like to setup a low disk space alert when space more than 70 percent without using powershell scripts and i required to alert for one server not for multiple server.
Please suggest..
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 4:27 PM
Points: 2,007,
Visits: 6,040
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, April 26, 2013 6:50 AM
Points: 114,
Visits: 948
|
|
There are different ways to monitor disk space and generate trend
1. xp_fixeddrives: gived you free space for all the drive 2. Sysinternal command prompt utility will give more information includes total drive size and free soace. You have to download the utility (http://technet.microsoft.com/en-us/sysinternals/bb897550.aspx). Run the command: "psinfo \\ServerName -d disk" 3. Use performance counters "LogicalDisk\%Free Space" and "LogicalDisk\Free Megabytes". Using these two values you can calculate total drive size
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:40 PM
Points: 2,610,
Visits: 3,116
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, April 26, 2013 6:50 AM
Points: 114,
Visits: 948
|
|
As far as I know, SQL server just gives free space information. To know % free space we need to find something ouside of sql like perfmon, psinfo, wmi etc...
please correct me if I am wrong
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:40 PM
Points: 2,610,
Visits: 3,116
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 11:01 AM
Points: 485,
Visits: 1,566
|
|
in order to get drive sizes vs free space and run a comparison in SQL, you have to use sp_OAMethod
so the xp_Fixeddrives is only half the solution. you have to use xp_FixedDrives to populate a temp table and then via a cursor, use sp_OAMethod to gather the rest of the info.
Its ugly, but it would like this;
DECLARE @hr int DECLARE @fso int DECLARE @drive char(1) DECLARE @odrive int DECLARE @TotalSize varchar(20) DECLARE @MB bigint ; SET @MB = 1048576
CREATE TABLE #drives (drive char(1) PRIMARY KEY, FreeSpace int NULL, TotalSize int NULL) INSERT #drives(drive,FreeSpace) EXEC master.dbo.xp_fixeddrives EXEC @hr=sp_OACreate 'Scripting.FileSystemObject',@fso OUT IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso DECLARE dcur CURSOR LOCAL FAST_FORWARD FOR SELECT drive from #drives ORDER by drive OPEN dcur FETCH NEXT FROM dcur INTO @drive WHILE @@FETCH_STATUS=0 BEGIN EXEC @hr = sp_OAMethod @fso,'GetDrive', @odrive OUT, @drive IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso EXEC @hr = sp_OAGetProperty @odrive,'TotalSize', @TotalSize OUT IF @hr <> 0 EXEC sp_OAGetErrorInfo @odrive
UPDATE #drives SET TotalSize=@TotalSize/@MB WHERE drive=@drive FETCH NEXT FROM dcur INTO @drive END
CLOSE dcur DEALLOCATE dcur
EXEC @hr=sp_OADestroy @fso IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso
SELECT @@servername as ServerName, drive, FreeSpace as 'Free(MB)', TotalSize as 'Total(MB)', CAST((FreeSpace/(TotalSize*1.0))*100.0 as int) as 'Free(%)', GETDATE() as Date_Entered FROM #drives
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 11:01 AM
Points: 485,
Visits: 1,566
|
|
and the whole thing to send an email from the server that has low disk space.....
DECLARE @hr int DECLARE @fso int DECLARE @drive char(1) DECLARE @odrive int DECLARE @TotalSize varchar(20) DECLARE @MB bigint ;
SET @MB = 1048576
CREATE TABLE #drives (drive char(1) PRIMARY KEY, FreeSpace int NULL, TotalSize int NULL) INSERT #drives(drive,FreeSpace) EXEC master.dbo.xp_fixeddrives EXEC @hr=sp_OACreate 'Scripting.FileSystemObject',@fso OUT IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso DECLARE dcur CURSOR LOCAL FAST_FORWARD FOR SELECT drive from #drives ORDER by drive OPEN dcur FETCH NEXT FROM dcur INTO @drive WHILE @@FETCH_STATUS=0 BEGIN EXEC @hr = sp_OAMethod @fso,'GetDrive', @odrive OUT, @drive IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso EXEC @hr = sp_OAGetProperty @odrive,'TotalSize', @TotalSize OUT IF @hr <> 0 EXEC sp_OAGetErrorInfo @odrive
UPDATE #drives SET TotalSize=@TotalSize/@MB WHERE drive=@drive FETCH NEXT FROM dcur INTO @drive END
CLOSE dcur DEALLOCATE dcur
EXEC @hr=sp_OADestroy @fso IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso
SELECT @@servername as ServerName, drive, FreeSpace as 'Free(MB)', TotalSize as 'Total(MB)', CAST((FreeSpace/(TotalSize*1.0))*100.0 as int) as 'Free(%)', GETDATE() as Date_Entered into #result_set FROM #drives
declare @servername nvarchar(100), @drive1 nvarchar(2), @freeMB int, @totalMB int, @free int, @date_entered nvarchar(50) declare db_crsr_T cursor for SELECT [ServerName], [drive], [Free(MB)], [Total(MB)], [Free(%)], [Date_Entered] from #result_set
open db_crsr_T fetch next from db_crsr_T into @servername, @drive1, @FreeMB, @totalMB, @free, @date_entered while @@fetch_status = 0 begin if @free < 30 and @free > 10 begin declare @msg1 nvarchar(500) SET @msg1 = 'Instance ' + RTRIM(@servername) + ' only has ' + CONVERT(NVARCHAR(9),@freeMB) + ' MB free on disk ' + CONVERT(CHAR(1),@drive1) + ':\. The percentage free is ' + CONVERT(NVARCHAR(3),@free) + '. Drive ' + CONVERT(CHAR(1),@drive1) +':\ has a total size of ' + LTRIM(CONVERT(NVARCHAR(10),@totalMB)) + ' MB and ' + CONVERT(NVARCHAR(9),@freeMB) + ' MB free.' EXEC msdb.dbo.sp_send_dbmail @profile_name = 'DB_Mail', -- CHANGE THIS TO YOUR SERVERS MAIL PROFILE NAME... @recipients = 'DBA@gmail.com', --CHANGE THIS TO YOUR EMAIL ADDRESS... @body = @msg1, @subject = 'Disk space alert' ; end if @free < 10 begin declare @msg2 nvarchar(500) SET @msg2 = 'WARNING!! Instance ' + RTRIM(@servername) + ' only has ' + CONVERT(NVARCHAR(9),@freeMB) + ' MB free on disk ' + CONVERT(CHAR(1),@drive1) + ':\. The percentage free is ' + CONVERT(NVARCHAR(3),@free) + '. Drive ' + CONVERT(CHAR(1),@drive1) +':\ has a total size of ' + LTRIM(CONVERT(NVARCHAR(10),@totalMB)) + ' MB and ' + CONVERT(NVARCHAR(9),@freeMB) + ' MB free.' EXEC msdb.dbo.sp_send_dbmail @profile_name = 'DB_Mail', -- CHANGE THIS TO YOUR SERVERS MAIL PROFILE NAME... @recipients = 'DBA@gmail.com', --CHANGE THIS TO YOUR EMAIL ADDRESS... @body = @msg2, @subject = 'Disk Space Warning!' ; end
fetch next from db_crsr_T into @servername, @drive1, @FreeMB, @totalMB, @free, @date_entered end
close db_crsr_T deallocate db_crsr_T
DROP TABLE #drives DROP TABLE #result_set
make sure to change the email address and profile name. schedule it to run once an hour via SQL agent and done.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, April 26, 2013 6:50 AM
Points: 114,
Visits: 948
|
|
Maybe you should read up on SQL Server Agent Alerts so you can see what they can do?
Here's a sample: http://msdn.microsoft.com/en-us/library/ms186385.aspx
Yes there are capabilities to setup various kind of alerts in SQL Agent.
What I am saying is, a custom code is required to get %free information as Geoff has mentioned
and yes I was not aware of sp_OAMethod
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:40 PM
Points: 2,610,
Visits: 3,116
|
|
Daxesh Patel (6/5/2012)
Maybe you should read up on SQL Server Agent Alerts so you can see what they can do?
Here's a sample: http://msdn.microsoft.com/en-us/library/ms186385.aspx
Yes there are capabilities to setup various kind of alerts in SQL Agent. What I am saying is, a custom code is required to get %free information as Geoff has mentioned and yes I was not aware of sp_OAMethod I understand now.
Thanks,
Jared SQL Know-It-All
How to post data/code on a forum to get the best help - Jeff Moden
|
|
|
|