LoginSignup
2
3

More than 5 years have passed since last update.

AWS RDSのログを監視する

Last updated at Posted at 2018-04-08

AWS RDSのログを監視する

RDSログを監視する方法がAWSにないのでシェルを定期的に動かして監視するバッチを作る。

1.AWS上のログをローカルにコピーする

sample.sh
#! /bin/bash

IFS=','
MAILING_LIST=(
 example@xxx.co.jp
 sample@xxx.com
)

date=`date +"%Y-%m-%d"`

current_hour=`date --date "9 hours ago" "+%k"`
current_file_name_hour=`printf "%02d" $current_hour`
current_file_name="error/postgresql.log.$date-$current_file_name_hour"
local_copy_file="current_postgresql.log"
region="ap-northeast-1"
instance_name=""

aws rds download-db-log-file-portion \
  --region $region \
  --db-instance-identifier $instance_name \
  --no-paginate \
  --output text \
  --log-file-name $current_file_name > $local_copy_file

2.監視してメール出力する

ログに指定文字列が記述されていたらメールを送信する
※sendmailについては割愛

sample.sh
#! /bin/bash

IFS=','
MAILING_LIST=(
 example@xxx.co.jp
 sample@xxx.com
)

date=`date +"%Y-%m-%d"`

current_hour=`date --date "9 hours ago" "+%k"`
current_file_name_hour=`printf "%02d" $current_hour`
current_file_name="error/postgresql.log.$date-$current_file_name_hour"
local_copy_file="current_postgresql.log"
region="ap-northeast-1"
instance_name=""

aws rds download-db-log-file-portion \
  --region $region \
  --db-instance-identifier $instance_name \
  --no-paginate \
  --output text \
  --log-file-name $current_file_name > $local_copy_file

+ current_log=$(cat $local_copy_file)
+
+ filter_word="ERROR"
+ mail_subject="alarm notice"
+ mail_body="mail body"
+
+ if [[ `echo $current_log|grep $filter_word` ]]; then
+   echo "Subject: $mail_subject \n\n $mail_body" | /usr/lib/sendmail + ${MAILING_LIST[*]}]}
+ fi

ソースコード

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3