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/libs | |
| 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/libs')
| -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 |
6 files changed, 237 insertions, 289 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) |
