CPU、内存的占用率

 要获取不包含百分比符号的内存占用率:

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

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

Current Memory Utilization is : 18.0433

 

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

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

Current Memory Utilization is : 18.06%


使用 free、grep 和 awk 命令的组合来获取内存占用率
#free -t | grep Mem | awk '{print "Current Memory Utilization is : " $3/$2*100}'

Current Memory Utilization is : 18.071

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

Current Memory Utilization is : 18.07%

 

top、print 和 awk 命令的组合来获取 CPU 的占用率
#top -b -n1 | grep ^%Cpu

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

要获取不包含百分比符号的 CPU 占用率:

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

Current CPU Utilization is : 100

 

要获取包含百分比符号及保留两位小数的 CPU 占用率:

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

Current CPU Utilization is : 100.00%

 

free:是一个标准命令,用于在 Linux 下查看内存使用情况。

awk:是一个专门用来做文本数据处理的强大命令。

FNR == 2:该命令给出了每一个输入文件的行数。其基本上用于挑选出给定的行(针对于这里,它选择的是行号为 2 的行)

NR == 2:该命令给出了处理的行总数。其基本上用于过滤给出的行(针对于这里,它选择的是行号为 2 的行)

$3/$2*100:该命令将列 3 除以列 2 并将结果乘以 100。

printf:该命令用于格式化和打印数据。

%.2f%:默认情况下,其打印小数点后保留 6 位的浮点数。使用后跟的格式来约束小数位。

posted @ 2022-10-20 08:30  槑槑DE  阅读(242)  评论(0编辑  收藏  举报