Nagios Plugins
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

__init__.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import json
  2. import logging
  3. import mqtt
  4. import nagios
  5. import time
  6. try:
  7. from config import APP_NAME as ROOT_LOGGER_NAME
  8. except ImportError:
  9. ROOT_LOGGER_NAME = 'root'
  10. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  11. class base(object):
  12. MONITORING_HEARTBEAT = "heartbeat"
  13. MONITORING_BATTERY = "battery"
  14. MONITORING_FOLLOW_SETPOINT = "follow_setpoint"
  15. #
  16. FOLLOW_REQUEST_WARNING = 5 # Seconds, till warning comes up, if device does not follow the command
  17. FOLLOW_REQUEST_ERROR = 60 # Seconds, till error comes up, if device does not follow the command
  18. FOLLOW_KEY = None
  19. #
  20. BATTERY_LVL_WARNING = 15
  21. BATTERY_LVL_ERROR = 5
  22. #
  23. LAST_MSG_WARNING = 6 * 60 * 60
  24. LAST_MSG_ERROR = 24 * 60 * 60
  25. def __init__(self, mqtt_client: mqtt.mqtt_client, topic):
  26. self.topic = topic
  27. #
  28. self.__unknown_tm__ = {}
  29. #
  30. mqtt_client.add_callback(topic, self.__rx__)
  31. mqtt_client.add_callback(topic + '/#', self.__rx__)
  32. #
  33. self.last_device_msg = None
  34. #
  35. self.__target_storage__ = {}
  36. self.__state_storage__ = {}
  37. #
  38. self.battery = None
  39. def __rx__(self, client, userdata, message):
  40. try:
  41. payload = json.loads(message.payload)
  42. except json.decoder.JSONDecodeError:
  43. pass
  44. else:
  45. if type(payload) is dict:
  46. #
  47. # heartbeat
  48. #
  49. if message.topic == self.topic:
  50. self.last_device_msg = time.time()
  51. #
  52. # follow setpoint
  53. #
  54. if self.FOLLOW_KEY is not None and self.FOLLOW_KEY in payload:
  55. if message.topic == self.topic + '/set':
  56. self.target(self.FOLLOW_KEY, payload[self.FOLLOW_KEY])
  57. if message.topic == self.topic:
  58. self.state(self.FOLLOW_KEY, payload[self.FOLLOW_KEY])
  59. #
  60. # battery level
  61. #
  62. if self.MONITORING_BATTERY in payload and message.topic == self.topic:
  63. self.battery = payload[self.MONITORING_BATTERY]
  64. def target(self, key, value):
  65. tm_t, value_t = self.__target_storage__.get(key, (0, None))
  66. if value != value_t:
  67. self.__target_storage__[key] = time.time(), value
  68. logger.debug("Target value for device identified: %s: %s", key, repr(value))
  69. def state(self, key, value):
  70. self.__state_storage__[key] = time.time(), value
  71. logger.debug("Device state identified: %s: %s", key, repr(value))
  72. def status(self, key):
  73. #
  74. # HEARTBEAT
  75. #
  76. if key == self.MONITORING_HEARTBEAT:
  77. if self.last_device_msg is None:
  78. return self.__nagios_return__(self.MONITORING_HEARTBEAT, nagios.Nagios.UNKNOWN, "Device exists, but no data received")
  79. else:
  80. dt = time.time() - self.last_device_msg
  81. dt_disp = dt / 60 / 60
  82. if dt > self.LAST_MSG_ERROR:
  83. return self.__nagios_return__(self.MONITORING_HEARTBEAT, nagios.Nagios.ERROR, "Last message %.1fh ago" % dt_disp)
  84. elif dt > self.LAST_MSG_WARNING:
  85. return self.__nagios_return__(self.MONITORING_HEARTBEAT, nagios.Nagios.WARNING, "Last message %.1fh ago" % dt_disp)
  86. else:
  87. return self.__nagios_return__(self.MONITORING_HEARTBEAT, nagios.Nagios.OK, "Last message %.1fh ago" % dt_disp)
  88. #
  89. # FOLLOW SETPOINT
  90. #
  91. elif key == self.MONITORING_FOLLOW_SETPOINT:
  92. if self.FOLLOW_KEY is None:
  93. return self.__nagios_return__(self.MONITORING_FOLLOW_SETPOINT, nagios.Nagios.UNKNOWN, "Device exist, but does not follow any setpoint.", force=True)
  94. tm_s, value_s = self.__state_storage__.get(self.FOLLOW_KEY, (0, None))
  95. try:
  96. tm_t, value_t = self.__target_storage__[self.FOLLOW_KEY]
  97. except KeyError:
  98. if value_s is not None:
  99. return self.__nagios_return__(self.MONITORING_FOLLOW_SETPOINT, nagios.Nagios.OK, "Current temperature setpoint %.1f°C, but never received a setpoint. That might be okay." % value_s)
  100. return self.__nagios_return__(self.MONITORING_FOLLOW_SETPOINT, nagios.Nagios.UNKNOWN, "Device exists, but no data received")
  101. else:
  102. tm = time.time()
  103. dt = tm - tm_t
  104. if value_t != value_s and dt > self.FOLLOW_REQUEST_ERROR:
  105. return self.__nagios_return__(self.MONITORING_FOLLOW_SETPOINT, nagios.Nagios.ERROR, "Requested setpoint %.1f°C unequal valve setpoint %.1f°C since %.1fmin" % (value_t, value_s, (time.time()-tm_s)/60))
  106. elif value_t != value_s and dt > self.FOLLOW_REQUEST_WARNING:
  107. return self.__nagios_return__(self.MONITORING_FOLLOW_SETPOINT, nagios.Nagios.WARNING, "Requested setpoint %.1f°C unequal valve setpoint %.1f°C since %.1fmin" % (value_t, value_s, (time.time()-tm_s)))
  108. return self.__nagios_return__(self.MONITORING_FOLLOW_SETPOINT, nagios.Nagios.OK, "Requested setpoint equal valve setpoint %.1f°C" % value_s)
  109. #
  110. # BATTERY
  111. #
  112. elif key == self.MONITORING_BATTERY:
  113. if self.battery is None:
  114. return self.__nagios_return__(self.MONITORING_BATTERY, nagios.Nagios.UNKNOWN, "Device exists, but no data received or unknown monitoring")
  115. elif self.battery <= self.BATTERY_LVL_ERROR:
  116. return self.__nagios_return__(self.MONITORING_BATTERY, nagios.Nagios.ERROR, "Battery level critical low (%.1f%%)" % self.battery)
  117. elif self.battery <= self.BATTERY_LVL_WARNING:
  118. return self.__nagios_return__(self.MONITORING_BATTERY, nagios.Nagios.WARNING, "Battery level low (%.1f%%)" % self.battery)
  119. else:
  120. return self.__nagios_return__(self.MONITORING_BATTERY, nagios.Nagios.OK, "Battery okay (%.1f%%)" % self.battery)
  121. def __nagios_return__(self, monitoring_name, status, msg, force=False):
  122. tm = time.time()
  123. if monitoring_name not in self.__unknown_tm__:
  124. self.__unknown_tm__[monitoring_name] = None
  125. if status == nagios.Nagios.UNKNOWN and not force:
  126. if self.__unknown_tm__[monitoring_name] is None:
  127. self.__unknown_tm__[monitoring_name] = tm
  128. dt = tm - self.__unknown_tm__[monitoring_name]
  129. if dt >= self.LAST_MSG_ERROR:
  130. status = nagios.Nagios.UNKNOWN
  131. elif dt >= self.LAST_MSG_WARNING:
  132. status = nagios.Nagios.WARNING
  133. else:
  134. status = nagios.Nagios.OK
  135. msg += " - since %.1fh" % (dt / 3600)
  136. else:
  137. self.__unknown_tm__[monitoring_name] = None
  138. return {"status": status, "msg": msg}
  139. class group(object):
  140. def __init__(self, *args, **kwargs):
  141. pass
  142. class shelly_sw1(base):
  143. pass
  144. class tradfri_sw(base):
  145. pass
  146. class tradfri_sw_br(base):
  147. pass
  148. class tradfri_sw_br_ct(base):
  149. pass
  150. class tradfri_button(base):
  151. LAST_MSG_WARNING = 24 * 60 * 60
  152. LAST_MSG_ERROR = 48 * 60 * 60
  153. class livarno_sw_br_ct(base):
  154. pass
  155. class brennenstuhl_heatingvalve(base):
  156. BATTERY_LVL_WARNING = 4
  157. BATTERY_LVL_ERROR = 3
  158. FOLLOW_KEY = "current_heating_setpoint"
  159. class silvercrest_powerplug(base):
  160. pass
  161. class silvercrest_motion_sensor(base):
  162. pass
  163. class my_powerplug(base):
  164. pass
  165. class audio_status(base):
  166. pass
  167. class remote(base):
  168. pass