一个用于获取主机规格信息的shell脚本

功能用于获取主机的 CPU、Mem、、HDD、虚拟化特性、OS版本信息

用途:

判断目标主机是否满足部署某种业务的 主机规格要求

***************************************************************************************

#!/bin/bash
echo "################ README INFO #########################################"
echo "### Purpose: Get the infomation of Machine and Host                ###"
echo "### Made By: PomanTeng                                             ###"
echo "### E-mail: [email protected]                               ###"
echo "### WeChat: 1807479153                                             ###"
echo "### Version Identification Number:V2.1.0                           ###"
echo "### Procedure Identification Number:20230916                       ###"
echo "######################################################################"

# Setting console color

C_RESET="$(tput sgr0)"
C_BLACK="\033[1;30m"
C_RED="\033[1;31m"
C_GREEN="\033[1;32m"
C_YELLOW="\033[1;33m"
C_BLUE="\033[1;34m"
C_PURPLE="\033[1;35m"
C_CYAN="\033[1;36m"
C_WHITE="\033[1;37m"

# Setting color for symbol "#################################################"

printf "${C_PURPLE}"
cat << EOF
#################################################
EOF
printf "${C_RESET}"

# Check the current OS user

[[ $(id -u) -gt 0 ]] && echo "请用root用户执行此脚本!" && exit 1

# Defining Variable "sysversion"

sysversion=$(rpm -q centos-release | cut -d- -f3)

# Defining Variable "double_line"

double_line="==============================================================="

# Defining Variable "line"

line="----------------------------------------------"

# Print the message of the top page

printHeadInfo() {
  cat << EOF
+---------------------------------------------------------------------------------+
|                           欢迎使用 【系统信息检查脚本】                          |
+---------------------------------------------------------------------------------+
EOF
}

# Print the message of the bottom page

printFootInfo() {
  cat << EOF
+---------------------------------------------------------------------------------+
|                            脚本执行结束,感谢使用!|
+---------------------------------------------------------------------------------+
EOF
}

# Defining the Variable "options" to get the name of the Menu

options=( "获取OS信息" "获取装机CPU信息" "获取装机内存信息" "获取装机硬盘信息" "退出" )

# Defining functions such as "printMenu" etc.

# Defining the Function "printMenu" to get the options to operate

printMenu() {
  printf "${C_BLUE}"
  printf "主菜单:\n"
  for i in "${!options[@]}"; do
    index=`expr ${i} + 1`
    val=`expr ${index} % 2`
    printf "\t(%02d) %-30s" "${index}" "${options[$i]}"
    if [[ ${val} -eq 0 ]]; then
      printf "\n"
    fi
  done
  printf "${C_BLUE}请输入需要执行的指令:\n"
  printf "${C_RESET}"
}

# Defining the Function "get_OS_info" to get the information of the OS-Release

get_OS_info() {

    os_release=$(cat /etc/*release)

    # Print the information of the OS-Release
   
    cat << EOF
【操作系统发行版信息】
操作系统发行版:$os_release
EOF

}

# Defining Function "get_cpu_info" to get information of onboard CPUs

get_cpu_info() {


  Physical_CPU_mumbers=$(lscpu | grep "Socket(s)")

  ecahPhysical_core_numbers=$(cat /proc/cpuinfo | grep "cpu cores" | uniq)

  # If sibings total equal to cpu cores , then the current CPU does not support Hyperthreading or Hyperthreading is closed now
  siblings=$(cat /proc/cpuinfo | grep "siblings" | uniq)

  instruction_CPU=$(gcc -march=native -Q --help=target | grep march)
  
  CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
  
  CPU_Arch=$(lscpu | grep "Architecture")
  
  total_logic_cpus=$(lscpu | grep "^CPU(s)")

  per_core_Threads=$(lscpu | grep "Thread(s) per core")

  virtual_type=$(lscpu | grep "Virtualization")

  virt_technology=$(lscpu | grep "^Flags" | grep -E -o "vmx|svm")

 
  compare=$(grep -e "cpu cores" -e "siblings" /proc/cpuinfo | sort | uniq)

  # Print the Information of CPU
   

  cat << EOF
【CPU信息】
物理CPU个数:$Physical_CPU_mumbers
每CPU核心数:$ecahPhysical_core_numbers
每核心超线程数:$per_core_Threads
逻辑CPU总个数:$total_logic_cpus
CPU型号:$CPU_Type
CPU架构:$CPU_Arch
CPU支持的指令集:$instruction_CPU
CPU的虚拟化支持类型:$virtual_type
CPU采用的虚拟化技术:$virt_technology
siblings信息:$siblings
注意:当sibilings等于每物理CPU核心数时,则说明当前的CPU 不支持超线程技术或者超线程技术设定被关闭了;当sibilings等于每核心超线程数*每CPU核心数时,则说明当前的CPU开启了超线程技术
确认是否开启了超线程:
$compare
EOF
}

# Defining the Function "Physical Memory" to get the Installed Capacity of Physical Memory

get_Ins_Mem() {

    perSolt_Capacity=$(dmidecode -t memory | grep Size | sort | uniq)
    
    signal_solt_value=$(dmidecode -t memory | grep Size | grep -v "No Module Installed" | uniq | awk -F " " '{print $2}')

    ins_Solts=$(dmidecode -t memory | grep Size | grep -v "No Module Installed" | wc -l)

    Ins_Mem=$[$signal_solt_value*$ins_Solts]

    # print the information of the Installed Capacity of Physical Memory

    cat << EOF
【装机内存信息】
每个槽位的内存条容量:
    $perSolt_Capacity
安装内存条的槽位数: $ins_Solts
装机内存总量: $Ins_Mem GB
EOF
}

# Defining the Function "get_Disk_info" to get the Installed Capacity of Hard Disks

get_Disk_info() {
    disk_info=$(fdisk -l | grep "Disk /dev" | cut -d, -f1)
    disk_numbers=$(lsblk)
    disk_use=$(df -hTP | awk '$2!="tmpfs"{print}')
    disk_used_percent=$(free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/')
    
    # Print the information of disks

      cat << EOF
【装机硬盘信息】
已经插入的硬盘:
${disk_numbers}
磁盘信息: 
${disk_info}
磁盘使用情况: 
${disk_use}
磁盘使用百分比: ${disk_used_percent}
EOF
}

# Defining the Function "get_all_info" to write the log to the file machine_info.log

get_all_info() {
  get_systatus_info
  echo ${double_line}
  get_cpu_info
  echo ${double_line}
  get_Ins_Mem
  echo ${double_line}
  get_Disk_info
  echo ${double_line}
}

# Defining the loop Function "main" to get the Custom Messages 

main() {
  while [[ 1 ]]
  do
    printMenu
    read option
    local index=$[ ${option} - 1 ]
    case ${options[${index}]} in
      "获取OS信息")
       get_OS_info ;;
      "获取装机CPU信息")
        get_cpu_info ;;
      "获取装机内存信息")
        get_Ins_Mem ;;
      "获取装机硬盘信息")
        get_Disk_info ;;
      "输出查询日志")
        get_all_info > ./$(date +%Y-%m-%d-%H:%M:%S)-machine_info.log
        printf "${C_GREEN}信息已经输出到 machine_info.log 中。${C_RESET}\n\n"
      ;;
      "退出")
        exit ;;
      *)
        clear
        echo "抱歉,不支持此选项" ;;
    esac
  done
}

# Execute Functions

printHeadInfo
main
printFootInfo
printf "${C_RESET}"


 

你可能感兴趣的:(服务器,linux,网络)