File: CControllerLatest.php

package info (click to toggle)
zabbix 1%3A5.0.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 135,196 kB
  • sloc: ansic: 250,674; sql: 209,763; php: 195,264; xml: 167,194; javascript: 46,671; sh: 5,339; makefile: 1,723; java: 1,337; cpp: 620; perl: 41
file content (340 lines) | stat: -rw-r--r-- 9,945 bytes parent folder | download
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
<?php
/*
** 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.
**/


/**
 * Base controller for the "Latest data" page and the "Latest data" asynchronous refresh page.
 */
abstract class CControllerLatest extends CController {

	/**
	 * Prepare the latest data based on the given filter and sorting options.
	 *
	 * @param array  $filter                       Item filter options.
	 * @param array  $filter['groupids']           Filter items by host groups.
	 * @param array  $filter['hostids']            Filter items by hosts.
	 * @param string $filter['application']        Filter items by application.
	 * @param string $filter['select']             Filter items by name.
	 * @param int    $filter['show_without_data']  Include items with empty history.
	 * @param string $sort_field                   Sorting field.
	 * @param string $sort_order                   Sorting order.
	 *
	 * @return array
	 */
	protected function prepareData(array $filter, $sort_field, $sort_order) {
		$config = select_config();

		$multiselect_hostgroup_data = [];
		$multiselect_host_data = [];

		// Sorting options for hosts, applications and items.

		$host_sort_options = [
			'field' => 'name',
			'order' => ($sort_field === 'host') ? $sort_order : 'ASC'
		];
		$application_sort_options = [
			'field' => 'name',
			'order' => ($sort_field === 'name') ? $sort_order : 'ASC'
		];
		$item_sort_options = [
			'field' => 'name',
			'order' => ($sort_field === 'name') ? $sort_order : 'ASC'
		];

		// Select groups for subsequent selection of hosts, applications and items.

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

			if ($groups) {
				$subgroup_names = [];

				foreach ($groups as $group) {
					$subgroup_names[] = $group['name'].'/';

					$multiselect_hostgroup_data[] = [
						'id' => $group['groupid'],
						'name' => $group['name']
					];
				}

				$groups += API::HostGroup()->get([
					'output' => ['groupid'],
					'search' => ['name' => $subgroup_names],
					'startSearch' => true,
					'searchByAny' => true,
					'preservekeys' => true
				]);
			}

			$groupids = array_keys($groups);
		}
		else {
			$groupids = null;
		}

		// Select hosts for subsequent selection of applications and items.

		$hosts = API::Host()->get([
			'output' => ['hostid', 'name', 'status'],
			'groupids' => $groupids,
			'hostids' => $filter['hostids'] ? $filter['hostids'] : null,
			'monitored_hosts' => true,
			'preservekeys' => true
		]);

		CArrayHelper::sort($hosts, [$host_sort_options]);
		$hostids = array_keys($hosts);
		$hostids_index = array_flip($hostids);

		$applications = [];

		$select_hosts = [];
		$select_items = [];

		foreach ($hosts as $hostid => $host) {
			if ($filter['application'] !== '') {
				$host_applications = API::Application()->get([
					'output' => ['applicationid', 'name'],
					'hostids' => [$hostid],
					'search' => ['name' => $filter['application']],
					'preservekeys' => true
				]);

				$host_applicationids = array_keys($host_applications);

				$applications += $host_applications;
			}
			else {
				$host_applicationids = null;
			}

			$host_items = API::Item()->get([
				'output' => ['itemid', 'hostid', 'value_type'],
				'hostids' => [$hostid],
				'applicationids' => $host_applicationids,
				'webitems' => true,
				'filter' => [
					'status' => [ITEM_STATUS_ACTIVE]
				],
				'search' => ($filter['select'] === '') ? null : [
					'name' => $filter['select']
				],
				'preservekeys' => true
			]);

			$select_hosts[$hostid] = true;

			$select_items += $filter['show_without_data']
				? $host_items
				: Manager::History()->getItemsHavingValues($host_items, ZBX_HISTORY_PERIOD);

			if (count($select_items) > $config['search_limit']) {
				break;
			}
		}

		if ($select_items) {
			$items = API::Item()->get([
				'output' => ['itemid', 'type', 'hostid', 'name', 'key_', 'delay', 'history', 'trends', 'status',
					'value_type', 'units', 'valuemapid', 'description', 'state', 'error'
				],
				'selectApplications' => ['applicationid'],
				'itemids' => array_keys($select_items),
				'webitems' => true,
				'preservekeys' => true
			]);

			if ($filter['application'] === '') {
				$applications = API::Application()->get([
					'output' => ['applicationid', 'name'],
					'hostids' => array_keys($select_hosts),
					'preservekeys' => true
				]);
			}

			CArrayHelper::sort($applications, [$application_sort_options]);
			$applicationids = array_keys($applications);
			$applicationids_index = array_flip($applicationids);

			$applications_size = [];
			$items_grouped = [];

			foreach ($items as $itemid => $item) {
				if (!array_key_exists($item['hostid'], $applications_size)) {
					$applications_size[$item['hostid']] = [];
				}

				$item_applicationids = $item['applications']
					? array_column($item['applications'], 'applicationid')
					: [0];

				foreach ($item_applicationids as $applicationid) {
					if ($applicationid != 0 && !array_key_exists($applicationid, $applications)) {
						continue;
					}

					$items_grouped[$item['hostid']][$applicationid][$itemid] = $item;

					if (array_key_exists($applicationid, $applications_size[$item['hostid']])) {
						$applications_size[$item['hostid']][$applicationid]++;
					}
					else {
						$applications_size[$item['hostid']][$applicationid] = 1;
					}
				}
			}

			$applications_index = [];
			$items = [];
			$rows = [];

			uksort($items_grouped, function($hostid_1, $hostid_2) use ($hostids_index) {
				return ($hostids_index[$hostid_1] <=> $hostids_index[$hostid_2]);
			});

			foreach ($items_grouped as $hostid => $host_items_grouped) {
				uksort($host_items_grouped,
					function($id_1, $id_2) use ($applicationids_index, $application_sort_options) {
						if ($id_1 == 0 || $id_2 == 0) {
							return bccomp($id_1, $id_2) * (($application_sort_options['order'] === 'ASC') ? -1 : 1);
						}

						return ($applicationids_index[$id_1] <=> $applicationids_index[$id_2]);
					}
				);

				foreach ($host_items_grouped as $applicationid => $application_items) {
					CArrayHelper::sort($application_items, [$item_sort_options]);

					$applications_index[$hostid][$applicationid] = [
						'start' => count($rows)
					];

					foreach ($application_items as $itemid => $item) {
						unset($item['applications']);

						$applications_index[$hostid][$applicationid]['end'] = count($rows);
						$items[$itemid] = $item;
						$rows[] = [
							'itemid' => $itemid,
							'applicationid' => $applicationid
						];

						if (count($rows) > $config['search_limit']) {
							break 3;
						}
					}
				}
			}
		}
		else {
			$rows = [];
			$hosts = [];
			$applications = [];
			$applications_size = [];
			$applications_index = [];
			$items = [];
		}

		if ($filter['hostids']) {
			$filter_hosts = API::Host()->get([
				'output' => ['hostid', 'name'],
				'hostids' => $filter['hostids']
			]);

			foreach ($filter_hosts as $host) {
				$multiselect_host_data[] = [
					'id' => $host['hostid'],
					'name' => $host['name']
				];
			}
		}

		return [
			'rows' => $rows,
			'hosts' => $hosts,
			'applications' => $applications,
			'applications_size' => $applications_size,
			'applications_index' => $applications_index,
			'items' => $items,
			'multiselect_hostgroup_data' => $multiselect_hostgroup_data,
			'multiselect_host_data' => $multiselect_host_data
		];
	}

	/**
	 * Extend previously prepared data.
	 *
	 * @param array $prepared_data      Data returned by prepareData method.
	 * @param int   $show_without_data  Include items with empty history.
	 */
	protected function extendData(array &$prepared_data, $show_without_data) {
		$items = array_intersect_key($prepared_data['items'],
			array_flip(array_column($prepared_data['rows'], 'itemid'))
		);

		// Resolve macros.

		$items = CMacrosResolverHelper::resolveItemKeys($items);
		$items = CMacrosResolverHelper::resolveItemNames($items);
		$items = CMacrosResolverHelper::resolveTimeUnitMacros($items, ['delay', 'history', 'trends']);

		$history = Manager::History()->getLastValues($items, 2, ZBX_HISTORY_PERIOD);

		$prepared_data['items'] = $items;
		$prepared_data['history'] = $history;
	}

	/**
	 * Add collapsed data from user profile.
	 *
	 * @param array $prepared_data  Data returned by prepareData method.
	 */
	protected function addCollapsedDataFromProfile(array &$prepared_data) {
		$collapsed_index = [];
		$collapsed_all = true;

		foreach ($prepared_data['rows'] as $row) {
			$hostid = $prepared_data['items'][$row['itemid']]['hostid'];
			$applicationid = $row['applicationid'];

			if (array_key_exists($hostid, $collapsed_index)
					&& array_key_exists($applicationid, $collapsed_index[$hostid])) {
				continue;
			}

			$collapsed = $applicationid
				? (CProfile::get('web.latest.toggle', null, $applicationid) !== null)
				: (CProfile::get('web.latest.toggle_other', null, $hostid) !== null);

			$collapsed_index[$hostid][$applicationid] = $collapsed;
			$collapsed_all = $collapsed_all && $collapsed;
		}

		$prepared_data['collapsed_index'] = $collapsed_index;
		$prepared_data['collapsed_all'] = $collapsed_all;
	}
}