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

correlation.inc.php « include « php « frontends - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 280219b4ac38180e8b3eb09fa61081cf010a1023 (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
<?php
/*
** 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.
**/


/**
 * Convert a correlation condition operator to string.
 *
 * @param int $operator
 *
 * @return string
 */
function corrConditionOperatorToString($operator) {
	$operators = [
		CONDITION_OPERATOR_EQUAL => _('equals'),
		CONDITION_OPERATOR_NOT_EQUAL => _('does not equal'),
		CONDITION_OPERATOR_LIKE => _('contains'),
		CONDITION_OPERATOR_NOT_LIKE => _('does not contain')
	];

	return $operators[$operator];
}

/**
 * Returns correlation condition types or one type depending on input.
 *
 * @param int $type			Default: null. Returns all condition types.
 *
 * @return mixed			Returns condition type and it's string translation as array key => value pair.
 */
function corrConditionTypes($type = null) {
	$types = [
		ZBX_CORR_CONDITION_OLD_EVENT_TAG => _('Old event tag name'),
		ZBX_CORR_CONDITION_NEW_EVENT_TAG => _('New event tag name'),
		ZBX_CORR_CONDITION_NEW_EVENT_HOSTGROUP => _('New event host group'),
		ZBX_CORR_CONDITION_EVENT_TAG_PAIR => _('Event tag pair'),
		ZBX_CORR_CONDITION_OLD_EVENT_TAG_VALUE => _('Old event tag value'),
		ZBX_CORR_CONDITION_NEW_EVENT_TAG_VALUE => _('New event tag value')
	];

	return ($type === null) ? $types : $types[$type];
}

/**
 * Returns correlation operation types or one type depending on input.
 *
 * @param int $type			Default: null. Returns all operation types.
 *
 * @return mixed			Returns operation type and it's string translation as array key => value pair.
 */
function corrOperationTypes($type = null) {
	$types = [
		ZBX_CORR_OPERATION_CLOSE_OLD => _('Close old events'),
		ZBX_CORR_OPERATION_CLOSE_NEW => _('Close new event')
	];

	return ($type === null) ? $types : $types[$type];
}

/**
 * Return an array of operators supported by the given correlation condition.
 *
 * @param int $type						Correlation condition type.
 *
 * @return array						Returns array of supported operators.
 */
function getOperatorsByCorrConditionType($type) {
	switch ($type) {
		case ZBX_CORR_CONDITION_OLD_EVENT_TAG:
		case ZBX_CORR_CONDITION_NEW_EVENT_TAG:
		case ZBX_CORR_CONDITION_EVENT_TAG_PAIR:
			return [CONDITION_OPERATOR_EQUAL];

		case ZBX_CORR_CONDITION_OLD_EVENT_TAG_VALUE:
		case ZBX_CORR_CONDITION_NEW_EVENT_TAG_VALUE:
			return [CONDITION_OPERATOR_EQUAL, CONDITION_OPERATOR_NOT_EQUAL, CONDITION_OPERATOR_LIKE,
				CONDITION_OPERATOR_NOT_LIKE
			];

		case ZBX_CORR_CONDITION_NEW_EVENT_HOSTGROUP:
			return [CONDITION_OPERATOR_EQUAL, CONDITION_OPERATOR_NOT_EQUAL];
	}
}

/**
 * Correlation condition type "host group" contains IDs. IDs are collected and then validated. For other
 * condition types, values are returned as they are.
 *
 * @param array $correlations							An array of correlations.
 * @param array $correlations['filter']					An array containing arrays of correlation conditions.
 * @param array $correlations['filter']['conditions']	An array of correlation conditions.
 *
 * @return array										Returns an array of correlation condition string values.
 */
function corrConditionValueToString(array $correlations) {
	$result = [];

	$groupids = [];

	foreach ($correlations as $i => $correlation) {
		$result[$i] = [];

		foreach ($correlation['filter']['conditions'] as $j => $condition) {
			switch ($condition['type']) {
				case ZBX_CORR_CONDITION_OLD_EVENT_TAG:
				case ZBX_CORR_CONDITION_NEW_EVENT_TAG:
					$result[$i][$j] = ['tag' => $condition['tag']];
					break;

				case ZBX_CORR_CONDITION_NEW_EVENT_HOSTGROUP:
					$result[$i][$j] = ['group' => _('Unknown')];
					$groupids[$condition['groupid']] = true;
					break;

				case ZBX_CORR_CONDITION_EVENT_TAG_PAIR:
					$result[$i][$j] = ['oldtag' => $condition['oldtag'], 'newtag' => $condition['newtag']];
					break;

				case ZBX_CORR_CONDITION_OLD_EVENT_TAG_VALUE:
				case ZBX_CORR_CONDITION_NEW_EVENT_TAG_VALUE:
					$result[$i][$j] = ['tag' => $condition['tag'], 'value' => $condition['value']];
					break;
			}
		}
	}

	if ($groupids) {
		$groups = API::HostGroup()->get([
			'output' => ['name'],
			'groupids' => array_keys($groupids),
			'preservekeys' => true
		]);

		if ($groups) {
			foreach ($correlations as $i => $correlation) {
				foreach ($correlation['filter']['conditions'] as $j => $condition) {
					if ($condition['type'] == ZBX_CORR_CONDITION_NEW_EVENT_HOSTGROUP
							&& array_key_exists($condition['groupid'], $groups)) {
						$result[$i][$j] = ['group' => $groups[$condition['groupid']]['name']];
					}
				}
			}
		}
	}

	return $result;
}

/**
 * Return the HTML representation of correlation operation type.
 *
 * @param array $operation					An array of correlation operation data.
 * @param int   $operation['type']			Correlation operation type.
 *
 * @return string
 */
function getCorrOperationDescription(array $operation) {
	return corrOperationTypes($operation['type']);
}

/**
 * Returns the HTML representation of a correlation condition.
 *
 * @param array  $condition					Array of correlation condition data.
 * @param int	 $condition['type']			Condition type.
 * @param int	 $condition['operator']		Condition operator.
 * @param array  $values					Array of condition values.
 * @param string $values['tag']				Condition event tag.
 * @param string $values['group']			Condition host group name.
 * @param string $values['oldtag']			Condition event old tag.
 * @param string $values['newtag']			Condition event new tag.
 * @param string $values['value']			Condition event tag value.
 *
 * @return array
 */
function getCorrConditionDescription(array $condition, array $values) {
	$description = [];

	switch ($condition['type']) {
		case ZBX_CORR_CONDITION_OLD_EVENT_TAG:
			$description[] = _('Old event tag name').' '.corrConditionOperatorToString($condition['operator']).' ';
			$description[] = italic(CHtml::encode($values['tag']));
			break;

		case ZBX_CORR_CONDITION_NEW_EVENT_TAG:
			$description[] = _('New event tag name').' '.corrConditionOperatorToString($condition['operator']).' ';
			$description[] = italic(CHtml::encode($values['tag']));
			break;

		case ZBX_CORR_CONDITION_NEW_EVENT_HOSTGROUP:
			$description[] = _('New event host group').' '.corrConditionOperatorToString($condition['operator']).' ';
			$description[] = italic(CHtml::encode($values['group']));
			break;

		case ZBX_CORR_CONDITION_EVENT_TAG_PAIR:
			$description[] = _('Value of old event tag').' ';
			$description[] = italic(CHtml::encode($values['oldtag']));
			$description[] = ' '.corrConditionOperatorToString($condition['operator']).' '.
				_('value of new event tag').' ';
			$description[] = italic(CHtml::encode($values['newtag']));
			break;

		case ZBX_CORR_CONDITION_OLD_EVENT_TAG_VALUE:
			$description[] = _('Value of old event tag').' ';
			$description[] = italic(CHtml::encode($values['tag']));
			$description[] = ' '.corrConditionOperatorToString($condition['operator']).' ';
			$description[] = italic(CHtml::encode($values['value']));
			break;

		case ZBX_CORR_CONDITION_NEW_EVENT_TAG_VALUE:
			$description[] = _('Value of new event tag').' ';
			$description[] = italic(CHtml::encode($values['tag']));
			$description[] = ' '.corrConditionOperatorToString($condition['operator']).' ';
			$description[] = italic(CHtml::encode($values['value']));
			break;
	}

	return $description;
}