scripts/beep-notify.sh

63 lines
1.2 KiB
Bash
Raw Normal View History

2021-01-21 16:58:32 -05:00
#!/bin/bash
#
# Icinga2 Notification
#
# This will beep the system speaker when a problem occur
DEBUG_VARS_PATH=".beep-notify.vars"
if [ -e ${DEBUG_VARS_PATH} ]; then
. ${DEBUG_VARS_PATH}
fi
function _beep {
if [ -z "${RPID}" ]; then
# We need to store our subshell pid
#
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
MYPID=$( : ; bash -c 'echo $PPID' )
else
MYPID=$BASHPID
fi
echo $MYPID > $PID_PATH
while true; do
sudo /bin/beep -f 1000 -n -f 2000 -n -f 1500 ## Only ROOT can access /dev/tty0
if [ $? != 0 ]; then ## If we can't beep, no need to keep trying
exit 1 ## No TTY to output error to, just exit
fi
sleep 2
done
fi
}
PID_PATH=/tmp/beep-notify.pid
RPID=
if [ -e ${PID_PATH} ]; then
if [ -f /proc/$(cat ${PID_PATH})/status ]; then
RPID=$(cat ${PID_PATH})
fi
fi
case $NOTIFICATIONTYPE in
PROBLEM)
if [ "${HOSTSTATE}" = "DOWN" ]; then
echo "OK HOST DOWN - INITIATING SYSTEM BEEP"
_beep &
exit 0
elif [ "${SERVICESTATE}" = "CRITICAL" ]; then
echo "OK SERVICE CRITICAL - INITIATING SYSTEM BEEP"
_beep &
exit 0
fi
;;
ACKNOWLEDGEMENT|RECOVERY)
echo "OK ${NOTIFICATIONTYPE} - KILLING SYSTEM BEEP"
kill -SIGTERM $RPID 2> /dev/null
rm -f $PID_PATH
exit 0
;;
esac
exit 1