Linux shell脚本当用之服务器硬件检测1

Linux shell脚本应用之服务器硬件检测1

阿铭Linux:http://www.apelearn.com/bbs/forum.php

expr参考:http://blog.chinaunix.net/uid-209416-id-2410742.html

stty参考:http://blog.****.net/ysdaniel/article/details/6965339

$?#&01参考:http://www.2cto.com/os/201305/210831.html

exit参考:http://blog.163.com/bobile45@126/blog/static/96061992201311712658570/

trap 参考:http://blog.****.net/cheungjustin/article/details/5678679

break & continue参考:http://www.2cto.com/os/201305/209361.html

for & while参考:http://www.cnblogs.com/kangyoung/p/3556186.html

if参考:http://www.2cto.com/os/201503/379433.html

echo参考:http://www.linuxidc.com/Linux/2014-10/107550.htm

read参考:http://man.linuxde.net/read

awk参考:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html

去除空格参考:http://bbs.chinaunix.net/thread-2195137-1-1.html

tee参考:http://blog.****.net/yucan1001/article/details/8453527

tr参考:http://blog.sina.com.cn/s/blog_58c3f7960100uttl.html & http://man.linuxde.net/tr

sed参考:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html

 

  1 #!/bin/bash
  2 # Author:Yingtao.Gu
  3 # Copyright (c) 2016.1
  4 # Server Hardware detect and information collect.
  5 # It is suitable for Centos 7 Linux system.
  6 
  7 ##########Main  Title################
  8 echo -e "Which item you want to choose[1-6] ?\n\
  9 You can type exit to exit this interactive window.\n\
 10 1.CPU\n2.RAM\n3.Disk\n4.HCA\n5.NIC\n6.MotherBoard\n\
 11 The results will be saved in the opt directory."
 12 read Choice
 13 
 14 ##########Public variable############
 15 Begin=####################################################Begin####################################################
 16 End=#####################################################End#####################################################
 17 echo1="----->"
 18 SPLIT=-----------------------------------------------------------------------------------------
 19 save="/opt/ServerInformationCollection"
 20 
 21 #########1.CPU######
 22 if [ $Choice == 1 ];then
 23   PHNUM=`/usr/bin/lscpu  | egrep  Socket | gawk -F: ' { print$2 }'`
 24   CONUM=`/usr/bin/lscpu | egrep ^Core | awk '{ print$4 }'`
 25   echo "$Begin" 2>&1 | tee -a $save
 26   echo -e "Cpu Mode is $echo1`/usr/bin/lscpu | egrep "Model name" | gawk -F: '{ print $2 }'| tr -d " "`" 2>&1 | tee -a $save
 27   echo "Phsical Cpu is $echo1 `/usr/bin/lscpu  | egrep  Socket | gawk -F: ' { print$2 }'|tr -d " "`" 2>&1 | tee -a $save
 28   echo "Phsical Core is $echo1`expr $PHNUM \* $CONUM`" 2>&1 | tee -a $save                                                                                                    
 29   echo "Phsical Cpu is $echo1 `/usr/bin/lscpu  | egrep  Socket | gawk -F: ' { print$2 }'|tr -d " "`" 2>&1 | tee -a $save
 30   echo "Phsical Core is $echo1`expr $PHNUM \* $CONUM`" 2>&1 | tee -a $save
 31   echo "Cpu Total Thread is $echo1 `/usr/bin/lscpu  | egrep  "^CPU\(s\)\:" | gawk -F: ' { print$2 }'|tr -d " "`" 2>&1 | tee -a $save
 32   echo "$End" 2>&1 | tee -a $save
 33 #########2.Memory######
 34 elif [ $Choice == 2 ];then
 35   echo "$Begin" 2>&1 | tee -a $save
 36   echo "`/usr/sbin/dmidecode -t memory | egrep -A8 -i size  | egrep "(Locator:|Size:|Speed:|Manufacturer:)" | egrep -vi "No Module Installed" | egrep -A4 -i  Size 
 37  | egrep -vi "Bank Locator:"`" 2>&1 | tee -a $save
 38   echo "$End" 2>&1 | tee -a $save
 39 ########3.LSI########
 40 elif [ $Choice == 3 ];then
 41   echo -e "which item you want to choose[1-2] ?\n1.Megaraid Card\n2.SAS Hba Card"
 42   read choice2
 43   if [ $choice2 == 1 ] ; then
 44     RANUM=`/usr/sbin/lspci | egrep -i RAID | awk '{ print $12 }'|wc -l`
 45     if  [ $RANUM -gt 0 ];then
 46       echo "$Begin" 2>&1 | tee -a $save
 47       echo "`/opt/MegaRAID/MegaCli/MegaCli64 -AdpAllInfo  -aALL | egrep "Product Name|FW Version|Serial No"`" 2>&1 | tee -a $save
 48       echo "`/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aALL | egrep -i "Raw Size|Enclosure Device ID:|Slot Number:|Firmware state|Inquiry Data|foreign"|sort`" 2>&1| tee -a $save
 49       echo "`/opt/MegaRAID/MegaCli/MegaCli64   -LDGetNum -aAll`" 2>&1 | tee -a $save
 50       echo "`/opt/MegaRAID/MegaCli/MegaCli64  -CfgDsply -a0 | egrep "RAID Level"`" 2>&1 | tee -a $save
 51       echo "$End" 2>&1 | tee -a $save
 52     else
 53       echo "$Begin" 2>&1 | tee -a $save
 54       echo "Here are not any megaraid card can be detected" 2>&1 | tee -a $save
 55       echo "$End" 2>&1 | tee -a $save
 56                                                                                                                                                  
 57       echo "Here are not any megaraid card can be detected" 2>&1 | tee -a $save
 58       echo "$End" 2>&1 | tee -a $save
 59     fi
 60   elif [ $choice2 == 2 ];then
 61     SERNUM=`/usr/sbin/lspci | egrep -i Serial | wc -l`
 62     if [ $SERNUM -gt 0 ];then
 63       echo "$Begin" 2>&1 | tee -a $save
 64       for ((i=0;i<$SERNUM;i++));do
 65       echo -e "`/opt/smartmgr/scripts/sas2ircu $i display |  egrep "Controller type|Firmware version|Volume ID|Status of volume|Enclosure #|Slot #|State|Size|Model
 66  Number|Firmware Revision|Serial No" | sort`" 2>&1 | tee -a $save
 67       echo $SPLIT 2>&1 | tee -a $save
 68       done
 69       echo "$End" 2>&1 | tee -a $save
 70     else
 71       echo "$Begin" 2>&1 | tee -a $save
 72       echo "Here are not any sas hba card can be detected " 2>&1 | tee -a $save
 73       echo "$End" 2>&1 | tee -a $save
 74     fi
 75   else
 76   echo "$Begin" 2>&1 | tee -a $save
 77   echo "Here are not any lsi sas and raid card can be detected." 2>&1 | tee -a $save
 78   echo "$End" 2>&1 | tee -a $save
 79   fi
 80 ########4.IB########
 81 elif [ $Choice == 4 ];then
 82   MT=`/usr/sbin/lspci | egrep -i mt`
 83   if [ -n "$MT" ];then                                                                                                                                           
 84   MT=`/usr/sbin/lspci | egrep -i mt`
 85   if [ -n "$MT" ];then
 86     echo "$Begin" 2>&1 | tee -a $save
 87     echo "`/usr/sbin/lspci | egrep -i mellanox`" 2>&1 | tee -a $save
 88     ME=`/usr/sbin/lspci  | egrep -i mt | awk -F: '{ print$1 }'`
 89     for i1 in $ME;do
 90     echo "`/usr/sbin/lspci -vv -s 0000:$i1:00.0 |grep SN |awk -F ': ' '{print$2}'`" 2>&1 | tee -a $save
 91     done
 92     echo "$End" 2>&1 | tee -a $save
 93     else
 94     echo "$Begin" 2>&1 | tee -a $save
 95     echo "Here are not any Mellanox ib card can be detected" 2>&1 | tee -a $save
 96     echo "$End" 2>&1 | tee -a $save
 97   fi
 98 ######5.NIC#######
 99 elif [ $Choice == 5 ];then
100   NIC=`/usr/sbin/lspci | egrep -i 'Ethernet|Network' | egrep -v MT`
101   if [ -n "$NIC" ];then
102     echo "$Begin" 2>&1 | tee -a $save
103     echo "`/usr/sbin/lspci  | egrep -i 'network|ethernet' | egrep -v MT`" 2>&1 | tee -a $save
104     echo "$End" 2>&1 | tee -a $save
105   else
106     echo "$Begin" 2>&1 | tee -a $save
107     echo "Here are not any network card can be detected" 2>&1 | tee -a $save
108     echo "$End" 2>&1 | tee -a $save
109    fi
110 ######6.MotherBoard######
111 elif [ $Choice == 6 ];then                                                                                                                                            
112   if [ $Choice == 6 ];then
113   echo "$Begin" 2>&1 | tee -a $save                                                                                                                                                                                                                                                                           
114   echo -e "`/usr/sbin/dmidecode  -s  bios-vendor`\n`/usr/sbin/dmidecode  -s  bios-version`\n`/usr/sbin/dmidecode  -s  bios-release-date`\n`/usr/sbin/dmidecode  -
115     s  system-product-name`\n`/usr/sbin/dmidecode  -s   baseboard-serial-number`" 2>&1 | tee -a $save                                                                  
116      echo "$End" 2>&1 | tee -a $save                                                                                                                                
117   else                                                                                                                                                               
118      echo "Please do it carefully!!!" 2>&1 | tee -a $save                                                                                                           
119 fi