原文:6. SQL Server数据库监控 - 如何告警

常用的告警方式大致有:短信、邮件、应用程序 (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_number nvarchar(15)
,@soap_object int
,@status int
,@output nvarchar(255) set @text_message = N'Testing Mail'
set @phone_number = N'' --Create MSSOAP.SoapClient object
exec @status=sp_OACreate 'MSSOAP.SoapClient', @soap_object out --SmsManager is Web Service name
exec @status = sp_OAMethod @object, 'mssoapinit', null, 'http://123.456.789.000/SmsManager.asmx?wsdl', 'SmsManager' --SendTextMessage is webservice method
exec @status = sp_OAMethod @object, 'SendTextMessage', @output OUT, @phone_number, @text_message if @status <> 0
begin
exec sp_OAGetErrorInfo @soap_object
select @soap_object
end
else
begin
select @output
end --Destroy MSSOAP.SoapClient object
exec @status = sp_OADestroy @soap_object
GO

对于HTTP, DLL接口,和SOAP接口类似,用OLE Automation也都可以调用,主要区别就是在CreateObject() 时。

以VBS为例,调用HTTP, DLL时CreateObject()如下:

Dim http
Set http = CreateObject("Msxml2.XMLHTTP") Dim dll
Set dll = CreateObject("工程名.类名")

2. 邮件

(1)  监控工具/应用程序中,通常都留有SMTP配置项,配置SMTP参数即可;

(2) 在脚本中配置,Windows环境通常要借助OLE Automation;

VBS发送邮件如下:

Dim ns
ns = "http://schemas.microsoft.com/cdo/configuration/" Dim title, content
title = "db_maint_alert"
content = ""
content = content&"Hi All,"
content = content&chr()&chr()
content = content&" "
content = content&chr()&chr()
content = content&"----test mail----"
Msgbox('~1~') Set cm = CreateObject("CDO.Message")
cm.from = "from_user_name@abc.com"
cm.to = "to_user_name@abc.com"
cm.cc = "cc_user_name@abc.com"
cm.subject = title
cm.textbody = content
'cm.AddAttachment ""
Msgbox('~2~') 'sendusing: 1 = pickup, 2 = port
'smtpauthenticate: 0 = anonymous,1 = common,2 = NTLM
'smtpusessl: 0 = no,1 = yes
With cm.configuration.fields
.item(ns & "sendusing") =
.item(ns & "smtpserver") = "xxx.xxx.xxx.xxx"
.item(ns & "smtpserverport") =
.item(ns & "smtpauthenticate") =
.item(ns & "sendusername") = "user_name@abc.com"
.item(ns & "sendpassword") = "*****************"
.item(ns & "smtpconnectiontimeout") =
.item(ns & "smtpusessl") =
.update
End With
Msgbox('~3~') cm.send
Set cm = nothing
Msgbox('~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_SendDatabaseMail
go CREATE PROCEDURE sp_SendDatabaseMail
@recipients varchar(8000), --'001@abc.com; 002@abc.com;'
@Subject varchar(400) = '',
@HtmlBody varchar(8000) = ''
as
Declare @From varchar(100)
Declare @To varchar(100)
Declare @Bcc varchar(500)
Declare @AddAttachment varchar(100)
Declare @object int
Declare @hr int
Declare @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= '<body><h1><font color=Red>' +@HtmlBody+'</font></h1></body>' EXEC @hr = sp_OACreate 'CDO.Message', @object OUT
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value',''
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',''
EXEC @hr = sp_OASetProperty @object, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value',''
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_OAGetErrorInfo NULL, @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 mail
EXEC @hr = sp_OAMethod @object, 'Send', NULL
IF @hr <>0
select @hr
BEGIN
EXEC @hr = sp_OAGetErrorInfo NULL, @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
PRINT 'Send Success!!!' --destroy object
EXEC @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 mail
use master
GO
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Database mail XPs',1
reconfigure
GO --2. 添加account
exec msdb..sysmail_add_account_sp
@account_name = 'SqlAlert' -- mail account
,@email_address = 'SqlAlert@test.com' -- sendmail address
,@display_name = 'SqlAlert' -- sendusername
,@replyto_address = null
,@description = null
,@mailserver_name = '***,***,***,***' -- SMTP Address
,@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' -- profile name
,@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 mail
EXEC 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的字符编码发送,然后再解析回来。

6. SQL Server数据库监控 - 如何告警的更多相关文章

  1. sql server数据库状态监控

    sql server数据库监控 转自:https://www.cnblogs.com/seusoftware/category/500793.html   6. SQL Server数据库监控 - 如 ...

  2. 1. SQL Server服务器监控实现方法

    对于服务器的监控,和对数据库的监控,很少有合二为一的工具,如果有的话,一般是付费软件,或者自行开发的工具.所以如果不想购买软件,也不想花精力去开发的话,可以结合一些免费/开源的工具.自定义脚本,来完成 ...

  3. 4. SQL Server数据库状态监控 - 作业状态

    原文:4. SQL Server数据库状态监控 - 作业状态 有很多地方可以设置定时任务,比如:Windows的计划任务,Linux下的crontab,各种开发工具里的timer组件.SQL Serv ...

  4. 2. SQL Server数据库状态监控 - 错误日志

    原文:2. SQL Server数据库状态监控 - 错误日志 无论是操作系统 (Unix 或者Windows),还是应用程序 (Web 服务,数据库系统等等) ,通常都有自身的日志机制,以便故障时追溯 ...

  5. 3. SQL Server数据库状态监控 - 可用空间

    原文:3. SQL Server数据库状态监控 - 可用空间 数据库用来存放数据,那么肯定需要存储空间,所以对磁盘空间的监视自然就很有必要了. 一. 磁盘可用空间 1. 操作系统命令或脚本.接口或工具 ...

  6. 5. SQL Server数据库性能监控 - 当前请求

    原文:5. SQL Server数据库性能监控 - 当前请求 对于在线运行的系统,当前数据库性能监控,通常监视以下几点: (1) 是否有阻塞 (Blocking); (2) 是否有等待 (Waitin ...

  7. Python监控SQL Server数据库服务器磁盘使用情况

    本篇博客总结一下Python采集SQL Server数据库服务器的磁盘使用信息,其实这里也是根据需求不断推进演化的一个历程,我们监控服务器的磁盘走了大概这样一个历程: 1:使用SQL Server作业 ...

  8. SQL Server Profiler监控执行语句

    SQL Server Profiler监控执行语句,这个功能主要用在实时的监控对数据库执行了什么操作,从而及时有效的跟踪系统的运行. 常规配置选项,名称.模板.保存到文件(可以复用). 事件选择,可以 ...

  9. SQL Server Profiler监控SQL Server性能

    全面掌握SQL Server Profiler 1.       原理与相关概念介绍 SQL Server Profiler,大家已经非常熟悉.常常在性能优化中使用,本文档详细介绍SQL Server ...

随机推荐

  1. bash no such file or directory in ubuntu 1404

    我在我的今天macbook pro retina 里面安装的虚拟机ubuntu 1404. 当我试图执行cadence ncverilog时间.ubuntu终端错误"bash no such ...

  2. 高效C++规划

    推荐写C++代码风格.看似easy.坚持不易,且写且珍惜! --陈国林 1. 版本号和版本号声明 版本号和版本号文件声明位于头文件和定义文件的开头,主要内容 (1)版本号信息 (2)文件名.标识符.摘 ...

  3. 他们控制的定义-DragButton

    一个.叙述性说明 可拖动Button 两.无图无真相 这是用在实际工程效果图.和demo不太一样. 三.源代码 https://github.com/mentor811/Demo_DragButton ...

  4. js在方法Ajax请求数据来推断,验证无效(OnClientClick=&quot;return Method();&quot;),或者直接运行的代码隐藏

    function CheckAdd() { var flag = true; $.ajax({ cache: false, async: false, url: "/ajaxpage/get ...

  5. Codeforces Round #274 (Div. 2) B. Towers

    As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting o ...

  6. Oracle 实现 mysql 的 find_in_set 排序!

    oracle 实现类似MYSQL的 find_in_set 排序,函数 decode: select * from tb_info_game where gameid in(23,20,19,26,1 ...

  7. 补间动画实现(tween)

    1.补间动画的概念: 补间动画:仅仅须要开发人员设置好动画的開始与结束的关键帧 中间帧有喜用计算机补齐. 2.种类:分为4种: ①alpha 透明度 ②alpha 透明度 ③translate 位置移 ...

  8. SQL Server 2012 复制(发布订阅的研究)

    原文:SQL Server 2012 复制(发布订阅的研究) 已实现发布订阅功能,可以实现局域网内双击备份. 一.注意事项: a) 使用[事务复制]功能 b) 必须是相同的SqlServer 帐号和密 ...

  9. 接收一个IT招聘促销信息,试着想参加,有兴趣的可以携手并进,共同。

    时隆重举行! 招聘的企业: (个人认为,中智.也买酒还是非常有吸引力的) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHdia2Zj/font/5a6L ...

  10. hdu 4919 Exclusive or

    Exclusive or Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) T ...