LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-27-2004, 01:40 PM   #1
nstanley
LQ Newbie
 
Registered: Feb 2004
Posts: 5

Rep: Reputation: 0
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
 
Old 02-27-2004, 01:48 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
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

Last edited by david_ross; 02-27-2004 at 01:50 PM.
 
Old 02-27-2004, 01:57 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
Old 02-27-2004, 02:02 PM   #4
nstanley
LQ Newbie
 
Registered: Feb 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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!
 
Old 02-27-2004, 02:47 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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 :-)
 
Old 02-27-2004, 03:25 PM   #6
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
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.
 
Old 02-27-2004, 03:28 PM   #7
nstanley
LQ Newbie
 
Registered: Feb 2004
Posts: 5

Original Poster
Rep: Reputation: 0
sorry, code druuna gave me?

Thanks btw....I'm reworking it and will test it soon
 
Old 02-27-2004, 04:36 PM   #8
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
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.
 
Old 02-27-2004, 04:37 PM   #9
nstanley
LQ Newbie
 
Registered: Feb 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 02-27-2004, 05:57 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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).
 
Old 02-28-2004, 12:59 PM   #11
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
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.
 
Old 03-01-2004, 10:35 AM   #12
nstanley
LQ Newbie
 
Registered: Feb 2004
Posts: 5

Original Poster
Rep: Reputation: 0
just ran it, and all it looks to be working swelll....thanks so much to all of you fellas1!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
separating a comma delimited line mrobertson Programming 7 07-27-2005 02:56 PM
Comma-Delimited Website Filenames Apocalypse General 1 11-09-2003 10:05 AM
Parsing a tab delimited text file jajanes Programming 9 08-08-2003 11:34 AM
comma delimited file cdragon Programming 5 06-21-2002 08:55 PM


All times are GMT -5. The time now is 08:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration