1. Creating an Alert for Notification
Once the operator has been created, we are ready to
create our Alert. An Alert monitors the database for events. When an
event occurs, a notification is sent to the Operators that are assigned
to the Alert.
Alerts are dependent upon the SQL Server Agent, which
must be running. If the SQL Server Agent is not running when an Alert
is created, a message will be presented stating that it is not running
and that the Alert will not function.
We can create alerts using the sp_add_alert system stored procedure, example syntax for which is as follows:
sp_add_alert [Alert Name],[Message ID],
[Severity],[Enabled],
[Delay Between Responses],[Notification Message],
[Include Event Description In],[Database Name],
[Event Description Keyword],[Job ID],
[Job Name],[Raise SNMP Trap],
[Performance Condition],[Category Name],
[WMI Namespace], [WMI Query]
This system stored procedure's arguments are as follows:
Alert Name – the textual reference to the Alert.
Message ID–
the value that identifies the message that is sent. In our case, our
messages from the Server Audit are being captured in the Windows
Application Log; therefore we can use the Error ID that is found in the sysmessages system table.
Severity– the value that indicates the severity of the message sent. If the Message ID is used, this value must be 0.
Enabled – indicates whether the Alert is active.
Delay Between Responses
– indicates the wait time for a notification to be sent after a
previous notification. The value of 0 indicates that there is no delay.
Notification Message– additional text that is sent with the event message. This is optional.
Include Event Description In–
identifies where the SQL Server event message should be provided. A
value of 0 indicates that the SQL Server event message is not to be
sent. A value of 1 indicates that it should be included in an e-mail.
The other options that are available for this argument are noted to be
removed in later versions of SQL Server and should be avoided.
Database Name – the database where the event message will occur.
Event Description Keyword
– the pattern of characters that will occur in an event that will
trigger a notification. This is necessary only when filtering events.
Job ID–
the id reference to the job that will be launched in response to the
event. This is necessary only when launching a job in response to an
event.
Job Name–
the textual reference to the job that will be launched in response to
the event. This is necessary only when launching a job in response to an
event.
Raise SNMP Trap
– indicates whether a Simple Network Management Protocol (SNMP) trap is
raised in response to the event. This is optional and the default value
is 0.
Performance Condition
– defines the performance conditions that will trigger a notification.
This is necessary only when using performance events to raise the Alert.
Category Name – the category of the Alert. This is optional.
WMI Namespace
– the Windows Management Instrumentation (WMI) namespace that is
referenced by the WMI query. This is necessary only when using WMI
events to raise the Alert.
WMI Query
– the query that identifies a WMI event that will trigger the alert.
This is necessary only when using WMI events to raise the Alert.
Listing 1 creates a "Honeycomb Alert" in the msdb database.
We are capturing our Server Audit events in
the Windows Application Log; therefore we can use the message id of
33205 to identify that a Server Audit event has occurred.
2. Creating a Notification
Having created the Alert, Operators will need to be
assigned to the Alert to receive notification messages. This can be
accomplished by executing the sp_add_notification system stored procedure in Management Studio. The syntax of this system stored procedure is relatively straightforward:
sp_add_notification [Alert Name],[Operator Name],
[Notification Method]
This system stored procedure's arguments are:
Alert Name – the textual reference to the Alert that will send a message.
Operator Name – the textual reference to the Operator that will receive a message.
Notification Method – identifies the method by which the message will be sent to the Operator. The values are: 1 (e-mail), 2 (pager), 4 (net send).
Listing 2 adds the Operator named DBA1 to the HoneycombAlert Alert, and specifies that notification should be sent via e-mail.
For this alert to be successful, the SQL Server Agent
must be configured to send mail. This can be accomplished using the
Database Mail Setup Wizard.
Avoid SQL Mail
Please note that SQL Mail is a feature of
SQL Server that is scheduled for retirement; therefore it is recommended
to use Database Mail instead.
The Database Mail Setup Wizard is accessible in
Management Studio within the Management folder in Object Explorer;
simply right-click on the "Database Mail" option and select "Configure
Database Mail".
In order to make sure that everything is
working as expected, it's worth sending a test e-mail; simply
right-click the Database Mail option and select the "Send Test E-Mail
..." option.