Monitoring space in production server is one of the top priority tasks for a DBA. But when the number of servers to monitor increases, it becomes difficult to do that. These scripts will do the job for you. It will collect free space information from all the servers and send it to your mail. Here are the necessary steps to do it.
- Create linked server to all the production servers and store the information in a table (DBA_ALL_SERVERS)
- Create a table for drive space in servers (DBA_ALL_SERVER_SPACE)
- Create the procedure which loop through the server lists and checks drive space in each of them and store it to the table DBA_ALL_SERVER_SPACE
- Put the procedure in a job and schedule it accordingly
Before taking advantage of this script, you need to configure DB Mail, and create linked servers for the servers. Use BOL in case of any doubts regarding this
Once you are ready
1) Create the Servers table
CREATE TABLE [dbo].[DBA_All_servers]( |
Now go ahead and populate your linked servers into the table. All the columns other than server name don't have significance at present. However I have created it for any future use.
2) Create the Space table
CREATE TABLE [dbo].[DBA_All_Server_Space]( |
3) Create the procedure
CREATE PROCEDURE [DBO].[USP_DBA_GETSERVERSPACE] AS BEGIN CREATE TABLE #TEMPSPACE DECLARE @SERVER_NAME VARCHAR(200) --** PUT LOCAL SERVER FIRST. INSERT INTO #TEMPSPACE INSERT INTO DBA_ALL_SERVER_SPACE INSERT INTO DBA_ALL_SERVER_SPACE
PRINT @@SERVERNAME +' COMPLETED.' DECLARE ALLSERVER CURSOR WHILE @@FETCH_STATUS=0
EXEC ('INSERT INTO #TEMPSPACE INSERT INTO DBA_ALL_SERVER_SPACE /* Optionally insert a blank line. This was added INSERT INTO DBA_ALL_SERVER_SPACE PRINT 'SERVER ' +@SERVER_NAME+' COMPLETED.' FETCH NEXT FROM ALLSERVER INTO @SERVER_NAME DECLARE @SERVERNAME VARCHAR(200) DECLARE SPACECUR CURSOR FOR OPEN SPACECUR FETCH NEXT FROM SPACECUR DECLARE @BODY1 VARCHAR(2000)
WHILE @@FETCH_STATUS=0
EXEC MSDB.DBO.SP_SEND_DBMAIL @RECIPIENTS='<YourEmailID>', -------------------------------------------------------
END |
That's it. Now schedule the Procedure in a Job to send you report whenever you need them, twice a day, thrice a day or whatever.
---------------------------------------------------------------------------------------------------------
Note: insert into Exec may not work if distributed transaction is not configured. Now there is a workaround for this. You need to create a stored procedure in the remote server to store the result into a local table and then copy it to our central table
Remember to run the following in all servers
Eg
USE [MASTER]
CREATE PROC [DBO].[USP_TEMPSPACE_POP] |
In the procedure change the While loop as
WHILE @@FETCH_STATUS=0
EXEC ('EXEC ' + @SERVER_NAME+'.MASTER.DBO.USP_TEMPSPACE_POP') INSERT INTO DBA_ALL_SERVER_SPACE /* Optionally insert a blank line. This was added INSERT INTO DBA_ALL_SERVER_SPACE PRINT 'SERVER ' +@SERVER_NAME+' COMPLETED.' FETCH NEXT FROM ALLSERVER INTO @SERVER_NAME |