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 "daemon.h"
24 #include "proxy.h"
25 #include "zbxself.h"
26 
27 #include "proxyconfig.h"
28 #include "../servercomms.h"
29 #include "../../libs/zbxcrypto/tls.h"
30 
31 #define CONFIG_PROXYCONFIG_RETRY	120	/* seconds */
32 
33 extern unsigned char	process_type, program_type;
34 extern int		server_num, process_num;
35 
zbx_proxyconfig_sigusr_handler(int flags)36 static void	zbx_proxyconfig_sigusr_handler(int flags)
37 {
38 	if (ZBX_RTC_CONFIG_CACHE_RELOAD == ZBX_RTC_GET_MSG(flags))
39 	{
40 		if (0 < zbx_sleep_get_remainder())
41 		{
42 			zabbix_log(LOG_LEVEL_WARNING, "forced reloading of the configuration cache");
43 			zbx_wakeup();
44 		}
45 		else
46 			zabbix_log(LOG_LEVEL_WARNING, "configuration cache reloading is already in progress");
47 	}
48 }
49 
50 /******************************************************************************
51  *                                                                            *
52  * Function: process_configuration_sync                                       *
53  *                                                                            *
54  ******************************************************************************/
process_configuration_sync(size_t * data_size)55 static void	process_configuration_sync(size_t *data_size)
56 {
57 	const char	*__function_name = "process_configuration_sync";
58 
59 	zbx_socket_t	sock;
60 	struct		zbx_json_parse jp;
61 	char		value[16], *error = NULL;
62 
63 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
64 
65 	/* reset the performance metric */
66 	*data_size = 0;
67 
68 	if (FAIL == connect_to_server(&sock, 600, CONFIG_PROXYCONFIG_RETRY))	/* retry till have a connection */
69 		goto out;
70 
71 	if (SUCCEED != get_data_from_server(&sock, ZBX_PROTO_VALUE_PROXY_CONFIG, &error))
72 	{
73 		zabbix_log(LOG_LEVEL_WARNING, "cannot obtain configuration data from server at \"%s\": %s",
74 				sock.peer, error);
75 		goto error;
76 	}
77 
78 	if ('\0' == *sock.buffer)
79 	{
80 		zabbix_log(LOG_LEVEL_WARNING, "cannot obtain configuration data from server at \"%s\": %s",
81 				sock.peer, "empty string received");
82 		goto error;
83 	}
84 
85 	if (SUCCEED != zbx_json_open(sock.buffer, &jp))
86 	{
87 		zabbix_log(LOG_LEVEL_WARNING, "cannot obtain configuration data from server at \"%s\": %s",
88 				sock.peer, zbx_json_strerror());
89 		goto error;
90 	}
91 
92 	*data_size = (size_t)(jp.end - jp.start + 1);     /* performance metric */
93 
94 	/* if the answer is short then most likely it is a negative answer "response":"failed" */
95 	if (128 > *data_size &&
96 			SUCCEED == zbx_json_value_by_name(&jp, ZBX_PROTO_TAG_RESPONSE, value, sizeof(value), NULL) &&
97 			0 == strcmp(value, ZBX_PROTO_VALUE_FAILED))
98 	{
99 		char	*info = NULL;
100 		size_t	info_alloc = 0;
101 
102 		if (SUCCEED != zbx_json_value_by_name_dyn(&jp, ZBX_PROTO_TAG_INFO, &info, &info_alloc, NULL))
103 			info = zbx_dsprintf(info, "negative response \"%s\"", value);
104 
105 		zabbix_log(LOG_LEVEL_WARNING, "cannot obtain configuration data from server at \"%s\": %s",
106 				sock.peer, info);
107 		zbx_free(info);
108 		goto error;
109 	}
110 
111 	zabbix_log(LOG_LEVEL_WARNING, "received configuration data from server at \"%s\", datalen " ZBX_FS_SIZE_T,
112 			sock.peer, (zbx_fs_size_t)*data_size);
113 
114 	process_proxyconfig(&jp);
115 error:
116 	disconnect_server(&sock);
117 
118 	zbx_free(error);
119 out:
120 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
121 }
122 
123 /******************************************************************************
124  *                                                                            *
125  * Function: main_proxyconfig_loop                                            *
126  *                                                                            *
127  * Purpose: periodically request config data                                  *
128  *                                                                            *
129  * Parameters:                                                                *
130  *                                                                            *
131  * Return value:                                                              *
132  *                                                                            *
133  * Author: Alexander Vladishev                                                *
134  *                                                                            *
135  * Comments: never returns                                                    *
136  *                                                                            *
137  ******************************************************************************/
ZBX_THREAD_ENTRY(proxyconfig_thread,args)138 ZBX_THREAD_ENTRY(proxyconfig_thread, args)
139 {
140 	size_t	data_size;
141 	double	sec;
142 
143 	process_type = ((zbx_thread_args_t *)args)->process_type;
144 	server_num = ((zbx_thread_args_t *)args)->server_num;
145 	process_num = ((zbx_thread_args_t *)args)->process_num;
146 
147 	zabbix_log(LOG_LEVEL_INFORMATION, "%s #%d started [%s #%d]", get_program_type_string(program_type),
148 			server_num, get_process_type_string(process_type), process_num);
149 	update_selfmon_counter(ZBX_PROCESS_STATE_BUSY);
150 	zbx_set_sigusr_handler(zbx_proxyconfig_sigusr_handler);
151 #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
152 	zbx_tls_init_child();
153 #endif
154 	zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type));
155 
156 	DBconnect(ZBX_DB_CONNECT_NORMAL);
157 
158 	zbx_setproctitle("%s [syncing configuration]", get_process_type_string(process_type));
159 	DCsync_configuration(ZBX_DBSYNC_INIT);
160 
161 	while (ZBX_IS_RUNNING())
162 	{
163 		sec = zbx_time();
164 		zbx_update_env(sec);
165 
166 		zbx_setproctitle("%s [loading configuration]", get_process_type_string(process_type));
167 
168 		process_configuration_sync(&data_size);
169 		sec = zbx_time() - sec;
170 
171 		zbx_setproctitle("%s [synced config " ZBX_FS_SIZE_T " bytes in " ZBX_FS_DBL " sec, idle %d sec]",
172 				get_process_type_string(process_type), (zbx_fs_size_t)data_size, sec,
173 				CONFIG_PROXYCONFIG_FREQUENCY);
174 
175 		zbx_sleep_loop(CONFIG_PROXYCONFIG_FREQUENCY);
176 	}
177 
178 	zbx_setproctitle("%s #%d [terminated]", get_process_type_string(process_type), process_num);
179 
180 	while (1)
181 		zbx_sleep(SEC_PER_MIN);
182 }
183