Monit is a process monitor (and much more) that automatically restarts services when they are down. Sadly the version included in EPEL repository is quite old and doesn't support all the directives we want to use. We are going to install it through yum anyway, so we get the configuration and init scripts shiped with it:
yum install monit
To fetch the newest version directly from the developer's website, proceed with these steps:
cd /usr/local/src
wget http://mmonit.com/monit/dist/binary/5.8.1/monit-5.8.1-linux-x64.tar.gz
tar -xzvf monit-5.8.1-linux-x64.tar.gz
and move it to our location for non-distribution packages:
mv /usr/local/src/monit-5.8.1 /usr/local/pkgs
Consulting the Appendix, we can use graft
to link it into our $PATH
. Before we do that, we create a .graft-exclude
file that instructs graft
, which files and directories we want to symlink:
nano /usr/local/pkgs/monit-5.8.1/.graft-exclude
CHANGES
COPYING
conf
followed by issuing:
cd /usr/local/pkgs
graft -i -P monit-5.8.1
The last three steps include changing the default configuration file as well as fixing the init script to use our custom monit binary.
First, change some of the default configuration options:
nano /etc/monit.conf
set daemon 30 # check services at 30 sec intervals
with start delay 30 # optional: delay the first check by another 30 sec (by
# default Monit check immediately after Monit start)
set mailserver localhost # primary mailserver
set alert root@localhost # receive all alerts
set httpd port 2812 and
use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
A set of configuration files for various services can be found in the Appendix.
Secondly, fix the init script:
nano /etc/init.d/monit
#!/bin/bash
#
# Init file for Monit system monitor
# Written by Stewart Adam <[email protected]>
# based on script by Dag Wieers <[email protected]>.
#
# chkconfig: - 98 02
# description: Utility for monitoring services on a Unix system
#
# processname: monit
# config: /etc/monit.conf
# pidfile: /var/run/monit.pid
# Short-Description: Monit is a system monitor
# Source function library.
. /etc/init.d/functions
### Default variables
CONFIG="/etc/monit.conf"
pidfile="/var/run/monit.pid"
prog="monit"
binary="/usr/local/bin/$prog"
# Check if requirements are met
[ -x "$binary" ] || exit 1
[ -r "$CONFIG" ] || exit 1
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon $binary -c $CONFIG
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Shutting down $prog: "
killproc -p ${pidfile}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
$binary -c "$CONFIG" reload
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
And lastly, create an alias around the monit
binary so one does not have to specify the system configuration file every time:
nano ~/.bash_aliases
alias monit='monit -c /etc/monit.conf'
Please note that installing through
yum
is not required - and only done to steal the configuration file, logrotate settings etc. If you prefer doing everything by hand, feel free to do so.