Monitor Free Space

download Monitor Free Space

of 3

Transcript of Monitor Free Space

  • 8/7/2019 Monitor Free Space

    1/3

    Monitoring Free Space on ServerDatabase administrators need to take up the stewardship of the server; any issues and be

    prepared to burn the midnight oil. A job goes down or a database fails to respond or the

    server fails to start, DBAs are always on the hot seat. Today we will be discussing a very

    important aspect of disk usage and space monitoring. Its one of the fundamental duties of

    a DBA to manage and monitor the disk usage.

    All the databases reside physically in .MDF .LDF files on the drives. There are jobs running,

    backups taken, maintenance plans and most importantly the database keeps growing. In

    this scenario monitoring the Server to ensure that there is enough space to support the

    growing demands is really important.

    There are a lot of different ways in which we can monitor the drives for free spaces. I would

    be discussing a very simple yet effective way that does the trick for us, xp_fixeddrives.

    SQL Server offers this undocumented, extended stored procedure for monitoring free spaces

    on the system that hosts the SQL Server.

    Executing the below statement gives the system details. All the available drives and the free

    spaces in MB on each of them are listed.

    EXECmaster.dbo.xp_fixeddrives

    drive MB free----- -----------C 38126D 79906

    (2 row(s) affected)

    It is not feasible for a DBA to run this statement every 30 mins and check if the system has

    enough free space. Trust me when I say this that DBAs are no free birds so let us try tomake their job a little less tiresome.

    CREATEPROCEDURE [dbo].[MonitorFreeSpace]

    AS

    SETNOCOUNTONSETQUOTED_IDENTIFIERONSETANSI_NULLSON

    BEGIN

    DECLARE @ThreshHold INTDECLARE @COUNT INTDECLARE @CatchDrive CHAR(1)DECLARE @SubStr VARCHAR(2000)DECLARE @MsgStr VARCHAR(2000)

    /*We have set the thresh hold limit for our drives as 2 GB*/SET @ThreshHold=2048

    /*Table variable to host drives & free spaces details*/

  • 8/7/2019 Monitor Free Space

    2/3

    DECLARE @SpaceInfo TABLE(ID INTIDENTITY(1,1),Drive CHAR(1),MBFreeSpace INT)

    INSERTINTO @SpaceInfo EXECxp_fixeddrives

    SELECT @COUNT=MIN(ID)FROM @SpaceInfo/*Looping through all the drives */WHILE (@COUNT ISNOTNULLAND @COUNT

  • 8/7/2019 Monitor Free Space

    3/3

    Administering the disk usage is really important as this will help the DBA to get things in

    place in time else one would live in fear of running out of spaces and also putting all the

    database supported applications in a critical state.