... sorry, the line following 'else' must read
[ -e $LOCK ] && /display/$status/on/web && mailx -s "$SERVICE rearmed" $MAILTO
Hi,
I have wrote a script in bash which shows the status of the unix services as up or down depending on output of snmpwalk command. Basically when the output of snmp command is 0 the service is displayed as UP and when the output is 1 the service is displayed as down on the webpage. Also when the service is down then the script also send a email using sendmail command. The script runs every minute as a cron job. The major flaw of this scripts is till the time the service is down the script keeps sending email every minute.
Now i want that script should send email only once when it find outs that service is down. There should not be any emails till the service comes up again. Once the service is up the count should reset to 0 and when next time the service goes down again the script should send the email again. How can i achieve this ???
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Well,
I don't know where you use to find the information which services to query.
To be able to construct a script nonetheless I assumed it to be in a file.
You could as well do
for SERVICE in $(/query/to/find/the/servi
do
done
or simply tell us what your script looks like now.
Hi Woolmilkproc,
Please see the attach output of my snmpwalk command... And following is the bash code i wrote to get the up and down value from this output...Also please note that the services shown in snmp walk command are defined in snmpd.conf so the snmp will only monitor this 5 services...
#!/bin/bash
serv_array=( $(snmpwalk -v 1 -c public -m all xxx.xxx.xxx.xxx ucdavis.prTable.prEntry.pr
aceserverbe=${serv_array[0
acesyncd=${serv_array[1]}
sdadmind=${serv_array[2]}
sdtacplusd=${serv_array[3]
sdxauthd=${serv_array[4]}
if [ $aceserverbe = 1 ];then
aceserverbe_col="bgcolor=r
aceserverbe_st="DOWN"
elif [ $aceserverbe = 0 ];then
aceserverbe_col="bgcolor=g
aceserverbe_st="UP"
else
aceserverbe_col="bgcolor=y
aceserverbe_st="NO DATA"
fi
echo "<tr><td align=center "$aceserverbe_col">"$aceser
Thanks for the reply woolmilkproc...
So if i run your script as a cron job which will runs every minute then the script will send me the mail only once for each down and up cycle... in other words if a service is down for half hour then script will send me only one "service down "email instead of 30 emails (1 email per minute) and it will send me one "Service Up" email when the service will come back up again. And if suppose service goes down again after few hours then the same thing will repeat.. Right ???
That's how it is designed. I'm pretty sure that it will work (but who knows ...).
Should you find some bugs, let me know!
You could test it by adding "echo" in front of the command "mailx" and by commenting out the "names_array" and "serv_array" lines, and by adding
names_array=( aceserverbe acesyncd sdadmind sdtacplusd sdxauthd )
serv_array=( 0 0 0 0 0 )
Now you can experiment by changing a "0" to "1" or whatever in "serv_array"
An echoed line "mailx..." will indicate that mail would have been sent, if nothing is echoed - no mail.
Business Accounts
Answer for Membership
by: woolmilkporcPosted on 2009-08-25 at 07:46:07ID: 25178122
Hi,
basically, such a solution could look like this -
#!/bin/bash
# --- Environment --- #
MAILTO=USER@company.com
SE
# --- DOIT --- #
while read SERVICE
do
LOCK=/tmp/${SERVICE}.down
status=$(/path/to/snmp/que
if [ $status -eq 1 ] ; then
[ -e $LOCK ] || /display/$status/on/web || mailx -s "$SERVICE down" $MAILTO
touch $LOCK
else
[ -e $LOCK ] && /display/$status/on/web || mailx -s "$SERVICE rearmed" $MAILTO
rm $LOCK >/dev/null 2>&1
fi
done < $SERVICES
# --- EXIT --- #
exit
I'm sure that your script is by far mor elaborate, the above should just give you an idea how it could be done.
status=$(/path/to/snm p/query/fo r $SERVICE giving 0 or 1) is there to represent your snmp query, which of course I don't know, and /display/$status/on/web stands for your method to update the web page.
Good luck!
Cheers
wmp