Last active
February 9, 2017 22:36
-
-
Save hiszpanski/8e25b99286dc58ab1d55d000e47126e5 to your computer and use it in GitHub Desktop.
Fetch RDS log files via command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Fetch RDS log file via command line | |
AWS deprecated their RDS command-line utility, and the AWS CLI only includes | |
a subcommand for downloading portion of log file. This downloads full log | |
file, so that it may be analyzed in say pgbadger, for example: | |
pgbadger -A 1 -a 1 -j 4 --prefix '%t:%r:%u@%d:[%p]:' \ | |
--log-duration --outfile postgresql.log-2017-02-09-07.html \ | |
postgresql.log.2017-02-09-07 | |
""" | |
import boto3 | |
import sys | |
rds = boto3.client('rds') | |
def fetch(db_instance_identifier, log_file_name): | |
from os.path import basename | |
marker = '0' | |
with open(basename(log_file_name), 'w') as f: | |
incomplete = True | |
while incomplete: | |
# Download portion | |
response = rds.download_db_log_file_portion( | |
DBInstanceIdentifier=db_instance_identifier, | |
LogFileName=log_file_name, | |
Marker=marker) | |
# Write data to file | |
f.write(response['LogFileData']) | |
marker = response['Marker'] | |
incomplete = response['AdditionalDataPending'] | |
sys.stderr.write('.') | |
sys.stderr.write('\n') | |
if __name__ == '__main__': | |
fetch(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment