Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace zabbix_log() macro provided by Zabbix #142

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/libzbxpgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}

Expand Down
17 changes: 17 additions & 0 deletions src/libzbxpgsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <log.h>
/* 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 <zbxjson.h>
#include <version.h>

Expand Down