Skip to content

Instantly share code, notes, and snippets.

@jontow
Created October 30, 2021 18:35
Show Gist options
  • Save jontow/98bab496f302a349601706a8edcc362f to your computer and use it in GitHub Desktop.
Save jontow/98bab496f302a349601706a8edcc362f to your computer and use it in GitHub Desktop.
openbsd hw_sensors_metrics.sed
#!/usr/bin/env sed -E -f
################################################################################
#
# 2021-10-30 -- [email protected]
#
# Make heavy use of backreferences to avoid forking and translate OpenBSD sysctl(8)
# hw.sensors output into prometheus-compatible metrics
#
# Run this sed script like:
# $ sysctl hw.sensors | sed -E -f thisscript.sed
#
# Example operation, these:
# hw.sensors.nsclpcsio0.volt13=1.49 VDC (TS3)
# hw.sensors.owtemp0.temp1=17.12 degC (sn 0000078fa11b)
# Become these:
# hw_sensors_nsclpcsio_volt{unit="VDC",controller_id="0",sensor_id="13"} 1.49
# hw_sensors_owtemp_temp{unit="degC",controller_id="0",sensor_id="1",serial="0000078fa11b",name="closet"} 17.12
#
################################################################################
# Change any metric name periods into underscores, some will need changing back, later:
s/\./_/g
# Drop any equals signs
s/\=/ /g
# Drop (sn from serial numbers, we just turn the entire (val) into serial="val"
s/\(sn\ /\(/g
# Turn (sn into serial=, also transforms things like (TS3
s/\(([0-9A-Za-z]+)/,serial="\1/g
# Replace trailing parentheses with a closing double quote
s/\)/"/g
# Carefully chop out controller and sensor ids, place at end, wrapped/quoted
s/^(hw_sensors_[a-z]+)([0-9]+)_([a-z]+)([0-9]+)\ (.+)$/\1_\3 \5,controller_id="\2",sensor_id="\4"/g
################################################################################
###
### Sensor-specific transforms
###
# Wrap unit for temperature sensors
# Raw: hw.sensors.owtemp0.temp0=17.06 degC (sn 0000078fa11b)
# Broken: hw_sensors_owtemp_temp 17_06 degC ,serial="0000078fa11b",controller_id="0",sensor_id="0"
s/([^ ]+_temp)\ ([0-9_]+)\ ([^ ]+) ([^ ]+)$/\1{unit="\3",\4} \2/g
# Wrap unit for voltage sensors, move value to end
# Raw: hw.sensors.nsclpcsio0.volt6=2.62 VDC
# Broken: hw_sensors_nsclpcsio_volt 2_62 VDC,controller_id="0",sensor_id="6"
s/^([^ ]+_volt)\ ([0-9_]+)\ ([A-Za-z]+)[ ]*(,[^ ]+)$/\1{unit="\3"\4} \2/g
# Fix owid_raw sensors
# Raw: hw.sensors.owid0.raw0=2999314 (sn 0000002dc412)
# Broken: hw_sensors_owid_raw 2999314 ,serial="0000002dc412",controller_id="0",sensor_id="0"
s/^(hw_sensors_owid_raw)\ ([0-9]+)\ ,([^ ]+)$/\1{\3} \2/g
# Fix viomb sensors
# Raw: hw.sensors.viomb0.raw0=0 (desired)
# Broken: hw_sensors_viomb_raw 0 ,serial="desired",controller_id="0",sensor_id="0"
s/^(hw_sensors_viomb_raw)\ ([0-9]+)\ ,([^ ]+)$/\1{\3} \2/g
# Fix softraid drive sensors:
# Raw: hw.sensors.softraid0.drive0=online (sd2), OK
# Broken: hw_sensors_softraid_drive online ,serial="sd2", OK,controller_id="0",sensor_id="0"
s/^(hw_sensors_softraid_drive)\ ([^ ]+)\ ,([^ ]+), ([A-Za-z0-9]+),([^ ]+)$/\1{\3,status="\4",\5} \2/g
################################################################################
###
### Serial number to name lookups
###
s/(serial=\"0000078fa11b\")/\1,name="closet"/g
################################################################################
# Chop any double-commas to singles
s/,,/,/g
# Finally, fix underscore in value
s/^([^ ]+)\ ([0-9]+)_([0-9]+)$/\1 \2\.\3/g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment