Check under Linux CPU, memory occupancy rate of collection method

How to view memory usage under Linux method:

We can use a combination of the following command to achieve this purpose. In this method, we use a combination of free and awk commands to get the memory usage.

To get memory usage does not contain a percent sign:

#free -t | awk 'NR ==2 {print "CurrentMemory Utilization is: "$3/$2*100}'

or

#free -t | awk 'FNR ==2 {print"Current Memory Utilization is: "$3/$2*100}'

Current Memory Utilization is : 18.0433

To get memory usage contains a percent sign and two decimal places are:

#free -t | awk 'NR == 2{printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'

or

#free -t | awk 'FNR == 2{printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'

Current Memory Utilization is : 18.06%

Type the free command would be better to explain:

root@imx6qdlsolo:~# free

          total        used        free      shared buff/cache   available

Mem: 1024780 185096 783012 1036 56672 769256

Swap: 0 0 0

Details as follow:

free: a standard command is used to view memory usage in Linux.

awk: it is designed to make a powerful command text data processing.

FNR == 2: This command gives the number of lines of each input file. Substantially selected for a given row (in this respect, it is the row number of the selected row 2)

NR == 2: This command gives the total number of rows processed. Line for giving substantially filtering (in this respect, it is the row number of the selected row 2)

$ 3/2 $ 100 *: The command column 2 divided by column 3 and multiplying the result by 100.

printf: This command is used to format and print data.

% .2f%: By default, it prints the decimal point after six floating-point numbers. Use heel format constraints decimal places.

How to view memory usage under Linux Method Two:

We can use a combination of the following command to achieve this purpose. In this method, we use a combination of free, grep and awk commands to get the memory usage.

To get memory usage does not contain a percent sign:

#free -t | grep Mem | awk '{print"Current Memory Utilization is : " $3/$2*100}'

Current Memory Utilization is : 18.071

To get memory usage contains a percent sign and two decimal places are:

free -t | grep Mem | awk'{printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'

Current Memory Utilization is : 18.07%

How to view a method occupancy rate of CPU under Linux:

We can use a combination of the following command to achieve this purpose. In this method, we use a combination of top, print and awk command to obtain CPU usage.

If the output is displayed in the case of more than one CPU, then you need to use the following method.

#top -b -n1 | grep ^%Cpu

% People (s): 0.1 us, sy 0.4, 0.0 ni, 99.4 id, 0.0 times, 0.0 hi, si 0.0, 0.0 st

To get CPU utilization does not contain a percent sign:

#top -b -n1 | grep ^%Cpu | awk'{cpu+=$9}END{print "Current CPU Utilization is : " 100-cpu/NR}'

Current CPU Utilization is : 100

To get CPU utilization contains a percent sign and two decimal places are:

#top -b -n1 | grep ^%Cpu | awk'{cpu+=$9}END{printf("Current CPU Utilization is : %.2f%"),100-cpu/NR}'

Current CPU Utilization is : 100.00%

How to view the occupancy rate of the second method under Linux CPU:

We can use a combination of the following command to achieve this purpose. In this method, we are using the top, combination print / printf and awk command to obtain CPU usage.

If the show when all of a CPU together in a single output, then you need to use the following method.

#top -b -n1 | grep ^%Cpu

% People (s): 0.1 us, sy 0.4, 0.0 ni, 99.5 id, 0.0 times, 0.0 hi, si 0.0, 0.0 st

To get CPU utilization does not contain a percent sign:

#top -b -n1 | grep ^%Cpu | awk '{print"Current CPU Utilization is : " 100-$8}'

Current CPU Utilization is : 0.5

To get CPU utilization contains a percent sign and two decimal places are:

#top -b -n1 | grep ^%Cpu | awk'{printf("Current CPU Utilization is : %.2f%"), 100-$8}'

Current CPU Utilization is : 0.50%

Here are some details:

top: a very good command to view the current Linux system running processes.

-b: top option allows the command to switch to batch mode. When you run the top command from the local system to the remote system, it will be very useful.

-n1: the number of iterations.

^% Cpu: filtering beginning with% CPU line.

awk: a powerful command designed to make text data processing.

cpu + = $ 9: For each row, column 9 to add variables cpu.

printf: This command is used to format and print data.

% .2f%: By default, it prints the decimal point after six floating-point numbers. Use the format followed to limit the number of decimal places.

100-cpu / NR: CPU final print an average occupancy rate, i.e., 100 divided by the number of rows and subtracted.

I believe small partners must have mastered several ways, if there are other convenient way, can one communicate in the comments section oh ~

Guess you like

Origin blog.51cto.com/14586215/2447109