现在的位置: 首页 > 软件测试 > 测试工具 > 软件测试 > 正文

定时监控进程运行工具源码

2012年11月09日 测试工具, 软件测试 ⁄ 共 5060字 ⁄ 字号 暂无评论 ⁄ 阅读 3,836 次

本文主要介绍一个定时监控进程运行工具的源码,使用AutoIT编写。

工具会自动定时检查进程运行情况,如果进程异常退出工具则会自动向配置的邮箱地址发送邮件。代码比较简单,参考简单的注释即可看懂。(Http://loveso.org原创,转载请注明)


#include<file.au3>

Global $SmtpServer, $FromName, $FromAddress, $ToAddress, $CcAddress, $Subject, $Body, $AttachFiles, $Importance, $Username, $Password, $IPPort, $ssl, $BccAddress
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")

;参数配置
[email protected]&"/cfg.ini" ;配置文件
[email protected]&"/log/"&@YEAR&@MON&@MDAY&"_"&"MonitorD3_log.txt" ;日志文件
$ProcessName="test.exe" ;监控的进程名称
$Times=10000 ;监控次数
$Interval=10*60*1000 ;时间间隔,默认10分钟

;邮件配置
$SmtpServer = "smtp.163.com" ; address for the smtp-server to use - REQUIRED
$FromName = "Monitor" ; 名字来自谁是电子邮件的发送
$FromAddress = "[email protected]" ; address from where the mail should come
$ToAddress = "[email protected]" ; destination address of the email - REQUIRED
$Subject = "错误:"&$ProcessName&"异常退出!!!" ; 电子邮件的主题
$Body = $ProcessName&"进程已经异常退出!!!" ; 邮件消息内容 - 可留空,但你得到一个空白邮件
$AttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - 如果不需要留空白
$CcAddress = "" ; 为抄送地址 - 如果不需要留空白
$BccAddress = "" ; bcc的地址 - 如果不需要留空白
$Importance = "High" ; 发送消息的优先级:“高”,“正常”,“低”
$Username = "[email protected]" ; 从使用的用户名的帐户发送邮件获取
$Password = "test" ; 从使用密码的帐户发送邮件获取
$IPPort = 465 ; 端口用于发送邮件 $ssl = 1 ; 启用/禁用安全套接字层发送 - 如果把使用https 1

;获取参数值
GetValue($ProcessName,"ProcessName")
GetValue($Times,"Times")
GetValue($Interval,"Interval")
GetValue($FromAddress,"FromAddress")
GetValue($ToAddress,"ToAddress")
GetValue($Username,"Username")
GetValue($Password,"Password")

For $i=1 To $Times
 If Not ProcessExists($ProcessName) Then
 WriteLog($LogPath,$ProcessName&"进程已异常退出!监控次数: " & $i)
 $rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
 If @error Then
 WriteLog($LogPath,"邮件发送失败...Error code:" & @error & " Description:" & $rc)
 Else
 WriteLog($LogPath,"邮件发送成功...Return code:" & @error & " Description:" & $rc)
 EndIf
 Else
 WriteLog($LogPath,$ProcessName&"工作正常,监控次数: " & $i)
 EndIf
 Sleep($Interval)
Next

Func GetValue(ByRef $value,$section)
 Local $var=IniReadSection($iniPath,$section)
 If @error Then
 IniWriteSection($iniPath,$section,$section&"="&$value)
 Else
 $value=$var[1][1]
 EndIf
EndFunc

Func WriteLog($LogPath,$Message)
 $file = FileOpen($LogPath, 1+8)
 ; 检查文件能否被打开
 If $file = -1 Then
 MsgBox(16, "抱歉!", "不能打开日志文件!",2)
 Exit
 EndIf
 FileWrite($file, @YEAR&@MON&@MDAY&@HOUR&@MIN&@SEC&": "&$Message&@CRLF)
 FileClose($file)
EndFunc

; Mail UDF

Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)

Local $objEmail = ObjCreate("CDO.Message")
 $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
 $objEmail.To = $s_ToAddress
 Local $i_Error = 0
 Local $i_Error_desciption = ""
 If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
 If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
 $objEmail.Subject = $s_Subject
 If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
 $objEmail.HTMLBody = $as_Body
 Else
 $objEmail.Textbody = $as_Body & @CRLF
 EndIf
 If $s_AttachFiles <> "" Then
 Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
 For $x = 1 To $S_Files2Attach[0]
 $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
;~ ConsoleWrite('@@ Debug : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console

If FileExists($S_Files2Attach[$x]) Then
 ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
 $objEmail.AddAttachment($S_Files2Attach[$x])
 Else
 ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
 SetError(1)
 Return 0
 EndIf
 Next

EndIf
 $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
 If Number($IPPort) = 0 Then $IPPort = 25
 $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
 ;Authenticated SMTP
 If $s_Username <> "" Then
 $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
 $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
 $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
 EndIf
 If $ssl Then
 $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
 EndIf
 ;Update settings
 $objEmail.Configuration.Fields.Update
 ; Set Email Importance
 Switch $s_Importance
 Case "High"
 $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "High"
 Case "Normal"
 $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
 Case "Low"
 $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
 EndSwitch
 $objEmail.Fields.Update
 ; Sent the Message
 $objEmail.Send
 If @error Then
 SetError(2)
 Return $oMyRet[1]
 EndIf
 $objEmail = ""
EndFunc ;==>_INetSmtpMailCom

; Com Error Handler

Func MyErrFunc()
 $HexNumber = Hex($oMyError.number, 8)
 $oMyRet[0] = $HexNumber
 $oMyRet[1] = StringStripWS($oMyError.description, 3)
 ConsoleWrite("### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet[1] & @LF)
 SetError(1); something to check for when this function returns
 Return
EndFunc ;==>MyErrFunc