Technical Article

Drive space checking monitoring procedure

,

 

  1. First we need to confgure the ole automation procedures
  2. Then execute the script inturn it will create the sp PMON_DriveSpaceCheck(Monitoring Procedure).
  3. EXEC PMON_DriveSpaceCheck

--sp_configure 'show advanced options', 1;
--GO
--RECONFIGURE;
--GO
--sp_configure 'Ole Automation Procedures', 1;
--GO
--RECONFIGURE;
--GO

CREATE PROCEDURE [dbo].[PMON_DriveSpaceCheck] 
AS 
BEGIN
DECLARE @hr INT , 
@fso INT, 
@drive CHAR(1), 
@odrive INT, 
@TotalSize VARCHAR(20), 
@MB NUMERIC , 
@FreeSpace INT, 
@free INT, 
@RowId_1 INT, 
@LoopStatus_1 SMALLINT, 
@TotalSpace VARCHAR(10), 
@Percentage VARCHAR(3)

----------------------------------------------------------------------------------------------- 
--Table to Store Drive related information 
----------------------------------------------------------------------------------------------- 
CREATE TABLE #drives 
 ( 
 id INT IDENTITY(1,1) PRIMARY KEY, 
 drive CHAR(1), 
 FreeSpaceMB INT , 
 TotalSizeMB INT NULL, 
 percentage INT 
 ) 
 
 
----------------------------------------------------------------------------------------------- 
--Inserting the output of xp_fixeddrives to #SpaceSize Table 
----------------------------------------------------------------------------------------------- 
INSERT #drives(drive,FreeSpaceMB) EXEC master.dbo.xp_fixeddrives 

----------------------------------------------------------------------------------------------- 
--Using the sp_OACreate, sp_OAMethod and sp_OAGetProperty system stored procedures to create Ole Automation (ActiveX) applications that can do everything an ASP script can do*/ 
--Creates an instance of the OLE object 
----------------------------------------------------------------------------------------------- 
EXEC @hr=sp_OACreate 'Scripting.FileSystemObject',@fso OUT 
 
 SET @MB = 1048576 
 SET @RowId_1 = 1 
 SET @LoopStatus_1 = 1 
 
----------------------------------------------------------------------------------------------- 
--To Get Drive total space 
----------------------------------------------------------------------------------------------- 
 
WHILE (@LoopStatus_1 <> 0) BEGIN 
 
SELECT 
 @drive=drive, 
 @FreeSpace=FreeSpaceMB 
 FROM 
 #drives 
 WHERE 
 ( ID = @RowId_1 ) 
 
 IF ( @@ROWCOUNT = 0 ) 
 BEGIN 
 SET @LoopStatus_1 = 0 
 END 
 ELSE 
BEGIN 
 EXEC @hr = sp_OAMethod @fso,'GetDrive', @odrive OUT, @drive 
 EXEC @hr =sp_OAGetProperty @odrive,'TotalSize', @TotalSize OUT 
 UPDATE #drives SET TotalSizeMB=@TotalSize/@MB 
 WHERE 
 drive=@drive 
 UPDATE #drives SET Percentage=(@FreeSpace/(TotalSizeMB*1.0))*100.0 
 WHERE drive=@drive 
END 
 SET @RowId_1 = @RowId_1 + 1 
 
END 

SELECT * FROM #drives

DROP TABLE #drives

END

Rate

4 (6)

You rated this post out of 5. Change rating

Share

Share

Rate

4 (6)

You rated this post out of 5. Change rating