Is there a way to get disk IO and network usage as a percentage by psutil.
I found some useful function. But I don't know, how to get as a percentage using
psutil.disk_io_counters()
psutil.net_io_counters()
Is there a way to get disk IO and network usage as a percentage by psutil.
I found some useful function. But I don't know, how to get as a percentage using
psutil.disk_io_counters()
psutil.net_io_counters()
You can get the result as a percentage if you follow this method:
import psutil
n_c = tuple(psutil.disk_io_counters())
n_c = [(100.0*n_c[i+1]) / n_c[i] for i in xrange(0, len(n_c), 2)]
print n_c
For my system, the output was [18.154375810797326, 40.375844302056244, 40.33502202082432]
. Each index is a percentage of write upon read data. Eg n_c[0]
is the percentage write_count/read_count
and n_c[1]
is the percentage write_bytes/read_bytes
Similarly you can get percentage data for net_io_counters(). Hope this helps!
For you process the % usage of IO would be:
p = psutil.Process()
io_counters = p.io_counters()
disk_usage_process = io_counters[2] + io_counters[3] # read_bytes + write_bytes
disk_io_counter = psutil.disk_io_counters()
disk_total = disk_io_counter[2] + disk_io_counter[3] # read_bytes + write_bytes
disk_usage_process/disk_total * 100
I computed the IO speed with two measures with a sleep of 1 second:
# Get Start time and first measure
start_time = time.time()
disk_io_counter = psutil.disk_io_counters()
# Get start Read bytes and start read time
start_read_bytes = disk_io_counter[2]
# Get start Write bytes and start write time
start_write_bytes = disk_io_counter[3]
# Wait before next measure
time.sleep(1)
disk_io_counter = psutil.disk_io_counters()
# Get end Read bytes and end read time
end_read_bytes = disk_io_counter[2]
# Get end Write bytes and end write time
end_write_bytes = disk_io_counter[3]
# Get end time
end_time = time.time()
# Compute time diff
time_diff = end_time - start_time
# Compute Read speed : Read Byte / second
read_speed = (end_read_bytes - start_read_bytes)/time_diff
# Compute Write speed : Read Byte / second
write_speed = (end_write_bytes - start_write_bytes)/time_diff
# Convert to Mb/s
read_mega_bytes_sec = round(read_speed / (1024**2), 2)
write_mega_bytes_sec = round(write_speed / (1024**2), 2)