diff options
author | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2002-08-04 16:53:24 +0000 |
---|---|---|
committer | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2002-08-04 16:53:24 +0000 |
commit | 2f691b3628c4589fe258def9564d78cd134d3ebb (patch) | |
tree | 278a676e3e5540977348c86b9d7a96ccc105902d /include | |
parent | 89d810024288866e72586c1774dec254db9aa884 (diff) | |
download | zabbix-2f691b3628c4589fe258def9564d78cd134d3ebb.tar.gz zabbix-2f691b3628c4589fe258def9564d78cd134d3ebb.tar.xz zabbix-2f691b3628c4589fe258def9564d78cd134d3ebb.zip |
- added upgrades/dbpatches/1.0beta4_to_1.0beta5/ with patches (Alexei)
- added support for disktotal[*] (Alexei)
- added support for inodetotal[*] (Alexei)
- added support for *,/,+, and - in trigger expressions (Alexei)
- removed frontends/php/chart4.php (Alexei)
- update item status to UNSUPPORTED in case if SNMP support was not included
into zabbix_suckerd (Alexei)
- added mysql_init() to DBconnect() (Alexei)
- fixed Next100 and Prev100 for case if some hosts are hidden (Alexei)
- added select for latest values (Alexei)
- delete related services if trigger is deleted (Alexei)
- fixed problem with substitution of macros for messages (Alexei)
- eliminated DBis_empty() by replacing to DBnum_rows() (Alexei)
- MAX_STRING_LEN increased to 4096 (Alexei)
- fixed zabbix_log(). Possible coredump if data contains %s, etc (Alexei)
git-svn-id: svn://svn.zabbix.com/trunk@445 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'include')
-rw-r--r-- | include/common.h | 4 | ||||
-rw-r--r-- | include/db.c | 105 | ||||
-rw-r--r-- | include/db.h | 11 | ||||
-rw-r--r-- | include/expression.c | 139 | ||||
-rw-r--r-- | include/functions.c | 137 | ||||
-rw-r--r-- | include/log.c | 2 |
6 files changed, 242 insertions, 156 deletions
diff --git a/include/common.h b/include/common.h index fbdffa82..3018a20e 100644 --- a/include/common.h +++ b/include/common.h @@ -1,3 +1,5 @@ +/*#define TESTTEST*/ + #ifndef ZABBIX_COMMON_H #define ZABBIX_COMMON_H @@ -8,7 +10,7 @@ #define TIMEOUT_ERROR (-4) #define MAXFD 64 -#define MAX_STRING_LEN 1024 +#define MAX_STRING_LEN 4096 /* Item types */ #define ITEM_TYPE_ZABBIX 0 diff --git a/include/db.c b/include/db.c index 3f2edde0..a7bb3f07 100644 --- a/include/db.c +++ b/include/db.c @@ -33,6 +33,7 @@ void DBconnect( char *dbname, char *dbuser, char *dbpassword, char *dbsocket) #ifdef HAVE_MYSQL /* For MySQL >3.22.00 */ /* if( ! mysql_connect( &mysql, NULL, dbuser, dbpassword ) )*/ + mysql_init(&mysql); if( ! mysql_real_connect( &mysql, NULL, dbuser, dbpassword, dbname, 3306, dbsocket,0 ) ) { zabbix_log(LOG_LEVEL_ERR, "Failed to connect to database: Error: %s\n",mysql_error(&mysql) ); @@ -158,7 +159,6 @@ char *DBget_field(DB_RESULT *result, int rownum, int fieldnum) zabbix_log(LOG_LEVEL_ERR, "Error while mysql_fetch_row():[%s]", mysql_error(&mysql) ); exit(FAIL); } - zabbix_log(LOG_LEVEL_DEBUG, "Got field:%s", row[fieldnum] ); return row[fieldnum]; #endif #ifdef HAVE_PGSQL @@ -169,7 +169,7 @@ char *DBget_field(DB_RESULT *result, int rownum, int fieldnum) /* * Return SUCCEED if result conains no records */ -int DBis_empty(DB_RESULT *result) +/*int DBis_empty(DB_RESULT *result) { zabbix_log(LOG_LEVEL_DEBUG, "In DBis_empty"); if(result == NULL) @@ -180,27 +180,47 @@ int DBis_empty(DB_RESULT *result) { return SUCCEED; } -/* This is necessary to exclude situations like - * atoi(DBget_field(result,0,0). This lead to coredump. - */ if(DBget_field(result,0,0) == 0) { return SUCCEED; } return FAIL; -} +}*/ /* * Get number of selected records. */ int DBnum_rows(DB_RESULT *result) { - zabbix_log(LOG_LEVEL_DEBUG, "In DBnum_rows"); #ifdef HAVE_MYSQL - return mysql_num_rows(result); + int rows; + + zabbix_log(LOG_LEVEL_DEBUG, "In DBnum_rows"); + if(result == NULL) + { + return 0; + } +/* Order is important ! */ + rows = mysql_num_rows(result); + if(rows == 0) + { + return 0; + } + +/* This is necessary to exclude situations like + * atoi(DBget_field(result,0,0). This leads to coredump. + */ +/* This is required for empty results for count(*), etc */ + if(DBget_field(result,0,0) == 0) + { + return 0; + } + zabbix_log(LOG_LEVEL_DEBUG, "Result of DBnum_rows [%d]", rows); + return rows; #endif #ifdef HAVE_PGSQL + zabbix_log(LOG_LEVEL_DEBUG, "In DBnum_rows"); return PQntuples(result); #endif } @@ -210,24 +230,26 @@ int DBnum_rows(DB_RESULT *result) */ int DBget_function_result(float *result,char *functionid) { - DB_RESULT *res; + DB_RESULT *dbresult; + int res = SUCCEED; char sql[MAX_STRING_LEN+1]; sprintf( sql, "select lastvalue from functions where functionid=%s", functionid ); - res = DBselect(sql); + dbresult = DBselect(sql); - if(DBis_empty(res) == SUCCEED) + if(DBnum_rows(dbresult) == 0) { - DBfree_result(res); zabbix_log(LOG_LEVEL_WARNING, "Query failed for functionid:[%s]", functionid ); - return FAIL; + res = FAIL; } + else + { + *result=atof(DBget_field(dbresult,0,0)); + } + DBfree_result(dbresult); - *result=atof(DBget_field(res,0,0)); - DBfree_result(res); - - return SUCCEED; + return res; } /* SUCCEED if latest alarm with triggerid has this status */ @@ -245,7 +267,7 @@ int DBget_prev_trigger_value(int triggerid) zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == 0) { zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" ); DBfree_result(result); @@ -258,7 +280,7 @@ int DBget_prev_trigger_value(int triggerid) zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == 0) { zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" ); DBfree_result(result); @@ -271,7 +293,7 @@ int DBget_prev_trigger_value(int triggerid) zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == SUCCEED) { zabbix_log(LOG_LEVEL_DEBUG, "Result of [%s] is empty", sql ); DBfree_result(result); @@ -284,6 +306,7 @@ int DBget_prev_trigger_value(int triggerid) } /* SUCCEED if latest alarm with triggerid has this status */ +/* Rewrite required to simplify logic ?*/ int latest_alarm(int triggerid, int status) { char sql[MAX_STRING_LEN+1]; @@ -298,16 +321,14 @@ int latest_alarm(int triggerid, int status) zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == 0) { zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" ); ret = FAIL; } else { - zabbix_log(LOG_LEVEL_DEBUG,"In latest_alarm(0)"); clock=atoi(DBget_field(result,0,0)); - zabbix_log(LOG_LEVEL_DEBUG,"In latest_alarm(1)"); DBfree_result(result); sprintf(sql,"select value from alarms where triggerid=%d and clock=%d",triggerid,clock); @@ -362,7 +383,7 @@ int update_trigger_value(int triggerid,int value,int clock) return SUCCEED; } -int update_triggers_status_to_unknown(int hostid,int clock) +void update_triggers_status_to_unknown(int hostid,int clock) { int i; char sql[MAX_STRING_LEN+1]; @@ -376,13 +397,6 @@ int update_triggers_status_to_unknown(int hostid,int clock) zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); result = DBselect(sql); - if( DBis_empty(result) == SUCCEED ) - { - zabbix_log(LOG_LEVEL_DEBUG, "No triggers to update for hostid=[%d]",hostid); - DBfree_result(result); - return FAIL; - } - for(i=0;i<DBnum_rows(result);i++) { triggerid=atoi(DBget_field(result,i,0)); @@ -392,10 +406,10 @@ int update_triggers_status_to_unknown(int hostid,int clock) DBfree_result(result); zabbix_log(LOG_LEVEL_DEBUG,"End of update_triggers_status_to_unknown()"); - return SUCCEED; + return; } -int DBupdate_triggers_status_after_restart(void) +void DBupdate_triggers_status_after_restart(void) { int i; char sql[MAX_STRING_LEN+1]; @@ -413,13 +427,6 @@ int DBupdate_triggers_status_after_restart(void) zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); result = DBselect(sql); - if( DBis_empty(result) == SUCCEED ) - { - zabbix_log(LOG_LEVEL_DEBUG, "No triggers to update"); - DBfree_result(result); - return FAIL; - } - for(i=0;i<DBnum_rows(result);i++) { triggerid=atoi(DBget_field(result,i,0)); @@ -427,7 +434,7 @@ int DBupdate_triggers_status_after_restart(void) sprintf(sql,"select min(i.nextcheck+i.delay) from hosts h,items i,triggers t,functions f where f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and i.nextcheck<>0 and t.triggerid=%d and i.status<>%d",triggerid,ITEM_STATUS_TRAPPED); zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); result2 = DBselect(sql); - if( DBis_empty(result2) == SUCCEED ) + if( DBnum_rows(result2) == 0 ) { zabbix_log(LOG_LEVEL_DEBUG, "No triggers to update (2)"); DBfree_result(result2); @@ -443,10 +450,10 @@ int DBupdate_triggers_status_after_restart(void) DBfree_result(result); zabbix_log(LOG_LEVEL_DEBUG,"End of DBupdate_triggers_after_restart()"); - return SUCCEED; + return; } -int DBupdate_host_status(int hostid,int status,int clock) +void DBupdate_host_status(int hostid,int status,int clock) { DB_RESULT *result; char sql[MAX_STRING_LEN+1]; @@ -458,11 +465,11 @@ int DBupdate_host_status(int hostid,int status,int clock) zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); result = DBselect(sql); - if( DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == 0) { - zabbix_log(LOG_LEVEL_WARNING, "No host with hostid=[%d]",hostid); + zabbix_log(LOG_LEVEL_ERR, "Cannot select host with hostid [%d]",hostid); DBfree_result(result); - return FAIL; + return; } disable_until = atoi(DBget_field(result,0,1)); @@ -477,7 +484,7 @@ int DBupdate_host_status(int hostid,int status,int clock) { zabbix_log(LOG_LEVEL_DEBUG, "Host already has status [%d]",status); DBfree_result(result); - return FAIL; + return; } } @@ -511,13 +518,13 @@ int DBupdate_host_status(int hostid,int status,int clock) else { zabbix_log( LOG_LEVEL_ERR, "Unknown host status [%d]", status); - return FAIL; + return; } update_triggers_status_to_unknown(hostid,clock); zabbix_log(LOG_LEVEL_DEBUG,"End of update_host_status()"); - return SUCCEED; + return; } int DBupdate_item_status_to_notsupported(int itemid) diff --git a/include/db.h b/include/db.h index f4dba182..86e62b6a 100644 --- a/include/db.h +++ b/include/db.h @@ -4,6 +4,7 @@ /* time_t */ #include <time.h> #include "config.h" +#include "common.h" #ifdef HAVE_MYSQL #include "mysql.h" @@ -97,8 +98,8 @@ DB_ACTION int good; int delay; int lastcheck; - char *subject; - char *message; + char subject[MAX_STRING_LEN+1]; + char message[MAX_STRING_LEN+1]; }; DB_ALERT @@ -123,16 +124,16 @@ int DBexecute( char *query ); DB_RESULT *DBselect(char *query); char *DBget_field(DB_RESULT *result, int rownum, int fieldnum); int DBnum_rows(DB_RESULT *result); -int DBis_empty(DB_RESULT *result); +/*int DBis_empty(DB_RESULT *result);*/ int DBget_function_result(float *result,char *functionid); -int DBupdate_host_status(int hostid,int status,int clock); +void DBupdate_host_status(int hostid,int status,int clock); int DBupdate_item_status_to_notsupported(int itemid); int DBadd_history(int itemid, double value); int DBadd_history_str(int itemid, char *value); int DBadd_alarm(int triggerid, int status,int clock); int DBadd_alert(int actionid, char *type, char *sendto, char *subject, char *message); -int DBupdate_triggers_status_after_restart(void); +void DBupdate_triggers_status_after_restart(void); int DBget_prev_trigger_value(int triggerid); #endif diff --git a/include/expression.c b/include/expression.c index 0fff01bd..89b912d4 100644 --- a/include/expression.c +++ b/include/expression.c @@ -249,6 +249,118 @@ int evaluate_simple (float *result,char *exp) zabbix_log(LOG_LEVEL_DEBUG, "Result [%f]",*result ); return SUCCEED; } + else if( find_char(exp,'*') != FAIL ) + { + zabbix_log(LOG_LEVEL_DEBUG, "* is found" ); + l=find_char(exp,'*'); + strncpy(first, exp, MAX_STRING_LEN); + first[l]=0; + j=0; + for(i=l+1;i<strlen(exp);i++) + { + second[j]=exp[i]; + j++; + } + second[j]=0; + if( evaluate_simple(&value1,first) == FAIL ) + { + zabbix_log(LOG_LEVEL_WARNING, "Cannot evaluate expression [%s]", first ); + return FAIL; + } + if( evaluate_simple(&value2,second) == FAIL ) + { + zabbix_log(LOG_LEVEL_WARNING, "Cannot evaluate expression [%s]", second ); + return FAIL; + } + *result=value1*value2; + return SUCCEED; + } + else if( find_char(exp,'/') != FAIL ) + { + zabbix_log(LOG_LEVEL_DEBUG, "/ is found" ); + l=find_char(exp,'/'); + strncpy(first, exp, MAX_STRING_LEN); + first[l]=0; + j=0; + for(i=l+1;i<strlen(exp);i++) + { + second[j]=exp[i]; + j++; + } + second[j]=0; + if( evaluate_simple(&value1,first) == FAIL ) + { + zabbix_log(LOG_LEVEL_WARNING, "Cannot evaluate expression [%s]", first ); + return FAIL; + } + if( evaluate_simple(&value2,second) == FAIL ) + { + zabbix_log(LOG_LEVEL_WARNING, "Cannot evaluate expression [%s]", second ); + return FAIL; + } + if(cmp_double(value2,0) == 0) + { + zabbix_log(LOG_LEVEL_WARNING, "Division by zero. Cannot evaluate expression [%s/%s]", first,second ); + return FAIL; + } + else + { + *result=value1/value2; + } + return SUCCEED; + } + else if( find_char(exp,'+') != FAIL ) + { + zabbix_log(LOG_LEVEL_DEBUG, "+ is found" ); + l=find_char(exp,'+'); + strncpy(first, exp, MAX_STRING_LEN); + first[l]=0; + j=0; + for(i=l+1;i<strlen(exp);i++) + { + second[j]=exp[i]; + j++; + } + second[j]=0; + if( evaluate_simple(&value1,first) == FAIL ) + { + zabbix_log(LOG_LEVEL_WARNING, "Cannot evaluate expression [%s]", first ); + return FAIL; + } + if( evaluate_simple(&value2,second) == FAIL ) + { + zabbix_log(LOG_LEVEL_WARNING, "Cannot evaluate expression [%s]", second ); + return FAIL; + } + *result=value1+value2; + return SUCCEED; + } + else if( find_char(exp,'-') != FAIL ) + { + zabbix_log(LOG_LEVEL_DEBUG, "- is found" ); + l=find_char(exp,'-'); + strncpy(first, exp, MAX_STRING_LEN); + first[l]=0; + j=0; + for(i=l+1;i<strlen(exp);i++) + { + second[j]=exp[i]; + j++; + } + second[j]=0; + if( evaluate_simple(&value1,first) == FAIL ) + { + zabbix_log(LOG_LEVEL_WARNING, "Cannot evaluate expression [%s]", first ); + return FAIL; + } + if( evaluate_simple(&value2,second) == FAIL ) + { + zabbix_log(LOG_LEVEL_WARNING, "Cannot evaluate expression [%s]", second ); + return FAIL; + } + *result=value1-value2; + return SUCCEED; + } else if( find_char(exp,'=') != FAIL ) { zabbix_log(LOG_LEVEL_DEBUG, "= is found" ); @@ -366,13 +478,11 @@ int substitute_macros(char *exp) char function[MAX_STRING_LEN+1]; char parameter[MAX_STRING_LEN+1]; static char value[MAX_STRING_LEN+1]; - int i,j; + int i; int r,l; int r1,l1; - zabbix_log(LOG_LEVEL_DEBUG, "BEGIN substitute_macros" ); - - zabbix_log( LOG_LEVEL_DEBUG, "Expression1:[%s]", exp ); + zabbix_log(LOG_LEVEL_DEBUG, "In substitute_macros([%s])",exp); while( find_char(exp,'{') != FAIL ) { @@ -442,18 +552,23 @@ int substitute_macros(char *exp) } parameter[r1]=0; + zabbix_log( LOG_LEVEL_DEBUG, "Parameter:%s", parameter ); + i=get_lastvalue(value,host,key,function,parameter); zabbix_log( LOG_LEVEL_DEBUG, "Value3 [%s]", value ); -/* exp[l]='%'; + + + zabbix_log( LOG_LEVEL_DEBUG, "Value4 [%s]", exp ); + exp[l]='%'; exp[l+1]='s'; - exp[l+2]=' '; - for(i=l+3;i<=r;i++) - { - exp[i]=' '; - }*/ + zabbix_log( LOG_LEVEL_DEBUG, "Value41 [%s]", exp+l+2 ); + zabbix_log( LOG_LEVEL_DEBUG, "Value42 [%s]", exp+r+1 ); + strcpy(exp+l+2,exp+r+1); - j=0; + zabbix_log( LOG_LEVEL_DEBUG, "Value5 [%s]", exp ); + +/* j=0; for(i=0;i<strlen(exp);i++) { if( (i>=l+3) && (i<=r) ) @@ -465,7 +580,7 @@ int substitute_macros(char *exp) exp[j]='s'; j++; } - exp[j]=0; + exp[j]=0;*/ sprintf(res,exp,value); strncpy(exp,res, MAX_STRING_LEN); diff --git a/include/functions.c b/include/functions.c index 5102db45..1e9f7212 100644 --- a/include/functions.c +++ b/include/functions.c @@ -31,8 +31,8 @@ int evaluate_MIN(char *value,DB_ITEM *item,int parameter) DB_RESULT *result; char sql[MAX_STRING_LEN+1]; - int now; + int res = SUCCEED; if(item->value_type != 0) { @@ -44,18 +44,18 @@ int evaluate_MIN(char *value,DB_ITEM *item,int parameter) sprintf(sql,"select min(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == 0) { zabbix_log(LOG_LEVEL_DEBUG, "Result for MIN is empty" ); - DBfree_result(result); - return FAIL; + res = FAIL; + } + else + { + strncpy(value,DBget_field(result,0,0),MAX_STRING_LEN); } - - strncpy(value,DBget_field(result,0,0),MAX_STRING_LEN); - DBfree_result(result); - return SUCCEED; + return res; } /* @@ -66,8 +66,8 @@ int evaluate_MAX(char *value,DB_ITEM *item,int parameter) DB_RESULT *result; char sql[MAX_STRING_LEN+1]; - int now; + int res = SUCCEED; if(item->value_type != 0) { @@ -79,18 +79,18 @@ int evaluate_MAX(char *value,DB_ITEM *item,int parameter) sprintf(sql,"select max(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == 0) { zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" ); - DBfree_result(result); - return FAIL; + res = FAIL; + } + else + { + strncpy(value,DBget_field(result,0,0),MAX_STRING_LEN); } - - strncpy(value,DBget_field(result,0,0),MAX_STRING_LEN); - DBfree_result(result); - return SUCCEED; + return res; } /* @@ -210,13 +210,6 @@ void update_functions(DB_ITEM *item) result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No functions to update."); - DBfree_result(result); - return; - } - for(i=0;i<DBnum_rows(result);i++) { function.function=DBget_field(result,i,0); @@ -496,13 +489,6 @@ void send_to_user(int actionid,int userid,char *subject,char *message) sprintf(sql,"select type,sendto,active from media where active=%d and userid=%d",MEDIA_STATUS_ACTIVE,userid); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No active media defined for this user."); - DBfree_result(result); - return; - } - for(i=0;i<DBnum_rows(result);i++) { media.active=atoi(DBget_field(result,i,2)); @@ -523,12 +509,14 @@ void substitute_hostname(int triggerid,char *s) char sql[MAX_STRING_LEN+1]; DB_RESULT *result; + zabbix_log( LOG_LEVEL_DEBUG, "In substitute_hostname([%d],[%s])",triggerid,s); + if(strstr(s,"%s") != 0) { sprintf(sql,"select distinct t.description,h.host from triggers t, functions f,items i, hosts h where t.triggerid=%d and f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid", triggerid); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == 0) { zabbix_log( LOG_LEVEL_DEBUG, "No hostname in substitute_hostname()"); DBfree_result(result); @@ -539,6 +527,7 @@ void substitute_hostname(int triggerid,char *s) DBfree_result(result); } + zabbix_log( LOG_LEVEL_DEBUG, "End of substitute_hostname() Result [%s]",s); } /* @@ -552,7 +541,7 @@ void apply_actions(int triggerid,int good) char sql[MAX_STRING_LEN+1]; - int i,rows; + int i; int now; zabbix_log( LOG_LEVEL_DEBUG, "In apply_actions()"); @@ -563,7 +552,7 @@ void apply_actions(int triggerid,int good) sprintf(sql,"select count(*) from trigger_depends d,triggers t where d.triggerid_down=%d and d.triggerid_up=t.triggerid and t.value=%d",triggerid, TRIGGER_VALUE_TRUE); result = DBselect(sql); - if(DBis_empty(result) == FAIL) + if(DBnum_rows(result) == 1) { if(atoi(DBget_field(result,0,0))>0) { @@ -582,25 +571,17 @@ void apply_actions(int triggerid,int good) sprintf(sql,"select actionid,userid,delay,subject,message from actions where triggerid=%d and good=%d and nextcheck<=%d",triggerid,good,now); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + for(i=0;i<DBnum_rows(result);i++) { - zabbix_log( LOG_LEVEL_DEBUG, "No actions applied."); - DBfree_result(result); - return; - } - rows = DBnum_rows(result); - - for(i=0;i<rows;i++) - { zabbix_log( LOG_LEVEL_DEBUG, "i=[%d]",i); /* zabbix_log( LOG_LEVEL_DEBUG, "Fetched:%s %s %s %s %s\n",DBget_field(result,i,0),DBget_field(result,i,1),DBget_field(result,i,2),DBget_field(result,i,3),DBget_field(result,i,4));*/ action.actionid=atoi(DBget_field(result,i,0)); action.userid=atoi(DBget_field(result,i,1)); action.delay=atoi(DBget_field(result,i,2)); - action.subject=DBget_field(result,i,3); - action.message=DBget_field(result,i,4); + strncpy(action.subject,DBget_field(result,i,3),MAX_STRING_LEN); + strncpy(action.message,DBget_field(result,i,4),MAX_STRING_LEN); substitute_hostname(triggerid,action.message); substitute_hostname(triggerid,action.subject); @@ -632,48 +613,42 @@ void update_serv(int serviceid) sprintf(sql,"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; - if(DBis_empty(result) != SUCCEED) + for(i=0;i<DBnum_rows(result);i++) { - 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) { - 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) + } + else if(SERVICE_ALGORITHM_MAX == algorithm) + { + 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++) { - 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) { - if(atoi(DBget_field(result2,j,0))>status) - { - status=atoi(DBget_field(result2,j,0)); - } + status=atoi(DBget_field(result2,j,0)); } - sprintf(sql,"update services set status=%d where serviceid=%d",status,atoi(DBget_field(result,i,0))); - DBexecute(sql); - DBfree_result(result2); - } - else - { - zabbix_log( LOG_LEVEL_ERR, "Unknown calculation algorithm of service status [%d]", algorithm); } + sprintf(sql,"update services set status=%d where serviceid=%d",status,atoi(DBget_field(result,i,0))); + DBexecute(sql); + DBfree_result(result2); + } + else + { + zabbix_log( LOG_LEVEL_ERR, "Unknown calculation algorithm of service status [%d]", algorithm); } - } + } DBfree_result(result); sprintf(sql,"select serviceupid from services_links where servicedownid=%d",serviceid); result=DBselect(sql); - if(DBis_empty(result) != SUCCEED) + for(i=0;i<DBnum_rows(result);i++) { - for(i=0;i<DBnum_rows(result);i++) - { - update_serv(atoi(DBget_field(result,i,0))); - } + update_serv(atoi(DBget_field(result,i,0))); } DBfree_result(result); } @@ -692,13 +667,6 @@ void update_services(int triggerid, int status) sprintf(sql,"select serviceid from services where triggerid=%d", triggerid); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No service for this triggerid [%d]", triggerid); - DBfree_result(result); - return; - } - for(i=0;i<DBnum_rows(result);i++) { update_serv(atoi(DBget_field(result,i,0))); @@ -739,13 +707,6 @@ void update_triggers( int suckers, int flag, int sucker_num, int lastclock ) result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) - { - zabbix_log( LOG_LEVEL_DEBUG, "No triggers to update" ); - DBfree_result(result); - return; - } - for(i=0;i<DBnum_rows(result);i++) { trigger.triggerid=atoi(DBget_field(result,i,0)); @@ -837,7 +798,7 @@ int get_lastvalue(char *value,char *host,char *key,char *function,char *paramete sprintf(sql, "select i.itemid,i.prevvalue,i.lastvalue,i.value_type from items i,hosts h where h.host='%s' and h.hostid=i.hostid and i.key_='%s'", host, key ); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == 0) { DBfree_result(result); zabbix_log(LOG_LEVEL_WARNING, "Query [%s] returned empty result" ); @@ -896,7 +857,7 @@ int process_data(int sockfd,char *server,char *key,char *value) sprintf(sql,"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 from items i,hosts h where h.status in (0,2) and h.hostid=i.hostid and h.host='%s' and i.key_='%s' and i.status=%d", server, key, ITEM_TYPE_TRAPPER); result = DBselect(sql); - if(DBis_empty(result) == SUCCEED) + if(DBnum_rows(result) == 0) { DBfree_result(result); return FAIL; diff --git a/include/log.c b/include/log.c index 3ffb4cc6..3bd66d0c 100644 --- a/include/log.c +++ b/include/log.c @@ -79,7 +79,7 @@ void zabbix_log(int level, const char *fmt, ...) vsprintf(str,fmt,ap); strncat(str,"\n",MAX_STRING_LEN); strncat(str2,str,MAX_STRING_LEN); - fprintf(log_file,str2); + fprintf(log_file,"%s",str2); fflush(log_file); va_end(ap); } |