Checking server status via ASP.net

  • Thread starter Thread starter darrel
  • Start date Start date
D

darrel

I swear I've read you can do this somewhere before, but don't recall where
or what it'd even be called to do the correct googling.

What I've been asked to do is make a 'dashboard' app that checks the status
of various servers. If they are down, show an error.

I found/modified this snippet:

---

Try
Dim req As Net.HttpWebRequest = Net.WebRequest.Create("your url")
Dim res As Net.HttpWebResponse = req.GetResponse()
Dim str As IO.Stream = res.GetResponseStream()
Return New IO.StreamReader(str).ReadToEnd()
Catch ex As Exception
Return ex.Message
End Try
End Function

---

2 issues:

1) When I try to call the function ala:
<%=checkURL("http://google.com")%>

I get this error: "Invalid URI: The format of the URI could not be
determined."

Unfortunately, that appears to be a common SharePoint error, so Google
results are a bit cluttered.

2) Even if this does work, will it return anything if the server times out
(the main thing we're looking to check for?

-Darrel
 
I hope you changed this line
Dim req As Net.HttpWebRequest = Net.WebRequest.Create("your url")
"your url" suppose to be whatever you passing to checkURL

George.
 
I hope you changed this line
Dim req As Net.HttpWebRequest = Net.WebRequest.Create("your url")
"your url" suppose to be whatever you passing to checkURL

You have my permission to walk over here and smack me upside the head for
such a stupid mistake! ;o)

I am completely embarrassed by that one. ;o)

OK, so, that works! Now, to followup...

That returns the actual page. What I need to do is check for specific
errors. For instance, if it can't access the page at all, I get a "Unable to
connect to the remote server".

Is there a master list of these types of errors out there to reference
against?

Or, in general, is this even a good way to go about testing for the status
of a server?

-Darrel
 
the way I do it is to make custom page.
That connects to database, checks hard-drive space,.....
Basically does everything that needs to be checked. And outputs it as HTML.
Something like
Empty Space:1Gb<br>
Database: OK

Then your code simply displays it.

So you have for example
<%=checkURL("http://google.com/test.aspx")%>

And your checkURL will do folowing (pseducode, do not know VB well)

Function CheckUrl(sUrl) as String
Dim req As Net.HttpWebRequest
Dim res As Net.HttpWebResponse
Dim str As IO.Stream
Try
req = Net.WebRequest.Create("sUrl")
res = req.GetResponse()
str = res.GetResponseStream()
Return New IO.StreamReader(str).ReadToEnd()
Catch ex As WebException
ex.Response.Dispose()
Return "Connected to server but got an error: " & ex.Message
Catch e As Exception

Return "Could not connect to server: " & e.Message
Finally
if( str Not Is Nothing) Then str.Dispose()
if( res Not Is Nothing) Then res.Dispose()
End Try
End Function


PS: Do not forget to properly close Dispose objectes. You will run out of
resources soon otherwise.


But it only works if you can create your custom test.aspx page on the
server.
If not, you are limmited to was able to connect to server/was not able
to.....

George.
 
But it only works if you can create your custom test.aspx page on the
server.
If not, you are limmited to was able to connect to server/was not able
to.....

Good idea, though we really don't need anything that robust.

I found some suggestions one being to load the page, regex out the TITLE
tag, and compare it to what you think it should be. If it's different, then
either an error page loaded, or an error was returned.

The one unknown is what if the server just doesn't respond? I assume you'd
get some sort of time-out error. I'm just not quite sure how to test that
particular aspect yet. I'll have to wait for one of our servers to go down
;)

-Darrel
 
Also, to run this kind of request in your sharepoint server

I'm avoiding putting anything in SharePoint these days. ;0)

-Darrel
 
Back
Top