testAvailable.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. """Ping nodes and show unavailable."""
  3. import json
  4. import os
  5. import sys
  6. import subprocess
  7. RED = '\x1b[01;31m'
  8. GREEN = '\x1b[01;32m'
  9. YELLOW = '\x1b[01;33m'
  10. END = '\x1b[0m'
  11. def validate(path):
  12. """Test a single set of peering creds."""
  13. result = True
  14. print("Pinging %s" % path)
  15. try:
  16. with open(path) as f:
  17. for host in json.loads(f.read()):
  18. if host[0] == '[': continue
  19. ipv4 = host.split(':')[0]
  20. res = subprocess.call(['ping', '-c3', '-W5', ipv4], stdout=subprocess.DEVNULL)
  21. if res:
  22. print(" %s%s is failed%s" % (RED, ipv4, END))
  23. result = False
  24. else:
  25. print(" %s%s is ok%s" % (GREEN, ipv4, END))
  26. except ValueError:
  27. print(" %sInvalid JSON!%s" % (RED, END))
  28. result = False
  29. except KeyboardInterrupt:
  30. print(" %sInterrupt%s" % (RED, END))
  31. sys.exit(1)
  32. return result
  33. if __name__ == "__main__":
  34. success = True
  35. for directory, subdirs, files in os.walk('.'):
  36. if len(files) > 0:
  37. if directory != '.' and not directory.startswith('./.git'):
  38. for f in files:
  39. result = validate("%s/%s" % (directory, f))
  40. if not result:
  41. success = False
  42. if not success:
  43. sys.exit(1)