Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Monitoring data file facets for availble space in SQL 2005

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
monarchit
Asked:
monarchit
1 Solution
 
jogosCommented:
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
 
lcohanDatabase AnalystCommented:
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
 
monarchitAuthor Commented:
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now