Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

This simple VBS script can be used to health monitor your infrastracture

This simple VBS script can be used to health monitor your infrastracture. Each monitored machine should have turn on RPC service.

The provided code should be saved as .vbs, e.g. HostsHealthMon.vbs

To call the script, run in CL this (or make cmd file to wrap this call):
cscript.exe HostsHealthMonitor.vbs>>\\YouRStorage\HostsHealtMon_Log.txt



Option Explicit

Dim Hosts, RootUser, RootPwd, forceAlarmCPU, forceAlarmHD, forceAlarmRAM, forceAlarmConnection
Dim ptr, partitionRedLimit, CPUUsageRedLimit, RAMFreeRedLimit, IterationDelay, resetCount
Dim lastCacheLogPath
Dim httpServerUrl

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description: The Sub is responsible for initialization and reseting (on beginning of each cycle) settings
' Populate this set of variables With your data as descibed in comments for each block to configure run
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub InitVars ()

'Machines for checking details: host name or IP, User Id and password
Hosts = Array("host1", "10.90.23.255", "host02", "host03", "Server01")
RootUser = Array("Admin", "User", "Admin", "User01", "TestUser")
RootPwd = Array("123west", "123west", "123west", "123west", "123west")

'Flags reseting
forceAlarmConnection = Array (True, True, True, True, True)
forceAlarmCPU = Array (True, True, True, True, True)
forceAlarmHD = Array (True, True, True, True, True)
forceAlarmRAM = Array (True, True, True, True, True)

'Threasholds
partitionRedLimit = "5"
CPUUsageRedLimit = "95"
RAMFreeRedLimit = "50"

'Settings for defining frequency of the reporting
IterationDelay = 100
resetCount = 5

'Only latest results will be stored in this file (no history). E.g. a host had a problem with CPU load in previous full iteration and now it soes not, then the log file will contain nothing about that
lastCacheLogPath = "\\hostStorage\HostsHealtMon_Cache.txt"
Call Delete_File ( lastCacheLogPath )

End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description: removes specified file
' Parameter sFile - name of folder to be removed
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Delete_File (ByVal sFile)
On Error Resume Next
Const c_Proc = "Delete_File"
Const c_DeleteReadOnly = True

Dim iResult, oFSO

' Define default error code
iResult = True

Set oFSO = CreateObject("Scripting.FileSystemObject")

' Check if specified folder exists
If Not oFSO.FileExists(sFile) Then
iResult = True

' The file found
Else

' Remove specified folder
Call oFSO.DeleteFile (sFile, c_DeleteReadOnly)

' Check operation result
If Err.Number <> 0 Then
Wscript.Echo "Can't remove the file '" &amp; sFile
iResult = False
Else
iResult = True
End If

End If ' The folder found
Set oFSO = Nothing
Delete_File = iResult

End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description: Writes in file
' Parameters: objFile - file object
' strText - text to write
' Result: True / False
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function FileWrite(ByRef sFile, ByVal sText)
Dim iResult
Dim objFSO, objFile
iResult = True
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")

Err.Clear
Set objFile = objFSO.OpenTextFile (sFile, ForAppending, True)

If Err.Number <> 0 Then
Wscript.Echo "Error appending to file " &amp; sFile
iResult = False
Err.Clear
Else
objFile.WriteLine sText
objFile.Close

End If

Set objFSO = Nothing
Set objFile = Nothing

FileWrite = iResult
End Function


Sub Main()

InitVars ()

Do While True
On Error Resume Next
ptr=0
resetCount = resetCount - 1
For Each Host In Hosts
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

Dim alarmStr, tmpStr
alarmStr = ""
tmpStr = ""

Err.Clear
Set objWMIService = objSWbemLocator.ConnectServer(Host, "root\CIMV2", RootUser (ptr), RootPwd (ptr), "MS_409")

If Err.Number <> 0 Then
'Wscript.Echo "Warning establishing RPC connection with " &amp; Host &amp; ": "&amp; Err.description
tmpStr = Date &amp; " " &amp; Time &amp; " " &amp; "Error: Unable to establish RPC connection with " &amp; Host &amp; ": "&amp; Err.description
If CBool(forceAlarmConnection(ptr)) = True Then
Wscript.Echo tmpStr
alarmStr = alarmStr &amp; " <br />" &amp; tmpStr
forceAlarmConnection(ptr) = False
End If
End If

'Wscript.Echo Date &amp; " " &amp; Time &amp; " " &amp; Host

'Show free space on logical disk
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_LogicalDisk",,48)
For Each objItem In colItems
'Wscript.Echo "FreeSpace: " &amp; objItem.FreeSpace
If objItem.FreeSpace <>"" Then
If CInt(Left(objItem.FreeSpace, Len(objItem.FreeSpace)- 9)) < CInt(partitionRedLimit) Then
tmpStr = Date &amp; " " &amp; Time &amp; " " &amp; "ALARM event - Free space on a partition at : " &amp; Host &amp; " = " &amp; Left(objItem.FreeSpace, Len(objItem.FreeSpace)- 9) &amp; " GBs"
If CBool(forceAlarmHD(ptr)) = True Then
Wscript.Echo tmpStr
alarmStr = alarmStr &amp; " <br />" &amp; tmpStr
forceAlarmHD(ptr) = False
End If
End If

'Else
' forceAlarmHD(ptr) = True
End If
Next

'Show CPU load percentage
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Processor",,48)
For Each objItem In colItems
'Wscript.Echo "CPU load on: " &amp; Host &amp; " = " &amp; objItem.LoadPercentage
If CInt(objItem.LoadPercentage) > CInt(CPUUsageRedLimit) Then
WScript.Sleep(3000)
If CInt(objItem.LoadPercentage) > CInt(CPUUsageRedLimit) Then 'Double check after 5 sec

tmpStr = Date &amp; " " &amp; Time &amp; " " &amp; "ALARM event - CPU usage on: " &amp; Host &amp; " = " &amp; objItem.LoadPercentage &amp; " %"

If CBool(forceAlarmCPU(ptr)) = True Then
Wscript.Echo tmpStr
alarmStr = alarmStr &amp; " <br />" &amp; tmpStr
forceAlarmCPU(ptr) = False
End If

End If

'Else
' forceAlarmCPU(ptr) = True
End If
Next

'Show Memory MB free space
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_PerfFormattedData_PerfOS_Memory",,48)
For Each objItem In colItems

If CInt(objItem.AvailableMBytes) < CInt(RAMFreeRedLimit) Then
WScript.Sleep(1000)
If CInt(objItem.AvailableMBytes) < CInt(RAMFreeRedLimit) Then 'Double check after 5 sec

tmpStr = Date &amp; " " &amp; Time &amp; " " &amp; "ALARM event - RAM free space: " &amp; Host &amp; " = " &amp; objItem.AvailableMBytes &amp; " MB"

If CBool(forceAlarmRAM(ptr)) = True Then
Wscript.Echo tmpStr
alarmStr = alarmStr &amp; " <br />" &amp; tmpStr
forceAlarmRAM(ptr) = False
End If

End If

End If
Next

'Wscript.Echo alarmStr
If alarmStr<>"" And alarmStr<>" <br />" Then
Call FileWrite(lastCacheLogPath, alarmStr)
End If

Set objSWbemLocator = Nothing
ptr = ptr + 1

Next

WScript.Sleep(IterationDelay)

If resetCount <= 0 Then
Call InitVars()
End If

Loop

End Sub

Call Main


This post first appeared on AT4QA, please read the originial post: here

Share the post

This simple VBS script can be used to health monitor your infrastracture

×

Subscribe to At4qa

Get updates delivered right to your inbox!

Thank you for your subscription

×