#!/bin/bash # Service checking utility # Requires NetCat # Run from cron. # creates a lot of files...sorry # 2003 William H. Backman # if you want a log, create an empty "flap.log" NOTIFY="noc@yourdomain.org" for SERVICE in \ "mail.yourdomain.org 80" \ "mail.yourdomain.org 22" \ do # Check to see if a state file exists already if [ -f "$SERVICE" ]; then STATE=`cat "$SERVICE"` else STATE="first check" fi # NetCat does the work here. nc -z $SERVICE if [ $? -eq 0 ]; then CATSTATE="success" echo $CATSTATE > "$SERVICE" else CATSTATE="failure" echo $CATSTATE > "$SERVICE" fi # compare the old state to the current if [ "$STATE" != "$CATSTATE" ]; then echo `date` State change: "$SERVICE" $STATE to $CATSTATE >> flap.log echo State change: "$SERVICE" $STATE to $CATSTATE | mail -s "$SERVICE $CATSTATE" $NOTIFY fi done