diff options
author | Scott Moser <smoser@ubuntu.com> | 2011-08-22 23:24:08 -0400 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2011-08-22 23:24:08 -0400 |
commit | 5e0edd8cf0a8431d453649037da913285e28850f (patch) | |
tree | 26b915b89803f97bc63ebb98a97e3e6ff12e236d | |
parent | a3373fb60aac7f3fee9ec28afebc0e55d28d5438 (diff) | |
download | vyos-cloud-init-5e0edd8cf0a8431d453649037da913285e28850f.tar.gz vyos-cloud-init-5e0edd8cf0a8431d453649037da913285e28850f.zip |
Add some network debug info printed to the console
When 'cloud-init start' (network) runs, it will print information
about the systems network info to the console. This will help in debugging
instances.
LP: #828186
-rw-r--r-- | ChangeLog | 4 | ||||
-rwxr-xr-x | cloud-init.py | 3 | ||||
-rw-r--r-- | cloudinit/netinfo.py | 83 |
3 files changed, 89 insertions, 1 deletions
@@ -42,7 +42,9 @@ apt-add-repository would get stdin which was attached to a terminal (/dev/console) and would thus hang when running during boot. (LP: 831505) This was done by changing all users of util.subp to have None input unless specified - + - Add some debug info to the console when cloud-init runs. + This is useful if debugging, IP and route information is printed to the console. + 0.6.1: - fix bug in fixing permission on /var/log/cloud-init.log (LP: #704509) - improve comment strings in rsyslog file tools/21-cloudinit.conf diff --git a/cloud-init.py b/cloud-init.py index 0ba00699..c38512fe 100755 --- a/cloud-init.py +++ b/cloud-init.py @@ -24,6 +24,7 @@ import cloudinit import cloudinit.util as util import cloudinit.CloudConfig as CC import cloudinit.DataSource as ds +import cloudinit.netinfo as netinfo import time import logging import errno @@ -87,6 +88,8 @@ def main(): nonet_path = "%s/%s" % (cloudinit.get_cpath("data"), "no-net") if cmd == "start": + print netinfo.debug_info() + stop_files = ( cloudinit.get_ipath_cur("obj_pkl"), nonet_path ) # if starting as the network start, there are cases # where everything is already done for us, and it makes diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py new file mode 100644 index 00000000..1b6af7e2 --- /dev/null +++ b/cloudinit/netinfo.py @@ -0,0 +1,83 @@ +import subprocess + +def netdev_info(): + fields = ( "hwaddr", "addr", "bcast", "mask" ) + ifcfg_out = subprocess.check_output(["ifconfig", "-a"]) + devs = { } + for line in ifcfg_out.splitlines(): + if len(line) == 0: + continue + if line[0] not in ("\t", " "): + curdev = line.split()[0] + devs[curdev] = { "up": False } + for field in fields: + devs[curdev][field] = "" + toks = line.lower().strip().split() + if toks[0] == "up": + devs[curdev]['up'] = True + + fieldpost = "" + if toks[0] == "inet6": + fieldpost = "6" + + for i in range(len(toks)): + if toks[i] == "hwaddr": + try: + devs[curdev]["hwaddr"] = toks[i+1] + except IndexError as e: + pass + for field in ("addr", "bcast", "mask"): + target = "%s%s" % (field, fieldpost) + if devs[curdev].get(target,""): + continue + if toks[i] == "%s:" % field: + try: + devs[curdev][target] = toks[i+1] + except IndexError as e: + pass + elif toks[i].startswith("%s:" % field): + devs[curdev][target] = toks[i][len(field)+1:] + return(devs) + +def route_info(): + route_out = subprocess.check_output(["route", "-n"]) + routes = [ ] + for line in route_out.splitlines()[1:]: + if not line: + continue + toks = line.split() + if toks[0] == "Kernel" or toks[0] == "Destination": + continue + routes.append(toks) + return(routes) + +def getgateway(): + for r in route_info(): + if r[3].find("G") >= 0: + return("%s[%s]" % (r[1],r[7])) + return(None) + +def debug_info(pre="ci-info: "): + lines = [ ] + try: + netdev = netdev_info() + except Exception as e: + lines.append("netdev_info failed!") + netdev = [] + for (dev, d) in netdev.iteritems(): + lines.append("%s%-6s: %i %-15s %-15s %s" % + (pre, dev, d["up"], d["addr"],d["mask"], d["hwaddr"])) + try: + routes = route_info() + except Exception as e: + lines.append("route_info failed") + routes = [] + n = 0 + for r in routes: + lines.append("%sroute-%d: %-15s %-15s %-15s %-6s %s" % + (pre, n, r[0], r[1], r[2], r[7], r[3])) + n = n+1 + return('\n'.join(lines)) + +if __name__ == '__main__': + print debug_info() |