ping – openSUSE Lizards https://lizards.opensuse.org Blogs and Ramblings of the openSUSE Members Fri, 06 Mar 2020 11:29:40 +0000 en-US hourly 1 Is my server alive and how good is my connection https://lizards.opensuse.org/2014/03/10/is-my-server-alive-and-how-good-is-my-connection/ Mon, 10 Mar 2014 07:25:14 +0000 http://lizards.opensuse.org/?p=10639 If you have time to setup real solution and need something reliable test Zabbix (http://zabbix.com). Zabbix is wonderful all in one solutions for monitoring network and your hosts (servers). If you are in need of knowing how good you Internet/(W)LAN connections is then things are getting complicated.
Case is like this: You have computer and WIFI-card (Wireless Internet card). Most of the time it’s working great! Speed is high and there is no hick-ups any kind. Out of the blue comes troubles. In other network video is not playing from net or you keep getting connection errors in browser.. so what to do? How to debug this one? It can be that other people are just consuming all the router bandwidth available or there is many WIFI routers in same frequency (be sure to check first all the hardware before getting into software level). If hardware is not the case and you have double checked with normal Wire connection to Internet/LAN then there is definitely something else. With WIFI connection it still can be your grandma’s old beloved 1950 microwave oven (If you on 5Ghz band then you can’t blame her either)..
So I assume you are on TCP/IP-network if you don’t know what it is you should first find out and then read the rest of this story.
WIFI-cards have signal strength monitor. I have notice that many times it’s just a estimation. You can have 100% strength and your connection is next to nothing. Manufacturers tends to make connections look way too better than they are or they make them look even worst than they are.
So how to find out is my connection good or bad? Word of warning in this point: this is just one way to find out and it’s not very good (for some use cases it is). Way is to use little bit BASH (Bourne Again SHell) and ping-command which is installed in openSUSE default.

What ping does

Ping consumes ICMP-protocol which is very low level and I don’t know is it used for anything anymore these days (correct me if I’m very wrong) only with ping-command. If Wikipedia is correct ping calculates round-trip so how long takes data to find it’s way to server and come back (Small tip here! You can use traceroute to see how you packet is finding it’s way on the Internet and back to you).
So how can I use ping to tell me is my server up? Short answer is simple: ‘you can’t’. Little bit longer answer: ‘Yes you can but it’s little bit more complicated than you first thought’ and the long version: ‘You get information about if your server’s network card answers to (and most of the time kernel network stack) received ICMP-packet. If there is firewall rule that turns ICMP-packet answer off you are out of luck. Ping doesn’t tell if your service is up or Apache running correctly and reliable. Remember ping answer can work even if most of user space is smashed down and kernel is malfunctioning (been there done that!)’.
So what you get it answer from network card and that’s it. If it’s enough (99% times it is) keep reading.

Script

Show me code you scream and I give it to you and please read comments on code before using. This is edited version from Stackoverflow.com message script (so nothing original here):

#!/bin/bash

echo "Press [CTRL+C] to stop.."

# Make at least 10 pings in one time
COUNT=50
SUCCESS=0
LASTFAIL=NONE
# Change this to Domain name/IP
# or ip you want to monitor
IP=192.168.1.1
FAILS=0

while true
do
        #
        # Ping with interval 0.5s to IP (192.168.1.1)
        # COUNT (50) times
        # after that we just grep 'byte from' from output
        # or ping and them calculate how many we got
        # with wc -l (Lines of output) bacause we drop
        # other from output
        # You can use up (normally) 2000 size (-s) of package
        # to see if it's starts to drop..
        LINES=$(ping -s 1024 -i 0.5 -c ${COUNT} ${IP} | \
                grep "bytes from ${SERVERSERVER}" | wc -l)

        # Calculate how many fails there were
        let FAILS=${COUNT}-${LINES}+${FAILS}
        # Add to success rate
        let SUCCESS=${LINES}+${SUCCESS}

        # If there were error but date to LASTFAIL
        # so if you not every moment looking you can
        # see when this error happened
        if [ ${LINES} -ne ${COUNT} ]; then
             LASTFAIL=$(date)
        fi

        # Ok ping all together failed
        if [ $? -ne 0 ] ; then
          echo "Can't Connect to server ${IP}"
        else
          # Print statistics to screen
          echo "$(date) ${IP} Success: ${LINES} (Tot. F ${FAILS}) \
(Tot. S ${SUCCESS}) (Last F ${LASTFAIL})"
        fi

        # Sleep a while (not consume all the bandwidth)
        sleep 1
done

Output

Script prints mystical output. Things that matters that Success rate is 50 (or what pinging rate you are using) and Fails are not rising fast. It they are rising fast packets are dropped if you are not getting anything check IP/domain name. With this one you can get ping time. If success level is 50 but ping time is high you network is still very slow but that you have to check with ping-command by hand. Even with 2G mobile modem you should get under 300ms (3G should be like 100ms).
To have any clue about your connection you have make this at least 30 minutes (First ping packets can tell everything but if you want to see whole picture). Actually this is stupid way to find out is your server up but if your WIFI-card is working everywhere else great you can check are you packets going through. If ICMP-packets are dropping very fast then your TCP/IP packages are dropping very fast and you network stack have to re-send them so you notice that your network is slow (even though it has nothing to do with that). Again it can be route, WIFI-card or firewall problem still.. and ping doesn’t answer to that question. So this was this days script stuff. If you have better ways to do this.. please share I’m always interested in this topic.

]]>