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 
22 #include "db.h"
23 #include "daemon.h"
24 #include "zbxself.h"
25 #include "log.h"
26 #include "dbconfig.h"
27 #include "dbcache.h"
28 
29 extern int		CONFIG_CONFSYNCER_FREQUENCY;
30 extern unsigned char	process_type, program_type;
31 extern int		server_num, process_num;
32 
33 static volatile sig_atomic_t	secrets_reload;
34 
zbx_dbconfig_sigusr_handler(int flags)35 static void	zbx_dbconfig_sigusr_handler(int flags)
36 {
37 	int	msg;
38 
39 	/* it is assumed that only one signal is used at a time, any subsequent signals are ignored */
40 	if (ZBX_RTC_CONFIG_CACHE_RELOAD == (msg = ZBX_RTC_GET_MSG(flags)))
41 	{
42 		if (0 < zbx_sleep_get_remainder())
43 		{
44 			zabbix_log(LOG_LEVEL_WARNING, "forced reloading of the configuration cache");
45 			zbx_wakeup();
46 		}
47 		else
48 			zabbix_log(LOG_LEVEL_WARNING, "configuration cache reloading is already in progress");
49 	}
50 	else if (ZBX_RTC_SECRETS_RELOAD == msg)
51 	{
52 		if (0 < zbx_sleep_get_remainder())
53 		{
54 			secrets_reload = 1;
55 			zabbix_log(LOG_LEVEL_WARNING, "forced reloading of the secrets");
56 			zbx_wakeup();
57 		}
58 		else
59 			zabbix_log(LOG_LEVEL_WARNING, "configuration cache reloading is already in progress");
60 	}
61 }
62 
63 /******************************************************************************
64  *                                                                            *
65  * Function: main_dbconfig_loop                                               *
66  *                                                                            *
67  * Purpose: periodically synchronises database data with memory cache         *
68  *                                                                            *
69  * Parameters:                                                                *
70  *                                                                            *
71  * Return value:                                                              *
72  *                                                                            *
73  * Author: Alexander Vladishev                                                *
74  *                                                                            *
75  * Comments: never returns                                                    *
76  *                                                                            *
77  ******************************************************************************/
ZBX_THREAD_ENTRY(dbconfig_thread,args)78 ZBX_THREAD_ENTRY(dbconfig_thread, args)
79 {
80 	double	sec = 0.0;
81 	int	nextcheck = 0;
82 
83 	process_type = ((zbx_thread_args_t *)args)->process_type;
84 	server_num = ((zbx_thread_args_t *)args)->server_num;
85 	process_num = ((zbx_thread_args_t *)args)->process_num;
86 
87 	zabbix_log(LOG_LEVEL_INFORMATION, "%s #%d started [%s #%d]", get_program_type_string(program_type),
88 			server_num, get_process_type_string(process_type), process_num);
89 
90 	update_selfmon_counter(ZBX_PROCESS_STATE_BUSY);
91 
92 	zbx_set_sigusr_handler(zbx_dbconfig_sigusr_handler);
93 
94 	zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type));
95 
96 	DBconnect(ZBX_DB_CONNECT_NORMAL);
97 
98 	sec = zbx_time();
99 	zbx_setproctitle("%s [syncing configuration]", get_process_type_string(process_type));
100 	DCsync_configuration(ZBX_DBSYNC_INIT, NULL);
101 	zbx_setproctitle("%s [synced configuration in " ZBX_FS_DBL " sec, idle %d sec]",
102 			get_process_type_string(process_type), (sec = zbx_time() - sec), CONFIG_CONFSYNCER_FREQUENCY);
103 	zbx_sleep_loop(CONFIG_CONFSYNCER_FREQUENCY);
104 
105 	while (ZBX_IS_RUNNING())
106 	{
107 		zbx_setproctitle("%s [synced configuration in " ZBX_FS_DBL " sec, syncing configuration]",
108 				get_process_type_string(process_type), sec);
109 
110 		sec = zbx_time();
111 		zbx_update_env(sec);
112 
113 		if (1 == secrets_reload)
114 		{
115 			DCsync_configuration(ZBX_SYNC_SECRETS, NULL);
116 			secrets_reload = 0;
117 		}
118 		else
119 		{
120 			DCsync_configuration(ZBX_DBSYNC_UPDATE, NULL);
121 			DCupdate_interfaces_availability();
122 			nextcheck = time(NULL) + CONFIG_CONFSYNCER_FREQUENCY;
123 		}
124 
125 		sec = zbx_time() - sec;
126 
127 		zbx_setproctitle("%s [synced configuration in " ZBX_FS_DBL " sec, idle %d sec]",
128 				get_process_type_string(process_type), sec, CONFIG_CONFSYNCER_FREQUENCY);
129 
130 		zbx_sleep_loop(nextcheck - time(NULL));
131 	}
132 
133 	zbx_setproctitle("%s #%d [terminated]", get_process_type_string(process_type), process_num);
134 
135 	while (1)
136 		zbx_sleep(SEC_PER_MIN);
137 }
138