summaryrefslogtreecommitdiffstats
path: root/python.d/sensors.chart.py
blob: edfe125dbd94babefafc1570f566eab55bc3b088 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
# Description: sensors netdata python.d plugin
# Author: Pawel Krupa (paulfantom)

from base import SimpleService
import lm_sensors as sensors

# default module values (can be overridden per job in `config`)
# update_every = 2

ORDER = ['temperature', 'fan', 'voltage', 'current', 'power', 'energy', 'humidity']

# This is a prototype of chart definition which is used to dynamically create self.definitions
CHARTS = {
    'temperature': {
        'options': [None, ' temperature', 'Celsius', 'temperature', 'sensors.temp', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]},
    'voltage': {
        'options': [None, ' voltage', 'Volts', 'voltage', 'sensors.volt', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]},
    'current': {
        'options': [None, ' current', 'Ampere', 'current', 'sensors.curr', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]},
    'power': {
        'options': [None, ' power', 'Watt', 'power', 'sensors.power', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000000]
        ]},
    'fan': {
        'options': [None, ' fans speed', 'Rotations/min', 'fans', 'sensors.fans', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]},
    'energy': {
        'options': [None, ' energy', 'Joule', 'energy', 'sensors.energy', 'areastack'],
        'lines': [
            [None, None, 'incremental', 1, 1000000]
        ]},
    'humidity': {
        'options': [None, ' humidity', 'Percent', 'humidity', 'sensors.humidity', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]}
}


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
        except Exception as e:
            self.error(e)
            return None

        if len(data) == 0:
            return None
        return data

    def _create_definitions(self):
        for type in ORDER:
            for chip in sensors.iter_detected_chips():
                prefix = '_'.join(str(chip.path.decode()).split('/')[3:])
                name = ""
                lines = []
                pref = str(chip.prefix.decode())
                if len(self.chips) != 0 and not any([ex.startswith(pref) for ex in self.chips]):
                    continue
                for feature in chip:
                    if feature.get_value() == 0:
                        continue
                    if sensors.TYPE_DICT[feature.type] == type:
                        name = pref + "_" + sensors.TYPE_DICT[feature.type]
                        if name not in self.order:
                            options = list(CHARTS[type]['options'])
                            options[1] = pref + options[1]
                            self.definitions[name] = {'options': options}
                            self.definitions[name]['lines'] = []
                            self.order.append(name)
                        line = list(CHARTS[type]['lines'][0])
                        line[0] = prefix + "_" + str(feature.name.decode())
                        line[1] = str(feature.label)
                        self.definitions[name]['lines'].append(line)

    def check(self):
        try:
            self.chips = list(self.configuration['chips'])
        except (KeyError, TypeError):
            self.error("No path to log specified. Using all chips.")
        try:
            global ORDER
            ORDER = list(self.configuration['types'])
        except (KeyError, TypeError):
            self.error("No path to log specified. Using all sensor types.")
        try:
            sensors.init()
        except Exception as e:
            self.error(e)
            return False
        try:
            self._create_definitions()
        except:
            return False

        if len(self.definitions) == 0:
            self.error("No sensors found")
            return False

        return True