概述

因项目需要需统计服务器设备型号/SN序列号/CPU型号/硬盘序列号等信息.

查询服务器基础信息

服务器硬件信息

查看服务器序列号及服务器型号

sudo dmidecode -s system-serial-number
sudo dmidecode -s system-product-name
CPU相关信息

获取CPU型号及CPU数量

awk -F':' '/model name/ {print $2}' /proc/cpuinfo |sort |uniq
awk -F':' '/physical id/ {print $2}' /proc/cpuinfo |sort |uniq |wc -l
内存相关信息

获取内存大小及数量

sudo dmidecode -t memory |egrep '^[[:space:]]Size:' |egrep -v 'Unknown|No' |awk '{i+=1}END{print "Mem Info: "$2"GB * "i}'
磁盘相关信息

通过megacli查看硬件RAID磁盘类型及数量

sudo megaclisas-status |awk -F'|' '/\[/ {print $2$4}' |sort|uniq -c

通过脚本获取详细数量及序列号

#!/bin/bash
# 获取RAID磁盘信息
# Author: admin@lolicp.com
# Date: 2021-04-08
 
# 获取磁盘硬件型号信息
Disk_Info=`sudo megacli -PDList -aALL |awk -F':' '/^Inquiry Data:/ {print $2}'`
# 获取磁盘空间信息
Disk_Size_Info=`sudo megacli -PDList -aALL |awk '/^Coerced Size:/ {print $3,$4}'`
# 获取磁盘接口类型
Disk_PD_Type_Info=`sudo megacli -PDList -aALL |awk '/^PD Type:/ {print $3}'`
# 获取磁盘属性
Disk_Media_Type_Info=`sudo megacli -PDList -aALL |awk '/^Media Type:/ {print $3}'`
Disk_Num=1
Disk_D_Sum=1
Disk_Model_D=""
IFS=$'\n'
for Disk_Type in ${Disk_Info};do
Disk_Size=`echo "${Disk_Size_Info}" | awk "NR==${Disk_Num} {print \\$0}"`
# 存储容量
Disk_Size_D=`echo "${Disk_Size}"|awk '{print $1}'`
# 存储容量类型
Disk_Size_Type=`echo "${Disk_Size}"|awk '{print $2}'`
if [ "${Disk_Size_Type}" = 'TB' ];then
Disk_Size_A_Info=`echo "${Disk_Size_D}"|awk '{print $1*1024*1024*1024*1024/1000/1000/1000/1000}'`
if [ "$[${Disk_Size_D: -1}%2]" -eq 0 ];then
Disk_Size_A=`echo "${Disk_Size_A_Info}" |head -c 4|awk '{print $1}'`
else
Disk_Size_A=`echo "${Disk_Size_A_Info}" |head -c 4|awk '{print $1+0.01}'`
fi
elif [ "${Disk_Size_Type}" = 'GB' ];then
Disk_Size_A_Info=`echo "${Disk_Size_D}"|awk '{print $1*1024*1024*1024/1000/1000/1000}'`
 
if [ "$[${Disk_Size_D: -1}%2]" -eq 0 ];then
Disk_Size_A=`echo "${Disk_Size_A_Info}" |head -c 5|awk '{print $1+0.5}'`
else
Disk_Size_A=`echo "${Disk_Size_A_Info}" |head -c 5|awk '{print $1+1.1}'`
fi
fi
Disk_PD_Type=`echo "${Disk_PD_Type_Info}" | awk "NR==${Disk_Num} {print \\$0}"`
Disk_Media_Type=`echo "${Disk_Media_Type_Info}" | awk "NR==${Disk_Num} {print \\$0}"`
if [ "${Disk_Media_Type}" = 'Hard' ];then
Disk_Media_Type='HDD'
elif [ "${Disk_Media_Type}" = 'Solid' ];then
Disk_Media_Type='SSD'
else
echo "获取设备属性失败!"
fi
 
if [[ "${Disk_Type}" =~ "SAMSUNG" ]];then
if [ "${Disk_Model_D}" != "SAMSUNG" ];then
Disk_D_Sum=1
fi
Disk_Model=`echo "${Disk_Type}" |awk '{print $1}'`
Disk_Model_D="SAMSUNG"
elif [[ "${Disk_Type}" =~ "INTEL" ]];then
if [ "${Disk_Model_D}" != "INTEL" ];then
Disk_D_Sum=1
fi
Disk_Model=`echo "${Disk_Type}" |awk '{print $1}'`
Disk_Model_D="INTEL"
elif [[ "${Disk_Type}" =~ "SEAGATE" ]];then
if [ "${Disk_Model_D}" != "SEAGATE" ];then
Disk_D_Sum=1
fi
Disk_Model=`echo "${Disk_Type}" |awk '{print $3}'`
Disk_Model_D="SEAGATE"
 
elif [[ "${Disk_Type}" =~ "T010" ]];then
if [ "${Disk_Model_D}" != "T010" ];then
Disk_D_Sum=1
fi
Disk_Model=`echo "${Disk_Type}" |awk '{print $1}'`
Disk_Model_D="T010"
elif [[ "${Disk_Type}" =~ "ZR09RP41" ]];then
if [ "${Disk_Model_D}" != "ZR09RP41" ];then
Disk_D_Sum=1
fi
Disk_Model=`echo "${Disk_Type}" |awk '{print $1}'`
Disk_Model_D="ZR09RP41"
else
echo "获取磁盘序列号失败!"
fi
echo "${Disk_D_Sum} ${Disk_Model} ${Disk_Media_Type} ${Disk_Size} Row ${Disk_Size_A} ${Disk_Size_Type}"
let Disk_D_Sum++
let Disk_Num++
done
END

本文标题:服务器硬件信息获取

本文作者:宇宙最帅的男人

本文链接:https://lolicp.com/linux/202115113.html

版权声明:转载或者引用本文内容请注明来源及原作者,本文著作权归 (lolicp.com) 所有。

除非另有说明,本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

最后修改:2024 年 10 月 28 日
如果觉得我的文章对你有用,请随意赞赏