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