監控DB2資料庫指令碼

wdnmg發表於2011-02-24

PRODUCT DB2 SPACE MONITOR SHELL

#! /bin/sh
Month=`date "+%m"`
Day=`date "+%d"`
Year=`date "+%y"`
YEAR=`date "+%Y"`

LogDir=/tmp/sysmonitor

DISK=$LogDir/FILESYSTEM_$YEAR$Month$Day.txt
Temp_file1=$LogDir/temp1.log
Temp_file2=$LogDir/temp2.log

#####Monitor FileSpace#####
date>> $DISK
df -m >> $DISK

CNT1=`grep dev $DISK|grep -E -w "hd4|var|maestro"|awk '{if($3/$2<0.2){print $0}}'|wc -l`
CNT2=`grep dev $DISK|grep -E -w -v "hd4|var|hd3|usr|maestro|hd1|opt|fix|proc"|awk '{if($3/$2<0.1){print $0}}'|wc -l`
CNT3=`grep dev $DISK|grep -E -w "hd3"|awk '{if($3<1500){print $0}}'|wc -l`

if [ `expr $CNT1 + $CNT2 + $CNT3` -gt 0 ] ; then
  CRITICAL=$LogDir/CRITICAL_FILESYSTEM_$YEAR$Month$Day.txt
  echo "***********CRITICAL***********" >> $CRITICAL
  date >> $CRITICAL
  grep -w Free $DISK >> $CRITICAL
  grep dev $DISK|grep -E -w "hd4|var|maestro"|awk '{if($3/$2<0.2){print $0}}' >> $CRITICAL
  grep dev $DISK|grep -E -w -v "hd4|var|hd3|usr|maestro|hd1|opt|fix|proc"|awk '{if($3/$2<0.1){print $0}}' >> $CRITICAL
  grep dev $DISK|grep -E -w "hd3"|awk '{if($3<1500){print $0}}' >> $CRITICAL
fi

CNT1=`grep dev $DISK|grep -E -w "hd4|var|maestro"|awk '{if($3/$2<0.35){print $0}}'|wc -l`
CNT2=`grep dev $DISK|grep -E -w -v "hd4|var|hd3|usr|maestro|hd1|opt|fix|proc"|awk '{if($3/$2<0.2){print $0}}'|wc -l`
CNT3=`grep dev $DISK|grep -E -w "hd3"|awk '{if($3<2000){print $0}}'|wc -l`

if [ `expr $CNT1 + $CNT2 + $CNT3` -gt 0 ] ; then
  WARNING=$LogDir/WARNING_FILESYSTEM_$YEAR$Month$Day.txt
  echo "***********WARNING***********" >> $WARNING
  date >> $WARNING
  grep -w Free $DISK >> $WARNING
  grep dev $DISK|grep -E -w "hd4|var|maestro"|awk '{if($3/$2<0.35){print $0}}' >> $WARNING
  grep dev $DISK|grep -E -w -v "hd4|var|hd3|usr|maestro|hd1|opt|fix|proc"|awk '{if($3/$2<0.2){print $0}}' >> $WARNING
  grep dev $DISK|grep -E -w "hd3"|awk '{if($3<2000){print $0}}' >> $WARNING
fi


####Monitor TableSpace####
CRITICAL=$LogDir/CRITICAL_TABLESPACE_$YEAR$Month$Day.txt

TABLE=$LogDir/TABLESPACE_GPCDB_$YEAR$Month$Day.txt
su - gpcp <db2 connect to gpcdb > /dev/null
db2 list tablespaces show detail >> $TABLE
db2 connect reset > /dev/null
exit
!!

grep -E -w "Name|Useable pages|Used pages" $TABLE > $Temp_file1
awk '{if(NR%3==1){name=$NF};if(NR%3==2){x=$NF};if(NR%3==0){y=$NF;print name,y/x*100}}' $Temp_file1 > $Temp_file2

CNT=`awk '$1 ~ /^G/ {if($2>95){print $0"%"}}' $Temp_file2|wc -l`

if [ $CNT -gt 0 ] ; then
  echo "***********CRITICAL GPCDB***********" >> $CRITICAL
  date >> $CRITICAL
  awk '$1 ~ /^G/ {if($2>95){print $0"%"}}' $Temp_file2 >> $CRITICAL
fi


TABLE=$LogDir/TABLESPACE_GALDB_$YEAR$Month$Day.txt
su - galp <db2 connect to galdb > /dev/null
db2 list tablespaces show detail >> $TABLE
db2 connect reset > /dev/null
exit
!!

grep -E -w "Name|Useable pages|Used pages" $TABLE > $Temp_file1
awk '{if(NR%3==1){name=$NF};if(NR%3==2){x=$NF};if(NR%3==0){y=$NF;print name,y/x*100}}' $Temp_file1 > $Temp_file2

CNT=`awk '$1 ~ /^G/ {if($2>95){print $0"%"}}' $Temp_file2|wc -l`

if [ $CNT -gt 0 ] ; then
  echo "***********CRITICAL GALDB***********" >> $CRITICAL
  date >> $CRITICAL
  awk '$1 ~ /^G/ {if($2>95){print $0"%"}}' $Temp_file2 >> $CRITICAL
fi


TABLE=$LogDir/TABLESPACE_GSIDB_$YEAR$Month$Day.txt
su - gsip <db2 connect to gsidb > /dev/null
db2 list tablespaces show detail >> $TABLE
db2 connect reset > /dev/null
exit
!!

grep -E -w "Name|Useable pages|Used pages" $TABLE > $Temp_file1
awk '{if(NR%3==1){name=$NF};if(NR%3==2){x=$NF};if(NR%3==0){y=$NF;print name,y/x*100}}' $Temp_file1 > $Temp_file2

CNT=`awk '$1 ~ /^G/ {if($2>95){print $0"%"}}' $Temp_file2|wc -l`

if [ $CNT -gt 0 ] ; then
  CRITICAL=$LogDir/CRITICAL_TABLESPACE_$YEAR$Month$Day.txt
  echo "***********CRITICAL GSIDB***********" >> $CRITICAL
  date >> $CRITICAL
  awk '$1 ~ /^G/ {if($2>95){print $0"%"}}' $Temp_file2 >> $CRITICAL
fi


######Remove Temp file#####
rm $Temp_file1
rm $Temp_file2

######Remove Old file#####
find $LogDir -name "*.txt" -mtime +90 -print -exec rm {} ;

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

相關文章