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 "log.h"
22 #include "zbxself.h"
23 #include "zbxavailability.h"
24 #include "zbxipcservice.h"
25 #include "avail_manager.h"
26 #include "daemon.h"
27 #include "sighandler.h"
28 #include "dbcache.h"
29 #include "zbxalgo.h"
30 #include "avail_protocol.h"
31 
32 extern unsigned char	process_type, program_type;
33 extern int		server_num, process_num;
34 static sigset_t		orig_mask;
35 
36 #define ZBX_AVAILABILITY_MANAGER_DELAY			1
37 #define ZBX_AVAILABILITY_MANAGER_FLUSH_DELAY_SEC	5
38 
interface_availability_compare(const void * d1,const void * d2)39 static int	interface_availability_compare(const void *d1, const void *d2)
40 {
41 	const zbx_interface_availability_t	*ia1 = *(const zbx_interface_availability_t **)d1;
42 	const zbx_interface_availability_t	*ia2 = *(const zbx_interface_availability_t **)d2;
43 
44 	ZBX_RETURN_IF_NOT_EQUAL(ia1->interfaceid, ia2->interfaceid);
45 
46 	return ia1->id - ia2->id;
47 }
48 
ZBX_THREAD_ENTRY(availability_manager_thread,args)49 ZBX_THREAD_ENTRY(availability_manager_thread, args)
50 {
51 	zbx_ipc_service_t		service;
52 	char				*error = NULL;
53 	zbx_ipc_client_t		*client;
54 	zbx_ipc_message_t		*message;
55 	int				ret, processed_num = 0;
56 	double				time_stat, time_idle = 0, time_now, time_flush, sec;
57 	zbx_vector_availability_ptr_t	interface_availabilities;
58 
59 #define	STAT_INTERVAL	5	/* if a process is busy and does not sleep then update status not faster than */
60 				/* once in STAT_INTERVAL seconds */
61 
62 	process_type = ((zbx_thread_args_t *)args)->process_type;
63 	server_num = ((zbx_thread_args_t *)args)->server_num;
64 	process_num = ((zbx_thread_args_t *)args)->process_num;
65 
66 	zabbix_log(LOG_LEVEL_INFORMATION, "%s #%d started [%s #%d]", get_program_type_string(program_type),
67 				server_num, get_process_type_string(process_type), process_num);
68 
69 	update_selfmon_counter(ZBX_PROCESS_STATE_BUSY);
70 
71 	zbx_setproctitle("%s #%d [connecting to the database]", get_process_type_string(process_type), process_num);
72 
73 	DBconnect(ZBX_DB_CONNECT_NORMAL);
74 
75 	if (FAIL == zbx_ipc_service_start(&service, ZBX_IPC_SERVICE_AVAILABILITY, &error))
76 	{
77 		zabbix_log(LOG_LEVEL_CRIT, "cannot start availability manager service: %s", error);
78 		zbx_free(error);
79 		exit(EXIT_FAILURE);
80 	}
81 
82 	/* initialize statistics */
83 	time_stat = zbx_time();
84 	time_flush = time_stat;
85 
86 	zbx_vector_availability_ptr_create(&interface_availabilities);
87 
88 	zbx_setproctitle("%s #%d started", get_process_type_string(process_type), process_num);
89 
90 	while (ZBX_IS_RUNNING())
91 	{
92 		time_now = zbx_time();
93 
94 		if (STAT_INTERVAL < time_now - time_stat)
95 		{
96 			zbx_setproctitle("%s #%d [queued %d, processed %d values, idle "
97 					ZBX_FS_DBL " sec during " ZBX_FS_DBL " sec]",
98 					get_process_type_string(process_type), process_num,
99 					interface_availabilities.values_num, processed_num, time_idle, time_now - time_stat);
100 
101 			time_stat = time_now;
102 			time_idle = 0;
103 			processed_num = 0;
104 		}
105 
106 		update_selfmon_counter(ZBX_PROCESS_STATE_IDLE);
107 		ret = zbx_ipc_service_recv(&service, ZBX_AVAILABILITY_MANAGER_DELAY, &client, &message);
108 		update_selfmon_counter(ZBX_PROCESS_STATE_BUSY);
109 		sec = zbx_time();
110 		zbx_update_env(sec);
111 
112 		if (ZBX_IPC_RECV_IMMEDIATE != ret)
113 			time_idle += sec - time_now;
114 
115 		if (NULL != message)
116 		{
117 			zbx_availability_deserialize(message->data, message->size, &interface_availabilities);
118 			zbx_ipc_message_free(message);
119 		}
120 
121 		if (NULL != client)
122 			zbx_ipc_client_release(client);
123 
124 		if (ZBX_AVAILABILITY_MANAGER_FLUSH_DELAY_SEC < time_now - time_flush)
125 		{
126 			time_flush = time_now;
127 
128 			if (0 == interface_availabilities.values_num)
129 				continue;
130 
131 			zbx_block_signals(&orig_mask);
132 			zbx_vector_availability_ptr_sort(&interface_availabilities, interface_availability_compare);
133 			zbx_db_update_interface_availabilities(&interface_availabilities);
134 			zbx_unblock_signals(&orig_mask);
135 
136 			processed_num = interface_availabilities.values_num;
137 			zbx_vector_availability_ptr_clear_ext(&interface_availabilities,
138 					zbx_interface_availability_free);
139 		}
140 	}
141 
142 	zbx_block_signals(&orig_mask);
143 	if (0 != interface_availabilities.values_num)
144 	{
145 		zbx_vector_availability_ptr_sort(&interface_availabilities, interface_availability_compare);
146 		zbx_db_update_interface_availabilities(&interface_availabilities);
147 	}
148 	DBclose();
149 	zbx_unblock_signals(&orig_mask);
150 
151 	exit(EXIT_SUCCESS);
152 #undef STAT_INTERVAL
153 }
154 
155