Solved

Monitoring data file facets for availble space in SQL 2005

Posted on 2012-03-16
3
233 Views
Last Modified: 2012-03-27
Hi,

It is possoble to monitor and alert the size of ndf file in databases in sql 2005? I can see you can do it in SQL 2008.

Thanks
0
Comment
Question by:monarchit
3 Comments
 
LVL 25

Expert Comment

by:jogos
Comment Utility
Monitor when the size changes or when it's getting nearly full (so before it has to extend)?

Can the dmv sys.dm_db_file_space_usage give you the info you want? And integrate that in  a monitoring-report you already have.
http://msdn.microsoft.com/en-us/library/ms174412(v=sql.90).aspx

Or alerts
http://www.mssqltips.com/sqlservertip/1523/how-to-setup-sql-server-alerts-and-email-operator-notifications/
And look at the counters available if there is something that matches what you want.
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
Comment Utility
Or you could use code like below in both SQL 2005/2008 (that takes into account the MAX file growth which SQL alert does not) and schedule a job to run the SP every how often you like and also you could change the treshold to whatever you want - below I used PercentFree < 25:



--data files
CREATE PROCEDURE dbo.dba_AlertDBDataSpace
AS
SET NOCOUNT ON;

create table #dbsize (DBFileName varchar(100), MAXSizeInMB decimal(12,4), UsedSpaceInMB  decimal(12,4), PercentFree decimal(12,4))
insert into #dbsize exec dbo.dba_CheckDBDataSpace
if exists (select * from #dbsize where PercentFree < 25)

begin
declare @report_file_name varchar(30),
            @sql varchar(max),
            @emailbody varchar(4000)
      
      set @sql = N'SET NOCOUNT ON
                        create table #dbsize (DBFileName varchar(100), MAXSizeInMB decimal(12,4), UsedSpaceInMB  decimal(12,4), PercentFree decimal(12,4))
                        insert into #dbsize exec dbo.dba_CheckDBDataSpace
                        select * from #dbsize where PercentFree < 25 order by 1,2;'

      SET @report_file_name = 'SQL_data_FileList.csv'
      SET @emailbody = 'WARNING !!! '+ CHAR(13) + CHAR(10)+'DB Files close to get FULL as at today(yyyy.mm.dd):  '+ convert(varchar(10),getdate(), 102)+ CHAR(13) + CHAR(10)

      EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'sqlmailProfile',
            @recipients='mail@mail.com',
            @subject = 'SQL ALERT - DB files almost FULL',
            @body = @emailbody,
            @body_format = 'TEXT',
            @query = @sql,
            @attach_query_result_as_file = 1,
            @execute_query_database = 'YourDBnameHere',
            @query_attachment_filename = @report_file_name,
            @query_result_header = 1,
            @query_result_separator = '      ',
            @query_result_no_padding = 1,
            @query_result_width = 1000;
end
GO

ALTER PROCEDURE [dbo].[dba_CheckDBDataSpace]
AS
SET NOCOUNT ON

declare @dbname sysname,
            @sqlstr varchar(4000)
declare  dblist cursor for select name from master..sysdatabases order by name
OPEN dblist
FETCH next FROM dblist INTO @dbname
WHILE @@fetch_status=0
BEGIN
      --set @sqlstr = 'use '+@dbname+'; SELECT name,size/128.0 as SizeInMB ,size/128.0 - CAST(FILEPROPERTY(name, ''SpaceUsed'') AS int)/128.0 AS AvailableSpaceInMB FROM sys.database_files;'
      set @sqlstr = 'use '+@dbname+'; SELECT name, max_size/128.0 as MAXSizeInMB, CAST(FILEPROPERTY(name, ''SpaceUsed'') AS int)/128.0 AS Used,
100 - ((CAST(FILEPROPERTY(name, ''SpaceUsed'')AS int)/128.0)/(max_size/128.0) * 100) as PercentFree
FROM sys.database_files where growth > 0 and file_guid is not null and data_space_id > 0;'
      exec (@sqlstr)
      FETCH next FROM dblist INTO @dbname
END
CLOSE dblist
DEALLOCATE dblist
GO


--tlog files
CREATE PROCEDURE dbo.dba_AlertDBLogSpace
AS
SET NOCOUNT ON;

create table #dbsize (DBFileName varchar(100), MAXSizeInMB decimal(12,4), UsedSpaceInMB  decimal(12,4), PercentFree decimal(12,4))
insert into #dbsize exec dbo.dba_CheckDBLogSpace
if exists (select * from #dbsize where PercentFree < 25)

begin
declare @report_file_name varchar(30),
            @sql varchar(max),
            @emailbody varchar(4000)
      
      set @sql = N'SET NOCOUNT ON
                        create table #dbsize (DBFileName varchar(100), MAXSizeInMB decimal(12,4), UsedSpaceInMB  decimal(12,4), PercentFree decimal(12,4))
                        insert into #dbsize exec dbo.dba_CheckDBDataSpace
                        select * from #dbsize where PercentFree < 25 order by 1,2;'

      SET @report_file_name = 'SQL_log_FileList.csv'
      SET @emailbody = 'WARNING !!! '+ CHAR(13) + CHAR(10)+'DB Files close to get FULL as at today(yyyy.mm.dd):  '+ convert(varchar(10),getdate(), 102)+ CHAR(13) + CHAR(10)
      EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'sqlmailProfile',
            @recipients='mail@mail.com',
            @subject = 'SQL ALERT - DB files almost FULL',
            @body = @emailbody,
            @body_format = 'TEXT',
            @query = @sql,
            @attach_query_result_as_file = 1,
            @execute_query_database = 'YourDBnameHere',
            @query_attachment_filename = @report_file_name,
            @query_result_header = 1,
            @query_result_separator = '      ',
            @query_result_no_padding = 1,
            @query_result_width = 1000;
end
GO


CREATE PROCEDURE [dbo].[dba_CheckDBLogSpace]
AS
SET NOCOUNT ON

declare @dbname sysname,
            @sqlstr varchar(4000)
declare  dblist cursor for select name from master..sysdatabases order by name
OPEN dblist
FETCH next FROM dblist INTO @dbname
WHILE @@fetch_status=0
BEGIN
      set @sqlstr = 'use '+@dbname+'; SELECT name, max_size/128.0 as MAXSizeInMB, CAST(FILEPROPERTY(name, ''SpaceUsed'') AS int)/128.0 AS Used,
100 - ((CAST(FILEPROPERTY(name, ''SpaceUsed'')AS int)/128.0)/(max_size/128.0) * 100) as PercentFree
FROM sys.database_files where growth > 0 and file_guid is not null and data_space_id = 0;'
      exec (@sqlstr)
      FETCH next FROM dblist INTO @dbname
END
CLOSE dblist
DEALLOCATE dblist
GO
0
 

Author Closing Comment

by:monarchit
Comment Utility
Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now