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

service_actions.c « service « zabbix_server « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c96d5b8db7fd9856c6a61529c1f112a358859e7c (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
/*
** 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 "log.h"
#include "service_actions.h"

/******************************************************************************
 *                                                                            *
 * Function: condition_match_service                                          *
 *                                                                            *
 * Purpose: match service update by service id                                *
 *                                                                            *
 ******************************************************************************/
static int	condition_match_service(const zbx_service_action_condition_t *condition,
		const zbx_service_update_t *update)
{
	zbx_uint64_t	serviceid;

	if (SUCCEED != is_uint64(condition->value, &serviceid) || serviceid != update->service->serviceid)
		return FAIL;

	return SUCCEED;
}

/******************************************************************************
 *                                                                            *
 * Function: condition_match_service_name                                     *
 *                                                                            *
 * Purpose: match service update by service name                              *
 *                                                                            *
 ******************************************************************************/
static int	condition_match_service_name(const zbx_service_action_condition_t *condition,
		const zbx_service_update_t *update)
{
	return  zbx_strmatch_condition(update->service->name, condition->value, condition->op);
}

/******************************************************************************
 *                                                                            *
 * Function: match_tags                                                       *
 *                                                                            *
 * Purpose: match tag/tag+value using the specified operator                  *
 *                                                                            *
 * Parameters: tags  - [IN] the tags to match                                 *
 *             name  - [IN] the target tag name                               *
 *             value - [IN] the target tag value (NULL if only tag name are   *
 *                          being matched                                     *
 *             op    - [IN] the matching operator (CONDITION_OPERATOR_*)      *
 *                                                                            *
 * Return value: SUCCEED - the tags matches                                   *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 * Comments: When matching tag+value the operator is using only to match      *
 *           value - the tag name will be always matched as 'equal'           *
 *                                                                            *
 ******************************************************************************/
static int	match_tags(const zbx_vector_ptr_t *tags, const char *name, const char *value, unsigned char op)
{
	int	i, ret, expected_ret;

	if (CONDITION_OPERATOR_EQUAL == op || CONDITION_OPERATOR_LIKE == op)
	{
		expected_ret = SUCCEED;
		ret = FAIL;
	}
	else
	{
		expected_ret = FAIL;
		ret = SUCCEED;
	}

	for (i = 0; i < tags->values_num; i++)
	{
		zbx_service_tag_t	*tag = (zbx_service_tag_t *)tags->values[i];

		if (NULL != value)
		{
			if (0 == strcmp(tag->name, name))
				ret = zbx_strmatch_condition(tag->value, value, op);
			else
				continue;
		}
		else
			ret = zbx_strmatch_condition(tag->name, name, op);

		if (expected_ret == ret)
			return ret;
	}

	return ret;
}

/******************************************************************************
 *                                                                            *
 * Function: condition_match_service_tag                                      *
 *                                                                            *
 * Purpose: match service update by service tag name                          *
 *                                                                            *
 ******************************************************************************/
static int	condition_match_service_tag(const zbx_service_action_condition_t *condition,
		const zbx_service_update_t *update)
{
	return match_tags(&update->service->tags, condition->value, NULL, condition->op);
}

/******************************************************************************
 *                                                                            *
 * Function: condition_match_service_tag_value                                *
 *                                                                            *
 * Purpose: match service update by service tag and its value                 *
 *                                                                            *
 ******************************************************************************/
static int	condition_match_service_tag_value(const zbx_service_action_condition_t *condition,
		const zbx_service_update_t *update)
{
	return match_tags(&update->service->tags, condition->value2, condition->value, condition->op);
}

/******************************************************************************
 *                                                                            *
 * Function: service_update_match_condition                                   *
 *                                                                            *
 * Purpose: match service update by the specified condition                   *
 *                                                                            *
 ******************************************************************************/
static const char	*service_update_match_condition(const zbx_service_update_t *update,
		const zbx_service_action_condition_t *condition)
{
	int	ret;

	switch (condition->conditiontype)
	{
		case CONDITION_TYPE_SERVICE:
			ret = condition_match_service(condition, update);
			break;
		case CONDITION_TYPE_SERVICE_NAME:
			ret = condition_match_service_name(condition, update);
			break;
		case CONDITION_TYPE_EVENT_TAG:
			ret = condition_match_service_tag(condition, update);
			break;
		case CONDITION_TYPE_EVENT_TAG_VALUE:
			ret = condition_match_service_tag_value(condition, update);
			break;
		default:
			THIS_SHOULD_NEVER_HAPPEN;
			ret = FAIL;
	}

	return SUCCEED == ret ? "1" : "0";
}

/******************************************************************************
 *                                                                            *
 * Function: service_update_match_action                                      *
 *                                                                            *
 * Purpose: match service update against the specified action                 *
 *                                                                            *
 ******************************************************************************/
static int	service_update_match_action(const zbx_service_update_t *update, const zbx_service_action_t *action)
{
	int		index;
	size_t		pos = 0, last_pos = 0, expr_alloc = 0, expr_offset = 0;
	char		*expr = NULL, error[256];
	const char	*value;
	zbx_token_t	token;
	zbx_uint64_t	id;
	double		res;

	if (0 == action->conditions.values_num)
		return SUCCEED;

	for (; SUCCEED == zbx_token_find(action->formula, (int)pos, &token, ZBX_TOKEN_SEARCH_FUNCTIONID); pos++)
	{
		switch (token.type)
		{
			case ZBX_TOKEN_OBJECTID:
				if (SUCCEED == is_uint64_n(action->formula + token.data.objectid.name.l,
						token.data.objectid.name.r - token.data.objectid.name.l + 1, &id))
				{
					zbx_strncpy_alloc(&expr, &expr_alloc, &expr_offset,
							action->formula + last_pos, token.loc.l - last_pos);

					if (FAIL != (index = zbx_vector_ptr_search(&action->conditions, &id,
							ZBX_DEFAULT_UINT64_PTR_COMPARE_FUNC)))
					{
						value = service_update_match_condition(update,
								action->conditions.values[index]);
					}
					else
						value = "0";

					zbx_strcpy_alloc(&expr, &expr_alloc, &expr_offset, value);

					last_pos = token.loc.r + 1;
				}
				pos = token.loc.r;
				break;
			case ZBX_TOKEN_MACRO:
			case ZBX_TOKEN_USER_MACRO:
			case ZBX_TOKEN_LLD_MACRO:
				pos = token.loc.r;
				break;
		}
	}

	zbx_strcpy_alloc(&expr, &expr_alloc, &expr_offset, action->formula + last_pos);

	if (FAIL == evaluate(&res, expr, error, sizeof(error), NULL))
	{
		zabbix_log(LOG_LEVEL_DEBUG, "cannot evaluate action \"" ZBX_FS_UI64 "\" formula \"%s\": %s",
			action->actionid, action->formula, error);
		res = 0;
	}

	zbx_free(expr);

	return SUCCEED == zbx_double_compare(res, 0) ? FAIL : SUCCEED;
}

/******************************************************************************
 *                                                                            *
 * Function: service_update_process_actions                                   *
 *                                                                            *
 * Purpose: match service update against service actions                      *
 *                                                                            *
 * Parameters: update    - [IN] the service update generated when service     *
 *                              state changes                                 *
 *             actions   - [IN] the service actions                           *
 *             actionids - [OUT] the matched action identifiers               *
 *                                                                            *
 ******************************************************************************/
void	service_update_process_actions(const zbx_service_update_t *update, zbx_hashset_t *actions,
		zbx_vector_uint64_t *actionids)
{
	zbx_hashset_iter_t	iter;
	zbx_service_action_t	*action;

	zabbix_log(LOG_LEVEL_DEBUG, "In %s() serviceid:" ZBX_FS_UI64, __func__, update->service->serviceid);

	zbx_hashset_iter_reset(actions, &iter);
	while (NULL != (action = (zbx_service_action_t *)zbx_hashset_iter_next(&iter)))
	{
		if (SUCCEED == service_update_match_action(update, action))
			zbx_vector_uint64_append(actionids, action->actionid);
	}

	zabbix_log(LOG_LEVEL_DEBUG, "End of %s() matched:%d", __func__, actionids->values_num);
}