107

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?

14 Answers 14

150

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.

7
  • 35
    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.
    – MadHatter
    Sep 19, 2011 at 10:28
  • 5
    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)
    – tsykoduk
    Sep 19, 2011 at 16:26
  • 5
    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.
    – user606723
    Sep 19, 2011 at 19:48
  • 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
    – Rob Moir
    Sep 19, 2011 at 21:13
  • 4
    Assuming the machine responds to ping under normal circumstances, you can use ping as some sort of bloom filter: If the ping fails, the service is definitely down (You have a network problem, since we've established the ping works usually). However, if the ping succeds the service could still be down as described in this answer
    – 3Doubloons
    Sep 20, 2011 at 15:28
11

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.

6

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

5

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.

3

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... :-)

3

There are special tools for testing and monitoring like Nagios / Icinga.
With these tools you can (of course) do checks with various ping-test but also do checks on your services.

All checks can use the returned value to classify the result as "good", "warning" and "critical" and can be written in nearly every programming language.

Of course not easy to setup (like point and click), but customisable, reliable and extensible. Runs well on various Linux and Unix distributions.

2

Test the services you are looking for, just ping a server does not mean the services is working.

For example:

Imagine a webserver with dozen websites, then I need to know if the websites is UP, I did myself a tiny script in php and run it every 10 minutes.

The script do the follow ->

<?php
    $website1 = "http://www.mywebsite.com/";
    $myWebsite = file_get_contents($website1);
    $message = 'My website' . $website1 . ' is DOWN at the moment.';
    if (empty($myWebsite)) mail('[email protected]', 'Website is DOWN', $message);
?>
2

Using ping to determine if a server is available is like an ER doctor checking to see if a patient is breathing. Yes, it is a good place to start, but there may be other issues.

1

We use ping to do a precheck, that the host is powered-on and reachable, before launching our systemd service that attempts a ssh connection to it. This saves some time debugging, as the systemctl start command will immediately fail, instead of silently fail and get lost in the journalctl jungle.

Note that ping is not "reliable" in the same sense as TCP. If you have a bad connection (or crappy network stack, thank you Intel mpss) and packets are being dropped, a single packet ping may fail. On the other hand, a TCP connection is reliable against dropped packets. So, ironically, an ssh connection might work immediately after a single ping failure. So if you use ping to do a sanity check, be sure to allow some failure.

0

Just my two cents: We have a legacy application that uses this method, and had to service it because the ping was not sufficient for determining service availability.

Ping merely shows that the server is capable of listening, but in our case the service was unable to start without human intervention.

As a result units, which naively assumed the server was available, were attempting to connect and timeout. Instead of displaying our "Server is Unavailable" message.

--

Our current application, which communicates via XMLHTTPRequests to a web server, sends a formed message which the server will respond to with a status code. The status code is computed by the server doing a number of checks to ensure that various subsystems are online (DB, necessary directories are writable, etc.)

0

If under normal circumstances your server responds to ping, it is useful to ping it at one minute intervals to check if it responds. This of course only tells you that there is a server at that IP address and that there is a network path from the source of the ping to the destination. Setting a threshold for the response time can allow you to also monitor the state of the network. If you are pinging a server on the Internet there may be little you can do to fix the network but if a customer calls to complain, you will already be aware of the problem. Pinging google.com in addition is also useful. If you and google are both down, something is happening.

As others have mentioned, it is important to monitor that the service you are providing is responding and that its performance is OK. I.e. you might want to check why a web age that usually responds in a second is now responding I'm 10 seconds.

So knowing that a service is not responding and it is ping failing gives you much more information than just one approach. Also if you monitor the processes too, knowing that ping responds, the service does not respond and the web server does not have the correct number of processes tells you where to look first.

You can go crazy with monitoring so just monitor enough to tell you when something bad has happened or is getting dangerous. I.e. too much swapping, >90% disk usage, high disk io, 100% CPU for extended periods and remember that monitoring is just a denial of service attack carried out very slowly.

0

Ping (Packet Internet Groper) makes you known whether your system is communicating with the system with whom you want to establish connection over the network. It even pings, does not mean the service for example RemoteRegistry service is running.

However, to fix any issue ping is necessary. You can remotely fix any issue. Hence, ping has its own importance.

-3

The best way I use in my scripts is

#rsh servername.com "date"
Mon Sep 19 04:42:20 PDT 2011

in place of rsh alternatives like remsh can be used. This ensures that your remote system is booted completely and you can run commands on it. Simple ping is not sufficient as during boot when network services are started, system starts responding to ping.

2
  • 3
    rsh? Really? Why not use ssh instead? Sep 19, 2011 at 11:57
  • 5
    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).
    – user
    Sep 19, 2011 at 12:23
-3

When I reboot a windows server I open up a command prompt box and enter

ping <box> -t

First it will suggest that it's available-that's the box going down. Then you'll get a lot of "request time out." When you start getting replys the box is up.

4
  • 7
    That doesn't mean that it's available to do real work, just that it's responding to ping.
    – user9517
    Sep 19, 2011 at 16:53
  • Maybe. It's always ment I could log on to the server and that's all I ever need.
    – Dave
    Sep 19, 2011 at 20:28
  • It's perfectly possible for a server to be responding to ping but not (yet, or even at all) to have started any higher level network functions. The only thing ping tests is ping. And maybe name resolution if you ping by name rather than IP
    – Rob Moir
    Sep 19, 2011 at 21:15
  • 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. Sep 20, 2011 at 3:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .