bash SAR命令输入多个文件

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

Input multiple files for SAR command

bashwildcardsar

提问by user3795293

I am trying to input multiple sar files for a graph I am generating. I can input the files one at a time like so:

我正在尝试为我生成的图形输入多个 sar 文件。我可以一次输入一个文件,如下所示:

LC_ALL=C sar -A -f /var/log/sa/sa05 >> /tmp/sar.data3.txt

LC_ALL=C sar -A -f /var/log/sa/sa05 >> /tmp/sar.data3.txt

This systax does not work, but this is the idea of what I'm trying to do:

这个 systax 不起作用,但这是我想要做的想法:

LC_ALL=C sar -A -f /var/log/sa/sa* >> /tmp/sar.data3.txt

LC_ALL=C sar -A -f /var/log/sa/sa* >> /tmp/sar.data3.txt

回答by AKS

sa* files seems to be binary files i.e. cat on one of the saXX file will not echo valid human readable words. The following can be used to see its human readable contents.

sa* 文件似乎是二进制文件,即 saXX 文件之一上的 cat 不会回显有效的人类可读单词。以下内容可用于查看其人类可读的内容。

strings /var/log/sa/sa01 or saXX 

The file you might need is sarXX: you can try "LC_ALL=C ...." command as you mentioned above in place of just what I mentioned below.

您可能需要的文件是 sarXX:您可以尝试使用上面提到的“LC_ALL=C ....”命令来代替我下面提到的命令。

for file in /var/log/sa/sar*; do sar -A -f "$file"  >> /tmp/sar.data3.txt; done

Now, the following command will show/have what you need.

现在,以下命令将显示/拥有您需要的内容。

cat /tmp/sar.data3.txt 

I didn't see all the options of SAR command, would recommend you to check if there is any which would support sar* or sar??

我没有看到 SAR 命令的所有选项,建议您检查是否有支持 sar* 或 sar 的选项?

回答by David C. Rankin

You can use the find command to aggregate the files and concatenate them into your output file with something similar to:

您可以使用 find 命令聚合文件并将它们连接到您的输出文件中,类似于:

find /var/log/sa -type f -name "sa*" \
-exec LC_ALL=C sar -A -f '{}' >> /tmp/sar.data3.txt \;

What you are attempting to do looks to be failing due to sarnot accepting multiple input files with the -f option. In the future, you may consider using the -ooption to prepare files containing the data you want in records that can later be output with the -foption

由于sar不接受带有 -f 选项的多个输入文件,您尝试执行的操作看起来失败了。将来,您可以考虑使用该-o选项准备包含您想要的记录中的数据的文件,这些数据稍后可以使用该-f选项输出

回答by Thyag

To see the multiple-day sar report for memorystats:

要查看内存统计信息的多日 sar 报告:

find /var/log/sa/ -type f|grep sar|xargs grep kbmem -A144

To see the CPU status, try this

要查看CPU 状态,请尝试此操作

find /var/log/sa/ -type f|grep sar|xargs grep "CPU     %user" -A720

How it works

这个怎么运作

The directory /var/log/sa/ will have files such as sar23, sar24, sar25, sar26, sar27. The sar-suffix corresponds to the day of the month.

目录 /var/log/sa/ 将包含 sar23、sar24、sar25、sar26、sar27 等文件。sar-后缀对应于月份中的日期。

  • First find and grep only the sar-suffix files
  • Then pass the sar files as argument to the "grep keyword" command. This will give you only the header of the parameter you are interested in (kbmem for memory, or "CPU %user" for cpu status)
  • But you would also like the next 144 lines afterthe header for memory details (or 720 lines for CPU). So include the -A144 or -A720 options to the grep command.
  • 首先只查找并grep sar-suffix 文件
  • 然后将 sar 文件作为参数传递给“grep 关键字”命令。这将只为您提供您感兴趣的参数的标头(kbmem 表示内存,或“CPU %user”表示 CPU 状态)
  • 但是您还希望在内存详细信息的标题之后的下 144 行(或 CPU 的 720 行)。因此,在 grep 命令中包含 -A144 或 -A720 选项。

Note:

笔记:

  • For memory stats, a single line is logged every 10 mins. Hence 144 lines per day.
  • For CPU stats, 5 lines are logged every 10 mins, hence 720 lines per day (at least in my env). This might be different in your env, so please verify or calculate this manually at your end.
  • 对于内存统计,每 10 分钟记录一行。因此每天 144 行。
  • 对于 CPU 统计数据,每 10 分钟记录 5 行,因此每天 720 行(至少在我的环境中)。这在您的环境中可能会有所不同,因此请最终手动验证或计算。

If you love aliases:

如果你喜欢别名:

alias sarcpu='find /var/log/sa/ -type f|grep sar|xargs grep CPU     %user -A720'
alias sario='find /var/log/sa/ -type f|grep sar|xargs grep bread -A144'
alias sarmem='find /var/log/sa/ -type f|grep sar|xargs grep kbmem -A144'