#!/bin/bash
# 脚本文件名：system_info.sh
# 修订版本：1.0
# 日期：2024/12/01
# 作者：Jack.liu
# 电子邮件：lqf@xlsys.cn
# 网址：www.xlsys.cn

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印标题
print_title() {
  echo -e "${GREEN}================== $1 信息 ==================${NC}"
}

# 查看物理CPU的个数
print_title "物理CPU个数"
physical_cpu_count=$(cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l)
echo "物理CPU个数: $physical_cpu_count"

# 查看逻辑CPU的个数
print_title "逻辑CPU个数"
logical_cpu_count=$(cat /proc/cpuinfo | grep "processor" | wc -l)
echo "逻辑CPU个数: $logical_cpu_count"

# 查看CPU是几核
print_title "CPU核心数"
cpu_cores=$(cat /proc/cpuinfo | grep "cpu cores" | uniq)
echo "$cpu_cores"

# 查看CPU的主频
print_title "CPU主频"
cpu_mhz=$(cat /proc/cpuinfo | grep "MHz" | uniq)
echo "$cpu_mhz"

# 内存信息
print_title "内存"
total_mem_kb=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')
used_mem_kb=$(free -k | awk 'NR==2{print $3}')
free_mem_kb=$(free -k | awk 'NR==2{print $4}')

total_mem_gb=$(awk "BEGIN {printf \"%.2f\", $total_mem_kb / 1024 / 1024}")
used_mem_gb=$(awk "BEGIN {printf \"%.2f\", $used_mem_kb / 1024 / 1024}")
free_mem_gb=$(awk "BEGIN {printf \"%.2f\", $free_mem_kb / 1024 / 1024}")

echo "总内存: $total_mem_gb GB"
echo "已用内存: $used_mem_gb GB"
echo "空闲内存: $free_mem_gb GB"

# 硬盘信息
print_title "硬盘"
lsblk -b -o NAME,SIZE | awk 'NR>1 && $2 > 0 {size_gb = $2 / (1024^3); printf "%-10s : %.2f GB\n", $1, size_gb}'

# IP地址信息
print_title "IP地址"
ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | while read ip; do
  interface=$(ip -4 addr show | grep "$ip" -B1 | grep -oP '(?<=\d:\s)\S+')
  printf "%-15s : %s\n" "$interface" "$ip"
done