github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/charms/network-health/actions/unit-info.py (about) 1 #!/usr/bin/python3 2 import subprocess 3 import re 4 5 from charmhelpers.core.hookenv import ( 6 action_set 7 ) 8 9 10 def main(): 11 unit_interfaces = interfaces() 12 action_set({'interfaces': unit_interfaces}) 13 14 15 def interfaces(): 16 raw = subprocess.check_output('ifconfig', shell=True).decode("utf-8") 17 patterns = ['(?P<device>^[a-zA-Z0-9:]+)(.*)Link encap:(.*).*', 18 '(.*)Link encap:(.*)(HWaddr )(?P<ether>[^\s]*).*', 19 '.*(inet addr:)(?P<inet>[^\s]*).*', 20 '.*(inet6 addr: )(?P<inet6>[^\s\/]*/(?P<prefixlen>[\d]*)).*', 21 '.*(P-t-P:)(?P<ptp>[^\s]*).*', 22 '.*(Bcast:)(?P<broadcast>[^\s]*).*', 23 '.*(Mask:)(?P<netmask>[^\s]*).*', 24 '.*(Scope:)(?P<scopeid>[^\s]*).*', 25 '.*(RX bytes:)(?P<rxbytes>\d+).*', 26 '.*(TX bytes:)(?P<txbytes>\d+).*'] 27 interfaces = {} 28 cur = None 29 all_keys = [] 30 31 for line in raw.splitlines(): 32 for pattern in patterns: 33 match = re.search(pattern, line) 34 if match: 35 groupdict = match.groupdict() 36 if 'device' in groupdict: 37 cur = groupdict['device'] 38 if cur not in interfaces: 39 interfaces[cur] = {} 40 41 for key in groupdict: 42 if key not in all_keys: 43 all_keys.append(key) 44 interfaces[cur][key] = groupdict[key] 45 return interfaces 46 47 48 if __name__ == "__main__": 49 main()