脚本-批量收集服务器信息

本脚本用于一键获取服务器的信息(cpu, mem, 磁盘分组, 序列号等)

脚本依赖常用命令ls, cat, df, ip, lscpu, hostname, uname, free, dmidecode, ethtool等, 可能dmidocode和ethtool有些机器上没有, 需要安装一下, 其他的命令, 基本上都是linux服务器自带的

脚本

内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
➜  ~ cat get-system-info.sh
#!/bin/bash

#set -exu

get_serial_number=$(dmidecode -s system-serial-number |sed s/[[:space:]]//g)
get_hostname=$(hostname)

get_system=$(cat /etc/os-release | grep "PRETTY_NAME" | awk -F'=' '{print $NF}' | tr "\"" " ")
get_kernel=$(uname -r)

get_usage_root=$(df -lh / | grep -v Avail | awk '{print $4,$5}' | awk -F'[ %]' '{print $1"("100-$2"%)"}')
get_total_cpu=$(lscpu | grep "^CPU(s):" | awk '{print $NF}')
get_cpu_type=$(cat /proc/cpuinfo | grep 'model name' | uniq | cut -d ':' -f 2)
get_total_mem=$(free -mh | grep Mem | awk '{print $2}')


get_disk_info="-XxX"
for disk in $(lsblk | grep -w disk | awk '{print $1}')
do
disk_type=$(cat /sys/block/$disk/queue/rotational)
if [ "$disk_type" == "1" ]
then
get_disk_type="HDD"
elif [ "$disk_type" == "0" ]
then
get_disk_type="SSD"
else
get_disk_type="Unknown"
fi

get_disk_size=$(lsblk -o NAME,SIZE,TYPE | grep -w disk | grep $disk | awk '{print $2}')

is_system_disk=$(lsblk /dev/$disk | grep -w / | wc -l)
if [ "$is_system_disk" == "1" ]
then
get_disk_role="系统盘"
else
get_disk_role="数据盘"
fi

have_partition=$(lsblk /dev/$disk | grep -v NAME | wc -l)
if [ "$have_partition" == "1" ]
then
get_disk_used="未使用"
else
get_disk_used="使用中"
fi

get_disk_info="$get_disk_type-$get_disk_role-$get_disk_used-$disk-$get_disk_size-XxX$get_disk_info"
done


get_interface_info="-XxX"
for interface in $(ls -lh /sys/class/net/ | grep devices | grep -v virtual | awk '{print $9}')
do
interface_link=$(ethtool $interface | grep "Link detected" | awk '{print $NF}')
if [ "$interface_link" == "yes" ]
then
get_interface_link="已连接"
elif [ "$interface_link" == "no" ]
then
get_interface_link="未连接"
else
get_interface_link="Unknown"
fi

interface_type=$(ethtool $interface | grep "Supported ports" | awk '{print $(NF-1)}')
if [ "$interface_type" == "TP" ]
then
get_interface_type="电口"
elif [ "$interface_type" == "FIBRE" ]
then
get_interface_type="光口"
else
get_interface_type="Unknown"
fi

get_interface_info="$get_interface_link-$get_interface_type-$interface-XxX$get_interface_info"
done


get_ip_interface_info=$(ip -4 a | awk '/^[a-zA-Z0-9]+/{interface=$2; next} /inet /{print interface "" $2}' | sed ":a;N;s/\n/-XxX/g;ta")

tmpfile="/tmp/tmp-server-info.csv"
echo "序列号,主机名,系统,内核,根分区剩余,Cpu型号,Cpu,Mem,物理磁盘,物理网卡,IPv4信息" > $tmpfile
echo -n \=\"$get_serial_number\",$get_hostname,$get_system,$get_kernel,$get_usage_root,$get_cpu_type,$get_total_cpu,$get_total_mem,\"$get_disk_info\",\"$get_interface_info\",\"$get_ip_interface_info\" | sed 's@-XxX@\n@g' >> $tmpfile


echo "File: $tmpfile"
➜ ~

运行

1
bash get-system-info.sh 

跑完后,会产生文件/tmp/tmp-server-info.csv, 直接用excel打开,调整下格式就好

效果

最终的预览效果如下

system-info

作者

好方

发布于

2024-05-08

更新于

2024-06-11

许可协议