Beispiel #1
0
def networkstate():

    d = {}
    for k in psutil.net_if_addrs().keys():
        l = {}
        for netcardinfo in psutil.net_if_addrs()[k]:
            if platform.system() == "Linux":
                if netcardinfo.family == 2:
                    l.setdefault("device", k)
                    l.setdefault("address", netcardinfo.address)
                    l.setdefault("netmask", netcardinfo.netmask)
                elif netcardinfo.family == 17:
                    l.setdefault("mac", netcardinfo.address)
            if platform.system() == "Windows":
                if netcardinfo.netmask == "":
                    continue
                else:
                    if netcardinfo.family == 2:
                        l.setdefault("device", k)
                        l.setdefault("address", netcardinfo.address)
                        l.setdefault("netmask", netcardinfo.netmask)
                    elif netcardinfo.family == -1:
                        l.setdefault("mac", netcardinfo.address)
        d.setdefault(k, l)
    return d
def interfaces():
    """
    Gets the network interfaces on this server.

    :returns: list of network interfaces
    """

    results = []
    if not sys.platform.startswith("win"):
        for interface in sorted(psutil.net_if_addrs().keys()):
            ip_address = ""
            mac_address = ""
            for addr in psutil.net_if_addrs()[interface]:
                # get the first available IPv4 address only
                if addr.family == socket.AF_INET:
                    ip_address = addr.address
                if addr.family == psutil.AF_LINK:
                    mac_address = addr.address
            results.append({"id": interface,
                            "name": interface,
                            "ip_address": ip_address,
                            "mac_address": mac_address})
    else:
        try:
            results = get_windows_interfaces()
        except ImportError:
            message = "pywin32 module is not installed, please install it on the server to get the available interface names"
            raise aiohttp.web.HTTPInternalServerError(text=message)
        except Exception as e:
            log.error("uncaught exception {type}".format(type=type(e)), exc_info=1)
            raise aiohttp.web.HTTPInternalServerError(text="uncaught exception: {}".format(e))
    return results
Beispiel #3
0
def getSysInfo(id):
		# hostInfo=None
		# if hostAddr:
		# 	hostInfo = hostSysInfo(self.hostAddr)
		global version
		cpu_percent = psutil.cpu_percent(interval=None)
		mem_usage = psutil.virtual_memory().percent
		license_key = os.getenv('license_key')
		if NETWORK_INTERFACE in psutil.net_if_addrs():
			ip = psutil.net_if_addrs()[NETWORK_INTERFACE][0].address
			mask = psutil.net_if_addrs()[NETWORK_INTERFACE][0].netmask
			mac = psutil.net_if_addrs()[NETWORK_INTERFACE][getMacAddressIndex()].address
			ipv6address = getIpv6Address()
		else:
			ip = None
			mask = None
			mac = None
			ipv6address = []
		bytes_sent = psutil.net_io_counters().bytes_sent
		bytes_recv = psutil.net_io_counters().bytes_recv
		bytes_sent_avg = round(float(bytes_sent-lastNetIo["sent"])/HEARTBEAT_INTERVAL/1024,2) if bytes_sent-lastNetIo["sent"] > 0 else 0  #Kbytes/s
		bytes_recv_avg = round(float(bytes_recv-lastNetIo["recv"])/HEARTBEAT_INTERVAL/1024,2) if bytes_recv-lastNetIo["recv"] > 0 else 0  #Kbytes/s
		lastNetIo["sent"] = bytes_sent
		lastNetIo["recv"] = bytes_recv
		task_processes = getTaskProcessInfo()
		topTotal,processs = getProgress()
		probeInfo = dict(cpu = cpu_percent, memory = mem_usage,ipaddress=ip,device_type = 'soft',ipv6address=ipv6address,toptotal=topTotal,topprocess=processs,license_key=license_key,netmask=mask,macaddress=mac,arpNeighbors=arpNeighbors,bytesSent=bytes_sent_avg,bytesRecv=bytes_recv_avg,task_processes=task_processes)
		result = dict(clientId=id,type="heartbeat",time=time.time(),content=probeInfo)
		#result['currentVersion'] = gitutil.getCurrentVersion(config.APPLICATION_PATH).__str__()
		result['currentVersion'] = version
		return result
Beispiel #4
0
def Sysinfo():  
    Hostname = platform.node()
    Sys_version = platform.platform()
    Boot_Start = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(psutil.boot_time()))    
    time.sleep(0.5)  
    Cpu_usage = psutil.cpu_percent()  
    RAM = int(psutil.virtual_memory().total/(1027*1024))  
    RAM_percent = psutil.virtual_memory().percent  
    Swap = int(psutil.swap_memory().total/(1027*1024))  
    Swap_percent = psutil.swap_memory().percent  
    Net_sent = psutil.net_io_counters().bytes_sent  
    Net_recv = psutil.net_io_counters().bytes_recv  
    Net_spkg = psutil.net_io_counters().packets_sent  
    Net_rpkg = psutil.net_io_counters().packets_recv  
    BFH = r'%'
    print " \033[1;32m主机名: %s\033[1;m" % Hostname  
    print " \033[1;32m系统版本: %s\033[1;m" % Sys_version  
    print " \033[1;32m开机时间:%s\033[1;m"  % Boot_Start  
    print " \033[1;32m当前CPU使用率:%s%s\033[1;m" % (Cpu_usage,BFH)  
    print " \033[1;32m物理内存:%dM\t使用率:%s%s\033[1;m" % (RAM,RAM_percent,BFH)  
    print " \033[1;32mSwap内存:%dM\t使用率:%s%s\033[1;m" % (Swap,Swap_percent,BFH)  
    print " \033[1;32m发送:%d Byte\t发送包数:%d个\033[1;m" % (Net_sent,Net_spkg)  
    print " \033[1;32m接收:%d Byte\t接收包数:%d个\033[1;m" % (Net_recv,Net_rpkg)  
  
    for i in psutil.disk_partitions():  
        print " \033[1;32m盘符: %s 挂载点: %s 使用率: %s%s\033[1;m" % (i[0],i[1],psutil.disk_usage(i[1])[3],BFH)  
    for key in psutil.net_if_addrs().keys():  
        print " \033[1;32m网卡: %s IP: %s\033[1;m" % (key,psutil.net_if_addrs()[key][0][1])  
Beispiel #5
0
def get_ipinfo():
    dict = {}
    data = psutil.net_if_addrs()
    for key in data:
        ip = psutil.net_if_addrs()[key][0][1]
        if ip.startswith("1"):
            dict[key]=ip
    return dict
Beispiel #6
0
def getIpv6Address():
	ipv6 = []	
	for index in range(len(psutil.net_if_addrs()[NETWORK_INTERFACE])):
		address = psutil.net_if_addrs()[NETWORK_INTERFACE][index].address
		if index == 0 or 'fe80::' in address or len(address) == 17:
			pass
		else: 
			ipv6.append(address)
	return ipv6
Beispiel #7
0
	def network_scan(self):
		print("Network interfaces and there states")
		net_list = psutil.net_if_addrs()
		for n in net_list:
			print n
			print(psutil.net_if_addrs()[str(n)])
		net_add_list = psutil.net_if_addrs()
		print("Network interfaces adresses")
		for addr in net_list:
			print addr
			print(psutil.net_if_addrs()[str(addr)])
Beispiel #8
0
def get_ip():
    '''
    获取本地除"lo"之外的所有网卡ip地址信息.
    '''
    ips = {}
    inetnames = psutil.net_if_addrs().keys()
    inetnames.remove("lo")
    for inet in inetnames:
        addr = psutil.net_if_addrs()[str(inet)][0].address
        ips[str(inet)] = addr
    return ips
 def update(self):
     """ Get the latest system informations. """
     import psutil
     if self.type == 'disk_use_percent':
         self._state = psutil.disk_usage(self.argument).percent
     elif self.type == 'disk_use':
         self._state = round(psutil.disk_usage(self.argument).used /
                             1024**3, 1)
     elif self.type == 'disk_free':
         self._state = round(psutil.disk_usage(self.argument).free /
                             1024**3, 1)
     elif self.type == 'memory_use_percent':
         self._state = psutil.virtual_memory().percent
     elif self.type == 'memory_use':
         self._state = round((psutil.virtual_memory().total -
                              psutil.virtual_memory().available) /
                             1024**2, 1)
     elif self.type == 'memory_free':
         self._state = round(psutil.virtual_memory().available / 1024**2, 1)
     elif self.type == 'swap_use_percent':
         self._state = psutil.swap_memory().percent
     elif self.type == 'swap_use':
         self._state = round(psutil.swap_memory().used / 1024**3, 1)
     elif self.type == 'swap_free':
         self._state = round(psutil.swap_memory().free / 1024**3, 1)
     elif self.type == 'processor_use':
         self._state = round(psutil.cpu_percent(interval=None))
     elif self.type == 'process':
         if any(self.argument in l.name() for l in psutil.process_iter()):
             self._state = STATE_ON
         else:
             self._state = STATE_OFF
     elif self.type == 'network_out':
         self._state = round(psutil.net_io_counters(pernic=True)
                             [self.argument][0] / 1024**2, 1)
     elif self.type == 'network_in':
         self._state = round(psutil.net_io_counters(pernic=True)
                             [self.argument][1] / 1024**2, 1)
     elif self.type == 'packets_out':
         self._state = psutil.net_io_counters(pernic=True)[self.argument][2]
     elif self.type == 'packets_in':
         self._state = psutil.net_io_counters(pernic=True)[self.argument][3]
     elif self.type == 'ipv4_address':
         self._state = psutil.net_if_addrs()[self.argument][0][1]
     elif self.type == 'ipv6_address':
         self._state = psutil.net_if_addrs()[self.argument][1][1]
     elif self.type == 'last_boot':
         self._state = dt_util.datetime_to_date_str(
             dt_util.as_local(
                 dt_util.utc_from_timestamp(psutil.boot_time())))
     elif self.type == 'since_last_boot':
         self._state = dt_util.utcnow() - dt_util.utc_from_timestamp(
             psutil.boot_time())
Beispiel #10
0
def get_ip(i):
    if i in psutil.net_if_addrs():
        try:
            for k in psutil.net_if_addrs()[i]:
                family, address, netmask, broadcast, ptp = k
                if family == 2:
                    return address
            return None
        except Exception as ex:
            logging.info("get_ip {} {}".format(i, ex.message))
            return None
    else:
        return None
Beispiel #11
0
def init():

    # init global var
    global total_mem
    total_mem = ps.virtual_memory()[0]/1024/1024   # use MB
    logging.debug('total_mem: {}'.format(total_mem))

    global ip
    for nic,addrs in ps.net_if_addrs().items():
        for addr in addrs:
            if addr.family==socket.AF_INET:
                _ip = IP(addr.address)
                if _ip.iptype()=='PUBLIC':
                    ip = str(_ip)
    logging.debug('public ip: {}'.format(ip))

    #singleton
    for proc in ps.process_iter():
        try:
            pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline', 'create_time', 'memory_info', 'memory_percent'])
        except:
            pass
        else:
            for x in pinfo['cmdline']:
                if 'sscontrol' in x and pinfo['pid']!=os.getpid():
                    proc.kill()
Beispiel #12
0
def getInterfaces():
    """
    Get a list of available interfaces.

    Requires `psutil`.

    Returns:
        dict of {nic_name: {type: address}}
    """
    interfaces = {}

    ret = psutil.net_if_addrs()
    for nic_name in ret:
        n = {}
        for snic in ret[nic_name]:
            if not snic.address:
                continue
            if snic.family == socket.AF_INET:
                n['ipv4'] = snic.address
            elif snic.family == socket.AF_INET6:
                n['ipv6'] = snic.address
            elif snic.family == psutil.AF_LINK:
                n['mac'] = snic.address
        interfaces[nic_name] = n
    return interfaces
Beispiel #13
0
def determine_ip_address():
    """Return the first IP address for an ethernet interface on the system."""
    addrs = [
        x.address for k, v in psutil.net_if_addrs().items() if k[0] == "e"
        for x in v if x.family == AddressFamily.AF_INET
    ]
    return addrs[0]
Beispiel #14
0
def main():
    stats = psutil.net_if_stats()
    io_counters = psutil.net_io_counters(pernic=True)
    for nic, addrs in psutil.net_if_addrs().items():
        print("%s:" % (nic))
        if nic in stats:
            st = stats[nic]
            print("    stats          : ", end='')
            print("speed=%sMB, duplex=%s, mtu=%s, up=%s" % (
                st.speed, duplex_map[st.duplex], st.mtu,
                "yes" if st.isup else "no"))
        if nic in io_counters:
            io = io_counters[nic]
            print("    incoming       : ", end='')
            print("bytes=%s, pkts=%s, errs=%s, drops=%s" % (
                io.bytes_recv, io.packets_recv, io.errin, io.dropin))
            print("    outgoing       : ", end='')
            print("bytes=%s, pkts=%s, errs=%s, drops=%s" % (
                io.bytes_sent, io.packets_sent, io.errout, io.dropout))
        for addr in addrs:
            print("    %-4s" % af_map.get(addr.family, addr.family), end="")
            print(" address   : %s" % addr.address)
            if addr.broadcast:
                print("         broadcast : %s" % addr.broadcast)
            if addr.netmask:
                print("         netmask   : %s" % addr.netmask)
            if addr.ptp:
                print("      p2p       : %s" % addr.ptp)
        print("")
Beispiel #15
0
    def get_network():
        """
        returns dictionary with ip information:
        {ifname: {'mac': mac,
                  'ip': ip,
                  'traffic': {'in': txin,
                              'out': txout}
                  }
        }
        """
        out = {}
        iostats = psutil.net_io_counters(True)
        for ifname, ifdata in psutil.net_if_addrs().iteritems():
            ifstats = iostats.get(ifname)
            if not ifstats:
                continue
            mac = None
            if len(ifdata) == 2:
                mac = ifdata[1].address
            ip = ifdata[0].address

            out[ifname] = {'ip': ip,
                           'mac': mac,
                           'traffic': {'in': ifstats.bytes_recv,
                                       'out': ifstats.bytes_sent}}
        return out
Beispiel #16
0
def interfaces():
    """
    Gets the network interfaces on this server.

    :returns: list of network interfaces
    """

    results = []
    if not sys.platform.startswith("win"):
        net_if_addrs = psutil.net_if_addrs()
        for interface in sorted(net_if_addrs.keys()):
            ip_address = ""
            mac_address = ""
            netmask = ""
            interface_type = "ethernet"
            for addr in net_if_addrs[interface]:
                # get the first available IPv4 address only
                if addr.family == socket.AF_INET:
                    ip_address = addr.address
                    netmask = addr.netmask
                if addr.family == psutil.AF_LINK:
                    mac_address = addr.address
            if interface.startswith("tap"):
                # found no way to reliably detect a TAP interface
                interface_type = "tap"
            results.append({"id": interface,
                            "name": interface,
                            "ip_address": ip_address,
                            "netmask": netmask,
                            "mac_address": mac_address,
                            "type": interface_type})
    else:
        try:
            service_installed = True
            if not _check_windows_service("npf") and not _check_windows_service("npcap"):
                service_installed = False
            else:
                results = get_windows_interfaces()
        except ImportError:
            message = "pywin32 module is not installed, please install it on the server to get the available interface names"
            raise aiohttp.web.HTTPInternalServerError(text=message)
        except Exception as e:
            log.error("uncaught exception {type}".format(type=type(e)), exc_info=1)
            raise aiohttp.web.HTTPInternalServerError(text="uncaught exception: {}".format(e))

        if service_installed is False:
            raise aiohttp.web.HTTPInternalServerError(text="The Winpcap or Npcap is not installed or running")

    # This interface have special behavior
    for result in results:
        result["special"] = False
        for special_interface in ("lo", "vmnet", "vboxnet", "docker", "lxcbr",
                                  "virbr", "ovs-system", "veth", "fw", "p2p",
                                  "bridge", "vmware", "virtualbox", "gns3"):
            if result["name"].lower().startswith(special_interface):
                result["special"] = True
        for special_interface in ("-nic"):
            if result["name"].lower().endswith(special_interface):
                result["special"] = True
    return results
Beispiel #17
0
    def _add_ubridge_connection(self, nio, adapter_number):
        """
        Creates a connection in uBridge.

        :param nio: NIO instance or None if it's a dummy interface (if an interface is missing in ubridge you can't see it via ifconfig in the container)
        :param adapter_number: adapter number
        """

        try:
            adapter = self._ethernet_adapters[adapter_number]
        except IndexError:
            raise DockerError("Adapter {adapter_number} doesn't exist on Docker container '{name}'".format(name=self.name,
                                                                                                           adapter_number=adapter_number))

        for index in range(4096):
            if "tap-gns3-e{}".format(index) not in psutil.net_if_addrs():
                adapter.host_ifc = "tap-gns3-e{}".format(str(index))
                break
        if adapter.host_ifc is None:
            raise DockerError("Adapter {adapter_number} couldn't allocate interface on Docker container '{name}'. Too many Docker interfaces already exists".format(name=self.name,
                                                                                                                                                                    adapter_number=adapter_number))

        yield from self._ubridge_send('bridge create bridge{}'.format(adapter_number))
        yield from self._ubridge_send('bridge add_nio_tap bridge{adapter_number} {hostif}'.format(adapter_number=adapter_number,
                                                                                                  hostif=adapter.host_ifc))
        log.debug("Move container %s adapter %s to namespace %s", self.name, adapter.host_ifc, self._namespace)
        try:
            yield from self._ubridge_send('docker move_to_ns {ifc} {ns} eth{adapter}'.format(ifc=adapter.host_ifc,
                                                                                             ns=self._namespace,
                                                                                             adapter=adapter_number))
        except UbridgeError as e:
            raise UbridgeNamespaceError(e)

        if nio:
            yield from self._connect_nio(adapter_number, nio)
Beispiel #18
0
def run():
    stats = psutil.net_if_stats()
    for nic, addrs in psutil.net_if_addrs().items():
      print('<div class="panel panel-default">')
      ut.printTableHeader(nic + ' tarmog`ining xususiyatlari')
      print('<table class="table">')
      print('<tr>')
      ut.printTH('Tezligi')
      ut.printTH('Duplex')
      ut.printTH('MTU')
      ut.printTH('isUp')
      print('</tr>')
      print('<tr>')
      ut.printTD(stats[nic].speed)
      ut.printTD(duplex_map[stats[nic].duplex])
      ut.printTD(stats[nic].mtu)
      ut.printTD("yes" if stats[nic].isup else "no")
      print('</tr>')
      for addr in addrs:
            print('<tr>')
            print('<td>')
            print("    %-8s" % af_map.get(addr.family, addr.family), end="")
            print('</td>')
            ut.printTD(" address   : %s" % addr.address)
            ut.printTD("             broadcast : %s" % addr.broadcast)
            ut.printTD("             netmask   : %s" % addr.netmask)
            print('</tr>')
      print('</table>')
      print('</div>')
 def net_if_addr_info(self):
     '''
     Note: Get info for single nic.
     net_if_addrs_dict:
     :return: dict type
     {
         nic_name:{
                     nic_family: [address, netmask, broadcast, ptp]
                     ......
                  },
         nic_name:{
                     nic_family: [address, netmask, broadcast, ptp]
                     ......
                  }
         ......
     }
     '''
     net_if_addrs_dict = defaultdict(list)
     net_if_addrs = psutil.net_if_addrs()
     for i in net_if_addrs:
         net_if_addrs_dict[i] = defaultdict(list)
         for j in net_if_addrs[i]:
             net_if_addrs_dict[i][j.family].append(j.address)
             net_if_addrs_dict[i][j.family].append(j.netmask)
             net_if_addrs_dict[i][j.family].append(j.broadcast)
             net_if_addrs_dict[i][j.family].append(j.ptp)
     return net_if_addrs_dict
Beispiel #20
0
def get_simple_info():

    simple_info = {}

    cpu_count = psutil.cpu_count()
    ram = psutil.virtual_memory()
    network = psutil.net_if_addrs()
    disk = psutil.disk_usage("/")

    for x in simple:
        num_cpu = cpu_count
        total_ram = ram[0]
        ip_address = get_network_info(addresses[0][1])
        ip_address = get_network_info("ip_address")
        ip_address = network[0][1]
        total_disk = disk[0]
        hostname = os.uname()[1]

        simple_info = {
            "CPU": num_cpu,
            "RAM": total_ram,
            "IP Address": ip_address,
            "Total HDD": total_disk,
            "Hostname": hostname,
        }

        return simple_info
Beispiel #21
0
def net_if_addrs():
    name = ['family', 'address', 'netmask', 'broadcast', 'ptp']
    r = dict()
    for key, value in psutil.net_if_addrs().iteritems():
        r[key] = dict(zip(name, value))

    return r
Beispiel #22
0
def is_interface_up(interface):
    """
    Checks if an interface is up.

    :param interface: interface name

    :returns: boolean
    """

    if sys.platform.startswith("linux"):

        if interface not in psutil.net_if_addrs():
            return False

        import fcntl
        SIOCGIFFLAGS = 0x8913
        try:
            with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
                result = fcntl.ioctl(s.fileno(), SIOCGIFFLAGS, interface + '\0' * 256)
                flags, = struct.unpack('H', result[16:18])
                if flags & 1:  # check if the up bit is set
                    return True
            return False
        except OSError as e:
            raise aiohttp.web.HTTPInternalServerError(text="Exception when checking if {} is up: {}".format(interface, e))
    else:
        # TODO: Windows & OSX support
        return True
Beispiel #23
0
 def test_net_if_addrs_ips(self):
     for name, addrs in psutil.net_if_addrs().items():
         for addr in addrs:
             if addr.family == psutil.AF_LINK:
                 self.assertEqual(addr.address, get_mac_address(name))
             elif addr.family == socket.AF_INET:
                 self.assertEqual(addr.address, get_ipv4_address(name))
    def get_interfaces(self):
        net = psutil.net_if_addrs()
        interfaces = net.keys()
        result = []
        # -------------------------------------------------------------------------------
        if 'win32' in sys.platform: #FOR WINDOWS
            for iface in interfaces:
                obj_len = len(net[iface])
                if obj_len == 1:
                    if net[iface][0].family == 17 or net[iface][0].family == 2:
                        result.append(iface)
                else:
                    if net[iface][1].family == 2:
                        result.append(iface)
        # -------------------------------------------------------------------------------
        elif 'linux' in sys.platform:  # FOR LINUX
            for iface in interfaces:
                result.append(iface)
        #-------------------------------------------------------------------------------
        elif 'darwin' in sys.platform: #FOR MAC OSX
            for iface in interfaces:
                if net[iface][0].family == 2:
                    try:
                        if not '::' in net[iface][1].address:
                            result.append(iface)
                    except Exception:
                        pass
        # -------------------------------------------------------------------------------


        return result
Beispiel #25
0
    def getNetworkInfo(cls):
        interfaces = {}

        stats = psutil.net_if_stats()
        for key, value in six.iteritems(stats):
            interfaces[key] = value.__dict__
            interfaces[key]['addresses'] = []
            interfaces[key]['io_counters'] = None

        addresses = psutil.net_if_addrs()
        for key, value in six.iteritems(addresses):
            if key not in interfaces:
                continue

            for addr in value:
                interfaces[key]['addresses'].append(addr.__dict__)

        traffic = psutil.net_io_counters(pernic=True)
        for key, value in six.iteritems(traffic):
            if key not in interfaces:
                continue

            interfaces[key]['io_counters'] = value.__dict__

        return interfaces
Beispiel #26
0
def get_network_info():
    try:
        addresses = psutil.net_if_addrs()
        stats = psutil.net_if_stats()
    except: 
        print "psutil information not found."   #if no psutil found return error
        return "error"
    
    dict = {}
    for name in addresses:                      #for each individual value, say no ___ if not found
        try:
            mac_address = getHwAddr(name)
        except:
            mac_address = "No mac_address found"

        try:
            ip_address = addresses[name][0][1]
        except:
            ip_address = "no ip_address found"        

        try:
            speed = stats[name][2] / 1000.0
        except:
            speed = "no speed found"        

        try:
            link = stats[name][0]
        except:
            link = False        

        dict[name] =  {"mac_address": mac_address, "ip_address": ip_address, \
                        "speed": speed, "link": link}
    return dict
Beispiel #27
0
    def gather_system(self):
        """
        Get System Information
        """
        try:
            import psutil
            PS = True
        except:
            self.logger.info('Could not Import psutil')
            self.logger.info('pip install psutil for Network Interface Output')
            PS = False

        interfaces = {}
        if PS:
            netaddr = psutil.net_if_addrs()
            for n, a in netaddr.iteritems():
                addresses = {}
                for i in a:
                    if i.family == 2:
                        addresses['ip4'] = i.address
                    if i.family == 10:
                        addresses['ip6'] = i.address
                    if i.family == 17:
                        addresses['mac'] = i.address
                interfaces[n] = addresses

        data = {'name': socket.getfqdn(),
                'net': interfaces}
        self.system = {'system': data}
Beispiel #28
0
    def test_net_if_addrs(self):
        nics = psutil.net_if_addrs()
        assert nics, nics

        nic_stats = psutil.net_if_stats()

        # Not reliable on all platforms (net_if_addrs() reports more
        # interfaces).
        # self.assertEqual(sorted(nics.keys()),
        #                  sorted(psutil.net_io_counters(pernic=True).keys()))

        families = set([socket.AF_INET, socket.AF_INET6, psutil.AF_LINK])
        for nic, addrs in nics.items():
            self.assertIsInstance(nic, str)
            self.assertEqual(len(set(addrs)), len(addrs))
            for addr in addrs:
                self.assertIsInstance(addr.family, int)
                self.assertIsInstance(addr.address, str)
                self.assertIsInstance(addr.netmask, (str, type(None)))
                self.assertIsInstance(addr.broadcast, (str, type(None)))
                self.assertIn(addr.family, families)
                if sys.version_info >= (3, 4):
                    self.assertIsInstance(addr.family, enum.IntEnum)
                if nic_stats[nic].isup:
                    # Do not test binding to addresses of interfaces
                    # that are down
                    if addr.family == socket.AF_INET:
                        s = socket.socket(addr.family)
                        with contextlib.closing(s):
                            s.bind((addr.address, 0))
                    elif addr.family == socket.AF_INET6:
                        info = socket.getaddrinfo(
                            addr.address, 0, socket.AF_INET6,
                            socket.SOCK_STREAM, 0, socket.AI_PASSIVE)[0]
                        af, socktype, proto, canonname, sa = info
                        s = socket.socket(af, socktype, proto)
                        with contextlib.closing(s):
                            s.bind(sa)
                for ip in (addr.address, addr.netmask, addr.broadcast,
                           addr.ptp):
                    if ip is not None:
                        # TODO: skip AF_INET6 for now because I get:
                        # AddressValueError: Only hex digits permitted in
                        # u'c6f3%lxcbr0' in u'fe80::c8e0:fff:fe54:c6f3%lxcbr0'
                        if addr.family != socket.AF_INET6:
                            check_net_address(ip, addr.family)
                # broadcast and ptp addresses are mutually exclusive
                if addr.broadcast:
                    self.assertIsNone(addr.ptp)
                elif addr.ptp:
                    self.assertIsNone(addr.broadcast)

        if BSD or OSX or SUNOS:
            if hasattr(socket, "AF_LINK"):
                self.assertEqual(psutil.AF_LINK, socket.AF_LINK)
        elif LINUX:
            self.assertEqual(psutil.AF_LINK, socket.AF_PACKET)
        elif WINDOWS:
            self.assertEqual(psutil.AF_LINK, -1)
Beispiel #29
0
def serverIp():
   _ip_addr = psutil.net_if_addrs()
   ip_all = {}
   for key in _ip_addr:
       ip = _ip_addr[key][0][1]
       ip_all[key] = ip
   ip_all.pop('lo')
   return  ip_all
Beispiel #30
0
def interface_info(interface):
    interface_dict = psutil.net_if_addrs()
    for addr in interface_dict[interface]:
        if af_net.get(addr.family) == 'IPv4':
            print "\t%-12s: %s" % ('IPv4 Address', addr.address)
            print "\t%-12s: %s" % ('IPv4 Netmask', addr.netmask)
        elif af_net.get(addr.family) == 'MAC':
            print "\t%-12s: %s" % ('MAC Address', addr.address)
Beispiel #31
0
#------------------------------------------------------------


cpu = psutil.cpu_count()

mem = psutil.virtual_memory()
ram_total = mem.total / (1024 ** 3)
ram_used = mem.used / (1024 ** 3)
ram_free = mem.free / (1024 ** 3)

disk = psutil.disk_usage('/')
disk_total = disk.total / (1024 ** 3)
disk_used = disk.used / (1024 ** 3)
disk_free = disk.free / (1024 ** 3)

net = psutil.net_if_addrs()

hostname = socket.gethostname()


#------------------------------------------------------------
#-------------------- CREATE JSON FILE ----------------------
#------------------------------------------------------------


speclist = []
vmspec = OrderedDict()

vmspec['OS Name'] = os.name
vmspec['Hostname'] = hostname
vmspec['VCPUs'] = cpu
Beispiel #32
0
print('磁盘分区信息')
print(psutil.disk_partitions())
print('-' * 100)
print('磁盘使用情况')
print(psutil.disk_usage('/'))
print('-' * 100)
print('磁盘IO')
print(psutil.disk_io_counters())
print('-' * 100)

# 获取网络信息
print('获取网络读写字节/包的个数')
print(psutil.net_io_counters())
print('-' * 100)
print('# 获取网络接口信息')
print(psutil.net_if_addrs())
print('-' * 100)
print('# 获取网络接口状态')
print(psutil.net_if_stats())
print('-' * 100)
print('获取当前网络连接信息')
print(psutil.net_connections())
print('-' * 100)

# 获取进程信息
print('# 所有进程ID')
print(psutil.pids())
print('-' * 100)
print('# 获取指定进程ID')
p = psutil.Process(8840)
print('进程名称', p.name())
Beispiel #33
0
def getIpAddress(family, name):
    for interface, snics in psutil.net_if_addrs().items():
        for snic in snics:
            if snic.family == family and interface == name:
                return snic.address
Beispiel #34
0
mem = psutil.virtual_memory()

s['RAM'] = {}
s['RAM']["Total"] = str(bytes2gb(mem.total)) + 'GB'
s['RAM']["Available"] = str(bytes2gb(mem.available)) + 'GB'
s['RAM']["Used"] = str(bytes2gb(mem.used)) + 'GB'

s['system variables']['swap space'] = psutil.swap_memory()
s['system variables']['disk partitions'] = psutil.disk_partitions()
s['system variables']['disk usage by root'] = psutil.disk_usage('/')
s['system variables']['disk i/o counters'] = psutil.disk_io_counters()
s['system variables'][
    'network i/o counters (per NIC)'] = psutil.net_io_counters(pernic=True)
s['system variables']['network connections'] = psutil.net_connections()
s['system variables']['network addresses'] = psutil.net_if_addrs()
s['system variables']['network statistics'] = psutil.net_if_stats()
#s['system variables']['sensor temperature'] = psutil.sensors_temperatures()
s['system variables']['fans'] = psutil.sensors_fans()
s['system variables']['Battery Left'] = secs2hours(battery.secsleft)
s['system variables']['Battery Percentage'] = battery.percent
s['system variables']['boot time'] = datetime.datetime.fromtimestamp(
    psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
s['system variables']['users'] = psutil.users()

with open('systeminfo.json', 'w') as outfile:
    outfile.write(json.dumps(
        s,
        sort_keys=True,
        ensure_ascii=False,
        indent=4,
Beispiel #35
0
#!/usr/bin/env python
"""
Usage:
  changehost.py <name>

"""
import docopt
import psutil

fmt = """
127.0.1.1	{name}.qbtrade.org  {name}
127.0.0.1 localhost

{ip}  {name}

"""
args = docopt.docopt(__doc__)
ip = psutil.net_if_addrs()['eth0'][0].address
fmt = fmt.format(ip=ip, name=args['<name>'])
print(fmt)
open('/etc/hosts', 'w').write(fmt)
open('/etc/hostname', 'w').write(args['<name>'] + '\n')
 def __init__(self):
     self.parser = psutil.net_if_addrs()
    async def systeminfo(self):
        logging.info("SYSTEMINFO")
        system = ""
        temp = 0
        cpuload = 0
        cpucount = 0
        cpufreq = 0
        totalmem = 0
        availmem = 0
        mempercent = 0
        eth0IP = "N/A"
        wlan0IP = "N/A"
        eth0speed = "N/A"
        wlan0speed = "N/A"

        TEMP_UNIT = self.cbpi.config.get("TEMP_UNIT", "C")
        FAHRENHEIT = False if TEMP_UNIT == "C" else True

        af_map = {
            socket.AF_INET: 'IPv4',
            socket.AF_INET6: 'IPv6',
        }

        try:
            if psutil.LINUX == True:
                system = "Linux"
            elif psutil.WINDOWS == True:
                system = "Windows"
            elif psutil.MACOS == True:
                system = "MacOS"
            cpuload = round(psutil.cpu_percent(interval=None), 1)
            cpucount = psutil.cpu_count(logical=False)
            cpufreq = psutil.cpu_freq()
            mem = psutil.virtual_memory()
            availmem = round((int(mem.available) / (1024 * 1024)), 1)
            mempercent = round(float(mem.percent), 1)
            totalmem = round((int(mem.total) / (1024 * 1024)), 1)
            if system == "Linux":
                try:
                    temps = psutil.sensors_temperatures(fahrenheit=FAHRENHEIT)
                    for name, entries in temps.items():
                        for entry in entries:
                            if name == "cpu_thermal":
                                temp = round(float(entry.current), 1)
                except:
                    pass
            else:
                temp = "N/A"
            if system == "Linux":
                try:
                    ethernet = psutil.net_if_addrs()
                    for nic, addrs in ethernet.items():
                        if nic == "eth0":
                            for addr in addrs:
                                if str(addr.family) == "AddressFamily.AF_INET":
                                    if addr.address:
                                        eth0IP = addr.address
                        if nic == "wlan0":
                            for addr in addrs:
                                if str(addr.family) == "AddressFamily.AF_INET":
                                    if addr.address:
                                        wlan0IP = addr.address
                    info = psutil.net_if_stats()
                    try:
                        for nic in info:
                            if nic == 'eth0':
                                if info[nic].isup == True:
                                    if info[nic].speed:
                                        eth0speed = info[nic].speed
                                else:
                                    eth0speed = "down"
                            if nic == 'wlan0':
                                if info[nic].isup == True:
                                    ratestring = os.popen(
                                        'iwlist wlan0 rate | grep Rate').read(
                                        )
                                    start = ratestring.find("=") + 1
                                    end = ratestring.find(" Mb/s")
                                    wlan0speed = ratestring[start:end]
                                else:
                                    wlan0speed = "down"
                    except Exception as e:
                        logging.info(e)
                except:
                    pass

            if system == "Windows":
                try:
                    ethernet = psutil.net_if_addrs()
                    for nic, addrs in ethernet.items():
                        if nic == "Ethernet":
                            for addr in addrs:
                                if str(addr.family) == "AddressFamily.AF_INET":
                                    if addr.address:
                                        eth0IP = addr.address
                        if nic == "WLAN":
                            for addr in addrs:
                                if str(addr.family) == "AddressFamily.AF_INET":
                                    if addr.address:
                                        wlan0IP = addr.address
                    info = psutil.net_if_stats()
                    try:
                        for nic in info:
                            if nic == 'Ethernet':
                                if info[nic].isup == True:
                                    if info[nic].speed:
                                        eth0speed = info[nic].speed
                                else:
                                    eth0speed = "down"
                            if nic == 'WLAN':
                                if info[nic].isup == True:
                                    if info[nic].speed:
                                        wlan0speed = info[nic].speed
                                else:
                                    wlan0speed = "down"
                    except Exception as e:
                        logging.info(e)
                except:
                    pass

        except:
            pass

        systeminfo = {
            'system': system,
            'cpuload': cpuload,
            'cpucount': cpucount,
            'cpufreq': cpufreq.current,
            'totalmem': totalmem,
            'availmem': availmem,
            'mempercent': mempercent,
            'temp': temp,
            'temp_unit': TEMP_UNIT,
            'eth0': eth0IP,
            'wlan0': wlan0IP,
            'eth0speed': eth0speed,
            'wlan0speed': wlan0speed
        }
        return systeminfo
Beispiel #38
0
class rspi:
    USER = getpass.getuser()
    IP = psutil.net_if_addrs()['wlp8s0'][0][1]

    def getOsVersion(self):
        with open('/etc/issue') as fd:
            for line in fd:
                osver = line.strip()
                break
        return osver[:-6]

    def CpuInfo(self):
        cpuNum = psutil.cpu_count()
        cpuPercent = psutil.cpu_percent(None)
        cpuEachPercent = psutil.cpu_percent(percpu=True)
        # current -> [1] highest -> [2]
        if len(psutil.sensors_temperatures()) == 0:
            cpuTemperature = 99
        else:
            cpuTemperature = psutil.sensors_temperatures()['coretemp'][0]

        return {
            'cpuNum': cpuNum,
            'cpuPercent': cpuPercent,
            'cpuEachPercent': cpuEachPercent,
            'cpuTemperature': cpuTemperature
        }

    def MemoryInfo(self):
        #virtual_memory
        virtual_memory = psutil.virtual_memory()
        virtual_total = virtual_memory[0]
        virtual_available = virtual_memory[1]
        virtual_percent = virtual_memory[2]
        virtual_used = virtual_memory[3]
        virtual_free = virtual_memory[4]
        virtual_active = virtual_memory[5]
        virtual_inactive = virtual_memory[6]
        virtual_buffers = virtual_memory[7]
        virtual_cached = virtual_memory[8]
        virtual_shared = virtual_memory[9]
        #swap_memory
        swap_memory = psutil.swap_memory()
        swap_total = swap_memory[0]
        swap_used = swap_memory[1]
        swap_free = swap_memory[2]
        swap_percent = swap_memory[3]

        return {'virtual_memory': virtual_memory, 'swap_memory': swap_memory}

    def IOInfo(self):
        net_io_counters = psutil.net_io_counters(pernic=True)['wlp8s0']
        sent = net_io_counters[0]
        recv = net_io_counters[1]
        time.sleep(1)
        net_io_counters = psutil.net_io_counters(pernic=True)['wlp8s0']
        sent_1s = net_io_counters[0]
        recv_1s = net_io_counters[1]

        IO_sent_speed = sent_1s - sent
        IO_recv_speed = recv_1s - recv

        bytes_sent = psutil.net_io_counters()[0]
        bytes_recv = psutil.net_io_counters()[1]

        return {
            'bytes_sent': bytes_sent,
            'IO_sent_speed': IO_sent_speed,
            'bytes_recv': bytes_recv,
            'IO_recv_speed': IO_recv_speed
        }

    def HardDiskInfo(self):

        disk_total = psutil.disk_usage('/')[0]
        disk_used = psutil.disk_usage('/')[1]
        disk_free = psutil.disk_usage('/')[2]
        disk_percent = psutil.disk_usage('/')[3]

        return {
            'disk_total': disk_total,
            'disk_used': disk_used,
            'disk_free': disk_free,
            'disk_percent': disk_percent
        }
# 使用psutil获取交换内存信息
print(psutil.swap_memory())
"""
获取磁盘信息
"""
# 通过psutil获取磁盘分区、磁盘使用率和磁盘IO信息
print(psutil.disk_partitions())  # 磁盘分区信息
print(psutil.disk_usage('C:\\'))  # 磁盘使用情况
print(psutil.disk_io_counters())  # 磁盘IO
"""
获取网络信息
"""
# psutil可以获取网络接口和网络连接信息
print(psutil.net_io_counters())  # 获取网络读写字节/包的个数
print(psutil.net_if_addrs())  # 获取网络接口信息
print(psutil.net_if_stats())  # 获取网络接口状态

# 要获取当前网络连接信息,使用net_connections()
print(psutil.net_connections())
"""
获取进程信息
"""
print("===============================进程信息===============================")
print(psutil.pids())  # 所有进程ID
p = psutil.Process(6872)  # 获取指定进程ID=3776
print(p.name())  # 进程名称
print(p.exe())  # 进程exe路径
print(p.cwd())  # 进程工作目录
print(p.cmdline())  # 进程启动的命令行
print(p.ppid())  # 父进程ID
Beispiel #40
0
def configure(section):
    print("Running in configure mode")
    if section == 'main':
        # Check if fileconfig and dir exists, if not create
        if not os.path.exists(configPath):
            print(configPath +
                  " don't exists, creating it with default values.")
            IP = {
                'Interface': psutil.net_if_addrs().keys[0],
                'Local-IPv4': '0.0.0.0',
                'Net-Length-v4': 29,
                'Remote-IPv4': '0.0.0.0'
            }
            BGP = {'Local-AS': 0, 'Remote-AS': 0}
            MRT = {'File': ''}
            WriteRINConf(IP, BGP, MRT)

        else:
            # Inquirer!! :)
            q = [
                inquirer.List(
                    'Interface',
                    message="Select interface to configure",
                    choices=psutil.net_if_addrs().keys(),
                    default=getCurrentParam('Interface'),
                ),
                # Local address
                inquirer.Text('Local-IPv4',
                              message='Local IPv4 Address (without netmask)',
                              default=getCurrentParam('Local-IPv4')),
                # Remote address
                inquirer.Text('Remote-IPv4',
                              message='Remote IPv4 Address (without netmask)',
                              default=getCurrentParam('Remote-IPv4')),
                #Net-Length
                inquirer.Text(
                    'Net-Length-v4',
                    message='Network Length for IPv4 (29, 30, 24...)',
                    default=getCurrentParam('Net-Length-v4')),

                # BGP
                inquirer.Text('Local-AS',
                              message='Local BGP AS Number (3356, 174...)',
                              default=getCurrentParam('Local-AS')),
                inquirer.Text(
                    'Remote-AS',
                    message='Remote BGP AS Number (33932, 213303...)',
                    default=getCurrentParam('Remote-AS')),

                # MRT
                inquirer.Text('file',
                              message='MRT File Path (level3.mrt)',
                              default=getCurrentParam('file')),
            ]

            r = inquirer.prompt(q)

            IP = {
                'Interface': r['Interface'],
                'Local-IPv4': r['Local-IPv4'],
                'Net-Length-v4': r['Net-Length-v4'],
                'Remote-IPv4': r['Remote-IPv4'],
            }
            BGP = {'Local-AS': r['Local-AS'], 'Remote-AS': r['Remote-AS']}
            MRT = {'File': r['file']}
            WriteRINConf(IP, BGP, MRT)
            WriteExaConf(IP, BGP)
Beispiel #41
0
    def __get_nw_devices(self):
        # {'default': {2: ('172.16.0.1', 'brq2376512c-13')}, 2: [('10.0.0.1', 'eno4', True), ('172.16.0.1', 'brq2376512c-13', True), ('172.16.1.1', 'brqf110e342-9b', False), ('10.0.0.1', 'eno4', False)]}

        nets = []
        intfs = psutil.net_if_stats().keys()
        gws = netifaces.gateways().get(2)
        if gws is None:
            gws = []

        default_gw = self.__get_default_gw()
        if default_gw == '':
            self.agent.logger.warning('__get_nw_devices()',
                                      'Default gw not found!!')
        for k in intfs:
            intf_info = psutil.net_if_addrs().get(k)
            if intf_info is not None:
                ipv4_info = [
                    x for x in intf_info
                    if x[0] == socket.AddressFamily.AF_INET
                ]
                ipv6_info = [
                    x for x in intf_info
                    if x[0] == socket.AddressFamily.AF_INET6
                ]
                l2_info = [
                    x for x in intf_info
                    if x[0] == socket.AddressFamily.AF_PACKET
                ]

                if len(ipv4_info) > 0:
                    ipv4_info = ipv4_info[0]
                    ipv4 = ipv4_info[1]
                    ipv4mask = ipv4_info[2]
                    search_gw = [x[0] for x in gws if x[1] == k]
                    if len(search_gw) > 0:
                        ipv4gateway = search_gw[0]
                    else:
                        ipv4gateway = ''

                else:
                    ipv4 = ''
                    ipv4gateway = ''
                    ipv4mask = ''

                if len(ipv6_info) > 0:
                    ipv6_info = ipv6_info[0]
                    ipv6 = ipv6_info[1]
                    ipv6mask = ipv6_info[2]
                else:
                    ipv6 = ''
                    ipv6mask = ''

                if len(l2_info) > 0:
                    l2_info = l2_info[0]
                    mac = l2_info[1]
                else:
                    mac = ''

                speed = psutil.net_if_stats().get(k)[2]
                inft_conf = {
                    'ipv4_address': ipv4,
                    'ipv4_netmask': ipv4mask,
                    'ipv4_gateway': ipv4gateway,
                    'ipv6_address': ipv6,
                    'ipv6_netmask': ipv6mask
                }

                iface_info = {
                    'intf_name': k,
                    'inft_configuration': inft_conf,
                    'intf_mac_address': mac,
                    'intf_speed': speed,
                    'type': self.get_intf_type(k),
                    'available': True,
                    'default_gw': False
                }
                if k == default_gw:
                    iface_info.update({'available': False})
                    iface_info.update({'default_gw': True})
                nets.append(iface_info)

        return nets
Beispiel #42
0
def get_system_stats():
    """
    Collects interesting system stats

    From: https://www.thepythoncode.com/article/get-hardware-system-information-python

    :return: a string containing useful stats
    """
    import psutil
    import platform
    from datetime import datetime

    uname = platform.uname()
    print(f"System: {uname.system}")
    print(f"Node Name: {uname.node}")
    print(f"Release: {uname.release}")
    print(f"Version: {uname.version}")
    print(f"Machine: {uname.machine}")
    print(f"Processor: {uname.processor}")

    ##### Boot Time ########
    print("=" * 40, "Boot Time", "=" * 40)
    boot_time_timestamp = psutil.boot_time()
    bt = datetime.fromtimestamp(boot_time_timestamp)
    print(f"Boot Time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}")

    ###### CPU ########
    # let's print CPU information
    print("=" * 40, "CPU Info", "=" * 40)
    # number of cores
    print("Physical cores:", psutil.cpu_count(logical=False))
    print("Total cores:", psutil.cpu_count(logical=True))
    # CPU frequencies
    cpufreq = psutil.cpu_freq()
    print(f"Max Frequency: {cpufreq.max:.2f}Mhz")
    print(f"Min Frequency: {cpufreq.min:.2f}Mhz")
    print(f"Current Frequency: {cpufreq.current:.2f}Mhz")
    # CPU usage
    print("CPU Usage Per Core:")
    for i, percentage in enumerate(psutil.cpu_percent(percpu=True)):
        print(f"Core {i}: {percentage}%")
    print(f"Total CPU Usage: {psutil.cpu_percent()}%")

    ### MEMORY
    # Memory Information
    print("=" * 40, "Memory Information", "=" * 40)
    # get the memory details
    svmem = psutil.virtual_memory()
    print(f"Total: {get_size(svmem.total)}")
    print(f"Available: {get_size(svmem.available)}")
    print(f"Used: {get_size(svmem.used)}")
    print(f"Percentage: {svmem.percent}%")
    print("=" * 20, "SWAP", "=" * 20)
    # get the swap memory details (if exists)
    swap = psutil.swap_memory()
    print(f"Total: {get_size(swap.total)}")
    print(f"Free: {get_size(swap.free)}")
    print(f"Used: {get_size(swap.used)}")
    print(f"Percentage: {swap.percent}%")

    ### Network
    # Network information
    print("=" * 40, "Network Information", "=" * 40)
    # get all network interfaces (virtual and physical)
    if_addrs = psutil.net_if_addrs()
    for interface_name, interface_addresses in if_addrs.items():
        for address in interface_addresses:
            print(f"=== Interface: {interface_name} ===")
            if str(address.family) == 'AddressFamily.AF_INET':
                print(f"  IP Address: {address.address}")
                print(f"  Netmask: {address.netmask}")
                print(f"  Broadcast IP: {address.broadcast}")
            elif str(address.family) == 'AddressFamily.AF_PACKET':
                print(f"  MAC Address: {address.address}")
                print(f"  Netmask: {address.netmask}")
                print(f"  Broadcast MAC: {address.broadcast}")
    # get IO statistics since boot
    net_io = psutil.net_io_counters()
    print(f"Total Bytes Sent: {get_size(net_io.bytes_sent)}")
    print(f"Total Bytes Received: {get_size(net_io.bytes_recv)}")

    # Combine as a string
    os_stats = f"""
    System: {uname.system}
    Node Name: {uname.node}
    Release: {uname.release}
    Version: {uname.version}
    Machine: {uname.machine}
    Processor: {uname.processor}
    

    {'=' * 40} Boot Time {'=' * 40}
    Boot Time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}
    
    {'=' * 40}    CPU    {'=' * 40}
    Physical cores: {psutil.cpu_count(logical=False)}
    Total cores: {psutil.cpu_count(logical=True)}
    Max Frequency: {cpufreq.max:.2f}Mhz
    Min Frequency: {cpufreq.min:.2f}Mhz
    Current Frequency: {cpufreq.current:.2f}Mhz
    CPU Usage Per Core:
        # ToDo
    Total CPU Usage: {psutil.cpu_percent()}

    
    {'=' * 40} Memory Information {'=' * 40}
    Total: {get_size(svmem.total)}
    Available: {get_size(svmem.available)}
    Used: {get_size(svmem.used)}
    Percentage: {svmem.percent}
    
    {'=' * 40} SWAP {'=' * 40}
    Total: {get_size(swap.total)}
    Free: {get_size(swap.free)}
    Used: {get_size(swap.used)}
    Percentage: {swap.percent}
    """

    return os_stats
Beispiel #43
0
def _run(args):
    if args.check_build:
        check_build(args.verbose)

    # if hosts are not specified, either parse from hostfile, or default as
    # localhost
    if not args.hosts:
        if args.hostfile:
            args.hosts = parse_host_files(args.hostfile)
        else:
            # Set hosts to localhost if not specified
            args.hosts = 'localhost:{np}'.format(np=args.np)

    host_list = args.hosts.split(',')
    all_host_names = []
    pattern = re.compile(r'^[\w.-]+:\d+$')
    for host in host_list:
        if not pattern.match(host.strip()):
            raise ValueError('Invalid host input, please make sure it has '
                             'format as : worker-0:2,worker-1:2.')
        all_host_names.append(host.strip().split(':')[0])

    # horovodrun has to finish all the checks before this timeout runs out.
    if args.start_timeout:
        start_timeout = args.start_timeout
    else:
        # Lookup default timeout from the environment variable.
        start_timeout = int(os.getenv('HOROVOD_START_TIMEOUT', '30'))

    tmout = timeout.Timeout(start_timeout,
                            message='Timed out waiting for {activity}. Please '
                            'check connectivity between servers. You '
                            'may need to increase the --start-timeout '
                            'parameter if you have too many servers.')
    settings = hvd_settings.Settings(verbose=2 if args.verbose else 0,
                                     ssh_port=args.ssh_port,
                                     extra_mpi_args=args.mpi_args,
                                     key=secret.make_secret_key(),
                                     timeout=tmout,
                                     num_hosts=len(all_host_names),
                                     num_proc=args.np,
                                     hosts=args.hosts,
                                     output_filename=args.output_filename,
                                     run_func_mode=args.run_func is not None,
                                     nic=args.nic)

    # This cache stores the results of checks performed by horovodrun
    # during the initialization step. It can be disabled by setting
    # --disable-cache flag.
    fn_cache = None
    if not args.disable_cache:
        params = ''
        if args.np:
            params += str(args.np) + ' '
        if args.hosts:
            params += str(args.hosts) + ' '
        if args.ssh_port:
            params += str(args.ssh_port)
        parameters_hash = hashlib.md5(params.encode('utf-8')).hexdigest()
        fn_cache = cache.Cache(CACHE_FOLDER, CACHE_STALENESS_THRESHOLD_MINUTES,
                               parameters_hash)

    if settings.verbose >= 2:
        print('Filtering local host names.')
    remote_host_names = network.filter_local_addresses(all_host_names)
    if settings.verbose >= 2:
        print('Remote host found: ' + ' '.join(remote_host_names))

    if len(remote_host_names) > 0:
        if settings.verbose >= 2:
            print('Checking ssh on all remote hosts.')
        # Check if we can ssh into all remote hosts successfully.
        _check_all_hosts_ssh_successful(remote_host_names,
                                        args.ssh_port,
                                        fn_cache=fn_cache)
        if settings.verbose >= 2:
            print('SSH was successful into all the remote hosts.')

    if len(remote_host_names) > 0:
        if settings.verbose >= 2:
            print('Testing interfaces on all the hosts.')

        local_host_names = set(all_host_names) - set(remote_host_names)
        # Find the set of common, routed interfaces on all the hosts (remote
        # and local) and specify it in the args to be used by NCCL. It is
        # expected that the following function will find at least one interface
        # otherwise, it will raise an exception.
        common_intfs = _driver_fn(all_host_names,
                                  local_host_names,
                                  settings,
                                  fn_cache=fn_cache)

        if settings.verbose >= 2:
            print('Interfaces on all the hosts were successfully checked.')
            print('Common interface found: ' + ' '.join(common_intfs))

    else:
        if settings.verbose >= 2:
            print('All hosts are local, finding the interfaces '
                  'with address 127.0.0.1')
        # If all the given hosts are local, find the interfaces with address
        # 127.0.0.1
        common_intfs = set()
        for iface, addrs in net_if_addrs().items():
            if settings.nic and iface != settings.nic:
                continue
            for addr in addrs:
                if addr.family == AF_INET and addr.address == '127.0.0.1':
                    common_intfs.add(iface)
                    break

        if len(common_intfs) == 0:
            raise ValueError('No interface is found for address 127.0.0.1.')

        if settings.verbose >= 2:
            print('Local interface found ' + ' '.join(common_intfs))

    # get the driver IPv4 address
    driver_ip = _get_driver_ip(common_intfs)

    if args.run_func:
        run_func_server = KVStoreServer(verbose=settings.verbose)
        run_func_server_port = run_func_server.start_server()
        pickled_exec_func = cloudpickle.dumps(args.run_func)
        put_data_into_kvstore(driver_ip, run_func_server_port, 'runfunc',
                              'func', pickled_exec_func)

        command = [
            sys.executable, '-m', 'horovod.run.run_task',
            str(driver_ip),
            str(run_func_server_port)
        ]

        try:
            _launch_job(args, remote_host_names, settings, common_intfs,
                        command)
            results = [None] * args.np
            # TODO: make it parallel to improve performance
            for i in range(args.np):
                pickled_result = read_data_from_kvstore(
                    driver_ip, run_func_server_port, 'runfunc_result', str(i))
                results[i] = cloudpickle.loads(pickled_result)
            return results
        finally:
            run_func_server.shutdown_server()
    else:
        command = args.command
        _launch_job(args, remote_host_names, settings, common_intfs, command)
        return None
Beispiel #44
0
#!/usr/bin/env python3

import shutil
import psutil
import email
import smtplib

disk_usage = shutil.disk_usage('/')
percent_free = disk_usage[2] / disk_usage[0] * 100
free_memory = psutil.virtual_memory()[1] / 1000000
localhost_key = list(psutil.net_if_addrs().keys())[0]
localhost = psutil.net_if_addrs()[localhost_key][0][1]

sender = '*****@*****.**'
recipient = '*****@*****.**'
body = 'Please check your system and resolve the issue as soon as possible.'


def generate_error_report(sender, recipient, subject, body):
    # Basic Email formatting
    message = email.message.EmailMessage()
    message["From"] = sender
    message["To"] = recipient
    message["Subject"] = subject
    message.set_content(body)

    mail_server = smtplib.SMTP('localhost')
    mail_server.send_message(message)
    mail_server.quit()

Beispiel #45
0
#Information about disk

print("DiSK PARTITIONS:",psutil.disk_partitions())

print("DISK USAGE:",psutil.disk_usage('/'))

print("DISK I/O DETAILS:",psutil.disk_io_counters())

#Information about network

print("NETWORK I/O DEATILS:",psutil.net_io_counters())

print("NETWORK CONNECTON TYPES:",psutil.net_connections())

print("NETWORK IP ADDRESS:",psutil.net_if_addrs())

print("NETWORK INTERFACE CARD INFORMATION:",psutil.net_if_stats())


#Laptop battery details

#print("BATTERY DETAILS:",psutil.sensors_battery())

#System boot time

print("SYSTEM BOOT ON TIME:",datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))

#System users

print("SYSTEM USERS:",psutil.users())
Beispiel #46
0
def getIp():
    networks = psutil.net_if_addrs()
    ip = networks['wlan0'][0].address
    return ip
Beispiel #47
0
def _net_if_addrs() -> Any:
    return psutil.net_if_addrs()
Beispiel #48
0
 def get_nics(self):
     return psutil.net_if_addrs()
Beispiel #49
0
 def _infer_socket_ifname(self):
     addrs = psutil.net_if_addrs()
     # a hacky way to deal with varying network interface names
     ifname = next((addr for addr in addrs if addr.startswith("e")), None)
     return ifname
Beispiel #50
0
def get_if(name=None):
    d = psutil.net_if_addrs()
    if name:
        return {k: v for k, v in d.items() if k.startswith(name)}
    return d
Beispiel #51
0
 def get_ip(self):
     return psutil.net_if_addrs()['eth0'][0][1]
Beispiel #52
0
    def get(self, container=False):
        """
        Get host stats (uptime, cpu, ram, etc)
        """

        host_cpu_infos = lwp.host_cpu_infos()

        cpu_count_logical = psutil.cpu_count()
        cpu_count_physical = psutil.cpu_count(logical=False)
        cpu_percent = lwp.host_cpu_percent()

        virtual_memory = psutil.virtual_memory()
        swap_memory = psutil.swap_memory()

        disk_partitions = psutil.disk_partitions()
        disk_partitions_usage = []
        for partition in disk_partitions:
            partition_data = psutil.disk_usage(partition.mountpoint)
            disk_partitions_usage.append({
                'name': partition.mountpoint,
                'total': partition_data.total,
                'used': partition_data.used,
                'free': partition_data.free,
                'percent': partition_data.percent
            })

        net_if_addrs = psutil.net_if_addrs()

        adapters = []

        for adapter in net_if_addrs:
            adapters.append({
                'name': adapter,
                'ipv4': None,
                'ipv6': None
            })
            index = len(adapters) - 1
            for snic in net_if_addrs[adapter]:
                if snic.family.name == 'AF_INET':
                    adapters[index]['ipv4'] = snic.address
                if snic.family.name == 'AF_INET6':
                    adapters[index]['ipv6'] = snic.address

        json_output = {
            'uptime': lwp.host_uptime(),
            'hostname': socket.gethostname(),
            'distrib': ' '.join(linux_distribution()),
            'disk': disk_partitions_usage,
            'cpu': {
                'usage': cpu_percent,
                'model': host_cpu_infos['name'],
                'physical': cpu_count_physical,
                'logical': cpu_count_logical
            },
            'memory': {
                'virtual': {
                    'total': virtual_memory.total,
                    'used': virtual_memory.used,
                    'free': virtual_memory.free,
                    'percent': virtual_memory.percent
                },
                'swap': {
                    'total': swap_memory.total,
                    'used': swap_memory.used,
                    'free': swap_memory.free,
                    'percent': swap_memory.percent
                }
            },
            'adapters': adapters,
            'kernel': platform.release(),
            'lxc': {
                'version': lxc.version,
                'lxcpath': lxc.get_global_config_item('lxc.lxcpath'),
                'default_config': lxc.get_global_config_item('lxc.default_config')
            }
        }

        if not container:
            output = {
                'attributes': json_output
            }
        else:
            output = json_output

        return {'data': output}
Beispiel #53
0
    # Percentual de uso de Disco
    if msg.decode('ascii') == '3':
        res = []
        res = ps.disk_usage('.')
        sendResponse(res.percent)

    # Informações da CPU
    if msg.decode('ascii') == '4':
        res = []
        info = cpuinfo.get_cpu_info()
        res = getCpuInfo(info)
        sendResponse(res)

    # Ip da maquina
    if msg.decode('ascii') == '5':
        dic_interfaces = ps.net_if_addrs()
        machine_ip = dic_interfaces['Ethernet'][1].address
        sendResponse(machine_ip)

    # retornar as informações do arquivo
    if msg.decode('ascii') == '6':
        path = cliente.recv(1024)
        decode_path = path.decode('ascii')
        list_files = os.listdir(decode_path)
        sendResponse(list_files)

    # retornar as informações de um diretorio
    if msg.decode('ascii') == '7':
        path = cliente.recv(1024)
        decode_path = path.decode('ascii')
Beispiel #54
0
async def request(interface: str,
                  mac_address: str,
                  hostname: Optional[str] = None,
                  timeout: int = DEFAULT_TIMEOUT) -> DHCPReply:

    logger.debug('sending DHCP discovery for %s (%s) on %s', mac_address,
                 hostname if hostname else '<no hostname>', interface)

    # Generate random transaction id
    xid = int(random.random() * 0xFFFFFFFF)

    # Find MAC & IP address of given interface
    try:
        if_addrs = psutil.net_if_addrs()[interface]

    except KeyError:
        raise DHCPException(
            f'Cannot find own address for interface {interface}')

    own_mac_address = None
    own_ip_address = None
    for addr in if_addrs:
        if addr.family == socket.AF_PACKET:
            own_mac_address = addr.address

        elif addr.family == socket.AF_INET:
            own_ip_address = addr.address

    if not own_ip_address or not own_mac_address:
        raise DHCPException(
            f'Cannot find own address for interface {interface}')

    dhcp_opts = _build_dhcp_options(hostname)
    dhcp_header = _build_dhcp_header(mac_address, xid, own_ip_address)
    dhcp_packet = dhcp_header + dhcp_opts

    udp_header = _build_udp_header(dhcp_packet)
    udp_packet = udp_header + dhcp_packet

    ipv4_header = _build_ipv4_header(udp_packet)
    ipv4_packet = ipv4_header + udp_packet

    ethernet_header = _build_ethernet_header(own_mac_address)
    ethernet_frame = ethernet_header + ipv4_packet

    # Open sockets
    sock = socket.socket(family=socket.PF_PACKET,
                         type=socket.SOCK_RAW,
                         proto=socket.htons(ETH_P_ALL))
    sock.bind((interface, 0))

    # Actually send packet
    sock.send(ethernet_frame)

    # Wait for data; filter out stuff that we don't need
    start_time = time.time()
    while True:
        try:
            data = sock.recv(1024, socket.MSG_DONTWAIT)

        except BlockingIOError:
            data = None

        if data:
            offer = _check_received_frame(data, xid)
            if offer:
                break

        offer = _check_and_prune_pending_dhcp_replies(xid)
        if offer:
            break

        if time.time() - start_time > timeout:
            break

        await asyncio.sleep(0)

    # Don't forget to close socket
    sock.close()

    if offer:
        logger.debug('received DHCP offer for %s (%s) on %s: %s', mac_address,
                     hostname if hostname else '<no hostname>', interface,
                     offer.ip_address)

        return offer

    else:
        logger.warning('timeout waiting for DHCP offer for %s (%s) on %s',
                       mac_address, hostname if hostname else '<no hostname>',
                       interface)

        raise DHCPTimeout()
Beispiel #55
0
def check_interface(interface):
    interface_addrs = psutil.net_if_addrs().get(interface) or []
    return socket.AF_INET in [snicaddr.family for snicaddr in interface_addrs]
Beispiel #56
0
def get_common_interfaces(settings,
                          all_host_names,
                          remote_host_names=None,
                          fn_cache=None):
    '''
    Find the set of common and routed interfaces on all the hosts.
    :param settings: the object that contains the setting for running horovod
    :type settings: Horovod.run.common.util.settings.Settings
    :param all_host_names: list of the host names
    :type all_host_names: list(string)
    :param remote_host_names: list of the remote host names.
    :type remote_host_names: list(string)
    :param fn_cache: Cache storing the results of checks performed by horovod
    :type fn_cache: Horovod.run.util.cache.Cache
    :return: List of common interfaces
    '''
    # Skipping interface discovery for LSF cluster as it slows down considerably the job start
    if lsf.LSFUtils.using_lsf():
        return None

    if remote_host_names is None:
        remote_host_names = network.filter_local_addresses(all_host_names)

    if len(remote_host_names) > 0:
        if settings.nics:
            # If args.nics is provided, we will use those interfaces. All the workers
            # must have at least one of those interfaces available.
            nics = settings.nics
        else:
            # Find the set of common, routed interfaces on all the hosts (remote
            # and local) and specify it in the args to be used by NCCL. It is
            # expected that the following function will find at least one interface
            # otherwise, it will raise an exception.
            if settings.verbose >= 2:
                print('Testing interfaces on all the hosts.')

            local_host_names = set(all_host_names) - set(remote_host_names)
            nics = _driver_fn(all_host_names,
                              local_host_names,
                              settings,
                              fn_cache=fn_cache)

            if settings.verbose >= 2:
                print('Interfaces on all the hosts were successfully checked.')
                print('Common interface found: ' + ' '.join(nics))

    else:
        if settings.verbose >= 2:
            print('All hosts are local, finding the interfaces '
                  'with address 127.0.0.1')
        # If all the given hosts are local, find the interfaces with address
        # 127.0.0.1
        nics = set()
        for iface, addrs in net_if_addrs().items():
            if settings.nics and iface not in settings.nics:
                continue
            for addr in addrs:
                if addr.family == AF_INET and addr.address == '127.0.0.1':
                    nics.add(iface)
                    break

        if len(nics) == 0:
            raise ValueError('No interface is found for address 127.0.0.1.')

        if settings.verbose >= 2:
            print('Local interface found ' + ' '.join(nics))
    return nics
Beispiel #57
0
def netUpdate(self):
    nettemp=ps.net_io_counters(pernic=True)
    nettempaddr=ps.net_if_addrs()
    self.nett2=time()##
    timenetDiff=self.nett2-self.nett1
    self.netstate2=[]
    self.byterecpersecString=[]
    self.bytesendpersecString=[]
    for i in range(0,self.numOfNets):
        try:
            self.netstate2.append(nettemp[self.netNameList[i]])
        except:
            pass

    self.netDiff=[]
    for i in range(0,self.numOfNets):
        try:
            self.netDiff.append([x2-x1 for x1,x2 in zip(self.netstate1[i],self.netstate2[i])])
            bytesendpersec=(self.netDiff[i][0]/timenetDiff)           ##default in KB
            byterecpersec=(self.netDiff[i][1]/timenetDiff) 
            totalbyterec=nettemp[self.netNameList[i]][1]           ##default in KB
            totalbytesent=nettemp[self.netNameList[i]][0]

            ## total received
            self.netWidgetList[i].nettotalreclabelvalue.set_text(byte_to_human(totalbyterec,persec=False))

            ## total bytes sent
            self.netWidgetList[i].nettotalsentlabelvalue.set_text(byte_to_human(totalbytesent,persec=False))

            ## send per sec (uploading speed)
            self.bytesendpersecString.append(byte_to_human(bytesendpersec))
            self.netWidgetList[i].netsendlabelvalue.set_text(self.bytesendpersecString[i])

            ## received per sec (downloading speed)
            self.byterecpersecString.append(byte_to_human(byterecpersec))
            self.netWidgetList[i].netreclabelvalue.set_text(self.byterecpersecString[i])

            if self.update_graph_direction:
                self.netReceiveArray[i].pop(0)
                self.netReceiveArray[i].append(byterecpersec)         ## in KBs

                self.netSendArray[i].pop(0)
                self.netSendArray[i].append(bytesendpersec)
            else:
                self.netReceiveArray[i].pop()
                self.netReceiveArray[i].insert(0,byterecpersec)         ## in KBs

                self.netSendArray[i].pop()
                self.netSendArray[i].insert(0,bytesendpersec)

            self.netWidgetList[i].givedata(self,i)

            self.netWidgetList[i].net4addrlablevalue.set_text(nettempaddr[self.netNameList[i]][0][1])
            try:
                self.netWidgetList[i].net6addrlabelvalue.set_text(nettempaddr[self.netNameList[i]][1][1])
            except Exception:
                pass
        except Exception as e:
            print(f'some error in net update: {e}')
            


    self.netstate1=self.netstate2
    #print(self.nett2-self.nett1)
    self.nett1=self.nett2
Beispiel #58
0
 def update(self):
     """Get the latest system information."""
     import psutil
     if self.type == 'disk_use_percent':
         self._state = psutil.disk_usage(self.argument).percent
     elif self.type == 'disk_use':
         self._state = round(
             psutil.disk_usage(self.argument).used / 1024**3, 1)
     elif self.type == 'disk_free':
         self._state = round(
             psutil.disk_usage(self.argument).free / 1024**3, 1)
     elif self.type == 'memory_use_percent':
         self._state = psutil.virtual_memory().percent
     elif self.type == 'memory_use':
         self._state = round((psutil.virtual_memory().total -
                              psutil.virtual_memory().available) / 1024**2,
                             1)
     elif self.type == 'memory_free':
         self._state = round(psutil.virtual_memory().available / 1024**2, 1)
     elif self.type == 'swap_use_percent':
         self._state = psutil.swap_memory().percent
     elif self.type == 'swap_use':
         self._state = round(psutil.swap_memory().used / 1024**3, 1)
     elif self.type == 'swap_free':
         self._state = round(psutil.swap_memory().free / 1024**3, 1)
     elif self.type == 'processor_use':
         self._state = round(psutil.cpu_percent(interval=None))
     elif self.type == 'process':
         if any(self.argument in l.name() for l in psutil.process_iter()):
             self._state = STATE_ON
         else:
             self._state = STATE_OFF
     elif self.type == 'network_out' or self.type == 'network_in':
         counters = psutil.net_io_counters(pernic=True)
         if self.argument in counters:
             counter = counters[self.argument][IO_COUNTER[self.type]]
             self._state = round(counter / 1024**2, 1)
         else:
             self._state = STATE_UNKNOWN
     elif self.type == 'packets_out' or self.type == 'packets_in':
         counters = psutil.net_io_counters(pernic=True)
         if self.argument in counters:
             self._state = counters[self.argument][IO_COUNTER[self.type]]
         else:
             self._state = STATE_UNKNOWN
     elif self.type == 'ipv4_address' or self.type == 'ipv6_address':
         addresses = psutil.net_if_addrs()
         if self.argument in addresses:
             self._state = addresses[self.argument][IF_ADDRS[self.type]][1]
         else:
             self._state = STATE_UNKNOWN
     elif self.type == 'last_boot':
         self._state = dt_util.as_local(
             dt_util.utc_from_timestamp(
                 psutil.boot_time())).date().isoformat()
     elif self.type == 'since_last_boot':
         self._state = dt_util.utcnow() - dt_util.utc_from_timestamp(
             psutil.boot_time())
     elif self.type == 'load_1m':
         self._state = os.getloadavg()[0]
     elif self.type == 'load_5m':
         self._state = os.getloadavg()[1]
     elif self.type == 'load_15m':
         self._state = os.getloadavg()[2]
Beispiel #59
0
    def get(self):

        # help
        if str(self.recibir_todo) == 'help':
            cmd_dispoinbles = [
                'Get-info.py [system]', 'Get-info.py [interfaces]',
                'Get-info.py [all]', 'Get-info.py [all_brief]',
                'Get-info.py [wan]'
            ]
            return cmd_dispoinbles

        # list
        elif str(self.recibir_todo) == 'interfaces':
            addrs = psutil.net_if_addrs()
            return addrs.keys()

        # system
        elif str(self.recibir_todo) == 'system':
            var_return = {'Operating system detected:': platform.system()}
            return var_return
        # all
        elif str(self.recibir_todo) == 'all':
            var_list = []
            addrs = psutil.net_if_addrs()

            for x in addrs:
                var_list.append(
                    os.popen('ip addr show ' + str(x) + ' | grep inet').read())
            return var_list
        # all_brief
        elif str(self.recibir_todo) == 'all_brief':
            addrs = psutil.net_if_addrs()
            list_sav = []
            var_return = {}
            #leemos los valores para cada interfaz y lo guardamos.
            for x in addrs:
                p1 = os.popen('ip addr show ' + str(x) + ' | grep inet').read()
                nuevo_p1 = p1.splitlines()
                for x in p1.splitlines():
                    list_sav.append(x)

            p1 = re.compile(
                r'(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)',
                re.M)

            linea = []
            for list_interface in addrs.keys(
            ):  #para cada interfaz de del host.
                for valor in list_sav:  #para cada línea del output ip addr show all_brief
                    name = (r'\s') + re.escape(list_interface)
                    p2 = re.compile(name, re.M)
                    res1 = re.search(p2, valor)
                    if res1:
                        linea.append(valor)

            for x in linea:
                for list_interface2 in addrs.keys():
                    name = (r'\s') + re.escape(list_interface2)
                    p22 = re.compile(name, re.M)
                    res11 = re.search(p1, x)
                    res12 = re.search(p22, x)
                    if res11 and res12 is not None:
                        var_return.update({res12.group(): res11.group()})

            return var_return  #Return dict
        elif str(self.recibir_todo) == 'wan':
            var_return = {
                'IP WAN detected':
                str((get('https://api.ipify.org').content).decode("utf-8"))
            }
            return var_return

        else:
            print(
                "This argument does not exist.\nUse the help to know the arguments"
            )
Beispiel #60
0
def general():
    # function that converts large number of bytes into a scaled format (e.g in kilo, mega, giga, etc.):
    def get_size(bytes, suffix="B"):
        """
        Scale bytes to its proper format
        e.g:
            1253656 => '1.20MB'
            1253656678 => '1.17GB'
        """
        factor = 1024
        for unit in ["", "K", "M", "G", "T", "P"]:
            if bytes < factor:
                return f"{bytes:.2f}{unit}{suffix}"
            bytes /= factor

    print("="*40, "System Information", "="*40)
    uname = platform.uname()
    print(f"System: {uname.system}")
    print(f"Node Name: {uname.node}")
    print(f"Release: {uname.release}")
    print(f"Version: {uname.version}")
    print(f"Machine: {uname.machine}")
    print(f"Processor: {uname.processor}")

    # Boot Time
    print("="*40, "Boot Time", "="*40)
    boot_time_timestamp = psutil.boot_time()
    bt = datetime.fromtimestamp(boot_time_timestamp)
    print(
        f"Boot Time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}")

    # let's print CPU information
    print("="*40, "CPU Info", "="*40)
    # number of cores
    print("Physical cores:", psutil.cpu_count(logical=False))
    print("Total cores:", psutil.cpu_count(logical=True))
    # CPU frequencies
    cpufreq = psutil.cpu_freq()
    print(f"Max Frequency: {cpufreq.max:.2f}Mhz")
    print(f"Min Frequency: {cpufreq.min:.2f}Mhz")
    print(f"Current Frequency: {cpufreq.current:.2f}Mhz")
    # CPU usage
    print("CPU Usage Per Core:")
    for i, percentage in enumerate(psutil.cpu_percent(percpu=True, interval=1)):
        print(f"Core {i}: {percentage}%")
    print(f"Total CPU Usage: {psutil.cpu_percent()}%")

    # Memory Information
    print("="*40, "Memory Information", "="*40)
    # get the memory details
    svmem = psutil.virtual_memory()
    print(f"Total: {get_size(svmem.total)}")
    print(f"Available: {get_size(svmem.available)}")
    print(f"Used: {get_size(svmem.used)}")
    print(f"Percentage: {svmem.percent}%")
    print("="*20, "SWAP", "="*20)
    # get the swap memory details (if exists)
    swap = psutil.swap_memory()
    print(f"Total: {get_size(swap.total)}")
    print(f"Free: {get_size(swap.free)}")
    print(f"Used: {get_size(swap.used)}")
    print(f"Percentage: {swap.percent}%")

    # Disk Information
    print("="*40, "Disk Information", "="*40)
    print("Partitions and Usage:")
    # get all disk partitions
    partitions = psutil.disk_partitions()
    for partition in partitions:
        print(f"=== Device: {partition.device} ===")
        print(f"  Mountpoint: {partition.mountpoint}")
        print(f"  File system type: {partition.fstype}")
        try:
            partition_usage = psutil.disk_usage(partition.mountpoint)
        except PermissionError:
            # this can be catched due to the disk that
            # isn't ready
            continue
        print(f"  Total Size: {get_size(partition_usage.total)}")
        print(f"  Used: {get_size(partition_usage.used)}")
        print(f"  Free: {get_size(partition_usage.free)}")
        print(f"  Percentage: {partition_usage.percent}%")
    # get IO statistics since boot
    disk_io = psutil.disk_io_counters()
    print(f"Total read: {get_size(disk_io.read_bytes)}")
    print(f"Total write: {get_size(disk_io.write_bytes)}")

    # Network information
    print("="*40, "Network Information", "="*40)
    # get all network interfaces (virtual and physical)
    if_addrs = psutil.net_if_addrs()
    for interface_name, interface_addresses in if_addrs.items():
        for address in interface_addresses:
            print(f"=== Interface: {interface_name} ===")
            if str(address.family) == 'AddressFamily.AF_INET':
                print(f"  IP Address: {address.address}")
                print(f"  Netmask: {address.netmask}")
                print(f"  Broadcast IP: {address.broadcast}")
            elif str(address.family) == 'AddressFamily.AF_PACKET':
                print(f"  MAC Address: {address.address}")
                print(f"  Netmask: {address.netmask}")
                print(f"  Broadcast MAC: {address.broadcast}")
    # get IO statistics since boot
    net_io = psutil.net_io_counters()
    print(f"Total Bytes Sent: {get_size(net_io.bytes_sent)}")
    print(f"Total Bytes Received: {get_size(net_io.bytes_recv)}")