BranchSiteMonitoring.vbs

Branchmonitoring.vbs
--------------------------------------------------------------------

Dim f1, f2, fso, i, WshShell, oExec, var1, var2, var3, tmpf, rmsSrv, oAPI
Dim monitorscriptFolder, groupMMscriptPath, powershellPath, MaxSize, fold
Dim routerUp, tmpvar1
Const ForReading = 1, ForWriting = 2, ForAppending = 8

' ***Enter your environment-specific parameters (below are examples)***

' Your RMS Server
rmsSrv = "RMS"
' Folder path where you placed the BranchSiteMonitoring script
monitorscriptFolder = "C:\Scripts\"
' Full path to GroupMM.ps1 script
groupMMscriptPath = "C:\Scripts\GroupMM.ps1"
' Full path to Powershell executable
powershellPath = "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe"
' Event ID to log if router is unavailable
badEvt = 18041
' Event ID to log if router is available
goodEvt = 18040

' ***No edits needed below this point***

Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.OpenTextFile(monitorscriptFolder & "\input.txt", ForReading)
Set f2 = fso.OpenTextFile(monitorscriptFolder & "\results.txt", ForAppending, TRUE)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oAPI = CreateObject("MOM.ScriptAPI")

var1 = f1.ReadAll
var2 = Split(var1, vbcrlf)
f2.Writeline date & "_" & time & ": " & "Begin branch site availability check..."
routerUp = 0
For i = 0 To UBound(var2)
var3 = split(var2(i), "~")
f2.writeline vbtab & "Checking router for " & _
var3(1) & " branch site."
tmpvar1 = 0
If Not Reachable(var3(0)) Then ' first ping check
tmpvar1 = tmpvar1 + 1
wscript.sleep 5000 ' wait ~5 seconds
If Not Reachable(var3(0)) Then ' second ping check
tmpvar1 = tmpvar1 + 1
End If
End If
If tmpvar1 = 2 then ' 2 pings failed...router down...take action
routerUp = 1
Call oAPI.LogScriptEvent("BranchSiteMonitoring.vbs", badEvt + i, 1, "Branch " & _
"site router for the " & var3(1) & " computer group is unavailable.  " & _
"Computers in this group should be in maintenance mode currently.")
If Not fso.FileExists(monitorscriptFolder & var3(1) & ".txt") Then
Set oExec = WshShell.Exec(powershellPath & " " & groupMMscriptPath & _
" -groupName:'" & var3(1) & "' -hours:10 -rmsServerName:'" & _
rmsSrv & "' -startMM:$true")
Set tmpf = fso.OpenTextFile(monitorscriptFolder & var3(1) & ".txt", _
ForWriting, TRUE)
Set tmpf = Nothing
Set oExec = Nothing
f2.writeline vbtab & "***" & var3(1) & " was just put " & _
"into maintenance mode***"
Else
f2.writeline vbtab & "***" & var3(1) & " is still in " & _
"maintenance mode***"
End If
Else
If fso.FileExists(monitorscriptFolder & var3(1) & ".txt") Then
fso.DeleteFile(monitorscriptFolder & var3(1) & ".txt")
Set oExec = WshShell.Exec(powershellPath & " " & groupMMscriptPath & _
" -groupName:'" & var3(1) & "' -hours:10 -rmsServerName:'" & _
rmsSrv & "' -startMM:$false")
Set oExec = Nothing
f2.writeline vbtab & var3(1) & " has been taken out of " & _
"maintenance mode."
End If
f2.writeline vbtab & var3(1) & " router is reachable on the " & _
"network."
End If
Next
If routerUp <> 1 Then
Call oAPI.LogScriptEvent("BranchSiteMonitoring.vbs", goodEvt, 4, "All branch " & _
"site routers are available")
End If
f2.Writeline date & "_" & time & ": " & "End branch site availability check..." & vbcrlf

' Clean up file when it gets larger than MaxSize (2 MB)
MaxSize = 2048000
Set fold = fso.GetFolder(monitorscriptFolder)
For Each fil In fold.Files
If LCase(fil.Name) = "results.txt" Then
If fil.Size > MaxSize Then
Compress monitorscriptFolder & fil.Name, monitorscriptFolder & "results_" & GetReadableDate() & ".zip"
f2.close
FSO.DeleteFile fil, True
End If
End If
Next

' End main script, begin functions and subs.
Function Reachable(strComputer)
Dim wmiQuery, objWMIService, objPing, objStatus

wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery)

For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
Reachable = False 'if computer is unreacable, return false
Else
Reachable = True 'if computer is reachable, return true
End If
Next
End Function

Sub Compress(Input, ZipFile)
Dim Shell : Set Shell = CreateObject("Shell.Application")
Dim FSO : Set FSO = CreateObject("Scripting.fileSystemObject")
FSO.CreateTextFile(ZipFile, true).WriteLine "PK" & Chr(5) & Chr(6) & String(18, 0)
Set ZipFile = Shell.NameSpace(ZipFile)
ZipFile.CopyHere Input
Do Until ZipFile.items.Count = 1
'important, makes the script not fall out and dispose of objects before they are done
'items.count is the amount of root items you anticipate to be in the zip file
        wscript.sleep 100
Loop
Set Shell = Nothing
Set FSO = Nothing
Set ZipFile = Nothing
End Sub

Function GetReadableDate()
Dim strYear, strMonth, strDay
strYear  = Right(DatePart("yyyy",Date), 4)
strMonth = Right(100+DatePart("m",Date),2)
strDay   = Right(100+DatePart("d",Date),2)
GetReadableDate = strYear & strMonth & strDay
End Function

No comments:

Post a Comment

analytics