views:

164

answers:

5

Can you please advice on the best way to check if a server is running using java? I mean I want to check a site for downtime. Is connecting to the server and getting the status (if 200 returned then it's up!) the right way?

Won't this slow down the server in case connection is established and status checked every 60 seconds?

I'm currently connecting using HttpConnection and getting the status code through HttpConnection.getResponseCode. If the code is 200 then the server's running fine. Does this method not affect the server, I mean won't it load the server with its own requests every minute?

+1  A: 

A single request every minute should be absolutely fine.

Just think of the volume of other requests that are happening at the same time :-)

toolkit
Thanks for the heads up... so you mean to say if a server's experiencing a high load then this isn't a good idea? What if I implement this for a shared host, which usually is in trouble due to large number of requests from all the hosted sites :)
Nischal
A: 

if downtime for you means that site is not accessible and you have sized the server for adequate bandwidth and load, your solution is ok.. but not good enough for enterprise reporting use. You need more.

Having said that I'm sure that your hosting provider will be better positioned to give you more accurate statistics about the health and availability of your server without you having to do any monitoring. Check your contract with them.

Ryan Fernandes
I intend to have a service that would notify me the moment my site is inaccessible. Hosting providers usually react faster if they're notified of the downtime, hence this initiative. Any ideas on implementing it in a more efficient way, or you think this would be fine? Sites involved might be one's experiencing a high load, that's the reason I want to be sure that this method doesn't cause extra strain on the poor thing :D
Nischal
">Hosting providers usually react faster if they're notified of the downtime, hence this initiative".. that's strange! One of our hosting provider has constant monitoring of ports and server processes (web and app server processes). Port monitoring extends from the machines running the app to the firewall and front end (layer 4-7) content switches. Again, your solution will work, but going with the hosting provider adds the layer of accountability needed for this type of work
Ryan Fernandes
A: 

One request per minute sounds negligible unless that request causes the server to do a lot of heavy lifting. You could also request a static asset like a css file to simply check that container is responsive.

If you want to conserve every last bit of network bandwidth, you can make your request method "HEAD" rather than "GET".

Jon Hoffman
uh..huh! So you mean to say, a "HEAD" request would be less demanding compared to a "GET"? Will it still be able to confirm the availability of the site?
Nischal
Well, depending on what you are requesting-- a HEAD request to a dynamic page will most likely still cause the page to be rendered, it's just that the result won't be sent back over the wire.
Jon Hoffman
A: 

Is there a reason to do it yourself and not rely on one of the companies who make this thing for living? Usually they have servers around the world, and they can notify you by mail and SMS, with very flexible settings.

Some of the companies I know are host-tracker, monitis, watchour and mon.itor.us. I'm not affiliated with them, just a customer. I guess there are others as well

David Rabinowitz
And I want to compete with them :) I intend to start a simple and cost effective service that would help notify you instantly the moment your site goes down.Do you think one simple guy like me would be able to do it? :)
Nischal
I suggest you register to these services and check their feature list. Hitting a page every 60 seconds is not always what is needed.
David Rabinowitz
For example, some of the tests can be if the body contains a certain phrase, perform some kind of authentication, etc.
David Rabinowitz
but if I take that direction (body consisting of certain phrase etc) then I would need to get the entire page content. getResponseCode wouldn't do that thus reducing the load. And I do not want users to add anything in their code. All they need to do is register.
Nischal
I've tried getResponseCode on an application running on jBoss server. When I do that, the console shows no activity. I mean it gives me code "200" if the server is up and running else the error code. But I never see any activity in the console.
Nischal
If the page does not log anything, you won't...
David Rabinowitz
A: 

Using the HEAD request will generate a response similar to that with GET but only including HTTP headers.

If you have control over the server you're monitoring, why not set up a URL solely fir monitoring purposes, that returns a simple text response an tyhe appropriate response code, and makes no extra demand on resources.

Don't forget that you may want to monitor not just the HTTP response but the health of the underlying services (eg databases etc.). Your monitoring URL can check these during the request and modify the HTTP response code appropriately (eg return a 500 - server error or similar)

Brian Agnew
I might not have control over the servers, would only get the url :)
Nischal
Then you're only testing the webserver, unfortunately, and you can derive only a little about the health of the underlying system, unless you choose your URL carefully
Brian Agnew
you mean, even if the url returns a status code of "200" there would still be a possibility of the site being down?
Nischal
Yes. If the underlying database is down your website might say 'sorry' and still return a 200
Brian Agnew
oh! Thanks for letting me know about that...!
Nischal