shell编程--获取服务器资产信息

项目背景:

  需要获取服务器CPU、主板、内存、网卡、硬盘、GPU等信息

  设计思路:通过脚本命令获取系统各个指标,之后通过ansible将脚本下发至所有管理服务器,通过运行脚本将内容导入至csv文件,根据思路一步一步操作。

操作步骤:

1、首先将需要的脚本和查看硬盘的命令,放到了ansible的服务器/tmp目录下,并编写了如下playbook用于将命令和脚本放置所有需要获取指标的目标服务器。

---
- hosts: tw_infor
  remote_user: root
  tasks:
    - name: copy script file
      copy:
        src: /tmp/get_host_info.sh
        dest: /tmp/get_host_info.sh
        mode: 0755
      register: copy_status
    - debug: var=copy_status.changed
    - name: copy storcli64 file
      copy:
        src: /tmp/storcli64
        dest: /tmp/storcli64
        mode: 0755
      register: copy_status
    - debug: var=copy_status.changed
View Code

 2、查看一下获取服务器基本信息的脚本内容

#!/bin/bash
#
#
#
#获取主机IP地址
ips=$(ip addr|grep "/24"|awk -F"/" {'print $1'}|awk {'print $2'})
#获取CPU型号
cpumodel=$(sudo cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq)
#获取CPU个数
cpunumber=$(sudo cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l)
#CPU信息
#echo "($cpumodel)*$cpunumber"
#获取服务器主板信息
board_info=$(sudo dmidecode -t 1| grep "Manufacturer"|awk -F":" {'print $2'})
#获取内存槽位大小
memory_size=$(sudo dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range|grep GB|uniq|awk '{gsub(/^\s+|\s+$/, "");print}')
#获取内存槽位个数
memory_num=$(sudo dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range|grep GB|wc -l)
#内存信息
#echo "(${memory_size})*${memory_num}"
#磁盘厂商
disk_product=$(cd /tmp && sudo ./storcli64 /call show|grep "Product Name"|awk -F"=" {'print $2'})
#raid盘信息
disk_type=$(cd /tmp && sudo ./storcli64 /call show|grep "RAID"|grep -i "ON"|grep -v "RAID-Mode"|awk {'print $2'})
#raid盘型号
disk_info=$(cd /tmp && sudo ./storcli64 /call show|grep INTEL|awk {'print $13'}|sort|uniq -c)
disk_type1=`echo $disk_type|tr -s '\n'`
disk_info1=`echo $disk_info|tr -s '\n'`

#echo ${disk_type} ${disk_info}
#获取网卡个数
net_number=$(lspci |egrep -i "net|eth"|grep  "+"|cut -d: -f1|sort |uniq -c|wc -l)
#net_number1=$(lspci |egrep -i "net|eth"|grep  "+"|cut -d: -f1|sort|uniq -c|head -1|awk {'print $1'})
#net_num=`expr $net_number \* $net_number1`
#网卡信息
net_info=$(lspci |egrep -i "net|eth"|grep  "+"|awk -F":" {'print $3'}|sort|uniq)
#GPU信息
nvidia-smi -L > /dev/null 2>&1
result=`echo $?`
if [ $result -eq 0 ];then
        gpu_info=`nvidia-smi -L|awk -F: {'print $2'}|awk -F"(" {'print $1'}|sort|uniq -c`
else
        gpu_info=NULL
fi

#printf "IP\tCPU\tboard\tMemory\tRAID_Product\tRAID_Info\tnetwork\tGPU\n"
printf "$ips\t($cpumodel)*$cpunumber\t$board_info\t$memory_size*$memory_num\t$disk_product\t$disk_type1 $disk_info1\t$net_number $net_info\t$gpu_info\n"
get_host_info.sh

3、配置需要查询的服务器清单列表

4、执行playbook将命令和脚本分发至所有被管理机

 1 ansible-playbook -i /root/idc-tw/hosts/asset_infor asset_checkscript.yml  

5、批量执行脚本内容获取服务器资产信息,并保持至csv文件中

 1 ansible -i /root/idc-tw/hosts/asset_infor tw_infor -m shell -a "sh /tmp/get_host_info.sh"|grep -v ">>" > /tmp/tw.csv 

6、将csv文件格式进行改动,否则无法展示

 1 sed -e 's/\t/,/g' /tmp/tw.csv > /tmp/tw_host.csv 

7、针对于mac电脑可能会有字符集问题,可以使用如下命令进行解决

 1 iconv -f UTF8 -t GB18030 tw_host.csv > tw_hostv1.csv 

posted @ 2022-09-20 19:37  摩天居士-谢烟客  阅读(153)  评论(0)    收藏  举报