1 # Copyright 2015 Intel Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """linux-metrics system statistics model.
17 Provides linux-metrics system statistics generic "helper" functions.
19 This requires the following setting in your config:
21 * SYSMETRICS_LINUX_METRICS_CPU_SAMPLES_INTERVAL
22 Number of seconds in between samples to take for CPU percentages
24 If this doesn't exist, the application will raise an exception
31 from conf import settings
32 from tools.collectors.collector import collector
33 from linux_metrics import cpu_stat, mem_stat
35 _ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
37 class LinuxMetrics(collector.ICollector):
38 """A logger based on the linux-metrics module.
40 Currently it supports the logging of memory and CPU statistics
43 self._logger = logging.getLogger(__name__)
44 self._num_samples = settings.getValue(
45 'SYSMETRICS_LINUX_METRICS_CPU_SAMPLES_INTERVAL')
49 def log_mem_stats(self):
50 """See ICollector for descripion
52 self._mem_stats = mem_stat.mem_stats()
53 # pylint: disable=unbalanced-tuple-unpacking
54 mem_active, mem_total, mem_cached, mem_free, swap_total, swap_free = \
56 self._logger.info('%s mem_active: %s, mem_total: %s, mem_cached: %s, '
57 'mem_free: %s, swap_total: %s, swap_free: %s',
59 mem_active, mem_total, mem_cached, mem_free,
60 swap_total, swap_free)
61 return self._mem_stats
63 def log_cpu_stats(self):
64 """See ICollector for descripion
66 self._cpu_stats = cpu_stat.cpu_percents(self._num_samples)
67 self._logger.info('%s user: %.2f%%, nice: %.2f%%, system: %.2f%%, '
68 'idle: %.2f%%, iowait: %.2f%%, irq: %.2f%%, '
71 self._cpu_stats['user'],
72 self._cpu_stats['nice'],
73 self._cpu_stats['system'],
74 self._cpu_stats['idle'],
75 self._cpu_stats['iowait'],
76 self._cpu_stats['irq'],
77 self._cpu_stats['softirq'])
78 return self._cpu_stats