summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorViacheslav Hletenko <v.gletenko@vyos.io>2024-06-10 11:11:02 +0000
committerViacheslav Hletenko <v.gletenko@vyos.io>2024-06-10 11:11:02 +0000
commitd6108d6e0a14f32d7ee28df01b5a4fcd678acada (patch)
tree037aea284881427577cfe3dc4edeea69dd083ddd /src
parentf6752de811ba8a553abd631427e7735b191c1f91 (diff)
downloadvyos-1x-d6108d6e0a14f32d7ee28df01b5a4fcd678acada.tar.gz
vyos-1x-d6108d6e0a14f32d7ee28df01b5a4fcd678acada.zip
T6442: CGNAT add log for address allocation
Add the configuration command to log current CGNAT allocation set nat cgnat log-allocation
Diffstat (limited to 'src')
-rwxr-xr-xsrc/conf_mode/nat_cgnat.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/conf_mode/nat_cgnat.py b/src/conf_mode/nat_cgnat.py
index 957b12c28..bd6855e8b 100755
--- a/src/conf_mode/nat_cgnat.py
+++ b/src/conf_mode/nat_cgnat.py
@@ -16,9 +16,11 @@
import ipaddress
import jmespath
+import logging
import os
from sys import exit
+from logging.handlers import SysLogHandler
from vyos.config import Config
from vyos.template import render
@@ -32,6 +34,18 @@ airbag.enable()
nftables_cgnat_config = '/run/nftables-cgnat.nft'
+# Logging
+logger = logging.getLogger('cgnat')
+logger.setLevel(logging.DEBUG)
+
+syslog_handler = SysLogHandler(address="/dev/log")
+syslog_handler.setLevel(logging.INFO)
+
+formatter = logging.Formatter('%(name)s: %(message)s')
+syslog_handler.setFormatter(formatter)
+
+logger.addHandler(syslog_handler)
+
class IPOperations:
def __init__(self, ip_prefix: str):
@@ -315,6 +329,22 @@ def apply(config):
return None
cmd(f'nft --file {nftables_cgnat_config}')
+ # Logging allocations
+ if 'log_allocation' in config:
+ allocations = config['proto_map_elements']
+ allocations = allocations.split(',')
+ for allocation in allocations:
+ try:
+ # Split based on the delimiters used in the nft data format
+ internal_host, rest = allocation.split(' : ')
+ external_host, port_range = rest.split(' . ')
+ # Log the parsed data
+ logger.info(
+ f"Internal host: {internal_host.lstrip()}, external host: {external_host}, Port range: {port_range}")
+ except ValueError as e:
+ # Log error message
+ logger.error(f"Error processing line '{allocation}': {e}")
+
if __name__ == '__main__':
try: