使用shell批量監控磁碟壞塊(二)

jeanron100發表於2016-01-16
之前分享了第一篇 使用shell批量監控磁碟壞塊(一),今天來簡單說說這個需求的實現內容 。
首先主要思路是通過中控的機器來傳送監控的細節,然後返回結果。

首先就是我們需要有一個伺服器列表,裡面會有這些需要的後設資料資訊。
比如列表如下:
192.127.133.13   某某服務中心主庫           jeanron   Primary Linux   Oracle  N    xxx
192.127.133.12   某某服務備庫           jeanron  Standby Linux   Oracle  N   xxxx
192.127.133.18   某某業務主庫   jeanron  Primary Linux   Oracle  N       xxxx
192.127.133.52   某某業務備庫  jeanron  Primary Linux   Oracle  N       xxxx
通過這個列表就可以開始分析了。指令碼實現內容如下:
. ~/.bash_profile > /dev/null
BASEDIR="/home/hardcheck"
mkdir -p ${BASEDIR}/{tmplog,log} && cd ${BASEDIR}
DATE=`date +%Y%m%d%H%M`
LOG="hardcheck_${DATE}.log"
TMP_SCRIPT="hardcheck_${DATE}.sh"
 
exec 3>&1 4>&2 1>$LOG 2>&1
### 將伺服器列表作為引數傳遞進來,為了考慮字元相容性,統一採用utf8來處理
SERVERLIST=$1
if [ -z "${SERVERLIST}" ] || [ ! -s "${SERVERLIST}" ] ; then
cat /home/jeanron/alldbserver-linux.txt|iconv -f GBK -t UTF8|grep jeanron|grep -v Solaris|grep -v nopingdb|grep -v "#1"> db_list.all.lst
SERVERLIST=$BASEDIR/db_list.all.lst
fi

SSH="ssh -oConnectionAttempts=3 -oConnectTimeout=5 -oStrictHostKeyChecking=no"
echo "#start hardcheck"
### {{{ 以下為真正的hardcheck檢查,遠端呼叫
while read line
do
  tmp_host=`echo $line|egrep -iv '^$|^#|Solaris|AIX'`    --排除solaris,AIX的檢測
  IP=`echo $tmp_host|awk '{print $1}'`
  DB=`echo $tmp_host|awk '{print $2}'`
  OWNER=`echo $tmp_host|awk '{print $3}'`
  STD_TYPE=`echo $tmp_host|awk '{print $4}'`
  DB_TYPE=`echo $tmp_host|awk '{print $6}'`
  ILO_INFO=`echo $tmp_host|awk '{print $8}'`
  echo "echo \"$IP;$DB;$OWNER;$STD_TYPE;$DB_TYPE;$ILO_INFO\"" >> $TMP_SCRIPT
  echo "${SSH} $IP \"/opt/MegaRAID/MegaCli/MegaCli64 -CfgDsply -a0|grep Error;/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aALL|grep Firmware|grep -v Level|grep -v Up|grep -v  Online \" " >>$TMP_SCRIPT
done < ${SERVERLIST}
### }}}

### {{{ 郵件報警
sh $TMP_SCRIPT |tee $LOG |grep -v "Error Count: 0"| grep -v \;\;\;\;\;> mail.txt

MAILTO="jeanron100@hotmail.com"
/bin/bash $BASEDIR/beautymail.sh   -t "DBIP;DB用途;負責人;主備庫;資料庫型別;ILO資訊" -m ${MAILTO} -s "Disk Error Count Health Daily Check(${DATE})" ${BASEDIR}/mail.txt

裡面有幾個地方可以改進一下,對於命令的報告形式,發現還是有一個指令碼非常不錯,beautymail.sh
內容如下,對於格式美化還是非常給力的,我們只需要輸入一個基本格式的資料,就會輸出一個格式化後的結果內容,非常給力。
輸出的結果類似下面的效果。

指令碼內容如下,當然也是參考了很多的出處,我只是引用一下。
source /etc/profile > /dev/null
. ~/.bash_profile > /dev/null

title=''
topline=""
html='/tmp/info.html'
mail='jeanron100@hotmail.com'
subject='【報警輸出優化】Test'

while getopts "a:T:t:H:m:s:h" Option
do
        case $Option in
                a) addfile="$OPTARG";;
                T) title="$OPTARG";;
                t) topline="$OPTARG";;
                H) html="$OPTARG";;
                m) mail="$OPTARG";;
                s) subject="$OPTARG";;
                h)
                        echo -e "beautymail V2.0 copyright To make your mail more human-readable\n\
Usage: beautymail [-a addfile] [-T title-in-mail] [-t topline] [-H html-location] [-m mail-addr] [-s subject] content-file\n\
Options:
-a:     設定郵件里加附件,addfile需要附件的絕對路徑
-T:     設定郵件裡表格上方的說明文字
-t:     設定表格中首行,即各列的含義,如-t \" 日誌型別;日誌量;檔案數 \", 注意是用英文分號隔開的多列
-m:     指定收件人
-s:     郵件主題
"
                exit
        esac
done
shift $(($OPTIND - 1))

function init(){
        chars=$(echo $topline|wc -c)
        width=$(echo $chars*50|bc)
cat >$html<<EOF
<font color = green ><b>資料情況: ${title}</b></font>
</form>
<table width=$width border="1">
</table>
EOF
        topline=$(echo $topline |sed -e 's/^/<b>/' -e 's/;/<\/b>;<b>/g' -e 's/$/<\/b>/')
        write2table "$topline" yellow
}

function write2table() {
        sed -i '$i\<tr>\n<\/tr>' $html
#       line=$(echo "$1"|awk -F';' '{i=1; while (i<=NF) {print $i;i++}}')
#       for colume in $line;do
    echo "$1"|awk -F';' '{i=1; while (i<=NF) {print $i;i++}}'|while read colume
        do
                wcl=$(cat $html|wc -l)
                insert=$(echo ${wcl}-1|bc)
#                echo $insert $colume
#                echo
            if [ -n $2 ] ; then
              sed -i "$insert i\ <td bgColor=$2>$colume<\/td>" $html
            else
              sed -i "$insert i\ <td>$colume<\/td>" $html
            fi
        done
}


init
[ -z $1 ] && echo 'At least need a log file to mail' && exit 2
cp -p $1 $1.tmp
dos2unix $1.tmp # && sed -i -e 's/ /;/g' -e 's/\t/;/g' $1
#for i in $(cat $1);do
### 按行處理
cat $1.tmp |while read line
do
        write2table "$line"
done
rm -f ${1}.tmp

#傳送帶附件的HTML格式正文的函式 (對於非txt或者cvs格式的檔案,例如excel的xls,需要單獨修改Type:搜MIMEtype)
#$1: mail_from
#$2: mail_to
#$3: subject
#$4: content mimetype, such as "text/plain"
#$5: content
#$6: attach mimetype, such as "text/csv"
#$7: attach display name
#$8: attach file path
function SendMailMultiMediaAttach(){
    local MSG_FILE="/tmp/mail.tmp"
 
    sub=$(echo $3|iconv -f GB2312 -t UTF-8)
    echo "From: $1" > $MSG_FILE
    echo "To: $2" >> $MSG_FILE
    echo "Subject: $sub" >> $MSG_FILE
    echo "Mime-Version: 1.0" >> $MSG_FILE
    echo 'Content-Type: multipart/mixed; boundary="GvXjxJ+pjyke8COw"' >> $MSG_FILE
    echo "Content-Disposition: inline" >> $MSG_FILE
    echo "" >> $MSG_FILE
    echo "--GvXjxJ+pjyke8COw" >> $MSG_FILE
    echo "Content-Type: $4" >> $MSG_FILE
    echo "Content-Disposition: inline" >> $MSG_FILE
    echo "" >> $MSG_FILE
    echo "$5" >> $MSG_FILE
    echo "" >> $MSG_FILE
    echo "" >> $MSG_FILE
    if [ ! -z $8 ];then
        echo "--GvXjxJ+pjyke8COw" >> $MSG_FILE
        echo "Content-Type: $6" >> $MSG_FILE
        echo "Content-Transfer-Encoding: base64" >> $MSG_FILE
        echo "Content-Disposition: attachement; filename=$7" >> $MSG_FILE
        echo "" >> $MSG_FILE
        echo "" >> $MSG_FILE
        /usr/bin/base64 $8 >> $MSG_FILE
    fi
 
    cat $MSG_FILE | /usr/lib/sendmail -t
}
 
##! @TODO: 傳送郵件
##! @AUTHOR: http://neoremind.net/2011/02/linux_sendmail_attachment_mutt/
##! @VERSION: 1.0
##! @IN:
##! @OUT:
function sendMail()
{
        echo "Sending $subject mail from $from to $to"
 
        from="root@$(hostname)"
        to="$1"
        #subject="$2"
        subject="=?UTF-8?B?`echo -n $2|base64`?="
        content_type="text/html;charset=utf-8"
        MAIL_HTML="$3"
        body="$(cat $MAIL_HTML)"
        attach_type="text/csv"
        attach_path="$4"
        attach_name=$(echo "$attach_path"|awk -F'/' '{print $NF".txt"}')
        SendMailMultiMediaAttach "$from" "$to" "$subject" "$content_type" "$body" "$attach_type" "$attach_name" "$attach_path"
        echo "Send mail done."
}

echo $addfile
if [ -z "$addfile" ];then
        sendMail "$mail" "$subject" "$html"
else
        sendMail "$mail" "$subject" "$html" "$addfile"
fi
#mutt -e 'my_hdr Content-Type: text/html' "$mail" -s "$subject" < "$html";

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23718752/viewspace-1977726/,如需轉載,請註明出處,否則將追究法律責任。

相關文章