1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/*
** 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, CONFIG_SOURCE_IP, 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;
}
|