Skip to content

Instantly share code, notes, and snippets.

@nmaggioni
Created February 11, 2020 14:57
Show Gist options
  • Save nmaggioni/f7ec0fc3685988aeb1da1d42309c8201 to your computer and use it in GitHub Desktop.
Save nmaggioni/f7ec0fc3685988aeb1da1d42309c8201 to your computer and use it in GitHub Desktop.
List Proxmox LXC containers by memory usage
#!/bin/bash
TMPFILE="$(mktemp)"
PCTLIST="$(pct list | grep running | awk '{ print $1 }')"
PCTCOUNT="$(echo $PCTLIST | wc -w)"
i=0;
for id in $PCTLIST; do
i=$((i+1))
echo -ne "Analyzing $i/$PCTCOUNT...\r"
STATUS="$(pct status $id -verbose)"
NAME="$(echo $STATUS | grep -Po '\sname: \K\w+')"
MAXMEM="$(echo $STATUS | grep -Po '\smaxmem: \K[0-9]+')"
MEM="$(echo $STATUS | grep -Po '\smem: \K[0-9]+')"
MEMPCT=$(( ($MEM * 100) / $MAXMEM ))
echo "$id $NAME $(($MEM/1000000)) $(($MAXMEM/1000000)) ${MEMPCT}%" >> "$TMPFILE"
done
undl="$(printf '\u001b[4m')"
nrml="$(printf '\u001b[0m')"
awk '{ print substr($NF, 1, length($NF)-1) " " $0 }' "$TMPFILE" | sort -nr | cut -d' ' -f2- | sed "1i${undl}ID${nrml} ${undl}NAME${nrml} ${undl}USED_MB${nrml} ${undl}TOTAL_MB${nrml} ${undl}USED_PCT${nrml}" | column -t
rm "$TMPFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment