Skip to content

Commit cb38c7c

Browse files
committed
Fix ignoring custom monitoring window in per-bot checks
1 parent b7b9bb5 commit cb38c7c

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

intelmq_monitoring_checkmk/checks/bot_status.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99

1010
import logging
11-
from datetime import datetime
11+
from datetime import datetime, timedelta
1212

1313
from ..base import BaseServiceCheck
1414
from ..config import Config
@@ -75,6 +75,14 @@ def bot_config(self):
7575
def bot_parameters(self):
7676
return self.bot_config.get("parameters") or {}
7777

78+
def _get_time_window(self):
79+
cfg_value = self.get_custom_bot_monitoring_config(
80+
self._bot_id, "activity-window-days"
81+
)
82+
if not cfg_value:
83+
return self.config.TIME_WINDOW
84+
return timedelta(days=cfg_value)
85+
7886
def _check_bot_state(self):
7987
"""Verify that enabled bot is running"""
8088
status_code, status_description = self.intelmq_cli.bot_status(self._bot_id)
@@ -90,7 +98,7 @@ def _check_bot_state(self):
9098

9199
def _update_stat_queues_cache(self):
92100
logger.debug("Updating stats from statistic database")
93-
period_start = datetime.utcnow() - self.config.TIME_WINDOW
101+
period_start = datetime.utcnow() - self._get_time_window()
94102
for cache_key, redis_template in STATS_CACHE_MAPPING.items():
95103
current_value = 0
96104
for queue in self.stat_db.keys(redis_template.format(self._bot_id)):
@@ -127,12 +135,12 @@ def _check_collector_processed_data(self):
127135
if not total_sum:
128136
self.writer.status = CheckStatus.CRITICAL
129137
self.writer.add_summary_line(
130-
f"FAIL: Collector processed no data in last {self.config.TIME_WINDOW}"
138+
f"FAIL: Collector processed no data in last {self._get_time_window()}"
131139
)
132140
self.writer.set_short_summary("Collector doesn't produce data")
133141
else:
134142
self.writer.add_summary_line(
135-
f"OK: Collector processed data in last {self.config.TIME_WINDOW}"
143+
f"OK: Collector processed data in last {self._get_time_window()}"
136144
)
137145

138146
@property

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "intelmq_monitoring_checkmk"
7-
version = "0.6.1"
7+
version = "0.6.2"
88
description = "Scripts to monitor IntelMQ instances using CheckMK as a report tool"
99
readme = "README.md"
1010
requires-python = ">=3.7"

tests/test_bot_status.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_check_bot_status_all_failing_checks_reported(
6868

6969

7070
@freeze_time("2023-03-08T12:00:01")
71-
def test_stat_queues_are_cached_and_cleaned(bot_status_check, fake_stats):
71+
def test_stat_queues_are_cached_and_cleaned(bot_status_check, fake_stats, runtime):
7272
fake_stats["test-bot.total._default"] = "5"
7373
fake_stats["test-bot.total.path"] = "2"
7474
fake_stats["test-bot.stats.success"] = "6"
@@ -87,6 +87,8 @@ def test_stat_queues_are_cached_and_cleaned(bot_status_check, fake_stats):
8787
{"time": "2023-03-07T12:00:00", "value": 1},
8888
]
8989

90+
runtime["test-bot"] = {"groupname": "collectors", "enabled": True}
91+
9092
bot_status_check._update_stat_queues_cache()
9193

9294
assert bot_status_check.storage["total-processed-timeline"] == [
@@ -191,6 +193,28 @@ def test_check_collector_proceeds_data_fail(bot_status_check, runtime):
191193
)
192194

193195

196+
def test_check_collector_proceeds_data_custom_length(bot_status_check, runtime):
197+
runtime["test-bot"] = {
198+
"groupname": "collectors",
199+
"enabled": True,
200+
"parameters": {"monitoring-activity-window-days": 14},
201+
}
202+
# this check just uses cache updated by separated method
203+
bot_status_check.storage["total-processed-timeline"] = [
204+
{"time": (datetime.now() - timedelta(days=5)).isoformat(), "value": 2},
205+
{"time": (datetime.now() - timedelta(days=3)).isoformat(), "value": 7},
206+
{"time": (datetime.now() - timedelta(days=1)).isoformat(), "value": 7},
207+
]
208+
209+
bot_status_check._check_collector_processed_data()
210+
211+
assert bot_status_check.writer.status is None
212+
assert (
213+
"OK: Collector processed data in last 14 days, 0:00:00"
214+
in bot_status_check.writer._summary_lines
215+
)
216+
217+
194218
def test_check_collector_proceeds_data_ignored(bot_status_check, runtime):
195219
runtime["test-bot"] = {
196220
"groupname": "collectors",

0 commit comments

Comments
 (0)