Welcome to mirror list, hosted at ThFree Co, Russian Federation.

report_protocol.c « reporter « zabbix_server « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e205efd33c59d013c2bd7873a007ae494774ceb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

#include "common.h"
#include "zbxreport.h"
#include "report_protocol.h"
#include "zbxipcservice.h"
#include "zbxserialize.h"
#include "zbxalgo.h"
#include "db.h"
#include "zbxalert.h"

static int	json_uint_by_tag(const struct zbx_json_parse *jp, const char *tag, zbx_uint64_t *value, char **error)
{
	char	buf[MAX_ID_LEN + 1];

	if (SUCCEED != zbx_json_value_by_name(jp, tag, buf, sizeof(buf), NULL))
	{
		*error = zbx_dsprintf(*error, "cannot find tag: %s", tag);
		return FAIL;
	}

	if (SUCCEED != is_uint64(buf, value))
	{
		*error = zbx_dsprintf(*error, "invalid tag %s value: %s", tag, buf);
		return FAIL;
	}

	return SUCCEED;
}

/******************************************************************************
 *                                                                            *
 * ZBX_IPC_REPORTER_TEST_REPORT message serialization/deserialization         *
 *                                                                            *
 ******************************************************************************/

static zbx_uint32_t	report_serialize_test_report(unsigned char **data, const char *name, zbx_uint64_t dashboardid,
		zbx_uint64_t userid, zbx_uint64_t viewer_userid, int report_time, unsigned char period,
		const zbx_vector_ptr_pair_t *params)
{
	zbx_uint32_t	data_len = 0, *len, name_len;
	int		i;
	unsigned char	*ptr;

	zbx_serialize_prepare_str(data_len, name);
	zbx_serialize_prepare_value(data_len, dashboardid);
	zbx_serialize_prepare_value(data_len, userid);
	zbx_serialize_prepare_value(data_len, viewer_userid);
	zbx_serialize_prepare_value(data_len, report_time);
	zbx_serialize_prepare_value(data_len, period);
	zbx_serialize_prepare_value(data_len, params->values_num);

	len = (zbx_uint32_t *)zbx_malloc(NULL, params->values_num * 2 * sizeof(zbx_uint32_t));

	for (i = 0; i < params->values_num; i++)
	{
		zbx_serialize_prepare_str_len(data_len, params->values[i].first, len[i * 2]);
		zbx_serialize_prepare_str_len(data_len, params->values[i].second, len[i * 2 + 1]);
	}

	*data = (unsigned char *)zbx_malloc(NULL, data_len);
	ptr = *data;

	ptr += zbx_serialize_str(ptr, name, name_len);
	ptr += zbx_serialize_value(ptr, dashboardid);
	ptr += zbx_serialize_value(ptr, userid);
	ptr += zbx_serialize_value(ptr, viewer_userid);
	ptr += zbx_serialize_value(ptr, report_time);
	ptr += zbx_serialize_value(ptr, period);
	ptr += zbx_serialize_value(ptr, params->values_num);

	for (i = 0; i < params->values_num; i++)
	{
		ptr += zbx_serialize_str(ptr, params->values[i].first, len[i * 2]);
		ptr += zbx_serialize_str(ptr, params->values[i].second, len[i * 2 + 1]);
	}

	zbx_free(len);

	return data_len;
}

void	report_deserialize_test_report(const unsigned char *data, char **name, zbx_uint64_t *dashboardid,
		zbx_uint64_t *userid, zbx_uint64_t *viewer_userid, int *report_time, unsigned char *period,
		zbx_vector_ptr_pair_t *params)
{
	int		params_num, i;
	zbx_uint32_t	len;

	data += zbx_deserialize_str(data, name, len);
	data += zbx_deserialize_value(data, dashboardid);
	data += zbx_deserialize_value(data, userid);
	data += zbx_deserialize_value(data, viewer_userid);
	data += zbx_deserialize_value(data, report_time);
	data += zbx_deserialize_value(data, period);
	data += zbx_deserialize_value(data, &params_num);

	zbx_vector_ptr_pair_reserve(params, (size_t)params_num);
	for (i = 0; i < params_num; i++)
	{
		zbx_ptr_pair_t	pair;

		data += zbx_deserialize_str(data, (char **)&pair.first, len);
		data += zbx_deserialize_str(data, (char **)&pair.second, len);
		zbx_vector_ptr_pair_append(params, pair);
	}
}

//#include "log.h"

/******************************************************************************
 *                                                                            *
 * ZBX_IPC_REPORTER_TEST_REPORT_RESULT, ZBX_IPC_REPORTER_REPORT_RESULT        *
 * message serialization/deserialization                                      *
 *                                                                            *
 ******************************************************************************/

zbx_uint32_t	report_serialize_response(unsigned char **data, int status, const char *error,
		const zbx_vector_ptr_t *results)
{
	zbx_uint32_t			data_len = 0, error_len, *recipient_len, *info_len;
	unsigned char			*ptr;
	int				i;
	zbx_alerter_dispatch_result_t	*result;

	zbx_serialize_prepare_value(data_len, status);
	zbx_serialize_prepare_str(data_len, error);

	if (SUCCEED == status)
	{
		zbx_serialize_prepare_value(data_len, results->values_num);
		recipient_len = (zbx_uint32_t *)zbx_malloc(NULL, sizeof(zbx_uint32_t) * results->values_num);
		info_len = (zbx_uint32_t *)zbx_malloc(NULL, sizeof(zbx_uint32_t) * results->values_num);

		for (i = 0; i < results->values_num; i++)
		{
			result = (zbx_alerter_dispatch_result_t *)results->values[i];
			zbx_serialize_prepare_value(data_len, result->status);
			zbx_serialize_prepare_str_len(data_len, result->recipient, recipient_len[i]);
			zbx_serialize_prepare_str_len(data_len, result->info, info_len[i]);
		}
	}
	else
	{
		i = 0;
		zbx_serialize_prepare_value(data_len, i);
	}

	*data = (unsigned char *)zbx_malloc(NULL, data_len);
	ptr = *data;

	ptr += zbx_serialize_value(ptr, status);
	ptr += zbx_serialize_str(ptr, error, error_len);

	if (SUCCEED == status)
	{
		ptr += zbx_serialize_value(ptr, results->values_num);

		for (i = 0; i < results->values_num; i++)
		{
			result = (zbx_alerter_dispatch_result_t *)results->values[i];
			ptr += zbx_serialize_value(ptr, result->status);
			ptr += zbx_serialize_str(ptr, result->recipient, recipient_len[i]);
			ptr += zbx_serialize_str(ptr, result->info, info_len[i]);
		}

		zbx_free(info_len);
		zbx_free(recipient_len);
	}
	else
		(void)zbx_serialize_value(ptr, i);

	return data_len;
}

void	report_deserialize_response(const unsigned char *data, int *status, char **error, zbx_vector_ptr_t *results)
{
	zbx_uint32_t	len;

	data += zbx_deserialize_value(data, status);
	data += zbx_deserialize_str(data, error, len);

	if (SUCCEED == *status && NULL != results)
	{
		int				i, results_num;
		zbx_alerter_dispatch_result_t	*result;

		data += zbx_deserialize_value(data, &results_num);

		if (0 != results_num)
		{
			zbx_vector_ptr_reserve(results, (size_t)results_num);

			for (i = 0; i < results_num; i++)
			{
				result = zbx_malloc(NULL, sizeof(zbx_alerter_dispatch_result_t));
				data += zbx_deserialize_value(data, &result->status);
				data += zbx_deserialize_str(data, &result->recipient, len);
				data += zbx_deserialize_str(data, &result->info, len);
				zbx_vector_ptr_append(results, result);
			}
		}
	}
}

/******************************************************************************
 *                                                                            *
 * ZBX_IPC_REPORTER_BEGIN_REPORT message serialization/deserialization        *
 *                                                                            *
 ******************************************************************************/

zbx_uint32_t	report_serialize_begin_report(unsigned char **data, const char *name, const char *url,
		const char *cookie, int width, int height, const zbx_vector_ptr_pair_t *params)
{
	zbx_uint32_t	data_len = 0, *params_len, url_len, cookie_len, name_len;
	unsigned char	*ptr;
	int		i;

	zbx_serialize_prepare_str(data_len, name);
	zbx_serialize_prepare_str(data_len, url);
	zbx_serialize_prepare_str(data_len, cookie);
	zbx_serialize_prepare_value(data_len, width);
	zbx_serialize_prepare_value(data_len, height);
	zbx_serialize_prepare_value(data_len, params->values_num);

	params_len = (zbx_uint32_t *)zbx_malloc(NULL, params->values_num * 2 * sizeof(zbx_uint32_t));
	for (i = 0; i < params->values_num; i++)
	{
		zbx_serialize_prepare_str_len(data_len, params->values[i].first, params_len[i * 2]);
		zbx_serialize_prepare_str_len(data_len, params->values[i].second, params_len[i * 2 + 1]);
	}

	*data = (unsigned char *)zbx_malloc(NULL, data_len);
	ptr = *data;

	ptr += zbx_serialize_str(ptr, name, name_len);
	ptr += zbx_serialize_str(ptr, url, url_len);
	ptr += zbx_serialize_str(ptr, cookie, cookie_len);
	ptr += zbx_serialize_value(ptr, width);
	ptr += zbx_serialize_value(ptr, height);

	ptr += zbx_serialize_value(ptr, params->values_num);

	for (i = 0; i < params->values_num; i++)
	{
		ptr += zbx_serialize_str(ptr, params->values[i].first, params_len[i * 2]);
		ptr += zbx_serialize_str(ptr, params->values[i].second, params_len[i * 2 + 1]);
	}

	zbx_free(params_len);

	return data_len;
}

void	report_deserialize_begin_report(const unsigned char *data, char **name, char **url, char **cookie,
		int *width, int *height, zbx_vector_ptr_pair_t *params)
{
	zbx_uint32_t	len;
	int		i, params_num;

	data += zbx_deserialize_str(data, name, len);
	data += zbx_deserialize_str(data, url, len);
	data += zbx_deserialize_str(data, cookie, len);
	data += zbx_deserialize_value(data, width);
	data += zbx_deserialize_value(data, height);

	data += zbx_deserialize_value(data, &params_num);
	zbx_vector_ptr_pair_reserve(params, (size_t)params_num);
	for (i = 0; i < params_num; i++)
	{
		zbx_ptr_pair_t	pair;

		data += zbx_deserialize_str(data, (char **)&pair.first, len);
		data += zbx_deserialize_str(data, (char **)&pair.second, len);
		zbx_vector_ptr_pair_append(params, pair);
	}
}

/******************************************************************************
 *                                                                            *
 * ZBX_IPC_REPORTER_SEND_REPORT message serialization/deserialization        *
 *                                                                            *
 ******************************************************************************/

zbx_uint32_t	report_serialize_send_report(unsigned char **data, const DB_MEDIATYPE *mt,
		const zbx_vector_str_t *emails)
{
	zbx_uint32_t	data_len = 0, data_alloc = 1024, data_offset = 0, *params_len;
	unsigned char	*ptr;
	int		i;

	*data = zbx_malloc(NULL, data_alloc);
	zbx_serialize_mediatype(data, &data_alloc, &data_offset, mt);

	zbx_serialize_prepare_value(data_len, emails->values_num);
	params_len = (zbx_uint32_t *)zbx_malloc(NULL, emails->values_num * sizeof(zbx_uint32_t));
	for (i = 0; i < emails->values_num; i++)
	{
		zbx_serialize_prepare_str_len(data_len, emails->values[i], params_len[i]);
	}

	if (data_alloc - data_offset < data_len)
	{
		data_alloc = data_offset + data_len;
		*data = (unsigned char *)zbx_realloc(*data, data_alloc);
	}

	ptr = *data + data_offset;
	ptr += zbx_serialize_value(ptr, emails->values_num);
	for (i = 0; i < emails->values_num; i++)
	{
		ptr += zbx_serialize_str(ptr, emails->values[i], params_len[i]);
	}

	zbx_free(params_len);

	return data_offset + data_len;
}

void	report_deserialize_send_report(const unsigned char *data, DB_MEDIATYPE *mt, zbx_vector_str_t *sendtos)
{
	zbx_uint32_t	len;
	int		i, sendto_num;

	data += zbx_deserialize_mediatype(data, mt);

	data += zbx_deserialize_value(data, &sendto_num);
	zbx_vector_str_reserve(sendtos, (size_t)sendto_num);
	for (i = 0; i < sendto_num; i++)
	{
		char	*sendto;

		data += zbx_deserialize_str(data, &sendto, len);
		zbx_vector_str_append(sendtos, sendto);
	}
}

static void	report_clear_ptr_pairs(zbx_vector_ptr_pair_t *params)
{
	int	i;

	for (i = 0; i < params->values_num; i++)
	{
		zbx_free(params->values[i].first);
		zbx_free(params->values[i].second);
	}
}

void	report_clear_params(zbx_vector_ptr_pair_t *params)
{
	report_clear_ptr_pairs(params);
	zbx_vector_ptr_pair_clear(params);
}

void	report_destroy_params(zbx_vector_ptr_pair_t *params)
{
	report_clear_ptr_pairs(params);
	zbx_vector_ptr_pair_destroy(params);
}

void	zbx_report_test(const struct zbx_json_parse *jp, zbx_uint64_t userid, struct zbx_json *j)
{
	zbx_uint64_t		dashboardid, viewer_userid, ui64;
	int			ret = FAIL, period, report_time;
	struct zbx_json_parse	jp_params;
	zbx_vector_ptr_pair_t	params;
	zbx_vector_ptr_t	results;
	zbx_uint32_t		size;
	unsigned char		*data = NULL, *response = NULL;
	char			*name = NULL, *error = NULL;
	size_t			name_alloc = 0;

	zbx_vector_ptr_pair_create(&params);
	zbx_vector_ptr_create(&results);

	if (SUCCEED != zbx_json_value_by_name_dyn(jp, ZBX_PROTO_TAG_NAME, &name, &name_alloc, NULL))
	{
		error = zbx_dsprintf(error, "cannot find tag: %s", ZBX_PROTO_TAG_NAME);
		goto out;
	}

	if (SUCCEED != json_uint_by_tag(jp, ZBX_PROTO_TAG_DASHBOARDID, &dashboardid, &error))
		goto out;

	if (SUCCEED != json_uint_by_tag(jp, ZBX_PROTO_TAG_USERID, &viewer_userid, &error))
		goto out;

	if (SUCCEED != json_uint_by_tag(jp, ZBX_PROTO_TAG_PERIOD, &ui64, &error))
		goto out;
	period = (int)ui64;

	if (SUCCEED != json_uint_by_tag(jp, ZBX_PROTO_TAG_NOW, &ui64, &error))
		goto out;
	report_time = (int)ui64;

	if (SUCCEED == zbx_json_brackets_by_name(jp, ZBX_PROTO_TAG_PARAMS, &jp_params))
	{
		const char		*pnext = NULL;
		char			key[MAX_STRING_LEN];

		while (NULL != (pnext = zbx_json_pair_next(&jp_params, pnext, key, sizeof(key))))
		{
			char		*value = NULL;
			size_t		value_alloc = 0;
			zbx_ptr_pair_t	pair;

			zbx_json_decodevalue_dyn(pnext, &value, &value_alloc, NULL);
			pair.first = zbx_strdup(NULL, key);
			pair.second = value;
			zbx_vector_ptr_pair_append(&params, pair);
		}
	}

	size = report_serialize_test_report(&data, name, dashboardid, userid, viewer_userid, report_time, period,
			&params);

	if (SUCCEED != zbx_ipc_async_exchange(ZBX_IPC_SERVICE_REPORTER, ZBX_IPC_REPORTER_TEST,
			SEC_PER_MIN, data, size, &response, &error))
	{
		goto out;
	}

	report_deserialize_response(response, &ret, &error, &results);
out:
	zbx_json_init(j, 1024);

	if (SUCCEED == ret)
	{
		int				i;
		zbx_alerter_dispatch_result_t	*result;

		zbx_json_addstring(j, ZBX_PROTO_TAG_RESPONSE, ZBX_PROTO_VALUE_SUCCESS, ZBX_JSON_TYPE_STRING);
		zbx_json_addobject(j, ZBX_PROTO_TAG_DATA);
		zbx_json_addarray(j, ZBX_PROTO_TAG_RECIPIENTS);

		for (i = 0; i < results.values_num; i++)
		{
			zbx_json_addobject(j, NULL);

			result = (zbx_alerter_dispatch_result_t *)results.values[i];
			zbx_json_addint64(j, ZBX_PROTO_TAG_STATUS, result->status);
			zbx_json_addstring(j, ZBX_PROTO_TAG_RECIPIENT, result->recipient, ZBX_JSON_TYPE_STRING);
			if (NULL != result->info)
				zbx_json_addstring(j, ZBX_PROTO_TAG_INFO, result->info, ZBX_JSON_TYPE_STRING);

			zbx_json_close(j);
		}
	}
	else
	{
		zbx_json_addstring(j, ZBX_PROTO_TAG_RESPONSE, ZBX_PROTO_VALUE_FAILED, ZBX_JSON_TYPE_STRING);
		zbx_json_addstring(j, ZBX_PROTO_TAG_INFO, error, ZBX_JSON_TYPE_STRING);
	}

	zbx_free(error);
	zbx_free(response);
	zbx_free(data);
	zbx_free(name);

	zbx_vector_ptr_clear_ext(&results, (zbx_clean_func_t)zbx_alerter_dispatch_result_free);
	zbx_vector_ptr_destroy(&results);
	report_destroy_params(&params);
}