summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoralex <alex@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2007-07-22 19:49:37 +0000
committeralex <alex@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2007-07-22 19:49:37 +0000
commit6c17fbc184e98be0a8c46c1ed383f838de57f80c (patch)
treeba5a657540603e3d25deeba135b5864f4f65efe8
parenta9bb4776d6000ced0fdbcbee731a13d5ebf1922c (diff)
downloadzabbix-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
-rw-r--r--ChangeLog1
-rwxr-xr-xgo4
-rw-r--r--include/dbcache.h6
-rw-r--r--src/libs/zbxdbcache/dbcache.c151
-rw-r--r--src/zabbix_server/server.c11
-rw-r--r--src/zabbix_server/watchdog/watchdog.c2
6 files changed, 153 insertions, 22 deletions
diff --git a/ChangeLog b/ChangeLog
index eb0cca7c..4a0ed128 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
Changes for 1.5:
+ - support of database cache for history and trends (Alexei)
- basic support of database caching (Alexei)
- added pie graphs (Artem)
- added new columns graphs.show_legend, graphs.show_3d (Alexei)
diff --git a/go b/go
index 8ed2189b..ed64f3c2 100755
--- a/go
+++ b/go
@@ -26,14 +26,14 @@ automake
#exit
echo Configuring...
#export CFLAGS="-Wall -Wuninitialized -O -DDEBUG"
-export CFLAGS="-Wall -Wuninitialized -O"
+export CFLAGS="-Wall -Wuninitialized -O -g"
cd create/schema
./gen.pl c >../../include/dbsync.h
cd -
#export CFLAGS="-Wall -pedantic"
#for db in sqlite3 pgsql mysql; do
for db in mysql; do
- ./configure --enable-agent --enable-server --with-$db --prefix=`pwd` 2>>WARNINGS >/dev/null
+ ./configure --enable-agent --with-net-snmp --with-libcurl --with-ldap --enable-server --with-$db --prefix=`pwd` 2>>WARNINGS >/dev/null
echo Cleaning...
make clean 2>>WARNINGS >/dev/null
echo Making...
diff --git a/include/dbcache.h b/include/dbcache.h
index 0650d45f..dfbc6f26 100644
--- a/include/dbcache.h
+++ b/include/dbcache.h
@@ -31,6 +31,9 @@
#define ZBX_TREND_SIZE 100000
#define ZBX_ITEMS_SIZE 10000
+#define ZBX_TREND_OP_UPDATE 0
+#define ZBX_TREND_OP_INSERT 1
+
extern char *CONFIG_FILE;
ZBX_DC_HISTORY
@@ -48,6 +51,7 @@ ZBX_DC_HISTORY
ZBX_DC_TREND
{
+ int operation;
zbx_uint64_t itemid;
int clock;
int num;
@@ -82,6 +86,8 @@ int DCadd_history(zbx_uint64_t itemid, double value, int clock);
int DCadd_history_uint(zbx_uint64_t itemid, zbx_uint64_t value, int clock);
int DCadd_history_str(zbx_uint64_t itemid, char *value, int clock);
void DCshow(void);
+void DCsync(void);
+void DCsync_all(void);
void init_database_cache(void);
void free_database_cache(void);
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;