Skip to content

Instantly share code, notes, and snippets.

@tataranovich
Last active October 11, 2022 12:41
Show Gist options
  • Save tataranovich/2ea97830275ffc1e5d5c7cd2ea25750f to your computer and use it in GitHub Desktop.
Save tataranovich/2ea97830275ffc1e5d5c7cd2ea25750f to your computer and use it in GitHub Desktop.
Display LXC containers memory and swap information
#!/bin/bash
RAM_TOTAL=0
RAM_TOTAL_MAX=0
SWAP_TOTAL=0
SWAP_TOTAL_MAX=0
printf "%20s\t%11s\t%11s\n" "NAME" "RAM" "SWAP"
echo "-------------------------------------------------------"
for i in $(find /sys/fs/cgroup -mindepth 1 -maxdepth 1 -type d -name 'lxc.payload.*')
do
NAME=$(echo $i | sed -e 's@/sys/fs/cgroup/lxc.payload.@@')
RAM=$(( $(cat $i/memory.current) / 1024 / 1024 ))
RAM_MAX=$(cat $i/memory.max)
if [ "$RAM_MAX" != "max" ]; then
RAM_MAX=$(( $RAM_MAX / 1024 / 1024 ))
fi
SWAP=$(( $(cat $i/memory.swap.current ) / 1024 / 1024 ))
SWAP_MAX=$(cat $i/memory.swap.max)
if [ "$SWAP_MAX" != "max" ]; then
SWAP_MAX=$(( $SWAP_MAX / 1024 / 1024 ))
fi
printf "%20s\t%5s/%5s\t%5s/%5s\n" $NAME $RAM $RAM_MAX $SWAP $SWAP_MAX
RAM_TOTAL=$(( $RAM_TOTAL + $RAM ))
RAM_TOTAL_MAX=$(( $RAM_TOTAL_MAX + $RAM_MAX ))
SWAP_TOTAL=$(( $SWAP_TOTAL + $SWAP ))
SWAP_TOTAL_MAX=$(( $SWAP_TOTAL_MAX + $SWAP_MAX ))
done
echo "-------------------------------------------------------"
printf "%20s\t%5s/%5s\t%5s/%5s\n" "TOTAL" $RAM_TOTAL $RAM_TOTAL_MAX $SWAP_TOTAL $SWAP_TOTAL_MAX
@tataranovich
Copy link
Author

Example output

                NAME            RAM            SWAP
-------------------------------------------------------
              gitlab     2216/ 3072       255/  256
       gitlab-runner       88/  512        84/  128
             jenkins      999/ 1536       255/  256
             nagios4       24/  256        15/  128
           nextcloud      162/  512       114/  256
------------------------------------------------------
               TOTAL     3489/ 5888       723/ 1024

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