Skip to content

Commit

Permalink
Support wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Dec 24, 2024
1 parent b1bd44b commit 0e988ff
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ __pycache__
*.iml
.idea

imalive.yml

# Eclipse Core
.project

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.19
4.0.20
9 changes: 7 additions & 2 deletions src/utils/monitor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import yaml
import requests
import asyncio
Expand All @@ -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()

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 0e988ff

Please sign in to comment.