<small id='f0gk'></small> <noframes id='cCByf4'>

  • <tfoot id='GoV5m6'></tfoot>

      <legend id='F1wPf'><style id='C5sVZu'><dir id='Vzcq0OL'><q id='uN8OUlT'></q></dir></style></legend>
      <i id='oKU74G'><tr id='35ulLAGr'><dt id='7lvBib'><q id='U70i'><span id='HnGsdkB'><b id='ID7eQ'><form id='iLd1'><ins id='k2KhBc'></ins><ul id='ix67aUmnG'></ul><sub id='4hU2f'></sub></form><legend id='DYXlAW6h'></legend><bdo id='YJkc8P'><pre id='RwHFWoLUP'><center id='LtzyMo'></center></pre></bdo></b><th id='B57e'></th></span></q></dt></tr></i><div id='zjdtmhA'><tfoot id='ZTyps68bQ'></tfoot><dl id='waZf'><fieldset id='z6EVP2HZ'></fieldset></dl></div>

          <bdo id='DW3sXt7V'></bdo><ul id='4sqG'></ul>

          1. <li id='PmH5J'></li>
            登陆

            章鱼竞猜下载-shell脚本整合json数值输出然后获取硬件信息

            admin 2019-08-13 260人围观 ,发现0个评论

            检查硬件信息,并将信息整组成json数值,然后传给前段进行剖析,最终再进行相应的处理。在装体系的时分,或是进行监控时,都是一个规范的自动化运维流程。运用shell直接生成好json数据再进行传输,会变得十分便利。所以抽时间写了这个小脚本,供我们参阅,里边还有许多不完善的当地,有爱好的朋友能够自行更改

            环境

            需求装置必要的软件,这儿我为了保证脚本合理的进行,运用的是真机环境,没有运用虚拟机

            [root@sys-idc-pxe01 ~]# yum install jq lsscsi MegaCli

            Loaded plugins: fastestmirror, security

            Loading mirror speeds from cached hostfile

            Setting up Install Process

            Package jq-1.3-2.el6.x86_64 already installed and latest version

            Package lsscsi-0.23-3.el6.x86_64 already installed and latest version

            Package MegaCli-8.00.40-1.i386 already installed and latest version

            Nothing to do

            脚本内容

            我的shell风格比较倾向模块化,所以运用函数会多一些,这样便利了解,信任我们也能看得懂。

            我不太喜爱python风格的代码格局(尽管我python写的也很不错),看起来给人的感觉太紧凑,太乱。据说是长辈们出书书本的时分为了省纸,才呈现的那种风格,晚辈怀着对长辈的敬仰就连续了下来,其实长辈也没有那么写过那种风格的代码,不知道是不是真的。

            [root@sys-idc-pxe01 ~]# cat ./get_server_info.sh

            #!/bin/sh

            #deion: get server hardware info

            #author: liuxin

            #date: 20180122

            #需求装置jq东西 yum install jq

            #用于寄存该服务器的一切信息,个人喜爱把全局变量写到外面

            #写到函数里边,没有加local的变量也是全局变量

            INFO="{}"

            #界说一个东西函数,用于生成json数值,后边将会频频用到

            functioncreate_json

            {

            #utility章鱼竞猜下载-shell脚本整合json数值输出然后获取硬件信息 function

            localkey=$1

            localvalue="$2"

            localjson=""

            #if value is string

            if[ -z "$(echo "$value" |egrep "[|]|{|}")"]

            then

            json=$(jq -n {"$key":""$value""})

            #if value is json, object

            elif[ "$(echo "$value" |jq -r type)"== "object"]

            then

            json=$(jq -n {"$key":"$value"})

            #if value is array

            elif[ "$(echo "$value" |jq -r type)"== "array"]

            then

            json=$(jq -n "{$key:$value}")

            else

            echo"value type error..."

            exit1

            return0

            fi

            echo$json

            return0

            }

            #获取CPU信息

            functionget_cpu

            {

            #获取cpu信息,去掉空格和制表符和空行,以便于for循环

            localcpu_model_1=$(dmidecode -s processor-version |grep '@'|tr -d " "|tr -s "n"|tr -d "t")

            localcpu_info="{}"

            locali=0

            #由于去掉了空格和制表符,以下默许运用换行符分隔

            forline in$(echo"$cpu_model_1")

            do

            localcpu_model="$line"

            localcpu1=$(create_json "cpu_model""$cpu_model")

            #获取每块cpu的信息,这儿只记录了类型

            localcpu=$(create_json "cpu_$i""$cpu1")

            localcpu_info=$(jq -n "$cpu_info+ $cpu")

            i=$[ $i+ 1]

            done

            #将cpu的信息整组成一个json,key是cpu

            localinfo=$(create_json "cpu""$cpu_info")

            #将信息加入到全局变量中

            INFO=$(jq -n "$INFO+ $info")

            return0

            }

            functionget_mem

            {

            #generate json {Locat章鱼竞猜下载-shell脚本整合json数值输出然后获取硬件信息or:{sn:sn,size:size}}

            localmem_info="{}"

            #获取每个内存的信息,包含Size:|Locator:|Serial Number:

            localmem_info_1=$(dmidecode -t memory |egrep 'Size:|Locator:|Serial Number:'|grep -v 'Bank Locator:'|awk '

            {

            if (NR%3==1 && $NF=="MB")

            {

            size=$2;

            getline (NR+1);

            locator=$2;

            getline (NR+2);

            sn=$NF;

            printf("%s,%s,%sn",locator,size,sn)

            }

            }')

            #依据上面的信息,将信息过滤并整组成json

            locali=0

            forline in$(echo"$mem_info_1")

            do

            locallocator=$(echo$line|awk -F , '{print $1}')

            localsn=$(echo$line|awk -F , '{print $3}')

            localsize=$(echo$line|awk -F , '{print $2}')

            localmem1=$(create_json "locator""$locator")

            localmem2=$(create_json "sn""$sn")

            localmem3=$(create_json "size""$size")

            localmem4=$(jq -n "$mem1+ $mem2+ $mem3")

            #每条内存的信息,key是内存从0开端的序号

            localmem=$(create_json "mem_$i""$mem4")

            #将这些内存的信息组合到一个json中

            mem_info=$(jq -n "$mem_info+ $mem")

            i=$[ $i+ 1 ]

            done

            #给这些内存的信息设置key,mem

            localinfo=$(create_json "mem""$mem_info")

            INFO=$(jq -n "$INFO+ $info")

            return0

            }

            functionget_megacli_disk

            {

            #设置megacli东西的途径章鱼竞猜下载-shell脚本整合json数值输出然后获取硬件信息,此条能够依据状况更改

            localraid_tool="/opt/MegaRAID/MegaCli/MegaCli64"

            #将硬盘信息获取,保存下来,省去每次都履行的操作

            $raid_toolpdlist aall >/tmp/megacli_pdlist.txt

            localdisk_info="{}"

            #获取硬盘的必要信息

            localdisk_info_1=$(cat /tmp/megacli_pdlist.txt |egrep 'Enclosure Device ID:|Slot Number:|PD Type:|Raw Size:|Inquiry Data:|Me章鱼竞猜下载-shell脚本整合json数值输出然后获取硬件信息dia Type:'|awk '

            {

            if(NR%6==1 && $1$2=="EnclosureDevice")

            {

            E=$NF;

            getline (NR+1);

            S=$NF;

            getline (NR+2);

            pdtype=$NF;

            getline (NR+3);

            size=$3$4;

            getline (NR+4);

            sn=$3$4$5$6;

            getline (NR+5);

            mediatype=$3$4$5$6;

            printf("%s,%s,%s,%s,%s,%sn",E,S,pdtype,size,sn,mediatype)

            }

            }')

            #将获取到的硬盘信息进行整合,生成json

            locali=0

            forline in$(echo$disk_info_1)

            do

            #local key=$(echo $line |awk -F , '{printf("ES%s_%sn",$1,$2)}')

            localE=$(echo$line|awk -F , '{print $1}')

            localS=$(echo$line|awk -F , '{print $2}')

            localpdtype=$(echo$line|awk -F , '{print $3}')

            localsize=$(echo$line|awk -F , '{print $4}')

            localsn=$(echo$line|awk -F , '{print $5}')

            localmediatype=$(echo$line|awk -F , '{print $6}')

            localdisk1=$(create_json "pdtype""$pdtype")

            localdisk1_1=$(create_json "enclosure_id""$E")

            localdisk1_2=$(create_json "slot_id""$S")

            localdisk2=$(create_json "size""$size")

            localdisk3=$(create_json "sn""$sn")

            localdisk4=$(create_json "mediatype""$mediatype")

            localdisk5=$(jq -n "$disk1+ $disk1_1+ $disk1_2+ $disk2+ $disk3+ $disk4")

            localdisk=$(create_json "disk_$i""$disk5")

            disk_info=$(jq -n "$disk_info+ $disk")

            i=$[ $i+ 1 ]

            done

            #echo $disk_info

            localinfo=$(create_json "disk""$disk_info")

            INFO=$(jq -n "$INFO+ $info")

            return0

            }

            functionget_hba_disk

            {

            #关于hba卡的硬盘,运用smartctl获取硬盘信息

            localdisk_tool="smartctl"

            localdisk_info="{}"

            #lsscsi 需求运用yum install lsscsi 装置

            localdisk_info_1=$(lsscsi -g |grep -v 'enclosu'|awk '{printf("%s,%s,%s,%sn",$1,$2,$(NF-1),$NF)}')

            locali=0

            forline in$(echo$disk_info_1)

            do

            localE=$(echo$line|awk -F , '{章鱼竞猜下载-shell脚本整合json数值输出然后获取硬件信息print $1}'|awk -F ':''{print $1}'|tr -d '[|]')

            localS=$(echo$line|awk -F , '{print $NF}'|egrep -o [0-9]*)

            localsd=$(echo$line|awk -F , '{print $(NF-1)}')

            $disk_tool-i $sd>/tmp/disk_info.txt

            localpdtype="SATA"

            if[ "$(cat /tmp/disk_info.txt |grep 'Transport protocol:' |awk '{print $NF}')"== "SAS"]

            then

            localpdtype="SAS"

            fi

            localsize=$(cat /tmp/disk_info.txt |grep 'User Capacity:'|awk '{printf("%s%sn",$(NF-1),$NF)}'|tr -d '[|]')

            localsn=$(cat /tmp/disk_info.txt |grep 'Serial Number:'|awk '{print $NF}')

            localmediatype="disk"

            localdisk1=$(create_json "pdtype""$pdtype")

            localdisk1_1=$(create_json "enclosure_id""$E")

            localdisk1_2=$(create_json "slot_id""$S")

            localdisk2=$(create_json "size""$size")

            localdisk3=$(create_json "sn""$sn")

            localdisk4=$(create_json "mediatype""$mediatype")

            localdisk5=$(jq -n "$disk1+ $disk1_1+ $disk1_2+ $disk2+ $disk3+ $disk4")

            localdisk=$(create_json "disk_$i""$disk5")

            disk_info=$(jq -n "$disk_info+ $disk")

            i=$[ $i+ 1 ]

            done

            #echo $disk_info

            localinfo=$(create_json "disk""$disk_info")

            INFO=$(jq -n "$INFO+ $info")

            return0

            }

            functionget_disk

            {

            #依据获取到的硬盘控制器类型,来判别运用什么东西收集硬盘信息

            if[ "$(echo "$INFO" |jq -r .disk_ctrl.disk_ctrl_0.type)"== "raid"]

            then

            get_megacli_disk

            elif[ "$(echo "$INFO" |jq -r .disk_ctrl.disk_ctrl_0.type)"== "hba"]

            then

            get_hba_disk

            else

            localinfo=$(create_json "disk""error")

            INFO=$(jq -n "$INFO+ $info")

            fi

            #hp机器比较特别,这儿我没有做hp机器硬盘信息收集,有爱好的朋友能够自行增加上

            #if hp machine

            return0

            }

            functionget_diskController

            {

            localdisk_ctrl="{}"

            #if LSI Controller

            localdisk_ctrl_1="$(lspci -nn |grep LSI)"

            locali=0

            #以换行符分隔

            IFS_OLD=$IFS&& IFS=$'n'

            forline in$(echo"$disk_ctrl_1")

            do

            #echo $line

            localctrl_id=$(echo"$line"|awk -F ']:''{print $1}'|awk '{print $NF}'|tr -d '[|]')

            case"$ctrl_id"in

            #依据控制器的id或进行判别是raid卡仍是hba卡,由于品牌比较多,后续能够在此处进行扩展增加

            0104)

            # 获取Logic今后的字符串,并进行拼接

            localctrl_name=$(echo"${line##*"Logic"}"|awk '{printf("%s_%s_%sn",$1,$2,$3)}')

            localctrl1=$(create_json "id""$ctrl_id")

            localctrl2=$(create_json "type""raid")

            localctrl3=$(create_json "name""$ctrl_name")

            ;;

            0100|0107)

            localctrl_name=$(echo"${line##*"Logic"}"|awk '{printf("%s_%s_%sn",$1,$3,$4)}')

            localctrl1=$(create_json "id""$ctrl_id")

            localctrl2=$(create_json "type""hba")

            localctrl3=$(create_json "name""$ctrl_name")

            ;;

            *)

            localctrl1=$(create_json "id""----")

            localctrl2=$(create_json "type""----")

            localctrl3=$(create_json "name""----")

            ;;

            esac

            localctrl_tmp=$(jq -n "$ctrl1+ $ctrl2+ $ctrl3")

            localctrl=$(create_json "disk_ctrl_$i""$ctrl_tmp")

            disk_ctrl=$(jq -n "$disk_ctrl+ $ctrl")

            i=$[ $i+ 1 ]

            done

            IFS=$IFS_OLD

            localinfo=$(create_json "disk_ctrl""$disk_ctrl")

            INFO=$(jq -n "$INFO+ $info")

            return0

            }

            functionget_netcard

            {

            localnetcard_info="{}"

            localnetcard_info_1="$(lspci -nn |grep Ether)"

            locali=0

            #echo "$netcard_info_1"

            IFS_OLD=$IFS&& IFS=$'n'

            forline in$(echo"$netcard_info_1")

            do

            localnet_id=$(echo$line|egrep -o '[0-9a-z]{4}:[0-9a-z]{4}')

            localnet_id_1=$(echo$net_id|awk -F : '{print $1}')

            case"$net_id_1"in

            8086)

            localnet_name=$(echo"${line##*": "}"|awk '{printf("%s_%s_%s_%sn",$1,$3,$4,$5)}')

            localtype=$(echo$line|egrep -o SFP || echo"TP")

            localnet1=$(create_json "id""$net_id")

            localnet2=$(create_json "name""$net_name")

            localnet3=$(create_json "type""$type")

            ;;

            14e4)

            localnet_name=$(echo"${line##*": "}"|awk '{printf("%s_%s_%s_%sn",$1,$3,$4,$5)}')

            localtype=$(echo$line|egrep -o SFP || echo"TP")

            localnet1=$(create_json "id""$net_id")

            localnet2=$(create_json "name""$net_name")

            localnet3=$(create_json "type""$type")

            ;;

            *)

            localnet_name=$(echo"${line##*": "}"|awk '{printf("%s_%s_%s_%sn",$1,$3,$4,$5)}')

            localtype=$(echo$line|egrep -o SFP || echo"TP")

            localnet1=$(create_json "id""$net_id")

            localnet2=$(create_json "name""$net_name")

            localnet3=$(create_json "type""$type")

            ;;

            esac

            localnet1=$(jq -n "$net1+ $net2+ $net3")

            #echo $net

            localnet2=$(create_json "net_$i""$net1")

            netcard_info=$(jq -n "$netcard_info+ $net2")

            i=$[ $i+ 1 ]

            done

            IFS=$IFS_OLD

            localinfo=$(create_json "net""$netcard_info")

            INFO=$(jq -n "$INFO+ $info")

            return0

            }

            functionget_server

            {

            localproduct=$(dmidecode -s system-product-name |grep -v '^#'|tr -d ' '|head -n1)

            localmanufacturer=$(dmidecode -s system-manufacturer |grep -v '^#'|tr -d ' '|head -n1)

            localserver1=$(create_json "manufacturer""$manufacturer")

            localserver2=$(create_json "product""$product")

            localserver3=$(jq -n "$server1+ $server2")

            localinfo=$(create_json "basic_info""$server3")

            INFO=$(jq -n "$INFO+ $info")

            return0

            }

            ALL_INFO=""

            functionget_all

            {

            #由于硬盘信息的获取依靠硬盘控制器的信息,所以get_diskController要放到get_disk前面

            get_server

            get_cpu

            get_mem

            get_diskController

            get_disk

            get_netcard

            localsn=$(dmidecode -s system-serial-number |grep -v '^#'|tr -d ' '|head -n1)

            ALL_INFO=$(create_json "$sn""$INFO")

            return0

            }

            functionmain

            {

            get_all

            echo$ALL_INFO

            return0

            }

            #---------------章鱼竞猜下载-shell脚本整合json数值输出然后获取硬件信息----------------------------------

            main

            赵咏瑶
            请关注微信公众号
            微信二维码
            不容错过
            Powered By Z-BlogPHP