Especially with Tomcat startup monitoring tends to be less than reliable. In my experience, it starts up okay, but if there is a problem, it exits after a few seconds or minutes -- depending on the exact cause. (In my case, the culprit is usually not really Tomcat, but the 'ware running in it.)
Consider adding a monitoring script to crontab, so that it is run regularly. Keep a status file in e.g. /var/run/monitor.status, so that you only send mail when the services' status changes. Use the
sendmail command to send a digest message (a summary of all services) whenever service status changes. Use the fact that
/etc/init.d/service status will return exit status 0 (success) if it is running, and nonzero (error) otherwise.
Here is an untested example script. You need to run it as root; probably the best place to add it is in system crontab.
Code:
#!/bin/bash
servicelist=/etc/monitor.services
statusfile=/var/run/monitor.services
recipient='your.email@example.com'
[ -f "$servicelist" ] || exit 0
# Temporary file for the status report. Autodeleted.
newstatus=$(mktemp)
trap "rm -f '$newstatus'" EXIT
# Generate new service status table
while read service title ; do
[ -n "$service" ] || continue
[ -n "$title" ] || continue
[ -x "/etc/init.d/$service" ] || continue
if "/etc/init.d/$service" status &>/dev/null ; then
printf '%60s: OK\n' "$title"
else
printf '%60s: Not running\n' "$title"
fi
done <"$servicelist" >"$statusfile"
# Status changes?
if cmp -s "$newstatus" "$statusfile" ; then
# No. Update service list timestamp, then exit.
touch "$statusfile"
rm -f "$newstatus"
exit 0
fi
# Status has changed. Send mail.
mv -f "$newstatus" "$statusfile"
( echo "\"$(hostname -s) service monitor\" <root@$(hostname -f)>"
echo "To: $recipient"
echo "Subject: $(hostname -s) service report"
echo ""
date
echo ""
cat "$statusfile"
echo "."
) | sendmail -bm -t
The service list file
/etc/monitor.services contains service names, one per line, followed by the respective title in the report. Services not installed are ignored. For example:
Code:
ssh SSH server
apache2 HTTP server (apache2)
tomcat HTTP server (tomcat)
httpd HTTP server (httpd)
cups CUPS print server
It is just an example of such monitoring script, you probably have to edit it to suit your needs (and perhaps even to get it working, since I did not test it). The idea of this script is that it will only send an e-mail if the services' status has changed from the last run. Since it is very lightweight, you could run it every couple of minutes or so, without ill effects.
You can see last status summary always in the status file,
/var/run/monitor.services, and the time of last check from the timestamp of that file.
If you have Apache running on the same machine, you could consider running
Code:
#!/bin/sh
echo "Content-type: text/plain"
echo ""
stat -c %y /var/run/monitor.services
echo ""
cat /var/run/monitor.services
as a CGI script; it will always show the latest status report (and its time) in plain text format. With a bit more script, you could have it emitted as a nice HTML table, too, say automatically reloading (using meta refresh) every half update interval?
I hope you find this useful,