Created
January 2, 2019 12:48
-
-
Save labeneator/6964683247bb08ef38daac578c09e70e to your computer and use it in GitHub Desktop.
Sends APC UPS metrics to graphite by running apcaccess cmd on Linux.
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
* * * * * root python /usr/local/bin/send_apc_metrics.py --carbon-host metrics.yourcompany.co.ke 2>&1 | logger -t "apc_metrics" |
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
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example log output