3 4
An alert is an action that occurs on a server in response to an event or to a performance condition. Alerts can notify operators, cause specified jobs to be executed, and forward events to another server. An event is an error or a message that is written to the Windows NT or Windows 2000 application event log (which you can view by using the Windows NT or Windows 2000 Event Viewer). A performance condition is a characteristic of the system's performance that can be monitored by using the Windows NT Performance Monitor or the Windows 2000 System Monitor, such as CPU utilization or the number of locks being used by SQL Server. In this chapter, we will focus on the System Monitor from Windows 2000, although the Windows NT Performance Monitor is very similar.
When an event occurs, SQLServerAgent compares the event to the list of alerts you have defined, and if an alert is defined for that event, the alert is triggered. An alert for a performance condition is fired when a defined performance threshold is reached for a specified SQL Server object in System Monitor, such as the counter User Connections under the System Monitor object General Statistics. For example, you could set an alert to occur if the value for this counter reaches 50. (System Monitor is described in Chapter 36.)
NOTE
The SQLServerAgent service must be running in order for your alerts to function.
Before we look at how to create an alert for an event, let's review the types of events that cause messages to be sent to the Windows NT or Windows 2000 application event log; only these events are available to be used to set alerts. Events (or errors) with a severity level of 19 through 25 are automatically reported to the Windows NT or Windows 2000 application event log and are therefore able to trigger alerts. By default, events that have a severity level less than 19 are not logged, and therefore, an alert cannot be triggered by those events. To enable those events to be logged so that an alert can be triggered, you must use sp_altermessage, the RAISERROR WITH LOG statement, or xp_logevent to change the logging status of the event or message. In this section, you'll learn how to create a user-defined event message and how to alter that message to ensure that it will be written to the application event log.
NOTE
When a SQL Server message is logged in the Windows NT or Windows 2000 application event log, it is also logged in the SQL Server log. To view the SQL Server log in Enterprise Manager, expand the Management folder for your server, and expand the SQL Server Logs folder.
All information for system and user-defined messages is stored in the sysmessages table in the master database. To create a user-defined message, use the T-SQL system stored procedure sp_addmessage. The syntax is shown here:
sp_addmessage [@msg_num =] msg_id, [@severity=] severity, [@msg_text=] 'msg_text' [,[@lang =] 'language'] [,[@with_log=] 'with_log'] [,[@replace =] 'replace']
A user-defined messag must have a message ID value (msg_id) of 50001 or greater. The severity parameter is the severity level of the error that the message refers to, ranging from 1 through 25, with higher numbers indicating higher severity of error. Only the system administrator can set severity levels 19 through 25. The msg_text parameter is the text of the error message that will appear in the application event log when the error occurs. The language parameter specifies in which language the message is written, because multiple languages can be installed with SQL Server. The with_log parameter can be set to either TRUE or FALSE, indicating whether the message will always be logged to the Windows NT or Windows 2000 application event log. The default value is FALSE. Using RAISERROR WITH LOG (described in the next section) will override this setting if it is FALSE. The replace parameter indicates that the message should replace an existing message that has the same message ID number.
Members of the public role have permission to execute sp_addmessage, but to create a message with a severity level greater than 18 or to set with_log to TRUE, you must be a member of the sysadmin role.
Let's look at an example of using sp_addmessage. The following statement creates a new message that will always be logged in the event log (because with_log is set to TRUE):
sp_addmessage 50001, 16, "Customer ID is out of range.", @with_log = "TRUE" GO
Suppose an existing message or a message you just created does not allow logging (or you did not include the with_log parameter), as in the following example:
sp_addmessage 50001, 16, "Customer ID is out of range.", @with_log = "FALSE" GO
If you later want to log the message, you must change the logging state of the message. To do so, use sp_altermessage to set logging to always occur, as in the following example:
sp_altermessage 50001, WITH_LOG, "TRUE" GO
Alternatively, you could use the RAISERROR statement with the WITH LOG option to return the message to your application, as well as to the application event log and the SQL Server log. For example, the following statement sends the message 50001 with a severity level of 16 and a state value of 1, where state is a number that can be used for tracking when the error is raised in more than one place in your code:
RAISERROR (50001, 16, 1) WITH LOG GO
MORE INFO
For more details about using RAISERROR, look up "RAISERROR" in the Books Online index and select "Using RAISERROR" in the Topics Found dialog box.
To change the logging status of a message, you can also use the extended stored procedure xp_logevent, which is located in the master database. When you use this procedure, the message is sent to the event log and to the SQL Server log, but not to the client application. An example of using this procedure is shown here:
USE master GO xp_logevent 50002, "Customer ID out of range", warning GO
The first two parameters are required and consist of a user-defined message ID number (which, again, must be greater than 50,000) and message text that will appear in the output to the logs. The third parameter, the severity level, is optional. Severity can be one of three character strings: informational, warning, or error. The severity level setting determines what type of icon appears next to the message in Event Viewer, enabling you to quickly identify warnings or errors. For Windows 2000, an informational message has a blue "i" icon, a warning has a yellow "!" icon, and an error has a red "X" icon. If no severity is specified, the default value is informational.
Now we're ready to create an alert on an event and on a performance condition. To create an alert, you can use Enterprise Manager, T-SQL, or SQL-DMO. Again, we will look at only the Enterprise Manager and T-SQL methods because SQL-DMO is beyond the scope of this book.
In this example, we'll create an alert on a system message that already has a severity level of 24. The message will be logged by default in the event log, with no user intervention needed to change its logging status. To create the event alert, follow these steps:
Figure 31-17. The General tab of the New Alert Properties window.
Figure 31-18. The Search tab of the Manage SQL Server Messages dialog box.
Figure 31-19. The Messages tab of the Manage SQL Server Messages dialog box.
Figure 31-20. The Response tab of the New Alert Properties window.
If you specify an operator to be notified by e-mail and also select the Include Alert Error Text In E-mail check box, the error text will be sent to the operator in the alert message. You include the additional text in the e-mail by typing it in the Additional Notification Message To Send text box at the bottom of the tab. The maximum number of additional text characters is 512. Figure 31-20 shows an operator, TestOperator, selected to be notified by e-mail. An additional message is included. Notice also the Delay Between Responses spin boxes. The settings in these spin boxes specify how often the operator will be notified if an alert occurs repeatedly. A setting of 60 minutes means that the operator will be notified only once during any 60-minute period.
Now we'll use Enterprise Manager to create an alert that fires when a certain performance condition occurs. Note that SQLServerAgent polls performance counters at 20-second intervals, so if a peak or a low occurs for only a few seconds between polls, it might not be detected. To create the alert, follow these steps:
Figure 31-21. The General tab of the New Alert Properties window.
You can also use T-SQL to create your alerts, but remember that if you create alerts by using Enterprise Manager, you can later generate the T-SQL scripts for those alerts. (To do so, right-click Alerts in the SQL Server Agent folder, point to All Tasks in the shortcut menu, and then choose Generate SQL Script.) You might find using Enterprise Manager to create alerts easier because the T-SQL method requires learning and remembering many optional parameters and their defaults.
To add an alert by using T-SQL, you use the sp_add_alert stored procedure. You use this procedure whether you want to create an event alert or a performance condition alert. The parameter options determine which type of alert is created. The syntax of sp_add_alert is shown here:
sp_add_alert [@name =] 'name' [, [@message_id =] message_id] [, [@severity =] severity] [, [@enabled =] enabled] [, [@delay_between_responses =] delay_between_responses] [, [@notification_message =] 'notification_message'] [, [@include_event_description_in =] include_event_description_in] [, [@database_name =] 'database'] [, [@event_description_keyword =] 'event_description_keyword_pattern'] [, {[@job_id =] job_id | [@job_name =] 'job_name'}] [, [@raise_snmp_trap =] raise_snmp_trap] [, [@performance_condition =] 'performance_condition'] [, [@category_name =] 'category']
The stored procedures used to modify an alert, view alert information, and delete an alert are sp_update_alert, sp_help_alert, and sp_delete_alert, respectively. Remember, all of these stored procedures are found in the msdb database.
MORE INFO
For more details about the procedures described in this section, look up the procedures in the Books Online index.