June 26, 2009 at 5:19 pm
Comments posted to this topic are about the item Drive space checking monitoring procedure
July 7, 2009 at 10:32 am
I liked the code sample. I cleand it up by removing unused/unneeded variables, unneeded columns and tightend up on the error and loopin logic. Hope you approve of the updates.
set nocount on;
declare
@cmd nvarchar(130)
, @drive char(1)
, @freeMb int
, @hResult int
, @hWnd int
, @MB int
, @oDrive int
, @totalSize bigint
declare @drives table
( Drive char(1) not null primary key
, FreeMb int not null
, TotalMb int not null default (0)
);
-- initialize the variables
set @drive = '';
set @MB = 1048576; -- conversion ratio for bytes to megabytes
-- collect the attached Drives and FreeSpace
insert @drives(Drive, FreeMb) exec master.dbo.xp_fixeddrives ;
-- build the command to allow the creation/use of the FileSystemObject...
set @cmd = '
exec sp_configure ''show advanced options'', 1;
reconfigure;
exec sp_configure ''Ole Automation Procedures'', 1;
reconfigure;'
exec sp_executeSql @cmd;
-- create the FileSystemObject
exec @hResult = sp_OACreate 'Scripting.FileSystemObject', @hWnd out;
-- proceed if sucessful creating the OLE Object...
if @hResult = 0
begin
-- loop thru the drives and retrieve the total size...
while exists ( select top 1 * from @drives
where Drive > @drive )
begin
-- retrieve the next available drive...
select top 1
@drive = Drive
, @freeMb = FreeMb
from @drives
where Drive > @drive
order by Drive;
-- get the Drive object and then read the TotalSize (in bytes) property
exec @hResult = sp_OAMethod @hWnd, 'GetDrive', @oDrive out, @drive;
exec @hResult = sp_OAGetProperty @oDrive, 'TotalSize', @totalSize out;
-- update the drive...
update @drives
set TotalMb = @totalSize / @MB
where Drive = @drive;
end;
-- destroy the FileSystemObject object...
exec sp_OADestroy @hWnd;
-- return the results...
select Drive
, MbTotal
, MbFree
, convert(decimal(4,1), (MbFree / convert(float, MbTotal,0)) * 100, 0)) as PercentFree
from @drives
order by Drive;
end;
-- disable OLE Automation...
set @cmd = '
exec sp_configure ''Ole Automation Procedures'', 0;
reconfigure;
exec sp_configure ''show advanced options'', 0;
reconfigure;'
exec sp_executeSql @cmd;
return @@error;
--Paul Hunter
July 8, 2009 at 10:19 am
Great script, but unfortunately enabling ole automation procedures is a security risk for us so we can never run this type of disk drive script. Our drive space script is more crude and simply runs xp_fixeddrives. We then apply a hard threshold of free space, ie 50GB, regardless of drive size. It doesn't give us all of the drive information, but it's enough for us to respond to low drive space without us opening up ole automation procedures.
LC
May 24, 2016 at 7:01 am
Thanks for the script.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply