Nagios Plugins
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

check_shelly 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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']
  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. if __name__ == "__main__":
  19. parser = argparse.ArgumentParser(
  20. prog=os.path.basename(__file__),
  21. description='Check shelly for nagios monitorin',
  22. # epilog='Text at the bottom of help'
  23. )
  24. parser.add_argument('-H', '--hostname', required=True)
  25. parser.add_argument('-c', '--check', choices=CHECKS, required=True)
  26. args = parser.parse_args()
  27. #
  28. n = nagios.Nagios()
  29. status = n.UNKNOWN
  30. #
  31. with urllib.request.urlopen(f"http://{args.hostname}/status") as response:
  32. data = json.load(response)
  33. #
  34. #
  35. if args.check == 'wifi':
  36. connected = data['wifi_sta']['connected']
  37. quality = data['wifi_sta']['rssi']
  38. if not connected or quality > WIFI_QUALITY_ERROR:
  39. status = n.ERROR
  40. elif quality > WIFI_QUALITY_WARNING:
  41. status = n.WARNING
  42. else:
  43. status = n.OK
  44. n.exit(status, f"connected: {connected} - quality: {quality} < {WIFI_QUALITY_WARNING} < {WIFI_QUALITY_ERROR}")
  45. elif args.check == 'mqtt':
  46. connected = data['mqtt']['connected']
  47. if not connected:
  48. status = n.ERROR
  49. else:
  50. status = n.OK
  51. n.exit(status, f"connected: {connected}")
  52. elif args.check == 'memory':
  53. ram_total = data['ram_total']
  54. ram_free = data['ram_free']
  55. ram_left = ram_free / ram_total
  56. if ram_left < RAM_ERROR:
  57. status = n.ERROR
  58. elif ram_left < RAM_WARNING:
  59. status = n.WARNING
  60. else:
  61. status = n.OK
  62. n.exit(status, f"ram_left: {ram_left} ({ram_total}) > {RAM_WARNING} > {RAM_ERROR}")
  63. elif args.check == 'filesystem':
  64. fs_size = data['fs_size']
  65. fs_free = data['fs_free']
  66. fs_left = fs_free / fs_size
  67. if fs_left < FS_ERROR:
  68. status = n.ERROR
  69. elif fs_left < FS_WARNING:
  70. status = n.WARNING
  71. else:
  72. status = n.OK
  73. n.exit(status, f"fs_left: {fs_left} ({fs_size}) > {FS_WARNING} > {FS_ERROR}")