使用Shell脚本查询服务器硬件信息
生活随笔
收集整理的這篇文章主要介紹了
使用Shell脚本查询服务器硬件信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、為了方便查詢服務器的硬件信息,將需要查詢的內容整理成Shell腳本,輸出自己想要的格式。其實就是通過dmidecode命令輸出,然后通過sed,awk,cut命令的處理。
2、此腳步適用于HP DL系列的服務器,Dell PowerEdge系列的會有少許內容不同,所以在抓取過程會有小小報錯。
3、腳本內容如下:
#!/bin/bash ############################################################################ #?Check?the?server?hardware?configuration # #?History:?2016/04/16?zhuwei?First?release #############################################################################?set?a?safe?path?before?doing?anything?else PATH=/sbin:/usr/sbin:/bin:/usr/bin;?export?PATH#?This?script?must?be?executed?as?root RUID=`/usr/bin/id|awk?-F\(?'{print?$1}'|awk?-F\='{print?$2}'` #?#OR#?RUID=`id?|?cut?-d\(?-f1?|?cut?-d\=?-f2`?#OR#ROOT_UID=0 if?[?${RUID}?!=?"0"?];thenecho"This?script?must?be?executed?as?root"exit?1 fi#?Display?an?error?and?exit errorExit()?{echo"$@"?>&2exit?1 }#?Display?the?normal?print display()?{echo?-e"\033[32m***********************************************************\ ******************************\033[0m"echo?-e"\033[32m*\033[0m"$@""echo?-e"\033[32m***********************************************************\ ******************************\033[0m" }#Check?the?server?model,?serial?number model()?{dmidecode|grep?"System?Information"?-A9|egrep"Manufacturer|Product|Serial"?\|?awk?'BEGIN?{FS="\n";RS=""}?{print?$1,$2,$3}'?|?sed?'s/^[\t]//g' } #Query?cpu?Information cpu(){dmidecode|?grep?-A55?"Processor?Information"?|?\egrep?"Version:|Core?Count:|Thread?Count:"|sed?-e?'s/^[\t]//'?\-e'/Version:/i\\'?|?sed?'1d'?|?awk?'BEGIN?{FS="\n";RS=""}{print?$1,$2,"\t"$3}'\|?sed's/Version://' } #Query?memory?Information memory(){dmidecode|?grep?-A12?"Memory?Device"|egrep?"Type:|Size|Speed:|Locator:P"?\|?sed?-n?-e?'/MB/,/MHz/p'?|?sed?'/Size:/i\\'?|?sed?-e?'1d'?-e?'s/^[\t]*//'?\|?awk'BEGIN?{FS="\n";RS=""}?{print?$2}?{print"\t"$3,"\t"$1,"\t"$4}'#sed'/Size:/i\\'?|?sed?-e?'1d'?-e?'s/^[\t]*//'?#replaced##awk'BEGIN?{FS="\n"}{if(NR%4==1){print?"\n"}{print?$1}}'?|?sed's/^[\t]*//' } #Query?Network?Information network(){i=0count=`dmidecode?|?grep?-A4?"BIOS?NIC"|sed?-e?'1d'?|?wc?-l`if?[?-f?"/tmp/speed.txt"?];thenrm?-rf?/tmp/speed.txtfiwhile?[[${i}?-lt?${count}?]]doethtool?eth"${i}"|grep?"Speed:"?>>?/tmp/speed.txtlet?"i++"donedmidecode|?grep?-A4?"BIOS?NIC"|sed?-e?'1d'?-e?'s/^[\t]//'|?cut?-d,?-f2?|?\awk??'FNR==0?{print?"\r\n"?FILENAME}{print?et?NR-1?"\t"?$0}'?\et="eth"?>?/tmp/network.txtpaste?/tmp/network.txt?/tmp/speed.txt }display?"System?Information:"?&&?model?||?errorExit?"This?command?is?error!" display?"CPU?Information:"?&&?cpu||?errorExit?"This?command?is?error!" display?"Memory?Information:"?&&?memory?||?errorExit?"This?command?is?error!" display?"Network?Information:"?&&?network?||?errorExit?"This?command?is?error!"4、這里只對服務器廠商,內存,CPU配置和網卡信息,對于硬盤信息可根據不同廠商的管理工具來獲取。HP使用hp-health-*rpm,G9版本以上使用hpssacli-*.rpm,安裝以后使用:hpacucli ctrl all show config 查看RAID信息。Dell安裝Dell OpenManage Server Administrator工具查看,通過命令:omreport storage pdisk controller=0來查看。
5、查看RAID信息還可以使用MegaCli開源工具進行查看,這里不作介紹。
轉載于:https://blog.51cto.com/wyzwl/1765386
總結
以上是生活随笔為你收集整理的使用Shell脚本查询服务器硬件信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql置疑原因_SQL Server
- 下一篇: Depth-first Search深度