Changeset 1403


Ignore:
Timestamp:
02/23/07 17:42:40 (6 years ago)
Author:
mglb1
Message:

Add two new helper functions

  • getFQDN - returns the FQDN of the current hosts
  • getTrafficCounters - returns a dictionary of the current iface traffic counters for each interface on the host
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ccsd/trunk/crcnetd/_utils/ccsd_common.py

    r1395 r1403  
    577577 
    578578    return socket.gethostbyname(name) 
     579 
     580def getFQDN(): 
     581    """Returns the fully qualified hostname of the current host""" 
     582    fd = os.popen("/bin/hostname -f", "r") 
     583    hostname = fd.read() 
     584    fd.close() 
     585    return hostname.strip() 
    579586 
    580587def bitsToNetmask(length): 
     
    10441051    return "" 
    10451052 
     1053def getTrafficCounters(): 
     1054    """Returns a dictionary of the current traffic counters for each interface""" 
     1055    ifaces = {} 
     1056     
     1057    # Read /proc 
     1058    try: 
     1059        fp = open("/proc/net/dev", "r") 
     1060        lines = fp.readlines() 
     1061        fp.close() 
     1062    except: 
     1063        log_error("Could not read interface traffic counters!", sys.exc_info()) 
     1064        return ifaces 
     1065     
     1066    # Parse values, skip first two header lines 
     1067    for line in lines[2:]: 
     1068        parts = line.strip().split(":") 
     1069        if len(parts) != 2: 
     1070            log_warn("Ignoring invalid /proc/net/dev line: %s" % line) 
     1071            continue 
     1072        ifname = parts[0].strip() 
     1073        counters = parts[1].strip().split() 
     1074        if len(counters) != 16: 
     1075            log_warn("Ignoring invalid /proc/net/dev entry '%s': %s" % \ 
     1076                    (ifname, parts[2])) 
     1077            continue 
     1078        iface = {"rx":long(counters[0]), "rx_packets":long(counters[1]), \ 
     1079                "tx":long(counters[8]), "tx_packets":long(counters[9])} 
     1080        ifaces[ifname] = iface 
     1081     
     1082    return ifaces 
     1083 
    10461084def route_cmp(a, b): 
    10471085    """Comparison function to sort the route table by netmask then prefix""" 
Note: See TracChangeset for help on using the changeset viewer.