add boot and shutdown script
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
nutNotify.conf
|
||||
48
nutNotify.sh
48
nutNotify.sh
@@ -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
|
||||
|
||||
58
nutNotifyBoot.sh
Normal file
58
nutNotifyBoot.sh
Normal file
@@ -0,0 +1,58 @@
|
||||
#!/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
|
||||
|
||||
MAILTO=root
|
||||
|
||||
function aide() {
|
||||
echo "$0 [mail|pushbullet]"
|
||||
}
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
if [ $# == 0 ] ; then
|
||||
if [ ! -e $BIN_MAIL ] ; then
|
||||
echo "Error $BIN_MAIL not found" 1>&2 && exit 1
|
||||
fi
|
||||
notifynut_method=mail
|
||||
elif [ $# == 1 ] ; then
|
||||
if [ "$1" == "mail" ] && [ ! -e $BIN_MAIL ] ; 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
|
||||
notifynut_method="$1"
|
||||
else
|
||||
aide
|
||||
fi
|
||||
else
|
||||
echo "Error the method $1 is not supported"
|
||||
fi
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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 ;;
|
||||
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)" ;;
|
||||
esac
|
||||
rm $flagfile
|
||||
fi
|
||||
|
||||
exit 0
|
||||
28
nutShutdown.sh
Normal file
28
nutShutdown.sh
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
# Auteur : Belgotux
|
||||
# Site : www.monlinux.net
|
||||
# Licence : GPLV3
|
||||
# Version : 1.0
|
||||
# Date : 08/04/2012
|
||||
|
||||
# Variables
|
||||
logfile=/var/log/nutNotify/nutNotify.log #logfile for nutNotify
|
||||
flagfile=/var/log/nutNotify/nutShutdown.flag
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
|
||||
addLog "poweroff $HOSTNAME"
|
||||
date +'%s' > $flagfile
|
||||
shutdown -h +0
|
||||
|
||||
exit 0
|
||||
19
systemd-notify
Normal file
19
systemd-notify
Normal file
@@ -0,0 +1,19 @@
|
||||
[Unit]
|
||||
Description=send mail at boot after a power loss
|
||||
After=network.target postfix.service
|
||||
ConditionPathExists=/etc/nut/nut.conf
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
Group=root
|
||||
ExecStart=/usr/local/bin/nutNotifyBoot.sh
|
||||
|
||||
SuccessExitStatus=0
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user