[mrtg] {partial solution} RE: Re: Threshold examples

Justin Shore listuser at vinnie.ksu.ksu.edu
Mon Feb 7 12:23:06 MET 2000


Anthony,
	I spent a few hours tonight and wrote a nice shell script to 
help regulate the Threshold error e-mail.  It works on my setup just 
fine (be warned though, $CONFIG_FILE doesn't work yet.  Use the path 
instead where applicable).  I've added variables at the top that 
users can easily edit and comments to describe each of them.  If 
people find it useful, I may add it to the MRTG contrib.
	While I was writing the text of the email to be sent I 
thought to myself that the script could be reusable for multiple 
Targets if MRTG would just return more variables.  It returns 
$THRESH_DESC which is a start but it should be able to return a lot 
more.  Even if the variable means nothing to MRTG is could still be 
defined and returned to the external program.  Possible variables 
would be the time MRTG encountered the Threshold breach, URL for the 
page to view the graph, the hostname that MRTG was running on, 
MaxBytes and AbsMax.  There are many more that could be used; it just 
depends on how much you want to customize your e-mail.  As it stands 
now, I have to duplicate that script 5 times for each of our dialin 
pools when I could have just made it once.  Anyways, that's a feature 
suggestion for the author.  Here's the script so far.  Again, 
$CONFIG_FILE doesn't work yet.  Use the path instead where 
applicable.  Let me know what you think of it.

Justin

#!/bin/bash
# This was written by Justin Shore on 2/7/00.
# Sorry, I don't have a name for it yet. :-)
#
# What is does is e-mail a user-defined e-mail to a user defined address
# when a certain MRTG's Threshold features call it.  While this can be acheived
# via a simple command line, you can't define how often you want MRTG to e-mail
# you.  This script takes care of that for you.  You define via cron how often
# you want MRTG to run.  You define within this script how many iteration you
# want the script to ignore before sending out any subsequent messages.  Let's
# say for example that you run your MRTG config through every 3 minutes.  If
# you define the maximum number of iterations to be 20, this script will have
# to be called 20 times before it will send out another message.  20 iterations
# times 3 minutes equals an hour.  From that you can define how often you
# want MRTG to send you a message about your exceded Threshold.  You must
# configure your mrtg config file properly for Threshold checking.  Look at
# this page for help.
# http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/config.html#Threshold_Checking
# Your config must have similar lines for the target you want to Threshol
# monitor:
#
#ThreshDesc[your_target]: "A brief description"
#ThreshProgI[your_target]: scripts/your_target.script
#
# You must define ThreshDesc or remove it from the script below.  You will also
# have to adjust the spacing if you remove it.  You also *must* specify the
# following line:
#
#ThreshProgOKI[your_target]: scripts/your_target.script-remove
#
# In that file put "rm -f ~/scripts/elk-pool-mail.count" and make it executable
# or the count file will throw off this script the next MRTG asks it to run.
# Replace "your_target" with your Target name.  Replace the paths and filenames
# above with your own also.
# MRTG <http://www.ee.ethz.ch/~oetiker/webtools/mrtg/mrtg.html>

# This is the file that will hold the current number of iterations MRTG has
# been told to skip before sending the followup e-mail.
# Important!  Uh, this may or may not work.  Give it a try.  If it hurls a slew
# of errors at you, replace every $COUNT_FILE with your own path. :-)
COUNT_FILE="~/scripts/your_target.count"

# You don't want MRTG to e-mail you every single time it runs and the threshold
# has been met (which is every 3 minutes on my setup) define a number of cycles
# you want MRTG to skip before it e-mails you.
# (i.e. 20 cycles at 3 minutes each is 1 hour)
MAX_COUNT="20"

# Since I wrote this to e-mail me when the maximum number of dialin ports were
# used on one of our terminal servers, I also worded the warning e-mail that
# way.  Word you message however you like to suit your needs.
MAX_USERS="24"

# This is the address that the message will be sent to.
CONTACT_EMAIL="your_contact at e-mail.address"

# The current date in the format The date in the format "2:54 AM Mon Feb 07"
#  (no quotes).  DO NOT comment this out unless you intend on removing it from
# $INITIAL_MESSAGE and $FOLLOWUP_MESSAGE
DATE=`date '+%l:%M %p %a %b %d' |awk '{print $1, $2, $3, $4, $5}'`

# Subject line for the initial e-mail you're sending.  Make it something that
# stands outs a bit.
INITIAL_SUBJECT_LINE="[MRTG] $THRESH_DESC Maximum reached"

# Subject line for the followup e-mail you're sending.  Make it something that
# stands outs a bit.
FOLLOWUP_SUBJECT_LINE="[MRTG] Reminder, $THRESH_DESC Maximum reached"

# If you don't define the fully qualified hostname here, it will be assigned
# later on in the script. The only problem produced by *not* specifying it here
# is that $HOST won't be printed in $INITIAL_MESSAGE or either of the $FOLLOWUP
# messages.
HOST="vinnie.ksu.ksu.edu"

# This is the URL you want printed in the body of the message.  Its customary
# to use the same URL as the MRTG Target that produced the Threshold error.
URL="http://$HOST/path_to_the_mrtg_page.html"

# Enter the body of the initial e-mail that you want to send here.  This is
# the message that is sent the first time the Threshold is breached.
#
# Below is a list of variables you can use within you message.
INITIAL_MESSAGE="
This message is an automated repsonse from MRTG running on '$HOST'.
It was sent to let you know that at '$DATE' '$THRESH_DESC' reached
its max limit of $MAX_USERS simultaneous users.

$URL"

# Enter the top half body of the followup e-mail that you want to send here.
# This is only the top half.  You enter the bottom half just below.  Between
# the two the initial time that the problem was encountered is printed.
# This is the message that is sent after the time you specified has passed,
# assuming the Threshold is still breached and it didn't fall below that
# Threshold since the first breach.  The time passed is calculated on the
# frequency MRTG running on your system and $MAX_COUNT.
# (i.e.  how often you run MRTG in minutes * $MAX_COUNT = how often you'll
# recieve the warning e-mail.
#
# Below is a list of variables you can use within you message.
FOLLOWUP_MESSAGE_TOP="
This followup message is an automated repsonse from MRTG running on
'$HOST'.  It was sent to remind you that at:
"

# Enter the bottom half of the followup message.
FOLLOWUP_MESSAGE_BOTTOM="
'$THRESH_DESC' reached its max limit of $MAX_USERS simultaneous users.

$URL
"

# $DATE  The date in the format "2:54 AM Mon Feb 07" (no quotes)
# $HOST  The fully-qualified domain name composed of $HOSTNAME.$DNSDOMAINAME.
# The rest are self explanitory or are explained above.
# $HOSTNAME
# $DNSDOMAINNAME
# $CONTACT_EMAIL
# $URL
# $MAX_USERS
# $MAX_COUNT
# $SUBJECT_LINE

#------------------------------------------------------------------------------
# Unless you were told to modify something below this line, or unless you know
# what you're doing...
#
#    Don't modify anything below this line.
#------------------------------------------------------------------------------

# piped through awk to eliminate the annoying space at the beginning.
DATE=`date '+%l:%M %p %a %b %d' |awk '{print $1, $2, $3, $4, $5}'`
HOSTNAME=`hostname`
DNSDOMAINNAME=`dnsdomainname`
HOST="$HOSTNAME.$DNSDOMAINNAME"

if [ -f ~/scripts/elk-pool-mail.count ];
  then
    CURRENT_COUNT=`cat ~/scripts/your_target.count |awk '{print $1}'`
    START_DATE=`cat ~/scripts/your_target.count |awk '{print $2, $3, 
$4, $5, $6}'`
    echo $CURRENT_COUNT;
    echo $START_DATE;
    NEW_COUNT=`expr "$CURRENT_COUNT" \+ 1`;
    echo "$NEW_COUNT  $DATE" > ~/scripts/your_target.count;
    echo $NEW_COUNT;
    if [ $NEW_COUNT -eq $MAX_COUNT ];
     then {
       echo $NEW_COUNT;
       mail -s "$FOLLOWUP_SUBJECT_LINE" $CONTACT_EMAIL << EOM \

$FOLLOWUP_MESSAGE_TOP
       $START_DATE
$FOLLOWUP_MESSAGE_BOTTOM
EOM
     }
     echo "1  $START_DATE" > ~/scripts/your_target.count;
    fi
   else {
     mail -s "$INITIAL_SUBJECT_LINE" $CONTACT_EMAIL << EOM \

     $INITIAL_MESSAGE
EOM
     CURRENT_COUNT="1"
     echo "$CURRENT_COUNT  $DATE" > ~/scripts/your_target.count;
   }
fi

--
Justin Shore
K-State Linux Distro Mirror, Sysadmin
macdaddy at vinnie.ksu.ksu.edu
<http://vinnie.ksu.ksu.edu/mirror/rpm2html>
<ftp://vinnie.ksu.ksu.edu/pub/mirror/linux>

--
Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:mrtg-request at list.ee.ethz.ch?subject=help
Archive     http://www.ee.ethz.ch/~slist/mrtg



More information about the mrtg mailing list