Jquery中文網 www.lfcgx.club
Jquery中文網 >  腳本編程  >  shell  >  正文 幾個檢測cpu信息的shell腳本(圖文)

幾個檢測cpu信息的shell腳本(圖文)

發布時間:2014-11-12   編輯:www.lfcgx.club
本文介紹下,用于檢測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.lfcgx.club

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.lfcgx.club

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

您可能感興趣的文章:
shell腳本統計多個CPU利用率
檢測cpu相關信息的幾個shell腳本(圖文)
監測服務器內存、磁盤、cpu、swap的腳本
幾個檢測cpu信息的shell腳本(圖文)
監控linux中CPU 內存 磁盤數據的shell腳本(圖文)
磁盤空間使用狀況檢測腳本
幾個shell自動化腳本(定期清理、磁盤空間、搜尋關鍵字)
一個自動監控進程的shell腳本
限制用戶進程CPU與內存占用率的SHELL腳本
shell磁盤監控及報警的腳本

[關閉]
矮木头走势图
可以赚钱的传奇页游 炒股的技巧 广东十一选5开奖 麻将代理公司 登录北京时时开奖结果 重庆时时彩一直跟34567 69棋牌游戏 金七乐规则 亿客隆彩票官网 哪个视频网站发视频赚钱 王国新大陆赚钱 浙江快乐12玩法 雪缘园足彩比分直播 时时彩漏洞教程 黑龙江22选5开奖详情 江苏7位数