Thats right, i saw the new lxc-top command that is available in 3.0 and i was kind of impressed.

Since i’m running version 2 and wanted something similar, i decided to go with a simple bash script, the data is collected via proc and the values are converted with some help from BC (arbitrary precision calculator language)

This is what it looks like

KMem is the the kernel memory, CPU Time is the combined system and user processing time.

#!/bin/bash

function show_time () {
    num=$1
    min=0
    hour=0
    day=0
    if((num>59));then
        ((sec=num%60))
        ((num=num/60))
        if((num>59));then
            ((min=num%60))
            ((num=num/60))
            if((num>23));then
                ((hour=num%24))
                ((day=num/24))
            else
                ((hour=num))
            fi
        else
            ((min=num))
        fi
    else
        ((sec=num))
    fi
    echo "$day"d "$hour"h "$min"m "$sec"s
}

printf "%6s  %6s  %6s  %-15s %s\n" "KMem" "Memory" "Limit" "CPU time" "Container"

for i in /sys/fs/cgroup/memory/lxc/*/
do
	name="$(basename $i)"
	mem="$(cat ${i}memory.usage_in_bytes | numfmt --to=iec)"
	kmem="$(cat ${i}memory.kmem.usage_in_bytes | numfmt --to=iec)"
	memlim="$(cat ${i}memory.limit_in_bytes | numfmt --to=iec)"
	rawcpu=$(cat /sys/fs/cgroup/cpuacct/lxc/${name}/cpuacct.usage)
	cpu=$(echo "scale=2;${rawcpu}/1000000000" | bc)
	cpusec=$((${rawcpu}/1000000000))
	cputime=$(show_time ${cpusec})
	printf "%6s  %6s  %6s  %-15s %s\n" "$kmem" "$mem" "$memlim" "${cputime}" "$name"
done

The show_time() function was found online somewhere.

Published by Nimpen J. Nordström

System Developer and Network Security Enthusiast