Is Ping a reliable way to check if a server is available?

116,898

Solution 1

The best way to tell if any given remote service is alive is to ask it to service a request in the way it's meant to - in fact it's the only way to truly know something is working correctly.

As an example I always get my load-balancers to get an actual 'head' response back from our web servers, you could do the same for a small select on a DB box if you wanted to, or whatever your actual server serves. As a tip you can create an 'online.txt' (or whatever name you want to give it) on your web servers, have your LBs try to get that file and if it fails then it removes the server from the VIP, this is a nice way of manually taking individual servers out of your VIPs simply by renaming a single file.

Ping only tests for the ability to respond to pings, so that's base OS, parts of the IP stack and the physical links - but that's all, everything else could be down and you'd not know.

I know this is mentioned below, but it bears repeating again and again.

ICMP Echo Requests (aka "Pings") (aka ICMP Type 8) are built onto the IP stack spec, yes, but are not required to be either implemented or used. As a matter of fact, there are a large number of internet providers who refuse to forward those and silently drop those requests, as they are a form of network attack (called a pingflood).

As mentioned above, this is handled by the OS (specifically at the network stack level) and so it is up to the OS configuration to respond to those or not. If this is turned off (a security precaution?), you can't do anything about receiving ping replies from the other end. This is why it's not reliable.

Solution 2

Most of time, yes, however:

  • some servers block ping requests

  • just because the server is responding doesn't automatically mean the website (or whatever service you expect to use) is working, you should also check if the response matches the expected contents.

Solution 3

It is true that on many occasions ICMP traffic is filtered out so it could be unreliable...

A better way perhaps could be to telnet the server at the service port you are interested in.

i.e. telnet 127.0.0.1 8080

Solution 4

If the server is only required to respond to pings then this is a good method of determining it's availability. If is is required to provide for example a web service then you should carry out some form of test to see if that is working similarly for fileservices etc.

Solution 5

ping has 2 drawbacks:

  • ping sends icmp, that can be filtered by the firewall
  • the tcp or udp port your application uses could be busy or not open - ping doesn't check that

a better solution is to check your udp/tcp port directly, to see if the service is still available... :-)

Share:
116,898

Related videos on Youtube

Peter Kelly
Author by

Peter Kelly

Check out my about.me profile! Currently working on clustering, gossip protocols, next generation security applications.

Updated on September 18, 2022

Comments

  • Peter Kelly
    Peter Kelly almost 2 years

    In my application I am pinging a server and waiting for a response. I am using this to determine whether the server is available and responsive or not.

    Is this a reliable way of determining availability? I assume a firewall could be filtering icmp traffic... Are there any other drawbacks? Is there a more reliable method?

  • MadHatter
    MadHatter almost 13 years
    What the man said! I always advise clients that the best way to tell if a server is currently offering service X is to request service X.
  • Joachim Sauer
    Joachim Sauer almost 13 years
    rsh? Really? Why not use ssh instead?
  • user
    user almost 13 years
    rsh vs ssh aside, how does being able to execute date (assuming you are, to begin with) say anything about whether a web server, SMTP server, DNS server, local database server or whatnot is running and able to serve requests? Better to actually ask for the specific service you want to verify availability of (which might be a remote shell, but sure doesn't have to be).
  • tsykoduk
    tsykoduk almost 13 years
    We actually build a "test" RESTful API into our apps just for this. So we know that if an app responds to blah/whatever_app/pulse it's up, servicing requests, and has all of the tools that it needs (DB, dependancies etc)
  • user9517
    user9517 almost 13 years
    That doesn't mean that it's available to do real work, just that it's responding to ping.
  • user606723
    user606723 almost 13 years
    To add to MadHatter, it's often a good idea to do a ping and a request. This way you can know right away whether you're dealing with network connectivity or a service outage... Either one tends to create completely different things than the other.
  • Dave
    Dave almost 13 years
    Maybe. It's always ment I could log on to the server and that's all I ever need.
  • Rob Moir
    Rob Moir almost 13 years
    Ping isn't even a reliable test that the server itself can respond to ping - if it does not then all you know is that something between you and it is filtering ICMP traffic
  • John Gardeniers
    John Gardeniers almost 13 years
    I've had many occasions where a server responded to pings but failed to server any other kind of request, including logons. A ping can be a very crude means of telling when the server has started to come back up but that's all. Of course that's assuming the server even responds to pings.
  • Dave Cousineau
    Dave Cousineau about 9 years
    just recently thought an smtp server was down because I couldn't ping it. half hour later, sure enough I realize that while I can't ping it, I can still telnet to it on port 25.
  • David Cowden
    David Cowden over 8 years
    I logged in to up-vote this answer. It is concise, informative, assertive, and correct.