Skip to content

Instantly share code, notes, and snippets.

@sherwind
Forked from matheusoliveira/README.md
Last active June 16, 2022 13:58
Show Gist options
  • Save sherwind/962fabb187769517e93a0ac57bf88f4e to your computer and use it in GitHub Desktop.
Save sherwind/962fabb187769517e93a0ac57bf88f4e to your computer and use it in GitHub Desktop.

Revisions

  1. sherwind revised this gist Sep 13, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rds-top.sh
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,7 @@ Usage: $0 [options] rds_instance_id
    OPTIONS:
    -s or --start-time=t Optional: Specify the start time in seconds since the Unix epoch
    -m or --sort-by-mem Optional: Sorts output by memory. Default is to sort by CPU
    EOF
    }

  2. sherwind revised this gist Sep 13, 2019. 1 changed file with 18 additions and 7 deletions.
    25 changes: 18 additions & 7 deletions rds-top.sh
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@
    # Usage:
    # ./rds-top.sh rds-instance
    # ./rds-top.sh --start-time=$(date -v-13d +%s) rds-instance
    # ./rds-top.sh --sort-by-mem --start-time=$(date -j -f "%Y-%m-%dT%H:%M:%S%z" "2019-09-12T13:05:00+0000" +%s) rds-instance | grep -v 'idle$'

    set -e

    @@ -32,7 +33,11 @@ do
    case $i in
    -s=*|--start-time=*)
    START_TIME="${i#*=}"
    shift # past argument=value
    shift
    ;;
    -m | --sort-by-mem )
    SORT_BY_MEM=1
    shift
    ;;
    *)
    # unknown option
    @@ -42,12 +47,12 @@ done
    INSTANCE_ID="$1"


    if [[ "$INSTANCE_ID" == "" ]];
    if [[ "$INSTANCE_ID" == "" ]]
    then
    error "You have to supply an instance ID!";
    usage;
    exit;
    fi;
    error "You have to supply an instance ID!"
    usage
    exit
    fi


    # Get resource id
    @@ -82,6 +87,12 @@ jq -r '.diskIO[] | "Disk \(.device): \(.tps) tps, \(.rrqmPS) rrqm/s, \(.wrqmPS)

    echo

    sort_by=".cpuUsedPc"
    if [[ $SORT_BY_MEM -eq 1 ]]
    then
    sort_by=".memoryUsedPc"
    fi

    echo "${message_json}" |
    jq -r '["PID", "PPID", "VSS", "RSS", "%CPU", "%MEM", "COMMAND"], (.processList | sort_by(.cpuUsedPc) | reverse[] | [.id, .parentID, .vss, .rss, .cpuUsedPc, .memoryUsedPc, .name]) | @tsv' |
    jq -r '["PID", "PPID", "VSS", "RSS", "%CPU", "%MEM", "COMMAND"], (.processList | sort_by('"$sort_by"') | reverse[] | [.id, .parentID, .vss, .rss, .cpuUsedPc, .memoryUsedPc, .name]) | @tsv' |
    column -t -s $'\t'
  3. sherwind revised this gist Aug 29, 2019. 2 changed files with 76 additions and 29 deletions.
    14 changes: 0 additions & 14 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +0,0 @@
    This is a very simple script, so don't expect something great.

    You can change it, distribute, do whatever you like, I don't mind.
    But if you improve it, you could share it back ;)

    To use, you have to export variables to use aws-cli, like `AWS_PROFILE`, `AWS_DEFAULT_REGION`, etc. Then just call the script using `watch` (or however you like) and giving the RDS instance name:

    watch ./rds-top.sh <your instance name>

    Requirements:

    * Before using it, you need to enable enhanced monitoring on your RDS instance.

    * You need to install [aws-cli](https://github.com/aws/aws-cli) and [jq](https://stedolan.github.io/jq/).
    91 changes: 76 additions & 15 deletions rds-top.sh
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,87 @@
    #!/bin/sh
    #!/bin/bash
    # rds-top.sh
    # by <[email protected]>, 20190822
    # based on the work of Matheus de Oliveira <[email protected]>
    #
    # Usage:
    # ./rds-top.sh rds-instance
    # ./rds-top.sh --start-time=$(date -v-13d +%s) rds-instance

    # Author: Matheus de Oliveira <[email protected]>
    # Gist: https://gist.github.com/matheusoliveira/0e9b13d2fca6e7ab993c03e946806503
    set -e

    function usage()
    {
    cat << EOF
    Usage: $0 [options] rds_instance_id
    OPTIONS:
    -s or --start-time=t Optional: Specify the start time in seconds since the Unix epoch
    EOF
    }


    if [ $# -eq 0 ]
    then
    usage
    exit
    fi


    for i in "$@"
    do
    case $i in
    -s=*|--start-time=*)
    START_TIME="${i#*=}"
    shift # past argument=value
    ;;
    *)
    # unknown option
    ;;
    esac
    done
    INSTANCE_ID="$1"


    if [[ "$INSTANCE_ID" == "" ]];
    then
    error "You have to supply an instance ID!";
    usage;
    exit;
    fi;

    instanceid="$1"

    resourceid="$( aws rds describe-db-instances | jq -r '.DBInstances[] | select(.DBInstanceIdentifier == "'${instanceid}'") | .DbiResourceId' )"
    # Get resource id
    resource_id="$(aws rds describe-db-instances --db-instance-identifier "$INSTANCE_ID" --output json | jq -r '.DBInstances[].DbiResourceId')"
    [[ "$resource_id" == "" ]] && exit 1

    message_json="$( aws logs get-log-events --log-group-name RDSOSMetrics --log-stream-name "${resourceid}" --limit 1 | jq -r '"\(.events[].message)"' )"
    echo "${message_json}" | jq -r \
    '"\(.instanceID) - \(.timestamp) - \(.uptime) up, load average: \(.loadAverageMinute.one), \(.loadAverageMinute.five), \(.loadAverageMinute.fifteen)
    # Build logs parameters
    params=(--log-group-name RDSOSMetrics --log-stream-name "$resource_id" --limit 1 --output json)
    if [[ "$START_TIME" =~ ^[[:digit:]]+$ ]];
    then
    params+=(--start-time "$((START_TIME * 1000))" --start-from-head)
    fi

    # Get log events
    message_json=$(aws logs get-log-events "${params[@]}" | jq -r '.events[].message')

    echo "${message_json}" |
    jq -r '"\(.instanceID) - \(.timestamp) - \(.uptime) up, load average: \(.loadAverageMinute.one), \(.loadAverageMinute.five), \(.loadAverageMinute.fifteen)
    Tasks: \(.tasks.total) total, \(.tasks.running) running, \(.tasks.sleeping) sleeping, \(.tasks.stopped) stopped, \(.tasks.zombie) zombie
    %Cpu(s): \(.cpuUtilization.user) us, \(.cpuUtilization.system) sy, \(.cpuUtilization.nice) ni, \(.cpuUtilization.idle) id, \(.cpuUtilization.wait) wa, \(.cpuUtilization.steal) st
    MiB Mem: \(.memory.total / 1024) total, \(.memory.free / 1024) free, \((.memory.total - .memory.free) / 1024) used, \((.memory.cached + .memory.buffers) / 1024) buff/cache
    MiB Swap: \(.swap.total / 1024) total, \(.swap.free / 1024) free, \(.swap.cached) cached"'
    echo "${message_json}" | jq -r '.network[] | "Net \(.interface): \(.rx) rx, \(.tx) tx"'

    echo

    echo "${message_json}" |
    jq -r '.network[] | "Net \(.interface): \(.rx) rx, \(.tx) tx"'

    # Mimic `iostat -x`
    echo "${message_json}" | jq -r '.diskIO[] | "Disk \(.device): \(.tps) tps, \(.rrqmPS) rrqm/s, \(.wrqmPS) wrqm/s, \(.writeKbPS) wKB/S, \(.readKbPS) rKB/S, \(.avgReqSz) avgrq-sz, \(.avgQueueLen) avgqu-sz, \(.await) await, \(.util) %util"'
    echo "${message_json}" |
    jq -r '.diskIO[] | "Disk \(.device): \(.tps) tps, \(.rrqmPS) rrqm/s, \(.wrqmPS) wrqm/s, \(.writeKbPS) wKB/S, \(.readKbPS) rKB/S, \(.avgReqSz) avgrq-sz, \(.avgQueueLen) avgqu-sz, \(.await) await, \(.util) %util"'

    echo
    tab="$( echo -ne "\t")"
    {
    echo -e "PID\tVSS\tRSS\t%CPU\t%MEM\tCOMMAND"
    echo "${message_json}" | jq -r '.processList[] | "\(.id)\t\(.vss)\t\(.rss)\t\(.cpuUsedPc)\t\(.memoryUsedPc)\t\(.name)"' | sort -nrk 4
    } | column -t -s "${tab}"

    echo "${message_json}" |
    jq -r '["PID", "PPID", "VSS", "RSS", "%CPU", "%MEM", "COMMAND"], (.processList | sort_by(.cpuUsedPc) | reverse[] | [.id, .parentID, .vss, .rss, .cpuUsedPc, .memoryUsedPc, .name]) | @tsv' |
    column -t -s $'\t'
  4. @matheusoliveira matheusoliveira revised this gist Mar 29, 2018. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,8 @@ To use, you have to export variables to use aws-cli, like `AWS_PROFILE`, `AWS_DE

    watch ./rds-top.sh <your instance name>

    Before using it, you need to enable enhanced monitoring on your RDS instance.
    Requirements:

    * Before using it, you need to enable enhanced monitoring on your RDS instance.

    * You need to install [aws-cli](https://github.com/aws/aws-cli) and [jq](https://stedolan.github.io/jq/).
  5. @matheusoliveira matheusoliveira revised this gist Mar 29, 2018. 2 changed files with 3 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    This is a very simple script, so don't expect something great.

    You can change it, distribute, do whatever you like, I don't mind ;)
    You can change it, distribute, do whatever you like, I don't mind.
    But if you improve it, you could share it back ;)

    To use, you have to export variables to use aws-cli, like `AWS_PROFILE`, `AWS_DEFAULT_REGION`, etc. Then just call the script using `watch` (or however you like) and giving the RDS instance name:

    1 change: 1 addition & 0 deletions rds-top.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/bin/sh

    # Author: Matheus de Oliveira <[email protected]>
    # Gist: https://gist.github.com/matheusoliveira/0e9b13d2fca6e7ab993c03e946806503

    instanceid="$1"

  6. @matheusoliveira matheusoliveira created this gist Mar 29, 2018.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    This is a very simple script, so don't expect something great.

    You can change it, distribute, do whatever you like, I don't mind ;)

    To use, you have to export variables to use aws-cli, like `AWS_PROFILE`, `AWS_DEFAULT_REGION`, etc. Then just call the script using `watch` (or however you like) and giving the RDS instance name:

    watch ./rds-top.sh <your instance name>

    Before using it, you need to enable enhanced monitoring on your RDS instance.
    25 changes: 25 additions & 0 deletions rds-top.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/bin/sh

    # Author: Matheus de Oliveira <[email protected]>

    instanceid="$1"

    resourceid="$( aws rds describe-db-instances | jq -r '.DBInstances[] | select(.DBInstanceIdentifier == "'${instanceid}'") | .DbiResourceId' )"

    message_json="$( aws logs get-log-events --log-group-name RDSOSMetrics --log-stream-name "${resourceid}" --limit 1 | jq -r '"\(.events[].message)"' )"
    echo "${message_json}" | jq -r \
    '"\(.instanceID) - \(.timestamp) - \(.uptime) up, load average: \(.loadAverageMinute.one), \(.loadAverageMinute.five), \(.loadAverageMinute.fifteen)
    Tasks: \(.tasks.total) total, \(.tasks.running) running, \(.tasks.sleeping) sleeping, \(.tasks.stopped) stopped, \(.tasks.zombie) zombie
    %Cpu(s): \(.cpuUtilization.user) us, \(.cpuUtilization.system) sy, \(.cpuUtilization.nice) ni, \(.cpuUtilization.idle) id, \(.cpuUtilization.wait) wa, \(.cpuUtilization.steal) st
    MiB Mem: \(.memory.total / 1024) total, \(.memory.free / 1024) free, \((.memory.total - .memory.free) / 1024) used, \((.memory.cached + .memory.buffers) / 1024) buff/cache
    MiB Swap: \(.swap.total / 1024) total, \(.swap.free / 1024) free, \(.swap.cached) cached"'
    echo "${message_json}" | jq -r '.network[] | "Net \(.interface): \(.rx) rx, \(.tx) tx"'
    # Mimic `iostat -x`
    echo "${message_json}" | jq -r '.diskIO[] | "Disk \(.device): \(.tps) tps, \(.rrqmPS) rrqm/s, \(.wrqmPS) wrqm/s, \(.writeKbPS) wKB/S, \(.readKbPS) rKB/S, \(.avgReqSz) avgrq-sz, \(.avgQueueLen) avgqu-sz, \(.await) await, \(.util) %util"'

    echo
    tab="$( echo -ne "\t")"
    {
    echo -e "PID\tVSS\tRSS\t%CPU\t%MEM\tCOMMAND"
    echo "${message_json}" | jq -r '.processList[] | "\(.id)\t\(.vss)\t\(.rss)\t\(.cpuUsedPc)\t\(.memoryUsedPc)\t\(.name)"' | sort -nrk 4
    } | column -t -s "${tab}"