JAVA jps jstat の利用と応用

1) jps コマンドで、今動いてるJAVAのプロセスを確認

2) jstat で Javaのメモリ使用状況を確認

3) メモリ使用状況のログ追加と、警告メール配信

[ ログテーブルのスキーマ ]


sqlite> .schema log_jstat
CREATE TABLE log_jstat (
tm timestamp not null,
pid integer not null,
pnm text not null,
S0C real not null, 
S1C real not null,
S0U real not null,
S1U real not null,

EC real not null,
EU real not null,
OC real not null,
OU real not null,
ttlCapM integer not null default 0, 
ttlUseM integer not null default 0,
ttlRT   integer not null default 0);
CREATE INDEX log_jstat_tm on log_jstat (tm);
CREATE TRIGGER log_jstat_ai after insert on log_jstat for each row
begin
update log_jstat
set
ttlCapM = cast((S0C + S1C + EC + OC) / 1024 as int),
ttlUseM = cast((S0U + S1U + EU + OU) / 1024 as int),
ttlRT = cast((S0U + S1U + EU + OU) / (S0C + S1C + EC + OC) * 100 as int)
where
tm > current_date || ' 00:00:00';
end;
sqlite> 

[ Shellコードサンプル ]


#! /bin/bash

# --------------------const ------------------------ #

TMP_OUT_FILENAME_JPS=/home/sqlite/sql/jps.txt
TMP_OUT_FILENAME_JSTL=/home/sqlite/sql/jstl.txt

SQL_OUT_FILENAME=/home/sqlite/sql/tmp_jstl_add.sql
DBNAME=/home/sqlite/logs
INSERT_DML="insert into log_jstat (tm, pid, pnm, S0C,S1C,S0U, S1U, EC, EU, OC, OU) values (datetime('now','localtime'), "
echo $INSERT_DML

LAST_URATIO_SQL="SELECT mount, uratio FROM log_df ORDER BY tm DESC LIMIT 0, 3;"
LAST_URATIO_SQL_FILENAME=/home/sqlite/sql/lastgcratio.sql

GC_OUT_TEXTFILENAME=/home/ApacheDoc/APIStatus/tmpgcres.txt

LAST_URATIO_RES_FILENAME=/home/sqlite/sql/lastdf.txt
LAST_URATIO_RES_FILENAME2=/home/sqlite/sql/lastdf2.txt

WARN_TEMPLATE_FILENAME=/home/ssmtptxt/dfwarn.txt
WARN_TMP_FILENAME=/home/ssmtptxt/tmp_dfwarn.txt
WARN_TMP_FILENAME2=/home/ssmtptxt/tmp_dfwarn2.txt

MAX_ALLOWED=89

# ---------------------------------------------------- #

fmtnow=`date +%Y-%m-%d' '%H:%M:%S -d today`
echo "------------------- "$fmtnow" -------------------"

/usr/bin/jps | grep Bootstrap > ${TMP_OUT_FILENAME_JPS}
cat ${TMP_OUT_FILENAME_JPS}

#exit 0

# Write temporaly SQL file ##

echo -n > ${SQL_OUT_FILENAME}

pid=""
pnm=""
jstatres=""
inssql=""
cat ${TMP_OUT_FILENAME_JPS} | while read line
do
  pid=`echo ${line} | gawk '{print $1}'`
  pnm=`echo ${line} | gawk '{print $2}' | sed -e 's/^/"/' -e 's/$/"/'`
  echo ${pid} ${pnm} 

  jstatres=`jstat -gc ${pid} | grep -v S0C | gawk '{print $1","$2","$3","$4","$5","$6","$7","$8}'`
  #echo ${jstatres} 
  inssql=${INSERT_DML}${pid}","${pnm}","${jstatres}");"


  echo ${inssql} >> ${SQL_OUT_FILENAME}
done

cat ${SQL_OUT_FILENAME}
#exit 0


# Write sqlite database #
/usr/bin/sqlite3 ${DBNAME} < ${SQL_OUT_FILENAME}


# Output last res by sqlite (uratio updated by trigger) #
/usr/bin/sqlite3 $DBNAME < ${LAST_URATIO_SQL_FILENAME} > ${GC_OUT_TEXTFILENAME}

exit 0

echo "--- each uratio ---"
#cat ${LAST_URATIO_RES_FILENAME}
#cat ${LAST_URATIO_RES_FILENAME} | sed -e 's/|/;/g'  > ${LAST_URATIO_RES_FILENAME2}
#exit 0

# Loop and send a mail #
tmpmp=""
tmprt=0
wrntxt=""
wrncnt=0
while read line
do
  #echo ${line}

  tmpmp=`echo ${line} | gawk -F"|"  '{print $1}'`
  tmprt=`echo ${line} | gawk  -F"|" '{print $2}'`
  echo ${tmpmp}":"${tmprt}

  if [ ${tmprt} -gt ${MAX_ALLOWED} ]; then
    wrntxt=${wrntxt}${tmpmp}":"${tmprt}"%zzzzz"
    #wrntxt=`echo -e ${wrntxt}${tmpmp}" : "${tmprt}"%  \n"`
    #wrntxt=`echo ""${wrntxt}`
    let wrncnt++
  fi

done < ${LAST_URATIO_RES_FILENAME}

# send a mail by ssmtp #
if [ $[wrncnt] -gt 0 ]; then
  cp ${WARN_TEMPLATE_FILENAME} ${WARN_TMP_FILENAME}
  echo ${wrntxt} >> ${WARN_TMP_FILENAME}
  cat ${WARN_TMP_FILENAME} | sed 's/zzzzz/\n/g' > ${WARN_TMP_FILENAME2}

  echo "" >> ${WARN_TMP_FILENAME}
  echo "Issued Time : "`date`  >> ${WARN_TMP_FILENAME}

  /usr/sbin/ssmtp -t < ${WARN_TMP_FILENAME2}
fi

echo "------------------------------------------------"



4) cronにセットしてスケジュール実行


## Intervaled jstat log add ##
1,21,41 * * * * root /home/sh/jstatcheck.sh >> /home/Log/last_jstatlogadd.log 2>&1

5) メモリ使用状況ログをページで表示

コメントを残す