获取硬件信息_Hardware_info

需要获取的硬件
1.内存 √
2.CPU √
3.硬盘 √
4.主板 √
5.网卡 √
6.raid卡 √
7.os版本 √
8.电源 √
9.BMC_IP √

Hardware_info()
{
which lspci &> /dev/null
if [ $? == "0" ]
then
 :
else
  yum install pciutils* -y &> /dev/null
fi
which smartctl &> /dev/null
if [ $? == "0" ]
then
 :
else
  yum install smartmontools -y &> /dev/null
fi
#
which ipmitool &> /dev/null
if [ $? == "0" ]
then
 :
else
  yum install ipmi* -y &> /dev/null
fi
#
#os version
os=$(hostnamectl | grep "Operating Syste"|awk -F ":" '{print $2}'|sed 's/^ *//')
#cpu
numcpus=$(cat /proc/cpuinfo | grep processor | wc -l) # num of online cpus
cpu=$(cat /proc/cpuinfo | grep "model name" | head -1 | awk '{$1=$2=$3=""; print}')
cpucores=$(cat /proc/cpuinfo | grep "cpu cores" | head -1 | awk '{print $4}')
cpusiblings=$(cat /proc/cpuinfo | grep "siblings" | head -1 | awk '{print $3}')
let "cpucount=($((`cat /proc/cpuinfo | grep processor | tail -1 | awk {'print $3'}`))+1)/$cpusiblings"
#machine
#machine=$(dmidecode |grep -A2 "System Information"|tail -2 |awk '{print $NF}' |xargs)
machine=$(dmidecode|grep "System Information" -A9|egrep  "Manufacturer|Product|Serial" |awk '{print $NF}'| xargs )
#BMC_IP
ipmitool lan print 1 &> /dev/null
if [ "$?" == "1" ]
 then
   BMC_IP="N/A"
 else 
   IP=$(ipmitool lan print 1 | grep "IP Address" | grep -v "IP Address Source " | awk -F ":" '{print $NF}'| sed 's/[ ]*//g')
   MAST=$(ipmitool  lan print 1 | grep "Subnet Mask"| awk -F ":" '{print $NF}'| sed 's/[ ]*//g')
   GATEWAY=$(ipmitool  lan print 1 | grep "Default Gateway IP"| awk -F ":" '{print $NF}'| sed 's/[ ]*//g')
   BMC_IP=$(echo $IP/$MAST/$GATEWAY)
fi
#kernel
kernel=$(uname -r)
#memory 
mem_size=$(dmidecode -t memory | grep Size: | grep -v "No Module Installed" |tail -1|awk -F ":" '{print $2}')
mem_nu=$(dmidecode -t memory | grep Size: | grep -v "No Module Installed" | wc -l)
mem_Manufacturer=$(dmidecode -t memory | grep Manufacturer: | grep -v "NO DIMM" | tail -1 |awk '{print $NF}')
mem_total=$(cat /proc/meminfo |grep MemTotal |awk '{printf("%d\n",$2/1024/1000)}')
memory=$(echo $mem_Manufacturer: $mem_size \* $mem_nu = $mem_total GB)
#主板型号
Main_board_model=$(dmidecode | grep "Product Name:" | tail -n 1 | awk '{print $NF}')
#电源
Power=$(dmidecode | grep -A15 "System Power Supply" | grep "Name" |grep -v "O.E.M"| awk -F":" '{print $NF}'| sort | uniq  -c| sed 's/^[ ]*//g')
#网卡信息
ethernet=$(lspci  | grep Ethernet |awk -F ":" '{print $NF}'| sort |uniq -c  | awk 'NF{NF--}1'| awk 'NF{NF--}1'|sed 's/^/                  /')
#
if  [ -n "$ethernet" ] 
  then
    ethernet=$ethernet
else
   ethernet="                  N/A" 
fi
#
#raid卡
raid=$(lspci | grep LSI | awk -F ":" '{print $NF}' | awk 'NF{NF--}1'| awk 'NF{NF--}1'| sed 's/^/                  /')
#
if  [ -n "$raid" ] 
  then
    raid=$raid
else
   raid="                  N/A" 
fi
#
#disk
disk=$(lsscsi -s| grep -v SMC |awk '{for(i=NF;i>0;i--)printf $i" ";printf "\n"}'  |\
       awk '{$2=""; print $0}' |awk '{for(i=NF;i>0;i--)printf $i" ";printf "\n"}'|\
       awk '{$1=$2=""; print $0}'| sed 's/^[ ]*//g'| sort | uniq -c |sed 's/^/             /')
#
if  [ -n "$disk" ] 
  then
    disk=$disk
else
   disk="                  N/A" 
fi
#
#
disk_all=$(for i in $(lsblk -dn |sort | awk '{print $1}');do all_disk=$(smartctl -i /dev/$i |\
           egrep  "Device Model|Serial Number|Firmware Version|User Capacity" | awk -F ":" '{print $NF}'|\
           xargs|awk '{for(i=NF;i>0;i--)printf $i" ";printf "\n"}'|awk '{$3=$4="";print $0}'|\
           awk '{for(i=NF;i>0;i--)printf $i" ";printf "\n"}')&&printf "%-17s %-9s %-4s %-10s\n" "" /dev/$i --\> "${all_disk}" ;done)

#------------------------------------------------------------------------------------------------------------------------------------------
echo -e "
---------------------------------Hardware_info -------------------------------------

         Machine: $machine
Main_board_model: $Main_board_model
          BMC_IP: $BMC_IP
              OS: $os
          Kernel: $kernel
             Cpu: $cpucount * $cpucores $cpu 
          Memory: $memory
           Power: $Power
        Ethernet:↓
$ethernet
            Raid: ↓
$raid
            Disk: ↓
$disk
   Disk_all_list: ↓
$disk_all
"
}
Hardware_info

你可能感兴趣的:(获取硬件信息_Hardware_info)