Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Drive space checking monitoring procedure


Drive space checking monitoring procedure

Author
Message
prashan
prashan
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 641
Comments posted to this topic are about the item Drive space checking monitoring procedure
SQLNightOwl
SQLNightOwl
SSC Veteran
SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)

Group: General Forum Members
Points: 280 Visits: 503
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
LSCIV
LSCIV
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 154
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
Iwas Bornready
Iwas Bornready
SSCrazy Eights
SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)

Group: General Forum Members
Points: 9750 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search