script for continous ping

Hi,
I want to run a script for continuous ping for a day then delete the previous day after checking if there is a gap in between with the other servers connectivity. Output the result in a /tmp file everyday. Mainly i am trying to troubleshoot cacti graph gap i am seeing in certain time in a particular server. Not sure why it is doing that.so i want to keep pinging the cacti server. Is there a better way of monitoring this ?

The command ping usually accepts a flag for a count, typically -c or -n so you could choose to either rin it with a count of 1 and append to the log files then scan through them later, or you could schedule the ping and set the count for 60 seconds * 60 minutes * 24 hours (86400) then read the output for packets dropped.

I hope that this helps
Robin
Liverpool/Blackburn
UK

Thanks for the response!
Can you please provide the actual syntax.

Something like:-

ping -c 1 10.20.30.40

...will get you a one second ping to reconcile, or

ping -c 86400 10.20.30.40

... will give you 24Hrs. of output.

If you ran the latter with a cron entry thus:-

0 0 * * * ping -c 86400 10.20.30.40 > /tmp/`date +%Y_%m_%d`

The, at a later date you could:-

grep "packets transmitted" /tmp/output_file read in p t out rest

if [ $in -ne $out ]
then
   echo "We have a problem"
else
   echo "All okay"
fi

Does that help at all?

Robin
Liverpool/Blackburn
UK

Thanks again.
I am running it for 60 sec. But facing a problem
# cat ping.sh
ping -c 60 serverip > /tmp/`date +%Y_%m_%d`
[root@test1 tmp]# cat ping_out.sh
grep "packets transmitted" /tmp/2012_04_20

if [ $in -ne $out ]
then
echo "We have a problem"
else
echo "All okay"
fi

But when i am running it its saying "We have a problem"
# ./ping_out.sh
60 packets transmitted, 60 received, 0% packet loss, time 59034ms
We have a problem

Can you please explain whats
[ $in -ne $out ] doing ?

Oh, a typo from me there. :o I missed the pipe into the read statement. Corrected code below:-

grep "packets transmitted" /tmp/output_file | read in p t out rest

if [ $in -ne $out ]
then
   echo "We have a problem"
else
   echo "All okay"
fi

Is that a little better?

Robin

Thanks. But why is it saying "We have a problem" , as there were no gaps or errors shouldnt it say "All okay". Also i didnt catch the logic of
[ $in -ne $out ]

If you saved this in a file myscript, can you run it as:-

ksh -x myscript

... and paste the output back.

Thanks, in advance,
Robin

Now its showing. Not sure why ?

# ksh -x ping_out.sh
+ read in p t out rest
+ grep 'packets transmitted' /tmp/2012_04_20
+ [ 60 -ne 60 ]
+ echo 'All okay'
All okay

Well, that looks good to me. In the "We have a problem" section, you would be best to put some reasons in using the values $in and $out so you can see what's happening, such as:-

...
...
if [ $in -ne $out ]
then
   echo "We have a problem.  I sent $in, but received $out."
else
...
...

The if statement

if [ $in -ne $out ]

is a numeric comparison. The operator -ne is "Not Numerically Equal" but causes trouble if you try to use it with alphanumerics on many systems. So, if the packets sent in are not equal to the packets got out, then you get the warning message. That's the logic.

Robin
Liverpool/Blackburn
UK

Morning.

Not sure what OS or shell you're running, but this may help:

while :;do date >> /tmp/monitor.log;ping -c1 <server> | awk '/packets/{if($4 ~ 0) print $0)' >> /tmp/monitor.log;echo >> /tmp/monitor.log;done

Output to log:
Fri Apr 20 11:56:36 EDT 2012
1 packets transmitted, 0 received, 100% packet loss, time 0ms

This one-liner will dump the current date and time to a log file, send a single ping to the server in question, and use awk to only print when there is no response to the ping. You can open another shell and tail -f the log file to watch it in real-time.

Hope this helps.

Thanks in2nix4life, i will try that. Thats a redhat linux machine.
2.6.18-308.1.1.el5, bash shell

Also thanks to rbatte1, but wondering why only ksh running it properly?

Hi in2nix4life,
When running your script i am getting syntax errors.

awk: /packets/{if($4 ~ 0) print $0)
awk:                              ^ syntax error
awk: /packets/{if($4 ~ 0) print $0)
awk:                              ^ syntax error
awk: /packets/{if($4 ~ 0) print $0)
awk:                              ^ syntax error

That last ) ought to be a }.

1 Like

Sorry about that. Coffee was wearing off.

in2nix4life,
Script is running fine now. I do see a few problem.

  1. Why am i not able to delete the monitor.log file
  2. Though i set the ping for 5 sec but it is continuously running till now. Why is that?
  3. Can you please explain the later part
    awk '/packets/{if($4 ~ 0) print $0)' >> /tmp/monitor.log;echo >> /tmp/monitor.log;done