Skip to content

Commit

Permalink
Merge pull request #142 from i-ky/compatibility-hack-2
Browse files Browse the repository at this point in the history
Replace zabbix_log() macro provided by Zabbix
  • Loading branch information
OrangeDog authored Feb 18, 2022
2 parents f039714 + 1d5a1e7 commit 2e93bc1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
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

0 comments on commit 2e93bc1

Please sign in to comment.