JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / agent / perf / vnstat.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import subprocess
11 import time
12 import re
13 from signal import SIGINT
14 import os
15 import logging
16 from vstf.common.utils import check_call, my_popen, kill_by_name
17
18 LOG = logging.getLogger(__name__)
19
20
21 class VnStat(object):
22     def __init__(self):
23         self.netns_exec_str = "ip netns exec %s "
24         self.vnstat_cmd_str = "vnstat -l -i %s"
25         self.child_process = {}
26
27     def run_vnstat(self, device, namespace=None):
28         cmd = self.vnstat_cmd_str
29         if namespace:
30             cmd1 = (self.netns_exec_str + "ifconfig %s") % (namespace, device)
31             check_call(cmd1, shell=True)
32             cmd = self.netns_exec_str + cmd
33             cmd = cmd % (namespace, device)
34         else:
35             cmd = cmd % device
36         check_call("which vnstat", shell=True)
37         child = my_popen(cmd.split(), stdout=subprocess.PIPE)
38         self.child_process[child.pid] = child
39         return child.pid
40
41     def kill_vnstat(self, pid, namespace=None):
42         assert pid in self.child_process
43         os.kill(pid, SIGINT)
44         process = self.child_process.pop(pid)
45         out = process.stdout.read()
46         process.wait()
47         LOG.info("os.kill(pid = %s)", pid)
48         data = {'tool': 'vnstat', 'type': 'nic', 'raw_data': out}
49         return data
50
51     def clean(self):
52         for _, process in self.child_process.items():
53             process.kill()
54             process.wait()
55             LOG.info("process.kill(vnstat:%s)", process.pid)
56         self.child_process = {}
57         return True
58
59     def process(self, raw):
60         buf = raw.splitlines()
61         buf = buf[9:]
62         buf = ' '.join(buf)
63         m = {}
64
65         digits = re.compile(r"\d+\.?\d*")
66         units = re.compile("(?:gib|mib|kib|kbit/s|gbits/s|mbit/s|p/s)", re.IGNORECASE | re.MULTILINE)
67         units_arr = units.findall(buf)
68
69         LOG.debug(units_arr)
70
71         digits_arr = digits.findall(buf)
72
73         for i in range(len(digits_arr)):
74             digits_arr[i] = round(float(digits_arr[i]), 2)
75
76         m['rxpck'], m['txpck'] = digits_arr[8], digits_arr[9]
77         m['time'] = digits_arr[-1]
78         digits_arr = digits_arr[:8] + digits_arr[10:-1]
79         index = 0
80         for unit in units_arr:
81             unit = unit.lower()
82             if unit == 'gib':
83                 digits_arr[index] *= 1024
84             elif unit == 'kib':
85                 digits_arr[index] /= 1024
86             elif unit == 'gbit/s':
87                 digits_arr[index] *= 1000
88             elif unit == 'kbit/s':
89                 digits_arr[index] /= 1000
90             else:
91                 pass
92             index += 1
93
94         for i in range(len(digits_arr)):
95             digits_arr[i] = round(digits_arr[i], 2)
96
97         m['rxmB'], m['txmB'] = digits_arr[0:2]
98         m['rxmB_max/s'], m['txmB_max/s'] = digits_arr[2:4]
99         m['rxmB/s'], m['txmB/s'] = digits_arr[4:6]
100         m['rxmB_min/s'], m['txmB_min/s'] = digits_arr[6:8]
101         m['rxpck_max/s'], m['txpck_max/s'] = digits_arr[8:10]
102         m['rxpck/s'], m['txpck/s'] = digits_arr[10:12]
103         m['rxpck_min/s'], m['txpck_min/s'] = digits_arr[12:14]
104         return m
105
106     def force_clean(self):
107         LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
108         kill_by_name("vnstat")
109         self.child_process = {}
110         return True