]> arthur.barton.de Git - netdata.git/blob - python.d/sensors.chart.py
I added test to establish if sensor value is a number.
[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         self.chips = []
59
60     def _get_data(self):
61         data = {}
62         try:
63             for chip in sensors.iter_detected_chips():
64                 prefix = '_'.join(str(chip.path.decode()).split('/')[3:])
65                 lines = {}
66                 for feature in chip:
67                     data[prefix + "_" + str(feature.name.decode())] = feature.get_value() * 1000
68         except Exception as e:
69             self.error(e)
70             return None
71
72         if len(data) == 0:
73             return None
74         return data
75
76     def _create_definitions(self):
77         for type in ORDER:
78             for chip in sensors.iter_detected_chips():
79                 prefix = '_'.join(str(chip.path.decode()).split('/')[3:])
80                 name = ""
81                 lines = []
82                 pref = str(chip.prefix.decode())
83                 if len(self.chips) != 0 and not any([ex.startswith(pref) for ex in self.chips]):
84                     continue
85                 for feature in chip:
86                     try:
87                         float(feature.get_value())
88                     except ValueError:
89                         continue
90                     if feature.get_value() == 0:
91                         continue
92                     if sensors.TYPE_DICT[feature.type] == type:
93                         name = pref + "_" + sensors.TYPE_DICT[feature.type]
94                         if name not in self.order:
95                             options = list(CHARTS[type]['options'])
96                             options[1] = pref + options[1]
97                             self.definitions[name] = {'options': options}
98                             self.definitions[name]['lines'] = []
99                             self.order.append(name)
100                         line = list(CHARTS[type]['lines'][0])
101                         line[0] = prefix + "_" + str(feature.name.decode())
102                         line[1] = str(feature.label)
103                         self.definitions[name]['lines'].append(line)
104
105     def check(self):
106         try:
107             self.chips = list(self.configuration['chips'])
108         except (KeyError, TypeError):
109             self.error("No path to log specified. Using all chips.")
110         try:
111             global ORDER
112             ORDER = list(self.configuration['types'])
113         except (KeyError, TypeError):
114             self.error("No path to log specified. Using all sensor types.")
115         try:
116             sensors.init()
117         except Exception as e:
118             self.error(e)
119             return False
120         try:
121             self._create_definitions()
122         except:
123             return False
124
125         if len(self.definitions) == 0:
126             self.error("No sensors found")
127             return False
128
129         return True