nutNotifyBoot use fct - no depend on other scripts + change systemd conditions

This commit is contained in:
belgotux
2023-01-04 03:22:53 +01:00
parent 187b910577
commit 749fde13dc
4 changed files with 36 additions and 38 deletions
+5 -1
View File
@@ -3,12 +3,16 @@
# EXAMPLE # # EXAMPLE #
################################## ##################################
logfile=/var/log/nutNotify/nutNotify.log # logfile for nutNotify
flagfile=/var/log/nutNotify/nutShutdown.flag
curlBin="/usr/bin/curl"
mailBin="/usr/bin/mail"
subjectDefault="$HOSTNAME UPS event $argument on $ups@$server !" #default subject subjectDefault="$HOSTNAME UPS event $argument on $ups@$server !" #default subject
bodyDefault="UPS event $argument on $ups at $(date +'%d-%m-%y %H:%M:%S')" # default body message bodyDefault="UPS event $argument on $ups at $(date +'%d-%m-%y %H:%M:%S')" # default body message
#--- Data for mail ---# #--- Data for mail ---#
FROM="" #sender FROM="" #sender
dom=$(echo "$FROM" | awk -F'@' '{printf $2}') #don't change it
MAILTO="" #recipient(s) MAILTO="" #recipient(s)
subjectMail="$subjectDefault" #subject sent by mail subjectMail="$subjectDefault" #subject sent by mail
textMail="$bodyDefault" #body message sent by mail textMail="$bodyDefault" #body message sent by mail
+4 -8
View File
@@ -27,10 +27,10 @@ server=$(echo "$1" | awk '{printf $2}' | awk -F "[@:]" '{print $2}')
powerdownflag=$(sed -ne 's#^ *POWERDOWNFLAG *\(.*\)$#\1#p' /etc/nut/upsmon.conf) powerdownflag=$(sed -ne 's#^ *POWERDOWNFLAG *\(.*\)$#\1#p' /etc/nut/upsmon.conf)
# Variables # Variables
logfile=/var/log/nutNotify.log # logfile for nutNotify configFile=/usr/local/etc/nutNotify.conf
curlBin="/usr/bin/curl"
mailBin="/usr/bin/mail" source "$configFile"
configFile=/usr/local/bin/nutNotify.conf source "$(dirname $0)/nutNotifyFct.sh"
if [ ! -e "$configFile" ] ; then if [ ! -e "$configFile" ] ; then
echo "File $configFile doesn't exist" 1>&2 echo "File $configFile doesn't exist" 1>&2
@@ -47,10 +47,6 @@ if [ ! -x $mailBin ] ; then
exit 1 exit 1
fi fi
source "$configFile"
source "$(dirname $0)/nutNotifyFct.sh"
case "$argument" in case "$argument" in
ONLINE) ONLINE)
text="UPS $ups is now online at $(date +'%H:%M:%S')" text="UPS $ups is now online at $(date +'%H:%M:%S')"
+24 -28
View File
@@ -1,39 +1,35 @@
#!/bin/bash #!/bin/bash
# Variables
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 # variables
configFile=/usr/local/etc/nutNotify.conf
source "$configFile"
source "$(dirname $0)/nutNotifyFct.sh"
if [ ! -e "$configFile" ] ; then
echo "File $configFile doesn't exist" 1>&2
exit 1
fi
function aide() { function aide() {
echo "$0 [mail|pushbullet|telegram]" echo "$0 [mail|pushbullet|telegram]"
} }
# add to log # verify method
function addLog() {
if [ "$logfile" == "" ] ; then
echo "Can't write to log !" 1>&2
return 1
else
echo "$(date +'%a %d %H:%M:%S') $1" >> $logfile
return $?
fi
}
if [ $# == 0 ] ; then if [ $# == 0 ] ; then
if [ ! -e $BIN_MAIL ] ; then if [ ! -x $mailBin ] ; then
echo "Error $BIN_MAIL not found" 1>&2 && exit 1 echo "No mail command found at $mailBin, you need to install bsd-mailx!" 1>&2
exit 1
fi fi
notifynut_method=mail notifynut_method=mail
elif [ $# == 1 ] ; then elif [ $# == 1 ] ; then
if [ "$1" == "mail" ] && [ ! -e $BIN_MAIL ] ; then if [ "$1" == "mail" ] && [ ! -e $mailBin ] ; then
echo "Error $BIN_MAIL not found" 1>&2 && exit 1 echo "Error $BIN_MAIL not found" 1>&2 && exit 1
elif [ "$1" == "pushbullet" ] && [ ! -e $BIN_PUSHBULLET ] ; then elif [ "$1" == "pushbullet" ] && [ ! -x $curlBin ] && [ "$pushbulletAccessToken" != "" ] ; then
echo "Error $BIN_PUSHBULLET not found" 1>&2 && exit 1 echo "Error pushbullet not configured" 1>&2 && echo "Error telegram not configured" > $logfile && exit 1
elif [ "$1" == "mail" ] || [ "$1" == "pushbullet" ] ; then elif [ "$1" == "telegram" ] && [ ! -x $curlBin ] && [ "$telegramAccessToken" != "" ] && [ "$telegramChatID" != "" ] ; then
echo "Error telegram not configured" 1>&2 && echo "Error telegram not configured" > $logfile && exit 1
elif [ "$1" == "mail" ] || [ "$1" == "pushbullet" ] || [ "$1" == "telegram" ] ; then
notifynut_method="$1" notifynut_method="$1"
else else
aide aide
@@ -44,16 +40,16 @@ fi
text="$(date '+%d/%m/%y %H:%M:%S') $HOSTNAME booting\n Downtime $(date -d @$(( $(date +'%s') - $(cat $flagfile))) -u +%H:%M:%S)"
if [ -e $flagfile ] ; then if [ -e $flagfile ] ; then
case "$notifynut_method" in case "$notifynut_method" in
mail) mail)
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 ;; sendMail "$subjectMail" "$text" ;;
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)" ;; sendPushBullet "$pushbulletSubject" "$text" ;;
telegram) 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" ;; sendTelegram "$text" "$telegramSubject" ;;
esac esac
rm $flagfile rm $flagfile
fi fi
+3 -1
View File
@@ -1,7 +1,9 @@
[Unit] [Unit]
Description=send mail at boot after a power loss Description=send mail at boot after a power loss
After=network.target postfix.service After=network.target postfix.service
ConditionPathExists=/etc/nut/nut.conf ConditionPathExists=/usr/local/etc/nutNotify.conf
ConditionFileIsExecutable=/usr/local/bin/nutNotifyBoot.sh
ConditionPathIsDirectory=/var/log/nutNotify
[Service] [Service]
Type=simple Type=simple