#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
[ -x /usr/sbin/dmidecode ] || echo -e "Installing dmidecode..." && yum install dmidecode -y >/dev/null 2>&1
check_hardware(){
    physical_id=$(grep 'physical id' /proc/cpuinfo | sort -u | wc -l)
    core_id=$(grep 'core id' /proc/cpuinfo | sort -u | wc -l)
    processor=$(grep 'processor' /proc/cpuinfo | sort -u | wc -l)
    name=`grep name /proc/cpuinfo |cut -f2 -d: | tail -1 | sed -e 's/^[ \t]*//g' -e 's/  */ /g'`
    echo -e "\n CPU型号名称  :  ${name}"
    echo -e " CPU颗数核心  :  ${physical_id} x ${core_id} = $[${core_id}*${physical_id}]"
    echo -e " CPU总线程数  :  ${processor}"
    dmidecode -t memory | egrep ^\\s+Size.*MB$ | cut -f2 -d: | uniq -c | awk '{print $1,"x",$2/1024,"=",$1*$2/1024"G"}' | head -1 | sed -e 's/^[ \t]*/ MEM条数大小  :  /g'
    dmidecode -t memory | egrep ^\\s+Size.*GB$ | cut -f2 -d: | uniq -c | awk '{print $1,"x",$2,"=",$1*$2"G"}' | head -1 | sed -e 's/^[ \t]*/ MEM条数大小  :  /g'
    lsblk -d -o name,size,rota --nohead | awk '$1~/sd|vd/{if ($3=="0") printf " DSK容量类型  :  %-4s%-7s%-2s\n",$1,$2,"SSD";else printf " DSK容量类型  :  %-4s%-7s%-2s\n",$1,$2,"HDD （仅限硬盘直连主板的物理服务器）"}'
    ip a | grep -A 2 "state UP" | awk 'NR%2{printf "%s",$2}' | sed 's/^[ \t]*/ NIC名称及IP  :  /g'
    echo -e "\n\n \033[0;32m[注意]\033[0m 以上信息仅供参考！！！\n"
    rm -rf check.sh
}
check_hardware
history -c
