We have an internal IIS server that hosts several websites that redirects to external hosted web applications. Occasionally people will do nutty things like restart servers, stop services, unplug cables, you name it it happens.... I want to verify that our internal IIS webserver is up and running every 15 minutes, and if not try to restart the service and send me an email.
I wrote this powershell script, but am not sure if this is the best way to accomplish this?
###################################################################
# Summary: Verify W3SVC service is running, send email if it's down
###################################################################
##############################
# Get service status
##############################
$serviceName = "W3SVC"
$serverName = hostname
$status = (Get-Service $serviceName).Status
if ($status -ne "Running"){
sendMail "$serviceName" "$serverName" #sendmail is a function (not shown)
Restart-Service $ServiceName
}else{
# Service is running, do nothing;
}
However Im not sure if W3SVC
is the best service to check that IIS7 is up? Do I need to check WAS
and IISADMIN
as well? Is there one that will accomplish everything?
I also thought about checking the HTTP status code for these hosts, but even if the site is down and IIS is up, my return code comes back 200
. This was more of a brain storming idea, are there better "best practices" to accomplish something like this?
$url = "http://intranet_site"
$xHTTP = new-object -com msxml2.xmlhttp;
$xHTTP.open("GET",$url,$false);
$xHTTP.send();
$xHTTP.status # returns the status code
if ($xHTTP.status -ne "200"){
sendMail "$serviceName" "$serverName"
Restart-Service $ServiceName
}
This was more of a brain storming idea, are there better "best practices" to accomplish something like this? I want to verify that the site is up and the redirects are working.