脚本
1cat > /tmp/get_os_info.sh <<"EOF"
2#!/bin/bash
3
4export LANG=en_US.UTF-8
5
6
7# 如果 cat /proc/1/cgroup | grep docker | wc -l 大于0 或 systemd-detect-virt 返回 docker,则为 docker容器,
8# 如果 virt-what 返回 kvm或vmware或hyperv或xen、xen-hvm、lxc 或 systemd-detect-virt 返回 vmware、kvm 则为虚拟机
9# 如果 cat /sys/class/dmi/id/product_name 返回 Bochs 、OpenStack Nova则为虚拟机 若返回Alibaba Cloud ECS,则代表阿里云主机
10# 如果 systemd-detect-virt 返回 none 则表示物理机
11
12# 判断当前主机类型
13if [ "$(cat /proc/1/cgroup | grep -c docker)" -gt 0 ] || systemd-detect-virt | grep -q docker; then
14 host_type="Docker容器"
15elif [[ -f /sys/class/dmi/id/product_name && $(cat /sys/class/dmi/id/product_name) == "Alibaba Cloud ECS" ]]; then
16 host_type="阿里云主机"
17elif [[ $(virt-what) =~ (kvm|vmware|hyperv|xen|xen-hvm|lxc) ]] || [[ $(systemd-detect-virt) =~ (vmware|kvm) ]]; then
18 host_type="虚拟机"
19elif [[ $(cat /sys/class/dmi/id/product_name) =~ (Bochs|OpenStack Nova) ]]; then
20 host_type="虚拟机"
21else
22 host_type="物理机"
23fi
24
25echo "- **当前主机类型**: $host_type"
26
27# 获取并显示系统制造商、产品名称和版本
28echo " - 当前主机系统硬件信息:"
29dmidecode -t 1 | awk -F: '/Manufacturer/{printf " - 制造商: %s\n", $2}
30 /Product Name/{printf " - 产品名称: %s\n", $2}'
31
32
33# 获取操作系统信息
34os_info=$(cat /etc/os-release | grep 'PRETTY_NAME' | cut -d '"' -f 2)
35# 如果操作系统是 CentOS Linux 7 (Core),查询 /etc/redhat-release
36if [[ "$os_info" == "CentOS Linux 7 (Core)" ]]; then
37 os_info=$(cat /etc/redhat-release)
38fi
39echo "- **操作系统信息**: $os_info"
40
41
42# 显示当前内核版本
43kernel_version=$(uname -r)
44echo "- **内核版本**: $kernel_version"
45
46
47echo "- **硬件配置**:"
48# 获取CPU信息
49cpu_model=$(lscpu | grep "Model name" | awk -F: '{print $2}' | xargs)
50cpu_cores=$(lscpu | grep "^CPU(s):" | awk '{print $2}')
51
52# 获取每核的线程数
53threads_per_core=$(lscpu | grep -i "Thread(s) per core" | awk '{print $4}')
54
55# 如果每核线程数大于1,则说明开启了超线程
56if [[ $threads_per_core -gt 1 ]]; then
57 HyperThreading="超线程已开启 (每核线程数: $threads_per_core)"
58else
59 HyperThreading="超线程未开启 (每核线程数: $threads_per_core)"
60fi
61
62
63echo " - **CPU**: $cpu_model ,**共 $cpu_cores 核** ,$HyperThreading "
64
65
66
67
68
69# 获取内存信息
70memory=$(free -h | awk '/^Mem:/{print $2}')
71echo " - **内存**: $memory"
72
73
74# 磁盘类型
75# 虚拟化平台(如 VMware、KVM、Hyper-V 等)可能会将虚拟硬盘映射为虚拟的 HDD 类型,即使宿主机的物理硬盘是 SSD。
76#
77# 使用dd测试,若超过500MB/s,则肯定是SSD盘,有的盘差点在300MB/s,完成时间在15秒以下
78# mkidr -p /data/
79# dd if=/dev/zero of=/data/testssd bs=1M count=5000 oflag=direct
80# dd if=/data/testssd of=/dev/null bs=1M count=5000 iflag=direct
81# rm -rf /data/testssd
82#
83# 如果 TRAN 列显示为 sata,则硬盘是 SATA 接口。
84# 如果 TRAN 列显示为 pcie 或 nvme,则硬盘是 NVMe 接口。
85# 如果 TRAN 列显示为 fc,则表示该硬盘使用的是 Fibre Channel 接口。
86# 获取指定磁盘信息
87echo " - **磁盘**: $disk_info"
88# lsblk -d -o NAME,ROTA,TRAN,SIZE | grep -E '^(sd|vd)' | awk '{print " -", $1,$2,$3,$4, ($2 == 1 ? "HDD机械硬盘" : "SSD固态硬盘")}'
89lsblk -d -o NAME,ROTA,TRAN,SIZE | grep -E '^(sd|vd)' | awk -v host_type="$host_type" '{
90 if (host_type == "物理机") {
91 print " -", $1, $2, $3, $4, ($2 == 0 ? "SSD固态硬盘" : "HDD机械硬盘")
92 } else {
93 print " -", $1, $2, $3, $4, ($2 == 0 ? "SSD固态硬盘" : "疑似HDD机械硬盘,还需测试IO或咨询主机管理员")
94 }
95}'
96
97
98echo ' ```'
99
100if command -v lshw &> /dev/null; then
101 lshw -short | grep disk | grep -v "cdrom" | awk '{$1=""; print " "$0}'
102fi
103
104lsscsi | grep -v "cd/dvd" | grep "disk" | awk '{$1=""; print " "$0}'
105
106echo ' ```'
107
108
109# 显示文件系统类型
110echo "- **文件系统**:"
111# df -Th 2>/dev/null | grep -v "loop" | grep -v "tmpfs" | awk '{print " -", $1, "->", $2}'
112echo ' ```'
113df -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}'
114echo ' ```'
115
116
117
118
119# 1. 对闪存(SSD)等存储介质,优先使用noop或none调度算法
120# 2. 对IO压力比较重且非SSD存储设备,且功能比较单一的场景,例如数据库服务器,使用deadline或mq-deadline调度算法
121
122# for dev in $(lsblk -dno NAME | grep -E '^(sd|vd)'); do echo "$dev: $(cat /sys/block/$dev/queue/scheduler)"; done
123# echo noop | sudo tee /sys/block/sda/queue/scheduler
124# for dev in $(lsblk -dno NAME | grep -E '^(sd|vd)'); do echo "echo noop | sudo tee /sys/block/$dev/queue/scheduler"; done
125
126
127# 获取I/O调度器信息
128echo "- **I/O 调度器**:"
129for dev in $(lsblk -dno NAME | grep -E '^(sd|vd)'); do
130 # 提取中括号中的当前调度器
131 scheduler=$(cat /sys/block/$dev/queue/scheduler 2>/dev/null | grep -o '\[[^]]*\]' | tr -d '[]')
132 echo " - $dev: $scheduler"
133done
134
135
136# 查看时区
137timezone=$(timedatectl | grep "Time zone:" | awk '{print $3,$4,$5}')
138echo "- **当前主机时区信息**:$(echo $timezone)"
139
140
141# 查看IP地址
142ip_add=`hostname -I`
143hostname1=`hostname`
144sysuptime=`uptime -s`
145echo "- **当前主机IP地址**:$ip_add"
146echo "- **当前主机的主机名**:$hostname1"
147echo "- **系统启动时间**:$sysuptime"
148
149
150# 查看网卡信息
151echo "- **主机网卡信息**:"
152echo '```'
153# 表头
154printf "%-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"
155echo "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
156
157for interface in $(ls /sys/class/net/)
158do
159 if [ ! -d "/sys/class/net/$interface" ]; then
160 continue
161 fi
162
163 # 基础信息
164 mac=$(cat /sys/class/net/$interface/address 2>/dev/null || echo "N/A")
165 ip=$(ip -4 addr show $interface | grep -oP '(?<=inet\s)\d+(\.\d+){3}' || echo "N/A")
166 state=$(ip link show $interface | grep -oP '(?<=state\s)\w+' || echo "UNKNOWN")
167
168
169 rx_bytes=$(cat /sys/class/net/$interface/statistics/rx_bytes 2>/dev/null || echo "0")
170 tx_bytes=$(cat /sys/class/net/$interface/statistics/tx_bytes 2>/dev/null || echo "0")
171 # 转换 RX 和 TX
172 if (( rx_bytes >= 1024**4 )); then
173 rx=$(awk "BEGIN {printf \"%.2f TB\", $rx_bytes / (1024^4)}")
174 elif (( rx_bytes >= 1024**3 )); then
175 rx=$(awk "BEGIN {printf \"%.2f GB\", $rx_bytes / (1024^3)}")
176 else
177 rx=$(awk "BEGIN {printf \"%.2f MB\", $rx_bytes / (1024^2)}")
178 fi
179
180 if (( tx_bytes >= 1024**4 )); then
181 tx=$(awk "BEGIN {printf \"%.2f TB\", $tx_bytes / (1024^4)}")
182 elif (( tx_bytes >= 1024**3 )); then
183 tx=$(awk "BEGIN {printf \"%.2f GB\", $tx_bytes / (1024^3)}")
184 else
185 tx=$(awk "BEGIN {printf \"%.2f MB\", $tx_bytes / (1024^2)}")
186 fi
187
188
189 type="Virtual"
190 [ -e "/sys/class/net/$interface/device" ] && type="Physical"
191 mtu=$(cat /sys/class/net/$interface/mtu 2>/dev/null || echo "N/A")
192
193 # 获取 GENERAL.TYPE 信息
194 general_type=$(nmcli device show $interface | grep -i "GENERAL.TYPE" | awk '{print $2}' || echo "N/A")
195
196 # 获取 GENERAL.STATE 信息
197 general_state=$(nmcli device show $interface | grep -i "GENERAL.STATE" | awk '{print $2,$3,$4,$5}' || echo "N/A")
198
199 # 获取网关地址
200 gateway=$(nmcli device show $interface | grep -i "IP4.GATEWAY" | awk '{print $2}' || echo "N/A")
201
202 # 判断光口或电口
203 port_type="Unknown"
204 if command -v ethtool >/dev/null; then
205 port=$(ethtool $interface 2>/dev/null | grep "Port" | awk '{print $2}')
206 case "$port" in
207 FIBRE) port_type="Optical" ;;
208 TP) port_type="Electrical" ;;
209 esac
210 fi
211
212
213
214 # 打印结果
215 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"
216done
217echo '```'
218
219
220EOF
221
222chmod +x /tmp/get_os_info.sh
223
224
225sleep 1
226
227sh /tmp/get_os_info.sh
228
229
230\cp /tmp/get_os_info.sh /usr/bin/
结果
可直接粘贴到markdown的文档中进行解析即可:
1[root@alldb ~]# sh /tmp/get_os_info.sh
2- **当前主机类型**: 虚拟机
3 - 当前主机系统硬件信息:
4 - 制造商: Bochs
5 - 产品名称: Bochs
6- **操作系统信息**: CentOS Linux release 7.9.2009 (Core)
7- **内核版本**: 3.10.0-1160.95.1.el7.x86_64
8- **硬件配置**:
9 - **CPU**: Intel(R) Xeon(R) Gold 6342 CPU @ 2.80GHz ,**共 32 核**
10 - **内存**: 62G
11 - **磁盘**:
12 - vda : HDD机械硬盘
13 - vdb : HDD机械硬盘
14 ```
15 /dev/vda disk 4294GB Virtual I/O device
16 /dev/vdb disk 2147MB Virtual I/O device
17 ```
18- **文件系统**:
19 ```
20 Filesystem Type Size Used Avail Use% Mounted on
21 /dev/mapper/centos-root xfs 3.9T 2.3T 1.7T 58% /
22 root@192.164.7.163:/bk fuse.sshfs 2.0T 796G 1.2T 40% /163_bk
23 ```
24- **I/O 调度器**:
25 - vda: mq-deadline
26 - vdb: mq-deadline
27- **当前主机时区信息**:Asia/Shanghai (CST, +0800)
28- **当前主机IP地址**:192.16.17.162 192.168.188.162
29- **当前主机的主机名**:alldb
30- **系统启动时间**:2024-08-07 15:46:55
31- **主机网卡信息**:
32```
33Interface Type Type2 MAC_Address IP_Address Gateway State RX TX Port_Type MTU General_State
34-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
35docker0 Virtual bridge 02:42:e3:8b:6a:13 182.82.0.1 -- UP 1.30 TB 2.07 TB Unknown 1500 10 (unmanaged)
36docker_gwbridge Virtual bridge 02:42:28:66:a3:c6 182.83.83.1 -- UP 615.27 MB 430.14 MB Unknown 1500 100 (connected)
37edge66 Virtual tun 36:71:6c:20:d5:24 182.166.66.163 -- UNKNOWN 160.63 MB 223.68 MB Unknown 1280 100 (connected)
38eth0 Physical ethernet fe:fc:fe:31:3f:43 163.15.7.163 163.15.7.254 UP 3.56 TB 2.45 TB Unknown 1500 10 (unmanaged)
39lo Virtual loopback 00:00:00:00:00:00 127.0.0.1 -- UNKNOWN 632.51 GB 632.51 GB Unknown 65536 10 (unmanaged)
40StarVPN Virtual tun 72:3a:25:c5:a0:16 182.166.166.4 -- UNKNOWN 125.24 MB 3.13 MB Unknown 1280 100 (connected)
41veth12cbd31 Virtual ethernet f6:8d:61:a6:82:cf N/A UP 614.34 GB 35.68 GB Unknown 1500 10 (unmanaged)
42veth227cc44 Virtual ethernet 86:28:23:ca:4e:64 N/A UP 103.37 MB 81.44 MB Unknown 1500 10 (unmanaged)
43veth27057ae Virtual ethernet ce:b4:f5:74:62:4f N/A UP 0.21 MB 32.31 MB Unknown 1500 10 (unmanaged)
44vethf0f646f Virtual ethernet 26:6c:51:fc:41:1d N/A UP 14.17 GB 152.13 GB Unknown 1500 10 (unmanaged)
45vethf781b88 Virtual ethernet 8a:22:7d:b1:30:c2 N/A UP 0.00 MB 16.26 MB Unknown 1500 10 (unmanaged)
46vethfcbac21 Virtual ethernet 52:1e:d6:72:66:17 N/A UP 635.62 MB 415.43 MB Unknown 1500 10 (unmanaged)
47ztqu3pmtrm Virtual tun 1e:c5:16:76:0a:b7 182.166.66.163 -- UNKNOWN 3.86 MB 1.55 MB Unknown 2600 100 (connected)
48```
解析后的效果: