Skip to content

Instantly share code, notes, and snippets.

@matsubo
Last active June 30, 2024 16:25
Show Gist options
  • Save matsubo/764666d00d25f2a53b505292c9a7c501 to your computer and use it in GitHub Desktop.
Save matsubo/764666d00d25f2a53b505292c9a7c501 to your computer and use it in GitHub Desktop.

APC upsd mackerel plugin

Screenshot

image

Setup

curl https://gist.githubusercontent.com/matsubo/764666d00d25f2a53b505292c9a7c501/raw/ec20fbb26a966c0b101aa44b82c96e1ebbdb06b7/ups.py > ups.py
chmod 755 ups.py
sudo mv ups.py /usr/local/bin/

Add folloing setting on /etc/mackerel-agent/mackerel-agent.conf

[plugin.metrics.ups]
command = ["python3", "/usr/local/bin/ups.py"]

Then restart mackerel agent daemon.

sudo systemctl restart mackerel-agent.service
#!/usr/bin/env python3
import subprocess
import re
import time
def get_apcaccess_output():
result = subprocess.run(['apcaccess'], capture_output=True, text=True)
return result.stdout
def parse_apcaccess_output(output):
metrics = {}
patterns = {
'linev': r'LINEV\s+:\s+([\d.]+)\s+Volts',
'loadpct': r'LOADPCT\s+:\s+([\d.]+)\s+Percent',
'bcharge': r'BCHARGE\s+:\s+([\d.]+)\s+Percent',
'timeleft': r'TIMELEFT\s+:\s+([\d.]+)\s+Minutes',
'battv': r'BATTV\s+:\s+([\d.]+)\s+Volts'
}
for key, pattern in patterns.items():
match = re.search(pattern, output)
if match:
metrics[key] = float(match.group(1))
return metrics
def main():
output = get_apcaccess_output()
metrics = parse_apcaccess_output(output)
timestamp = int(time.time())
for key, value in metrics.items():
print(f"ups.{key}\t{value}\t{timestamp}")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment