Ad Widget

Collapse

Hosts List Page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thesubmitter
    Member
    • Feb 2012
    • 42

    #1

    Hosts List Page

    I was annoyed there was no direct way to access "Hosts Screens" for machines that have templated screens.

    I hacked the hosts.php page and came up with this. Works well. Stupid attachment limit wont let me upload the file, so its pasted below

    Code:
    <?php
    /*
    ** Zabbix
    ** Copyright (C) 2000-2012 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.
    **/
    
    require_once dirname(__FILE__).'/include/config.inc.php';
    require_once dirname(__FILE__).'/include/forms.inc.php';
    
    if (isset($_REQUEST['go']) && $_REQUEST['go'] == 'export' && isset($_REQUEST['hosts'])) {
    	$page['file'] = 'zbx_export_hosts.xml';
    	$page['type'] = detect_page_type(PAGE_TYPE_XML);
    
    	$EXPORT_DATA = true;
    }
    else {
    	$page['title'] = _('Hosts List');
    	$page['file'] = 'hosts-list.php';
    	$page['type'] = detect_page_type(PAGE_TYPE_HTML);
    	$page['hist_arg'] = array('groupid');
    
    	$EXPORT_DATA = false;
    }
    
    require_once dirname(__FILE__).'/include/page_header.php';
    
    // VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
    $fields = array(
    	'hosts' =>		array(T_ZBX_INT, O_OPT, P_SYS,		DB_ID,		null),
    	'groups' =>		array(T_ZBX_INT, O_OPT, P_SYS,		DB_ID,		null),
    	'hostids' =>		array(T_ZBX_INT, O_OPT, P_SYS,		DB_ID,		null),
    	'groupids' =>		array(T_ZBX_INT, O_OPT, P_SYS,		DB_ID,		null),
    	'applications' =>	array(T_ZBX_INT, O_OPT, P_SYS,		DB_ID,		null),
    	'groupid' =>		array(T_ZBX_INT, O_OPT, P_SYS,		DB_ID,		null),
    	'hostid' =>		array(T_ZBX_INT, O_OPT, P_SYS,		DB_ID,		'isset({form})&&({form}=="update")'),
    	'host' =>		array(T_ZBX_STR, O_OPT, null,		NOT_EMPTY,	'isset({save})', _('Host name')),
    	'visiblename' =>	array(T_ZBX_STR, O_OPT, null,		null,		'isset({save})'),
    	'proxy_hostid' =>	array(T_ZBX_INT, O_OPT, P_SYS,		DB_ID,		'isset({save})'),
    	'status' =>		array(T_ZBX_INT, O_OPT, null,		IN('0,1,3'),	'isset({save})'),
    	'newgroup' =>		array(T_ZBX_STR, O_OPT, null,		null,		null),
    	'interfaces' =>		array(T_ZBX_STR, O_OPT, null,		NOT_EMPTY,	'isset({save})', _('Agent or SNMP or JMX or IPMI interface')),
    	'mainInterfaces' =>	array(T_ZBX_INT, O_OPT, null,		DB_ID,		null),
    	'templates' =>		array(T_ZBX_STR, O_OPT, null,		NOT_EMPTY,	null),
    	'templates_rem' =>	array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'clear_templates' =>	array(T_ZBX_INT, O_OPT, null,		DB_ID,		null),
    	'ipmi_authtype' =>	array(T_ZBX_INT, O_OPT, null,		BETWEEN(-1, 6), null),
    	'ipmi_privilege' =>	array(T_ZBX_INT, O_OPT, null,		BETWEEN(0, 5),	null),
    	'ipmi_username' =>	array(T_ZBX_STR, O_OPT, null,		null,		null),
    	'ipmi_password' =>	array(T_ZBX_STR, O_OPT, null,		null,		null),
    	'mass_replace_tpls' =>	array(T_ZBX_STR, O_OPT, null,		null,		null),
    	'mass_clear_tpls' =>	array(T_ZBX_STR, O_OPT, null,		null,		null),
    	'inventory_mode' =>	array(T_ZBX_INT, O_OPT, null,
    		IN(HOST_INVENTORY_DISABLED.','.HOST_INVENTORY_MANUAL.','.HOST_INVENTORY_AUTOMATIC), null),
    	'host_inventory' =>	array(T_ZBX_STR, O_OPT, P_UNSET_EMPTY,	null,		null),
    	'macros_rem' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'macros' =>		array(T_ZBX_STR, O_OPT, P_SYS,		null,		null),
    	'macro_new' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		'isset({macro_add})'),
    	'value_new' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		'isset({macro_add})'),
    	'macro_add' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'massupdate' =>		array(T_ZBX_STR, O_OPT, P_SYS,		null,		null),
    	'visible' =>		array(T_ZBX_STR, O_OPT, null,		null,		null),
    	// actions
    	'go' =>			array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'add_to_group' =>	array(T_ZBX_INT, O_OPT, P_SYS|P_ACT,	DB_ID,		null),
    	'delete_from_group' =>	array(T_ZBX_INT, O_OPT, P_SYS|P_ACT,	DB_ID,		null),
    	'unlink' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'unlink_and_clear' =>	array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'save' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'masssave' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'clone' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'full_clone' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'delete' =>		array(T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,		null),
    	'cancel' =>		array(T_ZBX_STR, O_OPT, P_SYS,		null,		null),
    	'form' =>		array(T_ZBX_STR, O_OPT, P_SYS,		null,		null),
    	'form_refresh' =>	array(T_ZBX_STR, O_OPT, null,		null,		null),
    	// filter
    	'filter_set' =>		array(T_ZBX_STR, O_OPT, P_ACT,		null,		null),
    	'filter_host' =>	array(T_ZBX_STR, O_OPT, null,		null,		null),
    	'filter_ip' =>		array(T_ZBX_STR, O_OPT, null,		null,		null),
    	'filter_dns' =>		array(T_ZBX_STR, O_OPT, null,		null,		null),
    	'filter_port' =>	array(T_ZBX_STR, O_OPT, null,		null,		null),
    	// ajax
    	'favobj' =>		array(T_ZBX_STR, O_OPT, P_ACT,		null,		null),
    	'favref' =>		array(T_ZBX_STR, O_OPT, P_ACT,		NOT_EMPTY,	'isset({favobj})'),
    	'favstate' =>		array(T_ZBX_INT, O_OPT, P_ACT,		NOT_EMPTY,	'isset({favobj})&&("filter"=={favobj})')
    );
    check_fields($fields);
    validate_sort_and_sortorder('name', ZBX_SORT_UP);
    
    $_REQUEST['go'] = get_request('go', 'none');
    
    
    
    /*
     * Permissions
     */
    
    if (get_request('groupid', 0) > 0) {
    	$groupids = available_groups($_REQUEST['groupid'], 1);
    	if (empty($groupids)) {
    		access_deny();
    	}
    }
    if (get_request('hostid', 0) > 0) {
    	$hostids = available_hosts($_REQUEST['hostid'], 1);
    	if (empty($hostids)) {
    		access_deny();
    	}
    }
    
    
    
    /*
     * Ajax
     */
    if (isset($_REQUEST['favobj'])) {
    	if ('filter' == $_REQUEST['favobj']) {
    		CProfile::update('web.hosts.filter.state', $_REQUEST['favstate'], PROFILE_TYPE_INT);
    	}
    }
    
    if (PAGE_TYPE_JS == $page['type'] || PAGE_TYPE_HTML_BLOCK == $page['type']) {
    	require_once dirname(__FILE__).'/include/page_footer.php';
    	exit();
    }
    
    $hostids = get_request('hosts', array());
    
    
    /*
     * Filter
     */
    if (isset($_REQUEST['filter_set'])) {
    	$_REQUEST['filter_ip'] = get_request('filter_ip');
    	$_REQUEST['filter_dns'] = get_request('filter_dns');
    	$_REQUEST['filter_host'] = get_request('filter_host');
    	$_REQUEST['filter_port'] = get_request('filter_port');
    
    	CProfile::update('web.hosts.filter_ip', $_REQUEST['filter_ip'], PROFILE_TYPE_STR);
    	CProfile::update('web.hosts.filter_dns', $_REQUEST['filter_dns'], PROFILE_TYPE_STR);
    	CProfile::update('web.hosts.filter_host', $_REQUEST['filter_host'], PROFILE_TYPE_STR);
    	CProfile::update('web.hosts.filter_port', $_REQUEST['filter_port'], PROFILE_TYPE_STR);
    }
    else {
    	$_REQUEST['filter_ip'] = CProfile::get('web.hosts.filter_ip');
    	$_REQUEST['filter_dns'] = CProfile::get('web.hosts.filter_dns');
    	$_REQUEST['filter_host'] = CProfile::get('web.hosts.filter_host');
    	$_REQUEST['filter_port'] = CProfile::get('web.hosts.filter_port');
    }
    
    
    /*
     * Display
     */
    $hosts_wdgt = new CWidget();
    
    $pageFilter = new CPageFilter(array(
    	'groups' => array(
    		'real_hosts' => 1,
    		'editable' => false
    	),
    	'groupid' => get_request('groupid', null)
    ));
    
    $_REQUEST['groupid'] = $pageFilter->groupid;
    $_REQUEST['hostid'] = get_request('hostid', 0);
    
    if ($_REQUEST['go'] == 'massupdate' && isset($_REQUEST['hosts'])) {
    	$hosts_wdgt->addPageHeader(_('CONFIGURATION OF HOSTS'));
    
    	$data = array(
    		'hosts' => get_request('hosts', array()),
    		'visible' => get_request('visible', array()),
    		'mass_replace_tpls' => get_request('mass_replace_tpls'),
    		'mass_clear_tpls' => get_request('mass_clear_tpls'),
    		'groups' => get_request('groups', array()),
    		'newgroup' => get_request('newgroup', ''),
    		'status' => get_request('status', HOST_STATUS_MONITORED),
    		'proxy_hostid' => get_request('proxy_hostid', ''),
    		'ipmi_authtype' => get_request('ipmi_authtype', -1),
    		'ipmi_privilege' => get_request('ipmi_privilege', 2),
    		'ipmi_username' => get_request('ipmi_username', ''),
    		'ipmi_password' => get_request('ipmi_password', ''),
    		'inventory_mode' => get_request('inventory_mode', HOST_INVENTORY_DISABLED),
    		'host_inventory' => get_request('host_inventory', array()),
    		'templates' => get_request('templates', array())
    	);
    
    	// sort templates
    	natsort($data['templates']);
    
    	// get groups
    	$data['all_groups'] = API::HostGroup()->get(array(
    		'output' => API_OUTPUT_EXTEND,
    		'editable' => false
    	));
    	order_result($data['all_groups'], 'name');
    
    	// get proxies
    	$data['proxies'] = DBfetchArray(DBselect(
    		'SELECT h.hostid,h.host'.
    		' FROM hosts h'.
    		' WHERE h.status IN ('.HOST_STATUS_PROXY_ACTIVE.','.HOST_STATUS_PROXY_PASSIVE.')'.
    			' AND '.DBin_node('h.hostid').
    		' ORDER BY h.host'
    	));
    
    	// get inventories
    	if ($data['inventory_mode'] != HOST_INVENTORY_DISABLED) {
    		$data['inventories'] = getHostInventories();
    		$data['inventories'] = zbx_toHash($data['inventories'], 'db_field');
    	}
    
    //	$hostForm = new CView('configuration.host.massupdate', $data);
    //	$hosts_wdgt->addItem($hostForm->render());
    }
    elseif (isset($_REQUEST['form'])) {
    	$hosts_wdgt->addPageHeader(_('CONFIGURATION OF HOSTS'));
    
    	if ($hostid = get_request('hostid', 0)) {
    		$hosts_wdgt->addItem(get_header_host_table('', $_REQUEST['hostid']));
    		$hosts_wdgt->setRootClass('host-list');
    	}
    	$hosts_wdgt->setRootClass('host-list');
    
    	$hostForm = new CView('configuration.host.edit');
    	$hosts_wdgt->addItem($hostForm->render());
    	$hosts_wdgt->setRootClass('host-edit');
    }
    else {
    	$frmForm = new CForm();
    	$frmForm->cleanItems();
    	$buttons = new CDiv(array(
    		new CSubmit('form', _('Create host')),
    		new CButton('form', _('Import'), 'redirect("conf.import.php?rules_preset=host")')
    	));
    	$frmForm->addItem($buttons);
    	$frmForm->addItem(new CVar('groupid', $_REQUEST['groupid'], 'filter_groupid_id'));
    
    	$hosts_wdgt->addPageHeader(_('CONFIGURATION OF HOSTS'), $frmForm);
    
    	$frmGroup = new CForm('get');
    	$frmGroup->addItem(array(_('Group').SPACE, $pageFilter->getGroupsCB()));
    
    	$hosts_wdgt->addHeader(_('Hosts'), $frmGroup);
    	$hosts_wdgt->addHeaderRowNumber();
    	$hosts_wdgt->setRootClass('host-list');
    
    	// filter
    	$filter_table = new CTable('', 'filter');
    	$filter_table->addRow(array(
    		array(array(bold(_('Name')), SPACE._('like').': '), new CTextBox('filter_host', $_REQUEST['filter_host'], 20)),
    		array(array(bold(_('DNS')), SPACE._('like').': '), new CTextBox('filter_dns', $_REQUEST['filter_dns'], 20)),
    		array(array(bold(_('IP')), SPACE._('like').': '), new CTextBox('filter_ip', $_REQUEST['filter_ip'], 20)),
    		array(bold(_('Port').': '), new CTextBox('filter_port', $_REQUEST['filter_port'], 20))
    	));
    
    	$filter = new CButton('filter', _('Filter'), "javascript: create_var('zbx_filter', 'filter_set', '1', true);");
    	$filter->useJQueryStyle('main');
    
    	$reset = new CButton('reset', _('Reset'), "javascript: clearAllForm('zbx_filter');");
    	$reset->useJQueryStyle();
    
    	$div_buttons = new CDiv(array($filter, SPACE, $reset));
    	$div_buttons->setAttribute('style', 'padding: 4px 0;');
    
    	$filter_table->addRow(new CCol($div_buttons, 'center', 4));
    
    	$filter_form = new CForm('get');
    	$filter_form->setAttribute('name', 'zbx_filter');
    	$filter_form->setAttribute('id', 'zbx_filter');
    	$filter_form->addItem($filter_table);
    
    	$hosts_wdgt->addFlicker($filter_form, CProfile::get('web.hosts.filter.state', 0));
    
    	// table hosts
    	$form = new CForm();
    	$form->setName('hosts-list');
    
    	$table = new CTableInfo(_('No hosts defined.'));
    	$table->setHeader(array(
    		new CCheckBox('all_hosts', null, "checkAll('".$form->getName()."', 'all_hosts', 'hosts-list');"),
    		make_sorting_header(_('Name'), 'name'),
    		_('Custom Screen'),
    		_('Latest Data'),
    		_('Graphs'),
    		_('All Interface'),
    //		_('Templates'),
    //		make_sorting_header(_('Status'), 'status'),
    		_('Availability')
    	));
    
    	// get Hosts
    	$hosts = array();
    
    	$sortfield = getPageSortField('name');
    	$sortorder = getPageSortOrder();
    
    	if ($pageFilter->groupsSelected) {
    		$options = array(
    			'editable' => false,
    			'sortfield' => $sortfield,
    			'sortorder' => $sortorder,
    			'limit' => $config['search_limit'] + 1,
    			'search' => array(
    				'name' => (empty($_REQUEST['filter_host']) ? null : $_REQUEST['filter_host']),
    				'ip' => (empty($_REQUEST['filter_ip']) ? null : $_REQUEST['filter_ip']),
    				'dns' => (empty($_REQUEST['filter_dns']) ? null : $_REQUEST['filter_dns'])
    			),
    			'filter' => array(
    				'port' => (empty($_REQUEST['filter_port']) ? null : $_REQUEST['filter_port'])
    			)
    		);
    
    		if ($pageFilter->groupid > 0) {
    			$options['groupids'] = $pageFilter->groupid;
    		}
    
    		$hosts = API::Host()->get($options);
    	}
    	else {
    		$hosts = array();
    	}
    
    	// sorting && paging
    	order_result($hosts, $sortfield, $sortorder);
    	$paging = getPagingLine($hosts);
    
    	$hosts = API::Host()->get(array(
    		'hostids' => zbx_objectValues($hosts, 'hostid'),
    		'output' => API_OUTPUT_EXTEND,
    		'selectParentTemplates' => array('hostid', 'name'),
    		'selectInterfaces' => API_OUTPUT_EXTEND,
    		'selectItems' => API_OUTPUT_COUNT,
    		'selectDiscoveries' => API_OUTPUT_COUNT,
    		'selectTriggers' => API_OUTPUT_COUNT,
    		'selectGraphs' => API_OUTPUT_COUNT,
    		'selectApplications' => API_OUTPUT_COUNT
    	));
    	order_result($hosts, $sortfield, $sortorder);
    
    	// selecting linked templates to templates linked to hosts
    	$templateids = array();
    	foreach ($hosts as $host) {
    		$templateids = array_merge($templateids, zbx_objectValues($host['parentTemplates'], 'templateid'));
    	}
    	$templateids = array_unique($templateids);
    
    	$templates = API::Template()->get(array(
    		'templateids' => $templateids,
    		'selectParentTemplates' => array('hostid', 'name')
    	));
    	$templates = zbx_toHash($templates, 'templateid');
    
    	foreach ($hosts as $host) {
    		$interface = reset($host['interfaces']);
    
    		$applications = array(new CLink(_('Host Screen'), 'host_screen.php?groupid='.$_REQUEST['groupid'].'&hostid='.$host['hostid']),
    			' ('.$host['applications'].')');
    		$items = array(new CLink(_('Items'), 'latest.php?form_refresh=1&hostid='.$host['hostid']),
    			' ('.$host['items'].')');
    		$graphs = array(new CLink(_('Graphs'), 'charts.php?groupid='.$_REQUEST['groupid'].'&hostid='.$host['hostid']),
    			' ('.$host['graphs'].')');
    
    		$description = array();
    		if ($host['proxy_hostid']) {
    			$proxy = API::Proxy()->get(array(
    				'proxyids' => $host['proxy_hostid'],
    				'output' => API_OUTPUT_EXTEND
    			));
    			$proxy = reset($proxy);
    			$description[] = $proxy['host'].':';
    		}
    
    //		$description[] = new CLink($host['name'], 'hosts.php?form=update&hostid='.$host['hostid'].url_param('groupid'));
    		$description[] = $host['name'];
    
    		$hostIF = ($interface['useip'] == INTERFACE_USE_IP) ? $interface['ip'] : $interface['dns'];
    		$hostIF .= empty($interface['port']) ? '' : ': '.$interface['port'];
    
    		$status_script = null;
    
    //		$status = new CLink($status_caption, $status_url, $status_class, $status_script);
    
    		if (empty($host['parentTemplates'])) {
    			$hostTemplates = '-';
    		}
    		else {
    			$hostTemplates = array();
    			order_result($host['parentTemplates'], 'name');
    
    			foreach ($host['parentTemplates'] as $template) {
    				$caption = array();
    				$caption[] = new CLink($template['name'], 'templates.php?form=update&templateid='.$template['templateid'], 'unknown');
    
    				if (!empty($templates[$template['templateid']]['parentTemplates'])) {
    					order_result($templates[$template['templateid']]['parentTemplates'], 'name');
    
    					$caption[] = ' (';
    					foreach ($templates[$template['templateid']]['parentTemplates'] as $tpl) {
    						$caption[] = new CLink($tpl['name'],'templates.php?form=update&templateid='.$tpl['templateid'], 'unknown');
    						$caption[] = ', ';
    					}
    					array_pop($caption);
    
    					$caption[] = ')';
    				}
    
    				$hostTemplates[] = $caption;
    				$hostTemplates[] = ', ';
    			}
    
    			if (!empty($hostTemplates)) {
    				array_pop($hostTemplates);
    			}
    		}
    
    		$table->addRow(array(
    			new CCheckBox('hosts['.$host['hostid'].']', null, null, $host['hostid']),
    			$description,
    			$applications,
    			$items,
    			$graphs,
    			$hostIF,
    		//	new CCol($hostTemplates, 'wraptext'),
    //			$status,
    			getAvailabilityTable($host)
    		));
    	}
    
    	// footer
    
    	$form->addItem($table);
    	$hosts_wdgt->addItem($form);
    }
    
    $hosts_wdgt->show();
    
    require_once dirname(__FILE__).'/include/page_footer.php';
Working...