Skip to content

Instantly share code, notes, and snippets.

@matheusoliveira
Last active April 6, 2023 16:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save matheusoliveira/0e9b13d2fca6e7ab993c03e946806503 to your computer and use it in GitHub Desktop.
Save matheusoliveira/0e9b13d2fca6e7ab993c03e946806503 to your computer and use it in GitHub Desktop.
Get AWS RDS Enhanced Monitoring statistics from CloudWatch and show something similar to Linux top command

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 and jq.

#!/bin/sh
# Author: Matheus de Oliveira <matioli.matheus@gmail.com>
# Gist: https://gist.github.com/matheusoliveira/0e9b13d2fca6e7ab993c03e946806503
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}"
@sherwind
Copy link

Thanks for sharing your work, @matheusoliveira!

Here's a fork which adds options to sort-by-mem and to specify start-time:

https://gist.github.com/sherwind/962fabb187769517e93a0ac57bf88f4e

# 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$'

@calh
Copy link

calh commented Jun 16, 2022

@sherwind and @matheusoliveira, I've forked, fixed a few bugs, and added a couple features:

https://gist.github.com/calh/65c358694227aade156a852a7962f772

@calh
Copy link

calh commented Jul 12, 2022

@sherwind and @matheusoliveira, a real time display wasn't quite giving me enough history to investigate what I needed. I created a Lambda script which pulls the RDSOSMetrics data, and re-publishes custom CloudWatch metrics.

Check out my project here!

https://github.com/calh/RDSOSMetrics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment