Skip to content

Commit 34ead79

Browse files
sebixaaronkaplan
authored andcommitted
wip: ctl: fix logging level filtering
fixes #2596
1 parent 05f1176 commit 34ead79

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Please refer to the [NEWS](NEWS.md) for a list of changes which have an affect o
7373
- `intelmq.bin.intelmq_psql_initdb`: Use `JSONB` type by default, Postgres supports it since version 9 (PR#2597 by Sebastian Wagner).
7474
- `intelmq.bin.rewrite_config_files`: Removed obsolete JSON configuration file rewriter (PR#2613 by Sebastian Wagner).
7575
- `intelmq/lib/bot_debugger.py`: Fix overwriting the runtime logging level by command line parameter (PR#2603 by Sebastian Wagner, fixes #2563).
76+
- `intelmq.bin.intelmqctl`: Fix bot log level filtering (PR#2607 by Sebastian Wagner, fixes #2596).
7677

7778
### Contrib
7879
- Bash Completion: Adapt to YAML-style runtime configuration (PR#2642 by Sebastian Wagner, fixes #2094).

intelmq/bin/intelmqctl.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,8 @@ def clear_queue(self, queue):
801801
self._logger.exception("Error while clearing queue %s.", queue)
802802
return 1, 'error'
803803

804-
def read_bot_log(self, bot_id, log_level, number_of_lines):
804+
def read_bot_log(self, bot_id: str, log_level: str, number_of_lines: int):
805+
""" Read logs of a bot filtered by logging level """
805806
if self._parameters.logging_handler == 'file':
806807
bot_log_path = os.path.join(self._parameters.logging_path,
807808
bot_id + '.log')
@@ -829,13 +830,13 @@ def read_bot_log(self, bot_id, log_level, number_of_lines):
829830
if self._parameters.logging_handler == 'syslog':
830831
log_message = utils.parse_logline(line, regex=utils.SYSLOG_REGEX)
831832

832-
if type(log_message) is not dict:
833+
if not isinstance(log_message, dict):
833834
if self._parameters.logging_handler == 'file':
834835
message_overflow = '\n'.join([line, message_overflow])
835836
continue
836837
if log_message['bot_id'] != bot_id:
837838
continue
838-
if LogLevel[log_message['log_level']].value > LogLevel[log_level].value:
839+
if LogLevel[log_message['log_level']].value < LogLevel[log_level].value:
839840
continue
840841

841842
if message_overflow:

intelmq/tests/assets/test-bot.log

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2025-04-23 23:17:52,212 - test-bot - INFO - ASNLookupExpertBot initialized with id asn-expert and intelmq 3.4.1.alpha1 and python 3.13.2 (main, Feb 05 2025, 09:57:44) [GCC] as process 257689. Standalone mode: True.
2+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Library path: '/home/sebastianw/dev/intelmq/intelmq/lib/bot.py'.
3+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Loading runtime configuration from '/etc/intelmq/runtime.yaml'.
4+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Defaults configuration: parameter 'autoupdate_cached_database' loaded with value False.
5+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Defaults configuration: parameter 'destination_pipeline_broker' loaded with value 'redis'.
6+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Defaults configuration: parameter 'process_manager' loaded with value 'intelmq'.
7+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Defaults configuration: parameter 'source_pipeline_broker' loaded with value 'redis'.
8+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Defaults configuration: parameter 'ssl_ca_certificate' loaded with value None.
9+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Defaults configuration: parameter 'statistics_database' loaded with value 3.
10+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Defaults configuration: parameter 'statistics_host' loaded with value '127.0.0.1'.
11+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Defaults configuration: parameter 'statistics_password' loaded with value 'HIDDEN'.
12+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Defaults configuration: parameter 'statistics_port' loaded with value 6379.
13+
2025-04-23 23:17:52,212 - test-bot - DEBUG - System configuration: parameter 'module' loaded with value 'intelmq.bots.experts.asn_lookup.expert'.
14+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Runtime configuration: parameter 'database' loaded with value '/etc/resolv.conf'.
15+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Runtime configuration: parameter 'autoupdate_cached_database' loaded with value True.
16+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Environment configuration: parameter 'paths_no_opt' loaded with value 1.
17+
2025-04-23 23:17:52,212 - test-bot - INFO - Bot is starting.
18+
2025-04-23 23:17:52,212 - test-bot - DEBUG - Loading Harmonization configuration from '/etc/intelmq/harmonization.conf'.
19+
2025-04-23 23:17:52,239 - test-bot - INFO - Loading source pipeline and queue 'asn-expert-queue'.
20+
2025-04-23 23:17:52,240 - test-bot - INFO - Connected to source queue.
21+
2025-04-23 23:17:52,240 - test-bot - WARNING - Something is abnormal.
22+
2025-04-23 23:17:52,240 - test-bot - INFO - No destination queues to load.
23+
2025-04-23 23:17:52,240 - test-bot - ERROR - Bot initialization failed.
24+
Traceback (most recent call last):
25+
File "/home/sebastianw/dev/intelmq/intelmq/lib/bot.py", line 241, in __init__
26+
self.init()
27+
~~~~~~~~~^^
28+
File "/home/sebastianw/dev/intelmq/intelmq/bots/experts/asn_lookup/expert.py", line 37, in init
29+
self._database = pyasn.pyasn(self.database)
30+
~~~~~~~~~~~^^^^^^^^^^^^^^^
31+
File "/usr/lib64/python3.13/site-packages/pyasn/__init__.py", line 71, in __init__
32+
self._records = self.radix.load_ipasndb(ipasn_file, "")
33+
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
34+
RuntimeError: Error while parsing/adding IPASN database (record: 1)!
35+
2025-04-23 23:17:52,241 - test-bot - DEBUG - Disconnected from source pipeline.
36+
2025-04-23 23:17:52,241 - test-bot - INFO - Bot stopped.
37+
2025-04-23 23:17:52,241 - test-bot - INFO - Bot stopped.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SPDX-FileCopyrightText: 2025 Institute for Common Good Technology
2+
SPDX-License-Identifier: AGPL-3.0-or-later

intelmq/tests/bin/test_intelmqctl.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import unittest
88
from tempfile import TemporaryDirectory
99
from unittest import mock
10+
from pathlib import Path
1011

1112
from pkg_resources import resource_filename
1213

@@ -137,6 +138,28 @@ def test_check_imports_real_bot_module(self):
137138

138139
import_mock.assert_called_once_with("mocked-module")
139140

141+
def test_intelmqctl_log(self):
142+
path = Path(__file__).absolute().parent / '../assets/'
143+
self.intelmqctl._parameters.logging_path = path
144+
retval, error_log = self.intelmqctl.read_bot_log('test-bot', 'ERROR', 10)
145+
assert retval == 0 and len(error_log) == 1
146+
assert error_log[0]['extended_message'].startswith('Traceback')
147+
del error_log[0]['extended_message']
148+
assert error_log[0] == {'date': '2025-04-23T23:17:52.240000',
149+
'bot_id': 'test-bot', 'thread_id': None, 'log_level': 'ERROR',
150+
'message': 'Bot initialization failed.'}
151+
for level, allowed_levels, number_messages in (
152+
('DEBUG', ('DEBUG', 'INFO', 'WARNING', 'ERROR'), 26),
153+
('INFO', ('INFO', 'WARNING', 'ERROR'), 9),
154+
('WARNING', ('WARNING', 'ERROR'), 2),
155+
('ERROR', ('ERROR', ), 1),
156+
('CRITICAL', ('CRITICAL', ), 0),
157+
):
158+
retval, log_messages = self.intelmqctl.read_bot_log('test-bot', level, 30)
159+
assert retval == 0 and len(log_messages) == number_messages
160+
for log_message in log_messages:
161+
assert log_message['log_level'] in allowed_levels
162+
140163

141164
if __name__ == '__main__': # pragma: nocover
142165
unittest.main()

0 commit comments

Comments
 (0)