LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 06-07-2012, 01:43 AM   #1
arun5002
Member
 
Registered: Aug 2011
Location: Chennai,India
Distribution: Redhat,Centos,Ubuntu,Dedian
Posts: 549
Blog Entries: 5

Rep: Reputation: Disabled
Need Help of shell scripts


Hi all,
I Had written a script to monitor the service down event ,When node down i require only mail to be received for just half hour and then mail alert should be stopped even node is down.Because it was filling up my my mailbox rapidly.Can anyone help me how can i sleep the monitor process after half hour


# script -- Purpose: Script to monitor the service down events

# Exits with zero if no error.
#Step -1 scripts to alert if httpd service fails to startup during execution of scripts with log status

# Step -2 MAIL ID of person whose going to get mail alert
mailid1=xx@gmail.com
mailid2=xx@gmail.com


SERVICE='httpd'

if ps ax | grep -v grep | grep $SERVICE > /dev/null

then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
echo "$SERVICE is not running!" | mail -s "$SERVICE down" $mailid1 $mailid2 < /etc/httpd/logs/error_log
fi

#Step -2 scripts to alert if Tomcat service fails to startup during execution of scripts with log status

SERVICE='tomcat'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
echo "$SERVICE is not running!" | mail -s "$SERVICE down" $mailid1 $mailid2 < /opt/apps/tomcat/apache-tomcat-6.0.26/logs/catalina.out
fi

#Step -3 scripts to alert if Tomcat service fails to startup during execution of scripts with log status

SERVICE='mysqld'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
echo "$SERVICE is not running!" | mail -s "$SERVICE down" $mailid1 $mailid2
fi

Last edited by arun5002; 06-07-2012 at 02:22 AM.
 
Old 06-07-2012, 03:14 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6
Posts: 1,473

Rep: Reputation: 438Reputation: 438Reputation: 438Reputation: 438Reputation: 438
Ok, change your alerting code from this:
Code:
if ps ax | grep -v grep | grep $SERVICE > /dev/null
 
then
 echo "$SERVICE service running, everything is fine"
 else
 echo "$SERVICE is not running"
 echo "$SERVICE is not running!" | mail -s "$SERVICE down" $mailid1 $mailid2 < /etc/httpd/logs/error_log
 fi
To something like this: It means it'll only send 30 alerts, so assuming you are doing the checking once a minute then it'll stop after 30 minutes.

Code:
if ps ax | grep -v grep | grep $SERVICE > /dev/null
 then
   echo "$SERVICE service running, everything is fine"
   echo "0" > /tmp/${SERVICE}.COUNT                # Zero the alert counts
 else
   echo "$SERVICE is not running"

   ALERTCOUNT=`cat /tmp/${SERVICE}.COUNT`          # Find the number of alerts already sent
   if [ "${ALERTCOUNT}" -le "30" ] ; then          # If it's under 30 alerts we send do this.
     echo `expr ${ALERTCOUNT} + 1` > /tmp/${SERVICE}.COUNT # Increase the alert count and put it back in the file.     
     # Sent alert as per existing code
     echo "$SERVICE is not running!" | mail -s "$SERVICE down" $mailid1 $mailid2 < /etc/httpd/logs/error_log
   fi
fi
 
Old 06-07-2012, 03:22 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,310
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
Quote:
Originally Posted by arun5002 View Post
if ps ax | grep -v grep | grep $SERVICE > /dev/null
Quote:
Originally Posted by TenTenths View Post
Code:
if ps ax | grep -v grep | grep $SERVICE > /dev/null
...you mean
Code:
pgrep $SERVICE >/dev/null 2>&1; case $? in # etc, etc
(or if you're fetish is 'ps' then use at least 'ps -C $SERVICE' ;-p)

Last edited by unSpawn; 06-07-2012 at 03:23 AM.
 
Old 06-07-2012, 03:28 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6
Posts: 1,473

Rep: Reputation: 438Reputation: 438Reputation: 438Reputation: 438Reputation: 438
Quote:
Originally Posted by unSpawn View Post
...you mean
Code:
pgrep $SERVICE >/dev/null 2>&1; case $? in # etc, etc
(or if you're fetish is 'ps' then use at least 'ps -C $SERVICE' ;-p)
I left all that part of the OP's code intact, I merely added the counting loop
 
Old 06-07-2012, 11:22 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
More than 200 posts here and you still don't know how to use [code][/code] tags?

Even though I've told you about them at least once before?

For shame!
 
  


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
[SOLVED] shell scripts Ramesh Programming 2 11-02-2010 11:49 AM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
shell scripts Coldfan20 Programming 5 08-08-2007 10:50 PM
shell scripts in RT nipunos Linux - Software 0 05-09-2007 02:06 AM
Shell scripts??? F_ANTHONY Programming 2 10-27-2004 06:28 PM


All times are GMT -5. The time now is 09:48 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration