diff options
author | Vladislavs Sokurenko <vladislavs.sokurenko@zabbix.com> | 2020-04-20 16:12:17 +0300 |
---|---|---|
committer | Vladislavs Sokurenko <vladislavs.sokurenko@zabbix.com> | 2020-04-20 16:12:17 +0300 |
commit | 9c7a31a3e9d8bed4aa0259c453dd4ade7e8e6e97 (patch) | |
tree | ec9b034defa1d8f521961b68e4df2ea49b1516a8 | |
parent | b5bb46bd006f66f4f6fa832718cc84d535269c7b (diff) |
........S. [ZBXNEXT-5877] added duration of the script
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | include/common.h | 15 | ||||
-rw-r--r-- | include/zbxembed.h | 2 | ||||
-rw-r--r-- | src/libs/zbxcommon/misc.c | 9 | ||||
-rw-r--r-- | src/libs/zbxembed/embed.c | 10 | ||||
-rw-r--r-- | src/libs/zbxembed/zabbix.c | 5 | ||||
-rw-r--r-- | src/zabbix_js/zabbix_js.c | 2 | ||||
-rw-r--r-- | src/zabbix_server/alerter/alerter.c | 24 | ||||
-rw-r--r-- | src/zabbix_server/preprocessor/item_preproc.c | 2 |
9 files changed, 42 insertions, 28 deletions
diff --git a/.gitignore b/.gitignore index 2dd25dbb6be..e68362a7fa6 100644 --- a/.gitignore +++ b/.gitignore @@ -203,3 +203,4 @@ sbin/zabbix_agent2 etc/zabbix_agent2.conf bin/win32 bin/win64 +/Debug/ diff --git a/include/common.h b/include/common.h index 9dd7aed56f6..acc5431dc70 100644 --- a/include/common.h +++ b/include/common.h @@ -1082,13 +1082,14 @@ void zbx_setproctitle(const char *fmt, ...) __zbx_attr_format_printf(1, 2); /* max length of base64 data */ #define ZBX_MAX_B64_LEN (16 * ZBX_KIBIBYTE) -double zbx_time(void); -void zbx_timespec(zbx_timespec_t *ts); -double zbx_current_time(void); -void zbx_get_time(struct tm *tm, long *milliseconds, zbx_timezone_t *tz); -long zbx_get_timezone_offset(time_t t, struct tm *tm); -int zbx_utc_time(int year, int mon, int mday, int hour, int min, int sec, int *t); -int zbx_day_in_month(int year, int mon); +double zbx_time(void); +void zbx_timespec(zbx_timespec_t *ts); +double zbx_current_time(void); +void zbx_get_time(struct tm *tm, long *milliseconds, zbx_timezone_t *tz); +long zbx_get_timezone_offset(time_t t, struct tm *tm); +int zbx_utc_time(int year, int mon, int mday, int hour, int min, int sec, int *t); +int zbx_day_in_month(int year, int mon); +zbx_uint64_t zbx_get_duration(struct timeval start_time); void zbx_error(const char *fmt, ...) __zbx_attr_format_printf(1, 2); diff --git a/include/zbxembed.h b/include/zbxembed.h index e6258f8896e..0a1e22d6258 100644 --- a/include/zbxembed.h +++ b/include/zbxembed.h @@ -38,7 +38,7 @@ int zbx_es_is_env_initialized(zbx_es_t *es); int zbx_es_fatal_error(zbx_es_t *es); int zbx_es_compile(zbx_es_t *es, const char *script, char **code, int *size, char **error); int zbx_es_execute(zbx_es_t *es, const char *script, const char *code, int size, const char *param, char **output, - char **error); + char **error, zbx_uint64_t *duration); void zbx_es_set_timeout(zbx_es_t *es, int timeout); void zbx_es_set_debug(zbx_es_t *es, struct zbx_json *json); diff --git a/src/libs/zbxcommon/misc.c b/src/libs/zbxcommon/misc.c index e17da86cb02..d3d94e3064f 100644 --- a/src/libs/zbxcommon/misc.c +++ b/src/libs/zbxcommon/misc.c @@ -485,6 +485,15 @@ int zbx_day_in_month(int year, int mon) return 30; } +zbx_uint64_t zbx_get_duration(struct timeval start_time) +{ + struct timeval current_time; + + gettimeofday(¤t_time, NULL); + + return (current_time.tv_sec - start_time.tv_sec) * 1000 + (current_time.tv_usec - start_time.tv_usec) / 1000; +} + /****************************************************************************** * * * Function: zbx_calloc2 * diff --git a/src/libs/zbxembed/embed.c b/src/libs/zbxembed/embed.c index 17731753359..b457fbc6f25 100644 --- a/src/libs/zbxembed/embed.c +++ b/src/libs/zbxembed/embed.c @@ -429,13 +429,15 @@ out: * * ******************************************************************************/ int zbx_es_execute(zbx_es_t *es, const char *script, const char *code, int size, const char *param, char **output, - char **error) + char **error, zbx_uint64_t *duration) { void *buffer; volatile int ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__); + gettimeofday(&es->env->start_time, NULL); + if (SUCCEED == zbx_es_fatal_error(es)) { *error = zbx_strdup(*error, "cannot continue javascript processing after fatal scripting engine error"); @@ -455,8 +457,6 @@ int zbx_es_execute(zbx_es_t *es, const char *script, const char *code, int size, duk_load_function(es->env->ctx); duk_push_string(es->env->ctx, param); - gettimeofday(&es->env->start_time, NULL); - if (DUK_EXEC_SUCCESS != duk_pcall(es->env->ctx, 1)) { duk_small_int_t rc = 0; @@ -498,6 +498,9 @@ int zbx_es_execute(zbx_es_t *es, const char *script, const char *code, int size, duk_pop(es->env->ctx); es->env->rt_error_num = 0; out: + if (NULL != duration) + *duration = zbx_get_duration(es->env->start_time); + zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s %s", __func__, zbx_result_string(ret), ZBX_NULL2EMPTY_STR(*error)); return ret; @@ -522,3 +525,4 @@ void zbx_es_set_debug(zbx_es_t *es, struct zbx_json *json) { es->env->json = json; } + diff --git a/src/libs/zbxembed/zabbix.c b/src/libs/zbxembed/zabbix.c index 14ffff0ed14..ac30da2d6a9 100644 --- a/src/libs/zbxembed/zabbix.c +++ b/src/libs/zbxembed/zabbix.c @@ -70,7 +70,6 @@ static duk_ret_t es_zabbix_log(duk_context *ctx) const char *message; int level; duk_memory_functions out_funcs; - struct timeval tv; level = duk_to_int(ctx, 0); message = duk_to_string(ctx, 1); @@ -86,9 +85,7 @@ static duk_ret_t es_zabbix_log(duk_context *ctx) zbx_json_addobject(env->json, NULL); zbx_json_adduint64(env->json, "level", (zbx_uint64_t)level); - gettimeofday(&tv, NULL); - zbx_json_adduint64(env->json, "ms", (tv.tv_sec - env->start_time.tv_sec) * 1000 + - (tv.tv_usec - env->start_time.tv_usec) / 1000); + zbx_json_adduint64(env->json, "ms", zbx_get_duration(env->start_time)); zbx_json_addstring(env->json, "message", message, ZBX_JSON_TYPE_STRING); zbx_json_close(env->json); diff --git a/src/zabbix_js/zabbix_js.c b/src/zabbix_js/zabbix_js.c index 03904f5c414..6508220e6fd 100644 --- a/src/zabbix_js/zabbix_js.c +++ b/src/zabbix_js/zabbix_js.c @@ -163,7 +163,7 @@ static char *execute_script(const char *script, const char *param, int timeout, goto out; } - if (FAIL == zbx_es_execute(&es, script, code, size, param, &result, &errmsg)) + if (FAIL == zbx_es_execute(&es, script, code, size, param, &result, &errmsg, NULL)) { *error = zbx_dsprintf(NULL, "cannot execute script: %s", errmsg); goto out; diff --git a/src/zabbix_server/alerter/alerter.c b/src/zabbix_server/alerter/alerter.c index 189209d0525..d8c047ebd9a 100644 --- a/src/zabbix_server/alerter/alerter.c +++ b/src/zabbix_server/alerter/alerter.c @@ -212,9 +212,10 @@ static void alerter_process_exec(zbx_ipc_socket_t *socket, zbx_ipc_message_t *ip ******************************************************************************/ static void alerter_process_webhook(zbx_ipc_socket_t *socket, zbx_ipc_message_t *ipc_message) { - char *script_bin = NULL, *params = NULL, *error = NULL, *output = NULL; - int script_bin_sz, ret, timeout; + char *script_bin = NULL, *params = NULL, *error = NULL, *output = NULL; + int script_bin_sz, ret, timeout; struct zbx_json json; + zbx_uint64_t duration = 0; zbx_alerter_deserialize_webhook(ipc_message->data, &script_bin, &script_bin_sz, &timeout, ¶ms); @@ -232,11 +233,9 @@ static void alerter_process_webhook(zbx_ipc_socket_t *socket, zbx_ipc_message_t { zbx_es_set_timeout(&es_engine, timeout); - ret = zbx_es_execute(&es_engine, NULL, script_bin, script_bin_sz, params, &output, &error); + ret = zbx_es_execute(&es_engine, NULL, script_bin, script_bin_sz, params, &output, &error, &duration); } - - if (SUCCEED == zbx_es_fatal_error(&es_engine)) { char *errmsg = NULL; @@ -248,18 +247,21 @@ static void alerter_process_webhook(zbx_ipc_socket_t *socket, zbx_ipc_message_t } } + if (ZBX_IPC_ALERTER_WEBHOOK_EXTERNAL == ipc_message->code) + { + zbx_json_close(&json); + zbx_json_adduint64(&json, "ms", duration); + zabbix_log(LOG_LEVEL_INFORMATION, "json '%s'", json.buffer); + zbx_json_free(&json); + zbx_es_set_debug(&es_engine, NULL); + } + alerter_send_result(socket, output, ret, error); zbx_free(output); zbx_free(error); zbx_free(params); zbx_free(script_bin); - - if (ZBX_IPC_ALERTER_WEBHOOK_EXTERNAL == ipc_message->code) - { - zbx_json_free(&json); - zbx_es_set_debug(&es_engine, NULL); - } } /****************************************************************************** diff --git a/src/zabbix_server/preprocessor/item_preproc.c b/src/zabbix_server/preprocessor/item_preproc.c index 4be0598b8b0..ab250f7b79c 100644 --- a/src/zabbix_server/preprocessor/item_preproc.c +++ b/src/zabbix_server/preprocessor/item_preproc.c @@ -1590,7 +1590,7 @@ static int item_preproc_script(zbx_variant_t *value, const char *params, zbx_var size = zbx_variant_data_bin_get(bytecode->data.bin, (void **)&code); - if (SUCCEED == zbx_es_execute(&es_engine, params, code, size, value->data.str, &output, errmsg)) + if (SUCCEED == zbx_es_execute(&es_engine, params, code, size, value->data.str, &output, errmsg, NULL)) { zbx_variant_clear(value); |