|
| 1 | +importpsutil |
| 2 | +importplatform |
| 3 | +fromdatetimeimportdatetime |
| 4 | + |
| 5 | +defget_size(bytes,suffix="B"): |
| 6 | +""" |
| 7 | + Scale bytes to its proper format |
| 8 | + e.g: |
| 9 | + 1253656 => '1.20MB' |
| 10 | + 1253656678 => '1.17GB' |
| 11 | + """ |
| 12 | +factor=1024 |
| 13 | +forunitin ["","K","M","G","T","P"]: |
| 14 | +ifbytes<factor: |
| 15 | +returnf"{bytes:.2f}{unit}{suffix}" |
| 16 | +bytes/=factor |
| 17 | + |
| 18 | + |
| 19 | +print("="*40,"System Information","="*40) |
| 20 | +uname=platform.uname() |
| 21 | +print(f"System:{uname.system}") |
| 22 | +print(f"Node Name:{uname.node}") |
| 23 | +print(f"Release:{uname.release}") |
| 24 | +print(f"Version:{uname.version}") |
| 25 | +print(f"Machine:{uname.machine}") |
| 26 | +print(f"Processor:{uname.processor}") |
| 27 | + |
| 28 | +# Boot Time |
| 29 | +print("="*40,"Boot Time","="*40) |
| 30 | +boot_time_timestamp=psutil.boot_time() |
| 31 | +bt=datetime.fromtimestamp(boot_time_timestamp) |
| 32 | +print(f"Boot Time:{bt.year}/{bt.month}/{bt.day}{bt.hour}:{bt.minute}:{bt.second}") |
| 33 | + |
| 34 | +# let's print CPU information |
| 35 | +print("="*40,"CPU Info","="*40) |
| 36 | +# number of cores |
| 37 | +print("Physical cores:",psutil.cpu_count(logical=False)) |
| 38 | +print("Total cores:",psutil.cpu_count(logical=True)) |
| 39 | +# CPU frequencies |
| 40 | +cpufreq=psutil.cpu_freq() |
| 41 | +print(f"Max Frequency:{cpufreq.max:.2f}Mhz") |
| 42 | +print(f"Min Frequency:{cpufreq.min:.2f}Mhz") |
| 43 | +print(f"Current Frequency:{cpufreq.current:.2f}Mhz") |
| 44 | +# CPU usage |
| 45 | +print("CPU Usage Per Core:") |
| 46 | +fori,percentageinenumerate(psutil.cpu_percent(percpu=True)): |
| 47 | +print(f"Core{i}:{percentage}%") |
| 48 | +print(f"Total CPU Usage:{psutil.cpu_percent()}%") |
| 49 | + |
| 50 | +# Memory Information |
| 51 | +print("="*40,"Memory Information","="*40) |
| 52 | +# get the memory details |
| 53 | +svmem=psutil.virtual_memory() |
| 54 | +print(f"Total:{get_size(svmem.total)}") |
| 55 | +print(f"Available:{get_size(svmem.available)}") |
| 56 | +print(f"Used:{get_size(svmem.used)}") |
| 57 | +print(f"Percentage:{svmem.percent}%") |
| 58 | +print("="*20,"SWAP","="*20) |
| 59 | +# get the swap memory details (if exists) |
| 60 | +swap=psutil.swap_memory() |
| 61 | +print(f"Total:{get_size(swap.total)}") |
| 62 | +print(f"Free:{get_size(swap.free)}") |
| 63 | +print(f"Used:{get_size(swap.used)}") |
| 64 | +print(f"Percentage:{swap.percent}%") |
| 65 | + |
| 66 | +# Disk Information |
| 67 | +print("="*40,"Disk Information","="*40) |
| 68 | +print("Partitions and Usage:") |
| 69 | +# get all disk partitions |
| 70 | +partitions=psutil.disk_partitions() |
| 71 | +forpartitioninpartitions: |
| 72 | +print(f"=== Device:{partition.device} ===") |
| 73 | +print(f" Mountpoint:{partition.mountpoint}") |
| 74 | +print(f" File system type:{partition.fstype}") |
| 75 | +try: |
| 76 | +partition_usage=psutil.disk_usage(partition.mountpoint) |
| 77 | +exceptPermissionError: |
| 78 | +# this can be catched due to the disk that |
| 79 | +# isn't ready |
| 80 | +continue |
| 81 | +print(f" Total Size:{get_size(partition_usage.total)}") |
| 82 | +print(f" Used:{get_size(partition_usage.used)}") |
| 83 | +print(f" Free:{get_size(partition_usage.free)}") |
| 84 | +print(f" Percentage:{partition_usage.percent}%") |
| 85 | +# get IO statistics since boot |
| 86 | +disk_io=psutil.disk_io_counters() |
| 87 | +print(f"Total read:{get_size(disk_io.read_bytes)}") |
| 88 | +print(f"Total write:{get_size(disk_io.write_bytes)}") |
| 89 | + |
| 90 | +# Network information |
| 91 | +print("="*40,"Network Information","="*40) |
| 92 | +# get all network interfaces (virtual and physical) |
| 93 | +if_addrs=psutil.net_if_addrs() |
| 94 | +forinterface_name,interface_addressesinif_addrs.items(): |
| 95 | +foraddressininterface_addresses: |
| 96 | +print(f"=== Interface:{interface_name} ===") |
| 97 | +ifstr(address.family)=='AddressFamily.AF_INET': |
| 98 | +print(f" IP Address:{address.address}") |
| 99 | +print(f" Netmask:{address.netmask}") |
| 100 | +print(f" Broadcast IP:{address.broadcast}") |
| 101 | +elifstr(address.family)=='AddressFamily.AF_PACKET': |
| 102 | +print(f" MAC Address:{address.address}") |
| 103 | +print(f" Netmask:{address.netmask}") |
| 104 | +print(f" Broadcast MAC:{address.broadcast}") |
| 105 | +# get IO statistics since boot |
| 106 | +net_io=psutil.net_io_counters() |
| 107 | +print(f"Total Bytes Sent:{get_size(net_io.bytes_sent)}") |
| 108 | +print(f"Total Bytes Received:{get_size(net_io.bytes_recv)}") |
| 109 | + |
| 110 | + |