summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/sensors/sensors.chart.py
blob: 701bf641472cf097e9ec609de8ae2f49fc453dee (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
# Description: sensors netdata python.d plugin
# Author: Pawel Krupa (paulfantom)
# SPDX-License-Identifier: GPL-3.0-or-later

from collections import defaultdict

from bases.FrameworkServices.SimpleService import SimpleService
from third_party import lm_sensors as sensors

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.temperature', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]
    },
    'voltage': {
        'options': [None, 'Voltage', 'Volts', 'voltage', 'sensors.voltage', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]
    },
    'current': {
        'options': [None, 'Current', 'Ampere', 'current', 'sensors.current', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]
    },
    'power': {
        'options': [None, 'Power', 'Watt', 'power', 'sensors.power', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]
    },
    'fan': {
        'options': [None, 'Fans speed', 'Rotations/min', 'fans', 'sensors.fan', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]
    },
    'energy': {
        'options': [None, 'Energy', 'Joule', 'energy', 'sensors.energy', 'line'],
        'lines': [
            [None, None, 'incremental', 1, 1000]
        ]
    },
    'humidity': {
        'options': [None, 'Humidity', 'Percent', 'humidity', 'sensors.humidity', 'line'],
        'lines': [
            [None, None, 'absolute', 1, 1000]
        ]
    }
}

LIMITS = {
    'temperature': [-127, 1000],
    'voltage': [-127, 127],
    'current': [-127, 127],
    'fan': [0, 65535]
}

TYPE_MAP = {
    0: 'voltage',
    1: 'fan',
    2: 'temperature',
    3: 'power',
    4: 'energy',
    5: 'current',
    6: 'humidity',
    # 7: 'max_main',
    # 16: 'vid',
    # 17: 'intrusion',
    # 18: 'max_other',
    # 24: 'beep_enable'
}


class Service(SimpleService):
    def __init__(self, configuration=None, name=None):
        SimpleService.__init__(self, configuration=configuration, name=name)
        self.order = list()
        self.definitions = dict()
        self.chips = configuration.get('chips')
        self.priority = 60000

    def get_data(self):
        seen, data = dict(), dict()
        try:
            for chip in sensors.ChipIterator():
                chip_name = sensors.chip_snprintf_name(chip)
                seen[chip_name] = defaultdict(list)

                for feat in sensors.FeatureIterator(chip):
                    if feat.type not in TYPE_MAP:
                        continue

                    feat_type = TYPE_MAP[feat.type]
                    feat_name = str(feat.name.decode())
                    feat_label = sensors.get_label(chip, feat)
                    feat_limits = LIMITS.get(feat_type)
                    sub_feat = next(sensors.SubFeatureIterator(chip, feat))  # current value

                    if not sub_feat:
                        continue

                    try:
                        v = sensors.get_value(chip, sub_feat.number)
                    except sensors.SensorsError:
                        continue

                    if v is None:
                        continue

                    seen[chip_name][feat_type].append((feat_name, feat_label))

                    if feat_limits and (v < feat_limits[0] or v > feat_limits[1]):
                        continue

                    data[chip_name + '_' + feat_name] = int(v * 1000)

        except sensors.SensorsError as error:
            self.error(error)
            return None

        self.update_sensors_charts(seen)

        return data or None

    def update_sensors_charts(self, seen):
        for chip_name, feat in seen.items():
            if self.chips and not any([chip_name.startswith(ex) for ex in self.chips]):
                continue

            for feat_type, sub_feat in feat.items():
                if feat_type not in ORDER or feat_type not in CHARTS:
                    continue

                chart_id = '{}_{}'.format(chip_name, feat_type)
                if chart_id in self.charts:
                    continue

                params = [chart_id] + list(CHARTS[feat_type]['options'])
                new_chart = self.charts.add_chart(params)
                new_chart.params['priority'] = self.get_chart_priority(feat_type)

                for name, label in sub_feat:
                    lines = list(CHARTS[feat_type]['lines'][0])
                    lines[0] = chip_name + '_' + name
                    lines[1] = label
                    new_chart.add_dimension(lines)

    def check(self):
        try:
            sensors.init()
        except sensors.SensorsError as error:
            self.error(error)
            return False

        self.priority = self.charts.priority

        return bool(self.get_data() and self.charts)

    def get_chart_priority(self, feat_type):
        for i, v in enumerate(ORDER):
            if v == feat_type:
                return self.priority + i
        return self.priority