Monday 5 November 2007

Monitoring Application Pools and Restarting them

Hi,

I was trying to find an application that would monitor the state of application pool on an IIS server but didn't have any luck. Instead I decided to write a simple .net page that would loop through all the app pools and if it's down, it would restart it and email me to say it's down. I will extent this to actually monitor SharePoint sites and see if they are down and if so, to also notify me.

Here's the code:
=======================================

Partial Class _Default
Inherits System.Web.UI.Page
Dim LogDir = "C:\AppPoolLog"
Dim ToAddress = "notify@someemailaddress.com"
Dim FromAddress = "system@someemailaddress.com"

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
showAppPools() 'Runs showAppPools Sub
End Sub

Sub showAppPools()
Dim obj, apool
obj = GetObject("IIS://localhost/W3SVC/apppools")
For Each apool In obj
apppoolStatus(apool.name)
Next
obj = Nothing
End Sub

' This will get the status of the app pool
Sub apppoolStatus(ByVal apppool)
Dim obj
obj = GetObject("IIS://localhost/W3SVC/apppools/" & apppool)
Select Case obj.apppoolstate
Case 0
writetoLog(apppool, "0") 'Do not know what this status is
Case 1
writetoLog(apppool, "1") 'Do not know what this status is
Case 2
writetoLog(apppool, "Up")
Case 3
writetoLog(apppool, "3") 'Do not know what this status is
Case 4
writetoLog(apppool, "DOWN")
startapppool(apppool)
emailSendCDOSYS(apppool)
writetoLog(apppool, "Restarted")
End Select
obj = Nothing
End Sub

' This will write information to a log file
Sub writetoLog(ByVal apppool, ByVal response)
Dim fso
Dim lday = DatePart("d", Now())
Dim lmonth = DatePart("m", Now())
Dim lyear = DatePart("yyyy", Now())
Dim logdate = lmonth & lday & lyear
fso = CreateObject("Scripting.FileSystemObject")
Dim objFSOwriteline = fso.OpenTextFile(LogDir & "\" & logdate & ".log", 8, True)
objFSOwriteline.WriteLine(response & "," & apppool & "," & now())
objFSOwriteline.close()
objFSOwriteline = Nothing
fso = Nothing
End Sub

' This starts the down app pool
Sub startapppool(ByVal apppool)
Dim obj
obj = GetObject("IIS://localhost/W3SVC/apppools/" & apppool)
obj.start()
obj = Nothing
End Sub

' This send a email regarding the down/restarted app pool
Sub emailSendCDOSYS(ByVal apppool)
Dim Mailer
Dim sch = "http://schemas.microsoft.com/cdo/configuration/"
Dim cdoConfig = Server.CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(sch & "sendusing") = 2 ' cdoSendUsingPort
.Item(sch & "smtpserver") = "smtp.someemailaddress.com"
.update()
End With
Mailer = CreateObject("CDO.Message")
Mailer.Configuration = cdoConfig
Dim Message = "===============================================" & vbCrLf
Message = Message & "Application Pool: " & apppool & " has been restarted" & VbCrLf
Message = Message & "Date/Time: " & now() & "" & VbCrLf
Message = Message & "===============================================" & VbCrLf
Dim Subject = "Application Pool: " & apppool & " has been restarted"
Mailer.TextBody = Message
Mailer.Subject = Subject
Mailer.To = ToAddress
Mailer.From = FromAddress
Mailer.Send()
Mailer = Nothing
End Sub

End Class

==================================

Once I modify it to monitor SharePoint sites, I will upload the code.

2 comments:

Alejandro Loaiza Arango said...

Thats' VB6 right? could you post the project please?, i need to do the same thing at my company. thank you.

Fadi Noja said...

Thats VB.net. I will upload the project for you shortly. I've extended it now to monitor HDD space and specific services also.