├── README.md ├── License.md └── run_pgbadger_rds.sh /README.md: -------------------------------------------------------------------------------- 1 | # pgbadger_on_rds 2 | -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /run_pgbadger_rds.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | . ~/.bash_profile 3 | # script to generate pgbadger reports 4 | #Arguments = -i aws-instance-name -r aws-region-name -c cron_or_not -v 5 | usage() 6 | { 7 | cat << EOF 8 | usage: $0 options 9 | This script will downalod the postgres rds log files and generate pgbadger reports 10 | OPTIONS: 11 | -h Show this message 12 | -i DBInstanceIdentifier 13 | -r AWS Region 14 | -v Verbose 15 | EOF 16 | } 17 | 18 | AWS_INSTANCE= 19 | AWS_REGION= 20 | VERBOSE= 21 | IS_CRON=0 22 | while getopts “hi:r:c::v” OPTION 23 | do 24 | case $OPTION in 25 | h) 26 | usage 27 | exit 1 28 | ;; 29 | i) 30 | AWS_INSTANCE=$OPTARG 31 | ;; 32 | r) 33 | AWS_REGION=$OPTARG 34 | ;; 35 | c) 36 | IS_CRON=$OPTARG 37 | ;; 38 | v) 39 | VERBOSE=1 40 | ;; 41 | ?) 42 | usage 43 | exit 44 | ;; 45 | esac 46 | done 47 | 48 | if [[ -z $AWS_INSTANCE ]] || [[ -z $AWS_REGION ]] 49 | then 50 | usage 51 | exit 1 52 | fi 53 | 54 | 55 | # environment variables 56 | #AWS_CREDENTIAL_FILE=~/.aws/aws_credential_file 57 | #export AWS_CREDENTIAL_FILE 58 | 59 | # let's put date in a variable 60 | TODAY=$(date '+%Y-%m-%d') 61 | #YESTERDAY=`/bin/date -d "1 day ago" +\%Y-\%m-\%d` 62 | YESTERDAY=$(date -d "1 day ago" +\%Y-\%m-\%d) 63 | 64 | DOWNLOAD_DATE='' 65 | 66 | if [ $IS_CRON -eq 0 ] 67 | then 68 | DOWNLOAD_DATE=$TODAY 69 | else 70 | DOWNLOAD_DATE=$YESTERDAY 71 | fi 72 | 73 | # pgbadger home 74 | PGBADGER_HOME=/home/ec2-user/pgbadger/pgbadger-report/ 75 | mkdir -p $PGBADGER_HOME/$AWS_INSTANCE 76 | 77 | #function starts 78 | download_rds_logs_and_generate_html() { 79 | 80 | #remove file, if exists 81 | rm -f $PGBADGER_HOME/$AWS_INSTANCE/postgresql.log.$DOWNLOAD_DATE.txt 82 | 83 | #describe and downlowd log files for yesterday 84 | #describe and downlowd log files for yesterday 85 | for filename in $( aws rds describe-db-log-files --db-instance-identifier $AWS_INSTANCE --region $AWS_REGION |grep error/postgresql |grep $DOWNLOAD_DATE | awk '{gsub("\"","",$2)} {gsub(",","",$2); print $2}' ) 86 | do 87 | echo $filename 88 | aws rds download-db-log-file-portion --db-instance-identifier $AWS_INSTANCE --region $AWS_REGION --starting-token 0 --output text --log-file-name $filename >> $PGBADGER_HOME/$AWS_INSTANCE/postgresql.log.$DOWNLOAD_DATE.txt 89 | #cd $PGBADGER_HOME/$AWS_INSTANCE 90 | #ls -ltr 91 | done 92 | 93 | # run pgbadger report 94 | cd $PGBADGER_HOME/$AWS_INSTANCE 95 | #ls -ltr 96 | pgbadger --p '%t:%r:%u@%d:[%p]:' postgresql.log.$DOWNLOAD_DATE.txt -o postgresql.log.$DOWNLOAD_DATE.html 97 | 98 | # remove log file 99 | rm $PGBADGER_HOME/$AWS_INSTANCE/postgresql.log.$DOWNLOAD_DATE.txt 100 | return 0 101 | } 102 | #function ends 103 | 104 | #call the function to download log files and generate pgbadger html file 105 | download_rds_logs_and_generate_html $DOWNLOAD_DATE 106 | 107 | 108 | # Download log files and run pgbadger report 109 | 110 | #if [ $IS_CRON -eq 0 ] 111 | #then 112 | # DOWNLOAD_DATE = $TODAY 113 | # download_rds_logs_and_generate_html $DOWNLOAD_DATE 114 | #else 115 | # DOWNLOAD_DATE = $YESTERDAY 116 | # download_rds_logs_and_generate_html $DOWNLOAD_DATE 117 | #fi 118 | --------------------------------------------------------------------------------