fix pushbullet tempfile + add telegram notification

This commit is contained in:
belgotux
2022-12-24 16:59:57 +01:00
parent 57623baab2
commit f39fb6f8c1
3 changed files with 74 additions and 23 deletions
+11 -4
View File
@@ -22,7 +22,14 @@ toSms=+32xxxxxxxx #the mobile number to send to
textSms="$bodyDefault" #body message to send by SMS textSms="$bodyDefault" #body message to send by SMS
#--- Data for pushBullet ---# #--- Data for pushBullet ---#
providerApi='https://api.pushbullet.com/v2/pushes' pushbulletProviderApi='https://api.pushbullet.com/v2/pushes'
accessToken='o.xxxxxxxxxxxxxxxxxxxxxxxxxxxx' pushbulletAccessToken='o.xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
subjectPushBullet="$HOSTNAME UPS event $argument" #subject sent by pushBullet pushbulletSubject="UPS event $argument" #subject sent by pushBullet
textPushBullet="$bodyDefault" #body message sent by pushBullet pushbulletMessage="$bodyDefault" #body message sent by pushBullet
#--- Data for telegram ---#
telegramProviderApi='https://api.telegram.org'
telegramAccessToken=''
telegramChatID=''
telegramSubject="UPS event $argument" #subject sent by telegram
telegramMessage="$bodyDefault" #body message sent by telegram
+57 -16
View File
@@ -145,16 +145,46 @@ function sendPushBullet {
fi fi
#var verification #var verification
if [ "$providerApi" == "" ] || [ "$accessToken" == "" ] ; then if [ "$pushbulletProviderApi" == "" ] || [ "$pushbulletAccessToken" == "" ] ; then
echo "Can't sen push notification without complete variables for PushBullet" 1>&2 echo "Can't sen push notification without complete variables for PushBullet" 1>&2
addLog "Can't sen push notification without complete variables for PushBullet" addLog "Can't sen push notification without complete variables for PushBullet"
return 1 return 1
fi fi
tempfile=$(tempfile -p 'nutNotifyPushBullet-') tempfile=$(mktemp --suffix '.nutNotifyPushBullet')
curl -s -o $tempfile --header "Access-Token: $accessToken" --header 'Content-Type: application/json' --request POST --data-binary "{\"type\":\"note\",\"title\":\"$subjectPushBullet\",\"body\":\"$textPushBullet\"}" "$providerApi" curl -s -o "$tempfile" --header "Access-Token: $pushbulletAccessToken" --header 'Content-Type: application/json' --request POST --data-binary "{\"type\":\"note\",\"title\":\"$HOSTNAME - $subjectPushBullet\",\"body\":\"$textPushBullet\"}" "$pushbulletProviderApi"
# TODO check return returnCurl=$?
if [ $returnCurl -ne 0 ] ; then cat $tempfile ; fi
rm $tempfile rm $tempfile
return $?
}
#send push notification with Telegram
# $1 message
# $2 title
# $3 emoji
function sendTelegram {
#replace default mesg
if [ "$2" != "" ] && [ "$3" != "" ] ; then
local textTelegram=$(echo -e "$3 $HOSTNAME $3 $2\n$1")
elif [ "$2" != "" ] ; then
local textTelegram=$(echo -e "$HOSTNAME - $2 \n$1")
else
local textTelegram="$3$HOSTNAME : $1"
fi
#var verification
if [ "$telegramProviderApi" == "" ] || [ "$telegramAccessToken" == "" ] ; then
echo "Can't send notification without complete variables for Telegram" 1>&2
addLog "Can't send notification without complete variables for Telegram"
return 1
fi
tempfile=$(mktemp --suffix '.telegram-notification')
curl -s -o "$tempfile" --data "chat_id=${telegramChatID}" --data "text=${textTelegram}" "${telegramProviderApi}/bot${telegramAccessToken}/sendMessage"
returnCurl=$?
if [ $returnCurl -ne 0 ] ; then cat $tempfile ; fi
rm $tempfile
return $returnCurl
} }
case "$argument" in case "$argument" in
@@ -162,14 +192,17 @@ ONLINE)
text="UPS $ups is now online at $(date +'%H:%M:%S')" text="UPS $ups is now online at $(date +'%H:%M:%S')"
writeLog writeLog
sendMail "$subjectMail" "$text" sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text" #sendPushBullet "$pushbulletSubject" "$text"
sendTelegram "$text" "$telegramSubject"
;; ;;
ONBATT) ONBATT)
text="Powercut at $(date +'%H:%M:%S')! UPS $ups run on battery!" text="Powercut at $(date +'%H:%M:%S')! UPS $ups run on battery!"
writeLog writeLog
sendMail "$subjectMail" "$text" #sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text" #sendPushBullet "$pushbulletSubject" "$text"
emoji=$(echo -e "\xE2\x9A\xA0")
sendTelegram "$text" "$telegramSubject" "$emoji"
# sendSms "$text" # sendSms "$text"
;; ;;
@@ -177,8 +210,10 @@ LOWBATT)
# note : notify get when /sbin/upsdrvctl shutdown executed # note : notify get when /sbin/upsdrvctl shutdown executed
text="Low level battery at $(date +'%H:%M:%S') UPS $ups... Shutdown imminent !" text="Low level battery at $(date +'%H:%M:%S') UPS $ups... Shutdown imminent !"
writeLog writeLog
sendMail "$subjectMail" "$text" #sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text" #sendPushBullet "$pushbulletSubject" "$text"
emoji=$(echo -e "\xF0\x9F\x94\xA5")
sendTelegram "$text" "$telegramSubject" "$emoji"
# sendSms "$text" # sendSms "$text"
;; ;;
@@ -186,8 +221,10 @@ FSD)
# note : for slave only # note : for slave only
text="Force shutdown slave server $server at $(date +'%H:%M:%S') !" text="Force shutdown slave server $server at $(date +'%H:%M:%S') !"
writeLog writeLog
sendMail "$subjectMail" "$text" #sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text" #sendPushBullet "$pushbulletSubject" "$text"
emoji=$(echo -e "\xE2\x9A\xA0")
sendTelegram "$text" "$telegramSubject" "$emoji"
# sendSms "$text" # sendSms "$text"
;; ;;
@@ -195,15 +232,18 @@ SHUTDOWN)
# note : executed on the master only # note : executed on the master only
text="Shutdown master serveur $server at $(date +'%H:%M:%S') !" text="Shutdown master serveur $server at $(date +'%H:%M:%S') !"
writeLog writeLog
sendMail "$subjectMail" "$text" #sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text" #sendPushBullet "$pushbulletSubject" "$text"
emoji=$(echo -e "\xF0\x9F\x94\xA5")
sendTelegram "$text" "$telegramSubject" "$emoji"
# sendSms "$text" # sendSms "$text"
;; ;;
COMMOK|COMMBAD|REPLBATT|NOCOMM) COMMOK|COMMBAD|REPLBATT|NOCOMM)
writeLog writeLog
sendMail #sendMail
sendPushBullet #sendPushBullet
sendTelegram "$text" "$telegramSubject"
# sendSms # sendSms
;; ;;
@@ -216,7 +256,8 @@ SERVERONLINE)
rm -f $powerdownflag && \ rm -f $powerdownflag && \
writeLog && \ writeLog && \
sendMail "$subjectMail" "$text" sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text" sendPushBullet "$pushbulletSubject" "$text"
sendTelegram "$text" "$telegramSubject"
fi fi
;; ;;
+4 -1
View File
@@ -4,11 +4,12 @@ logfile=/var/log/nutNotify/nutNotify.log
flagfile=/var/log/nutNotify/nutShutdown.flag flagfile=/var/log/nutNotify/nutShutdown.flag
BIN_MAIL=/usr/bin/mail BIN_MAIL=/usr/bin/mail
BIN_PUSHBULLET=/usr/local/bin/pushbullet.sh BIN_PUSHBULLET=/usr/local/bin/pushbullet.sh
BIN_TELEGRAM=/usr/local/bin/telegram-notification.sh
MAILTO=root MAILTO=root
function aide() { function aide() {
echo "$0 [mail|pushbullet]" echo "$0 [mail|pushbullet|telegram]"
} }
# add to log # add to log
@@ -51,6 +52,8 @@ if [ -e $flagfile ] ; then
echo -e "$(date '+%d/%m/%y %H:%M:%S') $HOSTNAME booting\n Downtime $(date -d @$(( $(date +'%s') - $(cat $flagfile))) -u +%H:%M:%S)" | mail -s "booting $HOSTNAME" $MAILTO ;; echo -e "$(date '+%d/%m/%y %H:%M:%S') $HOSTNAME booting\n Downtime $(date -d @$(( $(date +'%s') - $(cat $flagfile))) -u +%H:%M:%S)" | mail -s "booting $HOSTNAME" $MAILTO ;;
pushbullet) pushbullet)
$BIN_PUSHBULLET "booting $HOSTNAME" "$(date '+%d/%m/%y %H:%M:%S') $HOSTNAME booting - Downtime $(date -d @$(( $(date +'%s') - $(cat $flagfile))) -u +%H:%M:%S)" ;; $BIN_PUSHBULLET "booting $HOSTNAME" "$(date '+%d/%m/%y %H:%M:%S') $HOSTNAME booting - Downtime $(date -d @$(( $(date +'%s') - $(cat $flagfile))) -u +%H:%M:%S)" ;;
telegram)
$BIN_TELEGRAM "$(date '+%d/%m/%y %H:%M:%S') $HOSTNAME booting - Downtime $(date -d @$(( $(date +'%s') - $(cat $flagfile))) -u +%H:%M:%S)" "booting $HOSTNAME" ;;
esac esac
rm $flagfile rm $flagfile
fi fi