$sortedItems[$key] = $items[$key];
* Returns the name of the given interface type. Items "status" and "state" properties must be defined.
function interfaceType2str($type) {
$interfaceGroupLabels = [
INTERFACE_TYPE_OPT => _('None'),
INTERFACE_TYPE_AGENT => _('Agent'),
INTERFACE_TYPE_SNMP => _('SNMP'),
INTERFACE_TYPE_JMX => _('JMX'),
INTERFACE_TYPE_IPMI => _('IPMI')
return isset($interfaceGroupLabels[$type]) ? $interfaceGroupLabels[$type] : null;
return array_key_exists($type, $interfaceGroupLabels) ? $interfaceGroupLabels[$type] : null;
function itemTypeInterface($type = null) {
ITEM_TYPE_SNMP => INTERFACE_TYPE_SNMP,
ITEM_TYPE_SNMPTRAP => INTERFACE_TYPE_SNMP,
ITEM_TYPE_IPMI => INTERFACE_TYPE_IPMI,
ITEM_TYPE_ZABBIX => INTERFACE_TYPE_AGENT,
ITEM_TYPE_SIMPLE => INTERFACE_TYPE_ANY,
ITEM_TYPE_EXTERNAL => INTERFACE_TYPE_ANY,
ITEM_TYPE_SSH => INTERFACE_TYPE_ANY,
ITEM_TYPE_TELNET => INTERFACE_TYPE_ANY,
ITEM_TYPE_JMX => INTERFACE_TYPE_JMX,
ITEM_TYPE_HTTPAGENT => INTERFACE_TYPE_ANY
ITEM_TYPE_HTTPAGENT => INTERFACE_TYPE_OPT
elseif (isset($types[$type])) {
elseif (array_key_exists($type, $types)) {
* Convert a list of interfaces to an array of interface type => interfaceids.
* @param array $interfaces List of (host) interfaces.
* @return array Interface IDs grouped by type.
function interfaceIdsByType(array $interfaces) {
$interface_ids_by_type = [];
foreach ($interfaces as $interface) {
$interface_ids_by_type[$interface['type']][] = $interface['interfaceid'];
return $interface_ids_by_type;
* Copies the given items to the given hosts or templates.
* @param array $src_itemids Items which will be copied to $dst_hostids.
* @param array $dst_hostids Hosts and templates to whom add items.
$valuemapids_map[$src_valuemapid][$dst_valuemap['hostid']] = $dst_valuemapid;
foreach ($dstHosts as $dstHost) {
foreach ($dstHost['interfaces'] as $interface) {
if ($interface['main'] == 1) {
if ($interface['main'] == INTERFACE_PRIMARY) {
$interfaceids[$interface['type']] = $interface['interfaceid'];
$current_dependency = reset($create_order);
foreach ($create_order as $itemid => $dependency_level) {
if ($current_dependency != $dependency_level) {
if ($dstHost['status'] != HOST_STATUS_TEMPLATE) {
$type = itemTypeInterface($item['type']);
if ($type == INTERFACE_TYPE_ANY) {
foreach ([INTERFACE_TYPE_AGENT, INTERFACE_TYPE_SNMP, INTERFACE_TYPE_JMX, INTERFACE_TYPE_IPMI] as $itype) {
if (isset($interfaceids[$itype])) {
foreach (CItem::INTERFACE_TYPES_BY_PRIORITY as $itype) {
if (array_key_exists($type, $interfaceids)) {
$item['interfaceid'] = $interfaceids[$itype];
elseif ($type !== false) {
elseif ($type !== false && $type != INTERFACE_TYPE_OPT) {
if (!isset($interfaceids[$type])) {
error(_s('Cannot find host interface on "%1$s" for item key "%2$s".', $dstHost['host'],
$item['interfaceid'] = $interfaceids[$type];
if ($item['valuemapid'] != 0) {
if (array_key_exists($item['valuemapid'], $valuemapids_map)
&& array_key_exists($dstHost['hostid'], $valuemapids_map[$item['valuemapid']])) {
$item['valuemapid'] = $valuemapids_map[$item['valuemapid']][$dstHost['hostid']];
if ($create_items && !API::Item()->create($create_items)) {
function copyItems($srcHostId, $dstHostId) {
function copyItems($srcHostId, $dstHostId, $assign_opt_interface = false) {
$srcItems = API::Item()->get([
'output' => ['type', 'snmp_oid', 'name', 'key_', 'delay', 'history', 'trends', 'status', 'value_type',
'trapper_hosts', 'units', 'logtimefmt', 'valuemapid', 'params', 'ipmi_sensor', 'authtype', 'username',
'password', 'publickey', 'privatekey', 'flags', 'description', 'inventory_link', 'jmx_endpoint',
'master_itemid', 'templateid', 'url', 'query_fields', 'timeout', 'posts', 'status_codes',
'password', 'publickey', 'privatekey', 'flags', 'interfaceid', 'description', 'inventory_link',
'jmx_endpoint', 'master_itemid', 'templateid', 'url', 'query_fields', 'timeout', 'posts', 'status_codes',
'follow_redirects', 'post_type', 'http_proxy', 'headers', 'retrieve_mode', 'request_method',
'output_format', 'ssl_cert_file', 'ssl_key_file', 'ssl_key_password', 'verify_peer', 'verify_host',
'allow_traps', 'parameters'
'selectTags' => ['tag', 'value'],
'selectPreprocessing' => ['type', 'params', 'error_handler', 'error_handler_params'],
'selectValueMap' => ['name'],
'filter' => ['flags' => ZBX_FLAG_DISCOVERY_NORMAL],
$itemkey_to_id[$srcItem['key_']] = $srcItem['itemid'];
else if ($srcItem['valuemapid'] != 0) {
$srcItem['valuemapid'] = array_key_exists($srcItem['valuemap']['name'], $valuemap_map)
? $valuemap_map[$srcItem['valuemap']['name']]
if ($dstHost['status'] != HOST_STATUS_TEMPLATE) {
// find a matching interface
$interface = CItem::findInterfaceForItem($srcItem['type'], $dstHost['interfaces']);
if ($interface === false && $assign_opt_interface
&& itemTypeInterface($srcItem['type']) == INTERFACE_TYPE_OPT
&& $srcItem['interfaceid'] != 0) {
$interface = CItem::findInterfaceByPriority($dstHost['interfaces']);
$srcItem['interfaceid'] = $interface['interfaceid'];
// no matching interface found, throw an error
elseif ($interface !== false) {
error(_s('Cannot find host interface on "%1$s" for item key "%2$s".', $dstHost['host'], $srcItem['key_']));
unset($srcItem['itemid']);
unset($srcItem['templateid']);
$srcItem['hostid'] = $dstHostId;
if (!$srcItem['preprocessing']) {
unset($srcItem['preprocessing']);
* @param array $interfaces
function getInterfaceSelect(array $interfaces): CSelect {
$interface_select = new CSelect('interfaceid');
/** @var CSelectOption[] $options_by_type */
$interface_select->addOption(new CSelectOption(INTERFACE_TYPE_OPT, interfaceType2str(INTERFACE_TYPE_OPT)));
foreach ($interfaces as $interface) {
$option = new CSelectOption($interface['interfaceid'], getHostInterface($interface));
if ($interface['type'] == INTERFACE_TYPE_SNMP) {
$option->setExtra('description', getSnmpInterfaceDescription($interface));
$options_by_type[$interface['type']][] = $option;
foreach ([INTERFACE_TYPE_AGENT, INTERFACE_TYPE_SNMP, INTERFACE_TYPE_JMX, INTERFACE_TYPE_IPMI] as $interface_type) {
foreach (CItem::INTERFACE_TYPES_BY_PRIORITY as $interface_type) {
if (array_key_exists($interface_type, $options_by_type)) {
$interface_group = new CSelectOptionGroup((string) interfaceType2str($interface_type));
if ($interface_type == INTERFACE_TYPE_SNMP) {
$interface_group->setOptionTemplate('#{label}'.(new CDiv('#{description}'))->addClass('description'));
$interface_group->addOptions($options_by_type[$interface_type]);
$interface_select->addOptionGroup($interface_group);