psutil
Process and System Utilities for Python

Psutil is a cross-platform library for retrieving information about running processes and system utilization in Python. It is useful mainly for system monitoring, profiling, limiting process resources, and managing running processes. Psutil implements many functionalities offered by UNIX command line tool such as ps, top, free, iotop, netstat, ifconfig, lsof and others (see shell equivalents).

pip install psutil
>>> import psutil
>>> psutil.cpu_times()
scputimes(user=3961.46, nice=169.72, system=2150.65, idle=16900.54, iowait=629.59, ...)
>>> psutil.cpu_percent(interval=1)
4.0
>>> psutil.cpu_count(logical=False)
2
>>> psutil.cpu_freq()
scpufreq(current=931.42, min=800.0, max=3500.0)
>>> import psutil
>>> psutil.virtual_memory()
svmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, ...)
>>> psutil.swap_memory()
sswap(total=2097147904, used=296128512, free=1801019392, percent=14.1, sin=304193536, sout=677842944)
>>> import psutil
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/sda1', mountpoint='/',     fstype='ext4', opts='rw,nosuid'),
 sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext4', opts='rw')]
>>> psutil.disk_usage('/')
sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)
>>> psutil.disk_io_counters()
sdiskio(read_count=719566, write_count=1082197, read_bytes=18626220, write_bytes=24081764, ...)
>>> import psutil
>>> psutil.net_io_counters(pernic=True)
{'eth0': netio(bytes_sent=485291293, bytes_recv=6004858642, ...),
 'lo':   netio(bytes_sent=2838627,   bytes_recv=2838627,   ...)}
>>> psutil.net_connections(kind='tcp')
[sconn(family=2, type=1, laddr=addr('10.0.0.1', 48776), raddr=addr('93.186.135.91', 80), status='ESTABLISHED', pid=1254), ...]
>>> psutil.net_if_addrs()['wlan0']
[snicaddr(family=2,  address='192.168.1.3', netmask='255.255.255.0', ...), ...]
>>> psutil.net_if_stats()['wlan0']
snicstats(isup=True, duplex=2, speed=100, mtu=1500, flags='up,broadcast,running')
>>> import psutil
>>> psutil.sensors_temperatures()
{'coretemp': [shwtemp(label='Physical id 0', current=52.0, high=100.0, critical=100.0),
              shwtemp(label='Core 0',        current=45.0, high=100.0, critical=100.0)],
 'acpitz':   [shwtemp(label='',              current=47.0, high=103.0, critical=103.0)]}
>>> psutil.sensors_fans()
{'asus': [sfan(label='cpu_fan', current=3200)]}
>>> psutil.sensors_battery()
sbattery(percent=93, secsleft=16628, power_plugged=False)
>>> import psutil
>>> p = psutil.Process(7055)
>>> p.name(), p.exe(), p.cmdline()
('python3', '/usr/bin/python3', ['/usr/bin/python3', 'main.py'])
>>> p.status()
<ProcessStatus.STATUS_RUNNING: 'running'>
>>> p.cpu_percent(interval=1.0)
12.1
>>> p.memory_info()
pmem(rss=3164160, vms=4410163, shared=897433, text=302694, data=2422374)
>>> p.parent(), p.children(recursive=True)
(psutil.Process(pid=4699, name='bash'), [psutil.Process(pid=29835, name='python3'), ...])