Skip to content
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
23 changes: 15 additions & 8 deletions package/shared/pylib/parser_source_cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ipaddress
import traceback
import socket
import struct
Expand Down Expand Up @@ -45,6 +46,12 @@ def int_to_ip6(num):
return int_to_ip6(addr)


def ip_cache_key(addr):
ip = ipaddress.ip_address(addr)
prefix = "v4" if ip.version == 4 else "v6"
return f"{prefix}:{ip}"


hostdict = str("/var/lib/syslog-ng/hostip")


Expand All @@ -60,10 +67,10 @@ def deinit(self):
def parse(self, log_message):
try:
ipaddr = log_message.get_as_str("SOURCEIP", "", repr="internal")
ip_int = ip2int(ipaddr)
self.logger.debug(f"psc.parse sourceip={ipaddr} int={ip_int}")
ip_key = ip_cache_key(ipaddr)
self.logger.debug(f"psc.parse sourceip={ipaddr} key={ip_key}")
try:
name = self.db[ip_int]
name = self.db[ip_key]
except KeyError:
return False
self.logger.debug(f"psc.parse host={name}")
Expand Down Expand Up @@ -120,20 +127,20 @@ def send(self, log_message):
return self.SUCCESS

try:
ip_int = ip2int(ipaddr)
except OSError:
ip_key = ip_cache_key(ipaddr)
except ValueError:
self.logger.debug(
f"psc.send skipped: invalid SOURCEIP sourceip={ipaddr!r}"
)
return self.SUCCESS

try:
current = self.db[ip_int]
current = self.db[ip_key]
except KeyError:
self.db[ip_int] = host
self.db[ip_key] = host
else:
if current != host:
self.db[ip_int] = host
self.db[ip_key] = host
except Exception:
self.logger.debug(traceback.format_exc())
return self.ERROR
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cache_destinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_name_cache_new_entry_is_published_only_when_batch_is_flushed(
assert destination.init({}) is True
assert destination.open() is True
reader = SqliteDict(database_file, outer_stack=False)
database_key = 3221225986
database_key = "v4:192.0.2.2"

try:
result = destination.send(
Expand Down Expand Up @@ -153,7 +153,7 @@ def test_name_cache_changed_entry_is_published_only_after_flush(
)
assert destination.init({}) is True
assert destination.open() is True
database_key = 3221225986
database_key = "v4:192.0.2.2"
destination.db[database_key] = "old-cache-host"
destination.db.commit()
reader = SqliteDict(database_file, outer_stack=False)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cache_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_name_cache_miss_preserves_message_without_logging_traceback():


def test_name_cache_hit_applies_cached_host():
parser = make_name_cache_parser({3221225994: "cache-host"})
parser = make_name_cache_parser({"v4:192.0.2.10": "cache-host"})
message = LogMessage(
{
"SOURCEIP": "192.0.2.10",
Expand All @@ -73,7 +73,7 @@ def test_name_cache_hit_applies_cached_host():


def test_name_cache_unexpected_hit_failure_logs_traceback():
parser = make_name_cache_parser({3221225994: "cache-host"})
parser = make_name_cache_parser({"v4:192.0.2.10": "cache-host"})
message = RejectingLogMessage(
{
"SOURCEIP": "192.0.2.10",
Expand Down
Loading