现象
有个程序会自动关闭,另外一个程序可能有BUG,这个程序会导致数据库死锁。
处理思路:
1、数据库监控:对数据库写入数据进行监控,对写入记录少于一定量进行异常报出;
2、对于进程进行监控及重启,查询到没有进程时,直接重启进程。
3、发现监控数据库时,有时数据库也会因死锁,导致查询不到数据,对查询数据库进行判断,当返回数据库查询异常时重启数据库服务。
脚本实现
1、数据库监控(vb脚本)gn2.vbs:
Function Format_Time(s_Time)
Dim y, m, d, h, mi, s
Format_Time = ""
If IsDate(s_Time) = False Then Exit Function
y = cstr(year(s_Time))
m = cstr(month(s_Time))
If len(m) = 1 Then m = "0" & m
d = cstr(day(s_Time))
If len(d) = 1 Then d = "0" & d
h = cstr(hour(s_Time))
If len(h) = 1 Then h = "0" & h
mi = cstr(minute(s_Time))
If len(mi) = 1 Then mi = "0" & mi
s = cstr(second(s_Time))
If len(s) = 1 Then s = "0" & s
Format_Time = y & "-" & m & "-" & d & " " & h & ":" & mi & ":" & s
End Function
‘数据库连接字符串
Dim DBConnStr
DBConnStr = "PROVIDER=SQLOLEDB;DATA SOURCE=111.111.222.222,1433;UID=sa;PWD=1122aa.0;DATABASE=database"
Dim v_DBRecordSet
Dim v_DBRecCnt
Dim v_CheckTime
‘计算查询时间 当前时间往前5分钟
v_CheckTime = DateAdd("n",-5,now())
v_DBRecCnt = 0
‘创建记录集对象
On Error Resume Next
Set v_DBRecordSet=CreateObject("ADODB.Recordset")
If Err.Number Then
WScript.Echo "Message:" & Err.Description
WScript.Quit(1)
End If
‘初始化记录集参数
v_DBRecordSet.CursorType = 0
v_DBRecordSet.CursorLocation = 2
v_DBRecordSet.LockType = 1
v_DBRecordSet.Source = "Select count(*) as CN from RunLog where SysDatetime >‘" & Format_Time(v_CheckTime) & "‘"
On Error Resume Next
v_DBRecordSet.ActiveConnection = DBConnStr
If Err.Number Then
WScript.Echo "Message:" & Err.Description
WScript.Quit(1)
End If
On Error Resume Next
v_DBRecordSet.Open()
If Err.Number Then
WScript.Echo "Message:" & Err.Description
WScript.Quit(1)
End If
On Error Resume Next
v_DBRecCnt = v_DBRecordSet.Fields("CN")
If Err.Number Then
WScript.Echo "Message:" & Err.Description
WScript.Quit(1)
End If
‘如果等于0,则报警
if CInt(v_DBRecCnt) >100 Then
‘关闭记录集
v_DBRecordSet.Close()
Set v_DBRecordSet = Nothing
‘正常退出
WScript.Echo "前5分钟存储条数:" & v_DBRecCnt
WScript.Quit(0)
else
‘关闭记录集
v_DBRecordSet.Close()
Set v_DBRecordSet = Nothing
‘异常退出
WScript.Echo "前5分钟存储条数:" & v_DBRecCnt
WScript.Quit(1)
end if
2、监控数据库及重启服务相关脚本(bat脚本):
setlocal enabledelayedexpansion
cscript //nologo c:\gn2.vbs
If ERRORLEVEL 1 (net stop SQLSERVERAGENT
net stop MSSQLSERVER
PING 172.18.18.2 -n 2
net start MSSQLSERVER
net start SQLSERVERAGENT
echo %date:~0,10% %time:~0,8% Restart MSSQLSERVER Service >>c:\RESTART_GN.txt ) else goto check_gn_carrunlog
:check_gn_carrunlog
set /a carrunlog2data=0
PING 172.18.18.2 -n 50
for /f "delims=" %%! in (‘cscript //nologo c:\gn2.vbs‘) do set /a carrunlog2data =%%!
if "%carrunlog2data%" leq "0" (
call RESTART_GN.bat)
else exit
3、自动监控进程及重启进程脚本(bat脚本):
setlocal enabledelayedexpansion
echo ############################时时监控,请勿关闭!#########################
ping 172.18.18.2 -n 10
tasklist /nh|find /i "w3wp.exe"
If ERRORLEVEL 1 (start C:\web\web通信服务器\w3wp.exe
echo %date:~0,10% %time:~0,8% Restart w3wp.exe >>c:\Restart_WEB.txt
) else exit
4、为定期执行以上脚本,原计划通过开机自动执行,但考虑偶尔会有人工重启程序,改为通过WINDOWS 计划任务定期(5分钟或10分钟)调用执行,目前执行效果较好。