Linux端口监控脚本

昭君出塞 2011-03-31

最近jboss进程经常无缘无故的被Halt,怀疑是某同学远程Debug时,直接点击了Eclipse上的Terminate按钮。但是又没有证据,所以写了一段脚本,专门监控8787端口的占用情况,脚本如下:

#! /bin/sh
#Author lanbo.xj

LSNPORTS="8787";
INTERVAL=60;
iter=0;
LOGPATH=$HOME/wlbmywlb/logs;

if [ -n $LOGPATH ]; then mkdir -p $LOGPATH; fi;

while [ 1 ]; do
 for port in $LSNPORTS
 do
  CCTLST=`netstat -an | grep ".$port" | awk 'BEGIN{OFS="|";}{print $1,$2,$3,$4,$5,$6,$7;}'`;
  LOGDATE=`date +%Y-%m-%d`;
  LOGFILE="${LOGPATH}/netstat[${port}]-${LOGDATE}.log";
  LOGTIME=`date '+DATE:%Y-%m-%d %H:%M:%S'`;

  for ns in $CCTLST
  do
   NETLST=`echo $ns | awk -F"|" 'BEGIN{OFS=" ";}{print $1,$2,$3,$4,$5,$6,$7;}'`;

   iter=`expr $iter + 1`;
   loginfo=`echo "$LOGTIME" ">>" "$NETLST"`;
   echo "$loginfo" >> $LOGFILE;
#echo "$loginfo";
  done;
  echo "$LOGTIME >> Total connections[$port]: " $iter >> $LOGFILE;
#echo "Total connections[$port]: " $iter;
  iter=0;
  sleep $INTERVAL;
    done;
done;
 

相关推荐