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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/python3
  2. #
  3. import argparse
  4. import json
  5. import nagios
  6. import os
  7. import urllib.request
  8. def get_shelly_data(hostname):
  9. with urllib.request.urlopen(f"http://{hostname}/status") as response:
  10. return json.load(response)
  11. def check_wifi(hostname, warn_lvl, crit_lvl):
  12. data = get_shelly_data(hostname)
  13. #
  14. connected = data['wifi_sta']['connected']
  15. quality = data['wifi_sta']['rssi']
  16. if not connected or quality <= crit_lvl:
  17. status = nagios.Nagios.ERROR
  18. elif quality <= warn_lvl:
  19. status = nagios.Nagios.WARNING
  20. else:
  21. status = nagios.Nagios.OK
  22. return (status, f"shelly: connected={connected}; quality={quality} shall be > warn={warn_lvl} / crit={crit_lvl}")
  23. def check_mqtt(hostname, warn_lvl, crit_lvl):
  24. data = get_shelly_data(hostname)
  25. #
  26. connected = data['mqtt']['connected']
  27. if not connected:
  28. status = nagios.Nagios.ERROR
  29. else:
  30. status = nagios.Nagios.OK
  31. return (status, f"shelly: connected={connected}")
  32. def check_memory(hostname, warn_lvl, crit_lvl):
  33. data = get_shelly_data(hostname)
  34. #
  35. ram_total = data['ram_total']
  36. ram_free = data['ram_free']
  37. ram_left = ram_free / ram_total * 100
  38. if ram_left <= crit_lvl:
  39. status = nagios.Nagios.ERROR
  40. elif ram_left <= warn_lvl:
  41. status = nagios.Nagios.WARNING
  42. else:
  43. status = nagios.Nagios.OK
  44. return (status, f"shelly: ram_left={ram_left:.1f}percent shall be > warn={warn_lvl} / crit={crit_lvl}")
  45. def check_filesystem(hostname, warn_lvl, crit_lvl):
  46. data = get_shelly_data(hostname)
  47. #
  48. fs_size = data['fs_size']
  49. fs_free = data['fs_free']
  50. fs_left = fs_free / fs_size * 100
  51. if fs_left <= crit_lvl:
  52. status = nagios.Nagios.ERROR
  53. elif fs_left <= warn_lvl:
  54. status = nagios.Nagios.WARNING
  55. else:
  56. status = nagios.Nagios.OK
  57. return (status, f"shelly: fs_left={fs_left:.1f}percent shall be > warn={warn_lvl} / crit={crit_lvl}")
  58. def check_temperature(hostname, warn_lvl, crit_lvl):
  59. data = get_shelly_data(hostname)
  60. #
  61. temperature = data.get('tmp', {}).get('tC')
  62. if temperature is None:
  63. if args.hostname.startswith('shelly1-'):
  64. return (nagios.Nagios.OK, "shelly: Shelly1 has no temperature information")
  65. status = nagios.Nagios.UNKNOWN
  66. elif temperature >= crit_lvl:
  67. status = nagios.Nagios.ERROR
  68. elif temperature >= warn_lvl:
  69. status = nagios.Nagios.WARNING
  70. else:
  71. status = nagios.Nagios.OK
  72. return (status, f"shelly: temperature:={temperature} C shall be < warn={warn_lvl} / crit={crit_lvl}")
  73. if __name__ == "__main__":
  74. # Parse arguments
  75. parser = argparse.ArgumentParser(
  76. prog=os.path.basename(__file__),
  77. description='Check shelly for nagios monitorin',
  78. )
  79. parser.add_argument('-H', '--hostname', required=True)
  80. parser.add_argument('-w', '--warn_lvl', type=float, required=True)
  81. parser.add_argument('-c', '--critical_lvl', type=float, required=True)
  82. args = parser.parse_args()
  83. # Init nagios instance
  84. n = nagios.Nagios()
  85. # Identify check
  86. this_check = parser.prog[len("check_shelly_"):]
  87. check_function = {
  88. 'wifi': check_wifi,
  89. 'mqtt': check_mqtt,
  90. 'memory': check_memory,
  91. 'filesystem': check_filesystem,
  92. 'temperature': check_temperature
  93. }.get(this_check)
  94. # process check and return feedback
  95. if check_function is None:
  96. status = n.UNKNOWN
  97. message = f"Unknown check - {this_check}"
  98. else:
  99. status, message = check_function(args.hostname, args.warn_lvl, args.critical_lvl)
  100. n.exit(status, message)