linux cpu加载超过X min

时间:2015-03-31 14:22:39

标签: linux

你好专家!
能否请你帮忙。我想计算过去5分钟的CPU负载。我需要永久地获得这些价值。

我找到了非常有用和详细的文章:http://phoxis.org/2013/09/05/finding-overall-and-per-core-cpu-utilization/
但是我不确定检索这些数字的最佳方法是:每隔一段时间运行脚本,并且每300次迭代计算一次超量数(例如)? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

如果您编写自己的代码来监控使用情况,那么您的计算等效于海森堡的不确定性原则。或者,您可以使用“uptime”命令。

% uptime
  16:00:29 up 3 days, 18:31,  2 users,  load average: 0.04, 0.12, 0.09

最后三个数字是过去1,5和15分钟内的系统负载平均值。从手册页:

   System load averages is the average number of processes that are
   either in a runnable or uninterruptable state.  A process  in  a
   runnable  state  is  either  using the CPU or waiting to use the
   CPU.  A process in uninterruptable state is waiting for some I/O
   access,  eg  waiting  for disk.  The averages are taken over the
   three time intervals.  Load averages are not normalized for  the
   number  of CPUs in a system, so a load average of 1 means a sin-
   gle CPU system is loaded all the time while on a 4 CPU system it
   means it was idle 75% of the time.

只要您知道系统中有多少CPU,就可以满足您的需求。

第二次迭代:

# cat /proc/uptime
353615.60 344666.42

从/ proc手册页:

   /proc/uptime
          This file contains two numbers: the uptime of the sys-
          tem  (seconds),  and  the amount of time spent in idle
          process (seconds).

您可以每五分钟轮询一次并记录值的变化。