Created
July 8, 2022 12:21
-
-
Save gioxx/066500b1193d26b9d11c22409aef82fc to your computer and use it in GitHub Desktop.
Tieni sotto controllo eventuali blackout di corrente tramite eventi / controlli sul Raspberry Pi. https://gioxx.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Electricity Power Check Script | |
# GSolone, 2022 - https://gioxx.org | |
#================================================================================ | |
# Credits: | |
# https://raspberrypi-guide.github.io/programming/run-script-on-boot | |
# https://unix.stackexchange.com/a/616085 | |
# https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/ | |
# https://www.cyberciti.biz/tips/howto-linux-unix-write-to-syslog.html | |
# https://askubuntu.com/a/77739 | |
#================================================================================ | |
powercheck=/home/pi/PWR_lastcheck.txt | |
notificationaddress=admin@contoso.com | |
BASHDATE=`date +"%b %d, %Y"` | |
BASHTIME=`date +"%T"` | |
TICK="[\e[32m ✔ \e[0m]" | |
ERROR="[\e[31m ✖ \e[0m]" | |
COL_NC="\e[0m" # No Color | |
COL_LIGHT_GREEN="\e[1;32m" | |
COL_LIGHT_RED="\e[1;31m" | |
VERSION="0.3 (20220629-02)" | |
case "$1" in | |
help) | |
scriptname=$(basename "$0") | |
echo | |
echo -e " ██████████ | |
████ ████ | |
██ ▒▒▒▒▒▒ ██ | |
██ ▒▒▒▒▒▒▒▒▒▒ ██ | |
██ ▒▒▒▒▒▒▒▒▒▒ ██ | |
██▒▒ ▒▒▒▒▒▒▒▒▒▒ ▒▒██ | |
██▒▒▒▒ ▒▒▒▒▒▒ ▒▒▒▒██ | |
██▒▒▒▒ ▒▒▒▒██ | |
██▒▒ ████████ ▒▒▒▒██ | |
██ ██████░░░░██████ ▒▒██ | |
██ ██░░░░██░░░░██░░░░██ ██ | |
████░░░░██░░░░██░░░░░░██ ██ | |
██░░░░░░░░░░░░░░░░░░██ ██ | |
██░░░░████████░░░░░░████ | |
██░░░░░░░░░░░░░░████ ${COL_LIGHT_GREEN}Electricity Power Check Script ${VERSION}${COL_NC} | |
████████████████████ ${COL_LIGHT_RED}GSolone, 2022, https://gioxx.org${COL_NC} | |
████▓▓██░░░░██▓▓██░░░░██ (Toad Ascii Art made by textart.sh) | |
██░░██▓▓██░░░░██▓▓▓▓██░░░░██ | |
██░░██▓▓██░░░░░░██▓▓▓▓██░░░░██ | |
██░░██▓▓██████████▓▓▓▓▓▓██░░░░██ | |
██░░██▓▓██ ██▓▓▓▓██░░░░██ | |
██████ ██ ██████████ | |
██ ██ ██ | |
██▓▓▓▓▓▓████▓▓▓▓██████ | |
██▓▓▓▓▓▓▓▓██▓▓▓▓▓▓▓▓▓▓██ | |
████████████████████████" | |
echo | |
echo -e "${COL_LIGHT_GREEN}Electricity Power Check: Is there power in the house?${COL_NC}" | |
echo -e "This script save (via crontab) the latest date and time when power is available in the house." | |
echo -e "Checking RPi uptime, you can obtain an email notification and verify how many minutes (hours/days/etc) the power was not available (when uptime is not greater than XX mins)." | |
echo | |
echo -e "\033[1mHow to use\033[0m" | |
echo -e " Modify $scriptname and change 'powercheck' (absoute path where you want to save PWR_lastcheck.txt file) and 'notificationaddress' (email address to notify when Raspberry Pi reboots) vars. Save and exit." | |
echo -e " chmod +x $scriptname" | |
echo | |
echo -e "\033[1mSchedule $scriptname\033[0m" | |
echo -e " Manual Execution (not recommended): ${COL_LIGHT_GREEN}./$scriptname${COL_NC} (to execute and save last check file, $powercheck)" | |
echo -e " Automatic Execution (recommended): use crontab, ${COL_LIGHT_GREEN}0 */5 * * * /ABSOLUTE/PATH/WHERE/FIND/$scriptname${COL_NC}" | |
echo | |
;; | |
clean) | |
rm $powercheck | |
echo -e " ${TICK} \e[32m Log deleted \e[0m" | |
;; | |
*) | |
echo Power available on ${BASHDATE} at ${BASHTIME}. > $powercheck | |
upSeconds="$(cat /proc/uptime | grep -o '^[0-9]\+')" | |
upMins=$((${upSeconds} / 60)) | |
if [ "${upMins}" -gt "5" ] | |
then | |
echo -e " ${TICK} \e[32m Up for ${upMins} minutes. \e[0m" | |
else | |
echo -e " ${ERROR} \e[31m Up 5 minutes or less, send notification ... \e[0m" | |
while [ "$(hostname -I)" = "" ]; do | |
echo -e " ${ERROR} \e[31m No network, wait 1s. \e[0m" | |
sleep 1 | |
done | |
echo -e " ${TICK} \e[32m I have network, send notification now! \e[0m" | |
lastcheck=$(<"$powercheck") | |
if [ -z "$notificationaddress" ] | |
then | |
echo -e "Email address not specified, log to /var/log/messages" | |
logger "RPi Startup on ${BASHDATE} at ${BASHTIME}. Latest electricity power check on $lastcheck. Uptime: ${upMins}." | |
else | |
echo -e "RPi Startup on ${BASHDATE} at ${BASHTIME}\nLatest electricity power check on $lastcheck.\nUptime: ${upMins}." | mail -s "RPi3-Startup alert" $notificationaddress | |
fi | |
fi | |
echo -e " ${TICK} \e[32m Last check on ${BASHDATE} at ${BASHTIME} \e[0m" | |
exit 0 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment