"sensors" module fixes
lgz committed
Oct 14, 2017 at 20:16 UTC
c962b613edbf702b52891d97e04d3c069b6a0379
1 file changed
+31
-43
python.d/sensors.chart.py
+31
-43
@@ -2,8 +2,8 @@
2
# Description: sensors netdata python.d plugin
3
# Author: Pawel Krupa (paulfantom)
4
5
-from base import SimpleService
6
-import lm_sensors as sensors
5
+from bases.FrameworkServices.SimpleService import SimpleService
6
+from third_party import lm_sensors as sensors
7
8
# default module values (can be overridden per job in `config`)
9
# update_every = 2
@@ -75,15 +75,13 @@ TYPE_MAP = {
75
class Service(SimpleService):
76
def __init__(self, configuration=None, name=None):
77
SimpleService.__init__(self, configuration=configuration, name=name)
78
- self.order = []
79
- self.definitions = {}
80
- self.celsius = ('Celsius', lambda x: x)
81
- self.fahrenheit = ('Fahrenheit', lambda x: x * 9 / 5 + 32) if self.configuration.get('fahrenheit') else False
82
- self.choice = (choice for choice in [self.fahrenheit, self.celsius] if choice)
83
- self.chips = []
78
+ self.order = list()
79
+ self.definitions = dict()
80
+ self.fahrenheit = self.configuration.get('fahrenheit')
81
+ self.chips = list()
82
85
- def _get_data(self):
86
- data = {}
83
+ def get_data(self):
84
+ data = dict()
85
try:
86
for chip in sensors.ChipIterator():
87
prefix = sensors.chip_snprintf_name(chip)
@@ -92,46 +90,44 @@ class Service(SimpleService):
90
for sf in sfi:
91
val = sensors.get_value(chip, sf.number)
92
break
95
- typeName = TYPE_MAP[feature.type]
96
- if typeName in LIMITS:
97
- limit = LIMITS[typeName];
93
+ type_name = TYPE_MAP[feature.type]
94
+ if type_name in LIMITS:
95
+ limit = LIMITS[type_name]
96
if val < limit[0] or val > limit[1]:
97
continue
98
if 'temp' in str(feature.name.decode()):
101
- data[prefix + "_" + str(feature.name.decode())] = int(self.calc(val) * 1000)
99
+ data[prefix + "_" + str(feature.name.decode())] = int(self.calc_temperature(val) * 1000)
100
else:
101
data[prefix + "_" + str(feature.name.decode())] = int(val * 1000)
104
- except Exception as e:
105
- self.error(e)
102
+ except Exception as error:
103
+ self.error(error)
104
return None
105
108
- if len(data) == 0:
109
- return None
110
- return data
106
+ return data or None
107
112
- def _create_definitions(self):
113
- for type in ORDER:
108
+ def create_definitions(self):
109
+ for sensor in ORDER:
110
for chip in sensors.ChipIterator():
111
chip_name = sensors.chip_snprintf_name(chip)
116
- if len(self.chips) != 0 and not any([chip_name.startswith(ex) for ex in self.chips]):
112
+ if self.chips and not any([chip_name.startswith(ex) for ex in self.chips]):
113
continue
114
for feature in sensors.FeatureIterator(chip):
115
sfi = sensors.SubFeatureIterator(chip, feature)
116
vals = [sensors.get_value(chip, sf.number) for sf in sfi]
117
if vals[0] == 0:
118
continue
123
- if TYPE_MAP[feature.type] == type:
119
+ if TYPE_MAP[feature.type] == sensor:
120
# create chart
121
name = chip_name + "_" + TYPE_MAP[feature.type]
122
if name not in self.order:
123
self.order.append(name)
128
- chart_def = list(CHARTS[type]['options'])
124
+ chart_def = list(CHARTS[sensor]['options'])
125
chart_def[1] = chip_name + chart_def[1]
130
- if chart_def[2] == 'Celsius':
131
- chart_def[2] = self.choice[0]
126
+ if chart_def[2] == 'Celsius' and self.fahrenheit:
127
+ chart_def[2] = 'Fahrenheit'
128
self.definitions[name] = {'options': chart_def}
129
self.definitions[name]['lines'] = []
134
- line = list(CHARTS[type]['lines'][0])
130
+ line = list(CHARTS[sensor]['lines'][0])
131
line[0] = chip_name + "_" + str(feature.name.decode())
132
line[1] = sensors.get_label(chip, feature)
133
self.definitions[name]['lines'].append(line)
@@ -139,23 +135,15 @@ class Service(SimpleService):
135
def check(self):
136
try:
137
sensors.init()
142
- except Exception as e:
143
- self.error(e)
138
+ except Exception as error:
139
+ self.error(error)
140
return False
145
-
146
- try:
147
- self.choice = next(self.choice)
148
- except StopIteration:
149
- # That can not happen but..
150
- self.choice = ('Celsius', lambda x: x)
151
- self.calc = self.choice[1]
152
- else:
153
- self.calc = self.choice[1]
141
155
- try:
156
- self._create_definitions()
157
- except Exception as e:
158
- self.error(e)
159
- return False
142
+ self.create_definitions()
143
144
return True
145
+
146
+ def calc_temperature(self, v):
147
+ if not self.fahrenheit:
148
+ return v
149
+ return v * 9 / 5 + 32