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
|
##############################################################################
# Copyright (c) 2016 HUAWEI TECHNOLOGIES CO.,LTD and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import yaml
import sys
import subprocess
import log as logging
LOG = logging.getLogger("net-check")
def is_ip_reachable(ip):
cmd = "ping -c 2 %s" % ip
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=None,
shell=True)
output = process.communicate()[0]
if " 0% packet loss" in output:
LOG.info("%s is reachable", ip)
elif "100% packet loss" in output:
LOG.error("%s is unreachable" % (ip))
return False
else:
LOG.warn("%r", output)
return True
def is_host_ips_reachable(settings):
external = settings["br-prv"]["ip"]
external_gw = settings["br-prv"]["gw"]
storage = settings["storage"]["ip"]
mgmt = settings["mgmt"]["ip"]
return is_ip_reachable(external) \
and is_ip_reachable(external_gw) \
and is_ip_reachable(storage) \
and is_ip_reachable(mgmt)
def main(hostname, config):
LOG.info("host is %s", hostname)
result = True
for host, settings in config.iteritems():
LOG.info("check %s network connectivity start", host)
result = result and is_host_ips_reachable(settings)
if result:
LOG.info("All hosts ips are reachable")
else:
LOG.error("Some hosts ips are unreachable !!!")
sys.exit(-1)
if __name__ == "__main__":
hostname = yaml.load(sys.argv[1])
config = yaml.load(sys.argv[2])
config.pop(hostname, None)
main(hostname, config)
|