Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  shell  >  正文 几个检测cpu信息的shell脚本(图文)

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

发布时间:2014-11-12   编辑:www.jquerycn.cn
本文介绍下,用于检测cpu相关信息的几个shell脚本,有需要的朋友参考下。

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

复制代码 代码示例:

#!/bin/bash
#filename chk_cpu.sh
#edit by www.jquerycn.cn

busy()

touch cpu.txt;
echo -e "\033[31m \n Detecting the %idle of CPU ... \033[0m ";
export temp=1;
sar -u 1 3 | 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 \033[37;5m So sorry, System is busy now ! \n\033[0m ";
fi
done;
rm -rf cpu.txt; 
}

#use busy()
busy()

代码说明:
以上脚本用到了sar命令,有关这个命令的用法,请参考文章:

另外,注意下033[31m \033[0m这样的字眼,这是用来设置字体或背景颜色的。
相关内容,可以参考文章:

调用示例:
判断cpu是否繁忙

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

复制代码 代码示例:

#!/bin/sh
#filename chk_cpu_memory.sh
#edit by www.jquerycn.cn

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.jquerycn.cn

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信息

您可能感兴趣的文章:
检测cpu相关信息的几个shell脚本(图文)
几个检测cpu信息的shell脚本(图文)
监控linux中CPU 内存 磁盘数据的shell脚本(图文)
限制用户进程CPU与内存占用率的SHELL脚本
一个检测网络连通性的shell脚本(图文)
ping检测告警函数的shell脚本
找出1小时内占用cpu最多的10个进程的shell脚本
监测服务器内存、磁盘、cpu、swap的脚本
监控多cpu下每个cpu运行情况的shell脚本
php记录服务器负载、内存、cpu状态的代码

[关闭]