[lxc-devel] [PATCH] lxc-ls: Add a few new columns
Stéphane Graber
stgraber at ubuntu.com
Thu Feb 13 17:54:29 UTC 2014
On Thu, Feb 13, 2014 at 12:20:20PM -0500, Michael H. Warfield wrote:
> On Thu, 2014-02-13 at 11:56 -0500, Stéphane Graber wrote:
> > This adds support for:
> > - memory (total memory)
> > - ram
> > - swap
>
> Any chance of getting the number of processes running in the container
> (it's something I've used in the past to detect a container stuck or
> hung on init with 1 process).
>
> Just a thought.
>
> Regards,
> Mike
I can't seem to find an easy way to grab that information from the API.
The easiest way that comes to mind is using the tasks file from one of
the cgroups but get_cgroup_item doesn't let me access it.
>
> > Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
> > ---
> > src/lxc/lxc-ls | 43 +++++++++++++++++++++++++++++++++++++++++--
> > 1 file changed, 41 insertions(+), 2 deletions(-)
> >
> > diff --git a/src/lxc/lxc-ls b/src/lxc/lxc-ls
> > index fa53fac..c65ae81 100755
> > --- a/src/lxc/lxc-ls
> > +++ b/src/lxc/lxc-ls
> > @@ -111,7 +111,8 @@ def getSubContainers(container):
> > return None
> >
> > # Constants
> > -FIELDS = ("name", "state", "ipv4", "ipv6", "autostart", "pid")
> > +FIELDS = ("name", "state", "ipv4", "ipv6", "autostart", "pid",
> > + "memory", "ram", "swap")
> >
> > # Begin parsing the command line
> > parser = argparse.ArgumentParser(description=_("LXC: List containers"),
> > @@ -254,6 +255,45 @@ for container_name in lxc.list_containers(config_path=nest_lxcpath):
> > except KeyError:
> > pass
> >
> > + if 'memory' in args.fancy_format or \
> > + 'ram' in args.fancy_format or \
> > + 'swap' in args.fancy_format:
> > +
> > + if container.running:
> > + try:
> > + memory_total = int(container.get_cgroup_item(
> > + "memory.usage_in_bytes"))
> > + except:
> > + memory_total = 0
> > +
> > + try:
> > + memory_swap = int(container.get_cgroup_item(
> > + "memory.memsw.usage_in_bytes"))
> > + except:
> > + memory_swap = 0
> > + else:
> > + memory_total = 0
> > + memory_swap = 0
> > +
> > + if 'memory' in args.fancy_format:
> > + if container.running:
> > + entry['memory'] = "%sMB" % round(memory_total / 1048576, 2)
> > + else:
> > + entry['memory'] = "-"
> > +
> > + if 'ram' in args.fancy_format:
> > + if container.running:
> > + entry['ram'] = "%sMB" % round(
> > + (memory_total - memory_swap) / 1048576, 2)
> > + else:
> > + entry['ram'] = "-"
> > +
> > + if 'swap' in args.fancy_format:
> > + if container.running:
> > + entry['swap'] = "%sMB" % round(memory_swap / 1048576, 2)
> > + else:
> > + entry['swap'] = "-"
> > +
> > # Get the IPs
> > for family, protocol in {'inet': 'ipv4', 'inet6': 'ipv6'}.items():
> > if protocol in args.fancy_format or args.nesting:
> > @@ -263,7 +303,6 @@ for container_name in lxc.list_containers(config_path=nest_lxcpath):
> > entry[protocol] = state
> > continue
> >
> > - # FIXME: We should get get_ips working as non-root
> > if container.running:
> > if not SUPPORT_SETNS_NET:
> > entry[protocol] = 'UNKNOWN'
> > --
> > 1.9.rc1
> >
> > _______________________________________________
> > lxc-devel mailing list
> > lxc-devel at lists.linuxcontainers.org
> > http://lists.linuxcontainers.org/listinfo/lxc-devel
> >
>
> --
> Michael H. Warfield (AI4NB) | (770) 978-7061 | mhw at WittsEnd.com
> /\/\|=mhw=|\/\/ | (678) 463-0932 | http://www.wittsend.com/mhw/
> NIC whois: MHW9 | An optimist believes we live in the best of all
> PGP Key: 0x674627FF | possible worlds. A pessimist is sure of it!
>
> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-devel
--
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20140213/c17a9479/attachment.pgp>
More information about the lxc-devel
mailing list