Skip to content

Instantly share code, notes, and snippets.

@dorkmatt
Created December 29, 2018 19:16
Show Gist options
  • Save dorkmatt/946bd88119cbe963e3d5c20b6c62399a to your computer and use it in GitHub Desktop.
Save dorkmatt/946bd88119cbe963e3d5c20b6c62399a to your computer and use it in GitHub Desktop.
prometheus node_exporter digitemp text_collector
#!/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 <matt@peterson.org>
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