tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

"rcutil" - a script to monitor status of daemons started in rc.d



Attached is a script that checks to see what daemons in rc.d have
rcvar=YES, and then runs "/etc/rc.d/XXXX status" to see if the daemon
is running.  I use this to "sanity check" a box, like after it's run
out of swap and has randomly killed who-knows-what.  It can also be run
out of cron or snmpd to test whether all the configured daemons on a box
are running.

Since the original version was released in 2005, it's been updated to
do some extra parsing for cases where the rcvar name in the script is
not the same as the filename (like "nfs_server" in rc.d/nfsd).

The careful consistency and code re-use of the NetBSD rc.d system is
what makes this utility possible.  

        -- Ed
#!/bin/sh

# $Id: rcutil,v 1.2 2009/12/21 04:18:52 root Exp $

set -u

# See if all the services started in /etc/rc.d are actually running,
# and optionally restart them if they are down.
# Exit status is non-zero if a service was "not running", otherwise zero

USAGE="Usage: $0 {showyes | showdown | fixdown}"

if [ "${DEBUG_RCUTIL:-no}" = YES ]
then
        set -x
fi

command=${1:-help}
case $command in
        showyes|showdown|fixdown) ;;
        *) echo $USAGE; exit 23;;
esac

RCORDER=/sbin/rcorder
RCDIR=/etc/rc.d

rcbad=0

set +u
. /etc/rc.conf
set -u

for rcscript in $($RCORDER $RCDIR/*)
do
        rcname=${rcscript##*/}

        case $rcname in
        *[!_a-zA-Z0-9]*) continue;;  # filename is an illegal variable name, 
skip
        esac

        rcvar=$rcname
        rcyes=false

        eval rcvarsetting=\${$rcname:-UNSET}

        case $rcvarsetting in

        YES)  rcyes=true ;;
        NO)   ;;
        UNSET)
        # check for scripts whose filenames don't match $rcvar
                if egrep "^[    ]*rcvar=" $rcscript >/dev/null
                then
                        rcvarline=$(egrep "^[   ]*rcvar=" $rcscript | head -1)
                        rcvarval=$(echo "$rcvarline" | cut -f2 -d=)
                        if expr "$rcvarval" : '.*[`$]' > /dev/null;  # no vars 
here, please
                        then :
                        else
                                rcvarval=$(echo $rcvarval | tr -d '"'"'")  # 
kill quotes
                                if eval [ \${$rcvarval:-NO} = YES ]
                                then
                                        rcyes=true
                                        rcvar=$rcvarval
                                fi
                        fi
                fi
                ;;
        *)      echo "$0: internal error - cannot evaulate $rcname" ;;
        esac

        if $rcyes
        then
                case $command in
                showyes) 
                        echo $rcvar=YES ;;
                showdown)
                        if $rcscript status 2>&1 | grep -iw 'not running'
                        then
                                rcbad=1
                        fi
                        ;;
                fixdown)
                        if $rcscript status 2>&1 | grep -iw 'not running'
                        then
                                $rcscript start
                                rcbad=2
                        fi
                        ;;
                esac
        fi
done

exit $rcbad

#################################

=head1 NAME

rcutil - show or restart rc.d services that are not running

=head1 SYNOPSIS

B<rcutil>  {I<showyes> | I<showdown> | I<fixdown>}

=head1 DESCRIPTION

B<rcutil> tests all services that have been configured to start at boot
time using the I</etc/rc.conf> configuration file and the I<rc.d>
startup scripts, and complains if any of them are not running.  <rcutil>
can also display a list of services that are supposed to be running,
or automatically restart a service that is not running but should be.

=head1 INVOCATION

=item B<rcutil showyes>  List all services that are configured to start
at boot time.  The display is similar to that of the B<rcvar> command to
an I<rc.d> script.

=item B<rcutil showdown>  List all services that are not running, but
should be.  They will only display if the I<rc.d> script prints a message
of the form "XXXXX is not running."

=item B<rcutil fixdown>  Like B<showdown>, but also runs the I<rc.d>
script to start a service that was found to be not running.

=head1 RETURN VALUE

B<rcutil showdown> and B<rcutil fixdown> will return non-zero (false) if
any "down" services were found.

=head1 NOTES

Some I<rc.d> script authors use an internal name for for the service that
does not match the filename of the script.  B<rcutil> attempts to catch
this condition but the test may not be exhaustive.

=head1 SEE ALSO

rc.conf(5), rc.subr(8), rcorder(8), rc(8)

=head1 AUTHOR

B<rcutil> was written by Ed Ravin <eravin%panix.com@localhost>, and is made
available to the public by courtesy of PANIX (http://www.panix.com).
This script is licensed under the GPL.



Home | Main Index | Thread Index | Old Index