如何使用Bash脚本从SAR报告中获取CPU和内存使用情况

如何使用Bash脚本从SAR报告中获取CPU和内存使用情况

大多数 Linux 管理员使用 SAR 报告监控系统性能,因为它会收集一周的性能数据。但是,你可以通过更改 /etc/sysconfig/sysstat 文件轻松地将其延长到四周。同样,这段时间可以延长一个月以上。如果超过 28,那么日志文件将放在多个目录中,每月一个。

要将覆盖期延长至 28 天,请对 /etc/sysconfig/sysstat 文件做以下更改。

编辑 sysstat 文件并将 HISTORY=7 更改为 HISTORY=28

在本文中,我们添加了三个 bash 脚本,它们可以帮助你在一个地方轻松查看每个数据文件的平均值。

这些脚本简单明了。出于测试目的,我们仅包括两个性能指标,即 CPU 和内存。你可以修改脚本中的其他性能指标以满足你的需求。

脚本 1:从 SAR 报告中获取平均 CPU 利用率的 Bash 脚本

该 bash 脚本从每个数据文件中收集 CPU 平均值并将其显示在一个页面上。

由于是月末,它显示了 2019 年 8 月的 28 天数据。

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">vi</span><span class="pln"> </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="kwd">sar</span><span class="pun">-</span><span class="pln">cpu</span><span class="pun">-</span><span class="pln">avg</span><span class="pun">.</span><span class="pln">sh</span>
  2.  
  3. <span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="pln">sh</span>
  4.  
  5. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"+----------------------------------------------------------------------------------+"</span>
  6. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"|Average: CPU %user %nice %system %iowait %steal %idle |"</span>
  7. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"+----------------------------------------------------------------------------------+"</span>
  8.  
  9. <span class="kwd">for</span><span class="pln"> </span><span class="kwd">file</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span class="str">`ls -tr /var/log/sa/sa* | grep -v sar`</span>
  10. <span class="kwd">do</span>
  11. <span class="pln"> dat</span><span class="pun">=</span><span class="str">`sar -f $file | head -n 1 | awk '{print $4}'`</span>
  12. <span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="pun">-</span><span class="pln">n $dat</span>
  13. <span class="pln"> </span><span class="kwd">sar</span><span class="pln"> </span><span class="pun">-</span><span class="pln">f $file </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i </span><span class="typ">Average</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">sed</span><span class="pln"> </span><span class="str">"s/Average://"</span>
  14. <span class="kwd">done</span>
  15.  
  16. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"+----------------------------------------------------------------------------------+"</span>

运行脚本后,你将看到如下输出。

  1. <span class="com">#</span><span class="pln"> sh </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="kwd">sar</span><span class="pun">-</span><span class="pln">cpu</span><span class="pun">-</span><span class="pln">avg</span><span class="pun">.</span><span class="pln">sh</span>
  2.  
  3. <span class="pun">+----------------------------------------------------------------------------------+</span>
  4. <span class="pun">|</span><span class="typ">Average</span><span class="pun">:</span><span class="pln"> CPU </span><span class="pun">%</span><span class="pln">user </span><span class="pun">%</span><span class="kwd">nice</span><span class="pln"> </span><span class="pun">%</span><span class="pln">system </span><span class="pun">%</span><span class="pln">iowait </span><span class="pun">%</span><span class="pln">steal </span><span class="pun">%</span><span class="pln">idle </span><span class="pun">|</span>
  5. <span class="pun">+----------------------------------------------------------------------------------+</span>
  6. <span class="lit">08</span><span class="pun">/</span><span class="lit">01</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">0.70</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">1.19</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">98.10</span>
  7. <span class="lit">08</span><span class="pun">/</span><span class="lit">02</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">1.73</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">3.16</span><span class="pln"> </span><span class="lit">0.01</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">95.10</span>
  8. <span class="lit">08</span><span class="pun">/</span><span class="lit">03</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">1.73</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">3.16</span><span class="pln"> </span><span class="lit">0.01</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">95.11</span>
  9. <span class="lit">08</span><span class="pun">/</span><span class="lit">04</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">1.02</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">1.80</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">97.18</span>
  10. <span class="lit">08</span><span class="pun">/</span><span class="lit">05</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">0.68</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">1.08</span><span class="pln"> </span><span class="lit">0.01</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">98.24</span>
  11. <span class="lit">08</span><span class="pun">/</span><span class="lit">06</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">0.71</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">1.17</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">98.12</span>
  12. <span class="lit">08</span><span class="pun">/</span><span class="lit">07</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">1.79</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">3.17</span><span class="pln"> </span><span class="lit">0.01</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">95.03</span>
  13. <span class="lit">08</span><span class="pun">/</span><span class="lit">08</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">1.78</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">3.14</span><span class="pln"> </span><span class="lit">0.01</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">95.08</span>
  14. <span class="lit">08</span><span class="pun">/</span><span class="lit">09</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">1.07</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">1.82</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">97.10</span>
  15. <span class="lit">08</span><span class="pun">/</span><span class="lit">10</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">0.38</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">0.50</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">99.12</span>
  16. <span class="pun">.</span>
  17. <span class="pun">.</span>
  18. <span class="pun">.</span>
  19. <span class="lit">08</span><span class="pun">/</span><span class="lit">29</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">1.50</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">2.33</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">96.17</span>
  20. <span class="lit">08</span><span class="pun">/</span><span class="lit">30</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> all </span><span class="lit">2.32</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">3.47</span><span class="pln"> </span><span class="lit">0.01</span><span class="pln"> </span><span class="lit">0.00</span><span class="pln"> </span><span class="lit">94.20</span>
  21. <span class="pun">+----------------------------------------------------------------------------------+</span>

脚本 2:从 SAR 报告中获取平均内存利用率的 Bash 脚本

该 bash 脚本从每个数据文件中收集内存平均值并将其显示在一个页面上。

由于是月末,它显示了 2019 年 8 月的 28 天数据。

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">vi</span><span class="pln"> </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="kwd">sar</span><span class="pun">-</span><span class="pln">memory</span><span class="pun">-</span><span class="pln">avg</span><span class="pun">.</span><span class="pln">sh</span>
  2.  
  3. <span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="pln">sh</span>
  4.  
  5. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"+-------------------------------------------------------------------------------------------------------------------+"</span>
  6. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"|Average: kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty |"</span>
  7. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"+-------------------------------------------------------------------------------------------------------------------+"</span>
  8.  
  9. <span class="kwd">for</span><span class="pln"> </span><span class="kwd">file</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span class="str">`ls -tr /var/log/sa/sa* | grep -v sar`</span>
  10. <span class="kwd">do</span>
  11. <span class="pln"> dat</span><span class="pun">=</span><span class="str">`sar -f $file | head -n 1 | awk '{print $4}'`</span>
  12. <span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="pun">-</span><span class="pln">n $dat</span>
  13. <span class="pln"> </span><span class="kwd">sar</span><span class="pln"> </span><span class="pun">-</span><span class="pln">r </span><span class="pun">-</span><span class="pln">f $file </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i </span><span class="typ">Average</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">sed</span><span class="pln"> </span><span class="str">"s/Average://"</span>
  14. <span class="kwd">done</span>
  15.  
  16. <span class="kwd">echo</span><span class="pln"> </span><span class="str">"+-------------------------------------------------------------------------------------------------------------------+"</span>

运行脚本后,你将看到如下输出。

  1. <span class="com">#</span><span class="pln"> sh </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="kwd">sar</span><span class="pun">-</span><span class="pln">memory</span><span class="pun">-</span><span class="pln">avg</span><span class="pun">.</span><span class="pln">sh</span>
  2.  
  3. <span class="pun">+--------------------------------------------------------------------------------------------------------------------+</span>
  4. <span class="pun">|</span><span class="typ">Average</span><span class="pun">:</span><span class="pln"> kbmemfree kbmemused </span><span class="pun">%</span><span class="pln">memused kbbuffers kbcached kbcommit </span><span class="pun">%</span><span class="pln">commit kbactive kbinact kbdirty </span><span class="pun">|</span>
  5. <span class="pun">+--------------------------------------------------------------------------------------------------------------------+</span>
  6. <span class="lit">08</span><span class="pun">/</span><span class="lit">01</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1492331</span><span class="pln"> </span><span class="lit">2388461</span><span class="pln"> </span><span class="lit">61.55</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1152142</span><span class="pln"> </span><span class="lit">1560615</span><span class="pln"> </span><span class="lit">12.72</span><span class="pln"> </span><span class="lit">1693031</span><span class="pln"> </span><span class="lit">380472</span><span class="pln"> </span><span class="lit">6</span>
  7. <span class="lit">08</span><span class="pun">/</span><span class="lit">02</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1493126</span><span class="pln"> </span><span class="lit">2387666</span><span class="pln"> </span><span class="lit">61.53</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1147811</span><span class="pln"> </span><span class="lit">1569624</span><span class="pln"> </span><span class="lit">12.79</span><span class="pln"> </span><span class="lit">1696387</span><span class="pln"> </span><span class="lit">373346</span><span class="pln"> </span><span class="lit">3</span>
  8. <span class="lit">08</span><span class="pun">/</span><span class="lit">03</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1489582</span><span class="pln"> </span><span class="lit">2391210</span><span class="pln"> </span><span class="lit">61.62</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1147076</span><span class="pln"> </span><span class="lit">1581711</span><span class="pln"> </span><span class="lit">12.89</span><span class="pln"> </span><span class="lit">1701480</span><span class="pln"> </span><span class="lit">370325</span><span class="pln"> </span><span class="lit">3</span>
  9. <span class="lit">08</span><span class="pun">/</span><span class="lit">04</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1490403</span><span class="pln"> </span><span class="lit">2390389</span><span class="pln"> </span><span class="lit">61.60</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1148206</span><span class="pln"> </span><span class="lit">1569671</span><span class="pln"> </span><span class="lit">12.79</span><span class="pln"> </span><span class="lit">1697654</span><span class="pln"> </span><span class="lit">373484</span><span class="pln"> </span><span class="lit">4</span>
  10. <span class="lit">08</span><span class="pun">/</span><span class="lit">05</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1484506</span><span class="pln"> </span><span class="lit">2396286</span><span class="pln"> </span><span class="lit">61.75</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1152409</span><span class="pln"> </span><span class="lit">1563804</span><span class="pln"> </span><span class="lit">12.75</span><span class="pln"> </span><span class="lit">1702424</span><span class="pln"> </span><span class="lit">374628</span><span class="pln"> </span><span class="lit">4</span>
  11. <span class="lit">08</span><span class="pun">/</span><span class="lit">06</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1473593</span><span class="pln"> </span><span class="lit">2407199</span><span class="pln"> </span><span class="lit">62.03</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1151137</span><span class="pln"> </span><span class="lit">1577491</span><span class="pln"> </span><span class="lit">12.86</span><span class="pln"> </span><span class="lit">1715426</span><span class="pln"> </span><span class="lit">371000</span><span class="pln"> </span><span class="lit">8</span>
  12. <span class="lit">08</span><span class="pun">/</span><span class="lit">07</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1467150</span><span class="pln"> </span><span class="lit">2413642</span><span class="pln"> </span><span class="lit">62.19</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1155639</span><span class="pln"> </span><span class="lit">1596653</span><span class="pln"> </span><span class="lit">13.01</span><span class="pln"> </span><span class="lit">1716900</span><span class="pln"> </span><span class="lit">372574</span><span class="pln"> </span><span class="lit">13</span>
  13. <span class="lit">08</span><span class="pun">/</span><span class="lit">08</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1451366</span><span class="pln"> </span><span class="lit">2429426</span><span class="pln"> </span><span class="lit">62.60</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1162253</span><span class="pln"> </span><span class="lit">1604672</span><span class="pln"> </span><span class="lit">13.08</span><span class="pln"> </span><span class="lit">1725931</span><span class="pln"> </span><span class="lit">376998</span><span class="pln"> </span><span class="lit">5</span>
  14. <span class="lit">08</span><span class="pun">/</span><span class="lit">09</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1451191</span><span class="pln"> </span><span class="lit">2429601</span><span class="pln"> </span><span class="lit">62.61</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1158696</span><span class="pln"> </span><span class="lit">1582192</span><span class="pln"> </span><span class="lit">12.90</span><span class="pln"> </span><span class="lit">1728819</span><span class="pln"> </span><span class="lit">371025</span><span class="pln"> </span><span class="lit">4</span>
  15. <span class="lit">08</span><span class="pun">/</span><span class="lit">10</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1450050</span><span class="pln"> </span><span class="lit">2430742</span><span class="pln"> </span><span class="lit">62.64</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1160916</span><span class="pln"> </span><span class="lit">1579888</span><span class="pln"> </span><span class="lit">12.88</span><span class="pln"> </span><span class="lit">1729975</span><span class="pln"> </span><span class="lit">370844</span><span class="pln"> </span><span class="lit">5</span>
  16. <span class="pun">.</span>
  17. <span class="pun">.</span>
  18. <span class="pun">.</span>
  19. <span class="lit">08</span><span class="pun">/</span><span class="lit">29</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1365699</span><span class="pln"> </span><span class="lit">2515093</span><span class="pln"> </span><span class="lit">64.81</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1198832</span><span class="pln"> </span><span class="lit">1593567</span><span class="pln"> </span><span class="lit">12.99</span><span class="pln"> </span><span class="lit">1781733</span><span class="pln"> </span><span class="lit">376157</span><span class="pln"> </span><span class="lit">15</span>
  20. <span class="lit">08</span><span class="pun">/</span><span class="lit">30</span><span class="pun">/</span><span class="lit">2019</span><span class="pln"> </span><span class="lit">1361920</span><span class="pln"> </span><span class="lit">2518872</span><span class="pln"> </span><span class="lit">64.91</span><span class="pln"> </span><span class="lit">29888</span><span class="pln"> </span><span class="lit">1200785</span><span class="pln"> </span><span class="lit">1595105</span><span class="pln"> </span><span class="lit">13.00</span><span class="pln"> </span><span class="lit">1784556</span><span class="pln"> </span><span class="lit">375641</span><span class="pln"> </span><span class="lit">8</span>
  21. <span class="pun">+-------------------------------------------------------------------------------------------------------------------+</span>

脚本 3:从 SAR 报告中获取 CPU 和内存平均利用率的 Bash 脚本

该 bash 脚本从每个数据文件中收集 CPU 和内存平均值并将其显示在一个页面上。

该脚本与上面相比稍微不同。它在同一位置同时显示两者(CPU 和内存)平均值,而不是其他数据。

  1. <span class="com">#</span><span class="pln"> </span><span class="kwd">vi</span><span class="pln"> </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="kwd">sar</span><span class="pun">-</span><span class="pln">cpu</span><span class="pun">-</span><span class="pln">mem</span><span class="pun">-</span><span class="pln">avg</span><span class="pun">.</span><span class="pln">sh</span>
  2.  
  3. <span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="kwd">bash</span>
  4.  
  5. <span class="kwd">for</span><span class="pln"> </span><span class="kwd">file</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span class="str">`ls -tr /var/log/sa/sa* | grep -v sar`</span>
  6. <span class="kwd">do</span>
  7. <span class="pln"> </span><span class="kwd">sar</span><span class="pln"> </span><span class="pun">-</span><span class="pln">f $file </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">head</span><span class="pln"> </span><span class="pun">-</span><span class="pln">n </span><span class="lit">1</span><span class="pln"> </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $4}'</span>
  8. <span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"-----------"</span>
  9. <span class="pln"> </span><span class="kwd">sar</span><span class="pln"> </span><span class="pun">-</span><span class="pln">u </span><span class="pun">-</span><span class="pln">f $file </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'/Average:/{printf("CPU Average: %.2f%\n"), 100 - $8}'</span>
  10. <span class="pln"> </span><span class="kwd">sar</span><span class="pln"> </span><span class="pun">-</span><span class="pln">r </span><span class="pun">-</span><span class="pln">f $file </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'/Average:/{printf("Memory Average: %.2f%\n"),(($3-$5-$6)/($2+$3)) * 100 }'</span>
  11. <span class="pln"> </span><span class="kwd">printf</span><span class="pln"> </span><span class="str">"\n"</span>
  12. <span class="kwd">done</span>

相关推荐