From e6ba14f6791965101c8b70d62645cdcaf8ccd301 Mon Sep 17 00:00:00 2001 From: Dmitrijs Goloscapovs Date: Tue, 4 Apr 2023 10:03:14 -0400 Subject: [PATCH] ........S. [ZBX-22492] removed limitation for size of logged messages in JS (cherry picked from commit 562776f4893db7ed018fbdafbcee9e8d7433f938) --- src/libs/zbxembed/console.c | 27 +++++++++++++++++---------- src/libs/zbxembed/embed.c | 1 + src/libs/zbxembed/embed.h | 3 ++- src/libs/zbxembed/zabbix.c | 27 +++++++++++++++++---------- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/libs/zbxembed/console.c b/src/libs/zbxembed/console.c index 64b4041a4bb..2a5e6ccb658 100644 --- a/src/libs/zbxembed/console.c +++ b/src/libs/zbxembed/console.c @@ -88,25 +88,32 @@ static duk_ret_t es_log_message(duk_context *ctx, int level) duk_get_memory_functions(ctx, &out_funcs); env = (zbx_es_env_t *)out_funcs.udata; - if (ZBX_ES_LOG_MEMORY_LIMIT < env->log_size) + if (ZBX_ES_LOG_MSG_LIMIT < env->logged_msgs) { - err_index = duk_push_error_object(ctx, DUK_RET_EVAL_ERROR, "log exceeds the maximum size of " - ZBX_FS_UI64 " bytes.", ZBX_ES_LOG_MEMORY_LIMIT); + err_index = duk_push_error_object(ctx, DUK_RET_EVAL_ERROR, + "maximum count of logged messages was reached"); goto out; } zabbix_log(level, "%s", msg_output); - if (NULL != env->json) + if (NULL == env->json) + goto out; + + if (ZBX_ES_LOG_MEMORY_LIMIT < env->json->buffer_size) /* approximate limit */ { - zbx_json_addobject(env->json, NULL); - zbx_json_adduint64(env->json, "level", (zbx_uint64_t)level); - zbx_json_adduint64(env->json, "ms", zbx_get_duration_ms(&env->start_time)); - zbx_json_addstring(env->json, "message", msg_output, ZBX_JSON_TYPE_STRING); - zbx_json_close(env->json); + err_index = duk_push_error_object(ctx, DUK_RET_EVAL_ERROR, "log exceeds the maximum size of " + ZBX_FS_UI64 " bytes.", ZBX_ES_LOG_MEMORY_LIMIT); + goto out; } + + zbx_json_addobject(env->json, NULL); + zbx_json_adduint64(env->json, "level", (zbx_uint64_t)level); + zbx_json_adduint64(env->json, "ms", zbx_get_duration_ms(&env->start_time)); + zbx_json_addstring(env->json, "message", msg_output, ZBX_JSON_TYPE_STRING); + zbx_json_close(env->json); out: - env->log_size += strlen(msg_output); + env->logged_msgs++; zbx_free(msg_output); if (-1 != err_index) diff --git a/src/libs/zbxembed/embed.c b/src/libs/zbxembed/embed.c index d3335573d3e..e44084da90e 100644 --- a/src/libs/zbxembed/embed.c +++ b/src/libs/zbxembed/embed.c @@ -545,6 +545,7 @@ int zbx_es_execute(zbx_es_t *es, const char *script, const char *code, int size, zbx_timespec(&es->env->start_time); es->env->http_req_objects = 0; + es->env->logged_msgs = 0; if (NULL != es->env->json) { diff --git a/src/libs/zbxembed/embed.h b/src/libs/zbxembed/embed.h index 8ad8dc6f13d..7ddc01f046a 100644 --- a/src/libs/zbxembed/embed.h +++ b/src/libs/zbxembed/embed.h @@ -24,6 +24,7 @@ #include "zbxtime.h" #define ZBX_ES_LOG_MEMORY_LIMIT (ZBX_MEBIBYTE * 8) +#define ZBX_ES_LOG_MSG_LIMIT 8000 /* this macro can be used in time intensive C functions to check for script timeout execution */ #define ZBX_ES_CHECK_TIMEOUT(ctx, env) \ @@ -50,7 +51,7 @@ struct zbx_es_env jmp_buf loc; int http_req_objects; - size_t log_size; + int logged_msgs; }; zbx_es_env_t *zbx_es_get_env(duk_context *ctx); diff --git a/src/libs/zbxembed/zabbix.c b/src/libs/zbxembed/zabbix.c index 96cdb9f9471..98b76cb042a 100644 --- a/src/libs/zbxembed/zabbix.c +++ b/src/libs/zbxembed/zabbix.c @@ -78,25 +78,32 @@ static duk_ret_t es_zabbix_log(duk_context *ctx) duk_get_memory_functions(ctx, &out_funcs); env = (zbx_es_env_t *)out_funcs.udata; - if (ZBX_ES_LOG_MEMORY_LIMIT < env->log_size) + if (ZBX_ES_LOG_MSG_LIMIT < env->logged_msgs) { - err_index = duk_push_error_object(ctx, DUK_RET_EVAL_ERROR, "log exceeds the maximum size of " - ZBX_FS_UI64 " bytes.", ZBX_ES_LOG_MEMORY_LIMIT); + err_index = duk_push_error_object(ctx, DUK_RET_EVAL_ERROR, + "maximum count of logged messages was reached"); goto out; } zabbix_log(level, "%s", message); - if (NULL != env->json) + if (NULL == env->json) + goto out; + + if (ZBX_ES_LOG_MEMORY_LIMIT < env->json->buffer_size) /* approximate limit */ { - zbx_json_addobject(env->json, NULL); - zbx_json_adduint64(env->json, "level", (zbx_uint64_t)level); - zbx_json_adduint64(env->json, "ms", zbx_get_duration_ms(&env->start_time)); - zbx_json_addstring(env->json, "message", message, ZBX_JSON_TYPE_STRING); - zbx_json_close(env->json); + err_index = duk_push_error_object(ctx, DUK_RET_EVAL_ERROR, "log exceeds the maximum size of " + ZBX_FS_UI64 " bytes.", ZBX_ES_LOG_MEMORY_LIMIT); + goto out; } + + zbx_json_addobject(env->json, NULL); + zbx_json_adduint64(env->json, "level", (zbx_uint64_t)level); + zbx_json_adduint64(env->json, "ms", zbx_get_duration_ms(&env->start_time)); + zbx_json_addstring(env->json, "message", message, ZBX_JSON_TYPE_STRING); + zbx_json_close(env->json); out: - env->log_size += strlen(message); + env->logged_msgs++; zbx_free(message); if (-1 != err_index) -- 2.30.2