VB Script for IIS Health Check

Dim tempstatus
Dim spaced
Dim usernamea
Dim username
Dim sFile, dFSO
Dim serverstatus
Dim userid,tempuserid
Dim outmem
Dim wql
Dim StrQuery
Dim Htout
Dim Appstatus1
Dim Appstatus2

serverstatus="Normal"
userid=0
tempuserid=0

On Error Resume Next
Set objNTInfo = CreateObject("WinNTSystemInfo")
serverName = lcase(objNTInfo.ComputerName)
Set objNTInfo = nothing

Htout=Htout+"<table border=1 bgcolor='#000080' width=100%><tr><th align=left>"_
             + FormatHeaderColumn("Production ICE Web Heath Check") +"</th></tr></table><table border=1 bgcolor='#000080' width=100%><tr><th align=left>"_
             + FormatHeaderColumn("Server-Name") + "</th><th align=left >" +  FormatHeaderColumn("AppPools-Status")_
             + "</th><th align=left >" + FormatHeaderColumn("W3wp-Usage") + "</th><th align=left >" _
             + FormatHeaderColumn("Connection-Status") + "</th><th align=left >" + FormatHeaderColumn("OOMemory-Error") _
             + "</th><th align=left >" + FormatHeaderColumn("AppRelated-Error") + "</th><th align=left >" _
             + FormatHeaderColumn("Status") + "</th></tr><tr><td valign=top bgcolor='#F3F1F8'>"+ FormatRowColumn(serverName) +"</td><td valign=top bgcolor='#F3F1F8'>"

'Htout=Htout+"<td valign=top bgcolor='#F3F1F8'>"+ FormatRowColumn(serverName) +"</td><td valign=top bgcolor='#F3F1F8'>"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Security)}!\\" & strComputer & "\root\cimv2")


Htout=Htout+"<table border=1 cellspacing=0 width=100% bgcolor='#F3F1F8'><tr><th>" + FormatRowColumn("AppPools") +"</th><th>" + FormatRowColumn("Status") +"</th></tr>"
set obj = GetObject("IIS://localhost/W3SVC/apppools")

Appstatus1=""
Appstatus2=""
for each apool in obj
   tempstatus = apppoolStatus(apool.name)
   if StrComp ("Running",tempstatus)= 0 then
      Appstatus1 = Appstatus1+"<tr><td valign=top>"& FormatRowColumn(apool.name) &"</td><td valign=top>"& FormatRowColumn(tempstatus) +"</td></tr>"
   else
      Appstatus2 = Appstatus2+"<tr><td valign=top>"& FormatRowColumn(apool.name) &"</td><td valign=top>"& FormatRowColumn(tempstatus) +"</td></tr>"
   end if
Next
set obj = nothing

Htout=Htout+Appstatus2+Appstatus1+"</table>"

Htout=Htout+"</td><td valign=top bgcolor='#F3F1F8'>"
processstatus()

Htout=Htout+"</td><td valign=top bgcolor='#F3F1F8'>"



Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("E:\Support\Tools\webhealthcheck\iiscount.cfg", ForReading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrL = Split(strNextLine , ":")
    Htout=Htout & "<u>" & FormatRowColumn(arrL(0)) &"</u><br>"
    arrl1= Split(arrL(1) , ",")
    For count=0 to Ubound(arrL1)
        Htout =Htout & FormatRowColumn(FormatRowColumn(arrL1(count))) &"="
        call ExecuteWithTerminalOutput("cmd.exe /c netstat -an | findstr ESTAB | findstr "& arrL1(count))
    Next
Loop
objTextFile.Close
Set objFSO=nothing

Htout=Htout+"</td>"


tempt= DateAdd("n",-30,Now)

Htout=Htout+"</td><td valign=top bgcolor='#F3F1F8'><table cellspacing=0 cellpadding=0 bgcolor='#F3F1F8'><tr>"
outmem=0
Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent " & "Where Logfile = 'Application' and type='Error' and EventCode=1334 and TimeWritten > '"& tempt &"'")
  For Each objEvent in colLoggedEvents
   Htout=Htout &"<td>" & FormatRowColumn(objEvent.TimeWritten) &"</td><td><font color=red>" & FormatRowColumn(objEvent.Message) & "</font></td></tr>"
   outmem=outmem+1
  Next


if outmem <= 0 then
 Htout=Htout+"<td bgcolor='#F3F1F8'>" + FormatRowColumn("No Error") +"</td></tr></table>"
else
  Htout=Htout+"</table>"
  serverstatus="<font color=red>Critical</font>"
end if


Htout=Htout+"</td><td valign=top bgcolor='#F3F1F8'><table cellspacing=0 cellpadding=0 bgcolor='#F3F1F8'><tr>"

evelog=0
Const ForReadings = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("E:\Support\Tools\webhealthcheck\EventError.cfg", ForReadings)
Do Until objTextFile.AtEndOfStream
    StrQuery="Select * from Win32_NTLogEvent "
    strNextLine = objTextFile.Readline
    arrL = Split(strNextLine , ":")
    StrQuery= StrQuery+"Where Logfile = '"+arrL(0)+"' "
     if  isNull(arrL(1)) or arrL(1) <>"" then
      StrQuery= StrQuery+"and EventCode="+arrL(1)
     end if
     if  isNull(arrL(2)) or arrL(2) <>"" then
      StrQuery= StrQuery+" and Message like '%"+arrL(2)+"%'"
     end if
    Set colLoggedEvents = objWMIService.ExecQuery (StrQuery)
    For Each objEvent in colLoggedEvents
     Htout=Htout &"<td bgcolor='#F3F1F8'>" & FormatRowColumn(objEvent.TimeWritten) &"</td><td><font color=red>" & FormatRowColumn(objEvent.Message) & "</font></td></tr>"
      evelog=evelog+1
    Next
Loop
objTextFile.Close
Set objFSO=nothing

if evelog <= 0 then
  Htout=Htout+"<td bgcolor='#F3F1F8'>" + FormatRowColumn("No Error") + "</td></tr></table>"
else
  Htout=Htout+"</table>"
  serverstatus="Critical"
end if


Htout=Htout+"</td><td valign=top bgcolor='#F3F1F8'>"+ FormatRowColumn(serverstatus) +"</td></tr>"
WScript.Echo Htout

Function ExecuteWithTerminalOutput(cmd)
Set sh = WScript.CreateObject("WScript.Shell")
 Set exec =  sh.Exec(cmd)
 a = 0
 Do While exec.Status = 0
 varo=exec.StdOut.ReadLine()
 if  isNull(varo) or varo <>"" then
      a= a + 1
 end if
 Loop
 ExecuteWithTerminalOutput = exec.Status
 Htout =Htout & FormatRowColumn(a) & "<br>"
set sh = nothing
End Function


Function apppoolStatus(apppool)
Dim obj
set obj = GetObject("IIS://localhost/W3SVC/apppools/" & apppool)
 Select Case obj.apppoolstate
Case 0
apppoolStatus="<font color=red>verify the status</font>"
serverstatus="<font color=red>Critical</font>"
Case 1
apppoolStatus="<font color=red>verify the status</font>"
serverstatus="<font color=red>Critical</font>"
Case 2
apppoolStatus="Running"
Case 3
apppoolStatus="<font color=red>verify the status</font>"
serverstatus="<font color=red>Critical</font>"
Case 4
apppoolStatus="<font color=red>DOWN</font>"
                        serverstatus="<font color=red>Critical</font>"

 End Select
set obj = nothing
End Function

Function processstatus()
Dim processusage
Htout=Htout+"<table border=1 cellspacing=0 width=100% bgcolor='#F3F1F8'><tr>"
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process WHERE Name = 'w3wp.exe'")
  Htout=Htout+"<th>" + FormatRowColumn("UserName") +"</th><th>" + FormatRowColumn("ProcID") +"</th><th>" + FormatRowColumn("PUsage") +"</th></tr>"
For Each objProcess in colProcessList
    colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)
   
      if objProcess.PageFileUsage > 1500000 then
         serverstatus="<font color=red>Critical</font>"
         processusage="<font color=red>" & objProcess.PageFileUsage & "</font>"
      else
         processusage=objProcess.PageFileUsage
      end if
      Htout=Htout & "<tr><td>" & FormatRowColumn(strNameOfUser) &" </td><td> " & FormatRowColumn(objProcess.ProcessID) &" </td><td> " & FormatRowColumn(processusage)  &"</td><tr>"
Next
Htout=Htout+"</table>"
set colProcessList = nothing
End Function

Function Convert2DMTFDate(dDate)
Dim sTemp
dDate=dDate
sTemp = cstr(year(dDate))+right("00"+cstr(month(dDate)),2)+right("00"+cstr(day(dDate)),2)
sTemp = sTemp & " "+right("00"+cstr(hour(dDate)),2)+":"+right("00"+cstr(minute(dDate)),2)
Convert2DMTFDate = sTemp
End Function


Function FormatRowColumn(colname)
Dim sTemp
sTemp =  "<b><font face='Calibri' size='2' color='black'>" &  colname &  "</font></b>"
FormatRowColumn = sTemp
End Function

Function FormatHeaderColumn(colname)
Dim sTemp
sTemp =  "<b><font face='Calibri' size='4' color='#FFFFFF'>" &  colname &  "</font></b>"
FormatHeaderColumn = sTemp
End Function

Comments

Popular posts from this blog

Base 64 encoding and decoding

LINQ Queries with GROUP BY, INNER JOIN, COUNT and SUM: Examples

How to write Custom delete Confirmation Modal for Kendo Grid in MVC: