Solved

Monitoring data file facets for availble space in SQL 2005

Posted on 2012-03-16
3
240 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
ID: 37729507
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
ID: 37729902
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
ID: 37771396
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change variables in SQL table 6 87
Upgrading SQL 2005 Express to 2008 R2 Express 31 93
TSQL mapping detailed records to group records 9 51
Sql Server group by 10 27
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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