summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2008-08-11 12:33:48 +0000
committersasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2008-08-11 12:33:48 +0000
commit79954547dab0e2a87436fba02f95ee0dbcf7a1d9 (patch)
tree0feaed6696018fc1447c32ffe7c565f29145e3a7 /src
parenta00be6b72f85d6fe57de4fbb4dbf92c67bc6976a (diff)
downloadzabbix-79954547dab0e2a87436fba02f95ee0dbcf7a1d9.tar.gz
zabbix-79954547dab0e2a87436fba02f95ee0dbcf7a1d9.tar.xz
zabbix-79954547dab0e2a87436fba02f95ee0dbcf7a1d9.zip
- [DEV-137] Misc bugs and improvements
[improved processing of proxy configuration] git-svn-id: svn://svn.zabbix.com/trunk@5892 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
-rw-r--r--src/libs/zbxcommon/misc.c136
-rw-r--r--src/libs/zbxdbhigh/db.c34
-rw-r--r--src/zabbix_proxy/proxyconfig/proxyconfig.c152
-rw-r--r--src/zabbix_server/httppoller/httptest.c11
-rw-r--r--src/zabbix_server/pinger/pinger.c9
-rw-r--r--src/zabbix_server/poller/poller.c20
-rw-r--r--src/zabbix_server/trapper/trapper.c11
7 files changed, 302 insertions, 71 deletions
diff --git a/src/libs/zbxcommon/misc.c b/src/libs/zbxcommon/misc.c
index 3c91dd30..de92d03b 100644
--- a/src/libs/zbxcommon/misc.c
+++ b/src/libs/zbxcommon/misc.c
@@ -1064,3 +1064,139 @@ int uint64_in_list(char *list, zbx_uint64_t value)
return ret;
}
+
+/******************************************************************************
+ * *
+ * Function: get_nearestindex *
+ * *
+ * Purpose: *
+ * *
+ * Parameters: *
+ * *
+ * Return value: *
+ * *
+ * Author: Alekasander Vladishev *
+ * *
+ * Comments: *
+ * *
+ ******************************************************************************/
+static int get_nearestindex(zbx_uint64_t *values, int num, zbx_uint64_t value)
+{
+ int first_index, last_index, index;
+
+ if (num == 0)
+ return 0;
+
+ first_index = 0;
+ last_index = num - 1;
+ while (1)
+ {
+ index = first_index + (last_index - first_index) / 2;
+
+ if (values[index] == value)
+ return index;
+ else if (last_index == first_index)
+ {
+ if (values[index] < value)
+ index++;
+ return index;
+ }
+ else if (values[index] < value)
+ first_index = index + 1;
+ else
+ last_index = index;
+ }
+}
+
+/******************************************************************************
+ * *
+ * Function: uint64_array_add *
+ * *
+ * Purpose: add uint64 value to dynamic array *
+ * *
+ * Parameters: *
+ * *
+ * Return value: *
+ * *
+ * Author: Aleksander Vladishev *
+ * *
+ * Comments: *
+ * *
+ ******************************************************************************/
+int uint64_array_add(zbx_uint64_t **values, int *alloc, int *num, zbx_uint64_t value)
+{
+ int index;
+
+ index = get_nearestindex(*values, *num, value);
+ if (index < (*num) && (*values)[index] == value)
+ return index;
+
+ if (*alloc == *num)
+ {
+ *alloc += 100;
+ *values = zbx_realloc(*values, *alloc * sizeof(zbx_uint64_t));
+ }
+
+ memmove(&(*values)[index + 1], &(*values)[index], sizeof(zbx_uint64_t) * (*num - index));
+
+ (*values)[index] = value;
+ (*num)++;
+
+ return index;
+}
+
+/******************************************************************************
+ * *
+ * Function: uint64_array_exists *
+ * *
+ * Purpose: *
+ * *
+ * Parameters: *
+ * *
+ * Return value: *
+ * *
+ * Author: Aleksander Vladishev *
+ * *
+ * Comments: *
+ * *
+ ******************************************************************************/
+int uint64_array_exists(zbx_uint64_t *values, int num, zbx_uint64_t value)
+{
+ int index;
+
+ index = get_nearestindex(values, num, value);
+ if (index < num && values[index] == value)
+ return SUCCEED;
+
+ return FAIL;
+}
+
+/******************************************************************************
+ * *
+ * Function: uint64_array_remove *
+ * *
+ * Purpose: add uint64 value to dynamic array *
+ * *
+ * Parameters: *
+ * *
+ * Return value: *
+ * *
+ * Author: Aleksander Vladishev *
+ * *
+ * Comments: *
+ * *
+ ******************************************************************************/
+void uint64_array_rm(zbx_uint64_t *values, int *num, zbx_uint64_t *rm_values, int rm_num)
+{
+ int rindex, index;
+
+ for (rindex = 0; rindex < rm_num; rindex++)
+ {
+ index = get_nearestindex(values, *num, rm_values[rindex]);
+ if (index == *num || values[index] != rm_values[rindex])
+ continue;
+
+ memmove(&values[index], &values[index + 1], sizeof(zbx_uint64_t) * ((*num) - index - 1));
+ (*num)--;
+ }
+}
diff --git a/src/libs/zbxdbhigh/db.c b/src/libs/zbxdbhigh/db.c
index d188d873..a887d252 100644
--- a/src/libs/zbxdbhigh/db.c
+++ b/src/libs/zbxdbhigh/db.c
@@ -2139,3 +2139,37 @@ void DBproxy_add_history_log(zbx_uint64_t itemid, char *value, int clock, int ti
}
}
+void DBadd_condition_alloc(char **sql, int *sql_alloc, int *sql_offset, const char *fieldname, const zbx_uint64_t *values, const int num)
+{
+#define MAX_EXPRESSIONS 950
+ int i;
+
+ if (0 == num)
+ return;
+
+ zbx_snprintf_alloc(sql, sql_alloc, sql_offset, 2, " ");
+ if (num > MAX_EXPRESSIONS)
+ zbx_snprintf_alloc(sql, sql_alloc, sql_offset, 2, "(");
+
+ for (i = 0; i < num; i++)
+ {
+ if (0 == (i % MAX_EXPRESSIONS))
+ {
+ if (0 != i)
+ {
+ (*sql_offset)--;
+ zbx_snprintf_alloc(sql, sql_alloc, sql_offset, 8, ") or ");
+ }
+ zbx_snprintf_alloc(sql, sql_alloc, sql_offset, 128, "%s in (",
+ fieldname);
+ }
+ zbx_snprintf_alloc(sql, sql_alloc, sql_offset, 128, ZBX_FS_UI64 ",",
+ values[i]);
+ }
+
+ (*sql_offset)--;
+ zbx_snprintf_alloc(sql, sql_alloc, sql_offset, 2, ")");
+
+ if (num > MAX_EXPRESSIONS)
+ zbx_snprintf_alloc(sql, sql_alloc, sql_offset, 2, ")");
+}
diff --git a/src/zabbix_proxy/proxyconfig/proxyconfig.c b/src/zabbix_proxy/proxyconfig/proxyconfig.c
index 514c1f33..f6fa8efe 100644
--- a/src/zabbix_proxy/proxyconfig/proxyconfig.c
+++ b/src/zabbix_proxy/proxyconfig/proxyconfig.c
@@ -46,34 +46,42 @@
******************************************************************************/
static int process_proxyconfig_table(struct zbx_json_parse *jp, const char *tablename, const char *p)
{
- int t, f, field_count, insert, offset, execute;
- ZBX_TABLE *table = NULL;
- ZBX_FIELD *fields[ZBX_MAX_FIELDS];
+ int f, field_count, insert;
+ const ZBX_TABLE *table = NULL;
+ const ZBX_FIELD *fields[ZBX_MAX_FIELDS];
struct zbx_json_parse jp_obj, jp_data, jp_row;
- char buf[MAX_STRING_LEN],
- sql[MAX_STRING_LEN],
- esc[MAX_STRING_LEN],
- recid[21]; /* strlen("18446744073709551615") == 20 */
+ char buf[MAX_STRING_LEN], *esc;
+ zbx_uint64_t recid;
const char *pf;
+ static zbx_uint64_t *new = NULL, *old = NULL;
+ static int new_alloc = 100, old_alloc = 100;
+ int new_num = 0, old_num = 0;
+ static char *sql = NULL;
+ static int sql_alloc = 4096;
+ int sql_offset;
DB_RESULT result;
+ DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG, "In process_proxyconfig_table() [tablename:%s]",
tablename);
- for (t = 0; tables[t].table != NULL; t++ )
- if (0 == strcmp(tables[t].table, tablename))
- table = &tables[t];
-
- if (NULL == table) {
+ if (NULL == (table = DBget_table(tablename))) {
zabbix_log(LOG_LEVEL_WARNING, "Invalid table name \"%s\"",
tablename);
return FAIL;
}
- if (ZBX_DB_OK > DBexecute("create table %1$s_%2$s_tmp (%2$s "ZBX_DBTYPE_INT64")",
- table->table,
- table->recid))
- goto db_error;
+ if (NULL == new)
+ new = zbx_malloc(new, new_alloc * sizeof(zbx_uint64_t));
+ if (NULL == old)
+ old = zbx_malloc(old, old_alloc * sizeof(zbx_uint64_t));
+ if (NULL == sql)
+ sql = zbx_malloc(sql, sql_alloc * sizeof(char));
+
+ result = DBselect("select %s from %s", table->recid, table->table);
+ while (NULL != (row = DBfetch(result)))
+ uint64_array_add(&old, &old_alloc, &old_num, zbx_atoui64(row[0]));
+ DBfree_result(result);
/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...}
* ^---------------------------------------------------------------------------^
@@ -125,100 +133,112 @@ static int process_proxyconfig_table(struct zbx_json_parse *jp, const char *tabl
*/ if (FAIL == zbx_json_brackets_open(p, &jp_data))
goto json_error;
+ p = NULL;
+ sql_offset = 0;
+
+#ifdef HAVE_ORACLE
+ zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 8, "begin\n");
+#endif
+
/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...}
* ^
- */ p = NULL;
- while (NULL != (p = zbx_json_next(&jp_data, p))) {
+ */ while (NULL != (p = zbx_json_next(&jp_data, p))) {
/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...}
* ^-------------^
*/ if (FAIL == zbx_json_brackets_open(p, &jp_row))
goto json_error;
pf = NULL;
- if (NULL == (pf = zbx_json_next_value(&jp_row, pf, recid, sizeof(recid))))
+ if (NULL == (pf = zbx_json_next_value(&jp_row, pf, buf, sizeof(buf))))
goto json_error;
- result = DBselect("select 0 from %s where %s=%s",
- table->table,
- table->recid,
- recid);
- insert = (NULL == DBfetch(result));
- DBfree_result(result);
+ recid = zbx_atoui64(buf);
+
+ insert = (SUCCEED == uint64_array_exists(old, old_num, recid)) ? 0 : 1;
+
+ if (insert)
+ {
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 128, "insert into %s (", table->table);
- offset = 0;
- if (insert) {
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, "insert into %s (",
- table->table);
for (f = 0; f < field_count; f ++)
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, "%s,", fields[f]->name);
- offset--;
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, ") values (%s,",
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 128, "%s,", fields[f]->name);
+
+ sql_offset--;
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 128, ") values (" ZBX_FS_UI64 ",",
recid);
- } else
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, "update %s set ",
+ }
+ else
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 128, "update %s set ",
table->table);
- execute = 0;
-
/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...}
* ^
*/ f = 1;
- while (NULL != (pf = zbx_json_next_value(&jp_row, pf, buf, sizeof(buf)))) {
- execute = 1;
-
- if (f == field_count) {
+ while (NULL != (pf = zbx_json_next_value(&jp_row, pf, buf, sizeof(buf))))
+ {
+ if (f == field_count)
+ {
zabbix_log(LOG_LEVEL_WARNING, "Invalid number of fields \"%.*s\"",
jp_row.end - jp_row.start + 1,
jp_row.start);
return FAIL;
}
- if (fields[f]->type == ZBX_TYPE_INT || fields[f]->type == ZBX_TYPE_UINT || fields[f]->type == ZBX_TYPE_ID || fields[f]->type == ZBX_TYPE_FLOAT) {
+ if (fields[f]->type == ZBX_TYPE_INT || fields[f]->type == ZBX_TYPE_UINT || fields[f]->type == ZBX_TYPE_ID || fields[f]->type == ZBX_TYPE_FLOAT)
+ {
if (0 == insert)
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, "%s=", fields[f]->name);
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, "%s,", buf);
- } else {
- DBescape_string(buf, esc, sizeof(esc));
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 128, "%s=", fields[f]->name);
+
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 128, "%s,", buf);
+ }
+ else
+ {
if (0 == insert)
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, "%s=", fields[f]->name);
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, "'%s',", esc);
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 128, "%s=", fields[f]->name);
+
+ esc = DBdyn_escape_string(buf);
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, strlen(esc) + 8, "'%s',", esc);
}
f++;
}
- if (f != field_count) {
+ if (f != field_count)
+ {
zabbix_log(LOG_LEVEL_WARNING, "Invalid number of fields \"%.*s\"",
jp_row.end - jp_row.start + 1,
jp_row.start);
return FAIL;
}
- offset--;
+ sql_offset--;
if (insert)
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, ")");
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 4, ");\n");
else
- offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, " where %s=%s",
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 256, " where %s=" ZBX_FS_UI64 ";\n",
table->recid,
recid);
- if ((insert || execute) && ZBX_DB_OK > DBexecute("%s", sql))
- goto db_error;
- if (ZBX_DB_OK > DBexecute("insert into %1$s_%2$s_tmp (%2$s) values (%3$s)",
- table->table,
- table->recid,
- recid))
- goto db_error;
+ uint64_array_add(&new, &new_alloc, &new_num, recid);
}
- if (ZBX_DB_OK > DBexecute("delete from %1$s where not %2$s in (select %2$s from %1$s_%2$s_tmp)",
- table->table,
- table->recid))
- goto db_error;
+#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; */
+ if (ZBX_DB_OK > DBexecute("%s", sql))
+ goto db_error;
+
+ uint64_array_rm(old, &old_num, new, new_num);
- if (ZBX_DB_OK > DBexecute("drop table %s_%s_tmp",
- table->table,
- table->recid))
- goto db_error;
+ if (old_num > 0)
+ {
+ sql_offset = 0;
+ zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 128, "delete from %s where", table->table);
+ DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, table->recid, old, old_num);
+ if (ZBX_DB_OK > DBexecute("%s", sql))
+ goto db_error;
+ }
zabbix_log(LOG_LEVEL_DEBUG, "End process_proxyconfig_table()");
diff --git a/src/zabbix_server/httppoller/httptest.c b/src/zabbix_server/httppoller/httptest.c
index 6b527477..81a08c18 100644
--- a/src/zabbix_server/httppoller/httptest.c
+++ b/src/zabbix_server/httppoller/httptest.c
@@ -99,7 +99,16 @@ static int process_value(zbx_uint64_t itemid, AGENT_RESULT *value)
DBcommit();
}
else
- process_new_value(&item, value, now);
+ {
+ switch (zbx_process) {
+ case ZBX_PROCESS_SERVER:
+ process_new_value(&item, value, now);
+ break;
+ case ZBX_PROCESS_PROXY:
+ proxy_process_new_value(&item, value, now);
+ break;
+ }
+ }
DBfree_result(result);
diff --git a/src/zabbix_server/pinger/pinger.c b/src/zabbix_server/pinger/pinger.c
index a84f6b52..97de1fa2 100644
--- a/src/zabbix_server/pinger/pinger.c
+++ b/src/zabbix_server/pinger/pinger.c
@@ -110,7 +110,14 @@ static void process_value(char *key, ZBX_FPING_HOST *host, zbx_uint64_t *value_u
}
else
{
- process_new_value(&item, &value, now);
+ switch (zbx_process) {
+ case ZBX_PROCESS_SERVER:
+ process_new_value(&item, &value, now);
+ break;
+ case ZBX_PROCESS_PROXY:
+ proxy_process_new_value(&item, &value, now);
+ break;
+ }
DCadd_nextcheck(&item, now, NULL);
}
diff --git a/src/zabbix_server/poller/poller.c b/src/zabbix_server/poller/poller.c
index 5ac355f5..dbabb5b2 100644
--- a/src/zabbix_server/poller/poller.c
+++ b/src/zabbix_server/poller/poller.c
@@ -225,7 +225,16 @@ static void update_key_status(zbx_uint64_t hostid, int host_status, time_t now)
}
}
else
- process_new_value(&item, &agent, now);
+ {
+ switch (zbx_process) {
+ case ZBX_PROCESS_SERVER:
+ process_new_value(&item, &agent, now);
+ break;
+ case ZBX_PROCESS_PROXY:
+ proxy_process_new_value(&item, &agent, now);
+ break;
+ }
+ }
free_result(&agent);
}
@@ -438,7 +447,14 @@ static int get_values(int now)
}
else
{
- process_new_value(&item, &agent, now);
+ switch (zbx_process) {
+ case ZBX_PROCESS_SERVER:
+ process_new_value(&item, &agent, now);
+ break;
+ case ZBX_PROCESS_PROXY:
+ proxy_process_new_value(&item, &agent, now);
+ break;
+ }
DCadd_nextcheck(&item, now, NULL);
}
}
diff --git a/src/zabbix_server/trapper/trapper.c b/src/zabbix_server/trapper/trapper.c
index 22ba5a9b..6422b0ed 100644
--- a/src/zabbix_server/trapper/trapper.c
+++ b/src/zabbix_server/trapper/trapper.c
@@ -270,7 +270,16 @@ static void process_mass_data(zbx_sock_t *sock, zbx_uint64_t proxy_hostid, AGENT
}
}
else
- process_new_value(&item, &agent, values[i].clock);
+ {
+ switch (zbx_process) {
+ case ZBX_PROCESS_SERVER:
+ process_new_value(&item, &agent, values[i].clock);
+ break;
+ case ZBX_PROCESS_PROXY:
+ proxy_process_new_value(&item, &agent, values[i].clock);
+ break;
+ }
+ }
(*processed)++;
}
else