shell指令碼:自動記憶體監控及日誌備份

regonly1發表於2008-05-21

由於系統經常出現記憶體溢位的情況,需要對系統記憶體的實時情況進行監控,一旦發現大於指定的臨界值時,根據指定郵件列表傳送郵件,自動備份並壓縮日誌,上傳到指定的ftp空間上。
並且實現了對於單個程式只做單次處理,只有在重啟之後,才會重新的新程式進行響應(記憶體超標的時候)。

以下的指令碼實現了這個功能,自動ftp上傳的功能在另外一個指令碼中實現(20080620更新新版本):
新版本:
說明:由於發現執行kill -3時可能對tomcat本身的java程式產生影響,使記憶體升高加劇,所以增加了2此檢查功能,在達到1500M時,傳送郵件提醒,但不做kill -3。因為此時可能記憶體還有降低的情況存在。一直到2000M時,才執行kill-3。
--1.增加了兩次檢查功能
#!/bin/sh

USER="regonly1";
CHKFLAG="java";
curdir=`pwd`;
runlogdir="$HOME/runlog";
msglogdir="$HOME/log";
#runlogdir="$HOME/local"; # Only for test.
maxchksize=1500; # If memory size great than maxchksize, mail to the users according to the mail list.
maxkill3sz=2000; # When memory size great than maxkill3sz, backup files and execute kill -3;
checkiter=1;
oldprocid=0;
interval=8;
#iter=0;

#Mail list. Use "," to delimit every mail address.
mailto="";


filetrans(){
 backupdate=`date +%Y%m%d`;
 if test -f ${curdir}/autoftptrans.epc
 then
  ${curdir}/autoftptrans.epc "$1" "${backupdate}";
 else
  echo "Request file dose not exist: ${curdir}/autoftptrans.epc.";
 fi;
}
packmsglogfile(){
 echo "Begin backup message file logs....";
 cd ${msglogdir};
 mkdir temp;
 targetfile=MsgFile.${chkdate}.tar.gz;
 for f in `ls Message*`; do
  cp $f temp/ ;
 done
 tar zcvf ${targetfile} temp;
 filetrans "${targetfile}";
 rm -fr temp "${targetfile}";
 echo "MessageFileLogs backup OK.";
}
packrunlogfile(){
 echo "Begin backup runlogs....";
 cd $runlogdir;
 cd `find ./ -type d | ls -lrt | tail -1 | awk '{print $9}'`;
 curfile=`find ./ -type f | ls -lrt | tail -1 | awk '{print $9}'`;
 targetfile="catalina.${chkdate}.tar.gz";
 cp ${curfile} ${curfile}.kk;
 tar zcvf ${targetfile} ${curfile}.kk;
 filetrans "${targetfile}";
 rm -fr ${curfile}.kk ${targetfile};
 echo "Runlog backup OK!";
}

while [ 1 ]; do
 getproc=`top -b -U ${USER} | grep ${USER} | grep ${CHKFLAG}`;
 memsize=`echo $getproc | awk '{print substr($7,1,length($7)-1);}'`;
 cpupcnt=`echo $getproc | awk '{print $10;}'`;
 procsid=`echo $getproc | awk '{print $1;}'`;
 chktime=`date +"%Y-%m-%d %H:%M:%S"`;
 chkdate=`date +"%Y%m%d"`;
 if [ "${memsize}" -gt "${maxchksize}" -a "${oldprocid}" -ne "${procsid}" ];then
  oldprocid=${procsid};
      echo "Memory overflow: ${memsize}M";
  echo "Send mail to " ${mailto};
  echo "Check time:[${chktime}]  Memory used:${memsize}M. Host:`hostname`." | mail ${mailto};
                checkiter=1;
 elif [ "${memsize}" -gt "${maxchksize}" -a "${oldprocid}" -eq "${procsid}" ]; then
  if [ "${memsize}" -gt "${maxkill3sz}" -a "${checkiter}" -eq 1 ]; then
   echo "Memory size large than ${memsize}.";
   echo "Begin to backup logfiles...";
   packmsglogfile;
   packrunlogfile;
   kill -3 ${procsid};
   echo "Logfile backup OK!";
   echo "Logfile backup OK." | mail ${mailto};
                        checkiter=0;
  fi;
  echo "Memory overflow:${memsize}M. CPU: ${cpupcnt}. Please check memory in time!";
  sleep ${interval};
 else
  echo "Memory status OK. Memory: ${memsize}M. CPU: ${cpupcnt}. at `hostname` [${chktime}].";
  sleep ${interval};
 fi;
done;



--原來版本
#!/bin/sh

USER="regonly1";#指定top的使用者
CHKFLAG="java";#監測的程式標誌
curdir=`pwd`;
runlogdir="$HOME/runlog";
msglogdir="$HOME/log";
#runlogdir="$HOME/local"; # 僅用於測試
maxsize=1500; #監控臨界點,超過這個值則傳送郵件給指定的郵件使用者
oldprocid=0;#記錄老的程式id
interval=8;#記錄監測的間隔時間
#iter=0;#迭代器,取消

#郵件列表,以","分隔
mailto="mymail@abc.com";

#自動傳輸函式,呼叫expect指令碼來實現自動ftp上傳的功能:
filetrans(){
 backupdate=`date +%Y%m%d`;
 ${curdir}/autoftptrans.epc "$1" "${backupdate}";
}
packmsgfile(){
 echo "Begin backup message file logs....";
 cd ${msglogdir};
 mkdir temp;
 targetfile=MsgFile.${chkdate}.tar.gz;
 for f in `ls Message*`; do
  cp $f temp/ ;
 done
 tar zcvf ${targetfile} temp;
 filetrans "${targetfile}";
 rm -fr temp "${targetfile}";
 echo "MessageFileLogs backup OK.";
}
packrunlogfile(){
 echo "Begin backup runlogs....";
 cd $runlogdir;
 cd `find ./ -type d | ls -lrt | tail -1 | awk '{print $9}'`;
 curfile=`find ./ -type f | ls -lrt | tail -1 | awk '{print $9}'`;
 targetfile="catalina.${chkdate}.tar.gz";
 cp ${curfile} ${curfile}.kk;
 tar zcvf ${targetfile} ${curfile}.kk;
 filetrans "${targetfile}";
 rm -fr ${curfile}.kk ${targetfile};
 echo "Runlog backup OK!";
}

while [ 1 ]; do
 getproc=`top -b -U ${USER} | grep ${USER} | grep ${CHKFLAG}`;
 memsize=`echo $getproc | awk '{print substr($7,1,length($7)-1);}'`;
 cpupcnt=`echo $getproc | awk '{print $10;}'`;
 procsid=`echo $getproc | awk '{print $1;}'`;
 chktime=`date +"%Y-%m-%d %H:%M:%S"`;
 chkdate=`date +"%Y%m%d"`;
#對於同一個程式id,只做一次記憶體超出郵件提醒,後面再程式重啟之前都不會再對該程式監測
 if [ "${memsize}" -gt "${maxsize}" -a "${oldprocid}" -ne "${procsid}" ];then
  oldprocid=${procsid};
      echo "Memory overflow: ${memsize}M";
  echo "Send mail to " ${mailto};
  echo "Check time:[${chktime}]  Memory used:${memsize}M. Host:`hostname`." | mail ${mailto};
  kill -3 ${procsid};
  packmsgfile;
  packrunlogfile;
 elsif [ "${memsize}" -gt "${maxsize}" -a "${oldprocid}" -eq "${procsid}" ]
  echo -n "Memory overflow:${memsize}M. CPU: ${cpupcnt}.";
  echo "Logs already been backuped. Please check memory in time!";
  sleep ${interval};
 else
  echo "Memory status OK. Memory: ${memsize}M. CPU: ${cpupcnt}. at `hostname` [${chktime}].";
  sleep ${interval};
 fi;
done;

自動ftp上傳指令碼,其中檔案引數和日期引數都是從上面的指令碼中傳過來的:
#!/usr/local/bin/expect

set file [lindex $argv 0];
set date [lindex $argv 1];
set timeout -1;
while {1} {
 spawn ftp 192.168.11.24;
 expect "Name";
 send "ftpuser\r";
 expect "Password";
 send "ftppasswd\r";
 expect "Login failed." {send "quit\r";close;wait} "ftp>" break;
}
send "cd hzdtvoss/daycheck/\r";
expect "ftp>";
send "mkdir $date\r";
expect "ftp>";
send "cd $date\r";
expect "ftp>";
send "bi\r";
expect "ftp>";
send "put $file\r"
expect "ftp>";
send "quit\r";

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

相關文章