framework: Add reworked framework to repo
[vswitchperf.git] / tools / collectors / sysmetrics / linuxmetrics.py
1 # Copyright 2015 Intel Corporation.
2 #
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
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 """linux-metrics system statistics model.
16
17 Provides linux-metrics system statistics generic "helper" functions.
18
19 This requires the following setting in your config:
20
21 * SYSMETRICS_LINUX_METRICS_CPU_SAMPLES_INTERVAL
22     Number of seconds in between samples to take for CPU percentages
23
24 If this doesn't exist, the application will raise an exception
25 (EAFP).
26 """
27
28
29 import logging
30 import os
31 from conf import settings
32 from tools.collectors.collector import collector
33 from linux_metrics import cpu_stat, mem_stat
34
35 _ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
36
37 class LinuxMetrics(collector.ICollector):
38     """A logger based on the linux-metrics module.
39
40     Currently it supports the logging of memory and CPU statistics
41     """
42     def __init__(self):
43         self._logger = logging.getLogger(__name__)
44         self._num_samples = settings.getValue(
45             'SYSMETRICS_LINUX_METRICS_CPU_SAMPLES_INTERVAL')
46         self._mem_stats = []
47         self._cpu_stats = []
48
49     def log_mem_stats(self):
50         """See ICollector for descripion
51         """
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 = \
55             self._mem_stats
56         self._logger.info('%s mem_active: %s, mem_total: %s, mem_cached: %s, '
57                           'mem_free: %s, swap_total: %s, swap_free: %s',
58                           collector.CMD_PREFIX,
59                           mem_active, mem_total, mem_cached, mem_free,
60                           swap_total, swap_free)
61         return self._mem_stats
62
63     def log_cpu_stats(self):
64         """See ICollector for descripion
65         """
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%%, '
69                           'softirq: %.2f%%',
70                           collector.CMD_PREFIX,
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
79