Created
December 29, 2018 19:16
-
-
Save dorkmatt/946bd88119cbe963e3d5c20b6c62399a to your computer and use it in GitHub Desktop.
prometheus node_exporter digitemp text_collector
This file contains hidden or 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 | |
# from 35c3 event, dump digitemp 1wire sensor data in prometheus text collector format | |
# Inspired by SuperQ's node_exporter/text_collector_examples/ntpd_metrics.py | |
# Author: Matt Peterson <[email protected]> | |
import subprocess | |
import sys | |
digitemp_command = ['digitemp_DS9097U','-q','-a','-o%R %C','-c','/root/.digitemprc'] | |
# iButtonLink QR code is little endian, swap digitemp output | |
def big2little(string): | |
t = bytearray.fromhex(string) | |
t.reverse() | |
return ''.join(format(x,'02x') for x in t).upper() | |
def get_output(command): | |
try: | |
output = subprocess.check_output(command) | |
except subprocess.CalledProcessError as e: | |
return None | |
for line in output.decode().split('\n'): | |
sensor_serial = big2little(line.rstrip().split(' ')[0]) | |
reading_celsius = line.rstrip().split(' ')[1] | |
return "digitemp_DS9097U_sensor{address=\"" + sensor_serial + "\"} " + reading_celsius | |
if __name__ == "__main__": | |
print get_output(digitemp_command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment