egscripts/egalert/egalert

159 lines
4.3 KiB
Bash
Executable file

#!/bin/bash
#
# File: egalert
#
# egalert - script's checking & alerting tool
# Handles all alert checking for all egressive scripts
# Usage: egalert FUNCTION DAYSTAMP-SCRIPT_NAME
# Author: Marek Kuziel (marek@egressive.com)
# Variables
#----------
# Directory settings
EGALERT_DIR=/etc/egscripts/egalert
EGALERT_BIN_DIR=`dirname $0`
EGALERT_LOG_DIR=/var/log/egalert
EGALERT_STATUS_DIR=$EGALERT_LOG_DIR/status
# Mail related stuff
MAINTAINER="dave@egressive.com"
MAIL_CMD=/bin/mail
MAIL_TO=""
MAIL_CC=""
MAIL_BCC="$MAINTAINER"
MAIL_SUBJECT="EGAlert"
MAIL_CONTENT=$EGALERT_BIN_DIR/tmp.txt
# *stamp
DAYSTAMP=`date '+%Y-%m-%d'`
TIMESTAMP=`date '+%Y-%m-%d %H:%M'`
TIMESTAMP_FULL=`date '+%Y-%m-%d %H:%M:%S'`
#Debug
EGALERT_DEBUG=1 # To switch on egalert script log for debugging set this to 1
EGALERT_DEBUG_LOG=$EGALERT_LOG_DIR/$DAYSTAMP.log # DAYSTAMP is more usefull for debugging 'cos creates just one file
EGALERT_DAILY_LOG=$EGALERT_LOG_DIR/egalert-daily-$DAYSTAMP.log
if [ 1 == $EGALERT_DEBUG ]
then
exec 10>> $EGALERT_DEBUG_LOG
exec 2>&10
exec 1>&10
else
exec 10>> /dev/null
exec 2>&10
exec 1>&10
fi
# Functions
#----------
# Check if log dirs exist
dirs_exist()
{
if [ ! -d "$EGALERT_LOG_DIR" ]
then
# Log dir doesn't exist. We make it now.
mkdir $EGALERT_LOG_DIR
else
if [ ! -d "$EGALERT_STATUS_DIR" ]
then
mkdir $EGALERT_STATUS_DIR
fi
fi
}
# Creates egalert log file
egalert_log()
{
# Create log file if it doesn't exist yet
if [ ! -f "$EGALERT_DAILY_LOG" ]
then
touch $EGALERT_DAILY_LOG
fi
echo "$TIMESTAMP_FULL $@" >> $EGALERT_DAILY_LOG
}
# Sends email. Content of email is in first parameter of this function
send_email()
{
$MAIL_CMD -s "$MAIL_SUBJECT" -c "$MAIL_CC" -b "$MAIL_BCC" "$MAIL_TO" < $MAIL_CONTENT
rm $MAIL_CONTENT
}
send_log()
{
egalert_log "INFO: sendlog"
cat $EGALERT_DAILY_LOG > $MAIL_CONTENT
MAIL_SUBJECT="$MAIL_SUBJECT: Infolog"
send_email
}
# EGAlert
#--------
dirs_exist
cd $EGALERT_BIN_DIR
case "$1" in
# EG:script ran. Touch run file for selected script.
run)
egalert_log "INFO: run ($2)"
touch $EGALERT_STATUS_DIR/$2
echo "Started at: $TIMESTAMP_FULL" >> $EGALERT_STATUS_DIR/$2
exit 0
;;
# EG:script ran with success. Create success status for selected script.
success)
egalert_log "INFO: success ($2)"
if [ -f $EGALERT_STATUS_DIR/$2 ]
then
echo "Finished with success at: $TIMESTAMP_FULL" >> $EGALERT_STATUS_DIR/$2
else
egalert_log "EXCEPTION: Script $2 run successfully but status file hasn't been created!"
echo "EXCEPTION: Script $2 run successfully but status file hasn't been created!" > $MAIL_CONTENT
MAIL_SUBJECT="$MAIL_SUBJECT: Exception!"
send_email
fi
exit 0
;;
# Send daily log to desired users
sendlog)
send_log
exit 0
;;
# Check status of all scripts they should've ran daily for current day
checkdaily)
egalert_log "INFO: checkdaily"
for file in $(ls $EGALERT_STATUS_DIR | grep $DAYSTAMP)
do
if [ 1 != `cat $EGALERT_STATUS_DIR/$file | grep success | wc -l` ]
then
egalert_log "ERROR: Script $file failed!!!"
echo "ERROR: Script $file failed!!!" > $MAIL_CONTENT
MAIL_SUBJECT="$MAIL_SUBJECT: ERROR!!!"
send_email
fi
done
# Check itself. If log file exists (debugging switched on)
# and if size of log is greater than zero. If so inform us.
# If file is zero remove it.
if [ -f $EGALERT_LOG_DIR/$DAYSTAMP.log ]
then
if [ -s $EGALERT_LOG_DIR/$DAYSTAMP.log ]
then
egalert_log "WARNING: EGAlert produced errors!!!"
echo "WARNING: EGAlert produced errors!!!" > $MAIL_CONTENT
MAIL_SUBJECT="$MAIL_SUBJECT: WARNING!"
send_email
else
rm $EGALERT_LOG_DIR/$DAYSTAMP.log
fi
fi
# Send daily log
send_log
exit 0
;;
esac