1、为了方便查询服务器的硬件信息,将需要查询的内容整理成Shell脚本,输出自己想要的格式。其实就是通过dmidecode命令输出,然后通过sed,awk,cut命令的处理。
2、此脚步适用于HP DL系列的服务器,Dell PowerEdge系列的会有少许内容不同,所以在抓取过程会有小小报错。
3、脚本内容如下:#!/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!"
4、这里只对服务器厂商,内存,CPU配置和网卡信息,对于硬盘信息可根据不同厂商的管理工具来获取。HP使用hp-health-*rpm,G9版本以上使用hpssacli-*.rpm,安装以后使用:hpacucli ctrl all show config 查看RAID信息。Dell安装Dell OpenManage Server Administrator工具查看,通过命令:omreport storage pdisk controller=0来查看。
5、查看RAID信息还可以使用MegaCli开源工具进行查看,这里不作介绍。