1 # Copyright 2019 Spirent Communications.
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.
16 Collects information using various command line tools.
19 #from tools.collectors.collector import collector
23 from collections import OrderedDict
24 from tools import tasks
25 from tools.collectors.collector import collector
26 from conf import settings
28 class MultiCmd(collector.ICollector):
29 """ Multiple command-line controllers
30 collectd, prox, crond, filebeat
32 def __init__(self, results_dir, test_name):
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
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()
57 def cleanup_collectd_metrics(self):
59 Cleaup the old or archived metrics
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)
66 # Command-1: Start Collectd
67 self.collectd_pid = tasks.run_background_task(
68 ['sudo', self.collectd_cmd],
69 self.logger, 'Staring Collectd')
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,
81 # Command-3: Start CROND
82 tasks.run_task(['sudo', 'systemctl', 'start', 'crond'],
83 self.logger, 'Staring CROND', True)
86 tasks.run_task(['sudo', 'systemctl', 'start', 'filebeat'],
87 self.logger, 'Starting BEATS', True)
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)
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()
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)
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)
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)
124 tasks.run_task(['sudo', 'systemctl', 'stop', 'filebeat'],
125 self.logger, 'Stopping BEATS', True)
127 def get_results(self):
133 def print_results(self):
137 logging.info("Multicmd Output is not collected by VSPERF")
138 logging.info("Please refer to corresponding command's output")