]> arthur.barton.de Git - netdata.git/blob - python.d/sensors.chart.py
fixed sensors
[netdata.git] / python.d / sensors.chart.py
1 # -*- coding: utf-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
7
8 # default module values (can be overridden per job in `config`)
9 # update_every = 2
10
11 ORDER = ['temperature', 'fan', 'voltage', 'current', 'power', 'energy', 'humidity']
12
13 # This is a prototype of chart definition which is used to dynamically create self.definitions
14 CHARTS = {
15     'temperature': {
16         'options': [None, ' temperature', 'Celsius', 'temperature', 'sensors.temp', 'line'],
17         'lines': [
18             [None, None, 'absolute', 1, 1000]
19         ]},
20     'voltage': {
21         'options': [None, ' voltage', 'Volts', 'voltage', 'sensors.volt', 'line'],
22         'lines': [
23             [None, None, 'absolute', 1, 1000]
24         ]},
25     'current': {
26         'options': [None, ' current', 'Ampere', 'current', 'sensors.curr', 'line'],
27         'lines': [
28             [None, None, 'absolute', 1, 1000]
29         ]},
30     'power': {
31         'options': [None, ' power', 'Watt', 'power', 'sensors.power', 'line'],
32         'lines': [
33             [None, None, 'absolute', 1, 1000000]
34         ]},
35     'fan': {
36         'options': [None, ' fans speed', 'Rotations/min', 'fans', 'sensors.fans', 'line'],
37         'lines': [
38             [None, None, 'absolute', 1, 1000]
39         ]},
40     'energy': {
41         'options': [None, ' energy', 'Joule', 'energy', 'sensors.energy', 'areastack'],
42         'lines': [
43             [None, None, 'incremental', 1, 1000000]
44         ]},
45     'humidity': {
46         'options': [None, ' humidity', 'Percent', 'humidity', 'sensors.humidity', 'line'],
47         'lines': [
48             [None, None, 'absolute', 1, 1000]
49         ]}
50 }
51
52
53 class Service(SimpleService):
54     def __init__(self, configuration=None, name=None):
55         SimpleService.__init__(self, configuration=configuration, name=name)
56         self.order = []
57         self.definitions = {}
58
59     def _get_data(self):
60         data = {}
61         try:
62             for chip in sensors.iter_detected_chips():
63                 prefix = '_'.join(str(chip.path.decode()).split('/')[3:])
64                 lines = {}
65                 for feature in chip:
66                     data[prefix + "_" + str(feature.name.decode())] = feature.get_value() * 1000
67         except Exception as e:
68             self.error(e)
69             return None
70
71         if len(data) == 0:
72             return None
73         return data
74
75     def _create_definitions(self):
76         for type in ORDER:
77             for chip in sensors.iter_detected_chips():
78                 prefix = '_'.join(str(chip.path.decode()).split('/')[3:])
79                 name = ""
80                 lines = []
81                 for feature in chip:
82                     if feature.get_value() != 0:
83                         continue
84                     if sensors.TYPE_DICT[feature.type] == type:
85                         name = str(chip.prefix.decode()) + "_" + sensors.TYPE_DICT[feature.type]
86                         if name not in self.order:
87                             options = list(CHARTS[type]['options'])
88                             options[1] = str(chip.prefix) + options[1]
89                             self.definitions[name] = {'options': options}
90                             self.definitions[name]['lines'] = []
91                             self.order.append(name)
92                         line = list(CHARTS[type]['lines'][0])
93                         line[0] = prefix + "_" + str(feature.name.decode())
94                         line[1] = str(feature.label)
95                         self.definitions[name]['lines'].append(line)
96
97     def check(self):
98         try:
99             sensors.init()
100         except Exception as e:
101             self.error(e)
102             return False
103         try:
104             self._create_definitions()
105         except:
106             return False
107         return True