def _interface_stats(): try: up_ifaces = interfacehelper.get_interfaces().get_interface_list() all_ifaces = interfacehelper.get_all_interfaces().get_interface_list() [rv, raw_table, err] = run_command([constants.CMD_IPTABLES_SAVE, '-t', 'raw'], retval=runcommand.FAIL) [rv, mangle_table, err] = run_command([constants.CMD_IPTABLES_SAVE, '-t', 'mangle'], retval=runcommand.FAIL) [rv, nat_table, err] = run_command([constants.CMD_IPTABLES_SAVE, '-t', 'nat'], retval=runcommand.FAIL) [rv, filter_table, err] = run_command([constants.CMD_IPTABLES_SAVE, '-t', 'filter'], retval=runcommand.FAIL) raw_len = len(raw_table.split('\n')) mangle_len = len(mangle_table.split('\n')) nat_len = len(nat_table.split('\n')) filter_len = len(filter_table.split('\n')) _log.info('NETWORKINFO: ' \ 'all interfaces: %d, ' \ 'up interfaces: %d, ' \ 'iptables size: %d (raw: %d, mangle: %d, nat: %d, filter: %d)', len(all_ifaces), len(up_ifaces), raw_len + mangle_len + nat_len + filter_len, raw_len, mangle_len, nat_len, filter_len) except: _log.exception('failed to get network stats')
def _check_interfaces(_log): from codebay.l2tpserver import interfacehelper _re_iftab_line = re.compile(r'^([^ #]+)\s+\w{3}\s+(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2})') infos = interfacehelper.get_all_interfaces() current_ifaces = {} for i in infos.get_interface_list(): _log.info('system interface: %s' % i.toString()) if not i.is_ethernet_device(): continue current_ifaces[i.get_device_name()] = i.get_mac() iftab_ifaces = {} try: iftab = open('/etc/iftab', 'r') for l in iftab.read().split('\n'): m = _re_iftab_line.match(l) if m is None or len(m.groups()) < 2: continue dev = m.groups()[0] mac = m.groups()[1] info = interfacehelper.InterfaceInfo(dev, 0, 0, 0, 0, 0, mac, 'ether') if info.is_ethernet_device(): iftab_ifaces[dev] = mac _log.info('iftab line: %s, %s' % (dev, mac)) iftab.close() except: # Note: file may be missing, do not panic _log.exception('Failed to read /etc/iftab, creating from scratch.') pass for i in set(current_ifaces.keys()) - set(iftab_ifaces.keys()): # Note: GUI detects new interfaces by itself, no action pass for i in set(iftab_ifaces.keys()) - set(current_ifaces.keys()): # Note: missing interfaces are ok.. and cannot tell if the # interface vanished *just* now because mac locks are not removed. pass # Note: this removes old interface bindings if any.. # Note: we do not care of invalid iftab bindinds, they are udev problems new_ifaces = iftab_ifaces new_ifaces.update(current_ifaces) if_lines = '' keys = new_ifaces.keys() keys.sort() for i in keys: # XXX: could also try to resolve the arp type and write if to # config, but propably not worth the effort? if_lines += '%s mac %s\n' % (i, new_ifaces[i]) helpers.write_file('/etc/iftab', textwrap.dedent("""\ # This file assigns persistent names to network interfaces. # See iftab(5) for syntax. # This file is autogenerated on system boot, do not edit. %s""") % if_lines)