案例分析:笔者架设了公司整体的服务器系统监控系统,实现运行数据收集,筛选,报警的机制,采用了windows系统自带的性能监视器,但是在实施的过程中,遇到了一个问题:性能监视器无法对sql server 2008(基于window server 2008)进行系统性能监控,无法从监控主机连上目前机器,就无法收集数据进行处理。因这台服务器比较重要,是公司的ERP数据库服务器,已经连续发生了两次因D盘空间爆满,sql server无法启动,导致大面积用户无法登陆正常使用的情况。笔者针对现状,决定采取脚本与批处理结合的办法,每天定时发送服务器运行数据到指定邮箱,并手工对D盘做阀值报警设置,步骤如下:

1. 通过脚本收集数据到指定文件夹(见前几批博文介绍)

2. 在发送数据到指定文件夹的同时,放置一份数据到临时文件,然后每次收集时进行更新

echo off

echo %date% %time% >>info.txt ‘写入固定文件夹,每次新增

echo CPU Information:>>info.txt

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

echo Memery Information:>>info.txt

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

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

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脚本。