How to get CPU utilization and Memory Usage in Python – psutil

  • 8
    Shares

Introduction

psutil (process and system 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 monitoringprofiling and limiting process resources and management of running processes. It implements many functionalities 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, OSX, FreeBSD, OpenBSD, NetBSD, Sun Solaris, AIX …both 32-bit and 64-bit architectures, with Python versions 2.6, 2.7, and 3.4+

Installation – psutil

To install psutil by Python Pip, use the following commands :

Ubuntu / Debian:

sudo apt-get install gcc python-dev python-pip
pip install psutil

RedHat / CentOS:

sudo yum install gcc python-devel python-pip
pip install psutil

If you’re on Python 3 use python3-dev and python3-pip instead.

Example usages

CPU Utilization:

[ansible@localhost ~]$ python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.cpu_times()
scputimes(user=61.55, nice=8.97, system=23.38, idle=2764.05, iowait=35.3, irq=0.0, softirq=2.27, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_stats()
scpustats(ctx_switches=365375, interrupts=413391, soft_interrupts=332063, syscalls=0)
>>> for x in range(3):
... psutil.cpu_percent(interval=1)
...
1.0
0.0
0.0
>>> psutil.cpu_count()
1
>>> for x in range(3):
... psutil.cpu_times_percent(interval=1, percpu=False)
...
scputimes(user=0.0, nice=0.0, system=0.0, idle=100.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
scputimes(user=0.0, nice=0.0, system=0.0, idle=100.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
scputimes(user=0.0, nice=0.0, system=0.0, idle=100.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
>>>
>>>
>>>
>>> quit()
[ansible@localhost ~]$
[ansible@localhost ~]$
[ansible@localhost ~]$ python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>

Memory Usage:

[ansible@localhost ~]$ python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.virtual_memory()
svmem(total=3975045120, available=2915545088, percent=26.7, used=791588864, free=1668673536, active=1010765824, inactive=994258944, buffers=2166784, cached=1512615936, shared=10043392, slab=162213888)
>>> psutil.swap_memory()
sswap(total=4001361920, used=0, free=4001361920, percent=0.0, sin=0, sout=0)
>>>

For more usage examples please visit psutil pypi project page

Avinash Pawar

DevOps Practitioner interested in learning new technologies and interested in sharing the knowledge with others.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle Plus

Avinash Pawar

DevOps Practitioner interested in learning new technologies and interested in sharing the knowledge with others.

Leave a Reply