diff --git a/src/libzbxpgsql.c b/src/libzbxpgsql.c index 79f6268..d95dfe2 100644 --- a/src/libzbxpgsql.c +++ b/src/libzbxpgsql.c @@ -29,6 +29,10 @@ size_t (*zbx_snprintf)(char *str, size_t count, const char *fmt, ...) = NULL; +int (*real_zabbix_check_log_level)(int level) = NULL; +int *real_zbx_log_level = NULL; +void (*real_zabbix_log)(int level, const char *fmt, ...) = NULL; + // Define custom keys static ZBX_METRIC keys[] = /* KEY FLAG FUNCTION TEST PARAMETERS */ @@ -186,11 +190,27 @@ int zbx_module_uninit() { int zbx_module_init() { void *handle; - zabbix_log(LOG_LEVEL_INFORMATION, "starting agent module %s", PACKAGE_STRING); + printf("starting agent module %s", PACKAGE_STRING); if (NULL == (handle = dlopen(NULL, RTLD_LAZY | RTLD_NOLOAD))) { - zabbix_log(LOG_LEVEL_ERR, "failed to dlopen() Zabbix binary: %s", dlerror()); + fprintf(stderr, "failed to dlopen() Zabbix binary: %s", dlerror()); + return ZBX_MODULE_FAIL; + } + + real_zabbix_check_log_level = dlsym(handle, "zabbix_check_log_level"); /* was available before ZBX-10889 */ + real_zbx_log_level = dlsym(handle, "zbx_log_level"); /* is available since ZBX-10889 */ + + if (NULL == real_zabbix_check_log_level && NULL == real_zbx_log_level) + { + fprintf(stderr, "failed to find both zabbix_check_log_level() and zbx_log_level," + " be aware that module may spam with log messages"); + /* not a critical error, we can continue */ + } + + if (NULL == (real_zabbix_log = dlsym(handle, "__zbx_zabbix_log"))) + { + fprintf(stderr, "failed to find __zbx_zabbix_log(): %s", dlerror()); return ZBX_MODULE_FAIL; } diff --git a/src/libzbxpgsql.h b/src/libzbxpgsql.h index 26378d0..84b8f75 100644 --- a/src/libzbxpgsql.h +++ b/src/libzbxpgsql.h @@ -49,6 +49,23 @@ #define zbx_snprintf pgsql_snprintf /* prevent symbol conflict with zbx_snprintf() function in new Zabbix binaries */ extern size_t (*zbx_snprintf)(char *str, size_t count, const char *fmt, ...); /* use old name to avoid changing much of our code */ #include +/* zabbix_log() macro has always been a wrapper of __zbx_zabbix_log() function which used to perform log level check, it's not guaranteed now */ +#undef zabbix_log /* it's time to stop using zabbix_log() provided by Zabbix and define our own */ +extern int (*real_zabbix_check_log_level)(int level); +extern int *real_zbx_log_level; +extern void (*real_zabbix_log)(int level, const char *fmt, ...); +#define zabbix_log(level, ...) \ +do \ +{ \ + if (NULL != real_zabbix_check_log_level && SUCCEED != real_zabbix_check_log_level(level)) \ + break; \ +\ + if (NULL != real_zbx_log_level && LOG_LEVEL_INFORMATION != level && (level > *real_zbx_log_level || LOG_LEVEL_EMPTY == level)) \ + break; \ +\ + real_zabbix_log(level, __VA_ARGS__); \ +} \ +while(0) #include #include