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

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介: 原文:6. SQL Server数据库监控 - 如何告警常用的告警方式大致有:短信、邮件、应用程序 (beep提示,图标提示,升窗提示等),可是不能一直坐在电脑前看着应用程序,或者用脚本部署监控,根本没有程序界面,所以通常用短信、邮件两种方式告警。
原文: 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'138000139000'

--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(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

调用上面这个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的字符编码发送,然后再解析回来。

 

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
8天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
51 10
|
8天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
68 6
|
3天前
|
SQL 安全 网络安全
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
8 0
|
4天前
|
SQL 数据库
数据库SQL语言实战(二)
数据库SQL语言实战(二)
|
5天前
|
SQL 关系型数据库 数据库
【后端面经】【数据库与MySQL】SQL优化:如何发现SQL中的问题?
【4月更文挑战第12天】数据库优化涉及硬件升级、操作系统调整、服务器/引擎优化和SQL优化。SQL优化目标是减少磁盘IO和内存/CPU消耗。`EXPLAIN`命令用于检查SQL执行计划,关注`type`、`possible_keys`、`key`、`rows`和`filtered`字段。设计索引时考虑外键、频繁出现在`where`、`order by`和关联查询中的列,以及区分度高的列。大数据表改结构需谨慎,可能需要停机、低峰期变更或新建表。面试中应准备SQL优化案例,如覆盖索引、优化`order by`、`count`和索引提示。优化分页查询时避免大偏移量,可利用上一批的最大ID进行限制。
32 3
|
8天前
|
SQL 监控 数据库
数据库管理与电脑监控软件:SQL代码优化与实践
本文探讨了如何优化数据库管理和使用电脑监控软件以提升效率。通过SQL代码优化,如使用索引和调整查询语句,能有效提高数据库性能。同时,合理设计数据库结构,如数据表划分和规范化,也能增强管理效率。此外,利用Python脚本自动化收集系统性能数据,并实时提交至网站,可实现对电脑监控的实时性和有效性。这些方法能提升信息系统稳定性和可靠性,满足用户需求。
32 0
|
8天前
|
SQL 存储 数据挖掘
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
服务器数据恢复环境: 一台安装windows server操作系统的服务器。一组由8块硬盘组建的RAID5,划分LUN供这台服务器使用。 在windows服务器内装有SqlServer数据库。存储空间LUN划分了两个逻辑分区。 服务器故障&初检: 由于未知原因,Sql Server数据库文件丢失,丢失数据涉及到3个库,表的数量有3000左右。数据库文件丢失原因还没有查清楚,也不能确定数据存储位置。 数据库文件丢失后服务器仍处于开机状态,所幸没有大量数据写入。 将raid5中所有磁盘编号后取出,经过硬件工程师检测,没有发现明显的硬件故障。以只读方式将所有磁盘进行扇区级的全盘镜像,镜像完成后将所
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
|
存储 SQL 程序员
【Sql Server】存储过程通过作业定时执行按天统计记录
通过前两篇文章的学习,我们已经对创建表、存储过程、作业等功能点有所了解 本次将结合前面所学习的知识点,创建统计表以及结合作业定时按天以及实时统计域名各个长度的记录值
302 0
【Sql Server】存储过程通过作业定时执行按天统计记录
|
9月前
|
存储 SQL 数据库
SQL Server——为什么要使用存储过程?不使用是什么样的?
提高数据库执行速度,可能第一次见到这句话的小伙伴们感觉到非常的匪夷所思叭!怎么就提高了它的执行速度捏,从哪方面可以表现出来呢?既然这里要说到的是为什么要使用存储过程,也就是说它的优点是什么。那我们肯定就要对使用和不使用存储过程两方面来进行对比才能看出它的优点对吧。
|
存储 SQL Go
SQL Server 存储过程
SQL Server 存储过程
118 0