6. SQL Server數據庫監控 - 如何告警

經常使用的告警方式大體有:短信、郵件、應用程序 (beep提示,圖標提示,升窗提示等),但是不能一直坐在電腦前看着應用程序,或者用腳本部署監控,根本沒有程序界面,因此一般用短信、郵件兩種方式告警。html

. 告警方式web

1. 短信shell

用程序發短信的方式通常有這兩種:數據庫

(1) 硬件api

須要1張SIM卡,1個SIM卡讀卡設備 (好比:短信貓),而後把設備鏈接到電腦,應用程序根據設備的軟件接口,傳參併發送短信。記得把SIM卡設備放在信號好,無干擾的地方;服務器

若是有大量短信要發,1張SIM卡是不夠用的,並且發送過分頻繁,可能被運營商視爲惡意短信,把SIM卡號加入黑名單,那麼就須要多張SIM卡甚至多個讀卡設備。併發

顯示號碼爲當前SIM卡號碼,大多供應商都支持DLL、HTTP等多種接口,固然也能夠基於接口二次開發。工具

DLL接口方法參考:SmsManager.sendTextMessage(…)編碼

 

(2) 第三方短信接口spa

有多種接口形式提供,好比: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的字符編碼發送,而後再解析回來。

相關文章
相關標籤/搜索