Solved

Monitoring data file facets for availble space in SQL 2005

Posted on 2012-03-16
3
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 40

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

635 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