monitnotifyer.sh: added debug log and fixed mulitline description
This commit is contained in:
parent
6447772598
commit
257ce53e7f
|
@ -1,31 +1,65 @@
|
|||
#!/bin/bash
|
||||
scrpa="$(/usr/bin/dirname ${0})"
|
||||
monitnotifymbox="/var/mail/monitnotify"
|
||||
monitgrep='^Subject..Monit-Message|^Monit-(Date|Action|Host|Description)'
|
||||
mailfullmsg=""
|
||||
monitgrepsubj='^Subject..Monit-Message'
|
||||
monitgrep='^Monit-(Date|Action|Host|Description)'
|
||||
actionscript="start"
|
||||
enabledebug=false
|
||||
debugenabledfile="/tmp/monitnotifyerdebugswitch"
|
||||
debugfile="${scrpa}/debug.log"
|
||||
dateformat="%d.%m.%Y %H:%M:%S"
|
||||
|
||||
monitnotifyeroutput() {
|
||||
if [ ${enabledebug} == true ]; then
|
||||
if ! [ -f "${debugfile}" ]; then
|
||||
/bin/touch "${debugfile}" > /dev/null 2>&1 &&
|
||||
/bin/chgrp "$(/usr/bin/id $(/usr/bin/whoami) | /bin/grep -E "groups.[0-9]+.[a-zA-Z0-9]+" | /usr/bin/cut -f1 -d\()" "${debugfile}" > /dev/null 2>&1 &&
|
||||
/bin/chmod g+w "${debugfile}" > /dev/null 2>&1 &&
|
||||
/bin/echo "$(date +"${dateformat}") - [MONITNOTIFYER] - ${1}" >> "${debugfile}" ||
|
||||
return
|
||||
fi
|
||||
/bin/echo "$(date +"${dateformat}") - [MONITNOTIFYER] - ${1}" >> "${debugfile}"
|
||||
fi
|
||||
}
|
||||
|
||||
trignotification() {
|
||||
mailmsg="$(cat ${monitnotifymbox} | grep -E "${monitgrep}")"
|
||||
if [ -n "${mailmsg}" ]
|
||||
mailfullmsg="$(/bin/cat ${monitnotifymbox})"
|
||||
mailmsgsubj="$(/bin/echo "${mailfullmsg}" | grep -E "${monitgrepsubj}")"
|
||||
if [ -n "${mailmsgsubj}" ]
|
||||
then
|
||||
echo "Monitmail detected"
|
||||
if [ $(/bin/echo ${mailmsg} | /usr/bin/wc -l) -gt 5 ]; then
|
||||
monitnotifyeroutput "Monitmail detected" &
|
||||
if [ $(/bin/echo ${mailmsgsubj} | /usr/bin/wc -l) -gt 1 ]; then
|
||||
multymsg=true
|
||||
echo "Detected multy mail"
|
||||
monitnotifyeroutput "Detected multy mail" &
|
||||
else
|
||||
echo "Detected single mail"
|
||||
monitnotifyeroutput "Detected single mail" &
|
||||
/bin/echo "d*" | /usr/bin/mail
|
||||
mservicename="$(/bin/echo "${mailmsg}" | /bin/grep -E '^Subject. Monit-Message' | /usr/bin/cut -f4 -d\:)"
|
||||
mservicestatus="$(/bin/echo "${mailmsg}" | /bin/grep -E '^Subject. Monit-Message' | /usr/bin/cut -f3 -d\:)"
|
||||
mserviceaction="$(/bin/echo "${mailmsg}" | /bin/grep -E '^Monit-Action' | /usr/bin/cut -f2 -d\:)"
|
||||
mservicehost="$(/bin/echo "${mailmsg}" | /bin/grep -E '^Monit-Host.' | /usr/bin/cut -f2 -d\:)"
|
||||
mservicedesc="$(/bin/echo "${mailmsg}" | /bin/grep -E '^Monit-Description.' | /bin/sed -e 's/^Monit-Description.//g')"
|
||||
monitnotifyeroutput "Generating data for notification" &
|
||||
mservicename="$(/bin/echo "${mailmsgsubj}" | /usr/bin/cut -f4 -d\:)"
|
||||
mservicestatus="$(/bin/echo "${mailfullmsg}" | /bin/grep -E '^Subject. Monit-Message' | /usr/bin/cut -f3 -d\:)"
|
||||
mserviceaction="$(/bin/echo "${mailfullmsg}" | /bin/grep -E '^Monit-Action' | /usr/bin/cut -f2 -d\:)"
|
||||
mservicehost="$(/bin/echo "${mailfullmsg}" | /bin/grep -E '^Monit-Host.' | /usr/bin/cut -f2 -d\:)"
|
||||
mservicedesc="$(/bin/echo "${mailfullmsg}" | /bin/sed -n '/^Monit-Description./,/^$/p' | /bin/sed -e 's/^Monit-Description.//g;/^$/d')"
|
||||
|
||||
${scrpa}/pretrigger_notification.sh "${mservicename}" "${mserviceaction}" "${mservicestatus}" "${mservicedesc}" "${mservicehost}" &
|
||||
monitnotifyeroutput "Running pretigger_notification script with parameters:
|
||||
Parameter1: (debugswitch) \"${enabledebug}\"
|
||||
------------------
|
||||
Parameter2: (logfile) \"${debugfile}\"
|
||||
------------------
|
||||
Parameter3: (service name) \"${mservicename}\"
|
||||
------------------
|
||||
Parameter4: (service action) \"${mserviceaction}\"
|
||||
------------------
|
||||
Parameter5: (service status) \"${mservicestatus}\"
|
||||
------------------
|
||||
Parameter6: (service description) \"${mservicedesc}\"
|
||||
------------------
|
||||
Parameter7: (service host) \"${mservicehost}\"" &
|
||||
"${scrpa}/pretrigger_notification.sh" ${enabledebug} "${debugfile}" "${mservicename}" "${mserviceaction}" "${mservicestatus}" "${mservicedesc}" "${mservicehost}" &
|
||||
fi
|
||||
else
|
||||
/bin/echo "Not a monit message, ignore it"
|
||||
monitnotifyeroutput "Not a monit message, ignore it" &
|
||||
return
|
||||
fi
|
||||
}
|
||||
|
@ -34,14 +68,18 @@ usage() {
|
|||
echo "==================== [ HELP for $(/usr/bin/basename ${0}) ] ===================="
|
||||
echo " -h > Will show you shit help"
|
||||
echo " -d > Will enable the debug mode"
|
||||
echo " -D > Will disable the debug mode"
|
||||
echo " -a > (default start) Can be set manually only to start or stop"
|
||||
echo " for starting or stopping the script"
|
||||
exit 0
|
||||
}
|
||||
|
||||
optstring="a:d?h"
|
||||
|
||||
actr10kbranch="$(git -C ${r10kpwd} branch 2>&1 | grep -E '^\*' | /usr/bin/awk '{print $2}')"
|
||||
if [ -f "${debugenabledfile}" ]; then
|
||||
enabledebug=true
|
||||
fi
|
||||
|
||||
optstring="a:d?h"
|
||||
|
||||
while getopts ${optstring} c; do
|
||||
case ${c} in
|
||||
|
@ -49,26 +87,38 @@ while getopts ${optstring} c; do
|
|||
actionscript="${OPTARG}"
|
||||
;;
|
||||
d)
|
||||
/bin/touch "${debugenabledfile}" &&
|
||||
monitnotifyeroutput "Debug Mode enabled" &
|
||||
enabledebug=true
|
||||
;;
|
||||
D)
|
||||
enabledebug=false
|
||||
;;
|
||||
*) usage ;;
|
||||
[h\?]) usage ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -f "${debugenabledfile}" ] && [ ${enabledebug} == false ]; then
|
||||
rm -f "${debugenabledfile}" > /dev/null 2>&1 &
|
||||
fi
|
||||
|
||||
actionscript="$(echo ${actionscript} | /usr/bin/tr '[:upper:]' '[:lower:]')"
|
||||
|
||||
monitnotifyeroutput "ACTION: ${actionscript}" &
|
||||
if [ "${actionscript}" == "start" ]; then
|
||||
if [ -z "$(/usr/bin/pgrep -l inotify -u $(/usr/bin/id monitnotify -u))" ]; then
|
||||
monitnotifyeroutput "Opening inotifywait on ${monitnotifymbox}" &
|
||||
/usr/bin/inotifywait -q -m -e close_write ${monitnotifymbox} |
|
||||
while read -r filename events
|
||||
do
|
||||
if [ "${filename}" == "${monitnotifymbox}" ] ; then trignotification ; fi
|
||||
done
|
||||
else
|
||||
echo "monitnotifyer is already running with pid $(/usr/bin/pgrep -l inotify -u $(/usr/bin/id monitnotify -u) | /usr/bin/awk 'print {$1}')"
|
||||
monitnotifyeroutput "monitnotifyer is already running with pid $(/usr/bin/pgrep -l inotify -u $(/usr/bin/id monitnotify -u) | /usr/bin/awk 'print {$1}')" &
|
||||
fi
|
||||
elif [ "${actionscript}" == "stop" ]; then
|
||||
monitnotifyeroutput "Stopping monitnotify scripts" &
|
||||
kill -9 $(echo $(/usr/bin/pgrep -l inotify -u $(/usr/bin/id monitnotify -u)) | /usr/bin/cut -f1 -d\ ) > /dev/null 2>&1
|
||||
exit 0
|
||||
else
|
||||
|
|
Loading…
Reference in New Issue