服务器硬件key,服务器硬件信息--获取

1.1 简介

之前写过一个服务器硬件信息抓取的脚本,虽然写的不好,但是却还被许多网友分享了。现在回头看看当初的脚本,感觉像屎一样,很惭愧。

人总是在成长嘛,不光是皱纹,还有体重,希望大家能体谅我当年技术的不足。虽然我现在技术也不高,但相比之前要好一些。所以,下决心再重写一遍。

如果您在此篇文章中有所收获,那么我的辛苦就没有白费。其实奉献是一件很幸福的事儿,只要不赔钱。

1.2 结构

之前写的脚本只是想着把结果显示出来,没有深层次的构思,所以扩展性差,可读性差,适用性也差。今天痛定思痛,好好的从逻辑上想了想该怎么做。

我还大概画了一张草图,帮助理解:

0908d7f56a3d?from=groupmessage@

没错,真的是画的。

大概意思就是,我把硬件信息分成两个模块:

一个是客户端的脚本,这个脚本负责采集数据,并将采集的数据上传;

另一个是服务端,负责接收传来的数据,并做数据处理,转化为人类容易理解的数据(比如客户端上传了vendor id号 8086,那么服务端可翻译成Intel)。

1.3 脚本

最早我是用shell写的脚本,原因是用shell比较方便。

后来我使用python写了一遍,原因是shell脚本做字符串处理太麻烦了,没有python用着舒服。

最后我又用回了shell,原因是shell基本上不依赖系统环境,基本上所有的linux系统都支持bash shell,但是并不是每台机器的python环境都一样,虽然可以创建python虚拟环境,但是毕竟需要创建。

使用bash shell写客户端脚本,当时也考虑过性能问题,其实完全没必要。就好比你有一个地球,然后在这个地球上要放两件东西,一个是西瓜,一个是桃子,所以没必要纠结是西瓜大,还是桃子小。而且我主要的目的是实现功能。我不喜欢在吃泡面的时候,有人跟我谈营养。

这里我为了编写方便,将功能拆分成了多个,也便于理解。具体代码含义以及目的,我在注释上有写。

1.3.1 获取CPU信息

#!/bin/bash

#文件名: get_cpu_info.sh

#作者:liuxin

#更新时间: 20190830

# 我们生成的内容是

#[

# {

# "cpu_mhz": "1200.604",

# "numa_node0_cpus": "0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30",

# "cpu_min_mhz": "1200.0000",

# "l3_cache": "20480K",

# "l1d_cache": "32K",

# "core_per_socket": "8",

# "cpu_max_mhz": "3000.0000",

# "vendor_id": "GenuineIntel",

# "bogomips": "4199.74",

# "thread_per_core": "2",

# "l1i_cache": "32K",

# "cpus": "32",

# "byte_order": "LittleEndian",

# "numa_node1_cpus": "1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31",

# "cpu_family": "6",

# "numa_nodes": "2",

# "model_name": "Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz",

# "op_modes": "32-bit,64-bit",

# "l2_cache": "256K",

# "architecture": "x86_64",

# "sockets": "2",

# "virtualization": "VT-x",

# "stepping": "1",

# "model": "79"

# }

#]

# 需要使用到的命令或工具是 lscpu

function get_cpu()

{

lscpu > /tmp/cpu.hw.info

declare -A cpu_info

# 获取CPU的 architecture op_mode byte_order cpus

cpu_info[architecture]=$(cat /tmp/cpu.hw.info |grep '^Architecture:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[op_modes]=$(cat /tmp/cpu.hw.info |grep '^CPU op-mode(s):' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[byte_order]=$(cat /tmp/cpu.hw.info |grep '^Byte Order:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[cpus]=$(cat /tmp/cpu.hw.info |grep '^CPU(s):' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[sockets]=$(cat /tmp/cpu.hw.info |grep '^Socket(s):' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[core_per_socket]=$(cat /tmp/cpu.hw.info |grep 'Core(s) per socket:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[thread_per_core]=$(cat /tmp/cpu.hw.info |grep '^Thread(s) per core:'|awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[numa_nodes]=$(cat /tmp/cpu.hw.info |grep '^NUMA node(s):' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[vendor_id]=$(cat /tmp/cpu.hw.info |grep '^Vendor ID:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[cpu_family]=$(cat /tmp/cpu.hw.info |grep '^CPU family:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[model]=$(cat /tmp/cpu.hw.info |grep '^Model:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[model_name]=$(cat /tmp/cpu.hw.info |grep '^Model name:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[stepping]=$(cat /tmp/cpu.hw.info |grep '^Stepping:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[cpu_mhz]=$(cat /tmp/cpu.hw.info |grep '^CPU MHz:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[bogomips]=$(cat /tmp/cpu.hw.info |grep '^BogoMIPS:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[virtualization]=$(cat /tmp/cpu.hw.info |grep '^Virtualization:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[l1d_cache]=$(cat /tmp/cpu.hw.info |grep '^L1d cache:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[l1i_cache]=$(cat /tmp/cpu.hw.info |grep '^L1i cache:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[l2_cache]=$(cat /tmp/cpu.hw.info |grep '^L2 cache:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[l3_cache]=$(cat /tmp/cpu.hw.info |grep '^L3 cache:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[numa_node0_cpus]=$(cat /tmp/cpu.hw.info |grep '^NUMA node0 CPU(s):' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[numa_node1_cpus]=$(cat /tmp/cpu.hw.info |grep '^NUMA node1 CPU(s):' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[cpu_max_mhz]=$(cat /tmp/cpu.hw.info |grep '^CPU max MHz:' |awk -F ':' '{print $NF}' |tr -d ' ')

cpu_info[cpu_min_mhz]=$(cat /tmp/cpu.hw.info |grep '^CPU min MHz:' |awk -F ':' '{print $NF}' |tr -d ' ')

# 如果没有获取到 model_name,那么通过别的方法获取一下

if [ -z "${cpu_info[model_name]}" ]; then

cpu_info[model_name]=$(cat /proc/cpuinfo |grep 'model name' |head -n 1|awk -F ':' '{print $NF}' |tr -d ' ')

fi

# 删除产生的临时文件

rm -f /tmp/cpu.hw.info

# 将关联数组转化为json字符串输出

local str_json="{}"

for key in ${!cpu_info[@]}

do

_temp_json="{\"$key\":\"${cpu_info[$key]}\"}"

str_json=$(jq -n "$str_json + $_temp_json")

done

echo $str_json

return 0

}

# 获取到所有cpu的信息

# 这里我用数据作为输出格式,只是为了格式统一

function get_cpu_all()

{

local cpu_all_info="[]"

echo "[$(get_cpu)]"

return 0

}

#------------------

## 入口函数,也是主函数

#function main()

#{

# get_cpu_all

#}

#

#main "$@"

1.3.2 获取内存信息

#!/bin/bash

#文件名: get_memory_info.sh

#作者:liuxin

#更新时间: 20190830

# 我们生成的内容是

#[

# {

# "set": "1",

# "maximum_voltage": "1.2V",

# "configured_voltage": "1.2V",

# "rank": "2",

# "asset_tag": "011850A0",

# "locator": "A1",

# "minimum_voltage": "1.2V",

# "form_factor": "DIMM",

# "speed": "2666MHz",

# "configured_clock_speed": "2400MHz",

# "manufacturer": "00AD063200AD",

# "total_width": "72bits",

# "bank_locator": "NotSpecified",

# "part_number": "HMA82GR7AFR8N-VK",

# "type_detail": "Synchronous",

# "data_width": "64bits",

# "serial_number": "2DEAAAAA",

# "size": "16384MB",

# "type": "DDR4"

# }

#]

# 需要使用到的命令或工具是 dmidecode csplit

# 获取每个槽位的内存信息

function get_memory()

{

local memory_info_file="$1"

declare -A memory_slot

# 获取set maximum_voltage configured_voltage minimum_voltage total_width type_detail rank asset_tag

memory_slot[set]=$(cat $memory_info_file |grep 'Set:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[maximum_voltage]=$(cat $memory_info_file |grep 'Maximum Voltage:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[configured_voltage]=$(cat $memory_info_file |grep 'Configured Voltage:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[minimum_voltage]=$(cat $memory_info_file |grep 'Minimum Voltage:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[total_width]=$(cat $memory_info_file |grep 'Total Width:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[type_detail]=$(cat $memory_info_file |grep 'Type Detail:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[data_width]=$(cat $memory_info_file |grep 'Data Width:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[rank]=$(cat $memory_info_file |grep 'Rank:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[asset_tag]=$(cat $memory_info_file |grep 'Asset Tag:' |awk -F ':' '{print $NF}' |tr -d ' ')

# 获取locator form_factor speed configured_clock_speed manufacturer bank_locator part_number serial_number size type

memory_slot[locator]=$(cat $memory_info_file |grep ' Locator:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[form_factor]=$(cat $memory_info_file |grep 'Form Factor:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[speed]=$(cat $memory_info_file |grep " Speed:" |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[configured_clock_speed]=$(cat $memory_info_file |grep 'Configured Clock Speed:'|awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[manufacturer]=$(cat $memory_info_file |grep 'Manufacturer:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[bank_locator]=$(cat $memory_info_file |grep 'Bank Locator:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[part_number]=$(cat $memory_info_file |grep 'Part Number:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[serial_number]=$(cat $memory_info_file |grep 'Serial Number:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[size]=$(cat $memory_info_file |grep 'Size:' |awk -F ':' '{print $NF}' |tr -d ' ')

memory_slot[type]=$(cat $memory_info_file |grep 'Type:' |awk -F ':' '{print $NF}' |tr -d ' ')

# 将关联数组转化为json字符串输出

local str_json="{}"

for key in ${!memory_slot[@]}

do

_temp_json="{\"$key\":\"${memory_slot[$key]}\"}"

str_json=$(jq -n "$str_json + $_temp_json")

done

echo $str_json |jq .

return 0

}

# 将内存槽位信息拆分成一条一条的,并且获取到所有的内存槽位信息

function get_memory_all()

{

local num=0

IFS_old=$IFS

IFS=$'\n'

rm -f /tmp/memory_info_*.hw.info

# 取到所有内存槽位的信息并保存

dmidecode -t memory >/tmp/memory_info_all.hw.info

# 使用 csplit 进行分割

csplit /tmp/memory_info_all.hw.info /Memory\ Device/ -n3 -s {*} -f /tmp/memory_info_ -b "%d.hw.info"

#ls /tmp/memory_info_*.hw.info

# 删除第一条信息和总信息

rm -f /tmp/memory_info_0.hw.info

rm -f /tmp/memory_info_all.hw.info

# 获取每条内存的信息

local memory_all_info="[]"

for i in $(ls /tmp/memory_info_*.hw.info)

do

#get_memory "$i"

local _temp="[$(get_memory "$i")]"

memory_all_info=$(echo $memory_all_info |jq ". + $_temp")

done

# 去掉没插内存的槽位信息

#echo $memory_all_info |jq 'map(select(.size != "NoModuleInstalled"))'

memory_all_info=$(echo $memory_all_info |jq 'map(select(.size != "NoModuleInstalled"))')

echo $memory_all_info

rm -f /tmp/memory_info_*.hw.info

return 0

}

#------------------

## 入口函数,也是主函数

#function main()

#{

# get_memory_all

#}

#

#main "$@"

1.3.3 获取硬盘信息

#!/bin/bash

#文件名: get_disk_info.sh

#作者:liuxin

#更新时间: 20190830

# 我们生成的内容是

#[

# {

# "compliance": "SPC-4",

# "transport_protocol": "SAS",

# "serial_number": "03GZ8AAA",

# "logical_block_size": "512bytes",

# "product": "HUC101860CSS200",

# "revision": "FU29",

# "local_time_is": "562019CST",

# "temperature_warning": "DisabledorNotSupported",

# "locator": "/dev/bus/0-dmegaraid,0",

# "lun_id": "0x5000cca02f3707fc",

# "smart_health_status": "OK",

# "device_type": "disk",

# "user_capacity": "600GB",

# "vendor": "HGST",

# "rotation_rate": "10000rpm",

# "form_factor": "2.5inches"

# }

#]

# 需要使用到的命令或工具是 smartctl ssacli (因为硬件发展比较快,建议将smartctl升级到最新,否则有些硬盘不能识别)

function get_disk()

{

#/dev/bus/0 -d megaraid,0

local slot="$*"

smartctl -iH $slot > /tmp/disk.hw.info

declare -A disk_slot

# 获取硬盘的locator

disk_slot[locator]=$(echo "$slot" |tr -d ' ')

# 获取硬盘的vendor product revision compliance serial_number

disk_slot[vendor]=$(cat /tmp/disk.hw.info |grep '^Vendor:' |awk -F ':' '{print $NF}' |awk '{print $1}')

disk_slot[product]=$(cat /tmp/disk.hw.info |grep '^Product:' |awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[revision]=$(cat /tmp/disk.hw.info |grep '^Revision:' |awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[compliance]=$(cat /tmp/disk.hw.info |grep '^Compliance:' |awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[serial_number]=$(cat /tmp/disk.hw.info |grep '^Serial ' |awk -F ':' '{print $NF}' |tr -d ' ')

# 获取硬盘的user_capacity(size) logical_block_size physical_block_size rotation_rate form_factor lun_id

disk_slot[user_capacity]=$(cat /tmp/disk.hw.info |grep '^User Capacity:' |egrep -o '\[.*\]' |tr -d '\[|\]| ')

disk_slot[rotation_rate]=$(cat /tmp/disk.hw.info |grep '^Rotation Rate:' |awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[form_factor]=$(cat /tmp/disk.hw.info |grep '^Form Factor:' |awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[lun_id]=$(cat /tmp/disk.hw.info |grep '^Logical Unit id:' |awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[logical_block_size]=$(cat /tmp/disk.hw.info |grep '^Logical block size:' |awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[physical_block_size]=$(cat /tmp/disk.hw.info |grep '^Sector Sizes:' |egrep -o ', [0-9].*physical' |sed s/physical//g |tr -d ' |,')

# 获取硬盘的device_type transport_protocol local_time_is smart_health_status temperature_warning firmware_version

disk_slot[device_type]=$(cat /tmp/disk.hw.info |grep '^Device type:' |awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[transport_protocol]=$(cat /tmp/disk.hw.info |grep '^Transport protocol:' |egrep -o 'SAS|SAT')

disk_slot[local_time_is]=$(cat /tmp/disk.hw.info |grep '^Local Time is:' |awk -F 's:' '{print $NF}' |tr -d ' ')

disk_slot[smart_health_status]=$(cat /tmp/disk.hw.info |grep '^SMART Health Status:'|awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[temperature_warning]=$(cat /tmp/disk.hw.info |grep '^Temperature Warning:'|awk -F ':' '{print $NF}' |tr -d ' ')

disk_slot[firmware_version]=$(cat /tmp/disk.hw.info |grep '^Firmware Version:' |awk -F ':' '{print $NF}' |tr -d ' ')

# 比较麻烦的数据抓取信息,需要逻辑判断一下

# 硬盘logical_block_size

if [ "${disk_slot[logical_block_size]}" == "" ]; then

disk_slot[logical_block_size]=$(cat /tmp/disk.hw.info |grep '^Sector Sizes:' |egrep -o '[0-9].*logical,' |sed s/logical//g |tr -d ' |,')

fi

# 硬盘的lun_id

if [ "${disk_slot[lun_id]}" == "" ]; then

disk_slot[lun_id]=$(cat /tmp/disk.hw.info |grep '^LU WWN Device Id:' |awk -F ':' '{print $NF}' |tr -d ' ')

fi

# 硬盘的型号

if [ "${disk_slot[product]}" == "" ]; then

disk_slot[product]=$(cat /tmp/disk.hw.info |grep '^Device Model:' |awk -F ':' '{print $NF}' |awk '{print $1}')

fi

# 硬盘的接口

if [ "${disk_slot[transport_protocol]}" == "" ]; then

disk_slot[transport_protocol]=$(cat /tmp/disk.hw.info |grep '^SATA Version is:' |grep -o 'SATA'|head -n1)

fi

# 硬盘健康状态

if [ "${disk_slot[smart_health_status]}" == "" ]; then

disk_slot[smart_health_status]=$(cat /tmp/disk.hw.info |grep '^SMART overall-health self-assessment test result:' |awk -F ':' '{print $NF}' |tr -d ' ')

fi

# 删除产生的临时文件

rm -f /tmp/disk.hw.info

# 将关联数组转化为json字符串输出

local str_json="{}"

for key in ${!disk_slot[@]}

do

_temp_json="{\"$key\":\"${disk_slot[$key]}\"}"

str_json=$(jq -n "$str_json + $_temp_json")

done

echo $str_json |jq .

return 0

}

# 获取到所有硬盘的信息

function get_disk_all()

{

local disk_all_info="[]"

# 将硬盘信息存到文件中

# 因为阵列卡种类繁多,驱动类型也不一样,导致了不通的机器使用smartctl查看信息的参数也不同

# 我这边只列举了常用的三种驱动类型,以及查看方法(megaraid_sas mpt3sas smartpqi)

local array_driver=$(lsmod |egrep '^(megaraid_sas|mpt3sas|smartpqi)' |awk '{print $1}')

if [ "$array_driver" == "megaraid_sas" ]; then

smartctl --scan-open |egrep 'megaraid' |grep -v '^#'|cut -d '#' -f1 >/tmp/disk1.hw.info

if [ $(cat /tmp/disk1.hw.info |wc -l) -eq 0 ]; then

smartctl --scan-open |egrep 'scsi' |grep -v '^#'|cut -d '#' -f1 >/tmp/disk1.hw.info

fi

elif [ "$array_driver" == "mpt3sas" ]; then

smartctl --scan-open |grep sat |grep -v '^#'|cut -d '#' -f1 >/tmp/disk1.hw.info

elif [ "$array_driver" == "smartpqi" ]; then

local disk_num=$(ssacli ctrl slot=0 pd all show |grep physicaldrive |awk '{print $2}' |wc -l)

for((i=0;i

echo "-d cciss,$i /dev/sda" >>/tmp/disk1.hw.info

done

else

>/tmp/disk1.hw.info

fi

# 循环读取每块硬盘的信息,并添加到数组中

while read Line

do

local _temp="[$(get_disk $Line)]"

disk_all_info=$(echo $disk_all_info |jq ". + $_temp")

done

rm -f /tmp/disk1.hw.info

# 输出结果

echo $disk_all_info

return 0

}

#------------------

## 入口函数,也是主函数

#function main()

#{

# get_disk_all

#}

#

#main "$@"

1.3.4 获取nvme信息

#!/bin/bash

#文件名: get_nvme_info.sh

#作者:liuxin

#更新时间: 20190830

# 我们生成的内容是

#[

# {

# "controller_id": "0",

# "ieee_out_identifier": "0x5cd2e4",

# "serial_number": "PHLE811300G82PAAAAA",

# "logical_block_size": "512",

# "product": "INTELSSDPE2KE020T7",

# "local_time_is": "FriAug3016:19:272019CST",

# "locator": "-dnvme/dev/nvme0",

# "smart_health_status": "PASSED",

# "user_capacity": "2.00TB",

# "vendor": "0x8086",

# "firmware_version": "QDV10170"

# }

#]

# 需要使用到的命令或工具是 smartctl (因为硬件发展比较快,建议将smartctl升级到最新,否则有些硬盘不能识别)

function get_nvme()

{

# -d nvme /dev/nvme0

local slot="$*"

smartctl -iH $slot > /tmp/nvme.hw.info

declare -A nvme_slot

# 获取nvme硬盘的locator

nvme_slot[locator]=$(echo "$slot" |tr -d ' ')

# 获取硬盘的vendor product serial_number firmware_version ieee_out_identifier user_capacity logical_block_size local_time_is smart_health_status controller_id

nvme_slot[vendor]=$(cat /tmp/nvme.hw.info |grep '^PCI Vendor' |awk -F ':' '{print $NF}' |tr -d ' ')

nvme_slot[product]=$(cat /tmp/nvme.hw.info |grep '^Model Number:' |awk -F ':' '{print $NF}' |tr -d ' ')

nvme_slot[serial_number]=$(cat /tmp/nvme.hw.info |grep '^Serial ' |awk -F ':' '{print $NF}' |tr -d ' ')

nvme_slot[firmware_version]=$(cat /tmp/nvme.hw.info |grep '^Firmware Version:' |awk -F ':' '{print $NF}' |tr -d ' ')

nvme_slot[ieee_out_identifier]=$(cat /tmp/nvme.hw.info |grep '^IEEE OUI Identifier:'|awk -F ':' '{print $NF}' |tr -d ' ')

nvme_slot[user_capacity]=$(cat /tmp/nvme.hw.info |grep '^Namespace 1 Size/Capacity:'|egrep -o '\[.*\]' |tr -d '\[|\]| ')

nvme_slot[logical_block_size]=$(cat /tmp/nvme.hw.info |grep '^Namespace 1 Formatted LBA Size:'|awk -F ':' '{print $NF}' |tr -d ' ')

nvme_slot[local_time_is]=$(cat /tmp/nvme.hw.info |grep '^Local Time is:' |awk -F 's:' '{print $NF}' |tr -d ' ')

nvme_slot[smart_health_status]=$(cat /tmp/nvme.hw.info |grep '^SMART overall-health'|awk -F ':' '{print $NF}' |tr -d ' ')

nvme_slot[controller_id]=$(cat /tmp/nvme.hw.info |grep '^Controller ID:' |awk -F ':' '{print $NF}' |tr -d ' ')

# 删除产生的临时文件

rm -f /tmp/nvme.hw.info

# 将关联数组转化为json字符串输出

local str_json="{}"

for key in ${!nvme_slot[@]}

do

_temp_json="{\"$key\":\"${nvme_slot[$key]}\"}"

str_json=$(jq -n "$str_json + $_temp_json")

done

echo $str_json |jq .

return 0

}

# 获取到所有nvme硬盘的信息

function get_nvme_all()

{

local nvme_all_info="[]"

# 获取nvme盘符

ls /dev/nvme* 2>/dev/null|egrep -o nvme[0-9]+$ >/tmp/nvme1.hw.info

#cat /tmp/nvme1.hw.info

# 判断是否存在nvme硬盘

if [ $(cat /tmp/nvme1.hw.info |wc -l) -eq 0 ]; then

echo $nvme_all_info

rm -f /tmp/nvme1.hw.info

return 0

fi

# 循环读取每块nvme硬盘的信息,并添加到数组中

while read Line

do

local _temp="[$(get_nvme "-d nvme /dev/$Line")]"

nvme_all_info=$(echo $nvme_all_info |jq ". + $_temp")

done

# 删除临时文件

rm -f /tmp/nvme1.hw.info

# 输出结果

echo $nvme_all_info

return 0

}

#------------------

## 入口函数,也是主函数

#function main()

#{

# get_nvme_all

#}

#

#main "$@"

1.3.5 获取网卡信息

#!/bin/bash

#文件名: get_netcard_info.sh

#作者:liuxin

#更新时间: 20190830

# 我们生成的内容是

#[

# {

# "slot": "01:00.0",

# "name": "eth2",

# "pci_id_1": "8086:1521",

# "pci_id_2": "1028:1f9a",

# "supported_ports": "TP",

# "mac": "e4:43:4b:42:73:d8",

# "chassis": "",

# "link_detected": "0",

# "speed": "Unknown!",

# "port": ""

# }

#]

# 需要使用到的命令或工具是 lspci ethtool lldpcli

function get_netcard()

{

local slot="$1"

lspci -nn -vmm | egrep 'Ethernet controller' -A4 -B1 |grep "$slot" -A5 >/tmp/netcard.hw.info

declare -A netcard_port

# 获取slot的id

netcard_port[slot]="$slot"

# 获取网卡的pciid,分成两个字段

local _temp1=$(cat /tmp/netcard.hw.info |grep "^Vendor:" |awk '{print $NF}' |tr -d '[|]')

local _temp2=$(cat /tmp/netcard.hw.info |grep "^Device:" |awk '{print $NF}' |tr -d '[|]')

netcard_port[pci_id_1]="$_temp1:$_temp2"

local _temp3=$(cat /tmp/netcard.hw.info |grep "^SVendor:" |awk '{print $NF}' |tr -d '[|]')

local _temp4=$(cat /tmp/netcard.hw.info |grep "^SDevice:" |awk '{print $NF}' |tr -d '[|]')

netcard_port[pci_id_2]="$_temp3:$_temp4"

# 获取对应的网口名称,如果没有(可能是驱动问题),则默认为空字符串

local _temp0=$(ls -l /sys/class/net/* |grep $slot |awk -F '/' '{print $NF}')

netcard_port[name]="$_temp0"

if [ -z "$_temp0" ]

then

netcard_port[supported_ports]=""

netcard_port[link_detected]=""

netcard_port[speed]=""

netcard_port[mac]=""

netcard_port[chassis]=""

netcard_port[port]=""

else

# 获取ethtool工具输出的信息

ethtool ${netcard_port[name]} >/tmp/netcard1.hw.info 2>/dev/null

local _temp5=$(cat /tmp/netcard1.hw.info |grep 'Supported ports:' |egrep -o '\[.*\]' |tr -d '[|]| ')

netcard_port[supported_ports]="$_temp5"

local _temp6=$(cat /tmp/netcard1.hw.info |grep 'Link detected:' |awk '{print $NF}')

netcard_port[link_detected]="$_temp6"

local _temp7=$(cat /tmp/netcard1.hw.info |grep 'Speed:' |awk '{print $NF}')

netcard_port[speed]="$_temp7"

# 获取mac

local _temp8=$(cat /sys/class/net/${netcard_port[name]}/address 2>/dev/null)

netcard_port[mac]="$_temp8"

# 获取交换机端信息,通过使用lldpcli

local _temp9=$(lldpcli show neighbors ports ${netcard_port[name]} summary -f json |jq .lldp.interface.${netcard_port[name]}.chassis |jq -r 'if . then . else "" end' |jq -r keys[0])

local _temp10=$(lldpcli show neighbors ports ${netcard_port[name]} summary -f json |jq .lldp.interface |jq -r "if . then .${netcard_port[name]}.port.id.value else \"\" end")

netcard_port[chassis]="$_temp9"

netcard_port[port]="$_temp10"

fi

# 删除产生的临时文件

rm -f /tmp/netcard.hw.info

rm -f /tmp/netcard1.hw.info

# 将关联数组转化为json字符串输出

local str_json="{}"

for key in ${!netcard_port[@]}

do

_temp_json="{\"$key\":\"${netcard_port[$key]}\"}"

str_json=$(jq -n "$str_json + $_temp_json")

done

echo $str_json |jq .

return 0

}

function get_netcard_all()

{

local netcard_all_info="[]"

for i in $(lspci -nn -vmm | egrep 'Ethernet controller' -B1 |grep Slot |awk '{print $NF}')

do

local _temp="[$(get_netcard "$i")]"

netcard_all_info=$(echo $netcard_all_info |jq ". + $_temp")

done

echo $netcard_all_info

return 0

}

#------------------

## 入口函数,也是主函数

#function main()

#{

# get_netcard_all

#}

#

#main "$@"

1.3.6 获取raid卡信息

#!/bin/bash

#文件名: get_raidcard_info.sh

#作者:liuxin

#更新时间: 20190830

# 我们生成的内容是

#[

# {

# "pci_id": "1000:005f"

# }

#]

# 需要使用到的命令或工具是 lscpi

function get_raidcard()

{

declare -A raidcard_info

raidcard_info[pci_id]=$(lspci -nn |egrep 'LSI|SCSI' |egrep -o '\[[0-9a-z]{4}\:[0-9a-z]{4}\]' |tr -d '\[\]')

# 将关联数组转化为json字符串输出

local str_json="{}"

for key in ${!raidcard_info[@]}

do

_temp_json="{\"$key\":\"${raidcard_info[$key]}\"}"

str_json=$(jq -n "$str_json + $_temp_json")

done

echo $str_json

return 0

}

# 获取到所有raidcard的信息

# 这里我用数据作为输出格式,只是为了格式统一

function get_raidcard_all()

{

local raidcard_all_info="[]"

echo "[$(get_raidcard)]"

return 0

}

#------------------

## 入口函数,也是主函数

#function main()

#{

# get_raidcard_all

#}

#

#main "$@"

1.3.7 获取基本信息

#!/bin/bash

#文件名: get_base_info.sh

#作者:liuxin

#更新时间: 20190830

# 我们生成的内容是

#[

# {

# "hostname": "",

# "processor": "x86_64",

# "version": "NotSpecified",

# "hardware_platform": "x86_64",

# "machine": "x86_64",

# "operating_system": "GNU/Linux",

# "serial_number": "AAAAAA",

# "wake-up_type": "PowerSwitch",

# "kernel_name": "Linux",

# "uuid": "4C4C4544-0039-4610-8033-C4C04F4B4C32",

# "product_name": "PowerEdgeR630",

# "kernel_release": "3.10.0-862.14.4.el7.x86_64",

# "family": "NotSpecified",

# "manufacturer": "DellInc."

# }

#]

# 需要使用到的命令或工具是 dmidecode uname

function get_base()

{

dmidecode -t1 > /tmp/base.hw.info

declare -A base_info

# 获取服务器的 manufacturer product_name version serial_number uuid wake-up_type family

base_info[manufacturer]=$(cat /tmp/base.hw.info |grep 'Manufacturer:' |awk -F ':' '{print $NF}' |tr -d ' ')

base_info[product_name]=$(cat /tmp/base.hw.info |grep 'Product Name:' |awk -F ':' '{print $NF}' |tr -d ' ')

base_info[version]=$(cat /tmp/base.hw.info |grep 'Version:' |awk -F ':' '{print $NF}' |tr -d ' ')

base_info[serial_number]=$(cat /tmp/base.hw.info |grep 'Serial Number:'|awk -F ':' '{print $NF}' |tr -d ' ')

base_info[uuid]=$(cat /tmp/base.hw.info |grep 'UUID:' |awk -F ':' '{print $NF}' |tr -d ' ')

base_info[wake-up_type]=$(cat /tmp/base.hw.info |grep 'Wake-up Type:' |awk -F ':' '{print $NF}' |tr -d ' ')

base_info[family]=$(cat /tmp/base.hw.info |grep 'Family:' |awk -F ':' '{print $NF}' |tr -d ' ')

# 获取机器的系统信息 hostname kernel_name kernel_release machine processor hardware_platform operating_system

base_info[hostname]=$(uname -n)

base_info[kernel_name]=$(uname -s)

base_info[kernel_release]=$(uname -r)

base_info[machine]=$(uname -m)

base_info[processor]=$(uname -p)

base_info[hardware_platform]=$(uname -i)

base_info[operating_system]=$(uname -o)

# 获取系统版本信息,这个比较麻烦,需要做几次判断

base_info[os]=""

if [ -f /etc/centos-release ]; then

base_info[os]="centos-"$(cat /etc/centos-release |egrep -o '[0-9]+(\.[0-9]+)+')

elif [ -n "$(grep -i red /etc/redhat-release)" ]; then

base_info[os]="redhat-"$(cat /etc/redhat-release |egrep -o '[0-9]+(\.[0-9]+)+')

elif [ -n "$(grep -i ubuntu /etc/issue)" ]; then

base_info[os]="ubuntu-"$(cat /etc/issue |egrep -o '[0-9]+(\.[0-9]+)+')

fi

# 删除产生的临时文件

rm -f /tmp/base.hw.info

# 将关联数组转化为json字符串输出

local str_json="{}"

for key in ${!base_info[@]}

do

_temp_json="{\"$key\":\"${base_info[$key]}\"}"

str_json=$(jq -n "$str_json + $_temp_json")

done

echo $str_json

return 0

}

# 获取到所有基础的信息

# 这里我用数据作为输出格式,只是为了格式统一

function get_base_all()

{

local base_all_info="[]"

echo "[$(get_base)]"

return 0

}

#------------------

## 入口函数,也是主函数

#function main()

#{

# get_base_all

#}

#

#main "$@"

1.3.8 主函数

#!/bin/bash

#文件名: main.sh

#作者:liuxin

#更新时间: 20190903

#------------------

# 入口函数,也是主函数

function main()

{

# 获取当前脚本所在路径

local file_path=$(dirname $(readlink -f "$0"))

# 导入其它函数

source $file_path/scripts/get_base_info.sh

source $file_path/scripts/get_cpu_info.sh

source $file_path/scripts/get_disk_info.sh

source $file_path/scripts/get_memory_info.sh

source $file_path/scripts/get_netcard_info.sh

source $file_path/scripts/get_nvme_info.sh

source $file_path/scripts/get_raidcard_info.sh

# 获取所有数据

all=$(jq -n "{base:$(get_base_all)} + {cpu:$(get_cpu_all)} + {disk:$(get_disk_all)} + {memory:$(get_memory_all)} + {netcard:$(get_netcard_all)} + {nvme:$(get_nvme_all)} + {raidcard:$(get_raidcard_all)}")

#echo $all

# 上传抓取到的数据

curl -X POST http://192.168.1.30:5000/api_2_0/server_info -H 'Content-Type: application/json' -d "{\"data\":$all}"

return 0

}

main "$@"

1.4 主函数

主结构

.

├── main.sh

└── scripts

├── get_base_info.sh

├── get_cpu_info.sh

├── get_disk_info.sh

├── get_memory_info.sh

├── get_netcard_info.sh

├── get_nvme_info.sh

└── get_raidcard_info.sh

1 directory, 8 files

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值