Jotne
Jotne

Reputation: 41456

Print ping response time while higher than a fixed value, including date

I like to have a running ping that only prints output while passing a threshold value.
And if passing this value, also add date to the output.

Here is what I have tried:

ping 8.8.8.8 | awk '{split($7,a,"[=.]");if (a[2]>58) print a[2],d}' d="$(date)"
ping 8.8.8.8 | awk '{"date"| getline date;split($7,a,"[=.]");if (a[2]>58) print a[2],date}' 

Problem with both of these is that date is not updated. All is printed with same date.

59 Fri Nov 15 08:55:04 CET 2013
59 Fri Nov 15 08:55:04 CET 2013
59 Fri Nov 15 08:55:04 CET 2013
60 Fri Nov 15 08:55:04 CET 2013
59 Fri Nov 15 08:55:04 CET 2013

I know this could be solved using a bash script, but I just like to have a simple command line when testing line ping time.

Upvotes: 1

Views: 1220

Answers (2)

devnull
devnull

Reputation: 123478

When you say:

ping 8.8.8.8 | awk '{split($7,a,"[=.]");if (a[2]>58) print a[2],d}' d="$(date)"

the variable d is evaluated only once due to which you get the same timestamp appended to all the lines in the output. You could instead use strftime as an argument to print:

ping 8.8.8.8 | awk '{split($7,a,"[=.]");if (a[2]>58) print a[2], strftime()}'

Upvotes: 2

Phillip Kinkade
Phillip Kinkade

Reputation: 1432

The following works for me. I'm using OSX Mavericks:

ping 8.8.8.8 | awk -F"[= ]" '{if($10>50) {cmd="date"; cmd | getline dt; close(cmd) ; print $10, dt}}'

This will output the ping line for times > 50 ms.

I get this sample output:

51.352 Fri Nov 15 00:33:40 PST 2013
50.519 Fri Nov 15 00:33:42 PST 2013
52.407 Fri Nov 15 00:33:44 PST 2013
50.904 Fri Nov 15 00:33:50 PST 2013
52.864 Fri Nov 15 00:33:54 PST 2013

Upvotes: 4

Related Questions