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

diag_server.c « diag « zabbix_server « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 09f2afa0794d9b30c7e9a87c798eae5c61945cf3 (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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
** Zabbix
** Copyright (C) 2001-2022 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 "zbxdiag.h"
#include "diag_server.h"

#include "zbxcachevalue.h"
#include "zbxlld.h"
#include "zbxalert.h"
#include "zbxtime.h"

#define ZBX_DIAG_LLD_RULES		0x00000001
#define ZBX_DIAG_LLD_VALUES		0x00000002

#define ZBX_DIAG_LLD_SIMPLE		(ZBX_DIAG_LLD_RULES | \
					ZBX_DIAG_LLD_VALUES)

#define ZBX_DIAG_ALERTING_ALERTS	0x00000001

#define ZBX_DIAG_ALERTING_SIMPLE	(ZBX_DIAG_ALERTING_ALERTS)

/******************************************************************************
 *                                                                            *
 * Purpose: sort itemid,values_num pair by values_num in descending order     *
 *                                                                            *
 ******************************************************************************/
static int	diag_valuecache_item_compare_values(const void *d1, const void *d2)
{
	zbx_vc_item_stats_t	*i1 = *(zbx_vc_item_stats_t **)d1;
	zbx_vc_item_stats_t	*i2 = *(zbx_vc_item_stats_t **)d2;

	return i2->values_num - i1->values_num;
}

/******************************************************************************
 *                                                                            *
 * Purpose: sort itemid,values_num pair by hourly_num in descending order     *
 *                                                                            *
 ******************************************************************************/
static int	diag_valuecache_item_compare_hourly(const void *d1, const void *d2)
{
	zbx_vc_item_stats_t	*i1 = *(zbx_vc_item_stats_t **)d1;
	zbx_vc_item_stats_t	*i2 = *(zbx_vc_item_stats_t **)d2;

	return i2->hourly_num - i1->hourly_num;
}

/******************************************************************************
 *                                                                            *
 * Purpose: add valuecache items diagnostic statistics to json                *
 *                                                                            *
 ******************************************************************************/
static void	diag_valuecache_add_items(struct zbx_json *json, const char *field, zbx_vc_item_stats_t **items,
		int items_num)
{
	int	i;

	zbx_json_addarray(json, field);

	for (i = 0; i < items_num; i++)
	{
		zbx_json_addobject(json, NULL);
		zbx_json_addint64(json, "itemid", items[i]->itemid);
		zbx_json_addint64(json, "values", items[i]->values_num);
		zbx_json_addint64(json, "request.values", items[i]->hourly_num);
		zbx_json_close(json);
	}
	zbx_json_close(json);
}

#define ZBX_DIAG_VALUECACHE_ITEMS		0x00000001
#define ZBX_DIAG_VALUECACHE_VALUES		0x00000002
#define ZBX_DIAG_VALUECACHE_MODE		0x00000004
#define ZBX_DIAG_VALUECACHE_MEMORY		0x00000008

#define ZBX_DIAG_VALUECACHE_SIMPLE	(ZBX_DIAG_VALUECACHE_ITEMS | \
					ZBX_DIAG_VALUECACHE_VALUES | \
					ZBX_DIAG_VALUECACHE_MODE)

/******************************************************************************
 *                                                                            *
 * Purpose: add requested value cache diagnostic information to json data     *
 *                                                                            *
 * Parameters: jp    - [IN] the request                                       *
 *             json  - [IN/OUT] the json to update                            *
 *             error - [OUT] error message                                    *
 *                                                                            *
 * Return value: SUCCEED - the information was added successfully             *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 ******************************************************************************/
static int	diag_add_valuecache_info(const struct zbx_json_parse *jp, struct zbx_json *json, char **error)
{
	zbx_vector_ptr_t	tops;
	int			ret;
	double			time1, time2, time_total = 0;
	zbx_uint64_t		fields;
	zbx_diag_map_t		field_map[] = {
					{"", ZBX_DIAG_VALUECACHE_SIMPLE | ZBX_DIAG_VALUECACHE_MEMORY},
					{"items", ZBX_DIAG_VALUECACHE_ITEMS},
					{"values", ZBX_DIAG_VALUECACHE_VALUES},
					{"mode", ZBX_DIAG_VALUECACHE_MODE},
					{"memory", ZBX_DIAG_VALUECACHE_MEMORY},
					{NULL, 0}
					};

	zbx_vector_ptr_create(&tops);

	if (SUCCEED == (ret = zbx_diag_parse_request(jp, field_map, &fields, &tops, error)))
	{
		zbx_json_addobject(json, ZBX_DIAG_VALUECACHE);

		if (0 != (fields & ZBX_DIAG_VALUECACHE_SIMPLE))
		{
			zbx_uint64_t	values_num, items_num;
			int		mode;

			time1 = zbx_time();
			zbx_vc_get_diag_stats(&items_num, &values_num, &mode);
			time2 = zbx_time();
			time_total += time2 - time1;

			if (0 != (fields & ZBX_DIAG_VALUECACHE_ITEMS))
				zbx_json_addint64(json, "items", items_num);
			if (0 != (fields & ZBX_DIAG_VALUECACHE_VALUES))
				zbx_json_addint64(json, "values", values_num);
			if (0 != (fields & ZBX_DIAG_VALUECACHE_MODE))
				zbx_json_addint64(json, "mode", mode);
		}

		if (0 != (fields & ZBX_DIAG_VALUECACHE_MEMORY))
		{
			zbx_shmem_stats_t	mem;

			time1 = zbx_time();
			zbx_vc_get_mem_stats(&mem);
			time2 = zbx_time();
			time_total += time2 - time1;

			zbx_diag_add_mem_stats(json, "memory", &mem);
		}

		if (0 != tops.values_num)
		{
			zbx_vector_ptr_t	items;
			int			i;

			zbx_vector_ptr_create(&items);

			time1 = zbx_time();
			zbx_vc_get_item_stats(&items);
			time2 = zbx_time();
			time_total += time2 - time1;

			zbx_json_addobject(json, "top");

			for (i = 0; i < tops.values_num; i++)
			{
				zbx_diag_map_t	*map = (zbx_diag_map_t *)tops.values[i];
				int		limit;

				if (0 == strcmp(map->name, "values"))
				{
					zbx_vector_ptr_sort(&items, diag_valuecache_item_compare_values);
				}
				else if (0 == strcmp(map->name, "request.values"))
				{
					zbx_vector_ptr_sort(&items, diag_valuecache_item_compare_hourly);
				}
				else
				{
					*error = zbx_dsprintf(*error, "Unsupported top field: %s", map->name);
					ret = FAIL;
					goto out;
				}

				limit = MIN((int)map->value, items.values_num);
				diag_valuecache_add_items(json, map->name, (zbx_vc_item_stats_t **)items.values, limit);
			}
			zbx_json_close(json);

			zbx_vector_ptr_clear_ext(&items, zbx_ptr_free);
			zbx_vector_ptr_destroy(&items);
		}

		zbx_json_addfloat(json, "time", time_total);

		zbx_json_close(json);
	}
out:
	zbx_vector_ptr_clear_ext(&tops, (zbx_ptr_free_func_t)zbx_diag_map_free);
	zbx_vector_ptr_destroy(&tops);

	return ret;
}

#undef ZBX_DIAG_VALUECACHE_SIMPLE

#undef ZBX_DIAG_VALUECACHE_ITEMS
#undef ZBX_DIAG_VALUECACHE_VALUES
#undef ZBX_DIAG_VALUECACHE_MODE
#undef ZBX_DIAG_VALUECACHE_MEMORY

/******************************************************************************
 *                                                                            *
 * Purpose: add lld item top list to output json                              *
 *                                                                            *
 ******************************************************************************/
static void	diag_add_lld_items(struct zbx_json *json, const char *field, const zbx_vector_uint64_pair_t *items)
{
	int	i;

	zbx_json_addarray(json, field);

	for (i = 0; i < items->values_num; i++)
	{
		zbx_json_addobject(json, NULL);
		zbx_json_adduint64(json, "itemid", items->values[i].first);
		zbx_json_adduint64(json, "values", items->values[i].second);
		zbx_json_close(json);
	}

	zbx_json_close(json);
}

/******************************************************************************
 *                                                                            *
 * Purpose: add requested lld manager diagnostic information to json data     *
 *                                                                            *
 * Parameters: jp    - [IN] the request                                       *
 *             json  - [IN/OUT] the json to update                            *
 *             error - [OUT] error message                                    *
 *                                                                            *
 * Return value: SUCCEED - the information was added successfully             *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 ******************************************************************************/
static int	diag_add_lld_info(const struct zbx_json_parse *jp, struct zbx_json *json, char **error)
{
	zbx_vector_ptr_t	tops;
	int			ret;
	double			time1, time2, time_total = 0;
	zbx_uint64_t		fields;
	zbx_diag_map_t		field_map[] = {
					{"", ZBX_DIAG_LLD_SIMPLE},
					{"rules", ZBX_DIAG_LLD_RULES},
					{"values", ZBX_DIAG_LLD_VALUES},
					{NULL, 0}
					};

	zbx_vector_ptr_create(&tops);

	if (SUCCEED == (ret = zbx_diag_parse_request(jp, field_map, &fields, &tops, error)))
	{
		zbx_json_addobject(json, ZBX_DIAG_LLD);

		if (0 != (fields & ZBX_DIAG_LLD_SIMPLE))
		{
			zbx_uint64_t	values_num, items_num;

			time1 = zbx_time();
			if (FAIL == (ret = zbx_lld_get_diag_stats(&items_num, &values_num, error)))
				goto out;
			time2 = zbx_time();
			time_total += time2 - time1;

			if (0 != (fields & ZBX_DIAG_LLD_RULES))
				zbx_json_addint64(json, "rules", items_num);
			if (0 != (fields & ZBX_DIAG_LLD_VALUES))
				zbx_json_addint64(json, "values", values_num);
		}

		if (0 != tops.values_num)
		{
			int	i;

			zbx_json_addobject(json, "top");

			for (i = 0; i < tops.values_num; i++)
			{
				zbx_diag_map_t	*map = (zbx_diag_map_t *)tops.values[i];

				if (0 == strcmp(map->name, "values"))
				{
					zbx_vector_uint64_pair_t	items;

					zbx_vector_uint64_pair_create(&items);

					time1 = zbx_time();
					if (FAIL == (ret = zbx_lld_get_top_items(map->value, &items, error)))
					{
						zbx_vector_uint64_pair_destroy(&items);
						goto out;
					}
					time2 = zbx_time();
					time_total += time2 - time1;

					diag_add_lld_items(json, map->name, &items);
					zbx_vector_uint64_pair_destroy(&items);
				}
				else
				{
					*error = zbx_dsprintf(*error, "Unsupported top field: %s", map->name);
					ret = FAIL;
					goto out;
				}
			}

			zbx_json_close(json);
		}

		zbx_json_addfloat(json, "time", time_total);
		zbx_json_close(json);
	}
out:
	zbx_vector_ptr_clear_ext(&tops, (zbx_ptr_free_func_t)zbx_diag_map_free);
	zbx_vector_ptr_destroy(&tops);

	return ret;
}

/******************************************************************************
 *                                                                            *
 * Purpose: add mediatype top list to output json                             *
 *                                                                            *
 * Parameters: json  - [OUT] the output json                                  *
 *             field - [IN] the field name                                    *
 *             items - [IN] a top mediatype list consisting of                *
 *                          mediatype, alerts_num pairs                       *
 *                                                                            *
 ******************************************************************************/
static void	diag_add_alerting_mediatypes(struct zbx_json *json, const char *field,
		const zbx_vector_uint64_pair_t *mediatypes)
{
	int	i;

	zbx_json_addarray(json, field);

	for (i = 0; i < mediatypes->values_num; i++)
	{
		zbx_json_addobject(json, NULL);
		zbx_json_adduint64(json, "mediatypeid", mediatypes->values[i].first);
		zbx_json_adduint64(json, "alerts", mediatypes->values[i].second);
		zbx_json_close(json);
	}

	zbx_json_close(json);
}

/******************************************************************************
 *                                                                            *
 * Purpose: add alert source top list to output json                          *
 *                                                                            *
 * Parameters: json  - [OUT] the output json                                  *
 *             field - [IN] the field name                                    *
 *             items - [IN] a top alert source list consisting of             *
 *                          zbx_am_source_stats_t structures                  *
 *                                                                            *
 ******************************************************************************/
static void	diag_add_alerting_sources(struct zbx_json *json, const char *field, const zbx_vector_ptr_t *sources)
{
	int	i;

	zbx_json_addarray(json, field);

	for (i = 0; i < sources->values_num; i++)
	{
		const zbx_am_source_stats_t	*source = (const zbx_am_source_stats_t *)sources->values[i];

		zbx_json_addobject(json, NULL);
		zbx_json_adduint64(json, "source", source->source);
		zbx_json_adduint64(json, "object", source->object);
		zbx_json_adduint64(json, "objectid", source->objectid);
		zbx_json_adduint64(json, "alerts", source->alerts_num);
		zbx_json_close(json);
	}

	zbx_json_close(json);
}

/******************************************************************************
 *                                                                            *
 * Purpose: add requested alert manager diagnostic information to json data   *
 *                                                                            *
 * Parameters: jp    - [IN] the request                                       *
 *             json  - [IN/OUT] the json to update                            *
 *             error - [OUT] error message                                    *
 *                                                                            *
 * Return value: SUCCEED - the information was added successfully             *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 ******************************************************************************/
static int	diag_add_alerting_info(const struct zbx_json_parse *jp, struct zbx_json *json, char **error)
{
	zbx_vector_ptr_t	tops;
	int			ret;
	double			time1, time2, time_total = 0;
	zbx_uint64_t		fields;
	zbx_diag_map_t		field_map[] = {
					{"", ZBX_DIAG_ALERTING_SIMPLE},
					{"alerts", ZBX_DIAG_ALERTING_ALERTS},
					{NULL, 0}
					};

	zbx_vector_ptr_create(&tops);

	if (SUCCEED == (ret = zbx_diag_parse_request(jp, field_map, &fields, &tops, error)))
	{
		zbx_json_addobject(json, ZBX_DIAG_ALERTING);

		if (0 != (fields & ZBX_DIAG_ALERTING_SIMPLE))
		{
			zbx_uint64_t	alerts_num;

			time1 = zbx_time();
			if (FAIL == (ret = zbx_alerter_get_diag_stats(&alerts_num, error)))
				goto out;
			time2 = zbx_time();
			time_total += time2 - time1;

			if (0 != (fields & ZBX_DIAG_ALERTING_ALERTS))
				zbx_json_addint64(json, "alerts", alerts_num);
		}

		if (0 != tops.values_num)
		{
			int	i;

			zbx_json_addobject(json, "top");

			for (i = 0; i < tops.values_num; i++)
			{
				zbx_diag_map_t	*map = (zbx_diag_map_t *)tops.values[i];

				if (0 == strcmp(map->name, "media.alerts"))
				{
					zbx_vector_uint64_pair_t	mediatypes;

					zbx_vector_uint64_pair_create(&mediatypes);

					time1 = zbx_time();
					if (FAIL == (ret = zbx_alerter_get_top_mediatypes(map->value, &mediatypes,
							error)))
					{
						zbx_vector_uint64_pair_destroy(&mediatypes);
						goto out;
					}
					time2 = zbx_time();
					time_total += time2 - time1;

					diag_add_alerting_mediatypes(json, map->name, &mediatypes);
					zbx_vector_uint64_pair_destroy(&mediatypes);
				}
				else if (0 == strcmp(map->name, "source.alerts"))
				{
					zbx_vector_ptr_t	sources;

					zbx_vector_ptr_create(&sources);

					time1 = zbx_time();
					if (FAIL == (ret = zbx_alerter_get_top_sources(map->value, &sources, error)))
					{
						zbx_vector_ptr_clear_ext(&sources, zbx_ptr_free);
						zbx_vector_ptr_destroy(&sources);
						goto out;
					}
					time2 = zbx_time();
					time_total += time2 - time1;

					diag_add_alerting_sources(json, map->name, &sources);
					zbx_vector_ptr_clear_ext(&sources, zbx_ptr_free);
					zbx_vector_ptr_destroy(&sources);
				}
				else
				{
					*error = zbx_dsprintf(*error, "Unsupported top field: %s", map->name);
					ret = FAIL;
					goto out;
				}
			}

			zbx_json_close(json);
		}

		zbx_json_addfloat(json, "time", time_total);
		zbx_json_close(json);
	}
out:
	zbx_vector_ptr_clear_ext(&tops, (zbx_ptr_free_func_t)zbx_diag_map_free);
	zbx_vector_ptr_destroy(&tops);

	return ret;
}

/******************************************************************************
 *                                                                            *
 * Purpose: add requested section diagnostic information                      *
 *                                                                            *
 * Parameters: section - [IN] the section name                                *
 *             jp      - [IN] the request                                     *
 *             json    - [IN/OUT] the json to update                          *
 *             error   - [OUT] the error message                              *
 *                                                                            *
 * Return value: SUCCEED - the information was retrieved successfully         *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 ******************************************************************************/
int	diag_add_section_info(const char *section, const struct zbx_json_parse *jp, struct zbx_json *json,
		char **error)
{
	int	ret = FAIL;

	if (0 == strcmp(section, ZBX_DIAG_HISTORYCACHE))
		ret = zbx_diag_add_historycache_info(jp, json, error);
	else if (0 == strcmp(section, ZBX_DIAG_VALUECACHE))
		ret = diag_add_valuecache_info(jp, json, error);
	else if (0 == strcmp(section, ZBX_DIAG_PREPROCESSING))
		ret = zbx_diag_add_preproc_info(jp, json, error);
	else if (0 == strcmp(section, ZBX_DIAG_LLD))
		ret = diag_add_lld_info(jp, json, error);
	else if (0 == strcmp(section, ZBX_DIAG_ALERTING))
		ret = diag_add_alerting_info(jp, json, error);
	else if (0 == strcmp(section, ZBX_DIAG_LOCKS))
	{
		zbx_diag_add_locks_info(json);
		ret = SUCCEED;
	}
	else
		*error = zbx_dsprintf(*error, "Unsupported diagnostics section: %s", section);

	return ret;
}