Skip to content

Instantly share code, notes, and snippets.

@FlorianHeigl
Created January 10, 2024 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlorianHeigl/47a33ac6c212928d7b021f328e3075f9 to your computer and use it in GitHub Desktop.
Save FlorianHeigl/47a33ac6c212928d7b021f328e3075f9 to your computer and use it in GitHub Desktop.
apcupsd
#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when apcupsd
# loses contact with the UPS (i.e. the serial connection is not responding).
# We send an email message to root to notify him.
#
HOSTNAME=`hostname`
MSG="$HOSTNAME Communications with UPS $1 lost"
wait=12
# Wait $wait seconds, and only send email
# if still not online
sleep $wait
status=$(/sbin/apcaccess status)
if echo "$status" | grep -q '^STATUS.*COMMLOST' ; then
logger -t apcupsd "commfailure for over $wait seconds. Sending mail"
(
echo "$MSG for over $wait seconds"
echo "$status"
) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN
# ensure mail will also be sent on restore
rm -f /run/apcupsd.nomail
else
touch /run/apcupsd.nomail
logger -t apcupsd "commfailure for less than $wait seconds. Created nomail file, and not sending any email."
fi
exit 0
#!/bin/sh
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when apcupsd
# restores contact with the UPS (i.e. the serial connection is restored).
# We send an email message to root to notify him.
#
# wait at least as long as the wait defined in commfailure
# so that we know if we need to send mail or not
wait=13
sleep $wait
if [ -f /run/apcupsd.nomail ]; then
logger -t apcupsd "Skip sending mail on restore (found nomail set by commfailure)"
exit
fi
HOSTNAME=`hostname`
MSG="$HOSTNAME Communications with UPS $1 restored"
#
(
echo "$MSG"
echo " "
/sbin/apcaccess status
) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN
exit 0
@FlorianHeigl
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment