Change param when check-iperf

Change-Id: I25d88abc6b8e2a7b03178c017baf9f9268b1f7cf
This commit is contained in:
changzhi1990 2016-01-29 15:19:01 +08:00
parent a3f1f28545
commit d9c6423b0f
6 changed files with 251 additions and 109 deletions

View File

@ -55,27 +55,21 @@ Configuration File
On start the client will read a configuration file. By default the configuration file is located at /etc/steth/steth.conf.
Here is an example about the configuration file: ::
# (ListOpt) Order list of networks prefix.
# The first item is treated as a list.
# If multiple networks are used, we can be specified as s list.
# Specify the prefix of the networks to be used.
# The ending '.' -- specifier indicates the network range to be used.
# Example: "10.0.4.,192.168.10."
networks_prefix=127.0.0.,192.168.20.,1.1.1.
# (ListOpt) list of networks types.
# We may have multi network types in one node, such as mgmt, net and stroage.
# so this value should be a list.
# We seperate each item by ":". Treat first item as network type.
# The second is physical nic name. And the third is network_prefix.
# Example: "mgmt:eth0:1.1.1.,net:eth1:2.2.2.,storage:eth2:3.3.3."
network_types=mgmt:eth0:1.1.1.,net:eth1:2.2.2.,storage:eth2:3.3.3.
# (ListOpt) This is the identifier of the nodes in group of network nodes.
# Example: 64, 65, 66
network_agents_info=64,65,66
# (ListOpt) All nodes info. Just need sequence number.
# Example: 64, 65, 66
nodes_id=39,233,64,65,66
# (ListOpt) This is the identifier of the nodes in group of compute nodes.
# Example: 67, 68
compute_agents_info=67,68
# (StrOpt) Prefix to be used in naming every node. By default, this value
# is "server". We combine "node_name_prefix" with
# "network_agents_info", "compute_agents_info" to
# define nodes. Such as "server-64", "server-68" and so on.
# In every region, we give every node a specific name.
# Ensure that DNS can be resolved correctly.
# these names when doing iperf.
node_name_prefix=server-
# (StrOpt) Name prefix of every node. By default, this value
# is "server". We combine "node_name_prefix" with
# "nodes_id", to define nodes. Such as "server-64", "server-68"
# and so on. In every region, we give every node a specific name.
# Ensure that DNS can resolve the nodes.
node_name_prefix=server-

View File

@ -1,24 +1,19 @@
[DEFAULT]
# (ListOpt) list of networks types.
# We may have multi network types in one node, such as mgmt, net and stroage.
# so this value should be a list.
# We seperate each item by ":". Treat first item as network type.
# The second is physical nic name. And the third is network_prefix.
# Example: "mgmt:eth0:1.1.1.,net:eth1:2.2.2.,storage:eth2:3.3.3."
network_types=mgmt:eth0:1.1.1.,net:eth1:2.2.2.,storage:eth2:3.3.3.
# (ListOpt) Order list of networks prefix.
# We treat first item as managed network.
# We may have multi networks in one node, so this value should be a list.
# Prefix of network in every agent. End of '.'
# Example: "10.0.4.,192.168.10."
networks_prefix=127.0.0.,192.168.20.,1.1.1.
# (ListOpt) Network nodes info. Just need sequence number.
# (ListOpt) All nodes info. Just need sequence number.
# Example: 64, 65, 66
network_agents_info=64,65,66
# (ListOpt) Compute nodes info. Just need sequence number.
# Example: 67, 68
compute_agents_info=67,68
nodes_id=39,233,64,65,66
# (StrOpt) Name prefix of every node. By default, this value
# is "server". We combine "node_name_prefix" with
# "network_agents_info", "compute_agents_info" to
# define nodes. Such as "server-64", "server-68" and so on.
# In every region, we give every node a specific name.
# "nodes_id", to define nodes. Such as "server-64", "server-68"
# and so on. In every region, we give every node a specific name.
# Ensure that DNS can resolve the nodes.
node_name_prefix=server-

View File

@ -13,38 +13,97 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import socket
import sys
from oslo_config import cfg
OPTS = [
cfg.ListOpt('network_agents_info', default=[],
help="Mappings of network agents and steth listened IP."),
cfg.ListOpt('compute_agents_info', default=[],
help="Mappings of compute agents and steth listened IP."),
cfg.StrOpt('managed_network_prefix', default='127.0.0.',
help="Managed network prefix."),
cfg.ListOpt('networks_prefix', default=['127.0.0.', '192.168.10.'],
help="Networks prefix."),
cfg.ListOpt('network_types', default=[],
help="Mappings of network types and prefix of networks."),
cfg.ListOpt('nodes_id', default=[],
help="List of nodes."),
cfg.StrOpt('node_name_prefix', default='server-',
help="Prefix of every node."),
]
MGMT_AGENTS_INFOS = {}
NET_AGENTS_INFOS = {}
STORAGE_AGENTS_INFOS = {}
ROOTDIR = os.path.dirname(__file__)
ETCDIR = os.path.join(ROOTDIR, '../', '../', 'etc')
def etcdir(*p):
return os.path.join(ETCDIR, *p)
steth_config_file = etcdir('steth.conf')
cfg.CONF.register_opts(OPTS)
cfg.CONF([], project='steth',
default_config_files=['/etc/steth/steth.conf'])
try:
cfg.CONF([], project='steth',
default_config_files=['/etc/steth/steth.conf'])
except:
# This exception will happen if the current environment doesn't have
# /etc/steth/steth.conf. If so, read configuration from
# etc/steth/steth.conf rather than /etc/steth/steth.conf.
cfg.CONF([], project='steth',
default_config_files=[steth_config_file])
all_agents = cfg.CONF.network_agents_info + cfg.CONF.compute_agents_info
MGMT_TYPE = 'mgmt'
NET_TYPE = 'net'
STORAGE_TYPE = 'storage'
# We use STETH_AGENT_INFOS to create connection to every node
STETH_AGENT_INFOS = {}
MGMT_INTERFACE = None
NET_INTERFACE = None
STORAGE_INTERFACE = None
# We use ALL_AGENT_INFOS to process iperf
ALL_AGENT_INFOS = {}
for agent in all_agents:
l = []
prefix = cfg.CONF.networks_prefix[0]
item = {cfg.CONF.node_name_prefix + agent: prefix + agent}
STETH_AGENT_INFOS.update(item)
for prefix in cfg.CONF.networks_prefix[1:]:
l.append(prefix + agent)
item = {cfg.CONF.node_name_prefix + agent: l}
ALL_AGENT_INFOS.update(item)
def is_ip(addr):
try:
socket.inet_aton(addr)
# valid
return 0
except socket.error:
# invalid
return 1
def check_ip_and_fill(agent_type, net_prefix):
d = {}
name_prefix = cfg.CONF.node_name_prefix
for node in cfg.CONF.nodes_id:
if not is_ip(net_prefix + node):
d[name_prefix + node] = net_prefix + node
agent_type.update(d)
else:
print "%s is not IP!" % name_prefix + node
def validate_and_parse_network_types():
if not cfg.CONF.network_types:
print 'You must fill network_types in config file!'
sys.exit()
for network_type in cfg.CONF.network_types:
net_type, net_interface, net_prefix = network_type.split(':')
# parse mgmt networks
if net_type == MGMT_TYPE:
check_ip_and_fill(MGMT_AGENTS_INFOS, net_prefix)
global MGMT_INTERFACE
MGMT_INTERFACE = net_interface
# parse net networks
elif net_type == NET_TYPE:
check_ip_and_fill(NET_AGENTS_INFOS, net_prefix)
global NET_INTERFACE
NET_INTERFACE = net_interface
# parse stor networks
elif net_type == STORAGE_TYPE:
check_ip_and_fill(STORAGE_AGENTS_INFOS, net_prefix)
global STORAGE_INTERFACE
STORAGE_INTERFACE = net_interface
else:
print "Unkown network_types: %s" % network_type
validate_and_parse_network_types()

View File

@ -19,9 +19,23 @@ import socket
import sys
from cliff.lister import Lister
from steth.stethclient import utils
from steth.stethclient.constants import MGMT_TYPE
from steth.stethclient.constants import NET_TYPE
from steth.stethclient.constants import STORAGE_TYPE
from steth.stethclient.utils import Logger
from steth.stethclient.utils import setup_server
from steth.stethclient import utils
try:
from steth.stethclient.constants import MGMT_AGENTS_INFOS
from steth.stethclient.constants import NET_AGENTS_INFOS
from steth.stethclient.constants import STORAGE_AGENTS_INFOS
except:
Logger.log_fail("Import configure file fail.")
MGMT_AGENTS_INFOS = NET_AGENTS_INFOS = STORAGE_AGENTS_INFOS = {
'agent-64': "127.0.0.1",
'agent-65': "127.0.0.1",
}
def get_ip_by_hostname(hostname):
@ -36,9 +50,12 @@ class CheckIperf(Lister):
def get_parser(self, prog_name):
parser = super(CheckIperf, self).get_parser(prog_name)
parser.add_argument('server_agent', default='bad')
parser.add_argument('client_agent', default='bad')
parser.add_argument('iperf_server_ip', default='bad')
parser.add_argument('server_agent', default='bad',
help='IPERF server will be started at this agent.')
parser.add_argument('client_agent', default='bad',
help='IPERF client will be started at this agent.')
parser.add_argument('iperf_server_type', default='mgmt',
help='Choose from "mgmt", "net" or "storage".')
parser.add_argument('--server_protocol', nargs='?', default='TCP')
parser.add_argument('--server_port', nargs='?', default='5001')
parser.add_argument('--client_protocol', nargs='?', default='TCP')
@ -48,20 +65,21 @@ class CheckIperf(Lister):
parser.add_argument('--client_bandwidth', nargs='?', default=None)
return parser
def take_iperf_client(self, client, host, protocol,
timeout, parallel, bandwidth, port):
res = client.start_iperf_client(protocol=protocol,
host=host,
timeout=timeout,
parallel=parallel,
bandwidth=bandwidth,
port=port)
return res
def take_action(self, parsed_args):
self.log.debug('Get parsed_args: %s' % parsed_args)
# check iperf client ip if legal
if utils.is_ip(parsed_args.iperf_server_ip):
Logger.log_fail('IP address not valid')
sys.exit()
server = setup_server(parsed_args.server_agent)
client = setup_server(parsed_args.client_agent)
iperf_server_pdid = None
# check iperf server ip exist
res = server.validate_ip(parsed_args.iperf_server_ip)
if res['code'] == 1:
Logger.log_fail(res['message'])
sys.exit()
# setup iperf server
res = server.setup_iperf_server(protocol=parsed_args.server_protocol,
port=parsed_args.server_port)
@ -74,26 +92,84 @@ class CheckIperf(Lister):
'pid:%s') % (res['data']['pid']))
self.log.debug(msg)
iperf_server_pdid = res['data']['pid']
# setup iperf client
#try:
# host = get_ip_by_hostname(parsed_args.server_agent)
#except Exception as e:
# self.log.info("We can not resolve this name: %s",
# (parsed_args.server_agent))
res = client.start_iperf_client(protocol=parsed_args.client_protocol,
host=parsed_args.iperf_server_ip,
timeout=parsed_args.client_timeout,
parallel=parsed_args.client_parallel,
bandwidth=parsed_args.client_bandwidth,
port=parsed_args.client_port)
self.log.debug('Response is %s' % res)
# kill iperf server
r = server.teardown_iperf_server(iperf_server_pdid)
if r['code'] == 1:
Logger.log_fail(r['message'])
if r['code'] == 0:
msg = (('Iperf server delete success and '
'pid:%s') % (iperf_server_pdid))
self.log.debug(msg)
return (('Field', 'Value'),
((k, v) for k, v in res['data'].items()))
if parsed_args.iperf_server_type == MGMT_TYPE or \
parsed_args.iperf_server_type == NET_TYPE or \
parsed_args.iperf_server_type == STORAGE_TYPE:
host = utils.get_ip_from_agent(parsed_args.server_agent,
parsed_args.iperf_server_type)
bandwidth = parsed_args.client_bandwidth
res = self.take_iperf_client(client=client,
protocol=parsed_args.client_protocol,
host=host,
timeout=parsed_args.client_timeout,
parallel=parsed_args.client_parallel,
bandwidth=bandwidth,
port=parsed_args.client_port)
self.log.debug('Response is %s' % res)
# kill iperf server
r = server.teardown_iperf_server(iperf_server_pdid)
if r['code'] == 1:
Logger.log_fail(r['message'])
if r['code'] == 0:
msg = (('Iperf server delete success and '
'pid:%s') % (iperf_server_pdid))
self.log.debug(msg)
return (('Field', 'Value'),
((k, v) for k, v in res['data'].items()))
elif parsed_args.iperf_server_type == 'others':
mgmt_host = MGMT_AGENTS_INFOS[parsed_args.server_agent]
net_host = NET_AGENTS_INFOS[parsed_args.server_agent]
storage_host = STORAGE_AGENTS_INFOS[parsed_args.server_agent]
bandwidth = parsed_args.client_bandwidth
mgmt_res = self.take_iperf_client(
client=client,
protocol=parsed_args.client_protocol,
host=mgmt_host,
timeout=parsed_args.client_timeout,
parallel=parsed_args.client_parallel,
bandwidth=bandwidth,
port=parsed_args.client_port)
msg = "Process mgmt iperf success, begain net iperf..."
Logger.log_normal(msg)
net_res = self.take_iperf_client(
client=client,
protocol=parsed_args.client_protocol,
host=net_host,
timeout=parsed_args.client_timeout,
parallel=parsed_args.client_parallel,
bandwidth=bandwidth,
port=parsed_args.client_port)
msg = "Process net iperf success, begain storage iperf..."
Logger.log_normal(msg)
storage_res = self.take_iperf_client(
client=client,
protocol=parsed_args.client_protocol,
host=storage_host,
timeout=parsed_args.client_timeout,
parallel=parsed_args.client_parallel,
bandwidth=bandwidth,
port=parsed_args.client_port)
# kill iperf server
r = server.teardown_iperf_server(iperf_server_pdid)
if r['code'] == 1:
Logger.log_fail(r['message'])
if r['code'] == 0:
msg = (('Iperf server delete success and '
'pid:%s') % (iperf_server_pdid))
self.log.debug(msg)
mgmt_data = [(k, v) for k, v in mgmt_res['data'].items()]
net_data = [(k, v) for k, v in net_res['data'].items()]
storage_data = [(k, v) for k, v in storage_res['data'].items()]
return (('Field', 'Value'),
[('Mgmt Result', ' ')] +
mgmt_data +
[('Net Result', ' ')] +
net_data +
[('Storage Result', ' ')] +
storage_data)
else:
msg = ("Get unsupport iperf server type: %s "
"Please choose from net, mgmt, storage, others "
% parsed_args.iperf_server_type)
Logger.log_fail(msg)
return (('Field', 'Value'), [(' ', ' ')])

View File

@ -81,27 +81,30 @@ class Logger():
LISTEN_PORT = 9698
try:
from steth.stethclient.constants import STETH_AGENT_INFOS
from steth.stethclient.constants import MGMT_AGENTS_INFOS
from steth.stethclient.constants import NET_AGENTS_INFOS
from steth.stethclient.constants import STORAGE_AGENTS_INFOS
except:
STETH_AGENT_INFOS = {
Logger.log_fail("Import configure file fail.")
MGMT_AGENTS_INFOS = NET_AGENTS_INFOS = STORAGE_AGENTS_INFOS = {
'agent-64': "127.0.0.1",
'agent-65': "127.0.0.1",
}
Logger.log_fail("Import steth configure file fail. Use fake data!")
def setup_server(agent):
log = logging.getLogger(__name__)
if agent in STETH_AGENT_INFOS:
if agent in MGMT_AGENTS_INFOS:
log.debug('get agent:%s ip_address:%s' % (
agent, STETH_AGENT_INFOS[agent]))
agent, MGMT_AGENTS_INFOS[agent]))
else:
log.error('Agent %s not configured. Please check it.' % (agent))
sys.exit()
log.debug('Begin create connection with http://%s:%s.' % (agent,
LISTEN_PORT))
log.debug('Begin create connection with http://%s:%s.' % (
agent,
LISTEN_PORT))
server = jsonrpclib.Server('http://%s:%s' %
(STETH_AGENT_INFOS[agent], LISTEN_PORT))
(MGMT_AGENTS_INFOS[agent], LISTEN_PORT))
log.debug('Create connection with %s success.' % (agent))
return server
@ -114,3 +117,20 @@ def is_ip(addr):
except socket.error:
# Not legal
return 1
def get_ip_from_agent(node, net_type):
from steth.stethclient.constants import MGMT_TYPE
from steth.stethclient.constants import NET_TYPE
from steth.stethclient.constants import STORAGE_TYPE
try:
if net_type == NET_TYPE:
return NET_AGENTS_INFOS[node]
elif net_type == MGMT_TYPE:
return MGMT_AGENTS_INFOS[node]
elif net_type == STORAGE_TYPE:
return STORAGE_AGENTS_INFOS[node]
else:
return 1
except Exception as e:
print "Can't get ip! Because: %s" % e

View File

@ -92,9 +92,7 @@ class TestStethClientMethods(unittest.TestCase):
self.server.validate_ip = mock.Mock(return_value=validate_ip_r)
self.server.teardown_iperf_server = mock.Mock(
return_value=teardown_iperf_r)
#iperf_api.get_ip_by_hostname = mock.Mock(return_value='10.0.0.64')
shell.main(['check-iperf', 'agent-64', 'agent-64', '10.0.0.64'])
shell.main(['check-iperf', 'agent-64', 'agent-64', 'mgmt'])
self.assertEqual(self.server.setup_iperf_server.called, True)
self.assertEqual(self.server.start_iperf_client.called, True)
self.assertEqual(self.server.validate_ip.called, True)
self.assertEqual(self.server.teardown_iperf_server.called, True)