]> arthur.barton.de Git - netdata.git/blob - python.d/sensors.chart.py
Merge remote-tracking branch 'firehol/master'
[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.temperature', 'line'],
17         'lines': [
18             [None, None, 'absolute', 1, 1000]
19         ]},
20     'voltage': {
21         'options': [None, ' voltage', 'Volts', 'voltage', 'sensors.voltage', 'line'],
22         'lines': [
23             [None, None, 'absolute', 1, 1000]
24         ]},
25     'current': {
26         'options': [None, ' current', 'Ampere', 'current', 'sensors.current', '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.fan', '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 TYPE_MAP = {
53     0: 'voltage',
54     1: 'fan',
55     2: 'temperature',
56     3: 'power',
57     4: 'energy',
58     5: 'current',
59     6: 'humidity',
60     7: 'max_main',
61     16: 'vid',
62     17: 'intrusion',
63     18: 'max_other',
64     24: 'beep_enable'
65 }
66
67
68 class Service(SimpleService):
69     def __init__(self, configuration=None, name=None):
70         SimpleService.__init__(self, configuration=configuration, name=name)
71         self.order = []
72         self.definitions = {}
73         self.chips = []
74
75     def _get_data(self):
76         data = {}
77         try:
78             for chip in sensors.ChipIterator():
79                 prefix = sensors.chip_snprintf_name(chip)
80                 for feature in sensors.FeatureIterator(chip):
81                     sfi = sensors.SubFeatureIterator(chip, feature)
82                     for sf in sfi:
83                         val = sensors.get_value(chip, sf.number)
84                         break
85                     data[prefix + "_" + str(feature.name.decode())] = int(val * 1000)
86         except Exception as e:
87             self.error(e)
88             return None
89
90         if len(data) == 0:
91             return None
92         return data
93
94     def _create_definitions(self):
95         prev_chip = ""
96         for type in ORDER:
97             for chip in sensors.ChipIterator():
98                 chip_name = sensors.chip_snprintf_name(chip)
99                 if len(self.chips) != 0 and not any([chip_name.startswith(ex) for ex in self.chips]):
100                     continue
101                 for feature in sensors.FeatureIterator(chip):
102                     sfi = sensors.SubFeatureIterator(chip, feature)
103                     vals = [sensors.get_value(chip, sf.number) for sf in sfi]
104                     if vals[0] == 0:
105                         continue
106                     if TYPE_MAP[feature.type] == type:
107                         # create chart
108                         if chip_name != prev_chip:
109                             name = chip_name + "_" + TYPE_MAP[feature.type]
110                             if name not in self.order:
111                                 self.order.append(name)
112                                 chart_def = list(CHARTS[type]['options'])
113                                 chart_def[1] = chip_name + chart_def[1]
114                                 self.definitions[name] = {'options': chart_def}
115                                 self.definitions[name]['lines'] = []
116                         line = list(CHARTS[type]['lines'][0])
117                         line[0] = chip_name + "_" + str(feature.name.decode())
118                         line[1] = sensors.get_label(chip, feature)
119                         self.definitions[name]['lines'].append(line)
120                 prev_chip = chip_name
121
122     def check(self):
123         try:
124             sensors.init()
125         except Exception as e:
126             self.error(e)
127             return False
128
129         try:
130             self._create_definitions()
131         except Exception as e:
132             self.error(e)
133             return False
134         return True