View Full Version : top question
September 14th, 2005, 06:18 PM
Does anyone know who top gets it %cpu usage? I know it's from the /proc, but I dont know where inside /proc.
I would like to script the %cpu usage every 1 sec and have the output saved into a file with a time stamp on every line.
September 15th, 2005, 12:17 AM
the file you're interested in is /proc/stat ... i wrote a simple python script to calculate the % a while back, i just modified it to add a time stamp.
TIMEFORMAT = "%m/%d/%y %H:%M:%S"
INTERVAL = 1
statFile = file("/proc/stat", "r")
timeList = statFile.readline().split(" ")[2:6]
for i in range(len(timeList)) :
timeList[i] = int(timeList[i])
def deltaTime(interval) :
x = getTimeList()
y = getTimeList()
for i in range(len(x)) :
y[i] -= x[i]
if __name__ == "__main__" :
while True :
dt = deltaTime(INTERVAL)
timeStamp = time.strftime(TIMEFORMAT)
cpuPct = 100 - (dt[len(dt) - 1] * 100.00 / sum(dt))
print timeStamp + "\t" + str('%.2f' %cpuPct) + "\t%"
you can put that in a file with a .py extension, do chmod 755 filename.py and ./filename.py to run it ... you can do ./filename.py > logfile to redirect the output to a file but i'm sure you knew that.
let me know if it works ok and if you want me to explain it .. if you have more than one cpu it might not behave right but that's an easy fix.
September 15th, 2005, 06:18 PM
Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.