check_ifupdown2 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/python
  2. #
  3. # Check state of interfaces configured with ifupdown2
  4. #
  5. # Maximilian Wilhelm <max@rfc2324.org>
  6. # -- Fri, 14 Apr 2017 20:05:45 +0200
  7. #
  8. import argparse
  9. import re
  10. import subprocess
  11. import sys
  12. parser = argparse.ArgumentParser (description = 'Check interface configuration.')
  13. parser.add_argument ("--ok_string", help = "Ifupdown success string", required = True)
  14. args = parser.parse_args ()
  15. cmd = [ "/usr/bin/sudo", "/sbin/ifquery", "-c", "-a" ]
  16. try:
  17. ifquery = subprocess.Popen (cmd, bufsize = 4194304, stdout = subprocess.PIPE, stderr = subprocess.PIPE).stdout
  18. # cmd exited with non-zero code
  19. except subprocess.CalledProcessError as c:
  20. print "Failed to run %s: %s" % (" ".join (cmd), c.output)
  21. sys.exit (1)
  22. # This should not have happend.
  23. except Exception as e:
  24. print "Unknown error while running %s: %s" % (" ".join (cmd), str (e))
  25. sys.exit (3)
  26. ################################################################################
  27. # Parse all entries from ifquery output into interfaces dict #
  28. ################################################################################
  29. interfaces_ok = []
  30. interfaces_err = []
  31. interface_re = re.compile (r'^iface (\S+)\s+\[(.+)\]$')
  32. ignore_re = re.compile (r'^(auto .*)?$')
  33. line_re = re.compile (r'^\s+(\S+)\s+(.+)\s+\[(.+)\]$')
  34. # Parse session list
  35. interface = None
  36. interface_dict = None
  37. for line in ifquery.readlines ():
  38. line = line.rstrip ()
  39. # Preamble or empty string
  40. if ignore_re.search (line):
  41. if not interface_dict:
  42. continue
  43. if interface_dict['ok']:
  44. interfaces_ok.append (interface)
  45. else:
  46. del interface_dict['ok']
  47. errors = ",".join (sorted (interface_dict.keys ()))
  48. if errors == "":
  49. errors = "DOWN"
  50. interfaces_err.append ("%s: %s" % (interface, errors))
  51. interface = None
  52. interface_dict = None
  53. continue
  54. # Start of a new interface
  55. match = interface_re.search (line)
  56. if match:
  57. interface = match.group (1)
  58. interface_dict = {
  59. 'ok' : True if match.group (2) == args.ok_string else False,
  60. }
  61. continue
  62. # Ignore any non-BGP protocols, empty lines, etc. XXX
  63. if interface == None:
  64. continue
  65. # Parse and store any interesting lines / fields
  66. match = line_re.search (line)
  67. if not match:
  68. continue
  69. attr = match.group (1)
  70. value = match.group (2)
  71. status = match.group (3)
  72. if status != args.ok_string:
  73. interface_dict['ok'] = False
  74. interface_dict[attr] = value
  75. ret_code = 0
  76. if len (interfaces_err) > 0:
  77. print "ERR: %s" % "; ".join (interfaces_err)
  78. ret_code = 2
  79. if len (interfaces_ok) > 0:
  80. print "OK: %s" % ", ".join (interfaces_ok)
  81. sys.exit (ret_code)