List memory usage for all containers indvidually

Hello,

I am aware that it is possible to list memory per container using ‘lxc info container’. Is it possible to get the memory used for all containers. What should be flag to use with lxc list -c ?

Thanks

So there’s currently no nice way to get the memory usage. It would be a pretty trivial change to add a column for it though.

Until then, you can get it with something like:

lxc list --format=json | jq '.[] | "\(.name) \(.state.memory.usage)"'

Memory and disk usage would definitely be very useful columns to add to lxc list.

Note that the ‘very trivial’ remark of @stgraber for memory use could be upgraded to ‘horribly difficult to do right’ for disk usage.

Thanks, another version removing stopped containers and changing formatting:

lxc list --format=json | jq  '.[] | "\(.name) \(.status) \(.state.memory.usage)"' | tr -d "\"" | grep -v Stopped | awk '{ printf "%-30s %10d\n", $1, $3/1000000 }'

God … keep up :roll_eyes: :stuck_out_tongue_winking_eye:

1 Like

can you explain how you did that please? the way above that @stgraber showed is not human readable (MB/GB)

Google “Convert bytes to Human Readable in MY_FAVE_PROGRAMMING_LANGUAGE”

Right, in Go, we do it with this function https://github.com/lxc/lxd/blob/master/shared/units/units.go#L179

Most languages will have some library that can handle that for you.

1 Like

you’re really helping with the popularity of LXD

1 Like

just to catchup what @gpatel-fr suggested but in human readable with memory, disk usage, cpu time:

alias lxcstats="printf \"%-20s %-8s %-5s %-5s %-s\n\" name status mem disk cpu-time; lxc list status=running --format=json | \
  jq  -r '.[] | \"\(.name) \(.status) \(.state.memory.usage) \(.state.disk.root.usage) \(.state.cpu.usage)\"' | \
  numfmt --field=3,4 --to=iec|\
  printf '%-20s %-8s %-5s %-5s %(%-dd %-Hh %-Mm )T\n' \$(</dev/stdin)"