--- ./include/views/js/common.macros.js.php.orig 2014-07-17 15:49:46.000000000 +0200 +++ ./include/views/js/common.macros.js.php 2014-08-16 19:57:43.000000000 +0200 @@ -20,6 +20,7 @@ 'use strict'; var rowTemplate = new Template(jQuery('#macroRow').html()); + var macroFields = jQuery('#macrosFormList:input'); function addMacroRow() { if (addMacroRow.macro_count === void(0)) { @@ -53,5 +54,22 @@ return confirm( + ' ' + removedCount + ' ' + +'?'); } }); - }); + + jQuery('div.jqueryinputset input[name=macro_view]').click(function() { + switch (jQuery(this).val()) { + case '': + macroFields.prop('disabled', true); + jQuery('#hostMacros').show(); + jQuery('#inheritedMacros').hide(); + break; + case '': + macroFields.prop('disabled', false); + // fixme: dirty hack to enforce table-row over item-list + jQuery('#inheritedMacros').css('display', 'table-row'); + jQuery('#hostMacros').hide(); + jQuery('#inheritedMacros').show(); + break; + } + }); +}); --- ./include/views/configuration.host.edit.php.orig 2014-07-17 15:49:46.000000000 +0200 +++ ./include/views/configuration.host.edit.php 2014-08-16 20:11:51.000000000 +0200 @@ -56,6 +56,44 @@ $frm_title .= SPACE.' ['.$dbHost['host'].']'; $original_templates = $dbHost['parentTemplates']; $original_templates = zbx_toHash($original_templates, 'templateid'); + $inheritedMacros = $originalTemplateIds = $hostIds = array(); + + $globalMacros = API::UserMacro()->get(array( + 'globalmacro' => true, + 'output' => API_OUTPUT_EXTEND + )); + + foreach ($globalMacros as $macro) { + $inheritedMacros[$macro['macro']]['globalValue'] = $macro['value']; + } + + foreach ($original_templates as $tnum => $tpl) { + $originalTemplateIds[] = $tpl['templateid']; + } + + $templateMacros = getMacrosRecursively($originalTemplateIds); + foreach ($templateMacros as $macro) $hostIds[] = $macro['hostid']; + + $macroHosts = API::Host()->get(array( + 'hostids' => array_unique($hostIds), + 'templated_hosts' => true, + 'output' => array('name') + )); + $macroHosts = zbx_toHash($macroHosts, 'hostid'); + + foreach ($templateMacros as $macro) { + $inheritedMacros[$macro['macro']]['templateId'] = $macro['hostid']; + $inheritedMacros[$macro['macro']]['templateValue'] = $macro['value']; + $inheritedMacros[$macro['macro']]['templateName'] = $macroHosts[$macro['hostid']]['name']; + } + + foreach ($inheritedMacros as $macro => $values) { + if (!array_key_exists('globalValue', $values)) $values['globalValue'] = null; + if (!array_key_exists('templateValue', $values)) $values['templateValue'] = null; + if (!array_key_exists('templateName', $values)) $values['templateName'] = null; + $inheritedMacros[$macro] = $values; + } + ksort($inheritedMacros); if (isset($_REQUEST['mainInterfaces'][INTERFACE_TYPE_AGENT])) { $mainAgentId = $_REQUEST['mainInterfaces'][INTERFACE_TYPE_AGENT]; @@ -749,8 +787,13 @@ $macros = array(array('macro' => '', 'value' => '')); } +if (empty($inheritedMacros)) { + $inheritedMacros = null; +} + $macrosView = new CView('common.macros', array( 'macros' => $macros, + 'inheritedMacros' => $inheritedMacros, 'readonly' => $isDiscovered )); $divTabs->addTab('macroTab', _('Macros'), $macrosView->render()); --- ./include/views/configuration.template.edit.php.orig 2014-07-17 15:49:46.000000000 +0200 +++ ./include/views/configuration.template.edit.php 2014-08-16 20:16:33.000000000 +0200 @@ -36,6 +36,45 @@ if ($templateid > 0) { $frm_title .= SPACE.' ['.$this->data['dbTemplate']['name'].']'; + $templateIds = $inheritedMacros = $hostIds = array(); + + foreach ($this->data['original_templates'] as $tnum => $tpl) { + $templateIds[] = $tpl; + } + + $globalMacros = API::UserMacro()->get(array( + 'globalmacro' => true, + 'output' => API_OUTPUT_EXTEND + )); + + foreach ($globalMacros as $macro) { + $inheritedMacros[$macro['macro']]['globalValue'] = $macro['value']; + } + + $templateMacros = getMacrosRecursively($templateIds); + foreach ($templateMacros as $macro) $hostIds[] = $macro['hostid']; + + $macroHosts = API::Host()->get(array( + 'hostids' => array_unique($hostIds), + 'templated_hosts' => true, + 'output' => array('name') + )); + $macroHosts = zbx_toHash($macroHosts, 'hostid'); + + foreach ($templateMacros as $macro) { + $inheritedMacros[$macro['macro']]['templateId'] = $macro['hostid']; + $inheritedMacros[$macro['macro']]['templateValue'] = $macro['value']; + $inheritedMacros[$macro['macro']]['templateName'] = $macroHosts[$macro['hostid']]['name']; + } + + foreach ($inheritedMacros as $macro => $values) { + if (!array_key_exists('globalValue', $values)) $values['globalValue'] = null; + if (!array_key_exists('templateValue', $values)) $values['templateValue'] = null; + if (!array_key_exists('templateName', $values)) $values['templateName'] = null; + $inheritedMacros[$macro] = $values; + } + ksort($inheritedMacros); + } $frmHost = new CForm(); $frmHost->setName('tpl_for'); @@ -442,8 +481,12 @@ if (empty($macros)) { $macros = array(array('macro' => '', 'value' => '')); } +if (empty($inheritedMacros)) { + $inheritedMacros = null; +} $macrosView = new CView('common.macros', array( - 'macros' => $macros + 'macros' => $macros, + 'inheritedMacros' => $inheritedMacros )); $divTabs->addTab('macroTab', _('Macros'), $macrosView->render()); --- ./include/views/common.macros.php.orig 2014-07-17 15:49:46.000000000 +0200 +++ ./include/views/common.macros.php 2014-08-16 19:52:44.000000000 +0200 @@ -34,7 +34,7 @@ $macrosTable = new CTable(SPACE, 'formElementTable'); $macrosTable->setAttribute('id', 'tbl_macros'); -$macrosTable->addRow(array(_('Macro'), SPACE, _('Value'), SPACE)); +$macrosTable->addRow(array(_('Macro'), SPACE, _('Host value'), SPACE)); // fields foreach ($macros as $i => $macro) { @@ -59,10 +59,51 @@ $row = array($text1, $span, $text2, $deleteButtonCell); $macrosTable->addRow($row, 'form_row'); + $hostMacroValues[$macro['macro']] = $macro['value']; +} + +if (!empty($this->data['inheritedMacros'])) { + $inheritedMacrosTable = new CTable(SPACE, 'formElementTable'); + $inheritedMacrosTable->setAttribute('id', 'tbl_inheritedMacros'); + $inheritedMacrosTable->addRow(array(_('Macro'), SPACE, _('Global value'), SPACE, _('Template value'))); + + foreach ($this->data['inheritedMacros'] as $macro => $value) { + $text1 = new CTextBox(null, $macro, 21, 'yes', 255); + $text2 = new CTextBox(null, $value['globalValue'], 21, 'yes', 255); + $text2->setAttribute('placeholder', '-'); + $text3 = new CTextBox(null, $value['templateValue'], 21, 'yes', 255); + $text3->setAttribute('placeholder', '-'); + if (array_key_exists($macro, $hostMacroValues)) { + $text1->setAttribute('title', _('Host macro').SPACE.RARR.SPACE.$hostMacroValues[$macro]); + $text1->setAttribute('style','text-decoration:line-through'); + $text2->setAttribute('placeholder', SPACE); + $text2->setAttribute('style','text-decoration:line-through'); + $text2->setAttribute('title', _('Host macro').SPACE.RARR.SPACE.$hostMacroValues[$macro]); + $text3->setAttribute('placeholder', SPACE); + $text3->setAttribute('style','text-decoration:line-through'); + $text3->setAttribute('title', _('Host macro').SPACE.RARR.SPACE.$hostMacroValues[$macro]); + } + if (!is_null($value['templateName'])) { + $templateLink = new CLink($value['templateName'], 'templates.php?form=update&templateid='.$value['templateId']); + $text3 = array($text3, ' ← ', $templateLink); + } + $span1 = new CSpan(RARR); + $span1->addStyle('vertical-align:top;'); + + $row = array($text1, $span1, $text2, $span1, $text3); + $inheritedMacrosTable->addRow($row, 'form_row'); + } } // buttons if (!$readonly) { + $macroViewRadioButton = array( + new CRadioButton('macro_view', 'host', null, 'macro_view_radio_host', true), + new CLabel(_('Host macros'), 'macro_view_radio_host'), + new CRadioButton('macro_view', 'inherited', null, 'macro_view_radio_inherited', false), + new CLabel(_('Inherited macros'), 'macro_view_radio_inherited') + ); + $addButton = new CButton('macro_add', _('Add'), null, 'link_menu'); $buttonColumn = new CCol($addButton); $buttonColumn->setAttribute('colspan', 5); @@ -76,6 +117,10 @@ // form list $macrosFormList = new CFormList('macrosFormList'); -$macrosFormList->addRow($macrosTable); +if (isset($inheritedMacrosTable)) { + $macrosFormList->addRow(new CDiv($macroViewRadioButton, 'jqueryinputset')); + $macrosFormList->addRow(null, $inheritedMacrosTable, true, 'inheritedMacros'); +} +$macrosFormList->addRow(null, $macrosTable, false, 'hostMacros'); return $macrosFormList; --- ./include/func.inc.php.orig 2014-07-17 15:49:47.000000000 +0200 +++ ./include/func.inc.php 2014-08-16 19:52:44.000000000 +0200 @@ -2871,3 +2871,30 @@ return $items; } + +function getMacrosRecursively($hostIds) { + $macros = API::UserMacro()->get(array( + 'hostids' => $hostIds, + 'output' => API_OUTPUT_EXTEND + )); + $macros = order_macros($macros, 'hostid', ZBX_SORT_DOWN); + + $templates = API::Template()->get(array( + 'templateids' => $hostIds, + 'selectParentTemplates' => array('templateid'), + 'output' => array('templateid') + )); + $templates = zbx_toHash($templates, 'templateid'); + + foreach ($templates as $template) { + $parents = isset($template['parentTemplates'])?$template['parentTemplates']:array(); + foreach($parents as $parent) $parentIds[] = $parent['templateid']; + } + + if (!isset($parentIds) || is_null($parentIds)) { + return $macros; + } else { + $parentMacros = getMacrosRecursively($parentIds); + return array_merge($parentMacros, $macros); + } +}