Ping Monitor

Hello,

I need a simple shell script. If you can help I'd appreciate it. I'm sorry for my English.

I want to send ping to 3 web site

I want to send 5 ping to 3 web site

result:

1- www.google.com UP
2- www.yahoo.com UP
3- www.myserver.com DOWN

If myserver.com is down I want to 10 ping all of them again (To avoid errors resulting)

result:

1- www.google.com UP
2- www.yahoo.com UP
3- www.myserver.com DOWN

I want to run a curl command.


If first ping result is:

1- www.google.com DOWN
2- www.yahoo.com UP
3- www.myserver.com DOWN

I don't want to run a curl command.

How about this:

#!/bin/bash
function checksites {
    SEND=$1
    shift
    while [ $# -gt 0 ]
    do
        ping -n $SEND $1 2>&1 | grep -q "[ (]0%"
        if [ $? -eq 0 ]
        then
            printf " UP"
        else
            printf " DOWN"
        fi
        shift
    done
}
CHECK=( www.google.com www.yahoo.com www.myserver.com )
RESULT=( $(checksites 5 ${CHECK[@]}) )

[ "${RESULT[2]}" = "DOWN" ] && RESULT=( $(checksites 10 ${CHECK[@]}) )

if [ "${RESULT[*]}" = "DOWN UP DOWN" ]
then
    echo "Run your curl command here"
fi

Thank you very much. I've tried a non-working ip number on server 3.
I did't get any results when I run.

Both server1 and server3 need to be non working (DOWN UP DOWN) to get a result, isn't that what you wanted?

For just server3 you will need:

if [ "${RESULT
[*]}" = "UP UP DOWN" ]
then     
    echo "Run your OTHER curl command here"
fi 

Hello,

I tried it. but any curl command does not work

please post your updated script, between

```text
 and 
```

tags and any output or error messages you are getting.

I am not receiving any output.

#!/bin/bash
function checksites {
    SEND=$1
    shift
    while [ $# -gt 0 ]
    do
        ping -n $SEND $1 2>&1 | grep -q "[ (]0%"
        if [ $? -eq 0 ]
        then
            printf " UP"
        else
            printf " DOWN"
        fi
        shift
    done
}
CHECK=( www.google.com www.yahoo.com www.ZB37NNRHHS4Fmyserver.com )
RESULT=( $(checksites 5 ${CHECK[@]}) )

[ "${RESULT[2]}" = "DOWN" ] && RESULT=( $(checksites 10 ${CHECK[@]}) )

if [ "${RESULT
[*]}" = "UP UP DOWN" ]
then     
    curl -o /root/100mb.test "http://cachefly.cachefly.net/100mb.test"
fi

It will take 45 seconds before curl is run (5x3 + 10x3) perhaps you could add some debugging eg:

#!/bin/bash
function checksites {
    SEND=$1
    shift
    while [ $# -gt 0 ]
    do
        ping -n $SEND $1 2>&1 | grep -q "[ (]0%"
        if [ $? -eq 0 ]
        then
            printf " UP"
        else
            printf " DOWN"
        fi
        shift
    done
}
CHECK=( www.google.com www.yahoo.com www.ZB37NNRHHS4Fmyserver.com )
RESULT=( $(checksites 5 ${CHECK[@]}) )

[ "${RESULT[2]}" = "DOWN" ] && RESULT=( $(checksites 10 ${CHECK[@]}) )

echo "Server status: ${RESULT[*]}"

if [ "${RESULT[*]}" = "UP UP DOWN" ]
then     
    echo "server3 is down about to fetch 100mb test file..."
    curl -o /root/100mb.test "http://cachefly.cachefly.net/100mb.test"
fi

I am receiving only this output. but Google and Yahoo! are UP on ping command.

Server status: DOWN DOWN DOWN

Ok it's probably the format of your ping output, can you post the output of ping -n 1 www.google.com

my server centos 5.8

result this:

connect: Invalid argument

Seems like you have some network issues you need to get ping working from the command prompt before this script will ever work.

I think "ping -c" and "ping -n" are same command now it is works :slight_smile:

Thank you very much for sparing your time

---------- Post updated at 05:12 PM ---------- Previous update was at 04:03 PM ----------

Can I check if the port 80 is open with script?