Linux 如何测量 CPU 使用率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7908953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to measure CPU usage
提问by dabest1
I would like to log CPU usage at a frequency of 1 second.
我想以 1 秒的频率记录 CPU 使用率。
One possible way to do it is via vmstat 1
command.
一种可能的方法是通过vmstat 1
命令。
The problem is that the time between each output is not always exactly one second, especially on a busy server. I would like to be able to output the timestamp along with the CPU usage every second. What would be a simple way to accomplish this, without installing special tools?
问题是每次输出之间的时间并不总是一秒,尤其是在繁忙的服务器上。我希望能够每秒输出时间戳和 CPU 使用率。在不安装特殊工具的情况下,实现此目的的简单方法是什么?
采纳答案by Simon C
Use the well-known UNIX tool top
that is normally available on Linux systems:
使用top
通常在 Linux 系统上可用的众所周知的 UNIX 工具:
top -b -d 1 > /tmp/top.log
The first line of each output block from top
contains a timestamp.
每个输出块的第一行top
包含一个时间戳。
I see no command line option to limit the number of rows that top
displays.
我看不到命令行选项来限制top
显示的行数。
Section 5a. SYSTEM Configuration Fileand 5b. PERSONAL Configuration Fileof the top
man page describes pressing W
when running top
in interactive mode to create a $HOME/.toprc
configuration file.
第5a节。系统配置文件和5b。个人配置文件中的top
手册页介绍了按W
运行时,top
在交互模式下创建$HOME/.toprc
配置文件。
I did this, then edited my .toprc
file and changed all maxtasks
values so that they are maxtasks=4
. Then top
only displays 4 rows of output.
我这样做了,然后编辑了我的.toprc
文件并更改了所有maxtasks
值,以便它们是maxtasks=4
. 然后top
只显示 4 行输出。
For completeness, the alternative way to do this using pipes is:
为了完整起见,使用管道执行此操作的另一种方法是:
top -b -d 1 | awk '/load average/ {n=10} {if (n-- > 0) {print}}' > /tmp/top.log
回答by Vasileios Lekakis
There are many ways to do that. Except top another way is to you the "sar" utility. So something like
有很多方法可以做到这一点。除了顶部,另一种方式是“sar”实用程序。所以像
sar -u 1 10
will give you the cpu utilization for 10 times every 1 second. At the end it will print averages for each one of the sys, user, iowait, idle
每 1 秒会给你 10 次的 CPU 利用率。最后,它将打印 sys、user、iowait、idle 中每一个的平均值
Another utility is the "mpstat", that gives you similar things with sar
另一个实用程序是“mpstat”,它为您提供与 sar 类似的功能
回答by Onlyjob
回答by dabest1
I found a neat way to get the timestamp information to be displayed along with the output of vmstat.
我找到了一种巧妙的方法来获取时间戳信息以及 vmstat 的输出。
Sample command:
vmstat -n 1 3 | while read line; do echo "$(date --iso-8601=seconds) $line"; done
示例命令:
vmstat -n 1 3 | 读行时;做 echo "$(date --iso-8601=seconds) $line"; 完毕
Output:
2013-09-13T14:01:31-0700 procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
2013-09-13T14:01:31-0700 r b swpd free buff cache si so bi bo in cs us sy id wa
2013-09-13T14:01:31-0700 1 1 4197640 29952 124584 12477708 12 5 449 147 2 0 7 4 82 7
2013-09-13T14:01:32-0700 3 0 4197780 28232 124504 12480324 392 180 15984 180 1792 1301 31 15 38 16
2013-09-13T14:01:33-0700 0 1 4197656 30464 124504 12477492 344 0 2008 0 1892 1929 32 14 43 10
输出:
2013-09-13T14:01:31-0700 procs -----------内存--------- ---swap--- -----io-- -- --system-- ----cpu----
2013-09-13T14:01:31-0700 rb swpd free buff cache si so bi bo in cs us sy id wa
2013-09-13T14:01: 31-0700 1 1 4197640 29952 124584 12477708 12 5 449 147 2 0 7 4 82 7
2013-09-13T14:01:32-0700 3 0 4197780 28232 124504 12480324 392 180 15984 180 1792 1301 31 15 38 16
2013-09- 13T14:01:33-0700 0 1 4197656 30464 124504 12477492 344 0 2008 0 1892 1929 32 14 43 10
回答by Brian van Rooijen
To monitor the disk usage, cpu and load i created a small bash scripts that writes the values to a log file every 10 seconds.
为了监控磁盘使用情况、cpu 和负载,我创建了一个小的 bash 脚本,每 10 秒将值写入一个日志文件。
This logfile is processed by logstash kibana and riemann.
此日志文件由 logstash kibana 和 riemann 处理。
# #!/usr/bin/env bash
# Define a timestamp function
LOGPATH="/var/log/systemstatus.log"
timestamp() {
date +"%Y-%m-%dT%T.%N"
}
#server load
while ( sleep 10 ) ; do
echo -n "$(timestamp) linux::systemstatus::load " >> $LOGPATH
cat /proc/loadavg >> $LOGPATH
#cpu usage
echo -n "$(timestamp) linux::systemstatus::cpu " >> $LOGPATH
top -bn 1 | sed -n 3p >> $LOGPAT
#disk usage
echo -n "$(timestamp) linux::systemstatus::storage " >> $LOGPATH
df --total|grep total|sed "s/total//g"| sed 's/^ *//' >> $LOGPATH
done