From d1705d3af1d1858aa2702b9667a97ef6dbb240af Mon Sep 17 00:00:00 2001 From: belgotux Date: Sun, 15 Jan 2023 23:10:00 +0100 Subject: [PATCH] feature configure method by the config file --- nutNotify.conf.txt | 15 ++++++++++++- nutNotify.sh | 53 +++++++++++++++------------------------------- nutNotifyFct.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 37 deletions(-) diff --git a/nutNotify.conf.txt b/nutNotify.conf.txt index 780d37e..63430eb 100644 --- a/nutNotify.conf.txt +++ b/nutNotify.conf.txt @@ -43,4 +43,17 @@ pushoverProviderApi='https://api.pushover.net/1/messages.json' pushoverAppToken='xxxxxxxxxxxxxxxxxxxxxxxxxxxx' #ApplicationToken pushoverUserkey='xxxxxxxxxxxxxxxxxxxxxxxxxxxx' #UserKey pushoverSubject="UPS event $argument" #subject sent by Pushover -pushoverMessage="$bodyDefault" #body message sent by Pushover \ No newline at end of file +pushoverMessage="$bodyDefault" #body message sent by Pushover + +# methods to use on event (array to multi alert) +# can be mail, telegram, pushober, pushbullet, sms +# or an array like '(telegram mail)' for multiple methods +methodDefault=mail + +methodOnline=$methodDefault +methodOnbatt=$methodDefault +methodLowbatt=$methodDefault +methodFsd=$methodDefault +methodShutdown=$methodDefault +methodComm=$methodDefault +methodServerOnline=$methodDefault \ No newline at end of file diff --git a/nutNotify.sh b/nutNotify.sh index 71873c4..715cc5e 100644 --- a/nutNotify.sh +++ b/nutNotify.sh @@ -29,10 +29,17 @@ powerdownflag=$(sed -ne 's#^ *POWERDOWNFLAG *\(.*\)$#\1#p' /etc/nut/upsmon.conf) # Variables configFile=/usr/local/etc/nutNotify.conf -source "$configFile" +if [ ! -e "$(dirname $0)/nutNotifyFct.sh" ] ; then + echo "Functions script $(dirname $0)/nutNotifyFct.sh doesn't exist" 1>&2 + exit 1 +fi source "$(dirname $0)/nutNotifyFct.sh" -if [ ! -e "$configFile" ] ; then +if [ -e "$configFile" ] ; then + source "$configFile" +elif [ -e "$(dirname $0)/nutNotify.conf" ] ; then + source "$(dirname $0)/nutNotify.conf" +else echo "File $configFile doesn't exist" 1>&2 exit 1 fi @@ -51,66 +58,43 @@ case "$argument" in ONLINE) text="UPS $ups is now online at $(date +'%H:%M:%S')" writeLog - sendMail "$subjectMail" "$text" - #sendPushBullet "$pushbulletSubject" "$text" - #sendPushover "$pushoverSubject" "$text" - sendTelegram "$text" "$telegramSubject" + conditionalNotification $argument "$text" "" ;; ONBATT) text="Powercut at $(date +'%H:%M:%S')! UPS $ups run on battery!" writeLog - #sendMail "$subjectMail" "$text" - #sendPushBullet "$pushbulletSubject" "$text" - #sendPushover "$pushoverSubject" "$text" emoji=$(echo -e "\xE2\x9A\xA0") - sendTelegram "$text" "$telegramSubject" "$emoji" -# sendSms "$text" + conditionalNotification $argument "$text" "$emoji" ;; LOWBATT) # note : notify get when /sbin/upsdrvctl shutdown executed text="Low level battery at $(date +'%H:%M:%S') UPS $ups... Shutdown imminent !" writeLog - #sendMail "$subjectMail" "$text" - #sendPushBullet "$pushbulletSubject" "$text" - #sendPushover "$pushoverSubject" "$text" emoji=$(echo -e "\xF0\x9F\x94\xA5") - sendTelegram "$text" "$telegramSubject" "$emoji" -# sendSms "$text" + conditionalNotification $argument "$text" "$emoji" ;; FSD) # note : for slave only text="Force shutdown slave server $server at $(date +'%H:%M:%S') !" writeLog - #sendMail "$subjectMail" "$text" - #sendPushBullet "$pushbulletSubject" "$text" - #sendPushover "$pushoverSubject" "$text" emoji=$(echo -e "\xE2\x9A\xA0") - sendTelegram "$text" "$telegramSubject" "$emoji" -# sendSms "$text" + conditionalNotification $argument "$text" "$emoji" ;; SHUTDOWN) # note : executed on the master only text="Shutdown master serveur $server at $(date +'%H:%M:%S') !" writeLog - #sendMail "$subjectMail" "$text" - #sendPushBullet "$pushbulletSubject" "$text" - #sendPushover "$pushoverSubject" "$text" emoji=$(echo -e "\xF0\x9F\x94\xA5") - sendTelegram "$text" "$telegramSubject" "$emoji" -# sendSms "$text" + conditionalNotification $argument "$text" "$emoji" ;; COMMOK|COMMBAD|REPLBATT|NOCOMM) writeLog - #sendMail - #sendPushBullet - #sendPushover "$pushoverSubject" "$text" - sendTelegram "$text" "$telegramSubject" - # sendSms + conditionalNotification $argument "$text" "" ;; SERVERONLINE) @@ -120,11 +104,8 @@ SERVERONLINE) # add your script here to wakeup other servers etc text="Serveur $server online at $(date +'%H:%M:%S') !" rm -f $powerdownflag && \ - writeLog && \ - sendMail "$subjectMail" "$text" - #sendPushBullet "$pushbulletSubject" "$text" - #sendPushover "$pushoverSubject" "$text" - sendTelegram "$text" "$telegramSubject" + writeLog + conditionalNotification $argument "$text" "" fi ;; diff --git a/nutNotifyFct.sh b/nutNotifyFct.sh index 74010f6..e34752d 100644 --- a/nutNotifyFct.sh +++ b/nutNotifyFct.sh @@ -154,4 +154,55 @@ function sendPushover { if [ $returnCurl -ne 0 ] ; then cat $tempfile ; fi rm $tempfile return $returnCurl +} + +function conditionalNotification { + local event=$1 + local text=$2 + local emoji=$3 + + case "$event" in + ONLINE) + method=${methodOnline[*]} + ;; + ONBATT) + method=${methodOnbatt[*]} + ;; + LOWBATT) + method=${methodLowbatt[*]} + ;; + FSD) + method=${methodFsd[*]} + ;; + SHUTDOWN) + method=${methodShutdown[*]} + ;; + COMMOK|COMMBAD|REPLBATT|NOCOMM) + method=${methodComm[*]} + ;; + SERVERONLINE) + method=${methodServerOnline[*]} + ;; + *) + echo "Error : this event is not managed" 1>&2 + ;; + esac + + echo "${method[*]}" + if [[ " ${method[*]} " =~ " mail " ]] ; then + sendMail "$subjectMail" "$text" + fi + if [[ " ${method[*]} " =~ " pushbullet " ]] ; then + sendPushBullet "$pushbulletSubject" "$text" + fi + if [[ " ${method[*]} " =~ " pushover " ]] ; then + sendPushover "$pushoverSubject" "$text" + fi + if [[ " ${method[*]} " =~ " telegram " ]] ; then + sendTelegram "$text" "$telegramSubject" "$emoji" + fi + if [[ " ${method[*]} " =~ " sms " ]] ; then + sendSms "$text" + fi + } \ No newline at end of file