Click an Ad

If you find this blog helpful, please support me by clicking an ad!

Monday, March 3, 2014

What if your Monitoring Server Goes Down? Monitoring a website via Powershell.

Shortly after I got my monitoring server (PRTG) up and running, I started looking around for things to monitor.
"If X happens, will we be notified?"

My monitoring server was a VM. My VMware cluster runs on a SAN. Oops!

I moved PRTG to a physical box, with appropriate redundancies (RAID10, Power Supplies, and power source). NOW if the SAN or my VMware cluster fails I'll know about it.

WAIT! What if my email system is down? I verified that I had set PRTG to use it's built-in SMTP server. I ALSO set up a notification that goes to IT's personal email addresses in case the email server goes down, because you can't get the notification emails if they're not delivered to the mailbox on the server that's down.

I also set up a Powershell script to check that the http website of the monitoring server is up. I have this script scheduled on my "General Purpose IT Server" (it has all the admin software, and runs all of my scheduled tasks) to check it every 5 minutes:

#--------------------BEGIN SCRIPT---------------------------

#The URL of the main page you want to monitor
$URL = "http://MonitoringServer:8080/index.htm"

#The hostname of your monitoring server (used in the email later on)
$URL_Server = "MONITOR"

#Set up a web client connection
$webclient = New-Object Net.WebClient

#Download the text of the html code for the URL
$webtext = ($webclient.DownloadString($URL) | out-string)

#Copy that over to a temporary text file for later
$webtext | out-file c:\temp\PRTGWebsite.txt

#If you are making this for YOUR site, stop here and go look at the text file, then identify some unique text.
#Here, specify the search string. Put in the unique text you found.
$SearchString = 'Paessler AG - The Network Monitoring Company'

#Initialize the response variable
$Response = "FALSE"

#Search for the string in the text file. If it's there, change the response to true.
if (select-string -Path C:\Temp\PRTGWebsite.txt -Pattern $SearchString){
$Response = "TRUE"}

#If the response is NOT true, then the script sends email, because something's wrong with your site
If ($Response -notlike "TRUE"){
$body = "$URL on $URL_Server is down!"
Send-Mailmessage -from helpdesk@contoso.com -to ITSupport@contoso.com -subject "$URL on $URL_Server is down!" -smtpserver SMTPServer.contoso.com -body $body
} #end if

#Delete the temp file you used earlier
del c:\temp\PRTGWebsite.txt

#--------------------END SCRIPT---------------------------

We had this script go off after an upgrade because the HTML code had changed. We just needed to manually run the code (above) down to where it created the temporary text file, look for a new search string, and update the script.

No comments:

Post a Comment