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 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 connect_to_server(&sock, 600, CONFIG_PROXYCONFIG_RETRY); /* retry till have a connection */
69
70 if (SUCCEED != get_data_from_server(&sock, ZBX_PROTO_VALUE_PROXY_CONFIG, &error))
71 {
72 zabbix_log(LOG_LEVEL_WARNING, "cannot obtain configuration data from server at \"%s\": %s",
73 sock.peer, error);
74 goto out;
75 }
76
77 if ('\0' == *sock.buffer)
78 {
79 zabbix_log(LOG_LEVEL_WARNING, "cannot obtain configuration data from server at \"%s\": %s",
80 sock.peer, "empty string received");
81 goto out;
82 }
83
84 if (SUCCEED != zbx_json_open(sock.buffer, &jp))
85 {
86 zabbix_log(LOG_LEVEL_WARNING, "cannot obtain configuration data from server at \"%s\": %s",
87 sock.peer, zbx_json_strerror());
88 goto out;
89 }
90
91 *data_size = (size_t)(jp.end - jp.start + 1); /* performance metric */
92
93 /* if the answer is short then most likely it is a negative answer "response":"failed" */
94 if (128 > *data_size &&
95 SUCCEED == zbx_json_value_by_name(&jp, ZBX_PROTO_TAG_RESPONSE, value, sizeof(value)) &&
96 0 == strcmp(value, ZBX_PROTO_VALUE_FAILED))
97 {
98 char *info = NULL;
99 size_t info_alloc = 0;
100
101 if (SUCCEED != zbx_json_value_by_name_dyn(&jp, ZBX_PROTO_TAG_INFO, &info, &info_alloc))
102 info = zbx_dsprintf(info, "negative response \"%s\"", value);
103
104 zabbix_log(LOG_LEVEL_WARNING, "cannot obtain configuration data from server at \"%s\": %s",
105 sock.peer, info);
106 zbx_free(info);
107 goto out;
108 }
109
110 zabbix_log(LOG_LEVEL_WARNING, "received configuration data from server at \"%s\", datalen " ZBX_FS_SIZE_T,
111 sock.peer, (zbx_fs_size_t)*data_size);
112
113 process_proxyconfig(&jp);
114 out:
115 disconnect_server(&sock);
116
117 zbx_free(error);
118
119 zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
120 }
121
122 /******************************************************************************
123 * *
124 * Function: main_proxyconfig_loop *
125 * *
126 * Purpose: periodically request config data *
127 * *
128 * Parameters: *
129 * *
130 * Return value: *
131 * *
132 * Author: Alexander Vladishev *
133 * *
134 * Comments: never returns *
135 * *
136 ******************************************************************************/
ZBX_THREAD_ENTRY(proxyconfig_thread,args)137 ZBX_THREAD_ENTRY(proxyconfig_thread, args)
138 {
139 size_t data_size;
140 double sec;
141
142 process_type = ((zbx_thread_args_t *)args)->process_type;
143 server_num = ((zbx_thread_args_t *)args)->server_num;
144 process_num = ((zbx_thread_args_t *)args)->process_num;
145
146 zabbix_log(LOG_LEVEL_INFORMATION, "%s #%d started [%s #%d]", get_program_type_string(program_type),
147 server_num, get_process_type_string(process_type), process_num);
148
149 #if defined(HAVE_POLARSSL) || defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
150 zbx_tls_init_child();
151 #endif
152 zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type));
153
154 DBconnect(ZBX_DB_CONNECT_NORMAL);
155
156 zbx_set_sigusr_handler(zbx_proxyconfig_sigusr_handler);
157
158 for (;;)
159 {
160 zbx_handle_log();
161
162 zbx_setproctitle("%s [loading configuration]", get_process_type_string(process_type));
163
164 sec = zbx_time();
165 process_configuration_sync(&data_size);
166 sec = zbx_time() - sec;
167
168 zbx_setproctitle("%s [synced config " ZBX_FS_SIZE_T " bytes in " ZBX_FS_DBL " sec, idle %d sec]",
169 get_process_type_string(process_type), (zbx_fs_size_t)data_size, sec,
170 CONFIG_PROXYCONFIG_FREQUENCY);
171
172 zbx_sleep_loop(CONFIG_PROXYCONFIG_FREQUENCY);
173
174 #if !defined(_WINDOWS) && defined(HAVE_RESOLV_H)
175 zbx_update_resolver_conf(); /* handle /etc/resolv.conf update */
176 #endif
177 }
178 }
179