CPU utilisation viewing : with Python

I was curious to know the amount of CPU that is being used to perform tasks on my ubuntu system. I wanted to know if all my cores in the chip were being utilised and if they were  working correctly. A little research led me to the discovery of  CONKY.  A tool that helps the user to monitor/view the utilisation of various system resources. I also found the CONKY MANAGER that allowed users to view the details in a cool graphical way.

 

Details of installing conky can be found here.
Details of installing conky manager can be found here.

So once Conky and the manager is added the widget/gadget may look somthing like this…
conky22

conkyblog

But I wanted only some of the details not all of them and not always.So I decided to write a python script to get the CPU utilisation in terms of percentage  on the whole as well as get the percentage corewise. That is if I have 4 cores then utilisation of each has to be displayed separately.

So on searching I found some useful packages and I wrote a script using them and it led me to get the exact details that I wanted.
The code can be found in my github account – here.
So this program identifies the number of cores in a system and extracts the details and prints it out onto the terminal in an organised way.

The packages i used for extracting the details and formatting the output are—
1)  PSUTIL – the functions of this module helped me to extrcat the average percentage of CPU being used and also the per core details.

2) CURSES- This module helped me to print the details in the same place on the terminal. So that the output looked neat and organised. This is used when there are 8 cores to be shown and
they are divided accross multiple line. If only there was a single line to be printed over and over again at the same place on the terminal, I was able to use the carraige
return sequence “\r” .
I have also used sys and time modules for formatting purposes.

akshay pai

I am a data science engineer and I love working on machine learning problems. I have experience in computer vision, OCR and NLP. I love writing and sharing my knowledge with others. This is why I created Source Dexter. Here I write about Python, Machine Learning, and Raspberry Pi the most. I also write about technology in general, books and topics related to science. I am also a freelance writer with over 3 years of writing high-quality, SEO optimized content for the web. I have written for startups, websites, and universities all across the globe. Get in Touch! We can discuss more.

Leave a Reply

%d bloggers like this: