From 767f3214f4d93f9278bc2f87ca8bd8109cb52171 Mon Sep 17 00:00:00 2001 From: Mihails Prihodko Date: Wed, 7 Jun 2023 10:17:00 +0300 Subject: [PATCH 1/4] ...G...... [ZBX-21560] fixed default Zabbix agent 1 and 2 config file name and path for Windows; fixed logging to stderr in Zabbix agent 2 --- ChangeLog.d/bugfix/ZBX-21560 | 1 + src/go/cmd/zabbix_agent2/zabbix_agent2.go | 3 +- .../zabbix_agent2/zabbix_agent2_windows.go | 2 +- src/zabbix_agent/zabbix_agentd.c | 55 +++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 ChangeLog.d/bugfix/ZBX-21560 diff --git a/ChangeLog.d/bugfix/ZBX-21560 b/ChangeLog.d/bugfix/ZBX-21560 new file mode 100644 index 000000000000..b32b48795116 --- /dev/null +++ b/ChangeLog.d/bugfix/ZBX-21560 @@ -0,0 +1 @@ +...G...... [ZBX-21560] fixed default Zabbix agent 1 and 2 config file name and path for Windows; fixed logging to stderr in Zabbix agent 2 (mprihodko) diff --git a/src/go/cmd/zabbix_agent2/zabbix_agent2.go b/src/go/cmd/zabbix_agent2/zabbix_agent2.go index febf04d906b4..0a90c7da97b3 100644 --- a/src/go/cmd/zabbix_agent2/zabbix_agent2.go +++ b/src/go/cmd/zabbix_agent2/zabbix_agent2.go @@ -621,10 +621,11 @@ func fatalExit(message string, err error) { message = fmt.Sprintf("%s: %s", message, err.Error()) } + fmt.Fprintf(os.Stderr, "zabbix_agent2 [%d]: ERROR: %s\n", os.Getpid(), message) + if agent.Options.LogType == "file" { log.Critf("%s", message) } - fmt.Fprintf(os.Stderr, "zabbix_agent2 [%d]: ERROR: %s\n", os.Getpid(), message) os.Exit(1) } diff --git a/src/go/cmd/zabbix_agent2/zabbix_agent2_windows.go b/src/go/cmd/zabbix_agent2/zabbix_agent2_windows.go index 99613d31dc7a..9679ed030dce 100644 --- a/src/go/cmd/zabbix_agent2/zabbix_agent2_windows.go +++ b/src/go/cmd/zabbix_agent2/zabbix_agent2_windows.go @@ -41,7 +41,7 @@ func loadOSDependentItems() error { func init() { if path, err := os.Executable(); err == nil { dir, name := filepath.Split(path) - confDefault = dir + strings.TrimSuffix(name, filepath.Ext(name)) + ".win.conf" + confDefault = dir + strings.TrimSuffix(name, filepath.Ext(name)) + ".conf" } } diff --git a/src/zabbix_agent/zabbix_agentd.c b/src/zabbix_agent/zabbix_agentd.c index 0c200dc34531..69dd2af7d96e 100644 --- a/src/zabbix_agent/zabbix_agentd.c +++ b/src/zabbix_agent/zabbix_agentd.c @@ -149,7 +149,11 @@ const char *help_message[] = { "", "Options:", " -c --config config-file Path to the configuration file", +#ifdef _WINDOWS + " (default: \"\\zabbix_agentd.conf\")", +#else " (default: \"" DEFAULT_CONFIG_FILE "\")", +#endif " -f --foreground Run Zabbix agent in foreground", " -p --print Print known items and exit", " -t --test item-key Test specified item and exit", @@ -347,6 +351,7 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) char ch; #ifdef _WINDOWS unsigned int opt_mask = 0; + char *process_name = NULL, *process_path = NULL, *progname_noext = NULL; #endif unsigned short opt_count[256] = {0}; @@ -547,8 +552,58 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) } if (NULL == config_file) + { +#ifdef _WINDOWS +#define PATH_BUF_LEN 4096 + size_t alloc_len = 0, offset = 0; + char *ptr; + wchar_t szProcessName[PATH_BUF_LEN]; + + if (0 == GetModuleFileNameEx(GetCurrentProcess(), NULL, szProcessName, ARRSIZE(szProcessName))) + { + zbx_error("failed to get Zabbix agent executable file path while initializing default config" + " path"); + ret = FAIL; + goto out; + } + + process_name = zbx_unicode_to_utf8(szProcessName); + + if (NULL == (ptr = strstr(process_name, progname))) + { + zbx_error("got unexpected Zabbix agent executable file path '%s' while initializing" + " default config path", process_name); + ret = FAIL; + goto out; + } + + zbx_strncpy_alloc(&process_path, &alloc_len, &offset, process_name, ptr - process_name); + + if (NULL == (ptr = strstr(progname, "."))) + { + zbx_error("failed to get Zabbix agent executable file name with file extension removed for" + " program name '%s' while initializing default config path", progname); + ret = FAIL; + goto out; + } + + zbx_strncpy_alloc(&progname_noext, &alloc_len, &offset, progname, + ptr - progname); + + alloc_len = 0, offset = 0; + + zbx_snprintf_alloc(&config_file, &alloc_len, &offset, "%s%s.conf", process_path, progname_noext); +#undef PATH_BUF_LEN +#else config_file = zbx_strdup(NULL, DEFAULT_CONFIG_FILE); +#endif + } out: +#ifdef _WINDOWS + zbx_free(process_name); + zbx_free(process_path); + zbx_free(progname_noext); +#endif if (FAIL == ret) { zbx_free(TEST_METRIC); From e0ca5d6c7a76469840796382ebfdb1c71dae9529 Mon Sep 17 00:00:00 2001 From: Mihails Prihodko Date: Tue, 11 Jul 2023 16:24:39 +0300 Subject: [PATCH 2/4] ...G...... [ZBX-21560] fixed problem with path to Zabbix agent containing directory zabbix_agentd.exe, removed redundant memory buffer --- src/zabbix_agent/zabbix_agentd.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/zabbix_agent/zabbix_agentd.c b/src/zabbix_agent/zabbix_agentd.c index 69dd2af7d96e..f12cacdb7480 100644 --- a/src/zabbix_agent/zabbix_agentd.c +++ b/src/zabbix_agent/zabbix_agentd.c @@ -351,7 +351,7 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) char ch; #ifdef _WINDOWS unsigned int opt_mask = 0; - char *process_name = NULL, *process_path = NULL, *progname_noext = NULL; + char *process_path = NULL, *progname_noext = NULL; #endif unsigned short opt_count[256] = {0}; @@ -555,7 +555,7 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) { #ifdef _WINDOWS #define PATH_BUF_LEN 4096 - size_t alloc_len = 0, offset = 0; + size_t alloc_len, offset; char *ptr; wchar_t szProcessName[PATH_BUF_LEN]; @@ -567,30 +567,36 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) goto out; } - process_name = zbx_unicode_to_utf8(szProcessName); + process_path = zbx_unicode_to_utf8(szProcessName); - if (NULL == (ptr = strstr(process_name, progname))) + if (NULL == (ptr = strstr(process_path, progname))) { zbx_error("got unexpected Zabbix agent executable file path '%s' while initializing" - " default config path", process_name); + " default config path", process_path); ret = FAIL; goto out; } + else + { + const char *ptr_next; + + while (NULL != (ptr_next = strstr(ptr + 1, progname))) + ptr = ptr_next; + } + + *ptr = '\0'; - zbx_strncpy_alloc(&process_path, &alloc_len, &offset, process_name, ptr - process_name); + progname_noext = strdup(progname); - if (NULL == (ptr = strstr(progname, "."))) + if (NULL == (ptr = strstr(progname_noext, ".exe"))) { zbx_error("failed to get Zabbix agent executable file name with file extension removed for" - " program name '%s' while initializing default config path", progname); + " program name '%s' while initializing default config path", progname_noext); ret = FAIL; goto out; } - zbx_strncpy_alloc(&progname_noext, &alloc_len, &offset, progname, - ptr - progname); - - alloc_len = 0, offset = 0; + *ptr = '\0'; zbx_snprintf_alloc(&config_file, &alloc_len, &offset, "%s%s.conf", process_path, progname_noext); #undef PATH_BUF_LEN @@ -600,7 +606,6 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) } out: #ifdef _WINDOWS - zbx_free(process_name); zbx_free(process_path); zbx_free(progname_noext); #endif From b412313e6c40a7feae03ba252b4b82ec62083385 Mon Sep 17 00:00:00 2001 From: Michael Veksler Date: Tue, 22 Aug 2023 11:43:43 +0300 Subject: [PATCH 3/4] ...G...... [ZBX-21560] refactored conf path creation --- src/zabbix_agent/zabbix_agentd.c | 52 ++++++++------------------------ 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/src/zabbix_agent/zabbix_agentd.c b/src/zabbix_agent/zabbix_agentd.c index a6046b386eef..e8728b9c1759 100644 --- a/src/zabbix_agent/zabbix_agentd.c +++ b/src/zabbix_agent/zabbix_agentd.c @@ -351,7 +351,6 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) char ch; #ifdef _WINDOWS unsigned int opt_mask = 0; - char *process_path = NULL, *progname_noext = NULL; #endif unsigned short opt_count[256] = {0}; @@ -551,64 +550,39 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) goto out; } - if (NULL == config_file) - { #ifdef _WINDOWS #define PATH_BUF_LEN 4096 - size_t alloc_len, offset; - char *ptr; + if (NULL == config_file) + { + char *ptr, *process_path = NULL; wchar_t szProcessName[PATH_BUF_LEN]; if (0 == GetModuleFileNameEx(GetCurrentProcess(), NULL, szProcessName, ARRSIZE(szProcessName))) { zbx_error("failed to get Zabbix agent executable file path while initializing default config" " path"); - ret = FAIL; - goto out; + goto skip; } process_path = zbx_unicode_to_utf8(szProcessName); - if (NULL == (ptr = strstr(process_path, progname))) + if (NULL == (ptr = get_program_name(process_path))) { zbx_error("got unexpected Zabbix agent executable file path '%s' while initializing" - " default config path", process_path); - ret = FAIL; - goto out; - } - else - { - const char *ptr_next; - - while (NULL != (ptr_next = strstr(ptr + 1, progname))) - ptr = ptr_next; + " default config path", ptr); + goto skip; } *ptr = '\0'; - - progname_noext = strdup(progname); - - if (NULL == (ptr = strstr(progname_noext, ".exe"))) - { - zbx_error("failed to get Zabbix agent executable file name with file extension removed for" - " program name '%s' while initializing default config path", progname_noext); - ret = FAIL; - goto out; - } - - *ptr = '\0'; - - zbx_snprintf_alloc(&config_file, &alloc_len, &offset, "%s%s.conf", process_path, progname_noext); + config_file = zbx_dsprintf(config_file, "%s%s", process_path, get_program_name(DEFAULT_CONFIG_FILE)); +skip: + zbx_free(process_path); + } #undef PATH_BUF_LEN -#else - config_file = zbx_strdup(NULL, DEFAULT_CONFIG_FILE); #endif - } + if (NULL == config_file) + config_file = zbx_strdup(NULL, DEFAULT_CONFIG_FILE); out: -#ifdef _WINDOWS - zbx_free(process_path); - zbx_free(progname_noext); -#endif if (FAIL == ret) { zbx_free(TEST_METRIC); From f43aea924d5db923078a597a70aaa831811cf4fb Mon Sep 17 00:00:00 2001 From: Michael Veksler Date: Tue, 22 Aug 2023 16:40:18 +0300 Subject: [PATCH 4/4] ...G...... [ZBX-21560] added info about conf full path to help message --- include/zbxcommon.h | 2 +- src/libs/zbxcommon/misc.c | 17 ++++++++++++++++- src/zabbix_agent/zabbix_agentd.c | 10 ++++++++-- src/zabbix_get/zabbix_get.c | 2 +- src/zabbix_js/zabbix_js.c | 2 +- src/zabbix_proxy/proxy.c | 2 +- src/zabbix_sender/zabbix_sender.c | 2 +- src/zabbix_server/server.c | 2 +- 8 files changed, 30 insertions(+), 9 deletions(-) diff --git a/include/zbxcommon.h b/include/zbxcommon.h index 2cca4d115b9c..67e09059fb95 100644 --- a/include/zbxcommon.h +++ b/include/zbxcommon.h @@ -462,7 +462,7 @@ extern const char *help_message[]; #define ARRSIZE(a) (sizeof(a) / sizeof(*a)) -void zbx_help(void); +void zbx_help(const char *param); void zbx_usage(void); void zbx_version(void); diff --git a/src/libs/zbxcommon/misc.c b/src/libs/zbxcommon/misc.c index 2946dd18cbe1..53b18191a976 100644 --- a/src/libs/zbxcommon/misc.c +++ b/src/libs/zbxcommon/misc.c @@ -18,6 +18,7 @@ **/ #include "zbxcommon.h" +#include "zbxstr.h" const int INTERFACE_TYPE_PRIORITY[INTERFACE_TYPE_COUNT] = { @@ -296,11 +297,13 @@ static const char help_message_footer[] = * Purpose: print help of application parameters on stdout by application * * request with parameter '-h' * * * + * Parameters: param - pointer to modification parameter * + * * * Comments: help_message - is global variable which must be initialized * * in each zabbix application * * * ******************************************************************************/ -void zbx_help(void) +void zbx_help(const char *param) { const char **p = help_message; @@ -308,7 +311,19 @@ void zbx_help(void) printf("\n"); while (NULL != *p) + { + if (NULL != param && NULL != strstr(*p, "{DEFAULT_CONFIG_FILE}")) + { + char *ptr; + + ptr = zbx_string_replace(*p++, "{DEFAULT_CONFIG_FILE}", param); + printf("%s\n", ptr); + zbx_free(ptr); + continue; + } + printf("%s\n", *p++); + } printf("\n"); puts(help_message_footer); diff --git a/src/zabbix_agent/zabbix_agentd.c b/src/zabbix_agent/zabbix_agentd.c index e8728b9c1759..56555cb0d244 100644 --- a/src/zabbix_agent/zabbix_agentd.c +++ b/src/zabbix_agent/zabbix_agentd.c @@ -150,7 +150,7 @@ const char *help_message[] = { "Options:", " -c --config config-file Path to the configuration file", #ifdef _WINDOWS - " (default: \"\\zabbix_agentd.conf\")", + " (default: \"{DEFAULT_CONFIG_FILE}\")", #else " (default: \"" DEFAULT_CONFIG_FILE "\")", #endif @@ -384,7 +384,12 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) #endif case 'h': t->task = ZBX_TASK_SHOW_HELP; +#ifdef _WINDOWS + goto cf_out; +#else goto out; +#endif + case 'V': t->task = ZBX_TASK_SHOW_VERSION; goto out; @@ -552,6 +557,7 @@ static int parse_commandline(int argc, char **argv, ZBX_TASK_EX *t) #ifdef _WINDOWS #define PATH_BUF_LEN 4096 +cf_out: if (NULL == config_file) { char *ptr, *process_path = NULL; @@ -1608,7 +1614,7 @@ int main(int argc, char **argv) exit(EXIT_SUCCESS); break; case ZBX_TASK_SHOW_HELP: - zbx_help(); + zbx_help(config_file); exit(EXIT_SUCCESS); break; default: diff --git a/src/zabbix_get/zabbix_get.c b/src/zabbix_get/zabbix_get.c index 4798e758d10a..462dad986aef 100644 --- a/src/zabbix_get/zabbix_get.c +++ b/src/zabbix_get/zabbix_get.c @@ -356,7 +356,7 @@ int main(int argc, char **argv) } break; case 'h': - zbx_help(); + zbx_help(NULL); exit(EXIT_SUCCESS); case 'V': zbx_version(); diff --git a/src/zabbix_js/zabbix_js.c b/src/zabbix_js/zabbix_js.c index ef194c8b7e05..73d7cf991842 100644 --- a/src/zabbix_js/zabbix_js.c +++ b/src/zabbix_js/zabbix_js.c @@ -175,7 +175,7 @@ int main(int argc, char **argv) break; case 'h': - zbx_help(); + zbx_help(NULL); ret = SUCCEED; goto clean; case 'V': diff --git a/src/zabbix_proxy/proxy.c b/src/zabbix_proxy/proxy.c index 23d9f74821cb..69dd44cbf459 100644 --- a/src/zabbix_proxy/proxy.c +++ b/src/zabbix_proxy/proxy.c @@ -1075,7 +1075,7 @@ int main(int argc, char **argv) t.task = ZBX_TASK_RUNTIME_CONTROL; break; case 'h': - zbx_help(); + zbx_help(NULL); exit(EXIT_SUCCESS); break; case 'V': diff --git a/src/zabbix_sender/zabbix_sender.c b/src/zabbix_sender/zabbix_sender.c index 5f64f2a827ee..699021b2a50c 100644 --- a/src/zabbix_sender/zabbix_sender.c +++ b/src/zabbix_sender/zabbix_sender.c @@ -1019,7 +1019,7 @@ static void parse_commandline(int argc, char **argv) config_file = zbx_strdup(config_file, zbx_optarg); break; case 'h': - zbx_help(); + zbx_help(NULL); exit(EXIT_SUCCESS); case 'V': zbx_version(); diff --git a/src/zabbix_server/server.c b/src/zabbix_server/server.c index 7af6f9666ee2..fb23ab4bb514 100644 --- a/src/zabbix_server/server.c +++ b/src/zabbix_server/server.c @@ -1175,7 +1175,7 @@ int main(int argc, char **argv) t.task = ZBX_TASK_RUNTIME_CONTROL; break; case 'h': - zbx_help(); + zbx_help(NULL); exit(EXIT_SUCCESS); break; case 'V':