diff options
author | Bala.FA <bala@gluster.com> | 2011-09-20 17:00:56 +0530 |
---|---|---|
committer | Tim <timothyasir@gluster.com> | 2011-09-23 13:58:01 +0530 |
commit | 3afd230b0abe5b333551175f757de37a3f5b76a2 (patch) | |
tree | 6e864381575ee114724b24a1785c497c2f999f31 /src/com.gluster.storage.management.gateway.scripts/src/backend | |
parent | eb94bb5dc369b1c57bbc12c76707db2e620d41f1 (diff) |
Major code cleanup getNetDeviceList()
Signed-off-by: Bala.FA <bala@gluster.com>
Diffstat (limited to 'src/com.gluster.storage.management.gateway.scripts/src/backend')
-rwxr-xr-x | src/com.gluster.storage.management.gateway.scripts/src/backend/NetworkUtils.py | 267 | ||||
-rwxr-xr-x | src/com.gluster.storage.management.gateway.scripts/src/backend/get_server_details.py | 48 |
2 files changed, 95 insertions, 220 deletions
diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/NetworkUtils.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/NetworkUtils.py index 8052fa4f..5bc6f1f9 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/NetworkUtils.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/NetworkUtils.py @@ -13,141 +13,65 @@ if not p2 in sys.path: import Globals import Utils -def readResolvConfFile(fileName=None, includeLocalHost=True): +def readResolvConfFile(fileName=None): nameServerList = [] domain = None searchDomain = None if not fileName: fileName = Globals.RESOLV_CONF_FILE - try: - for line in open(fileName): - tokens = line.split("#")[0].strip().split() - if len(tokens) < 2: - continue - if tokens[0].upper() == "NAMESERVER": - if includeLocalHost == False and tokens[1] == "127.0.0.1": - continue - nameServerList.append(tokens[1]) - continue - if tokens[0].upper() == "DOMAIN": - domain = tokens[1:] - continue - if tokens[0].upper() == "SEARCH": - searchDomain = tokens[1:] - continue - return nameServerList, domain, searchDomain - except IOError, e: - Utils.log("failed to read %s file: %s" % (fileName, str(e))) - return None, None, None + lines = Utils.readFile(fileName, lines=True) + for line in lines: + tokens = line.split("#")[0].strip().split() + if len(tokens) < 2: + continue + if tokens[0].upper() == "NAMESERVER": + nameServerList.append(tokens[1]) + continue + if tokens[0].upper() == "DOMAIN": + domain = tokens[1:] + continue + if tokens[0].upper() == "SEARCH": + searchDomain = tokens[1:] + continue + return nameServerList, domain, searchDomain def readIfcfgConfFile(deviceName, root=""): conf = {} fileName = "%s%s/ifcfg-%s" % (root, Globals.SYSCONFIG_NETWORK_DIR, deviceName) - try: - for line in open(fileName): - tokens = line.split("#")[0].split("=") - if len(tokens) != 2: - continue - conf[tokens[0].strip().lower()] = tokens[1].strip() - return conf - except IOError, e: - Utils.log("failed to read %s file: %s" % (fileName, str(e))) - return None - - -def getNetDeviceDetail(deviceName): - deviceDetail = {} - deviceDetail['Name'] = deviceName - rv = Utils.runCommand("ifconfig %s" % deviceName, output=True, root=True) - if rv["Status"] != 0: - return False - for line in rv["Stdout"].split(): - tokens = line.strip().split(":") - if tokens[0].upper() == "ENCAP": - deviceDetail['Model'] = tokens[1].strip().upper() - break - - for line in rv["Stdout"].split("\n"): - if line.strip().startswith("inet addr:"): - tokens = line.strip().split(":") - if tokens[0].upper() == "INET ADDR": - try: - deviceDetail['Ip'] = tokens[1].strip().split()[0] - deviceDetail['Mask'] = tokens[-1].strip() - except IndexError, e: - pass - break - return deviceDetail - -def getNetDeviceGateway(deviceName): - rv = Utils.runCommand("route -n", output=True, root=True) - if rv["Status"] != 0: - return None - if not rv["Stdout"]: - return None - lines = [line for line in rv["Stdout"].split("\n") if line.find("UG") != -1 and line.find(deviceName)] - if not lines: - return None - line = lines[-1].split() - if line and len(line) > 1: - return line[1] - return None - -def getNetSpeed(deviceName): - rv = Utils.runCommand("ethtool %s" % deviceName, output=True, root=True) - if rv["Status"] != 0: - return False - for line in rv["Stdout"].split("\n"): - tokens = line.strip().split(":") - if tokens[0].upper() == "SPEED": - return tokens[1].strip().upper().split("MB")[0] - return None - -def getLinkStatus(deviceName): - return True - ## ethtool takes very long time to respond. So its disabled now - rv = Utils.runCommand("ethtool %s" % deviceName, output=True, root=True) - if rv["Status"] != 0: - return False - for line in rv["Stdout"].split("\n"): - tokens = line.strip().split(":") - if tokens[0].upper() == "LINK DETECTED": - if tokens[1].strip().upper() == "YES": - return True - else: - return False - return False + lines = Utils.readFile(fileName, lines=True) + for line in lines: + tokens = line.split("#")[0].split("=") + if len(tokens) != 2: + continue + conf[tokens[0].strip().lower()] = tokens[1].strip() + return conf def getBondMode(deviceName, fileName=None): if not fileName: fileName = Globals.MODPROBE_CONF_FILE - try: - for line in open(fileName): - tokens = line.split("#")[0].split() - if len(tokens) < 4: - continue - if tokens[0].upper() == "OPTIONS" and tokens[1] == deviceName: - if tokens[2].startswith("mode="): - return tokens[2].split("=")[1] - if tokens[3].startswith("mode="): - return tokens[3].split("=")[1] - if tokens[4].startswith("mode="): - return tokens[4].split("=")[1] - if tokens[5].startswith("mode="): - return tokens[5].split("=")[1] - return None - except IOError, e: - Utils.log("failed to read %s file: %s" % (fileName, str(e))) - return None + lines = Utils.readFile(fileName, lines=True) + for line in lines: + tokens = line.split("#")[0].split() + if len(tokens) < 4: + continue + if tokens[0].upper() == "OPTIONS" and tokens[1] == deviceName: + if tokens[2].startswith("mode="): + return tokens[2].split("=")[1] + if tokens[3].startswith("mode="): + return tokens[3].split("=")[1] + if tokens[4].startswith("mode="): + return tokens[4].split("=")[1] + if tokens[5].startswith("mode="): + return tokens[5].split("=")[1] + return None def getNetDeviceList(root=""): - netDeviceList = [] + netDeviceList = {} for deviceName in os.listdir("/sys/class/net/"): netDevice = {} - netDevice["device"] = None netDevice["description"] = None netDevice["hwaddr"] = None netDevice["type"] = None @@ -165,100 +89,61 @@ def getNetDeviceList(root=""): netDevice["link"] = None netDevice["mode"] = None - #netDevice["device"] = device.Name netDevice["device"] = deviceName - #netDevice["description"] = device.Description netDevice["description"] = deviceName - #netDevice["type"] = device.Type - netDevice["type"] = None - netDevice["link"] = getLinkStatus(deviceName) + netDevice["hwaddr"] = Utils.readFile("/sys/class/net/%s/address" % deviceName).strip() + + rv = Utils.runCommand("ifconfig %s" % deviceName, output=True) + if rv["Status"] == 0: + for line in rv["Stdout"].split("\n"): + if line.find("Link encap:") != -1: + netDevice["type"] = line.split("Link encap:")[1].split()[0] + continue + if line.find("inet addr:") != -1: + tokens = line.split("inet addr:")[1].split() + netDevice["ipaddr"] = tokens[0] + #print tokens[1].split(":")[1] + netDevice["netmask"] = tokens[2].split(":")[1] + + rv = Utils.runCommand("ethtool %s" % deviceName, output=True, root=True) + if rv["Status"] == 0: + for line in rv["Stdout"].split("\n"): + if line.find("Speed: ") != -1: + netDevice["speed"] = line.split("Speed: ")[1].upper().split("MB")[0] + elif line.find("Link detected: ") != -1: + netDevice["link"] = line.split("Link detected: ")[1] + + rv = Utils.runCommand("route -n", output=True, root=True) + if rv["Status"] == 0: + for line in rv["Stdout"].split("\n"): + tokens = line.split() + if len(tokens) == 8 and tokens[-1] == deviceName and tokens[3] == "UG": + netDevice["gateway"] = tokens[1] + netDevice["mode"] = getBondMode(deviceName, root + Globals.MODPROBE_CONF_FILE) - deviceDetail = getNetDeviceDetail(deviceName) - if deviceDetail.has_key('Model'): - netDevice["model"] = deviceDetail['Model'] - else: - netDevice["model"] = None - if deviceDetail.has_key('Ip'): - netDevice["ipaddr"] = deviceDetail['Ip'] - else: - netDevice["ipaddr"] = None - if deviceDetail.has_key('Mask'): - netDevice["netmask"] = deviceDetail['Mask'] - else: - netDevice["netmask"] = None - netDevice["speed"] = getNetSpeed(deviceName) - try: - netDevice["hwaddr"] = open("/sys/class/net/%s/address" % deviceName).read().strip() - except IOError, e: - pass - - netDeviceList.append(netDevice) + + netDeviceList[deviceName] = netDevice conf = readIfcfgConfFile(deviceName, root) if not conf: continue try: + if not netDevice["ipaddr"]: + netDevice["ipaddr"] = conf["ipaddr"] + if not netDevice["netmask"]: + netDevice["netmask"] = conf["netmask"] + if not netDevice["gateway"]: + netDevice["gateway"] = conf["gateway"] netDevice["onboot"] = conf["onboot"] - except KeyError, e: - pass - try: netDevice["bootproto"] = conf["bootproto"] - except KeyError, e: - pass - if conf.has_key("ipaddr") and conf["ipaddr"]: - netDevice["ipaddr"] = conf["ipaddr"] - try: - netDevice["netmask"] = conf["netmask"] - except KeyError, e: - pass - if conf.has_key("gateway") and conf["gateway"]: - netDevice["gateway"] = conf["gateway"] - else: - netDevice["gateway"] = getNetDeviceGateway(deviceName) - try: netDevice["peerdns"] = conf["peerdns"] - except KeyError, e: - pass - try: netDevice["autodns"] = conf["autodns"] - except KeyError, e: - pass - try: netDevice["dns1"] = conf["dns1"] - except KeyError, e: - pass - try: netDevice["dns2"] = conf["dns2"] - except KeyError, e: - pass - try: netDevice["dns3"] = conf["dns3"] - except KeyError, e: - pass - try: netDevice["master"] = conf["master"] - except KeyError, e: - pass - try: netDevice["slave"] = conf["slave"] - except KeyError, e: - pass - try: netDevice["nmcontrolled"] = conf["nmcontrolled"] except KeyError, e: pass - return netDeviceList - - ## bondDevices = [os.path.basename(device) for device in glob.glob("/sys/class/net/bond*")] - - ## bondDevices = [os.path.basename(device) for device in glob.glob("/sys/class/net/bond*")] - ## for deviceName in bondDevices: - ## if deviceName in linkedBondList: - ## if deviceName in sysConfigDeviceList: - ## deviceList[deviceName] = sysConfigDeviceList[deviceName] - ## else: - ## deviceList[deviceName] = {'device':deviceName, 'onboot':'no', 'bootproto':'none'} - ## continue - ## if len(ethDevices) > 2: - ## deviceList[deviceName] = {'device':deviceName, 'onboot':'no', 'bootproto':'none'} diff --git a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_server_details.py b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_server_details.py index 5c5b4c4a..d5ffb917 100755 --- a/src/com.gluster.storage.management.gateway.scripts/src/backend/get_server_details.py +++ b/src/com.gluster.storage.management.gateway.scripts/src/backend/get_server_details.py @@ -193,39 +193,29 @@ def getServerDetails(listall): #TODO: probe and retrieve timezone, ntp-server details and update the tags - deviceList = {} interfaces = responseDom.createTag("networkInterfaces", None) - for device in getNetDeviceList(): - if device["model"] in ['LOCAL', 'IPV6-IN-IPV4']: - continue - deviceList[device["device"]] = device - try: - macAddress = open("/sys/class/net/%s/address" % device["device"]).read().strip() - except IOError, e: + for deviceName, values in getNetDeviceList().iteritems(): + if values["type"].upper() in ['LOCAL', 'IPV6-IN-IPV4']: continue interfaceTag = responseDom.createTag("networkInterface", None) - interfaceTag.appendChild(responseDom.createTag("name", device["device"])) - interfaceTag.appendChild(responseDom.createTag("hwAddr",macAddress)) - interfaceTag.appendChild(responseDom.createTag("speed", device["speed"])) - interfaceTag.appendChild(responseDom.createTag("model", device["model"])) - if deviceList[device["device"]]: - if deviceList[device["device"]]["onboot"]: - interfaceTag.appendChild(responseDom.createTag("onboot", "yes")) - else: - interfaceTag.appendChild(responseDom.createTag("onBoot", "no")) - interfaceTag.appendChild(responseDom.createTag("bootProto", deviceList[device["device"]]["bootproto"])) - interfaceTag.appendChild(responseDom.createTag("ipAddress", deviceList[device["device"]]["ipaddr"])) - interfaceTag.appendChild(responseDom.createTag("netMask", deviceList[device["device"]]["netmask"])) - interfaceTag.appendChild(responseDom.createTag("defaultGateway", deviceList[device["device"]]["gateway"])) - if deviceList[device["device"]]["mode"]: - interfaceTag.appendChild(responseDom.createTag("mode", deviceList[device["device"]]["mode"])) - if deviceList[device["device"]]["master"]: - interfaceTag.appendChild(responseDom.createTag("bonding", "yes")) - spliter = re.compile(r'[\D]') - interfaceTag.appendChild(responseDom.createTag("bondid", spliter.split(device["master"])[-1])) + interfaceTag.appendChild(responseDom.createTag("name", deviceName)) + interfaceTag.appendChild(responseDom.createTag("hwAddr", values["hwaddr"])) + interfaceTag.appendChild(responseDom.createTag("speed", values["speed"])) + interfaceTag.appendChild(responseDom.createTag("model", values["type"])) + if values["onboot"]: + interfaceTag.appendChild(responseDom.createTag("onBoot", "yes")) else: - interfaceTag.appendChild(responseDom.createTag("onBoot", "no")) - interfaceTag.appendChild(responseDom.createTag("bootProto", "none")) + interfaceTag.appendChild(responseDom.createTag("onBoot", "no")) + interfaceTag.appendChild(responseDom.createTag("bootProto", values["bootproto"])) + interfaceTag.appendChild(responseDom.createTag("ipAddress", values["ipaddr"])) + interfaceTag.appendChild(responseDom.createTag("netMask", values["netmask"])) + interfaceTag.appendChild(responseDom.createTag("defaultGateway", values["gateway"])) + if values["mode"]: + interfaceTag.appendChild(responseDom.createTag("mode", values["mode"])) + if values["master"]: + interfaceTag.appendChild(responseDom.createTag("bonding", "yes")) + spliter = re.compile(r'[\D]') + interfaceTag.appendChild(responseDom.createTag("bondid", spliter.split(values["master"])[-1])) interfaces.appendChild(interfaceTag) serverTag.appendChild(interfaces) |