def GetInfoString(): active_interfaces = ethtool.get_active_devices() all_interfaces = GetInterfaceList() for i in active_interfaces: if ethtool.get_flags('%s' % i) & ethtool.IFF_POINTOPOINT: active_interfaces.remove(i) ret = '' for inter in active_interfaces: if inter in all_interfaces: all_interfaces.remove(inter) else: continue t = 'Static' if IsInterfaceDHCP(inter): t = 'DHCP' ret = ret + ' %s - Active, %s\n' % (inter, t) ret = ret + ' IP: %s\n' % ethtool.get_ipaddr(inter) ret = ret + ' Netmask: %s\n' % ethtool.get_netmask(inter) ret = ret + ' HWAddr: %s\n' % ethtool.get_hwaddr(inter) for inter in all_interfaces: t = 'Static' if IsInterfaceDHCP(inter): t = 'DHCP' ret = ret + ' %s - Inactive, %s\n' % (inter, t) if t == 'Static': ip, mask, gw, dns = GetInterfaceConf(inter) ret = ret + ' IP: %s\n' % ip ret = ret + ' Netmask: %s\n' % mask ret = ret + ' HWAddr: %s\n' % ethtool.get_hwaddr(inter) return ret
def _functions_accepting_devnames(self, devname): self.assertIsString(devname) scraped = ifconfig.get_device_by_name(devname) try: broadcast = ethtool.get_broadcast(devname) except (OSError, IOError): # Some devices might end up with # [Errno 99] Cannot assign requested address # That's IOError on 2.7, OSError on 3 assignable = False else: assignable = True self.assertIsString(broadcast) # Broadcast is optional in ifconfig output if scraped.broadcast: self.assertEqualIpv4Str(broadcast, scraped.broadcast) self.assertIsStringExceptForLoopback( ethtool.get_businfo, devname, '[Errno 95] Operation not ' 'supported') self.assertIsInt(ethtool.get_flags(devname)) # flagsint cannot be obtained from old ifconfig format if not ifconfig.oldFormat: self.assertEqual(ethtool.get_flags(devname), scraped.flagsint) self.assertIsInt(ethtool.get_gso(devname)) self.assertIsInt(ethtool.get_gro(devname)) self.assertIsString(ethtool.get_hwaddr(devname)) self.assertEqualHwAddr(ethtool.get_hwaddr(devname), scraped.hwaddr) if assignable: self.assertIsString(ethtool.get_ipaddr(devname)) self.assertEqual(ethtool.get_ipaddr(devname), scraped.inet) self.assertIsStringExceptForLoopback( ethtool.get_module, devname, '[Errno 95] Operation not ' 'supported') if assignable: self.assertIsString(ethtool.get_netmask(devname)) self.assertEqual(ethtool.get_netmask(devname), scraped.netmask) # Operation is not supported only on loopback device if devname == 'lo': self.assertRaisesIOError(ethtool.get_ringparam, (devname, ), '[Errno 95] Operation not supported') self.assertIsInt(ethtool.get_sg(devname)) try: self.assertIsInt(ethtool.get_ufo(devname)) except (OSError, IOError): # This test may fail due to insufficient privileges # That's IOError on 2.7, OSError (PermissionError) on 3 pass self.assertIsInt(ethtool.get_tso(devname))
def get_network_info(): try: import ethtool except: raise InfoException("the python-ethtool module is required to use this feature (is your OS>=EL3?)") interfaces = {} # get names inames = ethtool.get_devices() for iname in inames: mac = ethtool.get_hwaddr(iname) if mac == "00:00:00:00:00:00": mac = "?" try: ip = ethtool.get_ipaddr(iname) if ip == "127.0.0.1": ip = "?" except: ip = "?" bridge = 0 module = "" try: nm = ethtool.get_netmask(iname) except: nm = "?" interfaces[iname] = {"ip_address": ip, "mac_address": mac, "netmask": nm, "bridge": bridge, "module": module} # print interfaces return interfaces
def processInfo(self): """ Extracts info from the screen, and puts it into a device object """ self.dev.DeviceId = self.name.value() self.dev.Device = self.hwdev.value() if self.dynip.value(): self.dev.BootProto = "dhcp" self.dev.AutoDNS = True self.dev.IP = None self.dev.Netmask = None self.dev.Gateway = None else: self.dev.IP = self.statip.value() self.dev.Netmask = self.netmask.value() self.dev.Gateway = self.gwy.value() self.dev.BootProto = None try: hwaddr = ethtool.get_hwaddr(self.dev.Device) except IOError: pass else: self.dev.HardwareAddress = hwaddr dnsstr = self.dnsserver1.value() if dnsstr: self.dev.PrimaryDNS = dnsstr dnsstr = self.dnsserver2.value() if dnsstr: self.dev.SecondaryDNS = dnsstr if self.dev.BootProto == None: self.dev.AutoDNS = None
def get_network_info(): interfaces = {} # get names inames = ethtool.get_devices() for iname in inames: mac = ethtool.get_hwaddr(iname) if mac == "00:00:00:00:00:00": mac = "?" try: ip = ethtool.get_ipaddr(iname) if ip == "127.0.0.1": ip = "?" except: ip = "?" bridge = 0 module = "" try: nm = ethtool.get_netmask(iname) except: nm = "?" interfaces[iname] = {"ip_address": ip, "mac_address": mac, "netmask": nm, "bridge": bridge, "module": module} # print interfaces return interfaces
def on_hwProbeButton_clicked(self, button): # pylint: disable-msg=W0613 hw = self.xml.get_widget("ethernetDeviceEntry").get_text() fields = hw.split() device = fields[0] try: hwaddr = ethtool.get_hwaddr(device) except IOError, err: error_str = str(err) GUI_functions.gui_error_dialog(error_str, self.dialog)
def get_nic_name_from_mac(self, mac): if not mac: return "name-error" temp_path = local.path("/sys/class/net") for i in temp_path: temp_mac = ethtool.get_hwaddr(str(i.name)) if temp_mac == mac: return i.name return "name-error"
def on_hardwareProbeButton_clicked(widget, xml, device): # pylint: disable-msg=W0613 omenu = xml.get_widget("hwdvOmenu") hw = omenu.get_children()[0].get() device = hw.split()[0] try: hwaddr = ethtool.get_hwaddr(device) except IOError, err: error_str = str(err) GUI_functions.gui_error_dialog(error_str, omenu.get_toplevel())
def on_finish_page_prepare(self, druid_page, druid): self.device.DeviceId = self.device.Device if self.device.Alias: self.device.DeviceId = self.device.DeviceId + ":" \ + str(self.device.Alias) try: hwaddr = ethtool.get_hwaddr(self.device.Device) except IOError, err: pass
def get_network_info(): try: # python 2 import ethtool ethtool_available = True except ImportError: # python 3 import netifaces ethtool_available = False interfaces = {} # get names if ethtool_available: inames = ethtool.get_devices() else: inames = netifaces.interfaces() for iname in inames: if ethtool_available: mac = ethtool.get_hwaddr(iname) else: mac = netifaces.ifaddresses(iname)[netifaces.AF_LINK][0]['addr'] if mac == "00:00:00:00:00:00": mac = "?" try: if ethtool_available: ip = ethtool.get_ipaddr(iname) else: ip = netifaces.ifaddresses(iname)[netifaces.AF_INET][0]['addr'] if ip == "127.0.0.1": ip = "?" except: ip = "?" bridge = 0 module = "" try: if ethtool_available: nm = ethtool.get_netmask(iname) else: nm = netifaces.ifaddresses(iname)[netifaces.AF_INET][0]['netmask'] except: nm = "?" interfaces[iname] = { "ip_address" : ip, "mac_address" : mac, "netmask" : nm, "bridge" : bridge, "module" : module } # print(interfaces) return interfaces
def _functions_accepting_devnames(self, devname): self.assertIsString(devname) scraped = ifconfig.get_device_by_name(devname) self.assertIsString(ethtool.get_broadcast(devname)) self.assertEqualIpv4Str(ethtool.get_broadcast(devname), scraped.broadcast) self.assertIsStringExceptForLoopback( ethtool.get_businfo, devname, '[Errno 95] Operation not supported') # ethtool.get_coalesce(devname) # this gives me: # IOError: [Errno 95] Operation not supported # on my test box self.assertIsInt(ethtool.get_flags(devname)) # flagsint cannot be obtained from old ifconfig format if not ifconfig.oldFormat: self.assertEqual(ethtool.get_flags(devname), scraped.flagsint) self.assertIsInt(ethtool.get_gso(devname)) self.assertIsString(ethtool.get_hwaddr(devname)) self.assertEqualHwAddr(ethtool.get_hwaddr(devname), scraped.hwaddr) self.assertIsString(ethtool.get_ipaddr(devname)) self.assertEqual(ethtool.get_ipaddr(devname), scraped.inet) self.assertIsStringExceptForLoopback( ethtool.get_module, devname, '[Errno 95] Operation not supported') self.assertIsString(ethtool.get_netmask(devname)) self.assertEqual(ethtool.get_netmask(devname), scraped.netmask) #self.assertRaisesIOError(ethtool.get_ringparam, (devname, ), # '[Errno 95] Operation not supported') # Disabling until BZ#703089 is investigated #self.assertIsInt(ethtool.get_sg(devname)) #self.assertIsInt(ethtool.get_ufo(devname)) self.assertIsInt(ethtool.get_tso(devname))
def get_all_interfaces(): import ethtool # Not sure how robust this will be; need to test with real gear. # In theory, should do the job to exclude IPoIB and lo interfaces. hwaddr_blacklist = ['00:00:00:00:00:00', '80:00:00:48:fe:80'] eth_interfaces = [] for device in ethtool.get_devices(): if ethtool.get_hwaddr(device) not in hwaddr_blacklist: eth_interfaces.append(CorosyncRingInterface(device)) return eth_interfaces
def get_addrinfo(self,param): addr={} try: act_device = ethtool.get_active_devices() if "lo" in act_device: act_device.remove("lo") for i in act_device: mac = ethtool.get_hwaddr(i) addr[mac] = ethtool.get_ipaddr(i) except OSError , e: Logger.error("Get addr information failed: %s" % str(e)) return
def _functions_accepting_devnames(self, devname): self.assertIsString(devname) scraped = ifconfig.get_device_by_name(devname) self.assertIsString(ethtool.get_broadcast(devname)) self.assertEqualIpv4Str(ethtool.get_broadcast(devname), scraped.broadcast) self.assertIsStringExceptForLoopback(ethtool.get_businfo, devname, "[Errno 95] Operation not supported") # ethtool.get_coalesce(devname) # this gives me: # IOError: [Errno 95] Operation not supported # on my test box self.assertIsInt(ethtool.get_flags(devname)) # flagsint cannot be obtained from old ifconfig format if not ifconfig.oldFormat: self.assertEqual(ethtool.get_flags(devname), scraped.flagsint) self.assertIsInt(ethtool.get_gso(devname)) self.assertIsString(ethtool.get_hwaddr(devname)) self.assertEqualHwAddr(ethtool.get_hwaddr(devname), scraped.hwaddr) self.assertIsString(ethtool.get_ipaddr(devname)) self.assertEqual(ethtool.get_ipaddr(devname), scraped.inet) self.assertIsStringExceptForLoopback(ethtool.get_module, devname, "[Errno 95] Operation not supported") self.assertIsString(ethtool.get_netmask(devname)) self.assertEqual(ethtool.get_netmask(devname), scraped.netmask) # self.assertRaisesIOError(ethtool.get_ringparam, (devname, ), # '[Errno 95] Operation not supported') # Disabling until BZ#703089 is investigated # self.assertIsInt(ethtool.get_sg(devname)) # self.assertIsInt(ethtool.get_ufo(devname)) self.assertIsInt(ethtool.get_tso(devname))
def show_config(device): ipaddr = ethtool.get_ipaddr(device) netmask = ethtool.get_netmask(device) flags = ethtool.get_flags(device) print '%-9.9s' % device, if not (flags & ethtool.IFF_LOOPBACK): print "HWaddr %s" % ethtool.get_hwaddr(device), print ''' inet addr:%s''' % ipaddr, if not (flags & (ethtool.IFF_LOOPBACK | ethtool.IFF_POINTOPOINT)): print "Bcast:%s" % ethtool.get_broadcast(device), print ''' Mask:%s %s ''' % (netmask, flags2str(flags))
def getAllNetworkInterfaces(self): interfaces = list() try: for dev in ethtool.get_active_devices(): flags = ethtool.get_flags(dev) if not(flags & ethtool.IFF_LOOPBACK): devinfo = ethtool.get_interfaces_info(dev)[0] interfaces.append({ 'name' : dev, 'inet' : [ ethtool.get_ipaddr(dev) ], 'inet6' : map(lambda ip: ip.address, devinfo.get_ipv6_addresses()), 'hw' : ethtool.get_hwaddr(dev) }) except: logging.exception("Error retrieving network interfaces.") return interfaces
def get_network_info(): interfaces = {} # get names if ethtool_available: inames = ethtool.get_devices() else: inames = netifaces.interfaces() for iname in inames: if ethtool_available: mac = ethtool.get_hwaddr(iname) else: mac = netifaces.ifaddresses(iname)[netifaces.AF_LINK][0]['addr'] if mac == "00:00:00:00:00:00": mac = "?" try: if ethtool_available: ip = ethtool.get_ipaddr(iname) else: ip = netifaces.ifaddresses(iname)[netifaces.AF_INET][0]['addr'] if ip == "127.0.0.1": ip = "?" except: ip = "?" bridge = 0 module = "" try: if ethtool_available: nm = ethtool.get_netmask(iname) else: nm = netifaces.ifaddresses(iname)[netifaces.AF_INET][0]['netmask'] except: nm = "?" interfaces[iname] = { "ip_address": ip, "mac_address": mac, "netmask": nm, "bridge": bridge, "module": module } # print interfaces return interfaces
def get_network_info(): try: import ethtool except: try: import rhpl.ethtool ethtool = rhpl.ethtool except: raise InfoException( "the rhpl or ethtool module is required to use this feature (is your OS>=EL3?)" ) interfaces = {} # get names inames = ethtool.get_devices() for iname in inames: mac = ethtool.get_hwaddr(iname) if mac == "00:00:00:00:00:00": mac = "?" try: ip = ethtool.get_ipaddr(iname) if ip == "127.0.0.1": ip = "?" except: ip = "?" bridge = 0 module = "" try: nm = ethtool.get_netmask(iname) except: nm = "?" interfaces[iname] = { "ip_address": ip, "mac_address": mac, "netmask": nm, "bridge": bridge, "module": module } # print interfaces return interfaces
def show_config(device): ipaddr = ethtool.get_ipaddr(device) netmask = ethtool.get_netmask(device) flags = ethtool.get_flags(device) print '%-9.9s' % device, if not (flags & ethtool.IFF_LOOPBACK): print "HWaddr %s" % ethtool.get_hwaddr(device), print ''' inet addr:%s''' % ipaddr, if not (flags & (ethtool.IFF_LOOPBACK | ethtool.IFF_POINTOPOINT)): print "Bcast:%s" % ethtool.get_broadcast(device), print ' Mask:%s' % netmask for info in ethtool.get_interfaces_info(device): for addr in info.get_ipv6_addresses(): print (" inet6 addr: %s/%s Scope: %s" % (addr.address, addr.netmask, addr.scope)) print ' %s' % flags2str(flags) print
def getAvailableInterfaces(liveDVD=True): """ Returns all the network interfaces (eth/en[pP]) available on the system @type liveDVD: boolean @param liveDVD: True is running in liveDVD @rtype: dict @returns: (key = ethernet interface / value = mac addresss) available """ interfaces = {} devices = ethtool.get_devices() virtualIfaces = Network.getVirtualNic() for dev in devices: if dev not in virtualIfaces: if liveDVD: device = Network.getUdevPredictableName(dev) else: device = dev interfaces[device] = ethtool.get_hwaddr(dev) return interfaces
def getDeviceType(devname, module=None): if devname in deviceTypes: return devname if module == "qeth": return QETH if module == "lcs": return LCS UNKNOWN = _('Unknown') mtype = UNKNOWN if not devname or devname == "": return mtype for i in __deviceTypeDict.keys(): if re.search(i, devname): mtype = __deviceTypeDict[i] if mtype == UNKNOWN: try: # if still unknown, try to get a MAC address hwaddr = ethtool.get_hwaddr(devname) if hwaddr: mtype = ETHERNET except: pass if mtype == ETHERNET: try: import iwlib # test for wireless iwlib.get_iwconfig(devname) mtype = WIRELESS except: pass return mtype
def on_finish_page_prepare(self, druid_page, druid): # pylint: disable-msg=W0613 self.device.DeviceId = self.device.Device if self.device.Alias: self.device.DeviceId = self.device.DeviceId + ":" \ + str(self.device.Alias) try: hwaddr = ethtool.get_hwaddr(self.device.Device) except IOError: pass else: self.device.HardwareAddress = hwaddr # FIXME: Bad style for translation s = str( _("You have selected the following information:") + "\n\n" + " " + _("Device:") + " " + str(self.device.DeviceId) + " ") hardwarelist = NCHardwareList.getHardwareList() for hw in hardwarelist: if hw.Name == self.device.Device: s = s + "(" + hw.Description + ")" break s = s + "\n" + " " if self.device.BootProto == "static": s = s + str( _("Address:") + " " + self.device.IP + "\n" + " " + _("Subnet mask:") + " " + self.device.Netmask + "\n" + " " + _("Default gateway address:") + " " + self.device.Gateway + "\n") else: s = s + str( _("Automatically obtain IP address settings with:") + " " + self.device.BootProto + "\n") druid_page.set_text(s)
def get_network_info(): interfaces = {} # get names inames = ethtool.get_devices() for iname in inames: mac = ethtool.get_hwaddr(iname) if mac == "00:00:00:00:00:00": mac = "?" try: ip = ethtool.get_ipaddr(iname) if ip == "127.0.0.1": ip = "?" except: ip = "?" bridge = 0 module = "" try: nm = ethtool.get_netmask(iname) except: nm = "?" interfaces[iname] = { "ip_address": ip, "mac_address": mac, "netmask": nm, "bridge": bridge, "module": module } # print interfaces return interfaces
__author__ = 'yippee' import ethtool interfaces = [] for dev in ethtool.get_devices(): flags = ethtool.get_flags(dev) if flags & ethtool.IFF_UP and \ not (flags & ethtool.IFF_LOOPBACK): # print dev, flags dev_info = ethtool.get_interfaces_info(dev)[0] # print dev_info.get_ipv4_addresses() interfaces.append( {'name': dev, 'inet': ethtool.get_ipv4_addresses(dev_info), 'inet6': ethtool.get_ipv6_addresses(dev_info), 'hw': ethtool.get_hwaddr(dev)}) import pprint pprint(interfaces)
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" if not ethtool_present and not netifaces_present: # ethtool is not available on non-linux platforms (as kfreebsd), skip it sys.stderr.write( "Warning: information about network interfaces could not be retrieved on this platform.\n" ) return intDict if ethtool_present: interfaces = list( set(ethtool.get_devices() + ethtool.get_active_devices())) else: interfaces = netifaces.interfaces() for interface in interfaces: try: if ethtool_present: hwaddr = ethtool.get_hwaddr(interface) else: hwaddr = netifaces.ifaddresses(interface)[ netifaces.AF_LINK][0]['addr'] except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: if ethtool_present: module = ethtool.get_module(interface) else: driver_file = open( '/sys/class/net/%s/device/uevent' % interface, 'r') module = driver_file.readline().split('=')[1].strip() driver_file.close() except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: if ethtool_present: ipaddr = ethtool.get_ipaddr(interface) else: ipaddr = netifaces.ifaddresses(interface)[ netifaces.AF_INET][0]['addr'] except: ipaddr = "" try: if ethtool_present: netmask = ethtool.get_netmask(interface) else: netmask = netifaces.ifaddresses(interface)[ netifaces.AF_INET][0]['netmask'] except: netmask = "" try: if ethtool_present: broadcast = ethtool.get_broadcast(interface) else: broadcast = netifaces.ifaddresses(interface)[ netifaces.AF_INET][0]['broadcast'] except: broadcast = "" ip6_list = [] if ethtool_present: dev_info = ethtool.get_interfaces_info(interface) for info in dev_info: # one interface may have more IPv6 addresses for ip6 in info.get_ipv6_addresses(): scope = ip6.scope if scope == 'global': scope = 'universe' ip6_list.append({ 'scope': scope, 'addr': ip6.address, 'netmask': ip6.netmask }) else: try: for dev_info in netifaces.ifaddresses(interface)[ netifaces.AF_INET6]: ip6_addr = dev_info['addr'].split('%')[0] scope_info = ipaddress.IPv6Address(ip6_addr) if scope_info.is_global: scope = 'universe' elif scope_info.is_link_local: scope = 'link' elif scope_info.is_loopback: scope = 'host' elif scope_info.is_site_local: scope = 'site' # count number of '1' bits in netmask returned by netifaces ip6_netmask = dev_info['netmask'] netmask_bits = 0 for two_octets in ip6_netmask.split(':'): if not two_octets: break elif two_octets.lower() == 'ffff': netmask_bits += 16 else: # remove '0b' from begin and find last '1' in the string netmask_bits += 1 + bin( int(two_octets.split('/')[0], 16))[2:].rindex('1') ip6_list.append({ 'scope': scope, 'addr': ip6_addr, 'netmask': netmask_bits }) except KeyError: pass # no ipv6 for this interface intDict[interface] = { 'hwaddr': hwaddr, 'ipaddr': ipaddr, 'netmask': netmask, 'broadcast': broadcast, 'module': module, 'ipv6': ip6_list } return intDict
def genxml(self, duration, accum, samples, xslt=None): seconds = duration.seconds hours = seconds / 3600 if hours: seconds -= hours * 3600 minutes = seconds / 60 if minutes: seconds -= minutes * 60 (sys, node, release, ver, machine) = os.uname() # Start new XML report self.xmlreport = xmlout.XMLOut("rteval", self.version) self.xmlreport.NewReport() self.xmlreport.openblock( "run_info", {"days": duration.days, "hours": hours, "minutes": minutes, "seconds": seconds} ) self.xmlreport.taggedvalue("date", self.start.strftime("%Y-%m-%d")) self.xmlreport.taggedvalue("time", self.start.strftime("%H:%M:%S")) if self.annotate: self.xmlreport.taggedvalue("annotate", self.annotate) self.xmlreport.closeblock() self.xmlreport.openblock("uname") self.xmlreport.taggedvalue("node", node) isrt = 1 if ver.find(" RT ") == -1: isrt = 0 self.xmlreport.taggedvalue("kernel", release, {"is_RT": isrt}) self.xmlreport.taggedvalue("arch", machine) self.xmlreport.taggedvalue("baseos", self.baseos) self.xmlreport.closeblock() self.xmlreport.openblock("clocksource") self.xmlreport.taggedvalue("current", self.current_clocksource) self.xmlreport.taggedvalue("available", self.available_clocksource) self.xmlreport.closeblock() self.xmlreport.openblock("hardware") self.xmlreport.AppendXMLnodes(self.cputopology) self.xmlreport.taggedvalue("numa_nodes", self.numanodes) self.xmlreport.taggedvalue("memory_size", "%.3f" % self.memsize[0], {"unit": self.memsize[1]}) self.xmlreport.closeblock() self.xmlreport.openblock("services") for s in self.services: self.xmlreport.taggedvalue("service", self.services[s], {"name": s}) self.xmlreport.closeblock() keys = self.kthreads.keys() if len(keys): keys.sort() self.xmlreport.openblock("kthreads") for pid in keys: self.xmlreport.taggedvalue( "thread", self.kthreads[pid]["name"], {"policy": self.kthreads[pid]["policy"], "priority": self.kthreads[pid]["priority"]}, ) self.xmlreport.closeblock() modlist = util.get_modules() if len(modlist): self.xmlreport.openblock("kernelmodules") for mod in modlist: self.xmlreport.openblock("module") self.xmlreport.taggedvalue( "info", mod["modname"], {"size": mod["modsize"], "state": mod["modstate"], "numusers": mod["numusers"]}, ) if mod["usedby"] != "-": self.xmlreport.openblock("usedby") for ub in mod["usedby"].split(","): if len(ub): self.xmlreport.taggedvalue("module", ub, None) self.xmlreport.closeblock() self.xmlreport.closeblock() self.xmlreport.closeblock() # # Retrieve configured IP addresses # self.xmlreport.openblock("network_config") # Get the interface name for the IPv4 default gw route = open("/proc/net/route") defgw4 = None if route: rl = route.readline() while rl != "": rl = route.readline() splt = rl.split("\t") if len(splt) > 2 and splt[2] != "00000000": # Only catch default route defgw4 = splt[0] break route.close() # Get lists over all devices, remove loopback device ifdevs = ethtool.get_active_devices() ifdevs.remove("lo") ifdevs.sort() # Make an interface tag for each device found for dev in ifdevs: self.xmlreport.openblock("interface", {"device": dev, "hwaddr": ethtool.get_hwaddr(dev)}) # Protcol configurations self.xmlreport.openblock( "IPv4", { "ipaddr": ethtool.get_ipaddr(dev), "netmask": ethtool.get_netmask(dev), "defaultgw": (defgw4 == dev) and "1" or "0", }, ) self.xmlreport.closeblock() self.xmlreport.closeblock() self.xmlreport.closeblock() self.xmlreport.openblock("loads", {"load_average": str(accum / samples)}) for load in self.loads: load.genxml(self.xmlreport) self.xmlreport.closeblock() self.cyclictest.genxml(self.xmlreport) # now generate the dmidecode data for this host d = dmi.DMIinfo(self.config.GetSection("rteval")) d.genxml(self.xmlreport) # Close the report - prepare for return the result self.xmlreport.close() # Write XML (or write XSLT parsed XML if xslt != None) if self.xml != None: self.xmlreport.Write(self.xml, xslt) else: # If no file is set, use stdout self.xmlreport.Write("-", xslt) # libxml2 defines a filename as "-" to be stdout
def genxml(self, duration, accum, samples, xslt=None): seconds = duration.seconds hours = seconds / 3600 if hours: seconds -= (hours * 3600) minutes = seconds / 60 if minutes: seconds -= (minutes * 60) (sys, node, release, ver, machine) = os.uname() # Start new XML report self.xmlreport = xmlout.XMLOut('rteval', self.version) self.xmlreport.NewReport() self.xmlreport.openblock( 'run_info', { 'days': duration.days, 'hours': hours, 'minutes': minutes, 'seconds': seconds }) self.xmlreport.taggedvalue('date', self.start.strftime('%Y-%m-%d')) self.xmlreport.taggedvalue('time', self.start.strftime('%H:%M:%S')) if self.annotate: self.xmlreport.taggedvalue('annotate', self.annotate) self.xmlreport.closeblock() self.xmlreport.openblock('uname') self.xmlreport.taggedvalue('node', node) isrt = 1 if ver.find(' RT ') == -1: isrt = 0 self.xmlreport.taggedvalue('kernel', release, {'is_RT': isrt}) self.xmlreport.taggedvalue('arch', machine) self.xmlreport.taggedvalue('baseos', self.baseos) self.xmlreport.closeblock() self.xmlreport.openblock("clocksource") self.xmlreport.taggedvalue('current', self.current_clocksource) self.xmlreport.taggedvalue('available', self.available_clocksource) self.xmlreport.closeblock() self.xmlreport.openblock('hardware') self.xmlreport.AppendXMLnodes(self.cputopology) self.xmlreport.taggedvalue('numa_nodes', self.numanodes) self.xmlreport.taggedvalue('memory_size', "%.3f" % self.memsize[0], {"unit": self.memsize[1]}) self.xmlreport.closeblock() self.xmlreport.openblock('services', {'init': self.init}) for s in self.services: self.xmlreport.taggedvalue("service", self.services[s], {"name": s}) self.xmlreport.closeblock() keys = self.kthreads.keys() if len(keys): keys.sort() self.xmlreport.openblock('kthreads') for pid in keys: self.xmlreport.taggedvalue( 'thread', self.kthreads[pid]['name'], { 'policy': self.kthreads[pid]['policy'], 'priority': self.kthreads[pid]['priority'], }) self.xmlreport.closeblock() modlist = util.get_modules() if len(modlist): self.xmlreport.openblock('kernelmodules') for mod in modlist: self.xmlreport.openblock('module') self.xmlreport.taggedvalue( 'info', mod['modname'], { 'size': mod['modsize'], 'state': mod['modstate'], 'numusers': mod['numusers'] }) if mod['usedby'] != '-': self.xmlreport.openblock('usedby') for ub in mod['usedby'].split(','): if len(ub): self.xmlreport.taggedvalue('module', ub, None) self.xmlreport.closeblock() self.xmlreport.closeblock() self.xmlreport.closeblock() # # Retrieve configured IP addresses # self.xmlreport.openblock('network_config') # Get the interface name for the IPv4 default gw route = open('/proc/net/route') defgw4 = None if route: rl = route.readline() while rl != '': rl = route.readline() splt = rl.split("\t") # Only catch default route if len(splt) > 2 and splt[2] != '00000000' and splt[ 1] == '00000000': defgw4 = splt[0] break route.close() # Make an interface tag for each device found if hasattr(ethtool, 'get_interfaces_info'): # Using the newer python-ethtool API (version >= 0.4) for dev in ethtool.get_interfaces_info(ethtool.get_devices()): if cmp(dev.device, 'lo') == 0: continue self.xmlreport.openblock('interface', { 'device': dev.device, 'hwaddr': dev.mac_address }) # Protcol configurations if dev.ipv4_address: self.xmlreport.openblock( 'IPv4', { 'ipaddr': dev.ipv4_address, 'netmask': dev.ipv4_netmask, 'broadcast': dev.ipv4_broadcast, 'defaultgw': (defgw4 == dev.device) and '1' or '0' }) self.xmlreport.closeblock() for ip6 in dev.get_ipv6_addresses(): self.xmlreport.openblock( 'IPv6', { 'ipaddr': ip6.address, 'netmask': ip6.netmask, 'scope': ip6.scope }) self.xmlreport.closeblock() self.xmlreport.closeblock() else: # Fall back to older python-ethtool API (version < 0.4) ifdevs = ethtool.get_active_devices() ifdevs.remove('lo') ifdevs.sort() for dev in ifdevs: self.xmlreport.openblock('interface', { 'device': dev, 'hwaddr': ethtool.get_hwaddr(dev) }) self.xmlreport.openblock( 'IPv4', { 'ipaddr': ethtool.get_ipaddr(dev), 'netmask': ethtool.get_netmask(dev), 'defaultgw': (defgw4 == dev) and '1' or '0' }) self.xmlreport.closeblock() self.xmlreport.closeblock() self.xmlreport.closeblock() self.xmlreport.openblock('loads', {'load_average': str(accum / samples)}) for load in self.loads: load.genxml(self.xmlreport) self.xmlreport.closeblock() self.cyclictest.genxml(self.xmlreport) # now generate the dmidecode data for this host d = dmi.DMIinfo(self.config.GetSection('rteval')) d.genxml(self.xmlreport) # Close the report - prepare for return the result self.xmlreport.close() # Write XML (or write XSLT parsed XML if xslt != None) if self.xml != None: self.xmlreport.Write(self.xml, xslt) else: # If no file is set, use stdout self.xmlreport.Write( "-", xslt) # libxml2 defines a filename as "-" to be stdout
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" if not ethtool_present and not netifaces_present: # ethtool is not available on non-linux platforms (as kfreebsd), skip it sys.stderr.write("Warning: information about network interfaces could not be retrieved on this platform.\n") return intDict if ethtool_present: interfaces = list(set(ethtool.get_devices() + ethtool.get_active_devices())) else: interfaces = netifaces.interfaces() for interface in interfaces: try: if ethtool_present: hwaddr = ethtool.get_hwaddr(interface) else: hwaddr = netifaces.ifaddresses(interface)[netifaces.AF_LINK][0]['addr'] except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: if ethtool_present: module = ethtool.get_module(interface) else: driver_file = open('/sys/class/net/%s/device/uevent' % interface, 'r') module = driver_file.readline().split('=')[1].strip() driver_file.close() except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: if ethtool_present: ipaddr = ethtool.get_ipaddr(interface) else: ipaddr = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'] except: ipaddr = "" try: if ethtool_present: netmask = ethtool.get_netmask(interface) else: netmask = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['netmask'] except: netmask = "" try: if ethtool_present: broadcast = ethtool.get_broadcast(interface) else: broadcast = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['broadcast'] except: broadcast = "" ip6_list = [] if ethtool_present: dev_info = ethtool.get_interfaces_info(interface) for info in dev_info: # one interface may have more IPv6 addresses for ip6 in info.get_ipv6_addresses(): scope = ip6.scope if scope == 'global': scope = 'universe' ip6_list.append({ 'scope': scope, 'addr': ip6.address, 'netmask': ip6.netmask }) else: try: for dev_info in netifaces.ifaddresses(interface)[netifaces.AF_INET6]: ip6_addr = dev_info['addr'].split('%')[0] scope_info = ipaddress.IPv6Address(ip6_addr) if scope_info.is_global: scope = 'universe' elif scope_info.is_link_local: scope = 'link' elif scope_info.is_loopback: scope = 'host' elif scope_info.is_site_local: scope = 'site' # count number of '1' bits in netmask returned by netifaces ip6_netmask = dev_info['netmask'] netmask_bits = 0 for two_octets in ip6_netmask.split(':'): if not two_octets: break elif two_octets.lower() == 'ffff': netmask_bits += 16 else: # remove '0b' from begin and find last '1' in the string netmask_bits += 1 + bin(int(two_octets.split('/')[0], 16))[2:].rindex('1') ip6_list.append({ 'scope': scope, 'addr': ip6_addr, 'netmask': netmask_bits }) except KeyError: pass # no ipv6 for this interface intDict[interface] = {'hwaddr': hwaddr, 'ipaddr': ipaddr, 'netmask': netmask, 'broadcast': broadcast, 'module': module, 'ipv6': ip6_list} return intDict
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" if not ethtool_present: # ethtool is not available on non-linux platforms (as kfreebsd), skip it return intDict interfaces = list(set(ethtool.get_devices() + ethtool.get_active_devices())) for interface in interfaces: try: hwaddr = ethtool.get_hwaddr(interface) except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: module = ethtool.get_module(interface) except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: ipaddr = ethtool.get_ipaddr(interface) except: ipaddr = "" try: netmask = ethtool.get_netmask(interface) except: netmask = "" try: broadcast = ethtool.get_broadcast(interface) except: broadcast = "" ip6_list = [] dev_info = ethtool.get_interfaces_info(interface) for info in dev_info: # one interface may have more IPv6 addresses for ip6 in info.get_ipv6_addresses(): scope = ip6.scope if scope == 'global': scope = 'universe' ip6_list.append({ 'scope': scope, 'addr': ip6.address, 'netmask': ip6.netmask }) intDict[interface] = {'hwaddr':hwaddr, 'ipaddr':ipaddr, 'netmask':netmask, 'broadcast':broadcast, 'module': module, 'ipv6': ip6_list} return intDict
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" if not ethtool_present: # ethtool is not available on non-linux platforms (as kfreebsd), skip it return intDict interfaces = list(set(ethtool.get_devices() + ethtool.get_active_devices())) for interface in interfaces: try: hwaddr = ethtool.get_hwaddr(interface) except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: module = ethtool.get_module(interface) except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: ipaddr = ethtool.get_ipaddr(interface) except: ipaddr = "" try: netmask = ethtool.get_netmask(interface) except: netmask = "" try: broadcast = ethtool.get_broadcast(interface) except: broadcast = "" ip6_list = [] dev_info = ethtool.get_interfaces_info(interface) for info in dev_info: # one interface may have more IPv6 addresses for ip6 in info.get_ipv6_addresses(): scope = ip6.scope if scope == 'global': scope = 'universe' ip6_list.append({ 'scope': scope, 'addr': ip6.address, 'netmask': ip6.netmask }) intDict[interface] = { 'hwaddr': hwaddr, 'ipaddr': ipaddr, 'netmask': netmask, 'broadcast': broadcast, 'module': module, 'ipv6': ip6_list } return intDict
def get_mac_from_name(self, name): if name: return ethtool.get_hwaddr(name) else: return ""
def genxml(self, duration, accum, samples, xslt = None): seconds = duration.seconds hours = seconds / 3600 if hours: seconds -= (hours * 3600) minutes = seconds / 60 if minutes: seconds -= (minutes * 60) (sys, node, release, ver, machine) = os.uname() # Start new XML report self.xmlreport = xmlout.XMLOut('rteval', self.version) self.xmlreport.NewReport() self.xmlreport.openblock('run_info', {'days': duration.days, 'hours': hours, 'minutes': minutes, 'seconds': seconds}) self.xmlreport.taggedvalue('date', self.start.strftime('%Y-%m-%d')) self.xmlreport.taggedvalue('time', self.start.strftime('%H:%M:%S')) if self.annotate: self.xmlreport.taggedvalue('annotate', self.annotate) self.xmlreport.closeblock() self.xmlreport.openblock('uname') self.xmlreport.taggedvalue('node', node) isrt = 1 if ver.find(' RT ') == -1: isrt = 0 self.xmlreport.taggedvalue('kernel', release, {'is_RT':isrt}) self.xmlreport.taggedvalue('arch', machine) self.xmlreport.taggedvalue('baseos', self.baseos) self.xmlreport.closeblock() self.xmlreport.openblock("clocksource") self.xmlreport.taggedvalue('current', self.current_clocksource) self.xmlreport.taggedvalue('available', self.available_clocksource) self.xmlreport.closeblock() self.xmlreport.openblock('hardware') self.xmlreport.AppendXMLnodes(self.cputopology) self.xmlreport.taggedvalue('numa_nodes', self.numanodes) self.xmlreport.taggedvalue('memory_size', "%.3f" % self.memsize[0], {"unit": self.memsize[1]}) self.xmlreport.closeblock() self.xmlreport.openblock('services', {'init': self.init}) for s in self.services: self.xmlreport.taggedvalue("service", self.services[s], {"name": s}) self.xmlreport.closeblock() keys = self.kthreads.keys() if len(keys): keys.sort() self.xmlreport.openblock('kthreads') for pid in keys: self.xmlreport.taggedvalue('thread', self.kthreads[pid]['name'], { 'policy' : self.kthreads[pid]['policy'], 'priority' : self.kthreads[pid]['priority'], }) self.xmlreport.closeblock() modlist = util.get_modules() if len(modlist): self.xmlreport.openblock('kernelmodules') for mod in modlist: self.xmlreport.openblock('module') self.xmlreport.taggedvalue('info', mod['modname'], {'size': mod['modsize'], 'state': mod['modstate'], 'numusers': mod['numusers']}) if mod['usedby'] != '-': self.xmlreport.openblock('usedby') for ub in mod['usedby'].split(','): if len(ub): self.xmlreport.taggedvalue('module', ub, None) self.xmlreport.closeblock() self.xmlreport.closeblock() self.xmlreport.closeblock() # # Retrieve configured IP addresses # self.xmlreport.openblock('network_config') # Get the interface name for the IPv4 default gw route = open('/proc/net/route') defgw4 = None if route: rl = route.readline() while rl != '' : rl = route.readline() splt = rl.split("\t") # Only catch default route if len(splt) > 2 and splt[2] != '00000000' and splt[1] == '00000000': defgw4 = splt[0] break route.close() # Make an interface tag for each device found if hasattr(ethtool, 'get_interfaces_info'): # Using the newer python-ethtool API (version >= 0.4) for dev in ethtool.get_interfaces_info(ethtool.get_devices()): if cmp(dev.device,'lo') == 0: continue self.xmlreport.openblock('interface', {'device': dev.device, 'hwaddr': dev.mac_address} ) # Protcol configurations if dev.ipv4_address: self.xmlreport.openblock('IPv4', {'ipaddr': dev.ipv4_address, 'netmask': dev.ipv4_netmask, 'broadcast': dev.ipv4_broadcast, 'defaultgw': (defgw4 == dev.device) and '1' or '0'} ) self.xmlreport.closeblock() for ip6 in dev.get_ipv6_addresses(): self.xmlreport.openblock('IPv6', {'ipaddr': ip6.address, 'netmask': ip6.netmask, 'scope': ip6.scope} ) self.xmlreport.closeblock() self.xmlreport.closeblock() else: # Fall back to older python-ethtool API (version < 0.4) ifdevs = ethtool.get_active_devices() ifdevs.remove('lo') ifdevs.sort() for dev in ifdevs: self.xmlreport.openblock('interface', {'device': dev, 'hwaddr': ethtool.get_hwaddr(dev)} ) self.xmlreport.openblock('IPv4', {'ipaddr': ethtool.get_ipaddr(dev), 'netmask': ethtool.get_netmask(dev), 'defaultgw': (defgw4 == dev) and '1' or '0'} ) self.xmlreport.closeblock() self.xmlreport.closeblock() self.xmlreport.closeblock() self.xmlreport.openblock('loads', {'load_average':str(accum / samples)}) for load in self.loads: load.genxml(self.xmlreport) self.xmlreport.closeblock() self.cyclictest.genxml(self.xmlreport) # now generate the dmidecode data for this host d = dmi.DMIinfo(self.config.GetSection('rteval')) d.genxml(self.xmlreport) # Close the report - prepare for return the result self.xmlreport.close() # Write XML (or write XSLT parsed XML if xslt != None) if self.xml != None: self.xmlreport.Write(self.xml, xslt) else: # If no file is set, use stdout self.xmlreport.Write("-", xslt) # libxml2 defines a filename as "-" to be stdout
def read_network_interfaces(): intDict = {} intDict['class'] = "NETINTERFACES" interfaces = ethtool.get_devices() for interface in interfaces: try: hwaddr = ethtool.get_hwaddr(interface) except: hwaddr = "" # slave devices can have their hwaddr changed try: master = os.readlink('/sys/class/net/%s/master' % interface) except: master = None if master: master_interface = os.path.basename(master) hwaddr = get_slave_hwaddr(master_interface, interface) try: module = ethtool.get_module(interface) except: if interface == 'lo': module = "loopback" else: module = "Unknown" try: ipaddr = ethtool.get_ipaddr(interface) except: ipaddr = "" try: netmask = ethtool.get_netmask(interface) except: netmask = "" try: broadcast = ethtool.get_broadcast(interface) except: broadcast = "" ip6_list = [] try: # one interface may have more IPv6 addresses for ip6 in dev.get_ipv6_addresses(): ip6_list.append({ 'scope': ip6.scope, 'addr': ip6.address, 'netmask': ip6.netmask }) except: pass intDict[interface] = {'hwaddr':hwaddr, 'ipaddr':ipaddr, 'netmask':netmask, 'broadcast':broadcast, 'module': module, 'ipv6': ip6_list} return intDict