Skip to content

Instantly share code, notes, and snippets.

@bluemooninc
Created November 15, 2018 08:07
Show Gist options
  • Select an option

  • Save bluemooninc/39cc8a49a6894e9a08c940132ca9350c to your computer and use it in GitHub Desktop.

Select an option

Save bluemooninc/39cc8a49a6894e9a08c940132ca9350c to your computer and use it in GitHub Desktop.
長時間掛かるバッチの監視について ref: https://qiita.com/bluemooninc/items/2d583b5757affd878261
#/bin/bash
#
# pickup process files of the over 3600sec execution time.
# in the process target strings
#
PregStr="i\/usr\/bin\/java(.*)\.jar"
PID=$$
for i in `ps -ef | grep -E "$PregStr" | grep -v $PID | grep -v grep | awk '{print $2}'`
do
TIME=`ps -o lstart --noheader -p $i`
if [ -n "$TIME" ]; then
StartupTime=`date +%s -d "$TIME"`
CurrentTime=`date +%s`
ElapsedTime=`expr $CurrentTime - $StartupTime`
else
ElapsedTime=1
fi
if [ $ElapsedTime -gt 3600 ] ; then
fpath=`ps -ef | grep -E "^[0-9]{1,}\s+$i\s+" | awk '{print $11}'`
echo $fpath $ElapsedTime
fi
done
#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/jre
export AWS_CLOUDWATCH_URL=https://monitoring.amazonaws.com
export EC2_REGION=ap-northeast-1
instanceid=$(curl -sS http://IP_ADDRESS/latest/meta-data/instance-id)
# batch start
LOG_FILE="ログファイルのパス"
echo $(date +'%Y-%m-%d[%H:%M:%S]')"CheckOver1HourPS START" >> $LOG_FILE
# 先ほどのシェルの echo $fpath $ElapsedTime を変数に
OVER_PS=$fpath $ElapsedTime
# put cloudwatch
RESULT=$(aws cloudwatch put-metric-data --metric-name "CheckOver1HourPS" --namespace "System/Linux" --dimensions "InstanceId=$instanceid" --value "$OVER_PS" --unit "Files" --region $EC2_REGION)
# end log
echo $(date +'%Y-%m-%d[%H:%M:%S]')"CheckOver1HourPS END" >> $LOG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment