1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #include "common.h"
21 #include "db.h"
22 #include "log.h"
23 #include "proxy.h"
24 #include "dbcache.h"
25 
26 #include "proxyhosts.h"
27 
28 /******************************************************************************
29  *                                                                            *
30  * Function: recv_host_availability                                           *
31  *                                                                            *
32  * Purpose: update hosts availability, monitored by proxies                   *
33  *                                                                            *
34  * Author: Alexander Vladishev                                                *
35  *                                                                            *
36  ******************************************************************************/
recv_host_availability(zbx_socket_t * sock,struct zbx_json_parse * jp)37 void	recv_host_availability(zbx_socket_t *sock, struct zbx_json_parse *jp)
38 {
39 	const char	*__function_name = "recv_host_availability";
40 
41 	zbx_uint64_t	proxy_hostid;
42 	char		host[HOST_HOST_LEN_MAX], *error = NULL;
43 	int		ret;
44 
45 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
46 
47 	if (SUCCEED != (ret = get_active_proxy_id(jp, &proxy_hostid, host, sock, &error)))
48 	{
49 		zabbix_log(LOG_LEVEL_WARNING, "cannot parse host availability data from active proxy at \"%s\": %s",
50 				sock->peer, error);
51 		goto out;
52 	}
53 
54 	process_host_availability(jp);
55 out:
56 	zbx_send_response(sock, ret, error, CONFIG_TIMEOUT);
57 
58 	zbx_free(error);
59 
60 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
61 }
62 
63 /******************************************************************************
64  *                                                                            *
65  * Function: send_host_availability                                           *
66  *                                                                            *
67  * Purpose: send hosts availability data from proxy                           *
68  *                                                                            *
69  ******************************************************************************/
send_host_availability(zbx_socket_t * sock)70 void	send_host_availability(zbx_socket_t *sock)
71 {
72 	const char	*__function_name = "send_host_availability";
73 
74 	struct zbx_json	j;
75 	int		ret = FAIL, ts;
76 	char		*error = NULL;
77 
78 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
79 
80 	if (SUCCEED != check_access_passive_proxy(sock, ZBX_DO_NOT_SEND_RESPONSE, "host availability data request"))
81 	{
82 		/* do not send any reply to server in this case as the server expects host availability data */
83 		goto out1;
84 	}
85 
86 	zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN);
87 
88 	/* if there are no host availability changes we still have to send empty data in response */
89 	if (SUCCEED != get_host_availability_data(&j, &ts))
90 	{
91 		zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA);
92 		zbx_json_close(&j);
93 	}
94 
95 	zabbix_log(LOG_LEVEL_DEBUG, "%s() [%s]", __function_name, j.buffer);
96 
97 	if (SUCCEED != zbx_tcp_send_to(sock, j.buffer, CONFIG_TIMEOUT))
98 	{
99 		error = zbx_strdup(error, zbx_socket_strerror());
100 		goto out;
101 	}
102 
103 	if (SUCCEED != zbx_recv_response(sock, CONFIG_TIMEOUT, &error))
104 		goto out;
105 
106 	zbx_set_availability_diff_ts(ts);
107 
108 	ret = SUCCEED;
109 out:
110 	if (SUCCEED != ret)
111 	{
112 		zabbix_log(LOG_LEVEL_WARNING, "cannot send host availability data to server at \"%s\": %s",
113 				sock->peer, error);
114 	}
115 
116 	zbx_json_free(&j);
117 	zbx_free(error);
118 out1:
119 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
120 }
121