Skip to content

Instantly share code, notes, and snippets.

@akomakom
Last active June 28, 2021 14:03
Show Gist options
  • Save akomakom/2c0c18e5baf82f61cbcaf38b984f114d to your computer and use it in GitHub Desktop.
Save akomakom/2c0c18e5baf82f61cbcaf38b984f114d to your computer and use it in GitHub Desktop.
Plugin for apcupsd to prevent low battery shutdown when external DC source is connected
#!/usr/bin/env python
# Prevent shutdown when batteries are not really empty
# installed in /etc/apcupsd/ as 'doshutdown' and 'mainsback' (symlink)
import os
from sys import argv
from subprocess import check_output
from syslog import syslog
threshold=24.3 # If batteries are above this, don't shut down
apcupsd_restart_command=['service', 'apcupsd', 'restart'] #adjust to your needs
marker_file='/etc/apcupsd/shutdown_prevented'
def log(str):
syslog('%s: %s' % (argv[0], str))
# behavior determined by script name:
if "doshutdown" in argv[0]:
voltage=float(check_output(['apcaccess', '-p', 'BATTV', '-u']))
log('Voltage is %s' % voltage)
# if there was a prior marker file, remove it
if os.path.isfile(marker_file):
os.remove(marker_file)
# prevent shutdown if batteries are not actually low:
if voltage > threshold:
log('Above threshold of %s, preventing shutdown' % threshold)
open(marker_file,'a').close() # touch a file
exit(99) #prevent shutdown
elif "mainsback" in argv[0]:
if os.path.isfile(marker_file):
log("Cleaning up prevented shutdown by restarting apcupsd")
os.remove(marker_file)
# this was a prevented shutdown. apcupsd is in a weird state now, so let's restart it
# this will also remove /etc/apcupsd/powerfail marker file:
check_output(apcupsd_restart_command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment