monitnotifyer.sh: added debug log and fixed mulitline description
This commit is contained in:
		
							parent
							
								
									6447772598
								
							
						
					
					
						commit
						257ce53e7f
					
				| 
						 | 
					@ -1,31 +1,65 @@
 | 
				
			||||||
#!/bin/bash
 | 
					#!/bin/bash
 | 
				
			||||||
scrpa="$(/usr/bin/dirname ${0})"
 | 
					scrpa="$(/usr/bin/dirname ${0})"
 | 
				
			||||||
monitnotifymbox="/var/mail/monitnotify"
 | 
					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"
 | 
					actionscript="start"
 | 
				
			||||||
enabledebug=false
 | 
					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() {
 | 
					trignotification() {
 | 
				
			||||||
    mailmsg="$(cat ${monitnotifymbox} | grep -E "${monitgrep}")"
 | 
					    mailfullmsg="$(/bin/cat ${monitnotifymbox})"
 | 
				
			||||||
    if [ -n "${mailmsg}" ]
 | 
					    mailmsgsubj="$(/bin/echo "${mailfullmsg}" | grep -E "${monitgrepsubj}")"
 | 
				
			||||||
 | 
					    if [ -n "${mailmsgsubj}" ]
 | 
				
			||||||
    then
 | 
					    then
 | 
				
			||||||
        echo "Monitmail detected"
 | 
					        monitnotifyeroutput "Monitmail detected" &
 | 
				
			||||||
        if [ $(/bin/echo ${mailmsg} | /usr/bin/wc -l) -gt 5 ]; then
 | 
					        if [ $(/bin/echo ${mailmsgsubj} | /usr/bin/wc -l) -gt 1 ]; then
 | 
				
			||||||
            multymsg=true
 | 
					            multymsg=true
 | 
				
			||||||
            echo "Detected multy mail"
 | 
					            monitnotifyeroutput "Detected multy mail" &
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
            echo "Detected single mail"
 | 
					            monitnotifyeroutput "Detected single mail" &
 | 
				
			||||||
            /bin/echo "d*" | /usr/bin/mail
 | 
					            /bin/echo "d*" | /usr/bin/mail
 | 
				
			||||||
            mservicename="$(/bin/echo "${mailmsg}" | /bin/grep -E '^Subject. Monit-Message' | /usr/bin/cut -f4 -d\:)"
 | 
					            monitnotifyeroutput "Generating data for notification" &
 | 
				
			||||||
            mservicestatus="$(/bin/echo "${mailmsg}" | /bin/grep -E '^Subject. Monit-Message' | /usr/bin/cut -f3 -d\:)"
 | 
					            mservicename="$(/bin/echo "${mailmsgsubj}" | /usr/bin/cut -f4 -d\:)"
 | 
				
			||||||
            mserviceaction="$(/bin/echo "${mailmsg}" | /bin/grep -E '^Monit-Action' | /usr/bin/cut -f2 -d\:)"
 | 
					            mservicestatus="$(/bin/echo "${mailfullmsg}" | /bin/grep -E '^Subject. Monit-Message' | /usr/bin/cut -f3 -d\:)"
 | 
				
			||||||
            mservicehost="$(/bin/echo "${mailmsg}"  | /bin/grep -E '^Monit-Host.' | /usr/bin/cut -f2 -d\:)"
 | 
					            mserviceaction="$(/bin/echo "${mailfullmsg}" | /bin/grep -E '^Monit-Action' | /usr/bin/cut -f2 -d\:)"
 | 
				
			||||||
            mservicedesc="$(/bin/echo "${mailmsg}" | /bin/grep -E '^Monit-Description.' | /bin/sed -e 's/^Monit-Description.//g')"
 | 
					            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
 | 
					        fi
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
        /bin/echo "Not a monit message, ignore it"
 | 
					        monitnotifyeroutput "Not a monit message, ignore it" &
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -34,14 +68,18 @@ usage() {
 | 
				
			||||||
	echo "==================== [  HELP for $(/usr/bin/basename ${0})  ] ===================="
 | 
						echo "==================== [  HELP for $(/usr/bin/basename ${0})  ] ===================="
 | 
				
			||||||
	echo "    -h               > Will show you shit help"
 | 
						echo "    -h               > Will show you shit help"
 | 
				
			||||||
	echo "    -d               > Will enable the debug mode"
 | 
						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 "    -a               > (default start) Can be set manually only to start or stop"
 | 
				
			||||||
	echo "                       for starting or stopping the script"
 | 
						echo "                       for starting or stopping the script"
 | 
				
			||||||
	exit 0
 | 
						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
 | 
					while getopts ${optstring} c; do
 | 
				
			||||||
    case ${c} in
 | 
					    case ${c} in
 | 
				
			||||||
| 
						 | 
					@ -49,26 +87,38 @@ while getopts ${optstring} c; do
 | 
				
			||||||
            actionscript="${OPTARG}"
 | 
					            actionscript="${OPTARG}"
 | 
				
			||||||
            ;;
 | 
					            ;;
 | 
				
			||||||
        d)
 | 
					        d)
 | 
				
			||||||
 | 
					            /bin/touch "${debugenabledfile}" &&
 | 
				
			||||||
 | 
					                monitnotifyeroutput "Debug Mode enabled" &
 | 
				
			||||||
            enabledebug=true
 | 
					            enabledebug=true
 | 
				
			||||||
            ;;
 | 
					            ;;
 | 
				
			||||||
 | 
					        D)
 | 
				
			||||||
 | 
					            enabledebug=false
 | 
				
			||||||
 | 
					            ;;
 | 
				
			||||||
        *)  usage ;;
 | 
					        *)  usage ;;
 | 
				
			||||||
        [h\?]) usage ;;
 | 
					        [h\?]) usage ;;
 | 
				
			||||||
    esac
 | 
					    esac
 | 
				
			||||||
done
 | 
					done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [ -f "${debugenabledfile}" ] && [ ${enabledebug} == false ]; then
 | 
				
			||||||
 | 
					    rm -f "${debugenabledfile}" > /dev/null 2>&1 &
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
actionscript="$(echo ${actionscript} | /usr/bin/tr '[:upper:]' '[:lower:]')"
 | 
					actionscript="$(echo ${actionscript} | /usr/bin/tr '[:upper:]' '[:lower:]')"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					monitnotifyeroutput "ACTION: ${actionscript}" &
 | 
				
			||||||
if [ "${actionscript}" == "start" ]; then
 | 
					if [ "${actionscript}" == "start" ]; then
 | 
				
			||||||
	if [ -z "$(/usr/bin/pgrep -l inotify -u $(/usr/bin/id monitnotify -u))" ]; 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} |
 | 
							/usr/bin/inotifywait -q -m -e close_write ${monitnotifymbox} |
 | 
				
			||||||
		while read -r filename events
 | 
							while read -r filename events
 | 
				
			||||||
		do
 | 
							do
 | 
				
			||||||
			if [ "${filename}" == "${monitnotifymbox}" ] ; then trignotification ; fi
 | 
								if [ "${filename}" == "${monitnotifymbox}" ] ; then trignotification ; fi
 | 
				
			||||||
		done
 | 
							done
 | 
				
			||||||
	else
 | 
						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
 | 
						fi
 | 
				
			||||||
elif [ "${actionscript}" == "stop" ]; then
 | 
					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
 | 
					    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
 | 
						exit 0
 | 
				
			||||||
else
 | 
					else
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue