Get AWS RDS Enhanced Monitoring statistics from CloudWatch and show something similar to Linux top command
This file contains 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
#!/bin/bash | |
# rds-top.sh | |
# by <sherwin@daganato.com>, 20190822 | |
# based on the work of Matheus de Oliveira <matioli.matheus@gmail.com> | |
# | |
# 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 | |
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 | |
-m or --sort-by-mem Optional: Sorts output by memory. Default is to sort by CPU | |
EOF | |
} | |
if [ $# -eq 0 ] | |
then | |
usage | |
exit | |
fi | |
for i in "$@" | |
do | |
case $i in | |
-s=*|--start-time=*) | |
START_TIME="${i#*=}" | |
shift | |
;; | |
-m | --sort-by-mem ) | |
SORT_BY_MEM=1 | |
shift | |
;; | |
*) | |
# unknown option | |
;; | |
esac | |
done | |
INSTANCE_ID="$1" | |
if [[ "$INSTANCE_ID" == "" ]] | |
then | |
error "You have to supply an instance ID!" | |
usage | |
exit | |
fi | |
# 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 | |
# 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 | |
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 | |
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('"$sort_by"') | reverse[] | [.id, .parentID, .vss, .rss, .cpuUsedPc, .memoryUsedPc, .name]) | @tsv' | | |
column -t -s $'\t' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment