diff options
| author | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2006-07-13 07:55:39 +0000 |
|---|---|---|
| committer | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2006-07-13 07:55:39 +0000 |
| commit | 697332fe6532254a7c9125aefdfd3a68dda2a6f1 (patch) | |
| tree | b112002e78f9998589ee8885bad6a4f9655c6834 /src | |
| parent | 8990a1cb4b4245d01e93e7fd9cc0825348e7ef56 (diff) | |
- new more efficient definition of function DBexecute() (Alexei)
- new more efficient definition of function DBselect() (Alexei)
git-svn-id: svn://svn.zabbix.com/trunk@3042 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
| -rw-r--r-- | src/libs/zbxdbhigh/action.c | 8 | ||||
| -rw-r--r-- | src/libs/zbxdbhigh/db.c | 383 | ||||
| -rw-r--r-- | src/libs/zbxdbhigh/graph.c | 31 | ||||
| -rw-r--r-- | src/libs/zbxdbhigh/host.c | 36 | ||||
| -rw-r--r-- | src/libs/zbxdbhigh/item.c | 21 | ||||
| -rw-r--r-- | src/libs/zbxdbhigh/trigger.c | 47 | ||||
| -rw-r--r-- | src/zabbix_agent/zabbix_agentd.c | 3 | ||||
| -rw-r--r-- | src/zabbix_server/actions.c | 37 | ||||
| -rw-r--r-- | src/zabbix_server/alerter/alerter.c | 13 | ||||
| -rw-r--r-- | src/zabbix_server/evalfunc.c | 35 | ||||
| -rw-r--r-- | src/zabbix_server/expression.c | 10 | ||||
| -rw-r--r-- | src/zabbix_server/functions.c | 68 | ||||
| -rw-r--r-- | src/zabbix_server/housekeeper/housekeeper.c | 42 | ||||
| -rw-r--r-- | src/zabbix_server/pinger/pinger.c | 15 | ||||
| -rw-r--r-- | src/zabbix_server/poller/checks_aggregate.c | 3 | ||||
| -rw-r--r-- | src/zabbix_server/poller/poller.c | 42 | ||||
| -rw-r--r-- | src/zabbix_server/server.c | 8 | ||||
| -rw-r--r-- | src/zabbix_server/timer/timer.c | 5 | ||||
| -rw-r--r-- | src/zabbix_server/trapper/active.c | 4 | ||||
| -rw-r--r-- | src/zabbix_server/trapper/autoregister.c | 5 | ||||
| -rw-r--r-- | src/zabbix_server/zlog.c | 4 |
21 files changed, 326 insertions, 494 deletions
diff --git a/src/libs/zbxdbhigh/action.c b/src/libs/zbxdbhigh/action.c index 43759384..54971a4c 100644 --- a/src/libs/zbxdbhigh/action.c +++ b/src/libs/zbxdbhigh/action.c @@ -31,7 +31,6 @@ int DBadd_action(int triggerid, int userid, char *subject, char *message, int scope, int severity, int recipient, int usrgrpid) { - char sql[MAX_STRING_LEN]; int actionid; char subject_esc[ACTION_SUBJECT_LEN_MAX]; char message_esc[MAX_STRING_LEN]; @@ -44,8 +43,7 @@ int DBadd_action(int triggerid, int userid, char *subject, char *message, int sc userid = usrgrpid; } - zbx_snprintf(sql, sizeof(sql),"insert into actions (triggerid, userid, subject, message, scope, severity, recipient) values (%d, %d, '%s', '%s', %d, %d, %d)", triggerid, userid, subject_esc, message_esc, scope, severity, recipient); - if(FAIL == DBexecute(sql)) + if(FAIL == DBexecute("insert into actions (triggerid, userid, subject, message, scope, severity, recipient) values (%d, %d, '%s', '%s', %d, %d, %d)", triggerid, userid, subject_esc, message_esc, scope, severity, recipient)) { return FAIL; } @@ -64,13 +62,11 @@ int DBget_action_by_actionid(int actionid,DB_ACTION *action) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int ret = SUCCEED; zabbix_log( LOG_LEVEL_DEBUG, "In DBget_action_by_actionid(%d)", actionid); - zbx_snprintf(sql,sizeof(sql),"select userid,recipient,subject,message from actions where actionid=%d", actionid); - result=DBselect(sql); + result = DBselect("select userid,recipient,subject,message from actions where actionid=%d", actionid); row=DBfetch(result); if(!row) diff --git a/src/libs/zbxdbhigh/db.c b/src/libs/zbxdbhigh/db.c index b7fc1f7c..f4ea9d44 100644 --- a/src/libs/zbxdbhigh/db.c +++ b/src/libs/zbxdbhigh/db.c @@ -124,7 +124,73 @@ void DBconnect(void) * Execute SQL statement. For non-select statements only. * If fails, program terminates. */ -int DBexecute(char *query) +int DBexecute(const char *fmt, ...) +{ + char sql[ZBX_MAX_SQL_LEN]; + + va_list args; +#ifdef HAVE_PGSQL + PGresult *result; +#endif +#ifdef HAVE_ORACLE + int ret; +#endif + + va_start(args, fmt); + vsnprintf(sql, ZBX_MAX_SQL_LEN-1, fmt, args); + va_end(args); + + sql[ZBX_MAX_SQL_LEN-1]='\0'; + + zabbix_log( LOG_LEVEL_DEBUG, "Executing query:%s", sql); +#ifdef HAVE_MYSQL + if(mysql_query(&mysql,sql) != 0) + { + zabbix_log( LOG_LEVEL_ERR, "Query::%s",sql); + zabbix_log(LOG_LEVEL_ERR, "Query failed:%s [%d]", mysql_error(&mysql), mysql_errno(&mysql) ); + return FAIL; + } + return (long)mysql_affected_rows(&mysql); +#endif +#ifdef HAVE_PGSQL + result = PQexec(conn,sql); + + if( result==NULL) + { + zabbix_log( LOG_LEVEL_ERR, "Query::%s",sql); + zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", "Result is NULL" ); + PQclear(result); + return FAIL; + } + if( PQresultStatus(result) != PGRES_COMMAND_OK) + { + zabbix_log( LOG_LEVEL_ERR, "Query::%s",sql); + zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", PQresStatus(PQresultStatus(result)) ); + PQclear(result); + return FAIL; + } + PQclear(result); + return SUCCEED; +#endif +#ifdef HAVE_ORACLE + if ( (ret = sqlo_exec(oracle, sql))<0 ) + { + zabbix_log( LOG_LEVEL_ERR, "Query::%s",sql); + zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", sqlo_geterror(oracle) ); + zbx_error("Query::%s.",sql); + zbx_error("Query failed:%s.", sqlo_geterror(oracle) ); + ret = FAIL; + } + return ret; +#endif +} + + +/* + * Execute SQL statement. For non-select statements only. + * If fails, program terminates. + */ +int DBexecute_old(char *query) { /* Do not include any code here. Will break HAVE_PGSQL section */ #ifdef HAVE_MYSQL @@ -217,7 +283,69 @@ DB_ROW DBfetch(DB_RESULT result) * Execute SQL statement. For select statements only. * If fails, program terminates. */ -DB_RESULT DBselect(char *query) +DB_RESULT DBselect(const char *fmt, ...) +{ + char sql[ZBX_MAX_SQL_LEN]; + + va_list args; +#ifdef HAVE_PGSQL + PGresult *result; +#endif +#ifdef HAVE_ORACLE + sqlo_stmt_handle_t sth; +#endif + + va_start(args, fmt); + vsnprintf(sql, ZBX_MAX_SQL_LEN-1, fmt, args); + va_end(args); + + sql[ZBX_MAX_SQL_LEN-1]='\0'; + + zabbix_log( LOG_LEVEL_DEBUG, "Executing query:%s", sql); + +#ifdef HAVE_MYSQL + if(mysql_query(&mysql,sql) != 0) + { + zabbix_log( LOG_LEVEL_ERR, "Query::%s",sql); + zabbix_log(LOG_LEVEL_ERR, "Query failed:%s [%d]", mysql_error(&mysql), mysql_errno(&mysql) ); + + exit(FAIL); + } + return mysql_store_result(&mysql); +#endif +#ifdef HAVE_PGSQL + result = PQexec(conn,sql); + + if( result==NULL) + { + zabbix_log( LOG_LEVEL_ERR, "Query::%s",sql); + zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", "Result is NULL" ); + exit( FAIL ); + } + if( PQresultStatus(result) != PGRES_TUPLES_OK) + { + zabbix_log( LOG_LEVEL_ERR, "Query::%s",sql); + zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", PQresStatus(PQresultStatus(result)) ); + exit( FAIL ); + } + return result; +#endif +#ifdef HAVE_ORACLE + if(0 > (sth = (sqlo_open(oracle, sql,0,NULL)))) + { + zabbix_log( LOG_LEVEL_ERR, "Query::%s",sql); + zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", sqlo_geterror(oracle)); + exit(FAIL); + } + return sth; +#endif +} + +/* + * Execute SQL statement. For select statements only. + * If fails, program terminates. + */ +DB_RESULT DBselect_old(char *query) { /* Do not include any code here. Will break HAVE_PGSQL section */ #ifdef HAVE_MYSQL @@ -272,18 +400,14 @@ DB_RESULT DBselect(char *query) */ DB_RESULT DBselectN(char *query, int n) { - char sql[MAX_STRING_LEN]; #ifdef HAVE_MYSQL - zbx_snprintf(sql,sizeof(sql),"%s limit %d", query, n); - return DBselect(sql); + return DBselect("%s limit %d", query, n); #endif #ifdef HAVE_PGSQL - zbx_snprintf(sql,sizeof(sql),"%s limit %d", query, n); - return DBselect(sql); + return DBselect("%s limit %d", query, n); #endif #ifdef HAVE_ORACLE - zbx_snprintf(sql,sizeof(sql),"select * from (%s) where rownum<=%d", query, n); - return DBselect(sql); + return DBselect("select * from (%s) where rownum<=%d", query, n); #endif } @@ -423,11 +547,8 @@ int DBget_function_result(double *result,char *functionid) DB_ROW row; int res = SUCCEED; - char sql[MAX_STRING_LEN]; - /* 0 is added to distinguish between lastvalue==NULL and empty result */ - zbx_snprintf( sql, sizeof(sql), "select 0,lastvalue from functions where functionid=%s", functionid ); - dbresult = DBselect(sql); + dbresult = DBselect("select 0,lastvalue from functions where functionid=%s", functionid ); row = DBfetch(dbresult); @@ -454,7 +575,6 @@ int DBget_function_result(double *result,char *functionid) /* Returns previous trigger value. If not value found, return TRIGGER_VALUE_FALSE */ int DBget_prev_trigger_value(int triggerid) { - char sql[MAX_STRING_LEN]; int clock; int value; @@ -463,9 +583,7 @@ int DBget_prev_trigger_value(int triggerid) zabbix_log(LOG_LEVEL_DEBUG,"In DBget_prev_trigger_value[%d]", triggerid); - zbx_snprintf(sql,sizeof(sql),"select max(clock) from alarms where triggerid=%d",triggerid); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result = DBselect(sql); + result = DBselect("select max(clock) from alarms where triggerid=%d",triggerid); row=DBfetch(result); @@ -478,9 +596,7 @@ int DBget_prev_trigger_value(int triggerid) clock=atoi(row[0]); DBfree_result(result); - zbx_snprintf(sql,sizeof(sql),"select max(clock) from alarms where triggerid=%d and clock<%d",triggerid,clock); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result = DBselect(sql); + result=DBselect("select max(clock) from alarms where triggerid=%d and clock<%d",triggerid,clock); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) @@ -495,14 +611,12 @@ status changes to TRUE for te first time */ clock=atoi(row[0]); DBfree_result(result); - zbx_snprintf(sql,sizeof(sql),"select value from alarms where triggerid=%d and clock=%d",triggerid,clock); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result = DBselect(sql); + result = DBselect("select value from alarms where triggerid=%d and clock=%d",triggerid,clock); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { - zabbix_log(LOG_LEVEL_DEBUG, "Result of [%s] is empty", sql ); + zabbix_log(LOG_LEVEL_DEBUG, "Result of SQL is empty"); DBfree_result(result); return TRIGGER_VALUE_UNKNOWN; } @@ -550,7 +664,6 @@ static int latest_alarm(int triggerid, int status) /* Rewrite required to simplify logic ?*/ int latest_service_alarm(int serviceid, int status) { - char sql[MAX_STRING_LEN]; int clock; DB_RESULT result; DB_ROW row; @@ -559,9 +672,7 @@ int latest_service_alarm(int serviceid, int status) zabbix_log(LOG_LEVEL_DEBUG,"In latest_service_alarm()"); - zbx_snprintf(sql,sizeof(sql),"select max(clock) from service_alarms where serviceid=%d",serviceid); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result = DBselect(sql); + result = DBselect("select max(clock) from service_alarms where serviceid=%d",serviceid); row = DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) @@ -574,9 +685,7 @@ int latest_service_alarm(int serviceid, int status) clock=atoi(row[0]); DBfree_result(result); - zbx_snprintf(sql,sizeof(sql),"select value from service_alarms where serviceid=%d and clock=%d",serviceid,clock); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result = DBselect(sql); + result = DBselect("select value from service_alarms where serviceid=%d and clock=%d",serviceid,clock); row = DBfetch(result); if(row && DBis_null(row[0]) != SUCCEED) { @@ -595,8 +704,6 @@ int latest_service_alarm(int serviceid, int status) /* Returns alarmid or 0 */ int add_alarm(int triggerid,int status,int clock,int *alarmid) { - char sql[MAX_STRING_LEN]; - *alarmid=0; zabbix_log(LOG_LEVEL_DEBUG,"In add_alarm(%d,%d,%d)",triggerid, status, *alarmid); @@ -608,16 +715,14 @@ int add_alarm(int triggerid,int status,int clock,int *alarmid) return FAIL; } - zbx_snprintf(sql,sizeof(sql),"insert into alarms(triggerid,clock,value) values(%d,%d,%d)", triggerid, clock, status); - DBexecute(sql); + DBexecute("insert into alarms(triggerid,clock,value) values(%d,%d,%d)", triggerid, clock, status); *alarmid=DBinsert_id(); /* Cancel currently active alerts */ if(status == TRIGGER_VALUE_FALSE || status == TRIGGER_VALUE_TRUE) { - zbx_snprintf(sql,sizeof(sql),"update alerts set retries=3,error='Trigger changed its status. WIll not send repeats.' where triggerid=%d and repeats>0 and status=%d", triggerid, ALERT_STATUS_NOT_SENT); - DBexecute(sql); + DBexecute("update alerts set retries=3,error='Trigger changed its status. WIll not send repeats.' where triggerid=%d and repeats>0 and status=%d", triggerid, ALERT_STATUS_NOT_SENT); } zabbix_log(LOG_LEVEL_DEBUG,"End of add_alarm()"); @@ -627,8 +732,6 @@ int add_alarm(int triggerid,int status,int clock,int *alarmid) int DBadd_service_alarm(int serviceid,int status,int clock) { - char sql[MAX_STRING_LEN]; - zabbix_log(LOG_LEVEL_DEBUG,"In add_service_alarm()"); if(latest_service_alarm(serviceid,status) == SUCCEED) @@ -636,9 +739,7 @@ int DBadd_service_alarm(int serviceid,int status,int clock) return SUCCEED; } - zbx_snprintf(sql,sizeof(sql),"insert into service_alarms(serviceid,clock,value) values(%d,%d,%d)", serviceid, clock, status); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - DBexecute(sql); + DBexecute("insert into service_alarms(serviceid,clock,value) values(%d,%d,%d)", serviceid, clock, status); zabbix_log(LOG_LEVEL_DEBUG,"End of add_service_alarm()"); @@ -647,7 +748,6 @@ int DBadd_service_alarm(int serviceid,int status,int clock) int DBupdate_trigger_value(DB_TRIGGER *trigger, int new_value, int now, char *reason) { - char sql[MAX_STRING_LEN]; int alarmid; int ret = SUCCEED; @@ -667,13 +767,12 @@ int DBupdate_trigger_value(DB_TRIGGER *trigger, int new_value, int now, char *re { if(reason==NULL) { - zbx_snprintf(sql,sizeof(sql),"update triggers set value=%d,lastchange=%d,error='' where triggerid=%d",new_value,now,trigger->triggerid); + DBexecute("update triggers set value=%d,lastchange=%d,error='' where triggerid=%d",new_value,now,trigger->triggerid); } else { - zbx_snprintf(sql,sizeof(sql),"update triggers set value=%d,lastchange=%d,error='%s' where triggerid=%d",new_value,now,reason, trigger->triggerid); + DBexecute("update triggers set value=%d,lastchange=%d,error='%s' where triggerid=%d",new_value,now,reason, trigger->triggerid); } - DBexecute(sql); /* It is not required and is wrong! */ /* if(TRIGGER_VALUE_UNKNOWN == new_value) { @@ -730,8 +829,6 @@ int DBupdate_trigger_value(int triggerid,int value,int clock) void update_triggers_status_to_unknown(int hostid,int clock,char *reason) { - char sql[MAX_STRING_LEN]; - DB_RESULT result; DB_ROW row; DB_TRIGGER trigger; @@ -739,9 +836,7 @@ void update_triggers_status_to_unknown(int hostid,int clock,char *reason) zabbix_log(LOG_LEVEL_DEBUG,"In update_triggers_status_to_unknown()"); /* zbx_snprintf(sql,sizeof(sql),"select distinct t.triggerid 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 h.hostid=%d and i.key_<>'%s'",hostid,SERVER_STATUS_KEY);*/ - zbx_snprintf(sql,sizeof(sql),"select distinct t.triggerid,t.value 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 h.hostid=%d and i.key_ not in ('%s','%s','%s')",hostid,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result = DBselect(sql); + result = DBselect("select distinct t.triggerid,t.value 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 h.hostid=%d and i.key_ not in ('%s','%s','%s')",hostid,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY); while((row=DBfetch(result))) { @@ -758,24 +853,18 @@ void update_triggers_status_to_unknown(int hostid,int clock,char *reason) void DBdelete_service(int serviceid) { - char sql[MAX_STRING_LEN]; - - zbx_snprintf(sql,sizeof(sql),"delete from services_links where servicedownid=%d or serviceupid=%d", serviceid, serviceid); - DBexecute(sql); - zbx_snprintf(sql,sizeof(sql),"delete from services where serviceid=%d", serviceid); - DBexecute(sql); + DBexecute("delete from services_links where servicedownid=%d or serviceupid=%d", serviceid, serviceid); + DBexecute("delete from services where serviceid=%d", serviceid); } void DBdelete_services_by_triggerid(int triggerid) { int serviceid; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG,"In DBdelete_services_by_triggerid(%d)", triggerid); - zbx_snprintf(sql,sizeof(sql),"select serviceid from services where triggerid=%d", triggerid); - result = DBselect(sql); + result = DBselect("select serviceid from services where triggerid=%d", triggerid); while((row=DBfetch(result))) { @@ -789,35 +878,26 @@ void DBdelete_services_by_triggerid(int triggerid) void DBdelete_trigger(int triggerid) { - char sql[MAX_STRING_LEN]; - - zbx_snprintf(sql,sizeof(sql),"delete from trigger_depends where triggerid_down=%d or triggerid_up=%d", triggerid, triggerid); - DBexecute(sql); - zbx_snprintf(sql,sizeof(sql),"delete from functions where triggerid=%d", triggerid); - DBexecute(sql); - zbx_snprintf(sql,sizeof(sql),"delete from alarms where triggerid=%d", triggerid); - DBexecute(sql); + DBexecute("delete from trigger_depends where triggerid_down=%d or triggerid_up=%d", triggerid, triggerid); + DBexecute("delete from functions where triggerid=%d", triggerid); + DBexecute("delete from alarms where triggerid=%d", triggerid); /* zbx_snprintf(sql,sizeof(sql),"delete from actions where triggerid=%d and scope=%d", triggerid, ACTION_SCOPE_TRIGGER); DBexecute(sql);*/ DBdelete_services_by_triggerid(triggerid); - zbx_snprintf(sql,sizeof(sql),"update sysmaps_links set triggerid=NULL where triggerid=%d", triggerid); - DBexecute(sql); - zbx_snprintf(sql,sizeof(sql),"delete from triggers where triggerid=%d", triggerid); - DBexecute(sql); + DBexecute("update sysmaps_links set triggerid=NULL where triggerid=%d", triggerid); + DBexecute("delete from triggers where triggerid=%d", triggerid); } void DBdelete_triggers_by_itemid(int itemid) { int triggerid; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG,"In DBdelete_triggers_by_itemid(%d)", itemid); - zbx_snprintf(sql,sizeof(sql),"select triggerid from functions where itemid=%d", itemid); - result = DBselect(sql); + result = DBselect("select triggerid from functions where itemid=%d", itemid); while((row=DBfetch(result))) { @@ -826,48 +906,35 @@ void DBdelete_triggers_by_itemid(int itemid) } DBfree_result(result); - zbx_snprintf(sql,sizeof(sql),"delete from functions where itemid=%d", itemid); - DBexecute(sql); + DBexecute("delete from functions where itemid=%d", itemid); zabbix_log(LOG_LEVEL_DEBUG,"End of DBdelete_triggers_by_itemid(%d)", itemid); } void DBdelete_trends_by_itemid(int itemid) { - char sql[MAX_STRING_LEN]; - - zbx_snprintf(sql,sizeof(sql),"delete from trends where itemid=%d", itemid); - DBexecute(sql); + DBexecute("delete from trends where itemid=%d", itemid); } void DBdelete_history_by_itemid(int itemid) { - char sql[MAX_STRING_LEN]; - - zbx_snprintf(sql,sizeof(sql),"delete from history where itemid=%d", itemid); - DBexecute(sql); - zbx_snprintf(sql,sizeof(sql),"delete from history_str where itemid=%d", itemid); - DBexecute(sql); + DBexecute("delete from history where itemid=%d", itemid); + DBexecute("delete from history_str where itemid=%d", itemid); } void DBdelete_sysmaps_links_by_shostid(int shostid) { - char sql[MAX_STRING_LEN]; - - zbx_snprintf(sql,sizeof(sql),"delete from sysmaps_links where shostid1=%d or shostid2=%d", shostid, shostid); - DBexecute(sql); + DBexecute("delete from sysmaps_links where shostid1=%d or shostid2=%d", shostid, shostid); } void DBdelete_sysmaps_hosts_by_hostid(int hostid) { int shostid; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG,"In DBdelete_sysmaps_hosts(%d)", hostid); - zbx_snprintf(sql,sizeof(sql),"select shostid from sysmaps_hosts where hostid=%d", hostid); - result = DBselect(sql); + result = DBselect("select shostid from sysmaps_hosts where hostid=%d", hostid); while((row=DBfetch(result))) { @@ -876,8 +943,7 @@ void DBdelete_sysmaps_hosts_by_hostid(int hostid) } DBfree_result(result); - zbx_snprintf(sql,sizeof(sql),"delete from sysmaps_hosts where hostid=%d", hostid); - DBexecute(sql); + DBexecute("delete from sysmaps_hosts where hostid=%d", hostid); } /* @@ -898,7 +964,6 @@ int DBdelete_history_pertial(int itemid) void DBupdate_triggers_status_after_restart(void) { - char sql[MAX_STRING_LEN]; int lastchange; int now; @@ -912,18 +977,14 @@ void DBupdate_triggers_status_after_restart(void) now=time(NULL); - zbx_snprintf(sql,sizeof(sql),"select distinct t.triggerid,t.value 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+i.delay<%d and i.key_<>'%s' and h.status not in (%d,%d)",now,SERVER_STATUS_KEY, HOST_STATUS_DELETED, HOST_STATUS_TEMPLATE); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result = DBselect(sql); + result = DBselect("select distinct t.triggerid,t.value 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+i.delay<%d and i.key_<>'%s' and h.status not in (%d,%d)",now,SERVER_STATUS_KEY, HOST_STATUS_DELETED, HOST_STATUS_TEMPLATE); while((row=DBfetch(result))) { trigger.triggerid=atoi(row[0]); trigger.value=atoi(row[1]); - zbx_snprintf(sql,sizeof(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.type<>%d",trigger.triggerid,ITEM_TYPE_TRAPPER); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result2 = DBselect(sql); + result2 = DBselect("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.type<>%d",trigger.triggerid,ITEM_TYPE_TRAPPER); row2=DBfetch(result2); if(!row2 || DBis_null(row2[0])==SUCCEED) { @@ -948,7 +1009,6 @@ void DBupdate_host_availability(int hostid,int available,int clock, char *error) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; char error_esc[MAX_STRING_LEN]; int disable_until; @@ -963,9 +1023,7 @@ void DBupdate_host_availability(int hostid,int available,int clock, char *error) strscpy(error_esc,""); } - zbx_snprintf(sql,sizeof(sql),"select available,disable_until from hosts where hostid=%d",hostid); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result = DBselect(sql); + result = DBselect("select available,disable_until from hosts where hostid=%d",hostid); row=DBfetch(result); if(!row) @@ -996,9 +1054,7 @@ void DBupdate_host_availability(int hostid,int available,int clock, char *error) if(available==HOST_AVAILABLE_TRUE) { - zbx_snprintf(sql,sizeof(sql),"update hosts set available=%d,error=' ',errors_from=0 where hostid=%d",HOST_AVAILABLE_TRUE,hostid); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - DBexecute(sql); + DBexecute("update hosts set available=%d,error=' ',errors_from=0 where hostid=%d",HOST_AVAILABLE_TRUE,hostid); } else if(available==HOST_AVAILABLE_FALSE) { @@ -1011,9 +1067,7 @@ void DBupdate_host_availability(int hostid,int available,int clock, char *error) zbx_snprintf(sql,sizeof(sql),"update hosts set available=%d,disable_until=%d,error='%s' where hostid=%d",HOST_AVAILABLE_FALSE,clock+CONFIG_UNREACHABLE_DELAY,error_esc,hostid); }*/ /* '%s ' - space to make Oracle happy */ - zbx_snprintf(sql,sizeof(sql),"update hosts set available=%d,error='%s ' where hostid=%d",HOST_AVAILABLE_FALSE,error_esc,hostid); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - DBexecute(sql); + DBexecute("update hosts set available=%d,error='%s ' where hostid=%d",HOST_AVAILABLE_FALSE,error_esc,hostid); } else { @@ -1030,7 +1084,6 @@ void DBupdate_host_availability(int hostid,int available,int clock, char *error) int DBupdate_item_status_to_notsupported(int itemid, char *error) { - char sql[MAX_STRING_LEN]; char error_esc[MAX_STRING_LEN]; zabbix_log(LOG_LEVEL_DEBUG,"In DBupdate_item_status_to_notsupported()"); @@ -1045,9 +1098,7 @@ int DBupdate_item_status_to_notsupported(int itemid, char *error) } /* '&s ' to make Oracle happy */ - zbx_snprintf(sql,sizeof(sql),"update items set status=%d,error='%s ' where itemid=%d",ITEM_STATUS_NOTSUPPORTED,error_esc,itemid); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - DBexecute(sql); + DBexecute("update items set status=%d,error='%s ' where itemid=%d",ITEM_STATUS_NOTSUPPORTED,error_esc,itemid); return SUCCEED; } @@ -1056,7 +1107,6 @@ int DBadd_trend(int itemid, double value, int clock) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int hour; int num; double value_min, value_avg, value_max; @@ -1065,9 +1115,7 @@ int DBadd_trend(int itemid, double value, int clock) hour=clock-clock%3600; - zbx_snprintf(sql,sizeof(sql),"select num,value_min,value_avg,value_max from trends where itemid=%d and clock=%d", itemid, hour); - zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql); - result = DBselect(sql); + result = DBselect("select num,value_min,value_avg,value_max from trends where itemid=%d and clock=%d", itemid, hour); row=DBfetch(result); @@ -1083,13 +1131,12 @@ int DBadd_trend(int itemid, double value, int clock) if(value>value_max) value_max=value; value_avg=(num*value_avg+value)/(num+1); num++; - zbx_snprintf(sql,sizeof(sql),"update trends set num=%d, value_min=%f, value_avg=%f, value_max=%f where itemid=%d and clock=%d", num, value_min, value_avg, value_max, itemid, hour); + DBexecute("update trends set num=%d, value_min=%f, value_avg=%f, value_max=%f where itemid=%d and clock=%d", num, value_min, value_avg, value_max, itemid, hour); } else { - zbx_snprintf(sql,sizeof(sql),"insert into trends (clock,itemid,num,value_min,value_avg,value_max) values (%d,%d,%d,%f,%f,%f)", hour, itemid, 1, value, value, value); + DBexecute("insert into trends (clock,itemid,num,value_min,value_avg,value_max) values (%d,%d,%d,%f,%f,%f)", hour, itemid, 1, value, value, value); } - DBexecute(sql); DBfree_result(result); @@ -1098,12 +1145,9 @@ int DBadd_trend(int itemid, double value, int clock) int DBadd_history(int itemid, double value, int clock) { - char sql[MAX_STRING_LEN]; - zabbix_log(LOG_LEVEL_DEBUG,"In add_history()"); - zbx_snprintf(sql,sizeof(sql),"insert into history (clock,itemid,value) values (%d,%d,%f)",clock,itemid,value); - DBexecute(sql); + DBexecute("insert into history (clock,itemid,value) values (%d,%d,%f)",clock,itemid,value); DBadd_trend(itemid, value, clock); @@ -1112,12 +1156,9 @@ int DBadd_history(int itemid, double value, int clock) int DBadd_history_uint(int itemid, zbx_uint64_t value, int clock) { - char sql[MAX_STRING_LEN]; - zabbix_log(LOG_LEVEL_DEBUG,"In add_history_uint()"); - zbx_snprintf(sql,sizeof(sql),"insert into history_uint (clock,itemid,value) values (%d,%d," ZBX_FS_UI64 ")",clock,itemid,value); - DBexecute(sql); + DBexecute("insert into history_uint (clock,itemid,value) values (%d,%d," ZBX_FS_UI64 ")",clock,itemid,value); DBadd_trend(itemid, (double)value, clock); @@ -1126,14 +1167,12 @@ int DBadd_history_uint(int itemid, zbx_uint64_t value, int clock) int DBadd_history_str(int itemid, char *value, int clock) { - char sql[MAX_STRING_LEN]; char value_esc[MAX_STRING_LEN]; zabbix_log(LOG_LEVEL_DEBUG,"In add_history_str()"); DBescape_string(value,value_esc,MAX_STRING_LEN); - zbx_snprintf(sql,sizeof(sql),"insert into history_str (clock,itemid,value) values (%d,%d,'%s')",clock,itemid,value_esc); - DBexecute(sql); + DBexecute("insert into history_str (clock,itemid,value) values (%d,%d,'%s')",clock,itemid,value_esc); return SUCCEED; } @@ -1227,7 +1266,6 @@ lbl_exit: #else /* HAVE_ORACLE */ - char *sql; char *value_esc; int value_esc_max_len = 0; int sql_max_len = 0; @@ -1242,19 +1280,11 @@ lbl_exit: } sql_max_len = value_esc_max_len+100; - sql = malloc(sql_max_len); - if(sql == NULL) - { - free(value_esc); - return FAIL; - } DBescape_string(value,value_esc,value_esc_max_len); - zbx_snprintf(sql,sql_max_len, "insert into history_text (clock,itemid,value) values (%d,%d,'%s')",clock,itemid,value_esc); - DBexecute(sql); + DBexecute("insert into history_text (clock,itemid,value) values (%d,%d,'%s')",clock,itemid,value_esc); free(value_esc); - free(sql); return SUCCEED; @@ -1264,7 +1294,6 @@ lbl_exit: int DBadd_history_log(int itemid, char *value, int clock, int timestamp,char *source, int severity) { - char sql[MAX_STRING_LEN]; char value_esc[MAX_STRING_LEN]; char source_esc[MAX_STRING_LEN]; @@ -1272,8 +1301,7 @@ int DBadd_history_log(int itemid, char *value, int clock, int timestamp,char *so DBescape_string(value,value_esc,MAX_STRING_LEN); DBescape_string(source,source_esc,MAX_STRING_LEN); - zbx_snprintf(sql,sizeof(sql),"insert into history_log (clock,itemid,timestamp,value,source,severity) values (%d,%d,%d,'%s','%s',%d)",clock,itemid,timestamp,value_esc,source_esc,severity); - DBexecute(sql); + DBexecute("insert into history_log (clock,itemid,timestamp,value,source,severity) values (%d,%d,%d,'%s','%s',%d)",clock,itemid,timestamp,value_esc,source_esc,severity); return SUCCEED; } @@ -1288,9 +1316,8 @@ int DBget_items_count(void) zabbix_log(LOG_LEVEL_DEBUG,"In DBget_items_count()"); - zbx_snprintf(sql,sizeof(sql),"select count(*) from items"); + result = DBselect("select count(*) from items"); - result=DBselect(sql); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) @@ -1311,22 +1338,19 @@ int DBget_items_count(void) int DBget_triggers_count(void) { int res; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG,"In DBget_triggers_count()"); - zbx_snprintf(sql,sizeof(sql),"select count(*) from triggers"); - - result=DBselect(sql); + result = DBselect("select count(*) from triggers"); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { - zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql); - zabbix_syslog("Cannot execute query [%s]", sql); + zabbix_log(LOG_LEVEL_ERR, "Cannot execute query"); + zabbix_syslog("Cannot execute query"); DBfree_result(result); return 0; } @@ -1341,21 +1365,19 @@ int DBget_triggers_count(void) int DBget_items_unsupported_count(void) { int res; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG,"In DBget_items_unsupported_count()"); - zbx_snprintf(sql,sizeof(sql),"select count(*) from items where status=%d", ITEM_STATUS_NOTSUPPORTED); + result = DBselect("select count(*) from items where status=%d", ITEM_STATUS_NOTSUPPORTED); - result=DBselect(sql); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { - zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql); - zabbix_syslog("Cannot execute query [%s]", sql); + zabbix_log(LOG_LEVEL_ERR, "Cannot execute query"); + zabbix_syslog("Cannot execute query"); DBfree_result(result); return 0; } @@ -1370,21 +1392,19 @@ int DBget_items_unsupported_count(void) int DBget_history_str_count(void) { int res; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG,"In DBget_history_str_count()"); - zbx_snprintf(sql,sizeof(sql),"select count(*) from history_str"); + result = DBselect("select count(*) from history_str"); - result=DBselect(sql); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { - zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql); - zabbix_syslog("Cannot execute query [%s]", sql); + zabbix_log(LOG_LEVEL_ERR, "Cannot execute query"); + zabbix_syslog("Cannot execute query"); DBfree_result(result); return 0; } @@ -1399,21 +1419,19 @@ int DBget_history_str_count(void) int DBget_history_count(void) { int res; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG,"In DBget_history_count()"); - zbx_snprintf(sql,sizeof(sql),"select count(*) from history"); + result = DBselect("select count(*) from history"); - result=DBselect(sql); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { - zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql); - zabbix_syslog("Cannot execute query [%s]", sql); + zabbix_log(LOG_LEVEL_ERR, "Cannot execute query"); + zabbix_syslog("Cannot execute query"); DBfree_result(result); return 0; } @@ -1428,21 +1446,19 @@ int DBget_history_count(void) int DBget_trends_count(void) { int res; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; zabbix_log(LOG_LEVEL_DEBUG,"In DBget_trends_count()"); - zbx_snprintf(sql,sizeof(sql),"select count(*) from trends"); + result = DBselect("select count(*) from trends"); - result=DBselect(sql); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { - zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql); - zabbix_syslog("Cannot execute query [%s]", sql); + zabbix_log(LOG_LEVEL_ERR, "Cannot execute query"); + zabbix_syslog("Cannot execute query"); DBfree_result(result); return 0; } @@ -1457,7 +1473,6 @@ int DBget_trends_count(void) int DBget_queue_count(void) { int res; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; int now; @@ -1466,15 +1481,14 @@ int DBget_queue_count(void) now=time(NULL); /* zbx_snprintf(sql,sizeof(sql),"select count(*) from items i,hosts h where i.status=%d and i.type not in (%d) and h.status=%d and i.hostid=h.hostid and i.nextcheck<%d and i.key_<>'status'", ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, HOST_STATUS_MONITORED, now);*/ - zbx_snprintf(sql,sizeof(sql),"select count(*) from items i,hosts h where i.status=%d and i.type not in (%d) and ((h.status=%d and h.available!=%d) or (h.status=%d and h.available=%d and h.disable_until<=%d)) and i.hostid=h.hostid and i.nextcheck<%d and i.key_ not in ('%s','%s','%s','%s')", ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, now, now, SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY, SERVER_ZABBIXLOG_KEY); + result = DBselect("select count(*) from items i,hosts h where i.status=%d and i.type not in (%d) and ((h.status=%d and h.available!=%d) or (h.status=%d and h.available=%d and h.disable_until<=%d)) and i.hostid=h.hostid and i.nextcheck<%d and i.key_ not in ('%s','%s','%s','%s')", ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, now, now, SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY, SERVER_ZABBIXLOG_KEY); - result=DBselect(sql); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { - zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql); - zabbix_syslog("Cannot execute query [%s]", sql); + zabbix_log(LOG_LEVEL_ERR, "Cannot execute query"); + zabbix_syslog("Cannot execute query"); DBfree_result(result); return 0; } @@ -1489,7 +1503,6 @@ int DBget_queue_count(void) int DBadd_alert(int actionid, int userid, int triggerid, int mediatypeid, char *sendto, char *subject, char *message, int maxrepeats, int repeatdelay) { int now; - char sql[MAX_STRING_LEN]; char sendto_esc[MAX_STRING_LEN]; char subject_esc[MAX_STRING_LEN]; char message_esc[MAX_STRING_LEN]; @@ -1502,8 +1515,7 @@ int DBadd_alert(int actionid, int userid, int triggerid, int mediatypeid, char DBescape_string(sendto,sendto_esc,MAX_STRING_LEN); DBescape_string(subject,subject_esc,MAX_STRING_LEN); DBescape_string(message,message_esc,MAX_STRING_LEN); - zbx_snprintf(sql,sizeof(sql),"insert into alerts (actionid,triggerid,userid,clock,mediatypeid,sendto,subject,message,status,retries,maxrepeats,delay) values (%d,%d,%d,%d,%d,'%s','%s','%s',0,0,%d,%d)",actionid,triggerid,userid,now,mediatypeid,sendto_esc,subject_esc,message_esc, maxrepeats, repeatdelay); - DBexecute(sql); + DBexecute("insert into alerts (actionid,triggerid,userid,clock,mediatypeid,sendto,subject,message,status,retries,maxrepeats,delay) values (%d,%d,%d,%d,%d,'%s','%s','%s',0,0,%d,%d)",actionid,triggerid,userid,now,mediatypeid,sendto_esc,subject_esc,message_esc, maxrepeats, repeatdelay); return SUCCEED; } @@ -1528,8 +1540,7 @@ void DBvacuum(void) i=0; while (NULL != (table = table_for_housekeeping[i++])) { - zbx_snprintf(sql,sizeof(sql),"vacuum analyze %s", table); - DBexecute(sql); + DBexecute("vacuum analyze %s", table); } #endif diff --git a/src/libs/zbxdbhigh/graph.c b/src/libs/zbxdbhigh/graph.c index 2ed55840..ebf88c6c 100644 --- a/src/libs/zbxdbhigh/graph.c +++ b/src/libs/zbxdbhigh/graph.c @@ -31,14 +31,12 @@ int DBadd_graph(char *name, int width, int height, int yaxistype, double yaxismin, double yaxismax) { - char sql[MAX_STRING_LEN]; int graphid; char name_esc[GRAPH_NAME_LEN_MAX]; DBescape_string(name,name_esc,GRAPH_NAME_LEN_MAX); - zbx_snprintf(sql, sizeof(sql),"insert into graphs (name,width,height,yaxistype,yaxismin,yaxismax) values ('%s',%d,%d,%d,%f,%f)", name_esc, width, height, yaxistype, yaxismin, yaxismax); - if(FAIL == DBexecute(sql)) + if(FAIL == DBexecute("insert into graphs (name,width,height,yaxistype,yaxismin,yaxismax) values ('%s',%d,%d,%d,%f,%f)", name_esc, width, height, yaxistype, yaxismin, yaxismax)) { return FAIL; } @@ -55,14 +53,12 @@ int DBadd_graph(char *name, int width, int height, int yaxistype, double yaxismi int DBadd_item_to_graph(int graphid,int itemid, char *color,int drawtype, int sortorder) { - char sql[MAX_STRING_LEN]; int gitemid; char color_esc[GRAPH_ITEM_COLOR_LEN_MAX]; DBescape_string(color,color_esc,GRAPH_ITEM_COLOR_LEN_MAX); - zbx_snprintf(sql, sizeof(sql),"insert into graphs_items (graphid,itemid,drawtype,sortorder,color) values (%d,%d,%d,%d,'%s')", graphid, itemid, drawtype, sortorder, color_esc); - if(FAIL == DBexecute(sql)) + if(FAIL == DBexecute("insert into graphs_items (graphid,itemid,drawtype,sortorder,color) values (%d,%d,%d,%d,'%s')", graphid, itemid, drawtype, sortorder, color_esc)) { return FAIL; } @@ -81,13 +77,11 @@ int DBget_graph_item_by_gitemid(int gitemid, DB_GRAPH_ITEM *graph_item) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int ret = SUCCEED; zabbix_log( LOG_LEVEL_DEBUG, "In DBget_graph_item_by_gitemid(%d)", gitemid); - zbx_snprintf(sql,sizeof(sql),"select gitemid, graphid, itemid, drawtype, sortorder, color from graphs_items where gitemid=%d", gitemid); - result=DBselect(sql); + result = DBselect("select gitemid, graphid, itemid, drawtype, sortorder, color from graphs_items where gitemid=%d", gitemid); row=DBfetch(result); if(!row) @@ -113,13 +107,11 @@ int DBget_graph_by_graphid(int graphid, DB_GRAPH *graph) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int ret = SUCCEED; zabbix_log( LOG_LEVEL_DEBUG, "In DBget_graph_by_graphid(%d)", graphid); - zbx_snprintf(sql,sizeof(sql),"select graphid,name,width,height,yaxistype,yaxismin,yaxismax from graphs where graphid=%d", graphid); - result=DBselect(sql); + result = DBselect("select graphid,name,width,height,yaxistype,yaxismin,yaxismax from graphs where graphid=%d", graphid); row=DBfetch(result); if(!row) @@ -152,7 +144,6 @@ int DBadd_graph_item_to_linked_hosts(int gitemid,int hostid) DB_RESULT result2; DB_ROW row; DB_ROW row2; - char sql[MAX_STRING_LEN]; char name_esc[GRAPH_NAME_LEN_MAX]; int graphid; int itemid; @@ -177,24 +168,19 @@ int DBadd_graph_item_to_linked_hosts(int gitemid,int hostid) if(hostid==0) { - zbx_snprintf(sql,sizeof(sql),"select hostid,templateid,graphs from hosts_templates where templateid=%d", item.hostid); + result = DBselect("select hostid,templateid,graphs from hosts_templates where templateid=%d", item.hostid); } else { - zbx_snprintf(sql,sizeof(sql),"select hostid,templateid,graphs from hosts_templates where hostid=%d and templateid=%d", hostid, item.hostid); + result = DBselect("select hostid,templateid,graphs from hosts_templates where hostid=%d and templateid=%d", hostid, item.hostid); } - zabbix_log( LOG_LEVEL_DEBUG, "\tSQL [%s]", sql); - - result=DBselect(sql); while((row=DBfetch(result))) { if( (atoi(row[2])&1) == 0) continue; - zbx_snprintf(sql,sizeof(sql),"select i.itemid from items i where i.key_='%s' and i.hostid=%d", item.key, atoi(row[0])); - zabbix_log( LOG_LEVEL_DEBUG, "\t\tSQL [%s]", sql); + result2 = DBselect("select i.itemid from items i where i.key_='%s' and i.hostid=%d", item.key, atoi(row[0])); - result2=DBselect(sql); row2=DBfetch(result2); if(!row2) @@ -210,8 +196,7 @@ int DBadd_graph_item_to_linked_hosts(int gitemid,int hostid) if(DBget_host_by_hostid(atoi(row[0]), &host) == FAIL) continue; - zbx_snprintf(sql,sizeof(sql),"select distinct g.graphid from graphs g,graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=%d and g.graphid=gi.graphid and g.name='%s'", atoi(row[0]), name_esc); - result2=DBselect(sql); + result2 = DBselect("select distinct g.graphid from graphs g,graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=%d and g.graphid=gi.graphid and g.name='%s'", atoi(row[0]), name_esc); rows=0; while((row2=DBfetch(result2))) diff --git a/src/libs/zbxdbhigh/host.c b/src/libs/zbxdbhigh/host.c index 5d2e9b7b..4e696ed9 100644 --- a/src/libs/zbxdbhigh/host.c +++ b/src/libs/zbxdbhigh/host.c @@ -31,11 +31,9 @@ int DBadd_host(char *server, int port, int status, int useip, char *ip, int disable_until, int available) { - char sql[MAX_STRING_LEN]; int hostid; - zbx_snprintf(sql, sizeof(sql),"insert into hosts (host,port,status,useip,ip,disable_until,available) values ('%s',%d,%d,%d,'%s',%d,%d)", server, port, status, useip, ip, disable_until, available); - if(FAIL == DBexecute(sql)) + if(FAIL == DBexecute("insert into hosts (host,port,status,useip,ip,disable_until,available) values ('%s',%d,%d,%d,'%s',%d,%d)", server, port, status, useip, ip, disable_until, available)) { return FAIL; } @@ -54,11 +52,9 @@ int DBhost_exists(char *server) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int ret = SUCCEED; - zbx_snprintf(sql,sizeof(sql),"select hostid from hosts where host='%s'", server); - result = DBselect(sql); + result = DBselect("select hostid from hosts where host='%s'", server); row = DBfetch(result); if(!row) @@ -74,12 +70,10 @@ int DBadd_templates_to_host(int hostid,int host_templateid) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; zabbix_log( LOG_LEVEL_DEBUG, "In DBadd_templates_to_host(%d,%d)", hostid, host_templateid); - zbx_snprintf(sql,sizeof(sql),"select templateid,items,triggers,graphs from hosts_templates where hostid=%d", host_templateid); - result = DBselect(sql); + result = DBselect("select templateid,items,triggers,graphs from hosts_templates where hostid=%d", host_templateid); while((row=DBfetch(result))) { @@ -94,25 +88,19 @@ int DBadd_templates_to_host(int hostid,int host_templateid) int DBadd_template_linkage(int hostid,int templateid,int items,int triggers,int graphs) { - char sql[MAX_STRING_LEN]; - zabbix_log( LOG_LEVEL_DEBUG, "In DBadd_template_linkage(%d)", hostid); - zbx_snprintf(sql,sizeof(sql),"insert into hosts_templates (hostid,templateid,items,triggers,graphs) values (%d,%d,%d,%d,%d)",hostid, templateid, items, triggers, graphs); - - return DBexecute(sql); + return DBexecute("insert into hosts_templates (hostid,templateid,items,triggers,graphs) values (%d,%d,%d,%d,%d)",hostid, templateid, items, triggers, graphs); } int DBsync_host_with_templates(int hostid) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; zabbix_log( LOG_LEVEL_DEBUG, "In DBsync_host_with_templates(%d)", hostid); - zbx_snprintf(sql,sizeof(sql),"select templateid,items,triggers,graphs from hosts_templates where hostid=%d", hostid); - result = DBselect(sql); + result = DBselect("select templateid,items,triggers,graphs from hosts_templates where hostid=%d", hostid); while((row=DBfetch(result))) { @@ -129,13 +117,11 @@ int DBsync_host_with_template(int hostid,int templateid,int items,int triggers,i { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; zabbix_log( LOG_LEVEL_DEBUG, "In DBsync_host_with_template(%d,%d)", hostid, templateid); /* Sync items */ - zbx_snprintf(sql,sizeof(sql),"select itemid from items where hostid=%d", templateid); - result = DBselect(sql); + result = DBselect("select itemid from items where hostid=%d", templateid); while((row=DBfetch(result))) { @@ -144,8 +130,7 @@ int DBsync_host_with_template(int hostid,int templateid,int items,int triggers,i DBfree_result(result); /* Sync triggers */ - zbx_snprintf(sql,sizeof(sql),"select distinct t.triggerid from hosts h, items i,triggers t,functions f where h.hostid=%d and h.hostid=i.hostid and t.triggerid=f.triggerid and i.itemid=f.itemid", templateid); - result = DBselect(sql); + result = DBselect("select distinct t.triggerid from hosts h, items i,triggers t,functions f where h.hostid=%d and h.hostid=i.hostid and t.triggerid=f.triggerid and i.itemid=f.itemid", templateid); while((row=DBfetch(result))) { DBadd_trigger_to_linked_hosts(atoi(row[0]),hostid); @@ -153,8 +138,7 @@ int DBsync_host_with_template(int hostid,int templateid,int items,int triggers,i DBfree_result(result); /* Sync graphs */ - zbx_snprintf(sql,sizeof(sql),"select distinct gi.gitemid from graphs g,graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=%d and g.graphid=gi.graphid", templateid); - result = DBselect(sql); + result = DBselect("select distinct gi.gitemid from graphs g,graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=%d and g.graphid=gi.graphid", templateid); while((row=DBfetch(result))) { DBadd_graph_item_to_linked_hosts(atoi(row[0]),hostid); @@ -168,13 +152,11 @@ int DBget_host_by_hostid(int hostid,DB_HOST *host) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int ret = SUCCEED; zabbix_log( LOG_LEVEL_DEBUG, "In DBget_host_by_hostid(%d)", hostid); - zbx_snprintf(sql,sizeof(sql),"select hostid,host,useip,ip,port,status,disable_until,errors_from,error,available from hosts where hostid=%d", hostid); - result=DBselect(sql); + result = DBselect("select hostid,host,useip,ip,port,status,disable_until,errors_from,error,available from hosts where hostid=%d", hostid); row=DBfetch(result); if(!row) diff --git a/src/libs/zbxdbhigh/item.c b/src/libs/zbxdbhigh/item.c index 4b78b7e6..53b56b60 100644 --- a/src/libs/zbxdbhigh/item.c +++ b/src/libs/zbxdbhigh/item.c @@ -33,13 +33,11 @@ int DBget_item_by_itemid(int itemid,DB_ITEM *item) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int ret = SUCCEED; zabbix_log( LOG_LEVEL_DEBUG, "In DBget_item_by_itemid(%d)", itemid); - zbx_snprintf(sql,sizeof(sql),"select i.itemid,i.key_,h.hostid from items i,hosts h where h.hostid=i.hostid and i.itemid=%d", itemid); - result=DBselect(sql); + result = DBselect("select i.itemid,i.key_,h.hostid from items i,hosts h where h.hostid=i.hostid and i.itemid=%d", itemid); row = DBfetch(result); if(!row) @@ -67,12 +65,10 @@ int DBadd_item_to_linked_hosts(int itemid, int hostid) DB_ROW row2; DB_RESULT result3; DB_ROW row3; - char sql[MAX_STRING_LEN]; zabbix_log( LOG_LEVEL_DEBUG, "In DBadd_item_to_linked_hosts(%d,%d)", itemid, hostid); - zbx_snprintf(sql,sizeof(sql),"select description,key_,hostid,delay,history,status,type,snmp_community,snmp_oid,value_type,trapper_hosts,snmp_port,units,multiplier,delta,snmpv3_securityname,snmpv3_securitylevel,snmpv3_authpassphrase,snmpv3_privpassphrase,formula,trends,logtimefmt from items where itemid=%d", itemid); - result3=DBselect(sql); + result3 = DBselect("select description,key_,hostid,delay,history,status,type,snmp_community,snmp_oid,value_type,trapper_hosts,snmp_port,units,multiplier,delta,snmpv3_securityname,snmpv3_securitylevel,snmpv3_authpassphrase,snmpv3_privpassphrase,formula,trends,logtimefmt from items where itemid=%d", itemid); row3=DBfetch(result3); @@ -110,20 +106,18 @@ int DBadd_item_to_linked_hosts(int itemid, int hostid) /* Link with one host only */ if(hostid!=0) { - zbx_snprintf(sql,sizeof(sql),"select hostid,templateid,items from hosts_templates where hostid=%d and templateid=%d", hostid, item.hostid); + result = DBselect("select hostid,templateid,items from hosts_templates where hostid=%d and templateid=%d", hostid, item.hostid); } else { - zbx_snprintf(sql,sizeof(sql),"select hostid,templateid,items from hosts_templates where templateid=%d", item.hostid); + result = DBselect("select hostid,templateid,items from hosts_templates where templateid=%d", item.hostid); } - result = DBselect(sql); while((row=DBfetch(result))) { if( (atoi(row[2])&1) == 0) continue; - zbx_snprintf(sql,sizeof(sql),"select itemid from items where key_='%s' and hostid=%d", item.key, atoi(row[0])); - result2=DBselect(sql); + result2 = DBselect("select itemid from items where key_='%s' and hostid=%d", item.key, atoi(row[0])); row2=DBfetch(result2); if(!row2) { @@ -140,7 +134,6 @@ int DBadd_item_to_linked_hosts(int itemid, int hostid) int DBadd_item(char *description, char *key, int hostid, int delay, int history, int status, int type, char *snmp_community, char *snmp_oid,int value_type,char *trapper_hosts,int snmp_port,char *units,int multiplier,int delta, char *snmpv3_securityname,int snmpv3_securitylevel,char *snmpv3_authpassphrase,char *snmpv3_privpassphrase,char *formula,int trends,char *logtimefmt) { - char sql[MAX_STRING_LEN]; char key_esc[MAX_STRING_LEN]; char description_esc[MAX_STRING_LEN]; char logtimefmt_esc[MAX_STRING_LEN]; @@ -157,7 +150,5 @@ int DBadd_item(char *description, char *key, int hostid, int delay, int history, DBescape_string(snmpv3_authpassphrase,snmpv3_authpassphrase_esc,MAX_STRING_LEN); DBescape_string(snmpv3_privpassphrase,snmpv3_privpassphrase_esc,MAX_STRING_LEN); - zbx_snprintf(sql,sizeof(sql),"insert into items (description,key_,hostid,delay,history,nextcheck,status,type,snmp_community,snmp_oid,value_type,trapper_hosts,snmp_port,units,multiplier,delta,snmpv3_securityname,snmpv3_securitylevel,snmpv3_authpassphrase,snmpv3_privpassphrase,formula,trends,logtimefmt) values ('%s','%s',%d,%d,%d,0,%d,%d,'%s','%s',%d,'%s',%d,'%s',%d,%d,'%s',%d,'%s','%s','%s',%d,'%s')", description_esc, key_esc, hostid,delay,history,status,type,snmp_community,snmp_oid,value_type,trapper_hosts,snmp_port,units,multiplier,delta,snmpv3_securityname_esc,snmpv3_securitylevel,snmpv3_authpassphrase_esc,snmpv3_privpassphrase_esc,formula,trends,logtimefmt_esc); - - return DBexecute(sql); + return DBexecute("insert into items (description,key_,hostid,delay,history,nextcheck,status,type,snmp_community,snmp_oid,value_type,trapper_hosts,snmp_port,units,multiplier,delta,snmpv3_securityname,snmpv3_securitylevel,snmpv3_authpassphrase,snmpv3_privpassphrase,formula,trends,logtimefmt) values ('%s','%s',%d,%d,%d,0,%d,%d,'%s','%s',%d,'%s',%d,'%s',%d,%d,'%s',%d,'%s','%s','%s',%d,'%s')", description_esc, key_esc, hostid,delay,history,status,type,snmp_community,snmp_oid,value_type,trapper_hosts,snmp_port,units,multiplier,delta,snmpv3_securityname_esc,snmpv3_securitylevel,snmpv3_authpassphrase_esc,snmpv3_privpassphrase_esc,formula,trends,logtimefmt_esc); } diff --git a/src/libs/zbxdbhigh/trigger.c b/src/libs/zbxdbhigh/trigger.c index 8007532e..8b77ffd1 100644 --- a/src/libs/zbxdbhigh/trigger.c +++ b/src/libs/zbxdbhigh/trigger.c @@ -38,7 +38,6 @@ int DBadd_trigger_to_linked_hosts(int triggerid,int hostid) DB_ROW row; DB_ROW row2; DB_ROW row3; - char sql[MAX_STRING_LEN]; char old[MAX_STRING_LEN]; char new[MAX_STRING_LEN]; int functionid, triggerid_new; @@ -50,8 +49,7 @@ int DBadd_trigger_to_linked_hosts(int triggerid,int hostid) zabbix_log( LOG_LEVEL_DEBUG, "In DBadd_trigger_to_linked_hosts(%d,%d)",triggerid, hostid); - zbx_snprintf(sql,sizeof(sql),"select description, priority,status,comments,url,value,expression from triggers where triggerid=%d", triggerid); - result2=DBselect(sql); + result2 = DBselect("select description, priority,status,comments,url,value,expression from triggers where triggerid=%d", triggerid); row2=DBfetch(result2); if(!row2) @@ -72,10 +70,9 @@ int DBadd_trigger_to_linked_hosts(int triggerid,int hostid) DBfree_result(result2); - zbx_snprintf(sql,sizeof(sql),"select distinct h.hostid from hosts h,functions f, items i where i.itemid=f.itemid and h.hostid=i.hostid and f.triggerid=%d", triggerid); - result=DBselect(sql); + result2 = DBselect("select distinct h.hostid from hosts h,functions f, items i where i.itemid=f.itemid and h.hostid=i.hostid and f.triggerid=%d", triggerid); - row=DBfetch(result); + row=DBfetch(result2); if(!row) { @@ -84,16 +81,14 @@ int DBadd_trigger_to_linked_hosts(int triggerid,int hostid) if(hostid==0) { - zbx_snprintf(sql,sizeof(sql),"select hostid,templateid,triggers from hosts_templates where templateid=%d", atoi(row[0])); + result = DBselect("select hostid,templateid,triggers from hosts_templates where templateid=%d", atoi(row[0])); } /* Link to one host only */ else { - zbx_snprintf(sql,sizeof(sql),"select hostid,templateid,triggers from hosts_templates where hostid=%d and templateid=%d", hostid, atoi(row[0])); + result = DBselect("select hostid,templateid,triggers from hosts_templates where hostid=%d and templateid=%d", hostid, atoi(row[0])); } - DBfree_result(result); - - result=DBselect(sql); + DBfree_result(result2); /* Loop: linked hosts */ while((row=DBfetch(result))) @@ -107,38 +102,30 @@ int DBadd_trigger_to_linked_hosts(int triggerid,int hostid) DBescape_string(trigger.comments,comments_esc,TRIGGER_COMMENTS_LEN_MAX); DBescape_string(trigger.url,url_esc,TRIGGER_URL_LEN_MAX); - zbx_snprintf(sql,sizeof(sql),"insert into triggers (description,priority,status,comments,url,value,expression) values ('%s',%d,%d,'%s','%s',2,'%s')",description_esc, trigger.priority, trigger.status, comments_esc, url_esc, expression_old); - zabbix_log( LOG_LEVEL_DEBUG, "SQL [%s]",sql); + DBexecute("insert into triggers (description,priority,status,comments,url,value,expression) values ('%s',%d,%d,'%s','%s',2,'%s')",description_esc, trigger.priority, trigger.status, comments_esc, url_esc, expression_old); - DBexecute(sql); triggerid_new=DBinsert_id(); - zbx_snprintf(sql,sizeof(sql),"select i.key_,f.parameter,f.function,f.functionid from functions f,items i where i.itemid=f.itemid and f.triggerid=%d", triggerid); - result2=DBselect(sql); + result2 = DBselect("select i.key_,f.parameter,f.function,f.functionid from functions f,items i where i.itemid=f.itemid and f.triggerid=%d", triggerid); /* Loop: functions */ while((row2=DBfetch(result2))) { - zbx_snprintf(sql,sizeof(sql),"select itemid from items where key_='%s' and hostid=%d", row2[0], atoi(row[0])); - result3=DBselect(sql); + result3 = DBselect("select itemid from items where key_='%s' and hostid=%d", row2[0], atoi(row[0])); row3=DBfetch(result3); if(!row3) { DBfree_result(result3); - zbx_snprintf(sql,sizeof(sql),"delete from triggers where triggerid=%d", triggerid_new); - DBexecute(sql); - zbx_snprintf(sql,sizeof(sql),"delete from functions where triggerid=%d", triggerid_new); - DBexecute(sql); + DBexecute("delete from triggers where triggerid=%d", triggerid_new); + DBexecute("delete from functions where triggerid=%d", triggerid_new); break; } - zbx_snprintf(sql,sizeof(sql),"insert into functions (itemid,triggerid,function,parameter) values (%d,%d,'%s','%s')", atoi(row3[0]), triggerid_new, row2[2], row2[1]); + DBexecute("insert into functions (itemid,triggerid,function,parameter) values (%d,%d,'%s','%s')", atoi(row3[0]), triggerid_new, row2[2], row2[1]); - DBexecute(sql); functionid=DBinsert_id(); - zbx_snprintf(sql,sizeof(sql),"update triggers set expression='%s' where triggerid=%d", expression_old, triggerid_new ); - DBexecute(sql); + DBexecute("update triggers set expression='%s' where triggerid=%d", expression_old, triggerid_new ); zbx_snprintf(old, sizeof(old),"{%d}", atoi(row2[3])); zbx_snprintf(new, sizeof(new),"{%d}", functionid); @@ -148,11 +135,9 @@ int DBadd_trigger_to_linked_hosts(int triggerid,int hostid) strscpy(expression_old, expression); - zbx_snprintf(sql,sizeof(sql),"update triggers set expression='%s' where triggerid=%d", expression, triggerid_new ); + DBexecute("update triggers set expression='%s' where triggerid=%d", expression, triggerid_new ); free(expression); - DBexecute(sql); - DBfree_result(result3); } DBfree_result(result2); @@ -183,13 +168,11 @@ int DBget_trigger_by_triggerid(int triggerid,DB_TRIGGER *trigger) { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int ret = SUCCEED; zabbix_log( LOG_LEVEL_DEBUG, "In DBget_trigger_by_triggerid(%d)", triggerid); - zbx_snprintf(sql,sizeof(sql),"select triggerid, expression,description,url,comments,status,value,priority from triggers where triggerid=%d", triggerid); - result=DBselect(sql); + result = DBselect("select triggerid, expression,description,url,comments,status,value,priority from triggers where triggerid=%d", triggerid); row=DBfetch(result); if(!row) diff --git a/src/zabbix_agent/zabbix_agentd.c b/src/zabbix_agent/zabbix_agentd.c index 8ada7c96..ec212d2c 100644 --- a/src/zabbix_agent/zabbix_agentd.c +++ b/src/zabbix_agent/zabbix_agentd.c @@ -457,8 +457,7 @@ int main() /* #elif OFF or ON - Place your test code HERE!!! - +Place your test code HERE!!! */ #endif /* 0 */ diff --git a/src/zabbix_server/actions.c b/src/zabbix_server/actions.c index 4cfbb3d9..535ac3bd 100644 --- a/src/zabbix_server/actions.c +++ b/src/zabbix_server/actions.c @@ -131,12 +131,10 @@ static int check_time_period(const char *period) static void send_to_user_medias(DB_TRIGGER *trigger,DB_ACTION *action, int userid) { DB_MEDIA media; - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; - zbx_snprintf(sql,sizeof(sql),"select mediatypeid,sendto,active,severity,period from media where active=%d and userid=%d",MEDIA_STATUS_ACTIVE,userid); - result = DBselect(sql); + result = DBselect("select mediatypeid,sendto,active,severity,period from media where active=%d and userid=%d",MEDIA_STATUS_ACTIVE,userid); while((row=DBfetch(result))) { @@ -181,7 +179,6 @@ static void send_to_user_medias(DB_TRIGGER *trigger,DB_ACTION *action, int useri ******************************************************************************/ static void send_to_user(DB_TRIGGER *trigger,DB_ACTION *action) { - char sql[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; @@ -191,8 +188,7 @@ static void send_to_user(DB_TRIGGER *trigger,DB_ACTION *action) } else if(action->recipient == RECIPIENT_TYPE_GROUP) { - zbx_snprintf(sql,sizeof(sql),"select u.userid from users u, users_groups ug where ug.usrgrpid=%d and ug.userid=u.userid", action->userid); - result = DBselect(sql); + result = DBselect("select u.userid from users u, users_groups ug where ug.usrgrpid=%d and ug.userid=u.userid", action->userid); while((row=DBfetch(result))) { send_to_user_medias(trigger, action, atoi(row[0])); @@ -233,15 +229,12 @@ static void run_remote_command(char* host_name, char* command) DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; - assert(host_name); assert(command); zabbix_log(LOG_LEVEL_DEBUG, "run_remote_command START [hostname: '%s', command: '%s']", host_name, command); - zbx_snprintf(sql,sizeof(sql),"select distinct host,ip,useip,port from hosts where host='%s'", host_name); - result = DBselect(sql); + result = DBselect("select distinct host,ip,useip,port from hosts where host='%s'", host_name); row = DBfetch(result); if(row) { @@ -371,7 +364,6 @@ static int get_next_command(char** command_list, char** alias, int* is_group, ch DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; char *cmd_list = NULL; char *alias = NULL; char *command = NULL; @@ -388,8 +380,7 @@ static int get_next_command(char** command_list, char** alias, int* is_group, ch if(alias == '\0' || command == '\0') continue; if(is_group) { - zbx_snprintf(sql,sizeof(sql),"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'", alias); - result = DBselect(sql); + 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'", alias); while((row=DBfetch(result))) { run_remote_command(row[0], command); @@ -410,7 +401,6 @@ static int check_action_condition(DB_TRIGGER *trigger,int alarmid,int new_trigge { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int ret = FAIL; @@ -418,8 +408,7 @@ static int check_action_condition(DB_TRIGGER *trigger,int alarmid,int new_trigge if(condition->conditiontype == CONDITION_TYPE_HOST_GROUP) { - zbx_snprintf(sql,sizeof(sql),"select distinct hg.groupid from hosts_groups hg,hosts h, items i, functions f, triggers t where hg.hostid=h.hostid and h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and t.triggerid=%d", trigger->triggerid); - result = DBselect(sql); + result = DBselect("select distinct hg.groupid from hosts_groups hg,hosts h, items i, functions f, triggers t where hg.hostid=h.hostid and h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and t.triggerid=%d", trigger->triggerid); while((row=DBfetch(result))) { if(condition->operator == CONDITION_OPERATOR_EQUAL) @@ -448,8 +437,7 @@ static int check_action_condition(DB_TRIGGER *trigger,int alarmid,int new_trigge } else if(condition->conditiontype == CONDITION_TYPE_HOST) { - zbx_snprintf(sql,sizeof(sql),"select distinct h.hostid from hosts h, items i, functions f, triggers t where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and t.triggerid=%d", trigger->triggerid); - result = DBselect(sql); + result = DBselect("select distinct h.hostid from hosts h, items i, functions f, triggers t where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and t.triggerid=%d", trigger->triggerid); while((row=DBfetch(result))) { if(condition->operator == CONDITION_OPERATOR_EQUAL) @@ -596,7 +584,6 @@ static int check_action_conditions(DB_TRIGGER *trigger,int alarmid,int new_trigg { DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; DB_CONDITION condition; @@ -605,8 +592,7 @@ static int check_action_conditions(DB_TRIGGER *trigger,int alarmid,int new_trigg zabbix_log( LOG_LEVEL_DEBUG, "In check_action_conditions [actionid:%d]", actionid); - zbx_snprintf(sql,sizeof(sql),"select conditionid,actionid,conditiontype,operator,value from conditions where actionid=%d order by conditiontype", actionid); - result = DBselect(sql); + result = DBselect("select conditionid,actionid,conditiontype,operator,value from conditions where actionid=%d order by conditiontype", actionid); while((row=DBfetch(result))) { @@ -654,8 +640,6 @@ void apply_actions(DB_TRIGGER *trigger,int alarmid,int trigger_value) DB_ACTION action; - char sql[MAX_STRING_LEN]; - /* int now;*/ zabbix_log( LOG_LEVEL_DEBUG, "In apply_actions(triggerid:%d,alarmid:%d,trigger_value:%d)",trigger->triggerid, alarmid, trigger_value); @@ -664,8 +648,7 @@ void apply_actions(DB_TRIGGER *trigger,int alarmid,int trigger_value) { zabbix_log( LOG_LEVEL_DEBUG, "Check dependencies"); - zbx_snprintf(sql,sizeof(sql),"select count(*) from trigger_depends d,triggers t where d.triggerid_down=%d and d.triggerid_up=t.triggerid and t.value=%d",trigger->triggerid, TRIGGER_VALUE_TRUE); - result = DBselect(sql); + result = DBselect("select count(*) from trigger_depends d,triggers t where d.triggerid_down=%d and d.triggerid_up=t.triggerid and t.value=%d",trigger->triggerid, TRIGGER_VALUE_TRUE); row=DBfetch(result); if(row && DBis_null(row[0]) != SUCCEED) { @@ -687,9 +670,7 @@ void apply_actions(DB_TRIGGER *trigger,int alarmid,int trigger_value) /* zbx_snprintf(sql,sizeof(sql),"select actionid,userid,delay,subject,message,recipient,maxrepeats,repeatdelay,scripts,actiontype from actions where nextcheck<=%d and status=%d", now, ACTION_STATUS_ACTIVE);*/ /* No support of action delay anymore */ - zbx_snprintf(sql,sizeof(sql),"select actionid,userid,subject,message,recipient,maxrepeats,repeatdelay,scripts,actiontype from actions where status=%d", ACTION_STATUS_ACTIVE); - result = DBselect(sql); - zabbix_log( LOG_LEVEL_DEBUG, "SQL [%s]", sql); + result = DBselect("select actionid,userid,subject,message,recipient,maxrepeats,repeatdelay,scripts,actiontype from actions where status=%d", ACTION_STATUS_ACTIVE); while((row=DBfetch(result))) { diff --git a/src/zabbix_server/alerter/alerter.c b/src/zabbix_server/alerter/alerter.c index 54c854a7..472fba96 100644 --- a/src/zabbix_server/alerter/alerter.c +++ b/src/zabbix_server/alerter/alerter.c @@ -155,7 +155,6 @@ static int execute_action(DB_ALERT *alert,DB_MEDIATYPE *mediatype, char *error, ******************************************************************************/ int main_alerter_loop() { - char sql[MAX_STRING_LEN]; char error[MAX_STRING_LEN]; char error_esc[MAX_STRING_LEN]; @@ -178,8 +177,7 @@ int main_alerter_loop() now = time(NULL); /* zbx_snprintf(sql,sizeof(sql),"select a.alertid,a.mediatypeid,a.sendto,a.subject,a.message,a.status,a.retries,mt.mediatypeid,mt.type,mt.description,mt.smtp_server,mt.smtp_helo,mt.smtp_email,mt.exec_path from alerts a,media_type mt where a.status=0 and a.retries<3 and a.mediatypeid=mt.mediatypeid order by a.clock"); */ - zbx_snprintf(sql,sizeof(sql),"select a.alertid,a.mediatypeid,a.sendto,a.subject,a.message,a.status,a.retries,mt.mediatypeid,mt.type,mt.description,mt.smtp_server,mt.smtp_helo,mt.smtp_email,mt.exec_path,a.delay,mt.gsm_modem from alerts a,media_type mt where a.status=%d and a.retries<3 and (a.repeats<a.maxrepeats or a.maxrepeats=0) and a.nextcheck<=%d and a.mediatypeid=mt.mediatypeid order by a.clock", ALERT_STATUS_NOT_SENT, now); - result = DBselect(sql); + result = DBselect("select a.alertid,a.mediatypeid,a.sendto,a.subject,a.message,a.status,a.retries,mt.mediatypeid,mt.type,mt.description,mt.smtp_server,mt.smtp_helo,mt.smtp_email,mt.exec_path,a.delay,mt.gsm_modem from alerts a,media_type mt where a.status=%d and a.retries<3 and (a.repeats<a.maxrepeats or a.maxrepeats=0) and a.nextcheck<=%d and a.mediatypeid=mt.mediatypeid order by a.clock", ALERT_STATUS_NOT_SENT, now); while((row=DBfetch(result))) { @@ -217,18 +215,15 @@ int main_alerter_loop() if(res==SUCCEED) { zabbix_log( LOG_LEVEL_DEBUG, "Alert ID [%d] was sent successfully", alert.alertid); - zbx_snprintf(sql,sizeof(sql),"update alerts set repeats=repeats+1, nextcheck=%d where alertid=%d", now+alert.delay, alert.alertid); - DBexecute(sql); - zbx_snprintf(sql,sizeof(sql),"update alerts set status=%d where alertid=%d and repeats>=maxrepeats and status=%d and retries<3", ALERT_STATUS_SENT, alert.alertid, ALERT_STATUS_NOT_SENT); - DBexecute(sql); + DBexecute("update alerts set repeats=repeats+1, nextcheck=%d where alertid=%d", now+alert.delay, alert.alertid); + DBexecute("update alerts set status=%d where alertid=%d and repeats>=maxrepeats and status=%d and retries<3", ALERT_STATUS_SENT, alert.alertid, ALERT_STATUS_NOT_SENT); } else { zabbix_log( LOG_LEVEL_DEBUG, "Error sending alert ID [%d]", alert.alertid); zabbix_syslog("Error sending alert ID [%d]", alert.alertid); DBescape_string(error,error_esc,MAX_STRING_LEN); - zbx_snprintf(sql,sizeof(sql),"update alerts set retries=retries+1,error='%s' where alertid=%d", error_esc, alert.alertid); - DBexecute(sql); + DBexecute("update alerts set retries=retries+1,error='%s' where alertid=%d", error_esc, alert.alertid); } } diff --git a/src/zabbix_server/evalfunc.c b/src/zabbix_server/evalfunc.c index 44f0100d..679e92b0 100644 --- a/src/zabbix_server/evalfunc.c +++ b/src/zabbix_server/evalfunc.c @@ -178,7 +178,6 @@ static int evaluate_COUNT(char *value, DB_ITEM *item, int parameter) DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; char table[MAX_STRING_LEN]; int now; int res = SUCCEED; @@ -198,9 +197,8 @@ static int evaluate_COUNT(char *value, DB_ITEM *item, int parameter) { strscpy(table,"history"); } - zbx_snprintf(sql,sizeof(sql),"select count(value) from %s where clock>%d and itemid=%d",table,now-parameter,item->itemid); + result = DBselect("select count(value) from %s where clock>%d and itemid=%d",table,now-parameter,item->itemid); - result = DBselect(sql); row = DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) @@ -263,9 +261,8 @@ static int evaluate_SUM(char *value, DB_ITEM *item, int parameter, int flag) { strscpy(table,"history"); } - zbx_snprintf(sql,sizeof(sql),"select sum(value) from %s where clock>%d and itemid=%d",table, now-parameter,item->itemid); + result = DBselect("select sum(value) from %s where clock>%d and itemid=%d",table, now-parameter,item->itemid); - result = DBselect(sql); row = DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { @@ -362,9 +359,8 @@ static int evaluate_AVG(char *value,DB_ITEM *item,int parameter,int flag) if(flag == ZBX_FLAG_SEC) { - zbx_snprintf(sql,sizeof(sql),"select avg(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); + result = DBselect("select avg(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); - result = DBselect(sql); row = DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) @@ -460,8 +456,7 @@ static int evaluate_MIN(char *value,DB_ITEM *item,int parameter, int flag) { strscpy(table,"history"); } - zbx_snprintf(sql,sizeof(sql),"select min(value) from %s where clock>%d and itemid=%d",table, now-parameter,item->itemid); - result = DBselect(sql); + result = DBselect("select min(value) from %s where clock>%d and itemid=%d",table, now-parameter,item->itemid); row = DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { @@ -589,13 +584,9 @@ static int evaluate_MAX(char *value,DB_ITEM *item,int parameter,int flag) { strscpy(table,"history"); } - zbx_snprintf(sql,sizeof(sql),"select max(value) from %s where clock>%d and itemid=%d",table,now-parameter,item->itemid); + result = DBselect("select max(value) from %s where clock>%d and itemid=%d",table,now-parameter,item->itemid); -zabbix_log(LOG_LEVEL_DEBUG, "DBselect" ); - result = DBselect(sql); -zabbix_log(LOG_LEVEL_DEBUG, "DBfetch" ); row = DBfetch(result); -zabbix_log(LOG_LEVEL_DEBUG, "After DBfetch" ); if(!row || DBis_null(row[0])==SUCCEED) { @@ -604,9 +595,7 @@ zabbix_log(LOG_LEVEL_DEBUG, "After DBfetch" ); } else { -zabbix_log(LOG_LEVEL_DEBUG, "strcpy '0x%4x'",row[0]); strcpy(value,row[0]); -zabbix_log(LOG_LEVEL_DEBUG, "del_zeroes" ); del_zeroes(value); } } @@ -713,9 +702,8 @@ static int evaluate_DELTA(char *value,DB_ITEM *item,int parameter, int flag) if(flag == ZBX_FLAG_SEC) { - zbx_snprintf(sql,sizeof(sql),"select max(value)-min(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); + result = DBselect("select max(value)-min(value) from history where clock>%d and itemid=%d",now-parameter,item->itemid); - result = DBselect(sql); row = DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) { @@ -1227,9 +1215,8 @@ int replace_value_by_map(char *value, int valuemapid) if(valuemapid == 0) return FAIL; - zbx_snprintf(sql,sizeof(sql),"select newvalue from mappings where valuemapid=%d and value='%s'", + result = DBselect("select newvalue from mappings where valuemapid=%d and value='%s'", valuemapid, value); - result = DBselect(sql); row = DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) return FAIL; @@ -1272,21 +1259,19 @@ int evaluate_FUNCTION2(char *value,char *host,char *key,char *function,char *par DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; int res; zabbix_log(LOG_LEVEL_DEBUG, "In evaluate_FUNCTION2()" ); - zbx_snprintf(sql,sizeof(sql),"select %s where h.host='%s' and h.hostid=i.hostid and i.key_='%s'", ZBX_SQL_ITEM_SELECT, host, key ); - result = DBselect(sql); + result = DBselect("select %s where h.host='%s' and h.hostid=i.hostid and i.key_='%s'", ZBX_SQL_ITEM_SELECT, host, key ); row = DBfetch(result); if(!row) { DBfree_result(result); - zabbix_log(LOG_LEVEL_WARNING, "Query [%s] returned empty result", sql ); - zabbix_syslog("Query [%s] returned empty result", sql ); + zabbix_log(LOG_LEVEL_WARNING, "Query returned empty result"); + zabbix_syslog("Query returned empty result"); return FAIL; } diff --git a/src/zabbix_server/expression.c b/src/zabbix_server/expression.c index 5e5e2237..bafa05bd 100644 --- a/src/zabbix_server/expression.c +++ b/src/zabbix_server/expression.c @@ -562,7 +562,6 @@ void substitute_simple_macros(DB_TRIGGER *trigger, DB_ACTION *action, char *data { int found = SUCCEED; char *s; - char sql[MAX_STRING_LEN]; char str[MAX_STRING_LEN]; char tmp[MAX_STRING_LEN]; @@ -589,8 +588,7 @@ void substitute_simple_macros(DB_TRIGGER *trigger, DB_ACTION *action, char *data else if( (s = strstr(str,"{HOSTNAME}")) != NULL ) { /* zbx_snprintf(sql,sizeof(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", trigger->triggerid);*/ - zbx_snprintf(sql,sizeof(sql),"select distinct 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", trigger->triggerid); - result = DBselect(sql); + result = DBselect("select distinct 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", trigger->triggerid); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) @@ -614,8 +612,7 @@ void substitute_simple_macros(DB_TRIGGER *trigger, DB_ACTION *action, char *data } else if( (s = strstr(str,"{TRIGGER.KEY}")) != NULL ) { - zbx_snprintf(sql,sizeof(sql),"select distinct i.key_ 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 order by i.key_", trigger->triggerid); - result = DBselect(sql); + result = DBselect("select distinct i.key_ 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 order by i.key_", trigger->triggerid); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) @@ -639,8 +636,7 @@ void substitute_simple_macros(DB_TRIGGER *trigger, DB_ACTION *action, char *data } else if( (s = strstr(str,"{IPADDRESS}")) != NULL ) { - zbx_snprintf(sql,sizeof(sql),"select distinct h.ip 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 and h.useip=1", trigger->triggerid); - result = DBselect(sql); + result = DBselect("select distinct h.ip 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 and h.useip=1", trigger->triggerid); row = DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED) diff --git a/src/zabbix_server/functions.c b/src/zabbix_server/functions.c index 2ce902bf..f5942772 100644 --- a/src/zabbix_server/functions.c +++ b/src/zabbix_server/functions.c @@ -72,7 +72,6 @@ void update_functions(DB_ITEM *item) DB_FUNCTION function; DB_RESULT result; DB_ROW row; - char sql[MAX_STRING_LEN]; char value[MAX_STRING_LEN]; char value_esc[MAX_STRING_LEN]; char *lastvalue; @@ -82,9 +81,7 @@ void update_functions(DB_ITEM *item) /* 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);*/ - zbx_snprintf(sql,sizeof(sql),"select distinct function,parameter,itemid,lastvalue from functions where itemid=%d",item->itemid); - - result = DBselect(sql); + result = DBselect("select distinct function,parameter,itemid,lastvalue from functions where itemid=%d",item->itemid); while((row=DBfetch(result))) { @@ -108,8 +105,7 @@ void update_functions(DB_ITEM *item) if( (lastvalue == NULL) || (strcmp(lastvalue,value) != 0)) { DBescape_string(value,value_esc,MAX_STRING_LEN); - zbx_snprintf(sql,sizeof(sql),"update functions set lastvalue='%s' where itemid=%d and function='%s' and parameter='%s'", value_esc, function.itemid, function.function, function.parameter ); - DBexecute(sql); + DBexecute("update functions set lastvalue='%s' where itemid=%d and function='%s' and parameter='%s'", value_esc, function.itemid, function.function, function.parameter ); } else { @@ -138,7 +134,6 @@ void update_functions(DB_ITEM *item) ******************************************************************************/ void update_services_rec(int serviceid) { - char sql[MAX_STRING_LEN]; int status; int serviceupid, algorithm; time_t now; @@ -148,8 +143,7 @@ void update_services_rec(int serviceid) DB_ROW row; DB_ROW row2; - zbx_snprintf(sql,sizeof(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); + result = DBselect("select l.serviceupid,s.algorithm from services_links l,services s where s.serviceid=l.serviceupid and l.servicedownid=%d",serviceid); status=0; while((row=DBfetch(result))) { @@ -164,8 +158,7 @@ void update_services_rec(int serviceid) (SERVICE_ALGORITHM_MIN == algorithm)) { /* Why it was so complex ? - zbx_snprintf(sql, sizeof(sql), "select status from services s,services_links l where l.serviceupid=%d and s.serviceid=l.servicedownid",serviceupid); - result2=DBselect(sql); + result2 = DBselect("select status from services s,services_links l where l.serviceupid=%d and s.serviceid=l.servicedownid",serviceupid); for(j=0;j<DBnum_rows(result2);j++) { if(atoi(DBget_field(result2,j,0))>status) @@ -177,14 +170,13 @@ void update_services_rec(int serviceid) if(SERVICE_ALGORITHM_MAX == algorithm) { - zbx_snprintf(sql,sizeof(sql),"select count(*),max(status) from services s,services_links l where l.serviceupid=%d and s.serviceid=l.servicedownid",serviceupid); + result2 = DBselect("select count(*),max(status) from services s,services_links l where l.serviceupid=%d and s.serviceid=l.servicedownid",serviceupid); } /* MIN otherwise */ else { - zbx_snprintf(sql,sizeof(sql),"select count(*),min(status) from services s,services_links l where l.serviceupid=%d and s.serviceid=l.servicedownid",serviceupid); + result2 = DBselect("select count(*),min(status) from services s,services_links l where l.serviceupid=%d and s.serviceid=l.servicedownid",serviceupid); } - result2=DBselect(sql); row2=DBfetch(result2); if(row2 && DBis_null(row2[0]) != SUCCEED && DBis_null(row2[1]) != SUCCEED) { @@ -197,8 +189,7 @@ void update_services_rec(int serviceid) now=time(NULL); DBadd_service_alarm(atoi(row[0]),status,now); - zbx_snprintf(sql,sizeof(sql),"update services set status=%d where serviceid=%d",status,atoi(row[0])); - DBexecute(sql); + DBexecute("update services set status=%d where serviceid=%d",status,atoi(row[0])); } else { @@ -208,8 +199,7 @@ void update_services_rec(int serviceid) } DBfree_result(result); - zbx_snprintf(sql,sizeof(sql),"select serviceupid from services_links where servicedownid=%d",serviceid); - result=DBselect(sql); + result = DBselect("select serviceupid from services_links where servicedownid=%d",serviceid); while((row=DBfetch(result))) { @@ -236,16 +226,13 @@ void update_services_rec(int serviceid) ******************************************************************************/ void update_services(int triggerid, int status) { - char sql[MAX_STRING_LEN]; DB_ROW row; DB_RESULT result; - zbx_snprintf(sql,sizeof(sql),"update services set status=%d where triggerid=%d",status,triggerid); - DBexecute(sql); + DBexecute("update services set status=%d where triggerid=%d",status,triggerid); - zbx_snprintf(sql,sizeof(sql),"select serviceid from services where triggerid=%d", triggerid); - result = DBselect(sql); + result = DBselect("select serviceid from services where triggerid=%d", triggerid); while((row=DBfetch(result))) { @@ -273,7 +260,6 @@ void update_services(int triggerid, int status) ******************************************************************************/ void update_triggers(int itemid) { - char sql[MAX_STRING_LEN]; char exp[MAX_STRING_LEN]; char error[MAX_STRING_LEN]; int exp_value; @@ -287,9 +273,7 @@ void update_triggers(int itemid) /* Does not work for PostgreSQL */ /* zbx_snprintf(sql, sizeof(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,server_num);*/ /* Is it correct SQL? */ - zbx_snprintf(sql,sizeof(sql),"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); + result = DBselect("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); while((row=DBfetch(result))) { @@ -409,7 +393,6 @@ void calc_timestamp(char *line,int *timestamp, char *format) int process_data(int sockfd,char *server,char *key,char *value,char *lastlogsize, char *timestamp, char *source, char *severity) { - char sql[MAX_STRING_LEN]; AGENT_RESULT agent; DB_RESULT result; @@ -429,9 +412,7 @@ int process_data(int sockfd,char *server,char *key,char *value,char *lastlogsize DBescape_string(server, server_esc, MAX_STRING_LEN); DBescape_string(key, key_esc, MAX_STRING_LEN); - zbx_snprintf(sql,sizeof(sql),"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)", ZBX_SQL_ITEM_SELECT, HOST_STATUS_MONITORED, server_esc, key_esc, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE); - - result = DBselect(sql); + 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)", ZBX_SQL_ITEM_SELECT, HOST_STATUS_MONITORED, server_esc, key_esc, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE); row=DBfetch(result); @@ -443,7 +424,8 @@ int process_data(int sockfd,char *server,char *key,char *value,char *lastlogsize { DBfree_result(result); - result = DBselect(sql); + /* Same SQL */ + 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)", ZBX_SQL_ITEM_SELECT, HOST_STATUS_MONITORED, server_esc, key_esc, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE); row = DBfetch(result); if(!row) { @@ -525,7 +507,6 @@ int process_data(int sockfd,char *server,char *key,char *value,char *lastlogsize ******************************************************************************/ static int add_history(DB_ITEM *item, AGENT_RESULT *value, int now) { - char sql[MAX_STRING_LEN]; int ret = SUCCEED; zabbix_log( LOG_LEVEL_DEBUG, "In add_history(%s,,%X,%X)", item->key, item->value_type,value->type); @@ -628,8 +609,7 @@ static int add_history(DB_ITEM *item, AGENT_RESULT *value, int now) { if(value->type & AR_STRING) DBadd_history_log(item->itemid,value->str,now,item->timestamp,item->eventlog_source,item->eventlog_severity); - zbx_snprintf(sql,sizeof(sql),"update items set lastlogsize=%d where itemid=%d",item->lastlogsize,item->itemid); - DBexecute(sql); + DBexecute("update items set lastlogsize=%d where itemid=%d",item->lastlogsize,item->itemid); } else if(item->value_type==ITEM_VALUE_TYPE_TEXT) { @@ -664,7 +644,6 @@ static int add_history(DB_ITEM *item, AGENT_RESULT *value, int now) ******************************************************************************/ static int update_item(DB_ITEM *item, AGENT_RESULT *value, int now) { - char sql[MAX_STRING_LEN]; char value_esc[MAX_STRING_LEN]; char value_str[MAX_STRING_LEN]; double value_double; @@ -672,7 +651,6 @@ static int update_item(DB_ITEM *item, AGENT_RESULT *value, int now) zabbix_log( LOG_LEVEL_DEBUG, "In update_item()"); - sql[0] = '\0'; value_str[0] = '\0'; value_esc[0] = '\0'; value_double = 0; @@ -706,7 +684,7 @@ static int update_item(DB_ITEM *item, AGENT_RESULT *value, int now) { DBescape_string(value_str,value_esc,MAX_STRING_LEN); /* zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d,prevvalue=lastvalue,lastvalue='%s',lastclock=%d where itemid=%d",now+item->delay,value_esc,now,item->itemid);*/ - zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d,prevvalue=lastvalue,lastvalue='%s',lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_esc,(int)now,item->itemid); + DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,lastvalue='%s',lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_esc,(int)now,item->itemid); item->prevvalue=item->lastvalue; item->lastvalue=value_double; item->prevvalue_str=item->lastvalue_str; @@ -718,7 +696,7 @@ static int update_item(DB_ITEM *item, AGENT_RESULT *value, int now) else { /* zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d,lastclock=%d where itemid=%d",now+item->delay,now,item->itemid);*/ - zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d,lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),(int)now,item->itemid); + DBexecute("update items set nextcheck=%d,lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),(int)now,item->itemid); } } /* Logic for delta as speed of change */ @@ -727,12 +705,12 @@ static int update_item(DB_ITEM *item, AGENT_RESULT *value, int now) if((item->prevorgvalue_null == 0) && (item->prevorgvalue <= value_double) ) { /* zbx_snprintf(sql,sizeof(sql),"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);*/ - zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue=%f,lastvalue='%f',lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_double,(value_double - item->prevorgvalue)/(now-item->lastclock),(int)now,item->itemid); + DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue=%f,lastvalue='%f',lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_double,(value_double - item->prevorgvalue)/(now-item->lastclock),(int)now,item->itemid); } else { /* zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d,prevorgvalue=%f,lastclock=%d where itemid=%d",now+item->delay,value_double,now,item->itemid);*/ - zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d,prevorgvalue=%f,lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_double,(int)now,item->itemid); + DBexecute("update items set nextcheck=%d,prevorgvalue=%f,lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_double,(int)now,item->itemid); } item->prevvalue=item->lastvalue; @@ -748,11 +726,11 @@ static int update_item(DB_ITEM *item, AGENT_RESULT *value, int now) { if((item->prevorgvalue_null == 0) && (item->prevorgvalue <= value_double) ) { - zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue=%f,lastvalue='%f',lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_double,(value_double - item->prevorgvalue),(int)now,item->itemid); + DBexecute("update items set nextcheck=%d,prevvalue=lastvalue,prevorgvalue=%f,lastvalue='%f',lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_double,(value_double - item->prevorgvalue),(int)now,item->itemid); } else { - zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d,prevorgvalue=%f,lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_double,(int)now,item->itemid); + DBexecute("update items set nextcheck=%d,prevorgvalue=%f,lastclock=%d where itemid=%d",calculate_item_nextcheck(item->delay,now),value_double,(int)now,item->itemid); } item->prevvalue=item->lastvalue; @@ -763,7 +741,6 @@ static int update_item(DB_ITEM *item, AGENT_RESULT *value, int now) item->prevvalue_null=item->lastvalue_null; item->lastvalue_null=0; } - DBexecute(sql); /* Update item status if required */ if(item->status == ITEM_STATUS_NOTSUPPORTED) @@ -771,8 +748,7 @@ static int update_item(DB_ITEM *item, AGENT_RESULT *value, int now) zabbix_log( LOG_LEVEL_WARNING, "Parameter [%s] became supported by agent on host [%s]", item->key, item->host ); zabbix_syslog("Parameter [%s] became supported by agent on host [%s]", item->key, item->host ); item->status = ITEM_STATUS_ACTIVE; - zbx_snprintf(sql,sizeof(sql),"update items set status=%d where itemid=%d", ITEM_STATUS_ACTIVE, item->itemid); - DBexecute(sql); + DBexecute("update items set status=%d where itemid=%d", ITEM_STATUS_ACTIVE, item->itemid); } return ret; diff --git a/src/zabbix_server/housekeeper/housekeeper.c b/src/zabbix_server/housekeeper/housekeeper.c index 6f01694f..af45990c 100644 --- a/src/zabbix_server/housekeeper/housekeeper.c +++ b/src/zabbix_server/housekeeper/housekeeper.c @@ -68,7 +68,6 @@ ******************************************************************************/ static int housekeeping_process_log() { - char sql[MAX_STRING_LEN]; DB_HOUSEKEEPER housekeeper; DB_RESULT result; @@ -80,8 +79,7 @@ static int housekeeping_process_log() zabbix_log( LOG_LEVEL_DEBUG, "In housekeeping_process_log()"); /* order by tablename to effectively use DB cache */ - zbx_snprintf(sql,sizeof(sql),"select housekeeperid, tablename, field, value from housekeeper order by tablename"); - result = DBselect(sql); + result = DBselect("select housekeeperid, tablename, field, value from housekeeper order by tablename"); while((row=DBfetch(result))) { @@ -91,14 +89,13 @@ static int housekeeping_process_log() housekeeper.value=atoi(row[3]); #ifdef HAVE_ORACLE - zbx_snprintf(sql,sizeof(sql),"delete from %s where %s=%d and rownum<500",housekeeper.tablename, housekeeper.field,housekeeper.value); + deleted = DBexecute("delete from %s where %s=%d and rownum<500",housekeeper.tablename, housekeeper.field,housekeeper.value); #else - zbx_snprintf(sql,sizeof(sql),"delete from %s where %s=%d limit 500",housekeeper.tablename, housekeeper.field,housekeeper.value); + deleted = DBexecute("delete from %s where %s=%d limit 500",housekeeper.tablename, housekeeper.field,housekeeper.value); #endif - if(( deleted = DBexecute(sql)) == 0) + if(deleted == 0) { - zbx_snprintf(sql,sizeof(sql),"delete from housekeeper where housekeeperid=%d",housekeeper.housekeeperid); - DBexecute(sql); + DBexecute("delete from housekeeper where housekeeperid=%d",housekeeper.housekeeperid); } else { @@ -113,29 +110,28 @@ static int housekeeping_process_log() static int housekeeping_sessions(int now) { - char sql[MAX_STRING_LEN]; + int deleted; zabbix_log( LOG_LEVEL_DEBUG, "In housekeeping_sessions(%d)", now); - zbx_snprintf(sql,sizeof(sql),"delete from sessions where lastaccess<%d",now-24*3600); + deleted = DBexecute("delete from sessions where lastaccess<%d",now-24*3600); - zabbix_log( LOG_LEVEL_DEBUG, "Deleted [%ld] records from table [sessions]", DBexecute(sql)); + zabbix_log( LOG_LEVEL_DEBUG, "Deleted [%ld] records from table [sessions]", deleted); return SUCCEED; } static int housekeeping_alerts(int now) { - char sql[MAX_STRING_LEN]; int alert_history; DB_RESULT result; DB_ROW row; int res = SUCCEED; + int deleted; zabbix_log( LOG_LEVEL_DEBUG, "In housekeeping_alerts(%d)", now); - zbx_snprintf(sql,sizeof(sql),"select alert_history from config"); - result = DBselect(sql); + result = DBselect("select alert_history from config"); row=DBfetch(result); @@ -148,8 +144,8 @@ static int housekeeping_alerts(int now) { alert_history=atoi(row[0]); - zbx_snprintf(sql,sizeof(sql),"delete from alerts where clock<%d",now-24*3600*alert_history); - zabbix_log( LOG_LEVEL_DEBUG, "Deleted [%ld] records from table [alerts]", DBexecute(sql)); + deleted = DBexecute("delete from alerts where clock<%d",now-24*3600*alert_history); + zabbix_log( LOG_LEVEL_DEBUG, "Deleted [%ld] records from table [alerts]", deleted); } DBfree_result(result); @@ -158,7 +154,6 @@ static int housekeeping_alerts(int now) static int housekeeping_alarms(int now) { - char sql[MAX_STRING_LEN]; int alarm_history; DB_RESULT result; DB_RESULT result2; @@ -169,8 +164,8 @@ static int housekeeping_alarms(int now) zabbix_log( LOG_LEVEL_DEBUG, "In housekeeping_alarms(%d)", now); - zbx_snprintf(sql,sizeof(sql),"select alarm_history from config"); - result = DBselect(sql); + result = DBselect("select alarm_history from config"); + row1=DBfetch(result); if(!row1 || DBis_null(row1[0])==SUCCEED) @@ -182,17 +177,14 @@ static int housekeeping_alarms(int now) { alarm_history=atoi(row1[0]); - zbx_snprintf(sql,sizeof(sql),"select alarmid from alarms where clock<%d", now-24*3600*alarm_history); - result2 = DBselect(sql); + result2 = DBselect("select alarmid from alarms where clock<%d", now-24*3600*alarm_history); while((row2=DBfetch(result2))) { alarmid=atoi(row2[0]); - zbx_snprintf(sql,sizeof(sql),"delete from acknowledges where alarmid=%d",alarmid); - DBexecute(sql); + DBexecute("delete from acknowledges where alarmid=%d",alarmid); - zbx_snprintf(sql,sizeof(sql),"delete from alarms where alarmid=%d",alarmid); - zabbix_log( LOG_LEVEL_DEBUG, "Deleted [%ld] records from table [alarms]", DBexecute(sql)); + DBexecute("delete from alarms where alarmid=%d",alarmid); } DBfree_result(result2); diff --git a/src/zabbix_server/pinger/pinger.c b/src/zabbix_server/pinger/pinger.c index 61c9344d..112a9a1e 100644 --- a/src/zabbix_server/pinger/pinger.c +++ b/src/zabbix_server/pinger/pinger.c @@ -122,8 +122,6 @@ static int is_ip(char *ip) ******************************************************************************/ static int process_value(char *key, char *host, AGENT_RESULT *value) { - char sql[MAX_STRING_LEN]; - DB_RESULT result; DB_ROW row; DB_ITEM item; @@ -134,14 +132,12 @@ static int process_value(char *key, char *host, AGENT_RESULT *value) /* IP address? */ if(is_ip(host) == SUCCEED) { - zbx_snprintf(sql,sizeof(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,i.delta,i.units,i.multiplier,i.formula from items i,hosts h where h.status=%d and h.hostid=i.hostid and h.ip='%s' and i.key_='%s' and i.status=%d and i.type=%d", HOST_STATUS_MONITORED, host, key, ITEM_STATUS_ACTIVE, ITEM_TYPE_SIMPLE); + result = DBselect("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.ip='%s' and i.key_='%s' and i.status=%d and i.type=%d", HOST_STATUS_MONITORED, host, key, ITEM_STATUS_ACTIVE, ITEM_TYPE_SIMPLE); } else { - zbx_snprintf(sql,sizeof(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,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=%d", HOST_STATUS_MONITORED, host, key, ITEM_STATUS_ACTIVE, ITEM_TYPE_SIMPLE); + result = DBselect("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=%d", HOST_STATUS_MONITORED, host, key, ITEM_STATUS_ACTIVE, ITEM_TYPE_SIMPLE); } - zabbix_log( LOG_LEVEL_DEBUG, "SQL [%s]", sql); - result = DBselect(sql); row=DBfetch(result); if(!row) @@ -219,7 +215,6 @@ static int process_value(char *key, char *host, AGENT_RESULT *value) ******************************************************************************/ static int create_host_file(void) { - char sql[MAX_STRING_LEN]; FILE *f; int now; @@ -238,8 +233,7 @@ static int create_host_file(void) now=time(NULL); /* Select hosts monitored by IP */ - zbx_snprintf(sql,sizeof(sql),"select distinct h.ip from hosts h,items i where i.hostid=h.hostid and (h.status=%d or (h.status=%d and h.available=%d and h.disable_until<=%d)) and (i.key_='%s' or i.key_='%s') and i.type=%d and i.status=%d and h.useip=1", HOST_STATUS_MONITORED, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, now, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY, ITEM_TYPE_SIMPLE, ITEM_STATUS_ACTIVE); - result = DBselect(sql); + result = DBselect("select distinct h.ip from hosts h,items i where i.hostid=h.hostid and (h.status=%d or (h.status=%d and h.available=%d and h.disable_until<=%d)) and (i.key_='%s' or i.key_='%s') and i.type=%d and i.status=%d and h.useip=1", HOST_STATUS_MONITORED, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, now, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY, ITEM_TYPE_SIMPLE, ITEM_STATUS_ACTIVE); while((row=DBfetch(result))) { @@ -253,8 +247,7 @@ static int create_host_file(void) DBfree_result(result); /* Select hosts monitored by hostname */ - zbx_snprintf(sql,sizeof(sql),"select distinct h.host from hosts h,items i where i.hostid=h.hostid and (h.status=%d or (h.status=%d and h.available=%d and h.disable_until<=%d)) and (i.key_='%s' or i.key_='%s') and i.type=%d and i.status=%d and h.useip=0", HOST_STATUS_MONITORED, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, now, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY, ITEM_TYPE_SIMPLE, ITEM_STATUS_ACTIVE); - result = DBselect(sql); + result = DBselect("select distinct h.host from hosts h,items i where i.hostid=h.hostid and (h.status=%d or (h.status=%d and h.available=%d and h.disable_until<=%d)) and (i.key_='%s' or i.key_='%s') and i.type=%d and i.status=%d and h.useip=0", HOST_STATUS_MONITORED, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, now, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY, ITEM_TYPE_SIMPLE, ITEM_STATUS_ACTIVE); while((row=DBfetch(result))) { diff --git a/src/zabbix_server/poller/checks_aggregate.c b/src/zabbix_server/poller/checks_aggregate.c index 2faf1ee0..09e6261e 100644 --- a/src/zabbix_server/poller/checks_aggregate.c +++ b/src/zabbix_server/poller/checks_aggregate.c @@ -110,8 +110,7 @@ static int evaluate_aggregate(AGENT_RESULT *res,char *grpfunc, char *hostgroup, DBescape_string(hostgroup,hostgroup_esc,MAX_STRING_LEN); /* Get list of affected item IDs */ strscpy(items,"0"); - zbx_snprintf(sql,sizeof(sql),"select itemid from items,hosts_groups,hosts,groups where hosts_groups.groupid=groups.groupid and items.hostid=hosts.hostid and hosts_groups.hostid=hosts.hostid and groups.name='%s' and items.key_='%s' and items.status=%d and hosts.status=%d",hostgroup_esc, itemkey_esc, ITEM_STATUS_ACTIVE, HOST_STATUS_MONITORED); - result = DBselect(sql); + result = DBselect("select itemid from items,hosts_groups,hosts,groups where hosts_groups.groupid=groups.groupid and items.hostid=hosts.hostid and hosts_groups.hostid=hosts.hostid and groups.name='%s' and items.key_='%s' and items.status=%d and hosts.status=%d",hostgroup_esc, itemkey_esc, ITEM_STATUS_ACTIVE, HOST_STATUS_MONITORED); while((row=DBfetch(result))) { diff --git a/src/zabbix_server/poller/poller.c b/src/zabbix_server/poller/poller.c index 20377bd3..425f51de 100644 --- a/src/zabbix_server/poller/poller.c +++ b/src/zabbix_server/poller/poller.c @@ -111,8 +111,6 @@ int get_value(DB_ITEM *item, AGENT_RESULT *result) static int get_minnextcheck(int now) { - char sql[MAX_STRING_LEN]; - DB_RESULT result; DB_ROW row; @@ -123,21 +121,20 @@ static int get_minnextcheck(int now) 2 == UNREACHABLE */ if(my_server_num == 4) { - zbx_snprintf(sql,sizeof(sql),"select count(*),min(nextcheck) from items i,hosts h where i.nextcheck<=%d and i.status in (%d) and i.type not in (%d,%d) and h.status=%d and h.disable_until<=%d and h.errors_from!=0 and h.hostid=i.hostid and i.key_ not in ('%s','%s','%s','%s') order by i.nextcheck", now, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, HOST_STATUS_MONITORED, now, SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); + result = DBselect("select count(*),min(nextcheck) from items i,hosts h where i.nextcheck<=%d and i.status in (%d) and i.type not in (%d,%d) and h.status=%d and h.disable_until<=%d and h.errors_from!=0 and h.hostid=i.hostid and i.key_ not in ('%s','%s','%s','%s') order by i.nextcheck", now, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, HOST_STATUS_MONITORED, now, SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); } else { if(CONFIG_REFRESH_UNSUPPORTED != 0) { - zbx_snprintf(sql,sizeof(sql),"select count(*),min(nextcheck) from items i,hosts h where h.status=%d and h.disable_until<%d and h.errors_from=0 and h.hostid=i.hostid and i.status in (%d,%d) and i.type not in (%d,%d) and mod(i.itemid,%d)=%d and i.key_ not in ('%s','%s','%s','%s')", HOST_STATUS_MONITORED, now, ITEM_STATUS_ACTIVE, ITEM_STATUS_NOTSUPPORTED, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, CONFIG_POLLER_FORKS-5,my_server_num-5,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); + result = DBselect("select count(*),min(nextcheck) from items i,hosts h where h.status=%d and h.disable_until<%d and h.errors_from=0 and h.hostid=i.hostid and i.status in (%d,%d) and i.type not in (%d,%d) and mod(i.itemid,%d)=%d and i.key_ not in ('%s','%s','%s','%s')", HOST_STATUS_MONITORED, now, ITEM_STATUS_ACTIVE, ITEM_STATUS_NOTSUPPORTED, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, CONFIG_POLLER_FORKS-5,my_server_num-5,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); } else { - zbx_snprintf(sql,sizeof(sql),"select count(*),min(nextcheck) from items i,hosts h where h.status=%d and h.disable_until<%d and h.errors_from=0 and h.hostid=i.hostid and i.status in (%d,%d) and i.type not in (%d) and mod(i.itemid,%d)=%d and i.key_ not in ('%s','%s','%s','%s')", HOST_STATUS_MONITORED, now, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, CONFIG_POLLER_FORKS-5,my_server_num-5,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); + result = DBselect("select count(*),min(nextcheck) from items i,hosts h where h.status=%d and h.disable_until<%d and h.errors_from=0 and h.hostid=i.hostid and i.status in (%d,%d) and i.type not in (%d) and mod(i.itemid,%d)=%d and i.key_ not in ('%s','%s','%s','%s')", HOST_STATUS_MONITORED, now, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, CONFIG_POLLER_FORKS-5,my_server_num-5,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); } } - result = DBselect(sql); row=DBfetch(result); if(!row || DBis_null(row[0])==SUCCEED || DBis_null(row[1])==SUCCEED) @@ -164,7 +161,6 @@ static int get_minnextcheck(int now) /* Update special host's item - "status" */ static void update_key_status(int hostid,int host_status) { - char sql[MAX_STRING_LEN]; /* char value_str[MAX_STRING_LEN];*/ AGENT_RESULT agent; @@ -174,9 +170,7 @@ static void update_key_status(int hostid,int host_status) zabbix_log(LOG_LEVEL_DEBUG, "In update_key_status(%d,%d)",hostid,host_status); - zbx_snprintf(sql,sizeof(sql),"select %s where h.hostid=i.hostid and h.hostid=%d and i.key_='%s'", ZBX_SQL_ITEM_SELECT, hostid,SERVER_STATUS_KEY); - zabbix_log(LOG_LEVEL_DEBUG, "SQL [%s]", sql); - result = DBselect(sql); + result = DBselect("select %s where h.hostid=i.hostid and h.hostid=%d and i.key_='%s'", ZBX_SQL_ITEM_SELECT, hostid,SERVER_STATUS_KEY); row = DBfetch(result); @@ -221,8 +215,6 @@ static void update_key_status(int hostid,int host_status) ******************************************************************************/ int get_values(void) { - char sql[MAX_STRING_LEN]; - DB_RESULT result; DB_ROW row; @@ -237,20 +229,19 @@ int get_values(void) /* Poller for unreachable hosts */ if(my_server_num == 4) { - zbx_snprintf(sql,sizeof(sql),"select %s where i.nextcheck<=%d and i.status in (%d) and i.type not in (%d,%d) and h.status=%d and h.disable_until<=%d and h.errors_from!=0 and h.hostid=i.hostid and i.key_ not in ('%s','%s','%s','%s') order by i.nextcheck", ZBX_SQL_ITEM_SELECT, now, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, HOST_STATUS_MONITORED, now, SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); + result = DBselect("select %s where i.nextcheck<=%d and i.status in (%d) and i.type not in (%d,%d) and h.status=%d and h.disable_until<=%d and h.errors_from!=0 and h.hostid=i.hostid and i.key_ not in ('%s','%s','%s','%s') order by i.nextcheck", ZBX_SQL_ITEM_SELECT, now, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, HOST_STATUS_MONITORED, now, SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); } else { if(CONFIG_REFRESH_UNSUPPORTED != 0) { - zbx_snprintf(sql,sizeof(sql),"select %s where i.nextcheck<=%d and i.status in (%d,%d) and i.type not in (%d,%d) and h.status=%d and h.disable_until<=%d and h.errors_from=0 and h.hostid=i.hostid and mod(i.itemid,%d)=%d and i.key_ not in ('%s','%s','%s','%s') order by i.nextcheck", ZBX_SQL_ITEM_SELECT, now, ITEM_STATUS_ACTIVE, ITEM_STATUS_NOTSUPPORTED, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, HOST_STATUS_MONITORED, now, CONFIG_POLLER_FORKS-5,my_server_num-5,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); + result = DBselect("select %s where i.nextcheck<=%d and i.status in (%d,%d) and i.type not in (%d,%d) and h.status=%d and h.disable_until<=%d and h.errors_from=0 and h.hostid=i.hostid and mod(i.itemid,%d)=%d and i.key_ not in ('%s','%s','%s','%s') order by i.nextcheck", ZBX_SQL_ITEM_SELECT, now, ITEM_STATUS_ACTIVE, ITEM_STATUS_NOTSUPPORTED, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, HOST_STATUS_MONITORED, now, CONFIG_POLLER_FORKS-5,my_server_num-5,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); } else { - zbx_snprintf(sql,sizeof(sql),"select %s where i.nextcheck<=%d and i.status in (%d) and i.type not in (%d,%d) and h.status=%d and h.disable_until<=%d and h.errors_from=0 and h.hostid=i.hostid and mod(i.itemid,%d)=%d and i.key_ not in ('%s','%s','%s','%s') order by i.nextcheck", ZBX_SQL_ITEM_SELECT, now, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, HOST_STATUS_MONITORED, now, CONFIG_POLLER_FORKS-5,my_server_num-5,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); + result = DBselect("select %s where i.nextcheck<=%d and i.status in (%d) and i.type not in (%d,%d) and h.status=%d and h.disable_until<=%d and h.errors_from=0 and h.hostid=i.hostid and mod(i.itemid,%d)=%d and i.key_ not in ('%s','%s','%s','%s') order by i.nextcheck", ZBX_SQL_ITEM_SELECT, now, ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, ITEM_TYPE_ZABBIX_ACTIVE, HOST_STATUS_MONITORED, now, CONFIG_POLLER_FORKS-5,my_server_num-5,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY,SERVER_ZABBIXLOG_KEY); } } - result = DBselect(sql); while((row=DBfetch(result))&&(stop==0)) { @@ -277,9 +268,7 @@ int get_values(void) } if(item.host_errors_from!=0) { - zbx_snprintf(sql,sizeof(sql),"update hosts set errors_from=0 where hostid=%d", item.hostid); - zabbix_log( LOG_LEVEL_DEBUG, "SQL [%s]", sql); - DBexecute(sql); + DBexecute("update hosts set errors_from=0 where hostid=%d", item.hostid); stop=1; } @@ -289,8 +278,7 @@ int get_values(void) { if(item.status == ITEM_STATUS_NOTSUPPORTED) { - zbx_snprintf(sql,sizeof(sql),"update items set nextcheck=%d, lastclock=%d where itemid=%d",calculate_item_nextcheck(CONFIG_REFRESH_UNSUPPORTED,now), now, item.itemid); - DBexecute(sql); + DBexecute("update items set nextcheck=%d, lastclock=%d where itemid=%d",calculate_item_nextcheck(CONFIG_REFRESH_UNSUPPORTED,now), now, item.itemid); } else { @@ -319,9 +307,7 @@ int get_values(void) zabbix_syslog("Host [%s]: first network error, wait for %d seconds", item.host, CONFIG_UNREACHABLE_DELAY); item.host_errors_from=now; - zbx_snprintf(sql,sizeof(sql),"update hosts set errors_from=%d,disable_until=%d where hostid=%d", now, now+CONFIG_UNREACHABLE_DELAY, item.hostid); - zabbix_log( LOG_LEVEL_DEBUG, "SQL [%s]", sql); - DBexecute(sql); + DBexecute("update hosts set errors_from=%d,disable_until=%d where hostid=%d", now, now+CONFIG_UNREACHABLE_DELAY, item.hostid); } else { @@ -334,9 +320,7 @@ int get_values(void) update_key_status(item.hostid,HOST_AVAILABLE_FALSE); /* 2 */ item.host_available=HOST_AVAILABLE_FALSE; - zbx_snprintf(sql,sizeof(sql),"update hosts set disable_until=%d where hostid=%d", now+CONFIG_UNAVAILABLE_DELAY, item.hostid); - zabbix_log( LOG_LEVEL_DEBUG, "SQL [%s]", sql); - DBexecute(sql); + DBexecute("update hosts set disable_until=%d where hostid=%d", now+CONFIG_UNAVAILABLE_DELAY, item.hostid); } /* Still unavailable, but won't change status to UNAVAILABLE yet */ else @@ -344,9 +328,7 @@ int get_values(void) zabbix_log( LOG_LEVEL_WARNING, "Host [%s]: another network error, wait for %d seconds", item.host, CONFIG_UNREACHABLE_DELAY); zabbix_syslog("Host [%s]: another network error, wait for %d seconds", item.host, CONFIG_UNREACHABLE_DELAY); - zbx_snprintf(sql,sizeof(sql),"update hosts set disable_until=%d where hostid=%d", now+CONFIG_UNREACHABLE_DELAY, item.hostid); - zabbix_log( LOG_LEVEL_DEBUG, "SQL [%s]", sql); - DBexecute(sql); + DBexecute("update hosts set disable_until=%d where hostid=%d", now+CONFIG_UNREACHABLE_DELAY, item.hostid); } } diff --git a/src/zabbix_server/server.c b/src/zabbix_server/server.c index 3c714591..ad752bff 100644 --- a/src/zabbix_server/server.c +++ b/src/zabbix_server/server.c @@ -212,12 +212,10 @@ void trend(void) int i,j; - zbx_snprintf(sql,sizeof(sql),"select itemid from items"); - result2 = DBselect(sql); + result2 = DBselect("select itemid from items"); for(i=0;i<DBnum_rows(result2);i++) { - zbx_snprintf(sql,sizeof(sql),"select clock-clock%%3600, count(*),min(value),avg(value),max(value) from history where itemid=%d group by 1",atoi(DBget_field(result2,i,0))); - result = DBselect(sql); + result = DBselect("select clock-clock%%3600, count(*),min(value),avg(value),max(value) from history where itemid=%d group by 1",atoi(DBget_field(result2,i,0))); for(j=0;j<DBnum_rows(result);j++) { @@ -346,7 +344,6 @@ int main(int argc, char **argv) init_config(); DBconnect(); -// result = DBselect("select * from history where itemid=20272"); result = DBselect("select NULL from history where itemid=20272222"); row=DBfetch(result); if(!row) printf("OK"); @@ -362,7 +359,6 @@ int main(int argc, char **argv) #ifdef HAVE_ZZZ /* */ DBconnect(); -/* DBexecute("update history set value=value-10 where itemid=20272");*/ result = DBselect("select itemid,key_,description from items"); while ( SQLO_SUCCESS == sqlo_fetch(result, 1)) { diff --git a/src/zabbix_server/timer/timer.c b/src/zabbix_server/timer/timer.c index 648bd6eb..04c77d6c 100644 --- a/src/zabbix_server/timer/timer.c +++ b/src/zabbix_server/timer/timer.c @@ -63,7 +63,6 @@ extern void update_functions(DB_ITEM *item); ******************************************************************************/ void main_timer_loop() { - char sql[MAX_STRING_LEN]; int now; /* int itemid,functionid; @@ -90,9 +89,7 @@ void main_timer_loop() #endif */ - zbx_snprintf(sql,sizeof(sql),"select distinct %s, functions f where h.hostid=i.hostid and h.status=%d and i.status=%d and f.function in ('nodata','date','dayofweek','time','now') and i.itemid=f.itemid", ZBX_SQL_ITEM_SELECT, HOST_STATUS_MONITORED, ITEM_STATUS_ACTIVE); - - result = DBselect(sql); + result = DBselect("select distinct %s, functions f where h.hostid=i.hostid and h.status=%d and i.status=%d and f.function in ('nodata','date','dayofweek','time','now') and i.itemid=f.itemid", ZBX_SQL_ITEM_SELECT, HOST_STATUS_MONITORED, ITEM_STATUS_ACTIVE); while((row=DBfetch(result))) { diff --git a/src/zabbix_server/trapper/active.c b/src/zabbix_server/trapper/active.c index 31424422..8ac24c7f 100644 --- a/src/zabbix_server/trapper/active.c +++ b/src/zabbix_server/trapper/active.c @@ -62,16 +62,14 @@ ******************************************************************************/ int send_list_of_active_checks(int sockfd, char *host) { - char sql[MAX_STRING_LEN]; char s[MAX_STRING_LEN]; DB_RESULT result; DB_ROW row; zabbix_log( LOG_LEVEL_DEBUG, "In send_list_of_active_checks()"); - zbx_snprintf(sql,sizeof(sql),"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("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); while((row=DBfetch(result))) { zbx_snprintf(s,sizeof(s),"%s:%s:%s\n",row[0],row[1],row[2]); diff --git a/src/zabbix_server/trapper/autoregister.c b/src/zabbix_server/trapper/autoregister.c index 99f63203..747b4d42 100644 --- a/src/zabbix_server/trapper/autoregister.c +++ b/src/zabbix_server/trapper/autoregister.c @@ -44,7 +44,6 @@ int autoregister(char *server) DB_ROW row; int ret=SUCCEED; - char sql[MAX_STRING_LEN]; char *pattern; int len; int hostid; @@ -57,9 +56,7 @@ int autoregister(char *server) return FAIL; } - zbx_snprintf(sql,sizeof(sql),"select id,pattern,hostid from autoreg order by priority"); - - result = DBselect(sql); + result = DBselect("select id,pattern,hostid from autoreg order by priority"); while((row=DBfetch(result))) { diff --git a/src/zabbix_server/zlog.c b/src/zabbix_server/zlog.c index 3dffec26..783646a8 100644 --- a/src/zabbix_server/zlog.c +++ b/src/zabbix_server/zlog.c @@ -52,7 +52,6 @@ void zabbix_syslog(const char *fmt, ...) { va_list ap; - char sql[MAX_STRING_LEN]; char value_str[MAX_STRING_LEN]; DB_ITEM item; @@ -63,8 +62,7 @@ void zabbix_syslog(const char *fmt, ...) zabbix_log(LOG_LEVEL_DEBUG, "In zabbix_log()"); - zbx_snprintf(sql,sizeof(sql),"select %s where h.hostid=i.hostid and i.key_='%s' and i.value_type=%d", ZBX_SQL_ITEM_SELECT, SERVER_ZABBIXLOG_KEY, ITEM_VALUE_TYPE_STR); - result = DBselect(sql); + result = DBselect("select %s where h.hostid=i.hostid and i.key_='%s' and i.value_type=%d", ZBX_SQL_ITEM_SELECT, SERVER_ZABBIXLOG_KEY, ITEM_VALUE_TYPE_STR); while((row=DBfetch(result))) { |
