Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Drive space checking monitoring procedure Expand / Collapse
Author
Message
Posted Friday, June 26, 2009 5:19 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, October 17, 2014 2:29 PM
Points: 20, Visits: 487
Comments posted to this topic are about the item Drive space checking monitoring procedure
Post #743018
Posted Tuesday, July 7, 2009 10:32 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 9:55 AM
Points: 201, Visits: 404
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
Post #748713
Posted Wednesday, July 8, 2009 10:19 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: 2 days ago @ 2:22 PM
Points: 23, Visits: 150
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
Post #749463
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse