Solved

Monitoring data file facets for availble space in SQL 2005

Posted on 2012-03-16
3
241 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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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