目錄
' +@HtmlBody+'
首頁 資料庫 mysql教程 6.SQLServer数据库监控-如何告警

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

Jun 07, 2016 pm 02:53 PM
告警 如何 資料庫 監控

常用的告警方式大致有:短信、邮件、应用程序 (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 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&"----testmail----"
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 PROCEDUREsp_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 id="font-nbsp-color-Red-nbsp-HtmlBody-font"><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_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 mail
EXEC @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
                END
end
PRINT 'SendSuccess!!!' 
 
--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'       -- 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 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的字符编码发送,然后再解析回来。

 

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1317
25
PHP教程
1268
29
C# 教程
1246
24
iOS 18 新增「已復原」相簿功能 可找回遺失或損壞的照片 iOS 18 新增「已復原」相簿功能 可找回遺失或損壞的照片 Jul 18, 2024 am 05:48 AM

蘋果公司最新發布的iOS18、iPadOS18以及macOSSequoia系統為Photos應用程式增添了一項重要功能,旨在幫助用戶輕鬆恢復因各種原因遺失或損壞的照片和影片。這項新功能在Photos應用的"工具"部分引入了一個名為"已恢復"的相冊,當用戶設備中存在未納入其照片庫的圖片或影片時,該相冊將自動顯示。 "已恢復"相簿的出現為因資料庫損壞、相機應用未正確保存至照片庫或第三方應用管理照片庫時照片和視頻丟失提供了解決方案。使用者只需簡單幾步

在PHP中使用MySQLi建立資料庫連線的詳盡教學 在PHP中使用MySQLi建立資料庫連線的詳盡教學 Jun 04, 2024 pm 01:42 PM

如何在PHP中使用MySQLi建立資料庫連線:包含MySQLi擴充(require_once)建立連線函數(functionconnect_to_db)呼叫連線函數($conn=connect_to_db())執行查詢($result=$conn->query())關閉連線( $conn->close())

如何在PHP中處理資料庫連線錯誤 如何在PHP中處理資料庫連線錯誤 Jun 05, 2024 pm 02:16 PM

PHP處理資料庫連線報錯,可以使用下列步驟:使用mysqli_connect_errno()取得錯誤代碼。使用mysqli_connect_error()取得錯誤訊息。透過擷取並記錄這些錯誤訊息,可以輕鬆識別並解決資料庫連接問題,確保應用程式的順暢運作。

如何在 Golang 中使用資料庫回呼函數? 如何在 Golang 中使用資料庫回呼函數? Jun 03, 2024 pm 02:20 PM

在Golang中使用資料庫回呼函數可以實現:在指定資料庫操作完成後執行自訂程式碼。透過單獨的函數新增自訂行為,無需編寫額外程式碼。回調函數可用於插入、更新、刪除和查詢操作。必須使用sql.Exec、sql.QueryRow或sql.Query函數才能使用回呼函數。

如何在 Golang 中將 JSON 資料保存到資料庫中? 如何在 Golang 中將 JSON 資料保存到資料庫中? Jun 06, 2024 am 11:24 AM

可以透過使用gjson函式庫或json.Unmarshal函數將JSON資料儲存到MySQL資料庫中。 gjson函式庫提供了方便的方法來解析JSON字段,而json.Unmarshal函數需要一個目標類型指標來解組JSON資料。這兩種方法都需要準備SQL語句和執行插入操作來將資料持久化到資料庫中。

如何用 Golang 連接遠端資料庫? 如何用 Golang 連接遠端資料庫? Jun 01, 2024 pm 08:31 PM

透過Go標準庫database/sql包,可以連接到MySQL、PostgreSQL或SQLite等遠端資料庫:建立包含資料庫連接資訊的連接字串。使用sql.Open()函數開啟資料庫連線。執行SQL查詢和插入操作等資料庫操作。使用defer關閉資料庫連線以釋放資源。

PHP與不同資料庫的連接:MySQL、PostgreSQL、Oracle和更多 PHP與不同資料庫的連接:MySQL、PostgreSQL、Oracle和更多 Jun 01, 2024 pm 03:02 PM

PHP連接資料庫指南:MySQL:安裝MySQLi擴展,建立連線(servername、username、password、dbname)。 PostgreSQL:安裝PgSQL擴展,建立連線(host、dbname、user、password)。 Oracle:安裝OracleOCI8擴展,建立連線(servername、username、password)。實戰案例:取得MySQL資料、PostgreSQL查詢、OracleOCI8更新記錄。

如何使用C++處理資料庫連線和操作? 如何使用C++處理資料庫連線和操作? Jun 01, 2024 pm 07:24 PM

在C++中使用DataAccessObjects(DAO)函式庫連接和操作資料庫,包括建立資料庫連線、執行SQL查詢、插入新記錄和更新現有記錄。具體步驟為:1.包含必要的函式庫語句;2.開啟資料庫檔案;3.建立Recordset物件執行SQL查詢或操作資料;4.遍歷結果或依照特定需求更新記錄。

See all articles