--Create the Database CREATE DATABASE DBADatabase GO -- Switch to the new database for the rest of our setup USE DBADatabase GO -- Create the table we’ll be using to hold the details on the drive space CREATE TABLE dbo.DriveSpace ( [ID] uniqueidentifier NOT NULL ROWGUIDCOL, [ServerName] varchar(50) NOT NULL, [Date] datetime NOT NULL, [Drive] char(1) NOT NULL, [SpaceMB] int NOT NULL ) ON [PRIMARY] GO -- add some indexes and such ALTER TABLE dbo.DriveSpace ADD CONSTRAINT DF_DriveSpace_ID DEFAULT (newid()) FOR [ID] GO ALTER TABLE dbo.DriveSpace ADD CONSTRAINT PK_DriveSpace PRIMARY KEY CLUSTERED ([ID]) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO CREATE NONCLUSTERED INDEX IX_DriveSpace ON dbo.DriveSpace ([Date]) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO ALTER TABLE dbo.DriveSpace SET (LOCK_ESCALATION = TABLE) GO
That’s all we need to store it.. bug how to fill it? Well there is a function that returns the information of the drives of which the server is located called xp_fixeddrives, try it for yourselves and see the two columns returned (Drive and MB free). Anywho we need to set up a stored procedure that will record the details so that you can later set up a job using the GUI or sp_add_job to execute the stored proc daily.
USE DBADatabase GO CREATE PROCEDURE dba_UpdateDriveInfo AS BEGIN -- Temporary Table for our results DECLARE @DriveData AS TABLE ([Drive] CHAR(1), [MB Free] INT) -- Get results INSERT INTO @DriveData EXEC xp_fixeddrives -- Dump them in our table INSERT INTO dbo.DriveSpace ([ServerName], [Date], [Drive], [SpaceMB]) SELECT DB_NAME(), GETDATE(), [DD].[Drive], [DD].[MB Free] FROM @DriveData AS [DD] END
Again all you need to do is set up a job to run this daily and if you wanted to maybe send a mail when you lower than a specific amount? Good Luck
No comments:
Post a Comment