]> arthur.barton.de Git - netdata.git/blobdiff - python.d/sensors.chart.py
Update sensors.chart.py
[netdata.git] / python.d / sensors.chart.py
index 38c45657155f226d3c7a15930cf903cb08896bb5..8f7b903c3775ea5e041c3c00ba0e3067cdc852db 100644 (file)
@@ -49,21 +49,40 @@ CHARTS = {
         ]}
 }
 
+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.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
+                    data[prefix + "_" + str(feature.name.decode())] = int(val * 1000)
         except Exception as e:
             self.error(e)
             return None
@@ -73,26 +92,32 @@ class Service(SimpleService):
         return data
 
     def _create_definitions(self):
+        prev_chip = ""
         for type in ORDER:
-            for chip in sensors.iter_detected_chips():
-                prefix = '_'.join(str(chip.path.decode()).split('/')[3:])
-                name = ""
-                lines = []
-                for feature in chip:
-                    if feature.get_value() != 0:
+            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
+                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 sensors.TYPE_DICT[feature.type] == type:
-                        name = str(chip.prefix.decode()) + "_" + sensors.TYPE_DICT[feature.type]
-                        if name not in self.order:
-                            options = list(CHARTS[type]['options'])
-                            options[1] = str(chip.prefix) + options[1]
-                            self.definitions[name] = {'options': options}
-                            self.definitions[name]['lines'] = []
-                            self.order.append(name)
+                    if TYPE_MAP[feature.type] == type:
+                        # create chart
+                        if chip_name != prev_chip:
+                            name = chip_name + "_" + TYPE_MAP[feature.type]
+                            if name not in self.order:
+                                self.order.append(name)
+                                chart_def = list(CHARTS[type]['options'])
+                                chart_def[1] = chip_name + chart_def[1]
+                                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)
+                prev_chip = chip_name
 
     def check(self):
         try:
@@ -100,8 +125,10 @@ class Service(SimpleService):
         except Exception as e:
             self.error(e)
             return False
+
         try:
             self._create_definitions()
-        except:
+        except Exception as e:
+            self.error(e)
             return False
-        return True
\ No newline at end of file
+        return True