@cryptotaxi247 / netdata-1 / commits / 4168ddcb7

remove python.d lm_sensors.py (#18356)

Ilya Mashchenko committed Aug 17, 2024 at 19:21 UTC 4168ddcb78d36c89bbdd69bb4f224172abaedd40
1 file changed -327
src/collectors/python.d.plugin/python_modules/third_party/lm_sensors.py deleted
-327
@@ -1,327 +0,0 @@
1 -# SPDX-License-Identifier: LGPL-2.1
2 -"""
3 -@package sensors.py
4 -Python Bindings for libsensors3
5 -
6 -use the documentation of libsensors for the low level API.
7 -see example.py for high level API usage.
8 -
9 -@author: Pavel Rojtberg (http://www.rojtberg.net)
10 -@see: https://github.com/paroj/sensors.py
11 -@copyright: LGPLv2 (same as libsensors) <http://opensource.org/licenses/LGPL-2.1>
12 -"""
13 -
14 -from ctypes import *
15 -import ctypes.util
16 -
17 -_libc = cdll.LoadLibrary(ctypes.util.find_library("c"))
18 -# see https://github.com/paroj/sensors.py/issues/1
19 -_libc.free.argtypes = [c_void_p]
20 -
21 -_hdl = cdll.LoadLibrary(ctypes.util.find_library("sensors"))
22 -
23 -version = c_char_p.in_dll(_hdl, "libsensors_version").value.decode("ascii")
24 -
25 -
26 -class SensorsError(Exception):
27 - pass
28 -
29 -
30 -class ErrorWildcards(SensorsError):
31 - pass
32 -
33 -
34 -class ErrorNoEntry(SensorsError):
35 - pass
36 -
37 -
38 -class ErrorAccessRead(SensorsError, OSError):
39 - pass
40 -
41 -
42 -class ErrorKernel(SensorsError, OSError):
43 - pass
44 -
45 -
46 -class ErrorDivZero(SensorsError, ZeroDivisionError):
47 - pass
48 -
49 -
50 -class ErrorChipName(SensorsError):
51 - pass
52 -
53 -
54 -class ErrorBusName(SensorsError):
55 - pass
56 -
57 -
58 -class ErrorParse(SensorsError):
59 - pass
60 -
61 -
62 -class ErrorAccessWrite(SensorsError, OSError):
63 - pass
64 -
65 -
66 -class ErrorIO(SensorsError, IOError):
67 - pass
68 -
69 -
70 -class ErrorRecursion(SensorsError):
71 - pass
72 -
73 -
74 -_ERR_MAP = {
75 - 1: ErrorWildcards,
76 - 2: ErrorNoEntry,
77 - 3: ErrorAccessRead,
78 - 4: ErrorKernel,
79 - 5: ErrorDivZero,
80 - 6: ErrorChipName,
81 - 7: ErrorBusName,
82 - 8: ErrorParse,
83 - 9: ErrorAccessWrite,
84 - 10: ErrorIO,
85 - 11: ErrorRecursion
86 -}
87 -
88 -
89 -def raise_sensor_error(errno, message=''):
90 - raise _ERR_MAP[abs(errno)](message)
91 -
92 -
93 -class bus_id(Structure):
94 - _fields_ = [("type", c_short),
95 - ("nr", c_short)]
96 -
97 -
98 -class chip_name(Structure):
99 - _fields_ = [("prefix", c_char_p),
100 - ("bus", bus_id),
101 - ("addr", c_int),
102 - ("path", c_char_p)]
103 -
104 -
105 -class feature(Structure):
106 - _fields_ = [("name", c_char_p),
107 - ("number", c_int),
108 - ("type", c_int)]
109 -
110 - # sensors_feature_type
111 - IN = 0x00
112 - FAN = 0x01
113 - TEMP = 0x02
114 - POWER = 0x03
115 - ENERGY = 0x04
116 - CURR = 0x05
117 - HUMIDITY = 0x06
118 - MAX_MAIN = 0x7
119 - VID = 0x10
120 - INTRUSION = 0x11
121 - MAX_OTHER = 0x12
122 - BEEP_ENABLE = 0x18
123 -
124 -
125 -class subfeature(Structure):
126 - _fields_ = [("name", c_char_p),
127 - ("number", c_int),
128 - ("type", c_int),
129 - ("mapping", c_int),
130 - ("flags", c_uint)]
131 -
132 -
133 -_hdl.sensors_get_detected_chips.restype = POINTER(chip_name)
134 -_hdl.sensors_get_features.restype = POINTER(feature)
135 -_hdl.sensors_get_all_subfeatures.restype = POINTER(subfeature)
136 -_hdl.sensors_get_label.restype = c_void_p # return pointer instead of str so we can free it
137 -_hdl.sensors_get_adapter_name.restype = c_char_p # docs do not say whether to free this or not
138 -_hdl.sensors_strerror.restype = c_char_p
139 -
140 -### RAW API ###
141 -MODE_R = 1
142 -MODE_W = 2
143 -COMPUTE_MAPPING = 4
144 -
145 -
146 -def init(cfg_file=None):
147 - file = _libc.fopen(cfg_file.encode("utf-8"), "r") if cfg_file is not None else None
148 -
149 - result = _hdl.sensors_init(file)
150 - if result != 0:
151 - raise_sensor_error(result, "sensors_init failed")
152 -
153 - if file is not None:
154 - _libc.fclose(file)
155 -
156 -
157 -def cleanup():
158 - _hdl.sensors_cleanup()
159 -
160 -
161 -def parse_chip_name(orig_name):
162 - ret = chip_name()
163 - err = _hdl.sensors_parse_chip_name(orig_name.encode("utf-8"), byref(ret))
164 -
165 - if err < 0:
166 - raise_sensor_error(err, strerror(err))
167 -
168 - return ret
169 -
170 -
171 -def strerror(errnum):
172 - return _hdl.sensors_strerror(errnum).decode("utf-8")
173 -
174 -
175 -def free_chip_name(chip):
176 - _hdl.sensors_free_chip_name(byref(chip))
177 -
178 -
179 -def get_detected_chips(match, nr):
180 - """
181 - @return: (chip, next nr to query)
182 - """
183 - _nr = c_int(nr)
184 -
185 - if match is not None:
186 - match = byref(match)
187 -
188 - chip = _hdl.sensors_get_detected_chips(match, byref(_nr))
189 - chip = chip.contents if bool(chip) else None
190 - return chip, _nr.value
191 -
192 -
193 -def chip_snprintf_name(chip, buffer_size=200):
194 - """
195 - @param buffer_size defaults to the size used in the sensors utility
196 - """
197 - ret = create_string_buffer(buffer_size)
198 - err = _hdl.sensors_snprintf_chip_name(ret, buffer_size, byref(chip))
199 -
200 - if err < 0:
201 - raise_sensor_error(err, strerror(err))
202 -
203 - return ret.value.decode("utf-8")
204 -
205 -
206 -def do_chip_sets(chip):
207 - """
208 - @attention this function was not tested
209 - """
210 - err = _hdl.sensors_do_chip_sets(byref(chip))
211 - if err < 0:
212 - raise_sensor_error(err, strerror(err))
213 -
214 -
215 -def get_adapter_name(bus):
216 - return _hdl.sensors_get_adapter_name(byref(bus)).decode("utf-8")
217 -
218 -
219 -def get_features(chip, nr):
220 - """
221 - @return: (feature, next nr to query)
222 - """
223 - _nr = c_int(nr)
224 - feature = _hdl.sensors_get_features(byref(chip), byref(_nr))
225 - feature = feature.contents if bool(feature) else None
226 - return feature, _nr.value
227 -
228 -
229 -def get_label(chip, feature):
230 - ptr = _hdl.sensors_get_label(byref(chip), byref(feature))
231 - val = cast(ptr, c_char_p).value.decode("utf-8")
232 - _libc.free(ptr)
233 - return val
234 -
235 -
236 -def get_all_subfeatures(chip, feature, nr):
237 - """
238 - @return: (subfeature, next nr to query)
239 - """
240 - _nr = c_int(nr)
241 - subfeature = _hdl.sensors_get_all_subfeatures(byref(chip), byref(feature), byref(_nr))
242 - subfeature = subfeature.contents if bool(subfeature) else None
243 - return subfeature, _nr.value
244 -
245 -
246 -def get_value(chip, subfeature_nr):
247 - val = c_double()
248 - err = _hdl.sensors_get_value(byref(chip), subfeature_nr, byref(val))
249 - if err < 0:
250 - raise_sensor_error(err, strerror(err))
251 - return val.value
252 -
253 -
254 -def set_value(chip, subfeature_nr, value):
255 - """
256 - @attention this function was not tested
257 - """
258 - val = c_double(value)
259 - err = _hdl.sensors_set_value(byref(chip), subfeature_nr, byref(val))
260 - if err < 0:
261 - raise_sensor_error(err, strerror(err))
262 -
263 -
264 -### Convenience API ###
265 -class ChipIterator:
266 - def __init__(self, match=None):
267 - self.match = parse_chip_name(match) if match is not None else None
268 - self.nr = 0
269 -
270 - def __iter__(self):
271 - return self
272 -
273 - def __next__(self):
274 - chip, self.nr = get_detected_chips(self.match, self.nr)
275 -
276 - if chip is None:
277 - raise StopIteration
278 -
279 - return chip
280 -
281 - def __del__(self):
282 - if self.match is not None:
283 - free_chip_name(self.match)
284 -
285 - def next(self): # python2 compability
286 - return self.__next__()
287 -
288 -
289 -class FeatureIterator:
290 - def __init__(self, chip):
291 - self.chip = chip
292 - self.nr = 0
293 -
294 - def __iter__(self):
295 - return self
296 -
297 - def __next__(self):
298 - feature, self.nr = get_features(self.chip, self.nr)
299 -
300 - if feature is None:
301 - raise StopIteration
302 -
303 - return feature
304 -
305 - def next(self): # python2 compability
306 - return self.__next__()
307 -
308 -
309 -class SubFeatureIterator:
310 - def __init__(self, chip, feature):
311 - self.chip = chip
312 - self.feature = feature
313 - self.nr = 0
314 -
315 - def __iter__(self):
316 - return self
317 -
318 - def __next__(self):
319 - subfeature, self.nr = get_all_subfeatures(self.chip, self.feature, self.nr)
320 -
321 - if subfeature is None:
322 - raise StopIteration
323 -
324 - return subfeature
325 -
326 - def next(self): # python2 compability
327 - return self.__next__()