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 10-18-2013, 07:54 PM   #1
dotran
LQ Newbie
 
Registered: Apr 2012
Posts: 27
Blog Entries: 2

Rep: Reputation: Disabled
Please help the this script syntax not loop to multiple servers


Help help.....I want apply to multiple servers but seem not loop can some one help check on this syntax? Thanks

autorep -m server1
Machine Name Max Load Current Load Factor O/S Status
______ __________ ____________ _______ ___________ ______
bogus --- --- 1.00 Sys Agent Missing


SERVERS="server1|server2|server3"


Quote:
#!/bin/ksh

SERVERS="server1|server2|server3"

#for SER in $SERVERS
#do

CHECK_MACHINE=`autorep -m server1 | awk '{print $NF}' | awk 'NR>3' | egrep 'Offline|Missing|Unqualified'`
while true
do
CHECK_MACHINE=`autorep -m server1 | awk '{print $NF}' | awk 'NR>3' | egrep 'Offline|Missing|Unqualified'`
if [ $? = 0 ] ; then
sleep 10; echo "Check the agent is [${CHECK_MACHINE}]" |
mailx -s "WARNING --The agent is [${CHECK_MACHINE}]" abc@test.com
exit 1
fi
done
 
Old 10-18-2013, 08:12 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
that script is a mess

it just does the same thing , over and over.. badly

really what are you trying to do?


Code:
while true
until script is told to stop
Code:
autorep -m server1
run that command, ( I assume it produces some kind of output
Code:
awk '{print $NF}'
print number of fields for each line
Edit, doh, last field,


Code:
awk 'NR>3'
? Number of record ( line number ) greater than 3 ?????
ignore first 3 lines from the results of previous awk

Code:
 egrep 'Offline|Missing|Unqualified'
only print lines that contain the string "Offline" or "Missing" or "Unqualified"
Which will be none, since your first awk results in a list numbers, one per line
Edit, ..

so
CHECK_MACHINE="< some random string containing Offline Missing Unqualified "

Code:
if [ $? = 0 ]
since egrep was last command, and it will never match, it will always exit with 1
so your sleep and mailx will never run

will echo and mail the $CHECK_MACHINE
But this assume the last feild is Offline, Missing or Unqualified
which isn't of any use..
so it probably will still exit 1 like the strike out I typed

go back to while true

Last edited by Firerat; 10-18-2013 at 08:18 PM.
 
Old 10-18-2013, 08:38 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Code:
CHECK_MACHINE=( "$( autorep -m server1 | awk '/Offline|Missing|Unqualified/{if (NR>3){print $3}}' )" )
CHECK_MACHINE is now a bash array
I randomly picked field 3 as 'of interest'

I suspect the skipping lines isn't needed

Code:
CHECK_MACHINE=( "$( autorep -m server1 | awk '/Offline|Missing|Unqualified/{print $3}' )" )
do you want a mail for each result?
or one mail, containing all results per pass?

Code:
while true;do
    CHECK_MACHINE=( "$( autorep -m server1 | awk '/Offline|Missing|Unqualified/{print $3}' )" )
    for MailIt in "${CHECK_MACHINE[@]}";do
        mailx -s "WARNING --The agent is [${MailIt}]" abc@test.com <<< "Check the agent is [${MailIt}]"
        sleep 10 # so you don't spam the mail server?
    done
done
Code:
while true;do
    CHECK_MACHINE=( "$( autorep -m server1 | awk '/Offline|Missing|Unqualified/{print $3}' )" )
    mailx -s "WARNING --The agent is [${CHECK_MACHINE[@]]" abc@test.com <<< "Check the agent is [${CHECK_MACHINE[@]}]"
    sleep 10 # so you don't spam the mail server?
done
that last one is messy for big list
better formatting required



edit, ,, hmm, think I'm tired

ok, you had a exit when you had a result

check the number of elements in the array,. if greater than zero, do your thing then exit

Code:
while true;do
    CHECK_MACHINE=( "$( autorep -m server1 | awk '/Offline|Missing|Unqualified/{print $3}' )" )
    (( "${#CHECK_MACHINE[@]}" > 0 )) && (
        #<insert mail stuff>
        exit 1
    )
    sleep 10
done

Last edited by Firerat; 10-18-2013 at 08:47 PM. Reason: tired
 
  


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
Bash script syntax for while loop chimpsky Programming 2 08-23-2013 12:03 PM
to scp to multiple Linux systems by using for loop script dezavu Red Hat 8 05-23-2012 12:43 AM
how can we use multiple variables in single for loop in shell script nagendrar Linux - Newbie 9 11-13-2011 04:54 AM
Bash syntax help: WHILE loop with multiple comparison operators dz-015 Linux - Software 4 04-21-2009 12:13 PM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 05:08 PM


All times are GMT -5. The time now is 07:11 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