9

Is there a ping replacement that will display the system's date/time in addition to the round trip time and sequence number? I would prefer a tool that runs on Linux, but if there is a cli tool I can run on Windows that would be good too.

There is a system which users are reporting is intermittently pausing. This doesn't seem to happen at any consistent time. I haven't been able to get the reporting user to tell when it happened with enough specificity to be able to correlate the pause to any logs.

One of the techs left a ping running against the host for a day. The round trip time got pretty large at one point in time. I am trying to figure out when exactly this happens so I can narrow down which log entries I should be looking at, and possibly correlate this pause with other data I might be able to collect with performance logs, device logs and so on.

64 bytes from 10.2.4.241: icmp_seq=1825 ttl=64 time=0.321 ms
64 bytes from 10.2.4.241: icmp_seq=1826 ttl=64 time=0.371 ms
64 bytes from 10.2.4.241: icmp_seq=1827 ttl=64 time=13937.638 ms
64 bytes from 10.2.4.241: icmp_seq=1828 ttl=64 time=12937.526 ms
64 bytes from 10.2.4.241: icmp_seq=1829 ttl=64 time=11937.392 ms
64 bytes from 10.2.4.241: icmp_seq=1830 ttl=64 time=10937.275 ms
...
64 bytes from 10.2.4.241: icmp_seq=1840 ttl=64 time=936.073 ms
64 bytes from 10.2.4.241: icmp_seq=1841 ttl=64 time=0.410 ms
12

You can add timestamps using perl like this:

ping 127.0.0.1 | perl -pe 'BEGIN {use POSIX;} print strftime("%Y-%m-%d %H:%M:%S ", localtime)'
  • 1
    That's almost too easy! :) I searched for this for almost 30 minutes with not many good results, thanks! – l0c0b0x Nov 3 '09 at 0:54
12

Here's a bash solution :)

$ ping localhost | while read line ; do echo -e "$(date)\t $line" ; done
Tue Nov  3 04:46:26 MSK 2009     PING localhost (127.0.0.1) 56(84) bytes of data.
Tue Nov  3 04:46:26 MSK 2009     64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.033 ms
Tue Nov  3 04:46:27 MSK 2009     64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.040 ms
Tue Nov  3 04:46:28 MSK 2009     64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.046 ms
Tue Nov  3 04:46:29 MSK 2009     64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.046 ms

Now, let's make the date command produce a bit more nice output:

$ ping localhost | while read line ; do echo -e "$(date +%H:%I:%S)\t $line" ; done
04:04:13         PING localhost (127.0.0.1) 56(84) bytes of data.
04:04:13         64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.044 ms
04:04:14         64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.039 ms
04:04:15         64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.042 ms

Cheers!

  • Pedantic: I wouldn't say native because the date command is an external command, and not built in to bash. I only point it out because if you are not aware (you might be, I don't know), realizing that there are built-in and not built-in commands is important. Still a good solution though :-) – Kyle Brandt Nov 3 '09 at 15:30
  • 1
    Also don't need the while [ 0 ]: ping google.com | while read line; do echo -e "$(date) $line"; done – Kyle Brandt Nov 3 '09 at 15:37
5

A "heavier" option that we use to do regular checking of latency and packet loss is Smokeping. Not only does it give you a little more information in an easier to read format, but you can also do things like HTTP and DNS checks instead of relying on ICMP. Many firewalls and routers will de-prioritize ICMP resulting in false latency measurements.

Smokeping

  • Great tool, while heavier than the scripts it also can be nice to just setup for monitoring various systems. – sclarson Nov 3 '09 at 22:24
2

For Linux, install moreutils which will give you ts.

$ ping nu.nl | ts
jan 29 14:39:51 PING nu.nl (62.69.166.254) 56(84) bytes of data.
jan 29 14:39:51 64 bytes from 62-69-166-254.ptr.as24646.net (62.69.166.254): icmp_seq=1 ttl=247 time=29.8 ms
jan 29 14:39:52 64 bytes from 62-69-166-254.ptr.as24646.net (62.69.166.254): icmp_seq=2 ttl=247 time=29.4 ms
0

Let's use the shell built-in printf with the %(datefmt)T format specification to avoid calling heavier interpreters or spawning new a date process for every line.

ping -c4 localhost | while read line; do printf "%([%Y-%m-%dT%H:%M:%S])T %s\n" "-1" "$line"; done

Like the perl example, this can be done by piping to sed and awk too. I think ts is the easiest, but we don't have that on our servers. Same for environments like busybox.

If your command buffers its output, you can use unbuffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.