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

View File

@@ -22,7 +22,14 @@ toSms=+32xxxxxxxx #the mobile number to send to
textSms="$bodyDefault" #body message to send by SMS
#--- Data for pushBullet ---#
providerApi='https://api.pushbullet.com/v2/pushes'
accessToken='o.xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
subjectPushBullet="$HOSTNAME UPS event $argument" #subject sent by pushBullet
textPushBullet="$bodyDefault" #body message sent by pushBullet
pushbulletProviderApi='https://api.pushbullet.com/v2/pushes'
pushbulletAccessToken='o.xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
pushbulletSubject="UPS event $argument" #subject 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

View File

@@ -136,7 +136,7 @@ function sendSms {
# $1 title
# $2 text body - default $textPushBullet
function sendPushBullet {
# replace default mesg
#replace default mesg
if [ "$1" != "" ] ; then
subjectPushBullet=$1
fi
@@ -144,17 +144,47 @@ function sendPushBullet {
textPushBullet=$2
fi
# var verification
if [ "$providerApi" == "" ] || [ "$accessToken" == "" ] ; then
#var verification
if [ "$pushbulletProviderApi" == "" ] || [ "$pushbulletAccessToken" == "" ] ; then
echo "Can't sen push notification without complete variables for PushBullet" 1>&2
addLog "Can't sen push notification without complete variables for PushBullet"
return 1
fi
tempfile=$(tempfile -p '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"
# TODO check return
tempfile=$(mktemp --suffix '.nutNotifyPushBullet')
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"
returnCurl=$?
if [ $returnCurl -ne 0 ] ; then cat $tempfile ; fi
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
@@ -162,14 +192,17 @@ ONLINE)
text="UPS $ups is now online at $(date +'%H:%M:%S')"
writeLog
sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text"
#sendPushBullet "$pushbulletSubject" "$text"
sendTelegram "$text" "$telegramSubject"
;;
ONBATT)
text="Powercut at $(date +'%H:%M:%S')! UPS $ups run on battery!"
writeLog
sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text"
#sendMail "$subjectMail" "$text"
#sendPushBullet "$pushbulletSubject" "$text"
emoji=$(echo -e "\xE2\x9A\xA0")
sendTelegram "$text" "$telegramSubject" "$emoji"
# sendSms "$text"
;;
@@ -177,8 +210,10 @@ 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 "$subjectPushBullet" "$text"
#sendMail "$subjectMail" "$text"
#sendPushBullet "$pushbulletSubject" "$text"
emoji=$(echo -e "\xF0\x9F\x94\xA5")
sendTelegram "$text" "$telegramSubject" "$emoji"
# sendSms "$text"
;;
@@ -186,8 +221,10 @@ FSD)
# note : for slave only
text="Force shutdown slave server $server at $(date +'%H:%M:%S') !"
writeLog
sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text"
#sendMail "$subjectMail" "$text"
#sendPushBullet "$pushbulletSubject" "$text"
emoji=$(echo -e "\xE2\x9A\xA0")
sendTelegram "$text" "$telegramSubject" "$emoji"
# sendSms "$text"
;;
@@ -195,15 +232,18 @@ SHUTDOWN)
# note : executed on the master only
text="Shutdown master serveur $server at $(date +'%H:%M:%S') !"
writeLog
sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text"
#sendMail "$subjectMail" "$text"
#sendPushBullet "$pushbulletSubject" "$text"
emoji=$(echo -e "\xF0\x9F\x94\xA5")
sendTelegram "$text" "$telegramSubject" "$emoji"
# sendSms "$text"
;;
COMMOK|COMMBAD|REPLBATT|NOCOMM)
writeLog
sendMail
sendPushBullet
#sendMail
#sendPushBullet
sendTelegram "$text" "$telegramSubject"
# sendSms
;;
@@ -216,7 +256,8 @@ SERVERONLINE)
rm -f $powerdownflag && \
writeLog && \
sendMail "$subjectMail" "$text"
sendPushBullet "$subjectPushBullet" "$text"
sendPushBullet "$pushbulletSubject" "$text"
sendTelegram "$text" "$telegramSubject"
fi
;;

View File

@@ -4,11 +4,12 @@ logfile=/var/log/nutNotify/nutNotify.log
flagfile=/var/log/nutNotify/nutShutdown.flag
BIN_MAIL=/usr/bin/mail
BIN_PUSHBULLET=/usr/local/bin/pushbullet.sh
BIN_TELEGRAM=/usr/local/bin/telegram-notification.sh
MAILTO=root
function aide() {
echo "$0 [mail|pushbullet]"
echo "$0 [mail|pushbullet|telegram]"
}
# 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 ;;
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)" ;;
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
rm $flagfile
fi