Nagios Plugins
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

check_shelly 2.8KB

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