Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python compute iostat util column data
# http://stackoverflow.com/questions/4458183/how-the-util-of-iostat-is-computed
import os, sys, time
CURRENT_PATH = os.path.dirname(os.path.abspath(__file__))
#dynamic load the package, avoid install third module
for package in os.listdir(CURRENT_PATH+"/lib"):
if package[-4:] not in (".zip", ".egg"):
continue
sys.path.append("%s/lib/%s" % (CURRENT_PATH, package))
import psutil
cpu_sum = lambda ncpu: ncpu.user + ncpu.system + ncpu.idle + ncpu.iowait + ncpu.nice
ocpu = psutil.cpu_times()
oiostatus = {}
cpu_count = psutil.cpu_count(logical=True)
# http://stackoverflow.com/questions/4189123/python-how-to-get-number-of-mili-seconds-per-jiffy
HZ = os.sysconf(os.sysconf_names['SC_CLK_TCK'])
time.sleep(1)
while 1:
ncpu = psutil.cpu_times()
deltams = (cpu_sum(ncpu) - cpu_sum(ocpu)) * 1000.0 / 1 / cpu_count / HZ
for k, v in psutil.disk_io_counters(perdisk=True).iteritems():
rwtime = v.read_write_time - oiostatus.get(k, 0)
print k, rwtime / 5.0
oiostatus[k] = v.read_write_time
ocpu = ncpu
time.sleep(.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment