{"id":1114,"date":"2011-10-25T18:47:58","date_gmt":"2011-10-25T18:47:58","guid":{"rendered":"http:\/\/craftydba.com\/?p=1114"},"modified":"2017-10-15T16:25:35","modified_gmt":"2017-10-15T16:25:35","slug":"performance-alerts","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=1114","title":{"rendered":"Performance Alerts"},"content":{"rendered":"<p>Today, I am going to talk about setting up performance alerts so that you can notified when a server has issues.  Basically, there are two types of performance alerts, <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ms190382.aspx\">SQL <\/a>Server and <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee156560.aspx\">WMI<\/a>.<\/p>\n<p>I am going to use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms181597.aspx\">sp_add_category <\/a>system procedure to define a peformance category.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - custom alert category\">\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 Performance]' ;\r\nGO\r\n\r\n-- Show Alert categoryes\r\nEXEC msdb.dbo.sp_help_category @class=N'ALERT'\r\nGO\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189531.aspx\">sp_add_alert <\/a>stored procedure is used to create the alert and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms173843.aspx\">sp_add_notification <\/a>stored procedure is used to associate the alert with an operator.  This process is used below to show examples of both SQL and WMI performance alerts.<\/p>\n<p>The first type of performance alert is based upon SQL Server objects and counters that the database tracks.  To set up the alert, you need to specify the object, counter, database instance, and condition to be meet for the alert to be triggered.  The alert below notifies the operator when the log file on the adventure works database exceeds 50%.  It will send out a email with a description of the event to the operator.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - performance alert 4 log file\">\r\n-- Peformance Alert - Log file half full\r\nEXEC msdb.dbo.sp_add_alert @name=N'Alert Full Log File (AW)', \r\n        @message_id=0, \r\n\t@severity=0, \r\n\t@enabled=1, \r\n\t@delay_between_responses=60, \r\n\t@include_event_description_in=1, \r\n\t@category_name=N'[Alert By Performance]', \r\n\t@performance_condition=N'SQLServer:Databases|Percent Log Used|AdventureWorks2008R2|>|50', \r\n\t@job_id=N'00000000-0000-0000-0000-000000000000'\r\nGO\r\n\r\n-- Attach the operator to the alerts\r\nEXEC msdb.dbo.sp_add_notification\r\n    @alert_name = 'Alert Full Log File (AW)',\r\n    @operator_name = N'John Miner',\r\n    @notification_method = 1 ;\r\nGO\r\n<\/pre>\n<\/p>\n<p>The second type of performance alert is based upon WMI queries which came out with windows scripting in the year 2000.  The example below looks for overall CPU usage every 60 seconds and triggers the alert if it is greater than 75%.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - performance alert 4 processor usage\">\r\n-- Performance Alert - WMI Query - Processor > 75%\r\nEXEC msdb.dbo.sp_add_alert @name=N'Alert High CPU % (WMI)',\r\n    @message_id=0,\r\n    @severity=0,\r\n    @enabled=1,\r\n    @delay_between_responses=0,\r\n    @include_event_description_in=1,\r\n    @category_name=N'[Alert By Performance]', \r\n    @wmi_namespace=N'\\\\.\\ROOT\\CIMV2', \r\n    @wmi_query=N'SELECT * FROM __InstanceModificationEvent WITHIN 60 \r\n        WHERE TargetInstance ISA ''Win32_Processor'' AND TargetInstance.LoadPercentage > 75', \r\n    @job_id=N'00000000-0000-0000-0000-000000000000';\r\nGO\r\n\r\n-- Attach the operator to the alerts\r\nEXEC msdb.dbo.sp_add_notification\r\n    @alert_name = 'Alert High CPU % (WMI)',\r\n    @operator_name = N'John Miner',\r\n    @notification_method = 1 ;\r\nGO    \r\n<\/pre>\n<\/p>\n<p>To remove the previous alert, calls to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms173843.aspx\">sp_delete_notification <\/a>and <a href=\"http:\/\/social.msdn.microsoft.com\/Search\/en-us?query=sp_delete_alert&#038;x=13&#038;y=5\">sp_delete_alert <\/a>procedures need to be executed.  The operator to alert mapping is stored in the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ms188906(SQL.110).aspx\">msdb.dbo.sysnotifications <\/a>table.  <\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - remove performance alert\">\r\n-- Remove the operator 2 alert relationship\r\nEXEC msdb.dbo.sp_delete_notification\r\n    @alert_name = 'Alert High CPU % (WMI)',\r\n    @operator_name = N'John Miner';\r\nGO\r\n\r\n-- Remove the alert\r\nEXEC msdb.dbo.sp_delete_alert @alert_name = 'Alert High CPU % (WMI)';\r\nGO\r\n<\/pre>\n<\/p>\n<p>Something I forgot last time to mentions is that standard error messages can be retrieved from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187382.aspx\">msdb.sys.messages <\/a>view.  These messages can be added to by the end user using the standard <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms178649.aspx\">sp_addmessage <\/a>and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms174369.aspx\">sp_drop_message <\/a>system procedures.  The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms175094.aspx\">sp_altermessage <\/a>procedure actual logs the message as if it was raised by code.  The code snipet below demonstrates these techniques.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql - create user defined error message\">\r\n-- Add a new user defined message \r\nEXEC msdb.dbo.sp_addmessage \r\n    @msgnum = 60007, \r\n    @severity = 16 , \r\n    @msgtext = N'A prior operation check has failed during processing.', \r\n    @lang = 'us_english',\r\n    @with_log = 'FALSE';\r\nGO\r\n\r\n-- Place the error in the windows application log\r\nEXEC msdb.dbo.sp_altermessage \r\n    60007, 'WITH_LOG', 'true';  \r\n\r\n-- Drop user defined message\r\nEXEC sp_dropmessage \r\n    @msgnum = 60007,\r\n    @lang = 'us_english';\r\nGO\r\n<\/pre>\n<\/p>\n<p>In conclusion, SQL Server out of the box gives you the tools to create alerts for both standard errors and performance issues.  It is up to you to use these tools to build your own custom performance monitoring strategy.  Next time, I will introduce two test scripts that can be used to test two alerts we previously defined.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I am going to talk about setting up performance alerts so that you can notified when a server has issues. Basically, there are two types of performance alerts, SQL Server and WMI. I am going to use the sp_add_category system procedure to define a peformance category. &#8212; Add a custom category for alerts EXEC msdb.dbo.sp_add_category @class=N&#8217;ALERT&#8217;, @type=N&#8217;NONE&#8217;, @name=N'[Alert By Performance]&#8217; ; GO &#8212; Show Alert categoryes EXEC msdb.dbo.sp_help_category @class=N&#8217;ALERT&#8217; GO The sp_add_alert stored procedure is used to create the alert and the sp_add_notification stored procedure is used to associate&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,15,173,174,175,165,170,177,176,167,172,28,29],"class_list":["post-1114","post","type-post","status-publish","format-standard","hentry","category-db-admin","tag-database-administrator","tag-free-code","tag-john-f-miner-iii","tag-msdb-dbo-sysnotifications","tag-msdb-sys-messages","tag-sp_addmessage","tag-sp_add_alert","tag-sp_add_notification","tag-sp_altermessage","tag-sp_deletemessage","tag-sp_delete_alert","tag-sp_delete_notification","tag-sql-server","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/1114","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=1114"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/1114\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}