cat > /tmp/get_os_info.sh <<"EOF"#!/bin/bashexport LANG=en_US.UTF-8# 如果 cat /proc/1/cgroup | grep docker | wc -l 大于0 或 systemd-detect-virt 返回 docker,则为 docker容器,# 如果 virt-what 返回 kvm或vmware或hyperv或xen、xen-hvm、lxc 或 systemd-detect-virt 返回 vmware、kvm 则为虚拟机# 如果 cat /sys/class/dmi/id/product_name 返回 Bochs 、OpenStack Nova则为虚拟机 若返回Alibaba Cloud ECS,则代表阿里云主机# 如果 systemd-detect-virt 返回 none 则表示物理机# 判断当前主机类型if [ "$(cat /proc/1/cgroup | grep -c docker)" -gt 0 ] || systemd-detect-virt | grep -q docker; thenhost_type="Docker容器"elif [[ -f /sys/class/dmi/id/product_name && $(cat /sys/class/dmi/id/product_name) == "Alibaba Cloud ECS" ]]; thenhost_type="阿里云主机"elif [[ $(virt-what) =~ (kvm|vmware|hyperv|xen|xen-hvm|lxc) ]] || [[ $(systemd-detect-virt) =~ (vmware|kvm) ]]; thenhost_type="虚拟机"elif [[ $(cat /sys/class/dmi/id/product_name) =~ (Bochs|OpenStack Nova) ]]; thenhost_type="虚拟机"elsehost_type="物理机"fiecho "- **当前主机类型**: $host_type"# 获取并显示系统制造商、产品名称和版本echo " - 当前主机系统硬件信息:"dmidecode -t 1 | awk -F: '/Manufacturer/{printf " - 制造商: %s\n", $2}/Product Name/{printf " - 产品名称: %s\n", $2}'# 获取操作系统信息os_info=$(cat /etc/os-release | grep 'PRETTY_NAME' | cut -d '"' -f 2)# 如果操作系统是 CentOS Linux 7 (Core),查询 /etc/redhat-releaseif [[ "$os_info" == "CentOS Linux 7 (Core)" ]]; thenos_info=$(cat /etc/redhat-release)fiecho "- **操作系统信息**: $os_info"# 显示当前内核版本kernel_version=$(uname -r)echo "- **内核版本**: $kernel_version"echo "- **硬件配置**:"# 获取CPU信息cpu_model=$(lscpu | grep "Model name" | awk -F: '{print $2}' | xargs)cpu_cores=$(lscpu | grep "^CPU(s):" | awk '{print $2}')# 获取每核的线程数threads_per_core=$(lscpu | grep -i "Thread(s) per core" | awk '{print $4}')# 如果每核线程数大于1,则说明开启了超线程if [[ $threads_per_core -gt 1 ]]; thenHyperThreading="超线程已开启 (每核线程数: $threads_per_core)"elseHyperThreading="超线程未开启 (每核线程数: $threads_per_core)"fiecho " - **CPU**: $cpu_model ,**共 $cpu_cores 核** ,$HyperThreading "# 获取内存信息memory=$(free -h | awk '/^Mem:/{print $2}')echo " - **内存**: $memory"# 磁盘类型# 虚拟化平台(如 VMware、KVM、Hyper-V 等)可能会将虚拟硬盘映射为虚拟的 HDD 类型,即使宿主机的物理硬盘是 SSD。# # 使用dd测试,若超过500MB/s,则肯定是SSD盘,有的盘差点在300MB/s,完成时间在15秒以下# mkidr -p /data/# dd if=/dev/zero of=/data/testssd bs=1M count=5000 oflag=direct# dd if=/data/testssd of=/dev/null bs=1M count=5000 iflag=direct# rm -rf /data/testssd# # 如果 TRAN 列显示为 sata,则硬盘是 SATA 接口。# 如果 TRAN 列显示为 pcie 或 nvme,则硬盘是 NVMe 接口。# 如果 TRAN 列显示为 fc,则表示该硬盘使用的是 Fibre Channel 接口。# 获取指定磁盘信息echo " - **磁盘**: $disk_info"# lsblk -d -o NAME,ROTA,TRAN,SIZE | grep -E '^(sd|vd)' | awk '{print " -", $1,$2,$3,$4, ($2 == 1 ? "HDD机械硬盘" : "SSD固态硬盘")}'lsblk -d -o NAME,ROTA,TRAN,SIZE | grep -E '^(sd|vd)' | awk -v host_type="$host_type" '{ if (host_type == "物理机") {print " -", $1, $2, $3, $4, ($2 == 0 ? "SSD固态硬盘" : "HDD机械硬盘")} else {print " -", $1, $2, $3, $4, ($2 == 0 ? "SSD固态硬盘" : "疑似HDD机械硬盘,还需测试IO或咨询主机管理员")}}'echo ' ```'if command -v lshw &> /dev/null; thenlshw -short | grep disk | grep -v "cdrom" | awk '{$1=""; print " "$0}'
filsscsi | grep -v "cd/dvd" | grep "disk" | awk '{$1=""; print " "$0}'echo ' ```'# 显示文件系统类型
echo "- **文件系统**:"
# df -Th 2>/dev/null | grep -v "loop" | grep -v "tmpfs" | awk '{print " -", $1, "->", $2}'
echo ' ```'
df -Th 2>/dev/null | grep -v "loop" | grep -v "tmpfs" | grep -v "iso" | grep -v "/var/lib/docker/overlay2" | grep -v "/boot" | awk '{print " "$0}'
echo ' ```'# 1. 对闪存(SSD)等存储介质,优先使用noop或none调度算法
# 2. 对IO压力比较重且非SSD存储设备,且功能比较单一的场景,例如数据库服务器,使用deadline或mq-deadline调度算法# for dev in $(lsblk -dno NAME | grep -E '^(sd|vd)'); do echo "$dev: $(cat /sys/block/$dev/queue/scheduler)"; done
# echo noop | sudo tee /sys/block/sda/queue/scheduler
# for dev in $(lsblk -dno NAME | grep -E '^(sd|vd)'); do echo "echo noop | sudo tee /sys/block/$dev/queue/scheduler"; done# 获取I/O调度器信息
echo "- **I/O 调度器**:"
for dev in $(lsblk -dno NAME | grep -E '^(sd|vd)'); do# 提取中括号中的当前调度器scheduler=$(cat /sys/block/$dev/queue/scheduler 2>/dev/null | grep -o '\[[^]]*\]' | tr -d '[]')echo " - $dev: $scheduler"
done# 查看时区
timezone=$(timedatectl | grep "Time zone:" | awk '{print $3,$4,$5}')
echo "- **当前主机时区信息**:$(echo $timezone)"# 查看IP地址
ip_add=`hostname -I`
hostname1=`hostname`
sysuptime=`uptime -s`
echo "- **当前主机IP地址**:$ip_add"
echo "- **当前主机的主机名**:$hostname1"
echo "- **系统启动时间**:$sysuptime"# 查看网卡信息
echo "- **主机网卡信息**:"
echo '```'
# 表头
printf "%-20s %-10s %-10s %-18s %-18s %-18s %-10s %-10s %-10s %-10s %-8s %-10s\n" "Interface" "Type" "Type2" "MAC_Address" "IP_Address" "Gateway" "State" "RX" "TX" "Port_Type" "MTU" "General_State"
echo "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------"for interface in $(ls /sys/class/net/)
doif [ ! -d "/sys/class/net/$interface" ]; thencontinuefi# 基础信息mac=$(cat /sys/class/net/$interface/address 2>/dev/null || echo "N/A")ip=$(ip -4 addr show $interface | grep -oP '(?<=inet\s)\d+(\.\d+){3}' || echo "N/A")state=$(ip link show $interface | grep -oP '(?<=state\s)\w+' || echo "UNKNOWN")rx_bytes=$(cat /sys/class/net/$interface/statistics/rx_bytes 2>/dev/null || echo "0")tx_bytes=$(cat /sys/class/net/$interface/statistics/tx_bytes 2>/dev/null || echo "0")# 转换 RX 和 TXif (( rx_bytes >= 1024**4 )); thenrx=$(awk "BEGIN {printf \"%.2f TB\", $rx_bytes / (1024^4)}")elif (( rx_bytes >= 1024**3 )); thenrx=$(awk "BEGIN {printf \"%.2f GB\", $rx_bytes / (1024^3)}")elserx=$(awk "BEGIN {printf \"%.2f MB\", $rx_bytes / (1024^2)}")fiif (( tx_bytes >= 1024**4 )); thentx=$(awk "BEGIN {printf \"%.2f TB\", $tx_bytes / (1024^4)}")elif (( tx_bytes >= 1024**3 )); thentx=$(awk "BEGIN {printf \"%.2f GB\", $tx_bytes / (1024^3)}")elsetx=$(awk "BEGIN {printf \"%.2f MB\", $tx_bytes / (1024^2)}")fitype="Virtual"[ -e "/sys/class/net/$interface/device" ] && type="Physical"mtu=$(cat /sys/class/net/$interface/mtu 2>/dev/null || echo "N/A")# 获取 GENERAL.TYPE 信息general_type=$(nmcli device show $interface | grep -i "GENERAL.TYPE" | awk '{print $2}' || echo "N/A")# 获取 GENERAL.STATE 信息general_state=$(nmcli device show $interface | grep -i "GENERAL.STATE" | awk '{print $2,$3,$4,$5}' || echo "N/A")# 获取网关地址gateway=$(nmcli device show $interface | grep -i "IP4.GATEWAY" | awk '{print $2}' || echo "N/A")# 判断光口或电口port_type="Unknown"if command -v ethtool >/dev/null; thenport=$(ethtool $interface 2>/dev/null | grep "Port" | awk '{print $2}')case "$port" inFIBRE) port_type="Optical" ;;TP) port_type="Electrical" ;;esacfi# 打印结果printf "%-20s %-10s %-10s %-18s %-18s %-18s %-10s %-10s %-10s %-10s %-8s %-10s\n" "$interface" "$type" "$general_type" "$mac" "$ip" "$gateway" "$state" "$rx" "$tx" "$port_type" "$mtu" "$general_state"
done
echo '```'EOF
-------------------------
chmod +x /tmp/get_os_info.sh
sleep 1
sh /tmp/get_os_info.sh
--------
sh /tmp/get_os_info.sh
- **当前主机类型**: 虚拟机
- 当前主机系统硬件信息:
- 制造商: VMware, Inc.
- 产品名称: VMware Virtual Platform
- **操作系统信息**: Oracle Linux Server 7.9
- **内核版本**: 5.4.17-2011.6.2.el7uek.x86_64
- **硬件配置**:
- **CPU**: 13th Gen Intel(R) Core(TM) i7-1365U ,**共 2 核** ,超线程未开启 (每核线程数: 1)
- **内存**: 6.3G
- **磁盘**:
- sdb 1 spi 10G 疑似HDD机械硬盘,还需测试IO或咨询主机管理员
- sdc 1 spi 50G 疑似HDD机械硬盘,还需测试IO或咨询主机管理员
- sda 1 spi 50G 疑似HDD机械硬盘,还需测试IO或咨询主机管理员
```
/dev/sda disk 53GB VMware Virtual S
/dev/sdb disk 10GB VMware Virtual S
/dev/sdc disk 53GB VMware Virtual S
disk VMware, VMware Virtual S 1.0 /dev/sda
disk VMware, VMware Virtual S 1.0 /dev/sdb
disk VMware, VMware Virtual S 1.0 /dev/sdc
```
- **文件系统**:
```
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/ol-root xfs 47G 42G 5.0G 90% /
```
- **I/O 调度器**:
- sdb: mq-deadline
- sdc: mq-deadline
- sda: mq-deadline
- **当前主机时区信息**:America/New_York (EST, -0500)
- **当前主机IP地址**:192.168.1.120 10.10.1.23
- **当前主机的主机名**:rac3
- **系统启动时间**:2025-01-07 03:05:15
- **主机网卡信息**:
```
Interface Type Type2 MAC_Address IP_Address Gateway State RX TX Port_Type MTU General_State
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Error: NetworkManager is not running.
Error: NetworkManager is not running.
Error: NetworkManager is not running.
ens33 Physical 00:0c:29:b9:30:1f 192.168.1.120 UP 0.06 MB 0.15 MB Unknown 1500
Error: NetworkManager is not running.
Error: NetworkManager is not running.
Error: NetworkManager is not running.
ens38 Physical 00:0c:29:b9:30:29 10.10.1.23 UP 0.00 MB 0.02 MB Unknown 1500
Error: NetworkManager is not running.
Error: NetworkManager is not running.
Error: NetworkManager is not running.
lo Virtual 00:00:00:00:00:00 127.0.0.1 UNKNOWN 0.04 MB 0.04 MB Unknown 65536
```
----------------
sh get_os_info.sh
- **当前主机类型**: 物理机
- 当前主机系统硬件信息:
- 制造商: HPE
- 产品名称: ProLiant DL380 Gen10
- **操作系统信息**: Red Hat Enterprise Linux 8.8 (Ootpa)
- **内核版本**: 4.18.0-477.27.1.el8_8.x86_64
- **硬件配置**:
- **CPU**: Intel(R) Xeon(R) Gold 6258R CPU @ 2.70GHz Intel(R) Xeon(R) Gold 6258R CPU @ 2.70GHz ,**共 48 核** ,超线程已开启 (每核线程数: 2)
- **内存**: 2.0Ti
- **磁盘**:
- sda 0 sas 447.1G SSD固态硬盘
- sdb 1 fc 2T HDD机械硬盘
- sdc 1 fc 30G HDD机械硬盘
- sdd 1 fc 30G HDD机械硬盘
- sde 1 fc 30G HDD机械硬盘
- sdf 1 fc 2T HDD机械硬盘
- sdg 1 fc 2T HDD机械硬盘
- sdh 1 fc 2T HDD机械硬盘
- sdi 1 fc 2T HDD机械硬盘
- sdj 1 fc 2T HDD机械硬盘
- sdk 1 fc 2T HDD机械硬盘
- sdl 1 fc 2T HDD机械硬盘
- sdm 1 fc 2T HDD机械硬盘
- sdn 1 fc 2T HDD机械硬盘
- sdo 1 fc 500G HDD机械硬盘
- sdp 1 fc 500G HDD机械硬盘
- sdq 1 fc 2T HDD机械硬盘
- sdr 1 fc 2T HDD机械硬盘
- sds 1 fc 2T HDD机械硬盘
- sdt 1 fc 2T HDD机械硬盘
- sdu 1 fc 2T HDD机械硬盘
- sdv 1 fc 2T HDD机械硬盘
- sdw 1 fc 2T HDD机械硬盘
- sdx 1 fc 2T HDD机械硬盘
- sdy 1 fc 2T HDD机械硬盘
- sdz 1 fc 2T HDD机械硬盘
- sdaa 1 fc 2T HDD机械硬盘
- sdab 1 fc 2T HDD机械硬盘
- sdac 1 fc 2T HDD机械硬盘
- sdad 1 fc 2T HDD机械硬盘
- sdae 1 fc 2T HDD机械硬盘
- sdaf 1 fc 2T HDD机械硬盘
- sdag 1 fc 2T HDD机械硬盘
- sdah 1 fc 2T HDD机械硬盘
- sdai 1 fc 2T HDD机械硬盘
- sdaj 1 fc 2T HDD机械硬盘
- sdak 1 fc 2T HDD机械硬盘
- sdal 1 fc 2T HDD机械硬盘
- sdam 1 fc 2T HDD机械硬盘
- sdan 1 fc 2T HDD机械硬盘
- sdao 1 fc 2T HDD机械硬盘
- sdap 1 fc 2T HDD机械硬盘
- sdaq 1 fc 2T HDD机械硬盘
- sdar 1 fc 2T HDD机械硬盘
- sdas 1 fc 2T HDD机械硬盘
- sdat 1 fc 2T HDD机械硬盘
- sdau 1 fc 2T HDD机械硬盘
- sdav 1 fc 2T HDD机械硬盘
- sdaw 1 fc 2T HDD机械硬盘
- sdax 1 fc 2T HDD机械硬盘
- sday 1 fc 2T HDD机械硬盘
- sdaz 1 fc 2T HDD机械硬盘
- sdba 1 fc 2T HDD机械硬盘
- sdbb 1 fc 2T HDD机械硬盘
- sdbc 1 fc 2T HDD机械硬盘
- sdbd 1 fc 2T HDD机械硬盘
- sdbe 1 fc 2T HDD机械硬盘
- sdbf 1 fc 2T HDD机械硬盘
- sdbg 1 fc 2T HDD机械硬盘
- sdbh 1 fc 2T HDD机械硬盘
- sdbi 1 fc 2T HDD机械硬盘
- sdbj 1 fc 2T HDD机械硬盘
- sdbk 1 fc 2T HDD机械硬盘
- sdbl 1 fc 2T HDD机械硬盘
- sdbm 1 fc 2T HDD机械硬盘
- sdbn 1 fc 2T HDD机械硬盘
- sdbo 1 fc 2T HDD机械硬盘
- sdbp 1 fc 2T HDD机械硬盘
- sdbq 1 fc 2T HDD机械硬盘
- sdbr 1 fc 2T HDD机械硬盘
- sdbs 1 fc 2T HDD机械硬盘
- sdbt 1 fc 2T HDD机械硬盘
- sdbu 1 fc 2T HDD机械硬盘
- sdbv 1 fc 2T HDD机械硬盘
- sdbw 1 fc 2T HDD机械硬盘
- sdbx 1 fc 2T HDD机械硬盘
- sdby 1 fc 2T HDD机械硬盘
- sdbz 1 fc 2T HDD机械硬盘
- sdca 1 fc 2T HDD机械硬盘
- sdcb 1 fc 2T HDD机械硬盘
- sdcc 1 fc 2T HDD机械硬盘
- sdcd 1 fc 2T HDD机械硬盘
- sdce 1 fc 2T HDD机械硬盘
- sdcf 1 fc 2T HDD机械硬盘
- sdcg 1 fc 2T HDD机械硬盘
- sdch 1 fc 2T HDD机械硬盘
- sdci 1 fc 2T HDD机械硬盘
- sdcj 1 fc 2T HDD机械硬盘
- sdck 1 fc 2T HDD机械硬盘
- sdcl 1 fc 2T HDD机械硬盘
- sdcm 1 fc 2T HDD机械硬盘
- sdcn 1 fc 2T HDD机械硬盘
- sdco 1 fc 2T HDD机械硬盘
- sdcp 1 fc 2T HDD机械硬盘
- sdcq 1 fc 2T HDD机械硬盘
- sdcr 1 fc 2T HDD机械硬盘
- sdcs 1 fc 2T HDD机械硬盘
- sdct 1 fc 2T HDD机械硬盘
- sdcu 1 fc 2T HDD机械硬盘
- sdcv 1 fc 2T HDD机械硬盘
- sdcw 1 fc 2T HDD机械硬盘
- sdcx 1 fc 2T HDD机械硬盘
- sdcy 1 fc 2T HDD机械硬盘
- sdcz 1 fc 2T HDD机械硬盘
- sdda 1 fc 2T HDD机械硬盘
- sddb 1 fc 2T HDD机械硬盘
- sddc 1 fc 2T HDD机械硬盘
- sddd 1 fc 2T HDD机械硬盘
- sdde 1 fc 2T HDD机械硬盘
- sddf 1 fc 2T HDD机械硬盘
- sddg 1 fc 2T HDD机械硬盘
- sddh 1 fc 2T HDD机械硬盘
- sddi 1 fc 2T HDD机械硬盘
- sddj 1 fc 2T HDD机械硬盘
- sddk 1 fc 2T HDD机械硬盘
- sddl 1 fc 2T HDD机械硬盘
- sddm 1 fc 2T HDD机械硬盘
- sddn 1 fc 2T HDD机械硬盘
- sddo 1 fc 2T HDD机械硬盘
- sddp 1 fc 2T HDD机械硬盘
- sddq 1 fc 2T HDD机械硬盘
- sddr 1 fc 2T HDD机械硬盘
- sdds 1 fc 2T HDD机械硬盘
- sddt 1