如何在 Linux CPU、内存和时间中监控进程

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

How to monitor a process in Linux CPU, Memory and time

linuxmemoryprocessmonitoringcpu

提问by Pedro

How can I benchmark a process in Linux? I need something like "top" and "time" put together for a particular process name (it is a multiprocess program so many PIDs will be given)?

如何在 Linux 中对进程进行基准测试?我需要将诸如“top”和“time”之类的东西放在一起用于特定进程名称(它是一个多进程程序,因此会给出许多 PID)?

Moreover I would like to have a plot over time of memory and cpu usage for these processes and not just final numbers.

此外,我想对这些进程的内存和 cpu 使用情况绘制一个图,而不仅仅是最终数字。

Any ideas?

有任何想法吗?

采纳答案by stylusbrook

Following are the tools for monitoring a linux system

以下是监控linux系统的工具

  1. System commands like top, free -m, vmstat, iostat, iotop, sar, netstat, etc. Nothing comes near these linux utility when you are debugging a problem. These command give you a clear picture that is going inside your server
  2. SeaLion: Agent executes all the commands mentioned in #1 (also user defined) and outputs of these commands can be accessed in a beautiful web interface. This tool comes handy when you are debugging across hundreds of servers as installation is clear simple. And its FREE
  3. Nagios: It is the mother of all monitoring/alerting tools. It is very much customization but very much difficult to setup for beginners. There are sets of tools called nagios plugins that covers pretty much all important Linux metrics
  4. Munin
  5. Server Density: A cloudbased paid service that collects important Linux metrics and gives users ability to write own plugins.
  6. New Relic: Another well know hosted monitoring service.
  7. Zabbix
  1. 系统命令,如top, free -m, vmstat, iostat, iotop, sar, netstat, 等。当您调试问题时,这些 linux 实用程序无能为力。这些命令为您提供服务器内部的清晰图片
  2. SeaLion:代理执行 #1 中提到的所有命令(也是用户定义的),并且可以在漂亮的 Web 界面中访问这些命令的输出。当您在数百台服务器上进行调试时,这个工具会派上用场,因为安装非常简单。而且它是免费的
  3. Nagios:它是所有监控/警报工具之母。这是非常多的定制,但对于初学者来说设置起来非常困难。有一组称为 nagios 插件的工具,涵盖了几乎所有重要的 Linux 指标
  4. 穆宁
  5. 服务器密度:一种基于云的付费服务,可收集重要的 Linux 指标并让用户能够编写自己的插件。
  6. New Relic:另一个众所周知的托管监控服务。
  7. 扎比克斯

回答by Richard Troxell

I typically throw together a simple script for this type of work.

我通常会为此类工作编写一个简单的脚本。

Take a look at the kernel documentation for the proc filesystem (Google 'linux proc.txt').

查看 proc 文件系统的内核文档(Google 'linux proc.txt')。

The first line of /proc/stat(Section 1.8 in proc.txt) will give you cumulative cpu usage stats (i.e. user, nice, system, idle, ...). For each process, the file /proc/$PID/stat(Table 1-4 in proc.txt) will provide you with both process-specific cpu usage stats and memory usage stats (see rss).

第一行/proc/stat(proc.txt 中的第 1.8 节)将为您提供累积的 CPU 使用统计信息(即用户、nice、系统、空闲等)。对于每个进程,该文件/proc/$PID/stat(proc.txt 中的表 1-4)将为您提供特定于进程的 CPU 使用情况统计信息和内存使用情况统计信息(请参阅 rss)。

If you google a bit you'll find plenty of detailed info on these files, and pointers to libraries / apps / code snippets that can help you obtain / derive the values you need. With that in mind, I'll focus on the high-level strategy.

如果你用谷歌搜索一下,你会发现很多关于这些文件的详细信息,以及指向库/应用程序/代码片段的指针,可以帮助你获取/导出你需要的值。考虑到这一点,我将专注于高级策略。

For CPU stats, use your favorite scripting language to create an executable that takes a set of process ids for monitoring. At a fixed interval (ex: 1 second) poll / calculate the cumulative totals for each process and the system as a whole. During each poll interval, write all results on a single line to stdout.

对于 CPU 统计信息,请使用您最喜欢的脚本语言创建一个可执行文件,该可执行文件采用一组进程 ID 进行监控。以固定间隔(例如:1 秒)轮询/计算每个进程和整个系统的累计总数。在每个轮询间隔期间,将一行中的所有结果写入标准输出。

For memory stats, write a similar script, but simply log the per-process memory usage. Memory is a bit easier as we directly obtain the instantaneous values.

对于内存统计,编写一个类似的脚本,但只需记录每个进程的内存使用情况。由于我们直接获得瞬时值,因此记忆会更容易一些。

Run these script for the duration of your test, passing the set of processes ids that you'd like to monitor and redirecting its output to a log file.

在测试期间运行这些脚本,传递您想要监控的进程 ID 集并将其输出重定向到日志文件。

./logcpu $(pidof foo) $(pidof bar) > cpustats
./logmem $(pidof foo) $(pidof bar) > memstats

Import the contents of these files into a spreadsheet (for certain applications this is as easy as copy / paste). For CPU, you are after instantaneous values but have cumulative values, so you'll need to do some minor spreadsheet work to derive these values (it's just the delta 't(x + 1) - t(x)'). Of course you could have your cpu logger write the delta, but you'll be spending a bit more time up front on the script.

将这些文件的内容导入电子表格(对于某些应用程序,这就像复制/粘贴一样简单)。对于 CPU,您追求的是瞬时值但具有累积值,因此您需要做一些小的电子表格工作来导出这些值(它只是 delta 't(x + 1) - t(x)')。当然,您可以让 CPU 记录器编写增量,但是您将在脚本上花费更多时间。

Finally, use your spreadsheet to generate a nice plot.

最后,使用您的电子表格生成一个漂亮的绘图。