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

trapper_preproc.c « trapper « zabbix_server « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c680323ae57f294f46d3b74395896563c58a95a0 (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
/*
** Zabbix
** Copyright (C) 2001-2019 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 "log.h"
#include "zbxjson.h"
#include "zbxalgo.h"
#include "preproc.h"
#include "trapper_preproc.h"

/******************************************************************************
 *                                                                            *
 * Function: zbx_trapper_preproc_test                                         *
 *                                                                            *
 * Purpose: processes preprocessing test request                              *
 *                                                                            *
 * Parameters: sock - [IN] the request source socket (frontend)               *
 *             jp   - [IN] the request                                        *
 *                                                                            *
 * Return value: SUCCEED - the request was processed successfully             *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 * Comments: This function will send proper (success/fail) response to the    *
 *           request socket.                                                  *
 *           Preprocessing failure (error returned by a preprocessing step)   *
 *           is counted as successful test and will return success response.  *
 *                                                                            *
 ******************************************************************************/
int	zbx_trapper_preproc_test(zbx_socket_t *sock, const struct zbx_json_parse *jp)
{
	char			buffer[MAX_STRING_LEN], *error = NULL, *value = NULL, *last_value = NULL,
				*last_ts = NULL, *step_params = NULL, *error_handler_params = NULL,
				*preproc_error = NULL;
	zbx_user_t		user;
	int			ret = FAIL, i;
	struct zbx_json_parse	jp_data, jp_history, jp_steps, jp_step;
	size_t			size;
	unsigned char		value_type, step_type, error_handler;
	zbx_vector_ptr_t	steps, results;
	const char		*pnext;
	struct zbx_json		json;

	zbx_vector_ptr_create(&steps);
	zbx_vector_ptr_create(&results);

	if (FAIL == zbx_json_value_by_name(jp, ZBX_PROTO_TAG_SID, buffer, sizeof(buffer)) ||
			SUCCEED != DBget_user_by_active_session(buffer, &user) || USER_TYPE_ZABBIX_ADMIN > user.type)
	{
		error = zbx_strdup(NULL, "Permission denied.");
		goto out;
	}

	if (FAIL == zbx_json_brackets_by_name(jp, ZBX_PROTO_TAG_DATA, &jp_data))
	{
		error = zbx_strdup(NULL, "Missing data field.");
		goto out;
	}

	size = 0;
	if (FAIL == zbx_json_value_by_name_dyn(&jp_data, ZBX_PROTO_TAG_VALUE, &value, &size))
	{
		error = zbx_strdup(NULL, "Missing value field.");
		goto out;
	}

	if (FAIL == zbx_json_value_by_name(&jp_data, ZBX_PROTO_TAG_VALUE_TYPE, buffer, sizeof(buffer)))
	{
		error = zbx_strdup(NULL, "Missing value type field.");
		goto out;
	}
	value_type = atoi(buffer);

	if (SUCCEED == zbx_json_brackets_by_name(&jp_data, ZBX_PROTO_TAG_HISTORY, &jp_history))
	{
		size = 0;
		if (FAIL == zbx_json_value_by_name_dyn(&jp_history, ZBX_PROTO_TAG_VALUE, &last_value, &size))
		{
			error = zbx_strdup(NULL, "Missing history value field.");
			goto out;
		}

		size = 0;
		if (FAIL == zbx_json_value_by_name_dyn(&jp_history, ZBX_PROTO_TAG_TIMESTAMP, &last_ts, &size))
		{
			error = zbx_strdup(NULL, "Missing history timestamp field.");
			goto out;
		}
	}

	if (FAIL == zbx_json_brackets_by_name(&jp_data, ZBX_PROTO_TAG_STEPS, &jp_steps))
	{
		error = zbx_strdup(NULL, "Missing preprocessing steps field.");
		goto out;
	}

	for (pnext = NULL; NULL != (pnext = zbx_json_next(&jp_steps, pnext));)
	{
		zbx_preproc_op_t	*step;

		if (FAIL == zbx_json_brackets_open(pnext, &jp_step))
		{
			error = zbx_strdup(NULL, "Cannot parse preprocessing step.");
			goto out;
		}

		if (FAIL == zbx_json_value_by_name(&jp_step, ZBX_PROTO_TAG_TYPE, buffer, sizeof(buffer)))
		{
			error = zbx_strdup(NULL, "Missing preprocessing step type field.");
			goto out;
		}
		step_type = atoi(buffer);

		if (FAIL == zbx_json_value_by_name(&jp_step, ZBX_PROTO_TAG_ERROR_HANDLER, buffer, sizeof(buffer)))
		{
			error = zbx_strdup(NULL, "Missing preprocessing step type error handler field.");
			goto out;
		}
		error_handler = atoi(buffer);

		size = 0;
		if (FAIL == zbx_json_value_by_name_dyn(&jp_step, ZBX_PROTO_TAG_PARAMS, &step_params, &size))
		{
			error = zbx_strdup(NULL, "Missing preprocessing step type params field.");
			goto out;
		}

		size = 0;
		if (FAIL == zbx_json_value_by_name_dyn(&jp_step, ZBX_PROTO_TAG_ERROR_HANDLER_PARAMS,
				&error_handler_params, &size))
		{
			error = zbx_strdup(NULL, "Missing preprocessing step type error handler params field.");
			goto out;
		}

		step = (zbx_preproc_op_t *)zbx_malloc(NULL, sizeof(zbx_preproc_op_t));
		step->type = step_type;
		step->params = step_params;
		step->error_handler = error_handler;
		step->error_handler_params = error_handler_params;
		zbx_vector_ptr_append(&steps, step);

		step_params = NULL;
		error_handler_params = NULL;
	}

	if (FAIL == zbx_preprocessor_test(value_type, value, last_value, last_ts, &steps, &results, &preproc_error,
			&error))
	{
		goto out;
	}

	zbx_json_init(&json, results.values_num * 256);

	zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, "success", ZBX_JSON_TYPE_STRING);
	zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA);

	for (i = 0; i < results.values_num; i++)
	{
		zbx_preproc_result_t	*result = (zbx_preproc_result_t *)results.values[i];

		zbx_json_addobject(&json, NULL);

		if (NULL != result->error)
			zbx_json_addstring(&json, ZBX_PROTO_TAG_ERROR, result->error, ZBX_JSON_TYPE_STRING);

		if (ZBX_PREPROC_FAIL_DEFAULT != result->action)
			zbx_json_adduint64(&json, ZBX_PROTO_TAG_ACTION, result->action);

		if (i == results.values_num - 1 && NULL != preproc_error)
		{
			if (ZBX_PREPROC_FAIL_SET_ERROR == result->action)
				zbx_json_addstring(&json, ZBX_PROTO_TAG_FAILED, preproc_error, ZBX_JSON_TYPE_STRING);
		}
		else
		{
			if (ZBX_VARIANT_NONE != result->value.type)
			{
				zbx_json_addstring(&json, ZBX_PROTO_TAG_RESULT, zbx_variant_value_desc(&result->value),
						ZBX_JSON_TYPE_STRING);
			}
			else
				zbx_json_addstring(&json, ZBX_PROTO_TAG_RESULT, NULL, ZBX_JSON_TYPE_NULL);
		}

		zbx_json_close(&json);
	}

	zbx_tcp_send_bytes_to(sock, json.buffer, json.buffer_size, CONFIG_TIMEOUT);

	zbx_json_free(&json);

	ret = SUCCEED;
out:
	if (FAIL == ret)
	{
		zbx_send_response(sock, ret, error, CONFIG_TIMEOUT);
		zbx_free(error);
	}

	zbx_free(preproc_error);
	zbx_free(error);
	zbx_free(error_handler_params);
	zbx_free(step_params);
	zbx_free(last_ts);
	zbx_free(last_value);
	zbx_free(value);

	zbx_vector_ptr_clear_ext(&results, (zbx_clean_func_t)zbx_preproc_result_free);
	zbx_vector_ptr_destroy(&results);
	zbx_vector_ptr_clear_ext(&steps, (zbx_clean_func_t)zbx_preproc_op_free);
	zbx_vector_ptr_destroy(&steps);

	return ret;
}