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

WidgetView.php « actions « problemhosts « widgets « ui - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63ff9ee994765ddba72dc386d37a129893d29793 (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
<?php declare(strict_types = 0);
/*
** 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.
**/


namespace Widgets\ProblemHosts\Actions;

use API,
	CArrayHelper,
	CControllerDashboardWidgetView,
	CControllerResponseData,
	CRoleHelper;

class WidgetView extends CControllerDashboardWidgetView {

	protected function doAction(): void {
		$filter_groupids = $this->fields_values['groupids'] ? getSubGroups($this->fields_values['groupids']) : null;
		$filter_hostids = $this->fields_values['hostids'] ?: null;
		$filter_problem = $this->fields_values['problem'] !== '' ? $this->fields_values['problem'] : null;
		$filter_severities = $this->fields_values['severities'] ?: range(TRIGGER_SEVERITY_NOT_CLASSIFIED,
			TRIGGER_SEVERITY_COUNT - 1
		);
		$filter_show_suppressed = $this->fields_values['show_suppressed'];
		$filter_ext_ack = $this->fields_values['ext_ack'];

		if ($this->fields_values['exclude_groupids']) {
			$exclude_groupids = getSubGroups($this->fields_values['exclude_groupids']);

			if ($filter_hostids === null) {
				// Get all groups if no selected groups defined.
				if ($filter_groupids === null) {
					$filter_groupids = array_keys(API::HostGroup()->get([
						'output' => [],
						'with_hosts' => true,
						'preservekeys' => true
					]));
				}

				$filter_groupids = array_diff($filter_groupids, $exclude_groupids);

				// Get available hosts.
				$filter_hostids = array_keys(API::Host()->get([
					'output' => [],
					'groupids' => $filter_groupids,
					'preservekeys' => true
				]));
			}

			$exclude_hostids = array_keys(API::Host()->get([
				'output' => [],
				'groupids' => $exclude_groupids,
				'preservekeys' => true
			]));

			$filter_hostids = array_diff($filter_hostids, $exclude_hostids);
		}

		// Get host groups.
		$groups = API::HostGroup()->get([
			'output' => ['groupid', 'name'],
			'groupids' => $filter_groupids,
			'hostids' => $filter_hostids,
			'with_monitored_hosts' => true,
			'preservekeys' => true
		]);

		foreach ($groups as $groupid => $group) {
			$groups[$groupid]['highest_severity'] = TRIGGER_SEVERITY_NOT_CLASSIFIED;
			$groups[$groupid]['hosts_total_count'] = 0;
			$groups[$groupid]['hosts_problematic_unack_count'] = 0;
			$groups[$groupid]['hosts_problematic_unack_list'] = [];

			if ($filter_ext_ack != EXTACK_OPTION_UNACK) {
				$groups[$groupid]['hosts_problematic_count'] = 0;
				$groups[$groupid]['hosts_problematic_list'] = [];
			}
		}

		// Get hosts.
		$hosts = API::Host()->get([
			'output' => ['hostid', 'name', 'maintenanceid', 'maintenance_status', 'maintenance_type'],
			'selectHostGroups' => ['groupid'],
			'groupids' => array_keys($groups),
			'hostids' => $filter_hostids,
			'filter' => [
				'maintenance_status' => null
			],
			'monitored_hosts' => true,
			'preservekeys' => true
		]);

		// Get triggers.
		$triggers = API::Trigger()->get([
			'output' => [],
			'selectHosts' => ['hostid'],
			'hostids' => array_keys($hosts),
			'groupids' => array_keys($groups),
			'filter' => [
				'value' => TRIGGER_VALUE_TRUE
			],
			'monitored' => true,
			'preservekeys' => true
		]);

		// Add default values for each host group and count hosts inside.
		foreach ($hosts as $host) {
			foreach ($host['hostgroups'] as $group) {
				if (array_key_exists($group['groupid'], $groups)) {
					$groups[$group['groupid']]['hosts_total_count']++;
				}
			}
		}

		// Get problems.
		$problems = API::Problem()->get([
			'output' => ['objectid', 'acknowledged', 'severity'],
			'groupids' => array_keys($groups),
			'hostids' => array_keys($hosts),
			'objectids' => array_keys($triggers),
			'search' => [
				'name' => $filter_problem
			],
			'severities' => $filter_severities,
			'evaltype' => $this->fields_values['evaltype'],
			'tags' => $this->fields_values['tags'],
			'acknowledged' => ($filter_ext_ack == EXTACK_OPTION_UNACK) ? false : null,
			'suppressed' => ($filter_show_suppressed == ZBX_PROBLEM_SUPPRESSED_FALSE) ? false : null
		]);

		$hosts_data = [];

		// Process problems.
		foreach ($problems as $problem) {
			foreach ($triggers[$problem['objectid']]['hosts'] as $trigger_host) {
				if (!array_key_exists($trigger_host['hostid'], $hosts)) {
					continue;
				}

				$host = $hosts[$trigger_host['hostid']];

				// Prepare hosts data for tables displayed in hintboxes.
				if (!array_key_exists($host['hostid'], $hosts_data)) {
					$hosts_data[$host['hostid']] = [
						'host' => $host['name'],
						'hostid' => $host['hostid'],
						'maintenanceid' => $host['maintenanceid'],
						'maintenance_status' => $host['maintenance_status'],
						'maintenance_type' => $host['maintenance_type'],
						'severities' => array_fill_keys($filter_severities, 0)
					];
				}

				// Count number of host problems per severity.
				$hosts_data[$host['hostid']]['severities'][$problem['severity']]++;

				// Propagate problem to all host groups in which host is added.
				foreach ($host['hostgroups'] as $group) {
					$groupid = $group['groupid'];

					if (!array_key_exists($groupid, $groups)) {
						continue;
					}

					// Searches for the highest severity set for filtered problems in particular host group.
					if ($problem['severity'] > $groups[$groupid]['highest_severity']) {
						$groups[$groupid]['highest_severity'] = $problem['severity'];
					}

					/**
					 * Counts:
					 *  - problematic hosts (hosts with events in 'problem' state);
					 *  - unacknowledged problematic hosts (hosts with unacknowledged events in 'problem' state).
					 *
					 * Creates a list of problematic hosts and unacknowledged problematic hosts for each host group.
					 *
					 * Each host need to be counted only one time in each host group.
					 * Host name is added for sorting.
					 */
					if ($filter_ext_ack != EXTACK_OPTION_UNACK
							&& !array_key_exists($host['hostid'], $groups[$groupid]['hosts_problematic_list'])) {
						$groups[$groupid]['hosts_problematic_list'][$host['hostid']]['name'] = $host['name'];
						$groups[$groupid]['hosts_problematic_count']++;
					}

					if ($problem['acknowledged'] == EVENT_NOT_ACKNOWLEDGED
							&& !array_key_exists($host['hostid'], $groups[$groupid]['hosts_problematic_unack_list'])) {
						$groups[$groupid]['hosts_problematic_unack_list'][$host['hostid']]['name'] = $host['name'];
						$groups[$groupid]['hosts_problematic_unack_count']++;
					}
				}
			}
		}

		// Sort results.
		foreach ($groups as $groupid => $group) {
			if ($group['hosts_total_count'] != 0) {
				CArrayHelper::sort($groups[$groupid]['hosts_problematic_unack_list'], ['name']);

				if ($filter_ext_ack != EXTACK_OPTION_UNACK) {
					CArrayHelper::sort($groups[$groupid]['hosts_problematic_list'], ['name']);
				}
			}
			else {
				// Unset groups without any monitored hosts.
				unset($groups[$groupid]);
			}
		}

		CArrayHelper::sort($groups, ['name']);

		// Pass results to view.
		$this->setResponse(new CControllerResponseData([
			'name' => $this->getInput('name', $this->widget->getDefaultName()),
			'filter' => [
				'hostids' => $this->fields_values['hostids'],
				'problem' => $this->fields_values['problem'],
				'severities' => $filter_severities,
				'show_suppressed' => $this->fields_values['show_suppressed'],
				'hide_empty_groups' => $this->fields_values['hide_empty_groups'],
				'ext_ack' => $this->fields_values['ext_ack']
			],
			'hosts_data' => $hosts_data,
			'groups' => $groups,
			'user' => [
				'debug_mode' => $this->getDebugMode()
			],
			'allowed_ui_problems' => $this->checkAccess(CRoleHelper::UI_MONITORING_PROBLEMS)
		]));
	}
}