Register
It is currently Mon Sep 01, 2014 2:58 pm

Monitoring network state - discussion and a broken script..


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Wed Jun 06, 2012 12:19 pm   

Joined: Wed Jun 06, 2012 12:16 pm
Posts: 4
My script doesn't work quite as I want. Can you please show me why?

The aim is to match the numbers returned by ping (text is something like "3 packets transmitted, 3 received, 0% packet loss, time 2003ms")

Ideally, the first two numbers will match, the third number will be 0 and the final number will be about 2000.

So I started to bash together a scribble..
Code:
#!/bin/bash

PING_HOSTS=( list of urls )
PING_COUNT=3
# capture 2 groups of numbers ([0-9]+){2}
regex='([0-9]+)([0-9]+)'

echo " * Started"
for host in ${PING_HOSTS[@]}
do
        result=`ping -c $PING_COUNT -q $host | grep "received"`
        echo $result $host
        if [[ $result =~ $regex ]]
        then
                echo "capture[1] : ${BASH_REMATCH[1]}"
                echo "capture[2] : ${BASH_REMATCH[2]}"
        else
                echo "unexpected error"
        fi
done


The 1st scripting problem:

capture[1] and capture[2] are not the expected values (i.e. 3, 3). What have I done wrong in the regex? :)

-----------

Also, can you please speculate on the suitability of this type of script for my problem?

My problem: I have some machines and they periodically become unresponsive (i.e. once every 2 weeks or so) and the server logs do not show anything unusual.

An external watchdog is of little to no value because the downed machines cannot be restarted externally.

My intention is to run the above script continuously (when the script is corrected and finished by you guys), and if it detects 2+ ping failures then it will restart the network service or reboot the VM.

Can my approach be bettered, and is Bash the most dependable platform for this task?

Many thanks :)


Top
 Profile  
 PostPosted: Wed Jun 06, 2012 1:23 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
hi,

keep it simple.

inside the for loop, for example
Code:
$ host="192.168.1.1"
$ ping_count=3
$ result=`ping -c $ping_count -q $host | grep "received"`
$ IFS=',' read tx rx other <<<"$result"
$ echo ${tx%% *}
3
$ echo ${rx% *}
3
$


Top
 Profile  
 PostPosted: Wed Jun 06, 2012 2:07 pm   

Joined: Wed Jun 06, 2012 12:16 pm
Posts: 4
Thank you, the %% * works, although I'm not sure what it actually does.

For example, I tried to use to capture the % loss and fell flat on my face. Can you explain it please? How will it react to "ping: unknown host"

Also, the echo does not redirect to file. Why is that? :)

Code:
#!/bin/bash

echo " * Started network state monitoring"

1>&2
2>>"monitor.log"

PING_HOSTS=( google.com unknown.host yahoo.com microsoft.com )
PING_COUNT=3
LOG_PREFIX='date -u'

while true
do
        sleep 10
        error_count=0
        for host in ${PING_HOSTS[@]}
        do
                result=`ping -c $PING_COUNT -q $host | grep "received"`
                #echo $result $host
                IFS=',' read expected actual loss <<<"$result"
                if [[ ${expected%% *} -ne ${actual% *} ]]
                then
                        if [[ ${loss%% *} -eq '100' ]]
                        then
                                echo `$LOG_PREFIX`" No route to $host"
                                let error_count++
                        else
                                echo `$LOG_PREFIX`" $host has $loss% failure"
                        fi
                fi
        done
        if [[ error_count -eq ${#PING_HOSTS[@]} ]]
        then
                echo `$LOG_PREFIX`" Network service restart"
                $(sudo /etc/init.d/networking restart)
        fi
done


Many thanks again!


Top
 Profile  
 PostPosted: Wed Jun 06, 2012 2:29 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
Quote:
I tried to use to capture the % loss and fell flat on my face
that's because of a leading space:
Code:
$ rx="${rx# }" other="${other# }" # this removes leading space
$ echo ${rx%% *}
3
$ echo ${tx%% *}
3
$ echo ${other%% *}
0%


if you want to redict both stdout and stderr to a log file, then do this
Code:
exec > ${0##*/}.log 2>&1
log will be in current directory, under <yourscriptname>.log


Top
 Profile  
 PostPosted: Wed Jun 06, 2012 3:05 pm   

Joined: Wed Jun 06, 2012 12:16 pm
Posts: 4
That's a nice little tip, thank you again!

It's getting close now, and hopefully it will workaround the pesky server problems :)

There is a syntax error: operand expected (error token is %)

Code:
#!/bin/bash

echo " * Started network state monitoring"

#exec > ${0##*/}.log 2>&1

# This list includes 2 failing hosts and 2 passing hosts
PING_HOSTS=( nowhere.pluto google.com yahoo.com microsoft.com )
PING_COUNT=3
LOG_PREFIX='date -u'

while true
do
        error_count=0
        for host in ${PING_HOSTS[@]}
        do
                result=`ping -c $PING_COUNT -q $host | grep "received"`
                #echo $result $host
                IFS=',' read expected actual loss <<<"$result"
                loss="${loss# }"
                if [[ "$PING_COUNT" -ne ${actual% *} ]]
                then
                        if [[ ${loss%% *} -eq '100%' || ${loss} -eq '' ]] ## Syntax error is in this line
                        then
                                echo `$LOG_PREFIX`" No route to $host"
                                let error_count++
                        else
                                echo `$LOG_PREFIX`" $host has ${loss%% *} failure" ## microsoft.com hits this line, but should hit error_count++ above
                        fi
                fi
        done
        if [[ error_count -eq ${#PING_HOSTS[@]} ]]
        then
                echo `$LOG_PREFIX`" Network service restart"
                $(sudo /etc/init.d/networking restart)
        fi
done


Top
 Profile  
 PostPosted: Wed Jun 06, 2012 3:20 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
that's because you're using an arithmetic comparison operator (-eq), in text context comparison : 100% is not an integer.


Top
 Profile  
 PostPosted: Wed Jun 06, 2012 4:02 pm   

Joined: Wed Jun 06, 2012 12:16 pm
Posts: 4
lol, thanks :)

It's working.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP