From 57adc694c3e29aac0465e83046e952c50dfe3d51 Mon Sep 17 00:00:00 2001 From: "C. R. Oldham" Date: Wed, 6 Nov 2013 12:47:40 -0700 Subject: [PATCH 1/5] Better support for FreeBSD / Mac OS X traceroute --- salt/modules/network.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/salt/modules/network.py b/salt/modules/network.py index edd8bc4d9b..4f98ef78b3 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -91,12 +91,23 @@ def traceroute(host): ''' ret = [] cmd = 'traceroute {0}'.format(salt.utils.network.sanitize_host(host)) + out = __salt__['cmd.run'](cmd) # Parse version of traceroute cmd2 = 'traceroute --version' out2 = __salt__['cmd.run'](cmd2) - traceroute_version = re.findall(r'version (\d+)\.(\d+)\.(\d+)', out2)[0] + try: + # Linux traceroute version looks like: + # Modern traceroute for Linux, version 2.0.19, Dec 10 2012 + # Darwin and FreeBSD traceroute version looks like: Version 1.4a12+[FreeBSD|Darwin] + + traceroute_version = re.findall(r'.*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)', out2)[0] + + if len(traceroute_version) < 3: + traceroute_version.append(0) + except IndexError: + traceroute_version = [0, 0, 0] for line in out.splitlines(): if ' ' not in line: From 464aea10b95401489162b7dd95cdf55d326ac2e4 Mon Sep 17 00:00:00 2001 From: "C. R. Oldham" Date: Wed, 6 Nov 2013 14:47:34 -0700 Subject: [PATCH 2/5] Complete support for restricting scheduled process running. --- salt/modules/network.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/modules/network.py b/salt/modules/network.py index 4f98ef78b3..5b96df88a3 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -106,8 +106,8 @@ def traceroute(host): if len(traceroute_version) < 3: traceroute_version.append(0) - except IndexError: - traceroute_version = [0, 0, 0] + except: IndexError: + traceroute_version = [0, 0, 0] for line in out.splitlines(): if ' ' not in line: From c2caf83aa00b26b05d9e61c7540ec5783083fb98 Mon Sep 17 00:00:00 2001 From: "C. R. Oldham" Date: Wed, 6 Nov 2013 14:48:17 -0700 Subject: [PATCH 3/5] Complete support for restricting scheduled process running. --- salt/modules/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/modules/network.py b/salt/modules/network.py index 5b96df88a3..cfe4270f78 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -106,7 +106,7 @@ def traceroute(host): if len(traceroute_version) < 3: traceroute_version.append(0) - except: IndexError: + except IndexError: traceroute_version = [0, 0, 0] for line in out.splitlines(): From 75b49a33e6a31db4b2366a4c70db60ef95545be7 Mon Sep 17 00:00:00 2001 From: "C. R. Oldham" Date: Wed, 6 Nov 2013 15:53:04 -0700 Subject: [PATCH 4/5] Make network.traceroute work better on FreeBSD and MacOS X. --- salt/modules/network.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/salt/modules/network.py b/salt/modules/network.py index cfe4270f78..8bf874d371 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -90,6 +90,10 @@ def traceroute(host): salt '*' network.traceroute archlinux.org ''' ret = [] + if not salt.utils.which('traceroute'): + log.info("This minion does not have traceroute installed") + return ret + cmd = 'traceroute {0}'.format(salt.utils.network.sanitize_host(host)) out = __salt__['cmd.run'](cmd) @@ -102,7 +106,14 @@ def traceroute(host): # Modern traceroute for Linux, version 2.0.19, Dec 10 2012 # Darwin and FreeBSD traceroute version looks like: Version 1.4a12+[FreeBSD|Darwin] - traceroute_version = re.findall(r'.*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)', out2)[0] + traceroute_version_raw = re.findall(r'.*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)', out2)[0] + + traceroute_version = [] + for t in traceroute_version_raw: + try: + traceroute_version.append(int(t)) + except ValueError: + traceroute_version.append(t) if len(traceroute_version) < 3: traceroute_version.append(0) From ba5e42869c38b7fac9556172aa8d18dda4637997 Mon Sep 17 00:00:00 2001 From: "C. R. Oldham" Date: Thu, 7 Nov 2013 15:28:22 -0700 Subject: [PATCH 5/5] Make network.traceroute work better on FreeBSD and MacOS X. --- salt/modules/network.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/salt/modules/network.py b/salt/modules/network.py index 8bf874d371..86ab4d5415 100644 --- a/salt/modules/network.py +++ b/salt/modules/network.py @@ -107,7 +107,7 @@ def traceroute(host): # Darwin and FreeBSD traceroute version looks like: Version 1.4a12+[FreeBSD|Darwin] traceroute_version_raw = re.findall(r'.*[Vv]ersion (\d+)\.([\w\+]+)\.*(\w*)', out2)[0] - + log.debug("traceroute_version_raw: {}".format(traceroute_version_raw)) traceroute_version = [] for t in traceroute_version_raw: try: @@ -117,6 +117,9 @@ def traceroute(host): if len(traceroute_version) < 3: traceroute_version.append(0) + + log.debug("traceroute_version: {}".format(str(traceroute_version))) + except IndexError: traceroute_version = [0, 0, 0] @@ -126,7 +129,33 @@ def traceroute(host): if line.startswith('traceroute'): continue - if (traceroute_version[0] >= 2 and traceroute_version[2] >= 14 + if ('Darwin' in str(traceroute_version[1]) or 'FreeBSD' in str(traceroute_version[1])): + try: + traceline = re.findall(r'\s*(\d*)\s+(.*)\s+\((.*)\)\s+(.*)$', line)[0] + except IndexError: + traceline = re.findall(r'\s*(\d*)\s+(\*\s+\*\s+\*)', line)[0] + + log.debug("traceline: {}".format(traceline)) + delays = re.findall(r'(\d+\.\d+)\s*ms', str(traceline)) + + try: + if traceline[1] == '* * *': + result = { + 'count': traceline[0], + 'hostname': '*' + } + else: + result = { + 'count': traceline[0], + 'hostname': traceline[1], + 'ip': traceline[2], + } + for x in range(0, len(delays)): + result['ms{}'.format(x+1)] = delays[x] + except IndexError: + result = {} + + elif (traceroute_version[0] >= 2 and traceroute_version[2] >= 14 or traceroute_version[0] >= 2 and traceroute_version[1] > 0): comps = line.split(' ') if comps[1] == '* * *':