代码改变世界

SQL Server Alert发送告警邮件少了的原因

2017-06-21 17:53  潇湘隐者  阅读(1158)  评论(4编辑  收藏  举报

最近突然发现我们部署在数据库上面的告警(Alert),当错误日志里面出现错误时,并不是每个错误日志都会发送邮件出来。如下所示,设置了告警SQL Server Severity Event 14

 

USE [msdb]

GO

 

 

IF NOT EXISTS(SELECT 1 FROM msdb.dbo.syscategories WHERE NAME='DBA_MONITORING' AND category_class=2)

BEGIN

 

EXEC msdb.dbo.sp_add_category

    @class=N'ALERT',

    @type=N'NONE',

    @name=N'DBA_MONITORING' ;

 

END

GO

 

IF EXISTS(SELECT name FROM msdb.dbo.sysalerts WHERE name= N'SQL Server Severity Event 14')

 

    EXEC msdb.dbo.sp_delete_alert @name=N'SQL Server Severity Event 14'

GO

 

 

EXEC msdb.dbo.sp_add_alert @name=N'SQL Server Severity Event 14',

        @message_id=0,

        @severity=14,

        @enabled=1,

        @delay_between_responses=60,

        @include_event_description_in=1,

        @category_name=N'DBA_MONITORING',

        @job_id=N'00000000-0000-0000-0000-000000000000'

GO

 

 

EXEC msdb.dbo.sp_add_notification @alert_name=N'SQL Server Severity Event 14', @operator_name=N'YourSQLDba_Operator', @notification_method = 1

GO

 

 

然后我尝试用sa登录(sa已经被禁用)了三次,但是我只收到了一封邮件。特意查看了一下sp_add_alert的官方文档,才知道出现这个原因,是因为参数@delay_between_responses的值设置缘故,通过设置该值,可以防止在在短时间内重复发送一些不需要的电子邮件。如上所示,一分钟内,即使错误日志里面出现了大量类似的错误,也只会发送一封告警邮件。其实只是为了减少发送告警的频率,如果你想当错误日志里出现这个级别的告警时,都必须发送告警邮件,可以将其值设置为0。但是有时候,如果设置为0,你会收到铺天盖地的邮件。其实这个小问题,只是因为以前没有特意留意这个参数而已。存粹属于没有彻底了解这些功能罢了。

 

 

 

clip_image001

 

 

 

@delay_between_responses = ] delay_between_responses

The wait period, in seconds, between responses to the alert. delay_between_responsesis int, with a default of 0, which means there is no waiting between responses (each occurrence of the alert generates a response). The response can be in either or both of these forms:

·          

·         One or more notifications sent through e-mail or pager.

·          

·         A job to execute.

·         By setting this value, it is possible to prevent, for example, unwanted e-mail messages from being sent when an alert repeatedly occurs in a short period of time.

 

@delay_between_responses =] delay_between_responses

警报响应之间的等待时间 (以秒为单位)。delay_between_responsesis int, 默认值为 0, 这意味着在响应之间没有等待 (每次出现警报都会生成响应)。响应可以是在以下两种形式中的一个, 或者都是:

通过电子邮件或寻呼机发送的一个或多个通知。

要执行的作业。

通过设置此值, 可以防止例如, 在短时间内重复发生警报时发送不需要的电子邮件。

 

 

参考资料:

 

https://docs.microsoft.com/zh-cn/sql/relational-databases/system-stored-procedures/sp-add-alert-transact-sql