Skip to content

Commit

Permalink
added timecheck; default log file
Browse files Browse the repository at this point in the history
  • Loading branch information
emoscardini committed Dec 27, 2023
1 parent 76c2dfc commit 7b681d6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Changelog

All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.3.0] - 2023-12-27

### Added

- Added time check. If the local clock is more than 10 min away from pool.ntp.org
the script will exit with an error.

### Changed

- Changed default location of log from PWD, to `/var/log/router_registration.log`

## [1.2.4] - 2023-12-13

### Changed
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pyyaml
colorama
jinja2
distro
cryptography
cryptography
ntplib
44 changes: 42 additions & 2 deletions router_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import logging
import json
import time
from datetime import datetime, timedelta
import ssl
import ipaddress
import subprocess
import platform
from urllib.parse import urlparse
import ntplib
import yaml
from cryptography import x509
from cryptography.hazmat.backends import default_backend
Expand Down Expand Up @@ -200,6 +202,40 @@ def check_subnet(subnet_str):
logging.error("Unable to parser subnet")
sys.exit(1)

def check_time_diff(margin_minutes=10, server="pool.ntp.org"):
"""
Check if the local time is within a specified margin from the NTP server.
:param margin_minutes: The acceptable margin in minutes. Default is 5 minutes.
:param server: The NTP server address. Default is "pool.ntp.org".
return: True if local time is within the acceptable margin from the NTP server
"""
# Get the current NTP time
try:
client = ntplib.NTPClient()
response = client.request(server)
ntp_time = datetime.utcfromtimestamp(response.tx_time)
except SystemError:
logging.warning("Unable to compare time.")

if ntp_time is not None:
logging.debug("ntp time: %s", ntp_time)
# Get the local time
local_time = datetime.now()
logging.debug("local time: %s", local_time)

# Calculate the difference between local time and NTP time
time_difference = local_time - ntp_time
logging.debug("Time difference: %s", time_difference)

# Check if the absolute difference is within the specified margin
if abs(time_difference) >= timedelta(minutes=margin_minutes):
logging.error("Time difference: %s", time_difference)
logging.error("Unable to proceed, please check local time")
sys.exit(1)
else:
logging.warning("Unable to compare time.")

def create_netfoundry_tuning_file():
"""
Creates a file named '01-netfoundry_tuning.conf' containing specific tuning content
Expand Down Expand Up @@ -240,7 +276,7 @@ def create_parser():
:return: A Namespace containing arguments
"""
__version__ = '1.2.4'
__version__ = '1.3.0'
parser = argparse.ArgumentParser()

mgroup = parser.add_mutually_exclusive_group(required=True)
Expand All @@ -259,7 +295,8 @@ def create_parser():
help='Set the logging level - Default: INFO)')
parser.add_argument('--logFile', type=str,
help='Specify the log file -'
'Default router_registration.log')
'Default router_registration.log',
default='/var/log/router_registration.log')
parser.add_argument('-s', '--salt',
action="store_false",
help='Skip salt-stack setup',
Expand Down Expand Up @@ -868,6 +905,9 @@ def main():

logging.info("\033[0;35mStarting Registration\033[0m")

# check time
check_time_diff()

# check the number of interfaces
if not args.edge:
check_ipv4_interface_count(parser)
Expand Down

0 comments on commit 7b681d6

Please sign in to comment.