1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22require_once dirname(__FILE__).'/js/configuration.host.list.js.php';
23
24$widget = (new CWidget())
25	->setTitle(_('Hosts'))
26	->setControls(new CList([
27		(new CForm('get'))
28			->cleanItems()
29			->setAttribute('aria-label', _('Main filter'))
30			->addItem((new CList())
31				->addItem([
32					new CLabel(_('Group'), 'groupid'),
33					(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
34					$data['pageFilter']->getGroupsCB()
35				])
36			),
37		(new CTag('nav', true, (new CList())
38			->addItem(new CRedirectButton(_('Create host'), (new CUrl('hosts.php'))
39				->setArgument('form', 'create')
40				->getUrl()
41			))
42			->addItem(
43				(new CButton('form', _('Import')))
44					->onClick('redirect("conf.import.php?rules_preset=host")')
45					->removeId()
46			)
47		))
48			->setAttribute('aria-label', _('Content controls'))
49	]));
50
51// filter
52$filter = new CFilter(new CUrl('hosts.php'));
53
54$filter
55	->setProfile($data['profileIdx'])
56	->setActiveTab($data['active_tab'])
57	->addFilterTab(_('Filter'), [
58		(new CFormList())
59			->addRow(_('Name'),
60				(new CTextBox('filter_host', $data['filter']['host']))
61					->setWidth(ZBX_TEXTAREA_MEDIUM_WIDTH)
62					->setAttribute('autofocus', 'autofocus')
63			)
64			->addRow(
65				(new CLabel(_('Templates'), 'filter_templates__ms')),
66				(new CMultiSelect([
67					'name' => 'filter_templates[]',
68					'object_name' => 'templates',
69					'data' => $data['filter']['templates'],
70					'popup' => [
71						'parameters' => [
72							'srctbl' => 'templates',
73							'srcfld1' => 'hostid',
74							'srcfld2' => 'host',
75							'dstfrm' => $filter->getName(),
76							'dstfld1' => 'filter_templates_'
77						]
78					]
79				]))->setWidth(ZBX_TEXTAREA_MEDIUM_WIDTH)
80			)
81			->addRow(_('Monitored by'),
82				(new CRadioButtonList('filter_monitored_by', (int) $data['filter']['monitored_by']))
83					->addValue(_('Any'), ZBX_MONITORED_BY_ANY)
84					->addValue(_('Server'), ZBX_MONITORED_BY_SERVER)
85					->addValue(_('Proxy'), ZBX_MONITORED_BY_PROXY)
86					->setModern(true)
87			)
88			->addRow(
89				(new CLabel(_('Proxy'), 'filter_proxyids__ms')),
90				(new CMultiSelect([
91					'name' => 'filter_proxyids[]',
92					'object_name' => 'proxies',
93					'data' => $data['proxies_ms'],
94					'disabled' => ($data['filter']['monitored_by'] != ZBX_MONITORED_BY_PROXY),
95					'popup' => [
96						'parameters' => [
97							'srctbl' => 'proxies',
98							'srcfld1' => 'proxyid',
99							'srcfld2' => 'host',
100							'dstfrm' => 'zbx_filter',
101							'dstfld1' => 'filter_proxyids_'
102						]
103					]
104				]))->setWidth(ZBX_TEXTAREA_MEDIUM_WIDTH)
105			),
106		(new CFormList())
107			->addRow(_('DNS'),
108				(new CTextBox('filter_dns', $data['filter']['dns']))->setWidth(ZBX_TEXTAREA_MEDIUM_WIDTH)
109			)
110			->addRow(_('IP'),
111				(new CTextBox('filter_ip', $data['filter']['ip']))->setWidth(ZBX_TEXTAREA_MEDIUM_WIDTH)
112			)
113			->addRow(_('Port'),
114				(new CTextBox('filter_port', $data['filter']['port']))->setWidth(ZBX_TEXTAREA_MEDIUM_WIDTH)
115			)
116	]);
117
118$widget->addItem($filter);
119
120// table hosts
121$form = (new CForm())->setName('hosts');
122
123$table = (new CTableInfo())
124	->setHeader([
125		(new CColHeader(
126			(new CCheckBox('all_hosts'))->onClick("checkAll('".$form->getName()."', 'all_hosts', 'hosts');")
127		))->addClass(ZBX_STYLE_CELL_WIDTH),
128		make_sorting_header(_('Name'), 'name', $data['sortField'], $data['sortOrder'], 'hosts.php'),
129		_('Applications'),
130		_('Items'),
131		_('Triggers'),
132		_('Graphs'),
133		_('Discovery'),
134		_('Web'),
135		_('Interface'),
136		_('Templates'),
137		make_sorting_header(_('Status'), 'status', $data['sortField'], $data['sortOrder'], 'hosts.php'),
138		_('Availability'),
139		_('Agent encryption'),
140		_('Info')
141	]);
142
143$current_time = time();
144
145foreach ($data['hosts'] as $host) {
146	// Select an interface from the list with highest priority.
147	$interface = null;
148	foreach ([INTERFACE_TYPE_AGENT, INTERFACE_TYPE_SNMP, INTERFACE_TYPE_JMX, INTERFACE_TYPE_IPMI] as $interface_type) {
149		$host_interfaces = array_filter($host['interfaces'], function($host_interface) use($interface_type) {
150			return $host_interface['type'] == $interface_type;
151		});
152		if ($host_interfaces) {
153			$interface = reset($host_interfaces);
154			break;
155		}
156	}
157
158	$description = [];
159
160	if ($host['proxy_hostid'] != 0) {
161		$description[] = $data['proxies'][$host['proxy_hostid']]['host'];
162		$description[] = NAME_DELIMITER;
163	}
164	if ($host['discoveryRule']) {
165		$description[] = (new CLink(
166			$host['discoveryRule']['name'], 'host_prototypes.php?parent_discoveryid='.$host['discoveryRule']['itemid']
167		))
168			->addClass(ZBX_STYLE_LINK_ALT)
169			->addClass(ZBX_STYLE_ORANGE);
170		$description[] = NAME_DELIMITER;
171	}
172
173	$description[] = new CLink(CHtml::encode($host['name']),
174		'hosts.php?form=update&hostid='.$host['hostid'].url_param('groupid')
175	);
176
177	$maintenance_icon = false;
178	$hostInterface = ($interface['useip'] == INTERFACE_USE_IP) ? $interface['ip'] : $interface['dns'];
179	$hostInterface .= empty($interface['port']) ? '' : NAME_DELIMITER.$interface['port'];
180
181	if ($host['status'] == HOST_STATUS_MONITORED) {
182		if ($host['maintenance_status'] == HOST_MAINTENANCE_STATUS_ON) {
183			if (array_key_exists($host['maintenanceid'], $data['maintenances'])) {
184				$maintenance = $data['maintenances'][$host['maintenanceid']];
185				$maintenance_icon = makeMaintenanceIcon($host['maintenance_type'], $maintenance['name'],
186					$maintenance['description']
187				);
188			}
189			else {
190				$maintenance_icon = makeMaintenanceIcon($host['maintenance_type'], _('Inaccessible maintenance'), '');
191			}
192		}
193
194		$statusCaption = _('Enabled');
195		$statusClass = ZBX_STYLE_GREEN;
196		$confirm_message = _('Disable host?');
197		$statusUrl = 'hosts.php?hosts[]='.$host['hostid'].'&action=host.massdisable'.url_param('groupid');
198	}
199	else {
200		$statusCaption = _('Disabled');
201		$statusUrl = 'hosts.php?hosts[]='.$host['hostid'].'&action=host.massenable'.url_param('groupid');
202		$confirm_message = _('Enable host?');
203		$statusClass = ZBX_STYLE_RED;
204	}
205
206	$status = (new CLink($statusCaption, $statusUrl))
207		->addClass(ZBX_STYLE_LINK_ACTION)
208		->addClass($statusClass)
209		->addConfirmation($confirm_message)
210		->addSID();
211
212	if ($maintenance_icon) {
213		$status = [$maintenance_icon, $status];
214	}
215	elseif (count($data['maintenances'])) {
216		$status->addClass(ZBX_STYLE_ICON_NONE);
217	}
218
219	order_result($host['parentTemplates'], 'name');
220
221	$hostTemplates = [];
222	$i = 0;
223
224	foreach ($host['parentTemplates'] as $template) {
225		$i++;
226
227		if ($i > $data['config']['max_in_table']) {
228			$hostTemplates[] = ' &hellip;';
229
230			break;
231		}
232
233		if (array_key_exists($template['templateid'], $data['writable_templates'])) {
234			$caption = [
235				(new CLink(
236					CHtml::encode($template['name']), 'templates.php?form=update&templateid='.$template['templateid']
237				))
238					->addClass(ZBX_STYLE_LINK_ALT)
239					->addClass(ZBX_STYLE_GREY)
240			];
241		}
242		else {
243			$caption = [
244				(new CSpan(CHtml::encode($template['name'])))->addClass(ZBX_STYLE_GREY)
245			];
246		}
247
248		$parent_templates = $data['templates'][$template['templateid']]['parentTemplates'];
249
250		if ($parent_templates) {
251			order_result($parent_templates, 'name');
252			$caption[] = ' (';
253
254			foreach ($parent_templates as $parent_template) {
255				if (array_key_exists($parent_template['templateid'], $data['writable_templates'])) {
256					$caption[] = (new CLink(CHtml::encode($parent_template['name']),
257						'templates.php?form=update&templateid='.$parent_template['templateid']
258					))
259						->addClass(ZBX_STYLE_LINK_ALT)
260						->addClass(ZBX_STYLE_GREY);
261				}
262				else {
263					$caption[] = (new CSpan(CHtml::encode($parent_template['name'])))->addClass(ZBX_STYLE_GREY);
264				}
265				$caption[] = ', ';
266			}
267			array_pop($caption);
268
269			$caption[] = ')';
270		}
271
272		if ($hostTemplates) {
273			$hostTemplates[] = ', ';
274		}
275
276		$hostTemplates[] = $caption;
277	}
278
279	$info_icons = [];
280	if ($host['flags'] == ZBX_FLAG_DISCOVERY_CREATED && $host['hostDiscovery']['ts_delete'] != 0) {
281		$info_icons[] = getHostLifetimeIndicator($current_time, $host['hostDiscovery']['ts_delete']);
282	}
283
284	if ($host['tls_connect'] == HOST_ENCRYPTION_NONE
285			&& ($host['tls_accept'] & HOST_ENCRYPTION_NONE) == HOST_ENCRYPTION_NONE
286			&& ($host['tls_accept'] & HOST_ENCRYPTION_PSK) != HOST_ENCRYPTION_PSK
287			&& ($host['tls_accept'] & HOST_ENCRYPTION_CERTIFICATE) != HOST_ENCRYPTION_CERTIFICATE) {
288		$encryption = (new CDiv((new CSpan(_('None')))->addClass(ZBX_STYLE_STATUS_GREEN)))
289			->addClass(ZBX_STYLE_STATUS_CONTAINER);
290	}
291	else {
292		// Incoming encryption.
293		if ($host['tls_connect'] == HOST_ENCRYPTION_NONE) {
294			$in_encryption = (new CSpan(_('None')))->addClass(ZBX_STYLE_STATUS_GREEN);
295		}
296		elseif ($host['tls_connect'] == HOST_ENCRYPTION_PSK) {
297			$in_encryption = (new CSpan(_('PSK')))->addClass(ZBX_STYLE_STATUS_GREEN);
298		}
299		else {
300			$in_encryption = (new CSpan(_('CERT')))->addClass(ZBX_STYLE_STATUS_GREEN);
301		}
302
303		// Outgoing encryption.
304		$out_encryption = [];
305		if (($host['tls_accept'] & HOST_ENCRYPTION_NONE) == HOST_ENCRYPTION_NONE) {
306			$out_encryption[] = (new CSpan(_('None')))->addClass(ZBX_STYLE_STATUS_GREEN);
307		}
308		else {
309			$out_encryption[] = (new CSpan(_('None')))->addClass(ZBX_STYLE_STATUS_GREY);
310		}
311
312		if (($host['tls_accept'] & HOST_ENCRYPTION_PSK) == HOST_ENCRYPTION_PSK) {
313			$out_encryption[] = (new CSpan(_('PSK')))->addClass(ZBX_STYLE_STATUS_GREEN);
314		}
315		else {
316			$out_encryption[] = (new CSpan(_('PSK')))->addClass(ZBX_STYLE_STATUS_GREY);
317		}
318
319		if (($host['tls_accept'] & HOST_ENCRYPTION_CERTIFICATE) == HOST_ENCRYPTION_CERTIFICATE) {
320			$out_encryption[] = (new CSpan(_('CERT')))->addClass(ZBX_STYLE_STATUS_GREEN);
321		}
322		else {
323			$out_encryption[] = (new CSpan(_('CERT')))->addClass(ZBX_STYLE_STATUS_GREY);
324		}
325
326		$encryption = (new CDiv([$in_encryption, ' ', $out_encryption]))->addClass(ZBX_STYLE_STATUS_CONTAINER);
327	}
328
329	$table->addRow([
330		new CCheckBox('hosts['.$host['hostid'].']', $host['hostid']),
331		(new CCol($description))->addClass(ZBX_STYLE_NOWRAP),
332		[
333			new CLink(_('Applications'), 'applications.php?groupid='.$data['groupId'].'&hostid='.$host['hostid']),
334			CViewHelper::showNum($host['applications'])
335		],
336		[
337			new CLink(_('Items'), 'items.php?filter_set=1&hostid='.$host['hostid']),
338			CViewHelper::showNum($host['items'])
339		],
340		[
341			new CLink(_('Triggers'), 'triggers.php?groupid='.$data['groupId'].'&hostid='.$host['hostid']),
342			CViewHelper::showNum($host['triggers'])
343		],
344		[
345			new CLink(_('Graphs'), 'graphs.php?groupid='.$data['groupId'].'&hostid='.$host['hostid']),
346			CViewHelper::showNum($host['graphs'])
347		],
348		[
349			new CLink(_('Discovery'), 'host_discovery.php?hostid='.$host['hostid']),
350			CViewHelper::showNum($host['discoveries'])
351		],
352		[
353			new CLink(_('Web'), 'httpconf.php?&hostid='.$host['hostid']),
354			CViewHelper::showNum($host['httpTests'])
355		],
356		$hostInterface,
357		$hostTemplates,
358		$status,
359		getHostAvailabilityTable($host),
360		$encryption,
361		makeInformationList($info_icons)
362	]);
363}
364
365$form->addItem([
366	$table,
367	$data['paging'],
368	new CActionButtonList('action', 'hosts',
369		[
370			'host.massenable' => ['name' => _('Enable'), 'confirm' => _('Enable selected hosts?')],
371			'host.massdisable' => ['name' => _('Disable'), 'confirm' => _('Disable selected hosts?')],
372			'host.export' => ['name' => _('Export'), 'redirect' =>
373				(new CUrl('zabbix.php'))
374					->setArgument('action', 'export.hosts.xml')
375					->setArgument('backurl', (new CUrl('hosts.php'))
376						->setArgument('groupid', $data['pageFilter']->groupid)
377						->setArgument('page', getPageNumber())
378						->getUrl())
379					->getUrl()
380			],
381			'host.massupdateform' => ['name' => _('Mass update')],
382			'host.massdelete' => ['name' => _('Delete'), 'confirm' => _('Delete selected hosts?')]
383		]
384	)
385]);
386
387$widget->addItem($form);
388
389return $widget;
390