add boot and shutdown script

This commit is contained in:
Gaëtan Brogniaux
2022-09-12 19:51:12 +02:00
parent 08ec7617e4
commit 57623baab2
5 changed files with 130 additions and 24 deletions
+24 -24
View File
@@ -1,7 +1,7 @@
#!/bin/bash
# Auteur : Belgotux
# Site : www.monlinux.net
# Licence : CC-BY-NC-SA
# Licence : GPLV3
# Version : 1.1
# Date : 01/03/18
# changelog
@@ -9,8 +9,8 @@
# v1.1 use pushbullet and somes reviews
## Notes :
##edit logrotate if needed
##the nut user must access to the logfile below
## edit logrotate if needed
## the nut user must access to the logfile below
if [ $# != 1 ] ; then
echo "Error : only one argument needed" 1>&2
@@ -29,7 +29,7 @@ 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
logfile=/var/log/nutNotify.log # logfile for nutNotify
curlBin="/usr/bin/curl"
configFile=/usr/local/bin/nutNotify.conf
@@ -40,7 +40,7 @@ fi
source "$configFile"
#add to log
# add to log
function addLog {
if [ "$logfile" == "" ] ; then
echo "Can't write to log !" 1>&2
@@ -56,11 +56,11 @@ function writeLog {
return $?
}
#send mail
# send mail
# $1 subjectMail (optional) - default $subjectMail
# $2 text/HTML body message - default $textMail
function sendMail {
#replace default mesg
# replace default mesg
if [ "$1" != "" ] ; then
subjectMail=$1
fi
@@ -68,7 +68,7 @@ function sendMail {
textMail=$2
fi
#var verification
# 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"
@@ -97,15 +97,15 @@ function sendMail {
return $?
}
#send sms
# send sms
# $1 text body message - default $textSms
function sendSms {
#replace default mesg
# replace default mesg
if [ "$1" != "" ] ; then
textSms=$1
fi
#var verification
# 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"
@@ -131,12 +131,12 @@ function sendSms {
}
#send push notification with pushbullet
# send push notification with pushbullet
# see https://www.pushbullet.com
# $1 title
# $2 text body - default $textPushBullet
function sendPushBullet {
#replace default mesg
# replace default mesg
if [ "$1" != "" ] ; then
subjectPushBullet=$1
fi
@@ -144,7 +144,7 @@ function sendPushBullet {
textPushBullet=$2
fi
#var verification
# var verification
if [ "$providerApi" == "" ] || [ "$accessToken" == "" ] ; 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"
@@ -153,7 +153,7 @@ function sendPushBullet {
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
# TODO check return
rm $tempfile
}
@@ -174,7 +174,7 @@ ONBATT)
;;
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 !"
writeLog
sendMail "$subjectMail" "$text"
@@ -183,7 +183,7 @@ LOWBATT)
;;
FSD)
#note : for slave only
# note : for slave only
text="Force shutdown slave server $server at $(date +'%H:%M:%S') !"
writeLog
sendMail "$subjectMail" "$text"
@@ -192,7 +192,7 @@ FSD)
;;
SHUTDOWN)
#note : executed on the master only
# note : executed on the master only
text="Shutdown master serveur $server at $(date +'%H:%M:%S') !"
writeLog
sendMail "$subjectMail" "$text"
@@ -204,14 +204,14 @@ COMMOK|COMMBAD|REPLBATT|NOCOMM)
writeLog
sendMail
sendPushBullet
#sendSms
# sendSms
;;
SERVERONLINE)
#note : log not for nutNotify but was call when server is poweroff after a failure
#add this to init nut script or make a new one
# note : log not for nutNotify but was call when server is poweroff after a failure
# add this to init nut script or make a new one
if [ -f $powerdownflag ] ; then
#add your script here to wakeup other servers etc
# add your script here to wakeup other servers etc
text="Serveur $server online at $(date +'%H:%M:%S') !"
rm -f $powerdownflag && \
writeLog && \
@@ -221,13 +221,13 @@ SERVERONLINE)
;;
*)
#nothing
# nothing
echo "Error : this argument is not managed" 1>&2
addLog "bad argument"
;;
esac
#Possible values for type:
# Possible values for type:
# ONLINE - UPS is back online
# ONBATT - UPS is on battery