5

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()
Tushar Gautam
  • 458
  • 6
  • 19
irmorteza
  • 1,576
  • 3
  • 19
  • 32
  • It would help to have the output you are getting, and what you have tried. The documentation here should get you started: http://pythonhosted.org/psutil/ – eleventhend Aug 06 '15 at 15:51

2 Answers2

2

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!

Tushar Gautam
  • 458
  • 6
  • 19
  • Thanks a lot for the answer. But what I looking for is:what you see in the task manger of windows. The ratio of disk or network usage in the moment – irmorteza Aug 06 '15 at 16:50
  • 1
    Could you elaborate or share the final output you want to accomplish? Do you want to get the read/write speed (Mbps) ? If yes, then you can get the read/write speed using data obtained from `psutil.disk_to_counters()` . Actually, disk_to_counters() method returns an instance of `psutil._common.sdiskio`. If you inspect the return value, then you will get `read_byte` and `read_time`(in milliseconds) data. Finally, you can convert these values in Mbps or Kbps using simple Maths. Similarly for network usage too! Hope this helps! – Tushar Gautam Aug 06 '15 at 18:07
  • 3
    He asked for utilization %. As in iotop. – TimZaman Apr 18 '16 at 23:16
  • 1
    The psutil.net_io_counters() will show bytes sent/received as a count but not as used in the moment. The psutil methods that get you % results (cpu usage %) that can't be calculated from the net IO sent/received alone, because the utilization isn't measured by psutil (or whatever is underlying it) and you'd have to collect sent/received stats, diff them between collections over time. The transfer rate would be that diff divided by the time between intervals. e.g. 10kb/s – dubmojo Nov 15 '19 at 21:46
1

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)
Ali Ait-Bachir
  • 550
  • 4
  • 9