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 
zbx_dbconfig_sigusr_handler(int flags)33 static void	zbx_dbconfig_sigusr_handler(int flags)
34 {
35 	if (ZBX_RTC_CONFIG_CACHE_RELOAD == ZBX_RTC_GET_MSG(flags))
36 	{
37 		if (0 < zbx_sleep_get_remainder())
38 		{
39 			zabbix_log(LOG_LEVEL_WARNING, "forced reloading of the configuration cache");
40 			zbx_wakeup();
41 		}
42 		else
43 			zabbix_log(LOG_LEVEL_WARNING, "configuration cache reloading is already in progress");
44 	}
45 }
46 
47 /******************************************************************************
48  *                                                                            *
49  * Function: main_dbconfig_loop                                               *
50  *                                                                            *
51  * Purpose: periodically synchronises database data with memory cache         *
52  *                                                                            *
53  * Parameters:                                                                *
54  *                                                                            *
55  * Return value:                                                              *
56  *                                                                            *
57  * Author: Alexander Vladishev                                                *
58  *                                                                            *
59  * Comments: never returns                                                    *
60  *                                                                            *
61  ******************************************************************************/
ZBX_THREAD_ENTRY(dbconfig_thread,args)62 ZBX_THREAD_ENTRY(dbconfig_thread, args)
63 {
64 	double	sec = 0.0;
65 
66 	process_type = ((zbx_thread_args_t *)args)->process_type;
67 	server_num = ((zbx_thread_args_t *)args)->server_num;
68 	process_num = ((zbx_thread_args_t *)args)->process_num;
69 
70 	zabbix_log(LOG_LEVEL_INFORMATION, "%s #%d started [%s #%d]", get_program_type_string(program_type),
71 			server_num, get_process_type_string(process_type), process_num);
72 
73 	update_selfmon_counter(ZBX_PROCESS_STATE_BUSY);
74 
75 	zbx_set_sigusr_handler(zbx_dbconfig_sigusr_handler);
76 
77 	zbx_setproctitle("%s [connecting to the database]", get_process_type_string(process_type));
78 
79 	DBconnect(ZBX_DB_CONNECT_NORMAL);
80 
81 	sec = zbx_time();
82 	zbx_setproctitle("%s [syncing configuration]", get_process_type_string(process_type));
83 	DCsync_configuration(ZBX_DBSYNC_INIT);
84 	zbx_setproctitle("%s [synced configuration in " ZBX_FS_DBL " sec, idle %d sec]",
85 			get_process_type_string(process_type), (sec = zbx_time() - sec), CONFIG_CONFSYNCER_FREQUENCY);
86 	zbx_sleep_loop(CONFIG_CONFSYNCER_FREQUENCY);
87 
88 	while (ZBX_IS_RUNNING())
89 	{
90 		zbx_setproctitle("%s [synced configuration in " ZBX_FS_DBL " sec, syncing configuration]",
91 				get_process_type_string(process_type), sec);
92 
93 		sec = zbx_time();
94 		zbx_update_env(sec);
95 
96 		DCsync_configuration(ZBX_DBSYNC_UPDATE);
97 		DCupdate_hosts_availability();
98 		sec = zbx_time() - sec;
99 
100 		zbx_setproctitle("%s [synced configuration in " ZBX_FS_DBL " sec, idle %d sec]",
101 				get_process_type_string(process_type), sec, CONFIG_CONFSYNCER_FREQUENCY);
102 
103 		zbx_sleep_loop(CONFIG_CONFSYNCER_FREQUENCY);
104 	}
105 
106 	zbx_setproctitle("%s #%d [terminated]", get_process_type_string(process_type), process_num);
107 
108 	while (1)
109 		zbx_sleep(SEC_PER_MIN);
110 }
111