diff options
| author | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-01-30 10:11:11 +0000 |
|---|---|---|
| committer | sasha <sasha@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2008-01-30 10:11:11 +0000 |
| commit | 04f209fa6f5b7c041301a9575fa2318e1bf69ff2 (patch) | |
| tree | 87407ea8bcbfc4e46c401d8e66143ff78b1f7af6 /src/zabbix_proxy | |
| parent | ccebaf4a0a90941dd6d107fd6af72414e34f33d5 (diff) | |
| download | zabbix-04f209fa6f5b7c041301a9575fa2318e1bf69ff2.tar.gz zabbix-04f209fa6f5b7c041301a9575fa2318e1bf69ff2.tar.xz zabbix-04f209fa6f5b7c041301a9575fa2318e1bf69ff2.zip | |
- [DEV-106] Synchronizing of configuration tables
[Proxy]
git-svn-id: svn://svn.zabbix.com/trunk@5292 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src/zabbix_proxy')
23 files changed, 619 insertions, 2651 deletions
diff --git a/src/zabbix_proxy/Makefile.am b/src/zabbix_proxy/Makefile.am index 17d140d9..70c80e79 100644 --- a/src/zabbix_proxy/Makefile.am +++ b/src/zabbix_proxy/Makefile.am @@ -5,7 +5,7 @@ SUBDIRS = \ discoverer \ housekeeper \ httppoller \ - nodewatcher \ + proxyconfig \ pinger \ poller \ trapper @@ -26,7 +26,7 @@ zabbix_proxy_LDADD = \ discoverer/libzbxdiscoverer.a \ housekeeper/libzbxhousekeeper.a \ httppoller/libzbxhttppoller.a \ - nodewatcher/libzbxnodewatcher.a \ + proxyconfig/libzbxproxyconfig.a \ pinger/libzbxpinger.a \ poller/libzbxpoller.a \ trapper/libzbxtrapper.a \ diff --git a/src/zabbix_proxy/nodewatcher/Makefile.am b/src/zabbix_proxy/nodewatcher/Makefile.am deleted file mode 100644 index 5d7387a7..00000000 --- a/src/zabbix_proxy/nodewatcher/Makefile.am +++ /dev/null @@ -1,9 +0,0 @@ -## Process this file with automake to produce Makefile.in - -noinst_LIBRARIES = libzbxnodewatcher.a - -libzbxnodewatcher_a_SOURCES = \ - nodewatcher.c nodewatcher.h \ - nodesender.c nodesender.h \ - nodecomms.c nodecomms.h \ - history.c history.h diff --git a/src/zabbix_proxy/nodewatcher/history.c b/src/zabbix_proxy/nodewatcher/history.c deleted file mode 100644 index edcdd69c..00000000 --- a/src/zabbix_proxy/nodewatcher/history.c +++ /dev/null @@ -1,347 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2006 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 "cfg.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#include "history.h" -#include "nodewatcher.h" -#include "nodecomms.h" - -/****************************************************************************** - * * - * Function: get_history_lastid: * - * * - * Purpose: get last history id from master node * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int get_history_lastid(int master_nodeid, int nodeid, ZBX_TABLE *table, zbx_uint64_t *lastid) -{ - zbx_sock_t sock; - char data[MAX_STRING_LEN], *answer; - int res = FAIL; - - zabbix_log(LOG_LEVEL_DEBUG, "In get_history_lastid()"); - - if (SUCCEED == connect_to_node(master_nodeid, &sock)) { - zbx_snprintf(data, sizeof(data), "ZBX_GET_HISTORY_LAST_ID%c%d%c%d\n%s%c%s", - ZBX_DM_DELIMITER, CONFIG_NODEID, - ZBX_DM_DELIMITER, nodeid, - table->table, ZBX_DM_DELIMITER, table->recid); - - if (FAIL == send_data_to_node(master_nodeid, &sock, data)) - goto disconnect; - - if (FAIL == recv_data_from_node(master_nodeid, &sock, &answer)) - goto disconnect; - - if (0 == strncmp(answer, "FAIL", 4)) { - zabbix_log( LOG_LEVEL_ERR, "NODE %d: get_history_lastid() FAIL from node %d for node %d", - CONFIG_NODEID, - master_nodeid, - nodeid); - goto disconnect; - } - - ZBX_STR2UINT64(*lastid, answer); - res = SUCCEED; -disconnect: - disconnect_node(&sock); - } - return res; -} - -/****************************************************************************** - * * - * Function: get_trends_lastid: * - * * - * Purpose: get last history lastid and lastclock from master node * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int get_trends_lastid(int master_nodeid, int nodeid, ZBX_TABLE *table, zbx_uint64_t *lastid, int *lastclock) -{ - zbx_sock_t sock; - char data[MAX_STRING_LEN], *answer, *ptr; - int res = FAIL; - - zabbix_log(LOG_LEVEL_DEBUG, "In get_trends_lastclock()"); - - if (SUCCEED == connect_to_node(master_nodeid, &sock)) { - zbx_snprintf(data, sizeof(data), "ZBX_GET_TRENDS_LAST_ID%c%d%c%d\n%s", - ZBX_DM_DELIMITER, CONFIG_NODEID, - ZBX_DM_DELIMITER, nodeid, - table->table); - - if (FAIL == send_data_to_node(master_nodeid, &sock, data)) - goto disconnect; - - if (FAIL == recv_data_from_node(master_nodeid, &sock, &answer)) - goto disconnect; - - if (0 == strncmp(answer, "FAIL", 4)) { - zabbix_log( LOG_LEVEL_ERR, "NODE %d: get_trends_lastid() FAIL from node %d for node %d", - CONFIG_NODEID, - master_nodeid, - nodeid); - goto disconnect; - } - - if (NULL != (ptr = strchr(answer, ZBX_DM_DELIMITER))) { - *ptr++ = '\0'; - - ZBX_STR2UINT64(*lastid, answer); - *lastclock = atoi(ptr); - - res = SUCCEED; - } -disconnect: - disconnect_node(&sock); - } - return res; -} - -/****************************************************************************** - * * - * Function : process_hstory_table_data: * - * * - * Purpose: process new history data * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void process_history_table_data(ZBX_TABLE *table, int master_nodeid, int nodeid) -{ - DB_RESULT result; - DB_ROW row; - char *data = NULL, *tmp = NULL; - int data_allocated = 1024*1024, tmp_allocated = 4096, tmp_offset, data_offset, f, fld, len; - int data_found = 0; - zbx_uint64_t lastid; - int lastclock = 0, clock; - - zabbix_log( LOG_LEVEL_DEBUG, "In process_history_table_data()"); - - DBbegin(); - - if ((table->flags & ZBX_HISTORY) && FAIL == get_history_lastid(master_nodeid, nodeid, table, &lastid)) - return; - if ((table->flags & ZBX_HISTORY_TRENDS) && FAIL == get_trends_lastid(master_nodeid, nodeid, table, &lastid, &lastclock)) - return; - - data = zbx_malloc(data, data_allocated); - tmp = zbx_malloc(tmp, tmp_allocated); - - data_offset = 0; - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "History%c%d%c%d%c%s", - ZBX_DM_DELIMITER, CONFIG_NODEID, - ZBX_DM_DELIMITER, nodeid, - ZBX_DM_DELIMITER, table->table); - - /* Do not send history for current node if CONFIG_NODE_NOHISTORY is set */ -/* if ((CONFIG_NODE_NOHISTORY != 0) && (CONFIG_NODEID == nodeid)) - goto exit;*/ - - tmp_offset = 0; - if (table->flags & ZBX_HISTORY_SYNC) { - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 128, "select %s,", - table->recid); - } else { /* ZBX_HISTORY, ZBX_HISTORY_TRENDS */ - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 16, "select "); - } - - for (f = 0; table->fields[f].name != 0; f++) { - if ((table->flags & ZBX_HISTORY_SYNC) && 0 == (table->fields[f].flags & ZBX_HISTORY_SYNC)) - continue; - - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 128, "%s,", - table->fields[f].name); - } - tmp_offset--; - - if (table->flags & ZBX_HISTORY_SYNC) { - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 1024, " from %s where nodeid=%d order by %s", - table->table, - nodeid, - table->recid); - } else if (table->flags & ZBX_HISTORY_TRENDS) { - clock = time(NULL) - 600; /* -10min */ - clock -= clock % 3600; - - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 1024, " from %s where" - " (itemid>"ZBX_FS_UI64" or (itemid="ZBX_FS_UI64" and clock>%d)) and clock<%d" - " and"ZBX_COND_NODEID"order by itemid,clock", - table->table, - lastid, lastid, lastclock, clock, - ZBX_NODE("itemid", nodeid)); - } else { /* ZBX_HISTORY */ - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 1024, " from %s where %s>"ZBX_FS_UI64 - " and"ZBX_COND_NODEID"order by %2$s", - table->table, - table->recid, - lastid, - ZBX_NODE(table->recid, nodeid)); - } - - result = DBselectN(tmp, 10000); - while (NULL != (row = DBfetch(result))) { - if (table->flags & ZBX_HISTORY_SYNC) { - ZBX_STR2UINT64(lastid, row[0]); - fld = 1; - } else - fld = 0; - - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "\n"); - - for (f = 0; table->fields[f].name != 0; f++) { - if ((table->flags & ZBX_HISTORY_SYNC) && 0 == (table->fields[f].flags & ZBX_HISTORY_SYNC)) - continue; - - if (table->fields[f].type == ZBX_TYPE_INT || - table->fields[f].type == ZBX_TYPE_UINT || - table->fields[f].type == ZBX_TYPE_ID || - table->fields[f].type == ZBX_TYPE_FLOAT) - { - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s%c", - row[fld], ZBX_DM_DELIMITER); - } else { /* ZBX_TYPE_CHAR ZBX_TYPE_BLOB ZBX_TYPE_TEXT */ - len = (int)strlen(row[fld]); - len = zbx_binary2hex((u_char *)row[fld], len, &tmp, &tmp_allocated); - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, len + 8, "%s%c", - tmp, ZBX_DM_DELIMITER); - } - fld++; - } - data_offset--; - data_found = 1; - } - DBfree_result(result); - - data[data_offset] = '\0'; - - if (1 == data_found && SUCCEED == send_to_node(table->table, master_nodeid, nodeid, data)) { - if (table->flags & ZBX_HISTORY_SYNC) { - DBexecute("delete from %s where nodeid=%d and %s<="ZBX_FS_UI64, - table->table, - nodeid, - table->recid, - lastid); - } - } - - DBcommit(); - - zbx_free(tmp); - zbx_free(data); -} - -/****************************************************************************** - * * - * Function: process_history_tables * - * * - * Purpose: process new history data from tables with ZBX_HISTORY* flags * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static void process_history_tables(int master_nodeid, int nodeid) -{ - int t; - int start = time(NULL); - - zabbix_log(LOG_LEVEL_DEBUG, "In process_history_tables()"); - - for (t = 0; tables[t].table != 0; t++) { - if (tables[t].flags & (ZBX_HISTORY | ZBX_HISTORY_SYNC | ZBX_HISTORY_TRENDS)) - process_history_table_data(&tables[t], master_nodeid, nodeid); - } - - zabbix_log(LOG_LEVEL_DEBUG, "NODE %d: Spent %d seconds for node %d in process_history_tables", - CONFIG_NODEID, - time(NULL) - start, - nodeid); -} -/****************************************************************************** - * * - * Function: main_historysender * - * * - * Purpose: periodically sends historical data to master node * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void main_historysender() -{ - DB_RESULT result; - DB_ROW row; - int master_nodeid, nodeid; - - zabbix_log(LOG_LEVEL_DEBUG, "In main_historysender()"); - - master_nodeid = CONFIG_MASTER_NODEID; - if (0 == master_nodeid) - return; - - result = DBselect("select nodeid from nodes"); - while ((row = DBfetch(result))) { - nodeid = atoi(row[0]); - if (SUCCEED == is_master_node(CONFIG_NODEID, nodeid)) - continue; - - process_history_tables(master_nodeid, nodeid); - } - DBfree_result(result); -} diff --git a/src/zabbix_proxy/nodewatcher/history.h b/src/zabbix_proxy/nodewatcher/history.h deleted file mode 100644 index cf064cc3..00000000 --- a/src/zabbix_proxy/nodewatcher/history.h +++ /dev/null @@ -1,25 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 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. -**/ - -#ifndef ZABBIX_NODEWATCHER_HISTORY_H -#define ZABBIX_NODEWATCHER_HISTORY_H - -void main_historysender(); - -#endif diff --git a/src/zabbix_proxy/nodewatcher/nodecomms.c b/src/zabbix_proxy/nodewatcher/nodecomms.c deleted file mode 100644 index 7f87befc..00000000 --- a/src/zabbix_proxy/nodewatcher/nodecomms.c +++ /dev/null @@ -1,153 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2006 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 "cfg.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#include "comms.h" -#include "nodecomms.h" - -int connect_to_node(int nodeid, zbx_sock_t *sock) -{ - DB_RESULT result; - DB_ROW row; - unsigned short port; - int res = FAIL; - - zabbix_log(LOG_LEVEL_DEBUG, "In connect_to_node(nodeid:%d)", nodeid); - - result = DBselect("select ip,port from nodes where nodeid=%d", - nodeid); - - if (NULL != (row = DBfetch(result))) { - port = (unsigned short)atoi(row[1]); - - if (SUCCEED == zbx_tcp_connect(sock, row[0], port, 0)) - res = SUCCEED; - else - zabbix_log(LOG_LEVEL_ERR, "NODE %d: Unable to connect to Node [%d] error: %s", - CONFIG_NODEID, - nodeid, - zbx_tcp_strerror()); - } else - zabbix_log(LOG_LEVEL_ERR, "NODE %d: Node [%d] is unknown", - CONFIG_NODEID, - nodeid); - DBfree_result(result); - - return res; -} - -int send_data_to_node(int nodeid, zbx_sock_t *sock, const char *data) -{ - int res; - - if (FAIL == (res = zbx_tcp_send_ext(sock, data, ZBX_TCP_NEW_PROTOCOL))) { - zabbix_log(LOG_LEVEL_ERR, "NODE %d: Error while sending data to Node [%d] error: %s", - CONFIG_NODEID, - nodeid, - zbx_tcp_strerror()); - } else - zabbix_log(LOG_LEVEL_DEBUG, "NODE %d: Sending [%s] to Node [%d]", - CONFIG_NODEID, - data, - nodeid); - - return res; -} - -int recv_data_from_node(int nodeid, zbx_sock_t *sock, char **data) -{ - int res; - - if (FAIL == (res = zbx_tcp_recv_ext(sock, data, 0))) { - zabbix_log(LOG_LEVEL_ERR, "NODE %d: Error while receiving answer from Node [%d] error: %s", - CONFIG_NODEID, - nodeid, - zbx_tcp_strerror()); - } else - zabbix_log(LOG_LEVEL_DEBUG, "NODE %d: Receiving [%s] from Node [%d]", - CONFIG_NODEID, - *data, - nodeid); - - return res; -} - -void disconnect_node(zbx_sock_t *sock) -{ - zbx_tcp_close(sock); -} - -/****************************************************************************** - * * - * Function: send_to_node * - * * - * Purpose: send configuration changes to required node * - * * - * Parameters: * - * * - * Return value: SUCCESS - processed succesfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int send_to_node(const char *name, int dest_nodeid, int nodeid, char *data) -{ - int ret = FAIL; - zbx_sock_t sock; - char *answer; - - zabbix_log( LOG_LEVEL_WARNING, "NODE %d: Sending %s of node %d to node %d datalen %zd", - CONFIG_NODEID, - name, - nodeid, - dest_nodeid, - strlen(data)); - - if (FAIL == connect_to_node(dest_nodeid, &sock)) - return FAIL; - - if (FAIL == send_data_to_node(dest_nodeid, &sock, data)) - goto disconnect; - - if (FAIL == recv_data_from_node(dest_nodeid, &sock, &answer)) - goto disconnect; - - if (0 == strcmp(answer, "OK")) - { - zabbix_log( LOG_LEVEL_DEBUG, "OK"); - ret = SUCCEED; - } - else - { - zabbix_log( LOG_LEVEL_WARNING, "NOT OK"); - } -disconnect: - disconnect_node(&sock); - - return ret; -} diff --git a/src/zabbix_proxy/nodewatcher/nodesender.c b/src/zabbix_proxy/nodewatcher/nodesender.c deleted file mode 100644 index 0e49e60b..00000000 --- a/src/zabbix_proxy/nodewatcher/nodesender.c +++ /dev/null @@ -1,673 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2006 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 "cfg.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#include "nodesender.h" -#include "nodewatcher.h" -#include "nodecomms.h" -#include "../trapper/nodesync.h" - - -/****************************************************************************** - * * - * Function: calculate_checksums * - * * - * Purpose: calculate check sums of configuration data * - * * - * Parameters: * - * * - * Return value: SUCCESS - calculated succesfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int calculate_checksums(int nodeid, const char *tablename, const zbx_uint64_t id) -{ - char *sql = NULL; - int sql_allocated = 16*1024, sql_offset = 0; - int t, f, res = SUCCEED; - - zabbix_log(LOG_LEVEL_DEBUG, "In calculate_checksums"); - - sql = zbx_malloc(sql, sql_allocated); - - for (t = 0; tables[t].table != 0; t++) { - /* Do not sync some of tables */ - if ((tables[t].flags & ZBX_SYNC) == 0) - continue; - - if (NULL != tablename && 0 != strcmp(tablename, tables[t].table)) - continue; - -#ifdef HAVE_MYSQL - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, - "%s select %d,'%s',%s,%d,concat_ws(',',", - sql_offset > 0 ? "union all" : "insert into node_cksum (nodeid,tablename,recordid,cksumtype,cksum)", - nodeid, - tables[t].table, - tables[t].recid, - NODE_CKSUM_TYPE_NEW); -#else - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, - "%s select %d,'%s',%s,%d,", - sql_offset > 0 ? "union all" : "insert into node_cksum (nodeid,tablename,recordid,cksumtype,cksum)", - nodeid, - tables[t].table, - tables[t].recid, - NODE_CKSUM_TYPE_NEW); -#endif - - for (f = 0; tables[t].fields[f].name != 0; f ++) { - if ((tables[t].fields[f].flags & ZBX_SYNC) == 0) - continue; - - if (tables[t].fields[f].flags & ZBX_NOTNULL) { - switch ( tables[t].fields[f].type ) { - case ZBX_TYPE_ID : - case ZBX_TYPE_INT : - case ZBX_TYPE_UINT : - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "%s", - tables[t].fields[f].name); - break; - default : - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "md5(%s)", - tables[t].fields[f].name); - break; - } - } else { - switch ( tables[t].fields[f].type ) { - case ZBX_TYPE_ID : - case ZBX_TYPE_INT : - case ZBX_TYPE_UINT : - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "case when %s is null then 'NULL' else %1$s end", - tables[t].fields[f].name); - break; - default : - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "case when %s is null then 'NULL' else md5(%1$s) end", - tables[t].fields[f].name); - break; - } - } -#ifdef HAVE_MYSQL - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 16, - "," - ); -#else - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 16, - "||','||" - ); -#endif - } - - /* remove last delimiter */ - if (f > 0) { -#ifdef HAVE_MYSQL - sql_offset --; - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 16, ")"); -#else - sql_offset -= 7; -#endif - } - - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 512, - " from %s where"ZBX_COND_NODEID, - tables[t].table, - ZBX_NODE(tables[t].recid,nodeid)); - - if (0 != id) { - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, - "and %s="ZBX_FS_UI64, - tables[t].recid, - id); - } - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "\n"); - } - if (SUCCEED == res && DBexecute("delete from node_cksum where nodeid=%d and cksumtype=%d", - nodeid, - NODE_CKSUM_TYPE_NEW) < ZBX_DB_OK) - res = FAIL; - if (SUCCEED == res && DBexecute("%s", sql) < ZBX_DB_OK) - res = FAIL; - - return res; -} - -/****************************************************************************** - * * - * Function: send_config_data * - * * - * Purpose: send configuration changes to required node * - * * - * Parameters: * - * * - * Return value: SUCCESS - processed succesfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -char *get_config_data(int nodeid, int dest_nodetype) -{ - DB_RESULT result; - DB_RESULT result2; - DB_ROW row; - DB_ROW row2; - - char *data = NULL, *hex = NULL, *sql = NULL, c, sync[129], *s, *r[2], *d[2]; - int data_offset=0, sql_offset = 0; - int data_allocated=1024, hex_allocated=1024, sql_allocated=8*1024; - int t, f, j, rowlen; - - zabbix_log( LOG_LEVEL_DEBUG, "In get_config_data(node:%d,dest_nodetype:%s)", - nodeid, - dest_nodetype == ZBX_NODE_MASTER ? "MASTER" : "SLAVE"); - - data = zbx_malloc(data, data_allocated); - hex = zbx_malloc(hex, hex_allocated); - sql = zbx_malloc(sql, sql_allocated); - c = '1'; - - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "Data%c%d%c%d\n", - ZBX_DM_DELIMITER, - CONFIG_NODEID, - ZBX_DM_DELIMITER, - nodeid); - - /* Find updated records */ - result = DBselect("select curr.tablename,curr.recordid,prev.cksum,curr.cksum,prev.sync " - "from node_cksum curr, node_cksum prev " - "where curr.nodeid=%1$d and prev.nodeid=%1$d and " - "curr.tablename=prev.tablename and curr.recordid=prev.recordid and " - "curr.cksumtype=%3$d and prev.cksumtype=%2$d " - /*" and curr.tablename='hosts' "*/ - "union all " - /* Find new records */ - "select curr.tablename,curr.recordid,prev.cksum,curr.cksum,curr.sync " - "from node_cksum curr left join node_cksum prev " - "on prev.nodeid=%1$d and prev.tablename=curr.tablename and " - "prev.recordid=curr.recordid and prev.cksumtype=%2$d " - "where curr.nodeid=%1$d and curr.cksumtype=%3$d and prev.tablename is null " - /*" and curr.tablename='hosts' "*/ - "union all " - /* Find deleted records */ - "select prev.tablename,prev.recordid,prev.cksum,curr.cksum,prev.sync " - "from node_cksum prev left join node_cksum curr " - "on prev.nodeid=curr.nodeid and curr.nodeid=%1$d and curr.tablename=prev.tablename and " - "curr.recordid=prev.recordid and curr.cksumtype=%3$d " - "where prev.nodeid=%1$d and prev.cksumtype=%2$d and curr.tablename is null" - /*" and prev.tablename='hosts' "*/, - nodeid, - NODE_CKSUM_TYPE_OLD, /* prev */ - NODE_CKSUM_TYPE_NEW); /* curr */ - - while (NULL != (row = DBfetch(result))) { - for (t = 0; tables[t].table != 0 && strcmp(tables[t].table, row[0]) != 0; t++) - ; - - /* Found table */ - if (tables[t].table == 0) { - zabbix_log( LOG_LEVEL_WARNING, "Cannot find table [%s]", - row[0]); - continue; - } - - if (DBis_null(row[4]) == FAIL) - strcpy(sync, row[4]); - else - memset(sync, ' ', sizeof(sync)); - s = sync; - - /* Special (simpler) processing for operation DELETE */ - if (DBis_null(row[2]) == FAIL && DBis_null(row[3]) == SUCCEED && - ((dest_nodetype == ZBX_NODE_SLAVE && *s != c) || - (dest_nodetype == ZBX_NODE_MASTER && *(s+1) != c))) { - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s%c%s%c%d\n", - row[0], - ZBX_DM_DELIMITER, - row[1], - ZBX_DM_DELIMITER, - NODE_CONFIGLOG_OP_DELETE); - continue; - } - - r[0] = DBis_null(row[2]) == SUCCEED ? NULL : row[2]; - r[1] = DBis_null(row[3]) == SUCCEED ? NULL : row[3]; - f = 0; - sql_offset = 0; - - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "select "); - do { - while ((tables[t].fields[f].flags & ZBX_SYNC) == 0) - f++; - - d[0] = NULL; - d[1] = NULL; - if (NULL != r[0] && NULL != (d[0] = strchr(r[0], ','))) - *d[0] = '\0'; - if (NULL != r[1] && NULL != (d[1] = strchr(r[1], ','))) - *d[1] = '\0'; - - if (r[0] == NULL || r[1] == NULL || (dest_nodetype == ZBX_NODE_SLAVE && *s != c) || - (dest_nodetype == ZBX_NODE_MASTER && *(s+1) != c) || strcmp(r[0], r[1]) != 0) { - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, "%s,length(%1$s),", - tables[t].fields[f].name); - } - s += 2; - f++; - - if (d[0] != NULL) { - *d[0] = ','; - r[0] = d[0] + 1; - } else - r[0] = NULL; - if (d[1] != NULL) { - *d[1] = ','; - r[1] = d[1] + 1; - } else - r[1] = NULL; - } while (d[0] != NULL || d[1] != NULL); - - if (sql[sql_offset-1] != ',') - continue; - - sql_offset--; - zbx_snprintf_alloc(&sql, &sql_allocated, &sql_offset, 128, " from %s where %s=%s", - row[0], - tables[t].recid, - row[1]); - - result2 = DBselect("%s", sql); - if (NULL == (row2=DBfetch(result2))) - goto out; - - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s%c%s%c%d", - row[0], - ZBX_DM_DELIMITER, - row[1], - ZBX_DM_DELIMITER, - NODE_CONFIGLOG_OP_UPDATE); - - r[0] = DBis_null(row[2]) == SUCCEED ? NULL : row[2]; - r[1] = DBis_null(row[3]) == SUCCEED ? NULL : row[3]; - s = sync; - f = 0; - j = 0; - - do { - while ((tables[t].fields[f].flags & ZBX_SYNC) == 0) - f++; - - d[0] = NULL; - d[1] = NULL; - if (NULL != r[0] && NULL != (d[0] = strchr(r[0], ','))) - *d[0] = '\0'; - if (NULL != r[1] && NULL != (d[1] = strchr(r[1], ','))) - *d[1] = '\0'; - - if (r[0] == NULL || r[1] == NULL || (dest_nodetype == ZBX_NODE_SLAVE && *s != c) || - (dest_nodetype == ZBX_NODE_MASTER && *(s+1) != c) || strcmp(r[0], r[1]) != 0) { - - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%c%s%c%d%c", - ZBX_DM_DELIMITER, - tables[t].fields[f].name, - ZBX_DM_DELIMITER, - tables[t].fields[f].type, - ZBX_DM_DELIMITER); - - /* Fieldname, type, value */ - if (DBis_null(row2[j*2]) == SUCCEED) { - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "NULL"); - } else if(tables[t].fields[f].type == ZBX_TYPE_INT || - tables[t].fields[f].type == ZBX_TYPE_UINT || - tables[t].fields[f].type == ZBX_TYPE_ID || - tables[t].fields[f].type == ZBX_TYPE_FLOAT) { - - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "%s", row2[j*2]); - } else { - rowlen = atoi(row2[j*2+1]); - zbx_binary2hex((u_char *)row2[j*2], rowlen, &hex, &hex_allocated); - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, strlen(hex)+128, "%s", hex); -/*zabbix_log(LOG_LEVEL_CRIT, "----- [field:%s][type:%d][row:%s][hex:%s]",tables[t].fields[f].name,tables[t].fields[f].type,row2[j*2],hex);*/ - } - j++; - } - s += 2; - f++; - - if (d[0] != NULL) { - *d[0] = ','; - r[0] = d[0] + 1; - } else - r[0] = NULL; - if (d[1] != NULL) { - *d[1] = ','; - r[1] = d[1] + 1; - } else - r[1] = NULL; - } while (d[0] != NULL || d[1] != NULL); - zbx_snprintf_alloc(&data, &data_allocated, &data_offset, 128, "\n"); -out: - DBfree_result(result2); - } - DBfree_result(result); - - zbx_free(hex); - zbx_free(sql); - - return data; -} - -/****************************************************************************** - * * - * Function: update_checksums * - * * - * Purpose: overwrite old checksums with new ones * - * * - * Parameters: * - * * - * Return value: SUCCESS - calculated succesfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int update_checksums(int nodeid, int synked_nodetype, int synked, const char *tablename, const zbx_uint64_t id, char *fields) -{ - char *r[2], *d[2], sync[129], *s; - char c, sql[2][256]; - char cksum[32*64+32], *ck; - DB_RESULT result; - DB_ROW row; - int t, f; - - zabbix_log(LOG_LEVEL_DEBUG, "In update_checksums"); - - c = synked == SUCCEED ? '1' : ' '; - - if (NULL != tablename) { - zbx_snprintf(sql[0], sizeof(sql[0]), " and curr.tablename='%s' and curr.recordid="ZBX_FS_UI64, - tablename, id); - zbx_snprintf(sql[1], sizeof(sql[1]), " and prev.tablename='%s' and prev.recordid="ZBX_FS_UI64, - tablename, id); - } else { - *sql[0] = '\0'; - *sql[1] = '\0'; - } - - /* Find updated records */ - result = DBselect("select curr.tablename,curr.recordid,prev.cksum,curr.cksum,prev.sync " - "from node_cksum curr, node_cksum prev " - "where curr.nodeid=%1$d and prev.nodeid=%1$d and " - "curr.tablename=prev.tablename and curr.recordid=prev.recordid and " - "curr.cksumtype=%3$d and prev.cksumtype=%2$d%4$s " - "union all " - /* Find new records */ - "select curr.tablename,curr.recordid,prev.cksum,curr.cksum,NULL " - "from node_cksum curr left join node_cksum prev " - "on prev.nodeid=%1$d and prev.tablename=curr.tablename and " - "prev.recordid=curr.recordid and prev.cksumtype=%2$d " - "where curr.nodeid=%1$d and curr.cksumtype=%3$d and prev.tablename is null%4$s " - "union all " - /* Find deleted records */ - "select prev.tablename,prev.recordid,prev.cksum,curr.cksum,prev.sync " - "from node_cksum prev left join node_cksum curr " - "on prev.nodeid=curr.nodeid and curr.nodeid=%1$d and curr.tablename=prev.tablename and " - "curr.recordid=prev.recordid and curr.cksumtype=%3$d " - "where prev.nodeid=%1$d and prev.cksumtype=%2$d and curr.tablename is null%5$s", - nodeid, - NODE_CKSUM_TYPE_OLD, /* prev */ - NODE_CKSUM_TYPE_NEW, /* curr */ - sql[0], - sql[1]); - - while (NULL != (row = DBfetch(result))) { - for (t = 0; tables[t].table != 0 && strcmp(tables[t].table, row[0]) != 0; t++) - ; - - /* Found table */ - if (tables[t].table == 0) { - zabbix_log(LOG_LEVEL_WARNING, "Cannot find table [%s]", - row[0]); - continue; - } - - if (DBis_null(row[4]) == FAIL) - strcpy(sync, row[4]); - else - memset(sync, ' ', sizeof(sync)); - s = sync; - ck = cksum; - *ck = '\0'; - - /* Special (simpler) processing for operation DELETE */ - if (DBis_null(row[3]) == SUCCEED) { - if (*(s+2) != '\0') { - *s = ' '; - *(s+1) = ' '; - } - if (synked == SUCCEED) { - if (synked_nodetype == ZBX_NODE_SLAVE) - *s = c; - else if (synked_nodetype == ZBX_NODE_MASTER) - *(s+1) = c; - } - s += 2; - } else { - r[0] = DBis_null(row[2]) == SUCCEED ? NULL : row[2]; - r[1] = DBis_null(row[3]) == SUCCEED ? NULL : row[3]; - f = 0; - - do { - while ((tables[t].fields[f].flags & ZBX_SYNC) == 0) - f++; - - d[0] = NULL; - d[1] = NULL; - if (NULL != r[0] && NULL != (d[0] = strchr(r[0], ','))) - *d[0] = '\0'; - if (NULL != r[1] && NULL != (d[1] = strchr(r[1], ','))) - *d[1] = '\0'; - - if (NULL == tablename || SUCCEED == str_in_list(fields, tables[t].fields[f].name, ',')) { - ck += zbx_snprintf(ck, 64, "%s,", NULL != r[1] ? r[1] : r[0]); - - if (r[0] == NULL || r[1] == NULL || strcmp(r[0], r[1]) != 0) { - if (synked_nodetype == ZBX_NODE_SLAVE) { - *s = c; - *(s+1) = ' '; - } else if (synked_nodetype == ZBX_NODE_MASTER) { - *s = ' '; - *(s+1) = c; - } - } else { - if (synked == SUCCEED) { - if (synked_nodetype == ZBX_NODE_SLAVE) - *s = c; - else if (synked_nodetype == ZBX_NODE_MASTER) - *(s+1) = c; - } - } - } else - ck += zbx_snprintf(ck, 64, "%s,", NULL != r[0] ? r[0] : ""); - s += 2; - f++; - - if (d[0] != NULL) { - *d[0] = ','; - r[0] = d[0] + 1; - } else - r[0] = NULL; - if (d[1] != NULL) { - *d[1] = ','; - r[1] = d[1] + 1; - } else - r[1] = NULL; - } while (d[0] != NULL || d[1] != NULL); - } - *s = '\0'; - *--ck = '\0'; - - if (DBis_null(row[2]) == SUCCEED || DBis_null(row[3]) == SUCCEED || - strcmp(row[4], sync) != 0 || strcmp(row[2], row[3]) != 0) - { - DBexecute("update node_cksum set cksumtype=%d,cksum=\'%s\',sync=\'%s\' " - "where nodeid=%d and tablename=\'%s\' and recordid=%s and cksumtype=%d", - NODE_CKSUM_TYPE_OLD, - cksum, - sync, - nodeid, - row[0], - row[1], - DBis_null(row[2]) == SUCCEED ? NODE_CKSUM_TYPE_NEW : NODE_CKSUM_TYPE_OLD); - } - } - DBfree_result(result); - - return SUCCEED; -} - -/****************************************************************************** - * * - * Function: node_sync_lock * - * * - * Purpose: * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void node_sync_lock(int nodeid) -{ - zbx_mutex_lock(&node_sync_access); -} - -/****************************************************************************** - * * - * Function: node_sync_unlock * - * * - * Purpose: * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void node_sync_unlock(int nodeid) -{ - zbx_mutex_unlock(&node_sync_access); -} - -/****************************************************************************** - * * - * Function: process_nodes * - * * - * Purpose: calculates checks sum of config data * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Aleksander Vladishev * - * * - * Comments: never returns * - * * - ******************************************************************************/ -void process_nodes() -{ - DB_RESULT result; - DB_ROW row; - int nodeid; - int master_nodeid; - char *data, *answer; - zbx_sock_t sock; - int res; -/* int now = time(NULL);*/ - int sender_nodeid; - - master_nodeid = CONFIG_MASTER_NODEID; - if (0 == master_nodeid) - return; - - result = DBselect("select nodeid from nodes"); - while (NULL != (row=DBfetch(result))) { - nodeid = atoi(row[0]); - if (SUCCEED == is_master_node(CONFIG_NODEID, nodeid)) - continue; - - node_sync_lock(nodeid); - -/* DBbegin();*/ - - res = calculate_checksums(nodeid, NULL, 0); - if (SUCCEED == res && NULL != (data = get_config_data(nodeid, ZBX_NODE_MASTER))) { - zabbix_log( LOG_LEVEL_WARNING, "NODE %d: Sending configuration changes to master node %d for node %d datalen %d", - CONFIG_NODEID, - master_nodeid, - nodeid, - strlen(data)); - if (SUCCEED == (res = connect_to_node(master_nodeid, &sock))) { - if (SUCCEED == res) - res = send_data_to_node(master_nodeid, &sock, data); - if (SUCCEED == res) - res = recv_data_from_node(master_nodeid, &sock, &answer); - if (SUCCEED == res && 0 == strncmp(answer, "Data", 4)) { - res = update_checksums(nodeid, ZBX_NODE_MASTER, SUCCEED, NULL, 0, NULL); - if (SUCCEED == res) - res = node_sync(answer, &sender_nodeid, &nodeid); - send_data_to_node(master_nodeid, &sock, SUCCEED == res ? "OK" : "FAIL"); - } - disconnect_node(&sock); - } - zbx_free(data); - } - -/* DBcommit();*/ - - node_sync_unlock(nodeid); - } - DBfree_result(result); - -/* zabbix_log(LOG_LEVEL_CRIT, "<-----> process_nodes [Selected records in %d seconds]", time(NULL)-now);*/ -} diff --git a/src/zabbix_proxy/nodewatcher/nodesender.h b/src/zabbix_proxy/nodewatcher/nodesender.h deleted file mode 100644 index 7f64e5da..00000000 --- a/src/zabbix_proxy/nodewatcher/nodesender.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 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. -**/ - -#ifndef ZABBIX_NODESENDER_H -#define ZABBIX_NODESENDER_H - -#include "mutexs.h" - -#define ZBX_NODE_MASTER 0 -#define ZBX_NODE_SLAVE 1 - -extern ZBX_MUTEX node_sync_access; - -int calculate_checksums(int nodeid, const char *tablename, const zbx_uint64_t id); -char *get_config_data(int nodeid, int dest_nodetype); -int update_checksums(int nodeid, int synked_nodetype, int synked, const char *tablename, const zbx_uint64_t id, char *fields); -void node_sync_lock(int nodeid); -void node_sync_unlock(int nodeid); -void process_nodes(); - -#endif diff --git a/src/zabbix_proxy/nodewatcher/nodewatcher.c b/src/zabbix_proxy/nodewatcher/nodewatcher.c deleted file mode 100644 index 951bc871..00000000 --- a/src/zabbix_proxy/nodewatcher/nodewatcher.c +++ /dev/null @@ -1,120 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2006 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 "cfg.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#include "nodewatcher.h" -#include "nodesender.h" -#include "history.h" - -/****************************************************************************** - * * - * Function: is_master_node * - * * - * Purpose: * - * * - * Parameters: * - * * - * Return value: SUCCEED - nodeid is master node * - * FAIL - nodeid is slave node * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int is_master_node(int current_nodeid, int nodeid) -{ - DB_RESULT dbresult; - DB_ROW dbrow; - int res = FAIL; - - dbresult = DBselect("select masterid from nodes where nodeid=%d", - current_nodeid); - - if (NULL != (dbrow = DBfetch(dbresult))) { - current_nodeid = atoi(dbrow[0]); - if (current_nodeid == nodeid) - res = SUCCEED; - else if (0 != current_nodeid) - res = is_master_node(current_nodeid, nodeid); - } - DBfree_result(dbresult); - - return res; -} - -/****************************************************************************** - * * - * Function: main_nodewatcher_loop * - * * - * Purpose: periodically calculates checks sum of config data * - * * - * Parameters: * - * * - * Return value: * - * * - * Author: Alexei Vladishev * - * * - * Comments: never returns * - * * - ******************************************************************************/ -int main_nodewatcher_loop() -{ - int start, end; - int lastrun = 0; - - zabbix_log( LOG_LEVEL_DEBUG, "In main_nodeupdater_loop()"); - for(;;) - { - start = time(NULL); - - zbx_setproctitle("connecting to the database"); - zabbix_log( LOG_LEVEL_DEBUG, "Starting sync with nodes"); - - DBconnect(ZBX_DB_CONNECT_NORMAL); - - if(lastrun + 120 < start) - { - process_nodes(); - - lastrun = start; - } - - /* Send new history data to master node */ - main_historysender(); - - DBclose(); - - end = time(NULL); - - if(end-start<10) - { - zbx_setproctitle("sender [sleeping for %d seconds]", - 10-(end-start)); - zabbix_log( LOG_LEVEL_DEBUG, "Sleeping %d seconds", - 10-(end-start)); - sleep(10-(end-start)); - } - } -} diff --git a/src/zabbix_proxy/nodewatcher/nodewatcher.h b/src/zabbix_proxy/nodewatcher/nodewatcher.h deleted file mode 100644 index 60285d0f..00000000 --- a/src/zabbix_proxy/nodewatcher/nodewatcher.h +++ /dev/null @@ -1,26 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 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. -**/ - -#ifndef ZABBIX_NODEWATCHER_H -#define ZABBIX_NODEWATCHER_H - -int main_nodewatcher_loop(); -int is_master_node(int current_nodeid, int nodeid); - -#endif diff --git a/src/zabbix_proxy/operations.c b/src/zabbix_proxy/operations.c index e2f11a78..b4ae1996 100644 --- a/src/zabbix_proxy/operations.c +++ b/src/zabbix_proxy/operations.c @@ -104,6 +104,37 @@ static void send_to_user_medias(DB_EVENT *event,DB_OPERATION *operation, zbx_uin /****************************************************************************** * * + * Function: check_user_active * + * * + * Purpose: checks if user in any users_disabled group * + * * + * Parameters: userid - user id * + * * + * Return value: int SUCCEED / FAIL-if user disabled * + * * + * Author: Aly * + * * + * Comments: * + * * + ******************************************************************************/ +int check_user_active(zbx_uint64_t userid){ + DB_RESULT result; + DB_ROW row; + int rtrn = SUCCEED; + + result = DBselect("SELECT COUNT(g.usrgrpid) FROM users_groups ug, usrgrp g WHERE ug.userid=" ZBX_FS_UI64 " AND g.usrgrpid=ug.usrgrpid AND g.users_status=%d", userid, GROUP_STATUS_DISABLED); + + row = DBfetch(result); + if(row && (DBis_null(row[0])!=SUCCEED) && (atoi(row[0])>0)) + rtrn=FAIL; + + DBfree_result(result); + +return rtrn; +} + +/****************************************************************************** + * * * Function: op_notify_user * * * * Purpose: send notifications to user or user groupd * @@ -128,19 +159,15 @@ void op_notify_user(DB_EVENT *event, DB_ACTION *action, DB_OPERATION *operation) if(operation->object == OPERATION_OBJECT_USER) { - result = DBselect("SELECT count(u.userid) as user_cnt FROM users u WHERE u.userid= " ZBX_FS_UI64 " and u.status=%d", - operation->objectid, USER_STATUS_ACTIVE); - row = DBfetch(result); - if(row && (DBis_null(row[0])!=SUCCEED) && (atoi(row[0])>0)) + if(check_user_active(operation->objectid) == SUCCEED) { send_to_user_medias(event, operation, operation->objectid); } - DBfree_result(result); } else if(operation->object == OPERATION_OBJECT_GROUP) { - result = DBselect("select u.userid from users u, users_groups ug where ug.usrgrpid=" ZBX_FS_UI64 " and ug.userid=u.userid and u.status=%d", - operation->objectid,USER_STATUS_ACTIVE); + result = DBselect("select u.userid from users u, users_groups ug, usrgrp g where ug.usrgrpid=" ZBX_FS_UI64 " and ug.userid=u.userid and g.usrgrpid=ug.usrgrpid and g.users_status=%d", + operation->objectid,GROUP_STATUS_ACTIVE); while((row=DBfetch(result))) { ZBX_STR2UINT64(userid, row[0]); diff --git a/src/zabbix_proxy/proxy.c b/src/zabbix_proxy/proxy.c index ed94f74f..3202ebd1 100644 --- a/src/zabbix_proxy/proxy.c +++ b/src/zabbix_proxy/proxy.c @@ -49,7 +49,7 @@ #include "poller/poller.h" #include "poller/checks_snmp.h" #include "trapper/trapper.h" -#include "nodewatcher/nodewatcher.h" +#include "proxyconfig/proxyconfig.h" /* #define LISTENQ 1024 @@ -115,7 +115,7 @@ pid_t *threads=NULL; int CONFIG_DBSYNCER_FORKS = 0;//1; int CONFIG_DISCOVERER_FORKS = 1; int CONFIG_HOUSEKEEPER_FORKS = 1; -int CONFIG_NODEWATCHER_FORKS = 1; +/*int CONFIG_NODEWATCHER_FORKS = 1;*/ int CONFIG_PINGER_FORKS = 1; int CONFIG_POLLER_FORKS = 5; int CONFIG_HTTPPOLLER_FORKS = 5; @@ -148,9 +148,11 @@ char *CONFIG_DBSOCKET = NULL; int CONFIG_DBPORT = 0; int CONFIG_ENABLE_REMOTE_COMMANDS = 0; +char *CONFIG_SERVER = NULL; +int CONFIG_SERVER_PORT = 10050; +char *CONFIG_HOSTNAME = NULL; int CONFIG_NODEID = 0; int CONFIG_MASTER_NODEID = 0; -int CONFIG_NODE_NOEVENTS = 0; int CONFIG_NODE_NOHISTORY = 0; /* Global variable to control if we should write warnings to log[] */ @@ -185,7 +187,10 @@ void init_config(void) static struct cfg_line cfg[]= { /* PARAMETER ,VAR ,FUNC, TYPE(0i,1s),MANDATORY,MIN,MAX */ -/* {"StartDBSyncers",&CONFIG_DBSYNCER_FORKS,0,TYPE_INT,PARM_OPT,0,1},*/ + {"Server",&CONFIG_SERVER,0,TYPE_STRING,PARM_MAND,0,0}, + {"ServerPort",&CONFIG_SERVER_PORT,0,TYPE_INT,PARM_MAND,1024,32768}, + {"Hostname",&CONFIG_HOSTNAME,0,TYPE_STRING,PARM_MAND,0,0}, + {"StartDiscoverers",&CONFIG_DISCOVERER_FORKS,0,TYPE_INT,PARM_OPT,0,255}, {"StartHTTPPollers",&CONFIG_HTTPPOLLER_FORKS,0,TYPE_INT,PARM_OPT,0,255}, {"StartPingers",&CONFIG_PINGER_FORKS,0,TYPE_INT,PARM_OPT,0,255}, @@ -219,8 +224,6 @@ void init_config(void) {"DBSocket",&CONFIG_DBSOCKET,0,TYPE_STRING,PARM_OPT,0,0}, {"DBPort",&CONFIG_DBPORT,0,TYPE_INT,PARM_OPT,1024,65535}, {"NodeID",&CONFIG_NODEID,0,TYPE_INT,PARM_OPT,0,65535}, - {"NodeNoEvents",&CONFIG_NODE_NOEVENTS,0,TYPE_INT,PARM_OPT,0,1}, - {"NodeNoHistory",&CONFIG_NODE_NOHISTORY,0,TYPE_INT,PARM_OPT,0,1}, {0} }; @@ -279,7 +282,7 @@ printf("test()\n"); zbx_json_adduint64(&j, "useip", &useip); zbx_json_addstring(&j, "ip", "127.0.0.1"); zbx_json_adduint64(&j, "port", &port); - zbx_json_return(&j); + zbx_json_close(&j); id++; } @@ -463,7 +466,7 @@ int MAIN_ZABBIX_ENTRY(void) threads = calloc(1+CONFIG_POLLER_FORKS+CONFIG_TRAPPERD_FORKS+CONFIG_PINGER_FORKS +CONFIG_HOUSEKEEPER_FORKS+CONFIG_UNREACHABLE_POLLER_FORKS - +CONFIG_NODEWATCHER_FORKS+CONFIG_HTTPPOLLER_FORKS+CONFIG_DISCOVERER_FORKS, + +/*CONFIG_NODEWATCHER_FORKS+*/CONFIG_HTTPPOLLER_FORKS+CONFIG_DISCOVERER_FORKS, sizeof(pid_t)); if(CONFIG_TRAPPERD_FORKS > 0) @@ -476,7 +479,7 @@ int MAIN_ZABBIX_ENTRY(void) } for( i=1; - i<=CONFIG_POLLER_FORKS+CONFIG_TRAPPERD_FORKS+CONFIG_PINGER_FORKS+CONFIG_HOUSEKEEPER_FORKS+CONFIG_UNREACHABLE_POLLER_FORKS+CONFIG_NODEWATCHER_FORKS+CONFIG_HTTPPOLLER_FORKS+CONFIG_DISCOVERER_FORKS+CONFIG_DBSYNCER_FORKS; + i<=CONFIG_POLLER_FORKS+CONFIG_TRAPPERD_FORKS+CONFIG_PINGER_FORKS+CONFIG_HOUSEKEEPER_FORKS+CONFIG_UNREACHABLE_POLLER_FORKS+/*CONFIG_NODEWATCHER_FORKS+*/CONFIG_HTTPPOLLER_FORKS+CONFIG_DISCOVERER_FORKS+CONFIG_DBSYNCER_FORKS; i++) { if((pid = zbx_fork()) == 0) @@ -495,10 +498,11 @@ int MAIN_ZABBIX_ENTRY(void) if(server_num == 0) { init_main_process(); + main_proxyconfig_loop(server_num); /* zabbix_log( LOG_LEVEL_WARNING, "server #%d started [Watchdog]", server_num); main_watchdog_loop();*/ -/* for(;;) zbx_sleep(3600);*/ + for(;;) zbx_sleep(3600); } @@ -538,26 +542,26 @@ int MAIN_ZABBIX_ENTRY(void) SNMP_FEATURE_STATUS); main_poller_loop(ZBX_POLLER_TYPE_UNREACHABLE, server_num - (CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS)); - } else if(server_num <= CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS +/* } else if(server_num <= CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS + CONFIG_UNREACHABLE_POLLER_FORKS + CONFIG_NODEWATCHER_FORKS) { - zabbix_log( LOG_LEVEL_WARNING, "server #%d started [Node watcher. Node ID:%d]", +zabbix_log( LOG_LEVEL_WARNING, "server #%d started [Node watcher. Node ID:%d]", server_num, CONFIG_NODEID); - main_nodewatcher_loop(); + main_nodewatcher_loop();*/ } else if(server_num <= CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS + CONFIG_UNREACHABLE_POLLER_FORKS - + CONFIG_NODEWATCHER_FORKS + CONFIG_HTTPPOLLER_FORKS) + + /*CONFIG_NODEWATCHER_FORKS + */CONFIG_HTTPPOLLER_FORKS) { zabbix_log( LOG_LEVEL_WARNING, "server #%d started [HTTP Poller]", server_num); main_httppoller_loop(server_num - CONFIG_POLLER_FORKS - CONFIG_TRAPPERD_FORKS -CONFIG_PINGER_FORKS - CONFIG_HOUSEKEEPER_FORKS - - CONFIG_UNREACHABLE_POLLER_FORKS - CONFIG_NODEWATCHER_FORKS); + - CONFIG_UNREACHABLE_POLLER_FORKS/* - CONFIG_NODEWATCHER_FORKS*/); } else if(server_num <= CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS + CONFIG_UNREACHABLE_POLLER_FORKS - + CONFIG_NODEWATCHER_FORKS + CONFIG_HTTPPOLLER_FORKS + CONFIG_DISCOVERER_FORKS) + /*+ CONFIG_NODEWATCHER_FORKS */+ CONFIG_HTTPPOLLER_FORKS + CONFIG_DISCOVERER_FORKS) { #ifdef HAVE_SNMP init_snmp("zabbix_server"); @@ -567,10 +571,10 @@ int MAIN_ZABBIX_ENTRY(void) SNMP_FEATURE_STATUS); main_discoverer_loop(server_num - CONFIG_POLLER_FORKS - CONFIG_TRAPPERD_FORKS -CONFIG_PINGER_FORKS - CONFIG_HOUSEKEEPER_FORKS - - CONFIG_UNREACHABLE_POLLER_FORKS - CONFIG_NODEWATCHER_FORKS - CONFIG_HTTPPOLLER_FORKS); + - CONFIG_UNREACHABLE_POLLER_FORKS - /*CONFIG_NODEWATCHER_FORKS - */CONFIG_HTTPPOLLER_FORKS); } else if(server_num <= CONFIG_POLLER_FORKS + CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS + CONFIG_UNREACHABLE_POLLER_FORKS - + CONFIG_NODEWATCHER_FORKS + CONFIG_HTTPPOLLER_FORKS + CONFIG_DISCOVERER_FORKS + CONFIG_DBSYNCER_FORKS) + + /*CONFIG_NODEWATCHER_FORKS + */CONFIG_HTTPPOLLER_FORKS + CONFIG_DISCOVERER_FORKS + CONFIG_DBSYNCER_FORKS) { zabbix_log( LOG_LEVEL_WARNING, "server #%d started [DB Syncer]", server_num); @@ -588,7 +592,7 @@ void zbx_on_exit() if(threads != NULL) { - for(i = 1; i <= CONFIG_POLLER_FORKS+CONFIG_TRAPPERD_FORKS+CONFIG_PINGER_FORKS+CONFIG_HOUSEKEEPER_FORKS+CONFIG_UNREACHABLE_POLLER_FORKS+CONFIG_NODEWATCHER_FORKS+CONFIG_HTTPPOLLER_FORKS+CONFIG_DISCOVERER_FORKS+CONFIG_DBSYNCER_FORKS; i++) + for(i = 1; i <= CONFIG_POLLER_FORKS+CONFIG_TRAPPERD_FORKS+CONFIG_PINGER_FORKS+CONFIG_HOUSEKEEPER_FORKS+CONFIG_UNREACHABLE_POLLER_FORKS+/*CONFIG_NODEWATCHER_FORKS+*/CONFIG_HTTPPOLLER_FORKS+CONFIG_DISCOVERER_FORKS+CONFIG_DBSYNCER_FORKS; i++) { if(threads[i]) { kill(threads[i],SIGTERM); diff --git a/src/zabbix_proxy/proxyconfig/Makefile.am b/src/zabbix_proxy/proxyconfig/Makefile.am new file mode 100644 index 00000000..81625631 --- /dev/null +++ b/src/zabbix_proxy/proxyconfig/Makefile.am @@ -0,0 +1,7 @@ +## Process this file with automake to produce Makefile.in + +noinst_LIBRARIES = libzbxproxyconfig.a + +libzbxproxyconfig_a_SOURCES = \ + proxyconfig.c proxyconfig.h \ + servercomms.c servercomms.h diff --git a/src/zabbix_proxy/proxyconfig/proxyconfig.c b/src/zabbix_proxy/proxyconfig/proxyconfig.c new file mode 100644 index 00000000..22666a66 --- /dev/null +++ b/src/zabbix_proxy/proxyconfig/proxyconfig.c @@ -0,0 +1,405 @@ +/* +** ZABBIX +** Copyright (C) 2000-2006 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 "db.h" +#include "log.h" +#include "zbxjson.h" + +#include "proxyconfig.h" +#include "servercomms.h" + +/****************************************************************************** + * * + * Function: process_proxyconfig_table * + * * + * Purpose: update configuration table * + * * + * Parameters: * + * * + * Return value: SUCCESS - processed succesfully * + * FAIL - an error occured * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static int process_proxyconfig_table(struct zbx_json_parse *jp, const char *tablename, const char *p) +{ + int t, f, field_count, insert, offset; + ZBX_TABLE *table = NULL; + 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 */ + const char *pf; + DB_RESULT result; + + 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) { + 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; + +/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...} + * ^---------------------------------------------------------------------------^ + */ if (FAIL == zbx_json_brackets_open(p, &jp_obj)) + goto json_error; + +/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...} + * ^ + */ if (NULL == (p = zbx_json_pair_by_name(&jp_obj, "fields"))) { + zabbix_log(LOG_LEVEL_WARNING, "Can't find \"fields\" pair"); + return FAIL; + } + +/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...} + * ^-------------------^ + */ if (FAIL == zbx_json_brackets_open(p, &jp_data)) + goto json_error; + + p = NULL; + field_count = 0; + while (NULL != (p = zbx_json_next(&jp_data, p))) { + if (NULL == (p = zbx_json_decodevalue(p, buf, sizeof(buf)))) + goto json_error; + + fields[field_count] = NULL; + for(f = 0; table->fields[f].name != NULL; f++) + if (0 == strcmp(table->fields[f].name, buf)) { + fields[field_count] = &table->fields[f]; + break; + } + + if (NULL == fields[field_count]) { + zabbix_log(LOG_LEVEL_WARNING, "Invalid field name \"%s\"", + buf); + return FAIL; + } + field_count++; + } + +/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...} + * ^ + */ if (NULL == (p = zbx_json_pair_by_name(&jp_obj, "data"))) { + zabbix_log(LOG_LEVEL_WARNING, "Can't find \"data\" pair"); + return FAIL; + } + +/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...} + * ^-----------------------------------^ + */ if (FAIL == zbx_json_brackets_open(p, &jp_data)) + goto json_error; + +/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...} + * ^ + */ p = NULL; + 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; + + if (NULL == (pf = zbx_json_next(&jp_row, NULL)) || NULL == (pf = zbx_json_decodevalue(pf, recid, sizeof(recid))) ) + goto json_error; + + result = DBselect("select 0 from %s where %s=%s", + table->table, + table->recid, + recid); + insert = (NULL == DBfetch(result)); + DBfree_result(result); + + 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,", + recid); + } else + offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, "update %s set ", + table->table); + +/* {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...} + * ^ + */ f = 1; + while (NULL != (pf = zbx_json_next(&jp_row, pf))) { + if (NULL == (pf = zbx_json_decodevalue(pf, buf, sizeof(buf)))) + goto json_error; + + 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 (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)); + 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); + } + f++; + } + + 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--; + if (insert) + offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, ")"); + else + offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, " where %s=%s", + table->recid, + recid); + + if (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; + } + + 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; + + if (ZBX_DB_OK > DBexecute("drop table %s_%s_tmp1", + table->table, + table->recid)) + goto db_error; + + zabbix_log(LOG_LEVEL_DEBUG, "End process_proxyconfig_table()"); + + return SUCCEED; +json_error: + zabbix_log(LOG_LEVEL_DEBUG, "Can't proceed table \"%s\". %s", + tablename, + zbx_json_strerror()); +db_error: + return FAIL; +} + +/****************************************************************************** + * * + * Function: process_proxyconfig * + * * + * Purpose: update configuration * + * * + * Parameters: * + * * + * Return value: SUCCESS - processed succesfully * + * FAIL - an error occured * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +static int process_proxyconfig(struct zbx_json_parse *jp) +{ + char tablename[MAX_STRING_LEN]; + size_t len = sizeof(tablename); + const char *p = NULL; + int res = SUCCEED; + + zabbix_log(LOG_LEVEL_DEBUG, "In process_proxyconfig()"); + + DBbegin(); + +/* + * {"hosts":{"fields":["hostid","host",...],"data":[[1,"zbx01",...],[2,"zbx02",...],...]},"items":{...},...} + * ^ + */ + while (NULL != (p = zbx_json_pair_next(jp, p, tablename, len)) && res == SUCCEED) { + if (ZBX_JSON_TYPE_OBJECT != zbx_json_type(p)) { + zabbix_log(LOG_LEVEL_WARNING, "Invalid type of data for table \"%s\" \"%.40s...\"", + tablename, + p); + res = FAIL; + break; + } + + res = process_proxyconfig_table(jp, tablename, p); + } + if (res == SUCCEED) + DBcommit(); + else + DBrollback(); + + return res; +} + +/****************************************************************************** + * * + * Function: node_sync_lock * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +/*void node_sync_lock(int nodeid) +{ + zbx_mutex_lock(&node_sync_access); +}*/ + +/****************************************************************************** + * * + * Function: node_sync_unlock * + * * + * Purpose: * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +/*void node_sync_unlock(int nodeid) +{ + zbx_mutex_unlock(&node_sync_access); +}*/ + +/****************************************************************************** + * * + * Function: process_nodes * + * * + * Purpose: calculates checks sum of config data * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: never returns * + * * + ******************************************************************************/ +static void process_configuration_sync() +{ + zbx_sock_t sock; + char *data; +/* int now = time(NULL);*/ + struct zbx_json_parse jp; + + zabbix_log(LOG_LEVEL_DEBUG, "In process_configuration_sync()"); + + if (FAIL == connect_to_server(&sock)) + return; + + if (FAIL == get_data_from_server(&sock, "configuration data", "ZBX_PROXY_CONFIG", &data)) + goto exit; + + if (FAIL == zbx_json_open(data, &jp)) + goto exit; + + if (FAIL == process_proxyconfig(&jp)) + zabbix_log(LOG_LEVEL_DEBUG, "----- process_proxyconfig() ---> FAIL <---"); +exit: + disconnect_server(&sock); + +/* zabbix_log(LOG_LEVEL_DEBUG, "<-----> process_nodes [Selected records in %d seconds]", time(NULL)-now);*/ +} + +/****************************************************************************** + * * + * Function: main_proxyconfig_loop * + * * + * Purpose: periodically request config data * + * * + * Parameters: * + * * + * Return value: * + * * + * Author: Aleksander Vladishev * + * * + * Comments: never returns * + * * + ******************************************************************************/ +void main_proxyconfig_loop(int server_num) +{ + int start, end, sleeptime = 10; + + zabbix_log(LOG_LEVEL_DEBUG, "In main_nodewatcher_loop()"); + + zabbix_log(LOG_LEVEL_WARNING, "server #%d started [Configuration syncer]", + server_num); + + for (;;) { + start = time(NULL); + + zbx_setproctitle("Configuration syncer [load configuration]"); + + DBconnect(ZBX_DB_CONNECT_NORMAL); + + process_configuration_sync(); + + DBclose(); + + end = time(NULL); + + if (end - start < sleeptime) { + zbx_setproctitle("Configuration syncer [sleeping for %d seconds]", + sleeptime - (end - start)); + zabbix_log (LOG_LEVEL_DEBUG, "Sleeping %d seconds", + sleeptime - (end - start)); + sleep(sleeptime - (end - start)); + } + } +} diff --git a/src/zabbix_proxy/trapper/nodesync.h b/src/zabbix_proxy/proxyconfig/proxyconfig.h index 94c3a594..0bcf0a54 100644 --- a/src/zabbix_proxy/trapper/nodesync.h +++ b/src/zabbix_proxy/proxyconfig/proxyconfig.h @@ -17,10 +17,15 @@ ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ +#ifndef ZABBIX_PROXYCONFIG_H +#define ZABBIX_PROXYCONFIG_H -#ifndef ZABBIX_NODESYNC_H -#define ZABBIX_NODESYNC_H +/*#include "mutexs.h" -int node_sync(char *data, int *sender_nodeid, int *nodeid); +extern ZBX_MUTEX node_sync_access;*/ + +/*void node_sync_lock(int nodeid); +void node_sync_unlock(int nodeid);*/ +void main_proxyconfig_loop(int server_num); #endif diff --git a/src/zabbix_proxy/proxyconfig/servercomms.c b/src/zabbix_proxy/proxyconfig/servercomms.c new file mode 100644 index 00000000..f33bd999 --- /dev/null +++ b/src/zabbix_proxy/proxyconfig/servercomms.c @@ -0,0 +1,125 @@ +/* +** ZABBIX +** Copyright (C) 2000-2006 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 "cfg.h" +#include "db.h" +#include "log.h" +#include "zbxjson.h" + +#include "comms.h" +#include "servercomms.h" + +int connect_to_server(zbx_sock_t *sock) +{ + int res; + + zabbix_log(LOG_LEVEL_DEBUG, "In connect_to_server() [%s]:%d", + CONFIG_SERVER, + CONFIG_SERVER_PORT); + + if (FAIL == (res = zbx_tcp_connect(sock, CONFIG_SERVER, CONFIG_SERVER_PORT, 0))) + zabbix_log(LOG_LEVEL_ERR, "Unable connect to the server [%s]:%d [%s]", + CONFIG_SERVER, + CONFIG_SERVER_PORT, + zbx_tcp_strerror()); + + return res; +} + +static int send_data_to_server(zbx_sock_t *sock, const char *data) +{ + int res; + + zabbix_log(LOG_LEVEL_DEBUG, "In send_data_to_server() [%s]", + data); + + if (FAIL == (res = zbx_tcp_send(sock, data))) + zabbix_log(LOG_LEVEL_ERR, "Error while sending data to the server [%s]", + zbx_tcp_strerror()); + + return res; +} + +static int recv_data_from_server(zbx_sock_t *sock, char **data) +{ + int res; + + zabbix_log(LOG_LEVEL_DEBUG, "In recv_data_from_server()"); + if (FAIL == (res = zbx_tcp_recv_ext(sock, data, 0))) + zabbix_log(LOG_LEVEL_ERR, "Error while receiving answer from server [%s]", + zbx_tcp_strerror()); + else + zabbix_log(LOG_LEVEL_DEBUG, "Received [%s] from server", + *data); + + return res; +} + +void disconnect_server(zbx_sock_t *sock) +{ + zbx_tcp_close(sock); +} + +/****************************************************************************** + * * + * Function: get_server_data * + * * + * Purpose: get configuration and othed data from server * + * * + * Parameters: * + * * + * Return value: SUCCESS - processed succesfully * + * FAIL - an error occured * + * * + * Author: Alksander Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +int get_data_from_server(zbx_sock_t *sock, const char *name, const char *request, char **data) +{ + int ret = FAIL; + struct zbx_json j; + + zabbix_log (LOG_LEVEL_DEBUG, "In get_server_data() [name:%s] [request:%s]", + name, + request); + + zbx_json_init(&j, 128); + zbx_json_addstring(&j, "request", request, ZBX_JSON_TYPE_STRING); + zbx_json_addstring(&j, "host", CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); + + if (FAIL == send_data_to_server(sock, j.buffer)) + goto exit; + + if (FAIL == recv_data_from_server(sock, data)) + goto exit; + + zabbix_log (LOG_LEVEL_WARNING, "Received %s from server", + name); + + ret = SUCCEED; +exit: + zbx_json_free(&j); + + return ret; +} + diff --git a/src/zabbix_proxy/nodewatcher/nodecomms.h b/src/zabbix_proxy/proxyconfig/servercomms.h index 88303cf1..3a9b213a 100644 --- a/src/zabbix_proxy/nodewatcher/nodecomms.h +++ b/src/zabbix_proxy/proxyconfig/servercomms.h @@ -17,16 +17,20 @@ ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ -#ifndef ZABBIX_NODECOMMS_H -#define ZABBIX_NODECOMMS_H +#ifndef ZABBIX_SERVERCOMMS_H +#define ZABBIX_SERVERCOMMS_H + +extern char *CONFIG_SERVER; +extern int CONFIG_SERVER_PORT; +extern char *CONFIG_HOSTNAME; #include "comms.h" -int connect_to_node(int nodeid, zbx_sock_t *sock); -int send_data_to_node(int nodeid, zbx_sock_t *sock, const char *data); -int recv_data_from_node(int nodeid, zbx_sock_t *sock, char **data); -void disconnect_node(zbx_sock_t *sock); +int connect_to_server(zbx_sock_t *sock); +/*int send_data_to_server(zbx_sock_t *sock, const char *data); +int recv_data_from_server(zbx_sock_t *sock, char **data);*/ +void disconnect_server(zbx_sock_t *sock); -int send_to_node(const char *name, int dest_nodeid, int nodeid, char *data); +int get_data_from_server(zbx_sock_t *sock, const char *name, const char *request, char **data); #endif diff --git a/src/zabbix_proxy/trapper/Makefile.am b/src/zabbix_proxy/trapper/Makefile.am index dc09025f..9bd56edd 100644 --- a/src/zabbix_proxy/trapper/Makefile.am +++ b/src/zabbix_proxy/trapper/Makefile.am @@ -4,7 +4,4 @@ noinst_LIBRARIES = libzbxtrapper.a libzbxtrapper_a_SOURCES = \ active.c active.h \ - trapper.c trapper.h \ - nodesync.c nodesync.h \ - nodehistory.c nodehistory.h \ - nodecommand.c nodecommand.h + trapper.c trapper.h diff --git a/src/zabbix_proxy/trapper/nodecommand.c b/src/zabbix_proxy/trapper/nodecommand.c deleted file mode 100644 index 7fbc645e..00000000 --- a/src/zabbix_proxy/trapper/nodecommand.c +++ /dev/null @@ -1,284 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 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 <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <netinet/in.h> -#include <netdb.h> - -#include <string.h> - -#include <time.h> - -#include <sys/socket.h> -#include <errno.h> - -#include "comms.h" -#include "common.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#define MVAR_HOST_NAME "{HOSTNAME}" -#define MVAR_IPADDRESS "{IPADDRESS}" -#define MVAR_HOST_CONN "{HOST.CONN}" - - -/****************************************************************************** - * * - * Function: execute_script * - * * - * Purpose: executing command * - * * - * Parameters: * - * * - * Return value: SUCCEED - processed successfully * - * FAIL - an error occured * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void execute_script(const char *command, char **result, int *result_allocated) -{ - int result_offset = 0; - char buffer[MAX_STRING_LEN]; - FILE *f; - - zabbix_log(LOG_LEVEL_DEBUG, "In execute_script(command:%s)", command); - - if(0 != (f = popen(command, "r"))) { - zbx_snprintf_alloc(result, result_allocated, &result_offset, 8, "%d%c", - SUCCEED, - ZBX_DM_DELIMITER); - - while (NULL != fgets(buffer, sizeof(buffer)-1, f)) { - zbx_snprintf_alloc(result, result_allocated, &result_offset, sizeof(buffer), - "%s", - buffer); - } - (*result)[result_offset] = '\0'; - - pclose(f); - } else { - zbx_snprintf_alloc(result, result_allocated, &result_offset, 128, - "%d%cNODE %d: Cannot execute [%s] error:%s", - FAIL, - ZBX_DM_DELIMITER, - CONFIG_NODEID, - command, - strerror(errno)); - } -} - -/****************************************************************************** - * * - * Function: send_script * - * * - * Purpose: sending command to slave node * - * * - * Parameters: * - * * - * Return value: SUCCEED - processed successfully * - * FAIL - an error occured * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -void send_script(int nodeid, const char *data, char **result, int *result_allocated) -{ - DB_RESULT dbresult; - DB_ROW dbrow; - int result_offset = 0; - zbx_sock_t sock; - char *answer; - - zabbix_log(LOG_LEVEL_DEBUG, "In send_script(nodeid:%d)", nodeid); - - dbresult = DBselect("select ip,port from nodes where nodeid=%d", - nodeid); - - if (NULL != (dbrow = DBfetch(dbresult))) { - if (SUCCEED == zbx_tcp_connect(&sock, dbrow[0], atoi(dbrow[1]), 0)) { - if (FAIL == zbx_tcp_send(&sock, data)) { - zbx_snprintf_alloc(result, result_allocated, &result_offset, 128, - "%d%cNODE %d: Error while sending data to Node [%d] error: %s", - FAIL, - ZBX_DM_DELIMITER, - CONFIG_NODEID, - nodeid, - zbx_tcp_strerror()); - goto exit_sock; - } - - if (SUCCEED == zbx_tcp_recv(&sock, &answer/*, ZBX_TCP_READ_UNTIL_CLOSE*/)) { - zbx_snprintf_alloc(result, result_allocated, &result_offset, strlen(answer)+1, - "%s", - answer); - } else { - - zbx_snprintf_alloc(result, result_allocated, &result_offset, 128, - "%d%cNODE %d: Error while receiving answer from Node [%d] error: %s", - FAIL, - ZBX_DM_DELIMITER, - CONFIG_NODEID, - nodeid, - zbx_tcp_strerror()); - goto exit_sock; - } -exit_sock: - zbx_tcp_close(&sock); - } else { - zbx_snprintf_alloc(result, result_allocated, &result_offset, 128, - "%d%cNODE %d: Unable to connect to Node [%d] error: %s", - FAIL, - ZBX_DM_DELIMITER, - CONFIG_NODEID, - nodeid, - zbx_tcp_strerror()); - } - } else { - zbx_snprintf_alloc(result, result_allocated, &result_offset, 128, - "%d%cNODE %d: Node [%d] is unknown", - FAIL, - ZBX_DM_DELIMITER, - CONFIG_NODEID, - nodeid); - } - DBfree_result(dbresult); -} - -/****************************************************************************** - * * - * Function: get_next_point_to_node * - * * - * Purpose: find next point to slave node * - * * - * Parameters: * - * * - * Return value: SUCCEED - processed successfully * - * FAIL - an error occured * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int get_next_point_to_node(int current_nodeid, int slave_nodeid, int *nodeid) -{ - DB_RESULT dbresult; - DB_ROW dbrow; - int id, res = FAIL; - - dbresult = DBselect("select nodeid from nodes where masterid=%d", - current_nodeid); - - while (NULL != (dbrow = DBfetch(dbresult))) { - id = atoi(dbrow[0]); - if (id == slave_nodeid || SUCCEED == get_next_point_to_node(id, slave_nodeid, NULL)) { - if (NULL != nodeid) - *nodeid = id; - res = SUCCEED; - break; - } - } - DBfree_result(dbresult); - - return res; -} - -/****************************************************************************** - * * - * Function: node_process_command * - * * - * Purpose: process command received from a master node or php * - * * - * Parameters: * - * * - * Return value: SUCCEED - processed successfully * - * FAIL - an error occured * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int node_process_command(zbx_sock_t *sock, const char *data) -{ - const char *r; - char *tmp = NULL, *result = NULL; - int tmp_allocated = 64, result_allocated = 1024; - int datalen; - int nodeid, next_nodeid; - int result_offset = 0; - - result = zbx_malloc(result, result_allocated); - tmp = zbx_malloc(tmp, tmp_allocated); - datalen = strlen(data); - - zabbix_log(LOG_LEVEL_DEBUG, "In node_process_command(datalen:%d)", - datalen); - - r = data; - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* Constant 'Command' */ - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* NodeID */ - nodeid = atoi(tmp); - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - - if (nodeid == CONFIG_NODEID) { - zabbix_log(LOG_LEVEL_WARNING, "NODE %d: Received command \"%s\"", - CONFIG_NODEID, - tmp); - - execute_script(tmp, &result, &result_allocated); - } else if (SUCCEED == get_next_point_to_node(CONFIG_NODEID, nodeid, &next_nodeid)) { - zabbix_log(LOG_LEVEL_WARNING, "NODE %d: Sending command \"%s\" for nodeid %d" - "to node %d", - CONFIG_NODEID, - tmp, - nodeid, - next_nodeid); - - send_script(next_nodeid, data, &result, &result_allocated); - } else { - zbx_snprintf_alloc(&result, &result_allocated, &result_offset, 128, - "%d%cNODE %d: Node [%d] is unknown", - FAIL, - ZBX_DM_DELIMITER, - CONFIG_NODEID, - nodeid); - } - - if (zbx_tcp_send_raw(sock, result) != SUCCEED) { - zabbix_log(LOG_LEVEL_WARNING, "NODE %d: Error sending result of command to node %d", - CONFIG_NODEID, - nodeid); - } - zbx_free(tmp); - zbx_free(result); - - return SUCCEED; -} diff --git a/src/zabbix_proxy/trapper/nodecommand.h b/src/zabbix_proxy/trapper/nodecommand.h deleted file mode 100644 index 0c92e05b..00000000 --- a/src/zabbix_proxy/trapper/nodecommand.h +++ /dev/null @@ -1,26 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 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. -**/ - - -#ifndef ZABBIX_NODECOMMAND_H -#define ZABBIX_NODECOMMAND_H - -int node_process_command(zbx_sock_t *sock, const char *data); - -#endif diff --git a/src/zabbix_proxy/trapper/nodehistory.c b/src/zabbix_proxy/trapper/nodehistory.c deleted file mode 100644 index 51fec678..00000000 --- a/src/zabbix_proxy/trapper/nodehistory.c +++ /dev/null @@ -1,481 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 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 <stdio.h> -#include <stdlib.h> - -#include "common.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#include "../events.h" -#include "../nodewatcher/nodecomms.h" - -/****************************************************************************** - * * - * Function: send_history_last_id * - * * - * Purpose: send list of last historical tables ids * - * * - * Parameters: sock - opened socket of node-node connection * - * record * - * * - * Return value: SUCCEED - sent succesfully * - * FAIL - an error occured * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int send_history_last_id(zbx_sock_t *sock, const char *data) -{ - DB_RESULT result; - DB_ROW row; - const char *r; - char *tmp = NULL, tablename[MAX_STRING_LEN], fieldname[MAX_STRING_LEN]; - int tmp_allocated = 256, tmp_offset; - int sender_nodeid, nodeid, res; - - zabbix_log(LOG_LEVEL_DEBUG, "In send_list_of_history_ids()"); - - tmp = zbx_malloc(tmp, tmp_allocated); - - r = data; - if (NULL == r) - goto error; - - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* constant 'ZBX_GET_HISTORY_LAST_ID' */ - if (NULL == r) - goto error; - - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* sender_nodeid */ - sender_nodeid=atoi(tmp); - if (NULL == r) - goto error; - - zbx_get_next_field(&r, &tmp, &tmp_allocated, '\n'); /* nodeid */ - nodeid=atoi(tmp); - if (NULL == r) - goto error; - - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* table name */ - strcpy(tablename, tmp); - - if (NULL == r) - goto error; - - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* field name */ - strcpy(fieldname, tmp); - - tmp_offset= 0; - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 256, "select MAX(%s) " - "from %s where"ZBX_COND_NODEID, - fieldname, - tablename, - ZBX_NODE(fieldname, nodeid)); - - tmp_offset= 0; - result = DBselect("%s", tmp); - if (NULL != (row = DBfetch(result))) - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 128, "%s", - SUCCEED == DBis_null(row[0]) ? "0" : row[0]); - DBfree_result(result); - - if (tmp_offset == 0) - goto error; - - res = send_data_to_node(sender_nodeid, sock, tmp); - - zbx_free(tmp); - - return res; -error: - tmp_offset= 0; - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 128, "FAIL"); - - res = send_data_to_node(sender_nodeid, sock, tmp); - - zbx_free(tmp); - - zabbix_log( LOG_LEVEL_ERR, "NODE %d: Received invalid record from node %d for node %d [%s]", - CONFIG_NODEID, - sender_nodeid, - nodeid, - data); - - return FAIL; -} - -/****************************************************************************** - * * - * Function: send_trends_last_id * - * * - * Purpose: send last historical tables ids * - * * - * Parameters: sock - opened socket of node-node connection * - * record * - * * - * Return value: SUCCEED - sent succesfully * - * FAIL - an error occured * - * * - * Author: Aleksander Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int send_trends_last_id(zbx_sock_t *sock, const char *data) -{ - DB_RESULT result; - DB_ROW row; - const char *r; - char *tmp = NULL, tablename[MAX_STRING_LEN]; - int tmp_allocated = 256, tmp_offset; - int sender_nodeid, nodeid, res; - - zabbix_log(LOG_LEVEL_DEBUG, "In send_list_of_history_ids()"); - - tmp = zbx_malloc(tmp, tmp_allocated); - - r = data; - if (NULL == r) - goto error; - - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* constant 'ZBX_GET_HISTORY_LAST_ID' */ - if (NULL == r) - goto error; - - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* sender_nodeid */ - sender_nodeid=atoi(tmp); - if (NULL == r) - goto error; - - zbx_get_next_field(&r, &tmp, &tmp_allocated, '\n'); /* nodeid */ - nodeid=atoi(tmp); - if (NULL == r) - goto error; - - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* table name */ - strcpy(tablename, tmp); - - tmp_offset= 0; - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 256, "select itemid,clock " - "from %s where"ZBX_COND_NODEID"order by itemid desc,clock desc", - tablename, - ZBX_NODE("itemid", nodeid)); - - tmp_offset= 0; - result = DBselectN(tmp, 1); - if (NULL == (row = DBfetch(result))) - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 64, "0%c0", - ZBX_DM_DELIMITER); - else - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 128, "%s%c%s", - SUCCEED == DBis_null(row[0]) ? "0" : row[0], - ZBX_DM_DELIMITER, - SUCCEED == DBis_null(row[1]) ? "0" : row[1]); - DBfree_result(result); - - res = send_data_to_node(sender_nodeid, sock, tmp); - - zbx_free(tmp); - - return res; -error: - tmp_offset= 0; - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 128, "FAIL"); - - res = send_data_to_node(sender_nodeid, sock, tmp); - - zbx_free(tmp); - - zabbix_log( LOG_LEVEL_ERR, "NODE %d: Received invalid record from node %d for node %d [%s]", - CONFIG_NODEID, - sender_nodeid, - nodeid, - data); - - return FAIL; -} - -/****************************************************************************** - * * - * Function: process_record_event * - * * - * Purpose: process record update * - * * - * Parameters: * - * * - * Return value: SUCCEED - processed successfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int process_record_event(int sender_nodeid, int nodeid, const ZBX_TABLE *table, const char *record, char **tmp, int *tmp_allocated) -{ - const char *r; - int f, len; - DB_EVENT event; - - zabbix_log(LOG_LEVEL_DEBUG, "In process_record_event ()"); - - memset(&event, 0, sizeof(event)); - - r = record; - for (f = 0; table->fields[f].name != 0; f++) { - if (NULL == r) - goto error; - - len = zbx_get_next_field(&r, tmp, tmp_allocated, ZBX_DM_DELIMITER); - - if (0 == strcmp(table->fields[f].name, "eventid")) { - ZBX_STR2UINT64(event.eventid, *tmp); - } else if (0 == strcmp(table->fields[f].name, "source")) { - event.source = atoi(*tmp); - } else if (0 == strcmp(table->fields[f].name, "object")) { - event.object = atoi(*tmp); - } else if (0 == strcmp(table->fields[f].name, "objectid")) { - ZBX_STR2UINT64(event.objectid, *tmp); - } else if (0 == strcmp(table->fields[f].name, "clock")) { - event.clock=atoi(*tmp); - } else if (0 == strcmp(table->fields[f].name, "value")) { - event.value=atoi(*tmp); - } else if (0 == strcmp(table->fields[f].name, "acknowledged")) { - event.acknowledged=atoi(*tmp); - } - } - - return process_event(&event); -error: - zabbix_log( LOG_LEVEL_ERR, "NODE %d: Received invalid record from node %d for node %d [%s]", - CONFIG_NODEID, - sender_nodeid, - nodeid, - record); - - return FAIL; -} - -/****************************************************************************** - * * - * Function: process_record * - * * - * Purpose: process record update * - * * - * Parameters: * - * * - * Return value: SUCCEED - processed successfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int process_record(int sender_nodeid, int nodeid, const ZBX_TABLE *table, const char *record, char **tmp, int *tmp_allocated, char **sql, int *sql_allocated, int lastrecord) -{ - const char *r; - int f, len, sql_offset, lastvalue_type; - int res = FAIL; - char lastvalue[MAX_STRING_LEN], lastclock[MAX_STRING_LEN]; - - zabbix_log(LOG_LEVEL_DEBUG, "In process_record ()"); - - r = record; - - sql_offset = 0; - zbx_snprintf_alloc(sql, sql_allocated, &sql_offset, 128, "insert into %s (", - table->table); - - if (0 != (table->flags & ZBX_HISTORY_SYNC)) - zbx_snprintf_alloc(sql, sql_allocated, &sql_offset, 128, "nodeid,"); - - for (f = 0; table->fields[f].name != 0; f++) { - if (0 != (table->flags & ZBX_HISTORY_SYNC) && 0 == (table->fields[f].flags & ZBX_HISTORY_SYNC)) - continue; - - zbx_snprintf_alloc(sql, sql_allocated, &sql_offset, 128, "%s,", - table->fields[f].name); - } - - sql_offset--; - zbx_snprintf_alloc(sql, sql_allocated, &sql_offset, 64, ") values ("); - - if (0 != (table->flags & ZBX_HISTORY_SYNC)) - zbx_snprintf_alloc(sql, sql_allocated, &sql_offset, 128, "%d,", - nodeid); - - for (f = 0; table->fields[f].name != 0; f++) { - if ((table->flags & ZBX_HISTORY_SYNC) && 0 == (table->fields[f].flags & ZBX_HISTORY_SYNC)) - continue; - - if (NULL == r) - goto error; - - len = zbx_get_next_field(&r, tmp, tmp_allocated, ZBX_DM_DELIMITER); - - if (table->fields[f].type == ZBX_TYPE_INT || - table->fields[f].type == ZBX_TYPE_UINT || - table->fields[f].type == ZBX_TYPE_ID || - table->fields[f].type == ZBX_TYPE_FLOAT) - { - zbx_snprintf_alloc(sql, sql_allocated, &sql_offset, len + 8, "%s,", - *tmp); - } else { /* ZBX_TYPE_CHAR ZBX_TYPE_BLOB ZBX_TYPE_TEXT */ - if (0 == len) - zbx_snprintf_alloc(sql, sql_allocated, &sql_offset, 8, "'',"); - else - zbx_snprintf_alloc(sql, sql_allocated, &sql_offset, len + 8, "0x%s,", - *tmp); - } - - if (lastrecord && 0 != (table->flags & ZBX_HISTORY_SYNC)) { - if (0 == strcmp(table->fields[f].name, "clock")) { - strcpy(lastclock, *tmp); - } else if (0 == strcmp(table->fields[f].name, "value")) { - strcpy(lastvalue, *tmp); - lastvalue_type = table->fields[f].type; - } - } - } - - sql_offset--; - zbx_snprintf_alloc(sql, sql_allocated, &sql_offset, 8, ")"); - - if (DBexecute("%s", *sql) >= ZBX_DB_OK) - res = SUCCEED; - - return res; -error: - zabbix_log( LOG_LEVEL_ERR, "NODE %d: Received invalid record from node %d for node %d [%s]", - CONFIG_NODEID, - sender_nodeid, - nodeid, - record); - - return FAIL; -} - -/****************************************************************************** - * * - * Function: node_history * - * * - * Purpose: process new history received from a salve node * - * * - * Parameters: * - * * - * Return value: SUCCEED - processed successfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int node_history(char *data, size_t datalen) -{ - const char *r; - char *newline = NULL, *tmp = NULL, *sql = NULL; - char *pos; - int tmp_allocated = 4096, sql_allocated = 8192; - int sender_nodeid = 0, nodeid = 0, firstline = 1, events = 0; - const ZBX_TABLE *table_sync = NULL, *table = NULL; - int res = SUCCEED; - - assert(data); - - zabbix_log(LOG_LEVEL_DEBUG, "In node_history()"); - - tmp = zbx_malloc(tmp, tmp_allocated); - sql = zbx_malloc(sql, sql_allocated); - - DBbegin(); - - for (r = data; *r != '\0' && res == SUCCEED;) { - if (NULL != (newline = strchr(r, '\n'))) - *newline = '\0'; - - if (1 == firstline) { - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* constant 'History' */ - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* sender_nodeid */ - sender_nodeid=atoi(tmp); - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* nodeid */ - nodeid=atoi(tmp); - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* tablename */ - - table = DBget_table(tmp); - if (0 == (table->flags & (ZBX_HISTORY | ZBX_HISTORY_SYNC | ZBX_HISTORY_TRENDS))) - table = NULL; - - if (NULL != table && 0 != (table->flags & ZBX_HISTORY_SYNC)) { - table_sync = table; - if (NULL != (pos = strstr(tmp, "_sync"))) { - *pos = '\0'; - table = DBget_table(tmp); - } - } - - if (NULL != table && 0 == strcmp(table->table, "events")) - events = 1; - - if (NULL == table) { - zabbix_log(LOG_LEVEL_WARNING, "NODE %d: Invalid received data: unknown tablename \"%s\"", - CONFIG_NODEID, - tmp); - } - if (NULL != newline) { - zabbix_log(LOG_LEVEL_WARNING, "NODE %d: Received %s from node %d for node %d datalen %zd", - CONFIG_NODEID, - tmp, - sender_nodeid, - nodeid, - datalen); - } - firstline = 0; - } else if (NULL != table) { - if (events) { - res = process_record_event(sender_nodeid, nodeid, table, r, &tmp, &tmp_allocated); - } else { - res = process_record(sender_nodeid, nodeid, table, r, &tmp, &tmp_allocated, &sql, &sql_allocated, NULL == newline); - if (SUCCEED == res && NULL != table_sync && 0 != CONFIG_MASTER_NODEID) - res = process_record(sender_nodeid, nodeid, table_sync, r, &tmp, &tmp_allocated, &sql, &sql_allocated, 0); - } - } - - if (newline != NULL) { - *newline = '\n'; - r = newline + 1; - } else - break; - } - if (res == SUCCEED) - DBcommit(); - else - DBrollback(); - - zbx_free(sql); - zbx_free(tmp); - - return res; -} diff --git a/src/zabbix_proxy/trapper/nodehistory.h b/src/zabbix_proxy/trapper/nodehistory.h deleted file mode 100644 index bfcf3e01..00000000 --- a/src/zabbix_proxy/trapper/nodehistory.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 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. -**/ - - -#ifndef ZABBIX_NODEHISTORY_H -#define ZABBIX_NODEHISTORY_H - -#include "comms.h" - -int send_history_last_id(zbx_sock_t *sock, const char *data); -int send_trends_last_id(zbx_sock_t *sock, const char *data); -int node_history(char *data, size_t datalen); - -#endif diff --git a/src/zabbix_proxy/trapper/nodesync.c b/src/zabbix_proxy/trapper/nodesync.c deleted file mode 100644 index 6535d2a3..00000000 --- a/src/zabbix_proxy/trapper/nodesync.c +++ /dev/null @@ -1,338 +0,0 @@ -/* -** ZABBIX -** Copyright (C) 2000-2005 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 <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <netinet/in.h> -#include <netdb.h> - -#include <string.h> - -#include <time.h> - -#include <sys/socket.h> -#include <errno.h> - -/* Functions: pow(), round() */ -#include <math.h> - -#include "common.h" -#include "db.h" -#include "log.h" -#include "zlog.h" - -#include "nodesync.h" -#include "../nodewatcher/nodesender.h" - -/****************************************************************************** - * * - * Function: process_record * - * * - * Purpose: process record update * - * * - * Parameters: * - * * - * Return value: SUCCEED - processed successfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -static int process_record(int nodeid, const char *record, int sender_nodetype) -{ - char tablename[MAX_STRING_LEN]; - char fieldname[MAX_STRING_LEN]; - zbx_uint64_t recid; - int op, res = SUCCEED; - int valuetype; - char value_esc[MAX_STRING_LEN]; - int i; - char *key=NULL; - DB_RESULT result; - DB_ROW row; - const char *r; - char *buffer = NULL, *tmp = NULL, *fields_update = NULL, *fields = NULL, *values = NULL; - int buffer_allocated = 16*1024; - int tmp_allocated = 16*1024, tmp_offset = 0; - int fields_update_allocated = 16*1024, fields_update_offset = 0; - int fields_allocated = 4*1024, fields_offset = 0; - int values_allocated = 16*1024, values_offset = 0; -#if defined(HAVE_POSTGRESQL) - int len; -#endif /* HAVE_POSTGRESQL */ - - zabbix_log( LOG_LEVEL_DEBUG, "In process_record [%s]", record); - - r = record; - buffer = zbx_malloc(buffer, buffer_allocated); - tmp = zbx_malloc(tmp, tmp_allocated); - - zbx_get_next_field(&r, &buffer, &buffer_allocated, ZBX_DM_DELIMITER); - strcpy(tablename, buffer); - - zbx_get_next_field(&r, &buffer, &buffer_allocated, ZBX_DM_DELIMITER); - ZBX_STR2UINT64(recid, buffer); - - zbx_get_next_field(&r, &buffer, &buffer_allocated, ZBX_DM_DELIMITER); - op = atoi(buffer); - - for(i=0;tables[i].table!=0;i++) - { - if(strcmp(tables[i].table, tablename)==0) - { - key=tables[i].recid; - break; - } - } - - if(key == NULL) - { - zabbix_log( LOG_LEVEL_WARNING, "Cannot find key field for table [%s]", - tablename); - res = FAIL; - goto out; - } - if(op==NODE_CONFIGLOG_OP_DELETE) - { - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 256, "delete from %s where %s="ZBX_FS_UI64, - tablename, - key, - recid); - DBexecute("%s", tmp); - goto out; - } - - fields_update = zbx_malloc(fields_update, fields_update_allocated); - fields = zbx_malloc(fields, fields_allocated); - values = zbx_malloc(values, values_allocated); - - zbx_snprintf_alloc(&fields, &fields_allocated, &fields_offset, 128, "%s,", key); - zbx_snprintf_alloc(&values, &values_allocated, &values_offset, 128, ZBX_FS_UI64",", recid); - - while(r != NULL) - { - zbx_get_next_field(&r, &buffer, &buffer_allocated, ZBX_DM_DELIMITER); - strcpy(fieldname, buffer); - - zbx_get_next_field(&r, &buffer, &buffer_allocated, ZBX_DM_DELIMITER); - valuetype=atoi(buffer); - - zbx_get_next_field(&r, &buffer, &buffer_allocated, ZBX_DM_DELIMITER); - if(op==NODE_CONFIGLOG_OP_UPDATE) - { - if(strcmp(buffer, "NULL") == 0) - { - zbx_snprintf_alloc(&fields_update, &fields_update_allocated, &fields_update_offset, 128, "%s=NULL,", - fieldname); - zbx_snprintf_alloc(&values, &values_allocated, &values_offset, 128, "NULL,"); - } - else - { - if(valuetype == ZBX_TYPE_INT || valuetype == ZBX_TYPE_UINT || valuetype == ZBX_TYPE_ID || valuetype == ZBX_TYPE_FLOAT) - { - zbx_snprintf_alloc(&fields_update, &fields_update_allocated, &fields_update_offset, 256, "%s=%s,", - fieldname, - buffer); - zbx_snprintf_alloc(&values, &values_allocated, &values_offset, 128, "%s,", - buffer); - } - else if(valuetype == ZBX_TYPE_BLOB) - { - if(*buffer == '\0') - { - zbx_snprintf_alloc(&fields_update, &fields_update_allocated, &fields_update_offset, 128, "%s='',", - fieldname); - zbx_snprintf_alloc(&values, &values_allocated, &values_offset, 128, "'',"); - } - else - { -#if defined(HAVE_POSTGRESQL) - len = zbx_hex2binary(buffer); - zbx_pg_escape_bytea((u_char *)buffer, len, &tmp, &tmp_allocated); - zbx_snprintf_alloc(&fields_update, &fields_update_allocated, &fields_update_offset, strlen(tmp)+256, "%s='%s',", - fieldname, - tmp); - zbx_snprintf_alloc(&values, &values_allocated, &values_offset, strlen(tmp)+256, "'%s',", - tmp); -#else - zbx_snprintf_alloc(&fields_update, &fields_update_allocated, &fields_update_offset, strlen(buffer)+256, "%s=0x%s,", - fieldname, - buffer); - zbx_snprintf_alloc(&values, &values_allocated, &values_offset, strlen(buffer)+256, "0x%s,", - buffer); -#endif - } - } - else /* ZBX_TYPE_TEXT, ZBX_TYPE_CHAR */ - { - zbx_hex2binary(buffer); - DBescape_string(buffer, value_esc,MAX_STRING_LEN); - - zbx_snprintf_alloc(&fields_update, &fields_update_allocated, &fields_update_offset, 256, "%s='%s',", - fieldname, - value_esc); - zbx_snprintf_alloc(&values, &values_allocated, &values_offset, 256, "'%s',", - value_esc); - } - } - - zbx_snprintf_alloc(&fields, &fields_allocated, &fields_offset, 128, "%s,", fieldname); - } - else - { - zabbix_log( LOG_LEVEL_WARNING, "Unknown record operation [%d]", - op); - res = FAIL; - goto out; - } - } - if(fields_offset != 0) fields[fields_offset - 1]='\0'; - if(fields_update_offset != 0) fields_update[fields_update_offset - 1]='\0'; - if(values_offset != 0) values[values_offset - 1]='\0'; - - if(op==NODE_CONFIGLOG_OP_UPDATE) - { - result = DBselect("select 0 from %s where %s="ZBX_FS_UI64, - tablename, - key, - recid); - row = DBfetch(result); - if(row) - { - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 16*1024, "update %s set %s where %s=" ZBX_FS_UI64, - tablename, - fields_update, - key, - recid); - } - else - { - zbx_snprintf_alloc(&tmp, &tmp_allocated, &tmp_offset, 16*1024, "insert into %s (%s) values(%s)", - tablename, - fields, - values); - } - DBfree_result(result); - } - DBexecute("%s",tmp); - - if (FAIL == calculate_checksums(nodeid, tablename, recid) || - FAIL == update_checksums(nodeid, sender_nodetype, SUCCEED, tablename, recid, fields) ) { - res = FAIL; - goto out; - } -/* zabbix_log( LOG_LEVEL_CRIT, "RECORD [%s]", record);*/ -/* zabbix_log( LOG_LEVEL_CRIT, "SQL [%s] %s", tmp, res == FAIL ? "FAIL" : "SUCCEED");*/ - -out: - if (NULL != buffer) - zbx_free(buffer); - if (NULL != tmp) - zbx_free(tmp); - if (NULL != fields_update) - zbx_free(fields_update); - if (NULL != fields) - zbx_free(fields); - if (NULL != values) - zbx_free(values); - - return res; -} -/****************************************************************************** - * * - * Function: node_sync * - * * - * Purpose: process configuration changes received from a node * - * * - * Parameters: * - * * - * Return value: SUCCEED - processed successfully * - * FAIL - an error occured * - * * - * Author: Alexei Vladishev * - * * - * Comments: * - * * - ******************************************************************************/ -int node_sync(char *data, int *sender_nodeid, int *nodeid) -{ - const char *r; - char *newline, *tmp = NULL; - int tmp_allocated = 128; - int firstline=1; - int sender_nodetype=0; - int datalen; - int res = SUCCEED; - - datalen=strlen(data); - - zabbix_log( LOG_LEVEL_DEBUG, "In node_sync(len:%d)", datalen); - - tmp = zbx_malloc(tmp, tmp_allocated); - -/*zabbix_log(LOG_LEVEL_CRIT, "<----- [%s]", data);*/ - - for (r = data; *r != '\0' && res == SUCCEED;) { - if (NULL != (newline = strchr(r, '\n'))) - *newline = '\0'; - - if (firstline == 1) { - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); /* Data */ - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - *sender_nodeid=atoi(tmp); - sender_nodetype = *sender_nodeid == CONFIG_MASTER_NODEID ? ZBX_NODE_MASTER : ZBX_NODE_SLAVE; - zbx_get_next_field(&r, &tmp, &tmp_allocated, ZBX_DM_DELIMITER); - *nodeid=atoi(tmp); - - if (0 != *sender_nodeid && 0 != *nodeid) { - zabbix_log( LOG_LEVEL_WARNING, "NODE %d: Received data from %s node %d for node %d datalen %d", - CONFIG_NODEID, - sender_nodetype == ZBX_NODE_SLAVE ? "slave" : "master", - *sender_nodeid, - *nodeid, - datalen); - -/* DBbegin();*/ - - DBexecute("delete from node_cksum where nodeid=%d and cksumtype=%d", - *nodeid, - NODE_CKSUM_TYPE_NEW); - - firstline=0; - } else - res = FAIL; - } else - res = process_record(*nodeid, r, sender_nodetype); - - if (newline != NULL) { - *newline = '\n'; - r = newline + 1; - } else - break; - } - zbx_free(tmp); - - return res; -} diff --git a/src/zabbix_proxy/trapper/trapper.c b/src/zabbix_proxy/trapper/trapper.c index d6ee142b..65a5fb02 100644 --- a/src/zabbix_proxy/trapper/trapper.c +++ b/src/zabbix_proxy/trapper/trapper.c @@ -29,30 +29,23 @@ #include "../functions.h" #include "../expression.h" -#include "../nodewatcher/nodecomms.h" -#include "../nodewatcher/nodesender.h" -#include "nodesync.h" -#include "nodehistory.h" #include "trapper.h" #include "active.h" -#include "nodecommand.h" #include "daemon.h" static int process_trap(zbx_sock_t *sock,char *s, int max_len) { char *line,*host; - char *server,*key,*value_string, *data; + char *server,*key,*value_string; char copy[MAX_STRING_LEN]; char host_dec[MAX_STRING_LEN],key_dec[MAX_STRING_LEN],value_dec[MAX_STRING_LEN]; char lastlogsize[MAX_STRING_LEN]; char timestamp[MAX_STRING_LEN]; char source[MAX_STRING_LEN]; char severity[MAX_STRING_LEN]; - int sender_nodeid, nodeid; - char *answer; - int ret=SUCCEED, res; + int ret=SUCCEED; size_t datalen; zbx_rtrim(s, " \r\n\0"); @@ -73,60 +66,10 @@ static int process_trap(zbx_sock_t *sock,char *s, int max_len) { ret = send_list_of_active_checks(sock, host); } -/* Request for last ids */ - } else if (strncmp(s,"ZBX_GET_HISTORY_LAST_ID", 23) == 0) { - send_history_last_id(sock, s); - return ret; - } else if (strncmp(s,"ZBX_GET_TRENDS_LAST_ID", 22) == 0) { - send_trends_last_id(sock, s); - return ret; /* Process information sent by zabbix_sender */ } else { - /* Command? */ - if(strncmp(s,"Command",7) == 0) - { - node_process_command(sock, s); - return ret; - } - /* Node data exchange? */ - if(strncmp(s,"Data",4) == 0) - { - node_sync_lock(0); - -/* zabbix_log( LOG_LEVEL_WARNING, "Node data received [len:%d]", strlen(s)); */ - res = node_sync(s, &sender_nodeid, &nodeid); - if (FAIL == res) - send_data_to_node(sender_nodeid, sock, "FAIL"); - else { - res = calculate_checksums(nodeid, NULL, 0); - if (SUCCEED == res && NULL != (data = get_config_data(nodeid, ZBX_NODE_SLAVE))) { - res = send_data_to_node(sender_nodeid, sock, data); - zbx_free(data); - if (SUCCEED == res) - res = recv_data_from_node(sender_nodeid, sock, &answer); - if (SUCCEED == res && 0 == strcmp(answer, "OK")) - res = update_checksums(nodeid, ZBX_NODE_SLAVE, SUCCEED, NULL, 0, NULL); - } - } - - node_sync_unlock(0); - - return ret; - } - /* Slave node history ? */ - if(strncmp(s,"History",7) == 0) - { -/* zabbix_log( LOG_LEVEL_WARNING, "Slave node history received [len:%d]", strlen(s)); */ - if (node_history(s, datalen) == SUCCEED) { - if (zbx_tcp_send_raw(sock,"OK") != SUCCEED) { - zabbix_log( LOG_LEVEL_WARNING, "Error sending confirmation to node"); - zabbix_syslog("Trapper: error sending confirmation to node"); - } - } - return ret; - } /* New XML protocol? */ - else if(s[0]=='<') + if(s[0]=='<') { zabbix_log( LOG_LEVEL_DEBUG, "XML received [%s]", s); |
