summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/sensors/sensors.chart.py
blob: f089e147acb7516f5d3f4189938d9d1da09226c2 (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
# -*- coding: utf-8 -*-
# Description: sensors netdata python.d plugin
# Author: Pawel Krupa (paulfantom)
# SPDX-License-Identifier: GPL-3.0-or-later

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')

    def get_data(self):
        data = dict()
        try:
            for chip in sensors.ChipIterator():
                prefix = sensors.chip_snprintf_name(chip)
                for feature in sensors.FeatureIterator(chip):
                    sfi = sensors.SubFeatureIterator(chip, feature)
                    val = None
                    for sf in sfi:
                        try:
                            val = sensors.get_value(chip, sf.number)
                            break
                        except sensors.SensorsError:
                            continue
                    if val is None:
                        continue
                    type_name = TYPE_MAP[feature.type]
                    if type_name in LIMITS:
                        limit = LIMITS[type_name]
                        if val < limit[0] or val > limit[1]:
                            continue
                    data[prefix + '_' + str(feature.name.decode())] = int(val * 1000)
        except sensors.SensorsError as error:
            self.error(error)
            return None

        return data or None

    def create_definitions(self):
        for sensor in ORDER:
            for chip in sensors.ChipIterator():
                chip_name = sensors.chip_snprintf_name(chip)
                if self.chips and not any([chip_name.startswith(ex) for ex in self.chips]):
                    continue
                for feature in sensors.FeatureIterator(chip):
                    sfi = sensors.SubFeatureIterator(chip, feature)
                    vals = list()
                    for sf in sfi:
                        try:
                            vals.append(sensors.get_value(chip, sf.number))
                        except sensors.SensorsError as error:
                            self.error('{0}: {1}'.format(sf.name, error))
                            continue
                    if not vals or (vals[0] == 0 and feature.type != 1):
                        continue
                    if TYPE_MAP[feature.type] == sensor:
                        # create chart
                        name = chip_name + '_' + TYPE_MAP[feature.type]
                        if name not in self.order:
                            self.order.append(name)
                            chart_def = list(CHARTS[sensor]['options'])
                            self.definitions[name] = {'options': chart_def}
                            self.definitions[name]['lines'] = []
                        line = list(CHARTS[sensor]['lines'][0])
                        line[0] = chip_name + '_' + str(feature.name.decode())
                        line[1] = sensors.get_label(chip, feature)
                        self.definitions[name]['lines'].append(line)

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

        self.create_definitions()

        return bool(self.get_data())