LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   delimited and if (https://www.linuxquestions.org/questions/linux-newbie-8/delimited-and-if-151223/)

nstanley 02-27-2004 12:40 PM

delimited and if
 
I have two files (servers_all and servers_exclude) where each line has hostname and ip, delimited by comma, and I want to write a shell script that will check the hostname in servers_all, and if that fails, will try the IP. And failure of IP will result in an email sent with hostname and IP. Frankly I am stuck. Does anyone have any ideas?> Below is code

MAIL1="email@email.com"
for servers in `cat /export/home/xxx/servers_all`
do
echo "$servers"
grep $servers /export/home/xxx/servers_exclude >/dev/null 2>/dev/null
rc=$?
if [ "$rc" = "0" ]
then
continue
fi
if [ "`/opt/xx/bin/snmpget $servers <MIB goes here> 2>&1 | grep 'No response'`" ];
then
/usr/bin/echo "SNMPD Down on $servers" |/usr/bin/mailx -s "SNMPD Down on $s
ervers" $MAIL1;
fi
done

david_ross 02-27-2004 12:48 PM

I haven't tried but what about:
Code:

#!/bin/bash

MAIL1="email@email.com"
IFS="
"
for servers in `cat /export/home/xxx/servers_all`;do
 echo "$servers"
 if [ `grep -c $servers /export/home/xxx/servers_exclude` -eq 0 ];then
  echo OK.
 fi
 if [ `/opt/xx/bin/snmpget $servers <MIB goes here> 2>&1 | grep -c 'No response'` -gt 0 ]; then
  /usr/bin/echo "SNMPD Down on $servers" |/usr/bin/mailx -s "SNMPD Down on $servers" $MAIL1;
 fi
done


druuna 02-27-2004 12:57 PM

What's in the other file? Is server_all some sort of output file and server_exclude the file with all the correct host/ip combo's?

I think I know what you are trying to do, but there's not enough info. Give a few short examples, tell us which file holds what, are there many (uniq) hosts/ip combo's. Things like that.

nstanley 02-27-2004 01:02 PM

Servers_all: comma delimited file containing hostname, and corresponding IP

Servers_exclude: list of hostnames, ips that we are not looking to poll, because we know there are problems. It's currently comma delimited, but it does not have to be obviously.

What I want to do is:
1) Goto first line and grab hostname, check to see if it exists in servers_exclude
2) If not, try to poll it for a response. If poll succeeds, move onto next line
3) If no response, poll the corresponding IP (if it does not exist in servers_exclude)
4) If no response to IP, send message stating that 'SNMPD Down on <hostname> <IP>

Thanks so much for any direction you can give me!

druuna 02-27-2004 01:47 PM

Something like this works:

Code:

#!/bin/bash
MAIL1="email@email.com"

IFS=","

cat /export/home/xxx/servers_all | \
while read HOST_NAME HOST_IP
do
  echo $HOST_NAME $HOST_IP
  # Is entry present in servers_exclude
  egrep "$HOST_NAME|$HOST_IP" /export/home/xxx/servers_exclude > /dev/null 2>&1
  if [ "$?" -eq 0 ]
  then
    # nope, poll host
    echo "-----> code to poll $HOST_NAME goes here"
    if [ "$?" -ne 0 ]
      then
      # Could not poll host, trying ip number
      echo "-----> code to poll $HOST_IP goes here"
      if [ "$?" -ne 0 ]
      then
        # Could not poll ip, mail this
        echo "-----> code for mailing goes here"
      fi
    fi
  fi

echo "checking next line"

done

The code does need cleaning/tailoring, but that's up to you :-)

dford 02-27-2004 02:25 PM

I believe this only works if you change server all to space delimited. Otherwise the read takes all of the data for the HOST_NAME. That is if the format is:
host1,10.32.120.128
host2,10.32.120.120

Otherwise, if the format is
host1, 10.32.120.128
host2, 10.32.120.120

HOST_NAME gets host1,
HOST_IP gets 10.32.120.128

But if you just lose the comma altogether:
host1 10.32.120.128
host2 10.32.120.120

then the given code works.

nstanley 02-27-2004 02:28 PM

sorry, code druuna gave me?

Thanks btw....I'm reworking it and will test it soon

dford 02-27-2004 03:36 PM

Sorry, yeah the code druuna gave you. It's good code, just doesn't take into account the comma. Getting rid of the comma is pretty straight forward in perl or awk or sed. So, if you need it comma delimited let me know.

nstanley 02-27-2004 03:37 PM

thanks a bunch...I'm going to tinker around with it, think I'm pretty close...but it's almost quittin time!!

I'll let you know monday how it worked out.

druuna 02-27-2004 04:57 PM

Found the problem. It's not the comma, you need that to seperate the fields in server_all in order to make the while read construct work..

I checked the wrong exit code:

This line:
if [ "$?" -eq 0 ]
Should be
if [ "$?" -ne 0 ]

Sorry about that...........

Just to make sure we have the same input files, I tested with these:

$ cat server_all
aaa,1.2.3.4
bbb,2.3.4.5
ccc,3.4.5.6

$ cat server_exclude
aaa,3.4.5.6

This being the result after running the script:
aaa 1.2.3.4
checking next line

bbb 2.3.4.5
-----> code to poll bbb goes here
checking next line

ccc 3.4.5.6
checking next line

First en third entry from servers_all should not be polled (are in server_exclude, one with hostname the other with ip).

dford 02-28-2004 11:59 AM

Quote:

Originally posted by druuna
Found the problem. It's not the comma, you need that to seperate the fields in server_all in order to make the while read construct work..


You're right. At work I use an older version of bash and there the comma didn't work, but at home on my Linux box it works fine. Must have been a change in the definition of "whitespace" or something.

I'll have to check that out. :study:

nstanley 03-01-2004 09:35 AM

just ran it, and all it looks to be working swelll....thanks so much to all of you fellas1!


All times are GMT -5. The time now is 06:01 PM.