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

View File

@@ -3,12 +3,16 @@
# 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
bodyDefault="UPS event $argument on $ups at $(date +'%d-%m-%y %H:%M:%S')" # default body message
#--- Data for mail ---#
FROM="" #sender
dom=$(echo "$FROM" | awk -F'@' '{printf $2}') #don't change it
MAILTO="" #recipient(s)
subjectMail="$subjectDefault" #subject sent by mail
textMail="$bodyDefault" #body message sent by mail

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)
# Variables
logfile=/var/log/nutNotify.log # logfile for nutNotify
curlBin="/usr/bin/curl"
mailBin="/usr/bin/mail"
configFile=/usr/local/bin/nutNotify.conf
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
@@ -47,10 +47,6 @@ if [ ! -x $mailBin ] ; then
exit 1
fi
source "$configFile"
source "$(dirname $0)/nutNotifyFct.sh"
case "$argument" in
ONLINE)
text="UPS $ups is now online at $(date +'%H:%M:%S')"

View File

@@ -1,39 +1,35 @@
#!/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() {
echo "$0 [mail|pushbullet|telegram]"
}
# add to log
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
}
# verify method
if [ $# == 0 ] ; then
if [ ! -e $BIN_MAIL ] ; then
echo "Error $BIN_MAIL not found" 1>&2 && exit 1
if [ ! -x $mailBin ] ; then
echo "No mail command found at $mailBin, you need to install bsd-mailx!" 1>&2
exit 1
fi
notifynut_method=mail
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
elif [ "$1" == "pushbullet" ] && [ ! -e $BIN_PUSHBULLET ] ; then
echo "Error $BIN_PUSHBULLET not found" 1>&2 && exit 1
elif [ "$1" == "mail" ] || [ "$1" == "pushbullet" ] ; then
elif [ "$1" == "pushbullet" ] && [ ! -x $curlBin ] && [ "$pushbulletAccessToken" != "" ] ; then
echo "Error pushbullet not configured" 1>&2 && echo "Error telegram not configured" > $logfile && exit 1
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"
else
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
case "$notifynut_method" in
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)
$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)
$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
rm $flagfile
fi

View File

@@ -1,7 +1,9 @@
[Unit]
Description=send mail at boot after a power loss
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]
Type=simple