fix mail function to use mail daemon + split fct
This commit is contained in:
+16
-152
@@ -2,11 +2,14 @@
|
||||
# Auteur : Belgotux
|
||||
# Site : www.monlinux.net
|
||||
# Licence : GPLV3
|
||||
# Version : 1.1
|
||||
# Version : 2.0
|
||||
# Date : 01/03/18
|
||||
# Update : 04/01/23
|
||||
# changelog
|
||||
# v1.0 use mail and SMS
|
||||
# v1.1 use pushbullet and somes reviews
|
||||
# v1.2 add telegram support
|
||||
# v2.0 modify mail function to use mail daemon + split function file
|
||||
|
||||
## Notes :
|
||||
## edit logrotate if needed
|
||||
@@ -17,11 +20,6 @@ if [ $# != 1 ] ; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -x $curlBin ] ; then
|
||||
echo "No curl fount at $curlBin ! Install it!" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# statics variables - don't change it
|
||||
argument=$(echo "$1" | awk '{printf $1}')
|
||||
ups=$(echo "$1" | awk '{printf $2}' | awk -F "[@:]" '{print $1}')
|
||||
@@ -31,6 +29,7 @@ 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
|
||||
|
||||
if [ ! -e "$configFile" ] ; then
|
||||
@@ -38,154 +37,19 @@ if [ ! -e "$configFile" ] ; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -x $curlBin ] ; then
|
||||
echo "No curl fount at $curlBin ! Install it!" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -x $mailBin ] ; then
|
||||
echo "No mail command found at $mailBin, you need to install bsd-mailx!" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
source "$configFile"
|
||||
source "$(dirname $0)/nutNotifyFct.sh"
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
function writeLog {
|
||||
addLog "$HOSTNAME : UPS $ups state $argument"
|
||||
return $?
|
||||
}
|
||||
|
||||
# send mail
|
||||
# $1 subjectMail (optional) - default $subjectMail
|
||||
# $2 text/HTML body message - default $textMail
|
||||
function sendMail {
|
||||
# replace default mesg
|
||||
if [ "$1" != "" ] ; then
|
||||
subjectMail=$1
|
||||
fi
|
||||
if [ "$2" != "" ] ; then
|
||||
textMail=$2
|
||||
fi
|
||||
|
||||
# var verification
|
||||
if [ "$FROM" == "" ] || [ "$MAILTO" == "" ] || [ "$subjectMail" == "" ] || [ "$textMail" == "" ] ; then
|
||||
echo "Can't send mail without complete variables" 1>&2
|
||||
addLog "Can't send mail without complete variables"
|
||||
return 1
|
||||
fi
|
||||
|
||||
(
|
||||
echo "From: $FROM"
|
||||
echo "To: $MAILTO"
|
||||
echo "MIME-Version: 1.0"
|
||||
echo "Content-Type: multipart/mixed;"
|
||||
echo " boundary=\"PAA08673.1018277622/$dom\""
|
||||
echo "Subject: $subjectMail"
|
||||
echo ""
|
||||
echo "This is a MIME-encapsulated message"
|
||||
echo ""
|
||||
echo "--PAA08673.1018277622/$dom"
|
||||
echo "Content-Type: text/html"
|
||||
echo ""
|
||||
|
||||
echo "$textMail"
|
||||
|
||||
echo "--PAA08673.1018277622/$dom"
|
||||
|
||||
) | sendmail -t
|
||||
return $?
|
||||
}
|
||||
|
||||
# send sms
|
||||
# $1 text body message - default $textSms
|
||||
function sendSms {
|
||||
# replace default mesg
|
||||
if [ "$1" != "" ] ; then
|
||||
textSms=$1
|
||||
fi
|
||||
|
||||
# var verification
|
||||
if [ "$providerSms" == "" ] || [ "$usernameSms" == "" ] || [ "$passwordSms" == "" ] || [ "$fromSms" == "" ] || [ "$toSms" == "" ] || [ "$textSms" == "" ] ; then
|
||||
echo "Can't send SMS without complete variables" 1>&2
|
||||
addLog "Can't send mail without complete variables"
|
||||
return 1
|
||||
fi
|
||||
|
||||
textSms=$(echo $textSms | sed -e 's/ /%20/g' | iconv -t iso-8859-1)
|
||||
|
||||
url="https://www.$providerSms/myaccount/sendsms.php?username=$usernameSms&password=$passwordSms&from=$fromSms&to=$toSms&text=$textSms"
|
||||
tempfile=$(tempfile -p 'nutNotifySms-')
|
||||
curl -s -o $tempfile $url
|
||||
result=$(cat $tempfile | grep '<result>' | awk -F "[<>]" '{print $3}')
|
||||
description=$(cat $tempfile | grep '<description>' | awk -F "[<>]" '{print $3}')
|
||||
if [ "$result" == 1 ] ; then
|
||||
addLog "sendSMS : success"
|
||||
return 0
|
||||
else
|
||||
echo "sendSMS : fail - $description" 1>&2
|
||||
addLog "sendSMS : fail - $description"
|
||||
return 1
|
||||
fi
|
||||
rm $tempfile
|
||||
|
||||
}
|
||||
|
||||
# send push notification with pushbullet
|
||||
# see https://www.pushbullet.com
|
||||
# $1 title
|
||||
# $2 text body - default $textPushBullet
|
||||
function sendPushBullet {
|
||||
#replace default mesg
|
||||
if [ "$1" != "" ] ; then
|
||||
subjectPushBullet=$1
|
||||
fi
|
||||
if [ "$2" != "" ] ; then
|
||||
textPushBullet=$2
|
||||
fi
|
||||
|
||||
#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=$(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
|
||||
ONLINE)
|
||||
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
# 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
|
||||
}
|
||||
|
||||
function writeLog {
|
||||
addLog "$HOSTNAME : UPS $ups state $argument"
|
||||
return $?
|
||||
}
|
||||
|
||||
# send mail
|
||||
# $1 subjectMail (optional) - default $subjectMail
|
||||
# $2 text/HTML body message - default $textMail
|
||||
function sendMail {
|
||||
# replace default mesg
|
||||
if [ "$1" != "" ] ; then
|
||||
subjectMail=$1
|
||||
fi
|
||||
if [ "$2" != "" ] ; then
|
||||
textMail=$2
|
||||
fi
|
||||
|
||||
# var verification
|
||||
if [ "$FROM" == "" ] || [ "$MAILTO" == "" ] || [ "$subjectMail" == "" ] || [ "$textMail" == "" ] ; then
|
||||
echo "Can't send mail without complete variables" 1>&2
|
||||
addLog "Can't send mail without complete variables"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "$textMail" | mail -s "$subjectMail" -r "$FROM" "$MAILTO"
|
||||
return $?
|
||||
}
|
||||
|
||||
# send sms
|
||||
# $1 text body message - default $textSms
|
||||
function sendSms {
|
||||
# replace default mesg
|
||||
if [ "$1" != "" ] ; then
|
||||
textSms=$1
|
||||
fi
|
||||
|
||||
# var verification
|
||||
if [ "$providerSms" == "" ] || [ "$usernameSms" == "" ] || [ "$passwordSms" == "" ] || [ "$fromSms" == "" ] || [ "$toSms" == "" ] || [ "$textSms" == "" ] ; then
|
||||
echo "Can't send SMS without complete variables" 1>&2
|
||||
addLog "Can't send mail without complete variables"
|
||||
return 1
|
||||
fi
|
||||
|
||||
textSms=$(echo $textSms | sed -e 's/ /%20/g' | iconv -t iso-8859-1)
|
||||
|
||||
url="https://www.$providerSms/myaccount/sendsms.php?username=$usernameSms&password=$passwordSms&from=$fromSms&to=$toSms&text=$textSms"
|
||||
tempfile=$(tempfile -p 'nutNotifySms-')
|
||||
curl -s -o $tempfile $url
|
||||
result=$(cat $tempfile | grep '<result>' | awk -F "[<>]" '{print $3}')
|
||||
description=$(cat $tempfile | grep '<description>' | awk -F "[<>]" '{print $3}')
|
||||
if [ "$result" == 1 ] ; then
|
||||
addLog "sendSMS : success"
|
||||
return 0
|
||||
else
|
||||
echo "sendSMS : fail - $description" 1>&2
|
||||
addLog "sendSMS : fail - $description"
|
||||
return 1
|
||||
fi
|
||||
rm $tempfile
|
||||
|
||||
}
|
||||
|
||||
# send push notification with pushbullet
|
||||
# see https://www.pushbullet.com
|
||||
# $1 title
|
||||
# $2 text body - default $textPushBullet
|
||||
function sendPushBullet {
|
||||
#replace default mesg
|
||||
if [ "$1" != "" ] ; then
|
||||
subjectPushBullet=$1
|
||||
fi
|
||||
if [ "$2" != "" ] ; then
|
||||
textPushBullet=$2
|
||||
fi
|
||||
|
||||
#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=$(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
|
||||
}
|
||||
Reference in New Issue
Block a user