Pergunta

I am looking for a way to send alerts when a database or log reach 10% space remaining.

Let me preface this question by saying that I intentionally did not include the word "file" in the question. While I have researched this question it appears that most people have their databases set up for auto-growth and then struggle to manage their database(s) at the file system level. There are a ton of examples out there dealing with how to send disk space alerts. THIS IS NOT MY QUESTION! My databases are ALL set to fixed size files. That means files are thus ALL pre-allocated from the file system when they are created or when a database needs to be expanded. As a matter of policy I do not allow ANY database to grow, uncontrolled, to the point of bringing down a whole server at the hands of one badly behaved application. Each database is managed within its pre-alloted space and grown manually as necessary to meet growing demands.

That said I am looking for the best way to send an alert when the database "reaming space" drops below 10% for example - technically I'll probably set up a warning and alert threshold. So far I haven't been able to find anything on this subject since most people seem fixated on disk space which makes this a bit like looking for a needle in a haystack.

I kind of hoped that SQL Server would have simple alert mechanism to do such a simple, obvious, thing right out of the box, but it looks like alerts mostly are designed to catch error messages which is a little late in my book - I'm looking to be a little more proactive.

So, again, looking to send alerts when database "remaining space" drops below various thresholds. Has anyone done this or seen it done?

Thanks!

Foi útil?

Solução

Yes indeed. I have done this.

It is possible to set counters with queries against system tables. One possibility includes determining the percentage free space in a log or data file. Then, a SQL Alert can be created to E-mail a message to an operator that a particular threshold has been reached on a counter, such as there is only 5% space remaining in a database file. The solution requires several steps, but is possible using existing functionality.

To determine file names and space information, the following query may be used.

SELECT  name AS 'File Name' , 
 physical_name AS 'Physical Name', 
 size/128 AS 'Total Size in MB',
 size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS 'Available Space In MB',
 round((CAST(FILEPROPERTY(name, 'SpaceUsed') AS float)/size)* 100 ,2) AS  'Percentage Used',
 *
FROM sys.database_files;

Below are steps to set up an alert for percentage space free on a given file.

  1. Create procedure that sets counter with value. This example sets counter number 10.

    DECLARE @FreePercent int
    
    SELECT @FreePercent = 100 - round((CAST(FILEPROPERTY(name, 'SpaceUsed') AS float)/size)* 100 ,2)
     FROM sys.database_files
     WHERE sys.database_files.name = 'NameOfYourLogOrDataFileHere';
    
    EXEC sp_user_counter10 @FreePercent
    
  2. Create a scheduled job to run the aforementioned procedure

  3. Create SQL agent alert with counter, such that it executes when the free percentage drops below a certain threshold (i.e. 5%)

  4. Configure database mail, test it, and create at least one operator

  5. Enable SQL server agent alert E-mail (properties of agent), and restart the agent

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top