Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / 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
23     def __init__(self):
24         self.netns_exec_str = "ip netns exec %s "
25         self.vnstat_cmd_str = "vnstat -l -i %s"
26         self.child_process = {}
27
28     def run_vnstat(self, device, namespace=None):
29         cmd = self.vnstat_cmd_str
30         if namespace:
31             cmd1 = (self.netns_exec_str + "ifconfig %s") % (namespace, device)
32             check_call(cmd1, shell=True)
33             cmd = self.netns_exec_str + cmd
34             cmd = cmd % (namespace, device)
35         else:
36             cmd = cmd % device
37         check_call("which vnstat", shell=True)
38         child = my_popen(cmd.split(), stdout=subprocess.PIPE)
39         self.child_process[child.pid] = child
40         return child.pid
41
42     def kill_vnstat(self, pid, namespace=None):
43         assert pid in self.child_process
44         os.kill(pid, SIGINT)
45         process = self.child_process.pop(pid)
46         out = process.stdout.read()
47         process.wait()
48         LOG.info("os.kill(pid = %s)", pid)
49         data = {'tool': 'vnstat', 'type': 'nic', 'raw_data': out}
50         return data
51
52     def clean(self):
53         for _, process in self.child_process.items():
54             process.kill()
55             process.wait()
56             LOG.info("process.kill(vnstat:%s)", process.pid)
57         self.child_process = {}
58         return True
59
60     def process(self, raw):
61         buf = raw.splitlines()
62         buf = buf[9:]
63         buf = ' '.join(buf)
64         m = {}
65
66         digits = re.compile(r"\d+\.?\d*")
67         units = re.compile(
68             "(?:gib|mib|kib|kbit/s|gbits/s|mbit/s|p/s)",
69             re.IGNORECASE | re.MULTILINE)
70         units_arr = units.findall(buf)
71
72         LOG.debug(units_arr)
73
74         digits_arr = digits.findall(buf)
75
76         for i in range(len(digits_arr)):
77             digits_arr[i] = round(float(digits_arr[i]), 2)
78
79         m['rxpck'], m['txpck'] = digits_arr[8], digits_arr[9]
80         m['time'] = digits_arr[-1]
81         digits_arr = digits_arr[:8] + digits_arr[10:-1]
82         index = 0
83         for unit in units_arr:
84             unit = unit.lower()
85             if unit == 'gib':
86                 digits_arr[index] *= 1024
87             elif unit == 'kib':
88                 digits_arr[index] /= 1024
89             elif unit == 'gbit/s':
90                 digits_arr[index] *= 1000
91             elif unit == 'kbit/s':
92                 digits_arr[index] /= 1000
93             else:
94                 pass
95             index += 1
96
97         for i in range(len(digits_arr)):
98             digits_arr[i] = round(digits_arr[i], 2)
99
100         m['rxmB'], m['txmB'] = digits_arr[0:2]
101         m['rxmB_max/s'], m['txmB_max/s'] = digits_arr[2:4]
102         m['rxmB/s'], m['txmB/s'] = digits_arr[4:6]
103         m['rxmB_min/s'], m['txmB_min/s'] = digits_arr[6:8]
104         m['rxpck_max/s'], m['txpck_max/s'] = digits_arr[8:10]
105         m['rxpck/s'], m['txpck/s'] = digits_arr[10:12]
106         m['rxpck_min/s'], m['txpck_min/s'] = digits_arr[12:14]
107         return m
108
109     def force_clean(self):
110         LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
111         kill_by_name("vnstat")
112         self.child_process = {}
113         return True