Register
It is currently Wed Oct 01, 2014 10:24 pm

Monitoring bash script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Jul 19, 2012 5:34 am   

Joined: Thu Jul 19, 2012 5:15 am
Posts: 1
I guy!

I need an help...
I will try to create a monitoring bash script that ping our apparate and send mail if it go offline.
I need to use a list of apparates that have 3 column (name, ip gataway, ip address) and n row.

That file (list.txt) is like that:

Code:
server1      30.30.30.1   30.30.21.1
server2      30.30.30.2   30.30.21.2
server3      30.30.30.3   30.30.21.3


My bash script is:

Code:
#!/bin/bash

#Check IP
for i in $(cat list.txt | awk '{ print $2}')
do
                #n=$(cat lista | awk '{ print $1}')
                ping -c 1 -w 1 $i &> /dev/null
                if [ $? -ne 0 ]; then
                echo "GW $i DOWN!"
fi
done


This script run, but the $i variable is the GW address.
I need to insert in echo "GW $i DOWN!" not the GW address (column 2), but the name of the server (column 1)
If i remove the comment on #n=$(cat lista | awk '{ print $1}') and insert the variable $n in the "echo" (echo "GW $n DOWN!") the output is the list of the column 1...
I need also insert the check of the 3th column... and the alert mail!
Can you help me please?

I want to have this output
"GW server2 DOWN!" check it every 15 min (i can insert it in crontab) and then send it to email address

Thank you for your help!


Top
 Profile  
 PostPosted: Thu Jul 19, 2012 12:34 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
Hi,

reading a file using BASH is done this way:
Code:
while read -r name gw ip
do echo "$name $gw $ip" # for example
done <list.txt

generally, for i in $(command) leads to bad results.


Top
 Profile  
 PostPosted: Thu Sep 13, 2012 8:51 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
I've written this before but didn't post it for some unknown reason...

Code:
#!/bin/bash

# IPs LIST FILE PATH
servers_ip_list="list.txt"

# ADDRESS(ES) TO SEND CHECK TO
addr_to_mail=("foo@bar.com" "baz@qux.com")


if ! type -P mailx > /dev/null; then
    echo 'E: mailx(1) has to be installed to use this script.' >&2
    exit 1
fi

is_in_array()
{
    local str=$1 elmt
    shift

    for elmt; do
        if [[ $str = "$elmt" ]]; then
            return
        fi 
    done

    return 1
}

while read -r serv_name ip_gw ip_addr _; do
    for serv_ips in "$ip_gw" "$ip_addr"; do
        if ! ping -c1 -w1 "$serv_ips" > /dev/null 2>&1 &&
           ! is_in_array "$serv_name" "${down_servs[@]}"; then
               
                down_servs+=("$serv_name")
        fi
    done
done < "$servers_ip_list"

mailx -s '[SERVERS CHECK]' "${addr_to_mail[@]}" <<< "GW(s) $(IFS=,; echo "${down_servs[*]}") DOWN!"


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 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