您好,欢迎访问一九零五行业门户网

6.SQLServer数据库监控-如何告警

常用的告警方式大致有:短信、邮件、应用程序 (beep提示,图标提示,升窗提示等),可是不能一直坐在电脑前看着应用程序,或者用脚本部署监控,根本没有程序界面,所以通常用短信、邮件两种方式告警。 一 . 告警方式 1. 短信 用程序发短信的方式一般有这两种
常用的告警方式大致有:短信、邮件、应用程序 (beep提示,图标提示,升窗提示等),可是不能一直坐在电脑前看着应用程序,或者用脚本部署监控,根本没有程序界面,所以通常用短信、邮件两种方式告警。
一. 告警方式
1. 短信
用程序发短信的方式一般有这两种:
(1) 硬件
需要1张sim卡,1个sim卡读卡设备 (比如:短信猫),然后把设备连接到电脑,应用程序根据设备的软件接口,传参并发送短信。记得把sim卡设备放在信号好,无干扰的地方;
如果有大量短信要发,1张sim卡是不够用的,而且发送过度频繁,可能被运营商视为恶意短信,把sim卡号加入黑名单,那么就需要多张sim卡甚至多个读卡设备。
显示号码为当前sim卡号码,大多供应商都支持dll、http等多种接口,当然也可以基于接口二次开发。
dll接口方法参考:smsmanager.sendtextmessage(…)
(2) 第三方短信接口
有多种接口形式提供,比如:web service形式,http形式,还有邮件接口:往1380013900@xxx.com发个短小的邮件,这个邮件会以短信的形式转发到手机上,等等。只要往接口传参数,告诉它发给谁,发什么内容,就可以了。
显示号码为某个sim卡号码,或者为固定号码 (如:106开头的),这取决于短信平台和运营商的实现方式,因为运营商发短信是不要卡的,直接可以发给目标号码,而且可以显示为固定的某个号码。
web service接口地址参考:http://123.456.789.000/smsmanager.asmx?wsdl
http接口地址参考:http://api.abc.xyz/sms/send.html
2. 邮件
凡是实现了smtp协议的组件,都可以发送邮件。
在windows环境下,有系统自带的组件cdo (collaboration data objects,以前叫ole messaging 或者active messaging),是mapi库的com封装。不管是自己开发程序,使用vbs,还是sql server的sql mail/database mail,通常都是调用的这个组件。
smtp协议要求的参数大致如下:
smtp hostname: smtp服务器名,如mail.test.com或者ip
smtp port: smtp服务端口,25
smtp username: 通过smtp发送邮件用来验证的用户名, 如果不要求身份验证,留空
smtp password: 通过smtp发送邮件用来验证的密码, 如果不要求身份验证,留空
二. 选择告警方式并配置
1. 短信
不管是选择硬件,还是第三方接口,都需要一个程序来调用,可以是监控工具、脚本、甚至数据库。
(1)  监控工具/应用程序中,通常都留有短信接口的配置,配置接口地址即可;
(2) 在脚本中配置,windows环境通常要借助ole automation;
ole automation后来改名叫automation,是windows上基于com,用于脚本语言实现进程间通讯的机制,脚本如:vbs, sql, powershell,不包括bat(bat可以调用vbs)。
sql server中使用ole automation调用web service短信接口如下:
exec sp_configure'show advanced options', 1;reconfigure;exec sp_configure'ole automation procedures', 1;reconfigure; declare @text_message nvarchar(180) ,@phone_numbernvarchar(15) ,@soap_object int ,@status int ,@output nvarchar(255) set @text_message = n'testing mail'set @phone_number = n'138000139000' --create mssoap.soapclient objectexec @status=sp_oacreate'mssoap.soapclient',@soap_object out --smsmanager is web service nameexec @status =sp_oamethod@object, 'mssoapinit', null, 'http://123.456.789.000/smsmanager.asmx?wsdl', 'smsmanager' --sendtextmessage is webservice methodexec @status =sp_oamethod@object, 'sendtextmessage', @output out, @phone_number,@text_message if @status 0begin exec sp_oageterrorinfo@soap_object select@soap_objectendelsebegin select@outputend --destroy mssoap.soapclient objectexec @status =sp_oadestroy@soap_objectgo
对于http, dll接口,和soap接口类似,用ole automation也都可以调用,主要区别就是在createobject() 时。
以vbs为例,调用http, dll时createobject()如下:
dim httpset http =createobject(msxml2.xmlhttp) dim dllset dll = createobject(工程名.类名)
2. 邮件
(1) 监控工具/应用程序中,通常都留有smtp配置项,配置smtp参数即可;
(2) 在脚本中配置,windows环境通常要借助ole automation;
vbs发送邮件如下:
dim nsns =http://schemas.microsoft.com/cdo/configuration/ dim title, contenttitle = db_maint_alertcontent = content = content&hi all,content = content&chr(13)&chr(10)content = content& content = content&chr(13)&chr(10)content = content&----testmail----msgbox('~1~') set cm =createobject(cdo.message)cm.from =from_user_name@abc.comcm.to = to_user_name@abc.comcm.cc = cc_user_name@abc.comcm.subject = titlecm.textbody = content'cm.addattachment msgbox('~2~') 'sendusing: 1 = pickup, 2 = port'smtpauthenticate: 0 = anonymous,1 =common,2 = ntlm'smtpusessl: 0 = no,1 = yeswith cm.configuration.fields .item(ns& sendusing) = 2 .item(ns& smtpserver) = xxx.xxx.xxx.xxx .item(ns& smtpserverport) = 25 .item(ns& smtpauthenticate) = 1 .item(ns& sendusername) = user_name@abc.com .item(ns& sendpassword) = ***************** .item(ns& smtpconnectiontimeout) = 10 .item(ns& smtpusessl) = 0 .updateend withmsgbox('~3~') cm.sendset cm = nothingmsgbox('~success~')
sql server 2000发送邮件如下:
sql server 2000有sql mail,不过必须要同服务器上安装一个实现了mapi的邮件程序,如:outlook,因为sql mail需要借用邮件应用程序的mapi来发送邮件,配置起来不太方便,所以使用类似上面vbs的ole automation方法。
use master;if object_id('sp_senddatabasemail') is not null drop proc sp_senddatabasemailgo create proceduresp_senddatabasemail @recipients varchar(8000), --'001@abc.com; 002@abc.com;' @subject varchar(400) = '', @htmlbody varchar(8000) = ''asdeclare @from varchar(100)declare @to varchar(100)declare @bcc varchar(500)declare @addattachment varchar(100)declare @object intdeclare @hr intdeclare @source varchar(255) declare @description varchar(500) declare @output varchar(1000) set @from = 'sqlalert@abc.com' set @to = @recipients set @bcc = '' set@addattachment = '' --set @htmlbody='' +@htmlbody+'' exec @hr = sp_oacreate'cdo.message', @object out exec @hr = sp_oasetproperty@object, 'configuration.fields(http://schemas.microsoft.com/cdo/configuration/sendusing).value','2' exec @hr = sp_oasetproperty@object, 'configuration.fields(http://schemas.microsoft.com/cdo/configuration/smtpserver).value', 'xxx.xxx.xxx.xxx' exec @hr = sp_oasetproperty@object, 'configuration.fields(http://schemas.microsoft.com/cdo/configuration/smtpserverport).value','25' exec @hr = sp_oasetproperty@object, 'configuration.fields(http://schemas.microsoft.com/cdo/configuration/smtpauthenticate).value','1' exec @hr = sp_oasetproperty@object, 'configuration.fields(http://schemas.microsoft.com/cdo/configuration/sendusername).value','user_name@abc.com' exec @hr = sp_oasetproperty@object, 'configuration.fields(http://schemas.microsoft.com/cdo/configuration/sendpassword).value','*****************' exec @hr = sp_oamethod@object, 'configuration.fields.update',null exec @hr = sp_oasetproperty@object, 'to', @to exec @hr = sp_oasetproperty@object, 'bcc', @bcc exec @hr = sp_oasetproperty@object, 'from', @from exec @hr = sp_oasetproperty@object, 'subject', @subject exec @hr = sp_oasetproperty@object, 'htmlbody', @htmlbody --add attachment if@addattachment'' exec @hr = sp_oamethod@object, 'addattachment',null,@addattachment if @hr 0 select @hr begin exec@hr = sp_oageterrorinfonull, @source out, @description out if@hr = 0 begin select@output = ' source: '+ @source print@output select@output = 'description: ' + @description print@output end else begin print' sp_oageterrorinfo failed.' return end end --send mailexec @hr =sp_oamethod@object, 'send', null if @hr 0 select @hr begin exec@hr = sp_oageterrorinfonull, @source out, @description out if@hr = 0 begin select@output = ' source: '+ @source print@output select@output = 'description: ' + @description print@output end else begin print' sp_oageterrorinfo failed.' return endendprint 'sendsuccess!!!' --destroy objectexec @hr =sp_oadestroy@object
调用上面这个sp来发邮件:
exec sp_senddatabasemail @recipients= '001@test.com;002@test.com;', @body ='this is a testing mail', @htmlbody ='testing database mail'
sql server 2005起,使用database mail,脚本如下:
--1. 启用database mailuse mastergoexec sp_configure'show advanced options',1reconfigureexec sp_configure'database mail xps',1reconfigurego --2. 添加accountexec msdb..sysmail_add_account_sp @account_name = 'sqlalert' --mail account ,@email_address = 'sqlalert@test.com' -- sendmailaddress ,@display_name = 'sqlalert' --sendusername ,@replyto_address = null ,@description = null ,@mailserver_name = '***,***,***,***' -- smtpaddress ,@mailserver_type = 'smtp' --sql 2005 only support smtp ,@port =25 -- port --,@username = '*********@test.com' -- account --,@password = '******************' -- pwd ,@use_default_credentials= 0 ,@enable_ssl =0 --is ssl enabled on smtp server ,@account_id =null --3. 添加profile exec msdb..sysmail_add_profile_sp @profile_name= 'sqlalert' -- profilename ,@description = 'dba mail profile' --profile description ,@profile_id = null --4. 关联account and profile exec msdb..sysmail_add_profileaccount_sp @profile_name = 'sqlalert' -- profile name ,@account_name = 'sqlalert' -- account name ,@sequence_number= 1 -- account order in profile --5. 发送database mailexec msdb.dbo.sp_send_dbmail @profile_name= 'sqlalert', @recipients ='001@test.com; 002@test.com;', @body ='this is a testing mail', @subject ='testing database mail';go
注意:smtp服务器的配置,比如:是否使用smtp用户验证,ssl是否开启,必须要和服务端一致,否则无法发送邮件。
其他
(1) 告警的次数:被告警的问题也许正在处理中,告警还在反复频繁发送,尤其用脚本轮询时,注意设置次数和发送间隔;
(2) 告警的历史记录:短信或者邮件告警,最好都在数据库中留一份记录;
(3) 字符编码:如果应用程序/接口不支持中文,可以把中文转成utf-8的字符编码发送,然后再解析回来。
其它类似信息

推荐信息