ITPub博客

首页 > Linux操作系统 > Linux操作系统 > shell脚本:自动内存监控及日志备份

shell脚本:自动内存监控及日志备份

原创 Linux操作系统 作者:regonly1 时间:2008-05-21 22:10:50 0 删除 编辑

由于系统经常出现内存溢出的情况,需要对系统内存的实时情况进行监控,一旦发现大于指定的临界值时,根据指定邮件列表发送邮件,自动备份并压缩日志,上传到指定的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="regonly1@abc.com";


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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-05-10

  • 博文量
    257
  • 访问量
    1070164