LINUX和UBUNTU收集物理机设备信息的脚本

能通配一部份的设备,实际应用中可根据实际情况进行完善

bash 复制代码
#!/bin/bash
>mtmp1.txt.txt >mtmp2.txt.txt >cputmp1.txt.txt >cputmp2.txt.txt >disktmp1.txt.txt >disktmp2.txt.txt >bdtmp1.txt.txt >bdtmp2.txt.txt >Powertmp1.txt.txt >Powertmp2.txt.txt >results.txt.txt

echo "内存信息" >> results.txt.txt
dmidecode -t memory > mtmp1.txt.txt
awk 'BEGIN { RS="Memory Device" } NR>1 { gsub(/\n\s*/, " "); print $0 }' mtmp1.txt.txt | grep -v "Serial Number: NO DIMM" > mtmp2.txt.txt

while read line; do
    Size=$(echo $line | awk -F "Size:" '{print $2}' | awk '{print $1$2}')
    SN=$(echo $line  | awk -F "Serial Number:" '{print $2}' | awk '{print $1}')
    Locator=$(echo $line  | awk -F "Locator:" '{print $2}' | awk '{print $1}')
    Type=$(echo $line  | awk -F "Type:" '{print $2}' | awk '{print $1}')
    Speed=$(echo $line  | awk -F "Speed:" '{print $2}' | awk '{print $1$2}')
    Manufacturer=$(echo $line  | awk -F "Manufacturer:" '{print $2}' | awk '{print $1}')
	PN=$(echo $line  | awk -F "Part Number:" '{print $2}' | awk '{print $1}') 
    echo "Size:$Size##SN:$SN##Locator:$Locator##Type:$Type##Speed:$Speed##Manufacturer:$Manufacturer##PN:$PN" >> results.txt.txt
done < mtmp2.txt.txt
echo "--------" >> results.txt.txt


echo "CPU信息" >> results.txt.txt
dmidecode -t processor > cputmp1.txt.txt
awk 'BEGIN { RS="Processor Information" } NR>1 { gsub(/\n\s*/, " "); print $0 }' cputmp1.txt.txt > cputmp2.txt.txt
while read line; do
	SocketDes=$(echo $line | awk -F "Socket Designation:" '{print $2}' | awk '{print $1}') #插槽
	Version=$(echo $line |awk -F "Version:" '{print $2}' | awk -F "Voltage" '{print $1}')
	CoreCount=$(echo $line | awk -F "Core Count:" '{print $2}' | awk '{print $1}')
	Manufacturer=$(echo $line | awk -F "Manufacturer:" '{print $2}' | awk '{print $1$2}')
	echo "SocketDes:$SocketDes##Version:$Version##CoreCount:$CoreCount##Manufacturer:$Manufacturer" >> results.txt.txt
done < cputmp2.txt.txt
echo "--------" >> results.txt.txt

echo "硬盘信息" >> results.txt.txt
fun_lsblkgetinfos() {
sdlists=( $(lsblk | grep disk | awk '{print $1}') )
for sds in "${sdlists[@]}"
do
    Vendor=$(lsblk -o NAME,VENDOR | grep -w $sds | awk '{print $2}')
	Sn=$(lsblk -o NAME,SERIAL | grep -w $sds | awk '{print $2}')
	UserCapacity=$(lsblk -o NAME,SIZE | grep -w $sds | awk '{print $2}')
	echo "Vendor:$Vendor##Sn:$Sn##UserCapacity:$UserCapacity" >> results.txt.txt
done
}

fun_smartctlget() {
lsblk | grep disk | awk '{print "/dev/"$1}' | xargs -I {} smartctl -i {} > disktmp1.txt.txt
awk 'BEGIN { RS="START OF INFORMATION SECTION" } NR>1 { gsub(/\n\s*/, " "); print $0 }' disktmp1.txt.txt > disktmp2.txt.txt

while read line; do
	Vendor=$(echo $line | awk -F "Vendor:" '{print $2}' | awk '{print $1}')
	Sn=$(echo $line | awk -F "Serial number:" '{print $2}' | awk '{print $1}')
	UserCapacity=$(echo $line | awk -F "User Capacity:" '{print $2}' | awk '{print $1$2$3$4}')
	echo "Vendor:$Vendor##Sn:$Sn##UserCapacity:$UserCapacity" >> results.txt.txt
done < disktmp2.txt.txt
}

lsblk | grep disk | awk '{print "/dev/"$1}' | xargs -I {} smartctl -i {} > disktmp1.txt.txt >/dev/null 2>&1 && fun_smartctlget
lsblk | grep disk | awk '{print "/dev/"$1}' | xargs -I {} smartctl -i {} > disktmp1.txt.txt >/dev/null 2>&1 || fun_lsblkgetinfos
echo "--------" >> results.txt.txt

echo "网卡信息" >> results.txt.txt
OLD_IFS=$IFS
IFS=$'\n'
strlists=( $(ls /sys/class/net/ | grep -E 'ens|enp' | xargs -I {} ethtool {} | awk 'BEGIN { RS="Settings for " } NR>1 { gsub(/\n\s*/, " "); print $0 }') )
for strs in "${strlists[@]}"
do
eths=$(echo $strs | awk '{print $1}' | awk -F ":" '{print $1}')
spd=$(echo $strs | awk -F "Supported link modes:" '{print $2}' | awk -F "Supported pause frame use" '{print $1}'| sed 's/^[[:space:]]*//g')
spauto=$(echo $strs | awk -F "Supports auto-negotiation:" '{print $2}' | awk '{print $1}')
auto=$(echo $strs | awk -F "Auto-negotiation:" '{print $2}' | awk '{print $1}')
mac=$(cat /sys/class/net/$eths/address)
state=$(cat /sys/class/net/$eths/operstate)
echo "网卡:$eths##支持的链路模式:$spd##是否支持自动协商:$spauto##自动协商模式:$auto##MAC地址:$mac##状态:$state" >> results.txt.txt

done
IFS=$OLD_IFS
echo "--------" >> results.txt.txt

echo "主板信息" >> results.txt.txt
dmidecode -t baseboard > bdtmp1.txt.txt
awk 'BEGIN { RS="Handle 0x" } NR>1 { gsub(/\n\s*/, " "); print $0 }' bdtmp1.txt.txt > bdtmp2.txt.txt

FIRST_LINE=true
while read line; do
    if $FIRST_LINE; then
        Manufacturer=$(echo $line | awk -F "Manufacturer:" '{print $2}' | awk '{print $1}')
		ProductName=$(echo $line | awk -F "Product Name:" '{print $2}' | awk '{print $1}')
		Version=$(echo $line | awk -F "Version:" '{print $2}' | awk '{print $1}')
		SerialNumber=$(echo $line | awk -F "Serial Number:" '{print $2}' | awk '{print $1}')
		AssetTag=$(echo $line | awk -F "Asset Tag:" '{print $2}' | awk '{print $1}')
		echo "主板: Manufacturer:$Manufacturer##ProductName:$ProductName##Version:$Version##SerialNumber:$SerialNumber##资产标签:$AssetTag" >> results.txt.txt
    FIRST_LINE=false
    else
        ReDesign=$(echo $line | awk -F "Reference Designation:" '{print $2}' | awk -F "Type:" '{print $1}' | sed 's/^[[:space:]]*//g')
		Type=$(echo $line | awk -F "Type:" '{print $2}' | awk '{print $1}')
		Status=$(echo $line | awk -F "Status:" '{print $2}' | awk '{print $1}')
		BusAddress=$(echo $line | awk -F "Bus Address:" '{print $2}' | awk '{print $1}')
		echo "主板部件:  名称:$ReDesign##Type:$Type##Status:$Status##总线地址:$BusAddress" >> results.txt.txt
  fi
done < bdtmp2.txt.txt
echo "--------" >> results.txt.txt

echo "电源信息" >> results.txt.txt
dmidecode -t 39 > Powertmp1.txt.txt
awk 'BEGIN { RS="DMI type 39" } NR>1 { gsub(/\n\s*/, " "); print $0 }' Powertmp1.txt.txt > Powertmp2.txt.txt

while read line; do
	UnitGroup=$(echo $line | awk -F "Power Unit Group:" '{print $2}' | awk '{print $1}') #插槽
	Location=$(echo $line | awk -F "Location:" '{print $2}' | awk '{print $1}')
	Names=$(echo $line | awk -F "Name:" '{print $2}' | awk '{print $1}')
	Manufacturer=$(echo $line | awk -F "Manufacturer:" '{print $2}' | awk -F "Serial Number:" '{print $1}'| sed 's/^[[:space:]]*//g')
	SNumber=$(echo $line | awk -F "Serial Number:" '{print $2}' | awk '{print $1}')
	ModelPN=$(echo $line | awk -F "Model Part Number:" '{print $2}' | awk -F "Revision:" '{print $1}'| sed 's/^[[:space:]]*//g')
	Rev=$(echo $line | awk -F "Revision:" '{print $2}' | awk '{print $1}')
	MPC=$(echo $line | awk -F "Max Power Capacity:" '{print $2}' | awk -F "Status:" '{print $1}'| sed 's/^[[:space:]]*//g')
	Status=$(echo $line | awk -F "Status:" '{print $2}' | awk -F "Type:" '{print $1}'| sed 's/^[[:space:]]*//g')
	Hable=$(echo $line | awk -F "Hot Replaceable:" '{print $2}' | awk '{print $1}')
	echo "Group:$UnitGroup##位置:$Location##Names:$Names##Manufacturer:$Manufacturer##SN:$SNumber##ModelPN:$ModelPN##Revision:$Rev##最大功耗:$MPC##Status:$Status##热拔插:$Hable">> results.txt.txt
done < Powertmp2.txt.txt
echo "--------" >> results.txt.txt

#GPU
echo "GPU信息" >> results.txt.txt
#lspci -vnn | grep VGA -A 12

fun_nvidiasmiget() {
gnums=$(nvidia-smi -q | grep "Attached GPUs" | awk -F ": " '{print $2}')
DERV=$(nvidia-smi -q | grep "Driver Version" | awk -F ": " '{print $2}')
echo "GPU数量:$gnums##驱动程序版本:$DERV" >> results.txt.txt
OLD_IFS=$IFS
IFS=$'\n'
gpuinfolists=( $(nvidia-smi -q | awk 'BEGIN { RS="GPU 00000000:" } NR>1 { gsub(/\n\s*/, " "); print $0 }') )
for gpuinfostrs in "${gpuinfolists[@]}"
do
GPUID=$(echo $gpuinfostrs | awk '{print $1}')
pname=$(echo $gpuinfostrs | awk -F "Product Name                    :" '{print $2}' | awk -F "Product Brand" '{print $1}' | sed 's/^[[:space:]]*//g')
pbrand=$(echo $gpuinfostrs | awk -F "Product Brand                    :" '{print $2}' | awk -F "Display Mode" '{print $1}' | sed 's/^[[:space:]]*//g')
tmem=$(echo $gpuinfostrs | awk -F "FB Memory Usage" '{print $2}' | awk -F "BAR1 Memory Usage" '{print $1}' | sed 's/^[[:space:]]*//g' | awk -F ":" '{print $2}' | sed 's/^[[:space:]]*//g' | awk '{print $1,$2}')
umem=$(echo $gpuinfostrs | awk -F "FB Memory Usage" '{print $2}' | awk -F "BAR1 Memory Usage" '{print $1}' | sed 's/^[[:space:]]*//g' | awk -F ":" '{print $3}' | sed 's/^[[:space:]]*//g' | awk '{print $1,$2}')
freemem=$(echo $gpuinfostrs | awk -F "FB Memory Usage" '{print $2}' | awk -F "BAR1 Memory Usage" '{print $1}' | sed 's/^[[:space:]]*//g' | awk -F ":" '{print $4}' | sed 's/^[[:space:]]*//g' | awk '{print $1,$2}')
gpup=$(echo $gpuinfostrs | awk -F "Utilization" '{print $2}' | awk -F "Encoder Stats" '{print $1}' | sed 's/^[[:space:]]*//g' | awk -F ":" '{print $2}' | sed 's/^[[:space:]]*//g' | awk -F "%" '{print $1}')
memp=$(echo $gpuinfostrs | awk -F "Utilization" '{print $2}' | awk -F "Encoder Stats" '{print $1}' | sed 's/^[[:space:]]*//g' | awk -F ":" '{print $3}' | sed 's/^[[:space:]]*//g' | awk -F "%" '{print $1}')
echo "ID:$GPUID##产品名称:$pname##产品品牌:$pbrand##总内存:$tmem##使用的内存:$umem##空闲的内存:$freemem##GPU使用百分率:$gpup##内存使用百分率:$memp">> results.txt.txt
done
IFS=$OLD_IFS
}

fun_ttspotehrgpu() {
gnums=$(lspci -vnn | grep VGA -A 12 | grep "VGA compatible controller" | wc -l)
DERV=$(lspci -vnn | grep VGA -A 12 | grep "Kernel driver in use"  | awk -F ":" '{print $2}')
gpuname=$(lspci -vnn | grep VGA -A 12 | grep "DeviceName" | awk -F ":" '{print $2}' | sed 's/^[[:space:]]*//g')
echo "GPU数量:$gnums##驱动程序版本:$DERV##GPU名称:$gpuname" >> results.txt.txt
}


nvidia-smi >/dev/null 2>&1 && fun_nvidiasmiget
nvidia-smi >/dev/null 2>&1 || fun_ttspotehrgpu
echo "--------" >> results.txt.txt


rm -rf mtmp1.txt.txt mtmp2.txt.txt cputmp1.txt.txt cputmp2.txt.txt disktmp1.txt.txt disktmp2.txt.txt bdtmp1.txt.txt bdtmp2.txt.txt Powertmp1.txt.txt Powertmp2.txt.txt
相关推荐
PGCCC36 分钟前
【PGCCC】Postgresql 存储设计
数据库·postgresql
PcVue China3 小时前
PcVue + SQL Grid : 释放数据的无限潜力
大数据·服务器·数据库·sql·科技·安全·oracle
魔道不误砍柴功4 小时前
简单叙述 Spring Boot 启动过程
java·数据库·spring boot
锐策4 小时前
〔 MySQL 〕数据库基础
数据库·mysql
舞动CPU5 小时前
linux c/c++最高效的计时方法
linux·运维·服务器
远歌已逝5 小时前
管理Oracle实例(二)
数据库·oracle
日月星宿~6 小时前
【MySQL】summary
数据库·mysql
爱吃土豆的程序员6 小时前
在oracle官网下载资源显示400 Bad Request Request Header Or Cookie Too Large 解决办法
java·数据库·oracle·cookie
睿思达DBA_WGX6 小时前
Oracle 11g rac 集群节点的修复过程
数据库·oracle
尘浮生6 小时前
Java项目实战II基于微信小程序的移动学习平台的设计与实现(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·学习·微信小程序·小程序