Skip to content

Instantly share code, notes, and snippets.

@labeneator
Created January 2, 2019 12:48
Show Gist options
  • Save labeneator/6964683247bb08ef38daac578c09e70e to your computer and use it in GitHub Desktop.
Save labeneator/6964683247bb08ef38daac578c09e70e to your computer and use it in GitHub Desktop.
Sends APC UPS metrics to graphite by running apcaccess cmd on Linux.
* * * * * root python /usr/local/bin/send_apc_metrics.py --carbon-host metrics.yourcompany.co.ke 2>&1 | logger -t "apc_metrics"
#!/usr/bin/env python
import click
import time
import socket
import string
import platform
import subprocess
from contextlib import closing
def mk_metric_prefix():
return "apc_ups.%s" % ".".join(reversed(platform.node().split(".")))
def send_metrics(carbon_host, carbon_port):
fields_of_interest = ["LINEV", "LOADPCT", "BCHARGE", "TIMELEFT", "BATTV"]
apcaccess_output = subprocess.check_output(['/sbin/apcaccess'])
print("apcaccess cmd output: %s" % apcaccess_output)
timestamp = int(time.time())
metrics_prefix = mk_metric_prefix()
with closing(socket.socket()) as sock:
sock.connect((carbon_host, carbon_port))
for line in apcaccess_output.splitlines():
key, val = map(string.strip, line.split(": "))
if key in fields_of_interest:
print(key,val)
metric_name = "%s.%s" % (metrics_prefix, key)
metric_value = val.split(" ")[0]
send_metric(sock, timestamp, metric_name, metric_value)
def send_metric(sock, timestamp, metric_prefix, metric):
msg = "%s %s %s\n" % (metric_prefix, metric, timestamp)
print("Sending: %s" % msg)
sock.sendall(msg)
@click.command()
@click.option('--carbon-host', default="localhost", help='The carbon host')
@click.option('--carbon-port', default=2003, type=int, help='The carbon port')
def main(carbon_host, carbon_port):
"""Simple program that grabs apcupsd statsand sends metrics to Carbon"""
click.echo('Starting...')
send_metrics(carbon_host, carbon_port)
if __name__ == '__main__':
main()
@labeneator
Copy link
Author

Example log output

Jan  2 15:50:01 gpu CRON[29147]: (root) CMD (python /usr/local/bin/send_apc_metrics.py  --carbon-host metrics.yourcompany.co.ke 2>&1 | logger -t "apc_metrics")
Jan  2 15:50:01 gpu apc_metrics: Starting...
Jan  2 15:50:01 gpu apc_metrics: apcaccess cmd output: APC      : 001,036,0858
Jan  2 15:50:01 gpu apc_metrics: DATE     : 2019-01-02 15:49:58 +0300
Jan  2 15:50:01 gpu apc_metrics: HOSTNAME : gpu
Jan  2 15:50:01 gpu apc_metrics: VERSION  : 3.14.14 (31 May 2016) debian
Jan  2 15:50:01 gpu apc_metrics: UPSNAME  : gpu
Jan  2 15:50:01 gpu apc_metrics: CABLE    : USB Cable
Jan  2 15:50:01 gpu apc_metrics: DRIVER   : USB UPS Driver
Jan  2 15:50:01 gpu apc_metrics: UPSMODE  : Stand Alone
Jan  2 15:50:01 gpu apc_metrics: STARTTIME: 2019-01-02 13:52:40 +0300
Jan  2 15:50:01 gpu apc_metrics: MODEL    : Back-UPS XS 1400U
Jan  2 15:50:01 gpu apc_metrics: STATUS   : ONLINE
Jan  2 15:50:01 gpu apc_metrics: LINEV    : 242.0 Volts
Jan  2 15:50:01 gpu apc_metrics: LOADPCT  : 33.0 Percent
Jan  2 15:50:01 gpu apc_metrics: BCHARGE  : 57.0 Percent
Jan  2 15:50:01 gpu apc_metrics: TIMELEFT : 1.6 Minutes
Jan  2 15:50:01 gpu apc_metrics: MBATTCHG : 5 Percent
Jan  2 15:50:01 gpu apc_metrics: MINTIMEL : 3 Minutes
Jan  2 15:50:01 gpu apc_metrics: MAXTIME  : 0 Seconds
Jan  2 15:50:01 gpu apc_metrics: SENSE    : Medium
Jan  2 15:50:01 gpu apc_metrics: LOTRANS  : 155.0 Volts
Jan  2 15:50:01 gpu apc_metrics: HITRANS  : 280.0 Volts
Jan  2 15:50:01 gpu apc_metrics: ALARMDEL : 30 Seconds
Jan  2 15:50:01 gpu apc_metrics: BATTV    : 28.2 Volts
Jan  2 15:50:01 gpu apc_metrics: LASTXFER : No transfers since turnon
Jan  2 15:50:01 gpu apc_metrics: NUMXFERS : 0
Jan  2 15:50:01 gpu apc_metrics: TONBATT  : 0 Seconds
Jan  2 15:50:01 gpu apc_metrics: CUMONBATT: 0 Seconds
Jan  2 15:50:01 gpu apc_metrics: XOFFBATT : N/A
Jan  2 15:50:01 gpu apc_metrics: SELFTEST : NO
Jan  2 15:50:01 gpu apc_metrics: STATFLAG : 0x05000008
Jan  2 15:50:01 gpu apc_metrics: SERIALNO : 3B1503X25910
Jan  2 15:50:01 gpu apc_metrics: BATTDATE : 2015-01-18
Jan  2 15:50:01 gpu apc_metrics: NOMINV   : 230 Volts
Jan  2 15:50:01 gpu apc_metrics: NOMBATTV : 24.0 Volts
Jan  2 15:50:01 gpu apc_metrics: NOMPOWER : 700 Watts
Jan  2 15:50:01 gpu apc_metrics: FIRMWARE : 926.T1 .I USB FW:T1
Jan  2 15:50:01 gpu apc_metrics: END APC  : 2019-01-02 15:50:01 +0300
Jan  2 15:50:01 gpu apc_metrics:
Jan  2 15:50:01 gpu apc_metrics: ('LINEV', '242.0 Volts')
Jan  2 15:50:01 gpu apc_metrics: Sending: apc_ups.gpu.LINEV 242.0 1546433401
Jan  2 15:50:01 gpu apc_metrics:
Jan  2 15:50:01 gpu apc_metrics: ('LOADPCT', '33.0 Percent')
Jan  2 15:50:01 gpu apc_metrics: Sending: apc_ups.gpu.LOADPCT 33.0 1546433401
Jan  2 15:50:01 gpu apc_metrics:
Jan  2 15:50:01 gpu apc_metrics: ('BCHARGE', '57.0 Percent')
Jan  2 15:50:01 gpu apc_metrics: Sending: apc_ups.gpu.BCHARGE 57.0 1546433401
Jan  2 15:50:01 gpu apc_metrics:
Jan  2 15:50:01 gpu apc_metrics: ('TIMELEFT', '1.6 Minutes')
Jan  2 15:50:01 gpu apc_metrics: Sending: apc_ups.gpu.TIMELEFT 1.6 1546433401
Jan  2 15:50:01 gpu apc_metrics:
Jan  2 15:50:01 gpu apc_metrics: ('BATTV', '28.2 Volts')
Jan  2 15:50:01 gpu apc_metrics: Sending: apc_ups.gpu.BATTV 28.2 1546433401
Jan  2 15:50:01 gpu apc_metrics:

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