有没有办法在CentOS中获得CPU使用率?我需要解析这些信息并从Perl脚本中绘制图形,因此它最好是一个打印出一个单一输出的简单工具。
答案 0 :(得分:3)
更简单,看看/proc/loadavg
,它显示的内容如下:
$ cat /proc/loadavg
0.37 0.67 0.68 1/312 8594
前三个数字是“你需要处理当前负载的cpu数量”。这意味着在多核上你可以有5个负载没有问题。这三个数字在不同的时间间隔内平均(1,5和15分钟,根据man uptime
)
答案 1 :(得分:2)
我实际上使用了/ proc / stat ...这里是我用于dzen2小部件的python的重要部分。
while(1):
f = file("/proc/stat","r")
fcon = f.read().split("\n")[0].split()
new.user, unice, new.sys, new.idle, new.iowait = [
int(x) for x in fcon[1:6] ]
new.user += unice
master.user = new.user - old.user
master.sys = new.sys - old.sys
master.idle = new.idle - old.idle
master.iowait = new.iowait - old.iowait
maxval = master.user + master.sys + master.idle + master.iowait
curval = master.user + master.sys + master.iowait
old.copy(new)
print (float(curval)/maxval) * 100 )
time.sleep(1)
这将打印处理器使用百分比的次数。
我知道错误的语言,但您应该能够了解输出的哪些列引用的内容。
答案 2 :(得分:1)
您可以在批处理模式下使用top
进行1次(或更多次)迭代。然后使用grep
来解析它。
$ top -b -n 1 | grep -2 "load average"
top - 12:08:31 up 14 days, 19:03, 26 users, load average: 0.25, 0.45, 0.37
Tasks: 219 total, 1 running, 217 sleeping, 0 stopped, 1 zombie
Cpu(s): 4.2%us, 0.6%sy, 0.0%ni, 94.0%id, 1.0%wa, 0.0%hi, 0.3%si, 0.0%st
或
$ top -b -n 1 | grep "Cpu(s)\:"
Cpu(s): 4.2%us, 0.6%sy, 0.0%ni, 94.0%id, 1.0%wa, 0.0%hi, 0.3%si, 0.0%st
此外,您可以使用awk
来获取特定列并从那里开始工作。
$ top -b -n 1 | grep "Cpu(s)\:" | awk '{print $2}'
4.2%us,
答案 3 :(得分:1)
我使用“top”命令结合了一些“grep”,“sed”和“awk”命令:
答案 4 :(得分:0)
atop @ http://www.atoptool.nl/ 将写入日志文件等。详情请参阅:http://www.atoptool.nl/systemreports.php