«

»

Oct
16
2011

The Website Is Down

So our network at uni went down earlier this week for the first time since I’ve arrived last year, so I quickly wrote a PHP script to find out how to check if hosts are online or not (basically, whether they are responding to pings or not – you can see it in action here), it’s quite an easy task, however I wasn’t really aware of how to do this!

Essentially it boils down to a fantastic little package from PEAR (PHP Extension and Application Repository) called Net_Ping – only a few lines of code are required after installing this and then you are ready to go!

1
2
34
5
6
7
8
9
10
1112
13
<?php
require_once 'Net/Ping.php'; // include the Net_Ping package
$ping = Net_Ping::factory(); // create the object// check for any errors in the creation
if (PEAR::isError($ping))
{
  echo $ping->getMessage();
  exit();
}
// Now perform the ping!
$result = $ping->ping('www.example.com');print_r($result);
?>
<?php
require_once 'Net/Ping.php'; // include the Net_Ping package
$ping = Net_Ping::factory(); // create the object
// check for any errors in the creation
if (PEAR::isError($ping))
{
  echo $ping->getMessage();
  exit();
}
// Now perform the ping!
$result = $ping->ping('www.example.com');
print_r($result);
?>

If all goes well, the output should be similar to the following:

Net_Ping_Result Object
(
[_icmp_sequence] => Array
(
[1] => 17
[2] => 17
[3] => 17.1
)

[_target_ip] => 192.0.43.10
[_bytes_per_request] => 64
[_bytes_total] => 192
[_ttl] => 244
[_raw_data] => Array
(
[0] => PING www.example.com (192.0.43.10) 56(84) bytes of data.
[1] => 64 bytes from 43-10.any.icann.org (192.0.43.10): icmp_seq=1 ttl=244 time=17.0 ms
[2] => 64 bytes from 43-10.any.icann.org (192.0.43.10): icmp_seq=2 ttl=244 time=17.0 ms
[3] => 64 bytes from 43-10.any.icann.org (192.0.43.10): icmp_seq=3 ttl=244 time=17.1 ms
[4] =>
[5] => --- www.example.com ping statistics ---
[6] => 3 packets transmitted, 3 received, 0% packet loss, time 2003ms
[7] => rtt min/avg/max/mdev = 17.000/17.076/17.163/0.125 ms
)

[_sysname] => linux
[_round_trip] => Array
(
[min] => 17
[avg] => 17.076
[max] => 17.163
[stddev] => 0.125
)

[_transmitted] => 3
[_received] => 3
[_loss] => 0
)

So as you can see, you can now easily pull out all the information you may need! However, I found that if I used this, it’s very slow – these 3 pings took about 2 seconds to process (see the raw data) and would take even longer if the host is offline (waiting for timeouts)! So you may want to set some arguments. This fortunately is very simple and only requires you to add one additional line (excluding my comments):

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// set the arguments for the ping
/* There are various arguments which can be set, these include:
 * count - amount of pings to send.
 * quiet - "verboseness" of output.
 * iface - the network interface to ping from.
 * ttl - the time to live of the ping.
 * timeout - how long before we timeout (seconds).
 * size - size of the ping.
 *
 * Note: some arguments don't work on some OSes.
 */
// In my case I am pinging several hosts, and don't mind the
// occasional false negative so only send one ping, and
// timeout after one sec.
$ping->setArgs(array('count' => 1, 'timeout' => 1));
// set the arguments for the ping
/* There are various arguments which can be set, these include:
 * count - amount of pings to send.
 * quiet - "verboseness" of output.
 * iface - the network interface to ping from.
 * ttl - the time to live of the ping.
 * timeout - how long before we timeout (seconds).
 * size - size of the ping.
 *
 * Note: some arguments don't work on some OSes.
 */
// In my case I am pinging several hosts, and don't mind the
// occasional false negative so only send one ping, and
// timeout after one sec.
$ping->setArgs(array('count' => 1, 'timeout' => 1));

The performance I gained from this was massive, as I no longer have to wait a long time for each host if it is down (as the timeout is now 1 second), but also since only a single ping is sent, I very quickly ping the next host (beneficial to both online and offline hosts, due to the fact that when multiple pings in are sent in one ping command, there is a delay between each ping sent to prevent flooding the host, however, this additional delay in turn causes the script to take a long time to execute).

Leave a Reply

Your email address will not be published.