#!/bin/bash #Syn-3 state handling script (C)DatuX #Exit codes: #0 = OK #1 = Generic error #2 = email error #3 = snmptrap error source /sbin/lang.sh || exit 1 source /etc/alert.conf || exit 1 #read options while [ "${1:0:2}" == "--" ]; do #if options are valueless fill them with 1. if echo $1|grep = >/dev/null; then eval "${1:2}" else eval "${1:2}"=1 fi shift done FACILITY=$1 STATE=$2 MSG=$3 #Abort on error function Error() { echo "$@"; echo "Syntax: $0 [ --ignorenew ] [ --force ] [ --cautiontimeout= ] [ --alerttimeout= ] <\"message...\">"; echo " --force : to force a state-reset even if the user already has acknowledged the alert. (usefull for things like backups)" echo " --ignorenew : dont do anything if the facility isnt known yet." echo " --cautiontimeout = : if this facility didn't gave minutes a response, hale a caution." echo " --alerttimeout = : if this facility didn't gave minutes a response, hale an alert." exit 1; } SendSnmpTrap() { #check if everything is in place. if ! [ "$snmptrap_community" ] || ! [ "$snmptrap_receiver" ] || ! [ "$snmptrap_enabled" == "on" ] ; then return 0; fi TRAP_FACILITY="$FACILITY"; if [ "$STATE" == "DELETE" ]; then TRAP_STATE="$STATE"; TRAP_MSG=`cat "$ALERTDIR/$FACILITY/ack"` else TRAP_STATE=`cat "$ALERTDIR/$FACILITY/state"` TRAP_MSG=`cat "$ALERTDIR/$FACILITY/msg"` fi if [ "$STATE" == "ACK" ]; then TRAP_ACK="$MSG"; else TRAP_ACK=""; fi #many retries after error retries=3; if ! env MIBS="+SYN3-MIB" /usr/bin/snmptrap -v 2c -Ci -r $retries -c $snmptrap_community $snmptrap_receiver "" syn3FacilityTrap syn3FacilityName s "$TRAP_FACILITY" syn3FacilityStatus s "$TRAP_STATE" syn3FacilityDescription s "$TRAP_MSG" syn3FacilityAck s "$TRAP_ACK" &>/dev/null; then echo "Error while sending snmptrap" fi return 0; } if ! [ "$FACILITY" ]; then Error "Specify facility!" fi if [ "$STATE" != "OK" ] && [ "$STATE" != "CAUTION" ] && [ "$STATE" != "ALERT" ] && [ "$STATE" != "DELETE" ] && [ "$STATE" != "ACK" ]; then Error "State must be OK, CAUTION, ALERT or DELETE or ACK"; fi #delete this facility if [ "$STATE" == "DELETE" ]; then if [ -e "$ALERTDIR/$FACILITY" ]; then rm -r "$ALERTDIR/$FACILITY" 2>/dev/null SendSnmpTrap fi exit 0 fi #(un)acknowledge this facility if [ "$STATE" == "ACK" ]; then if [ "$MSG" ]; then echo "$MSG" > "$ALERTDIR/$FACILITY/ack"; else rm "$ALERTDIR/$FACILITY/ack" &>/dev/null fi #only update this acknowledment on the server cd /usr/webint/htdocs/monitoring/ php cli.php --send_ack "$FACILITY" "$MSG" &>/dev/null SendSnmpTrap exit 0 fi #ignore non existing facility? if [ "$ignorenew" ]; then if ! [ -e "$ALERTDIR/$FACILITY" ]; then exit 0 fi fi #get old state and message OLDSTATE=`cat "$ALERTDIR/$FACILITY/state" 2>/dev/null` OLDMSG=`cat "$ALERTDIR/$FACILITY/msg" 2>/dev/null` if ! [ "$force" ]; then #nothing has changed? if [ "$STATE" == "$OLDSTATE" ] && [ "$MSG" == "$OLDMSG" ]; then #only update the time touch "$ALERTDIR/$FACILITY/state" exit 0 fi fi #prepare statedir mkdir -p "$ALERTDIR/$FACILITY" &>/dev/null #delete old acknowledgement rm "$ALERTDIR/$FACILITY/ack" &>/dev/null #store alerttimeout if [ "$alerttimeout" ]; then echo -n $alerttimeout > "$ALERTDIR/$FACILITY/alerttimeout" || exit 1 else rm "$ALERTDIR/$FACILITY/alerttimeout" &>/dev/null fi #store cautiontimeout if [ "$cautiontimeout" ]; then echo -n $cautiontimeout > "$ALERTDIR/$FACILITY/cautiontimeout" || exit 1 else rm "$ALERTDIR/$FACILITY/cautiontimeout" &>/dev/null fi #store new state info echo -n "$STATE" > "$ALERTDIR/$FACILITY/state" || exit 1 echo -n "$MSG" > "$ALERTDIR/$FACILITY/msg" || exit 1 #log to syslog logger -p local7.warning -t system "$STATE $FACILITY $MSG" || exit 1 #log to splash/console if [ "$STATE" == "OK" ]; then splashinfo "[$FACILITY] OK: $MSG" > /dev/console elif [ "$STATE" == "CAUTION" ]; then splashwarn "[$FACILITY] CAUTION: $MSG" > /dev/console else splasherror "[$FACILITY] ALERT: $MSG" > /dev/console fi #do we need to inform zah user? STATETXT="" if [ "$OLDSTATE" != "$STATE" ] || [ "$force" ]; then if [ "$STATE" == "OK" ] && [ "$notify_ok" == "on" ]; then SUBJECT="`_StateSubjectOk $FACILITY $HOSTNAME`" elif [ "$STATE" == "CAUTION" ] && [ "$notify_caution" == "on" ]; then SUBJECT="`_StateSubjectCaution $FACILITY $HOSTNAME`" elif [ "$STATE" == "ALERT" ] && [ "$notify_alert" == "on" ]; then SUBJECT="`_StateSubjectAlert $FACILITY $HOSTNAME`" fi fi #anything usefull to mail? #TODO: make outgoing smtp configurable for this if [ "$SUBJECT" ]; then if [ -e /var/qmail/bin/sendmail ]; then echo -e "Subject: $SUBJECT\n\n$MSG" | /var/qmail/bin/sendmail "$mail_address" || exit 2 else echo "(syn3-state: can't send mail, no mailer found!)"; fi fi SendSnmpTrap