I want to write a Python code to read disk IO and network IO as percentage, like we see in the Windows Task Manager. Currently I am using psutil.disk_io_counters()
and psutil.net_io_counters()
. Through this I am getting byte read & byte write for disk IO and byte received & byte sent for network IO. But I do not know how to convert them into percentage.
It is also observed that disk_io_counters()
and net_io_counters()
does not give the instance value. I have tried the suggestion provided in this link. But I did not get the wanted value. My OS is Windows, but I want the script to be used in a platform independent way. So, without installing any tools like iotop
or iostat
, is it possible to get the values I require? I have tried the following code:
import psutil, os
print('Disk: ',psutil.disk_io_counters())
print('Network: ',psutil.net_io_counters())
I also tried the following code to check that use of psutil.io_counters()
gives the instant disk IO or not. The code is:
import psutil
import time
for x in range(10):
for proc in psutil.process_iter():
io_counters = proc.io_counters()
disk_usage_process = io_counters[2] + io_counters[3] # read_bytes + write_bytes
print("PID: ", proc.pid, "Disk", disk_usage_process)
print('************************************************************')
time.sleep(1)
But I have observed that the values are not of that moment. For prove I have collected disk IO of two system processes using io.counters()
and seen that after times it is changing in increasing order. It means it is adding the disk IO from beginning of the processes. Following is the snapshot:
PID: 10068 Disk 1597555 PID: 8608 Disk 99729700
PID: 10068 Disk 1597555 PID: 8608 Disk 99729828
PID: 10068 Disk 1597555 PID: 8608 Disk 99729956
PID: 10068 Disk 1597555 PID: 8608 Disk 99730212
PID: 10068 Disk 1598271 PID: 8608 Disk 99730340
PID: 10068 Disk 1598271 PID: 8608 Disk 99730596
PID: 10068 Disk 1598271 PID: 8608 Disk 99730724
PID: 10068 Disk 1598271 PID: 8608 Disk 99730852
PID: 10068 Disk 1598271 PID: 8608 Disk 99731108
PID: 10068 Disk 1598271 PID: 8608 Disk 99731236