]> arthur.barton.de Git - netdata.git/blobdiff - python.d/sensors.chart.py
ab-debian 0.20170327.01-0ab1, upstream v1.6.0-42-gaa6b96fc
[netdata.git] / python.d / sensors.chart.py
index 1288931add6a8d3c56e5663526c54d89c00b3944..e83aacfd81c9db1a87ce1f6fb831a6f355a2bf9d 100644 (file)
@@ -13,17 +13,17 @@ ORDER = ['temperature', 'fan', 'voltage', 'current', 'power', 'energy', 'humidit
 # This is a prototype of chart definition which is used to dynamically create self.definitions
 CHARTS = {
     'temperature': {
-        'options': [None, ' temperature', 'Celsius', 'temperature', 'sensors.temp', 'line'],
+        'options': [None, ' temperature', 'Celsius', 'temperature', 'sensors.temperature', 'line'],
         'lines': [
             [None, None, 'absolute', 1, 1000]
         ]},
     'voltage': {
-        'options': [None, ' voltage', 'Volts', 'voltage', 'sensors.volt', 'line'],
+        'options': [None, ' voltage', 'Volts', 'voltage', 'sensors.voltage', 'line'],
         'lines': [
             [None, None, 'absolute', 1, 1000]
         ]},
     'current': {
-        'options': [None, ' current', 'Ampere', 'current', 'sensors.curr', 'line'],
+        'options': [None, ' current', 'Ampere', 'current', 'sensors.current', 'line'],
         'lines': [
             [None, None, 'absolute', 1, 1000]
         ]},
@@ -33,7 +33,7 @@ CHARTS = {
             [None, None, 'absolute', 1, 1000000]
         ]},
     'fan': {
-        'options': [None, ' fans speed', 'Rotations/min', 'fans', 'sensors.fans', 'line'],
+        'options': [None, ' fans speed', 'Rotations/min', 'fans', 'sensors.fan', 'line'],
         'lines': [
             [None, None, 'absolute', 1, 1000]
         ]},
@@ -49,22 +49,58 @@ CHARTS = {
         ]}
 }
 
+LIMITS = {
+    'temperature': [-127, 1000],
+    'voltage': [-127, 127],
+    'current': [-127, 127],
+    'fan': [0, 65535]
+}
+
+TYPE_MAP = {
+    0: 'voltage',
+    1: 'fan',
+    2: 'temperature',
+    3: 'power',
+    4: 'energy',
+    5: 'current',
+    6: 'humidity',
+    7: 'max_main',
+    16: 'vid',
+    17: 'intrusion',
+    18: 'max_other',
+    24: 'beep_enable'
+}
+
 
 class Service(SimpleService):
     def __init__(self, configuration=None, name=None):
         SimpleService.__init__(self, configuration=configuration, name=name)
         self.order = []
         self.definitions = {}
+        self.celsius = ('Celsius', lambda x: x)
+        self.fahrenheit = ('Fahrenheit', lambda x: x * 9 / 5 + 32)  if self.configuration.get('fahrenheit') else False
+        self.choice = (choice for choice in [self.fahrenheit, self.celsius] if choice)
         self.chips = []
 
     def _get_data(self):
         data = {}
         try:
-            for chip in sensors.iter_detected_chips():
-                prefix = '_'.join(str(chip.path.decode()).split('/')[3:])
-                lines = {}
-                for feature in chip:
-                    data[prefix + "_" + str(feature.name.decode())] = feature.get_value() * 1000
+            for chip in sensors.ChipIterator():
+                prefix = sensors.chip_snprintf_name(chip)
+                for feature in sensors.FeatureIterator(chip):
+                    sfi = sensors.SubFeatureIterator(chip, feature)
+                    for sf in sfi:
+                        val = sensors.get_value(chip, sf.number)
+                        break
+                    typeName = TYPE_MAP[feature.type]
+                    if typeName in LIMITS:
+                        limit = LIMITS[typeName];
+                        if val < limit[0] or val > limit[1]:
+                            continue
+                    if 'temp' in str(feature.name.decode()):
+                        data[prefix + "_" + str(feature.name.decode())] = int(self.calc(val) * 1000)
+                    else:
+                        data[prefix + "_" + str(feature.name.decode())] = int(val * 1000)
         except Exception as e:
             self.error(e)
             return None
@@ -75,56 +111,51 @@ class Service(SimpleService):
 
     def _create_definitions(self):
         for type in ORDER:
-            for chip in sensors.iter_detected_chips():
-                prefix = '_'.join(str(chip.path.decode()).split('/')[3:])
-                name = ""
-                lines = []
-                pref = str(chip.prefix.decode())
-                if len(self.chips) != 0 and not any([ex.startswith(pref) for ex in self.chips]):
+            for chip in sensors.ChipIterator():
+                chip_name = sensors.chip_snprintf_name(chip)
+                if len(self.chips) != 0 and not any([chip_name.startswith(ex) for ex in self.chips]):
                     continue
-                pref = pref + '_' + str(chip.addr)
-                for feature in chip:
-                    try:
-                        float(feature.get_value())
-                    except ValueError:
+                for feature in sensors.FeatureIterator(chip):
+                    sfi = sensors.SubFeatureIterator(chip, feature)
+                    vals = [sensors.get_value(chip, sf.number) for sf in sfi]
+                    if vals[0] == 0:
                         continue
-                    if feature.get_value() < 0:
-                        continue
-                    if sensors.TYPE_DICT[feature.type] == type:
-                        name = pref + "_" + sensors.TYPE_DICT[feature.type]
+                    if TYPE_MAP[feature.type] == type:
+                        # create chart
+                        name = chip_name + "_" + TYPE_MAP[feature.type]
                         if name not in self.order:
-                            options = list(CHARTS[type]['options'])
-                            options[1] = pref + options[1]
-                            self.definitions[name] = {'options': options}
-                            self.definitions[name]['lines'] = []
                             self.order.append(name)
+                            chart_def = list(CHARTS[type]['options'])
+                            chart_def[1] = chip_name + chart_def[1]
+                            if chart_def[2] == 'Celsius':
+                                chart_def[2] = self.choice[0]
+                            self.definitions[name] = {'options': chart_def}
+                            self.definitions[name]['lines'] = []
                         line = list(CHARTS[type]['lines'][0])
-                        line[0] = prefix + "_" + str(feature.name.decode())
-                        line[1] = str(feature.label)
+                        line[0] = chip_name + "_" + str(feature.name.decode())
+                        line[1] = sensors.get_label(chip, feature)
                         self.definitions[name]['lines'].append(line)
 
     def check(self):
-        try:
-            self.chips = list(self.configuration['chips'])
-        except (KeyError, TypeError):
-            self.error("No path to log specified. Using all chips.")
-        try:
-            global ORDER
-            ORDER = list(self.configuration['types'])
-        except (KeyError, TypeError):
-            self.error("No path to log specified. Using all sensor types.")
         try:
             sensors.init()
         except Exception as e:
             self.error(e)
             return False
+        
         try:
-            self._create_definitions()
-        except:
-            return False
+            self.choice = next(self.choice)
+        except StopIteration:
+            # That can not happen but..
+            self.choice = ('Celsius', lambda x: x)
+            self.calc = self.choice[1]
+        else:
+            self.calc = self.choice[1]
 
-        if len(self.definitions) == 0:
-            self.error("No sensors found")
+        try:
+            self._create_definitions()
+        except Exception as e:
+            self.error(e)
             return False
 
         return True