Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Bash script to get CPU and memory usage

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use Bash scripts to get CPU and memory usage". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use Bash scripts to get CPU and memory usage.

Script 1: Bash script to get average CPU utilization from SAR report

The bash script collects CPU averages from each data file and displays them on a page.

Since it is the end of the month, it shows 28 days of data for August 2019.

# vi / opt/scripts/sar-cpu-avg.sh #! / bin/sh echo "+-+" echo "| Average: CPU% user % nice% system% iowait% steal% idle | "echo" +-+ "for file in `ls-tr / var/log/sa/ Sa* | grep-v sar`do dat= `sar-f $file | head-n 1 | awk'{print $4} '`echo-n $dat sar-f $file | grep-I Average | sed "s/Average://" done echo +- -+ "

After running the script, you will see the following output.

# sh / opt/scripts/sar-cpu-avg.sh +-+ | Average: CPU% user% nice% system% iowait % steal% idle | +-+ 08 Universe 01Universe 2019 all 0.70 0.00 1.19 0.00 0.00 98.1008/02/2019 all 1.73 0.00 3.16 0.01 0.00 95.1008/03/2019 all 1.73 0.00 3.16 0.01 0.00 95.1108/04/2019 all 1.02 0.00 1.80 0.00 0.00 97 .1808 / 05/2019 all 0.68 0.00 1.08 0.01 0.00 98.2408/06/2019 all 0.71 0.00 1.17 0.00 0.00 98.1208/07/2019 all 1.79 0.00 3.17 0.01 0.00 95.0308/08/2019 All 1.78 0.00 3.14 0.01 0.00 95.0808/09/2019 all 1.07 0.00 1.82 0.00 0.00 97.1008/10/2019 all 0.38 0.00 0.50 0.00 0.00 99.12...08/29/2019 All 1.50 0.00 2.33 0.00 0.00 96.1708 + 30max 2019 all 2.32 0.00 3.47 0.01 0.00 94.20- -- + script 2: Bash script to get the average memory utilization from the SAR report

The bash script collects memory averages from each data file and displays them on a page.

Since it is the end of the month, it shows 28 days of data for August 2019.

# vi / opt/scripts/sar-memory-avg.sh #! / bin/sh echo "+- -+ "echo" | Average: kbmemfree kbmemused% memused kbbuffers kbcached kbcommit% commit kbactive kbinact kbdirty | "echo" + -- + "for file in `ls-tr / var/log/sa/sa* | grep-v sar`do dat= `sar-f $file | head-N1 | awk'{print $4} '`echo-n $dat sar-r-f $file | grep-I Average | sed" s/Average:// "done echo" +- -+ "

After running the script, you will see the following output.

# sh / opt/scripts/sar-memory-avg.sh +- -+ | Average: kbmemfree kbmemused% memused kbbuffers kbcached kbcommit% commit kbactive kbinact kbdirty | +- -- + 08 Greater 01Compact 2019 1492331 2388461 61.55 29888 1152142 1560615 12.72 1693031 380472 608 Grey 2019 1493126 2387666 61.53 29888 1147811 1569624 12.79 1696387 373346 308 Universe 2019 1489582 2391210 61.62 29888 1147076 1581711 12.89 1701480 370325 308/04/2019 1490403 2390389 61.60 29888 1148206 1569671 12.79 1697654 373484 408/05/2019 1484506 2396286 61.75 29888 1152409 1563804 12.75 1702424 374628 408/06/2019 1473593 2407199 62.03 29888 1151137 1577491 12.86 1715426 371000 808/07/2019 1467150 2413642 62.19 29888 1155639 1596653 13.01 1716900 372574 1308/08/2019 1451366 2429426 62.60 29888 1162253 1604672 13.08 1725931 376998 508/09/2019 1451191 2429601 62.61 29888 1158696 1582192 12.90 1728819 371025 408/10/2019 1450050 2430742 62.64 29888 1160916 1579888 12.88 1729975 370844 5... 08 Universe 29Universe 2019 1365699 2515093 64.81 29888 1198832 1593567 12.99 1781733 376157 1508 Charley 2019 1361920 2518872 64.91 29888 1200785 1595105 13.00 1784556 375641 8 color- -+ script 3: Bash script to get CPU and memory average utilization from SAR report

The bash script collects CPU and memory averages from each data file and displays them on a page.

This script is slightly different from the above. It displays the average of both (CPU and memory) in the same place, rather than other data.

# vi / opt/scripts/sar-cpu-mem-avg.sh #! / bin/bash for file in `ls-tr / var/log/sa/sa* | grep-v sar`do sar- f $file | head-n 1 | awk'{print $4} 'echo "-" sar- u-f $file | awk' / Average:/ {printf ("CPU Average:% .2f%\ n") 100-$8} 'sar-r-f $file | awk' / Average:/ {printf ("Memory Average:% .2f%\ n"), (($3muri) / ($2m)) * 100} 'printf "\ n" done

After running the script, you will see the following output.

# sh / opt/scripts/sar-cpu-mem-avg.sh 08/01/2019-CPU Average: 1.90%Memory Average: 31.09% 08/02/2019-CPU Average: 4.90%Memory Average: 31.18% 08/03/2019-CPU Average: 4.89%Memory Average: 31.29% 04Mather 2019- -- CPU Average: 2.82%Memory Average: 31.24% 08/05/2019-CPU Average: 1.76%Memory Average: 31.28%...08/29/2019-CPU Average: 3.83%Memory Average: 33.15% 08/30/2019-CPU Average: 5.80%Memory Average: 33.19% I believe you have a deeper understanding of "how to use Bash scripts to get CPU and memory usage", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report