Bash-Command 获取当前网络带宽使用情况和 CPU 使用情况

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14258517/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:13:52  来源:igfitidea点击:

Bash-Command to get the current network bandwith usage and CPU Usage

linuxbashunixubuntuconsole

提问by Racer

I need some command line snippets to get the current CPU Usage (in percent) and the current network bandwith usage.

我需要一些命令行片段来获取当前的 CPU 使用率(百分比)和当前的网络带宽使用率。

To get the CPU-Usage i found top -bn1 | grep "Cpu(s)"But it always give me

为了获得我发现的 CPU 使用率top -bn1 | grep "Cpu(s)"但它总是给我

%Cpu(s): 12,3 us, 3,6 sy, 0,0 ni, 83,7 id, 0,3 wa, 0,0 hi, 0,0 si, 0,0 st

%Cpu(s): 12,3 us, 3,6 sy, 0,0 ni, 83,7 id, 0,3 wa, 0,0 hi, 0,0 si, 0,0 st

The numbers given by this command are never changing. I tried to set the server under heavy-load and i got 83,7% idle as well.

此命令给出的数字永远不会改变。我试图将服务器设置为高负载,并且我也有 83.7% 的空闲时间。

As well i found ps aux|awk 'NR > 0 { s +=$3 }; END {print "cpu %",s}'These values are changing, what looks nice, but i get values like

我还发现ps aux|awk 'NR > 0 { s +=$3 }; END {print "cpu %",s}'这些值正在发生变化,看起来不错,但我得到了类似的值

403.8

407.3

524.6

553.2

403.8

407.3

524.6

553.2

And I don't know, what these values mean I have 4 cores in my computer so i would expect 400% CPU-Usage means all cores are on 100% But how can I have more than 400% usage? even, if htop says the average CPU usage is about 3 - 5%

而且我不知道,这些值意味着我的计算机中有 4 个内核,所以我希望 400% CPU 使用率意味着所有内核都在 100% 上,但是我怎么能有超过 400% 的使用率呢?甚至,如果 htop 说平均 CPU 使用率约为 3 - 5%

For the network traffic monitor I haven't found anything usable. I just need the numbers in/out in kbps to use them in another program / script

对于网络流量监视器,我没有发现任何可用的东西。我只需要输入/输出 kbps 的数字即可在另一个程序/脚本中使用它们

Thanks a lot four your help :)

非常感谢四位您的帮助:)

回答by jgr

For the network usage you could poll data directly from /proc/net/dev.

对于网络使用情况,您可以直接从/proc/net/dev.

回答by Aaron Digulla

Try the command uptimewhich gives you the uptime and load averages for the past 1, 5, and 15 minutes.

尝试使用该命令uptime为您提供过去 1、5 和 15 分钟的正常运行时间和负载平均值。

The bandwidth can be found in the files /sys/class/net/$1/statistics/rx_bytesand /sys/class/net/$1/statistics/tx_bytes

带宽可以在文件/sys/class/net/$1/statistics/rx_bytes/sys/class/net/$1/statistics/tx_bytes

You should remember the current value and look again after a second to get the current bandwidth usage.

您应该记住当前值并在一秒钟后再次查看以获取当前带宽使用情况。

One word regarding load: A lot of people think that CPU usage is a useful measurement. That's not correct. It's easy to have a system where many processes are waiting for data (network, disk, etc) but since they are waiting, the CPU load will be low. If you try to work on such a system, it will still feel sluggish.

关于负载的一句话:很多人认为 CPU 使用率是一种有用的衡量标准。那不正确。有一个系统很容易有许多进程在等待数据(网络、磁盘等),但由于它们在等待,CPU 负载会很低。如果您尝试在这样的系统上工作,它仍然会感到迟钝。

So the system load is a much better meter for how "busy" your system is.

因此,系统负载是衡量系统“繁忙”程度的一个更好的指标。

And you should have a look at tools like atopwhich monitor the system constantly and give you historical data, like the processes which hogged the CPU in the past. (intro)

并且您应该查看诸如atop持续监控系统并为您提供历史数据之类的工具,例如过去占用 CPU 的进程。(介绍

EDITThe reason why top -bn1 | grepalways prints the same result is because you're measuring the startup of topitself. You need to run a monitor over an extended period of time before you can start use the values it gives you.

编辑之所以top -bn1 | grep总是打印相同的结果是因为您正在测量top自身的启动。您需要长时间运行监视器,然后才能开始使用它提供的值。