Skip to content

Instantly share code, notes, and snippets.

@markus-k
Last active November 8, 2022 16:21
Show Gist options
  • Save markus-k/42194d5aa6f856c7e21b0acbd23dc2cf to your computer and use it in GitHub Desktop.
Save markus-k/42194d5aa6f856c7e21b0acbd23dc2cf to your computer and use it in GitHub Desktop.
prometheus text exporter for hdparm sleep state
#!/usr/bin/env python3
# usage: hdparm -C /dev/sd[a-d] | ./sleepstate.py
import sys
hdparm_output = sys.stdin.read()
lines = list(filter(lambda line: line, hdparm_output.splitlines()))
drives = [lines[i : i + 2] for i in range(0, len(lines), 2)]
states = list(
map(
lambda drive: (
drive[0].rstrip(":"),
drive[1].lstrip().removeprefix("drive state is:").strip(),
),
drives,
)
)
for disk, state in states:
is_sleeping = 1 if state == "standby" else 0
print(f'hdparm_sleep_state{{disk="{disk}"}} {is_sleeping}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment