feature configure method by the config file

This commit is contained in:
belgotux
2023-01-15 23:10:00 +01:00
parent f6f02877e6
commit d1705d3af1
3 changed files with 82 additions and 37 deletions

View File

@@ -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
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

View File

@@ -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
;;

View File

@@ -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
}