Home » Create a system monitoring tool using psutil with python

Create a system monitoring tool using psutil with python

by maxguy71

If you want to create a tool such as Windows task manager or Process Explorer in Python then the psutil library is the perfect place to start

psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python.

It is useful mainly for system monitoringprofilinglimiting process resources and the management of running processes.

It implements many functionality offered by UNIX command line tools such as: ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap.

psutil currently supports the following platforms:

  • Linux
  • Windows
  • macOS
  • FreeBSD, OpenBSDNetBSD
  • Sun Solaris
  • AIX

The supported Python versions are 2.62.7 and 3.4+

Installation

pip install psutil

Example

This example shows many of the functions

# Retrieving information regarding the CPU
## Returns the system CPU times as a named tuple
print(psutil.cpu_times())

## Returns the system-wide CPU utilization as a percentage
print(psutil.cpu_percent())

## Returns the number of logical CPUs in the system
print(psutil.cpu_count())

## Returns the various CPU statistics as a tuple
print(psutil.cpu_stats())

## Returns the CPU frequency as a nameduple
print(psutil.cpu_freq())

# Retrieving information regarding the Memory
## Returns statistics about system memory usage as a named tuple
print(psutil.virtual_memory())

## Returns system swap memory statistics as a named tuple
print(psutil.swap_memory())

# Retrieving information regarding the Disks
## Returns all mounted disk partitions as a list of named tuples
print(psutil.disk_partitions())

## Returns disk usage statistics about the partition
print(psutil.disk_usage('/'))

## Returns system wide disk I/O statistics as a named tuple
print(psutil.disk_io_counters())

# Retrieving information regarding the Network
## Returns System wide network I/O statistics as a named tuple
print(psutil.net_io_counters())

## Returns system wide socket connections as a list of named tuples
print(psutil.net_connections())

## Returns addresses associated to each network interface card
print(psutil.net_if_addrs())

## Returns information regarding each network interface card
print(psutil.net_if_stats())

# Retrieving information regarding the Sensors
## Returns battery status information as a named tuple
print(psutil.sensors_battery())

# Retrieving information regarding Other System information
## Returns the system boot time
print(psutil.boot_time())

## Return users currently connected on the system
print(psutil.users())

 

Links

For more info regarding all the functions then the documentation is the best place to start

https://psutil.readthedocs.io/en/latest/#

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More