From bb5d1e26d8fa79be55c81c88cd8c6dc79f20d2eb Mon Sep 17 00:00:00 2001 From: Eugenio Marzo Date: Thu, 5 Jan 2023 11:01:02 +0100 Subject: [PATCH] fix log --- nginx/KubeInvaders.conf | 12 +++++---- scripts/logs_loop/start.py | 50 +++++++++++++++++++++++++----------- scripts/programming_mode.lua | 5 ++-- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/nginx/KubeInvaders.conf b/nginx/KubeInvaders.conf index 2af3e99..1c403f1 100644 --- a/nginx/KubeInvaders.conf +++ b/nginx/KubeInvaders.conf @@ -150,11 +150,13 @@ server { ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range'; ngx.req.read_body() - data = ngx.req.get_body_data() + local data = ngx.req.get_body_data() local okredis, errredis = red:connect("unix:/tmp/redis.sock") logid = args["logid"] - red:set("log_cleaner:" .. logid, "1") - red:expire("log_cleaner:" .. logid, "10") + red:set("do_not_clean_log:" .. logid, "1") + red:expire("do_not_clean_log:" .. logid, "10") + red:set("logs_enabled:" .. logid, "1") + red:expire("logs_enabled:" .. logid, "10") } } @@ -229,7 +231,7 @@ server { end local function is_key_empty(data, mykey) - cnt = 0 + local cnt = 0 for key, value in pairs(data[mykey]) do cnt = cnt + 1 end @@ -247,7 +249,7 @@ server { ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range'; ngx.req.read_body() - data = ngx.req.get_body_data() + local data = ngx.req.get_body_data() ngx.log(ngx.ERR, "[programming_mode_diagram] data sent from web interface => " .. data) math.randomseed(os.clock()*100000000000) diff --git a/scripts/logs_loop/start.py b/scripts/logs_loop/start.py index 806caca..215c4a9 100644 --- a/scripts/logs_loop/start.py +++ b/scripts/logs_loop/start.py @@ -128,17 +128,17 @@ while True: else: logging.info(f"[logid:{logid}] The Redis key logs_enabled does NOT exists...") - logging.info(f"[logid:{logid}] Checking log_cleaner Redis key") + logging.info(f"[logid:{logid}] Checking do_not_clean_log Redis key") - if not r.exists(f"log_cleaner:{logid}"): - logging.info(f"[logid:{logid}] The key log_cleaner:{logid} does not exists") + if not r.exists(f"do_not_clean_log:{logid}"): + logging.info(f"[logid:{logid}] The key do_not_clean_log:{logid} does not exists") if pathlib.Path(f"/var/www/html/chaoslogs-{logid}.html").exists(): logging.info(f"[logid:{logid}] Remove /var/www/html/chaoslogs-{logid}.html") os.remove(f"/var/www/html/chaoslogs-{logid}.html") - r.set(f"log_cleaner:{logid}", "1") - r.expire(f"log_cleaner:{logid}", 30) + r.set(f"do_not_clean_log:{logid}", "1") + r.expire(f"do_not_clean_log:{logid}", 30) else: - logging.info(f"[logid:{logid}] The key log_cleaner:{logid} esists. Clean /var/www/html/chaoslogs-{logid}.html is not needed") + logging.info(f"[logid:{logid}] The key do_not_clean_log:{logid} exists. Clean /var/www/html/chaoslogs-{logid}.html is not needed") logging.info(f"Loop iteration for log id {logid}") @@ -172,16 +172,36 @@ while True: labels_re = json_re["labels"] containers_re = json_re["containers"] - logging.info(f"[logid:{logid}] Gobal Json Regex is {json_re}") - logging.info(f"[logid:{logid}] Regex for pod name is {pod_re}") - logging.info(f"[logid:{logid}] Regex namespace name {namespace_re}") - logging.info(f"[logid:{logid}] Regex for labels is {labels_re}") - logging.info(f"[logid:{logid}] Regex for annotation is {annotations_re}") + logging.info(f"[logid:{logid}] Gobal Json Regex is |{json_re}|") + logging.info(f"[logid:{logid}] Regex for pod name is |{pod_re}|") + logging.info(f"[logid:{logid}] Regex namespace name |{namespace_re}|") + logging.info(f"[logid:{logid}] Regex for labels is |{labels_re}|") + logging.info(f"[logid:{logid}] Regex for annotation is |{annotations_re}|") for pod in api_response.items: - if re.search(r"{pod_re}", pod.metadata.name) and re.search(r"{namespace_re}", pod.metadata.namespace) and re.search(r"{labels_re}", str(pod.metadata.labels)) and re.search(r"{annotations_re}", str(pod.metadata.annotations)): - webtail_pods.append(pod) - #logging.info(f"[logid:{logid}] Taking log of {pod.metadata.name} because it is compliant with the regex {log_pod_regex}") + logging.info(f"[logid:{logid}] Regex comparison |{pod_re}| |{pod.metadata.name}|") + logging.info(f"[logid:{logid}] Regex comparison |{namespace_re}| |{pod.metadata.namespace}|") + logging.info(f"[logid:{logid}] Regex comparison |{labels_re}| |{str(pod.metadata.labels)}|") + logging.info(f"[logid:{logid}] Regex comparison |{annotations_re}| |{str(pod.metadata.annotations)}|") + + if re.search(f"{pod_re}", pod.metadata.name): + logging.info(f"[logid:{logid}] Regex comparison |{pod_re}| |{pod.metadata.name}| RESULT: OK") + if re.search(f"{namespace_re}", pod.metadata.namespace): + logging.info(f"[logid:{logid}] Regex comparison |{namespace_re}| |{pod.metadata.namespace}| RESULT: OK") + if re.search(f"{labels_re}", str(pod.metadata.labels)): + logging.info(f"[logid:{logid}] Regex comparison |{labels_re}| |{str(pod.metadata.labels)}| RESULT: OK") + if re.search(f"{annotations_re}", str(pod.metadata.annotations)): + logging.info(f"[logid:{logid}] Regex comparison |{annotations_re}| |{str(pod.metadata.annotations)}| RESULT: OK") + webtail_pods.append(pod) + logging.info(f"[logid:{logid}] Taking log of {pod.metadata.name} because it is compliant with the regex {log_pod_regex}") + # else: + # logging.info(f"[logid:{logid}] Regex comparison |{annotations_re}| |{str(pod.metadata.annotations)}| RESULT FAILED!") + # else: + # logging.info(f"[logid:{logid}] Regex comparison |{labels_re}| |{str(pod.metadata.labels)}| RESULT: FAILED!") + # else: + # logging.info(f"[logid:{logid}] Regex comparison |{namespace_re}| |{pod.metadata.namespace}| RESULT: FAILED!") + # else: + # logging.info(f"[logid:{logid}] Regex comparison |{pod_re}| |{pod.metadata.name}| RESULT FAILED!") try: api_response = api_instance.list_namespaced_pod(namespace="kubeinvaders") @@ -190,7 +210,7 @@ while True: webtail_switch = False - if r.get("programming_mode") == "0": + if r.get("programming_mode") == "0": final_pod_list = webtail_pods if len(webtail_pods) > 0: webtail_switch = True diff --git a/scripts/programming_mode.lua b/scripts/programming_mode.lua index 7847ddd..e739adf 100644 --- a/scripts/programming_mode.lua +++ b/scripts/programming_mode.lua @@ -41,10 +41,11 @@ io.close(file) ngx.log(ngx.ERR, "[programming_mode] set programming_mode Redis key") red:set("programming_mode", "1") -ngx.log(ngx.ERR, "[programming_mode] remove log_cleaner:" ..arg['id'] .. " Redis key") -red:del("log_cleaner:" .. arg['id']) +--ngx.log(ngx.ERR, "[programming_mode] remove do_not_clean_log:" ..arg['id'] .. " Redis key") +--red:del("do_not_clean_log:" .. arg['id']) ngx.log(ngx.ERR, "[programming_mode] set logs_enabled:" .. arg['id'] .. " Redis key") + red:set("logs_enabled:" .. arg['id'], "1") red:expire("logs_enabled:" .. arg['id'], "10")