Bây giờ tạo một công việc SQL Agent. Hãy chắc chắn rằng bạn thay đổi set @threshold = 20 --->>>>>>>>>>>>>>>>> CHANGE HERE <<<<<<<<<<<<<<<<<<<<<---
trong kịch bản dưới đây. Tôi đã đặt anh ta như một ngưỡng rất thấp, chỉ để mô phỏng cảnh báo. Lịch trình công việc để chạy cứ sau 30 phút (thay đổi điều này theo nhu cầu của bạn).
if object_id('tempdb..#dbserversize') is not null
DROP TABLE #dbserversize;
create table dbo.#dbserversize (
[id] int identity (1,1)
,[databaseName] sysname
,[Drive] varchar(3)
,[Logical Name] sysname
,[Physical Name] varchar(max)
,[File Size MB] decimal(38, 2)
,[Space Used MB] decimal(38, 2)
,[Free Space] decimal(38, 2)
,[%Free Space] decimal(38, 2)
,[Max Size] varchar(max)
,[Growth Rate] varchar(max)
)
declare @id int
declare @threshold int
declare @dbname sysname
declare @sqltext nvarchar(max)
declare @freespacePct int
set @threshold = 20 --->>>>>>>>>>>>>>>>> CHANGE HERE <<<<<<<<<<<<<<<<<<<<<---
select @dbname = min(name) from sys.databases where database_id > 4 and [state] = 0
while @dbname is not NULL
begin
select @dbname = name from sys.databases where name = @dbname and database_id > 4 and [state] = 0
--- Modified from Erin's blog : Proactive SQL Server Health Checks, Part 1 : Disk Space
--- source http://sqlperformance.com/2014/12/io-subsystem/proactive-sql-server-health-checks-1
set @sqltext = ' use '+@dbname+';'+'
insert into dbo.#dbserversize
select '''+@dbname+''' as [databaseName]
,substring([physical_name], 1, 3) as [Drive]
,[name] as [Logical Name]
,[physical_name] as [Physical Name]
,cast(CAST([size] as decimal(38, 2)) / 128.0 as decimal(38, 2)) as [File Size MB]
,cast(CAST(FILEPROPERTY([name], ''SpaceUsed'') as decimal(38, 2)) / 128.0 as decimal(38, 2)) as [Space Used MB]
,cast((CAST([size] as decimal(38, 0)) / 128) - (CAST(FILEPROPERTY([name], ''SpaceUsed'') as decimal(38, 0)) / 128.) as decimal(38, 2)) as [Free Space]
,cast(((CAST([size] as decimal(38, 2)) / 128) - (CAST(FILEPROPERTY([name], ''SpaceUsed'') as decimal(38, 2)) / 128.0)) * 100.0 / (CAST([size] as decimal(38, 2)) / 128) as decimal(38, 2)) as [%Free Space]
,case
when cast([max_size] as varchar(max)) = - 1
then ''UNLIMITED''
else cast([max_size] as varchar(max))
end as [Max Size]
,case
when is_percent_growth = 1
then cast([growth] as varchar(20)) + ''%''
else cast([growth] as varchar(20)) + ''MB''
end as [Growth Rate]
from sys.database_files
where type = 0 -- for Rows , 1 = LOG'
--print @sqltext
exec (@sqltext)
select @dbname = min(name) from sys.databases where name > @dbname and database_id > 4 and [state] = 0
end
--- delete the entries that do not meet the threshold
delete from dbo.#dbserversize
where [%Free Space] < @threshold;
--select * from dbo.#dbserversize
--- NOW Raise errors for the databases that we got flagged up
while exists (select null from dbo.#dbserversize)
begin
select top 1 @id = id,
@dbname = databaseName,
@freespacePct = [%Free Space]
from dbo.#dbserversize;
RAISERROR(911421, 10,1,@freespacePct, @dbname) with LOG;
delete from dbo.#dbserversize where id = @id;
end
Bây giờ tạo một cảnh báo để đáp ứng với 911421
số lỗi.
USE [msdb]
GO
EXEC msdb.dbo.sp_add_alert @name=N'MDF file alert',
@message_id=911421,
@severity=0,
@enabled=1,
@delay_between_responses=1800,
@include_event_description_in=0,
@job_id=N'019c4770-865b-406b-894e-72a1ff34f732'
GO
EXEC msdb.dbo.sp_add_notification @alert_name=N'MDF file alert', @operator_name=N'Notify 911 DBA for MDF files getting full', @notification_method = 1
GO