diff options
| author | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-07-29 09:43:53 +0000 |
|---|---|---|
| committer | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-07-29 09:43:53 +0000 |
| commit | f7cd0be8cdcedcc34656ced6982dd7abadca01fc (patch) | |
| tree | a6aa538b83fd84824e8a8431bd9c2ecc694dafc3 /src/libs | |
| parent | e3248ce4bc1b34d8623359d5333cfa9712b5eb73 (diff) | |
| download | zabbix-f7cd0be8cdcedcc34656ced6982dd7abadca01fc.tar.gz zabbix-f7cd0be8cdcedcc34656ced6982dd7abadca01fc.tar.xz zabbix-f7cd0be8cdcedcc34656ced6982dd7abadca01fc.zip | |
- [DEV-196] improved performance of server module
git-svn-id: svn://svn.zabbix.com/trunk@5848 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src/libs')
| -rw-r--r-- | src/libs/zbxdb/db.c | 34 | ||||
| -rw-r--r-- | src/libs/zbxdbcache/Makefile.am | 3 | ||||
| -rw-r--r-- | src/libs/zbxdbcache/dbcache.c | 2028 | ||||
| -rw-r--r-- | src/libs/zbxdbcache/nextchecks.c | 213 | ||||
| -rw-r--r-- | src/libs/zbxdbhigh/db.c | 649 | ||||
| -rw-r--r-- | src/libs/zbxserver/expression.c | 10 | ||||
| -rw-r--r-- | src/libs/zbxserver/expression.h | 1 | ||||
| -rw-r--r-- | src/libs/zbxserver/functions.c | 28 |
8 files changed, 2325 insertions, 641 deletions
diff --git a/src/libs/zbxdb/db.c b/src/libs/zbxdb/db.c index 1de8ac7d..61dad9fd 100644 --- a/src/libs/zbxdb/db.c +++ b/src/libs/zbxdb/db.c @@ -84,7 +84,7 @@ int zbx_db_connect(char *host, char *user, char *password, char *dbname, char *d conn = mysql_init(NULL); - if( ! mysql_real_connect( conn, host, user, password, dbname, port, dbsocket,0 ) ) + if( ! mysql_real_connect( conn, host, user, password, dbname, port, dbsocket, CLIENT_MULTI_STATEMENTS) ) { zabbix_log(LOG_LEVEL_ERR, "Failed to connect to database: Error: %s [%d]", mysql_error(conn), mysql_errno(conn)); @@ -191,7 +191,7 @@ int zbx_db_connect(char *host, char *user, char *password, char *dbname, char *d } if (ZBX_DB_OK == ret) - sqlo_autocommit_on(oracle); + sqlo_autocommit_off(oracle); return ret; #endif @@ -342,6 +342,9 @@ void zbx_db_commit(void) #ifdef HAVE_POSTGRESQL zbx_db_execute("%s","commit;"); #endif +#ifdef HAVE_ORACLE + zbx_db_execute("%s","commit"); +#endif #ifdef HAVE_SQLITE3 if(sqlite_transaction_started > 1) @@ -384,6 +387,9 @@ void zbx_db_rollback(void) #ifdef HAVE_POSTGRESQL zbx_db_execute("rollback;"); #endif +#ifdef HAVE_ORACLE + zbx_db_execute("rollback"); +#endif #ifdef HAVE_SQLITE3 if(sqlite_transaction_started > 1) @@ -420,6 +426,9 @@ int zbx_db_vexecute(const char *fmt, va_list args) #ifdef HAVE_SQLITE3 char *error=0; #endif +#ifdef HAVE_MYSQL + int status; +#endif /* sec = zbx_time();*/ @@ -434,7 +443,7 @@ int zbx_db_vexecute(const char *fmt, va_list args) } else { - if(mysql_query(conn,sql) != 0) + if ((status = mysql_query(conn,sql)) != 0) { zabbix_log(LOG_LEVEL_ERR, "Query failed: [%s] %s [%d]", sql, mysql_error(conn), mysql_errno(conn) ); @@ -453,7 +462,24 @@ int zbx_db_vexecute(const char *fmt, va_list args) } else { - ret = (int)mysql_affected_rows(conn); + do { + if (mysql_field_count(conn) == 0) + { +/* zabbix_log(LOG_LEVEL_DEBUG, ZBX_FS_UI64 " rows affected", + (zbx_uint64_t)mysql_affected_rows(conn));*/ + ret += (int)mysql_affected_rows(conn); + } + else /* some error occurred */ + { + zabbix_log(LOG_LEVEL_DEBUG, "Could not retrieve result set"); + break; + } + + /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */ + if ((status = mysql_next_result(conn)) > 0) + zabbix_log(LOG_LEVEL_ERR, "Error: %s [%d]", + mysql_error(conn), mysql_errno(conn)); + } while (status == 0); } } #endif diff --git a/src/libs/zbxdbcache/Makefile.am b/src/libs/zbxdbcache/Makefile.am index 4b1fb42f..b1955b03 100644 --- a/src/libs/zbxdbcache/Makefile.am +++ b/src/libs/zbxdbcache/Makefile.am @@ -3,4 +3,5 @@ noinst_LIBRARIES = libzbxdbcache.a libzbxdbcache_a_SOURCES = \ - dbcache.c + dbcache.c \ + nextchecks.c diff --git a/src/libs/zbxdbcache/dbcache.c b/src/libs/zbxdbcache/dbcache.c index dee47851..82866151 100644 --- a/src/libs/zbxdbcache/dbcache.c +++ b/src/libs/zbxdbcache/dbcache.c @@ -20,345 +20,1860 @@ #include "common.h" #include "log.h" #include "zlog.h" +#include "threads.h" #include "db.h" #include "dbcache.h" #include "mutexs.h" +#include "zbxserver.h" #define LOCK_CACHE zbx_mutex_lock(&cache_lock) #define UNLOCK_CACHE zbx_mutex_unlock(&cache_lock) -#define ZBX_GET_SHM_DBCACHE_KEY(smk_key) \ - {if( -1 == (shm_key = ftok(CONFIG_FILE, (int)'c') )) \ - { \ - zbx_error("Can not create IPC key for path '%s', try to create for path '.' [%s]", CONFIG_FILE, strerror(errno)); \ - if( -1 == (shm_key = ftok(".", (int)'c') )) \ - { \ - zbx_error("Can not create IPC key for path '.' [%s]", strerror(errno)); \ - exit(1); \ - } \ +#define ZBX_GET_SHM_DBCACHE_KEY(smk_key) \ + {if( -1 == (shm_key = ftok(CONFIG_FILE, (int)'c') )) \ + { \ + zbx_error("Can not create IPC key for path '%s', try to create for path '.' [%s]", \ + CONFIG_FILE, strerror(errno)); \ + if( -1 == (shm_key = ftok(".", (int)'c') )) \ + { \ + zbx_error("Can not create IPC key for path '.' [%s]", strerror(errno)); \ + exit(1); \ + } \ }} ZBX_DC_CACHE *cache = NULL; static ZBX_MUTEX cache_lock; +static char *sql = NULL; +static int sql_allocated = 65536; + +zbx_process_t zbx_process; + /****************************************************************************** * * - * Function: DCsync * + * Function: DCmass_update_triggers * * * - * Purpose: writes updates and new data from pool to database * + * Purpose: re-calculate and updates values of triggers related to the items * * * - * Parameters: * + * Parameters: history - array of history data * + * history_num - number of history structures * * * * Return value: * * * - * Author: Alexei Vladishev * + * Author: Alexei Vladishev, Aleksander Vladishev * * * * Comments: * * * ******************************************************************************/ -void DCsync() +void DCmass_update_triggers(ZBX_DC_HISTORY *history, int history_num) { - int i; + char *exp; + char error[MAX_STRING_LEN]; + int exp_value; + DB_TRIGGER trigger; + DB_RESULT result; + DB_ROW row; + int sql_offset = 0, i; + ZBX_DC_HISTORY *h; + zbx_uint64_t itemid; - ZBX_DC_HISTORY *history; - ZBX_DC_TREND *trend; - char value_esc[MAX_STRING_LEN]; + zabbix_log(LOG_LEVEL_DEBUG, "In DCmass_update_triggers()"); - zabbix_log(LOG_LEVEL_DEBUG,"In DCsync(items %d pool:trends %d pool:history:%d)", - cache->items_count, - cache->pool.trends_count, - cache->pool.history_count); + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 1024, + "select distinct t.triggerid,t.expression,t.description,t.url,t.comments,t.status,t.value,t.priority" + ",t.type,f.itemid from triggers t,functions f,items i where i.status not in (%d) and i.itemid=f.itemid" + " and t.status=%d and f.triggerid=t.triggerid and f.itemid in (", + ITEM_STATUS_NOTSUPPORTED, + TRIGGER_STATUS_ENABLED); - LOCK_CACHE; - DBbegin(); - + for (i = 0; i < history_num; i++) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 32, ZBX_FS_UI64 ",", + history[i].itemid); + + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 32, ")"); + } + + result = DBselect("%s", sql); - for(i=0;i<cache->pool.history_count;i++) + sql_offset = 0; + + while (NULL != (row = DBfetch(result))) { - history = &cache->pool.history[i]; - zabbix_log(LOG_LEVEL_DEBUG,"History " ZBX_FS_UI64, - history->itemid); - switch(history->value_type) + trigger.triggerid = zbx_atoui64(row[0]); + strscpy(trigger.expression, row[1]); + strscpy(trigger.description, row[2]); + trigger.url = row[3]; + trigger.comments = row[4]; + trigger.status = atoi(row[5]); + trigger.value = atoi(row[6]); + trigger.priority = atoi(row[7]); + trigger.type = atoi(row[8]); + itemid = zbx_atoui64(row[9]); + + h = NULL; + + for (i = 0; i < history_num; i++) { - case ITEM_VALUE_TYPE_UINT64: - DBexecute("insert into history_uint (clock,itemid,value) values (%d," ZBX_FS_UI64 "," ZBX_FS_UI64 ");\n", - history->clock, - history->itemid, - history->value.value_uint64); + if (itemid == history[i].itemid) + { + h = &history[i]; break; - case ITEM_VALUE_TYPE_FLOAT: - DBexecute("insert into history (clock,itemid,value) values (%d," ZBX_FS_UI64 "," ZBX_FS_DBL ");\n", - history->clock, - history->itemid, - history->value.value_float); + } + } + + if (NULL == h) + continue; + + exp = strdup(trigger.expression); + + if (evaluate_expression(&exp_value, &exp, &trigger, error, sizeof(error)) != 0) + { + zabbix_log(LOG_LEVEL_WARNING, "Expression [%s] cannot be evaluated [%s]", + trigger.expression, + error); + zabbix_syslog("Expression [%s] cannot be evaluated [%s]", + trigger.expression, + error); +/* We shouldn't update triggervalue if expressions failed */ +/* DBupdate_trigger_value(&trigger, exp_value, time(NULL), error);*/ + } + else + DBupdate_trigger_value(&trigger, exp_value, h->clock, NULL); + + zbx_free(exp); + } + DBfree_result(result); +} + +/****************************************************************************** + * * + * Function: DCmass_update_item * + * * + * Purpose: update items info after new values is received * + * * + * Parameters: history - array of history data * + * history_num - number of history structures * + * * + * Author: Alexei Vladishev, Eugene Grigorjev, Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void DCmass_update_item(ZBX_DC_HISTORY *history, int history_num) +{ + DB_RESULT result; + DB_ROW row; + DB_ITEM item; + char value_esc[ITEM_LASTVALUE_LEN_MAX]; + int sql_offset = 0, i; + ZBX_DC_HISTORY *h; + double value_float; + zbx_uint64_t value_uint64; + + zabbix_log( LOG_LEVEL_DEBUG, "In DCmass_update_item()"); + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 1024, + "select %s where h.hostid = i.hostid and i.itemid in (", + ZBX_SQL_ITEM_SELECT, + TRIGGER_STATUS_ENABLED); + + for (i = 0; i < history_num; i++) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 32, ZBX_FS_UI64 ",", + history[i].itemid); + + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 32, ")"); + } + + result = DBselect("%s", sql); + + sql_offset = 0; + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "begin\n"); +#endif + + while (NULL != (row = DBfetch(result))) + { + DBget_item_from_db(&item, row); + + h = NULL; + + for (i = 0; i < history_num; i++) + { + if (item.itemid == history[i].itemid) + { + h = &history[i]; break; - case ITEM_VALUE_TYPE_STR: - DBescape_string(history->value.value_str,value_esc,MAX_STRING_LEN); - DBexecute("insert into history_str (clock,itemid,value) values (%d," ZBX_FS_UI64 ",'%s');\n", - history->clock, - history->itemid, + } + } + + if (NULL == h) + continue; + +/* if (item.type == ITEM_TYPE_ZABBIX_ACTIVE || item.type == ITEM_TYPE_TRAPPER || item.type == ITEM_TYPE_HTTPTEST)*/ + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "update items set lastclock=%d", + h->clock); +/* else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "update items set nextcheck=%d,lastclock=%d", + calculate_item_nextcheck(item.itemid, item.type, item.delay, item.delay_flex, h->clock), + h->clock);*/ + + if (item.delta == ITEM_STORE_AS_IS) + { + if (h->value_type == ITEM_VALUE_TYPE_FLOAT) + zbx_snprintf(value_esc, sizeof(value_esc), ZBX_FS_DBL, h->value.value_float); + else if (h->value_type == ITEM_VALUE_TYPE_UINT64) + zbx_snprintf(value_esc, sizeof(value_esc), ZBX_FS_UI64, h->value.value_uint64); + else if (h->value_type == ITEM_VALUE_TYPE_STR + || h->value_type == ITEM_VALUE_TYPE_TEXT + || h->value_type == ITEM_VALUE_TYPE_LOG) + DBescape_string(h->value.value_str, value_esc, sizeof(value_esc)); + else + *value_esc = '\0'; + + if (h->value_type == ITEM_VALUE_TYPE_LOG) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "prevvalue=lastvalue,lastvalue='%s',lastlogsize=%d", + value_esc, + h->lastlogsize); + else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, ",prevvalue=lastvalue,lastvalue='%s'", value_esc); - break; - default: - zabbix_log(LOG_LEVEL_CRIT,"Unsupported history value type %d. Database cache corrupted?", - history->value_type); - exit(-1); - break; } + else if (item.delta == ITEM_STORE_SPEED_PER_SECOND) /* Logic for delta as speed of change */ + { + if (h->value_type == ITEM_VALUE_TYPE_FLOAT) + { + if (item.prevorgvalue_null == 0 && item.prevorgvalue_dbl <= h->value.value_float) + { + /* In order to continue normal processing, we assume difference 1 second + Otherwise function update_functions and update_triggers won't work correctly*/ + if (h->clock != item.lastclock) + value_float = (h->value.value_float - item.prevorgvalue_dbl) + / (h->clock - item.lastclock); + else + value_float = h->value.value_float - item.prevorgvalue_dbl; + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + ",prevvalue=lastvalue,prevorgvalue='" ZBX_FS_DBL "'" + ",lastvalue='" ZBX_FS_DBL "'", + h->value.value_float, + value_float); + } + else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + ",prevorgvalue='" ZBX_FS_DBL "'", + h->value.value_float); + } + else if (h->value_type == ITEM_VALUE_TYPE_UINT64) + { + if (item.prevorgvalue_null == 0 && item.prevorgvalue_uint64 <= h->value.value_uint64) + { + if (h->clock != item.lastclock) + value_uint64 = (h->value.value_uint64 - item.prevorgvalue_uint64) + / (h->clock - item.lastclock); + else + value_uint64 = h->value.value_uint64 - item.prevorgvalue_uint64; + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + ",prevvalue=lastvalue,prevorgvalue='" ZBX_FS_UI64 "'" + ",lastvalue='" ZBX_FS_UI64 "'", + h->value.value_uint64, + value_uint64); + } + else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + ",prevorgvalue='" ZBX_FS_DBL "'", + h->value.value_uint64); + } + } + else if (item.delta == ITEM_STORE_SIMPLE_CHANGE) /* Real delta: simple difference between values */ + { + if (h->value_type == ITEM_VALUE_TYPE_FLOAT) + { + if (item.prevorgvalue_null == 0 && item.prevorgvalue_dbl <= h->value.value_float) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + ",prevvalue=lastvalue,prevorgvalue='" ZBX_FS_DBL "'" + ",lastvalue='" ZBX_FS_DBL "'", + h->value.value_float, + h->value.value_float - item.prevorgvalue_dbl); + else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + ",prevorgvalue='" ZBX_FS_DBL "'", + h->value.value_float); + } + else if(h->value_type == ITEM_VALUE_TYPE_UINT64) + { + if (item.prevorgvalue_null == 0 && item.prevorgvalue_uint64 <= h->value.value_uint64) + { + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + ",prevvalue=lastvalue,prevorgvalue='" ZBX_FS_UI64 "'" + ",lastvalue='" ZBX_FS_UI64 "'", + h->value.value_uint64, + h->value.value_uint64 - item.prevorgvalue_uint64); + } + else + { + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + ",prevorgvalue='" ZBX_FS_UI64 "'", + h->value.value_uint64); + } + } + } + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, " where itemid=" ZBX_FS_UI64 ";\n", + item.itemid); + +/* Update item status if required */ + if (item.status == ITEM_STATUS_NOTSUPPORTED) + { + zabbix_log(LOG_LEVEL_WARNING, "Parameter [%s] became supported by agent on host [%s]", + item.key, + item.host_name); + zabbix_syslog("Parameter [%s] became supported by agent on host [%s]", + item.key, + item.host_name); + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, + "update items set status=%d,error='' where itemid=" ZBX_FS_UI64 ";\n", + ITEM_STATUS_ACTIVE, + item.itemid); + } + } + DBfree_result(result); + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "end;\n"); +#endif + + if (sql_offset > 16) /* In ORACLE always present begin..end; */ + DBexecute("%s", sql); +} + +/****************************************************************************** + * * + * Function: DCmass_proxy_update_item * + * * + * Purpose: update items info after new values is received * + * * + * Parameters: history - array of history data * + * history_num - number of history structures * + * * + * Author: Alexei Vladishev, Eugene Grigorjev, Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void DCmass_proxy_update_item(ZBX_DC_HISTORY *history, int history_num) +{ + int sql_offset = 0, i; + + zabbix_log( LOG_LEVEL_DEBUG, "In DCmass_proxy_update_item()"); + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "begin\n"); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_LOG) + { + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, + "update items set lastlogsize=%d where itemid=" ZBX_FS_UI64 ";\n", + history[i].lastlogsize, + history[i].itemid); + } + } + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "end;\n"); +#endif + + if (sql_offset > 16) /* In ORACLE always present begin..end; */ + DBexecute("%s", sql); +} + +/****************************************************************************** + * * + * Function: DCmass_function_update * + * * + * Purpose: update functions lastvalue after new values is received * + * * + * Parameters: history - array of history data * + * history_num - number of history structures * + * * + * Author: Alexei Vladishev, Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void DCmass_function_update(ZBX_DC_HISTORY *history, int history_num) +{ + DB_RESULT result; + DB_ROW row; + DB_FUNCTION function; + DB_ITEM item; + char *lastvalue; + char value[MAX_STRING_LEN], value_esc[MAX_STRING_LEN]; + int sql_offset = 0, i; + + zabbix_log(LOG_LEVEL_DEBUG, "In DCmass_function_update()"); + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 1024, + "select distinct %s,f.function,f.parameter,f.itemid,f.lastvalue from %s,functions f,triggers t" + " where f.itemid=i.itemid and h.hostid=i.hostid and f.triggerid=t.triggerid and t.status in (%d)" + " and f.itemid in (", + ZBX_SQL_ITEM_FIELDS, + ZBX_SQL_ITEM_TABLES, + TRIGGER_STATUS_ENABLED); + + for (i = 0; i < history_num; i++) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 32, ZBX_FS_UI64 ",", + history[i].itemid); + + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 32, ")"); } - for(i=0;i<cache->pool.trends_count;i++) + result = DBselect("%s", sql); + + sql_offset = 0; + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "begin\n"); +#endif + + while (NULL != (row = DBfetch(result))) { - trend = &cache->pool.trends[i]; - zabbix_log(LOG_LEVEL_DEBUG,"Trend " ZBX_FS_UI64, - trend->itemid); - if(trend->operation == ZBX_TREND_OP_INSERT) + DBget_item_from_db(&item, row); + + function.function = row[ZBX_SQL_ITEM_FIELDS_NUM]; + function.parameter = row[ZBX_SQL_ITEM_FIELDS_NUM + 1]; + function.itemid = zbx_atoui64(row[ZBX_SQL_ITEM_FIELDS_NUM + 2]); +/* It is not required to check lastvalue for NULL here */ + lastvalue = row[ZBX_SQL_ITEM_FIELDS_NUM + 3]; + + if (FAIL == evaluate_function(value, &item, function.function, function.parameter)) { - DBexecute("insert into trends (clock,itemid,num,value_min,value_avg,value_max) values (%d," ZBX_FS_UI64 ",%d," ZBX_FS_DBL "," ZBX_FS_DBL "," ZBX_FS_DBL ")", - trend->clock, - trend->itemid, - trend->num, - trend->value_min, - trend->value_avg, - trend->value_max); + zabbix_log(LOG_LEVEL_DEBUG, "Evaluation failed for function:%s", + function.function); + continue; } - else if(trend->operation == ZBX_TREND_OP_UPDATE) + + /* Update only if lastvalue differs from new one */ + if (DBis_null(lastvalue) == SUCCEED || strcmp(lastvalue, value) != 0) { - DBexecute("update trends set num=%d, value_min=" ZBX_FS_DBL ", value_avg=" ZBX_FS_DBL ", value_max=" ZBX_FS_DBL " where itemid=" ZBX_FS_UI64 " and clock=%d", - trend->num, - trend->value_min, - trend->value_avg, - trend->value_max, - trend->itemid, - trend->clock); + DBescape_string(value, value_esc, MAX_STRING_LEN); + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 1024, + "update functions set lastvalue='%s' where itemid=" ZBX_FS_UI64 + " and function='%s' and parameter='%s';\n", + value_esc, + function.itemid, + function.function, + function.parameter); } } + DBfree_result(result); - DBcommit(); +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "end;\n"); +#endif - cache->pool.history_count=0; - cache->pool.trends_count=0; - UNLOCK_CACHE; + if (sql_offset > 16) /* In ORACLE always present begin..end; */ + DBexecute("%s", sql); +} - zabbix_log(LOG_LEVEL_DEBUG,"End of DCsync()"); +/****************************************************************************** + * * + * Function: DCmass_add_history * + * * + * Purpose: inserting new history data after new values is received * + * * + * Parameters: history - array of history data * + * history_num - number of history structures * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void DCmass_add_history(ZBX_DC_HISTORY *history, int history_num) +{ + int sql_offset = 0, i; + char value_esc[MAX_STRING_LEN], *value_esc_dyn; + int history_text_num, history_log_num; + zbx_uint64_t id; +#if defined(HAVE_MYSQL) + int tmp_offset; +#endif + + zabbix_log(LOG_LEVEL_DEBUG, "In DCmass_add_history()"); + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "begin\n"); +#endif + +/* + * history + */ +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into history values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_FLOAT) + { +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "(" ZBX_FS_UI64 ",%d," ZBX_FS_DBL "),", + history[i].itemid, + history[i].clock, + history[i].value.value_float); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "insert into history values " + "(" ZBX_FS_UI64 ",%d," ZBX_FS_DBL ");\n", + history[i].itemid, + history[i].clock, + history[i].value.value_float); +#endif + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + + if (CONFIG_NODE_NOHISTORY == 0 && CONFIG_MASTER_NODEID > 0) + { +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into history_sync (nodeid,itemid,clock,value) values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_FLOAT) + { +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "(%d," ZBX_FS_UI64 ",%d," ZBX_FS_DBL "),", + get_nodeid_by_id(history[i].itemid), + history[i].itemid, + history[i].clock, + history[i].value.value_float); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "insert into history_sync (nodeid,itemid,clock,value) values " + "(%d," ZBX_FS_UI64 ",%d," ZBX_FS_DBL ");\n", + get_nodeid_by_id(history[i].itemid), + history[i].itemid, + history[i].clock, + history[i].value.value_float); +#endif + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + } + +/* + * history_uint + */ +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into history_uint values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_UINT64) + { +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "(" ZBX_FS_UI64 ",%d," ZBX_FS_UI64 "),", + history[i].itemid, + history[i].clock, + history[i].value.value_uint64); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "insert into history_uint values " + "(" ZBX_FS_UI64 ",%d," ZBX_FS_UI64 ");\n", + history[i].itemid, + history[i].clock, + history[i].value.value_uint64); +#endif + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + + if (CONFIG_NODE_NOHISTORY == 0 && CONFIG_MASTER_NODEID > 0) + { +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into history_uint_sync (nodeid,itemid,clock,value) values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_UINT64) + { +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "(%d," ZBX_FS_UI64 ",%d," ZBX_FS_UI64 "),", + get_nodeid_by_id(history[i].itemid), + history[i].itemid, + history[i].clock, + history[i].value.value_uint64); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "insert into history_uint_sync (nodeid,itemid,clock,value) values " + "(%d," ZBX_FS_UI64 ",%d," ZBX_FS_UI64 ");\n", + get_nodeid_by_id(history[i].itemid), + history[i].itemid, + history[i].clock, + history[i].value.value_uint64); +#endif + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + } + +/* + * history_str + */ +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into history_str values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_STR) + { + DBescape_string(history[i].value.value_str, value_esc, sizeof(value_esc)); +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "(" ZBX_FS_UI64 ",%d,'%s'),", + history[i].itemid, + history[i].clock, + value_esc); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "insert into history_str values " + "(" ZBX_FS_UI64 ",%d,'%s');\n", + history[i].itemid, + history[i].clock, + value_esc); +#endif + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + + if (CONFIG_NODE_NOHISTORY == 0 && CONFIG_MASTER_NODEID > 0) + { +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into history_str_sync (nodeid,itemid,clock,value) values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_STR) + { + DBescape_string(history[i].value.value_str, value_esc, sizeof(value_esc)); +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "(%d," ZBX_FS_UI64 ",%d,'%s'),", + get_nodeid_by_id(history[i].itemid), + history[i].itemid, + history[i].clock, + value_esc); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "insert into history_str_sync (nodeid,itemid,clock,value) values " + "(%d," ZBX_FS_UI64 ",%d,'%s');\n", + get_nodeid_by_id(history[i].itemid), + history[i].itemid, + history[i].clock, + value_esc); +#endif + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + } + + history_text_num = 0; + history_log_num = 0; + + for (i = 0; i < history_num; i++) + if (history[i].value_type == ITEM_VALUE_TYPE_TEXT) + history_text_num++; + else if (history[i].value_type == ITEM_VALUE_TYPE_LOG) + history_log_num++; + +/* + * history_text + */ + if (history_text_num > 0) + { + id = DBget_maxid_num("history_text", "id", history_text_num); + +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into history_text values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_TEXT) + { + value_esc_dyn = DBdyn_escape_string(history[i].value.value_str); +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4096, + "(" ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,'%s'),", + id, + history[i].itemid, + history[i].clock, + value_esc_dyn); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4096, + "insert into history_text values " + "(" ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,'%s');\n", + id, + history[i].itemid, + history[i].clock, + value_esc_dyn); +#endif + zbx_free(value_esc_dyn); + id++; + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + } + +/* + * history_log + */ + if (history_log_num > 0) + { + id = DBget_maxid_num("history_log", "id", history_log_num); + +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into history_log values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_LOG) + { + value_esc_dyn = DBdyn_escape_string(history[i].value.value_str); +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4096, + "(" ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,%d,'%s',%d,'%s'),", + id, + history[i].itemid, + history[i].clock, + history[i].timestamp, + history[i].source, + history[i].severity, + value_esc_dyn); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4096, + "insert into history_log values " + "(" ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,%d,'%s',%d,'%s');\n", + id, + history[i].itemid, + history[i].clock, + history[i].timestamp, + history[i].source, + history[i].severity, + value_esc_dyn); +#endif + zbx_free(value_esc_dyn); + id++; + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + } + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "end;\n"); +#endif + + if (sql_offset > 16) /* In ORACLE always present begin..end; */ + DBexecute("%s", sql); } -void DCshow() +/****************************************************************************** + * * + * Function: DCmass_proxy_add_history * + * * + * Purpose: inserting new history data after new values is received * + * * + * Parameters: history - array of history data * + * history_num - number of history structures * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void DCmass_proxy_add_history(ZBX_DC_HISTORY *history, int history_num) { - int i; + int sql_offset = 0, i; + char value_esc[MAX_STRING_LEN], *value_esc_dyn; +#if defined(HAVE_MYSQL) + int tmp_offset; +#endif - ZBX_DC_HISTORY *history; - ZBX_DC_TREND *trend; + zabbix_log(LOG_LEVEL_DEBUG, "In DCmass_proxy_add_history()"); - zabbix_log(LOG_LEVEL_WARNING,"In DCshow(items %d pool:trends %d pool:history:%d)", - cache->items_count, - cache->pool.trends_count, - cache->pool.history_count); +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "begin\n"); +#endif - LOCK_CACHE; - for(i=0;i<cache->pool.history_count;i++) +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into proxy_history (itemid,clock,value) values "); +#endif + + for (i = 0; i < history_num; i++) { - history = &cache->pool.history[i]; - zabbix_log(LOG_LEVEL_DEBUG,"History " ZBX_FS_UI64, - history->itemid); + if (history[i].value_type == ITEM_VALUE_TYPE_FLOAT) + { +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "(" ZBX_FS_UI64 ",%d,'" ZBX_FS_DBL "'),", + history[i].itemid, + history[i].clock, + history[i].value.value_float); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "insert into proxy_history (itemid,clock,value) values " + "(" ZBX_FS_UI64 ",%d,'" ZBX_FS_DBL "');\n", + history[i].itemid, + history[i].clock, + history[i].value.value_float); +#endif + } } - for(i=0;i<cache->pool.trends_count;i++) +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') { - trend = &cache->pool.trends[i]; - zabbix_log(LOG_LEVEL_DEBUG,"History " ZBX_FS_UI64, - trend->itemid); + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); } - UNLOCK_CACHE; + else + sql_offset = tmp_offset; +#endif + +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into proxy_history (itemid,clock,value) values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_UINT64) + { +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "(" ZBX_FS_UI64 ",%d,'" ZBX_FS_UI64 "'),", + history[i].itemid, + history[i].clock, + history[i].value.value_uint64); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "insert into proxy_history (itemid,clock,value) values " + "(" ZBX_FS_UI64 ",%d,'" ZBX_FS_UI64 "');\n", + history[i].itemid, + history[i].clock, + history[i].value.value_uint64); +#endif + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into proxy_history (itemid,clock,value) values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_STR) + { + DBescape_string(history[i].value.value_str, value_esc, sizeof(value_esc)); +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "(" ZBX_FS_UI64 ",%d,'%s'),", + history[i].itemid, + history[i].clock, + value_esc); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, + "insert into proxy_history (itemid,clock,value) values " + "(" ZBX_FS_UI64 ",%d,'%s');\n", + history[i].itemid, + history[i].clock, + value_esc); +#endif + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into proxy_history (itemid,clock,value) values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_TEXT) + { + value_esc_dyn = DBdyn_escape_string(history[i].value.value_str); +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4096, + "(" ZBX_FS_UI64 ",%d,'%s'),", + history[i].itemid, + history[i].clock, + value_esc_dyn); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4096, + "insert into proxy_history (itemid,clock,value) values " + "(" ZBX_FS_UI64 ",%d,'%s');\n", + history[i].itemid, + history[i].clock, + value_esc_dyn); +#endif + zbx_free(value_esc_dyn); + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + +#if defined(HAVE_MYSQL) + tmp_offset = sql_offset; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 64, + "insert into proxy_history (itemid,clock,timestamp,source,severity,value) values "); +#endif + + for (i = 0; i < history_num; i++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_LOG) + { + value_esc_dyn = DBdyn_escape_string(history[i].value.value_str); +#if defined(HAVE_MYSQL) + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4096, + "(" ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,%d,'%s',%d,'%s'),", + history[i].itemid, + history[i].clock, + history[i].timestamp, + history[i].source, + history[i].severity, + value_esc_dyn); +#else + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4096, + "insert into proxy_history (itemid,cloc,timestamp,source,severityk,value) values " + "(" ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,%d,'%s',%d,'%s');\n", + history[i].itemid, + history[i].clock, + history[i].timestamp, + history[i].source, + history[i].severity, + value_esc_dyn); +#endif + zbx_free(value_esc_dyn); + } + } + +#if defined(HAVE_MYSQL) + if (sql[sql_offset - 1] == ',') + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ";\n"); + } + else + sql_offset = tmp_offset; +#endif + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "end;\n"); +#endif - zabbix_log(LOG_LEVEL_DEBUG,"End of DCshow()"); + if (sql_offset > 16) /* In ORACLE always present begin..end; */ + DBexecute("%s", sql); } -static ZBX_DC_ITEM *get_item(zbx_uint64_t itemid) +static int DCitem_already_exists(ZBX_DC_HISTORY *history, int history_num, zbx_uint64_t itemid) { int i; - ZBX_DC_ITEM *item = NULL; - int found = 0; - for(i=0;i<cache->items_count;i++) + for (i = 0; i < history_num; i++) + if (itemid == history[i].itemid) + return SUCCEED; + + return FAIL; +} + +/****************************************************************************** + * * + * Function: DCsync * + * * + * Purpose: writes updates and new data from pool to database * + * * + * Parameters: * + * * + * Return value: number of synced values * + * * + * Author: Alexei Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +int DCsync_history(int sync_type) +{ + static ZBX_DC_HISTORY history[ZBX_SYNC_MAX]; + int i, j, history_num, n, f; + int syncs; + int total_num = 0; +/* double sec; + sec = zbx_time();*/ + + zabbix_log(LOG_LEVEL_DEBUG, "In DCsync_history(history_first:%d history_num:%d)", + cache->history_first, + cache->history_num); + + if (0 == cache->history_num) + return 0; + + syncs = cache->history_num / ZBX_SYNC_MAX; + + do { - item = &cache->items[i]; - if(item->itemid == itemid) + LOCK_CACHE; + + history_num = 0; + n = cache->history_num; + f = cache->history_first; + + while (n > 0 && history_num < ZBX_SYNC_MAX) { - found = 1; + if (zbx_process == ZBX_PROCESS_PROXY || FAIL == DCitem_already_exists(history, history_num, cache->history[f].itemid)) + { + memcpy(&history[history_num], &cache->history[f], sizeof(ZBX_DC_HISTORY)); + if (history[history_num].value_type == ITEM_VALUE_TYPE_STR + || history[history_num].value_type == ITEM_VALUE_TYPE_TEXT + || history[history_num].value_type == ITEM_VALUE_TYPE_LOG) + { + history[history_num].value.value_str = strdup(cache->history[f].value.value_str); + + if (history[history_num].value_type == ITEM_VALUE_TYPE_LOG) + history[history_num].source = strdup(cache->history[f].source); + } + + for (j = f; j != cache->history_first; j = (j == 0 ? ZBX_HISTORY_SIZE : j) - 1) + { + i = (j == 0 ? ZBX_HISTORY_SIZE : j) - 1; + memcpy(&cache->history[j], &cache->history[i], sizeof(ZBX_DC_HISTORY)); + } + + cache->history_num--; + cache->history_first++; + cache->history_first = cache->history_first % ZBX_HISTORY_SIZE; + + history_num++; + } + + n--; + f++; + f = f % ZBX_HISTORY_SIZE; + } + + UNLOCK_CACHE; + + if (0 == history_num) break; + + if (NULL == sql) + sql = zbx_malloc(sql, sql_allocated); + + DBbegin(); + + if (zbx_process == ZBX_PROCESS_SERVER) + { + DCmass_add_history(history, history_num); + DCmass_update_item(history, history_num); + DCmass_function_update(history, history_num); + DCmass_update_triggers(history, history_num); } - } + else + { + DCmass_proxy_add_history(history, history_num); + DCmass_proxy_update_item(history, history_num); + } + + DBcommit(); - if(found == 0) + for (i = 0; i < history_num; i ++) + { + if (history[i].value_type == ITEM_VALUE_TYPE_STR + || history[i].value_type == ITEM_VALUE_TYPE_TEXT + || history[i].value_type == ITEM_VALUE_TYPE_LOG) + { + zbx_free(history[i].value.value_str); + + if (history[i].value_type == ITEM_VALUE_TYPE_LOG) + zbx_free(history[i].source); + } + } + total_num += history_num; + } while (--syncs > 0 || sync_type == ZBX_SYNC_FULL); + +/* zabbix_log(LOG_LEVEL_CRIT, "DCsync_history first:%6d; cache:%6d; synced:%4d; spent " ZBX_FS_DBL " seconds", + cache->history_first, + cache->history_num, + total_num, + zbx_time() - sec);*/ + return total_num; +} + +/****************************************************************************** + * * + * Function: DCvacuum_text * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void DCvacuum_text() +{ + char *first_text; + int i, index; + size_t offset; + + zabbix_log(LOG_LEVEL_DEBUG, "In DCvacuum_text()"); + + /* vacuumng text buffer */ + first_text = NULL; + for (i = 0; i < cache->history_num; i++) { - item=&cache->items[cache->items_count]; - item->itemid=itemid; - cache->items_count++; + index = (cache->history_first + i) % ZBX_HISTORY_SIZE; + if (cache->history[index].value_type == ITEM_VALUE_TYPE_STR + || cache->history[index].value_type == ITEM_VALUE_TYPE_TEXT + || cache->history[index].value_type == ITEM_VALUE_TYPE_LOG) + { + first_text = cache->history[index].value.value_str; + break; + } } + if (NULL != first_text) + { + offset = first_text - cache->text; + for (i = 0; i < cache->history_num; i++) + { + index = (cache->history_first + i) % ZBX_HISTORY_SIZE; + if (cache->history[index].value_type == ITEM_VALUE_TYPE_STR + || cache->history[index].value_type == ITEM_VALUE_TYPE_TEXT + || cache->history[index].value_type == ITEM_VALUE_TYPE_LOG) + { + cache->history[index].value.value_str -= offset; - return item; + if (cache->history[index].value_type == ITEM_VALUE_TYPE_LOG) + cache->history[index].source -= offset; + } + } + cache->last_text -= offset; + } else + cache->last_text = cache->text; } -int DCadd_trend(zbx_uint64_t itemid, double value, int clock) +/****************************************************************************** + * * + * Function: DCget_history_ptr * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static ZBX_DC_HISTORY *DCget_history_ptr(zbx_uint64_t itemid, size_t text_len) { - int hour; - ZBX_DC_TREND *trend = NULL, - *trend_tmp = NULL; - ZBX_DC_ITEM *item = NULL; - DB_RESULT result; - DB_ROW row; - int trend_found=0; + ZBX_DC_HISTORY *history; + int index; + size_t free_len; - zabbix_log(LOG_LEVEL_DEBUG,"In DCadd_trend()"); +retry: + if (cache->history_num >= ZBX_HISTORY_SIZE) + { + UNLOCK_CACHE; - LOCK_CACHE; - hour=clock-clock%3600; + zabbix_log(LOG_LEVEL_DEBUG, "History buffer is full. Sleeping for 1 second."); + sleep(1); - item=get_item(itemid); + LOCK_CACHE; - trend=&item->trend; + goto retry; + } - if(hour == trend->clock) + if (text_len > sizeof(cache->text)) { - trend_found=1; + zabbix_log(LOG_LEVEL_ERR, "Insufficient shared memory"); + exit(-1); } - else if(trend->clock !=0) + + free_len = sizeof(cache->text) - (cache->last_text - cache->text); + + if (text_len > free_len) { -// add_trend2pool(trend); - trend_tmp=&cache->pool.trends[cache->pool.trends_count]; - cache->pool.trends_count++; + DCvacuum_text(); + + free_len = sizeof(cache->text) - (cache->last_text - cache->text); + + if (text_len > free_len) + { + UNLOCK_CACHE; - trend_tmp->operation = trend->operation; - trend_tmp->itemid = trend->itemid; - trend_tmp->clock = trend->clock; - trend_tmp->num = trend->num; - trend_tmp->value_min = trend->value_min; - trend_tmp->value_max = trend->value_max; - trend_tmp->value_avg = trend->value_avg; + zabbix_log(LOG_LEVEL_DEBUG, "History text buffer is full. Sleeping for 1 second."); + sleep(1); - trend->clock = 0; + LOCK_CACHE; + + goto retry; + } } - /* Not found with the same clock */ - if(0 == trend_found) + index = (cache->history_first + cache->history_num) % ZBX_HISTORY_SIZE; + history = &cache->history[index]; + + cache->history_num++; + + return history; +} + +/****************************************************************************** + * * + * Function: DCget_trend_nearestindex * + * * + * Purpose: find nearest index by itemid in array of ZBX_DC_TREND * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static int DCget_trend_nearestindex(zbx_uint64_t itemid) +{ + int first_index, last_index, index; + + if (cache->trends_num == 0) + return 0; + + first_index = 0; + last_index = cache->trends_num - 1; + while (1) { - zabbix_log(LOG_LEVEL_DEBUG,"Not found"); - /* Add new, do not look at the database */ - trend->operation = ZBX_TREND_OP_INSERT; - trend->itemid = itemid; - trend->clock = hour; - trend->num = 1; - trend->value_min = value; - trend->value_max = value; - trend->value_avg = value; + index = first_index + (last_index - first_index) / 2; - /* Try to find in the database */ - result = DBselect("select num,value_min,value_avg,value_max from trends where itemid=" ZBX_FS_UI64 " and clock=%d", - itemid, - hour); + if (cache->trends[index].itemid == itemid) + return index; + else if (last_index == first_index) + { + if (cache->trends[index].itemid < itemid) + index++; + return index; + } + else if (cache->trends[index].itemid < itemid) + first_index = index + 1; + else + last_index = index; + } +} - row=DBfetch(result); +/****************************************************************************** + * * + * Function: DCget_trend * + * * + * Purpose: find existing or add new structure and return pointer * + * * + * Parameters: * + * * + * Return value: pointer to a new structure or NULL if array is full * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static ZBX_DC_TREND *DCget_trend(zbx_uint64_t itemid) +{ + int index; + + index = DCget_trend_nearestindex(itemid); + if (index < cache->trends_num && cache->trends[index].itemid == itemid) + return &cache->trends[index]; + + if (cache->trends_num == ZBX_TREND_SIZE) + return NULL; + + memmove(&cache->trends[index + 1], &cache->trends[index], sizeof(ZBX_DC_TREND) * (cache->trends_num - index)); + memset(&cache->trends[index], 0, sizeof(ZBX_DC_TREND)); + cache->trends[index].itemid = itemid; + cache->trends_num++; - if(row) + return &cache->trends[index]; +} + +/****************************************************************************** + * * + * Function: DCflush_trend * + * * + * Purpose: flush trend to the database * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static void DCflush_trend(ZBX_DC_TREND *trend) +{ + DB_RESULT result; + DB_ROW row; + int num; + trend_value_t value_min, value_avg, value_max; + + switch (trend->value_type) + { + case ITEM_VALUE_TYPE_FLOAT: + result = DBselect("select num,value_min,value_avg,value_max from trends" + " where itemid=" ZBX_FS_UI64 " and clock=%d", + trend->itemid, + trend->clock); + break; + case ITEM_VALUE_TYPE_UINT64: + result = DBselect("select num,value_min,value_avg,value_max from trends_uint" + " where itemid=" ZBX_FS_UI64 " and clock=%d", + trend->itemid, + trend->clock); + break; + default: + zabbix_log(LOG_LEVEL_CRIT, "Invalid value type for trends."); + exit(-1); + } + + if (NULL != (row = DBfetch(result))) + { + num = atoi(row[0]); + switch (trend->value_type) { - trend->operation = ZBX_TREND_OP_UPDATE; - trend->itemid = itemid; - trend->clock = hour; - trend->num = atoi(row[0]); - trend->value_min = atof(row[1]); - trend->value_avg = atof(row[2]); - trend->value_max = atof(row[3]); - if(value<trend->value_min) trend->value_min=value; - if(value>trend->value_max) trend->value_max=value; - trend->value_avg = (trend->num*trend->value_avg+value)/(trend->num+1); - trend->num++; + case ITEM_VALUE_TYPE_FLOAT: + value_min.value_float = atof(row[1]); + value_avg.value_float = atof(row[2]); + value_max.value_float = atof(row[3]); + + if (value_min.value_float < trend->value_min.value_float) + trend->value_min.value_float = value_min.value_float; + if (value_max.value_float > trend->value_max.value_float) + trend->value_max.value_float = value_max.value_float; + trend->value_avg.value_float = (trend->num * trend->value_avg.value_float + + num * value_avg.value_float) / (trend->num + num); + trend->num += num; + + DBexecute("update trends set num=%d,value_min=" ZBX_FS_DBL ",value_avg=" ZBX_FS_DBL + ",value_max=" ZBX_FS_DBL " where itemid=" ZBX_FS_UI64 " and clock=%d", + trend->num, + trend->value_min.value_float, + trend->value_avg.value_float, + trend->value_max.value_float, + trend->itemid, + trend->clock); + break; + case ITEM_VALUE_TYPE_UINT64: + value_min.value_uint64 = zbx_atoui64(row[1]); + value_avg.value_uint64 = zbx_atoui64(row[2]); + value_max.value_uint64 = zbx_atoui64(row[3]); + + if (value_min.value_uint64 < trend->value_min.value_uint64) + trend->value_min.value_uint64 = value_min.value_uint64; + if (value_max.value_uint64 > trend->value_max.value_uint64) + trend->value_max.value_uint64 = value_max.value_uint64; + trend->value_avg.value_uint64 = (trend->num * trend->value_avg.value_uint64 + + num * value_avg.value_uint64) / (trend->num + num); + trend->num += num; + + DBexecute("update trends_uint set num=%d,value_min=" ZBX_FS_UI64 ",value_avg=" ZBX_FS_UI64 + ",value_max=" ZBX_FS_UI64 " where itemid=" ZBX_FS_UI64 " and clock=%d", + trend->num, + trend->value_min.value_uint64, + trend->value_avg.value_uint64, + trend->value_max.value_uint64, + trend->itemid, + trend->clock); + break; } - DBfree_result(result); } else { - zabbix_log(LOG_LEVEL_DEBUG,"Found"); - if(value<trend->value_min) trend->value_min=value; - if(value>trend->value_max) trend->value_max=value; - trend->value_avg=(trend->num*trend->value_avg+value)/(trend->num+1); - trend->num++; + switch (trend->value_type) + { + case ITEM_VALUE_TYPE_FLOAT: + DBexecute("insert into trends (itemid,clock,num,value_min,value_avg,value_max)" + " values (" ZBX_FS_UI64 ",%d,%d," ZBX_FS_DBL "," ZBX_FS_DBL "," ZBX_FS_DBL ")", + trend->itemid, + trend->clock, + trend->num, + trend->value_min.value_float, + trend->value_avg.value_float, + trend->value_max.value_float); + break; + case ITEM_VALUE_TYPE_UINT64: + DBexecute("insert into trends_uint (itemid,clock,num,value_min,value_avg,value_max)" + " values (" ZBX_FS_UI64 ",%d,%d," ZBX_FS_UI64 "," ZBX_FS_UI64 "," ZBX_FS_UI64 ")", + trend->itemid, + trend->clock, + trend->num, + trend->value_min.value_uint64, + trend->value_avg.value_uint64, + trend->value_max.value_uint64); + break; + } } - UNLOCK_CACHE; + DBfree_result(result); - zabbix_log(LOG_LEVEL_DEBUG,"End of add_trend()"); + trend->clock = 0; + trend->num = 0; + memset(&trend->value_min, 0, sizeof(trend_value_t)); + memset(&trend->value_avg, 0, sizeof(trend_value_t)); + memset(&trend->value_max, 0, sizeof(trend_value_t)); +} - return SUCCEED; +/****************************************************************************** + * * + * Function: DCsync_trends * + * * + * Purpose: flush all trends to the database * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCsync_trends() +{ + int i; + + zabbix_log(LOG_LEVEL_DEBUG, "In DCsync_trends(trends_num: %d)", + cache->trends_num); + + for (i = 0; i < cache->trends_num; i ++) + DCflush_trend(&cache->trends[i]); + + cache->trends_num = 0; + + zabbix_log(LOG_LEVEL_DEBUG, "End of DCsync_trends()"); } -int DCadd_history(zbx_uint64_t itemid, double value, int clock) +/****************************************************************************** + * * + * Function: DCadd_trend * + * * + * Purpose: add new value to the trends * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCadd_trend(ZBX_DC_HISTORY *history) { - ZBX_DC_HISTORY *history = NULL; + ZBX_DC_TREND *trend = NULL, trend_static; + int clock; + + clock = history->clock - history->clock % 3600; + + if (NULL != (trend = DCget_trend(history->itemid))) + { + if (trend->num > 0 && (trend->clock != clock || trend->value_type != history->value_type)) + DCflush_trend(trend); + + trend->value_type = history->value_type; + trend->clock = clock; + + switch (trend->value_type) + { + case ITEM_VALUE_TYPE_FLOAT: + if (trend->num == 0 || history->value.value_float < trend->value_min.value_float) + trend->value_min.value_float = history->value.value_float; + if (trend->num == 0 || history->value.value_float > trend->value_max.value_float) + trend->value_max.value_float = history->value.value_float; + trend->value_avg.value_float = (trend->num * trend->value_avg.value_float + + history->value.value_float) / (trend->num + 1); + trend->num++; + break; + case ITEM_VALUE_TYPE_UINT64: + if (trend->num == 0 || history->value.value_uint64 < trend->value_min.value_uint64) + trend->value_min.value_uint64 = history->value.value_uint64; + if (trend->num == 0 || history->value.value_uint64 > trend->value_max.value_uint64) + trend->value_max.value_uint64 = history->value.value_uint64; + trend->value_avg.value_uint64 = (trend->num * trend->value_avg.value_uint64 + + history->value.value_uint64) / (trend->num + 1); + trend->num++; + break; + } + } + else + { + zabbix_log(LOG_LEVEL_WARNING, "Insufficient space for trends. Flushing to disk."); + + trend_static.itemid = history->itemid; + trend_static.clock = clock; + trend_static.value_type = history->value_type; + trend_static.num = 1; + switch (trend_static.value_type) + { + case ITEM_VALUE_TYPE_FLOAT: + trend_static.value_min.value_float = history->value.value_float; + trend_static.value_avg.value_float = history->value.value_float; + trend_static.value_max.value_float = history->value.value_float; + break; + case ITEM_VALUE_TYPE_UINT64: + trend_static.value_min.value_uint64 = history->value.value_uint64; + trend_static.value_avg.value_uint64 = history->value.value_uint64; + trend_static.value_max.value_uint64 = history->value.value_uint64; + break; + } + + DCflush_trend(trend); + } +} - zabbix_log(LOG_LEVEL_DEBUG,"In DCadd_history(itemid:" ZBX_FS_UI64 ")", - itemid); +/****************************************************************************** + * * + * Function: DCadd_history * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCadd_history(zbx_uint64_t itemid, double value, int clock) +{ + ZBX_DC_HISTORY *history; LOCK_CACHE; - history=&cache->pool.history[cache->pool.history_count]; - cache->pool.history_count++; + history = DCget_history_ptr(itemid, 0); history->itemid = itemid; history->clock = clock; history->value_type = ITEM_VALUE_TYPE_FLOAT; history->value.value_float = value; - UNLOCK_CACHE; + DCadd_trend(history); - return SUCCEED; + UNLOCK_CACHE; } -int DCadd_history_uint(zbx_uint64_t itemid, zbx_uint64_t value, int clock) +/****************************************************************************** + * * + * Function: DCadd_history_uint * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCadd_history_uint(zbx_uint64_t itemid, zbx_uint64_t value, int clock) { - ZBX_DC_HISTORY *history = NULL; - - zabbix_log(LOG_LEVEL_DEBUG,"In DCadd_history_uint(itemid:" ZBX_FS_UI64 ")", - itemid); + ZBX_DC_HISTORY *history; LOCK_CACHE; - history=&cache->pool.history[cache->pool.history_count]; - cache->pool.history_count++; + + history = DCget_history_ptr(itemid, 0); history->itemid = itemid; history->clock = clock; history->value_type = ITEM_VALUE_TYPE_UINT64; history->value.value_uint64 = value; - UNLOCK_CACHE; - - zabbix_log(LOG_LEVEL_DEBUG,"End of DCadd_history_uint()"); + DCadd_trend(history); - return SUCCEED; + UNLOCK_CACHE; } -int DCadd_history_str(zbx_uint64_t itemid, char *value, int clock) +/****************************************************************************** + * * + * Function: DCadd_history_str * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCadd_history_str(zbx_uint64_t itemid, char *value, int clock) { - ZBX_DC_HISTORY *history = NULL; - - zabbix_log(LOG_LEVEL_DEBUG,"In DCadd_history_uint(itemid:" ZBX_FS_UI64 ")", - itemid); + ZBX_DC_HISTORY *history; + size_t len; LOCK_CACHE; - history=&cache->pool.history[cache->pool.history_count]; - cache->pool.history_count++; + + len = strlen(value) + 1; + history = DCget_history_ptr(itemid, len); history->itemid = itemid; history->clock = clock; history->value_type = ITEM_VALUE_TYPE_STR; - strscpy(history->value.value_str,value); + history->value.value_str = cache->last_text; + zbx_strlcpy(cache->last_text, value, len); + cache->last_text += len; UNLOCK_CACHE; +} - return SUCCEED; +/****************************************************************************** + * * + * Function: DCadd_history_text * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCadd_history_text(zbx_uint64_t itemid, char *value, int clock) +{ + ZBX_DC_HISTORY *history; + size_t len; + + LOCK_CACHE; + + len = strlen(value) + 1; + history = DCget_history_ptr(itemid, len); + + history->itemid = itemid; + history->clock = clock; + history->value_type = ITEM_VALUE_TYPE_TEXT; + history->value.value_str = cache->last_text; + zbx_strlcpy(cache->last_text, value, len); + cache->last_text += len; + + UNLOCK_CACHE; } +/****************************************************************************** + * * + * Function: DCadd_history_log * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alekasander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCadd_history_log(zbx_uint64_t itemid, char *value, int clock, int timestamp, char *source, int severity, int lastlogsize) +{ + ZBX_DC_HISTORY *history; + size_t len1, len2; + + LOCK_CACHE; + + len1 = strlen(value) + 1; + len2 = strlen(source) + 1; + history = DCget_history_ptr(itemid, len1 + len2); + + history->itemid = itemid; + history->clock = clock; + history->value_type = ITEM_VALUE_TYPE_LOG; + history->value.value_str = cache->last_text; + zbx_strlcpy(cache->last_text, value, len1); + cache->last_text += len1; + history->timestamp = timestamp; + history->source = cache->last_text; + zbx_strlcpy(cache->last_text, source, len2); + cache->last_text += len2; + history->severity = severity; + history->lastlogsize = lastlogsize; + + UNLOCK_CACHE; +} /****************************************************************************** * * @@ -375,7 +1890,7 @@ int DCadd_history_str(zbx_uint64_t itemid, char *value, int clock) * Comments: * * * ******************************************************************************/ -void init_database_cache(void) +void init_database_cache(zbx_process_t p) { #define ZBX_MAX_ATTEMPTS 10 int attempts = 0; @@ -383,6 +1898,8 @@ void init_database_cache(void) key_t shm_key; int shm_id; + zbx_process = p; + ZBX_GET_SHM_DBCACHE_KEY(shm_key); lbl_create: @@ -427,13 +1944,15 @@ lbl_create: zbx_error("Unable to create mutex for database cache"); exit(FAIL); } + + cache->last_text = cache->text; } /****************************************************************************** * * - * Function: free_database_cache * + * Function: DCsync_all * * * - * Purpose: Free memory aloccated for database cache * + * Purpose: writes updates and new data from pool and cache data to database * * * * Parameters: * * * @@ -444,46 +1963,22 @@ lbl_create: * Comments: * * * ******************************************************************************/ -void free_database_cache(void) +static void DCsync_all() { + zabbix_log(LOG_LEVEL_DEBUG,"In DCsync_all()"); - key_t shm_key; - int shm_id; - - zabbix_log(LOG_LEVEL_WARNING,"In free_database_cache()"); - - if(NULL == cache) return; - - DCsync_all(); - - LOCK_CACHE; - - ZBX_GET_SHM_DBCACHE_KEY(shm_key); - - shm_id = shmget(shm_key, sizeof(ZBX_DC_CACHE), 0); - - if (-1 == shm_id) - { - zabbix_log(LOG_LEVEL_ERR, "Can't find shared memory for database cache. [%s]",strerror(errno)); - exit(1); - } + DCsync_history(ZBX_SYNC_FULL); + if (zbx_process == ZBX_PROCESS_SERVER) + DCsync_trends(); - shmctl(shm_id, IPC_RMID, 0); - - cache = NULL; - - UNLOCK_CACHE; - - zbx_mutex_destroy(&cache_lock); - - zabbix_log(LOG_LEVEL_WARNING,"End of free_database_cache()"); + zabbix_log(LOG_LEVEL_DEBUG,"End of DCsync_all()"); } /****************************************************************************** * * - * Function: DCsync_all * + * Function: free_database_cache * * * - * Purpose: writes updates and new data from pool and cache data to database * + * Purpose: Free memory aloccated for database cache * * * * Parameters: * * * @@ -494,61 +1989,38 @@ void free_database_cache(void) * Comments: * * * ******************************************************************************/ -void DCsync_all() +void free_database_cache() { - int i; - ZBX_DC_ITEM *item; - ZBX_DC_TREND *trend; + key_t shm_key; + int shm_id; - zabbix_log(LOG_LEVEL_WARNING,"In DCsync_all(items %d pool:trends %d pool:history:%d)", - cache->items_count, - cache->pool.trends_count, - cache->pool.history_count); + zabbix_log(LOG_LEVEL_DEBUG, "In free_database_cache()"); - DCsync(); + if (NULL == cache) + return; + + DCsync_all(); LOCK_CACHE; - DBbegin(); + + ZBX_GET_SHM_DBCACHE_KEY(shm_key); - zabbix_log(LOG_LEVEL_WARNING,"In items_count %d", - cache->items_count); + shm_id = shmget(shm_key, sizeof(ZBX_DC_CACHE), 0); - for(i=0;i<cache->items_count;i++) + if (-1 == shm_id) { - item = &cache->items[i]; - trend = &item->trend; - - zabbix_log(LOG_LEVEL_DEBUG,"Trend " ZBX_FS_UI64, - trend->itemid); - - if(trend->clock == 0) continue; - - if(trend->operation == ZBX_TREND_OP_INSERT) - { - DBexecute("insert into trends (clock,itemid,num,value_min,value_avg,value_max) values (%d," ZBX_FS_UI64 ",%d," ZBX_FS_DBL "," ZBX_FS_DBL "," ZBX_FS_DBL ")", - trend->clock, - trend->itemid, - trend->num, - trend->value_min, - trend->value_avg, - trend->value_max); - } - else if(trend->operation == ZBX_TREND_OP_UPDATE) - { - DBexecute("update trends set num=%d, value_min=" ZBX_FS_DBL ", value_avg=" ZBX_FS_DBL ", value_max=" ZBX_FS_DBL " where itemid=" ZBX_FS_UI64 " and clock=%d", - trend->num, - trend->value_min, - trend->value_avg, - trend->value_max, - trend->itemid, - trend->clock); - } + zabbix_log(LOG_LEVEL_ERR, "Can't find shared memory for database cache. [%s]",strerror(errno)); + exit(1); } - DBcommit(); + shmctl(shm_id, IPC_RMID, 0); + + cache = NULL; UNLOCK_CACHE; - zabbix_log(LOG_LEVEL_WARNING,"End of DCsync_all()"); + zbx_mutex_destroy(&cache_lock); + + zabbix_log(LOG_LEVEL_DEBUG,"End of free_database_cache()"); } diff --git a/src/libs/zbxdbcache/nextchecks.c b/src/libs/zbxdbcache/nextchecks.c new file mode 100644 index 00000000..935f2cd9 --- /dev/null +++ b/src/libs/zbxdbcache/nextchecks.c @@ -0,0 +1,213 @@ +/* +** ZABBIX +** Copyright (C) 2000-2007 SIA Zabbix +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +**/ + +#include "common.h" +#include "log.h" + +#include "db.h" +#include "dbcache.h" + +static ZBX_DC_NEXTCHECK *nextchecks = NULL; +static int nextcheck_allocated = 64; +static int nextcheck_num; + +/****************************************************************************** + * * + * Function: DCinit_nextchecks * + * * + * Purpose: initialize nextchecks array * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCinit_nextchecks() +{ + zabbix_log(LOG_LEVEL_DEBUG, "In DCinit_nextchecks()"); + + if (NULL == nextchecks) + nextchecks = zbx_malloc(nextchecks, nextcheck_allocated * sizeof(ZBX_DC_NEXTCHECK)); + + nextcheck_num = 0; +} + +static int DCget_nextcheck_nearestindex(time_t clock) +{ + int first_index, last_index, index; + + if (nextcheck_num == 0) + return 0; + + first_index = 0; + last_index = nextcheck_num - 1; + while (1) + { + index = first_index + (last_index - first_index) / 2; + + if (nextchecks[index].clock == clock) + return index; + else if (last_index == first_index) + { + if (nextchecks[index].clock < clock) + index++; + return index; + } + else if (nextchecks[index].clock < clock) + first_index = index + 1; + else + last_index = index; + } +} + +/****************************************************************************** + * * + * Function: DCadd_nextcheck * + * * + * Purpose: add item nextcheck to the array * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCadd_nextcheck(DB_ITEM *item, time_t now, const char *error_msg) +{ + int index, nextcheck; + + zabbix_log(LOG_LEVEL_DEBUG, "In DCadd_nextcheck()"); + + if (nextcheck_allocated == nextcheck_num) + { + nextcheck_allocated *= 2; + nextchecks = zbx_realloc(nextchecks, nextcheck_allocated * sizeof(ZBX_DC_NEXTCHECK)); + } + + nextcheck = calculate_item_nextcheck(item->itemid, item->type, item->delay, item->delay_flex, now); + index = DCget_nextcheck_nearestindex(nextcheck); + + memmove(&nextchecks[index + 1], &nextchecks[index], sizeof(ZBX_DC_NEXTCHECK) * (nextcheck_num - index)); + + nextchecks[index].itemid = item->itemid; + nextchecks[index].clock = nextcheck; + nextchecks[index].error_msg = (NULL != error_msg) ? strdup(error_msg) : NULL; + + nextcheck_num ++; +} + +/****************************************************************************** + * * + * Function: DCflush_nextchecks * + * * + * Purpose: add item nextcheck to the array * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCflush_nextchecks() +{ + int i, sql_offset = 0; + static char *sql = NULL; + static int sql_allocated = 4096; + time_t last_clock = -1; + char error_esc[ITEM_ERROR_LEN_MAX * 2]; + + zabbix_log(LOG_LEVEL_DEBUG, "In DCflush_nextchecks()"); + + if (nextcheck_num == 0) + return; + + if (sql == NULL) + sql = zbx_malloc(sql, sql_allocated); + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "begin\n"); +#endif + + for (i = 0; i < nextcheck_num; i++ ) + { + if (NULL == nextchecks[i].error_msg) + { + if (last_clock != nextchecks[i].clock) { + if (last_clock != -1) + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ");\n"); + } + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, + "update items set nextcheck=%d where itemid in (", + (int)nextchecks[i].clock); + + last_clock = nextchecks[i].clock; + } + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 32, ZBX_FS_UI64 ",", + nextchecks[i].itemid); + } + } + + if (0 != sql_offset) + { + sql_offset--; + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 4, ");\n"); + } + + for (i = 0; i < nextcheck_num; i++ ) + { + if (NULL != nextchecks[i].error_msg) /* not supported items */ + { + DBescape_string(nextchecks[i].error_msg, error_esc, sizeof(error_esc)); + zbx_free(nextchecks[i].error_msg); + + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 1024, + "update items set status=%d,lastclock=%d,nextcheck=%d,error='%s' where itemid=" ZBX_FS_UI64 ";\n", + ITEM_STATUS_NOTSUPPORTED, + (int)nextchecks[i].clock, + (int)(nextchecks[i].clock + CONFIG_REFRESH_UNSUPPORTED), + error_esc, + nextchecks[i].itemid); + } + } + +#ifdef HAVE_ORACLE + zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "end;\n"); +#endif + + if (0 != sql_offset) + { + DBbegin(); + DBexecute("%s", sql); + DBcommit(); + } +} diff --git a/src/libs/zbxdbhigh/db.c b/src/libs/zbxdbhigh/db.c index 5eeb1a38..d188d873 100644 --- a/src/libs/zbxdbhigh/db.c +++ b/src/libs/zbxdbhigh/db.c @@ -807,191 +807,149 @@ void DBupdate_triggers_status_after_restart(void) return; } -void DBupdate_host_availability(zbx_uint64_t hostid,int available,int clock, char *error) +void DBupdate_host_availability(DB_ITEM *item, int available, int clock, const char *error) { - DB_RESULT result; - DB_ROW row; - char error_esc[MAX_STRING_LEN]; - int disable_until; - - zabbix_log(LOG_LEVEL_DEBUG,"In update_host_availability()"); - - if(error!=NULL) - { - DBescape_string(error,error_esc,MAX_STRING_LEN); - } - else - { - strscpy(error_esc,""); - } - - result = DBselect("select available,disable_until from hosts where hostid=" ZBX_FS_UI64, - hostid); - row=DBfetch(result); - - if(!row) - { - zabbix_log(LOG_LEVEL_ERR, "Cannot select host with hostid [" ZBX_FS_UI64 "]", - hostid); - zabbix_syslog("Cannot select host with hostid [" ZBX_FS_UI64 "]", - hostid); - DBfree_result(result); - return; - } + char error_esc[MAX_STRING_LEN], error_msg[MAX_STRING_LEN]; + int log_level = LOG_LEVEL_WARNING; - disable_until = atoi(row[1]); + zabbix_log(LOG_LEVEL_DEBUG, "In update_host_availability()"); - if(available == atoi(row[0])) + if (item->host_available == available) { -/* if((available==HOST_AVAILABLE_FALSE) - &&(clock+CONFIG_UNREACHABLE_PERIOD>disable_until) ) + if (available == HOST_AVAILABLE_FALSE) { + DBescape_string(error, error_esc, sizeof(error_esc)); + DBexecute("update hosts set error='%s',disable_until=%d where hostid=" ZBX_FS_UI64, + error_esc, + clock + CONFIG_UNAVAILABLE_DELAY, + item->hostid); } - else - {*/ - zabbix_log(LOG_LEVEL_DEBUG, "Host already has availability [%d]", - available); - DBfree_result(result); - return; -/* }*/ + return; } - DBfree_result(result); + item->host_available = available; - if(available==HOST_AVAILABLE_TRUE) + if (available == HOST_AVAILABLE_TRUE) { - DBexecute("update hosts set available=%d,error=' ',errors_from=0 where hostid=" ZBX_FS_UI64, - HOST_AVAILABLE_TRUE, - hostid); + zbx_snprintf(error_msg, sizeof(error_msg), "Enabling host [%s]", + item->host_name); + + DBexecute("update hosts set available=%d,error='',errors_from=0 where hostid=" ZBX_FS_UI64, + available, + item->hostid); + item->host_errors_from = 0; } - else if(available==HOST_AVAILABLE_FALSE) + else if (available == HOST_AVAILABLE_FALSE) { -/* if(disable_until+CONFIG_UNREACHABLE_PERIOD>clock) - { - zbx_snprintf(sql,sizeof(sql),"update hosts set available=%d,disable_until=disable_until+%d,error='%s' where hostid=%d",HOST_AVAILABLE_FALSE,CONFIG_UNREACHABLE_DELAY,error_esc,hostid); - } - else - { - 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 */ - DBexecute("update hosts set available=%d,error='%s ' where hostid=" ZBX_FS_UI64, - HOST_AVAILABLE_FALSE, - error_esc, - hostid); + zbx_snprintf(error_msg, sizeof(error_msg), "Host [%s] will be checked after %d seconds", + item->host_name, + CONFIG_UNAVAILABLE_DELAY); + + DBescape_string(error, error_esc, sizeof(error_esc)); + DBexecute("update hosts set available=%d,error='%s',disable_until=%d where hostid=" ZBX_FS_UI64, + available, + error_esc, + clock + CONFIG_UNAVAILABLE_DELAY, + item->hostid); + + update_triggers_status_to_unknown(item->hostid, clock, "Host is unavailable."); } else { - zabbix_log( LOG_LEVEL_ERR, "Unknown host availability [%d] for hostid [" ZBX_FS_UI64 "]", - available, - hostid); - zabbix_syslog("Unknown host availability [%d] for hostid [" ZBX_FS_UI64 "]", - available, - hostid); - return; + log_level = LOG_LEVEL_ERR; + zbx_snprintf(error_msg, sizeof(error_msg), "Unknown host availability [%d] for host [%s]", + available, + item->host_name); } - update_triggers_status_to_unknown(hostid,clock,"Host is unavailable."); - zabbix_log(LOG_LEVEL_DEBUG,"End of update_host_availability()"); - - return; + zabbix_log(log_level, "%s", error_msg); + zabbix_syslog("%s", error_msg); } -void DBproxy_update_host_availability(zbx_uint64_t hostid, int available, int clock) +void DBproxy_update_host_availability(DB_ITEM *item, int available, int clock) { - DB_RESULT result; - DB_ROW row; - int disable_until; + char error_msg[MAX_STRING_LEN]; + int log_level = LOG_LEVEL_WARNING; - zabbix_log(LOG_LEVEL_DEBUG,"In DBproxy_update_host_availability()"); + zabbix_log(LOG_LEVEL_DEBUG, "In DBproxy_update_host_availability()"); - result = DBselect("select available,disable_until from hosts where hostid=" ZBX_FS_UI64, - hostid); - - if (NULL == (row = DBfetch(result))) { - zabbix_log(LOG_LEVEL_ERR, "Cannot select host with hostid [" ZBX_FS_UI64 "]", - hostid); - zabbix_syslog("Cannot select host with hostid [" ZBX_FS_UI64 "]", - hostid); - goto out; + if (item->host_available == available) + { + if (available == HOST_AVAILABLE_FALSE) + { + DBexecute("update hosts set disable_until=%d where hostid=" ZBX_FS_UI64, + clock + CONFIG_UNAVAILABLE_DELAY, + item->hostid); + } + return; } - disable_until = atoi(row[1]); + item->host_available = available; - if (available == atoi(row[0])) { - zabbix_log(LOG_LEVEL_DEBUG, "Host already has availability [%d]", - available); - goto out; - } + if (available == HOST_AVAILABLE_TRUE) + { + zbx_snprintf(error_msg, sizeof(error_msg), "Enabling host [%s]", + item->host_name); - switch (available) { - case HOST_AVAILABLE_TRUE: DBexecute("update hosts set available=%d,errors_from=0 where hostid=" ZBX_FS_UI64, - HOST_AVAILABLE_TRUE, - hostid); - break; - case HOST_AVAILABLE_FALSE: - DBexecute("update hosts set available=%d where hostid=" ZBX_FS_UI64, - HOST_AVAILABLE_FALSE, - hostid); - break; - default: - zabbix_log( LOG_LEVEL_ERR, "Unknown host availability [%d] for hostid [" ZBX_FS_UI64 "]", available, - hostid); + item->hostid); + item->host_errors_from = 0; } + else if (available == HOST_AVAILABLE_FALSE) + { + zbx_snprintf(error_msg, sizeof(error_msg), "Host [%s] will be checked after %d seconds", + item->host_name, + CONFIG_UNAVAILABLE_DELAY); -out: - DBfree_result(result); - - zabbix_log(LOG_LEVEL_DEBUG,"End of DBproxy_update_host_availability()"); + DBexecute("update hosts set available=%d,disable_until=%d where hostid=" ZBX_FS_UI64, + available, + clock + CONFIG_UNAVAILABLE_DELAY, + item->hostid); + } + else + { + log_level = LOG_LEVEL_ERR; + zbx_snprintf(error_msg, sizeof(error_msg), "Unknown host availability [%d] for host [%s]", + available, + item->host_name); + } - return; + zabbix_log(log_level, "%s", error_msg); + zabbix_syslog("%s", error_msg); } -int DBupdate_item_status_to_notsupported(zbx_uint64_t itemid, const char *error) +int DBupdate_item_status_to_notsupported(DB_ITEM *item, int clock, const char *error) { char error_esc[MAX_STRING_LEN]; - int now; - zabbix_log(LOG_LEVEL_DEBUG,"In DBupdate_item_status_to_notsupported()"); + zabbix_log(LOG_LEVEL_DEBUG, "In DBupdate_item_status_to_notsupported()"); - if(error!=NULL) - { - DBescape_string(error,error_esc,MAX_STRING_LEN); - } - else - { - strscpy(error_esc,""); - } + DBescape_string(error, error_esc, sizeof(error_esc)); - now = time(NULL); + DBexecute("update items set status=%d,lastclock=%d,nextcheck=%d,error='%s' where itemid=" ZBX_FS_UI64, + ITEM_STATUS_NOTSUPPORTED, + clock, + clock + CONFIG_REFRESH_UNSUPPORTED, + error_esc, + item->itemid); - /* '%s ' to make Oracle happy */ - DBexecute("update items set status=%d,nextcheck=%d,error='%s ' where itemid=" ZBX_FS_UI64, - ITEM_STATUS_NOTSUPPORTED, - CONFIG_REFRESH_UNSUPPORTED+now, - error_esc, - itemid); + item->status = ITEM_STATUS_NOTSUPPORTED; return SUCCEED; } - -int DBproxy_update_item_status_to_notsupported(zbx_uint64_t itemid) +/* +int DBproxy_update_item_status_to_notsupported(DB_ITEM *item, int clock) { - int now; - - zabbix_log(LOG_LEVEL_DEBUG,"In DBproxy_update_item_status_to_notsupported()"); - - now = time(NULL); + zabbix_log(LOG_LEVEL_DEBUG, "In DBproxy_update_item_status_to_notsupported()"); DBexecute("update items set status=%d,nextcheck=%d where itemid=" ZBX_FS_UI64, - ITEM_STATUS_NOTSUPPORTED, - CONFIG_REFRESH_UNSUPPORTED+now, - itemid); + ITEM_STATUS_NOTSUPPORTED, + clock + CONFIG_REFRESH_UNSUPPORTED, + itemid); return SUCCEED; -} +}*/ static int DBadd_trend(zbx_uint64_t itemid, double value, int clock) { @@ -1003,11 +961,6 @@ static int DBadd_trend(zbx_uint64_t itemid, double value, int clock) zabbix_log(LOG_LEVEL_DEBUG,"In add_trend()"); - if(CONFIG_DBSYNCER_FORKS >0) - { - return DCadd_trend(itemid, value, clock); - } - hour=clock-clock%3600; result = DBselect("select num,value_min,value_avg,value_max from trends where itemid=" ZBX_FS_UI64 " and clock=%d", @@ -1118,17 +1071,17 @@ int DBadd_history(zbx_uint64_t itemid, double value, int clock) clock, itemid, value); - } - DBadd_trend(itemid, value, clock); + DBadd_trend(itemid, value, clock); - if((CONFIG_NODE_NOHISTORY == 0) && (CONFIG_MASTER_NODEID>0)) - { - DBexecute("insert into history_sync (nodeid,clock,itemid,value) values (%d,%d," ZBX_FS_UI64 "," ZBX_FS_DBL ")", - get_nodeid_by_id(itemid), - clock, - itemid, - value); + if((CONFIG_NODE_NOHISTORY == 0) && (CONFIG_MASTER_NODEID>0)) + { + DBexecute("insert into history_sync (nodeid,clock,itemid,value) values (%d,%d," ZBX_FS_UI64 "," ZBX_FS_DBL ")", + get_nodeid_by_id(itemid), + clock, + itemid, + value); + } } return SUCCEED; @@ -1148,17 +1101,17 @@ int DBadd_history_uint(zbx_uint64_t itemid, zbx_uint64_t value, int clock) clock, itemid, value); - } - DBadd_trend_uint(itemid, value, clock); + DBadd_trend_uint(itemid, value, clock); - if((CONFIG_NODE_NOHISTORY == 0) && (CONFIG_MASTER_NODEID>0)) - { - DBexecute("insert into history_uint_sync (nodeid,clock,itemid,value) values (%d,%d," ZBX_FS_UI64 "," ZBX_FS_UI64 ")", - get_nodeid_by_id(itemid), - clock, - itemid, - value); + if((CONFIG_NODE_NOHISTORY == 0) && (CONFIG_MASTER_NODEID>0)) + { + DBexecute("insert into history_uint_sync (nodeid,clock,itemid,value) values (%d,%d," ZBX_FS_UI64 "," ZBX_FS_UI64 ")", + get_nodeid_by_id(itemid), + clock, + itemid, + value); + } } return SUCCEED; @@ -1174,23 +1127,23 @@ int DBadd_history_str(zbx_uint64_t itemid, char *value, int clock) { DCadd_history_str(itemid, value, clock); } + else + { + DBescape_string(value,value_esc,MAX_STRING_LEN); - DBescape_string(value,value_esc,MAX_STRING_LEN); - - if(CONFIG_DBSYNCER_FORKS == 0) { DBexecute("insert into history_str (clock,itemid,value) values (%d," ZBX_FS_UI64 ",'%s')", clock, itemid, value_esc); - } - if((CONFIG_NODE_NOHISTORY == 0) && (CONFIG_MASTER_NODEID>0)) - { - DBexecute("insert into history_str_sync (nodeid,clock,itemid,value) values (%d,%d," ZBX_FS_UI64 ",'%s')", - get_nodeid_by_id(itemid), - clock, - itemid, - value_esc); + if((CONFIG_NODE_NOHISTORY == 0) && (CONFIG_MASTER_NODEID>0)) + { + DBexecute("insert into history_str_sync (nodeid,clock,itemid,value) values (%d,%d," ZBX_FS_UI64 ",'%s')", + get_nodeid_by_id(itemid), + clock, + itemid, + value_esc); + } } return SUCCEED; @@ -1204,133 +1157,141 @@ int DBadd_history_text(zbx_uint64_t itemid, char *value, int clock) int value_esc_max_len = 0; int ret = FAIL; zbx_uint64_t id; +#else + char *value_esc = NULL; + zbx_uint64_t id; +#endif - sqlo_lob_desc_t loblp; /* the lob locator */ - sqlo_stmt_handle_t sth = 0; + zabbix_log(LOG_LEVEL_DEBUG, "In add_history_text()"); - sqlo_autocommit_off(oracle); + if(CONFIG_DBSYNCER_FORKS > 0) + { + DCadd_history_text(itemid, value, clock); + } + else + { +#ifdef HAVE_ORACLE + sqlo_lob_desc_t loblp; /* the lob locator */ + sqlo_stmt_handle_t sth = 0; - zabbix_log(LOG_LEVEL_DEBUG,"In add_history_text()"); + sqlo_autocommit_off(oracle); - value_esc_max_len = strlen(value)+1024; - value_esc = zbx_malloc(value_esc, value_esc_max_len); + value_esc_max_len = strlen(value)+1024; + value_esc = zbx_malloc(value_esc, value_esc_max_len); - DBescape_string(value, value_esc, value_esc_max_len-1); - value_esc_max_len = strlen(value_esc); + DBescape_string(value, value_esc, value_esc_max_len-1); + value_esc_max_len = strlen(value_esc); - /* alloate the lob descriptor */ - if(sqlo_alloc_lob_desc(oracle, &loblp) < 0) - { - zabbix_log(LOG_LEVEL_DEBUG,"CLOB allocating failed:%s", sqlo_geterror(oracle)); - goto lbl_exit; - } + /* alloate the lob descriptor */ + if(sqlo_alloc_lob_desc(oracle, &loblp) < 0) + { + zabbix_log(LOG_LEVEL_DEBUG,"CLOB allocating failed:%s", sqlo_geterror(oracle)); + goto lbl_exit; + } - id = DBget_maxid("history_text", "id"); - zbx_snprintf(sql, sizeof(sql), "insert into history_text (id,clock,itemid,value)" - " values (" ZBX_FS_UI64 ",%d," ZBX_FS_UI64 ", EMPTY_CLOB()) returning value into :1", - id, - clock, - itemid); + id = DBget_maxid("history_text", "id"); + zbx_snprintf(sql, sizeof(sql), "insert into history_text (id,clock,itemid,value)" + " values (" ZBX_FS_UI64 ",%d," ZBX_FS_UI64 ", EMPTY_CLOB()) returning value into :1", + id, + clock, + itemid); - zabbix_log(LOG_LEVEL_DEBUG,"Query:%s", sql); + zabbix_log(LOG_LEVEL_DEBUG,"Query:%s", sql); - /* parse the statement */ - sth = sqlo_prepare(oracle, sql); - if(sth < 0) - { - zabbix_log(LOG_LEVEL_DEBUG,"Query prepearing failed:%s", sqlo_geterror(oracle)); - goto lbl_exit; - } + /* parse the statement */ + sth = sqlo_prepare(oracle, sql); + if(sth < 0) + { + zabbix_log(LOG_LEVEL_DEBUG,"Query prepearing failed:%s", sqlo_geterror(oracle)); + goto lbl_exit; + } - /* bind input variables. Note: we bind the lob descriptor here */ - if(SQLO_SUCCESS != sqlo_bind_by_pos(sth, 1, SQLOT_CLOB, &loblp, 0, NULL, 0)) - { - zabbix_log(LOG_LEVEL_DEBUG,"CLOB binding failed:%s", sqlo_geterror(oracle)); - goto lbl_exit_loblp; - } + /* bind input variables. Note: we bind the lob descriptor here */ + if(SQLO_SUCCESS != sqlo_bind_by_pos(sth, 1, SQLOT_CLOB, &loblp, 0, NULL, 0)) + { + zabbix_log(LOG_LEVEL_DEBUG,"CLOB binding failed:%s", sqlo_geterror(oracle)); + goto lbl_exit_loblp; + } - /* execute the statement */ - if(sqlo_execute(sth, 1) != SQLO_SUCCESS) - { - zabbix_log(LOG_LEVEL_DEBUG,"Query failed:%s", sqlo_geterror(oracle)); - goto lbl_exit_loblp; - } + /* execute the statement */ + if(sqlo_execute(sth, 1) != SQLO_SUCCESS) + { + zabbix_log(LOG_LEVEL_DEBUG,"Query failed:%s", sqlo_geterror(oracle)); + goto lbl_exit_loblp; + } - /* write the lob */ - ret = sqlo_lob_write_buffer(oracle, loblp, value_esc_max_len, value_esc, value_esc_max_len, SQLO_ONE_PIECE); - if(ret < 0) - { - zabbix_log(LOG_LEVEL_DEBUG,"CLOB writing failed:%s", sqlo_geterror(oracle) ); - goto lbl_exit_loblp; - } + /* write the lob */ + ret = sqlo_lob_write_buffer(oracle, loblp, value_esc_max_len, value_esc, value_esc_max_len, SQLO_ONE_PIECE); + if(ret < 0) + { + zabbix_log(LOG_LEVEL_DEBUG,"CLOB writing failed:%s", sqlo_geterror(oracle) ); + goto lbl_exit_loblp; + } - /* commiting */ - if(sqlo_commit(oracle) < 0) - { - zabbix_log(LOG_LEVEL_DEBUG,"Commiting failed:%s", sqlo_geterror(oracle) ); - } + /* commiting */ + if(sqlo_commit(oracle) < 0) + { + zabbix_log(LOG_LEVEL_DEBUG,"Commiting failed:%s", sqlo_geterror(oracle) ); + } - ret = SUCCEED; + ret = SUCCEED; lbl_exit_loblp: - sqlo_free_lob_desc(oracle, &loblp); + sqlo_free_lob_desc(oracle, &loblp); lbl_exit: - if(sth >= 0) sqlo_close(sth); - zbx_free(value_esc); + if(sth >= 0) sqlo_close(sth); + zbx_free(value_esc); - sqlo_autocommit_on(oracle); + sqlo_autocommit_on(oracle); - return ret; + return ret; #else /* HAVE_ORACLE */ + value_esc = DBdyn_escape_string(value); - char *value_esc = NULL; - zbx_uint64_t id; - - zabbix_log(LOG_LEVEL_DEBUG, "In add_history_str()"); - - value_esc = DBdyn_escape_string(value); - - id = DBget_maxid("history_text", "id"); + id = DBget_maxid("history_text", "id"); - DBexecute("insert into history_text (id,clock,itemid,value)" - " values (" ZBX_FS_UI64 ",%d," ZBX_FS_UI64 ",'%s')", - id, - clock, - itemid, - value_esc); - - zbx_free(value_esc); - - return SUCCEED; + DBexecute("insert into history_text (id,clock,itemid,value)" + " values (" ZBX_FS_UI64 ",%d," ZBX_FS_UI64 ",'%s')", + id, + clock, + itemid, + value_esc); + zbx_free(value_esc); #endif + } + return SUCCEED; } -int DBadd_history_log(zbx_uint64_t id, zbx_uint64_t itemid, char *value, int clock, int timestamp, char *source, int severity) +int DBadd_history_log(zbx_uint64_t itemid, char *value, int clock, int timestamp, char *source, int severity, int lastlogsize) { - char *value_esc = NULL, source_esc[MAX_STRING_LEN]; + char *value_esc = NULL, source_esc[MAX_STRING_LEN]; zabbix_log(LOG_LEVEL_DEBUG, "In add_history_log()"); - value_esc = DBdyn_escape_string(value); - DBescape_string(source, source_esc, sizeof(source_esc)); - - if (id == 0) - id = DBget_maxid("history_log", "id"); + if(CONFIG_DBSYNCER_FORKS > 0) + { + DCadd_history_log(itemid, value, clock, timestamp, source, severity, lastlogsize); + } + else + { + value_esc = DBdyn_escape_string(value); + DBescape_string(source, source_esc, sizeof(source_esc)); - DBexecute("insert into history_log (id,clock,itemid,timestamp,value,source,severity)" - " values (" ZBX_FS_UI64 ",%d," ZBX_FS_UI64 ",%d,'%s','%s',%d)", - id, - clock, - itemid, - timestamp, - value_esc, - source_esc, - severity); + DBexecute("insert into history_log (id,clock,itemid,timestamp,value,source,severity)" + " values (" ZBX_FS_UI64 ",%d," ZBX_FS_UI64 ",%d,'%s','%s',%d)", + DBget_maxid("history_log", "id"), + clock, + itemid, + timestamp, + value_esc, + source_esc, + severity); - zbx_free(value_esc); + zbx_free(value_esc); + } return SUCCEED; } @@ -1917,7 +1878,7 @@ const ZBX_FIELD *DBget_field(const ZBX_TABLE *table, const char *fieldname) return NULL; } -zbx_uint64_t DBget_maxid(char *tablename, char *fieldname) +zbx_uint64_t DBget_maxid_num(char *tablename, char *fieldname, int num) { DB_RESULT result; DB_ROW row; @@ -2001,10 +1962,11 @@ zbx_uint64_t DBget_maxid(char *tablename, char *fieldname) continue; } - DBexecute("update ids set nextid=nextid+1 where nodeid=%d and table_name='%s' and field_name='%s'", - nodeid, - tablename, - fieldname); + DBexecute("update ids set nextid=nextid+%d where nodeid=%d and table_name='%s' and field_name='%s'", + num, + nodeid, + tablename, + fieldname); result = DBselect("select nextid from ids where nodeid=%d and table_name='%s' and field_name='%s'", nodeid, @@ -2018,7 +1980,7 @@ zbx_uint64_t DBget_maxid(char *tablename, char *fieldname) } else { ZBX_STR2UINT64(ret2, row[0]); DBfree_result(result); - if (ret1 + 1 == ret2) + if (ret1 + num == ret2) found = SUCCEED; } } @@ -2030,7 +1992,7 @@ zbx_uint64_t DBget_maxid(char *tablename, char *fieldname) fieldname, ret2); - return ret2; + return ret2 - num + 1; /* if(CONFIG_NODEID == 0) { @@ -2071,106 +2033,109 @@ zbx_uint64_t DBget_maxid(char *tablename, char *fieldname) return ret;*/ } -int DBproxy_add_history(const char *host, const char *key, int clock, double value) +void DBproxy_add_history(zbx_uint64_t itemid, double value, int clock) { - char host_esc[MAX_STRING_LEN], key_esc[MAX_STRING_LEN]; - zabbix_log(LOG_LEVEL_DEBUG, "In proxy_add_history()"); - DBescape_string(host, host_esc, sizeof(host_esc)); - DBescape_string(key, key_esc, sizeof(key_esc)); - - DBexecute("insert into proxy_history (host,key_,clock,value) values ('%s','%s',%d,'" ZBX_FS_DBL "')", - host_esc, - key_esc, - clock, - value); - - return SUCCEED; + if(CONFIG_DBSYNCER_FORKS > 0) + { + DCadd_history(itemid, value, clock); + } + else + { + DBexecute("insert into proxy_history (itemid,clock,value) values (" ZBX_FS_UI64 ",%d,'" ZBX_FS_DBL "')", + itemid, + clock, + value); + } } -int DBproxy_add_history_uint(const char *host, const char *key, int clock, zbx_uint64_t value) +void DBproxy_add_history_uint(zbx_uint64_t itemid, zbx_uint64_t value, int clock) { - char host_esc[MAX_STRING_LEN], key_esc[MAX_STRING_LEN]; - zabbix_log(LOG_LEVEL_DEBUG, "In proxy_add_history_uint()"); - DBescape_string(host, host_esc, sizeof(host_esc)); - DBescape_string(key, key_esc, sizeof(key_esc)); - - DBexecute("insert into proxy_history (host,key_,clock,value) values ('%s','%s',%d,'" ZBX_FS_UI64 "')", - host_esc, - key_esc, - clock, - value); - - return SUCCEED; + if(CONFIG_DBSYNCER_FORKS > 0) + { + DCadd_history_uint(itemid, value, clock); + } + else + { + DBexecute("insert into proxy_history (itemid,clock,value) values (" ZBX_FS_UI64 ",%d,'" ZBX_FS_UI64 "')", + itemid, + clock, + value); + } } -int DBproxy_add_history_str(const char *host, const char *key, int clock, char *value) +void DBproxy_add_history_str(zbx_uint64_t itemid, char *value, int clock) { - char host_esc[MAX_STRING_LEN], key_esc[MAX_STRING_LEN], value_esc[MAX_STRING_LEN]; + char value_esc[MAX_STRING_LEN]; zabbix_log(LOG_LEVEL_DEBUG, "In proxy_add_history_str()"); - DBescape_string(host, host_esc, sizeof(host_esc)); - DBescape_string(key, key_esc, sizeof(key_esc)); - DBescape_string(value, value_esc, sizeof(value_esc)); - - DBexecute("insert into proxy_history (host,key_,clock,value) values ('%s','%s',%d,'%s')", - host_esc, - key_esc, - clock, - value_esc); + if(CONFIG_DBSYNCER_FORKS > 0) + { + DCadd_history_str(itemid, value, clock); + } + else + { + DBescape_string(value, value_esc, sizeof(value_esc)); - return SUCCEED; + DBexecute("insert into proxy_history (itemid,clock,value) values (" ZBX_FS_UI64 ",%d,'%s')", + itemid, + clock, + value_esc); + } } -int DBproxy_add_history_text(const char *host, const char *key, int clock, char *value) +void DBproxy_add_history_text(zbx_uint64_t itemid, char *value, int clock) { - char host_esc[MAX_STRING_LEN], key_esc[MAX_STRING_LEN], *value_esc; + char *value_esc; zabbix_log(LOG_LEVEL_DEBUG, "In proxy_add_history_text()"); - DBescape_string(host, host_esc, sizeof(host_esc)); - DBescape_string(key, key_esc, sizeof(key_esc)); - value_esc = DBdyn_escape_string(value); - - DBexecute("insert into proxy_history (host,key_,clock,value) values ('%s','%s',%d,'%s')", - host_esc, - key_esc, - clock, - value_esc); + if(CONFIG_DBSYNCER_FORKS > 0) + { + DCadd_history_text(itemid, value, clock); + } + else + { + value_esc = DBdyn_escape_string(value); - zbx_free(value_esc); + DBexecute("insert into proxy_history (itemid,clock,value) values (" ZBX_FS_UI64 ",%d,'%s')", + itemid, + clock, + value_esc); - return SUCCEED; + zbx_free(value_esc); + } } -int DBproxy_add_history_log(const char *host, const char *key, int clock, int timestamp, char *source, int severity, char *value) +void DBproxy_add_history_log(zbx_uint64_t itemid, char *value, int clock, int timestamp, char *source, int severity, int lastlogsize) { - char host_esc[MAX_STRING_LEN], key_esc[MAX_STRING_LEN], - source_esc[MAX_STRING_LEN], *value_esc; + char source_esc[MAX_STRING_LEN], *value_esc; zabbix_log(LOG_LEVEL_DEBUG, "In proxy_add_history_log()"); - DBescape_string(host, host_esc, sizeof(host_esc)); - DBescape_string(key, key_esc, sizeof(key_esc)); - DBescape_string(source, source_esc, sizeof(source_esc)); - value_esc = DBdyn_escape_string(value); - - DBexecute("insert into proxy_history (host,key_,clock,timestamp,source,severity,value)" - " values ('%s','%s',%d,%d,'%s',%d,'%s')", - host_esc, - key_esc, - clock, - timestamp, - source_esc, - severity, - value_esc); + if(CONFIG_DBSYNCER_FORKS > 0) + { + DCadd_history_log(itemid, value, clock, timestamp, source, severity, lastlogsize); + } + else + { + DBescape_string(source, source_esc, sizeof(source_esc)); + value_esc = DBdyn_escape_string(value); - zbx_free(value_esc); + DBexecute("insert into proxy_history (itemid,clock,timestamp,source,severity,value)" + " values (" ZBX_FS_UI64 ",%d,%d,'%s',%d,'%s')", + itemid, + clock, + timestamp, + source_esc, + severity, + value_esc); - return SUCCEED; + zbx_free(value_esc); + } } diff --git a/src/libs/zbxserver/expression.c b/src/libs/zbxserver/expression.c index fdc23363..3141d35d 100644 --- a/src/libs/zbxserver/expression.c +++ b/src/libs/zbxserver/expression.c @@ -1503,9 +1503,9 @@ void substitute_macros(DB_EVENT *event, DB_ACTION *action, char **data) if('\0' == *data[0]) return; - zabbix_log(LOG_LEVEL_DEBUG, "Before substitute_simple_macros(%s)", *data); +/* zabbix_log(LOG_LEVEL_DEBUG, "Before substitute_simple_macros(%s)", *data);*/ substitute_simple_macros(event, action, data, MACRO_TYPE_MESSAGE_SUBJECT | MACRO_TYPE_MESSAGE_BODY); - zabbix_log(LOG_LEVEL_DEBUG, "After substitute_simple_macros(%s)", *data); +/* zabbix_log(LOG_LEVEL_DEBUG, "After substitute_simple_macros(%s)", *data);*/ pl = *data; while((pr = strchr(pl, '{'))) @@ -1599,7 +1599,7 @@ void substitute_macros(DB_EVENT *event, DB_ACTION *action, char **data) * Comments: example: "({15}>10)|({123}=0)" => "(6.456>10)|(0=0) * * * ******************************************************************************/ -int substitute_functions(char **exp, char *error, int maxerrlen) +static int substitute_functions(char **exp, char *error, int maxerrlen) { char *value; char functionid[MAX_STRING_LEN]; @@ -1674,7 +1674,7 @@ int substitute_functions(char **exp, char *error, int maxerrlen) * ({a0:system[procload].max(300)}>3) * * * ******************************************************************************/ -int evaluate_expression(int *result,char **expression, int trigger_value, char *error, int maxerrlen) +int evaluate_expression(int *result,char **expression, DB_TRIGGER *trigger, char *error, int maxerrlen) { /* Required for substitution of macros */ DB_EVENT event; @@ -1686,7 +1686,7 @@ int evaluate_expression(int *result,char **expression, int trigger_value, char * /* Substitute macros first */ memset(&event,0,sizeof(DB_EVENT)); memset(&action,0,sizeof(DB_ACTION)); - event.value = trigger_value; + event.value = trigger->value; substitute_simple_macros(&event, &action, expression, MACRO_TYPE_TRIGGER_EXPRESSION); diff --git a/src/libs/zbxserver/expression.h b/src/libs/zbxserver/expression.h index 9876dc08..15678c81 100644 --- a/src/libs/zbxserver/expression.h +++ b/src/libs/zbxserver/expression.h @@ -26,7 +26,6 @@ int cmp_double(double a,double b); int find_char(char *str,char c); -int evaluate_expression(int *result,char **expression, int triggger_value, char *error, int maxerrlen); void delete_reol(char *c); #endif diff --git a/src/libs/zbxserver/functions.c b/src/libs/zbxserver/functions.c index f453a9ca..e63e4121 100644 --- a/src/libs/zbxserver/functions.c +++ b/src/libs/zbxserver/functions.c @@ -152,7 +152,7 @@ void update_triggers(zbx_uint64_t itemid) trigger.type = atoi(row[8]); exp = strdup(trigger.expression); - if( evaluate_expression(&exp_value, &exp, trigger.value, error, sizeof(error)) != 0 ) + if( evaluate_expression(&exp_value, &exp, &trigger, error, sizeof(error)) != 0 ) { zabbix_log( LOG_LEVEL_WARNING, "Expression [%s] cannot be evaluated [%s]", trigger.expression, @@ -295,7 +295,8 @@ static int add_history(DB_ITEM *item, AGENT_RESULT *value, int now) else if(item->value_type==ITEM_VALUE_TYPE_LOG) { if(GET_STR_RESULT(value)) - DBadd_history_log(0, item->itemid,value->str,now,item->timestamp,item->eventlog_source,item->eventlog_severity); + DBadd_history_log(item->itemid, value->str, now, item->timestamp, item->eventlog_source, + item->eventlog_severity, item->lastlogsize); } else if(item->value_type==ITEM_VALUE_TYPE_TEXT) { @@ -575,10 +576,16 @@ void process_new_value(DB_ITEM *item, AGENT_RESULT *value, time_t now) } } } +/* +zabbix_log(LOG_LEVEL_CRIT, "I"); +*/ add_history(item, value, now); - update_item(item, value, now); - update_functions(item); + if (0 == CONFIG_DBSYNCER_FORKS) + { + update_item(item, value, now); + update_functions(item); + } } /****************************************************************************** @@ -625,27 +632,27 @@ static int proxy_add_history(DB_ITEM *item, AGENT_RESULT *value, int now) if(item->value_type==ITEM_VALUE_TYPE_UINT64) { if(GET_UI64_RESULT(value)) - DBproxy_add_history_uint(item->host_name, item->key, now, value->ui64); + DBproxy_add_history_uint(item->itemid, value->ui64, now); } else if(item->value_type==ITEM_VALUE_TYPE_FLOAT) { if(GET_DBL_RESULT(value)) - DBproxy_add_history(item->host_name, item->key, now, value->dbl); + DBproxy_add_history(item->itemid, value->dbl, now); } else if(item->value_type==ITEM_VALUE_TYPE_STR) { if(GET_STR_RESULT(value)) - DBproxy_add_history_str(item->host_name, item->key, now, value->str); + DBproxy_add_history_str(item->itemid, value->str, now); } else if(item->value_type==ITEM_VALUE_TYPE_LOG) { if(GET_STR_RESULT(value)) - DBproxy_add_history_log(item->host_name, item->key, now, item->timestamp, item->eventlog_source, item->eventlog_severity, value->str); + DBproxy_add_history_log(item->itemid, value->str, now, item->timestamp, item->eventlog_source, item->eventlog_severity, item->lastlogsize); } else if(item->value_type==ITEM_VALUE_TYPE_TEXT) { if(GET_TEXT_RESULT(value)) - DBproxy_add_history_text(item->host_name, item->key, now, value->text); + DBproxy_add_history_text(item->itemid, value->str, now); } else { @@ -738,5 +745,6 @@ void proxy_process_new_value(DB_ITEM *item, AGENT_RESULT *value, time_t now) item->key); proxy_add_history(item, value, now); - proxy_update_item(item, value, now); + if (0 == CONFIG_DBSYNCER_FORKS) + proxy_update_item(item, value, now); } |
