检测cpu相关信息的几个shell脚本(图文)

发布时间:2020-03-29编辑:脚本学堂
本文介绍下,用于检测cpu相关信息的几个shell脚本,有需要的朋友参考下。

一、判断cpu是否繁忙
代码如下:
 

复制代码 代码示例:

#!/bin/bash
#filename chk_cpu.sh
#edit by www.jb200.com

busy()

touch cpu.txt;
echo -e "33[31m n Detecting the %idle of CPU ... 33[0m ";
export temp=1;
sar -u 1 3 | linuxjishu/13830.html target=_blank class=infotextkey>awk '{print$9}' | tail -n 4 >>cpu.txt ;
tail -n 4 cpu.txt | cut -d "." -f 1 | while read var;
do
#echo $var;
if [[ $var -ge 20 ]];then
  delete; ### Run into delete function
  exit;
fi
temp=$[ $temp + 1 ];
if [ $temp -eq 4 ];then
  echo -e "n 33[37;5m So sorry, System is busy now ! n33[0m ";
fi
done;
rm -rf cpu.txt; 
}

#use busy()
busy()

代码说明:
以上脚本用到了sar命令,有关这个命令的用法,请参考文章:
sar 命令行常用格式详解
linux的sar用法大全
另外,注意下033[31m 33[0m这样的字眼,这是用来设置字体或背景颜色的。
参考文章:
shell脚本中echo显示内容带颜色
Shell 颜色修改一例

调用示例:
 

检测cpu信息

二,统计cpu与内存的使用率
代码如下:
 

复制代码 代码示例:

#!/bin/sh
#filename chk_cpu_memory.sh
#edit by www.jb200.com

clear
while ((1>0))
do
 NET=$(netstat -anp | grep 1000 | grep EST | wc -l)
 PRC=$(ps aux | grep thread1 | grep -v grep | grep -v SCREEN | awk '{ print " CPU: " $3 "%,  MEM: " $4 "%, RSS: " $6/1024 "M, VSZ: " $5/1024 "M, PID: "
 $2 ""}')
 tput sc; echo -n -e "b"; tput rc;
 echo "SRV:  thread1, NET: $NET, $PRC"
 echo ""
 NET=$(netstat -anp | grep 1011 | grep EST | wc -l)
 PRC=$(ps aux | grep thread2 | grep -v grep | grep -v SCREEN | awk '{ print " CPU: " $3 "%, MEM: " $4 "%, RSS: " $6/1024 "M, VSZ: " $5/1024 "M, PID: "
 $2 ""}')
 tput sc; echo -n -e "b"; tput rc;
 echo "SRV:  thread2, NET: $NET, $PRC"
 echo ""
 NET=$(netstat -anp | grep 1022 | grep EST | wc -l)
 PRC=$(ps aux | grep thread3 | grep -v grep | grep -v SCREEN | awk '{ print " CPU: " $3 "%, MEM: " $4 "%, RSS: " $6/1024 "M, VSZ: " $5/1024 "M, PID: "
 $2 ""}')
 tput sc; echo -n -e "b"; tput rc;
 echo "SRV:  thread3, NET: $NET, $PRC"
 echo ""
 NET=$(netstat -anp | grep 1033 | grep EST | wc -l)
 PRC=$(ps aux | grep thread4 | grep -v grep | grep -v SCREEN | awk '{ print " CPU: " $3 "%, MEM: " $4 "%, RSS: " $6/1024 "M, VSZ: " $5/1024 "M, PID: "
 $2 ""}')
 tput sc; echo -n -e "b"; tput rc;
 echo "SRV:  thread4, NET: $NET, $PRC"
 echo ""
 NET=$(netstat -anp | grep beam.smp | grep EST | grep 222.222.18.222:800 | wc -l)
 PRC=$(ps aux | grep thread | grep -v grep | awk '{CPU += $3} {MEM += $4} {RSS += $6} {VSZ += $5}  END {print "CPU: " CPU "%, MEM: " MEM "%, RSS: "
 RSS/1024 "M, VSZ: " VSZ/1024 "M,  "  " "}'  | awk 'END {print}')
 tput sc; echo -n -e "b"; tput rc;
 echo "SRV: All, NET: $NET, $PRC"
 echo ""
 sleep 0.5
 tput cup 0 0
done

调用示例:
 

检测cpu信息

三,获取CPU信息
分析/proc/cpuinfo中有关CPU的信息,然后构造一个清晰的CPU信息状态输出,直观地看出当前CPU的相关内容。

在文件/proc/cpuinfo中,注意以下信息项:
processor 逻辑CPU号(核或线程)
core id  CPU核号
physical id 物理CPU号

脚本内容如下:
 

复制代码 代码示例:

#!/bin/bash
#filename get_cpu_info.sh
#edit by www.jb200.com

cpu_p=`cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l`
cpu_c=`cat /proc/cpuinfo | grep "core id" | uniq | wc -l`
cpu_l=`cat /proc/cpuinfo | grep "processor" | wc -l`

c_p=`expr $cpu_c / $cpu_p`
l_p=`expr $cpu_l / $cpu_p`
t_c=`expr $cpu_l / $cpu_c`

echo "Physical CPU Count: $cpu_p"
echo "CPU Core Count   : $cpu_c"
echo "Logical CPU Count: $cpu_l"

echo "$c_p cores per physical CPU"
echo "$l_p logical CPUs per physical CPU"
echo "$t_c threads per core"

if [ $c_p -eq 1 ]; then
echo "Your CPU is unicore"
else
echo "Your CPU is multicore"
fi

if [ $t_c -eq 1 ]; then
echo "Your CPU isn't of HT"
else
echo "Your CPU is of HT"
fi

echo "You have $cpu_p CPUs, each CPU has $c_p cores, each core has $t_c threads. In summary $cpu_c cores, $cpu_l threads."

调用示例:
检测cpu