diff options
| author | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2005-06-09 20:04:00 +0000 |
|---|---|---|
| committer | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2005-06-09 20:04:00 +0000 |
| commit | ec4e0a7ac29a651c98490487b27069bddaed4e92 (patch) | |
| tree | b4f1f3ab92822abd722a8a08b82fc165395ac921 /src | |
| parent | 515bb79a40246c8364cd8d7bef88337d6d6b1d79 (diff) | |
Minor changes.
git-svn-id: svn://svn.zabbix.com/trunk@1851 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
| -rw-r--r-- | src/libs/Makefile.am | 2 | ||||
| -rw-r--r-- | src/libs/zbxsysinfo/Makefile.am | 4 | ||||
| -rw-r--r-- | src/libs/zbxsysinfo/sysinfo.c (renamed from src/zabbix_agent/sysinfo.c) | 0 | ||||
| -rw-r--r-- | src/zabbix_agent/Makefile.am | 6 | ||||
| -rw-r--r-- | src/zabbix_agent/sysinfo.h | 134 | ||||
| -rw-r--r-- | src/zabbix_server/Makefile.am | 2 | ||||
| -rw-r--r-- | src/zabbix_server/Makefile.in | 10 | ||||
| -rw-r--r-- | src/zabbix_server/checks_simple.h | 3 | ||||
| -rw-r--r-- | src/zabbix_server/functions.c | 1198 | ||||
| -rw-r--r-- | src/zabbix_server/functions.h | 36 |
10 files changed, 1250 insertions, 145 deletions
diff --git a/src/libs/Makefile.am b/src/libs/Makefile.am index a41fbb80..fd02b3e8 100644 --- a/src/libs/Makefile.am +++ b/src/libs/Makefile.am @@ -1 +1 @@ -SUBDIRS = zbxlog zbxcrypto zbxpid zbxnet zbxconf zbxdbhigh zbxemail +SUBDIRS = zbxlog zbxcrypto zbxpid zbxnet zbxconf zbxdbhigh zbxemail zbxsysinfo diff --git a/src/libs/zbxsysinfo/Makefile.am b/src/libs/zbxsysinfo/Makefile.am new file mode 100644 index 00000000..ad4c174d --- /dev/null +++ b/src/libs/zbxsysinfo/Makefile.am @@ -0,0 +1,4 @@ +SUBDIRS=. +lib_LIBRARIES=libzbxsysinfo.a +libzbxsysinfo_a_SOURCES=sysinfo.c +libzbxsysinfo_a_LIBADD = ../zbxcrypto/libzbxcrypto.a diff --git a/src/zabbix_agent/sysinfo.c b/src/libs/zbxsysinfo/sysinfo.c index a472138e..a472138e 100644 --- a/src/zabbix_agent/sysinfo.c +++ b/src/libs/zbxsysinfo/sysinfo.c diff --git a/src/zabbix_agent/Makefile.am b/src/zabbix_agent/Makefile.am index ccec7010..4ea9620d 100644 --- a/src/zabbix_agent/Makefile.am +++ b/src/zabbix_agent/Makefile.am @@ -1,5 +1,5 @@ SUBDIRS = . bin_PROGRAMS = zabbix_agentd -zabbix_agentd_SOURCES = active.c cpustat.c diskdevices.c interfaces.c logfiles.c stats.c sysinfo.c zabbix_agentd.c -zabbix_agentd_LDADD = ../libs/zbxlog/libzbxlog.a ../libs/zbxcrypto/libzbxcrypto.a ../libs/zbxpid/libzbxpid.a \ - ../libs/zbxnet/libzbxnet.a ../libs/zbxconf/libzbxconf.a +zabbix_agentd_SOURCES = active.c cpustat.c diskdevices.c interfaces.c logfiles.c stats.c zabbix_agentd.c +zabbix_agentd_LDADD = ../libs/zbxsysinfo/libzbxsysinfo.a ../libs/zbxlog/libzbxlog.a ../libs/zbxcrypto/libzbxcrypto.a \ +../libs/zbxpid/libzbxpid.a ../libs/zbxnet/libzbxnet.a ../libs/zbxconf/libzbxconf.a diff --git a/src/zabbix_agent/sysinfo.h b/src/zabbix_agent/sysinfo.h deleted file mode 100644 index 4e5cc40e..00000000 --- a/src/zabbix_agent/sysinfo.h +++ /dev/null @@ -1,134 +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_SYSINFO_H -#define ZABBIX_SYSINFO_H - -/* #define TEST_PARAMETERS */ - -#define SYSINFO_RET_OK 0 -#define SYSINFO_RET_FAIL 1 -#define SYSINFO_RET_TIMEOUT 2 - -#define COMMAND struct command_type -COMMAND -{ - char *key; - int (*function)(); - int (*function_str)(); - char *parameter; -}; - - - -void process(char *command, char *value); -void init_metrics(); - -void add_user_parameter(char *key,char *command); -void test_parameters(void); -int getPROC(char *file,int lineno,int fieldno, double *value); - -int BUFFERSMEM(const char *cmd, const char *parameter,double *value); -int CACHEDMEM(const char *cmd, const char *parameter,double *value); -int CKSUM(const char *cmd, const char *filename,double *value); - -int CPUIDLE1(const char *cmd, const char *parameter,double *value); -int CPUIDLE5(const char *cmd, const char *parameter,double *value); -int CPUIDLE15(const char *cmd, const char *parameter,double *value); -int CPUUSER1(const char *cmd, const char *parameter,double *value); -int CPUUSER5(const char *cmd, const char *parameter,double *value); -int CPUUSER15(const char *cmd, const char *parameter,double *value); -int CPUNICE1(const char *cmd, const char *parameter,double *value); -int CPUNICE5(const char *cmd, const char *parameter,double *value); -int CPUNICE15(const char *cmd, const char *parameter,double *value); -int CPUSYSTEM1(const char *cmd, const char *parameter,double *value); -int CPUSYSTEM5(const char *cmd, const char *parameter,double *value); -int CPUSYSTEM15(const char *cmd, const char *parameter,double *value); - -int FILESIZE(const char *cmd, const char *filename,double *value); -int DISKTOTAL(const char *cmd, const char *mountPoint,double *value); -int DISKFREE(const char *cmd, const char *mountPoint,double *value); -int DISKUSED(const char *cmd, const char *mountPoint,double *value); -int DISKFREE_PERC(const char *cmd, const char *mountPoint,double *value); -int DISKUSED_PERC(const char *cmd, const char *mountPoint,double *value); - -int DISK_IO(const char *cmd, const char *parameter,double *value); -int DISK_RIO(const char *cmd, const char *parameter,double *value); -int DISK_WIO(const char *cmd, const char *parameter,double *value); -int DISK_RBLK(const char *cmd, const char *parameter,double *value); -int DISK_WBLK(const char *cmd, const char *parameter,double *value); -int FREEMEM(const char *cmd, const char *parameter,double *value); - -int INODEFREE(const char *cmd, const char *mountPoint,double *value); -int INODEFREE_PERC(const char *cmd, const char *mountPoint,double *value); -int INODETOTAL(const char *cmd, const char *mountPoint,double *value); - -int ISFILE(const char *cmd, const char *filename,double *value); - -int KERNEL_MAXFILES(const char *cmd, const char *parameter,double *value); -int KERNEL_MAXPROC(const char *cmd, const char *parameter,double *value); -int NETLOADIN1(const char *cmd, const char *parameter,double *value); -int NETLOADIN5(const char *cmd, const char *parameter,double *value); -int NETLOADIN15(const char *cmd, const char *parameter,double *value); -int NETLOADOUT1(const char *cmd, const char *parameter,double *value); -int NETLOADOUT5(const char *cmd, const char *parameter,double *value); -int NETLOADOUT15(const char *cmd, const char *parameter,double *value); -int DISKREADOPS1(const char *cmd, const char *parameter,double *value); -int DISKREADOPS5(const char *cmd, const char *parameter,double *value); -int DISKREADOPS15(const char *cmd, const char *parameter,double *value); -int DISKREADBLKS1(const char *cmd, const char *parameter,double *value); -int DISKREADBLKS5(const char *cmd, const char *parameter,double *value); -int DISKREADBLKS15(const char *cmd, const char *parameter,double *value); -int DISKWRITEOPS1(const char *cmd, const char *parameter,double *value); -int DISKWRITEOPS5(const char *cmd, const char *parameter,double *value); -int DISKWRITEOPS15(const char *cmd, const char *parameter,double *value); -int DISKWRITEBLKS1(const char *cmd, const char *parameter,double *value); -int DISKWRITEBLKS5(const char *cmd, const char *parameter,double *value); -int DISKWRITEBLKS15(const char *cmd, const char *parameter,double *value); -int PING(const char *cmd, const char *parameter,double *value); -int SHAREDMEM(const char *cmd, const char *parameter,double *value); -int TOTALMEM(const char *cmd, const char *parameter,double *value); -int PROCCNT(const char *cmd, const char *parameter,double *value); -int PROCCOUNT(const char *cmd, const char *parameter,double *value); -int PROCLOAD(const char *cmd, const char *parameter,double *value); -int PROCLOAD5(const char *cmd, const char *parameter,double *value); -int PROCLOAD15(const char *cmd, const char *parameter,double *value); - -int SENSOR_TEMP1(const char *cmd, const char *parameter,double *value); -int SENSOR_TEMP2(const char *cmd, const char *parameter,double *value); -int SENSOR_TEMP3(const char *cmd, const char *parameter,double *value); - -int SWAPFREE(const char *cmd, const char *parameter,double *value); -int SWAPTOTAL(const char *cmd, const char *parameter,double *value); -int UPTIME(const char *cmd, const char *parameter,double *value); - -int TCP_LISTEN(const char *cmd, const char *porthex,double *value); - -int EXECUTE(const char *cmd, const char *command,double *value); -int EXECUTE_STR(const char *cmd, const char *command, const char *parameter, char **value); -int STRVERSION(const char *cmd, const char *command,char **value); - -int MD5SUM(const char *cmd, const char *filename, char **value); - -int CHECK_SERVICE(const char *cmd, const char *service,double *value); -int CHECK_SERVICE_PERF(const char *cmd, const char *service,double *value); -int CHECK_PORT(const char *cmd, const char *ip_and_port,double *value); - -#endif diff --git a/src/zabbix_server/Makefile.am b/src/zabbix_server/Makefile.am index 76b3899d..82af684a 100644 --- a/src/zabbix_server/Makefile.am +++ b/src/zabbix_server/Makefile.am @@ -2,6 +2,6 @@ SUBDIRS = . INCLUDES=-I@top_srcdir@/include @MYSQL_INCLUDE@ bin_PROGRAMS = zabbix_server -zabbix_server_SOURCES = alerter.c pinger.c housekeeper.c checks_simple.c checks_snmp.c checks_agent.c checks_internal.c trapper.c escalator.c expression.c actions.c zlog.c server.c +zabbix_server_SOURCES = alerter.c pinger.c housekeeper.c checks_simple.c checks_snmp.c checks_agent.c checks_internal.c trapper.c escalator.c expression.c actions.c zlog.c functions.c server.c zabbix_server_LDADD = ../libs/zbxlog/libzbxlog.a ../libs/zbxpid/libzbxpid.a ../libs/zbxconf/libzbxconf.a ../libs/zbxdbhigh/libzbxdbhigh.a ../libs/zbxemail/libzbxemail.a zabbix_server_LDFLAGS=@MYSQL_LFLAGS@ @PGSQL_LFLAGS@ diff --git a/src/zabbix_server/Makefile.in b/src/zabbix_server/Makefile.in index f7dea599..fa95aa0a 100644 --- a/src/zabbix_server/Makefile.in +++ b/src/zabbix_server/Makefile.in @@ -80,7 +80,7 @@ SUBDIRS = . INCLUDES = -I@top_srcdir@/include @MYSQL_INCLUDE@ bin_PROGRAMS = zabbix_server -zabbix_server_SOURCES = alerter.c pinger.c housekeeper.c checks_simple.c checks_snmp.c checks_agent.c checks_internal.c trapper.c escalator.c expression.c actions.c zlog.c server.c +zabbix_server_SOURCES = alerter.c pinger.c housekeeper.c checks_simple.c checks_snmp.c checks_agent.c checks_internal.c trapper.c escalator.c expression.c actions.c zlog.c functions.c server.c zabbix_server_LDADD = ../libs/zbxlog/libzbxlog.a ../libs/zbxpid/libzbxpid.a ../libs/zbxconf/libzbxconf.a ../libs/zbxdbhigh/libzbxdbhigh.a ../libs/zbxemail/libzbxemail.a zabbix_server_LDFLAGS = @MYSQL_LFLAGS@ @PGSQL_LFLAGS@ mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs @@ -95,7 +95,8 @@ LDFLAGS = @LDFLAGS@ LIBS = @LIBS@ zabbix_server_OBJECTS = alerter.o pinger.o housekeeper.o \ checks_simple.o checks_snmp.o checks_agent.o checks_internal.o \ -trapper.o escalator.o expression.o actions.o zlog.o server.o +trapper.o escalator.o expression.o actions.o zlog.o functions.o \ +server.o zabbix_server_DEPENDENCIES = ../libs/zbxlog/libzbxlog.a \ ../libs/zbxpid/libzbxpid.a ../libs/zbxconf/libzbxconf.a \ ../libs/zbxdbhigh/libzbxdbhigh.a ../libs/zbxemail/libzbxemail.a @@ -112,8 +113,9 @@ TAR = tar GZIP_ENV = --best DEP_FILES = .deps/actions.P .deps/alerter.P .deps/checks_agent.P \ .deps/checks_internal.P .deps/checks_simple.P .deps/checks_snmp.P \ -.deps/escalator.P .deps/expression.P .deps/housekeeper.P .deps/pinger.P \ -.deps/server.P .deps/trapper.P .deps/zlog.P +.deps/escalator.P .deps/expression.P .deps/functions.P \ +.deps/housekeeper.P .deps/pinger.P .deps/server.P .deps/trapper.P \ +.deps/zlog.P SOURCES = $(zabbix_server_SOURCES) OBJECTS = $(zabbix_server_OBJECTS) diff --git a/src/zabbix_server/checks_simple.h b/src/zabbix_server/checks_simple.h index 1099f4b0..bb63ae3a 100644 --- a/src/zabbix_server/checks_simple.h +++ b/src/zabbix_server/checks_simple.h @@ -28,8 +28,7 @@ #include "config.h" #include "db.h" #include "log.h" - -#include "../zabbix_agent/sysinfo.h" +#include "sysinfo.h" extern int get_value_simple(double *result,char *result_str,DB_ITEM *item,char *error, int max_error_len); diff --git a/src/zabbix_server/functions.c b/src/zabbix_server/functions.c new file mode 100644 index 00000000..fa6862a9 --- /dev/null +++ b/src/zabbix_server/functions.c @@ -0,0 +1,1198 @@ +/* +** 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 <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 "security.h" + +#include "functions.h" + +/* Delete trailing zeroes */ +/* 10.0100 -> 10.01, 10. -> 10 */ +void del_zeroes(char *s) +{ + int i; + + if(strchr(s,'.')!=NULL) + { + for(i=strlen(s)-1;;i--) + { + if(s[i]=='0') + { + s[i]=0; + } + else if(s[i]=='.') + { + s[i]=0; + break; + } + else + { + break; + } + } + } +} + +/* Calculate nextcheck for items. + Old algorithm: now+delay + New one: preserve period over time, If delay==5, nextcheck = 0,5,10,15,20... +*/ +int calculate_item_nextcheck(int nextcheck, int delay, int now) +{ + int i; + + i=delay*(int)(now/delay); + + while(i<=now) i+=delay; + + return i; +} + +/* + * Evaluate function COUNT + */ +int evaluate_COUNT(char *value,DB_ITEM *item,int parameter) +{ + DB_RESULT *result; + + char sql[MAX_STRING_LEN]; + int now; + int res = SUCCEED; + + if(item->value_type != ITEM_VALUE_TYPE_FLOAT) + { + return FAIL; + } + + now=time(NULL); + + snprintf(sql,sizeof(sql)-1,"select count(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); + + result = DBselect(sql); + if(DBnum_rows(result) == 0) + { + zabbix_log(LOG_LEVEL_DEBUG, "Result for COUNT is empty" ); + res = FAIL; + } + else + { + strcpy(value,DBget_field(result,0,0)); + } + DBfree_result(result); + + return res; +} + +/* + * Evaluate function SUM + */ +int evaluate_SUM(char *value,DB_ITEM *item,int parameter) +{ + DB_RESULT *result; + + char sql[MAX_STRING_LEN]; + int now; + int res = SUCCEED; + + if(item->value_type != ITEM_VALUE_TYPE_FLOAT) + { + return FAIL; + } + + now=time(NULL); + + snprintf(sql,sizeof(sql)-1,"select sum(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); + + result = DBselect(sql); + if(DBnum_rows(result) == 0) + { + zabbix_log(LOG_LEVEL_DEBUG, "Result for SUM is empty" ); + res = FAIL; + } + else + { + strcpy(value,DBget_field(result,0,0)); + } + DBfree_result(result); + + return res; +} + +/* + * Evaluate function AVG + */ +int evaluate_AVG(char *value,DB_ITEM *item,int parameter) +{ + DB_RESULT *result; + + char sql[MAX_STRING_LEN]; + int now; + int res = SUCCEED; + + if(item->value_type != ITEM_VALUE_TYPE_FLOAT) + { + return FAIL; + } + + now=time(NULL); + + snprintf(sql,sizeof(sql)-1,"select avg(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); + + result = DBselect(sql); + if(DBnum_rows(result) == 0) + { + zabbix_log(LOG_LEVEL_DEBUG, "Result for AVG is empty" ); + res = FAIL; + } + else + { + strcpy(value,DBget_field(result,0,0)); + del_zeroes(value); + } + DBfree_result(result); + + return res; +} + +/* + * Evaluate function MIN + */ +int evaluate_MIN(char *value,DB_ITEM *item,int parameter) +{ + DB_RESULT *result; + + char sql[MAX_STRING_LEN]; + int now; + int res = SUCCEED; + + if(item->value_type != ITEM_VALUE_TYPE_FLOAT) + { + return FAIL; + } + + now=time(NULL); + + snprintf(sql,sizeof(sql)-1,"select min(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); + + result = DBselect(sql); + if(DBnum_rows(result) == 0) + { + zabbix_log(LOG_LEVEL_DEBUG, "Result for MIN is empty" ); + res = FAIL; + } + else + { + strcpy(value,DBget_field(result,0,0)); + del_zeroes(value); + } + DBfree_result(result); + + return res; +} + +/* + * Evaluate function MAX + */ +int evaluate_MAX(char *value,DB_ITEM *item,int parameter) +{ + DB_RESULT *result; + + char sql[MAX_STRING_LEN]; + int now; + int res = SUCCEED; + + if(item->value_type != ITEM_VALUE_TYPE_FLOAT) + { + return FAIL; + } + + now=time(NULL); + + snprintf(sql,sizeof(sql)-1,"select max(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); + + result = DBselect(sql); + if(DBnum_rows(result) == 0) + { + zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" ); + res = FAIL; + } + else + { + strcpy(value,DBget_field(result,0,0)); + del_zeroes(value); + } + DBfree_result(result); + + return res; +} + +/* + * Evaluate function DELTA + */ +int evaluate_DELTA(char *value,DB_ITEM *item,int parameter) +{ + DB_RESULT *result; + + char sql[MAX_STRING_LEN]; + int now; + int res = SUCCEED; + + if(item->value_type != ITEM_VALUE_TYPE_FLOAT) + { + return FAIL; + } + + now=time(NULL); + + snprintf(sql,sizeof(sql)-1,"select max(value)-min(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); + + result = DBselect(sql); + if(DBnum_rows(result) == 0) + { + zabbix_log(LOG_LEVEL_DEBUG, "Result for DELTA is empty" ); + res = FAIL; + } + else + { + strcpy(value,DBget_field(result,0,0)); + del_zeroes(value); + } + DBfree_result(result); + + return res; +} + +/* + * Evaluate function (avg,min,max,prev,last,diff,str,change,abschange,delta,time,date) + */ +int evaluate_FUNCTION(char *value,DB_ITEM *item,char *function,char *parameter, int flag) +{ + int ret = SUCCEED; + time_t now; + struct tm *tm; + + float value_float; + float value_float_abs; + char suffix[MAX_STRING_LEN]; + + int day; + + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() Function [%s] flag [%d]",function,flag); + + if(strcmp(function,"last")==0) + { + if(item->lastvalue_null==1) + { + ret = FAIL; + } + else + { + if(item->value_type==ITEM_VALUE_TYPE_FLOAT) + { + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() 1"); + snprintf(value,MAX_STRING_LEN-1,"%f",item->lastvalue); + del_zeroes(value); + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() 2 value [%s]", value); + } + else + { +/* *value=strdup(item->lastvalue_str);*/ + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() 3 [%s] [%s]",value,item->lastvalue_str); + strcpy(value,item->lastvalue_str); + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() 4"); + } + } + } + else if(strcmp(function,"prev")==0) + { + if(item->prevvalue_null==1) + { + ret = FAIL; + } + else + { + if(item->value_type==ITEM_VALUE_TYPE_FLOAT) + { + snprintf(value,MAX_STRING_LEN-1,"%f",item->prevvalue); + del_zeroes(value); + } + else + { + strcpy(value,item->prevvalue_str); + } + } + } + else if(strcmp(function,"min")==0) + { + ret = evaluate_MIN(value,item,atoi(parameter)); + } + else if(strcmp(function,"max")==0) + { + ret = evaluate_MAX(value,item,atoi(parameter)); + } + else if(strcmp(function,"avg")==0) + { + ret = evaluate_AVG(value,item,atoi(parameter)); + } + else if(strcmp(function,"sum")==0) + { + ret = evaluate_SUM(value,item,atoi(parameter)); + } + else if(strcmp(function,"count")==0) + { + ret = evaluate_COUNT(value,item,atoi(parameter)); + } + else if(strcmp(function,"delta")==0) + { + ret = evaluate_DELTA(value,item,atoi(parameter)); + } + else if(strcmp(function,"nodata")==0) + { + strcpy(value,"0"); + } + else if(strcmp(function,"date")==0) + { + now=time(NULL); + tm=localtime(&now); + snprintf(value,MAX_STRING_LEN-1,"%.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; + snprintf(value,MAX_STRING_LEN-1,"%d", day); + } + else if(strcmp(function,"time")==0) + { + now=time(NULL); + tm=localtime(&now); + snprintf(value,MAX_STRING_LEN-1,"%.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 + { + if(item->value_type==ITEM_VALUE_TYPE_FLOAT) + { + snprintf(value,MAX_STRING_LEN-1,"%f",(float)abs(item->lastvalue-item->prevvalue)); + del_zeroes(value); + } + else + { + if(strcmp(item->lastvalue_str, item->prevvalue_str) == 0) + { + strcpy(value,"0"); + } + else + { + strcpy(value,"1"); + } + } + } + } + else if(strcmp(function,"change")==0) + { + if((item->lastvalue_null==1)||(item->prevvalue_null==1)) + { + ret = FAIL; + } + else + { + if(item->value_type==ITEM_VALUE_TYPE_FLOAT) + { + snprintf(value,MAX_STRING_LEN-1,"%f",item->lastvalue-item->prevvalue); + del_zeroes(value); + } + else + { + if(strcmp(item->lastvalue_str, item->prevvalue_str) == 0) + { + strcpy(value,"0"); + } + else + { + strcpy(value,"1"); + } + } + } + } + else if(strcmp(function,"diff")==0) + { + if((item->lastvalue_null==1)||(item->prevvalue_null==1)) + { + ret = FAIL; + } + else + { + if(item->value_type==ITEM_VALUE_TYPE_FLOAT) + { + 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) + { + if(strstr(item->lastvalue_str, parameter) == NULL) + { + strcpy(value,"0"); + } + else + { + strcpy(value,"1"); + } + + } + else + { + ret = FAIL; + } + } + else if(strcmp(function,"now")==0) + { + now=time(NULL); + snprintf(value,MAX_STRING_LEN-1,"%d",(int)now); + } + else + { + zabbix_log( LOG_LEVEL_WARNING, "Unsupported function:%s",function); + zabbix_syslog("Unsupported function:%s",function); + ret = FAIL; + } + + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() pre-7"); + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() 7 Formula [%s]", item->formula); + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() 7 Value [%s]", value); + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() 7 Units [%s]", item->units); + zabbix_log( LOG_LEVEL_DEBUG, "In evaluate_FUNCTION() 7 Value [%s] Units [%s] Formula [%s]", value, item->units, item->formula); + + /* Add suffix: 1000000 -> 1 MB */ + if( (EVALUATE_FUNCTION_SUFFIX == flag) && (ITEM_VALUE_TYPE_FLOAT == item->value_type) && + (SUCCEED == ret) && strlen(item->units)>0) + { + value_float=atof(value); + /* Custom multiplier? */ +/* + if(item->multiplier == 1) + { + value_float=value_float*atof(item->formula); + }*/ + + value_float_abs=abs(value_float); + + if(value_float_abs<1024) + { + strscpy(suffix,""); + } + else if(value_float_abs<1024*1024) + { + strscpy(suffix,"K"); + value_float=value_float/1024; + } + else if(value_float_abs<1024*1024*1024) + { + strscpy(suffix,"M"); + value_float=value_float/(1024*1024); + } + else + { + strscpy(suffix,"G"); + value_float=value_float/(1024*1024*1024); + } + zabbix_log( LOG_LEVEL_DEBUG, "Value [%s] [%f] Suffix [%s] Units [%s]",value,value_float,suffix,item->units); +// if(cmp_double((double)round(value_float), value_float) == 0) + if(cmp_double((int)(value_float+0.5), value_float) == 0) + { + snprintf(value, MAX_STRING_LEN-1, "%.0f %s%s", value_float, suffix, item->units); + } + else + { + snprintf(value, MAX_STRING_LEN-1, "%.2f %s%s", value_float, suffix, item->units); + } + } + + zabbix_log( LOG_LEVEL_DEBUG, "End of evaluate_FUNCTION. Result [%s]",value); + return ret; +} + +/* + * Re-calculate values of functions related to given ITEM + */ +void update_functions(DB_ITEM *item) +{ + DB_FUNCTION function; + DB_RESULT *result; + char sql[MAX_STRING_LEN]; + char value[MAX_STRING_LEN]; + char value_esc[MAX_STRING_LEN]; + int ret=SUCCEED; + int i; + + zabbix_log( LOG_LEVEL_DEBUG, "In update_functions(%d)",item->itemid); + + snprintf(sql,sizeof(sql)-1,"select function,parameter,itemid from functions where itemid=%d group by 1,2,3 order by 1,2,3",item->itemid); + + result = DBselect(sql); + + for(i=0;i<DBnum_rows(result);i++) + { + function.function=DBget_field(result,i,0); + function.parameter=DBget_field(result,i,1); + function.itemid=atoi(DBget_field(result,i,2)); + + zabbix_log( LOG_LEVEL_DEBUG, "ItemId:%d Evaluating %s(%d)\n",function.itemid,function.function,function.parameter); + + ret = evaluate_FUNCTION(value,item,function.function,function.parameter, EVALUATE_FUNCTION_NORMAL); + if( FAIL == ret) + { + zabbix_log( LOG_LEVEL_DEBUG, "Evaluation failed for function:%s\n",function.function); + continue; + } + zabbix_log( LOG_LEVEL_DEBUG, "Result of evaluate_FUNCTION [%s]\n",value); + if (ret == SUCCEED) + { + DBescape_string(value,value_esc,MAX_STRING_LEN); + snprintf(sql,sizeof(sql)-1,"update functions set lastvalue='%s' where itemid=%d and function='%s' and parameter='%s'", value_esc, function.itemid, function.function, function.parameter ); + DBexecute(sql); + } + } + + DBfree_result(result); +} + +/* + * Recursive function! + */ +void update_serv(int serviceid) +{ + char sql[MAX_STRING_LEN]; + int i; + int status; + int serviceupid, algorithm; + int now; + + DB_RESULT *result,*result2; + + snprintf(sql,sizeof(sql)-1,"select l.serviceupid,s.algorithm from services_links l,services s where s.serviceid=l.serviceupid and l.servicedownid=%d",serviceid); + result=DBselect(sql); + status=0; + for(i=0;i<DBnum_rows(result);i++) + { + serviceupid=atoi(DBget_field(result,i,0)); + algorithm=atoi(DBget_field(result,i,1)); + if(SERVICE_ALGORITHM_NONE == algorithm) + { +/* Do nothing */ + } + else if((SERVICE_ALGORITHM_MAX == algorithm) + || + (SERVICE_ALGORITHM_MIN == algorithm)) + { + /* Why it was so complex ? + sprintf(sql,"select status from services s,services_links l where l.serviceupid=%d and s.serviceid=l.servicedownid",serviceupid); + result2=DBselect(sql); + for(j=0;j<DBnum_rows(result2);j++) + { + if(atoi(DBget_field(result2,j,0))>status) + { + status=atoi(DBget_field(result2,j,0)); + } + } + DBfree_result(result2);*/ + + if(SERVICE_ALGORITHM_MAX == algorithm) + { + snprintf(sql,sizeof(sql)-1,"select count(*),max(status) from services s,services_links l where l.serviceupid=%d and s.serviceid=l.servicedownid",serviceupid); + } + /* MIN otherwise */ + else + { + snprintf(sql,sizeof(sql)-1,"select count(*),min(status) from services s,services_links l where l.serviceupid=%d and s.serviceid=l.servicedownid",serviceupid); + } + result2=DBselect(sql); + if(atoi(DBget_field(result2,0,0))!=0) + { + status=atoi(DBget_field(result2,0,1)); + } + DBfree_result(result2); + + now=time(NULL); + DBadd_service_alarm(atoi(DBget_field(result,i,0)),status,now); + snprintf(sql,sizeof(sql)-1,"update services set status=%d where serviceid=%d",status,atoi(DBget_field(result,i,0))); + DBexecute(sql); + } + else + { + zabbix_log( LOG_LEVEL_ERR, "Unknown calculation algorithm of service status [%d]", algorithm); + zabbix_syslog("Unknown calculation algorithm of service status [%d]", algorithm); + } + } + DBfree_result(result); + + snprintf(sql,sizeof(sql)-1,"select serviceupid from services_links where servicedownid=%d",serviceid); + result=DBselect(sql); + + for(i=0;i<DBnum_rows(result);i++) + { + update_serv(atoi(DBget_field(result,i,0))); + } + DBfree_result(result); +} + +void update_services(int triggerid, int status) +{ + char sql[MAX_STRING_LEN]; + int i; + + DB_RESULT *result; + + snprintf(sql,sizeof(sql)-1,"update services set status=%d where triggerid=%d",status,triggerid); + DBexecute(sql); + + + snprintf(sql,sizeof(sql)-1,"select serviceid from services where triggerid=%d", triggerid); + result = DBselect(sql); + + for(i=0;i<DBnum_rows(result);i++) + { + update_serv(atoi(DBget_field(result,i,0))); + } + + DBfree_result(result); + return; +} + +/* +* Re-calculate values of triggers +*/ +void update_triggers(int itemid) +{ + char sql[MAX_STRING_LEN]; + char exp[MAX_STRING_LEN]; + int exp_value; + int now; + DB_TRIGGER trigger; + DB_RESULT *result; + + int i; + + zabbix_log( LOG_LEVEL_DEBUG, "In update_triggers [%d]", itemid); + +/* Does not work for PostgreSQL */ +/* sprintf(sql,"select t.triggerid,t.expression,t.status,t.dep_level,t.priority,t.value from triggers t,functions f,items i where i.status<>3 and i.itemid=f.itemid and t.status=%d and f.triggerid=t.triggerid and f.itemid=%d group by t.triggerid,t.expression,t.dep_level",TRIGGER_STATUS_ENABLED,sucker_num);*/ +/* Is it correct SQL? */ + snprintf(sql,sizeof(sql)-1,"select distinct t.triggerid,t.expression,t.status,t.dep_level,t.priority,t.value,t.description 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=%d",ITEM_STATUS_NOTSUPPORTED, TRIGGER_STATUS_ENABLED, itemid); + + result = DBselect(sql); + + for(i=0;i<DBnum_rows(result);i++) + { + trigger.triggerid=atoi(DBget_field(result,i,0)); + trigger.expression=DBget_field(result,i,1); + trigger.status=atoi(DBget_field(result,i,2)); + trigger.priority=atoi(DBget_field(result,i,4)); + + trigger.value=atoi(DBget_field(result,i,5)); + trigger.description=DBget_field(result,i,6); + strscpy(exp, trigger.expression); + if( evaluate_expression(&exp_value, exp) != 0 ) + { + zabbix_log( LOG_LEVEL_WARNING, "Expression [%s] cannot be evaluated.",trigger.expression); + zabbix_syslog("Expression [%s] cannot be evaluated.",trigger.expression); + continue; + } + +/* Oprimise a little bit */ +/* trigger.prevvalue=DBget_prev_trigger_value(trigger.triggerid);*/ + + zabbix_log( LOG_LEVEL_DEBUG, "exp_value trigger.value trigger.prevvalue [%d] [%d] [%d]", exp_value, trigger.value, trigger.prevvalue); + + now = time(NULL); + DBupdate_trigger_value(&trigger, exp_value, now); + +/* if(TRIGGER_VALUE_TRUE == exp_value) + { + if(trigger.value != TRIGGER_VALUE_TRUE) + { + now = time(NULL); + DBupdate_trigger_value(trigger.triggerid,TRIGGER_VALUE_TRUE,now); + } + if((trigger.value == TRIGGER_VALUE_FALSE) + || + ( + (trigger.value == TRIGGER_VALUE_UNKNOWN) && + (trigger.prevvalue == TRIGGER_VALUE_FALSE) + )) + { + apply_actions(&trigger,1); + + snprintf(sql,sizeof(sql)-1,"update actions set nextcheck=0 where triggerid=%d and good=0",trigger.triggerid); + DBexecute(sql); + + update_services(trigger.triggerid, trigger.priority); + } + } + + if(TRIGGER_VALUE_FALSE == exp_value) + { + if(trigger.value != TRIGGER_VALUE_FALSE) + { + now = time(NULL); + DBupdate_trigger_value(trigger.triggerid,TRIGGER_VALUE_FALSE,now); + } + if((trigger.value == TRIGGER_VALUE_TRUE) + || + ( + (trigger.value == TRIGGER_VALUE_UNKNOWN) && + (trigger.prevvalue == TRIGGER_VALUE_TRUE) + )) + { + apply_actions(&trigger,0); + + snprintf(sql,sizeof(sql)-1,"update actions set nextcheck=0 where triggerid=%d and good=1",trigger.triggerid); + DBexecute(sql); + + update_services(trigger.triggerid, 0); + } + }*/ + } + DBfree_result(result); +} + +/* + The fuction is used to evaluate macros for email notifications +*/ +int get_lastvalue(char *value,char *host,char *key,char *function,char *parameter) +{ + DB_ITEM item; + DB_RESULT *result; + + char sql[MAX_STRING_LEN]; + int res; + + zabbix_log(LOG_LEVEL_DEBUG, "In get_lastvalue()" ); + + snprintf(sql,sizeof(sql)-1,"select i.itemid,i.key_,h.host,h.port,i.delay,i.description,i.nextcheck,i.type,i.snmp_community,i.snmp_oid,h.useip,h.ip,i.history,i.lastvalue,i.prevvalue,i.hostid,h.status,i.value_type,h.network_errors,i.snmp_port,i.delta,i.prevorgvalue,i.lastclock,i.units,i.multiplier,i.snmpv3_securityname,i.snmpv3_securitylevel,i.snmpv3_authpassphrase,i.snmpv3_privpassphrase,i.formula,h.available from items i,hosts h where h.host='%s' and h.hostid=i.hostid and i.key_='%s'", host, key ); + result = DBselect(sql); + + if(DBnum_rows(result) == 0) + { + DBfree_result(result); + zabbix_log(LOG_LEVEL_WARNING, "Query [%s] returned empty result", sql ); + zabbix_syslog("Query [%s] returned empty result", sql ); + return FAIL; + } + + DBget_item_from_db(&item,result, 0); + +/* item.itemid=atoi(DBget_field(result,0,0)); + s=DBget_field(result,0,1); + if(s==NULL) + { + item.prevvalue_null=1; + } + else + { + item.prevvalue_null=0; + item.prevvalue_str=s; + item.prevvalue=atof(s); + } + s=DBget_field(result,0,2); + if(s==NULL) + { + item.lastvalue_null=1; + } + else + { + item.lastvalue_null=0; + item.lastvalue_str=s; + item.lastvalue=atof(s); + } + item.value_type=atoi(DBget_field(result,0,3)); + + item.multiplier=atoi(DBget_field(result,0,4)); + item.units=DBget_field(result,0,5); +*/ + + zabbix_log(LOG_LEVEL_DEBUG, "Itemid:%d", item.itemid ); + + zabbix_log(LOG_LEVEL_DEBUG, "Before evaluate_FUNCTION()" ); + + res = evaluate_FUNCTION(value,&item,function,parameter, EVALUATE_FUNCTION_SUFFIX); + +/* Cannot call DBfree_result until evaluate_FUNC */ + DBfree_result(result); + return res; +} + +/* For zabbix_trapper(d) */ +int send_list_of_active_checks(int sockfd, char *host) +{ + char sql[MAX_STRING_LEN]; + char s[MAX_STRING_LEN]; + DB_RESULT *result; + + int i; + + zabbix_log( LOG_LEVEL_DEBUG, "In send_list_of_active_checks()"); + + snprintf(sql,sizeof(sql)-1,"select i.key_,i.delay,i.lastlogsize from items i,hosts h where i.hostid=h.hostid and h.status=%d and i.status=%d and i.type=%d and h.host='%s'", HOST_STATUS_MONITORED, ITEM_STATUS_ACTIVE, ITEM_TYPE_ZABBIX_ACTIVE, host); + + result = DBselect(sql); + + for(i=0;i<DBnum_rows(result);i++) + { + snprintf(s,sizeof(s)-1,"%s:%s:%s\n",DBget_field(result,i,0),DBget_field(result,i,1),DBget_field(result,i,2)); + zabbix_log( LOG_LEVEL_DEBUG, "Sending [%s]", s); + if( write(sockfd,s,strlen(s)) == -1 ) + { + switch (errno) + { + case EINTR: + zabbix_log( LOG_LEVEL_WARNING, "Timeout while sending list of active checks"); + break; + default: + zabbix_log( LOG_LEVEL_WARNING, "Error while sending list of active checks [%s]", strerror(errno)); + } + close(sockfd); + return FAIL; + } + } + DBfree_result(result); + + snprintf(s,sizeof(s)-1,"%s\n","ZBX_EOF"); + zabbix_log( LOG_LEVEL_DEBUG, "Sending [%s]", s); + if( write(sockfd,s,strlen(s)) == -1 ) + { + switch (errno) + { + case EINTR: + zabbix_log( LOG_LEVEL_WARNING, "Timeout while sending list of active checks"); + break; + default: + zabbix_log( LOG_LEVEL_WARNING, "Error while sending list of active checks [%s]", strerror(errno)); + } + close(sockfd); + return FAIL; + } + + return SUCCEED; +} + +/* For zabbix_trapper(d) */ +/* int process_data(char *server,char *key, double value)*/ +int process_data(int sockfd,char *server,char *key,char *value) +{ + char sql[MAX_STRING_LEN]; + + DB_RESULT *result; + DB_ITEM item; + char *s; + char lastlogsize[MAX_STRING_LEN]; + + zabbix_log( LOG_LEVEL_DEBUG, "In process_data()"); + + snprintf(sql,sizeof(sql)-1,"select i.itemid,i.key_,h.host,h.port,i.delay,i.description,i.nextcheck,i.type,i.snmp_community,i.snmp_oid,h.useip,h.ip,i.history,i.lastvalue,i.prevvalue,i.value_type,i.trapper_hosts,i.delta,i.units,i.multiplier,i.formula from items i,hosts h 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)", HOST_STATUS_MONITORED, server, key, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE); + result = DBselect(sql); + + if(DBnum_rows(result) == 0) + { + DBfree_result(result); + return FAIL; + } + + item.itemid=atoi(DBget_field(result,0,0)); + item.key=DBget_field(result,0,1); + item.host=DBget_field(result,0,2); + item.type=atoi(DBget_field(result,0,7)); + item.trapper_hosts=DBget_field(result,0,16); + + if( (item.type==ITEM_TYPE_ZABBIX_ACTIVE) && (check_security(sockfd,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 ); + zabbix_syslog("Active parameter [%s] is not supported by agent on host [%s]", item.key, item.host ); + DBupdate_item_status_to_notsupported(item.itemid, "Not supported by agent"); + } + + item.port=atoi(DBget_field(result,0,3)); + item.delay=atoi(DBget_field(result,0,4)); + item.description=DBget_field(result,0,5); + item.nextcheck=atoi(DBget_field(result,0,6)); + item.snmp_community=DBget_field(result,0,8); + item.snmp_oid=DBget_field(result,0,9); + item.useip=atoi(DBget_field(result,0,10)); + item.ip=DBget_field(result,0,11); + item.history=atoi(DBget_field(result,0,12)); + s=DBget_field(result,0,13); + if(s==NULL) + { + item.lastvalue_null=1; + } + else + { + item.lastvalue_null=0; + item.lastvalue_str=s; + item.lastvalue=atof(s); + } + s=DBget_field(result,0,14); + if(s==NULL) + { + item.prevvalue_null=1; + } + else + { + item.prevvalue_null=0; + item.prevvalue_str=s; + item.prevvalue=atof(s); + } + item.value_type=atoi(DBget_field(result,0,15)); + item.delta=atoi(DBget_field(result,0,17)); + item.units=DBget_field(result,0,18); + item.multiplier=atoi(DBget_field(result,0,19)); + item.formula=DBget_field(result,0,20); + + s=value; + if(strncmp(item.key,"log[",4)==0) + { + s=strchr(value,':'); + if(s == NULL) + { + zabbix_log(LOG_LEVEL_WARNING, "Wrong value received for item [%s:%s]", item.host, item.key); + DBfree_result(result); + return FAIL; + } + s++; + + strncpy(lastlogsize, value, s-value-1); + lastlogsize[s-value-1]=0; + + item.lastlogsize=atoi(lastlogsize); + zabbix_log(LOG_LEVEL_DEBUG, "Value [%s] S [%s] Lastlogsize [%s] [%d]", value, s, lastlogsize, s-value-1); + } + + process_new_value(&item,s); + + update_triggers(item.itemid); + + DBfree_result(result); + + return SUCCEED; +} + +void process_new_value(DB_ITEM *item,char *value) +{ + int now; + char sql[MAX_STRING_LEN]; + char value_esc[MAX_STRING_LEN]; + char value_str[MAX_STRING_LEN]; + double value_double; + double multiplier; + char *e; + + now = time(NULL); + + strscpy(value_str, value); + + zabbix_log( LOG_LEVEL_DEBUG, "In process_new_value()"); + value_double=strtod(value_str,&e); + + if( (item->value_type==ITEM_VALUE_TYPE_FLOAT) && (item->multiplier == ITEM_MULTIPLIER_USE)) + { + multiplier = strtod(item->formula,&e); + value_double = value_double * multiplier; + snprintf(value_str,sizeof(value_str)-1,"%f",value_double); + } + + if(item->history>0) + { + if(item->value_type==ITEM_VALUE_TYPE_FLOAT) + { + /* Should we store delta or original value? */ + if(item->delta == ITEM_STORE_AS_IS) + { + DBadd_history(item->itemid,value_double,now); + } + /* Delta as speed of change */ + else if(item->delta == ITEM_STORE_SPEED_PER_SECOND) + { + /* Save delta */ + if((item->prevorgvalue_null == 0) && (item->prevorgvalue <= value_double) ) + { + DBadd_history(item->itemid, (value_double - item->prevorgvalue)/(now-item->lastclock), now); + } + } + /* Real delta: simple difference between values */ + else if(item->delta == ITEM_STORE_SIMPLE_CHANGE) + { + /* Save delta */ + if((item->prevorgvalue_null == 0) && (item->prevorgvalue <= value_double) ) + { + DBadd_history(item->itemid, (value_double - item->prevorgvalue), 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); + return; + } + } + else if(item->value_type==ITEM_VALUE_TYPE_STR) + { + DBadd_history_str(item->itemid,value_str,now); + } + else if(item->value_type==ITEM_VALUE_TYPE_LOG) + { + DBadd_history_log(item->itemid,value_str,now); + snprintf(sql,sizeof(sql)-1,"update items set lastlogsize=%d where itemid=%d",item->lastlogsize,item->itemid); + DBexecute(sql); + } + else + { + zabbix_log(LOG_LEVEL_ERR, "Unknown value type [%d] for itemid [%d]", item->value_type,item->itemid); + } + } + + if(item->delta == ITEM_STORE_AS_IS) + { + if((item->prevvalue_null == 1) || (strcmp(value_str,item->lastvalue_str) != 0) || (strcmp(item->prevvalue_str,item->lastvalue_str) != 0) ) + { + DBescape_string(value_str,value_esc,MAX_STRING_LEN); +/* snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevvalue=lastvalue,lastvalue='%s',lastclock=%d where itemid=%d",now+item->delay,value_esc,now,item->itemid);*/ + snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevvalue=lastvalue,lastvalue='%s',lastclock=%d where itemid=%d",calculate_item_nextcheck(item->nextcheck,item->delay,now),value_esc,now,item->itemid); + item->prevvalue=item->lastvalue; + item->lastvalue=value_double; + item->prevvalue_str=item->lastvalue_str; + /* Risky !!!*/ + item->lastvalue_str=value_str; + item->prevvalue_null=item->lastvalue_null; + item->lastvalue_null=0; + } + else + { +/* snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,lastclock=%d where itemid=%d",now+item->delay,now,item->itemid);*/ + snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,lastclock=%d where itemid=%d",calculate_item_nextcheck(item->nextcheck,item->delay,now),now,item->itemid); + } + } + /* Logic for delta as speed of change */ + else if(item->delta == ITEM_STORE_SPEED_PER_SECOND) + { + if((item->prevorgvalue_null == 0) && (item->prevorgvalue <= value_double) ) + { +/* snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue=%f,lastvalue='%f',lastclock=%d where itemid=%d",now+item->delay,value_double,(value_double - item->prevorgvalue)/(now-item->lastclock),now,item->itemid);*/ + snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue=%f,lastvalue='%f',lastclock=%d where itemid=%d",calculate_item_nextcheck(item->nextcheck,item->delay,now),value_double,(value_double - item->prevorgvalue)/(now-item->lastclock),now,item->itemid); + } + else + { +/* snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevorgvalue=%f,lastclock=%d where itemid=%d",now+item->delay,value_double,now,item->itemid);*/ + snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevorgvalue=%f,lastclock=%d where itemid=%d",calculate_item_nextcheck(item->nextcheck,item->delay,now),value_double,now,item->itemid); + } + + item->prevvalue=item->lastvalue; + item->lastvalue=(value_double - item->prevorgvalue)/(now-item->lastclock); + item->prevvalue_str=item->lastvalue_str; + /* Risky !!!*/ + item->lastvalue_str=value_str; + item->prevvalue_null=item->lastvalue_null; + item->lastvalue_null=0; + } + /* Real delta: simple difference between values */ + else if(item->delta == ITEM_STORE_SIMPLE_CHANGE) + { + if((item->prevorgvalue_null == 0) && (item->prevorgvalue <= value_double) ) + { +/* snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue=%f,lastvalue='%f',lastclock=%d where itemid=%d",now+item->delay,value_double,(value_double - item->prevorgvalue),now,item->itemid);*/ + snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue=%f,lastvalue='%f',lastclock=%d where itemid=%d",calculate_item_nextcheck(item->nextcheck,item->delay,now),value_double,(value_double - item->prevorgvalue),now,item->itemid); + } + else + { +/* snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevorgvalue=%f,lastclock=%d where itemid=%d",now+item->delay,value_double,now,item->itemid);*/ + snprintf(sql,sizeof(sql)-1,"update items set nextcheck=%d,prevorgvalue=%f,lastclock=%d where itemid=%d",calculate_item_nextcheck(item->nextcheck,item->delay,now),value_double,now,item->itemid); + } + + item->prevvalue=item->lastvalue; + item->lastvalue=(value_double - item->prevorgvalue); + item->prevvalue_str=item->lastvalue_str; + /* Risky !!!*/ + item->lastvalue_str=value_str; + item->prevvalue_null=item->lastvalue_null; + item->lastvalue_null=0; + } + DBexecute(sql); + + update_functions( item ); +} diff --git a/src/zabbix_server/functions.h b/src/zabbix_server/functions.h new file mode 100644 index 00000000..1949bb5a --- /dev/null +++ b/src/zabbix_server/functions.h @@ -0,0 +1,36 @@ +/* +** 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 "db.h" + +#define EVALUATE_FUNCTION_NORMAL 0 +#define EVALUATE_FUNCTION_SUFFIX 1 + +void update_triggers (int itemid); +int get_lastvalue(char *value,char *host,char *key,char *function,char *parameter); +int process_data(int sockfd,char *server,char *key, char *value); +void process_new_value(DB_ITEM *item,char *value); +int send_list_of_active_checks(int sockfd, char *host); + +#endif |
