diff options
author | alex <alex@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-07-22 19:49:37 +0000 |
---|---|---|
committer | alex <alex@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-07-22 19:49:37 +0000 |
commit | 6c17fbc184e98be0a8c46c1ed383f838de57f80c (patch) | |
tree | ba5a657540603e3d25deeba135b5864f4f65efe8 /src | |
parent | a9bb4776d6000ced0fdbcbee731a13d5ebf1922c (diff) | |
download | zabbix-6c17fbc184e98be0a8c46c1ed383f838de57f80c.tar.gz zabbix-6c17fbc184e98be0a8c46c1ed383f838de57f80c.tar.xz zabbix-6c17fbc184e98be0a8c46c1ed383f838de57f80c.zip |
- support of database cache for history and trends (Alexei)
git-svn-id: svn://svn.zabbix.com/trunk@4462 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
-rw-r--r-- | src/libs/zbxdbcache/dbcache.c | 151 | ||||
-rw-r--r-- | src/zabbix_server/server.c | 11 | ||||
-rw-r--r-- | src/zabbix_server/watchdog/watchdog.c | 2 |
3 files changed, 144 insertions, 20 deletions
diff --git a/src/libs/zbxdbcache/dbcache.c b/src/libs/zbxdbcache/dbcache.c index e2408e27..f7700d8c 100644 --- a/src/libs/zbxdbcache/dbcache.c +++ b/src/libs/zbxdbcache/dbcache.c @@ -111,16 +111,37 @@ void DCsync() for(i=0;i<cache->pool.trends_count;i++) { trend = &cache->pool.trends[i]; - zabbix_log(LOG_LEVEL_DEBUG,"History " ZBX_FS_UI64, + zabbix_log(LOG_LEVEL_DEBUG,"Trend " ZBX_FS_UI64, trend->itemid); + 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); + } } DBcommit(); cache->pool.history_count=0; + cache->pool.trends_count=0; UNLOCK_CACHE; - zabbix_log(LOG_LEVEL_DEBUG,"End of DCshow()"); + zabbix_log(LOG_LEVEL_DEBUG,"End of DCsync()"); } void DCshow() @@ -184,8 +205,9 @@ static ZBX_DC_ITEM *get_item(zbx_uint64_t itemid) int DCadd_trend(zbx_uint64_t itemid, double value, int clock) { int hour; - ZBX_DC_TREND *trend = NULL; - ZBX_DC_ITEM *item = NULL; + ZBX_DC_TREND *trend = NULL, + *trend_tmp = NULL; + ZBX_DC_ITEM *item = NULL; DB_RESULT result; DB_ROW row; int trend_found=0; @@ -203,9 +225,21 @@ int DCadd_trend(zbx_uint64_t itemid, double value, int clock) { trend_found=1; } - else + else if(trend->clock !=0) { // add_trend2pool(trend); + trend_tmp=&cache->pool.trends[cache->pool.trends_count]; + cache->pool.trends_count++; + + 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; + + trend->clock = 0; } /* Not found with the same clock */ @@ -213,11 +247,13 @@ int DCadd_trend(zbx_uint64_t itemid, double value, int clock) { zabbix_log(LOG_LEVEL_DEBUG,"Not found"); /* Add new, do not look at the database */ - trend->clock = hour; - trend->num = 1; - trend->value_min= value; - trend->value_max= value; - trend->value_avg= value; + 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; /* 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", @@ -228,14 +264,16 @@ int DCadd_trend(zbx_uint64_t itemid, double value, int clock) if(row) { - 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]); + 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->value_avg = (trend->num*trend->value_avg+value)/(trend->num+1); trend->num++; } DBfree_result(result); @@ -406,15 +444,18 @@ lbl_create: * Comments: * * * ******************************************************************************/ - void free_database_cache(void) { 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); @@ -434,4 +475,80 @@ void free_database_cache(void) UNLOCK_CACHE; zbx_mutex_destroy(&cache_lock); + + zabbix_log(LOG_LEVEL_WARNING,"End of free_database_cache()"); +} + +/****************************************************************************** + * * + * Function: DCsync_all * + * * + * Purpose: writes updates and new data from pool and cache data to database * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Alexei Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +void DCsync_all() +{ + int i; + + ZBX_DC_ITEM *item; + ZBX_DC_TREND *trend; + + 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); + + DCsync(); + + LOCK_CACHE; + DBbegin(); + + zabbix_log(LOG_LEVEL_WARNING,"In items_count %d", + cache->items_count); + + for(i=0;i<cache->items_count;i++) + { + 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); + } + } + + DBcommit(); + + UNLOCK_CACHE; + + zabbix_log(LOG_LEVEL_WARNING,"End of DCsync_all()"); } diff --git a/src/zabbix_server/server.c b/src/zabbix_server/server.c index 63d4a976..1bef1ccd 100644 --- a/src/zabbix_server/server.c +++ b/src/zabbix_server/server.c @@ -675,8 +675,9 @@ int MAIN_ZABBIX_ENTRY(void) DBclose(); /* To make sure that we can connect to the database before forking new processes */ - DBconnect(ZBX_DB_CONNECT_EXIT); - DBclose(); +/* DBconnect(ZBX_DB_CONNECT_EXIT);*/ +/* Do not close database. It is required for database cache */ +/* DBclose();*/ /*#define CALC_TREND*/ @@ -862,18 +863,22 @@ void zbx_on_exit() free_metrics(); zbx_sleep(2); /* wait for all threads closing */ + + DBconnect(ZBX_DB_CONNECT_EXIT); - zabbix_log(LOG_LEVEL_INFORMATION, "ZABBIX Server stopped"); if(CONFIG_DBSYNCER_FORKS!=0) { free_database_cache(); } + DBclose(); zabbix_close_log(); #ifdef HAVE_SQLITE3 php_sem_remove(&sqlite_access); #endif /* HAVE_SQLITE3 */ + zabbix_log(LOG_LEVEL_INFORMATION, "ZABBIX Server stopped"); + exit(SUCCEED); } diff --git a/src/zabbix_server/watchdog/watchdog.c b/src/zabbix_server/watchdog/watchdog.c index bf43d482..c12333c9 100644 --- a/src/zabbix_server/watchdog/watchdog.c +++ b/src/zabbix_server/watchdog/watchdog.c @@ -177,6 +177,8 @@ static void ping_database() ******************************************************************************/ void main_watchdog_loop() { + zabbix_log(LOG_LEVEL_WARNING, "In main_watchdog_loop()"); + /* Disable writing to database in zabbix_syslog() */ CONFIG_ENABLE_LOG = 0; |