diff --git a/hostinventories.php b/hostinventories.php index f623ec4..4f135a8 100644 --- a/hostinventories.php +++ b/hostinventories.php @@ -23,9 +23,36 @@ require_once dirname(__FILE__).'/include/config.inc.php'; require_once dirname(__FILE__).'/include/hosts.inc.php'; require_once dirname(__FILE__).'/include/forms.inc.php'; -$page['title'] = _('Host inventory'); -$page['file'] = 'hostinventories.php'; -$page['hist_arg'] = array('groupid', 'hostid'); +if (isset($_REQUEST['csv_export'])) { + $CSV_EXPORT = true; + $csvRows = array(); + $columnheader = array(); + + foreach (getHostInventories() as $if) { + $allInventoryFields[] = $if['db_field']; + $columnheader[] = $if['title']; + } +//Add "Host Name" column header + array_unshift($columnheader,"Host Name"); + + + $csvRows[] = $columnheader; + + $page['type'] = detect_page_type(PAGE_TYPE_CSV); + $page['file'] = 'zbx_inventory_export.csv'; + + require_once dirname(__FILE__).'/include/func.inc.php'; + + +} +else { + $CSV_EXPORT = false; + + $page['title'] = _('Host inventory'); + $page['file'] = 'hostinventories.php'; + $page['hist_arg'] = array('groupid', 'hostid'); +} + require_once dirname(__FILE__).'/include/page_header.php'; @@ -259,7 +286,53 @@ else { } } - $data['paging'] = getPagingLine($data['hosts'], $sortOrder); + + + if ($CSV_EXPORT) { + $csvOptions = array( + 'output' => array( 'name', 'status'), + 'selectInventory' => $allInventoryFields, + 'withInventory' => true, + 'selectGroups' => API_OUTPUT_EXTEND, + //'groupids' => getRequest('groupid', null) + ); + + if ($data['pageFilter']->groupid > 0) { + $csvOptions['groupids'] = $data['pageFilter']->groupid; + } + + $data['hosts'] = API::Host()->get($csvOptions); + + foreach($data['hosts'] as $num => $host) { + // remove host id... + array_shift($host['inventory']); + //Add "Host Name" to csv export first column + array_unshift($host['inventory'],$host['name']); + $csvRows[] = $host['inventory']; + + + if(!empty($data['filterField']) && !empty($data['filterFieldValue'])) { + // must we filter exactly or using a substring (both are case insensitive) + $match = $data['filterExact'] + ? zbx_strtolower($data['hosts'][$num]['inventory'][$data['filterField']]) === zbx_strtolower($data['filterFieldValue']) + : zbx_strpos( + zbx_strtolower($data['hosts'][$num]['inventory'][$data['filterField']]), + zbx_strtolower($data['filterFieldValue']) + ) !== false; + if (!$match) { + array_pop($csvRows); + } + } + } + + print(zbx_toCSV($csvRows)); + exit(); + } + + + + +$data['paging'] = getPagingLine($data['hosts'], $sortOrder); $hostinventoriesView = new CView('inventory.host.list', $data); $hostinventoriesView->render(); diff --git a/include/views/inventory.host.list.php b/include/views/inventory.host.list.php index 565678a..4dd7c73 100644 --- a/include/views/inventory.host.list.php +++ b/include/views/inventory.host.list.php @@ -23,6 +23,7 @@ $hostInventoryWidget = new CWidget(); $rForm = new CForm('get'); $rForm->addItem(array(_('Group'), SPACE, $this->data['pageFilter']->getGroupsCB())); +$rForm->addItem(new CSubmit('csv_export', _('Export to CSV'))); $hostInventoryWidget->addPageHeader(_('HOST INVENTORY'), SPACE); $hostInventoryWidget->addHeader(_('Hosts'), $rForm);