一、爲了方便查詢服務器的硬件信息,將須要查詢的內容整理成Shell腳本,輸出本身想要的格式。其實就是經過dmidecode命令輸出,而後經過sed,awk,cut命令的處理。bash
二、此腳步適用於HP DL系列的服務器,Dell PowerEdge系列的會有少量內容不一樣,因此在抓取過程會有小小報錯。服務器
三、腳本內容以下:ide
#!/bin/bash ############################################################################ # Check the server hardware configuration # # History: 2016/04/16 zhuwei First release ############################################################################ # set a safe path before doing anything else PATH=/sbin:/usr/sbin:/bin:/usr/bin; export PATH # This script must be executed as root RUID=`/usr/bin/id|awk -F\( '{print $1}'|awk -F\='{print $2}'` # #OR# RUID=`id | cut -d\( -f1 | cut -d\= -f2` #OR#ROOT_UID=0 if [ ${RUID} != "0" ];then echo"This script must be executed as root" exit 1 fi # Display an error and exit errorExit() { echo"$@" >&2 exit 1 } # Display the normal print display() { echo -e"\033[32m***********************************************************\ ******************************\033[0m" echo -e"\033[32m*\033[0m"$@"" echo -e"\033[32m***********************************************************\ ******************************\033[0m" } #Check the server model, serial number model() { dmidecode|grep "System Information" -A9|egrep"Manufacturer|Product|Serial" \ | awk 'BEGIN {FS="\n";RS=""} {print $1,$2,$3}' | sed 's/^[\t]//g' } #Query cpu Information cpu(){ dmidecode| grep -A55 "Processor Information" | \ egrep "Version:|Core Count:|Thread Count:"|sed -e 's/^[\t]//' \ -e'/Version:/i\\' | sed '1d' | awk 'BEGIN {FS="\n";RS=""}{print $1,$2,"\t"$3}'\ | sed's/Version://' } #Query memory Information memory(){ dmidecode| grep -A12 "Memory Device"|egrep "Type:|Size|Speed:|Locator:P" \ | sed -n -e '/MB/,/MHz/p' | sed '/Size:/i\\' | sed -e '1d' -e 's/^[\t]*//' \ | awk'BEGIN {FS="\n";RS=""} {print $2} {print"\t"$3,"\t"$1,"\t"$4}' #sed'/Size:/i\\' | sed -e '1d' -e 's/^[\t]*//' #replaced# #awk'BEGIN {FS="\n"}{if(NR%4==1){print "\n"}{print $1}}' | sed's/^[\t]*//' } #Query Network Information network(){ i=0 count=`dmidecode | grep -A4 "BIOS NIC"|sed -e '1d' | wc -l` if [ -f "/tmp/speed.txt" ];then rm -rf /tmp/speed.txt fi while [[${i} -lt ${count} ]] do ethtool eth"${i}"|grep "Speed:" >> /tmp/speed.txt let "i++" done dmidecode| grep -A4 "BIOS NIC"|sed -e '1d' -e 's/^[\t]//'| cut -d, -f2 | \ awk 'FNR==0 {print "\r\n" FILENAME}{print et NR-1 "\t" $0}' \ et="eth" > /tmp/network.txt paste /tmp/network.txt /tmp/speed.txt } display "System Information:" && model || errorExit "This command is error!" display "CPU Information:" && cpu|| errorExit "This command is error!" display "Memory Information:" && memory || errorExit "This command is error!" display "Network Information:" && network || errorExit "This command is error!"
四、這裏只對服務器廠商,內存,CPU配置和網卡信息,對於硬盤信息可根據不一樣廠商的管理工具來獲取。HP使用hp-health-*rpm,G9版本以上使用hpssacli-*.rpm,安裝之後使用:hpacucli ctrl all show config 查看RAID信息。Dell安裝Dell OpenManage Server Administrator工具查看,經過命令:omreport storage pdisk controller=0來查看。工具
五、查看RAID信息還能夠使用MegaCli開源工具進行查看,這裏不做介紹。spa