運維平臺的建設思考-後設資料管理(四)

jeanron100發表於2016-02-22
對於伺服器的一些資訊,如果資料量大了之後總是感覺力不從心,需要了解,但是感覺得到的這些資訊不夠清晰明瞭。
比如我們得到一臺伺服器,需要知道最基本的硬體配置,記憶體情況,磁碟空間情況,哪些磁碟空間問題需要關注,哪些磁碟空間問題可以忽略,swap的使用情況如何,伺服器的作業系統版本,核心版本,上面執行有幾個例項,是否啟用了ASM,甚至伺服器執行了多少天呢,這些資訊看起來非常瑣碎,也可以通過指令碼得到,但是一直以來感覺都是比較籠統模糊。
今天使用shell指令碼進行了簡單的改進。
我們來看看基本的效果情況。有了這些資訊,後設資料的管理總算是邁上了一個臺階。如果哪個磁碟空間又問題會單獨顯示出來一些資訊。

要得到這樣的結果,首先我們需要定義一個伺服器列表,這個列表中會有一些伺服器的基本資訊,比如IP地址,伺服器的業務歸屬,SN號,ILO IP等。剩下的資訊就要靠自己來發掘了。
完整的指令碼內容如下:
. ~/.bash_profile > /dev/null
BASEDIR="/home/hardcheck/tmp"
mkdir -p ${BASEDIR}/{tmplog,log} && cd ${BASEDIR}
DATE=`date +%Y%m%d%H%M`
LOG="hardcheck_${DATE}.log"
TMP_SCRIPT="newoscheck_${DATE}.sh"
 
exec 3>&1 4>&2 1>$LOG 2>&1
### 將伺服器列表作為引數傳遞進來
SERVERLIST=$1
if [ -z "${SERVERLIST}" ] || [ ! -s "${SERVERLIST}" ] ; then
#cat /home/raidcheck/alldbserver-linux.txt|iconv -f GBK -t UTF8|grep yangjr |grep -v Solaris|grep -v nopingdb|grep -v "#1"> db_list.all.lst
#SERVERLIST=$BASEDIR/db_list.all.lst
echo 'There is no conf files provided'
fi

SSH="ssh -oConnectionAttempts=3 -oConnectTimeout=5 -oStrictHostKeyChecking=no"
SCP="scp -oConnectionAttempts=3 -oConnectTimeout=5 -oStrictHostKeyChecking=no"

echo "#start hardcheck"
### {{{ 以下為真正的hardcheck檢查,遠端呼叫
while read line
do
  tmp_host=`echo $line|egrep -iv '^$|^#|Solaris|nopingdb'`
  IP=`echo $tmp_host|awk '{print $4}'|sed 's/;//g'`      
  SN=`echo $tmp_host|awk '{print $1}'`      
  OWNER=`echo $tmp_host|awk '{print $2}'`   
  DB_INFO=`echo $tmp_host|awk '{print $11}'`
  DB_TYPE=`echo $tmp_host|awk '{print $8}'`
  ILO_INFO=`echo $tmp_host|awk '{print $5}'|sed 's/;//g'`
  add_info=`cat /home/hardcheck/tmp/check_os.sh |$SSH $IP`
  echo "$IP;$SN;$OWNER;$DB_INFO;$DB_TYPE;$ILO_INFO;$add_info" >> $TMP_SCRIPT
  #echo "$IP;$SN;$OWNER;$DB_INFO;$DB_TYPE;$ILO_INFO;$add_info"
done < ${SERVERLIST}
### }}}

### {{{ 郵件報警
cat $TMP_SCRIPT |tee $LOG|iconv -f GBK -t UTF8  > mail.txt
#### }}}

MAILTO="test@test.com"
/bin/bash $BASEDIR/beautymail.sh   -t "DBIP;DB用途;負責人;主備庫;資料庫型別;ILO資訊;系統版本;核心版本;資料庫例項;ASM例項;剩餘記憶體;剩餘swap;記憶體;磁碟空間;執行天數" -m ${MAILTO} -s "Disk Error Count Health Daily Check(${DATE})" ${BASEDIR}/mail.txt

這個指令碼會額外呼叫一個重要的指令碼check_os.sh這個是系統層面的檢測。最後通過郵件的形式傳送到指定的郵箱。
check_os.sh指令碼內容如下:
OS_VER=`cat /etc/issue|sed  -e 's/Red Hat Enterprise Linux Server release/RHEL/g' -e 's/Red Hat Enterprise Linux AS release/RHEL/g' -e 's/ /_/g'|head -1`
OS_KER=`uname -r | awk -F. '{ printf("%d.%d\n",$1,$2); }'`

### MySQL版本
#MYSQL_VER=`mysql --disable-tee -N -e "select version()" < /dev/null`
MYSQL_EXISTS=`ps -ef|grep mysql|grep mysqld|grep -v grep |head -1`

### Oracle 版本
#export ORACLE_HOME=`cat /etc/oratab | tail -1 | awk -F: '{print $2}'`
#ORA_VER=`$ORACLE_HOME/bin/sqlplus -v`
ORA_LIST=`ps -ef|grep smon|grep -v grep|grep -v ASM|awk '{print $8}'|awk -F_ '{print $3}'`

### USE ASM
ASM_LIST=`ps -ef|grep smon|grep ASM|awk '{print $8}'|awk -F_ '{print $3}'`

### 記憶體剩餘率
MEMORY_FREE=`/usr/bin/free|grep 'Mem:'|/bin/awk '{print $4/$2*100}'`
MEMORY_FREE=`echo "${MEMORY_FREE:0:5}%"`

### SWAP剩餘率
SWAP_FREE=`/usr/bin/free|grep 'Swap:'|/bin/awk '{print $4/$2*100}'`
SWAP_FREE=`echo "${SWAP_FREE:0:5}%"`

### 磁碟空間使用率
df -hT|egrep -v 'Filesystem|/dev/shm'|while read line; do echo -e "`echo $line|awk '{print $7}'`:`echo $line|awk '{print $5}'`:`echo $line|awk '{print $6}'`"; done > /tmp/tmpdisk
cat /tmp/tmpdisk|while read line
do
  if [ `echo $line|awk -F':' '{print $3}'|sed 's/%//g'` -lt 80 ] ; then
    tmpline=`echo $line|sed 's/\///g'`
    sed -i "/$tmpline/d" /tmp/tmpdisk
  fi
done
if [ -s /tmp/tmpdisk ] ; then
  DISK_STAT=`cat /tmp/tmpdisk|sed -e ":a;N;s/\n/,/;ta"`
else
  DISK_STAT='OK'
fi
#rm -f /tmp/tmpdisk

### DB記憶體設定%
MEMORY_TOTAL=`/usr/bin/free|grep 'Mem:'|/bin/awk '{printf("%.f\n",$2*1000/1024/1024/1024)}'`
#MEMORY_MYSQL=`mysqladmin var|grep innodb_buffer_pool_size|awk -F'|' '{print $3}'|sed 's/ //g'`
#MYSQL_PERC=`mysql --disable-tee -N -e "select (${MEMORY_MYSQL}/${MEMORY_TOTAL}*100)"`
#MYSQL_PERC=`echo "${MYSQL_PERC:0:5}%"`

### DB服務時長
#if [ `/usr/bin/mysqladmin extended-status|grep -w Uptime|awk '{print $4}'` -lt 86400 ] ; then
#  UPTIME=`/usr/bin/mysql --disable-tee -e '\s'|grep Uptime|awk -F':' '{print $2}'|sed -e 's/\t//g'|sed -e 's/\t//g;s/\([0-9][0-9]*\) /\1/g;s/ /_/g'`
#else
#  UPTIME='OK'
#fi
UPTIME=`uptime |awk '{print $3}'`
echo  $OS_VER";" $OS_KER";" $ORA_LIST";" $ASM_LIST";" $MEMORY_FREE";" $SWAP_FREE";" $MEMORY_TOTAL";" $DISK_STAT ";"  $UPTIME

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

相關文章