diff --git a/.gitignore b/.gitignore index d2a395a..f2a1287 100644 --- a/.gitignore +++ b/.gitignore @@ -19,8 +19,6 @@ __pycache__ *.iml .idea -imalive.yml - # Eclipse Core .project diff --git a/README.md b/README.md index 39925e7..b49a709 100644 --- a/README.md +++ b/README.md @@ -250,7 +250,7 @@ monitors: method: POST # optional (GET by default, only POST, PUT and GET are supported) body: '{"foo": "bar"}' # optional (body is ignored if method is GET) check_tls: false # optional (true by default) - expected_http_code: 200 # optional (200 by default) + expected_http_code: "20*" # optional (20* by default), wildcard means "begin with" expected_contain: "\"status\":\"ok\"" # optional (no check on the body response if not present) timeout: 30 # optional (30 seconds if not present) username: changeit # optional (no basic auth if not present) diff --git a/VERSION b/VERSION index 805226c..86edc68 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.0.19 +4.0.20 diff --git a/src/utils/monitor.py b/src/utils/monitor.py index f3ca23d..a7216aa 100644 --- a/src/utils/monitor.py +++ b/src/utils/monitor.py @@ -1,4 +1,5 @@ import os +import re import yaml import requests import asyncio @@ -17,6 +18,10 @@ from utils.logger import log_msg from utils.otel import get_otel_tracer +def check_status_code_pattern(actual_code, pattern): + regexp = "^{}$".format(pattern.replace('*', '[0-9]+')) + return bool(re.match(regexp, str(actual_code))) + def check_http_monitor(monitor, gauges): vdate = datetime.now() @@ -49,7 +54,7 @@ def check_http_monitor(monitor, gauges): method = get_or_else(monitor, 'method', 'GET') timeout = get_or_else(monitor, 'timeout', 30) - expected_http_code = get_or_else(monitor, 'expected_http_code', 200) + expected_http_code = get_or_else(monitor, 'expected_http_code', '20*') expected_contain = get_or_else(monitor, 'expected_contain', None) body = get_or_else(monitor, 'body', None) check_tls = is_true(get_or_else(monitor, 'check_tls', True)) @@ -94,7 +99,7 @@ def check_http_monitor(monitor, gauges): set_gauge(gauges['result'], 0, {**labels, 'kind': 'result'}) return - if response.status_code != expected_http_code: + if not check_status_code_pattern(response.status_code, expected_http_code): log_msg("ERROR", { "status": "ko", "type": "monitor",