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 "log.h"
24 #include "daemon.h"
25 #include "zbxself.h"
26 
27 #include "dbcache.h"
28 #include "dbsyncer.h"
29 #include "export.h"
30 
31 extern int		CONFIG_HISTSYNCER_FREQUENCY;
32 extern unsigned char	process_type, program_type;
33 extern int		server_num, process_num;
34 static sigset_t		orig_mask;
35 
36 /******************************************************************************
37  *                                                                            *
38  * Function: block_signals                                                    *
39  *                                                                            *
40  * Purpose: block signals to avoid interruption                               *
41  *                                                                            *
42  ******************************************************************************/
block_signals(void)43 static	void	block_signals(void)
44 {
45 	sigset_t	mask;
46 
47 	sigemptyset(&mask);
48 	sigaddset(&mask, SIGUSR1);
49 	sigaddset(&mask, SIGUSR2);
50 	sigaddset(&mask, SIGTERM);
51 	sigaddset(&mask, SIGINT);
52 	sigaddset(&mask, SIGQUIT);
53 
54 	if (0 > sigprocmask(SIG_BLOCK, &mask, &orig_mask))
55 		zabbix_log(LOG_LEVEL_WARNING, "cannot set sigprocmask to block the signal");
56 }
57 
58 /******************************************************************************
59  *                                                                            *
60  * Function: unblock_signals                                                  *
61  *                                                                            *
62  * Purpose: unblock signals after blocking                                    *
63  *                                                                            *
64  ******************************************************************************/
unblock_signals(void)65 static	void	unblock_signals(void)
66 {
67 	if (0 > sigprocmask(SIG_SETMASK, &orig_mask, NULL))
68 		zabbix_log(LOG_LEVEL_WARNING,"cannot restore sigprocmask");
69 }
70 
71 /******************************************************************************
72  *                                                                            *
73  * Function: main_dbsyncer_loop                                               *
74  *                                                                            *
75  * Purpose: periodically synchronises data in memory cache with database      *
76  *                                                                            *
77  * Author: Alexei Vladishev                                                   *
78  *                                                                            *
79  * Comments: never returns                                                    *
80  *                                                                            *
81  ******************************************************************************/
ZBX_THREAD_ENTRY(dbsyncer_thread,args)82 ZBX_THREAD_ENTRY(dbsyncer_thread, args)
83 {
84 	int		sleeptime = -1, total_values_num = 0, values_num, more, total_triggers_num = 0, triggers_num;
85 	double		sec, total_sec = 0.0;
86 	time_t		last_stat_time;
87 	char		*stats = NULL;
88 	const char	*process_name;
89 	size_t		stats_alloc = 0, stats_offset = 0;
90 
91 	process_type = ((zbx_thread_args_t *)args)->process_type;
92 	server_num = ((zbx_thread_args_t *)args)->server_num;
93 	process_num = ((zbx_thread_args_t *)args)->process_num;
94 
95 	zabbix_log(LOG_LEVEL_INFORMATION, "%s #%d started [%s #%d]", get_program_type_string(program_type), server_num,
96 			(process_name = get_process_type_string(process_type)), process_num);
97 
98 	update_selfmon_counter(ZBX_PROCESS_STATE_BUSY);
99 
100 #define STAT_INTERVAL	5	/* if a process is busy and does not sleep then update status not faster than */
101 				/* once in STAT_INTERVAL seconds */
102 
103 	zbx_setproctitle("%s #%d [connecting to the database]", process_name, process_num);
104 	last_stat_time = time(NULL);
105 
106 	zbx_strcpy_alloc(&stats, &stats_alloc, &stats_offset, "started");
107 
108 	/* database APIs might not handle signals correctly and hang, block signals to avoid hanging */
109 	block_signals();
110 	DBconnect(ZBX_DB_CONNECT_NORMAL);
111 	unblock_signals();
112 
113 	if (SUCCEED == zbx_is_export_enabled())
114 	{
115 		zbx_history_export_init("history-syncer", process_num);
116 		zbx_problems_export_init("history-syncer", process_num);
117 	}
118 
119 	for (;;)
120 	{
121 		sec = zbx_time();
122 		zbx_update_env(sec);
123 
124 		if (0 != sleeptime)
125 			zbx_setproctitle("%s #%d [%s, syncing history]", process_name, process_num, stats);
126 
127 		/* clear timer trigger queue to avoid processing time triggers at exit */
128 		if (!ZBX_IS_RUNNING())
129 		{
130 			zbx_dc_clear_timer_queue();
131 			zbx_log_sync_history_cache_progress();
132 		}
133 
134 		/* database APIs might not handle signals correctly and hang, block signals to avoid hanging */
135 		block_signals();
136 		zbx_sync_history_cache(&values_num, &triggers_num, &more);
137 		unblock_signals();
138 
139 		total_values_num += values_num;
140 		total_triggers_num += triggers_num;
141 		total_sec += zbx_time() - sec;
142 
143 		sleeptime = (ZBX_SYNC_MORE == more ? 0 : CONFIG_HISTSYNCER_FREQUENCY);
144 
145 		if (0 != sleeptime || STAT_INTERVAL <= time(NULL) - last_stat_time)
146 		{
147 			stats_offset = 0;
148 			zbx_snprintf_alloc(&stats, &stats_alloc, &stats_offset, "processed %d values", total_values_num);
149 
150 			if (0 != (program_type & ZBX_PROGRAM_TYPE_SERVER))
151 			{
152 				zbx_snprintf_alloc(&stats, &stats_alloc, &stats_offset, ", %d triggers",
153 						total_triggers_num);
154 			}
155 
156 			zbx_snprintf_alloc(&stats, &stats_alloc, &stats_offset, " in " ZBX_FS_DBL " sec", total_sec);
157 
158 			if (0 == sleeptime)
159 				zbx_setproctitle("%s #%d [%s, syncing history]", process_name, process_num, stats);
160 			else
161 				zbx_setproctitle("%s #%d [%s, idle %d sec]", process_name, process_num, stats, sleeptime);
162 
163 			total_values_num = 0;
164 			total_triggers_num = 0;
165 			total_sec = 0.0;
166 			last_stat_time = time(NULL);
167 		}
168 
169 		if (ZBX_SYNC_MORE == more)
170 			continue;
171 
172 		if (!ZBX_IS_RUNNING())
173 			break;
174 
175 		zbx_sleep_loop(sleeptime);
176 	}
177 
178 	zbx_log_sync_history_cache_progress();
179 
180 	zbx_free(stats);
181 	DBclose();
182 	exit(EXIT_SUCCESS);
183 #undef STAT_INTERVAL
184 }
185