원문 : http://www.ischo.net -- 조인상 //시스템 엔지니어

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

top의 command line 을 이용하여 전체시스템 CPU,메모리, 특정 프로세스 메모리 사용량을 로깅하고

CSV 형식으로 출력하는 쉘 스크립트.

 

 

특정프로세스 (ex:java)만의 메모리 사용 변화량을 보기 위함.

 

#!/bin/ksh

# Shell - Logging Total used memory & specific PID's used memory


## ENVIRONMENT SET ##########################
DATE=`date +%Y%m%d_%H:%M:%S`
# entire log file name
LOGFILE=result_${DATE}.log
# csv file name
DESTFILE=result_${DATE}.csv
# set checking process's PID
PID=19686
# INTERVAL by second
INTERVAL=1
#############################################


## MAIN PROCEDURE ###########################
while [ 0 ];
do

        top -bM -n 1 -p${PID} >> ${LOGFILE}

        TIME=`tail -10 ${LOGFILE} | grep "top -" | awk '{print $3}'`

        CPU_USED=`tail -10 ${LOGFILE} | grep Cpu | awk '{print $2}' | sed 's/us//g'`
        MEM_USED=`tail -10 ${LOGFILE} | grep Mem: | awk '{print $4}'`
        PID_USED=`tail -10 ${LOGFILE} | grep ${PID} | awk '{print $6}'`
        echo "${TIME},${CPU_USED},${MEM_USED},${PID_USED}" >> ${DESTFILE}

        sleep ${INTERVAL}

done
#############################################

 

 

 

 

(*.118.235.233)