[Shell]服务器信息收集与整理输出wiki和excel

作者:Ajian 发布时间:August 28, 2012 分类:Shell

   前言:服务器信息的收集是认识你的工作环境的第一步,有了服务器的相关信息就知道如何去分配他们,更好的取舍,发挥每一台服务器的优点。之前一直有不同服务器信息收集脚本,最近又做了一次修改,主要采用awk,减少了各种管道和中间的过程,并且尽量的可以智能匹配,还有update wiki(redmine)和excel和输出更好的进行整理和对比。

Notice:由于脚本修改比较快,很多awk里面的变量没有什么规范,有时间再去整理吧,如果你对awk使用很熟练的话不会有什么问题的,真心发现awk越用越简单,使用了很多awk的特性,也发现awk不同版本造成的问题,像脚本中有一段是awk 4 支持关联数组 而awk 3不支持。就当是一次awk的脚本练习。

PS:最早期的脚本可见 http://www.ohlinux.com/archives/331/ 

服务器信息收集脚本 

ServerInfo_wiki_v2.sh

#!/bin/bash 
LANG=C

echo -e "{{>toc}} \n"

if [ ! `which dmidecode` ] || [ ! -e /usr/sbin/dmidecode ] ;then
if [ `which yum`];then yum -y install dmidecode > /dev/null 2>&1
else
echo "Please install dmidecode at first!! Exit."
exit
fi fi #OS Information if [ -e /etc/slackware-version ];then OS=`cat /etc/slackware-version | head -1` elif [ -e /etc/debian_version ]; then OS="Debian `cat /etc/debian_version`" elif [ -e /etc/redhat-release ]; then OS=`cat /etc/redhat-release` elif [ -e /etc/SuSE-release ]; then OS=`cat /etc/SuSE-release |head -n1` elif [ -e /etc/gentoo-release ]; then OS=`< /etc/gentoo-release` elif [ -e /etc/issue ]; then OS=`cat /etc/issue | head -1` elif [ -z $OS ] ;then OS='unknown' fi echo " h2. Base Infomation |_. Hostname | `/bin/hostname`|" #network Information /sbin/ifconfig -a | awk 'BEGIN{ in_section=0 end=1 }{ if (!end){ if (in_section){ if ( $0 ~ /^$/ ){ end=1 }else{ if ($0 ~ /inet addr:/){ split($2,ip,":") net=net""nic":"ip[2]" " } } } }else if (!($0 ~ /^ / || /lo/)){ end=0 in_section=1 nic=$1 } }END{ print "|_. Network |",net,"|" }' echo "|_. System kernel | `/bin/uname -sr`| |_. OS Name | $OS | |_. OS bit| `/bin/uname -m`|" #CPU Information cat /proc/cpuinfo | awk -F\: \ -v systembit=`/bin/uname -m` \ '{ if ($0 ~ /processor/){ cpucore++ } if ($0 ~ /model name/){ cputype=$2 } if ($0 ~ /physical id/){ cpuphy[$2]++ } if ($0 ~ /flags/){ if ($2 ~ /lm/){ cpubit=64 }else if ($2 ~/pm/){ cpubit=32 }else if($2 ~ /rm/){ cpubit=16 }else if (systembit ~ /i386|i586|i686/){ cpubit=32 }else{ cpubit=systembit } } }END{ cpu_phy=length(cpuphy) if (cpu_phy == 0 ){ cpu_phy=1 } print "|_. CPU Bit|",cpubit,"|" print "|_. CPU Type|",cputype,"|" print "|_. CPU Num|",cpu_phy,"|" print "|_. CPU Core|",cpucore,"|" }' #for awk above 4 version ,about dmidecode Information #/usr/sbin/dmidecode | awk -F\: 'BEGIN{ # in_section=0 # end=1 # num=1 # area["System Information"]="Product Name:,Manufacturer:" # area["Memory Device"]="Type:,Size:" # }{ # if (!end){ # if (in_section){ # if ( $0 ~ /^$/ ){ # end=1 # }else{ # for (k in info){ # if ( $0 ~ info[k] ){ # key=areainfo" "info[k] # if ( key in result) { # if ($2 in result[key]){ # result[key][$2]++ # }else{ # result[key][$2]=1 # } # }else{ # result[key][$2]=1 # } # } # } # } # } # }else if ($0 in area ){ # end=0 # in_section=1 # areainfo=$0 # split(area[$0],info,",") # } #}END{ # for (row1 in result){ # row3="" # for (row2 in result[row1]){ # if (result[row1][row2] > 1){ # row3=row3" "row2"*"result[row1][row2] # }else{ # row3=row3" "row2 # } # } # print "|_. "row1,"|",row3,"|" # } #} #' #for awk 3 version ,about dmidecode Information /usr/sbin/dmidecode | awk -F\: 'BEGIN{ in_section=0 end=1 num=1 area["System Information"]="Product Name:,Manufacturer:" area["Memory Device"]="Type:,Size:" }{ if (!end){ if (in_section){ if ( $0 ~ /^$/ ){ end=1 }else{ for (k in info){ if ( $0 ~ info[k] ){ key=areainfo" "info[k]"|"$2 if ( key in result) { result[key]++ }else{ result[key]=1 } } } } } }else if ($0 in area ){ end=0 in_section=1 areainfo=$0 split(area[$0],info,",") } }END{ for (key in result){ split(key,row,"|") if (row[1] in arr){ if (result[key] > 1){ arr[row[1]]=arr[row[1]]" "row[2]"*"result[key] }else{ arr[row[1]]=arr[row[1]]" "row[2] } }else{ if (result[key] > 1){ arr[row[1]]=row[2]"*"result[key] }else{ arr[row[1]]=row[2] } } } for (k in arr){ print "|_.",k,"|",arr[k],"|" } } ' #Disk Information df -hPT -x tmpfs -x devtmpfs -x rootfs | awk '{ if($0 ~ /dev/){ disk=disk""$1"=>"$7"=>"$3" " } }END{ print "|_. Disk |",disk,"|" }' echo #Show Hard_Info echo -e "h2. Filesystems \n" df -hPT -x tmpfs -x devtmpfs -x rootfs | awk '{print "|" $1 " |" $2 " |" $3 " | " $6 " | " $7 " |"}' echo #IDE devices information echo -e "h2. IDE devices \n" if [ "$(ls -1d /proc/ide/hd* 2> /dev/null)" ]; then for DEV in `ls -1d /proc/ide/hd* |sed 's/.*\///'` do MODEL=`cat /proc/ide/$DEV/model` if [ -e /proc/ide/$DEV/capacity ]; then SIZE=`cat /proc/ide/$DEV/capacity` SIZE=`expr $SIZE / 2097152` else if [ -e /sys/block/$DEV/size ]; then SIZE=`cat /sys/block/$DEV/size` SIZE=`expr $SIZE / 2097152` else SIZE='(unknown)' fi fi echo "| /dev/$DEV | $MODEL | $SIZE GB |" done fi echo #SCSI devices infomation if [ "$(ls -1d /sys/block/sd* 2> /dev/null)" ]; then echo -e "h2. SCSI devices \n" echo "| devices |_. Model |_. Size |" for DEV in `ls -1d /sys/block/sd* |sed 's/.*\///'` do MODEL=`cat /sys/block/$DEV/device/model` SIZE=`cat /sys/block/$DEV/size` SIZE=`expr $SIZE / 2097152` echo "| /dev/$DEV | $MODEL | $SIZE GB |" done fi echo #open port and services #notice need root permission echo -e "h2. Open Port and Serivce \n" /bin/netstat -tln | awk '{ if($0 ~ /^tcp/){ split($4,port,":") num=length(port) cmd="lsof -i :"port[num] while(( cmd | getline foo ) > 0 ){ if(!(foo ~ /^COMMAND/ )){ split(foo,ccc," ") abc[port[num]]=ccc[1]" "abc[port[num]] } } close(cmd) } }END{ print "| Port | Service|" for (key in abc){ value="" split(abc[key],com," ") for ( a in com){ uniqcmd[com[a]]++ } for (b in uniqcmd){ value=b"*"uniqcmd[b]" "value } delete uniqcmd print "|",key,"|",value,"|" } print "" }' function grep_towiki(){ if [ -f $1 ];then echo -e "h3. $1 \n" echo "<pre>" /bin/egrep -v "^#|^$" $1 echo "</pre>" echo fi } echo -e "h2. Auto Start \n" grep_towiki /etc/rc.local echo -e "h3. chkconfig \n" echo "<pre>" /sbin/chkconfig --list |awk '{if ($0 ~ /3:on/){print $1}}' echo "</pre>" echo echo -e "h2. Crond Tab \n" for file in /var/spool/cron/* ;do grep_towiki $file done echo -e "h2. System Kernel File \n" grep_towiki /etc/sysctl.conf echo -e "h2. Rsync Config \n" grep_towiki /etc/rsyncd.conf

更新redmine wiki 

批量更新redmine的服务器信息 结合pssh

#!/bin/bash 
#shell import wiki
#need bash
author_id=3
wiki_id=6
protected=0
default_page_id=215
log_dir=/tmp/serversout
hostlist=$1

export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/bin:/usr/local/webserver/mysql/bin

pssh -t 600 -h $hostlist -l root -o $log_dir "bash /home/sh/ServerInfo_wiki_v2.sh"

SQL()
{
mysql -h192.168.1.253 -uUser -pPassword redmine <<EOF
$1
EOF
}


for ip in `cat $hostlist | grep -v "#"`
do
    title=`echo $ip | sed 's/\.//g'`
    text=`cat $log_dir/$ip | awk '{printf($0"\\n");}'`
    updated_on=`date +%Y"-"%m"-"%d" "%k":"%M:"%S"`

    wiki_page_select="select id from wiki_pages where title='$title';"

    echo $wiki_page_select

    page_id=`echo $(SQL "$wiki_page_select") | awk '{print $2}'`

    #echo $page_id

    if [ ! -z $page_id ] ;then

        wiki_content_id=$page_id

        wiki_contents_update_SQL="update wiki_contents set text='$text',updated_on='$updated_on' where page_id=$page_id "

        wiki_version_select="select version from wiki_content_versions where page_id=$page_id"

        version=`echo $(SQL "$wiki_version_select") | awk '{print $NF+1}'`

        wiki_versions_insert_SQL="insert into wiki_content_versions values('','$wiki_content_id','$page_id','$author_id','$text','','','$updated_on','$version')"

        #deBug
        #echo $wiki_contents_update_SQL
        #echo $wiki_versions_insert_SQL

        SQL "$wiki_contents_update_SQL"
        SQL "$wiki_versions_insert_SQL"

        echo "[INFO] $ip is updated "

    else

        version=1
        wiki_pageid_select="select page_id from wiki_contents where id=(select max(id) from wiki_contents); "
        default_page_id=`echo $(SQL "$wiki_pageid_select") | awk '{print $2}'`

        ((default_page_id=${default_page_id}+1))

        wiki_content_id=$default_page_id
        echo content_id=$wiki_content_id

        #insert wiki_page
        wiki_page_SQL="insert into wiki_pages values('','$wiki_id','$title','$updated_on','$protected',NULL)"

        #insert wiki_contents
        wiki_contents_SQL="insert into wiki_contents values('','$default_page_id','$author_id','$text','','$updated_on','$version')"

        #insert wiki_content_versions
        wiki_content_versions_SQL="insert into wiki_content_versions values('','$wiki_content_id','$default_page_id','$author_id','$text','','','$updated_on','$version')"


        #deBug
        #echo $wiki_page_SQL
        #echo $wiki_contents_SQL
        #echo $wiki_content_versions_SQL

        SQL "$wiki_page_SQL"
        SQL "$wiki_contents_SQL"
        SQL "$wiki_content_versions_SQL"

        echo "[INFO] $ip is inserted "
    fi
done

执行 bash servers_to_wiki_mysql.sh /tmp/cn.list

 该脚本可以新建也可以更新redmine wiki中的服务器信息, 但一定要注意参数变量的替换 不然会破坏原有的redmine哦

output wiki

 

输出excel格式

如果是服务器信息对比这样看wiki也是挺累的,可能你比较习惯用excel的格式进行排序尤其是大量服务器的时候。

我们再将输出的数据转换成excel来进行对比查看。

#!/bin/bash 
#shell import excel
#need bash
log_dir=/tmp/serversout
hostlist=$1
outexcel=$2

#export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/bin:/usr/local/webserver/mysql/bin

#pssh -t 600 -h $hostlist -l root -o $log_dir "bash /home/server/code/ServerInfo/ServerInfo_wiki_v2.sh"

#print title 
for ip in  `grep -v "#" $hostlist | head -1` 
do
    cat $log_dir/$ip | awk -F\| \
        -v out=$outexcel \
        '{
     if($0 ~ /^\|_./){
         split($2,ti,"_.")
        printf "%s|",ti[2] > out
    }
    
}'
done
echo  >> $outexcel
for ip in `grep -v "#" $hostlist`
do
    cat $log_dir/$ip | awk -F\|  \
    -v out=$outexcel \
    '{
    if($0 ~ /^\|_./){
        info[row1]=info[row1]$3"|"
    }
}END{
    for (key in info){
        print key""info[key] >> out
    }
}'
    done

执行 bash servers_to_excel.sh /tmp/cn.list /tmp/cn.xls

得到如下列表 这样就很方便的进行比较和取舍了。PS:打开cn.xls 使用 | 为分割符就可以了。

output excel