Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add click cmd for minpoll/maxpoll configuration on a ntp server #2981

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6512,28 +6512,71 @@ def ntp(ctx):

@ntp.command('add')
@click.argument('ntp_ip_address', metavar='<ntp_ip_address>', required=True)
@click.argument('minpoll_maxpoll', metavar='[minpoll <minpoll> maxpoll <maxpoll>]', required=False, type=click.Path())
@click.pass_context
def add_ntp_server(ctx, ntp_ip_address):
def add_ntp_server(ctx, ntp_ip_address, minpoll_maxpoll):
""" Add NTP server IP """
DEFAULT_MINPOLL = 6
DEFAULT_MAXPOLL = 10
MIN_POLL = 3
MAX_POLL = 17

new_minpoll = DEFAULT_MINPOLL
new_maxpoll = DEFAULT_MAXPOLL

if ADHOC_VALIDATION:
if not clicommon.is_ipaddress(ntp_ip_address):
ctx.fail('Invalid IP address')
db = ValidatedConfigDBConnector(ctx.obj['db'])
ntp_servers = db.get_table("NTP_SERVER")
if ntp_ip_address in ntp_servers:
click.echo("NTP server {} is already configured".format(ntp_ip_address))
return
else:
try:
db.set_entry('NTP_SERVER', ntp_ip_address, {'NULL': 'NULL'})
except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
click.echo("NTP server {} added to configuration".format(ntp_ip_address))
try:
click.echo("Restarting ntp-config service...")
clicommon.run_command(['systemctl', 'restart', 'ntp-config'], display_cmd=False)
except SystemExit as e:
ctx.fail("Restart service ntp-config failed with error {}".format(e))

if len(minpoll_maxpoll) != 0 and len(minpoll_maxpoll) != 4:
ctx.fail('Invalid input for minpoll and maxpoll')

if len(minpoll_maxpoll) != 0:
new_minpoll = int(minpoll_maxpoll[1])
new_maxpoll = int(minpoll_maxpoll[3])

if new_minpoll not in range(MIN_POLL, MAX_POLL + 1) or new_maxpoll not in range(MIN_POLL, MAX_POLL + 1):
ctx.fail('minpoll and maxpoll must be in the range 3-17')

db = ValidatedConfigDBConnector(ctx.obj['db'])
ntp_server_entry = db.get_entry('NTP_SERVER', ntp_ip_address)

# init the curr_minpoll & curr_maxpoll with default values
curr_minpoll = DEFAULT_MINPOLL
curr_maxpoll = DEFAULT_MAXPOLL

if ntp_server_entry:
if ntp_server_entry.get('minpoll'):
curr_minpoll = int(ntp_server_entry.get('minpoll'))
if ntp_server_entry and ntp_server_entry.get('maxpoll'):
curr_maxpoll = int(ntp_server_entry.get('maxpoll'))

if (curr_minpoll == minpoll) and (curr_maxpoll == maxpoll):
click.echo("NTP server {} is already configured".format(ntp_ip_address))
return

# if create or update
if new_minpoll >= new_maxpoll:
ctx.fail("Invalid minpoll:{} and maxpoll:{}".format(curr_minpoll, curr_maxpoll))

serverInfo = {
"minpoll": new_minpoll,
"maxpoll": new_maxpoll
}
try:
if ntp_server_entry:
db.mod_entry('NTP_SERVER', ntp_ip_address, serverInfo)
else:
db.set_entry('NTP_SERVER', ntp_ip_address, serverInfo)

except ValueError as e:
ctx.fail("Invalid ConfigDB. Error: {}".format(e))
click.echo("NTP server {} minpoll {} maxpoll {} added to configuration".format(ntp_ip_address, new_minpoll, new_maxpoll))
try:
click.echo("Restarting ntp-config service...")
clicommon.run_command(['systemctl', 'restart', 'ntp-config'], display_cmd=False)
except SystemExit as e:
ctx.fail("Restart service ntp-config failed with error {}".format(e))

@ntp.command('del')
@click.argument('ntp_ip_address', metavar='<ntp_ip_address>', required=True)
Expand Down
19 changes: 12 additions & 7 deletions doc/Command-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -7411,19 +7411,25 @@ This sub-section of commands is used to add or remove the configured NTP servers
**config ntp add**

This command is used to add a NTP server IP address to the NTP server list. Note that more that one NTP server IP address can be added in the device.

User may optionally configure minpoll and maxpoll for each NTP server. Minpoll and maxpoll is in the range from 3-17. When not configured, minpoll has default value "6" and maxpoll has default value "10"
- Usage:
```
config ntp add <ip_address>
config ntp add <ip_address> [minpoll] [maxpoll]
```

- Example:
```
admin@sonic:~$ sudo config ntp add 9.9.9.9
NTP server 9.9.9.9 added to configuration
NTP server 9.9.9.9 {'minpoll': 6, 'maxpoll': 10} added to configuration
Restarting ntp-config service...
```

```
admin@sonic:~# config ntp add 8.8.8.8 12 16
NTP server 8.8.8.8 {'minpoll': 12, 'maxpoll': 16} added to configuration
Restarting ntp-config service...
```

**config ntp delete**

This command is used to delete a configured NTP server IP address.
Expand Down Expand Up @@ -9553,11 +9559,10 @@ This command displays the running configuration of the ntp module.

- Example:
```
admin@sonic:~$ show runningconfiguration ntp
NTP Servers
-------------
1.1.1.1
2.2.2.2
-----------------------------
8.8.8.8 minpoll 12 maxpoll 16
9.9.9.9 minpoll 6 maxpoll 10
```

**show runningconfiguration syslog**
Expand Down
9 changes: 7 additions & 2 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,8 +1511,13 @@ def ntp(verbose):
data = ntp_file.readlines()
for line in data:
if line.startswith("server "):
ntp_server = line.split(" ")[1]
ntp_servers.append(ntp_server)
info = line.split(" ")
click.echo("ntp server: {} minpoll {} maxpoll {}".format(info[1], info[4], info[6]))
#ntp_server = line.split(" ")[1]
ntp_server = info[1]
minpoll = info[4]
maxpoll = info[6]
ntp_servers.append(ntp_server + " minpoll " + minpoll + " maxpoll " + maxpoll)
ntp_dict['NTP Servers'] = ntp_servers
print(tabulate(ntp_dict, headers=list(ntp_dict.keys()), tablefmt="simple", stralign='left', missingval=""))

Expand Down