We recently had the need to add SpamAssassin to the list of services SIM monitors. While this is specific to SpamAssassin, it should be readily adaptable to most other system services (exercise left to the reader ):
- First, edit the /usr/local/sim/conf.sim file and add the following variables:
SERV_SPAM="true" # SPAMD Service
SPAM_NAME="spamd" # name of SpamAssassin service as appears in 'ps'
SPAM_PORT="783" # TCP/IP port for SpamAssassin service
SPAM_INIT="/etc/init.d/spamassassin" # path to SpamAssassin service init script
I put each of these variables along with the other variables of the same “type”, just for the sake of organization (e.g., SERV_SPAM went after the other SERV_* variables, SPAM_NAME went after all the other *_NAME variables, and so on)
- Open up /usr/local/sim/sim (this is the sim binary itself, so take care in here), and look for the following line:
## Lets load our service checks
Beneath that line, add the following code block:
if [ "$SERV_SPAM" = "true" ]; then
TIC=$[TIC+1]
TSERVC=$[TSERVC+1]
EVCOUNT_SPAM="0"
. $INSPATH/$CHKSERV/spam.chk
chk_spam
else
EVCOUNT_SPAM="0"
SPAM="module disabled"
fi
- Edit /usr/local/sim/internals/status.sim and look for:
if [ ! "$TSERVC" == "0" ]; then
Beneath that line, add:
if [ ! "$SPAM" == "module disabled" ]; then
echo "SPAM [$SPAM - $EVCOUNT_SPAM events]"
fi
- Create the file /usr/local/sim/internals/dat/spam.dat and put the following inside:
EVC:0
This is the event counter, and will be incremented each time spamassassin is restarted.
- Create the file /usr/local/sim/internals/chk/serv/spam.chk and put the following code inside:
chk_spam() {
SRV="spam"
# Creat/load our $SRV.dat file
srv_dat $SRV
if [ "$LAXCHK" == "true" ]; then
NET_SPAM=`cat $NS_CACHE | grep -w $SPAM_PORT` >> /dev/null 2>&1
PS_SPAM=`cat $PS_CACHE | grep -w $SPAM_NAME` >> /dev/null 2>&1
else
NET_SPAM=`cat $NS_CACHE | grep -w $SPAM_PORT | grep -w $SPAM_NAME` >> /dev/null 2>&1
PS_SPAM=`cat $PS_CACHE | grep -w $SPAM_NAME` >> /dev/null 2>&1
fi
EVCOUNT_SPAM=`cat $INSPATH/$DAT/$SRV.dat | tr ':' ' ' | grep EVC | awk '{print$2}'` >> /dev/null 2>&1
if [ "$NET_SPAM" == "" ]; then
echo "$PREFIX SPAM service is offline." >> $SIMLOG
spam_restart
else
if [ "$PS_SPAM" == "" ]; then
echo "$PREFIX SPAM service is offline." >> $SIMLOG
spam_restart
else
echo "$PREFIX SPAM service is online." >> $SIMLOG
SPAM="online"
fi
fi
}
spam_restart() {
if [ "$EVCOUNT_SPAM" -gt "$DISRST" ]; then
echo "$PREFIX SPAM offline, restart limit exceeded." >> $SIMLOG
SPAM="offline"
else
if [ ! -f "$SPAM_INIT" ]; then
echo "$PREFIX SPAM restart failed, could not find $SPAM_INIT." >> $SIMLOG
SPAM="restart failed"
ALERT="true"
TIE=$[TIE+1]
EVCOUNT_SPAM=$[EVCOUNT_SPAM+1]
else
if [ ! "$RST" = "false" ]; then
ALERT="true"
SPAM="restarted"
TIE=$[TIE+1]
EVCOUNT_SPAM=$[EVCOUNT_SPAM+1]
rm -f $INSPATH/$DAT/$SRV.dat
touch $INSPATH/$DAT/$SRV.dat
chmod 640 $INSPATH/$DAT/$SRV.dat
chown root:root $INSPATH/$DAT/$SRV.dat
echo "EVC:$EVCOUNT_SPAM" >> $INSPATH/$DAT/$SRV.dat
cd / ; $SPAM_INIT $INIT_ARG >> /dev/null 2>&1
echo "$PREFIX Restarted SPAM service ($EVCOUNT_SPAM SPAM events today)." >> $SIMLOG
else
SPAM="down, restart disabled"
ALERT="true"
TIE=$[TIE+1]
EVCOUNT_SPAM=$[EVCOUNT_SPAM+1]
echo "$PREFIX SPAM down, restart disabled via conf.sim." >> $SIMLOG
fi
fi
fi
}
That should be it. No need to restart anything, since SIM runs every five minutes and re-reads the config file each time it runs. Tail the sim log file (tail -f /usr/local/sim/sim.log) and within five minutes, you should see:
[11/23/06 13:50:01]: SPAM service is online.
If you do not see this, something’s not right, so retrace your steps.
Then, shutdown SpamAssassin (service spamassassin stop), and watch the sim log file again. Within another 5 minutes, you should see:
[11/23/06 13:55:01]: SPAM service is offline.
[11/23/06 13:55:01]: Restarted SPAM service (1 SPAM events today).
That’s it!