Subject: "rcutil" - a script to monitor status of daemons started in rc.d
To: None <tech-userlevel@netbsd.org>
From: Ed Ravin <eravin@panix.com>
List: tech-userlevel
Date: 08/31/2005 22:50:48
--6TrnltStXW4iwmi0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

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.  You could
also run it out of cron or snmpd to see whether the expected daemons
are running.

I only tested it on NetBSD 2.0, but it should work on current as well.

To see the man page, run perldoc on the file.  Invoke it with
"rcutil showdown" to see if anything isn't running, or "rcutil
fixdown" to run the "rc.d/XXXX start" script for anything that is
found to be down.

Comments and suggestions welcome.

	-- Ed

--6TrnltStXW4iwmi0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=rcutil

#!/bin/sh

# $Id$

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}"

command=$1
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

	if eval [ \${$rcname:-NO} = YES ]
	then
		case $command in
		showyes) 
			echo $rcname=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 SEE ALSO

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

=head1 AUTHOR

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


--6TrnltStXW4iwmi0--