[mrtg] MRTG (off-list): cronwrap and launcher scripts
Nick Price
np121 at hotmail.com
Fri Jan 6 13:27:52 CET 2017
Hi Niall
Thanks for the help
I have been out of the office for a few days I plan to look at this next
week.
Just a thought for the MRTG programmers.
Might it not be an idea to update MRTG so that it works with a default
install of perl as every new release or install I see a flurry of the same
type of emails.
Be nice to have it fixed once and for all
Nick
-----Original Message-----
From: Niall O'Reilly [mailto:niall.oreilly at ucd.ie]
Sent: Tuesday, January 03, 2017 18:51
To: Nick Price <np121 at hotmail.com>
Subject: MRTG (off-list): cronwrap and launcher scripts
Hi, Nick.
A former colleague hunted these scripts down for me.
They used to work on a number of RHEL systems; I expect they'll
not need any changes for Fedora.
The first script (cronwrap) catches the output from a cron job
in a temporary file, tests for success, "unhides" the output
on failure, and finally discards the temporary file. As cron
generates a mail message only when there is actually some output,
this may be all you need.
The MRTG installation guide has a number of suggested crontab
entries, including this one:
*/5 * * * * mrtg-user <mrtg-bin>/mrtg <path to mrtg-cfg>/mrtg.cfg \
--logging /var/log/mrtg.log
If you want to use cronwrap, then you'll need to change the crontab
entry to something like this:
*/5 * * * * mrtg-user /usr/local/bin/cronwrap <options> <mrtg-bin>/mrtg
<path to mrtg-cfg>/mrtg.cfg \
--logging /var/log/mrtg.log
The cronwrap script can be run from the command line to launch
some harmless command ('date' is my favourite) as a test. It
expects that there is a utility program called 'logger' on the
system; I believe this is present by default in RedHat-family
systems.
The '-m' and '-w' options may be of use to you. The former
sets file protection mask for new files, with 077 for owner-only
access as the default. The latter sets the working directory.
The second script (/etc/init.d/mrtg) may be used as a launcher
script for running MRTG in daemon mode. I see that three lines
in this script were wrapped already when my colleagues message
reached me; these are in the status() function of the second
script, and are fairly obvious. More wrapping may be result
from my forwarding the message to you. Beware.
I'm not certain that this launcher script will run cleanly on
your Fedora system, but expect it most likely will. It allows
multiple instances of MRTG to be started, each with its own
configuration file; I found this approach useful in a large
installation where certain devices could be slow to respond
and could stall MRTG's scanning pass while it waited for them.
If it works for you, it will work for just one instance too,
of course! It has the quirk of assuming that MRTG configuration
file names end in '.conf' rather than '.cfg'; if this is a
nuisance, you can replace the line
CFGTYP=.conf
with
CFGTYP=.cfg
Another use for multiple configurations can arise when testing
unusual SNMP references or when during commissioning of new,
unfamiliar devices.
Good luck!
Niall O'Reilly
Forwarded message:
> From: Concealed Former Colleague <former-colleague at example.com>
> To: Niall O'Reilly <niall.oreilly at ucd.ie>
> Subject: Re: Something completely different
> Date: Tue, 3 Jan 2017 17:00:50 +0000 (GMT Standard Time)
>
> Hi Niall,
>
> On Tue, 3 Jan 2017, Niall O'Reilly wrote:
>
>> If you happened, without inconvenience, to come across my 'cronwrap'
>> script or the init-script for MRTG, I'ld appreciate it if you passed
>> (either of) them on.
>
[...]
>
> Both scripts below. Some lines may be wrapped.
>
>
> [fergal at kirikee fergal]$ cat /usr/local/bin/cronwrap #!/bin/sh
>
> umask 077
> PROG=`basename $0`
> JOBID=$$
> LOGTO=/tmp/log.$JOBID
> WORKDIR=
> VERBOSE=
>
> logopt=
>
> # Strip options
> ready=
> until [ "$ready" ]
> do
> case "$1" in
> --) shift; ready=yes;;
> -m) shift; umask $1; shift;;
> -v) shift; VERBOSE=yes;;
> -w) shift; WORKDIR=$1; shift;;
> -*) echo "$PROG: illegal option: $1"; exit 1;;
> *) ready=yes;;
> esac
> done
>
> if [ "$WORKDIR" ] ; then
> if cd $WORKDIR ; then true
> else
> logger -s -p user.error \
> -t "cronwrap: `id -un`,`pwd`" "Can't cd to $WORKDIR for
> $*"
> exit 1
> fi
> fi
>
> WORKDIR=`pwd`
>
> if "$@" >$LOGTO 2>&1 ; then
> [ "$VERBOSE" ] && logopt='-s'
> logger $logopt -p user.info \
> -t "cronwrap: `id -un`,`pwd`" "$* [ OK ]"
> else
> logger -s -p user.error \
> -t "cronwrap: `id -un`,`pwd`" "$* [ FAILED ]"
> cat $LOGTO
> fi
>
> rm -f $LOGTO
>
>
>
>
>
> [fergal at kirikee fergal]$ cat /etc/init.d/mrtg #!/bin/bash #
> # mrtg This shell script takes care of starting and
> stopping
> # mrtg (Tobi Oetiker's wonderful graphing tool)
> #
> # chkconfig: 2345 30 70
> # description: MRTG system and network monitoring tool
>
> # -- defaults --
> MRTG=/usr/bin/mrtg
> BASE=`basename ${MRTG}`
> LOGDIR=/var/log/
> CFGDIR=/usr/local/etc/mrtg/
> CFGTYP=.conf
> LOCKTAG=.pid
> OPTIONS=''
>
> [ -f /etc/sysconfig/mrtg ] && . /etc/sysconfig/mrtg
>
> [ "${MRTG_USER}" ] && OPTIONS="${OPTIONS} --user ${MRTG_USER}"
> [ "${MRTG_GROUP}" ] && OPTIONS="${OPTIONS} --group ${MRTG_GROUP}"
>
> CMD=$1
> INSTANCE=$2
>
> # Source function library.
> . /etc/rc.d/init.d/functions
>
> # Source networking configuration.
> . /etc/sysconfig/network
>
> # A function to find the pid of an instance of MRTG.
> pidofinstance() {
> program=$1
> instance=$2
> base=`basename $program`
> lockfile=${CFGDIR}${instance}${LOCKTAG}
> gotpid=''
> warn=''
>
> # Test syntax.
> if [ $# -lt 2 ] ; then
> echo >&2 "Usage: pidofinstance {program} {instance}"
> return 1
> fi
>
> # Look for running pid with specified instance
> # Start with per-instance lock file
> if [ -f "${lockfile}" ] ; then
> gotpid=`cat $lockfile`
> if [ ! \( "${gotpid}" -a -d "/proc/${gotpid}" \) ]
> then gotpid=''
> fi
> # Check for running process; lock file might be stale
> [ "${gotpid}" ] && \
> grep -q "${instance}.conf" "/proc/${gotpid}/cmdline"
> if [ $? -ne 0 ]
> then gotpid=''
> fi
> fi
>
> # Now have real PID or empty string
> if [ "${gotpid}" ] ; then
> echo "${gotpid}"
> return 0
> else
> return 1
> fi
> }
>
> start() {
> base=`basename $1`
> echo -n "Starting ${base} ($2)"
> pid=`pidofinstance $1 $2`
> if [ $pid ] ; then
> echo
> echo " ${base} ($2) is already running (pid ${pid})"
> RETVAL=1
> elif [ -f ${CFGFILE} ] ; then
> MRTG_CONFIG="${CFGFILE}" \
> LANG=C \
> ${MRTG} ${OPTIONS} ${CFGFILE} >${LOGFILE} 2>&1
> RETVAL=$?
> else
> echo
> echo " missing configuration file: ${CFGFILE}"
> RETVAL=1
> fi
> [ ${RETVAL} -eq 0 ] && success || failure
> echo; return ${RETVAL}
> }
>
> stop() {
> base=`basename $1`
> echo -n "Stopping ${base} ($2)"
> pid=`pidofinstance $1 $2`
> if [ $pid ] ; then
> kill ${pid}
> [ -f ${LOCKFILE} ] && rm -f ${LOCKFILE}
> RETVAL=0
> else
> echo
> echo " ${base} ($2) was not running"
> RETVAL=1
> fi
> [ ${RETVAL} -eq 0 ] && success || failure
> echo; return ${RETVAL}
> }
>
> status() {
> base=`basename $1`
> pid=`pidofinstance $1 $2`
> if [ "$pid" ] ; then
> scr='unknown-script'
> ver='unknown-version'
> scr=`perl -ne 'printf "%s\n", (split /\0/)[-2]'
> /proc/$pid/cmdline`
> [ -r "$scr" ] && \
> ver=`perl -ne 'if (/^# MRTG/) {printf ("%s\n",
> (split)[2]); last}' $scr`
> printf ' %5d running %s from %s (%s)\n' "${pid}" "$2"
> $scr
> $ver
> [ -f ${LOCKFILE} ] || echo " file not found:
> ${LOCKFILE}"
> else
> printf ' %5s running %s\n' 'not' "$2"
> fi
> }
>
> runcmd() {
>
> CFGFILE=${CFGDIR}${INSTANCE}${CFGTYP}
> LOCKFILE=${CFGDIR}${INSTANCE}${LOCKTAG}
> LOGFILE=${LOGDIR}${INSTANCE}
>
> case "$CMD" in
> start)
> start ${MRTG} ${INSTANCE}
> ;;
>
> stop)
> stop ${MRTG} ${INSTANCE}
> ;;
>
> status)
> status ${MRTG} ${INSTANCE}
> ;;
>
> restart)
> stop ${MRTG} ${INSTANCE}
> sleep 1
> start ${MRTG} ${INSTANCE}
> ;;
>
> esac
>
> }
>
>
> # Check that networking is up.
> [ ${NETWORKING} = "no" ] && exit 0
>
> # Check for code and config files
> [ -f ${MRTG} ] || exit 0
>
> RETVAL=0
>
> echo
>
> if [ -z "${INSTANCE}" ]
> then
> for x in ${CFGDIR}*${CFGTYP}
> do INSTANCE=`basename $x ${CFGTYP}`
> runcmd ${INSTANCE}
> done
> else
> runcmd ${INSTANCE}
> fi
More information about the mrtg
mailing list