本文整理汇总了PHP中API::item方法的典型用法代码示例。如果您正苦于以下问题:PHP API::item方法的具体用法?PHP API::item怎么用?PHP API::item使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类API
的用法示例。
在下文中一共展示了API::item方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: massUpdate
//.........这里部分代码省略.........
}
/*
* update interfaces
*/
if (isset($updateInterfaces)) {
foreach ($hostids as $hostid) {
API::HostInterface()->replaceHostInterfaces(['hostid' => $hostid, 'interfaces' => $updateInterfaces]);
}
}
// link new templates
if (isset($updateTemplates)) {
$result = $this->massAdd(['hosts' => $hosts, 'templates' => $updateTemplates]);
if (!$result) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot link template'));
}
}
// macros
if (isset($updateMacros)) {
DB::delete('hostmacro', ['hostid' => $hostids]);
$this->massAdd(['hosts' => $hosts, 'macros' => $updateMacros]);
}
/*
* Inventory
*/
if (isset($updateInventory)) {
// disabling inventory
if ($updateInventory['inventory_mode'] == HOST_INVENTORY_DISABLED) {
$sql = 'DELETE FROM host_inventory WHERE ' . dbConditionInt('hostid', $hostids);
if (!DBexecute($sql)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete inventory.'));
}
} else {
$existingInventoriesDb = DBfetchArrayAssoc(DBselect('SELECT hostid,inventory_mode' . ' FROM host_inventory' . ' WHERE ' . dbConditionInt('hostid', $hostids)), 'hostid');
// check existing host inventory data
$automaticHostIds = [];
if ($updateInventory['inventory_mode'] === null) {
foreach ($hostids as $hostid) {
// if inventory is disabled for one of the updated hosts, throw an exception
if (!isset($existingInventoriesDb[$hostid])) {
$host = get_host_by_hostid($hostid);
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Inventory disabled for host "%1$s".', $host['host']));
} elseif ($existingInventoriesDb[$hostid]['inventory_mode'] == HOST_INVENTORY_AUTOMATIC) {
$automaticHostIds[] = $hostid;
}
}
}
$inventoriesToSave = [];
foreach ($hostids as $hostid) {
$hostInventory = $updateInventory;
$hostInventory['hostid'] = $hostid;
// if no 'inventory_mode' has been passed, set inventory 'inventory_mode' from DB
if ($updateInventory['inventory_mode'] === null) {
$hostInventory['inventory_mode'] = $existingInventoriesDb[$hostid]['inventory_mode'];
}
$inventoriesToSave[$hostid] = $hostInventory;
}
// when updating automatic inventory, ignore fields that have items linked to them
if ($updateInventory['inventory_mode'] == HOST_INVENTORY_AUTOMATIC || $updateInventory['inventory_mode'] === null && $automaticHostIds) {
$itemsToInventories = API::item()->get(['output' => ['inventory_link', 'hostid'], 'hostids' => $automaticHostIds ? $automaticHostIds : $hostids, 'nopermissions' => true]);
$inventoryFields = getHostInventories();
foreach ($itemsToInventories as $hinv) {
// 0 means 'no link'
if ($hinv['inventory_link'] != 0) {
$inventoryName = $inventoryFields[$hinv['inventory_link']]['db_field'];
unset($inventoriesToSave[$hinv['hostid']][$inventoryName]);
}
}
}
// save inventory data
foreach ($inventoriesToSave as $inventory) {
$hostid = $inventory['hostid'];
if (isset($existingInventoriesDb[$hostid])) {
DB::update('host_inventory', ['values' => $inventory, 'where' => ['hostid' => $hostid]]);
} else {
DB::insert('host_inventory', [$inventory], false);
}
}
}
}
/*
* Update host and host group linkage. This procedure should be done the last because user can unlink
* him self from a group with write permissions leaving only read premissions. Thus other procedures, like
* host-template linkage, inventory update, macros update, must be done before this.
*/
if (isset($updateGroups)) {
$updateGroups = zbx_toArray($updateGroups);
$hostGroups = API::HostGroup()->get(['output' => ['groupid'], 'hostids' => $hostids]);
$hostGroupIds = zbx_objectValues($hostGroups, 'groupid');
$newGroupIds = zbx_objectValues($updateGroups, 'groupid');
$groupsToAdd = array_diff($newGroupIds, $hostGroupIds);
if ($groupsToAdd) {
$this->massAdd(['hosts' => $hosts, 'groups' => zbx_toObject($groupsToAdd, 'groupid')]);
}
$groupIdsToDelete = array_diff($hostGroupIds, $newGroupIds);
if ($groupIdsToDelete) {
$this->massRemove(['hostids' => $hostids, 'groupids' => $groupIdsToDelete]);
}
}
return ['hostids' => $inputHostIds];
}
示例2: getItemFormData
/**
* Get data for item edit page.
*
* @param array $item item, item prototype or LLD rule to take the data from
* @param bool $options['is_discovery_rule']
*
* @return array
*/
function getItemFormData(array $item = array(), array $options = array())
{
$data = array('form' => getRequest('form'), 'form_refresh' => getRequest('form_refresh'), 'is_discovery_rule' => !empty($options['is_discovery_rule']), 'parent_discoveryid' => getRequest('parent_discoveryid', !empty($options['is_discovery_rule']) ? getRequest('itemid') : null), 'itemid' => getRequest('itemid'), 'limited' => false, 'interfaceid' => getRequest('interfaceid', 0), 'name' => getRequest('name', ''), 'description' => getRequest('description', ''), 'key' => getRequest('key', ''), 'hostname' => getRequest('hostname'), 'delay' => getRequest('delay', ZBX_ITEM_DELAY_DEFAULT), 'history' => getRequest('history', 90), 'status' => getRequest('status', isset($_REQUEST['form_refresh']) ? 1 : 0), 'type' => getRequest('type', 0), 'snmp_community' => getRequest('snmp_community', 'public'), 'snmp_oid' => getRequest('snmp_oid', 'interfaces.ifTable.ifEntry.ifInOctets.1'), 'port' => getRequest('port', ''), 'value_type' => getRequest('value_type', ITEM_VALUE_TYPE_UINT64), 'data_type' => getRequest('data_type', ITEM_DATA_TYPE_DECIMAL), 'trapper_hosts' => getRequest('trapper_hosts', ''), 'units' => getRequest('units', ''), 'valuemapid' => getRequest('valuemapid', 0), 'params' => getRequest('params', ''), 'multiplier' => getRequest('multiplier', 0), 'delta' => getRequest('delta', 0), 'trends' => getRequest('trends', DAY_IN_YEAR), 'new_application' => getRequest('new_application', ''), 'applications' => getRequest('applications', array()), 'delay_flex' => getRequest('delay_flex', array()), 'new_delay_flex' => getRequest('new_delay_flex', array('delay' => 50, 'period' => ZBX_DEFAULT_INTERVAL)), 'snmpv3_contextname' => getRequest('snmpv3_contextname', ''), 'snmpv3_securityname' => getRequest('snmpv3_securityname', ''), 'snmpv3_securitylevel' => getRequest('snmpv3_securitylevel', 0), 'snmpv3_authprotocol' => getRequest('snmpv3_authprotocol', ITEM_AUTHPROTOCOL_MD5), 'snmpv3_authpassphrase' => getRequest('snmpv3_authpassphrase', ''), 'snmpv3_privprotocol' => getRequest('snmpv3_privprotocol', ITEM_PRIVPROTOCOL_DES), 'snmpv3_privpassphrase' => getRequest('snmpv3_privpassphrase', ''), 'ipmi_sensor' => getRequest('ipmi_sensor', ''), 'authtype' => getRequest('authtype', 0), 'username' => getRequest('username', ''), 'password' => getRequest('password', ''), 'publickey' => getRequest('publickey', ''), 'privatekey' => getRequest('privatekey', ''), 'formula' => getRequest('formula', 1), 'logtimefmt' => getRequest('logtimefmt', ''), 'add_groupid' => getRequest('add_groupid', getRequest('groupid', 0)), 'valuemaps' => null, 'possibleHostInventories' => null, 'alreadyPopulated' => null, 'initial_item_type' => null, 'templates' => array());
// hostid
if (!empty($data['parent_discoveryid'])) {
$discoveryRule = API::DiscoveryRule()->get(array('itemids' => $data['parent_discoveryid'], 'output' => API_OUTPUT_EXTEND, 'editable' => true));
$discoveryRule = reset($discoveryRule);
$data['hostid'] = $discoveryRule['hostid'];
} else {
$data['hostid'] = getRequest('hostid', 0);
}
// types, http items only for internal processes
$data['types'] = item_type2str();
unset($data['types'][ITEM_TYPE_HTTPTEST]);
if (!empty($options['is_discovery_rule'])) {
unset($data['types'][ITEM_TYPE_AGGREGATE], $data['types'][ITEM_TYPE_CALCULATED], $data['types'][ITEM_TYPE_SNMPTRAP]);
}
// item
if ($item) {
$data['item'] = $item;
$data['hostid'] = !empty($data['hostid']) ? $data['hostid'] : $data['item']['hostid'];
$data['limited'] = $data['item']['templateid'] != 0;
// get templates
$itemid = $item['itemid'];
do {
$params = array('itemids' => $itemid, 'output' => array('itemid', 'templateid'), 'selectHosts' => array('name'));
if ($data['is_discovery_rule']) {
$item = API::DiscoveryRule()->get($params);
} else {
$params['selectDiscoveryRule'] = array('itemid');
$params['filter'] = array('flags' => null);
$item = API::Item()->get($params);
}
$item = reset($item);
if (!empty($item)) {
$host = reset($item['hosts']);
if (!empty($item['hosts'])) {
$host['name'] = CHtml::encode($host['name']);
if (bccomp($data['itemid'], $itemid) == 0) {
} elseif ($data['is_discovery_rule']) {
$data['templates'][] = new CLink($host['name'], 'host_discovery.php?form=update&itemid=' . $item['itemid'], 'highlight underline weight_normal');
$data['templates'][] = SPACE . '⇒' . SPACE;
} elseif ($item['discoveryRule']) {
$data['templates'][] = new CLink($host['name'], 'disc_prototypes.php?form=update&itemid=' . $item['itemid'] . '&parent_discoveryid=' . $item['discoveryRule']['itemid'], 'highlight underline weight_normal');
$data['templates'][] = SPACE . '⇒' . SPACE;
} else {
$data['templates'][] = new CLink($host['name'], 'items.php?form=update&itemid=' . $item['itemid'], 'highlight underline weight_normal');
$data['templates'][] = SPACE . '⇒' . SPACE;
}
}
$itemid = $item['templateid'];
} else {
break;
}
} while ($itemid != 0);
$data['templates'] = array_reverse($data['templates']);
array_shift($data['templates']);
}
// caption
if (!empty($data['is_discovery_rule'])) {
$data['caption'] = _('Discovery rule');
} else {
$data['caption'] = !empty($data['parent_discoveryid']) ? _('Item prototype') : _('Item');
}
// hostname
if (empty($data['is_discovery_rule']) && empty($data['hostname'])) {
if (!empty($data['hostid'])) {
$hostInfo = API::Host()->get(array('hostids' => $data['hostid'], 'output' => array('name'), 'templated_hosts' => true));
$hostInfo = reset($hostInfo);
$data['hostname'] = $hostInfo['name'];
} else {
$data['hostname'] = _('not selected');
}
}
// fill data from item
if (!hasRequest('form_refresh') && ($item || $data['limited'])) {
$data['name'] = $data['item']['name'];
$data['description'] = $data['item']['description'];
$data['key'] = $data['item']['key_'];
$data['interfaceid'] = $data['item']['interfaceid'];
$data['type'] = $data['item']['type'];
$data['snmp_community'] = $data['item']['snmp_community'];
$data['snmp_oid'] = $data['item']['snmp_oid'];
$data['port'] = $data['item']['port'];
$data['value_type'] = $data['item']['value_type'];
$data['data_type'] = $data['item']['data_type'];
$data['trapper_hosts'] = $data['item']['trapper_hosts'];
$data['units'] = $data['item']['units'];
$data['valuemapid'] = $data['item']['valuemapid'];
$data['multiplier'] = $data['item']['multiplier'];
$data['hostid'] = $data['item']['hostid'];
$data['params'] = $data['item']['params'];
//.........这里部分代码省略.........
示例3: getItem
/**
*
* @return object|null
*/
public function getItem($id)
{
return API::item($id);
}
示例4: massUpdate
//.........这里部分代码省略.........
}
if (isset($updateTemplatesClear)) {
$templateidsClear = zbx_objectValues($updateTemplatesClear, 'templateid');
if (!empty($updateTemplatesClear)) {
$this->massRemove(array('hostids' => $hostids, 'templateids_clear' => $templateidsClear));
}
} else {
$templateidsClear = array();
}
/*
* Update template linkage
*/
if (isset($updateTemplates)) {
$hostTemplates = API::Template()->get(array('hostids' => $hostids, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => true));
$hostTemplateids = array_keys($hostTemplates);
$newTemplateids = zbx_objectValues($updateTemplates, 'templateid');
$templatesToDel = array_diff($hostTemplateids, $newTemplateids);
$templatesToDel = array_diff($templatesToDel, $templateidsClear);
if (!empty($templatesToDel)) {
$result = $this->massRemove(array('hostids' => $hostids, 'templateids' => $templatesToDel));
if (!$result) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot unlink template'));
}
}
$result = $this->massAdd(array('hosts' => $hosts, 'templates' => $updateTemplates));
if (!$result) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot link template'));
}
}
// macros
if (isset($updateMacros)) {
DB::delete('hostmacro', array('hostid' => $hostids));
$this->massAdd(array('hosts' => $hosts, 'macros' => $updateMacros));
}
/*
* Inventory
*/
if (isset($updateInventory)) {
if ($updateInventory['inventory_mode'] == HOST_INVENTORY_DISABLED) {
$sql = 'DELETE FROM host_inventory WHERE ' . dbConditionInt('hostid', $hostids);
if (!DBexecute($sql)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete inventory.'));
}
} else {
$hostsWithInventories = array();
$existingInventoriesDb = DBselect('SELECT hostid FROM host_inventory WHERE ' . dbConditionInt('hostid', $hostids));
while ($existingInventory = DBfetch($existingInventoriesDb)) {
$hostsWithInventories[] = $existingInventory['hostid'];
}
// when hosts are being updated to use automatic mode for host inventories,
// we must check if some items are set to populate inventory fields of every host.
// if they do, mass update for those fields should be ignored
if ($updateInventory['inventory_mode'] == HOST_INVENTORY_AUTOMATIC) {
// getting all items on all affected hosts
$itemsToInventories = API::item()->get(array('output' => array('inventory_link', 'hostid'), 'filter' => array('hostid' => $hostids), 'nopermissions' => true));
// gathering links to array: 'hostid'=>array('inventory_name_1'=>true, 'inventory_name_2'=>true)
$inventoryLinksOnHosts = array();
$inventoryFields = getHostInventories();
foreach ($itemsToInventories as $hinv) {
if ($hinv['inventory_link'] != 0) {
// 0 means 'no link'
if (isset($inventoryLinksOnHosts[$hinv['hostid']])) {
$inventoryLinksOnHosts[$hinv['hostid']][$inventoryFields[$hinv['inventory_link']]['db_field']] = true;
} else {
$inventoryLinksOnHosts[$hinv['hostid']] = array($inventoryFields[$hinv['inventory_link']]['db_field'] => true);
}
}
}
// now we have all info we need to determine, which inventory fields should be saved
$inventoriesToSave = array();
foreach ($hostids as $hostid) {
$inventoriesToSave[$hostid] = $updateInventory;
$inventoriesToSave[$hostid]['hostid'] = $hostid;
foreach ($updateInventory as $inventoryName => $hinv) {
if (isset($inventoryLinksOnHosts[$hostid][$inventoryName])) {
unset($inventoriesToSave[$hostid][$inventoryName]);
}
}
}
} else {
// if mode is not automatic, all fields can be saved
$inventoriesToSave = array();
foreach ($hostids as $hostid) {
$inventoriesToSave[$hostid] = $updateInventory;
$inventoriesToSave[$hostid]['hostid'] = $hostid;
}
}
$hostsWithoutInventory = array_diff($hostids, $hostsWithInventories);
// hosts that have no inventory yet, need it to be inserted
foreach ($hostsWithoutInventory as $hostid) {
DB::insert('host_inventory', array($inventoriesToSave[$hostid]), false);
}
// those hosts that already have an inventory, need it to be updated
foreach ($hostsWithInventories as $hostid) {
DB::update('host_inventory', array('values' => $inventoriesToSave[$hostid], 'where' => array('hostid' => $hostid)));
}
}
}
return array('hostids' => $hostids);
}
示例5: getItemFormData
/**
* Get data for item edit page.
*
* @param array $item item, item prototype or LLD rule to take the data from
* @param bool $options['is_discovery_rule']
*
* @return array
*/
function getItemFormData(array $item = [], array $options = [])
{
$data = ['form' => getRequest('form'), 'form_refresh' => getRequest('form_refresh'), 'is_discovery_rule' => !empty($options['is_discovery_rule']), 'parent_discoveryid' => getRequest('parent_discoveryid', !empty($options['is_discovery_rule']) ? getRequest('itemid') : null), 'itemid' => getRequest('itemid'), 'limited' => false, 'interfaceid' => getRequest('interfaceid', 0), 'name' => getRequest('name', ''), 'description' => getRequest('description', ''), 'key' => getRequest('key', ''), 'hostname' => getRequest('hostname'), 'delay' => getRequest('delay', ZBX_ITEM_DELAY_DEFAULT), 'history' => getRequest('history', 90), 'status' => getRequest('status', isset($_REQUEST['form_refresh']) ? 1 : 0), 'type' => getRequest('type', 0), 'snmp_community' => getRequest('snmp_community', 'public'), 'snmp_oid' => getRequest('snmp_oid', 'interfaces.ifTable.ifEntry.ifInOctets.1'), 'port' => getRequest('port', ''), 'value_type' => getRequest('value_type', ITEM_VALUE_TYPE_UINT64), 'data_type' => getRequest('data_type', ITEM_DATA_TYPE_DECIMAL), 'trapper_hosts' => getRequest('trapper_hosts', ''), 'units' => getRequest('units', ''), 'valuemapid' => getRequest('valuemapid', 0), 'params' => getRequest('params', ''), 'multiplier' => getRequest('multiplier', 0), 'delta' => getRequest('delta', 0), 'trends' => getRequest('trends', DAY_IN_YEAR), 'new_application' => getRequest('new_application', ''), 'applications' => getRequest('applications', []), 'delay_flex' => getRequest('delay_flex', []), 'snmpv3_contextname' => getRequest('snmpv3_contextname', ''), 'snmpv3_securityname' => getRequest('snmpv3_securityname', ''), 'snmpv3_securitylevel' => getRequest('snmpv3_securitylevel', 0), 'snmpv3_authprotocol' => getRequest('snmpv3_authprotocol', ITEM_AUTHPROTOCOL_MD5), 'snmpv3_authpassphrase' => getRequest('snmpv3_authpassphrase', ''), 'snmpv3_privprotocol' => getRequest('snmpv3_privprotocol', ITEM_PRIVPROTOCOL_DES), 'snmpv3_privpassphrase' => getRequest('snmpv3_privpassphrase', ''), 'ipmi_sensor' => getRequest('ipmi_sensor', ''), 'authtype' => getRequest('authtype', 0), 'username' => getRequest('username', ''), 'password' => getRequest('password', ''), 'publickey' => getRequest('publickey', ''), 'privatekey' => getRequest('privatekey', ''), 'formula' => getRequest('formula', 1), 'logtimefmt' => getRequest('logtimefmt', ''), 'add_groupid' => getRequest('add_groupid', getRequest('groupid', 0)), 'valuemaps' => null, 'possibleHostInventories' => null, 'alreadyPopulated' => null, 'initial_item_type' => null, 'templates' => []];
// hostid
if (!empty($data['parent_discoveryid'])) {
$discoveryRule = API::DiscoveryRule()->get(['itemids' => $data['parent_discoveryid'], 'output' => API_OUTPUT_EXTEND, 'editable' => true]);
$discoveryRule = reset($discoveryRule);
$data['hostid'] = $discoveryRule['hostid'];
$data['new_application_prototype'] = getRequest('new_application_prototype', '');
$data['application_prototypes'] = getRequest('application_prototypes', array());
} else {
$data['hostid'] = getRequest('hostid', 0);
}
// types, http items only for internal processes
$data['types'] = item_type2str();
unset($data['types'][ITEM_TYPE_HTTPTEST]);
if (!empty($options['is_discovery_rule'])) {
unset($data['types'][ITEM_TYPE_AGGREGATE], $data['types'][ITEM_TYPE_CALCULATED], $data['types'][ITEM_TYPE_SNMPTRAP]);
}
// item
if ($item) {
$data['item'] = $item;
$data['hostid'] = !empty($data['hostid']) ? $data['hostid'] : $data['item']['hostid'];
$data['limited'] = $data['item']['templateid'] != 0;
// get templates
$itemid = $item['itemid'];
do {
$params = ['itemids' => $itemid, 'output' => ['itemid', 'templateid'], 'selectHosts' => ['name']];
if ($data['is_discovery_rule']) {
$item = API::DiscoveryRule()->get($params);
} else {
$params['selectDiscoveryRule'] = ['itemid'];
$params['filter'] = ['flags' => null];
$item = API::Item()->get($params);
}
$item = reset($item);
if (!empty($item)) {
$host = reset($item['hosts']);
if (!empty($item['hosts'])) {
$host['name'] = CHtml::encode($host['name']);
if (bccomp($data['itemid'], $itemid) == 0) {
} elseif ($data['is_discovery_rule']) {
$data['templates'][] = new CLink($host['name'], 'host_discovery.php?form=update&itemid=' . $item['itemid']);
$data['templates'][] = SPACE . '⇒' . SPACE;
} elseif ($item['discoveryRule']) {
$data['templates'][] = new CLink($host['name'], 'disc_prototypes.php?form=update' . '&itemid=' . $item['itemid'] . '&parent_discoveryid=' . $item['discoveryRule']['itemid']);
$data['templates'][] = SPACE . '⇒' . SPACE;
} else {
$data['templates'][] = new CLink($host['name'], 'items.php?form=update&itemid=' . $item['itemid']);
$data['templates'][] = SPACE . '⇒' . SPACE;
}
}
$itemid = $item['templateid'];
} else {
break;
}
} while ($itemid != 0);
$data['templates'] = array_reverse($data['templates']);
array_shift($data['templates']);
}
// caption
if (!empty($data['is_discovery_rule'])) {
$data['caption'] = _('Discovery rule');
} else {
$data['caption'] = !empty($data['parent_discoveryid']) ? _('Item prototype') : _('Item');
}
// hostname
if (empty($data['is_discovery_rule']) && empty($data['hostname'])) {
if (!empty($data['hostid'])) {
$hostInfo = API::Host()->get(['hostids' => $data['hostid'], 'output' => ['name'], 'templated_hosts' => true]);
$hostInfo = reset($hostInfo);
$data['hostname'] = $hostInfo['name'];
} else {
$data['hostname'] = _('not selected');
}
}
// fill data from item
if (!hasRequest('form_refresh') && ($item || $data['limited'])) {
$data['name'] = $data['item']['name'];
$data['description'] = $data['item']['description'];
$data['key'] = $data['item']['key_'];
$data['interfaceid'] = $data['item']['interfaceid'];
$data['type'] = $data['item']['type'];
$data['snmp_community'] = $data['item']['snmp_community'];
$data['snmp_oid'] = $data['item']['snmp_oid'];
$data['port'] = $data['item']['port'];
$data['value_type'] = $data['item']['value_type'];
$data['data_type'] = $data['item']['data_type'];
$data['trapper_hosts'] = $data['item']['trapper_hosts'];
$data['units'] = $data['item']['units'];
$data['valuemapid'] = $data['item']['valuemapid'];
$data['multiplier'] = $data['item']['multiplier'];
//.........这里部分代码省略.........
示例6: massUpdate
//.........这里部分代码省略.........
if ($updateTemplatesClear) {
$this->massRemove(array('hostids' => $hostIds, 'templateids_clear' => $templateIdsClear));
}
} else {
$templateIdsClear = array();
}
// unlink templates
if (isset($updateTemplates)) {
$hostTemplates = API::Template()->get(array('hostids' => $hostIds, 'output' => array('templateid'), 'preservekeys' => true));
$hostTemplateids = array_keys($hostTemplates);
$newTemplateids = zbx_objectValues($updateTemplates, 'templateid');
$templatesToDel = array_diff($hostTemplateids, $newTemplateids);
$templatesToDel = array_diff($templatesToDel, $templateIdsClear);
if ($templatesToDel) {
$result = $this->massRemove(array('hostids' => $hostIds, 'templateids' => $templatesToDel));
if (!$result) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot unlink template'));
}
}
}
/*
* update interfaces
*/
if (isset($updateInterfaces)) {
foreach ($hostIds as $hostId) {
API::HostInterface()->replaceHostInterfaces(array('hostid' => $hostId, 'interfaces' => $updateInterfaces));
}
}
// link new templates
if (isset($updateTemplates)) {
$result = $this->massAdd(array('hosts' => $hosts, 'templates' => $updateTemplates));
if (!$result) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot link template'));
}
}
// macros
if (isset($updateMacros)) {
DB::delete('hostmacro', array('hostid' => $hostIds));
$this->massAdd(array('hosts' => $hosts, 'macros' => $updateMacros));
}
/*
* Inventory
*/
if (isset($updateInventory)) {
// disabling inventory
if ($updateInventory['inventory_mode'] == HOST_INVENTORY_DISABLED) {
$sql = 'DELETE FROM host_inventory WHERE ' . dbConditionInt('hostid', $hostIds);
if (!DBexecute($sql)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete inventory.'));
}
} else {
$existingInventoriesDb = DBfetchArrayAssoc(DBselect('SELECT hostid,inventory_mode' . ' FROM host_inventory' . ' WHERE ' . dbConditionInt('hostid', $hostIds)), 'hostid');
// check existing host inventory data
$automaticHostIds = array();
if ($updateInventory['inventory_mode'] === null) {
foreach ($hostIds as $hostId) {
// if inventory is disabled for one of the updated hosts, throw an exception
if (!isset($existingInventoriesDb[$hostId])) {
$host = get_host_by_hostid($hostId);
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Inventory disabled for host "%1$s".', $host['host']));
} elseif ($existingInventoriesDb[$hostId]['inventory_mode'] == HOST_INVENTORY_AUTOMATIC) {
$automaticHostIds[] = $hostId;
}
}
}
$inventoriesToSave = array();
foreach ($hostIds as $hostId) {
$hostInventory = $updateInventory;
$hostInventory['hostid'] = $hostId;
// if no 'inventory_mode' has been passed, set inventory 'inventory_mode' from DB
if ($updateInventory['inventory_mode'] === null) {
$hostInventory['inventory_mode'] = $existingInventoriesDb[$hostId]['inventory_mode'];
}
$inventoriesToSave[$hostId] = $hostInventory;
}
// when updating automatic inventory, ignore fields that have items linked to them
if ($updateInventory['inventory_mode'] == HOST_INVENTORY_AUTOMATIC || $updateInventory['inventory_mode'] === null && $automaticHostIds) {
$itemsToInventories = API::item()->get(array('output' => array('inventory_link', 'hostid'), 'hostids' => $automaticHostIds ? $automaticHostIds : $hostIds, 'nopermissions' => true));
$inventoryFields = getHostInventories();
foreach ($itemsToInventories as $hinv) {
// 0 means 'no link'
if ($hinv['inventory_link'] != 0) {
$inventoryName = $inventoryFields[$hinv['inventory_link']]['db_field'];
unset($inventoriesToSave[$hinv['hostid']][$inventoryName]);
}
}
}
// save inventory data
foreach ($inventoriesToSave as $inventory) {
$hostId = $inventory['hostid'];
if (isset($existingInventoriesDb[$hostId])) {
DB::update('host_inventory', array('values' => $inventory, 'where' => array('hostid' => $hostId)));
} else {
DB::insert('host_inventory', array($inventory), false);
}
}
}
}
return array('hostids' => $inputHostIds);
}