123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #!/usr/bin/python
- #
- # Check state of interfaces configured with ifupdown2
- #
- # Maximilian Wilhelm <max@rfc2324.org>
- # -- Fri, 14 Apr 2017 20:05:45 +0200
- #
- import argparse
- import re
- import subprocess
- import sys
- parser = argparse.ArgumentParser (description = 'Check interface configuration.')
- parser.add_argument ("--ok_string", help = "Ifupdown success string", required = True)
- args = parser.parse_args ()
- cmd = [ "/usr/bin/sudo", "/sbin/ifquery", "-c", "-a" ]
- try:
- ifquery = subprocess.Popen (cmd, bufsize = 4194304, stdout = subprocess.PIPE, stderr = subprocess.PIPE).stdout
- # cmd exited with non-zero code
- except subprocess.CalledProcessError as c:
- print "Failed to run %s: %s" % (" ".join (cmd), c.output)
- sys.exit (1)
- # This should not have happend.
- except Exception as e:
- print "Unknown error while running %s: %s" % (" ".join (cmd), str (e))
- sys.exit (3)
- ################################################################################
- # Parse all entries from ifquery output into interfaces dict #
- ################################################################################
- interfaces_ok = []
- interfaces_err = []
- interface_re = re.compile (r'^iface (\S+)\s+\[(.+)\]$')
- ignore_re = re.compile (r'^(auto .*)?$')
- line_re = re.compile (r'^\s+(\S+)\s+(.+)\s+\[(.+)\]$')
- # Parse session list
- interface = None
- interface_dict = None
- for line in ifquery.readlines ():
- line = line.rstrip ()
- # Preamble or empty string
- if ignore_re.search (line):
- if not interface_dict:
- continue
- if interface_dict['ok']:
- interfaces_ok.append (interface)
- else:
- del interface_dict['ok']
- errors = ",".join (sorted (interface_dict.keys ()))
- if errors == "":
- errors = "DOWN"
- interfaces_err.append ("%s: %s" % (interface, errors))
- interface = None
- interface_dict = None
- continue
- # Start of a new interface
- match = interface_re.search (line)
- if match:
- interface = match.group (1)
- interface_dict = {
- 'ok' : True if match.group (2) == args.ok_string else False,
- }
- continue
- # Ignore any non-BGP protocols, empty lines, etc. XXX
- if interface == None:
- continue
- # Parse and store any interesting lines / fields
- match = line_re.search (line)
- if not match:
- continue
- attr = match.group (1)
- value = match.group (2)
- status = match.group (3)
- if status != args.ok_string:
- interface_dict['ok'] = False
- interface_dict[attr] = value
- ret_code = 0
- if len (interfaces_err) > 0:
- print "ERR: %s" % "; ".join (interfaces_err)
- ret_code = 2
- if len (interfaces_ok) > 0:
- print "OK: %s" % ", ".join (interfaces_ok)
- sys.exit (ret_code)
|