?
Solved

Expand bash script for linux into threads

Posted on 2008-11-18
3
Medium Priority
?
389 Views
Last Modified: 2013-12-26
This is a follow up to a question I had last night, where I got pretty good help right away(make a long story short, login to a webserver was solved using PERL)

What I want to expand on now, is as following; we have Nagios 3 running, and AFAIK it uses the exit codes to determine wether or not to raise an alarm.

It is in that spirit I've written this script as posted below.
It works now that if it finds one of the combinations of username/password incorrect in the external script it calls, it will then return an error code, move to the "else" and then post a log, before it exits with code 2 for ERROR

What I want todo, is if it is down, it should exit with error code 2, but before that, also check the 3 others.

How can I tell what error code it is to use at the end if it is to check all 3. Now it stops at the first that fails.

#!/bin/sh
 
echo ""
echo "||----------------------------------------||"
echo "|| GroupWise WebAccess sjekker            ||"
echo "|| Laget av Rune Darrud, BKB Data         ||"
echo "|| Versjon 4 - 18 november, 2008          ||"
echo "||----------------------------------------||"
echo ""
 
LOGGFIL=./logg.txt
GWWEBACC=./webacc
TEMPSAK=./temp.temp
PK1=./postkontor1.sh
PK2=./postkontor2.sh
PK3=./postkontor3.sh
 
# Date
DATE=$(date "+%Y.%m.%d %T")
 
TEXTTING=Login
PK1NAVN="****"
PK2NAVN="****"
PK3NAVN="****"
 
# Checking if the front page exists.
wget -q --no-check-certificate https://webmail/gw/webacc -O $GWWEBACC > $TEMPSAK
grep -q "$TEXTTING" $GWWEBACC
if [ $? -eq 0 ]
then 
 rm $GWWEBACC
 rm $TEMPSAK
else
 rm $GWWEBACC
 rm $TEMPSAK
 exit 2
fi
 
# Checking the first login
perl $PK1 > $TEMPSAK
grep -q "$PK1NAVN" $TEMPSAK
if [ $? -eq 0 ]
then
 rm $TEMPSAK
else
 rm $TEMPSAK
 exit 2
fi
 
# Checking the second login
perl $PK2 > $TEMPSAK
grep -q "$PK2NAVN" $TEMPSAK
if [ $? -eq 0 ]
then
 rm $TEMPSAK
else
 rm $TEMPSAK
 exit 2
fi
 
# Checking the third login
perl $PK3 > $TEMPSAK
grep -q "$PK3NAVN" $TEMPSAK
if [ $? -eq 0 ]
then
 rm $TEMPSAK
else
 rm $TEMPSAK
 exit 2
fi
echo $DATE - OK : ALT OK! >> $LOGGFIL
 
# Rydder opp
exit 0

Open in new window

0
Comment
Question by:TheFlyingCorpse
  • 2
3 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 22989886
Add

ExitCode=0

in the beginning and then change all

exit 2

to

ExitCode=2

then in the end add

exit ExitCode
0
 
LVL 58

Accepted Solution

by:
amit_g earned 2000 total points
ID: 22989901
The last line

echo $DATE - OK : ALT OK! >> $LOGGFIL

should be changed to

if [ $ExitCode -eq 0 ]
then
 echo $DATE - OK : ALT OK! >> $LOGGFIL
else
 exit $ExitCode
fi
0
 
LVL 2

Author Closing Comment

by:TheFlyingCorpse
ID: 31518056
Thanks for your quick reply amit_g,

I was unable to get it to work at first, but after I saw that I had used ExitCode AND EXITCODE (I use variables in capital letters to see them easier), the problem got solved.


Thank you very much!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question