SQL Server数据库监控 - 如何告警
作者:网络转载 发布时间:[ 2014/10/14 13:19:05 ] 推荐标签:软件开发 数据库 SQL Server
对于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(13)&chr(10)
content = content&" "
content = content&chr(13)&chr(10)
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") = 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
.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','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_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
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
在测试数据库性能时,需要注意哪些方面的内容?测试管理工具TC数据库报错的原因有哪些?怎么解决?数据库的三大范式以及五大约束编程常用的几种时间戳转换(java .net 数据库)优化mysql数据库的几个步骤数据库并行读取和写入之Python实现深入理解数据库(DB2)缓冲池(BufferPool)国内三大云数据库测试对比预警即预防:6大常见数据库安全漏洞数据库规划、设计与管理数据库-事务的概念SQL Server修改数据库物理文件存在位置使用PHP与SQL搭建可搜索的加密数据库用Python写一个NoSQL数据库详述 SQL 中的数据库操作详述 SQL 中的数据库操作Java面试准备:数据库MySQL性能优化

sales@spasvo.com