定時發送服務器運行數據並設置閥值警報方法

案例分析:筆者架設了公司總體的服務器系統監控系統,實現運行數據收集,篩選,報警的機制,採用了windows系統自帶的性能監視器,可是在實施的過程當中,遇到了一個問題:性能監視器沒法對sql server 2008(基於window server 2008)進行系統性能監控,沒法從監控主機連上目前機器,就沒法收集數據進行處理。因這臺服務器比較重要,是公司的ERP數據庫服務器,已經連續發生了兩次因D盤空間爆滿,sql server沒法啓動,致使大面積用戶沒法登錄正常使用的狀況。筆者針對現狀,決定採起腳本與批處理結合的辦法,天天定時發送服務器運行數據到指定郵箱,並手工對D盤作閥值報警設置,步驟以下:sql

1. 經過腳本收集數據到指定文件夾(見前幾批博文介紹)數據庫

2. 在發送數據到指定文件夾的同時,放置一份數據到臨時文件,而後每次收集時進行更新windows

echo off服務器

echo %date% %time% >>info.txt ‘寫入固定文件夾,每次新增ide

echo CPU Information:>>info.txt性能

cscript //Nologo cpu.vbs >> info.txtcode

echo Memery Information:>>info.txtorm

cscript //Nologo ram.vbs >> info.txtserver

cscript //Nologo hard.vbs >> info.txttoken

echo %date% %time% >temp.txt ‘寫入臨時文件夾,每次刷新

echo CPU Information:>>temp.txt

cscript //Nologo cpu.vbs >>temp.txt

echo Memery Information:>>temp.txt

cscript //Nologo ram.vbs >> temp.txt

cscript //Nologo hard.vbs >> temp.txt

3. 在監控服務器的計劃任務中添加郵件發送任務,天天10:00定時發送temp的內容到指定服務器

content= "C:\perflogs-remote\PROD-SQL\temp.txt"

set fso=createobject("scripting.filesystemobject")

if fso.fileexists(content) then

set fil=fso.getfile(content)

filename=fil.name

if lcase(right(filename,4))=".txt" then

set txt=fso.opentextfile(content,1)

code=txt.readall

txt.close

end if

end if

nr=code

Const Email_From = "abc@163.com"

'Const Password = ""

Const Email_To = "def@163.com"

Set CDO = CreateObject("CDO.Message")

CDO.Subject = " Status Report"

CDO.From = Email_From

CDO.To = Email_To

CDO.TextBody = nr

'cdo.AddAttachment = "C:\hello.txt"

Const schema = "http://schemas.microsoft.com/cdo/configuration/"

With CDO.Configuration.Fields

.Item(schema & "sendusing") = 2

.Item(schema & "smtpserver") = "10.4.100.70"

.Item(schema & "smtpauthenticate") = 0

.Item(schema & "sendusername") = Email_From

'.Item(schema & "sendpassword") = Password

.Item(schema & "smtpserverport") = 25

.Item(schema & "smtpusessl") = False

.Item(schema & "smtpconnectiontimeout") = 60

.Update

End With

CDO.Send

msgbox "Email sent!"

D盤閥值設置步驟:

1. 截取temp.txt中的關於D盤的須要字段

2012/11/21 16:44:09.22

CPU Information:

CPU Usage: 19%

Memery Information:

Total Memery: 16384 MB

Available Memery: 617 MB

Memery Usage:96%

Harddisk information£o

Partition:C

Available Space:3.733 GB

Total Space:39.9 GB

Usage:90.6433553637135%

Partition:D

Available Space:24.383 GB

Total Space:59.997 GB

Usage:59.3590957097753%

在上述信息中,須要截取D盤的使用率,值是59,代碼以下:

@echo off

for /f "tokens=*" %%i in (temp.txt) do set str=%%i&call set str=%%str:~6,2%%’獲取D盤的值並賦值給str

if %str% LSS 30 start c:\perflogs-remote\PROD-SQL\Prod-sql-alert.vbs’str值與30作比較,如小於30就發送報警郵件。

具體的如何實現報警的方法,請參見以前介紹過的SMTP腳本。

相關文章
相關標籤/搜索