sensors.chart.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # -*- coding: utf-8 -*-
  2. # Description: sensors netdata python.d plugin
  3. # Author: Pawel Krupa (paulfantom)
  4. from bases.FrameworkServices.SimpleService import SimpleService
  5. from third_party import lm_sensors as sensors
  6. # default module values (can be overridden per job in `config`)
  7. # update_every = 2
  8. ORDER = ['temperature', 'fan', 'voltage', 'current', 'power', 'energy', 'humidity']
  9. # This is a prototype of chart definition which is used to dynamically create self.definitions
  10. CHARTS = {
  11. 'temperature': {
  12. 'options': [None, ' temperature', 'Celsius', 'temperature', 'sensors.temperature', 'line'],
  13. 'lines': [
  14. [None, None, 'absolute', 1, 1000]
  15. ]},
  16. 'voltage': {
  17. 'options': [None, ' voltage', 'Volts', 'voltage', 'sensors.voltage', 'line'],
  18. 'lines': [
  19. [None, None, 'absolute', 1, 1000]
  20. ]},
  21. 'current': {
  22. 'options': [None, ' current', 'Ampere', 'current', 'sensors.current', 'line'],
  23. 'lines': [
  24. [None, None, 'absolute', 1, 1000]
  25. ]},
  26. 'power': {
  27. 'options': [None, ' power', 'Watt', 'power', 'sensors.power', 'line'],
  28. 'lines': [
  29. [None, None, 'absolute', 1, 1000000]
  30. ]},
  31. 'fan': {
  32. 'options': [None, ' fans speed', 'Rotations/min', 'fans', 'sensors.fan', 'line'],
  33. 'lines': [
  34. [None, None, 'absolute', 1, 1000]
  35. ]},
  36. 'energy': {
  37. 'options': [None, ' energy', 'Joule', 'energy', 'sensors.energy', 'areastack'],
  38. 'lines': [
  39. [None, None, 'incremental', 1, 1000000]
  40. ]},
  41. 'humidity': {
  42. 'options': [None, ' humidity', 'Percent', 'humidity', 'sensors.humidity', 'line'],
  43. 'lines': [
  44. [None, None, 'absolute', 1, 1000]
  45. ]}
  46. }
  47. LIMITS = {
  48. 'temperature': [-127, 1000],
  49. 'voltage': [-127, 127],
  50. 'current': [-127, 127],
  51. 'fan': [0, 65535]
  52. }
  53. TYPE_MAP = {
  54. 0: 'voltage',
  55. 1: 'fan',
  56. 2: 'temperature',
  57. 3: 'power',
  58. 4: 'energy',
  59. 5: 'current',
  60. 6: 'humidity',
  61. 7: 'max_main',
  62. 16: 'vid',
  63. 17: 'intrusion',
  64. 18: 'max_other',
  65. 24: 'beep_enable'
  66. }
  67. class Service(SimpleService):
  68. def __init__(self, configuration=None, name=None):
  69. SimpleService.__init__(self, configuration=configuration, name=name)
  70. self.order = list()
  71. self.definitions = dict()
  72. self.chips = list()
  73. def get_data(self):
  74. data = dict()
  75. try:
  76. for chip in sensors.ChipIterator():
  77. prefix = sensors.chip_snprintf_name(chip)
  78. for feature in sensors.FeatureIterator(chip):
  79. sfi = sensors.SubFeatureIterator(chip, feature)
  80. for sf in sfi:
  81. val = sensors.get_value(chip, sf.number)
  82. break
  83. type_name = TYPE_MAP[feature.type]
  84. if type_name in LIMITS:
  85. limit = LIMITS[type_name]
  86. if val < limit[0] or val > limit[1]:
  87. continue
  88. data[prefix + "_" + str(feature.name.decode())] = int(val * 1000)
  89. except Exception as error:
  90. self.error(error)
  91. return None
  92. return data or None
  93. def create_definitions(self):
  94. for sensor in ORDER:
  95. for chip in sensors.ChipIterator():
  96. chip_name = sensors.chip_snprintf_name(chip)
  97. if self.chips and not any([chip_name.startswith(ex) for ex in self.chips]):
  98. continue
  99. for feature in sensors.FeatureIterator(chip):
  100. sfi = sensors.SubFeatureIterator(chip, feature)
  101. vals = [sensors.get_value(chip, sf.number) for sf in sfi]
  102. if vals[0] == 0:
  103. continue
  104. if TYPE_MAP[feature.type] == sensor:
  105. # create chart
  106. name = chip_name + "_" + TYPE_MAP[feature.type]
  107. if name not in self.order:
  108. self.order.append(name)
  109. chart_def = list(CHARTS[sensor]['options'])
  110. chart_def[1] = chip_name + chart_def[1]
  111. self.definitions[name] = {'options': chart_def}
  112. self.definitions[name]['lines'] = []
  113. line = list(CHARTS[sensor]['lines'][0])
  114. line[0] = chip_name + "_" + str(feature.name.decode())
  115. line[1] = sensors.get_label(chip, feature)
  116. self.definitions[name]['lines'].append(line)
  117. def check(self):
  118. try:
  119. sensors.init()
  120. except Exception as error:
  121. self.error(error)
  122. return False
  123. self.create_definitions()
  124. return True