-
-
Save smereczynski/cd5691802e59ef7a0fd258bf340dab14 to your computer and use it in GitHub Desktop.
parse "sensors -u" output with Python
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 | |
# -*- coding: utf-8 -*- | |
import subprocess | |
class Metric(object): | |
def __init__(self, adapter_id, sensor_id, sensor_key, value, label): | |
self._value = self.parse_value(value) | |
self._adapter_id = adapter_id | |
self._sensor_id = sensor_id | |
self._sensor_key = sensor_key | |
self._label = label | |
@classmethod | |
def parse_value(cls, value): | |
if hasattr(cls, "parse"): | |
parse = getattr(cls, "parse") | |
return parse(value) | |
else: | |
return value | |
def to_dictionary(self): | |
# d = {'name': '<your_metric_name>', | |
# 'call_back': <metric_handler_function>, | |
# 'time_max': int(<your_time_max>), | |
# 'value_type': '<string | uint | float | double>', | |
# 'units': '<your_units>', | |
# 'slope': '<zero | positive | negative | both>', | |
# 'format': '<your_format>', | |
# 'description': '<your_description>'} | |
return { | |
"name": "%s_%s_%s" % (self._adapter_id, self._sensor_id, self._sensor_key), | |
"call_back": get_metric_value, | |
"time_max": 600, | |
"value_type": self.parse.__name__, | |
"units": self.unit, | |
"slope": "both", | |
"format": "%u", | |
"description": "lm-sensors metric", | |
##"groups": "lm" | |
} | |
def __repr__(self): | |
return "%s, %s, %s: %s %s [%s]" % ( | |
self._adapter_id, | |
self._sensor_id, | |
self._sensor_key, | |
self._value, | |
getattr(self, "unit", "?"), | |
self._label) | |
class TemperatureMetric(Metric): | |
parse = float | |
unit = "°C" | |
class FanMetric(Metric): | |
parse = float | |
unit = "RPM" | |
class VoltageMetric(Metric): | |
parse = float | |
unit = "V" | |
def metric_init(params): | |
metrics = get_metrics_list() | |
return [m.to_dictionary() for m in metrics] | |
def metric_cleanup(): | |
pass | |
def get_metric_value(name): | |
adapter_id, sensor_id, sensor_key = name.split("_") | |
metrics = get_metrics_list() | |
for metric in metrics: | |
if metric._adapter_id == adapter_id and metric._sensor_id == sensor_id and metric._sensor_key == sensor_key: | |
return metric.value | |
return None | |
def get_metrics_list(): | |
output = subprocess.check_output(["/usr/bin/sensors", "-u"]) | |
output = output.strip() | |
sections = output.split("\n\n") | |
metrics = [] | |
for section in sections: | |
fields = section.split("\n") | |
adapter_id = fields[0] | |
label = None | |
for field in fields[2:]: | |
if field.startswith(" "): | |
field = field.replace(" ", "") | |
field_key, field_value = field.split(": ") | |
if "_" in field_key: | |
sensor_id, sensor_key = field_key.split("_", 1) | |
if sensor_key == "input": | |
if sensor_id.startswith("temp"): | |
metrics.append( | |
TemperatureMetric(adapter_id, sensor_id, sensor_key, field_value, label=label)) | |
elif sensor_id.startswith("in"): | |
metrics.append( | |
VoltageMetric(adapter_id, sensor_id, sensor_key, field_value, label=label)) | |
elif sensor_id.startswith("fan"): | |
metrics.append( | |
FanMetric(adapter_id, sensor_id, sensor_key, field_value, label=label)) | |
else: | |
label = field[:-1] # strip off trailing ":" character | |
return metrics | |
if __name__ == '__main__': | |
metrics = get_metrics_list() | |
for metric in metrics: | |
print metric |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment