Add LXC swap metric collector and fix lxc bug

LXC plugin throw up a exception when try collect cpu metrics. This
patch fix it (tests are passing) and add swap collector.

Change-Id: I3b12ac6ce199006bc1e024d2b2626657519e4f0b
Story: 2001563
Task: 6507
This commit is contained in:
Eduardo 2018-02-20 09:24:52 -03:00
parent e76cb28897
commit 1133a0a04f
4 changed files with 54 additions and 10 deletions

View File

@ -16,5 +16,6 @@ instances:
- container: all
cpu: True
mem: True
swap: True
blkio: True
net: True

View File

@ -1852,6 +1852,7 @@ instances:
- container: all
cpu: True
mem: True
swap: True
blkio: True
net: True
```
@ -1860,6 +1861,11 @@ The LXC checks return the following metrics:
| Metric Name | Dimensions | Semantics |
| ----------- | ---------- | --------- |
| blkio.read | hostname, container_name, service=lxc | number of bytes read from the disk to the cgroup(container) |
| blkio.write | hostname, container_name, service=lxc | number of bytes written from the cgroup(container) to the disk |
| blkio.async | hostname, container_name, service=lxc | number of asynchronous bytes |
| blkio.sync | hostname, container_name, service=lxc | number of synchronous bytes |
| blkio.total | hostname, container_name, service=lxc | total number of bytes |
| cpuacct.usage | hostname, container_name, service=lxc | reports the total CPU time (in nanoseconds) consumed |
| cpuacct.usage_percpu.cpu{X} | hostname, container_name, service=lxc | reports the total CPU time (in nanoseconds) consumed by cpu X |
| cpuacct.user | hostname, container_name, service=lxc| CPU time consumed by tasks in user mode. Unit defined by the USER_HZ variable |
@ -1877,6 +1883,8 @@ The LXC checks return the following metrics:
| memory.unevictable | hostname, container_name, service=lxc | memory that cannot be reclaimed, in bytes |
| memory.hierarchical_memory_limit | hostname, container_name, service=lxc | memory limit for the hierarchy that contains the memory cgroup, in bytes |
| memory.hierarchical_memsw_limit | hostname, container_name, service=lxc | memory plus swap limit for the hierarchy that contains the memory cgroup, in bytes |
| memory.usage_in_bytes | hostname, container_name, service=lxc | memory usage, in bytes |
| memory.memsw.usage_in_bytes | hostname, container_name, service=lxc | swap memory usage, in bytes |
| net.rx.bytes | hostname, container_name, service=lxc, iface | number of received bytes |
| net.rx.packets | hostname, container_name, service=lxc, iface | number of received packets |
| net.rx.errs | hostname, container_name, service=lxc, iface | number of received error packets |
@ -1893,11 +1901,7 @@ The LXC checks return the following metrics:
| net.tx.frame | hostname, container_name, service=lxc, iface | number of transferred frame packets |
| net.tx.compressed | hostname, container_name, service=lxc, iface| number of transferred compressed bytes |
| net.tx.multicast | hostname, container_name, service=lxc, iface | number of transferred multicast packets |
| blkio.read | hostname, container_name, service=lxc | number of bytes read from the disk to the cgroup(container) |
| blkio.write | hostname, container_name, service=lxc | number of bytes written from the cgroup(container) to the disk |
| blkio.async | hostname, container_name, service=lxc | number of asynchronous bytes |
| blkio.sync | hostname, container_name, service=lxc | number of synchronous bytes |
| blkio.total | hostname, container_name, service=lxc | total number of bytes |
| running_containers| hostname, service=lxc | number of running containers |
## Mcache
See [the example configuration](https://github.com/openstack/monasca-agent/blob/master/conf.d/mcache.yaml.example) for how to configure the Mcache plugin.

View File

@ -26,17 +26,20 @@ _LXC_DISK_REGEX = re.compile(r'(\w+)\s(\d+)')
class LXC(checks.AgentCheck):
"""Agent to collect LXC cgroup information
"""Agent to collect LXC cgroup information.
The information is mostly based on cgroup files of each container
The information is mostly based on cgroup files of each container.
"""
def check(self, instance):
self.instance = instance
self.containers = self._containers_name()
self.increment('running_containers', len(self.containers),
{'service': 'lxc'})
for container_name in self.containers:
self._collect_cpu_metrics(container_name)
self._collect_mem_metrics(container_name)
self._collect_swap_metrics(container_name)
self._collect_net_metrics(container_name)
self._collect_disk_metrics(container_name)
@ -44,7 +47,8 @@ class LXC(checks.AgentCheck):
container_name = self.instance.get('container')
if container_name == 'all':
return [name for name in os.listdir(_LXC_CGROUP_CPU_PWD)
if os.path.isdir(_LXC_CGROUP_CPU_PWD + name)]
if os.path.isdir('{0}/{1}'.format(_LXC_CGROUP_CPU_PWD,
name))]
if os.path.isdir('{0}/{1}'.format(_LXC_CGROUP_CPU_PWD,
container_name)):
@ -71,6 +75,15 @@ class LXC(checks.AgentCheck):
for metric, value in metrics.iteritems():
self.gauge(metric, value, dimensions=mem_dimensions)
def _collect_swap_metrics(self, container_name):
if not self.instance.get('swap', True):
return
metrics = self._get_swap_metrics(container_name)
if metrics:
swap_dimensions = self._get_dimensions(container_name)
for metric, value in metrics.iteritems():
self.gauge(metric, value, dimensions=swap_dimensions)
def _collect_net_metrics(self, container_name):
if not self.instance.get('net', True):
return
@ -110,7 +123,7 @@ class LXC(checks.AgentCheck):
return metrics
def _get_mem_metrics(self, container_name):
"""Get metrics from memory.stat cgroup file
"""Get metrics from memory.stat and memory cgroup file
:returns: a dictionary containing memory metrics defined on
container cgroup
@ -118,6 +131,30 @@ class LXC(checks.AgentCheck):
mem_cgroup = '{0}/{1}/'.format(_LXC_CGROUP_MEM_PWD, container_name)
metrics = self._get_metrics_by_file(mem_cgroup + 'memory.stat',
'memory')
# Get others cgroup memory values
with open('{0}/memory.usage_in_bytes'.format(mem_cgroup)) as mem_file:
metrics['memory.usage_in_bytes'] = int(mem_file.read())
return metrics
def _get_swap_metrics(self, container_name):
"""Get swap metrics from memory cgroup file.
If swapaccount is defined, you can control swap memory. To active swap
memory control, set GRUB_CMDLINE_LINUX_DEFAULT="swapaccount=1" on
/etc/default/grub file and restart the machine.
:returns: a dictionary containing swap metrics defined on
container cgroup
"""
metrics = {}
swap_file = '{0}/{1}/memory.memsw.usage_in_bytes'.format(_LXC_CGROUP_MEM_PWD,
container_name)
if os.path.isfile(swap_file):
with open(swap_file) as mem_file:
metrics['memory.memsw.usage_in_bytes'] = int(mem_file.read())
else:
self.log.error('Swap cgroup fine not found. '
'Verify if swapaccount control is enable')
return metrics
def _get_net_metrics(self, container_name):
@ -189,7 +226,8 @@ class LXC(checks.AgentCheck):
def _get_dimensions(self, container_name, options=None):
dimensions = {'container_name': container_name,
'service': 'lxc'}
dimensions.update(options)
if options:
dimensions.update(options)
return self._set_dimensions(dimensions, self.instance)
def _get_pid_container(self, container_name):

View File

@ -52,6 +52,7 @@ class LXC(monasca_setup.detection.Plugin):
'state': True,
'cpu': True,
'mem': True,
'swap': True,
'blkio': True,
'net': True
}]}