检测线上服务器的系统硬件信息,包括:ip地址(内网、所有)、远程控制卡IP、cpu信息(核数)、内存、硬盘、机器码、制造商、产品名称等 #!/bin/sh #检测线上服务器的系统硬件信息 #2012/01/05 ip=`ifconfig|awk '{print $1,$2}'|grep -e 'Link' -e 'inet addr:'|awk '{if($2 ~ /Link/){ORS="";print $1}else if($1 ~ /inet/){ORS=""",""";split($2,a,":");print ":" a[2]}}'` ipfirst=`echo $ip|awk -F, '{print $1}'|awk -F: '{print $NF}'` ipall=`ip a s | awk -F '[ \t/]+' '/^ *inet /&&$NF!~/lo/{printf "%s:%s,",$NF,$3}' | sed 's/,$//'` #CPU类型/主频/核数 cpu=`sed -r 's/[ \t]+/ /g' /proc/cpuinfo | awk -F": +" '/^model name/{a[$2]++} END{for(i in a) printf "%s (x%s)\n",i,a[i]}'` #内存每条大小*条数 memory=`dmidecode | grep -A6 '^Memory Device' | sed -r 's/^[ \t]+//' | awk '/^Size.*MB/{a[$2/1024]++} END{for(i in a) printf "%sx%s,",i,a[i]}' | sed 's/,$//'` #硬盘大小,单位GB disk=`fdisk -l 2>/dev/null | awk '/^Disk/{printf "%.0f+",$3}' | sed 's/+$//'` #服务器机器码 SN=`dmidecode | grep -A6 'System Information' | sed -rn 's/^\s*Serial Number:\s([0-9A-Za-z]+)\s*$/\1/p'` #生产厂商 Manufacturer=`dmidecode | grep -A6 'System Information' | sed -rn 's/^\s*Manufacturer:\s+(.+)\s*$/\1/p'` #产品名称 Product=`dmidecode | grep -A6 'System Information' | sed -rn 's/^\s*Product Name:\s+(.+)\s*$/\1/p'` #远程控制卡IP drac_ip=`ipmitool -I open lan print 1 | grep "IP Address " | awk -F: '{print $2}' | sed 's/ //'` echo "{$HOSTNAME}" "{$ipfirst}" "{$ipall}" "{$cpu}" "{$memory}" "{$disk}" "{$SN}" "{$Manufacturer}" "{$Product}" "{$drac_ip}" 注意运行脚本之前先执行 /etc/init.d/ipmi start 不然drac_ip会获取不到。 运行结果为: {web01} {192.168.103.168} {eth0:192.168.103.168} {Intel(R) Xeon(R) CPU X5560 @ 2.80GHz (x16)} {8x2} {3999+299} {BTNKY2X} {Dell Inc.} {PowerEdge R710} {192.168.1.168} 虚拟机(由于无远程控制卡,最后项为空) {lx_web_s1} {192.168.0.5} {eth0:192.168.0.5,tun0:10.8.0.1} {Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz (x1)} {0.5x1} {21} {} {VMware, Inc.} {VMware Virtual Platform} {} 实现方法很多,若有补充的地方,欢迎大家分享。 (责任编辑:IT) |