-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17207 from louis-6wind/bmpserver-log
tests: add bmpserver logging
- Loading branch information
Showing
3 changed files
with
109 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,43 +5,105 @@ | |
# Authored by Farid Mihoub <[email protected]> | ||
# | ||
import argparse | ||
|
||
# XXX: something more reliable should be used "Twisted" a great choice. | ||
import signal | ||
import socket | ||
import sys | ||
|
||
from datetime import datetime | ||
|
||
from bmp import BMPMsg | ||
|
||
BGP_MAX_SIZE = 4096 | ||
|
||
# Global variable to track shutdown signal | ||
shutdown = False | ||
|
||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-a", "--address", type=str, default="0.0.0.0") | ||
parser.add_argument("-p", "--port", type=int, default=1789) | ||
parser.add_argument("-l", "--logfile", type=str, default="/var/log/bmp.log") | ||
|
||
|
||
def handle_signal(signum, frame): | ||
global shutdown | ||
timestamp_print(f"Received signal {signum}, shutting down.") | ||
shutdown = True | ||
|
||
|
||
def timestamp_print(message, file=sys.stderr): | ||
"""Helper function to timestamp_print messages with timestamps.""" | ||
|
||
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
print(f"[{current_time}] {message}", file=file) | ||
|
||
|
||
def main(): | ||
global shutdown | ||
|
||
# Set up signal handling for SIGTERM and SIGINT | ||
signal.signal(signal.SIGTERM, handle_signal) | ||
signal.signal(signal.SIGINT, handle_signal) | ||
|
||
args = parser.parse_args() | ||
ADDRESS, PORT = args.address, args.port | ||
LOG_FILE = args.logfile | ||
|
||
timestamp_print(f"Starting bmpserver on {args.address}:{args.port}") | ||
|
||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
s.bind((ADDRESS, PORT)) | ||
s.listen() | ||
connection, _ = s.accept() | ||
|
||
try: | ||
while True: | ||
data = connection.recv(BGP_MAX_SIZE) | ||
while len(data) > BMPMsg.MIN_LEN: | ||
data = BMPMsg.dissect(data, log_file=LOG_FILE) | ||
s.bind((ADDRESS, PORT)) | ||
s.listen() | ||
timestamp_print(f"Listening on TCP {args.address}:{args.port}") | ||
|
||
connection, client_address = s.accept() | ||
timestamp_print(f"TCP session opened from {client_address}") | ||
|
||
try: | ||
while not shutdown: # Check for shutdown signal | ||
data = connection.recv(BGP_MAX_SIZE) | ||
if shutdown: | ||
break | ||
|
||
if not data: | ||
# connection closed | ||
break | ||
|
||
timestamp_print( | ||
f"Data received from {client_address}: length {len(data)}" | ||
) | ||
|
||
while len(data) > BMPMsg.MIN_LEN: | ||
data = BMPMsg.dissect(data, log_file=LOG_FILE) | ||
|
||
timestamp_print( | ||
f"Finished dissecting data from {client_address}" | ||
) | ||
|
||
except Exception as e: | ||
timestamp_print(f"{e}") | ||
pass | ||
except KeyboardInterrupt: | ||
timestamp_print(f"Got Keyboard Interrupt.") | ||
pass | ||
finally: | ||
timestamp_print(f"TCP session closed with {client_address}") | ||
connection.close() | ||
except socket.error as sock_err: | ||
timestamp_print(f"Socket error: {e}") | ||
except Exception as e: | ||
# XXX: do something | ||
pass | ||
except KeyboardInterrupt: | ||
# XXX: do something | ||
pass | ||
timestamp_print(f"{e}") | ||
finally: | ||
connection.close() | ||
timestamp_print(f"Server shutting down on {ADDRESS}:{PORT}") | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) | ||
try: | ||
sys.exit(main()) | ||
except KeyboardInterrupt: | ||
logging.info("BMP server was interrupted and is shutting down.") | ||
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters