Exemplo n.º 1
0
function copyItems($srcHostId, $dstHostId)
{
    $srcItems = API::Item()->get(array('hostids' => $srcHostId, 'output' => array('type', 'snmp_community', 'snmp_oid', 'name', 'key_', 'delay', 'history', 'trends', 'status', 'value_type', 'trapper_hosts', 'units', 'multiplier', 'delta', 'snmpv3_contextname', 'snmpv3_securityname', 'snmpv3_securitylevel', 'snmpv3_authprotocol', 'snmpv3_authpassphrase', 'snmpv3_privprotocol', 'snmpv3_privpassphrase', 'formula', 'logtimefmt', 'valuemapid', 'delay_flex', 'params', 'ipmi_sensor', 'data_type', 'authtype', 'username', 'password', 'publickey', 'privatekey', 'flags', 'filter', 'port', 'description', 'inventory_link'), 'inherited' => false, 'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL), 'selectApplications' => API_OUTPUT_REFER));
    $dstHosts = API::Host()->get(array('output' => array('hostid', 'host', 'status'), 'selectInterfaces' => array('interfaceid', 'type', 'main'), 'hostids' => $dstHostId, 'preservekeys' => true, 'nopermissions' => true, 'templated_hosts' => true));
    $dstHost = reset($dstHosts);
    foreach ($srcItems as &$srcItem) {
        if ($dstHost['status'] != HOST_STATUS_TEMPLATE) {
            // find a matching interface
            $interface = CItem::findInterfaceForItem($srcItem, $dstHost['interfaces']);
            if ($interface) {
                $srcItem['interfaceid'] = $interface['interfaceid'];
            } 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;
        $srcItem['applications'] = get_same_applications_for_host(zbx_objectValues($srcItem['applications'], 'applicationid'), $dstHostId);
    }
    return API::Item()->create($srcItems);
}