diff options
| author | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-02-16 11:10:48 +0000 |
|---|---|---|
| committer | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-02-16 11:10:48 +0000 |
| commit | b85b92cbf8dd6626e391efc3643de15ea7bae8ce (patch) | |
| tree | 79783914144c2b2383574552f18a5f41121e90ee /src | |
| parent | 7f3c4b75b989aa9218908862db0af4233d1c009b (diff) | |
| download | zabbix-b85b92cbf8dd6626e391efc3643de15ea7bae8ce.tar.gz zabbix-b85b92cbf8dd6626e391efc3643de15ea7bae8ce.tar.xz zabbix-b85b92cbf8dd6626e391efc3643de15ea7bae8ce.zip | |
- [DEV-109] Server changes
git-svn-id: svn://svn.zabbix.com/trunk@5359 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
32 files changed, 56 insertions, 5526 deletions
diff --git a/src/libs/Makefile.am b/src/libs/Makefile.am index d9a556d9..5f84ad38 100644 --- a/src/libs/Makefile.am +++ b/src/libs/Makefile.am @@ -16,14 +16,16 @@ DIST_SUBDIRS = \ zbxplugin \ zbxsys \ zbxjabber \ - zbxjson + zbxjson \ + zbxserver if PROXY PROXY_SUBDIRS = \ zbxdb \ zbxdbcache \ zbxdbhigh \ - zbxjson + zbxjson \ + zbxserver endif if SERVER @@ -33,7 +35,8 @@ SERVER_SUBDIRS = \ zbxdbhigh \ zbxemail \ zbxsms \ - zbxjson + zbxjson \ + zbxserver endif if JABBER diff --git a/src/zabbix_server/evalfunc.c b/src/libs/zbxserver/evalfunc.c index 73b6b85b..73b6b85b 100644 --- a/src/zabbix_server/evalfunc.c +++ b/src/libs/zbxserver/evalfunc.c diff --git a/src/zabbix_proxy/evalfunc.h b/src/libs/zbxserver/evalfunc.h index 5da6b67d..5da6b67d 100644 --- a/src/zabbix_proxy/evalfunc.h +++ b/src/libs/zbxserver/evalfunc.h diff --git a/src/zabbix_proxy/expression.c b/src/libs/zbxserver/expression.c index f288f754..baaf1de1 100644 --- a/src/zabbix_proxy/expression.c +++ b/src/libs/zbxserver/expression.c @@ -23,16 +23,14 @@ #include <string.h> #include <math.h> +#include "zbxserver.h" #include "expression.h" -#include "functions.h" #include "evalfunc.h" #include "common.h" #include "db.h" #include "log.h" #include "zlog.h" - - /****************************************************************************** * * * Function: str2double * diff --git a/src/zabbix_server/expression.h b/src/libs/zbxserver/expression.h index d3808fcc..9876dc08 100644 --- a/src/zabbix_server/expression.h +++ b/src/libs/zbxserver/expression.h @@ -27,14 +27,6 @@ int cmp_double(double a,double b); int find_char(char *str,char c); int evaluate_expression(int *result,char **expression, int triggger_value, char *error, int maxerrlen); -void substitute_macros(DB_EVENT *event, DB_ACTION *action, char **data); void delete_reol(char *c); -#define MACRO_TYPE_TRIGGER_DESCRIPTION 1 -#define MACRO_TYPE_MESSAGE_SUBJECT 2 -#define MACRO_TYPE_MESSAGE_BODY 4 -#define MACRO_TYPE_TRIGGER_EXPRESSION 5 - -void substitute_simple_macros(DB_EVENT *event, DB_ACTION *action, char **data, int macro_type); - #endif diff --git a/src/zabbix_server/functions.c b/src/libs/zbxserver/functions.c index d1c962c8..77b06496 100644 --- a/src/zabbix_server/functions.c +++ b/src/libs/zbxserver/functions.c @@ -25,8 +25,8 @@ #include "log.h" #include "zlog.h" +#include "zbxserver.h" #include "evalfunc.h" -#include "functions.h" #include "expression.h" /****************************************************************************** diff --git a/src/zabbix_proxy/Makefile.am b/src/zabbix_proxy/Makefile.am index b33438c0..61c95dd3 100644 --- a/src/zabbix_proxy/Makefile.am +++ b/src/zabbix_proxy/Makefile.am @@ -8,17 +8,14 @@ SUBDIRS = \ proxyconfig \ ../zabbix_server/pinger \ ../zabbix_server/poller \ - ../zabbix_server/trapper + ../zabbix_server/trapper \ + ../zabbix_server/nodewatcher sbin_PROGRAMS = zabbix_proxy zabbix_proxy_SOURCES = \ - evalfunc.c evalfunc.h \ - expression.c expression.h \ - operations.c operations.h \ events.c events.h \ zlog.c \ - functions.c functions.h \ proxy.c zabbix_proxy_LDADD = \ @@ -30,6 +27,7 @@ zabbix_proxy_LDADD = \ $(top_srcdir)/src/zabbix_server/pinger/libzbxpinger.a \ $(top_srcdir)/src/zabbix_server/poller/libzbxpoller.a \ $(top_srcdir)/src/zabbix_server/trapper/libzbxtrapper.a \ + $(top_srcdir)/src/zabbix_server/nodewatcher/libzbxnodewatcher.a \ $(top_srcdir)/src/libs/zbxsysinfo/libzbxserversysinfo.a \ $(top_srcdir)/src/libs/zbxsysinfo/$(ARCH)/libspecsysinfo.a \ $(top_srcdir)/src/libs/zbxsysinfo/common/libcommonsysinfo.a \ @@ -45,9 +43,11 @@ zabbix_proxy_LDADD = \ $(top_srcdir)/src/libs/zbxcrypto/libzbxcrypto.a \ $(top_srcdir)/src/libs/zbxcomms/libzbxcomms.a \ $(top_srcdir)/src/libs/zbxjson/libzbxjson.a \ + $(top_srcdir)/src/libs/zbxserver/libzbxserver.a \ @PROXY_LIBS@ zabbix_proxy_LDFLAGS = \ @PROXY_LDFLAGS@ -zabbix_proxy_CFLAGS = -DZABBIX_DAEMON +zabbix_proxy_CFLAGS = \ + -DZABBIX_DAEMON diff --git a/src/zabbix_proxy/evalfunc.c b/src/zabbix_proxy/evalfunc.c deleted file mode 100644 index 642913f0..00000000 --- a/src/zabbix_proxy/evalfunc.c +++ /dev/null @@ -1,1826 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 SIA Zabbix -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -**/ - -#include "common.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#include "evalfunc.h" - -/****************************************************************************** - * * - * Function: evaluate_LOGSOURCE * - * * - * Purpose: evaluate function 'logsource' for the item * - * * - * Parameters: item - item (performance metric) * - * parameter - ignored * - * * - * Return value: SUCCEED - evaluated succesfully, result is stored in 'value' * - * FAIL - failed to evaluate function * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int evaluate_LOGSOURCE(char *value, DB_ITEM *item, char *parameter) -{ - DB_RESULT result; - DB_ROW row; - - char sql[MAX_STRING_LEN]; - int now; - int res = SUCCEED; - - if(item->value_type != ITEM_VALUE_TYPE_LOG) - { - return FAIL; - } - - now=time(NULL); - - zbx_snprintf(sql,sizeof(sql),"select source from history_log where itemid=" ZBX_FS_UI64 " order by clock desc", - item->itemid); - - result = DBselectN(sql,1); - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for LOGSOURCE is empty" ); - res = FAIL; - } - else - { - if(strcmp(row[0], parameter) == 0) - { - strcpy(value,"1"); - } - else - { - strcpy(value,"0"); - } - } - DBfree_result(result); - - return res; -} - -/****************************************************************************** - * * - * Function: evaluate_LOGSEVERITY * - * * - * Purpose: evaluate function 'logseverity' for the item * - * * - * Parameters: item - item (performance metric) * - * parameter - ignored * - * * - * Return value: SUCCEED - evaluated succesfully, result is stored in 'value' * - * FAIL - failed to evaluate function * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int evaluate_LOGSEVERITY(char *value, DB_ITEM *item, char *parameter) -{ - DB_RESULT result; - DB_ROW row; - - char sql[MAX_STRING_LEN]; - int now; - int res = SUCCEED; - - if(item->value_type != ITEM_VALUE_TYPE_LOG) - { - return FAIL; - } - - now=time(NULL); - - zbx_snprintf(sql,sizeof(sql),"select severity from history_log where itemid=" ZBX_FS_UI64 " order by clock desc", - item->itemid); - - result = DBselectN(sql,1); - row = DBfetch(result); - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for LOGSEVERITY is empty" ); - res = FAIL; - } - else - { - strcpy(value,row[0]); - } - DBfree_result(result); - - return res; -} - -/****************************************************************************** - * * - * Function: evaluate_COUNT * - * * - * Purpose: evaluate function 'count' for the item * - * * - * Parameters: item - item (performance metric) * - * parameter - number of seconds * - * * - * Return value: SUCCEED - evaluated succesfully, result is stored in 'value' * - * FAIL - failed to evaluate function * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int evaluate_COUNT(char *value, DB_ITEM *item, char *parameter) -{ - DB_RESULT result; - DB_ROW row; - - char period[MAX_STRING_LEN+1]; - char op[MAX_STRING_LEN+1]; - char cmp[MAX_STRING_LEN+1]; - char cmp_esc[MAX_STRING_LEN+1]; - - int now; - int res = SUCCEED; - - char *table = NULL; - char table_ui64[] = "history_uint"; - char table_float[] = "history"; - char table_log[] = "history_log"; - char table_str[] = "history_str"; - - zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_COUNT(param:%s)", - parameter); - - - switch(item->value_type) - { - case ITEM_VALUE_TYPE_FLOAT: table = table_float; break; - case ITEM_VALUE_TYPE_UINT64: table = table_ui64; break; - case ITEM_VALUE_TYPE_LOG: table = table_log; break; - case ITEM_VALUE_TYPE_STR: table = table_str; break; - default: - return FAIL; - } - - now=time(NULL); - - if(get_param(parameter, 1, period, MAX_STRING_LEN) != 0) - { - return FAIL; - } - if(get_param(parameter, 2, cmp, MAX_STRING_LEN) != 0) - { - result = DBselect("select count(value) from %s where clock>%d and itemid=" ZBX_FS_UI64, - table, - now-atoi(period), - item->itemid); - - } - else - { - if(get_param(parameter, 3, op, MAX_STRING_LEN) != 0) - { - strscpy(op,"eq"); - } - DBescape_string(cmp, cmp_esc, sizeof(cmp_esc)); - /* ITEM_VALUE_TYPE_UINT64 */ - if( (item->value_type == ITEM_VALUE_TYPE_UINT64) && (strcmp(op,"eq") == 0)) - { - result = DBselect("select count(value) from history_uint where clock>%d and value=" ZBX_FS_UI64 " and itemid=" ZBX_FS_UI64, - now-atoi(period), - zbx_atoui64(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_UINT64) && (strcmp(op,"ne") == 0)) - { - result = DBselect("select count(value) from history_uint where clock>%d and value<>" ZBX_FS_UI64 " and itemid=" ZBX_FS_UI64, - now-atoi(period), - zbx_atoui64(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_UINT64) && (strcmp(op,"gt") == 0)) - { - result = DBselect("select count(value) from history_uint where clock>%d and value>" ZBX_FS_UI64 " and itemid=" ZBX_FS_UI64, - now-atoi(period), - zbx_atoui64(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_UINT64) && (strcmp(op,"lt") == 0)) - { - result = DBselect("select count(value) from history_uint where clock>%d and value<" ZBX_FS_UI64 " and itemid=" ZBX_FS_UI64, - now-atoi(period), - zbx_atoui64(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_UINT64) && (strcmp(op,"ge") == 0)) - { - result = DBselect("select count(value) from history_uint where clock>%d and value>=" ZBX_FS_UI64 " and itemid=" ZBX_FS_UI64, - now-atoi(period), - zbx_atoui64(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_UINT64) && (strcmp(op,"le") == 0)) - { - result = DBselect("select count(value) from history_uint where clock>%d and value<=" ZBX_FS_UI64 " and itemid=" ZBX_FS_UI64, - now-atoi(period), - zbx_atoui64(cmp_esc), - item->itemid); - } - /* ITEM_VALUE_TYPE_FLOAT */ - else if( (item->value_type == ITEM_VALUE_TYPE_FLOAT) && (strcmp(op,"eq") == 0)) - { - result = DBselect("select count(value) from history where clock>%d and value+0.00001>" ZBX_FS_DBL " and value-0.0001<" ZBX_FS_DBL " and itemid=" ZBX_FS_UI64, - now-atoi(period), - atof(cmp_esc), - atof(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_FLOAT) && (strcmp(op,"ne") == 0)) - { - result = DBselect("select count(value) from history where clock>%d and ((value+0.00001<" ZBX_FS_DBL ") or (value-0.0001>" ZBX_FS_DBL ")) and itemid=" ZBX_FS_UI64, - now-atoi(period), - atof(cmp_esc), - atof(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_FLOAT) && (strcmp(op,"gt") == 0)) - { - result = DBselect("select count(value) from history where clock>%d and value>" ZBX_FS_DBL " and itemid=" ZBX_FS_UI64, - now-atoi(period), - atof(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_FLOAT) && (strcmp(op,"ge") == 0)) - { - result = DBselect("select count(value) from history where clock>=%d and value>" ZBX_FS_DBL " and itemid=" ZBX_FS_UI64, - now-atoi(period), - atof(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_FLOAT) && (strcmp(op,"lt") == 0)) - { - result = DBselect("select count(value) from history where clock>%d and value<" ZBX_FS_DBL " and itemid=" ZBX_FS_UI64, - now-atoi(period), - atof(cmp_esc), - item->itemid); - } - else if( (item->value_type == ITEM_VALUE_TYPE_FLOAT) && (strcmp(op,"le") == 0)) - { - result = DBselect("select count(value) from history where clock>%d and value<=" ZBX_FS_DBL " and itemid=" ZBX_FS_UI64, - now-atoi(period), - atof(cmp_esc), - item->itemid); - } - else if(item->value_type == ITEM_VALUE_TYPE_LOG) - { - result = DBselect("select count(value) from history_log where clock>%d and value like '%s' and itemid=" ZBX_FS_UI64, - now-atoi(period), - cmp_esc, - item->itemid); - } - else - { - result = DBselect("select count(value) from history_str where clock>%d and value like '%s' and itemid=" ZBX_FS_UI64, - now-atoi(period), - cmp_esc, - item->itemid); - } - } - - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for COUNT is empty" ); - res = FAIL; - } - else - { - strcpy(value,row[0]); - } - DBfree_result(result); - - zabbix_log( LOG_LEVEL_DEBUG, "End evaluate_COUNT"); - - return res; -} - -/****************************************************************************** - * * - * Function: evaluate_SUM * - * * - * Purpose: evaluate function 'sum' for the item * - * * - * Parameters: item - item (performance metric) * - * parameter - number of seconds * - * * - * Return value: SUCCEED - evaluated succesfully, result is stored in 'value' * - * FAIL - failed to evaluate function * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int evaluate_SUM(char *value, DB_ITEM *item, int parameter, int flag) -{ - DB_RESULT result; - DB_ROW row; - - char sql[MAX_STRING_LEN]; - int now; - int res = SUCCEED; - int rows = 0; - double sum=0; - zbx_uint64_t sum_uint64=0; - zbx_uint64_t value_uint64; - - char *table = NULL; - char table_ui64[] = "history_uint"; - char table_float[] = "history"; - - - switch(item->value_type) - { - case ITEM_VALUE_TYPE_FLOAT: table = table_float; break; - case ITEM_VALUE_TYPE_UINT64: table = table_ui64; break; - default: - return FAIL; - } - - now=time(NULL); - - if(flag == ZBX_FLAG_SEC) - { - result = DBselect("select sum(value) from %s where clock>%d and itemid=" ZBX_FS_UI64, - table, - now-parameter, - item->itemid); - - row = DBfetch(result); - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for SUM is empty" ); - res = FAIL; - } - else - { - strcpy(value,row[0]); - } - } - else if(flag == ZBX_FLAG_VALUES) - { - zbx_snprintf(sql,sizeof(sql),"select value from %s where itemid=" ZBX_FS_UI64 " order by clock desc", - table, - item->itemid); - result = DBselectN(sql, parameter); - if(item->value_type == ITEM_VALUE_TYPE_UINT64) - { - while((row=DBfetch(result))) - { - ZBX_STR2UINT64(value_uint64,row[0]); - sum_uint64+=value_uint64; - rows++; - } - if(rows>0) zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_UI64, sum_uint64); - } - else - { - while((row=DBfetch(result))) - { - sum+=atof(row[0]); - rows++; - } - if(rows>0) zbx_snprintf(value,MAX_STRING_LEN, ZBX_FS_DBL, sum); - } - if(0 == rows) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for SUM is empty" ); - res = FAIL; - } - } - else - { - zabbix_log(LOG_LEVEL_WARNING, "Unknown flag [%d] Expected [%d] or [%d]", - flag, - ZBX_FLAG_SEC, - ZBX_FLAG_VALUES); - return FAIL; - } - - DBfree_result(result); - - return res; -} - -/****************************************************************************** - * * - * Function: evaluate_AVG * - * * - * Purpose: evaluate function 'avg' for the item * - * * - * Parameters: item - item (performance metric) * - * parameter - number of seconds * - * * - * Return value: SUCCEED - evaluated succesfully, result is stored in 'value' * - * FAIL - failed to evaluate function * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int evaluate_AVG(char *value,DB_ITEM *item,int parameter,int flag) -{ - DB_RESULT result; - DB_ROW row; - - char sql[MAX_STRING_LEN]; - int now; - int res = SUCCEED; - int rows; - double sum=0; - - char *table = NULL; - char table_ui64[] = "history_uint"; - char table_float[] = "history"; - - - switch(item->value_type) - { - case ITEM_VALUE_TYPE_FLOAT: table = table_float; break; - case ITEM_VALUE_TYPE_UINT64: table = table_ui64; break; - default: - return FAIL; - } - - now=time(NULL); - - if(flag == ZBX_FLAG_SEC) - { - result = DBselect("select avg(value) from %s where clock>%d and itemid=" ZBX_FS_UI64, - table, - now-parameter, - item->itemid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for AVG is empty" ); - res = FAIL; - } - else - { - strcpy(value,row[0]); - del_zeroes(value); - } - } - else if(flag == ZBX_FLAG_VALUES) - { - zbx_snprintf(sql,sizeof(sql),"select value from %s where itemid=" ZBX_FS_UI64 " order by clock desc", - table, - item->itemid); - result = DBselectN(sql, parameter); - rows=0; - while((row=DBfetch(result))) - { - sum+=atof(row[0]); - rows++; - } - if(rows == 0) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for AVG is empty" ); - res = FAIL; - } - else - { - zbx_snprintf(value,MAX_STRING_LEN, ZBX_FS_DBL, sum/(double)rows); - } - } - else - { - zabbix_log(LOG_LEVEL_WARNING, "Unknown flag [%d] Expected [%d] or [%d]", - flag, - ZBX_FLAG_SEC, - ZBX_FLAG_VALUES); - return FAIL; - } - - DBfree_result(result); - - return res; -} - -/****************************************************************************** - * * - * Function: evaluate_MIN * - * * - * Purpose: evaluate function 'min' for the item * - * * - * Parameters: item - item (performance metric) * - * parameter - number of seconds * - * * - * Return value: SUCCEED - evaluated succesfully, result is stored in 'value' * - * FAIL - failed to evaluate function * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int evaluate_MIN(char *value,DB_ITEM *item,int parameter, int flag) -{ - DB_RESULT result; - DB_ROW row; - - char sql[MAX_STRING_LEN]; - int now; - int rows; - int res = SUCCEED; - - char *table = NULL; - char table_ui64[] = "history_uint"; - char table_float[] = "history"; - - zbx_uint64_t min_uint64=0; - zbx_uint64_t l; - - double min=0; - double f; - - switch(item->value_type) - { - case ITEM_VALUE_TYPE_FLOAT: table = table_float; break; - case ITEM_VALUE_TYPE_UINT64: table = table_ui64; break; - default: - return FAIL; - } - - now=time(NULL); - - - if(flag == ZBX_FLAG_SEC) - { - result = DBselect("select min(value) from %s where clock>%d and itemid=" ZBX_FS_UI64, - table, - now-parameter, - item->itemid); - row = DBfetch(result); - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for MIN is empty" ); - res = FAIL; - } - else - { - strcpy(value,row[0]); - del_zeroes(value); - } - } - else if(flag == ZBX_FLAG_VALUES) - { - zbx_snprintf(sql,sizeof(sql),"select value from %s where itemid=" ZBX_FS_UI64 " order by clock desc", - table, - item->itemid); - result = DBselectN(sql,parameter); - - rows=0; - while((row=DBfetch(result))) - { - if(item->value_type == ITEM_VALUE_TYPE_UINT64) - { - ZBX_STR2UINT64(l,row[0]); - - if(rows==0) min_uint64 = l; - else if(l<min_uint64) min_uint64 = l; - } - else - { - f=atof(row[0]); - if(rows==0) min = f; - else if(f<min) min = f; - } - rows++; - } - - if(rows==0) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for MIN is empty" ); - res = FAIL; - } - else - { - if(item->value_type == ITEM_VALUE_TYPE_UINT64) - { - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_UI64, min_uint64); - } - else - { - zbx_snprintf(value,MAX_STRING_LEN, ZBX_FS_DBL, min); - } - } - } - else - { - zabbix_log(LOG_LEVEL_WARNING, "Unknown flag [%d] Expected [%d] or [%d]", - flag, - ZBX_FLAG_SEC, - ZBX_FLAG_VALUES); - return FAIL; - } - - DBfree_result(result); - - return res; -} - -/****************************************************************************** - * * - * Function: evaluate_MAX * - * * - * Purpose: evaluate function 'max' for the item * - * * - * Parameters: item - item (performance metric) * - * parameter - number of seconds * - * * - * Return value: SUCCEED - evaluated succesfully, result is stored in 'value' * - * FAIL - failed to evaluate function * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int evaluate_MAX(char *value,DB_ITEM *item,int parameter,int flag) -{ - DB_RESULT result; - DB_ROW row; - - char sql[MAX_STRING_LEN]; - int now; - int res = SUCCEED; - int rows; - double f; - double max = 0; - - char *table = NULL; - char table_ui64[] = "history_uint"; - char table_float[] = "history"; - - zbx_uint64_t max_uint64=0; - zbx_uint64_t l; - - zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_MAX()"); - - switch(item->value_type) - { - case ITEM_VALUE_TYPE_FLOAT: table = table_float; break; - case ITEM_VALUE_TYPE_UINT64: table = table_ui64; break; - default: - return FAIL; - } - - now=time(NULL); - - if(flag == ZBX_FLAG_SEC) - { - result = DBselect("select max(value) from %s where clock>%d and itemid=" ZBX_FS_UI64, - table, - now-parameter, - item->itemid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" ); - res = FAIL; - } - else - { - strcpy(value,row[0]); - del_zeroes(value); - } - } - else if(flag == ZBX_FLAG_VALUES) - { - zbx_snprintf(sql,sizeof(sql),"select value from %s where itemid=" ZBX_FS_UI64 " order by clock desc", - table, - item->itemid); - result = DBselectN(sql,parameter); - rows=0; - while((row=DBfetch(result))) - { - if(item->value_type == ITEM_VALUE_TYPE_UINT64) - { - ZBX_STR2UINT64(l,row[0]); - - if(rows==0) max_uint64 = l; - else if(l>max_uint64) max_uint64 = l; - } - else - { - f=atof(row[0]); - if(rows==0) max=f; - else if(f>max) max=f; - } - rows++; - } - if(rows == 0) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" ); - res = FAIL; - } - else - { - if(item->value_type == ITEM_VALUE_TYPE_UINT64) - { - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_UI64, max_uint64); - } - else - { - zbx_snprintf(value,MAX_STRING_LEN, ZBX_FS_DBL, max); - } - } - } - else - { - zabbix_log(LOG_LEVEL_WARNING, "Unknown flag [%d] Expected [%d] or [%d]", - flag, - ZBX_FLAG_SEC, - ZBX_FLAG_VALUES); - return FAIL; - } - - DBfree_result(result); - - zabbix_log( LOG_LEVEL_DEBUG, "End of evaluate_MAX()"); - - return res; -} - -/****************************************************************************** - * * - * Function: evaluate_DELTA * - * * - * Purpose: evaluate function 'delat' for the item * - * * - * Parameters: item - item (performance metric) * - * parameter - number of seconds * - * * - * Return value: SUCCEED - evaluated succesfully, result is stored in 'value' * - * FAIL - failed to evaluate function * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int evaluate_DELTA(char *value,DB_ITEM *item,int parameter, int flag) -{ - DB_RESULT result; - DB_ROW row; - - char sql[MAX_STRING_LEN]; - int now; - int res = SUCCEED; - int rows; - double f; - double min = 0,max = 0; - - zbx_uint64_t max_uint64=0,min_uint64=0; - zbx_uint64_t l; - - char *table = NULL; - char table_ui64[] = "history_uint"; - char table_float[] = "history"; - - zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_DELTA()"); - - switch(item->value_type) - { - case ITEM_VALUE_TYPE_FLOAT: table = table_float; break; - case ITEM_VALUE_TYPE_UINT64: table = table_ui64; break; - default: - return FAIL; - } - - now=time(NULL); - - if(flag == ZBX_FLAG_SEC) - { - result = DBselect("select max(value)-min(value) from %s where clock>%d and itemid=" ZBX_FS_UI64, - table, - now-parameter, - item->itemid); - - row = DBfetch(result); - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for DELTA is empty" ); - res = FAIL; - } - else - { - strcpy(value,row[0]); - del_zeroes(value); - } - } - else if(flag == ZBX_FLAG_VALUES) - { - zbx_snprintf(sql,sizeof(sql),"select value from %s where itemid=" ZBX_FS_UI64 " order by clock desc", - table, - item->itemid); - result = DBselectN(sql,parameter); - rows=0; - while((row=DBfetch(result))) - { - if(item->value_type == ITEM_VALUE_TYPE_UINT64) - { - ZBX_STR2UINT64(l,row[0]); - - if(rows==0) - { - max_uint64 = l; - min_uint64 = l; - } - else - { - if(l>max_uint64) max_uint64 = l; - if(l<min_uint64) min_uint64 = l; - } - } - else - { - f=atof(row[0]); - if(rows==0) - { - min=f; - max=f; - } - else - { - if(f>max) max=f; - if(f<min) min=f; - } - } - rows++; - } - if(rows==0) - { - zabbix_log(LOG_LEVEL_DEBUG, "Result for DELTA is empty" ); - res = FAIL; - } - else - { - if(item->value_type == ITEM_VALUE_TYPE_UINT64) - { - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_UI64, max_uint64-min_uint64); - } - else - { - zbx_snprintf(value,MAX_STRING_LEN, ZBX_FS_DBL, max-min); - } - } - } - else - { - zabbix_log(LOG_LEVEL_WARNING, "Unknown flag [%d] Expected [%d] or [%d]", - flag, - ZBX_FLAG_SEC, - ZBX_FLAG_VALUES); - return FAIL; - } - - DBfree_result(result); - - zabbix_log( LOG_LEVEL_DEBUG, "End of evaluate_DELTA()"); - - return res; -} - -/****************************************************************************** - * * - * Function: evaluate_NODATA * - * * - * Purpose: evaluate function 'nodata' for the item * - * * - * Parameters: item - item (performance metric) * - * parameter - number of seconds * - * * - * Return value: SUCCEED - evaluated succesfully, result is stored in 'value' * - * FAIL - failed to evaluate function * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int evaluate_NODATA(char *value,DB_ITEM *item,int parameter) -{ - int now; - int res = SUCCEED; - - zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_NODATA()"); - - now = time(NULL); - - if((CONFIG_SERVER_STARTUP_TIME + parameter > now) || (item->lastclock + parameter > now)) - { - strcpy(value,"0"); - } - else - { - strcpy(value,"1"); - } - - zabbix_log( LOG_LEVEL_DEBUG, "End of evaluate_NODATA()"); - - return res; -} - -/****************************************************************************** - * * - * Function: evaluate_function * - * * - * Purpose: evaluate function * - * * - * Parameters: item - item to calculate function for * - * function - function (for example, 'max') * - * parameter - parameter of the function) * - * flag - if EVALUATE_FUNCTION_SUFFIX, then include units and * - * suffix (K,M,G) into result value (for example, 15GB) * - * * - * Return value: SUCCEED - evaluated succesfully, value contains its value * - * FAIL - evaluation failed * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int evaluate_function(char *value,DB_ITEM *item,char *function,char *parameter) -{ - int ret = SUCCEED; - time_t now; - struct tm *tm; - - int fuzlow, fuzhig; - - int day; - int len; - - zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_function(%s)", - function); - - if(strcmp(function,"last")==0) - { - if(item->lastvalue_null==1) - { - ret = FAIL; - } - else - { - switch (item->value_type) { - case ITEM_VALUE_TYPE_FLOAT: - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_DBL,item->lastvalue_dbl); - del_zeroes(value); - break; - case ITEM_VALUE_TYPE_UINT64: - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_UI64,item->lastvalue_uint64); - break; - default: - strcpy(value,item->lastvalue_str); - break; - } - } - } - else if(strcmp(function,"prev")==0) - { - if(item->prevvalue_null==1) - { - ret = FAIL; - } - else - { - switch (item->value_type) { - case ITEM_VALUE_TYPE_FLOAT: - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_DBL,item->prevvalue_dbl); - del_zeroes(value); - break; - case ITEM_VALUE_TYPE_UINT64: - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_UI64,item->prevvalue_uint64); - break; - default: - strcpy(value,item->prevvalue_str); - break; - } - } - } - else if(strcmp(function,"min")==0) - { - if(parameter[0]=='#') - ret = evaluate_MIN(value,item,atoi(parameter+1),ZBX_FLAG_VALUES); - else - ret = evaluate_MIN(value,item,atoi(parameter),ZBX_FLAG_SEC); - } - else if(strcmp(function,"max")==0) - { - if(parameter[0]=='#') - ret = evaluate_MAX(value,item,atoi(parameter+1),ZBX_FLAG_VALUES); - else - ret = evaluate_MAX(value,item,atoi(parameter),ZBX_FLAG_SEC); - } - else if(strcmp(function,"avg")==0) - { - if(parameter[0]=='#') - ret = evaluate_AVG(value,item,atoi(parameter+1),ZBX_FLAG_VALUES); - else - ret = evaluate_AVG(value,item,atoi(parameter),ZBX_FLAG_SEC); - } - else if(strcmp(function,"sum")==0) - { - if(parameter[0]=='#') - ret = evaluate_SUM(value,item,atoi(parameter+1),ZBX_FLAG_VALUES); - else - ret = evaluate_SUM(value,item,atoi(parameter),ZBX_FLAG_SEC); - } - else if(strcmp(function,"count")==0) - { - ret = evaluate_COUNT(value,item,parameter); - } - else if(strcmp(function,"delta")==0) - { - if(parameter[0]=='#') - ret = evaluate_DELTA(value,item,atoi(parameter+1),ZBX_FLAG_VALUES); - else - ret = evaluate_DELTA(value,item,atoi(parameter),ZBX_FLAG_SEC); - } - else if(strcmp(function,"nodata")==0) - { - ret = evaluate_NODATA(value,item,atoi(parameter)); - } - else if(strcmp(function,"date")==0) - { - now=time(NULL); - tm=localtime(&now); - zbx_snprintf(value,MAX_STRING_LEN,"%.4d%.2d%.2d", - tm->tm_year+1900, - tm->tm_mon+1, - tm->tm_mday); - } - else if(strcmp(function,"dayofweek")==0) - { - now=time(NULL); - tm=localtime(&now); - /* The number of days since Sunday, in the range 0 to 6. */ - day=tm->tm_wday; - if(0 == day) day=7; - zbx_snprintf(value,MAX_STRING_LEN,"%d", - day); - } - else if(strcmp(function,"time")==0) - { - now=time(NULL); - tm=localtime(&now); - zbx_snprintf(value,MAX_STRING_LEN,"%.2d%.2d%.2d", - tm->tm_hour, - tm->tm_min, - tm->tm_sec); - } - else if(strcmp(function,"abschange")==0) - { - if((item->lastvalue_null==1)||(item->prevvalue_null==1)) - { - ret = FAIL; - } - else - { - switch (item->value_type) { - case ITEM_VALUE_TYPE_FLOAT: - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_DBL, - (double)abs(item->lastvalue_dbl-item->prevvalue_dbl)); - del_zeroes(value); - break; - case ITEM_VALUE_TYPE_UINT64: - /* To avoid overflow */ - if(item->lastvalue_uint64>=item->prevvalue_uint64) - { - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_UI64, - labs(item->lastvalue_uint64-item->prevvalue_uint64)); - } - else - { - zbx_snprintf(value,MAX_STRING_LEN,"-" ZBX_FS_UI64, - labs(item->prevvalue_uint64 - item->lastvalue_uint64)); - } - break; - default: - if(strcmp(item->lastvalue_str, item->prevvalue_str) == 0) - { - strcpy(value,"0"); - } - else - { - strcpy(value,"1"); - } - break; - } - } - } - else if(strcmp(function,"change")==0) - { - if((item->lastvalue_null==1)||(item->prevvalue_null==1)) - { - ret = FAIL; - } - else - { - switch (item->value_type) { - case ITEM_VALUE_TYPE_FLOAT: - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_DBL, - item->lastvalue_dbl-item->prevvalue_dbl); - del_zeroes(value); - break; - case ITEM_VALUE_TYPE_UINT64: - /* To avoid overflow */ - if(item->lastvalue_uint64>=item->prevvalue_uint64) - { - zbx_snprintf(value,MAX_STRING_LEN,ZBX_FS_UI64, - item->lastvalue_uint64-item->prevvalue_uint64); - } - else - { - zbx_snprintf(value,MAX_STRING_LEN,"-" ZBX_FS_UI64, - item->prevvalue_uint64 - item->lastvalue_uint64); - } - break; - default: - if(strcmp(item->lastvalue_str, item->prevvalue_str) == 0) - { - strcpy(value,"0"); - } - else - { - strcpy(value,"1"); - } - break; - } - } - } - else if(strcmp(function,"diff")==0) - { - if((item->lastvalue_null==1)||(item->prevvalue_null==1)) - { - ret = FAIL; - } - else - { - switch (item->value_type) { - case ITEM_VALUE_TYPE_FLOAT: - if(cmp_double(item->lastvalue_dbl, item->prevvalue_dbl) == 0) - { - strcpy(value,"0"); - } - else - { - strcpy(value,"1"); - } - break; - case ITEM_VALUE_TYPE_UINT64: - if(item->lastvalue_uint64 == item->prevvalue_uint64) - { - strcpy(value,"0"); - } - else - { - strcpy(value,"1"); - } - break; - default: - if(strcmp(item->lastvalue_str, item->prevvalue_str) == 0) - { - strcpy(value,"0"); - } - else - { - strcpy(value,"1"); - } - break; - } -/* if( (item->value_type==ITEM_VALUE_TYPE_FLOAT) || (item->value_type==ITEM_VALUE_TYPE_UINT64)) - { - if(cmp_double(item->lastvalue, item->prevvalue) == 0) - { - strcpy(value,"0"); - } - else - { - strcpy(value,"1"); - } - } - else - { - if(strcmp(item->lastvalue_str, item->prevvalue_str) == 0) - { - strcpy(value,"0"); - } - else - { - strcpy(value,"1"); - } - }*/ - } - } - else if(strcmp(function,"str")==0) - { - if( (item->value_type==ITEM_VALUE_TYPE_STR) || (item->value_type==ITEM_VALUE_TYPE_LOG)) - { - if(strstr(item->lastvalue_str, parameter) == NULL) - { - strcpy(value,"0"); - } - else - { - strcpy(value,"1"); - } - - } - else - { - ret = FAIL; - } - } - else if(strcmp(function,"regexp")==0) - { - if( (item->value_type==ITEM_VALUE_TYPE_STR) || (item->value_type==ITEM_VALUE_TYPE_LOG)) - { - if(zbx_regexp_match(item->lastvalue_str, parameter, &len) != NULL) - { - strcpy(value,"1"); - } - else - { - strcpy(value,"0"); - } - } - else - { - ret = FAIL; - } - } - else if(strcmp(function,"iregexp")==0) - { - if( (item->value_type==ITEM_VALUE_TYPE_STR) || (item->value_type==ITEM_VALUE_TYPE_LOG)) - { - if(zbx_iregexp_match(item->lastvalue_str, parameter, &len) != NULL) - { - strcpy(value,"1"); - } - else - { - strcpy(value,"0"); - } - } - else - { - ret = FAIL; - } - } - else if(strcmp(function,"now")==0) - { - now=time(NULL); - zbx_snprintf(value,MAX_STRING_LEN,"%d",(int)now); - } - else if(strcmp(function,"fuzzytime")==0) - { - now=time(NULL); - fuzlow=(int)(now-atoi(parameter)); - fuzhig=(int)(now+atoi(parameter)); - - if(item->lastvalue_null==1) - { - ret = FAIL; - } - else - { - switch (item->value_type) { - case ITEM_VALUE_TYPE_FLOAT: - if((item->lastvalue_dbl>=fuzlow)&&(item->lastvalue_dbl<=fuzhig)) - { - strcpy(value,"1"); - } - else - { - strcpy(value,"0"); - } - break; - case ITEM_VALUE_TYPE_UINT64: - if((item->lastvalue_uint64>=fuzlow)&&(item->lastvalue_uint64<=fuzhig)) - { - strcpy(value,"1"); - } - else - { - strcpy(value,"0"); - } - break; - default: - ret = FAIL; - break; - } - } - } - else if(strcmp(function,"logseverity")==0) - { - ret = evaluate_LOGSEVERITY(value,item,parameter); - } - else if(strcmp(function,"logsource")==0) - { - ret = evaluate_LOGSOURCE(value,item,parameter); - } - else - { - zabbix_log( LOG_LEVEL_WARNING, "Unsupported function:%s", - function); - zabbix_syslog("Unsupported function:%s", - function); - ret = FAIL; - } - - zabbix_log( LOG_LEVEL_DEBUG, "End of evaluate_function(result:%s)", - value); - return ret; -} - -/****************************************************************************** - * * - * Function: add_value_suffix_uptime * - * * - * Purpose: Peocess suffix 'uptime' * - * * - * Parameters: value - value for adjusting * - * max_len - max len of the value * - * * - * Return value: * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static void add_value_suffix_uptime(char *value, int max_len) -{ - double value_double; - double days, hours, min; - - zabbix_log( LOG_LEVEL_DEBUG, "In add_value_suffix_uptime(%s)", - value); - - value_double = atof(value); - - if(value_double <0) return; - - days=floor(value_double/(24*3600)); - if(cmp_double(days,0) != 0) - { - value_double=value_double-days*(24*3600); - } - hours=floor(value_double/(3600)); - if(cmp_double(hours,0) != 0) - { - value_double=value_double-hours*3600; - } - min=floor(value_double/(60)); - if( cmp_double(min,0) !=0) - { - value_double=value_double-min*(60); - } - if(cmp_double(days,0) == 0) - { - zbx_snprintf(value, max_len, "%02d:%02d:%02d", - (int)hours, - (int)min, - (int)value_double); - } - else - { - zbx_snprintf(value, max_len, "%d days, %02d:%02d:%02d", - (int)days, - (int)hours, - (int)min, - (int)value_double); - } - zabbix_log( LOG_LEVEL_DEBUG, "End of add_value_suffix_uptime(%s)", - value); -} - -/****************************************************************************** - * * - * Function: add_value_suffix_s * - * * - * Purpose: Peocess suffix 's' * - * * - * Parameters: value - value for adjusting * - * max_len - max len of the value * - * * - * Return value: * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static void add_value_suffix_s(char *value, int max_len) -{ - double value_double; - double t; - char tmp[MAX_STRING_LEN]; - - zabbix_log( LOG_LEVEL_DEBUG, "In add_value_suffix_s(%s)", - value); - - value_double = atof(value); - if(value_double <0) return; - - value[0]='\0'; - - t=floor(value_double/(365*24*3600)); - if(cmp_double(t,0) != 0) - { - zbx_snprintf(tmp, sizeof(tmp), "%dy", (int)t); - zbx_strlcat(value, tmp, max_len); - value_double = value_double-t*(365*24*3600); - } - - t=floor(value_double/(30*24*3600)); - if(cmp_double(t,0) != 0) - { - zbx_snprintf(tmp, sizeof(tmp), "%dm", (int)t); - zbx_strlcat(value, tmp, max_len); - value_double = value_double-t*(30*24*3600); - } - - t=floor(value_double/(24*3600)); - if(cmp_double(t,0) != 0) - { - zbx_snprintf(tmp, sizeof(tmp), "%dd", (int)t); - zbx_strlcat(value, tmp, max_len); - value_double = value_double-t*(24*3600); - } - - t=floor(value_double/(3600)); - if(cmp_double(t,0) != 0) - { - zbx_snprintf(tmp, sizeof(tmp), "%dh", (int)t); - zbx_strlcat(value, tmp, max_len); - value_double = value_double-t*(3600); - } - - t=floor(value_double/(60)); - if(cmp_double(t,0) != 0) - { - zbx_snprintf(tmp, sizeof(tmp), "%dm", (int)t); - zbx_strlcat(value, tmp, max_len); - value_double = value_double-t*(60); - } - - zbx_snprintf(tmp, sizeof(tmp), "%02.2f", value_double); - zbx_rtrim(tmp,"0"); - zbx_rtrim(tmp,"."); - zbx_strlcat(tmp, "s", sizeof(tmp)); - zbx_strlcat(value, tmp, max_len); - - zabbix_log( LOG_LEVEL_DEBUG, "End of add_value_suffix_s(%s)", - value); -} - -/****************************************************************************** - * * - * Function: add_value_suffix_normsl * - * * - * Purpose: Peocess normal values and add K,M,G,T * - * * - * Parameters: value - value for adjusting * - * max_len - max len of the value * - * units - units (bps, b,B, etc) * - * * - * Return value: * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static void add_value_suffix_normal(char *value, int max_len, char *units) -{ - double base = 1024; - char kmgt[MAX_STRING_LEN]; - - zbx_uint64_t value_uint64; - double value_double; - - zabbix_log( LOG_LEVEL_DEBUG, "In add_value_normal(value:%s,units:%s)", - value, - units); - - value_uint64 = labs(zbx_atoui64(value)); - - /* SPecial processing for bits */ - if(strcmp(units,"b") == 0 || strcmp(units,"bps") == 0) - { - base = 1000; - } - - if(value_uint64 < base) - { - strscpy(kmgt,""); - value_double = (double)value_uint64; - } - else if(value_uint64 < base*base) - { - strscpy(kmgt,"K"); - value_double = (double)value_uint64/base; - } - else if(value_uint64 < base*base*base) - { - strscpy(kmgt,"M"); - value_double = (double)(value_uint64/(base*base)); - } - else if(value_uint64 < base*base*base*base) - { - strscpy(kmgt,"G"); - value_double = (double)value_uint64/(base*base*base); - } - else - { - strscpy(kmgt,"T"); - value_double = (double)value_uint64/(base*base*base*base); - } - - if(cmp_double((int)(value_double+0.5), value_double) == 0) - { - zbx_snprintf(value, MAX_STRING_LEN, ZBX_FS_DBL_EXT(0) " %s%s", - value_double, - kmgt, - units); - } - else - { - zbx_snprintf(value, MAX_STRING_LEN, ZBX_FS_DBL_EXT(2) " %s%s", - value_double, - kmgt, - units); - } - - zabbix_log( LOG_LEVEL_DEBUG, "End of add_value_normal(value:%s)", - value); -} - -/****************************************************************************** - * * - * Function: add_value_suffix * - * * - * Purpose: Add suffix for value * - * * - * Parameters: value - value to replacing * - * valuemapid - index of value map * - * * - * Return value: SUCCEED - suffix added succesfully, value contains new value * - * FAIL - adding failed, value contains old value * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ - -/* Do not forget to keep it in sync wiht convert_units in config.inc.php */ -int add_value_suffix(char *value, int max_len, char *units, int value_type) -{ - int ret = FAIL; - - struct tm *local_time = NULL; - time_t time; - - char tmp[MAX_STRING_LEN]; - - zabbix_log( LOG_LEVEL_DEBUG, "In add_value_suffix(value:%s,units:%s)", - value, - units); - - switch(value_type) - { - case ITEM_VALUE_TYPE_FLOAT: - if(strcmp(units,"s") == 0) - { - add_value_suffix_s(value, max_len); - ret = SUCCEED; - } - else if(strcmp(units,"uptime") == 0) - { - add_value_suffix_uptime(value, max_len); - ret = SUCCEED; - } - else if(strlen(units) != 0) - { - add_value_suffix_normal(value, max_len, units); - ret = SUCCEED; - } - else - { - /* Do nothing if units not set */ - } - break; - - case ITEM_VALUE_TYPE_UINT64: - if(strcmp(units,"s") == 0) - { - add_value_suffix_s(value, max_len); - ret = SUCCEED; - } - else if(strcmp(units,"unixtime") == 0) - { - time = (time_t)zbx_atoui64(value); - local_time = localtime(&time); - strftime(tmp, MAX_STRING_LEN, "%Y.%m.%d %H:%M:%S", - local_time); - zbx_strlcpy(value, tmp, max_len); - ret = SUCCEED; - } - else if(strcmp(units,"uptime") == 0) - { - add_value_suffix_uptime(value, max_len); - ret = SUCCEED; - } - else if(strlen(units) != 0) - { - add_value_suffix_normal(value, max_len, units); - ret = SUCCEED; - } - else - { - /* Do nothing if units not set */ - } - break; - default: - ret = FAIL; - break; - } - - zabbix_log(LOG_LEVEL_DEBUG, "End of add_value_suffix(%s)", - value); - - return ret; -} - -/****************************************************************************** - * * - * Function: replace_value_by_map * - * * - * Purpose: replace value by mapping value * - * * - * Parameters: value - value to replacing * - * valuemapid - index of value map * - * * - * Return value: SUCCEED - evaluated succesfully, value contains new value * - * FAIL - evaluation failed, value contains old value * - * * - * Author: Eugene Grigorjev * - * * - * Comments: * - * * - ******************************************************************************/ -int replace_value_by_map(char *value, zbx_uint64_t valuemapid) -{ - DB_RESULT result; - DB_ROW row; - - char new_value[MAX_STRING_LEN]; - char sql[MAX_STRING_LEN]; - char *or_value; - - zabbix_log(LOG_LEVEL_DEBUG, "In replace_value_by_map()" ); - - if(valuemapid == 0) return FAIL; - - result = DBselect("select newvalue from mappings where valuemapid=" ZBX_FS_UI64 " and value='%s'", - valuemapid, - value); - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) return FAIL; - - strcpy(new_value,row[0]); - DBfree_result(result); - - del_zeroes(new_value); - or_value = sql; /* sql variarbvle used as tmp - original value */ - zbx_strlcpy(sql,value,MAX_STRING_LEN); - - zbx_snprintf(value, MAX_STRING_LEN, "%s (%s)", - new_value, - or_value); - - zabbix_log(LOG_LEVEL_DEBUG, "End replace_value_by_map(result:%s)", - value); - return SUCCEED; -} - -/****************************************************************************** - * * - * Function: evaluate_function2 * - * * - * Purpose: evaluate function * - * * - * Parameters: host - host the key belongs to * - * key - item's key (for example, 'max') * - * function - function (for example, 'max') * - * parameter - parameter of the function) * - * * - * Return value: SUCCEED - evaluated succesfully, value contains its value * - * FAIL - evaluation failed * - * * - * Author: Alexei Vladishev * - * * - * Comments: Used for evaluation of notification macros * - * * - ******************************************************************************/ -int evaluate_function2(char *value,char *host,char *key,char *function,char *parameter) -{ - DB_ITEM item; - DB_RESULT result; - DB_ROW row; - - char host_esc[MAX_STRING_LEN]; - char key_esc[MAX_STRING_LEN]; - - int res; - - zabbix_log(LOG_LEVEL_DEBUG, "In evaluate_function2(%s,%s,%s,%s)", - host, - key, - function, - parameter); - - DBescape_string(host, host_esc, MAX_STRING_LEN); - DBescape_string(key, key_esc, MAX_STRING_LEN); - - result = DBselect("select %s where h.host='%s' and h.hostid=i.hostid and i.key_='%s' and" ZBX_COND_NODEID, - ZBX_SQL_ITEM_SELECT, - host_esc, - key_esc, - LOCAL_NODE("h.hostid")); - - row = DBfetch(result); - - if(!row) - { - DBfree_result(result); - zabbix_log(LOG_LEVEL_WARNING, "Query returned empty result"); - zabbix_syslog("Query returned empty result"); - return FAIL; - } - - DBget_item_from_db(&item,row); - - res = evaluate_function(value,&item,function,parameter); - - if(replace_value_by_map(value, item.valuemapid) != SUCCEED) - { - add_value_suffix(value, MAX_STRING_LEN, item.units, item.value_type); - } - -/* Cannot call DBfree_result until evaluate_FUNC */ - DBfree_result(result); - - zabbix_log(LOG_LEVEL_DEBUG, "End evaluate_function2(result:%s)", - value); - return res; -} diff --git a/src/zabbix_proxy/events.c b/src/zabbix_proxy/events.c index 3f514030..20c2c90a 100644 --- a/src/zabbix_proxy/events.c +++ b/src/zabbix_proxy/events.c @@ -17,137 +17,13 @@ ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ - -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <netinet/in.h> -#include <netdb.h> - -#include <signal.h> - -#include <string.h> - -#include <time.h> - -#include <sys/socket.h> -#include <errno.h> - -/* Functions: pow(), round() */ -#include <math.h> - #include "common.h" -#include "db.h" #include "log.h" -#include "zlog.h" -/*#include "actions.h"*/ -#include "functions.h" #include "events.h" /****************************************************************************** * * - * Function: add_trigger_info * - * * - * Purpose: add trigger info to event if required * - * * - * Parameters: event - event data (event.triggerid) * - * * - * Return value: * - * * - * Author: Alexei Vladishev * - * * - * Comments: use 'free_trigger_info' function to clear allocated memory * - * * - ******************************************************************************/ -static void add_trigger_info(DB_EVENT *event) -{ - DB_RESULT result; - DB_ROW row; - zbx_uint64_t triggerid; - - int event_prev_status, event_last_status; - - if(event->object==EVENT_OBJECT_TRIGGER && event->objectid != 0) - { - triggerid = event->objectid; - - result = DBselect("select description,priority,comments,url,type from triggers where triggerid=" ZBX_FS_UI64, - triggerid); - row = DBfetch(result); - event->trigger_description[0]=0; - zbx_free(event->trigger_comments); - zbx_free(event->trigger_url); - - if(row) - { - strscpy(event->trigger_description, row[0]); - event->trigger_priority = atoi(row[1]); - event->trigger_comments = strdup(row[2]); - event->trigger_url = strdup(row[3]); - event->trigger_type = atoi(row[4]); - } - DBfree_result(result); - - get_latest_event_status(triggerid, &event_prev_status, &event_last_status); - zabbix_log(LOG_LEVEL_DEBUG,"event_prev_status %d event_last_status %d event->value %d", - event_prev_status, - event_last_status, - event->value); - - event->skip_actions = 0; - - switch(event->trigger_type) - { - case TRIGGER_TYPE_NORMAL: - if( (event->value == TRIGGER_VALUE_UNKNOWN) || - (event_prev_status == TRIGGER_VALUE_TRUE && event_last_status == TRIGGER_VALUE_UNKNOWN && event->value == TRIGGER_VALUE_TRUE) || - (event_prev_status == TRIGGER_VALUE_FALSE && event_last_status == TRIGGER_VALUE_UNKNOWN && event->value == TRIGGER_VALUE_FALSE) || - (event_prev_status == TRIGGER_VALUE_UNKNOWN && event_last_status == TRIGGER_VALUE_UNKNOWN && event->value == TRIGGER_VALUE_FALSE) - ) - { - zabbix_log(LOG_LEVEL_DEBUG,"Skip actions"); - event->skip_actions = 1; - } - case TRIGGER_TYPE_MULTIPLE_TRUE: - if( (event->value == TRIGGER_VALUE_UNKNOWN) || -/* (event_prev_status == TRIGGER_VALUE_TRUE && event_last_status == TRIGGER_VALUE_UNKNOWN && event->value == TRIGGER_VALUE_TRUE) ||*/ - (event_prev_status == TRIGGER_VALUE_FALSE && event_last_status == TRIGGER_VALUE_UNKNOWN && event->value == TRIGGER_VALUE_FALSE) || - (event_prev_status == TRIGGER_VALUE_UNKNOWN && event_last_status == TRIGGER_VALUE_UNKNOWN && event->value == TRIGGER_VALUE_FALSE) - ) - { - zabbix_log(LOG_LEVEL_DEBUG,"Skip actions"); - event->skip_actions = 1; - } - } - } -} - -/****************************************************************************** - * * - * Function: free_trigger_info * - * * - * Purpose: clean allocated memory by function 'add_trigger_info' * - * * - * Parameters: event - event data (event.triggerid) * - * * - * Return value: * - * * - * Author: Eugene Grigorjev * - * * - * Comments: * - * * - ******************************************************************************/ -static void free_trigger_info(DB_EVENT *event) -{ - zbx_free(event->trigger_url); - zbx_free(event->trigger_comments); -} - -/****************************************************************************** - * * * Function: process_event * * * * Purpose: process new event * @@ -163,49 +39,24 @@ static void free_trigger_info(DB_EVENT *event) ******************************************************************************/ int process_event(DB_EVENT *event) { - zabbix_log(LOG_LEVEL_DEBUG,"In process_event(eventid:" ZBX_FS_UI64 ",object:%d,objectid:" ZBX_FS_UI64 ")", + zabbix_log(LOG_LEVEL_DEBUG,"In [proxy]process_event(eventid:" ZBX_FS_UI64 ",object:%d,objectid:" ZBX_FS_UI64 ")", event->eventid, event->object, event->objectid); - add_trigger_info(event); + if (event->eventid == 0) + event->eventid = DBget_maxid("events", "eventid"); - if(event->eventid == 0) - { - event->eventid = DBget_maxid("events","eventid"); - } - DBexecute("insert into events(eventid,source,object,objectid,clock,value) values(" ZBX_FS_UI64 ",%d,%d," ZBX_FS_UI64 ",%d,%d)", - event->eventid, - event->source, - event->object, - event->objectid, - event->clock, - event->value); - - /* Cancel currently active alerts */ -/* if(event->value == TRIGGER_VALUE_FALSE || event->value == TRIGGER_VALUE_TRUE) - { - DBexecute("update alerts set retries=3,error='Trigger changed its status. Will not send repeats.' where triggerid=" ZBX_FS_UI64 " and repeats>0 and status=%d", - event->triggerid, ALERT_STATUS_NOT_SENT); - }*/ -/* - if(event->skip_actions == 0) - { - process_actions(event); - } -*/ - if(event->value == TRIGGER_VALUE_TRUE) - { - DBupdate_services(event->objectid, event->trigger_priority); - } - else - { - DBupdate_services(event->objectid, 0); - } - - free_trigger_info(event); + DBexecute("insert into events(eventid,source,object,objectid,clock,value)" + " values(" ZBX_FS_UI64 ",%d,%d," ZBX_FS_UI64 ",%d,%d)", + event->eventid, + event->source, + event->object, + event->objectid, + event->clock, + event->value); - zabbix_log(LOG_LEVEL_DEBUG,"End of process_event()"); + zabbix_log(LOG_LEVEL_DEBUG, "End of [proxy]process_event()"); return SUCCEED; } diff --git a/src/zabbix_proxy/events.h b/src/zabbix_proxy/events.h index b56d9f77..68168467 100644 --- a/src/zabbix_proxy/events.h +++ b/src/zabbix_proxy/events.h @@ -21,7 +21,6 @@ #ifndef ZABBIX_EVENTS_H #define ZABBIX_EVENTS_H -#include "common.h" #include "db.h" int process_event(DB_EVENT *event); diff --git a/src/zabbix_proxy/expression.h b/src/zabbix_proxy/expression.h deleted file mode 100644 index d3808fcc..00000000 --- a/src/zabbix_proxy/expression.h +++ /dev/null @@ -1,40 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 SIA Zabbix -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -**/ - - -#ifndef ZABBIX_EXPRESSION_H -#define ZABBIX_EXPRESSION_H - -#include "common.h" -#include "db.h" - -int cmp_double(double a,double b); -int find_char(char *str,char c); -int evaluate_expression(int *result,char **expression, int triggger_value, char *error, int maxerrlen); -void substitute_macros(DB_EVENT *event, DB_ACTION *action, char **data); -void delete_reol(char *c); - -#define MACRO_TYPE_TRIGGER_DESCRIPTION 1 -#define MACRO_TYPE_MESSAGE_SUBJECT 2 -#define MACRO_TYPE_MESSAGE_BODY 4 -#define MACRO_TYPE_TRIGGER_EXPRESSION 5 - -void substitute_simple_macros(DB_EVENT *event, DB_ACTION *action, char **data, int macro_type); - -#endif diff --git a/src/zabbix_proxy/functions.c b/src/zabbix_proxy/functions.c deleted file mode 100644 index 53d3a2f4..00000000 --- a/src/zabbix_proxy/functions.c +++ /dev/null @@ -1,815 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 SIA Zabbix -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -**/ - - -#include "common.h" - -#include "comms.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#include "evalfunc.h" -#include "functions.h" -#include "expression.h" - -/****************************************************************************** - * * - * Function: update_functions * - * * - * Purpose: re-calculate and updates values of functions related to the item * - * * - * Parameters: item - item to update functions for * - * * - * Return value: * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void update_functions(DB_ITEM *item) -{ - DB_FUNCTION function; - DB_RESULT result; - DB_ROW row; - char value[MAX_STRING_LEN]; - char value_esc[MAX_STRING_LEN]; - char *lastvalue; - int ret=SUCCEED; - - zabbix_log( LOG_LEVEL_DEBUG, "In update_functions(" ZBX_FS_UI64 ")", - item->itemid); - -/* Oracle does'n support this */ -/* zbx_snprintf(sql,sizeof(sql),"select function,parameter,itemid,lastvalue from functions where itemid=%d group by function,parameter,itemid order by function,parameter,itemid",item->itemid);*/ - result = DBselect("select distinct function,parameter,itemid,lastvalue from functions where itemid=" ZBX_FS_UI64, - item->itemid); - - while((row=DBfetch(result))) - { - function.function=row[0]; - function.parameter=row[1]; - ZBX_STR2UINT64(function.itemid,row[2]); -/* function.itemid=atoi(row[2]); */ - lastvalue=row[3]; - - zabbix_log( LOG_LEVEL_DEBUG, "ItemId:" ZBX_FS_UI64 " Evaluating %s(%s)", - function.itemid, - function.function, - function.parameter); - - ret = evaluate_function(value,item,function.function,function.parameter); - if( FAIL == ret) - { - zabbix_log( LOG_LEVEL_DEBUG, "Evaluation failed for function:%s", - function.function); - continue; - } - if (ret == SUCCEED) - { - /* Update only if lastvalue differs from new one */ - if( (lastvalue == NULL) || (strcmp(lastvalue,value) != 0)) - { - DBescape_string(value,value_esc,MAX_STRING_LEN); - DBexecute("update functions set lastvalue='%s' where itemid=" ZBX_FS_UI64 " and function='%s' and parameter='%s'", - value_esc, - function.itemid, - function.function, - function.parameter ); - } - else - { - zabbix_log( LOG_LEVEL_DEBUG, "Do not update functions, same value"); - } - } - } - - DBfree_result(result); - - zabbix_log( LOG_LEVEL_DEBUG, "End update_functions()"); -} - -/****************************************************************************** - * * - * Function: update_triggers * - * * - * Purpose: re-calculate and updates values of triggers related to the item * - * * - * Parameters: itemid - item to update trigger values for * - * * - * Return value: * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void update_triggers(zbx_uint64_t itemid) -{ - char *exp; - char error[MAX_STRING_LEN]; - int exp_value; - DB_TRIGGER trigger; - DB_RESULT result; - DB_ROW row; - - zabbix_log( LOG_LEVEL_DEBUG, "In update_triggers [itemid:" ZBX_FS_UI64 "]", - itemid); - - result = DBselect("select distinct t.triggerid,t.expression,t.description,t.url,t.comments,t.status,t.value,t.priority,t.type from triggers t,functions f,items i where i.status<>%d and i.itemid=f.itemid and t.status=%d and f.triggerid=t.triggerid and f.itemid=" ZBX_FS_UI64, - ITEM_STATUS_NOTSUPPORTED, - TRIGGER_STATUS_ENABLED, - itemid); - - while((row=DBfetch(result))) - { - ZBX_STR2UINT64(trigger.triggerid,row[0]); - strscpy(trigger.expression,row[1]); - strscpy(trigger.description,row[2]); - trigger.url = row[3]; - trigger.comments = row[4]; - trigger.status = atoi(row[5]); - trigger.value = atoi(row[6]); - trigger.priority = atoi(row[7]); - trigger.type = atoi(row[8]); - - exp = strdup(trigger.expression); - if( evaluate_expression(&exp_value, &exp, trigger.value, error, sizeof(error)) != 0 ) - { - zabbix_log( LOG_LEVEL_WARNING, "Expression [%s] cannot be evaluated [%s]", - trigger.expression, - error); - zabbix_syslog("Expression [%s] cannot be evaluated [%s]", - trigger.expression, - error); -/* DBupdate_trigger_value(&trigger, exp_value, time(NULL), error);*//* We shouldn't update triggervalue if expressions failed */ - } - else - { - DBupdate_trigger_value(&trigger, exp_value, time(NULL), NULL); - } - zbx_free(exp); - } - DBfree_result(result); - zabbix_log( LOG_LEVEL_DEBUG, "End update_triggers [" ZBX_FS_UI64 "]", - itemid); -} - -void calc_timestamp(char *line,int *timestamp, char *format) -{ - int hh=0,mm=0,ss=0,yyyy=0,dd=0,MM=0; - int hhc=0,mmc=0,ssc=0,yyyyc=0,ddc=0,MMc=0; - int i,num; - struct tm tm; - time_t t; - - zabbix_log( LOG_LEVEL_DEBUG, "In calc_timestamp()"); - - hh=mm=ss=yyyy=dd=MM=0; - - for(i=0;(format[i]!=0)&&(line[i]!=0);i++) - { - if(isdigit(line[i])==0) continue; - num=(int)line[i]-48; - - switch ((char) format[i]) { - case 'h': - hh=10*hh+num; - hhc++; - break; - case 'm': - mm=10*mm+num; - mmc++; - break; - case 's': - ss=10*ss+num; - ssc++; - break; - case 'y': - yyyy=10*yyyy+num; - yyyyc++; - break; - case 'd': - dd=10*dd+num; - ddc++; - break; - case 'M': - MM=10*MM+num; - MMc++; - break; - } - } - - zabbix_log( LOG_LEVEL_DEBUG, "hh [%d] mm [%d] ss [%d] yyyy [%d] dd [%d] MM [%d]", - hh, - mm, - ss, - yyyy, - dd, - MM); - - /* Seconds can be ignored. No ssc here. */ - if(hhc!=0&&mmc!=0&&yyyyc!=0&&ddc!=0&&MMc!=0) - { - tm.tm_sec=ss; - tm.tm_min=mm; - tm.tm_hour=hh; - tm.tm_mday=dd; - tm.tm_mon=MM-1; - tm.tm_year=yyyy-1900; - - t=mktime(&tm); - if(t>0) - { - *timestamp=t; - } - } - - zabbix_log( LOG_LEVEL_DEBUG, "End timestamp [%d]", - *timestamp); -} - -/****************************************************************************** - * * - * Function: process_data * - * * - * Purpose: process new item value * - * * - * Parameters: sockfd - descriptor of agent-server socket connection * - * server - server name * - * key - item's key * - * value - new value of server:key * - * lastlogsize - if key=log[*], last size of log file * - * * - * Return value: SUCCEED - new value processed sucesfully * - * FAIL - otherwise * - * * - * Author: Alexei Vladishev * - * * - * Comments: for trapper server process * - * * - ******************************************************************************/ -int process_data(zbx_sock_t *sock,char *server,char *key,char *value,char *lastlogsize, char *timestamp, - char *source, char *severity) -{ - AGENT_RESULT agent; - - DB_RESULT result; - DB_ROW row; - DB_ITEM item; - - char server_esc[MAX_STRING_LEN]; - char key_esc[MAX_STRING_LEN]; - - zabbix_log( LOG_LEVEL_DEBUG, "In process_data([%s],[%s],[%s],[%s])", - server, - key, - value, - lastlogsize); - - init_result(&agent); - - DBescape_string(server, server_esc, MAX_STRING_LEN); - DBescape_string(key, key_esc, MAX_STRING_LEN); - - result = DBselect("select %s where h.status=%d and h.hostid=i.hostid and h.host='%s' and i.key_='%s' and i.status in (%d,%d) and i.type in (%d,%d) and" ZBX_COND_NODEID, - ZBX_SQL_ITEM_SELECT, - HOST_STATUS_MONITORED, - server_esc, - key_esc, - ITEM_STATUS_ACTIVE, ITEM_STATUS_NOTSUPPORTED, - ITEM_TYPE_TRAPPER, - ITEM_TYPE_ZABBIX_ACTIVE, - LOCAL_NODE("h.hostid")); - - row=DBfetch(result); - - if(!row) - { - DBfree_result(result); - return FAIL; -/* - zabbix_log( LOG_LEVEL_DEBUG, "Before checking autoregistration for [%s]", - server); - - if(autoregister(server) == SUCCEED) - { - DBfree_result(result); - - result = DBselect("select %s where h.status=%d and h.hostid=i.hostid and h.host='%s' and i.key_='%s' and i.status=%d and i.type in (%d,%d) and" ZBX_COND_NODEID, - ZBX_SQL_ITEM_SELECT, - HOST_STATUS_MONITORED, - server_esc, - key_esc, - ITEM_STATUS_ACTIVE, - ITEM_TYPE_TRAPPER, - ITEM_TYPE_ZABBIX_ACTIVE, - LOCAL_NODE("h.hostid")); - row = DBfetch(result); - if(!row) - { - DBfree_result(result); - return FAIL; - } - } - else - { - DBfree_result(result); - return FAIL; - } -*/ - } - - DBget_item_from_db(&item,row); - - if( (item.type==ITEM_TYPE_ZABBIX_ACTIVE) && (zbx_tcp_check_security(sock,item.trapper_hosts,1) == FAIL)) - { - DBfree_result(result); - return FAIL; - } - - zabbix_log( LOG_LEVEL_DEBUG, "Processing [%s]", - value); - - if(strcmp(value,"ZBX_NOTSUPPORTED") ==0) - { - zabbix_log( LOG_LEVEL_WARNING, "Active parameter [%s] is not supported by agent on host [%s]", - item.key, - item.host_name); - zabbix_syslog("Active parameter [%s] is not supported by agent on host [%s]", - item.key, - item.host_name); - DBupdate_item_status_to_notsupported(item.itemid, "Not supported by ZABBIX agent"); - } - else - { - if( (strncmp(item.key,"log[",4)==0) || - (strncmp(item.key,"eventlog[",9)==0) - ) - { - item.lastlogsize=atoi(lastlogsize); - item.timestamp=atoi(timestamp); - - calc_timestamp(value,&item.timestamp,item.logtimefmt); - - item.eventlog_severity=atoi(severity); - item.eventlog_source=source; - zabbix_log(LOG_LEVEL_DEBUG, "Value [%s] Lastlogsize [%s] Timestamp [%s]", - value, - lastlogsize, - timestamp); - } - - if(set_result_type(&agent, item.value_type, value) == SUCCEED) - { - process_new_value(&item,&agent); - update_triggers(item.itemid); - } - else - { - zabbix_log( LOG_LEVEL_WARNING, "Type of received value [%s] is not suitable for [%s@%s]", - value, - item.key, - item.host_name); - zabbix_syslog("Type of received value [%s] is not suitable for [%s@%s]", - value, - item.key, - item.host_name); - } - } - - DBfree_result(result); - - free_result(&agent); - - return SUCCEED; -} - -/****************************************************************************** - * * - * Function: add_history * - * * - * Purpose: add new value to history * - * * - * Parameters: item - item data * - * value - new value of the item * - * now - new value of the item * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int add_history(DB_ITEM *item, AGENT_RESULT *value, int now) -{ - int ret = SUCCEED; - - zabbix_log( LOG_LEVEL_DEBUG, "In add_history(key:%s,value_type:%X,type:%X)", - item->key, - item->value_type, - value->type); - - if (value->type & AR_UINT64) - zabbix_log( LOG_LEVEL_DEBUG, "In add_history(itemid:"ZBX_FS_UI64",UINT64:"ZBX_FS_UI64")", - item->itemid, - value->ui64); - if (value->type & AR_STRING) - zabbix_log( LOG_LEVEL_DEBUG, "In add_history(itemid:"ZBX_FS_UI64",STRING:%s)", - item->itemid, - value->str); - if (value->type & AR_DOUBLE) - zabbix_log( LOG_LEVEL_DEBUG, "In add_history(itemid:"ZBX_FS_UI64",DOUBLE:"ZBX_FS_DBL")", - item->itemid, - value->dbl); - if (value->type & AR_TEXT) - zabbix_log( LOG_LEVEL_DEBUG, "In add_history(itemid:"ZBX_FS_UI64",TEXT:[%s])", - item->itemid, - value->text); - - if(item->history>0) - { - if( (item->value_type==ITEM_VALUE_TYPE_FLOAT) || (item->value_type==ITEM_VALUE_TYPE_UINT64)) - { - /* Should we store delta or original value? */ - if(item->delta == ITEM_STORE_AS_IS) - { - if(item->value_type==ITEM_VALUE_TYPE_UINT64) - { - if(GET_UI64_RESULT(value)) - DBadd_history_uint(item->itemid,value->ui64,now); - } - else if(item->value_type==ITEM_VALUE_TYPE_FLOAT) - { - if(GET_DBL_RESULT(value)) - DBadd_history(item->itemid,value->dbl,now); - } - } - /* Delta as speed of change */ - else if(item->delta == ITEM_STORE_SPEED_PER_SECOND) - { - /* Save delta */ - if( ITEM_VALUE_TYPE_FLOAT == item->value_type ) - { - if(GET_DBL_RESULT(value) && (item->prevorgvalue_null == 0) && (item->prevorgvalue_dbl <= value->dbl) && (now != item->lastclock)) - { - DBadd_history( - item->itemid, - (value->dbl - item->prevorgvalue_dbl)/(now-item->lastclock), - now); - } - } - else if( ITEM_VALUE_TYPE_UINT64 == item->value_type ) - { - if(GET_UI64_RESULT(value) && (item->prevorgvalue_null == 0) && (item->prevorgvalue_uint64 <= value->ui64) && (now != item->lastclock)) - { - DBadd_history_uint( - item->itemid, - (zbx_uint64_t)(value->ui64 - item->prevorgvalue_uint64)/(now-item->lastclock), - now); - } - } - } - /* Real delta: simple difference between values */ - else if(item->delta == ITEM_STORE_SIMPLE_CHANGE) - { - /* Save delta */ - if( ITEM_VALUE_TYPE_FLOAT == item->value_type ) - { - if(GET_DBL_RESULT(value) && (item->prevorgvalue_null == 0) && (item->prevorgvalue_dbl <= value->dbl) ) - { - DBadd_history(item->itemid, (value->dbl - item->prevorgvalue_dbl), now); - } - } - else if(item->value_type==ITEM_VALUE_TYPE_UINT64) - { - if(GET_UI64_RESULT(value) && (item->prevorgvalue_null == 0) && (item->prevorgvalue_uint64 <= value->ui64) ) - { - DBadd_history_uint(item->itemid, value->ui64 - item->prevorgvalue_uint64, now); - } - } - } - else - { - zabbix_log(LOG_LEVEL_ERR, "Value not stored for itemid [%d]. Unknown delta [%d]", - item->itemid, - item->delta); - zabbix_syslog("Value not stored for itemid [%d]. Unknown delta [%d]", - item->itemid, - item->delta); - ret = FAIL; - } - } - else if(item->value_type==ITEM_VALUE_TYPE_STR) - { - if(GET_STR_RESULT(value)) - DBadd_history_str(item->itemid,value->str,now); - } - else if(item->value_type==ITEM_VALUE_TYPE_LOG) - { - if(GET_STR_RESULT(value)) - DBadd_history_log(0, item->itemid,value->str,now,item->timestamp,item->eventlog_source,item->eventlog_severity); - } - else if(item->value_type==ITEM_VALUE_TYPE_TEXT) - { - if(GET_TEXT_RESULT(value)) - DBadd_history_text(item->itemid,value->text,now); - } - else - { - zabbix_log(LOG_LEVEL_ERR, "Unknown value type [%d] for itemid [" ZBX_FS_UI64 "]", - item->value_type, - item->itemid); - } - } - - zabbix_log( LOG_LEVEL_DEBUG, "End of add_history"); - - return ret; -} - -/****************************************************************************** - * * - * Function: update_item * - * * - * Purpose: update item info after new value is received * - * * - * Parameters: item - item data * - * value - new value of the item * - * now - current timestamp * - * * - * Author: Alexei Vladishev, Eugene Grigorjev * - * * - * Comments: * - * * - ******************************************************************************/ -static void update_item(DB_ITEM *item, AGENT_RESULT *value, time_t now) -{ - char value_esc[MAX_STRING_LEN]; - - zabbix_log( LOG_LEVEL_DEBUG, "In update_item()"); - - value_esc[0] = '\0'; - - if(item->delta == ITEM_STORE_AS_IS) - { - if(GET_STR_RESULT(value)) - { - DBescape_string(value->str, value_esc, sizeof(value_esc)); - } - - if (item->value_type == ITEM_VALUE_TYPE_LOG) { - DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,lastvalue='%s',lastclock=%d,lastlogsize=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay, item->delay_flex, now), - value_esc, - (int)now, - item->lastlogsize, - item->itemid); - } else { - DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,lastvalue='%s',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay, item->delay_flex, now), - value_esc, - (int)now, - item->itemid); - } - } - /* Logic for delta as speed of change */ - else if(item->delta == ITEM_STORE_SPEED_PER_SECOND) - { - if(item->value_type == ITEM_VALUE_TYPE_FLOAT) - { - if(GET_DBL_RESULT(value)) - { - if((item->prevorgvalue_null == 0) && (item->prevorgvalue_dbl <= value->dbl) ) - { - /* In order to continue normal processing, we assume difference 1 second - Otherwise function update_functions and update_triggers won't work correctly*/ - if(now != item->lastclock) - { - DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue='" ZBX_FS_DBL "'," - "lastvalue='" ZBX_FS_DBL "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex,now), - value->dbl, - (value->dbl - item->prevorgvalue_dbl)/(now-item->lastclock), - (int)now, - item->itemid); - SET_DBL_RESULT(value, (double)(value->dbl - item->prevorgvalue_dbl)/(now-item->lastclock)); - } - else - { - DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue='" ZBX_FS_DBL "'," - "lastvalue='" ZBX_FS_DBL "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex,now), - value->dbl, - value->dbl - item->prevorgvalue_dbl, - (int)now, - item->itemid); - SET_DBL_RESULT(value, (double)(value->dbl - item->prevorgvalue_dbl)); - } - } - else - { - DBexecute("update items set nextcheck=%d,prevorgvalue='" ZBX_FS_DBL "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex,now), - value->dbl, - (int)now, - item->itemid); - } - } - } - else if(item->value_type == ITEM_VALUE_TYPE_UINT64) - { - if(GET_UI64_RESULT(value)) - { - if((item->prevorgvalue_null == 0) && (item->prevorgvalue_uint64 <= value->ui64) ) - { - if(now != item->lastclock) - { - DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue='" ZBX_FS_UI64 "'," - "lastvalue='" ZBX_FS_UI64 "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex,now), - value->ui64, - ((zbx_uint64_t)(value->ui64 - item->prevorgvalue_uint64))/(now-item->lastclock), - (int)now, - item->itemid); - SET_UI64_RESULT(value, (zbx_uint64_t)(value->ui64 - item->prevorgvalue_uint64)/(now-item->lastclock)); - } - else - { - DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue='" ZBX_FS_UI64 "'," - "lastvalue='" ZBX_FS_DBL "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex,now), - value->ui64, - (double)(value->ui64 - item->prevorgvalue_uint64), - (int)now, - item->itemid); - SET_UI64_RESULT(value, (zbx_uint64_t)(value->ui64 - item->prevorgvalue_uint64)); - } - } - else - { - DBexecute("update items set nextcheck=%d,prevorgvalue='" ZBX_FS_UI64 "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex,now), - value->ui64, - (int)now, - item->itemid); - } - } - } - } - /* Real delta: simple difference between values */ - else if(item->delta == ITEM_STORE_SIMPLE_CHANGE) - { - if(item->value_type == ITEM_VALUE_TYPE_FLOAT) - { - if(GET_DBL_RESULT(value)) - { - if((item->prevorgvalue_null == 0) && (item->prevorgvalue_dbl <= value->dbl)) - { - DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue='" ZBX_FS_DBL "'," - "lastvalue='" ZBX_FS_DBL "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex,now), - value->dbl, - (value->dbl - item->prevorgvalue_dbl), - (int)now, - item->itemid); - SET_DBL_RESULT(value, (double)(value->dbl - item->prevorgvalue_dbl)); - } - else - { - DBexecute("update items set nextcheck=%d,prevorgvalue='" ZBX_FS_DBL "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex, now), - value->dbl, - (int)now, - item->itemid); - } - } - } - else if(item->value_type == ITEM_VALUE_TYPE_UINT64) - { - if(GET_UI64_RESULT(value)) - { - if((item->prevorgvalue_null == 0) && (item->prevorgvalue_uint64 <= value->ui64)) - { - DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue='" ZBX_FS_UI64 "'," - "lastvalue='" ZBX_FS_UI64 "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex,now), - value->ui64, - (value->ui64 - item->prevorgvalue_uint64), - (int)now, - item->itemid); - SET_UI64_RESULT(value, (zbx_uint64_t)(value->ui64 - item->prevorgvalue_uint64)); - } - else - { - DBexecute("update items set nextcheck=%d,prevorgvalue='" ZBX_FS_UI64 "',lastclock=%d where itemid=" ZBX_FS_UI64, - calculate_item_nextcheck(item->itemid, item->type, item->delay,item->delay_flex, now), - value->ui64, - (int)now, - item->itemid); - } - } - } - } - - item->prevvalue_str = item->lastvalue_str; - item->prevvalue_dbl = item->lastvalue_dbl; - item->prevvalue_uint64 = item->lastvalue_uint64; - item->prevvalue_null = item->lastvalue_null; - - item->lastvalue_uint64 = value->ui64; - item->lastvalue_dbl = value->dbl; - item->lastvalue_str = value->str; - item->lastvalue_null = 0; - -/* Update item status if required */ - if(item->status == ITEM_STATUS_NOTSUPPORTED) - { - zabbix_log( LOG_LEVEL_WARNING, "Parameter [%s] became supported by agent on host [%s]", - item->key, - item->host_name); - zabbix_syslog("Parameter [%s] became supported by agent on host [%s]", - item->key, - item->host_name); - item->status = ITEM_STATUS_ACTIVE; - DBexecute("update items set status=%d where itemid=" ZBX_FS_UI64, - ITEM_STATUS_ACTIVE, - item->itemid); - } - - /* Required for nodata() */ - item->lastclock = now; - - zabbix_log( LOG_LEVEL_DEBUG, "End update_item()"); -} - -/****************************************************************************** - * * - * Function: process_new_value * - * * - * Purpose: process new item value * - * * - * Parameters: item - item data * - * value - new value of the item * - * * - * Author: Alexei Vladishev * - * * - * Comments: for trapper poller process * - * * - ******************************************************************************/ -void process_new_value(DB_ITEM *item, AGENT_RESULT *value) -{ - time_t now; - - zabbix_log( LOG_LEVEL_DEBUG, "In process_new_value(%s)", - item->key); - - now = time(NULL); - - if( ITEM_MULTIPLIER_USE == item->multiplier ) - { - if( ITEM_VALUE_TYPE_FLOAT == item->value_type ) - { - if(GET_DBL_RESULT(value)) - { - UNSET_RESULT_EXCLUDING(value, AR_DOUBLE); - SET_DBL_RESULT(value, value->dbl * strtod(item->formula, NULL)); - } - } - else if( ITEM_VALUE_TYPE_UINT64 == item->value_type ) - { - if(GET_UI64_RESULT(value)) - { - UNSET_RESULT_EXCLUDING(value, AR_UINT64); - if(is_uint(item->formula) == SUCCEED) - { - SET_UI64_RESULT(value, value->ui64 * zbx_atoui64((item->formula))); - } - else - { - SET_UI64_RESULT(value, (zbx_uint64_t)((double)value->ui64 * strtod(item->formula, NULL))); - } - } - } - } - - add_history(item, value, now); - update_item(item, value, now); - update_functions( item ); -} diff --git a/src/zabbix_proxy/functions.h b/src/zabbix_proxy/functions.h deleted file mode 100644 index 79d64bbd..00000000 --- a/src/zabbix_proxy/functions.h +++ /dev/null @@ -1,34 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 SIA Zabbix -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -**/ - -#ifndef ZABBIX_FUNCTIONS_H -#define ZABBIX_FUNCTIONS_H - -#include "common.h" -#include "comms.h" -#include "db.h" -#include "sysinfo.h" - -void update_triggers (zbx_uint64_t itemid); -void update_functions(DB_ITEM *item); -int process_data(zbx_sock_t *sock,char *server,char *key, char *value,char *lastlogsize,char *timestamp, - char *source, char *severity); -void process_new_value(DB_ITEM *item, AGENT_RESULT *value); - -#endif diff --git a/src/zabbix_proxy/operations.c b/src/zabbix_proxy/operations.c deleted file mode 100644 index b4ae1996..00000000 --- a/src/zabbix_proxy/operations.c +++ /dev/null @@ -1,860 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 SIA Zabbix -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -**/ - - -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <netinet/in.h> -#include <netdb.h> - -#include <signal.h> - -#include <string.h> - -#include <time.h> - -#include "common.h" -#include "comms.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#include "poller/poller.h" -#include "poller/checks_agent.h" - -/****************************************************************************** - * * - * Function: send_to_user_medias * - * * - * Purpose: send notifications to user's medias (email, sms, whatever) * - * * - * Parameters: trigger - trigger data * - * action - action data * - * userid - user id * - * * - * Return value: nothing * - * * - * Author: Alexei Vladishev * - * * - * Comments: Cannot use action->userid as it may also be groupid * - * * - ******************************************************************************/ -static void send_to_user_medias(DB_EVENT *event,DB_OPERATION *operation, zbx_uint64_t userid) -{ - DB_MEDIA media; - DB_RESULT result; - DB_ROW row; - - zabbix_log( LOG_LEVEL_DEBUG, "In send_to_user_medias(objectid:" ZBX_FS_UI64 ")", - event->objectid); - - result = DBselect("select mediatypeid,sendto,active,severity,period from media where active=%d and userid=" ZBX_FS_UI64, - MEDIA_STATUS_ACTIVE, - userid); - - while((row=DBfetch(result))) - { - ZBX_STR2UINT64(media.mediatypeid, row[0]); - - media.sendto = row[1]; - media.active = atoi(row[2]); - media.severity = atoi(row[3]); - media.period = row[4]; - - zabbix_log( LOG_LEVEL_DEBUG, "Trigger severity [%d] Media severity [%d] Period [%s]", - event->trigger_priority, - media.severity, - media.period); - if(((1<<event->trigger_priority)&media.severity)==0) - { - zabbix_log( LOG_LEVEL_DEBUG, "Won't send message (severity)"); - continue; - } - if(check_time_period(media.period, (time_t)NULL) == 0) - { - zabbix_log( LOG_LEVEL_DEBUG, "Won't send message (period)"); - continue; - } - - DBadd_alert(operation->actionid, userid, event->objectid, media.mediatypeid,media.sendto,operation->shortdata,operation->longdata); - } - DBfree_result(result); - - zabbix_log(LOG_LEVEL_DEBUG, "End send_to_user_medias()"); -} - -/****************************************************************************** - * * - * Function: check_user_active * - * * - * Purpose: checks if user in any users_disabled group * - * * - * Parameters: userid - user id * - * * - * Return value: int SUCCEED / FAIL-if user disabled * - * * - * Author: Aly * - * * - * Comments: * - * * - ******************************************************************************/ -int check_user_active(zbx_uint64_t userid){ - DB_RESULT result; - DB_ROW row; - int rtrn = SUCCEED; - - result = DBselect("SELECT COUNT(g.usrgrpid) FROM users_groups ug, usrgrp g WHERE ug.userid=" ZBX_FS_UI64 " AND g.usrgrpid=ug.usrgrpid AND g.users_status=%d", userid, GROUP_STATUS_DISABLED); - - row = DBfetch(result); - if(row && (DBis_null(row[0])!=SUCCEED) && (atoi(row[0])>0)) - rtrn=FAIL; - - DBfree_result(result); - -return rtrn; -} - -/****************************************************************************** - * * - * Function: op_notify_user * - * * - * Purpose: send notifications to user or user groupd * - * * - * Parameters: trigger - trigger data * - * action - action data * - * * - * Return value: nothing * - * * - * Author: Alexei Vladishev * - * * - * Comments: action->recipient specifies user or group * - * * - ******************************************************************************/ -void op_notify_user(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation) -{ - DB_RESULT result; - DB_ROW row; - zbx_uint64_t userid; - - zabbix_log(LOG_LEVEL_DEBUG, "In send_to_user()"); - - if(operation->object == OPERATION_OBJECT_USER) - { - if(check_user_active(operation->objectid) == SUCCEED) - { - send_to_user_medias(event, operation, operation->objectid); - } - } - else if(operation->object == OPERATION_OBJECT_GROUP) - { - result = DBselect("select u.userid from users u, users_groups ug, usrgrp g where ug.usrgrpid=" ZBX_FS_UI64 " and ug.userid=u.userid and g.usrgrpid=ug.usrgrpid and g.users_status=%d", - operation->objectid,GROUP_STATUS_ACTIVE); - while((row=DBfetch(result))) - { - ZBX_STR2UINT64(userid, row[0]); - send_to_user_medias(event, operation, userid); - } - DBfree_result(result); - } - else - { - zabbix_log( LOG_LEVEL_WARNING, "Unknown object type [%d] for operationid [" ZBX_FS_UI64 "]", - operation->object, - operation->operationid); - zabbix_syslog("Unknown object type [%d] for operationid [" ZBX_FS_UI64 "]", - operation->object, - operation->operationid); - } - zabbix_log(LOG_LEVEL_DEBUG, "End send_to_user()"); -} - - -/****************************************************************************** - * * - * Function: run_remote_commands * - * * - * Purpose: run remote command on specific host * - * * - * Parameters: host_name - host name * - * command - remote command * - * * - * Return value: nothing * - * * - * Author: Eugene Grigorjev * - * * - * Comments: * - * * - ******************************************************************************/ - -static void run_remote_command(char* host_name, char* command) -{ - int ret = 9; - - AGENT_RESULT agent_result; - DB_ITEM item; - DB_RESULT result; - DB_ROW row; - - assert(host_name); - assert(command); - - zabbix_log(LOG_LEVEL_DEBUG, "In run_remote_command(hostname:%s,command:%s)", - host_name, - command); - - result = DBselect("select distinct host,ip,useip,port,dns from hosts where host='%s' and " ZBX_COND_NODEID, - host_name, - LOCAL_NODE("hostid")); - row = DBfetch(result); - if(row) - { - item.host_name = row[0]; - item.host_ip=row[1]; - item.useip=atoi(row[2]); - item.port=atoi(row[3]); - item.host_dns=row[4]; - - zbx_snprintf(item.key,ITEM_KEY_LEN_MAX,"system.run[%s,nowait]",command); - - alarm(CONFIG_TIMEOUT); - - ret = get_value_agent(&item, &agent_result); - - alarm(0); - } - DBfree_result(result); - - zabbix_log(LOG_LEVEL_DEBUG, "End run_remote_command(result:%d)", - ret); -} - -/****************************************************************************** - * * - * Function: get_next_command * - * * - * Purpose: parse action script on remote commands * - * * - * Parameters: command_list - command list * - * alias - (output) of host name or group name * - * is_group - (output) 0 if alias is a host name * - * 1 if alias is a group name * - * command - (output) remote command * - * * - * Return value: 0 - correct comand is readed * - * 1 - EOL * - * * - * Author: Eugene Grigorjev * - * * - * Comments: * - * * - ******************************************************************************/ - -#define CMD_ALIAS 0 -#define CMD_REM_COMMAND 1 - -static int get_next_command(char** command_list, char** alias, int* is_group, char** command) -{ - int state = CMD_ALIAS; - int len = 0; - int i = 0; - - assert(alias); - assert(is_group); - assert(command); - - zabbix_log(LOG_LEVEL_DEBUG, "In get_next_command(command_list:%s)", - *command_list); - - *alias = NULL; - *is_group = 0; - *command = NULL; - - - if((*command_list)[0] == '\0' || (*command_list)==NULL) { - zabbix_log(LOG_LEVEL_DEBUG, "Result get_next_command [EOL]"); - return 1; - } - - *alias = *command_list; - len = strlen(*command_list); - - for(i=0; i < len; i++) - { - if(state == CMD_ALIAS) - { - if((*command_list)[i] == '#'){ - *is_group = 1; - (*command_list)[i] = '\0'; - state = CMD_REM_COMMAND; - *command = &(*command_list)[i+1]; - }else if((*command_list)[i] == ':'){ - *is_group = 0; - (*command_list)[i] = '\0'; - state = CMD_REM_COMMAND; - *command = &(*command_list)[i+1]; - } - } else if(state == CMD_REM_COMMAND) { - if((*command_list)[i] == '\r') - { - (*command_list)[i] = '\0'; - } else if((*command_list)[i] == '\n') - { - (*command_list)[i] = '\0'; - (*command_list) = &(*command_list)[i+1]; - break; - } - } - if((*command_list)[i+1] == '\0') - { - (*command_list) = &(*command_list)[i+1]; - break; - } - } - - zabbix_log(LOG_LEVEL_DEBUG, "End get_next_command(alias:%s,is_group:%i,command:%s)", - *alias, - *is_group, - *command); - - return 0; -} - -/****************************************************************************** - * * - * Function: run_commands * - * * - * Purpose: run remote commandlist for specific action * - * * - * Parameters: trigger - trigger data * - * action - action data * - * * - * Return value: nothing * - * * - * Author: Eugene Grigorjev * - * * - * Comments: commands devided with newline * - * * - ******************************************************************************/ -void op_run_commands(DB_EVENT *event, DB_OPERATION *operation) -{ - DB_RESULT result; - DB_ROW row; - - char *cmd_list = NULL; - char *alias = NULL; - char *command = NULL; - int is_group = 0; - - assert(event); - assert(operation); - - zabbix_log( LOG_LEVEL_DEBUG, "In run_commands(operationid:" ZBX_FS_UI64 ")", - operation->operationid); - - cmd_list = operation->longdata; - while(get_next_command(&cmd_list,&alias,&is_group,&command)!=1) - { - if(!alias || !command) continue; - if(alias == '\0' || command == '\0') continue; - if(is_group) - { - result = DBselect("select distinct h.host from hosts_groups hg,hosts h, groups g where hg.hostid=h.hostid and hg.groupid=g.groupid and g.name='%s' and" ZBX_COND_NODEID, - alias, - LOCAL_NODE("h.hostid")); - while((row=DBfetch(result))) - { - run_remote_command(row[0], command); - } - - DBfree_result(result); - } - else - { - run_remote_command(alias, command); - } -/* DBadd_alert(action->actionid,trigger->triggerid, userid, media.mediatypeid,media.sendto,action->subject,action->scripts); */ /* TODO !!! Add alert for remote commands !!! */ - } - zabbix_log( LOG_LEVEL_DEBUG, "End run_commands()"); -} - -/****************************************************************************** - * * - * Function: select dhostid by dserviceid * - * * - * Purpose: select discovered host id * - * * - * Parameters: dserviceid - servce id * - * * - * Return value: dhostid - existing dhostid, 0 - if not found * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static zbx_uint64_t select_dhostid_by_dserviceid(zbx_uint64_t dserviceid) -{ - DB_RESULT result; - DB_ROW row; - zbx_uint64_t dhostid = 0; - - zabbix_log(LOG_LEVEL_DEBUG, "In select_dhostid_by_dserviceid(dserviceid:" ZBX_FS_UI64 ")", - dserviceid); - - result = DBselect("select dhostid from dservices where dserviceid=" ZBX_FS_UI64, - dserviceid); - row = DBfetch(result); - if(row && DBis_null(row[0]) != SUCCEED) - { - ZBX_STR2UINT64(dhostid, row[0]); - } - DBfree_result(result); - - zabbix_log(LOG_LEVEL_DEBUG, "End select_dhostid_by_dserviceid()"); - - return dhostid; -} - -/****************************************************************************** - * * - * Function: select hostid of discovered host * - * * - * Purpose: select discovered host * - * * - * Parameters: dhostid - discovered host id * - * * - * Return value: hostid - existing hostid, o - if not found * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static zbx_uint64_t select_discovered_host(zbx_uint64_t dhostid) -{ - DB_RESULT result; - DB_ROW row; - zbx_uint64_t hostid = 0; - - zabbix_log(LOG_LEVEL_DEBUG, "In select_discovered_host(dhostid:" ZBX_FS_UI64 ")", - dhostid); - - result = DBselect("select h.hostid from dhosts d,hosts h where h.ip=d.ip and d.dhostid=" ZBX_FS_UI64, - dhostid); - row = DBfetch(result); - if(row && DBis_null(row[0]) != SUCCEED) - { - ZBX_STR2UINT64(hostid, row[0]); - } - DBfree_result(result); - - zabbix_log(LOG_LEVEL_DEBUG, "End select_discovered_host()"); - - return hostid; -} - -/****************************************************************************** - * * - * Function: add host if not added already * - * * - * Purpose: add discovered host * - * * - * Parameters: dhostid - discovered host id * - * * - * Return value: hostid - new/existing hostid * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static zbx_uint64_t add_discovered_host(zbx_uint64_t dhostid) -{ - DB_RESULT result; - DB_RESULT result2; - DB_ROW row; - DB_ROW row2; - zbx_uint64_t hostid = 0; - char *ip; - char host[MAX_STRING_LEN], host_esc[MAX_STRING_LEN]; - - zabbix_log(LOG_LEVEL_DEBUG, "In add_discovered_host(dhostid:" ZBX_FS_UI64 ")", - dhostid); - - result = DBselect("select ip from dhosts where dhostid=" ZBX_FS_UI64, - dhostid); - row = DBfetch(result); - if(row && DBis_null(row[0]) != SUCCEED) - { - ip=row[0]; - - alarm(CONFIG_TIMEOUT); - zbx_gethost_by_ip(ip, host, sizeof(host)); - alarm(0); - - DBescape_string(host, host_esc, sizeof(host_esc)); - - result2 = DBselect("select hostid from hosts where ip='%s' and " ZBX_COND_NODEID, - ip, - LOCAL_NODE("hostid")); - row2 = DBfetch(result2); - if(!row2 || DBis_null(row2[0]) == SUCCEED) - { - hostid = DBget_maxid("hosts","hostid"); - DBexecute("insert into hosts (hostid,host,useip,ip,dns) values (" ZBX_FS_UI64 ",'%s',1,'%s','%s')", - hostid, - (host[0] != '\0' ? host_esc : ip), /* Use host name if exists, IP otherwise */ - ip, - host_esc); - } - else - { - ZBX_STR2UINT64(hostid, row2[0]); - if(host_esc[0] != '\0') - { - DBexecute("update hosts set dns='%s' where hostid=" ZBX_FS_UI64, - host_esc, - hostid); - } - } - DBfree_result(result2); - } - DBfree_result(result); - - zabbix_log(LOG_LEVEL_DEBUG, "End add_discovered_host()"); - - return hostid; -} - -/****************************************************************************** - * * - * Function: op_host_add * - * * - * Purpose: add discovered host * - * * - * Parameters: trigger - trigger data * - * action - action data * - * * - * Return value: nothing * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void op_host_add(DB_EVENT *event) -{ - zbx_uint64_t hostid; - zbx_uint64_t dhostid = 0; - - zabbix_log(LOG_LEVEL_DEBUG, "In op_host_add()"); - - if(event->object == EVENT_OBJECT_DHOST) - { - dhostid = event->objectid; - } - else if(event->object == EVENT_OBJECT_DSERVICE) - { - dhostid = select_dhostid_by_dserviceid(event->objectid); - } - - hostid = add_discovered_host(dhostid); - - zabbix_log(LOG_LEVEL_DEBUG, "End op_host_add()"); -} - -/****************************************************************************** - * * - * Function: op_host_del * - * * - * Purpose: delete host * - * * - * Parameters: * - * * - * Return value: nothing * - * * - * Author: Eugene Grigorjev * - * * - * Comments: * - * * - ******************************************************************************/ -void op_host_del(DB_EVENT *event) -{ - zbx_uint64_t hostid, dhostid; - - zabbix_log(LOG_LEVEL_DEBUG, "In op_host_del()"); - - if(event->object == EVENT_OBJECT_DSERVICE) - { - dhostid = select_dhostid_by_dserviceid(event->objectid); - } - else - { - dhostid = event->objectid; - } - - hostid = select_discovered_host(dhostid); - if(hostid != 0) - { - DBdelete_host(hostid); - } - - zabbix_log(LOG_LEVEL_DEBUG, "End op_host_del()"); -} - -/****************************************************************************** - * * - * Function: op_group_add * - * * - * Purpose: add group to discovered host * - * * - * Parameters: trigger - trigger data * - * action - action data * - * * - * Return value: nothing * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void op_group_add(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation) -{ - DB_RESULT result; - DB_ROW row; - zbx_uint64_t hostgroupid, groupid, hostid, dhostid; - - zabbix_log(LOG_LEVEL_DEBUG, "In op_group_add(object:%d)", - event->object); - - if(operation->operationtype != OPERATION_TYPE_GROUP_ADD) return; - if(event->object != EVENT_OBJECT_DHOST && event->object != EVENT_OBJECT_DSERVICE) return; - - - if(event->object == EVENT_OBJECT_DSERVICE) - { - dhostid = select_dhostid_by_dserviceid(event->objectid); - } - else - { - dhostid = event->objectid; - } - - hostid = add_discovered_host(dhostid); - if(hostid != 0) - { - groupid = operation->objectid; - result = DBselect("select hostgroupid from hosts_groups where groupid=" ZBX_FS_UI64 " and hostid=" ZBX_FS_UI64, - groupid, - hostid); - row = DBfetch(result); - if(!row || DBis_null(row[0]) == SUCCEED) - { - hostgroupid = DBget_maxid("hosts_groups","hostgroupid"); - DBexecute("insert into hosts_groups (hostgroupid,hostid,groupid) values (" ZBX_FS_UI64 "," ZBX_FS_UI64 "," ZBX_FS_UI64 ")", - hostgroupid, - hostid, - groupid); - } - DBfree_result(result); - } - - zabbix_log(LOG_LEVEL_DEBUG, "End op_group_add()"); -} - -/****************************************************************************** - * * - * Function: op_group_del * - * * - * Purpose: delete group from discovered host * - * * - * Parameters: trigger - trigger data * - * action - action data * - * * - * Return value: nothing * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void op_group_del(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation) -{ - zbx_uint64_t groupid, hostid, dhostid; - - zabbix_log(LOG_LEVEL_DEBUG, "In op_group_del()"); - - if(operation->operationtype != OPERATION_TYPE_GROUP_REMOVE) return; - if(event->object != EVENT_OBJECT_DHOST && event->object != EVENT_OBJECT_DSERVICE) return; - - if(event->object == EVENT_OBJECT_DSERVICE) - { - dhostid = select_dhostid_by_dserviceid(event->objectid); - } - else - { - dhostid = event->objectid; - } - - hostid = select_discovered_host(dhostid); - if(hostid != 0) - { - groupid = operation->objectid; - DBexecute("delete from hosts_groups where hostid=" ZBX_FS_UI64 " and groupid=" ZBX_FS_UI64, - hostid, - groupid); - } - - zabbix_log(LOG_LEVEL_DEBUG, "End op_group_del()"); -} - -/****************************************************************************** - * * - * Function: op_template_add * - * * - * Purpose: link host with template * - * * - * Parameters: * - * * - * Return value: nothing * - * * - * Author: Eugene Grigorjev * - * * - * Comments: * - * * - ******************************************************************************/ -void op_template_add(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation) -{ - DB_RESULT result; - DB_ROW row; - zbx_uint64_t hosttemplateid, templateid, hostid, dhostid; - - zabbix_log(LOG_LEVEL_DEBUG, "In op_template_add(object:%d)", - event->object); - - if(operation->operationtype != OPERATION_TYPE_TEMPLATE_ADD) return; - if(event->object != EVENT_OBJECT_DHOST && event->object != EVENT_OBJECT_DSERVICE) return; - - - if(event->object == EVENT_OBJECT_DSERVICE) - { - dhostid = select_dhostid_by_dserviceid(event->objectid); - } - else - { - dhostid = event->objectid; - } - - hostid = add_discovered_host(dhostid); - if(hostid != 0) - { - templateid = operation->objectid; - - result = DBselect("select hosttemplateid from hosts_templates where templateid=" ZBX_FS_UI64 " and hostid=" ZBX_FS_UI64, - templateid, - hostid); - row = DBfetch(result); - if(!row || DBis_null(row[0]) == SUCCEED) - { - hosttemplateid = DBget_maxid("hosts_templates","hosttemplateid"); - DBexecute("begin;"); - - DBexecute("insert into hosts_templates (hosttemplateid,hostid,templateid) values (" ZBX_FS_UI64 "," ZBX_FS_UI64 "," ZBX_FS_UI64 ")", - hosttemplateid, - hostid, - templateid); - - DBsync_host_with_template(hostid, templateid); - - DBexecute("commit;"); - } - DBfree_result(result); - } - - zabbix_log(LOG_LEVEL_DEBUG, "End op_template_add()"); -} - -/****************************************************************************** - * * - * Function: op_template_del * - * * - * Purpose: unlink and clear host from template * - * * - * Parameters: * - * * - * Return value: nothing * - * * - * Author: Eugene Grigorjev * - * * - * Comments: * - * * - ******************************************************************************/ -void op_template_del(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation) -{ - DB_RESULT result; - DB_ROW row; - zbx_uint64_t templateid, hostid, dhostid; - - zabbix_log(LOG_LEVEL_DEBUG, "In op_template_del(object:%d)", - event->object); - - if(operation->operationtype != OPERATION_TYPE_TEMPLATE_REMOVE) return; - if(event->object != EVENT_OBJECT_DHOST && event->object != EVENT_OBJECT_DSERVICE) return; - - - if(event->object == EVENT_OBJECT_DSERVICE) - { - dhostid = select_dhostid_by_dserviceid(event->objectid); - } - else - { - dhostid = event->objectid; - } - - hostid = select_discovered_host(dhostid); - if(hostid != 0) - { - templateid = operation->objectid; - - result = DBselect("select hosttemplateid from hosts_templates where templateid=" ZBX_FS_UI64 " and hostid=" ZBX_FS_UI64, - templateid, - hostid); - - if( (row = DBfetch(result)) ) - { - DBexecute("begin;"); - - DBdelete_template_elements(hostid, templateid, 0 /* not a unlink mode */); - - DBexecute("delete from hosts_templates where " - "hostid=" ZBX_FS_UI64 " and templateid=" ZBX_FS_UI64, - hostid, - templateid); - - DBexecute("commit;"); - } - DBfree_result(result); - } - - zabbix_log(LOG_LEVEL_DEBUG, "End op_template_del()"); -} - diff --git a/src/zabbix_proxy/operations.h b/src/zabbix_proxy/operations.h deleted file mode 100644 index 523f03c3..00000000 --- a/src/zabbix_proxy/operations.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 SIA Zabbix -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -**/ - - -#ifndef ZABBIX_OPERATIONS_H -#define ZABBIX_OPERATIONS_H - -#include "common.h" -#include "db.h" - -void op_template_add(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation); -void op_template_del(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation); -void op_group_add(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation); -void op_group_del(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation); -void op_host_add(DB_EVENT *event); -void op_host_del(DB_EVENT *event); -void op_run_commands(DB_EVENT *event, DB_OPERATION *operation); -void op_notify_user(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation); - -#endif diff --git a/src/zabbix_proxy/proxy.c b/src/zabbix_proxy/proxy.c index e5b032fd..05f9490a 100644 --- a/src/zabbix_proxy/proxy.c +++ b/src/zabbix_proxy/proxy.c @@ -29,14 +29,14 @@ #include "log.h" #include "zlog.h" #include "zbxgetopt.h" +#include "mutexs.h" #ifdef ZABBIX_TEST #include "zbxjson.h" #endif -#include "functions.h" -#include "expression.h" #include "sysinfo.h" +#include "zbxserver.h" #include "daemon.h" @@ -163,6 +163,9 @@ int CONFIG_REFRESH_UNSUPPORTED = 0; /* Zabbix server sturtup time */ int CONFIG_SERVER_STARTUP_TIME = 0; +/* Mutex for node syncs */ +ZBX_MUTEX node_sync_access; + /****************************************************************************** * * * Function: init_config * @@ -436,6 +439,11 @@ int MAIN_ZABBIX_ENTRY(void) /* Do not close database. It is required for database cache */ /* DBclose();*/ + if (ZBX_MUTEX_ERROR == zbx_mutex_create_force(&node_sync_access, ZBX_MUTEX_NODE_SYNC)) { + zbx_error("Unable to create mutex for node syncs"); + exit(FAIL); + } + threads = calloc(1+CONFIG_POLLER_FORKS+CONFIG_TRAPPERD_FORKS+CONFIG_PINGER_FORKS +CONFIG_HOUSEKEEPER_FORKS+CONFIG_UNREACHABLE_POLLER_FORKS +/*CONFIG_NODEWATCHER_FORKS+*/CONFIG_HTTPPOLLER_FORKS+CONFIG_DISCOVERER_FORKS, @@ -520,8 +528,8 @@ int MAIN_ZABBIX_ENTRY(void) zabbix_log(LOG_LEVEL_WARNING, "server #%d started [Poller for unreachable hosts. SNMP:"SNMP_FEATURE_STATUS"]", server_num); - main_poller_loop(ZBX_POLLER_TYPE_UNREACHABLE, server_num - (CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS - + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS)); + main_poller_loop(ZBX_PROCESS_PROXY, ZBX_POLLER_TYPE_UNREACHABLE, server_num - (CONFIG_POLLER_FORKS + + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS)); } else if (server_num <= CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS + CONFIG_UNREACHABLE_POLLER_FORKS + CONFIG_HTTPPOLLER_FORKS) @@ -529,8 +537,8 @@ int MAIN_ZABBIX_ENTRY(void) zabbix_log( LOG_LEVEL_WARNING, "server #%d started [HTTP Poller]", server_num); - main_httppoller_loop(server_num - CONFIG_POLLER_FORKS - CONFIG_TRAPPERD_FORKS - CONFIG_PINGER_FORKS - - CONFIG_HOUSEKEEPER_FORKS - CONFIG_UNREACHABLE_POLLER_FORKS); + main_httppoller_loop(ZBX_PROCESS_PROXY, server_num - CONFIG_POLLER_FORKS - CONFIG_TRAPPERD_FORKS + - CONFIG_PINGER_FORKS - CONFIG_HOUSEKEEPER_FORKS - CONFIG_UNREACHABLE_POLLER_FORKS); } else if (server_num <= CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS + CONFIG_UNREACHABLE_POLLER_FORKS + CONFIG_HTTPPOLLER_FORKS + CONFIG_DISCOVERER_FORKS) @@ -594,6 +602,7 @@ void zbx_on_exit() free_database_cache(); } DBclose(); + zbx_mutex_destroy(&node_sync_access); zabbix_close_log(); #ifdef HAVE_SQLITE3 diff --git a/src/zabbix_proxy/zlog.c b/src/zabbix_proxy/zlog.c index a3071662..4eeccfc4 100644 --- a/src/zabbix_proxy/zlog.c +++ b/src/zabbix_proxy/zlog.c @@ -30,9 +30,9 @@ #include <time.h> #include "common.h" -#include "functions.h" #include "log.h" #include "zlog.h" +#include "zbxserver.h" /****************************************************************************** * * @@ -65,11 +65,11 @@ void __zbx_zabbix_syslog(const char *fmt, ...) /* This is made to disable writing to database for watchdog */ if(CONFIG_ENABLE_LOG == 0) return; - result = DBselect("select %s where h.hostid=i.hostid and i.key_='%s' and i.value_type=%d and" ZBX_COND_NODEID, + result = DBselect("select %s where h.hostid=i.hostid and i.key_='%s' and i.value_type=%d" DB_NODE, ZBX_SQL_ITEM_SELECT, SERVER_ZABBIXLOG_KEY, ITEM_VALUE_TYPE_STR, - LOCAL_NODE("h.hostid")); + DBnode_local("h.hostid")); while((row=DBfetch(result))) { diff --git a/src/zabbix_server/Makefile.am b/src/zabbix_server/Makefile.am index 2b05156a..e5adeee5 100644 --- a/src/zabbix_server/Makefile.am +++ b/src/zabbix_server/Makefile.am @@ -17,13 +17,10 @@ SUBDIRS = \ sbin_PROGRAMS = zabbix_server zabbix_server_SOURCES = \ - evalfunc.c evalfunc.h \ - expression.c expression.h \ actions.c actions.h \ operations.c operations.h \ events.c events.h \ zlog.c \ - functions.c functions.h \ server.c if JABBER @@ -60,6 +57,7 @@ zabbix_server_LDADD = \ $(top_srcdir)/src/libs/zbxcrypto/libzbxcrypto.a \ $(top_srcdir)/src/libs/zbxcomms/libzbxcomms.a \ $(top_srcdir)/src/libs/zbxjson/libzbxjson.a \ + $(top_srcdir)/src/libs/zbxserver/libzbxserver.a \ $(JABBER_LDADD) \ @SERVER_LIBS@ diff --git a/src/zabbix_server/actions.c b/src/zabbix_server/actions.c index 258bf7d0..a846bcf4 100644 --- a/src/zabbix_server/actions.c +++ b/src/zabbix_server/actions.c @@ -41,11 +41,10 @@ #include "common.h" #include "db.h" #include "log.h" -#include "zlog.h" +#include "zbxserver.h" #include "actions.h" #include "operations.h" -#include "expression.h" #include "poller/poller.h" #include "poller/checks_agent.h" diff --git a/src/zabbix_server/alerter/alerter.c b/src/zabbix_server/alerter/alerter.c index d3967e6f..817a272a 100644 --- a/src/zabbix_server/alerter/alerter.c +++ b/src/zabbix_server/alerter/alerter.c @@ -21,7 +21,6 @@ #include "cfg.h" #include "db.h" -#include "../functions.h" #include "log.h" #include "zlog.h" #include "email.h" @@ -30,6 +29,7 @@ # include "jabber.h" #endif #include "daemon.h" +#include "zbxserver.h" #include "alerter.h" diff --git a/src/zabbix_server/evalfunc.h b/src/zabbix_server/evalfunc.h deleted file mode 100644 index 5da6b67d..00000000 --- a/src/zabbix_server/evalfunc.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 SIA Zabbix -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -**/ - - -#ifndef ZABBIX_EVALFUNC_H -#define ZABBIX_EVALFUNC_H - -#include "common.h" -#include "db.h" - -#define EVALUATE_FUNCTION_NORMAL 0 -#define EVALUATE_FUNCTION_SUFFIX 1 - -#define ZBX_FLAG_SEC 0 -#define ZBX_FLAG_VALUES 1 - -extern int CONFIG_SERVER_STARTUP_TIME; - -int evaluate_function(char *value,DB_ITEM *item,char *function,char *parameter); -int evaluate_function2(char *value,char *host,char *key,char *function,char *parameter); -int add_value_suffix(char *value, int max_len, char *units, int value_type); - -#endif diff --git a/src/zabbix_server/events.c b/src/zabbix_server/events.c index 7c3b4ca1..7c5880ac 100644 --- a/src/zabbix_server/events.c +++ b/src/zabbix_server/events.c @@ -42,9 +42,9 @@ #include "db.h" #include "log.h" #include "zlog.h" +#include "zbxserver.h" #include "actions.h" -#include "functions.h" #include "events.h" /****************************************************************************** diff --git a/src/zabbix_server/expression.c b/src/zabbix_server/expression.c deleted file mode 100644 index f288f754..00000000 --- a/src/zabbix_server/expression.c +++ /dev/null @@ -1,1631 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 SIA Zabbix -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -**/ - - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <math.h> - -#include "expression.h" -#include "functions.h" -#include "evalfunc.h" -#include "common.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - - - -/****************************************************************************** - * * - * Function: str2double * - * * - * Purpose: convert string to double * - * * - * Parameters: str - string to convert * - * * - * Return value: converted double value * - * * - * Author: Alexei Vladishev * - * * - * Comments: the function automatically processes prefixes 'K','M','G' * - * * - ******************************************************************************/ -double str2double(char *str) -{ - if(str[strlen(str)-1] == 'K') - { - str[strlen(str)-1] = 0; - return (double)1024*atof(str); - } - else if(str[strlen(str)-1] == 'M') - { - str[strlen(str)-1] = 0; - return (double)1024*1024*atof(str); - } - else if(str[strlen(str)-1] == 'G') - { - str[strlen(str)-1] = 0; - return (double)1024*1024*1024*atof(str); - } - return atof(str); -} - - -/****************************************************************************** - * * - * Function: delete_spaces * - * * - * Purpose: delete all spaces * - * * - * Parameters: c - string to delete spaces * - * * - * Return value: the string wtihout spaces * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void delete_spaces(char *c) -{ - int i,j; - - j=0; - for(i=0;c[i]!=0;i++) - { - if( c[i] != ' ') - { - c[j]=c[i]; - j++; - } - } - c[j]=0; -} - -/****************************************************************************** - * * - * Function: evaluate_simple * - * * - * Purpose: evaluate simple expression * - * * - * Parameters: exp - expression string * - * * - * Return value: SUCCEED - evaluated succesfully, result - value of the exp * - * FAIL - otherwise * - * * - * Author: Alexei Vladishev * - * * - * Comments: format: <double> or <double> <operator> <double> * - * * - * It is recursive function! * - * * - ******************************************************************************/ -int evaluate_simple(double *result,char *exp,char *error,int maxerrlen) -{ - double value1,value2; - char first[MAX_STRING_LEN],second[MAX_STRING_LEN]; - char *p; - - zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_simple(%s)", - exp); - -/* Remove left and right spaces */ - lrtrim_spaces(exp); - -/* Compress repeating - and +. Add prefix N to negative numebrs. */ - compress_signs(exp); - - /* We should process negative prefix, i.e. N123 == -123 */ - if( exp[0]=='N' && is_double_prefix(exp+1) == SUCCEED ) - { -/* str2double support prefixes */ - *result=-str2double(exp+1); - return SUCCEED; - } - else if( exp[0]!='N' && is_double_prefix(exp) == SUCCEED ) - { -/* str2double support prefixes */ - *result=str2double(exp); - return SUCCEED; - } - - /* Operators with lowest priority come first */ - /* HIGHEST / * - + < > # = & | LOWEST */ - if( (p = strchr(exp,'|')) != NULL ) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", error); - zabbix_syslog("%s", error); - return FAIL; - } - if( value1 == 1) - { - *result=value1; - return SUCCEED; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", error); - zabbix_syslog("%s", error); - return FAIL; - } - if( value2 == 1) - { - *result=value2; - return SUCCEED; - } - *result=0; - return SUCCEED; - } - if( (p = strchr(exp,'&')) != NULL ) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", error); - zabbix_syslog("%s", error); - return FAIL; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", error); - zabbix_syslog("%s", error); - return FAIL; - } - if( (value1 == 1) && (value2 == 1) ) - { - *result=1; - } - else - { - *result=0; - } - return SUCCEED; - } - if((p = strchr(exp,'=')) != NULL) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( cmp_double(value1,value2) ==0 ) - { - *result=1; - } - else - { - *result=0; - } - return SUCCEED; - } - if((p = strchr(exp,'#')) != NULL) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( cmp_double(value1,value2) != 0 ) - { - *result=1; - } - else - { - *result=0; - } - return SUCCEED; - } - if((p = strchr(exp,'>')) != NULL) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", error); - zabbix_syslog("%s", error); - return FAIL; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", error); - zabbix_syslog("%s", error); - return FAIL; - } - if( value1 > value2 ) - { - *result=1; - } - else - { - *result=0; - } - return SUCCEED; - } - if((p = strchr(exp,'<')) != NULL) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( value1 < value2 ) - { - *result=1; - } - else - { - *result=0; - } - zabbix_log(LOG_LEVEL_DEBUG, "Result [" ZBX_FS_DBL "]",*result ); - return SUCCEED; - } - if((p = strchr(exp,'+')) != NULL) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - *result=value1+value2; - return SUCCEED; - } - if((p = strchr(exp,'-')) != NULL) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - *result=value1-value2; - return SUCCEED; - } - if((p = strchr(exp,'*')) != NULL) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - *result=value1*value2; - return SUCCEED; - } - if((p = strchr(exp,'/')) != NULL) - { - *p=0; - strscpy( first, exp); - *p='|'; - p++; - strscpy( second, p); - if( evaluate_simple(&value1,first,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if( evaluate_simple(&value2,second,error,maxerrlen) == FAIL ) - { - zabbix_log(LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if(cmp_double(value2,0) == 0) - { - zbx_snprintf(error,maxerrlen,"Division by zero. Cannot evaluate expression [%s/%s]", - first, - second); - zabbix_log(LOG_LEVEL_WARNING, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - else - { - *result=value1/value2; - } - return SUCCEED; - } - else - { - zbx_snprintf(error,maxerrlen,"Format error or unsupported operator. Exp: [%s]", - exp); - zabbix_log(LOG_LEVEL_WARNING, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - - return SUCCEED; -} - -/****************************************************************************** - * * - * Function: evaluate * - * * - * Purpose: evaluate simplified expression * - * * - * Parameters: exp - expression string * - * * - * Return value: SUCCEED - evaluated succesfully, result - value of the exp * - * FAIL - otherwise * - * * - * Author: Alexei Vladishev * - * * - * Comments: example: ({15}>10)|({123}=1) * - * * - ******************************************************************************/ -int evaluate(int *result, char *exp, char *error, int maxerrlen) -{ - double value; - char *res; - char simple[MAX_STRING_LEN]; - char tmp[MAX_STRING_LEN]; - char value_str[MAX_STRING_LEN]; - int i,l,r; - char c; - int t; - - zabbix_log(LOG_LEVEL_DEBUG, "In evaluate(%s)", - exp); - - res = NULL; - - strscpy(tmp, exp); - t=0; - while( find_char( tmp, ')' ) != FAIL ) - { - l=-1; - r=find_char(tmp,')'); - for(i=r;i>=0;i--) - { - if( tmp[i] == '(' ) - { - l=i; - break; - } - } - if( l == -1 ) - { - zbx_snprintf(error, maxerrlen, "Cannot find left bracket [(]. Expression:[%s]", - tmp); - zabbix_log(LOG_LEVEL_WARNING, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - for(i=l+1;i<r;i++) - { - simple[i-l-1]=tmp[i]; - } - simple[r-l-1]=0; - - if( evaluate_simple( &value, simple, error, maxerrlen ) != SUCCEED ) - { - /* Changed to LOG_LEVEL_DEBUG */ - zabbix_log( LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - - /* res = first+simple+second */ - c=tmp[l]; tmp[l]='\0'; - res = zbx_strdcat(res, tmp); - tmp[l]=c; - - zbx_snprintf(value_str,MAX_STRING_LEN-1,"%lf", - value); - res = zbx_strdcat(res, value_str); - res = zbx_strdcat(res, tmp+r+1); - - delete_spaces(res); - strscpy(tmp,res); - - zbx_free(res); res = NULL; - } - if( evaluate_simple( &value, tmp, error, maxerrlen ) != SUCCEED ) - { - zabbix_log(LOG_LEVEL_WARNING, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - if(cmp_double(value,0) == 0) - { - *result = TRIGGER_VALUE_FALSE; - } - else - { - *result = TRIGGER_VALUE_TRUE; - } - - zabbix_log(LOG_LEVEL_DEBUG, "End evaluate(result:%lf)", - value); - - return SUCCEED; -} - -/****************************************************************************** - * * - * Function: extract_numbers * - * * - * Purpose: Extract from string numbers with prefixes (A-Z) * - * * - * Return value: * - * * - * Author: Eugene Grigorjev * - * * - * Comments: !!! Don't forget sync code with PHP !!! * - * Use zbx_free_numbers to free allocated memory * - * * - ******************************************************************************/ -static char** extract_numbers(char *str, int *count) -{ - char *s = NULL; - char *e = NULL; - - char **result = NULL; - - int dot_founded = 0; - int len = 0; - - assert(count); - - *count = 0; - - /* find start of number */ - for ( s = str; *s; s++) - { - if ( !isdigit(*s) ) { - continue; /* for s */ - } - - if ( s != str && '{' == *(s-1) ) { - /* skip functions '{65432}' */ - s = strchr(s, '}'); - continue; /* for s */ - } - - dot_founded = 0; - /* find end of number */ - for ( e = s; *e; e++ ) - { - if ( isdigit(*e) ) { - continue; /* for e */ - } - else if ( '.' == *e && !dot_founded ) { - dot_founded = 1; - continue; /* for e */ - } - else if ( *e >= 'A' && *e <= 'Z' ) - { - e++; - } - break; /* for e */ - } - - /* number founded */ - len = e - s; - (*count)++; - result = zbx_realloc(result, sizeof(char*) * (*count)); - result[(*count)-1] = zbx_malloc(NULL, len + 1); - memcpy(result[(*count)-1], s, len); - result[(*count)-1][len] = '\0'; - - s = e; - } - - return result; -} - -static void zbx_free_numbers(char ***numbers, int count) -{ - register int i = 0; - - if ( !numbers ) return; - if ( !*numbers ) return; - - for ( i = 0; i < count; i++ ) - { - zbx_free((*numbers)[i]); - } - - zbx_free(*numbers); -} - -/****************************************************************************** - * * - * Function: expand_trigger_description_constants * - * * - * Purpose: substitute simple macros in data string with real values * - * * - * Parameters: data - trigger description * - * * - * Return value: * - * * - * Author: Eugene Grigorjev * - * * - * Comments: !!! Don't forget sync code with PHP !!! * - * replcae ONLY $1-9 macros NOT {HOSTNAME} * - * * - ******************************************************************************/ -static void expand_trigger_description_constants( - char **data, - zbx_uint64_t triggerid - ) -{ - DB_RESULT db_trigger; - DB_ROW db_trigger_data; - - char **numbers = NULL; - int numbers_cnt = 0; - - int i = 0; - - char *new_str = NULL; - - char replace[3] = "$0"; - - db_trigger = DBselect("select expression from triggers where triggerid=" ZBX_FS_UI64, triggerid); - - if ( (db_trigger_data = DBfetch(db_trigger)) ) { - - numbers = extract_numbers(db_trigger_data[0], &numbers_cnt); - - for ( i = 0; i < 9; i++ ) - { - replace[1] = '0' + i + 1; - new_str = string_replace( - *data, - replace, - i < numbers_cnt ? - numbers[i] : - "" - ); - zbx_free(*data); - *data = new_str; - } - - zbx_free_numbers(&numbers, numbers_cnt); - } - - DBfree_result(db_trigger); -} - -/****************************************************************************** - * * - * Function: substitute_simple_macros * - * * - * Purpose: substitute simple macros in data string with real values * - * * - * Parameters: trigger - trigger structure * - * action - action structure (NULL if unknown) * - * data - data string * - * * - * Return value: * - * * - * Author: Eugene Grigorjev * - * * - * Comments: {DATE},{TIME},{HOSTNAME},{IPADDRESS},{STATUS}, * - * {TRIGGER.NAME}, {TRIGGER.KEY}, {TRIGGER.SEVERITY} * - * * - ******************************************************************************/ -/* definition of macros variables */ -#define MVAR_DATE "{DATE}" -#define MVAR_EVENT_ID "{EVENT.ID}" -#define MVAR_HOST_NAME "{HOSTNAME}" -#define MVAR_IPADDRESS "{IPADDRESS}" -#define MVAR_TIME "{TIME}" -#define MVAR_ITEM_LASTVALUE "{ITEM.LASTVALUE}" -#define MVAR_ITEM_NAME "{ITEM.NAME}" -#define MVAR_TRIGGER_COMMENT "{TRIGGER.COMMENT}" -#define MVAR_TRIGGER_ID "{TRIGGER.ID}" -#define MVAR_TRIGGER_KEY "{TRIGGER.KEY}" -#define MVAR_TRIGGER_NAME "{TRIGGER.NAME}" -#define MVAR_TRIGGER_SEVERITY "{TRIGGER.SEVERITY}" -#define MVAR_TRIGGER_STATUS "{TRIGGER.STATUS}" -#define MVAR_TRIGGER_STATUS_OLD "{STATUS}" -#define MVAR_TRIGGER_VALUE "{TRIGGER.VALUE}" -#define MVAR_TRIGGER_URL "{TRIGGER.URL}" -#define MVAR_PROFILE_DEVICETYPE "{PROFILE.DEVICETYPE}" -#define MVAR_PROFILE_NAME "{PROFILE.NAME}" -#define MVAR_PROFILE_OS "{PROFILE.OS}" -#define MVAR_PROFILE_SERIALNO "{PROFILE.SERIALNO}" -#define MVAR_PROFILE_TAG "{PROFILE.TAG}" -#define MVAR_PROFILE_MACADDRESS "{PROFILE.MACADDRESS}" -#define MVAR_PROFILE_HARDWARE "{PROFILE.HARDWARE}" -#define MVAR_PROFILE_SOFTWARE "{PROFILE.SOFTWARE}" -#define MVAR_PROFILE_CONTACT "{PROFILE.CONTACT}" -#define MVAR_PROFILE_LOCATION "{PROFILE.LOCATION}" -#define MVAR_PROFILE_NOTES "{PROFILE.NOTES}" - -#define STR_UNKNOWN_VARIABLE "*UNKNOWN*" - -void substitute_simple_macros(DB_EVENT *event, DB_ACTION *action, char **data, int macro_type) -{ - - char - *pl = NULL, - *pr = NULL, - *str_out = NULL, - *replace_to = NULL; - - char tmp[MAX_STRING_LEN]; - - int var_len; - - time_t now; - struct tm *tm; - - DB_RESULT result; - DB_ROW row; - - zabbix_log(LOG_LEVEL_DEBUG, "In substitute_simple_macros()"); - - if(!data || !*data) return; - - zabbix_log(LOG_LEVEL_DEBUG, "In substitute_simple_macros (data:%s)", - *data); - - if('\0' == *data[0]) return; - - if ( macro_type & MACRO_TYPE_TRIGGER_DESCRIPTION ) { - expand_trigger_description_constants(data, event->objectid); - } - - pl = *data; - while((pr = strchr(pl, '{'))) - { - pr[0] = '\0'; -zabbix_log(LOG_LEVEL_DEBUG, "str_out1 [%s] pl [%s]", str_out, pl); - str_out = zbx_strdcat(str_out, pl); -zabbix_log(LOG_LEVEL_DEBUG, "str_out1 [%s] pl [%s]", str_out, pl); - pr[0] = '{'; - - replace_to = zbx_dsprintf(replace_to, "{"); - var_len = 1; - - if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_TRIGGER_NAME, strlen(MVAR_TRIGGER_NAME)) == 0) - { - var_len = strlen(MVAR_TRIGGER_NAME); - - zabbix_log(LOG_LEVEL_DEBUG, "Before replace_to [%s]", replace_to); - - replace_to = zbx_dsprintf(replace_to, "%s", event->trigger_description); - /* Why it was here? *//* For substituting macros in trigger description :) */ - substitute_simple_macros(event, action, &replace_to, MACRO_TYPE_TRIGGER_DESCRIPTION); - - zabbix_log(LOG_LEVEL_DEBUG, "After replace_to [%s]", replace_to); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_TRIGGER_COMMENT, strlen(MVAR_TRIGGER_COMMENT)) == 0) - { - var_len = strlen(MVAR_TRIGGER_COMMENT); - - replace_to = zbx_dsprintf(replace_to, "%s", event->trigger_comments); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_DEVICETYPE, strlen(MVAR_PROFILE_DEVICETYPE)) == 0) - { - var_len = strlen(MVAR_PROFILE_DEVICETYPE); - - result = DBselect("select distinct p.devicetype from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.DEVECETYPE in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_NAME, strlen(MVAR_PROFILE_NAME)) == 0) - { - var_len = strlen(MVAR_PROFILE_NAME); - - result = DBselect("select distinct p.name from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.NAME in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_OS, strlen(MVAR_PROFILE_OS)) == 0) - { - var_len = strlen(MVAR_PROFILE_OS); - - result = DBselect("select distinct p.os from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.OS in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_SERIALNO, strlen(MVAR_PROFILE_SERIALNO)) == 0) - { - var_len = strlen(MVAR_PROFILE_SERIALNO); - - result = DBselect("select distinct p.serialno from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.SERIALNO in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_TAG, strlen(MVAR_PROFILE_TAG)) == 0) - { - var_len = strlen(MVAR_PROFILE_TAG); - - result = DBselect("select distinct p.tag from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.TAG in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_MACADDRESS, strlen(MVAR_PROFILE_MACADDRESS)) == 0) - { - var_len = strlen(MVAR_PROFILE_MACADDRESS); - - result = DBselect("select distinct p.macaddress from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.MACADDRESS in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_HARDWARE, strlen(MVAR_PROFILE_HARDWARE)) == 0) - { - var_len = strlen(MVAR_PROFILE_HARDWARE); - - result = DBselect("select distinct p.hardware from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.HARDWARE in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_SOFTWARE, strlen(MVAR_PROFILE_SOFTWARE)) == 0) - { - var_len = strlen(MVAR_PROFILE_SOFTWARE); - - result = DBselect("select distinct p.software from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.SOFTWARE in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_CONTACT, strlen(MVAR_PROFILE_CONTACT)) == 0) - { - var_len = strlen(MVAR_PROFILE_CONTACT); - - result = DBselect("select distinct p.contact from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.CONTACT in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_LOCATION, strlen(MVAR_PROFILE_LOCATION)) == 0) - { - var_len = strlen(MVAR_PROFILE_LOCATION); - - result = DBselect("select distinct p.location from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.LOCATION in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_PROFILE_NOTES, strlen(MVAR_PROFILE_NOTES)) == 0) - { - var_len = strlen(MVAR_PROFILE_NOTES); - - result = DBselect("select distinct p.notes from triggers t, functions f,items i, hosts h, hosts_profiles p" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and p.hostid=h.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No PROFILE.NOTES in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY | MACRO_TYPE_TRIGGER_DESCRIPTION) && - strncmp(pr, MVAR_HOST_NAME, strlen(MVAR_HOST_NAME)) == 0) - { - var_len = strlen(MVAR_HOST_NAME); - - result = DBselect("select distinct h.host from triggers t, functions f,items i, hosts h " - "where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No hostname in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_ITEM_NAME, strlen(MVAR_ITEM_NAME)) == 0) - { - var_len = strlen(MVAR_ITEM_NAME); - - result = DBselect("select distinct i.description from triggers t, functions f,items i, hosts h" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid" - " order by i.description", - event->objectid); - - row=DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No ITEM.NAME in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY | MACRO_TYPE_TRIGGER_DESCRIPTION) && - strncmp(pr, MVAR_ITEM_LASTVALUE, strlen(MVAR_ITEM_LASTVALUE)) == 0) - { - var_len = strlen(MVAR_ITEM_LASTVALUE); - - result = DBselect("select distinct i.lastvalue,i.units,i.value_type" - " from triggers t, functions f,items i, hosts h" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid", - event->objectid); - - row=DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No ITEM.LASTVALUE in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - strscpy(tmp, row[0]); - - add_value_suffix(tmp, sizeof(tmp), row[1], atoi(row[2])); - - replace_to = zbx_dsprintf(replace_to, "%s", - tmp); - } - - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_TRIGGER_KEY, strlen(MVAR_TRIGGER_KEY)) == 0) - { - var_len = strlen(MVAR_TRIGGER_KEY); - - result = DBselect("select distinct i.key_ from triggers t, functions f,items i, hosts h" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid" - " order by i.key_", - event->objectid); - - row=DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No TRIGGER.KEY in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_IPADDRESS, strlen(MVAR_IPADDRESS)) == 0) - { - var_len = strlen(MVAR_IPADDRESS); - - result = DBselect("select distinct h.ip from triggers t, functions f,items i, hosts h" - " where t.triggerid=" ZBX_FS_UI64 " and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid", - event->objectid); - - row = DBfetch(result); - - if(!row || DBis_null(row[0])==SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No hostname in substitute_simple_macros. Triggerid [" ZBX_FS_UI64 "]", - event->objectid); - - replace_to = zbx_dsprintf(replace_to, "%s", - STR_UNKNOWN_VARIABLE); - } - else - { - replace_to = zbx_dsprintf(replace_to, "%s", - row[0]); - } - DBfree_result(result); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_DATE, strlen(MVAR_DATE)) == 0) - { - var_len = strlen(MVAR_TIME); - - now = time(NULL); - tm = localtime(&now); - replace_to = zbx_dsprintf(replace_to, "%.4d.%.2d.%.2d", - tm->tm_year+1900, - tm->tm_mon+1, - tm->tm_mday); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY)&& - strncmp(pr, MVAR_TIME, strlen(MVAR_TIME)) == 0) - { - var_len = strlen(MVAR_TIME); - - now = time(NULL); - tm = localtime(&now); - replace_to = zbx_dsprintf(replace_to, "%.2d:%.2d:%.2d", - tm->tm_hour, - tm->tm_min, - tm->tm_sec); - - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_TRIGGER_STATUS, strlen(MVAR_TRIGGER_STATUS)) == 0) - { - /* NOTE: if you make changes for this bloc, don't forgot MVAR_TRIGGER_STATUS_OLD block */ - var_len = strlen(MVAR_TRIGGER_STATUS); - - replace_to = zbx_dsprintf(replace_to, "%s", - event->value == TRIGGER_VALUE_TRUE ? "ON" : "OFF"); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_TRIGGER_STATUS_OLD, strlen(MVAR_TRIGGER_STATUS_OLD)) == 0) - { - /* NOTE: if you make changes for this bloc, don't forgot MVAR_TRIGGER_STATUS block */ - var_len = strlen(MVAR_TRIGGER_STATUS_OLD); - - replace_to = zbx_dsprintf(replace_to, "%s", - event->value == TRIGGER_VALUE_TRUE ? "ON" : "OFF"); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_TRIGGER_ID, strlen(MVAR_TRIGGER_ID)) == 0) - { - /* NOTE: if you make changes for this bloc, don't forgot MVAR_TRIGGER_STATUS block */ - var_len = strlen(MVAR_TRIGGER_ID); - - replace_to = zbx_dsprintf(replace_to, ZBX_FS_UI64, - event->objectid); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY | MACRO_TYPE_TRIGGER_EXPRESSION) && - strncmp(pr, MVAR_TRIGGER_VALUE, strlen(MVAR_TRIGGER_VALUE)) == 0) - { - var_len = strlen(MVAR_TRIGGER_VALUE); - - replace_to = zbx_dsprintf(replace_to, "%d", - event->value); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_TRIGGER_URL, strlen(MVAR_TRIGGER_URL)) == 0) - { - /* NOTE: if you make changes for this bloc, don't forgot MVAR_TRIGGER_STATUS block */ - var_len = strlen(MVAR_TRIGGER_URL); - - replace_to = zbx_dsprintf(replace_to, "%s", - event->trigger_url); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_EVENT_ID, strlen(MVAR_EVENT_ID)) == 0) - { - /* NOTE: if you make changes for this bloc, don't forgot MVAR_TRIGGER_STATUS block */ - var_len = strlen(MVAR_EVENT_ID); - - replace_to = zbx_dsprintf(replace_to, ZBX_FS_UI64, - event->eventid); - } - else if(macro_type & (MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY) && - strncmp(pr, MVAR_TRIGGER_SEVERITY, strlen(MVAR_TRIGGER_SEVERITY)) == 0) - { - var_len = strlen(MVAR_TRIGGER_SEVERITY); - - if(event->trigger_priority == 0) replace_to = zbx_dsprintf(replace_to, "Not classified"); - else if(event->trigger_priority == 1) replace_to = zbx_dsprintf(replace_to, "Information"); - else if(event->trigger_priority == 2) replace_to = zbx_dsprintf(replace_to, "Warning"); - else if(event->trigger_priority == 3) replace_to = zbx_dsprintf(replace_to, "Average"); - else if(event->trigger_priority == 4) replace_to = zbx_dsprintf(replace_to, "High"); - else if(event->trigger_priority == 5) replace_to = zbx_dsprintf(replace_to, "Disaster"); - else replace_to = zbx_dsprintf(replace_to, "Unknown"); - } - -zabbix_log(LOG_LEVEL_DEBUG, "str_out2 [%s] replace_to [%s]", str_out, replace_to); - str_out = zbx_strdcat(str_out, replace_to); -zabbix_log(LOG_LEVEL_DEBUG, "str_out2 [%s] replace_to [%s]", str_out, replace_to); - pl = pr + var_len; - - zbx_free(replace_to); - } -zabbix_log(LOG_LEVEL_DEBUG, "str_out3 [%s] pl [%s]", str_out, pl); - str_out = zbx_strdcat(str_out, pl); -zabbix_log(LOG_LEVEL_DEBUG, "str_out3 [%s] pl [%s]", str_out, pl); - - zbx_free(*data); - - *data = str_out; - - zabbix_log(LOG_LEVEL_DEBUG, "End substitute_simple_macros ()"); - zabbix_log(LOG_LEVEL_DEBUG, "End substitute_simple_macros (result:%s)", - *data); -} - -/****************************************************************************** - * * - * Function: substitute_macros * - * * - * Purpose: substitute macros in data string with real values * - * * - * Parameters: trigger - trigger structure * - * action - action structure * - * data - data string * - * * - * Return value: * - * * - * Author: Eugene Grigorjev * - * * - * Comments: example: "{127.0.0.1:system[procload].last(0)}" to "1.34" * - * * - ******************************************************************************/ -void substitute_macros(DB_EVENT *event, DB_ACTION *action, char **data) -{ - char - *str_out = NULL, - *replace_to = NULL, - *pl = NULL, - *pr = NULL, - *pms = NULL, - *pme = NULL, - *p = NULL; - char - host[MAX_STRING_LEN], - key[MAX_STRING_LEN], - function[MAX_STRING_LEN], - parameter[MAX_STRING_LEN]; - - if(!data || !*data) return; - - zabbix_log(LOG_LEVEL_DEBUG, "In substitute_macros(data:%s)", - *data); - - if('\0' == *data[0]) return; - - zabbix_log(LOG_LEVEL_DEBUG, "Before substitute_simple_macros(%s)", *data); - substitute_simple_macros(event, action, data, MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY); - zabbix_log(LOG_LEVEL_DEBUG, "After substitute_simple_macros(%s)", *data); - - pl = *data; - while((pr = strchr(pl, '{'))) - { - if((pme = strchr(pr, '}')) == NULL) - break; - - pme[0] = '\0'; - - pr = strrchr(pr, '{'); /* find '{' near '}' */ - - /* copy left side */ - pr[0] = '\0'; - str_out = zbx_strdcat(str_out, pl); - pr[0] = '{'; - - - /* copy original name of variable */ - replace_to = zbx_dsprintf(replace_to, "%s}", pr); /* in format used '}' */ - /* cose in 'pr' string symbol '}' is changed to '\0' by 'pme'*/ - pl = pr + strlen(replace_to); - - pms = pr + 1; - - if(NULL != (p = strchr(pms, ':'))) - { - *p = '\0'; - zbx_snprintf(host, sizeof(host), "%s", pms); - *p = ':'; - pms = p + 1; - if(NULL != (p = strrchr(pms, '.'))) - { - *p = '\0'; - zbx_snprintf(key, sizeof(key), "%s", pms); - *p = '.'; - pms = p + 1; - if(NULL != (p = strchr(pms, '('))) - { - *p = '\0'; - zbx_snprintf(function, sizeof(function), "%s", pms); - *p = '('; - pms = p + 1; - if(NULL != (p = strchr(pms, ')'))) - { - *p = '\0'; - zbx_snprintf(parameter, sizeof(parameter), "%s", pms); - *p = ')'; - pms = p + 1; - - /* function 'evaluate_function2' require 'replace_to' with size 'MAX_STRING_LEN' */ - zbx_free(replace_to); - replace_to = zbx_malloc(replace_to, MAX_STRING_LEN); - - if(evaluate_function2(replace_to,host,key,function,parameter) != SUCCEED) - zbx_snprintf(replace_to, MAX_STRING_LEN, "%s", STR_UNKNOWN_VARIABLE); - } - } - } - - } - pme[0] = '}'; - - str_out = zbx_strdcat(str_out, replace_to); - zbx_free(replace_to); - } - str_out = zbx_strdcat(str_out, pl); - - zbx_free(*data); - - *data = str_out; - - zabbix_log( LOG_LEVEL_DEBUG, "End substitute_macros(result:%s)", - *data ); -} - -/****************************************************************************** - * * - * Function: substitute_functions * - * * - * Purpose: substitute expression functions with theirs values * - * * - * Parameters: exp - expression string * - * error - place error message here if any * - * maxerrlen - max length of error msg * - * * - * Return value: SUCCEED - evaluated succesfully, exp - updated expression * - * FAIL - otherwise * - * * - * Author: Alexei Vladishev * - * * - * Comments: example: "({15}>10)|({123}=0)" => "(6.456>10)|(0=0) * - * * - ******************************************************************************/ -int substitute_functions(char **exp, char *error, int maxerrlen) -{ - char *value; - char functionid[MAX_STRING_LEN]; - int i,j; - int len; - char *out = NULL; - char c; - - zabbix_log(LOG_LEVEL_DEBUG, "In substitute_functions(%s)", - *exp); - - i = 0; - len = strlen(*exp); - while(i<len) - { - if((*exp)[i] == '{') - { - for(j=i+1;((*exp)[j]!='}')&&((*exp)[j]!='\0');j++) - { - functionid[j-i-1]=(*exp)[j]; - } - functionid[j-i-1]='\0'; - if( DBget_function_result( &value, functionid ) != SUCCEED ) - { -/* It may happen because of functions.lastvalue is NULL, so this is not warning */ - zbx_snprintf(error,maxerrlen, "Unable to get value for functionid [%s]", - functionid); - zabbix_log( LOG_LEVEL_DEBUG, "%s", - error); - zabbix_syslog("%s", - error); - return FAIL; - } - out = zbx_strdcat(out,value); - zbx_free(value); - i=j+1; - } - else - { - c=(*exp)[i+1]; (*exp)[i+1]='\0'; - out = zbx_strdcat(out, (*exp+i)); - (*exp)[i+1]=c; - i++; - } - } - zbx_free(*exp); - - *exp = out; - zabbix_log( LOG_LEVEL_DEBUG, "End substitute_functions [%s]", - *exp); - - return SUCCEED; -} - -/****************************************************************************** - * * - * Function: evaluate_expression * - * * - * Purpose: evaluate expression * - * * - * Parameters: exp - expression string * - * error - place rrror message if any * - * maxerrlen - max length of error message * - * * - * Return value: SUCCEED - evaluated succesfully, result - value of the exp * - * FAIL - otherwise * - * error - error message * - * * - * Author: Alexei Vladishev * - * * - * Comments: example: ({a0:system[procload].last(0)}>1)| * - * ({a0:system[procload].max(300)}>3) * - * * - ******************************************************************************/ -int evaluate_expression(int *result,char **expression, int trigger_value, char *error, int maxerrlen) -{ - /* Required for substitution of macros */ - DB_EVENT event; - DB_ACTION action; - - zabbix_log(LOG_LEVEL_DEBUG, "In evaluate_expression(%s)", - *expression); - - /* Substitute macros first */ - memset(&event,0,sizeof(DB_EVENT)); - memset(&action,0,sizeof(DB_ACTION)); - event.value = trigger_value; - - substitute_simple_macros(&event, &action, expression, MACRO_TYPE_TRIGGER_EXPRESSION); - - /* Evaluate expression */ - delete_spaces(*expression); - if( substitute_functions(expression, error, maxerrlen) == SUCCEED) - { - if( evaluate(result, *expression, error, maxerrlen) == SUCCEED) - { - zabbix_log(LOG_LEVEL_DEBUG, "End evaluate_expression(result:%d)", - *result); - return SUCCEED; - } - } - zabbix_log(LOG_LEVEL_DEBUG, "Evaluation of expression [%s] failed [%s]", - *expression, - error); - zabbix_syslog("Evaluation of expression [%s] failed [%s]", - *expression, - error); - - zabbix_log(LOG_LEVEL_DEBUG, "End evaluate_expression(result:FAIL)"); - return FAIL; -} diff --git a/src/zabbix_server/functions.h b/src/zabbix_server/functions.h deleted file mode 100644 index f88ce066..00000000 --- a/src/zabbix_server/functions.h +++ /dev/null @@ -1,33 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 SIA Zabbix -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -**/ - -#ifndef ZABBIX_FUNCTIONS_H -#define ZABBIX_FUNCTIONS_H - -#include "common.h" -#include "comms.h" -#include "db.h" -#include "sysinfo.h" - -void update_triggers (zbx_uint64_t itemid); -void update_functions(DB_ITEM *item); -void process_new_value(DB_ITEM *item, AGENT_RESULT *value); -void proxy_process_new_value(DB_ITEM *item, AGENT_RESULT *value); - -#endif diff --git a/src/zabbix_server/httppoller/httppoller.c b/src/zabbix_server/httppoller/httppoller.c index e996c0a0..370bd553 100644 --- a/src/zabbix_server/httppoller/httppoller.c +++ b/src/zabbix_server/httppoller/httppoller.c @@ -23,8 +23,6 @@ #include "db.h" #include "log.h" -#include "../functions.h" -#include "../expression.h" #include "httptest.h" #include "httppoller.h" diff --git a/src/zabbix_server/httppoller/httptest.c b/src/zabbix_server/httppoller/httptest.c index ea6806e5..d4fe457e 100644 --- a/src/zabbix_server/httppoller/httptest.c +++ b/src/zabbix_server/httppoller/httptest.c @@ -24,8 +24,8 @@ #include "db.h" #include "log.h" #include "zlog.h" +#include "zbxserver.h" -#include "../functions.h" #include "httpmacro.h" #include "httptest.h" diff --git a/src/zabbix_server/pinger/pinger.c b/src/zabbix_server/pinger/pinger.c index 4a7f9472..1d7c8d83 100644 --- a/src/zabbix_server/pinger/pinger.c +++ b/src/zabbix_server/pinger/pinger.c @@ -21,11 +21,11 @@ #include "cfg.h" #include "db.h" -#include "../functions.h" #include "log.h" #include "zlog.h" #include "sysinfo.h" #include "threads.h" +#include "zbxserver.h" #include "pinger.h" diff --git a/src/zabbix_server/poller/poller.c b/src/zabbix_server/poller/poller.c index d39e2837..eced40a3 100644 --- a/src/zabbix_server/poller/poller.c +++ b/src/zabbix_server/poller/poller.c @@ -23,9 +23,8 @@ #include "db.h" #include "sysinfo.h" #include "daemon.h" +#include "zbxserver.h" -#include "../functions.h" -#include "../expression.h" #include "poller.h" #include "checks_agent.h" diff --git a/src/zabbix_server/server.c b/src/zabbix_server/server.c index d01f8543..563d8f59 100644 --- a/src/zabbix_server/server.c +++ b/src/zabbix_server/server.c @@ -30,9 +30,8 @@ #include "zbxgetopt.h" #include "mutexs.h" -#include "functions.h" -#include "expression.h" #include "sysinfo.h" +#include "zbxserver.h" #include "daemon.h" diff --git a/src/zabbix_server/timer/timer.c b/src/zabbix_server/timer/timer.c index b4772139..cd695178 100644 --- a/src/zabbix_server/timer/timer.c +++ b/src/zabbix_server/timer/timer.c @@ -24,9 +24,9 @@ #include "db.h" #include "log.h" #include "zlog.h" +#include "zbxserver.h" #include "timer.h" -#include "../functions.h" /****************************************************************************** * * diff --git a/src/zabbix_server/trapper/trapper.c b/src/zabbix_server/trapper/trapper.c index d094b4ec..47c35557 100644 --- a/src/zabbix_server/trapper/trapper.c +++ b/src/zabbix_server/trapper/trapper.c @@ -26,9 +26,7 @@ #include "log.h" #include "zlog.h" #include "zbxjson.h" - -#include "../functions.h" -#include "../expression.h" +#include "zbxserver.h" #include "../nodewatcher/nodecomms.h" #include "../nodewatcher/nodesender.h" diff --git a/src/zabbix_server/zlog.c b/src/zabbix_server/zlog.c index 52a9b170..4284c0cc 100644 --- a/src/zabbix_server/zlog.c +++ b/src/zabbix_server/zlog.c @@ -30,8 +30,9 @@ #include <time.h> #include "common.h" -#include "functions.h" #include "log.h" +#include "zbxserver.h" + #include "zlog.h" /****************************************************************************** |
