Wednesday, March 22, 2006

WMI Monitoring Script

I came into a situation where there are several Exchange servers without any monitoring. While software is procured, I created the following script to do some basic monitoring of Exchange services and disk space (to make sure circular logging doesn't kill the server). I have the script running as a scheduled task every 15 minutes. The script will create a log file every time it runs. If one of the thresholds is reached, an email is sent.

On Error Resume Next
Const ForAppending=8
Const ForReading=1
Const ForWritting=2
Dim strComputer
Dim objWMIService
Dim propValue
Dim objItem
Dim SWBemlocator
Dim UserName
Dim Password
Dim colItems


'Create Log file
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = "C:\WMI Monitoring\"
strFileName = "server_status" & Hour(Now) & Minute(Now) & ".log"
strFullName = objFSO.BuildPath(strPath, strFileName)
Set objFile = objFSO.CreateTextFile(strFullName)
objFile.Close
Set objFile = objFSO.OpenTextFile(strFullName, ForWritting)
'Build array of servers
arrServers = Array("exchange01", "exchange02")

'username and password
strUserName = "Administrator"
strPassword = "Password1"

For Each strComputer In arrServers
Err.Clear
'WScript.Echo strComputer
ObjFile.writeline "===================================="
ObjFile.writeline "Computer: "& strComputer
ObjFile.writeline "===================================="
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",strUserName,strPassword)
If Err.Number = "-2147023174" Then
strAlertItem = Err.Description
strAlertThreshold = "!!"
Call SendAlert(strComputer, strAlertItem, strAlertThreshold)
Err.Clear
End If
If Err.Number <> 0 Then
objFile.WriteLine "Error Connecting: " & Err.Number & " " & Err.Description
Err.Clear
End If


'*****************************************************************************************************
'Check Logical Disk
''*****************************************************************************************************
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk",,48)
objfile.WriteLine "Checking Free Disk Space"
For Each objItem In colItems
If InStr(objItem.Description, "Fixed Disk") Then
strAlertItem = objItem.DeviceID & ", " & objItem.Description
intFreeSpace = objItem.FreeSpace
intFreeSpace = intFreeSpace/1048576
strAlertThreshold = "Free SPace: " & CLng(intFreeSpace) & " MB"
'If there are less than 200 MB of Free Disk Space then send out an alert
If intFreeSpace < 200 Then
Call SendAlert(strComputer, strAlertItem, strAlertThreshold)
End If
objfile.WriteLine strAlertItem
objFile.WriteLine strAlertThreshold
objFile.writeline " "
End If
Next
'*****************************************************************************************************
'Check Status of Services
'*****************************************************************************************************
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)
objfile.WriteLine "Checking Exchange Services"
For Each objItem in colItems
If InStr(objItem.Displayname, "Exchange") Then
If InStr(objItem.Displayname, "Sync") Then
'WScript.Echo objItem.DisplayName
ElseIf InStr(objItem.Displayname, "Lotus") Then
'WScript.Echo objItem.Displayname
ElseIf InStr(objItem.Displayname, "Mailbox Manager") Then
'WScript.Echo objItem.Displayname
Else
objfile.WriteLine "DisplayName: " & objItem.DisplayName
objfile.WriteLine "Name: " & objItem.Name
objfile.WriteLine "State: " & objItem.State
objfile.WriteLine "Status: " & objItem.Status
objfile.WriteLine " "
If objItem.State = "Stopped" Then
strAlertItem = objItem.Name & ":"
strAlertThreshold = objItem.State
Call SendAlert(strComputer, strAlertItem, strAlertThreshold)
End If
End If
End If
Next
strAlertItem = " "
strAlertThreshold = " "
Set objWMIService = Nothing
Next
objfile.Close
Set objFSO = Nothing


'*****************************************************************************************************
'Send Alerts Via Email
'*****************************************************************************************************
Function SendAlert(strComputer, strAlertItem, strAlertThreshold)
'WScript.Echo "Sent Alert"
Set objEmail = CreateObject("CDO.Message")
objEmail.From = strComputer & "@company.org"
objEmail.To = "teo@inventrix.net;5551212@pager.net"
objEmail.Subject = "Server Alert"
strText = strComputer & " is having the following problems: " & strAlertItem & strAlertThreshold
objFile.WriteLine "********************** ALERT SENT ********************************"
objEmail.TextBody = strText
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "nsmail01"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End Function