/ tuesdaytooling

Tooling Tuesday - psutil

Back for another interesting tool!

I used this in a project recently, found it by pure chance and I have now added it to my toolkit!

So what is it?

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. In other words we can get details of the underlying operating system in Python.

So how can I get it?

Via pip!

Linux / Mac

sudo pip3 install psutil

Windows

pip.exe install psutil

So how can I use it?

Scenario 1: Get the current CPU usage

Screenshot-from-2019-07-07-20-37-11

import psutil
while True:
    cpu = psutil.cpu_percent(interval=1)
    print("Current CPU usage is "+str(cpu)+"%")

So we need to check the CPU usage as a percentage. In the above example we import the psutil library and then in a loop we create a variable, cpu and store that data every 1 second using an interval. Lastly we print the valeu to the REPL as a nicely formatted string like this Current CPU usage is 17.0%

Scenario 2: Get the available RAM

Screenshot-from-2019-07-07-20-46-21

import psutil
memory = psutil.virtual_memory()
memory = memory[1]
memory = round(memory / 1024 / 1024)
print("There is "+str(memory)+"MB available")

Need to see how much RAM you have left? Well using psutil we create an object called memory and use it to store the output of the psutil.virtual_memory() function. This returns a named tuple, so we then look for the available memory item. This is the second item in the tuple, [1] in Python. The memory value is in bytes so if we divide by 1024, twice, we get the available RAM in MB. This is then printed to the REPL like this There is 3207MB available

Scenario 3: What is my IP address?

Screenshot-from-2019-07-07-21-15-35

import psutil
interfaces = psutil.net_if_addrs()
ip = interfaces["wlp1s0"][0][1]
print("The IP address for wlp1s0 is "+str(ip))

So we need to check our IP address while in a Python project. No worries! Using psutil we create an object called interfaces and in there we store the output of the psutil.net_if_addrs() function. This will dump a dictionary in the object. So if we know the name of our interface, in this case my WiFi interface was wlp1s0 then I can target that entry in the dictionary, and then look in a list at index [0] and another list at index [1] (yeah a dictionary with a list in a list...mind bending stuff!!)
Then we print the IP address to the REPL like this The IP address for wlp1s0 is 192.168.0.10

How do I know what interfaces there are?
I hear ya! If you are not sure then run

import psutil
interfaces = psutil.net_if_addrs()
print(interfaces)

This will print the entire dictionary and show all the interfaces. Just look for your interface, it could be eth0, wlan0 etc.

Any more information?

Sure! The psutil documentation has lots of great things to play with in the System related functions section have a look around and see what you can do.

Have fun and use responsibly!

Happy Hacking