Collector: Add Multi-command collector.
[vswitchperf.git] / tools / collectors / multicmd / multicmd.py
1 # Copyright 2019 Spirent Communications.
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 """
16 Collects information using various command line tools.
17 """
18
19 #from tools.collectors.collector import collector
20 import glob
21 import logging
22 import os
23 from collections import OrderedDict
24 from tools import tasks
25 from tools.collectors.collector import collector
26 from conf import settings
27
28 class MultiCmd(collector.ICollector):
29     """ Multiple command-line controllers
30         collectd, prox, crond, filebeat
31     """
32     def __init__(self, results_dir, test_name):
33         """
34         initialize collectrs
35         """
36         self.prox_home = settings.getValue('MC_PROX_HOME')
37         self.collectd_cmd = settings.getValue('MC_COLLECTD_CMD')
38         self.collectd_csv = settings.getValue('MC_COLLECTD_CSV')
39         self.prox_out = settings.getValue('MC_PROX_OUT')
40         self.prox_cmd = settings.getValue('MC_PROX_CMD')
41         self.cron_out = settings.getValue('MC_CRON_OUT')
42         self.logger = logging.getLogger(__name__)
43         self.results_dir = results_dir
44         self.collectd_pid = 0
45         self.prox_pid = 0
46         self.cleanup_collectd_metrics()
47         self.logger.debug('%s', 'Multicmd data for '+ str(test_name))
48         # There should not be a file by name stop in prox_home folder
49         # Else Prox will start and stop immediately. This is a Hack to
50         # control prox-runrapid, which by default runs for specified duration.
51         filename = os.path.join(self.prox_home, 'stop')
52         if os.path.exists(filename):
53             tasks.run_task(['sudo', 'rm', filename],
54                            self.logger, 'deleting stop')
55         self.results = OrderedDict()
56
57     def cleanup_collectd_metrics(self):
58         """
59         Cleaup the old or archived metrics
60         """
61         for name in glob.glob(os.path.join(self.collectd_csv, '*')):
62             tasks.run_task(['sudo', 'rm', '-rf', name], self.logger,
63                            'Cleaning up Metrics', True)
64
65     def start(self):
66         # Command-1: Start Collectd
67         self.collectd_pid = tasks.run_background_task(
68             ['sudo', self.collectd_cmd],
69             self.logger, 'Staring Collectd')
70
71         # Command-2: Start PROX
72         working_dir = os.getcwd()
73         if os.path.exists(self.prox_home):
74             os.chdir(self.prox_home)
75             self.prox_pid = tasks.run_background_task(['sudo', self.prox_cmd,
76                                                        '--test', 'irq',
77                                                        '--env', 'irq'],
78                                                       self.logger,
79                                                       'Start PROX')
80         os.chdir(working_dir)
81         # Command-3: Start CROND
82         tasks.run_task(['sudo', 'systemctl', 'start', 'crond'],
83                        self.logger, 'Staring CROND', True)
84
85         # command-4: BEATS
86         tasks.run_task(['sudo', 'systemctl', 'start', 'filebeat'],
87                        self.logger, 'Starting BEATS', True)
88
89     def stop(self):
90         """
91         Stop All commands
92         """
93         # Command-1: COLLECTD
94         tasks.terminate_task_subtree(self.collectd_pid, logger=self.logger)
95         tasks.run_task(['sudo', 'pkill', '--signal', '2', 'collectd'],
96                        self.logger, 'Stopping Collectd', True)
97
98         # Backup the collectd-metrics for this test into a results folder
99         # results_dir = os.path.join(settings.getValue('RESULTS_PATH'), '/')
100         tasks.run_task(['sudo', 'cp', '-r', self.collectd_csv,
101                         self.results_dir], self.logger,
102                        'Copying Collectd Results File', True)
103         self.cleanup_collectd_metrics()
104
105         # Command-2: PROX
106         filename = os.path.join(self.prox_home, 'stop')
107         if os.path.exists(self.prox_home):
108             tasks.run_task(['sudo', 'touch', filename],
109                            self.logger, 'Stopping PROX', True)
110
111         outfile = os.path.join(self.prox_home, self.prox_out)
112         if os.path.exists(outfile):
113             tasks.run_task(['sudo', 'mv', outfile, self.results_dir],
114                            self.logger, 'Moving PROX-OUT file', True)
115
116         # Command-3: CROND
117         tasks.run_task(['sudo', 'systemctl', 'stop', 'crond'],
118                        self.logger, 'Stopping CROND', True)
119         if os.path.exists(self.cron_out):
120             tasks.run_task(['sudo', 'mv', self.cron_out, self.results_dir],
121                            self.logger, 'Move Cron Logs', True)
122
123         # Command-4: BEATS
124         tasks.run_task(['sudo', 'systemctl', 'stop', 'filebeat'],
125                        self.logger, 'Stopping BEATS', True)
126
127     def get_results(self):
128         """
129         Return results
130         """
131         return self.results
132
133     def print_results(self):
134         """
135         Print results
136         """
137         logging.info("Multicmd Output is not collected by VSPERF")
138         logging.info("Please refer to corresponding command's output")