Nagios Plugins
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

check_shelly 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/python3
  2. #
  3. import argparse
  4. import json
  5. import nagios
  6. import os
  7. import urllib.request
  8. CHECKS = ['wifi', 'mqtt', 'memory', 'filesystem', 'temperature']
  9. #
  10. WIFI_QUALITY_ERROR = -30
  11. WIFI_QUALITY_WARNING = -50
  12. #
  13. RAM_ERROR = .15
  14. RAM_WARNING = .30
  15. #
  16. FS_ERROR = .15
  17. FS_WARNING = .30
  18. #
  19. TMP_WARNING = 50
  20. TMP_ERROR = 57
  21. if __name__ == "__main__":
  22. parser = argparse.ArgumentParser(
  23. prog=os.path.basename(__file__),
  24. description='Check shelly for nagios monitorin',
  25. # epilog='Text at the bottom of help'
  26. )
  27. parser.add_argument('-H', '--hostname', required=True)
  28. parser.add_argument('-c', '--check', choices=CHECKS, required=True)
  29. args = parser.parse_args()
  30. #
  31. n = nagios.Nagios()
  32. status = n.UNKNOWN
  33. #
  34. with urllib.request.urlopen(f"http://{args.hostname}/status") as response:
  35. data = json.load(response)
  36. #
  37. #
  38. if args.check == 'wifi':
  39. connected = data['wifi_sta']['connected']
  40. quality = data['wifi_sta']['rssi']
  41. if not connected or quality > WIFI_QUALITY_ERROR:
  42. status = n.ERROR
  43. elif quality > WIFI_QUALITY_WARNING:
  44. status = n.WARNING
  45. else:
  46. status = n.OK
  47. n.exit(status, f"connected: {connected} - quality: {quality} < {WIFI_QUALITY_WARNING} < {WIFI_QUALITY_ERROR}")
  48. elif args.check == 'mqtt':
  49. connected = data['mqtt']['connected']
  50. if not connected:
  51. status = n.ERROR
  52. else:
  53. status = n.OK
  54. n.exit(status, f"connected: {connected}")
  55. elif args.check == 'memory':
  56. ram_total = data['ram_total']
  57. ram_free = data['ram_free']
  58. ram_left = ram_free / ram_total
  59. if ram_left < RAM_ERROR:
  60. status = n.ERROR
  61. elif ram_left < RAM_WARNING:
  62. status = n.WARNING
  63. else:
  64. status = n.OK
  65. n.exit(status, f"ram_left: {ram_left} ({ram_total}) > {RAM_WARNING} > {RAM_ERROR}")
  66. elif args.check == 'filesystem':
  67. fs_size = data['fs_size']
  68. fs_free = data['fs_free']
  69. fs_left = fs_free / fs_size
  70. if fs_left < FS_ERROR:
  71. status = n.ERROR
  72. elif fs_left < FS_WARNING:
  73. status = n.WARNING
  74. else:
  75. status = n.OK
  76. n.exit(status, f"fs_left: {fs_left} ({fs_size}) > {FS_WARNING} > {FS_ERROR}")
  77. elif args.check == 'temperature':
  78. temperature = data.get('tmp', {}).get('tC')
  79. if temperature is None:
  80. status = n.UNKNOWN
  81. elif temperature >= TMP_ERROR:
  82. status = n.ERROR
  83. elif temperature >= TMP_WARNING:
  84. status = n.WARNING
  85. else:
  86. status = n.OK
  87. n.exit(status, f"temperature: {temperature}°C")