计划任务 自动 服务器检查 的vbs脚本

strDebug = "0"
If strDebug = "0" Then


SearchServer "ServerName1","[email protected]","[email protected]"
SearchServer "ServerName2","[email protected]","[email protected]"

End If


'Main Function
Function SearchServer(strServer, strRecipient, strCC)


strBody="<hr><br><br><b style=""color:red;font-size:16px"">[ " & ucase(strServer) & " ]</b><br><br>"
'Get Disk Information
strBody = strBody & GetDiskInfo(strServer)


'Get Service Status
strBody = strBody & GetServiceStatus(strServer)

'Get Event Log
strBody = strBody & "<BR><b style=""color:blue;font-size:14px"">Event Log</b><BR><Table border=1><TR bgcolor=""plum""><TD>Type</TD><TD>Event Type</TD>" & _
"<TD>Source Name</TD><TD>Event Code</TD><TD>Record Number</TD>" & _
"<TD>Time Written</TD><TD>Message</TD><TD>User</TD></TR>" & VBCRLF 


strComputer = strServer
strType = "Application"
strLog = GetLog (strComputer,strType)
strBody = strBody & strLog 
strType = "System"
strLog = GetLog (strComputer,strType)
strBody = strBody & strLog & "</Table>"
'WScript.Echo strBody
'exit function
'Notify the administrator of this server.
SendMail replace(strBody,"'",""""),"Server Daily Information - " & replace(strRecipient,"@Qisda.com",""), strRecipient, strCC
End Function


Function SendMail(strBody, strSubject, strRecipient, strCC)
Set conMail=CreateObject("ADODB.Connection")
conMail.open "Provider=SQLOLEDB.1;Data Source=*****;Initial Catalog=MailService;User ID=*****;Password=*****"
sendTime=cstr(year(now)) & "-" & cstr(month(now)) & "-" & cstr(day(now)) & " 8:00:00"
conMail.execute "exec SP_MS_MAIL_ADD 'ServerInfo','Mail','','"+strRecipient+"','"+strCC+"','','"+strSubject+"','"+strBody+"','','M','"+sendTime+"','ServerInfo','<head><meta http-equiv=Content-Type content=""text/html; charset=gb2312""><style>html,body,td,b,p { font-family:Verdana; font-size:11px; }</style></head>'"
conMail.close
End Function


Function GetDiskInfo(strServer)
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
   ("Select * from Win32_LogicalDisk Where DriveType = 3")

strTemp = "<BR><b style=""color:blue;font-size:14px"">Disk Info</b><BR><Table border=1><TR bgcolor=""plum""><TD>Disk ID</TD><TD align=right>Total Size</TD><TD align=right>Free Size</TD><TD align=right>Percent of Free Space</TD></TR>" & VBCRLF
For Each objDisk in colDisks
   intFreeSpace = round(objDisk.FreeSpace / 1024 / 1024 / 1024, 1)
   intTotalSpace = round(objDisk.Size / 1024 / 1024 / 1024, 1)
   pctFreeSpace = intFreeSpace / intTotalSpace
   
   strBackgrand = ""
   If pctFreeSpace < 0.3 Then strBackgrand = "bgcolor=""#99cc66"""
   If pctFreeSpace < 0.2 Then strBackgrand = "bgcolor=""#ff9999"""
   strTemp = strTemp & "<TR " & strBackgrand & "><TD>" & objDisk.DeviceID & "</TD><TD>" & intTotalSpace & "G</TD><TD>" & _
    intFreeSpace & "G</TD><TD>" & FormatPercent(pctFreeSpace) & "</TD></TR>" & VBCRLF
Next
strTemp = strTemp & "</Table>"
GetDiskInfo = strTemp
End Function




Function GetLog(strComputer,strType)
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate,(Security)}!\\" & _
       strComputer & "\root\cimv2")
'EventType = 1 Error EventType = 2 Warning
Set colLoggedEvents = objWMIService.ExecQuery _
   ("Select * FROM Win32_NTLogEvent WHERE Logfile = '" & strType & "' " & _
       "AND (EventType =1 or EventType = 2) AND TimeGenerated > '" & GetYestoday & "' ")
max=1
For Each objEvent in colLoggedEvents
   dtmEventDate = objEvent.TimeWritten
   strTimeWritten = WMIDateStringToDate(dtmEventDate)

strBackgrand = ""
If (LCase(objEvent.Type) = "error" or objEvent.Type="错误") Then strBackgrand = "bgcolor=""ff9999"""


   strTemp = strTemp & "<TR " & strBackgrand & ">" & _
"<TD>" & strType & "</TD>" & "<TD>" & objEvent.Type & "</TD>" & _
  "<TD>" & objEvent.SourceName & "</TD>" & _
"<TD>" & objEvent.EventCode & "</TD>" & "<TD>" & objEvent.RecordNumber & "</TD>" & _
"<TD>" & strTimeWritten & "</TD>" & "<TD>" & left(objEvent.Message,200) & " ...</TD>" & _
"<TD>" & objEvent.User & "&nbsp;</TD></TR>" & VBCRLF

max=max+1
if (max>20) then exit for end if
Next
GetLog = strTemp
End Function




Function GetYestoday
    strYear=cstr(year(now()-1))
strMonth=cstr(month(now()-1))
if len(strMonth)=1 then strMonth="0" & strMonth
strDay=cstr(day(now()-1))
if len(strDay)=1 then strDay="0" & strDay
GetYestoday=strYear & strMonth & strDay
End Function


Function WMIDateStringToDate(dtmEventDate)
    WMIDateStringToDate = CDate(Mid(dtmEventDate, 5, 2) & "/" & _
        Mid(dtmEventDate, 7, 2) & "/" & Left(dtmEventDate, 4) _
            & " " & Mid (dtmEventDate, 9, 2) & ":" & _
                Mid(dtmEventDate, 11, 2) & ":" & Mid(dtmEventDate, _
                    13, 2))
End Function




Function GetServiceStatus(strServer)
Set objWMIService = GetObject("winmgmts:\\" & strServer & "\root\cimv2")

strTemp = "<BR><b style=""color:blue;font-size:14px"">Service Status</b><BR><Table border=1><TR bgcolor=""plum""><TD>Service Name</TD><TD> Status</TD></TR>"

Set colServiceList = objWMIService.ExecQuery _
   ("Select * from Win32_Service where Name ='MSSQLSERVER'")
i=0
For Each objService in colServiceList
strBackcolor = ""
If LCase(objService.State) = "stopped" Then strBackcolor = " bgcolor = ""#ff9999"""
strTemp = strTemp + "<TR" & strBackcolor & "><TD>MSSQLServer</TD><TD>" & objService.State & "</TD></TR>"
i=1
Next
if i=0 then strTemp = strTemp + "<TR><TD>MSSQLSERVER</TD><TD>No Service</TD></TR>"


Set colServiceList = objWMIService.ExecQuery _
   ("Select * from Win32_Service where Name ='SQLSERVERAGENT'")
i=0
For Each objService in colServiceList
strBackcolor = ""
If LCase(objService.State) = "stopped" Then strBackcolor = " bgcolor = ""#ff9999"""
strTemp = strTemp + "<TR" & strBackcolor & "><TD>" & objService.Name & "</TD><TD>" & objService.State & "</TD></TR>"
i=1
Next
if i=0 then strTemp = strTemp + "<TR><TD>SQLSERVERAGENT</TD><TD>No Service</TD></TR>"


Set colServiceList = objWMIService.ExecQuery _
   ("Select * from Win32_Service where Name ='W3SVC'")
i=0
For Each objService in colServiceList
strBackcolor = ""
If LCase(objService.State) = "stopped" Then strBackcolor = " bgcolor = ""#ff9999"""
strTemp = strTemp + "<TR" & strBackcolor & "><TD>World Wide Web Publishing Service</TD><TD>" & objService.State & "</TD></TR>"
i=1
Next
if i=0 then strTemp = strTemp + "<TR><TD>World Wide Web Publishing Service</TD><TD>No Service</TD></TR>" 

Set colServiceList = objWMIService.ExecQuery _
   ("Select * from Win32_Service where Name ='ntrtscan'")
i=0
For Each objService in colServiceList
strBackcolor = ""
If LCase(objService.State) = "stopped" Then strBackcolor = " bgcolor = ""#ff9999"""
strTemp = strTemp + "<TR" & strBackcolor & "><TD>OfficeScanNT RealTime Scan</TD><TD>" & objService.State & "</TD></TR>"
i=1
Next
if i=0 then strTemp = strTemp + "<TR bgcolor = ""#ff9999""><TD>OfficeScanNT RealTime Scan</TD><TD>No Service</TD></TR>" 

Set colServiceList = objWMIService.ExecQuery _
   ("Select * from Win32_Service where Name ='OracleOraHome92TNSListener'")
i=0
For Each objService in colServiceList
strBackcolor = ""
If LCase(objService.State) = "stopped" Then strBackcolor = " bgcolor = ""#ff9999"""
strTemp = strTemp + "<TR" & strBackcolor & "><TD>OracleOraHome92TNSListener</TD><TD>" & objService.State & "</TD></TR>"
i=1
Next
if i=0 then strTemp = strTemp + "<TR><TD>OracleOraHome92TNSListener</TD><TD>No Service</TD></TR>" 


GetServiceStatus = strTemp & "</Table>"
End Function

你可能感兴趣的:(计划任务 自动 服务器检查 的vbs脚本)