Skip to content

Instantly share code, notes, and snippets.

@allanjamesvestal
Created May 4, 2021 01:21
Show Gist options
  • Select an option

  • Save allanjamesvestal/d8f81f27841a099649090adddd0a3d1d to your computer and use it in GitHub Desktop.

Select an option

Save allanjamesvestal/d8f81f27841a099649090adddd0a3d1d to your computer and use it in GitHub Desktop.
A simple util to print an EC2 instance's IP, along with disk and RAM usage stats.
#!/bin/zsh
SELF_IP=$(curl --silent http://checkip.amazonaws.com)
MEMORY_READING=$(free --bytes | grep 'Mem')
MEMORY_AVAILABLE=$(echo $MEMORY_READING | awk '{print $7}')
MEMORY_TOTAL=$(echo $MEMORY_READING | awk '{print $2}')
MEMORY_USED=$(echo "$MEMORY_TOTAL $MEMORY_AVAILABLE" | awk '{print($1 - $2)}')
MEMORY_PCT=$(
echo "$MEMORY_USED $MEMORY_TOTAL" | \
awk '{printf("%.2f\n", ($1 / $2) * 100)}' \
)
MEMORY_GIBI_USED=$(
echo "$MEMORY_USED" | awk '{printf("%.2f\n", ($1 / (2**30)))}' \
)
MEMORY_GIBI_TOTAL=$(
echo "$MEMORY_TOTAL" | awk '{printf("%.2f\n", ($1 / (2**30)))}' \
)
DISK_READING=$(df -B 1 /dev/xvda1 | tail -n+2)
DISK_USED=$(echo $DISK_READING | awk '{print $3}')
DISK_TOTAL=$(echo $DISK_READING | awk '{print $2}')
DISK_PCT=$(
echo "$DISK_USED $DISK_TOTAL" | \
awk '{printf("%.2f\n", ($1 / $2) * 100)}' \
)
DISK_GIBI_USED=$(
echo "$DISK_USED" | awk '{printf("%.2f\n", ($1 / (2**30)))}' \
)
DISK_GIBI_TOTAL=$(
echo "$DISK_TOTAL" | awk '{printf("%.2f\n", ($1 / (2**30)))}' \
)
OUTPUT_MESSAGE=$(cat << EOM
IP: $SELF_IP
-
SSD: $DISK_PCT% ($DISK_GIBI_USED / $DISK_GIBI_TOTAL GiB)
RAM: $MEMORY_PCT% ($MEMORY_GIBI_USED / $MEMORY_GIBI_TOTAL GiB)
EOM
)
OUTPUT_WRAPPED=$(echo $OUTPUT_MESSAGE | boxes -d columns -a c)
LINE_LENGTH=$(
echo $OUTPUT_WRAPPED | \
awk ' { if ( length > x ) { x = length; y = $0 } }END{ print length }'
)
echo "$LINE_LENGTH\n$OUTPUT_WRAPPED" | \
awk 'BEGIN{ RS = "" ; FS = "\n" }{for (i=2; i<=NF; i++) printf "%-*s\n", $1, $i}' | \
figlet -f term -c -w 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment