bash 用于记录 Linux 进程的 CPU 和内存使用情况的 Shell 脚本

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

Shell script for logging cpu and memory usage of a linux process

linuxbashshellawk

提问by Andy Dufresne

I am looking for a way to log and graphically display cpu and RAM usage of linux processes over time. Since I couldn't find a simple tool to so (I tried zabbix and munin but installation failed) I started writing a shell script to do so

我正在寻找一种方法来记录并以图形方式显示 Linux 进程随时间推移的 CPU 和 RAM 使用情况。由于我找不到一个简单的工具(我尝试了 zabbix 和 munin 但安装失败)我开始编写一个 shell 脚本来这样做

The script file parses the output of top command through awk and logs into a csv file. It

脚本文件通过awk解析top命令的输出,并记录到csv文件中。它

  1. Figures out the pid of the processes through ps command
  2. Uses top and awk to log cpu and memory usage.
  1. 通过ps命令找出进程的pid
  2. 使用 top 和 awk 来记录 CPU 和内存使用情况。

Here is how the script looks like

这是脚本的样子

#!/bin/sh
#A script to log the cpu and memory usage of linux processes namely - redis, logstash, elasticsearch and kibana

REDIS_PID=$(ps -ef | grep redis | grep -v grep | awk '{print }')

LOGSTASH_PID=$(ps -ef | grep logstash | grep -v grep | awk '{print }')

ELASTICSEARCH_PID=$(ps -ef | grep elasticsearch | grep -v grep | awk '{print }')

KIBANA_PID=$(ps -ef | grep kibana | grep -v grep | awk '{print }')

LOG_FILE=/var/log/user/usage.log
echo $LOG_FILE
top -b | awk -v redis="$REDIS_PID" -v logstash="$LOGSTASH_PID" '/redis|logstash/ {print ","","","}'

How do I

我如何能

  1. Print the resource usage for multiple processes. Specifying multiple variables in the awk pattern is not working. It prints the usage for the first pid (redis in the above script)
  2. Print current timestamp when printing the resource details (through date +"%T")
  3. Print the process name along with the resource usage. Redis, Logstash, ElasticSearch or Kibana in the above case
  4. Redirect the above commands output to a log file. I tried > $LOG_FILE but it didn't work.
  1. 打印多个进程的资源使用情况。在 awk 模式中指定多个变量不起作用。它打印第一个pid的用法(上面脚本中的redis)
  2. 打印资源详情时打印当前时间戳(截止日期+"%T")
  3. 打印进程名称以及资源使用情况。上述案例中的Redis、Logstash、ElasticSearch或Kibana
  4. 将上述命令输出重定向到日志文件。我试过 > $LOG_FILE 但它没有用。

Thoughts/Inputs?

想法/输入?

Thanks in advance.

提前致谢。

回答by anubhava

To figure out PIDs you can simplify your script greatly using pgrep:

要找出 PID,您可以使用 pgrep 大大简化您的脚本:

REDIS_PID=$(pgrep -f redis)

LOGSTASH_PID=$(pgrep -f logstash)

ELASTICSEARCH_PID=$(pgrep -f elasticsearch)

KIBANA_PID=$(pgrep -f kibana)

EDIT:Sorry had to leave for some work and couldn't provide the full answer.

编辑:抱歉不得不离开一些工作,无法提供完整的答案。

In order to capture top's output use following script:

为了捕获 top 的输出,请使用以下脚本:

while :; do 
  top -n 1 -b | awk -v redis="$REDIS_PID" -v logstash="$LOGSTASH_PID"
         ' == redis ||  == logstash {print ","","","}' >> $LOG_FILE
  sleep 3
done