2015-10-05 54 views
0

在我的java程序中,我使用Jsch连接到远程服务器,现在我需要获得该服务器的Cpu利用率。我已经使用iostat -c命令来获取CPU的详细信息...;我得到的细节如下:实时总CPU平均负载命令

Linux 3.1.0-1.2-desktop (ccitsuse06) 10/05/2015  _i686_ (4 CPU) 
avg-cpu: %user %nice %system %iowait %steal %idle 
      0.06 0.00 0.03 0.00 0.00 99.91 

我真正需要的总average CPU load;我怎样才能得到相同的使用命令?有没有简单的单一命令可以实时给出总平均Cpu ......还是我需要解析这些数据的结果?再次;什么因素有助于总平均CPU,以便我可以计算。我读过以前的类似问题,但没有一个能够解决我的疑问。

*请注意,这是我需要我的程序当前/实时平均CPU负载,我希望尽可能准确执行的命令,并尽可能快..

我一直在阅读使用awk,egrep等,但我仍然不知道如何使用这个命令来达到我想要的结果!

+1

这个问题属于superuser.com – specializt

+0

目前还不清楚“当前/实时平均”是什么意思。你想要现在的价值,还是最近一段时间的平均价值?彼得的答案假设后者;我认为前者。 –

+0

谢谢......我认为它会......而且当我逐步执行命令时,我对grep函数有了更好的理解......,并且我需要平均的CpuLoad(意味着此中所有4个内核的平均负载情况)即时;即;当前值。我认为你的命令对我来说工作得很好....谢谢.... –

回答

0

尝试

mpstat -u | grep -A 1 '%idle' | tail -1 | sed -r 's/ +/ /g' | cut -d' ' -f12 

如果你想了解它在做什么,你应该尝试执行它的点点滴滴(开始只是第一部分,然后再加上grep部分等)。

这会使用mpstat获取所有统计数据;然后仅使用grep的相关行;使用tail摆脱标题行;使用sed压缩额外空间;然后使用cut获得第12列。

这会给你的空闲看书,作为一个值超出100把它变成一个百分比数字CPU使用率,你需要减去100

0

该值以获取平均CPU加载一段时间后,您需要查看该时间段开始和结束时使用的CPU。您可以对具有每个逻辑CPU使用时间量的/proc/stat进行采样,或者需要运行实用程序以获取您想要采样的时间量。

例如

head -1 /proc/stat 

打印

cpu 170661 1384 22478 7836920 3244 0 2645 0 0 0 

从文档,这是

cpu 3357 0 4313 1362393 
    The amount of time, measured in units of USER_HZ 
    (1/100ths of a second on most architectures, use 
    sysconf(_SC_CLK_TCK) to obtain the right value), that 
    the system spent in various states: 

    user (1) Time spent in user mode. 

    nice (2) Time spent in user mode with low priority 
      (nice). 

    system (3) Time spent in system mode. 

    idle (4) Time spent in the idle task. This value 
      should be USER_HZ times the second entry in the 
      /proc/uptime pseudo-file. 

    iowait (since Linux 2.5.41) 
      (5) Time waiting for I/O to complete. 

    irq (since Linux 2.6.0-test4) 
      (6) Time servicing interrupts. 

    softirq (since Linux 2.6.0-test4) 
      (7) Time servicing softirqs. 

    steal (since Linux 2.6.11) 
      (8) Stolen time, which is the time spent in 
      other operating systems when running in a 
      virtualized environment 

    guest (since Linux 2.6.24) 
      (9) Time spent running a virtual CPU for guest 
      operating systems under the control of the Linux 
      kernel. 

    guest_nice (since Linux 2.6.33) 
      (10) Time spent running a niced guest (virtual 
      CPU for guest operating systems under the 
      control of the Linux kernel). 

如果你采取任何两个样本之间这些数值的差异,你可以的时候忙的百分比。