{"id":1099,"date":"2011-10-24T19:38:32","date_gmt":"2011-10-24T19:38:32","guid":{"rendered":"http:\/\/craftydba.com\/?p=1099"},"modified":"2017-10-15T16:28:52","modified_gmt":"2017-10-15T16:28:52","slug":"system-alerts","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=1099","title":{"rendered":"System Alerts"},"content":{"rendered":"<p>This article assumes that you configured both <a href=\"https:\/\/craftydba.com\/?p=1025\">database mail <\/a>and <a href=\"https:\/\/craftydba.com\/?p=1085\">system operators<\/a>.  Please see my previous articles if you have not done these tasks already.<\/p>\n<p>Today, I am going to talk about setting up alerts for system error messages that might occur with your SQL server database. Since alerts have been around for a long time, most of this information is backward compatible with older software versions.<\/p>\n<p>The database engine defines the importance of an error messages with a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa937483(SQL.80).aspx\">severity level<\/a>.  <\/p>\n<p>Low importance errors are defined by levels 0 to 10 (informational only);  medium importance errors are defined by levels 11 to 16 (user\/process issues); and high importance errors are defined by levels 17 to 25 (critical database issues).<\/p>\n<p>Every good database administrator should have alerting on high importance<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms164086.aspx\"> error messages<\/a>.  Optional alerting can be enabled on medium importance error messages if you have time to research possible issues.<\/p>\n<p>The code below uses <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms181597.aspx\">sp_add_category <\/a>to create a category for these alerts, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189531.aspx\">sp_add_alert <\/a>to add a new alert based upon severity, and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms173843.aspx\">sp_add_notification <\/a>to ensure a email goes out to the operator when the alert happens.  A simple WHILE construct is used to setup alerts for errors with severity ranging from 17 to 25.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - add alerts for severity codes\">\r\n--\r\n-- Add alerts for severity errors 17 to 25\r\n--\r\n\r\n-- Add a custom category for alerts\r\nEXEC msdb.dbo.sp_add_category\r\n    @class=N'ALERT',\r\n    @type=N'NONE',\r\n    @name=N'[Alert By Severity]' ;\r\nGO\r\n\r\n-- Loop from 17 to 26\r\nDECLARE @VAR_ACNT INT;\r\nDECLARE @VAR_ANAME VARCHAR(125);\r\nSET @VAR_ACNT = 17;\r\n\r\n-- Add severe alerts to system\r\nWHILE (@VAR_ACNT < 26)\r\nBEGIN\r\n    SET @VAR_ANAME = 'Alert For Severity ' + STR(@VAR_ACNT, 2, 0);\r\n\r\n    -- Add alert to system\r\n    EXEC msdb.dbo.sp_add_alert @name=@VAR_ANAME,\r\n\t\t@message_id=0, \r\n\t\t@severity=@VAR_ACNT, \r\n\t\t@enabled=1, \r\n\t\t@delay_between_responses=60, \r\n\t\t@include_event_description_in=5, \r\n\t\t@category_name=N'[Alert By Severity]', \r\n\t\t@job_id=N'00000000-0000-0000-0000-000000000000';\r\n\r\n    -- Attach the operator to the alerts\r\n    EXEC msdb.dbo.sp_add_notification\r\n        @alert_name = @VAR_ANAME,\r\n        @operator_name = N'John Miner',\r\n        @notification_method = 1 ;\r\n\t\t\r\n    PRINT 'Add: ' + @VAR_ANAME;\r\n    SET @VAR_ACNT = @VAR_ACNT + 1;\r\nEND\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186933.aspx\">sp_help_alert <\/a>stored procedure will return all alerts that are defined in the system.  These alerts are stored in the msdb.dbo.sysalerts table.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - show current alerts\">\r\n-- Show the current alerts\r\nEXEC msdb.dbo.sp_help_alert \r\nGO\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms173766.aspx\">sp_help_notification <\/a>stored procedure used below shows all the alerts in which I am the notified operator.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - show notifications\">\r\n-- Show the notification\r\nEXEC msdb.dbo.sp_help_notification \r\n    @object_type = N'ALERTS',\r\n    @name = N'John Miner',\r\n    @enum_type = N'ACTUAL',\r\n    @notification_method = 7 ;\r\nGO\r\n<\/pre>\n<\/p>\n<p>Like most system objects, alerting can be enabled or disabled.  The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms178616.aspx\">sp_update_alert <\/a>allows me to disable the 'Alert For Severity 25' alert.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - disable an alert\">\r\n-- Disable Alert 25\r\nEXEC msdb.dbo.sp_update_alert\r\n    @name = N'Alert For Severity 25',\r\n    @enabled = 0 ;\r\nGO<\r\n<\/pre>\n<\/p>\n<p>Last but not least, SQL Server supplies the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188054.aspx\">sp_delete_alert<\/a> stored procedure to remove the alerts.  The code below uses the WHILE construct to remove all alerts we just defined.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - remove my custom alerts\">\r\n-- Loop from 17 to 26\r\nDECLARE @VAR_DCNT INT;\r\nDECLARE @VAR_DNAME VARCHAR(125);\r\nSET @VAR_DCNT = 17;\r\n\r\n-- Remove the alerts\r\nWHILE (@VAR_DCNT < 26)\r\nBEGIN\r\n    SET @VAR_DNAME = 'Alert For Severity ' + STR(@VAR_DCNT, 2, 0)\r\n    EXEC msdb.dbo.sp_delete_alert @VAR_DNAME;\r\n    PRINT 'Delete: ' + @VAR_DNAME\r\n    SET @VAR_DCNT = @VAR_DCNT + 1\r\nEND\r\n<\/pre>\n<\/p>\n<p>In summary, I talked about adding alerting for high importance errors.  Each alert needs to be associated with an operator to be notified.  Optional alerting can be done on medium importance errors.  One feature that I did not go over is the @job parameter which allows a DBA to define a job to execute if an alert is fired.  I will continue next time by talking about setting performance alerts which depends upon WMI queries to monitor operating system counters such as memmory and CPU.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article assumes that you configured both database mail and system operators. Please see my previous articles if you have not done these tasks already. Today, I am going to talk about setting up alerts for system error messages that might occur with your SQL server database. Since alerts have been around for a long time, most of this information is backward compatible with older software versions. The database engine defines the importance of an error messages with a severity level. Low importance errors are defined by levels 0 to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[117,12,181,165,170,167,168,169,166,28,171,29],"class_list":["post-1099","post","type-post","status-publish","format-standard","hentry","category-db-admin","tag-database-administrator","tag-free-code","tag-john-f-miner-iiii","tag-sp_add_alert","tag-sp_add_notification","tag-sp_delete_alert","tag-sp_help_alert","tag-sp_help_notification","tag-sp_update_alert","tag-sql-server","tag-sysalerts","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1099"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/1099\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}