-
Notifications
You must be signed in to change notification settings - Fork 75
/
multi.py
271 lines (225 loc) · 7.47 KB
/
multi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: utf-8 -*-
"""
Collection of running multiple processes from a minion in parallel, typically
for network related tasks such as ping and iperf.
"""
from __future__ import absolute_import
import logging
import multiprocessing.dummy
import multiprocessing
import re
import socket
from subprocess import Popen
# pylint: disable=import-error
log = logging.getLogger(__name__)
try:
from salt.utils.path import which
except ImportError:
from distutils.spawn import which
try:
# pylint: disable=import-error,3rd-party-module-not-gated,redefined-builtin
from salt.ext.six.moves import range
except ImportError:
logging.error("Could not import salt.ext.six.moves -> range")
IPERF_PATH = which('iperf3')
LOCALHOST_NAME = socket.gethostname()
'''
multi is the module to call subprocess in minion host
Ping is a simple test to check if point to point nodes are connected
CLI Example:
.. code-block:: bash
sudo salt 'node' multi.ping_cmd <hostname>|<ip>
sudo salt 'node' multi.ping <hostname>|<ip> <hostname>|<ip>....
'''
def _all(func, hosts):
'''
Internal function that allow function to perform in all hosts
'''
all_instances = []
# threads should likely scale with cores or interfaces
cpus = multiprocessing.cpu_count()
threads = 4 * cpus
log.debug('multi._all cpus count={}, thread count={}'.format(cpus, threads))
pool = multiprocessing.dummy.Pool(threads)
for instance in pool.map(func, hosts):
all_instances.append(instance)
return all_instances
def _summarize_iperf(result):
'''
Scan the results and summarize for iperf result
'''
# pylint: disable=invalid-name,unused-variable
host, rc, out, err = result
msg = {}
msg['server'] = host
if rc == 0:
msg['succeeded'] = True
msg['speed'] = out
try:
msg['filter'] = re.match(
r'.*0.00-10.00.*sec\s(.*Bytes)\s+(.*Mbits/sec)',
out, re.DOTALL).group(2)
except AttributeError:
msg['filter'] = '0 Mbits/sec'
msg['failed'] = False
msg['errored'] = False
if rc == 1:
msg['succeeded'] = False
msg['failed'] = True
msg['errored'] = False
if rc == 2:
msg['succeeded'] = False
msg['failed'] = False
msg['errored'] = True
return msg
def _summarize_ping(results):
'''
Scan the results and summarize
'''
success = []
failed = []
errored = []
slow = []
avg = []
for result in results:
# pylint: disable=invalid-name,unused-variable
host, rc, out, err = result
if rc == 0:
success.append(host)
rtt = re.match(
r'.*rtt min/avg/max/mdev = \d+\.?\d+/(\d+\.?\d+)/',
out, re.DOTALL)
if rtt:
avg.append({'avg': float(rtt.group(1)), 'host': host})
if rc == 1:
failed.append(host)
if rc == 2:
errored.append(host)
log.debug('multi._summarize_ping average={}'.format(avg))
if avg:
avg_sum = sum(i.get('avg') for i in avg) / len(avg)
if len(avg) > 2:
for i in avg:
if (avg_sum * len(avg) / 2) < i.get('avg'):
log.debug('_summarize_ping: slow host = {} avg = {}, s'.
format(i.get('avg'), avg_sum))
slow.append(i.get('host'))
else:
avg_sum = 0
msg = {}
msg['succeeded'] = len(success)
if failed:
msg['failed'] = " ".join(failed)
if errored:
msg['errored'] = " ".join(errored)
if slow:
msg['slow'] = " ".join(slow)
msg['avg'] = avg_sum
return msg
def iperf(server, cpu, port):
'''
iperf test to a specific server
CLI Example:
.. code-block:: bash
sudo salt 'node' multi.iperf <hostname>|<ip> <cpu_core> <port>
'''
log.debug('iperf server ={}'.format(server))
return _summarize_iperf(iperf_client_cmd(server, cpu, port))
def iperf_client_cmd(server, cpu=0, port=5200):
'''
Use iperf to test minion to server
CLI Example:
.. code-block:: bash
salt 'node' multi.iperf_client_cmd <server_name/ip>
cpu=<which_cpu_core default 0> port=<default 5200>
'''
if IPERF_PATH is None:
ret = [LOCALHOST_NAME, 2, "0",
"iperf3 not found in path, please install"]
elif not server:
ret = [LOCALHOST_NAME, 2, "0", "Server name is empty"]
else:
iperf_cmd = ["/usr/bin/iperf3", "-fm", "-A"+str(cpu),
"-t10", "-c"+server, "-p"+str(port)]
log.debug('iperf_client_cmd: cmd {}'.format(iperf_cmd))
retcode, stdout, stderr = __salt__['helper.run'](iperf_cmd)
ret = (server, retcode, stdout, stderr)
return ret
def iperf_server_cmd(cpu=0, port=5200):
'''
Use iperf to test minion to server
CLI Example:
.. code-block:: bash
salt 'node' multi.iperf_server_cmd <server_name/ip>
cpu=<which_cpu_core default 0> port=<default 5200>
'''
if IPERF_PATH is None:
return LOCALHOST_NAME + ": iperf3 not found in path, please install"
iperf_cmd = ["/usr/bin/iperf3", "-s", "-D", "-A"+str(cpu), "-p"+str(port)]
log.debug('iperf_server_cmd: cmd {}'.format(iperf_cmd))
Popen(iperf_cmd)
# it doesn't report fail so no need to check
return LOCALHOST_NAME + ": iperf3 started at cpu " + str(cpu) + " port " + str(port) + "\n"
def kill_iperf_cmd():
'''
Clean up all the iperf3 server and clean it.
'''
kill_cmd = ["/usr/bin/killall", "-9", "iperf3"]
log.debug('kill_iperf_cmd: cmd {}'.format(kill_cmd))
Popen(kill_cmd)
return True
def ping(*hosts):
'''
Ping a list of hosts and summarize the results
CLI Example:
.. code-block:: bash
sudo salt 'node' multi.ping <hostname>|<ip> <hostname>|<ip>....
'''
# I should be filter all the localhost here?
log.debug('ping hostlist={}'.format(list(hosts)))
results = _all(ping_cmd, list(hosts))
return _summarize_ping(results)
def ping_cmd(host):
'''
Ping a host with 1 packet and return the result
CLI Example:
.. code-block:: bash
sudo salt 'node' multi.ping_cmd <hostname>|<ip>
'''
cmd = ["/usr/bin/ping", "-c1", "-q", "-W1", host]
retcode, stdout, stderr = __salt__['helper.run'](cmd)
return host, retcode, stdout, stderr
def jumbo_ping(*hosts):
'''
Ping a list of hosts and summarize the results
CLI Example:
.. code-block:: bash
sudo salt 'node' multi.ping <hostname>|<ip> <hostname>|<ip>....
'''
# I should be filter all the localhost here?
log.debug('jumbo_ping hostlist={}'.format(list(hosts)))
results = _all(jumbo_ping_cmd, list(hosts))
return _summarize_ping(results)
def jumbo_ping_cmd(host):
'''
Ping a host with 1 packet and return the result
CLI Example:
.. code-block:: bash
sudo salt 'node' multi.ping_cmd <hostname>|<ip>
'''
cmd = ["/usr/bin/ping", "-Mdo", "-s8972", "-c1", "-q", "-W1", host]
log.debug('ping_cmd hostname={}'.format(host))
retcode, stdout, stderr = __salt__['helper.run'](cmd)
return host, retcode, stdout, stderr
def prepare_iperf_server():
'''
Create N server base on the total core number of your cpu count
CLI Example:
.. code-block:: bash
salt 'node' multi.prepare_iperf_server
'''
cpus = multiprocessing.cpu_count()
iperf_log = ""
for cpu in range(cpus):
iperf_log += iperf_server_cmd(cpu, 5200+cpu)
return iperf_log