예제 #1
0
 /**
  * Prepares and returns an array of child items, inherited from items $itemsToInherit on the given hosts.
  *
  * @param array      $itemsToInherit
  * @param array|null $hostIds
  *
  * @return array an array of unsaved child items
  */
 protected function prepareInheritedItems(array $itemsToInherit, array $hostIds = null)
 {
     // fetch all child hosts
     $chdHosts = API::Host()->get(array('output' => array('hostid', 'host', 'status'), 'selectParentTemplates' => array('templateid'), 'selectInterfaces' => API_OUTPUT_EXTEND, 'templateids' => zbx_objectValues($itemsToInherit, 'hostid'), 'hostids' => $hostIds, 'preservekeys' => true, 'nopermissions' => true, 'templated_hosts' => true));
     if (empty($chdHosts)) {
         return array();
     }
     $newItems = array();
     foreach ($chdHosts as $hostId => $host) {
         $templateids = zbx_toHash($host['parentTemplates'], 'templateid');
         // skip items not from parent templates of current host
         $parentItems = array();
         foreach ($itemsToInherit as $inum => $parentItem) {
             if (isset($templateids[$parentItem['hostid']])) {
                 $parentItems[$inum] = $parentItem;
             }
         }
         // check existing items to decide insert or update
         $exItems = API::Item()->get(array('output' => array('itemid', 'type', 'key_', 'flags', 'templateid'), 'hostids' => $hostId, 'preservekeys' => true, 'nopermissions' => true, 'filter' => array('flags' => null)));
         $exItemsKeys = zbx_toHash($exItems, 'key_');
         $exItemsTpl = zbx_toHash($exItems, 'templateid');
         foreach ($parentItems as $parentItem) {
             $exItem = null;
             // check if an item of a different type with the same key exists
             if (isset($exItemsKeys[$parentItem['key_']])) {
                 $exItem = $exItemsKeys[$parentItem['key_']];
                 if ($exItem['flags'] != $parentItem['flags']) {
                     $this->errorInheritFlags($exItem['flags'], $exItem['key_'], $host['host']);
                 }
             }
             // update by templateid
             if (isset($exItemsTpl[$parentItem['itemid']])) {
                 $exItem = $exItemsTpl[$parentItem['itemid']];
                 if (isset($exItemsKeys[$parentItem['key_']]) && !idcmp($exItemsKeys[$parentItem['key_']]['templateid'], $parentItem['itemid'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_EXISTS), array($parentItem['key_'], $host['host'])));
                 }
             }
             // update by key
             if (isset($exItemsKeys[$parentItem['key_']])) {
                 $exItem = $exItemsKeys[$parentItem['key_']];
                 if ($exItem['templateid'] > 0 && !idcmp($exItem['templateid'], $parentItem['itemid'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_EXISTS_TEMPLATE), array($parentItem['key_'], $host['host'])));
                 }
             }
             if ($host['status'] == HOST_STATUS_TEMPLATE || !isset($parentItem['type'])) {
                 unset($parentItem['interfaceid']);
             } elseif (isset($parentItem['type']) && isset($exItem) && $parentItem['type'] != $exItem['type'] || !isset($exItem)) {
                 $interface = self::findInterfaceForItem($parentItem, $host['interfaces']);
                 if (!empty($interface)) {
                     $parentItem['interfaceid'] = $interface['interfaceid'];
                 } elseif ($interface !== false) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_NO_INTERFACE), array($host['host'], $parentItem['key_'])));
                 }
             } else {
                 unset($parentItem['interfaceid']);
             }
             // copying item
             $newItem = $parentItem;
             $newItem['hostid'] = $host['hostid'];
             $newItem['templateid'] = $parentItem['itemid'];
             // setting item application
             if (isset($parentItem['applications'])) {
                 $newItem['applications'] = get_same_applications_for_host($parentItem['applications'], $host['hostid']);
             }
             if ($exItem) {
                 $newItem['itemid'] = $exItem['itemid'];
             } else {
                 unset($newItem['itemid']);
             }
             $newItems[] = $newItem;
         }
     }
     return $newItems;
 }
예제 #2
0
 /**
  * Prepares and returns an array of child items, inherited from items $itemsToInherit on the given hosts.
  *
  * @param array      $itemsToInherit
  * @param array|null $hostIds
  *
  * @return array an array of unsaved child items
  */
 protected function prepareInheritedItems(array $itemsToInherit, array $hostIds = null)
 {
     // fetch all child hosts
     $chdHosts = API::Host()->get(['output' => ['hostid', 'host', 'status'], 'selectParentTemplates' => ['templateid'], 'selectInterfaces' => API_OUTPUT_EXTEND, 'templateids' => zbx_objectValues($itemsToInherit, 'hostid'), 'hostids' => $hostIds, 'preservekeys' => true, 'nopermissions' => true, 'templated_hosts' => true]);
     if (empty($chdHosts)) {
         return [];
     }
     $newItems = [];
     foreach ($chdHosts as $hostId => $host) {
         $templateids = zbx_toHash($host['parentTemplates'], 'templateid');
         // skip items not from parent templates of current host
         $parentItems = [];
         foreach ($itemsToInherit as $inum => $parentItem) {
             if (isset($templateids[$parentItem['hostid']])) {
                 $parentItems[$inum] = $parentItem;
             }
         }
         // check existing items to decide insert or update
         $exItems = API::Item()->get(['output' => ['itemid', 'type', 'key_', 'flags', 'templateid'], 'hostids' => $hostId, 'preservekeys' => true, 'nopermissions' => true, 'filter' => ['flags' => null]]);
         $exItemsKeys = zbx_toHash($exItems, 'key_');
         $exItemsTpl = zbx_toHash($exItems, 'templateid');
         $itemids_with_application_prototypes = [];
         foreach ($parentItems as $parentItem) {
             if (isset($parentItem['applicationPrototypes']) && is_array($parentItem['applicationPrototypes']) && !array_key_exists('ruleid', $parentItem)) {
                 $itemids_with_application_prototypes[$parentItem['itemid']] = true;
             }
         }
         if ($itemids_with_application_prototypes) {
             $discovery_rules = DBfetchArray(DBselect('SELECT id.itemid,id.parent_itemid' . ' FROM item_discovery id' . ' WHERE ' . dbConditionInt('id.itemid', array_keys($itemids_with_application_prototypes))));
             $discovery_rules = zbx_toHash($discovery_rules, 'itemid');
         }
         foreach ($parentItems as $parentItem) {
             $exItem = null;
             // check if an item of a different type with the same key exists
             if (isset($exItemsKeys[$parentItem['key_']])) {
                 $exItem = $exItemsKeys[$parentItem['key_']];
                 if ($exItem['flags'] != $parentItem['flags']) {
                     $this->errorInheritFlags($exItem['flags'], $exItem['key_'], $host['host']);
                 }
             }
             // update by templateid
             if (isset($exItemsTpl[$parentItem['itemid']])) {
                 $exItem = $exItemsTpl[$parentItem['itemid']];
                 if (isset($exItemsKeys[$parentItem['key_']]) && !idcmp($exItemsKeys[$parentItem['key_']]['templateid'], $parentItem['itemid'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_EXISTS), [$parentItem['key_'], $host['host']]));
                 }
             }
             // update by key
             if (isset($exItemsKeys[$parentItem['key_']])) {
                 $exItem = $exItemsKeys[$parentItem['key_']];
                 if ($exItem['templateid'] > 0 && !idcmp($exItem['templateid'], $parentItem['itemid'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_EXISTS_TEMPLATE), [$parentItem['key_'], $host['host']]));
                 }
             }
             if ($host['status'] == HOST_STATUS_TEMPLATE || !isset($parentItem['type'])) {
                 unset($parentItem['interfaceid']);
             } elseif (isset($parentItem['type']) && isset($exItem) && $parentItem['type'] != $exItem['type'] || !isset($exItem)) {
                 $interface = self::findInterfaceForItem($parentItem, $host['interfaces']);
                 if (!empty($interface)) {
                     $parentItem['interfaceid'] = $interface['interfaceid'];
                 } elseif ($interface !== false) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_NO_INTERFACE), [$host['host'], $parentItem['key_']]));
                 }
             } else {
                 unset($parentItem['interfaceid']);
             }
             // copying item
             $newItem = $parentItem;
             $newItem['hostid'] = $host['hostid'];
             $newItem['templateid'] = $parentItem['itemid'];
             // setting item application
             if (isset($parentItem['applications'])) {
                 $newItem['applications'] = get_same_applications_for_host($parentItem['applications'], $host['hostid']);
             }
             if ($parentItem['flags'] == ZBX_FLAG_DISCOVERY_PROTOTYPE && array_key_exists('applicationPrototypes', $parentItem)) {
                 // Get discovery rule ID for current item prototype, if it is not yet set.
                 if (array_key_exists('ruleid', $parentItem)) {
                     $discovery_ruleid = $parentItem['ruleid'];
                 } else {
                     $discovery_ruleid = $discovery_rules[$parentItem['itemid']]['parent_itemid'];
                 }
                 $newItem['applicationPrototypes'] = [];
                 $db_application_prototypes = DBfetchArray(DBselect('SELECT ap.application_prototypeid,ap.name' . ' FROM application_prototype ap' . ' WHERE ap.itemid=' . zbx_dbstr($discovery_ruleid) . ' AND ' . dbConditionString('ap.name', zbx_objectValues($parentItem['applicationPrototypes'], 'name'))));
                 $db_application_prototypes = zbx_toHash($db_application_prototypes, 'name');
                 foreach ($parentItem['applicationPrototypes'] as $application_prototype) {
                     $db_application_prototype = $db_application_prototypes[$application_prototype['name']];
                     $newItem['applicationPrototypes'][] = ['name' => $application_prototype['name'], 'templateid' => $db_application_prototype['application_prototypeid']];
                 }
             }
             if ($exItem) {
                 $newItem['itemid'] = $exItem['itemid'];
             } else {
                 unset($newItem['itemid']);
             }
             $newItems[] = $newItem;
         }
     }
     return $newItems;
 }
 /**
  * Copies all of the item prototypes from the source discovery to the target
  * discovery rule.
  *
  * @throws APIException if prototype saving fails
  *
  * @param array $srcDiscovery   The source discovery rule to copy from
  * @param array $dstDiscovery   The target discovery rule to copy to
  * @param array $dstHost        The target host to copy the deiscovery rule to
  *
  * @return array
  */
 protected function copyItemPrototypes(array $srcDiscovery, array $dstDiscovery, array $dstHost)
 {
     $prototypes = API::ItemPrototype()->get(array('discoveryids' => $srcDiscovery['itemid'], 'selectApplications' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true));
     $rs = array();
     if ($prototypes) {
         foreach ($prototypes as $key => $prototype) {
             $prototype['ruleid'] = $dstDiscovery['itemid'];
             $prototype['hostid'] = $dstDiscovery['hostid'];
             unset($prototype['templateid']);
             // map prototype interfaces
             if ($dstHost['status'] != HOST_STATUS_TEMPLATE) {
                 // find a matching interface
                 $interface = self::findInterfaceForItem($prototype, $dstHost['interfaces']);
                 if ($interface) {
                     $prototype['interfaceid'] = $interface['interfaceid'];
                 } elseif ($interface !== false) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot find host interface on "%1$s" for item key "%2$s".', $dstHost['name'], $prototype['key_']));
                 }
             }
             // add new applications
             $prototype['applications'] = get_same_applications_for_host(zbx_objectValues($prototype['applications'], 'applicationid'), $dstHost['hostid']);
             $prototypes[$key] = $prototype;
         }
         $rs = API::ItemPrototype()->create($prototypes);
         if (!$rs) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot clone item prototypes.'));
         }
     }
     return $rs;
 }
예제 #4
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);
}
예제 #5
0
/**
 * Copies web scenarios from given host ID to destination host.
 *
 * @param string $srcHostId		source host ID
 * @param string $dstHostId		destination host ID
 *
 * @return bool
 */
function copyHttpTests($srcHostId, $dstHostId)
{
    $httpTests = API::HttpTest()->get(array('output' => array('name', 'applicationid', 'delay', 'status', 'variables', 'agent', 'authentication', 'http_user', 'http_password', 'http_proxy', 'retries', 'ssl_cert_file', 'ssl_key_file', 'ssl_key_password', 'verify_peer', 'verify_host', 'headers'), 'hostids' => $srcHostId, 'selectSteps' => array('name', 'no', 'url', 'timeout', 'posts', 'required', 'status_codes', 'variables', 'follow_redirects', 'retrieve_mode', 'headers'), 'inherited' => false));
    if (!$httpTests) {
        return true;
    }
    // get destination application IDs
    $srcApplicationIds = array();
    foreach ($httpTests as $httpTest) {
        if ($httpTest['applicationid'] != 0) {
            $srcApplicationIds[] = $httpTest['applicationid'];
        }
    }
    if ($srcApplicationIds) {
        $dstApplicationIds = get_same_applications_for_host($srcApplicationIds, $dstHostId);
    }
    foreach ($httpTests as &$httpTest) {
        $httpTest['hostid'] = $dstHostId;
        if (isset($dstApplicationIds[$httpTest['applicationid']])) {
            $httpTest['applicationid'] = $dstApplicationIds[$httpTest['applicationid']];
        } else {
            unset($httpTest['applicationid']);
        }
        unset($httpTest['httptestid']);
    }
    unset($httpTest);
    return (bool) API::HttpTest()->create($httpTests);
}
 /**
  * Prepares and returns an array of child items, inherited from items $itemsToInherit on the given hosts.
  *
  * @param array      $itemsToInherit
  * @param array|null $hostIds
  * @param array      $errors         an array of messages to use for errors
  *
  * @return array an array of unsaved child items
  */
 protected function prepareInheritedItems(array $itemsToInherit, array $hostIds = null, array $errors = array())
 {
     $errors = array_merge(array('exists' => _('Item "%1$s" already exists on "%2$s", inherited from another template.'), 'noInterface' => _('Cannot find host interface on "%1$s" for item key "%2$s".')), $errors);
     // fetch all child hosts
     $chdHosts = API::Host()->get(array('output' => array('hostid', 'host', 'status'), 'selectInterfaces' => API_OUTPUT_EXTEND, 'templateids' => zbx_objectValues($itemsToInherit, 'hostid'), 'hostids' => $hostIds, 'preservekeys' => true, 'nopermissions' => true, 'templated_hosts' => true));
     if (empty($chdHosts)) {
         return array();
     }
     $newItems = array();
     foreach ($chdHosts as $hostId => $host) {
         $templateids = zbx_toHash($host['templates'], 'templateid');
         // skip items not from parent templates of current host
         $parentItems = array();
         foreach ($itemsToInherit as $inum => $parentItem) {
             if (isset($templateids[$parentItem['hostid']])) {
                 $parentItems[$inum] = $parentItem;
             }
         }
         // check existing items to decide insert or update
         $exItems = API::Item()->get(array('output' => array('itemid', 'type', 'key_', 'flags', 'templateid'), 'hostids' => $hostId, 'preservekeys' => true, 'nopermissions' => true, 'filter' => array('flags' => null)));
         $exItemsKeys = zbx_toHash($exItems, 'key_');
         $exItemsTpl = zbx_toHash($exItems, 'templateid');
         foreach ($parentItems as $parentItem) {
             $exItem = null;
             // update by templateid
             if (isset($exItemsTpl[$parentItem['itemid']])) {
                 $exItem = $exItemsTpl[$parentItem['itemid']];
             }
             // update by key
             if (isset($exItemsKeys[$parentItem['key_']])) {
                 $exItem = $exItemsKeys[$parentItem['key_']];
                 if ($exItem['flags'] != $parentItem['flags']) {
                     $this->errorInheritFlags($exItem['flags'], $exItem['key_'], $host['host']);
                 } elseif ($exItem['templateid'] > 0 && bccomp($exItem['templateid'], $parentItem['itemid']) != 0) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s($errors['exists'], $parentItem['key_'], $host['host']));
                 }
             }
             if ($host['status'] == HOST_STATUS_TEMPLATE || !isset($parentItem['type'])) {
                 unset($parentItem['interfaceid']);
             } elseif (isset($parentItem['type']) && isset($exItem) && $parentItem['type'] != $exItem['type'] || !isset($exItem)) {
                 $interface = self::findInterfaceForItem($parentItem, $host['interfaces']);
                 if (!empty($interface)) {
                     $parentItem['interfaceid'] = $interface['interfaceid'];
                 } elseif ($interface !== false) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s($errors['noInterface'], $host['host'], $parentItem['key_']));
                 }
             } else {
                 unset($parentItem['interfaceid']);
             }
             // copying item
             $newItem = $parentItem;
             $newItem['hostid'] = $host['hostid'];
             $newItem['templateid'] = $parentItem['itemid'];
             // setting item application
             if (isset($parentItem['applications'])) {
                 $newItem['applications'] = get_same_applications_for_host($parentItem['applications'], $host['hostid']);
             }
             if ($exItem) {
                 $newItem['itemid'] = $exItem['itemid'];
             } else {
                 unset($newItem['itemid']);
             }
             $newItems[] = $newItem;
         }
     }
     return $newItems;
 }
예제 #7
0
function copy_template_items($hostid, $templateid = null, $copy_mode = false)
{
    if ($templateid == null) {
        $templateid = array_keys(get_templates_by_hostid($hostid));
    }
    if (is_array($templateid)) {
        foreach ($templateid as $id) {
            copy_template_items($hostid, $id, $copy_mode);
        }
        // attention recursion
        return;
    }
    $db_tmp_items = get_items_by_hostid($templateid);
    while ($db_tmp_item = DBfetch($db_tmp_items)) {
        $db_tmp_item['hostid'] = $hostid;
        $db_tmp_item['applications'] = get_same_applications_for_host(get_applications_by_itemid($db_tmp_item['itemid']), $hostid);
        $db_tmp_item['templateid'] = $copy_mode ? 0 : $db_tmp_item['itemid'];
        add_item($db_tmp_item);
    }
}
예제 #8
0
 /**
  * Copies all of the item prototypes from the source discovery to the target
  * discovery rule.
  *
  * @throws APIException if prototype saving fails
  *
  * @param array $srcDiscovery   The source discovery rule to copy from
  * @param array $dstDiscovery   The target discovery rule to copy to
  * @param array $dstHost        The target host to copy the deiscovery rule to
  *
  * @return array
  */
 protected function copyItemPrototypes(array $srcDiscovery, array $dstDiscovery, array $dstHost)
 {
     $prototypes = API::ItemPrototype()->get(['output' => ['itemid', 'type', 'snmp_community', 'snmp_oid', 'name', 'key_', 'delay', 'history', 'trends', 'status', 'value_type', 'trapper_hosts', 'units', 'multiplier', 'delta', 'snmpv3_securityname', 'snmpv3_securitylevel', 'snmpv3_authpassphrase', 'snmpv3_privpassphrase', 'formula', 'logtimefmt', 'valuemapid', 'delay_flex', 'params', 'ipmi_sensor', 'data_type', 'authtype', 'username', 'password', 'publickey', 'privatekey', 'interfaceid', 'port', 'description', 'snmpv3_authprotocol', 'snmpv3_privprotocol', 'snmpv3_contextname'], 'selectApplications' => ['applicationid'], 'selectApplicationPrototypes' => ['name'], 'discoveryids' => $srcDiscovery['itemid'], 'preservekeys' => true]);
     $rs = [];
     if ($prototypes) {
         foreach ($prototypes as $key => $prototype) {
             $prototype['ruleid'] = $dstDiscovery['itemid'];
             $prototype['hostid'] = $dstDiscovery['hostid'];
             // map prototype interfaces
             if ($dstHost['status'] != HOST_STATUS_TEMPLATE) {
                 // find a matching interface
                 $interface = self::findInterfaceForItem($prototype, $dstHost['interfaces']);
                 if ($interface) {
                     $prototype['interfaceid'] = $interface['interfaceid'];
                 } elseif ($interface !== false) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot find host interface on "%1$s" for item key "%2$s".', $dstHost['name'], $prototype['key_']));
                 }
             }
             // add new applications
             $prototype['applications'] = get_same_applications_for_host(zbx_objectValues($prototype['applications'], 'applicationid'), $dstHost['hostid']);
             $prototypes[$key] = $prototype;
         }
         $rs = API::ItemPrototype()->create($prototypes);
         if (!$rs) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot clone item prototypes.'));
         }
     }
     return $rs;
 }