本文整理汇总了PHP中API::UserMacro方法的典型用法代码示例。如果您正苦于以下问题:PHP API::UserMacro方法的具体用法?PHP API::UserMacro怎么用?PHP API::UserMacro使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类API
的用法示例。
在下文中一共展示了API::UserMacro方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_macro
function get_macro($hostid, $macro_name, $force = false)
{
$macros = API::UserMacro()->get(array("output" => API_OUTPUT_EXTEND, "hostids" => array($hostid), "nopermissions" => $force));
foreach ($macros as $macro) {
if ($macro['macro'] === $macro_name) {
return $macro['value'];
}
}
return null;
}
示例2: update
/**
* Update Host.
*
* @param array $hosts multidimensional array with Hosts data
* @param string $hosts ['host'] Host name.
* @param int $hosts ['port'] Port. OPTIONAL
* @param int $hosts ['status'] Host Status. OPTIONAL
* @param int $hosts ['useip'] Use IP. OPTIONAL
* @param string $hosts ['dns'] DNS. OPTIONAL
* @param string $hosts ['ip'] IP. OPTIONAL
* @param int $hosts ['proxy_hostid'] Proxy Host ID. OPTIONAL
* @param int $hosts ['ipmi_authtype'] IPMI authentication type. OPTIONAL
* @param int $hosts ['ipmi_privilege'] IPMI privilege. OPTIONAL
* @param string $hosts ['ipmi_username'] IPMI username. OPTIONAL
* @param string $hosts ['ipmi_password'] IPMI password. OPTIONAL
* @param string $hosts ['groups'] groups
*
* @return boolean
*/
public function update($hosts)
{
$hosts = zbx_toArray($hosts);
$hostids = zbx_objectValues($hosts, 'hostid');
$this->checkInput($hosts, __FUNCTION__);
// fetch fields required to update host inventory
$inventories = array();
foreach ($hosts as $host) {
if (isset($host['inventory'])) {
$inventory = $host['inventory'];
$inventory['hostid'] = $host['hostid'];
$inventories[] = $inventory;
}
}
$inventories = $this->extendObjects('host_inventory', $inventories, array('inventory_mode'));
$inventories = zbx_toHash($inventories, 'hostid');
$macros = array();
foreach ($hosts as &$host) {
if (isset($host['macros'])) {
$macros[$host['hostid']] = $host['macros'];
unset($host['macros']);
}
}
unset($host);
if ($macros) {
API::UserMacro()->replaceMacros($macros);
}
foreach ($hosts as $host) {
// extend host inventory with the required data
if (isset($host['inventory']) && $host['inventory']) {
$inventory = $inventories[$host['hostid']];
// if no host inventory record exists in the DB, it's disabled
if (!isset($inventory['inventory_mode'])) {
$inventory['inventory_mode'] = HOST_INVENTORY_DISABLED;
}
$host['inventory'] = $inventory;
}
API::HostInterface()->replaceHostInterfaces($host);
unset($host['interfaces']);
$data = $host;
$data['hosts'] = $host;
$result = $this->massUpdate($data);
if (!$result) {
self::exception(ZBX_API_ERROR_INTERNAL, _('Host update failed.'));
}
}
return array('hostids' => $hostids);
}
示例3: update
/**
* Update Host.
*
* @param array $hosts multidimensional array with Hosts data
* @param string $hosts ['host'] Host name.
* @param int $hosts ['port'] Port. OPTIONAL
* @param int $hosts ['status'] Host Status. OPTIONAL
* @param int $hosts ['useip'] Use IP. OPTIONAL
* @param string $hosts ['dns'] DNS. OPTIONAL
* @param string $hosts ['ip'] IP. OPTIONAL
* @param int $hosts ['proxy_hostid'] Proxy Host ID. OPTIONAL
* @param int $hosts ['ipmi_authtype'] IPMI authentication type. OPTIONAL
* @param int $hosts ['ipmi_privilege'] IPMI privilege. OPTIONAL
* @param string $hosts ['ipmi_username'] IPMI username. OPTIONAL
* @param string $hosts ['ipmi_password'] IPMI password. OPTIONAL
* @param string $hosts ['groups'] groups
*
* @return boolean
*/
public function update($hosts)
{
$hosts = zbx_toArray($hosts);
$hostids = zbx_objectValues($hosts, 'hostid');
$this->checkInput($hosts, __FUNCTION__);
$macros = array();
foreach ($hosts as $host) {
API::HostInterface()->replaceHostInterfaces($host);
unset($host['interfaces']);
if (isset($host['macros'])) {
$macros[$host['hostid']] = $host['macros'];
unset($host['macros']);
}
$data = $host;
$data['hosts'] = $host;
$result = $this->massUpdate($data);
if (!$result) {
self::exception(ZBX_API_ERROR_INTERNAL, _('Host update failed.'));
}
}
if ($macros) {
API::UserMacro()->replaceMacros($macros);
}
return array('hostids' => $hostids);
}
示例4: update
/**
* Update Template
*
* @param array $templates multidimensional array with templates data
* @return boolean
*/
public function update($templates)
{
$templates = zbx_toArray($templates);
$templateids = zbx_objectValues($templates, 'templateid');
$updTemplates = $this->get(array('templateids' => $templateids, 'editable' => 1, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1));
foreach ($templates as $template) {
if (!isset($updTemplates[$template['templateid']])) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
}
}
$macros = array();
foreach ($templates as $template) {
// if visible name is not given or empty it should be set to host name
if (!isset($template['name']) || isset($template['name']) && zbx_empty(trim($template['name']))) {
if (isset($template['host'])) {
$template['name'] = $template['host'];
}
}
$tplTmp = $template;
$template['templates_link'] = isset($template['templates']) ? $template['templates'] : null;
if (isset($template['macros'])) {
$macros[$template['templateid']] = $template['macros'];
unset($template['macros']);
}
unset($template['templates']);
unset($template['templateid']);
unset($tplTmp['templates']);
$template['templates'] = array($tplTmp);
$result = $this->massUpdate($template);
if (!$result) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Failed to update template'));
}
}
if ($macros) {
API::UserMacro()->replaceMacros($macros);
}
return array('templateids' => $templateids);
}
示例5: calcTriggers
protected function calcTriggers()
{
$this->triggers = array();
if ($this->m_showTriggers != 1) {
return;
}
$max = 3;
$cnt = 0;
foreach ($this->items as $inum => $item) {
$db_triggers = DBselect('SELECT DISTINCT h.host,tr.description,tr.triggerid,tr.expression,tr.priority,tr.value' . ' FROM triggers tr,functions f,items i,hosts h' . ' WHERE tr.triggerid=f.triggerid' . " AND f.function IN ('last','min','avg','max')" . ' AND tr.status=' . TRIGGER_STATUS_ENABLED . ' AND i.itemid=f.itemid' . ' AND h.hostid=i.hostid' . ' AND f.itemid=' . $item['itemid'] . ' ORDER BY tr.priority');
while (($trigger = DBfetch($db_triggers)) && $cnt < $max) {
$db_fnc_cnt = DBselect('SELECT COUNT(*) AS cnt FROM functions f WHERE f.triggerid=' . $trigger['triggerid']);
$fnc_cnt = DBfetch($db_fnc_cnt);
if ($fnc_cnt['cnt'] != 1) {
continue;
}
$trigger = API::UserMacro()->resolveTrigger($trigger);
if (!preg_match('/^\\{([0-9]+)\\}\\s*?([\\<\\>\\=]{1})\\s*?([\\-0-9\\.]+)([TGMKsmhdw]?)$/', $trigger['expression'], $arr)) {
continue;
}
$val = convert($arr[3] . $arr[4]);
$minY = $this->m_minY[$this->items[$inum]['axisside']];
$maxY = $this->m_maxY[$this->items[$inum]['axisside']];
$this->triggers[] = array('skipdraw' => $val <= $minY || $val >= $maxY, 'y' => $this->sizeY - ($val - $minY) / ($maxY - $minY) * $this->sizeY + $this->shiftY, 'color' => getSeverityColor($trigger['priority']), 'description' => _('Trigger') . ': ' . CTriggerHelper::expandDescription($trigger), 'constant' => '[' . $arr[2] . ' ' . $arr[3] . $arr[4] . ']');
++$cnt;
}
}
}
示例6: addRelatedObjects
//.........这里部分代码省略.........
$relationMap = $this->createRelationMap($items, 'hostid', 'itemid');
$items = $this->unsetExtraFields($items, array('hostid', 'itemid'), $options['selectDiscoveries']);
$result = $relationMap->mapMany($result, $items, 'discoveries', $options['limitSelects']);
} else {
$items = API::DiscoveryRule()->get(array('hostids' => $hostids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
$items = zbx_toHash($items, 'hostid');
foreach ($result as $hostid => $host) {
$result[$hostid]['discoveries'] = isset($items[$hostid]) ? $items[$hostid]['rowscount'] : 0;
}
}
}
// adding triggers
if ($options['selectTriggers'] !== null) {
if ($options['selectTriggers'] != API_OUTPUT_COUNT) {
// discovered items
$res = DBselect('SELECT i.hostid,f.triggerid' . ' FROM items i,functions f' . ' WHERE ' . dbConditionInt('i.hostid', $hostids) . ' AND i.itemid=f.itemid');
$relationMap = new CRelationMap();
while ($relation = DBfetch($res)) {
$relationMap->addRelation($relation['hostid'], $relation['triggerid']);
}
$triggers = API::Trigger()->get(array('output' => $options['selectTriggers'], 'triggerids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
if (!is_null($options['limitSelects'])) {
order_result($triggers, 'description');
}
$result = $relationMap->mapMany($result, $triggers, 'triggers', $options['limitSelects']);
} else {
$triggers = API::Trigger()->get(array('hostids' => $hostids, 'countOutput' => true, 'groupCount' => true));
$triggers = zbx_toHash($triggers, 'hostid');
foreach ($result as $hostid => $host) {
$result[$hostid]['triggers'] = isset($triggers[$hostid]) ? $triggers[$hostid]['rowscount'] : 0;
}
}
}
// adding graphs
if ($options['selectGraphs'] !== null) {
if ($options['selectGraphs'] != API_OUTPUT_COUNT) {
// discovered items
$res = DBselect('SELECT i.hostid,gi.graphid' . ' FROM items i,graphs_items gi' . ' WHERE ' . dbConditionInt('i.hostid', $hostids) . ' AND i.itemid=gi.itemid');
$relationMap = new CRelationMap();
while ($relation = DBfetch($res)) {
$relationMap->addRelation($relation['hostid'], $relation['graphid']);
}
$graphs = API::Graph()->get(array('output' => $options['selectGraphs'], 'graphids' => $relationMap->getRelatedIds(), 'preservekeys' => true));
if (!is_null($options['limitSelects'])) {
order_result($graphs, 'name');
}
$result = $relationMap->mapMany($result, $graphs, 'graphs', $options['limitSelects']);
} else {
$graphs = API::Graph()->get(array('hostids' => $hostids, 'countOutput' => true, 'groupCount' => true));
$graphs = zbx_toHash($graphs, 'hostid');
foreach ($result as $hostid => $host) {
$result[$hostid]['graphs'] = isset($graphs[$hostid]) ? $graphs[$hostid]['rowscount'] : 0;
}
}
}
// adding http tests
if ($options['selectHttpTests'] !== null) {
if ($options['selectHttpTests'] != API_OUTPUT_COUNT) {
$httpTests = API::HttpTest()->get(array('output' => $this->outputExtend($options['selectHttpTests'], array('hostid', 'httptestid')), 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true));
if (!is_null($options['limitSelects'])) {
order_result($httpTests, 'name');
}
$relationMap = $this->createRelationMap($httpTests, 'hostid', 'httptestid');
$httpTests = $this->unsetExtraFields($httpTests, array('hostid', 'httptestid'), $options['selectHttpTests']);
$result = $relationMap->mapMany($result, $httpTests, 'httpTests', $options['limitSelects']);
} else {
$httpTests = API::HttpTest()->get(array('hostids' => $hostids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
$httpTests = zbx_toHash($httpTests, 'hostid');
foreach ($result as $hostId => $host) {
$result[$hostId]['httpTests'] = isset($httpTests[$hostId]) ? $httpTests[$hostId]['rowscount'] : 0;
}
}
}
// adding applications
if ($options['selectApplications'] !== null) {
if ($options['selectApplications'] != API_OUTPUT_COUNT) {
$applications = API::Application()->get(array('output' => $this->outputExtend($options['selectApplications'], array('hostid', 'applicationid')), 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true));
if (!is_null($options['limitSelects'])) {
order_result($applications, 'name');
}
$relationMap = $this->createRelationMap($applications, 'hostid', 'applicationid');
$applications = $this->unsetExtraFields($applications, array('hostid', 'applicationid'), $options['selectApplications']);
$result = $relationMap->mapMany($result, $applications, 'applications', $options['limitSelects']);
} else {
$applications = API::Application()->get(array('output' => $options['selectApplications'], 'hostids' => $hostids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true));
$applications = zbx_toHash($applications, 'hostid');
foreach ($result as $hostid => $host) {
$result[$hostid]['applications'] = isset($applications[$hostid]) ? $applications[$hostid]['rowscount'] : 0;
}
}
}
// adding macros
if ($options['selectMacros'] !== null && $options['selectMacros'] != API_OUTPUT_COUNT) {
$macros = API::UserMacro()->get(array('output' => $this->outputExtend($options['selectMacros'], array('hostid', 'hostmacroid')), 'hostids' => $hostids, 'preservekeys' => true));
$relationMap = $this->createRelationMap($macros, 'hostid', 'hostmacroid');
$macros = $this->unsetExtraFields($macros, array('hostid', 'hostmacroid'), $options['selectMacros']);
$result = $relationMap->mapMany($result, $macros, 'macros', $options['limitSelects']);
}
return $result;
}
示例7: getUserMacros
/**
* Get macros with values.
*
* @param array $data
* @param array $data[<id>] any identificator
* @param array $data['hostids'] the list of host ids; [<hostid1>, ...]
* @param array $data['macros'] the list of user macros to resolve, ['<usermacro1>' => null, ...]
*
* @return array
*/
protected function getUserMacros(array $data)
{
// User macros.
$hostids = [];
foreach ($data as $element) {
foreach ($element['hostids'] as $hostid) {
$hostids[$hostid] = true;
}
}
if (!$hostids) {
return $data;
}
/*
* @var array $host_templates
* @var array $host_templates[<hostid>] array of templates
*/
$host_templates = [];
/*
* @var array $host_macros
* @var array $host_macros[<hostid>]
* @var array $host_macros[<hostid>][<macro>] macro base without curly braces
* @var string $host_macros[<hostid>][<macro>]['value'] base macro value (without context); can be null
* @var array $host_macros[<hostid>][<macro>]['contexts'] context values; ['<context1>' => '<value1>', ...]
*/
$host_macros = [];
$user_macro_parser = new CUserMacroParser();
do {
$db_hosts = API::Host()->get(['hostids' => array_keys($hostids), 'templated_hosts' => true, 'output' => ['hostid'], 'selectParentTemplates' => ['templateid'], 'selectMacros' => ['macro', 'value']]);
$hostids = [];
foreach ($db_hosts as $db_host) {
$host_templates[$db_host['hostid']] = zbx_objectValues($db_host['parentTemplates'], 'templateid');
foreach ($db_host['macros'] as $db_macro) {
if ($user_macro_parser->parse($db_macro['macro']) != CParser::PARSE_SUCCESS) {
continue;
}
$macro = $user_macro_parser->getMacro();
$context = $user_macro_parser->getContext();
if (!array_key_exists($db_host['hostid'], $host_macros)) {
$host_macros[$db_host['hostid']] = [];
}
if (!array_key_exists($macro, $host_macros[$db_host['hostid']])) {
$host_macros[$db_host['hostid']][$macro] = ['value' => null, 'contexts' => []];
}
if ($context === null) {
$host_macros[$db_host['hostid']][$macro]['value'] = $db_macro['value'];
} else {
$host_macros[$db_host['hostid']][$macro]['contexts'][$context] = $db_macro['value'];
}
}
}
foreach ($db_hosts as $db_host) {
// Only unprocessed templates will be populated.
foreach ($host_templates[$db_host['hostid']] as $templateid) {
if (!array_key_exists($templateid, $host_templates)) {
$hostids[$templateid] = true;
}
}
}
} while ($hostids);
$all_macros_resolved = true;
$user_macro_parser = new CUserMacroParser();
foreach ($data as &$element) {
$hostids = [];
foreach ($element['hostids'] as $hostid) {
$hostids[$hostid] = true;
}
$hostids = array_keys($hostids);
natsort($hostids);
foreach ($element['macros'] as $usermacro => &$value) {
if ($user_macro_parser->parse($usermacro) == CParser::PARSE_SUCCESS) {
$value = $this->getHostUserMacros($hostids, $user_macro_parser->getMacro(), $user_macro_parser->getContext(), $host_templates, $host_macros);
if ($value['value'] === null) {
$all_macros_resolved = false;
}
} else {
// This macro cannot be resolved.
$value = ['value' => $usermacro, 'value_default' => null];
}
}
unset($value);
}
unset($element);
if (!$all_macros_resolved) {
// Global macros.
$db_global_macros = API::UserMacro()->get(['output' => ['macro', 'value'], 'globalmacro' => true]);
/*
* @var array $global_macros
* @var array $global_macros[<macro>] macro base without curly braces
* @var string $global_macros[<macro>]['value'] base macro value (without context); can be null
* @var array $global_macros[<macro>]['contexts'] context values; ['<context1>' => '<value1>', ...]
//.........这里部分代码省略.........
示例8: show_error_message
show_error_message(_('Cannot update macros'));
}
}
/*
* Display
*/
$form = new CForm();
$form->cleanItems();
$cmbConf = new CComboBox('configDropDown', 'adm.macros.php', 'redirect(this.options[this.selectedIndex].value);');
$cmbConf->addItems(array('adm.gui.php' => _('GUI'), 'adm.housekeeper.php' => _('Housekeeping'), 'adm.images.php' => _('Images'), 'adm.iconmapping.php' => _('Icon mapping'), 'adm.regexps.php' => _('Regular expressions'), 'adm.macros.php' => _('Macros'), 'adm.valuemapping.php' => _('Value mapping'), 'adm.workingtime.php' => _('Working time'), 'adm.triggerseverities.php' => _('Trigger severities'), 'adm.triggerdisplayoptions.php' => _('Trigger displaying options'), 'adm.other.php' => _('Other')));
$form->addItem($cmbConf);
$cnf_wdgt = new CWidget();
$cnf_wdgt->addPageHeader(_('CONFIGURATION OF MACROS'), $form);
$data = array();
$data['form_refresh'] = get_request('form_refresh', 0);
$data['macros'] = array();
if ($data['form_refresh']) {
$data['macros'] = get_request('macros', array());
} else {
$data['macros'] = API::UserMacro()->get(array('output' => API_OUTPUT_EXTEND, 'globalmacro' => 1));
}
if (empty($data['macros'])) {
$data['macros'] = array(0 => array('macro' => '', 'value' => ''));
}
if ($result) {
$data['macros'] = order_macros($data['macros'], 'macro');
}
$macrosForm = new CView('administration.general.macros.edit', $data);
$cnf_wdgt->addItem($macrosForm->render());
$cnf_wdgt->show();
require_once dirname(__FILE__) . '/include/page_footer.php';
示例9: getUserMacros
/**
* Get macros with values.
*
* @param array $data Macros to resolve (array(hostids => array(hostid), macros => array(macro => null)))
*
* @return array
*/
protected function getUserMacros(array $data)
{
/*
* User macros
*/
$hostIds = array();
foreach ($data as $element) {
foreach ($element['hostids'] as $hostId) {
$hostIds[$hostId] = $hostId;
}
}
if (!$hostIds) {
return $data;
}
// hostid => array(templateid)
$hostTemplates = array();
// hostid => array(macro => value)
$hostMacros = array();
do {
$dbHosts = API::Host()->get(array('hostids' => $hostIds, 'templated_hosts' => true, 'output' => array('hostid'), 'selectParentTemplates' => array('templateid'), 'selectMacros' => array('macro', 'value')));
$hostIds = array();
if ($dbHosts) {
foreach ($dbHosts as $dbHost) {
$hostTemplates[$dbHost['hostid']] = zbx_objectValues($dbHost['parentTemplates'], 'templateid');
foreach ($dbHost['macros'] as $dbMacro) {
if (!isset($hostMacros[$dbHost['hostid']])) {
$hostMacros[$dbHost['hostid']] = array();
}
$hostMacros[$dbHost['hostid']][$dbMacro['macro']] = $dbMacro['value'];
}
}
foreach ($dbHosts as $dbHost) {
// only unprocessed templates will be populated
foreach ($hostTemplates[$dbHost['hostid']] as $templateId) {
if (!isset($hostTemplates[$templateId])) {
$hostIds[$templateId] = $templateId;
}
}
}
}
} while ($hostIds);
$allMacrosResolved = true;
foreach ($data as &$element) {
$hostIds = array();
foreach ($element['hostids'] as $hostId) {
$hostIds[$hostId] = $hostId;
}
natsort($hostIds);
foreach ($element['macros'] as $macro => &$value) {
$value = $this->getHostUserMacros($hostIds, $macro, $hostTemplates, $hostMacros);
if ($value === null) {
$allMacrosResolved = false;
}
}
unset($value);
}
unset($element);
if ($allMacrosResolved) {
// there are no more hosts with unresolved macros
return $data;
}
/*
* Global macros
*/
$dbGlobalMacros = API::UserMacro()->get(array('output' => array('macro', 'value'), 'globalmacro' => true));
if ($dbGlobalMacros) {
$dbGlobalMacros = zbx_toHash($dbGlobalMacros, 'macro');
$allMacrosResolved = true;
foreach ($data as &$element) {
foreach ($element['macros'] as $macro => &$value) {
if ($value === null) {
if (isset($dbGlobalMacros[$macro])) {
$value = $dbGlobalMacros[$macro]['value'];
} else {
$allMacrosResolved = false;
}
}
}
unset($value);
}
unset($element);
if ($allMacrosResolved) {
// there are no more hosts with unresolved macros
return $data;
}
}
/*
* Unresolved macros stay as is
*/
foreach ($data as &$element) {
foreach ($element['macros'] as $macro => &$value) {
if ($value === null) {
$value = $macro;
//.........这里部分代码省略.........
示例10: getInheritedMacros
/**
* Get list of inherited macros by host ids.
*
* Returns an array like:
* array(
* '{$MACRO}' => array(
* 'macro' => '{$MACRO}',
* 'template' => array( <- optional
* 'value' => 'template-level value'
* 'templateid' => 10001,
* 'name' => 'Template OS Linux'
* ),
* 'global' => array( <- optional
* 'value' => 'global-level value'
* )
* )
* )
*
* @param array $hostids
*
* @return array
*/
function getInheritedMacros(array $hostids)
{
$user_macro_parser = new CUserMacroParser();
$all_macros = [];
$global_macros = [];
$db_global_macros = API::UserMacro()->get(['output' => ['macro', 'value'], 'globalmacro' => true]);
foreach ($db_global_macros as $db_global_macro) {
$all_macros[$db_global_macro['macro']] = true;
$global_macros[$db_global_macro['macro']] = $db_global_macro['value'];
}
// hostid => array('name' => name, 'macros' => array(macro => value), 'templateids' => array(templateid))
$hosts = [];
$templateids = $hostids;
do {
$db_templates = API::Template()->get(['output' => ['name'], 'selectParentTemplates' => ['templateid'], 'selectMacros' => ['macro', 'value'], 'templateids' => $templateids, 'preservekeys' => true]);
$templateids = [];
foreach ($db_templates as $hostid => $db_template) {
$hosts[$hostid] = ['templateid' => $hostid, 'name' => $db_template['name'], 'templateids' => zbx_objectValues($db_template['parentTemplates'], 'templateid'), 'macros' => []];
/*
* Global macros are overwritten by template macros and template macros are overwritten by host macros.
* Macros with contexts require additional checking for contexts, since {$MACRO:} is the same as
* {$MACRO:""}.
*/
foreach ($db_template['macros'] as $dbMacro) {
if (array_key_exists($dbMacro['macro'], $all_macros)) {
$hosts[$hostid]['macros'][$dbMacro['macro']] = $dbMacro['value'];
$all_macros[$dbMacro['macro']] = true;
} else {
$user_macro_parser->parse($dbMacro['macro']);
$tpl_macro = $user_macro_parser->getMacro();
$tpl_context = $user_macro_parser->getContext();
if ($tpl_context === null) {
$hosts[$hostid]['macros'][$dbMacro['macro']] = $dbMacro['value'];
$all_macros[$dbMacro['macro']] = true;
} else {
$match_found = false;
foreach ($global_macros as $global_macro => $global_value) {
$user_macro_parser->parse($global_macro);
$gbl_macro = $user_macro_parser->getMacro();
$gbl_context = $user_macro_parser->getContext();
if ($tpl_macro === $gbl_macro && $tpl_context === $gbl_context) {
$match_found = true;
unset($global_macros[$global_macro], $hosts[$hostid][$global_macro], $all_macros[$global_macro]);
$hosts[$hostid]['macros'][$dbMacro['macro']] = $dbMacro['value'];
$all_macros[$dbMacro['macro']] = true;
$global_macros[$dbMacro['macro']] = $global_value;
break;
}
}
if (!$match_found) {
$hosts[$hostid]['macros'][$dbMacro['macro']] = $dbMacro['value'];
$all_macros[$dbMacro['macro']] = true;
}
}
}
}
}
foreach ($db_templates as $db_template) {
// only unprocessed templates will be populated
foreach ($db_template['parentTemplates'] as $template) {
if (!array_key_exists($template['templateid'], $hosts)) {
$templateids[$template['templateid']] = $template['templateid'];
}
}
}
} while ($templateids);
$all_macros = array_keys($all_macros);
$all_templates = [];
$inherited_macros = [];
// resolving
foreach ($all_macros as $macro) {
$inherited_macro = ['macro' => $macro];
if (array_key_exists($macro, $global_macros)) {
$inherited_macro['global'] = ['value' => $global_macros[$macro]];
}
$templateids = $hostids;
do {
natsort($templateids);
//.........这里部分代码省略.........
示例11: expandUserMacros
/**
* Find user macros and store found ones with value to replace to $macroValues property.
*
* @param array $trigger
*
* @return mixed
*/
protected function expandUserMacros(array $trigger)
{
$macros = array();
if (preg_match_all('/' . ZBX_PREG_EXPRESSION_USER_MACROS . '/', $trigger['description'], $matches)) {
$macros = API::UserMacro()->getMacros(array('macros' => $matches[1], 'triggerid' => $trigger['triggerid']));
}
return $macros;
}
示例12: getUserMacros
/**
* Get user macros.
*
* @param array $texts
* @param array $options
* @param int $options['hostid']
* @param int $options['triggerid']
*
* @return array
*/
private function getUserMacros(array $texts, array $options = array())
{
$matches = $this->findMacros(ZBX_PREG_EXPRESSION_USER_MACROS, $texts);
if (empty($matches)) {
return array();
}
$options['macros'] = $matches;
return API::UserMacro()->getMacros($options);
}
示例13: get_global_macro
function get_global_macro($name)
{
$macros = API::UserMacro()->get(array('output' => API_OUTPUT_EXTEND, 'globalmacro' => true));
$macros = zbx_toHash($macros, 'macro');
return $macros[$name]['value'];
}
示例14: DBend
}
// deletehe
if ($dbMacros) {
$result = $result && (bool) API::UserMacro()->deleteGlobal(array_keys($dbMacros));
}
// create
if ($macros) {
$result = $result && (bool) API::UserMacro()->createGlobal(array_values($macros));
}
$result = DBend($result);
}
show_messages($result, _('Macros updated'), _('Cannot update macros'));
if ($result) {
unset($_REQUEST['form_refresh']);
}
}
/*
* Display
*/
$data = [];
if (hasRequest('form_refresh')) {
$data['macros'] = getRequest('macros', []);
} else {
$data['macros'] = API::UserMacro()->get(['output' => ['globalmacroid', 'macro', 'value'], 'globalmacro' => true]);
$data['macros'] = array_values(order_macros($data['macros'], 'macro'));
}
if (!$data['macros']) {
$data['macros'][] = ['macro' => '', 'value' => ''];
}
(new CView('administration.general.macros.edit', $data))->render()->show();
require_once dirname(__FILE__) . '/include/page_footer.php';
示例15: massRemove
/**
* Allows to:
* - remove hosts from groups;
* - unlink and clear templates from hosts;
* - remove macros from hosts.
*
* Supported $data parameters are:
* - hostids - an array of host IDs to be updated
* - templateids - an array of template IDs to be updated
* - groupids - an array of host group IDs the hosts should be removed from
* - templateids_link - an array of template IDs to unlink from the hosts
* - templateids_clear - an array of template IDs to unlink and clear from the hosts
* - macros - an array of macros to delete from the hosts
*
* @param array $data
*
* @return array
*/
public function massRemove(array $data)
{
$allHostIds = array_merge($data['hostids'], $data['templateids']);
if (isset($data['groupids'])) {
API::HostGroup()->massRemove(array('hostids' => $data['hostids'], 'templateids' => $data['templateids'], 'groupids' => zbx_toArray($data['groupids'])));
}
if (!empty($data['templateids_link'])) {
$this->unlink(zbx_toArray($data['templateids_link']), $allHostIds);
}
if (isset($data['templateids_clear'])) {
$this->unlink(zbx_toArray($data['templateids_clear']), $allHostIds, true);
}
if (isset($data['macros'])) {
$hostMacros = API::UserMacro()->get(array('hostids' => $allHostIds, 'filter' => array('macro' => $data['macros'])));
$hostMacroIds = zbx_objectValues($hostMacros, 'hostmacroid');
API::UserMacro()->delete($hostMacroIds);
}
return array($this->pkOption() => $data[$this->pkOption()]);
}