Skip to content

Instantly share code, notes, and snippets.

@guillaume-martin
Created January 10, 2022 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillaume-martin/f82ee33aaccfb671bac75253ef070f2c to your computer and use it in GitHub Desktop.
Save guillaume-martin/f82ee33aaccfb671bac75253ef070f2c to your computer and use it in GitHub Desktop.
A script for automatically downloading log files from an AWS RDS instance and generating a report using pgbadger.
#!/bin/bash
# Downloads all the log files for a selected instance on a given day. Then
# generates a report using pgbadger.
# Requirements:
# - AWS CLI (https://github.com/aws/aws-cli)
# - pgbadger (https://github.com/darold/pgbadger)
# How to use:
# rds-logs-report <instance_id> <date>
echo "================================================="
instance=$1
logdate=$2
echo "RDS Instance: ${instance}"
echo "Date: ${logdate}"
echo "-----------------------------------------"
# Setup a directory where the logs and reports will be saved
basedir=/path/to/logs/dir
# Directory where the log files are downloaded
logdir="${basedir}/logs/${instance}"
mkdir -p $logdir
if [ $? -ne 0 ]; then
echo "Failed to create ${logdir}"
exit 1
fi
echo "Saving log files in ${logdir}"
# Directory where the reports are saved
reportdir="${basedir}/reports/${instance}"
mkdir -p $reportdir
if [ $? -ne 0 ]; then
echo "Failed to create ${reportdir}"
exit 1
fi
echo "Saving report in ${reportdir}"
echo "-----------------------------------------"
# File where the downloaded logs are saved
logfile="postgresql.log.${logdate}-all"
echo "Generating log file ${logfile}"
for h in $(seq 0 23)
do
if [ ${#h} -eq 1 ]
then
h="0${h}"
fi
logportion="error/postgresql.log.${logdate}-${h}"
echo "Downloading ${logportion}"
aws rds download-db-log-file-portion \
--db-instance-identifier $instance \
--log-file-name $logportion \
--starting-token 0 \
--output text >> $logdir/$logfile
done
echo "-----------------------------------------"
echo "Generating the report from ${logfile}"
pgbadger \
-f stderr \
-p "%t:%r:%u@%d:[%p]:" \
-o "${reportdir}/logs-report-${logdate}.html" \
$logdir/$logfile
echo "Report saved in ${reportdir}/logs-report-${logdate}.html"
# Archive the logfile to save space
tar -czf "${logdir}/${logfile}.tar.gz" $logdir/$logfile
rm $logdir/$logfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment