Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
Merge pull request #6 from dbarrosop/master
Browse files Browse the repository at this point in the history
Fix get_ntp_peers to comply with the changes in napalm-automation/napalm-base#12
  • Loading branch information
dbarrosop committed Apr 19, 2016
2 parents 069eef6 + b1c1074 commit 3d0e9e4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ script:
- nosetests -v TestIOSDriver:TestGetterIOSDriver.test_get_lldp_neighbors
- nosetests -v TestIOSDriver:TestGetterIOSDriver.test_get_lldp_neighbors_detail
- nosetests -v TestIOSDriver:TestGetterIOSDriver.test_get_mac_address_table
- nosetests -v TestIOSDriver:TestGetterIOSDriver.test_get_ntp_peers
- nosetests -v TestIOSDriver:TestGetterIOSDriver.test_get_ntp_stats
- nosetests -v TestIOSDriver:TestGetterIOSDriver.test_get_snmp_information
- nosetests -v TestIOSDriver:TestGetterIOSDriver.test_ios_only_bgp_time_conversion
- cd ../..
84 changes: 24 additions & 60 deletions napalm_ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,78 +1155,42 @@ def cli(self, commands=None):

return cli_output

def get_ntp_peers(self):
"""
Returns a dictionary of dictionaries with the details of each NTP peer.
Each key of the dictionary is the IP Address of the NTP peer.
Details of the peer are represented by the following fields:
* referenceid (string)
* stratum (int)
* type (string)
* when (string)
* hostpoll (int)
* reachability (int)
* delay (float)
* offset (float)
* jitter (float)
Example:
{
'188.114.101.4': {
'referenceid' : '188.114.100.1',
'stratum' : 4,
'type' : '-',
'when' : 107,
'hostpoll' : 256,
'reachability' : 377,
'delay' : 164.228,
'offset' : -13.866,
'jitter' : 2.695
}
}
"""
def get_ntp_stats(self):
"""Implementation of get_ntp_stats for IOS."""
ntp_stats = []

ntp_peers = {}
command = 'show ntp associations'
output = self.device.send_command(command)

output = output.split('\n')

for line in output:
for line in output.splitlines():
# Skip first two lines and last line of command output
if line == "" or 'address' in line or 'sys.peer' in line:
continue

if '%NTP is not enabled' in line:
return {}
return []

elif len(line.split()) == 9:
address, ref_clock, st, when, poll, reach, delay, offset, disp = line.split()
address_regex = re.match('(\W*)([0-9.*]*)', address)
try:
ntp_stats.append({
'remote': unicode(address_regex.group(2)),
'synchronized': ('*' in address_regex.group(1)),
'referenceid': unicode(ref_clock),
'stratum': int(st),
'type': u'-',
'when': unicode(when),
'hostpoll': int(poll),
'reachability': int(reach),
'delay': float(delay),
'offset': float(offset),
'jitter': float(disp)
})
except Exception:
continue

# remove all characters from ip address other than period or numbers
# examples of input that needs cleaning +~129.6.15.28
regex = re.compile('[^0-9.]')
address = regex.sub('', address)

try:
peer = {
'referenceid': ref_clock,
'stratum': int(st),
'type': u'-',
'when': when,
'hostpoll': int(poll),
'reachability': int(reach),
'delay': float(delay),
'offset': float(offset),
'jitter': float(disp)
}

ntp_peers.setdefault(address, {})
ntp_peers[address].update(peer)
except ValueError:
print("Casting NTP variable failed")
else:
raise ValueError("Unexpected output from: {}".format(line.split()))

return ntp_peers
return ntp_stats

def get_mac_address_table(self):

Expand Down

0 comments on commit 3d0e9e4

Please sign in to comment.