Example #1
0
 /**
  * Returns the interface that best matches the given item.
  *
  * @param array $itemType   An item
  * @param array $interfaces An array of interfaces to choose from
  *
  * @return array|boolean    The best matching interface;
  *							an empty array of no matching interface was found;
  *							false, if the item does not need an interface
  */
 public static function findInterfaceForItem(array $item, array $interfaces)
 {
     $typeInterface = array();
     foreach ($interfaces as $interface) {
         if ($interface['main'] == 1) {
             $typeInterface[$interface['type']] = $interface;
         }
     }
     // find item interface type
     $type = itemTypeInterface($item['type']);
     $matchingInterface = array();
     // the item can use any interface
     if ($type == INTERFACE_TYPE_ANY) {
         $interfaceTypes = array(INTERFACE_TYPE_AGENT, INTERFACE_TYPE_SNMP, INTERFACE_TYPE_JMX, INTERFACE_TYPE_IPMI);
         foreach ($interfaceTypes as $itype) {
             if (isset($typeInterface[$itype])) {
                 $matchingInterface = $typeInterface[$itype];
                 break;
             }
         }
     } elseif ($type !== false) {
         $matchingInterface = isset($typeInterface[$type]) ? $typeInterface[$type] : array();
     } else {
         $matchingInterface = false;
     }
     return $matchingInterface;
 }
    $macros = order_macros($dbHost['macros'], 'macro');
    $host_groups = zbx_objectValues($dbHost['groups'], 'groupid');
    $host_inventory = $dbHost['inventory'];
    $inventory_mode = empty($host_inventory) ? HOST_INVENTORY_DISABLED : $dbHost['inventory']['inventory_mode'];
    $templateIds = array();
    foreach ($original_templates as $tpl) {
        $templateIds[$tpl['templateid']] = $tpl['templateid'];
    }
    $interfaces = $dbHost['interfaces'];
    foreach ($interfaces as $hinum => $interface) {
        $interfaces[$hinum]['items'] = 0;
        $interfaces[$hinum]['items'] = count($dbHost['interfaces'][$interface['interfaceid']]['items']);
        // check if interface has items that require specific interface type, if so type cannot be changed
        $locked = 0;
        foreach ($dbHost['interfaces'][$interface['interfaceid']]['items'] as $item) {
            $itemInterfaceType = itemTypeInterface($item['type']);
            if (!($itemInterfaceType === false || $itemInterfaceType === INTERFACE_TYPE_ANY)) {
                $locked = 1;
                break;
            }
        }
        $interfaces[$hinum]['locked'] = $locked;
    }
}
$clear_templates = array_intersect($clear_templates, array_keys($original_templates));
$clear_templates = array_diff($clear_templates, array_keys($templateIds));
natcasesort($templateIds);
// whether this is a discovered host
$isDiscovered = get_request('hostid') && $dbHost['flags'] == ZBX_FLAG_DISCOVERY_CREATED && get_request('form') == 'update';
$frmHost = new CForm();
$frmHost->setName('web.hosts.host.php.');
    // render view
    $itemView = new CView('configuration.item.edit', $data);
    $itemView->render();
    $itemView->show();
} elseif ($_REQUEST['go'] == 'massupdate' || isset($_REQUEST['massupdate']) && isset($_REQUEST['group_itemid'])) {
    $data = array('form' => get_request('form', null), 'hostid' => get_request('hostid', null), 'itemids' => get_request('group_itemid', array()), 'description' => get_request('description', ''), 'delay' => get_request('delay', ZBX_ITEM_DELAY_DEFAULT), 'delay_flex' => get_request('delay_flex', array()), 'history' => get_request('history', 90), 'status' => get_request('status', 0), 'type' => get_request('type', 0), 'interfaceid' => get_request('interfaceid', 0), 'snmp_community' => get_request('snmp_community', 'public'), 'port' => get_request('port', ''), 'value_type' => get_request('value_type', ITEM_VALUE_TYPE_UINT64), 'data_type' => get_request('data_type', ITEM_DATA_TYPE_DECIMAL), 'trapper_hosts' => get_request('trapper_hosts', ''), 'units' => get_request('units', ''), 'authtype' => get_request('authtype', ''), 'username' => get_request('username', ''), 'password' => get_request('password', ''), 'publickey' => get_request('publickey', ''), 'privatekey' => get_request('privatekey', ''), 'valuemapid' => get_request('valuemapid', 0), 'delta' => get_request('delta', 0), 'trends' => get_request('trends', DAY_IN_YEAR), 'applications' => get_request('applications', array()), 'snmpv3_securityname' => get_request('snmpv3_securityname', ''), 'snmpv3_securitylevel' => get_request('snmpv3_securitylevel', 0), 'snmpv3_authpassphrase' => get_request('snmpv3_authpassphrase', ''), 'snmpv3_privpassphrase' => get_request('snmpv3_privpassphrase', ''), 'formula' => get_request('formula', '1'), 'logtimefmt' => get_request('logtimefmt', ''), 'initial_item_type' => null, 'multiple_interface_types' => false);
    // hosts
    $data['hosts'] = API::Host()->get(array('itemids' => $data['itemids'], 'selectInterfaces' => API_OUTPUT_EXTEND));
    $data['is_multiple_hosts'] = count($data['hosts']) > 1;
    if (!$data['is_multiple_hosts']) {
        $data['hosts'] = reset($data['hosts']);
        // set the initial chosen interface to one of the interfaces the items use
        $items = API::Item()->get(array('itemids' => zbx_objectValues($data['hosts']['items'], 'itemid'), 'output' => array('itemid', 'type')));
        $usedInterfacesTypes = array();
        foreach ($items as $item) {
            $usedInterfacesTypes[$item['type']] = itemTypeInterface($item['type']);
        }
        $initialItemType = min(array_keys($usedInterfacesTypes));
        $data['type'] = get_request('type') !== null ? $data['type'] : $initialItemType;
        $data['initial_item_type'] = $initialItemType;
        $data['multiple_interface_types'] = count(array_unique($usedInterfacesTypes)) > 1;
    }
    // application
    if (count($data['applications']) == 0) {
        array_push($data['applications'], 0);
    }
    if (!empty($data['hostid'])) {
        $data['db_applications'] = DBfetchArray(DBselect('SELECT a.applicationid,a.name' . ' FROM applications a' . ' WHERE a.hostid=' . $data['hostid'] . ' ORDER BY a.name'));
    }
    // item types
    $data['itemTypes'] = item_type2str();
;
				organizeInterfaces(itemIntefaceTypes[<?php 
echo CJs::encodeJson($data['initial_item_type']);
?>
]);
			}
			else {
				jQuery('#type').trigger('change');
			}
		});

		jQuery('#type')
			.change(function() {
				// update the interface select with each item type change
				var itemIntefaceTypes = <?php 
echo CJs::encodeJson(itemTypeInterface());
?>
;
				organizeInterfaces(itemIntefaceTypes[parseInt(jQuery(this).val())]);
			})
			.trigger('change');

		if (jQuery('#visible_delay_flex').length != 0) {
			displayNewDeleyFlexInterval();

			jQuery('#visible_delay_flex').click(function() {
				displayNewDeleyFlexInterval();
			});
		}

		// create jquery buttonset object when authprotocol visible box is switched on
Example #5
0
function copyItemsToHosts($srcItemIds, $dstHostIds)
{
    $srcItems = API::Item()->get(array('itemids' => $srcItemIds, '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'), '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' => $dstHostIds, 'preservekeys' => true, 'nopermissions' => true, 'templated_hosts' => true));
    foreach ($dstHosts as $dstHost) {
        $interfaceids = array();
        foreach ($dstHost['interfaces'] as $interface) {
            if ($interface['main'] == 1) {
                $interfaceids[$interface['type']] = $interface['interfaceid'];
            }
        }
        foreach ($srcItems as &$srcItem) {
            if ($dstHost['status'] != HOST_STATUS_TEMPLATE) {
                $type = itemTypeInterface($srcItem['type']);
                if ($type == INTERFACE_TYPE_ANY) {
                    foreach (array(INTERFACE_TYPE_AGENT, INTERFACE_TYPE_SNMP, INTERFACE_TYPE_JMX, INTERFACE_TYPE_IPMI) as $itype) {
                        if (isset($interfaceids[$itype])) {
                            $srcItem['interfaceid'] = $interfaceids[$itype];
                            break;
                        }
                    }
                } elseif ($type !== false) {
                    if (!isset($interfaceids[$type])) {
                        error(_s('Cannot find host interface on "%1$s" for item key "%2$s".', $dstHost['host'], $srcItem['key_']));
                        return false;
                    }
                    $srcItem['interfaceid'] = $interfaceids[$type];
                }
            }
            unset($srcItem['itemid']);
            $srcItem['hostid'] = $dstHost['hostid'];
            $srcItem['applications'] = get_same_applications_for_host(zbx_objectValues($srcItem['applications'], 'applicationid'), $dstHost['hostid']);
        }
        if (!API::Item()->create($srcItems)) {
            return false;
        }
    }
    return true;
}
 /**
  * Create host interfaces from the host properties and items and add them to the "host" element.
  *
  * @param array $host
  *
  * @return array
  */
 protected function convertHostInterfaces(array $host)
 {
     $interfaces = [];
     $i = 0;
     // create an agent interface from the host properties
     if (isset($host['useip']) && isset($host['ip']) && isset($host['dns']) && isset($host['port'])) {
         $agentInterface = ['type' => INTERFACE_TYPE_AGENT, 'useip' => $host['useip'], 'ip' => $host['ip'], 'dns' => $host['dns'], 'port' => $host['port'], 'default' => INTERFACE_PRIMARY, 'interface_ref' => 'if' . $i];
         $interfaces[] = $agentInterface;
         $i++;
     }
     $hasIpmiItem = false;
     $snmpItems = [];
     if (isset($host['items']) && $host['items']) {
         foreach ($host['items'] as $item) {
             if (!isset($item['type'])) {
                 continue;
             }
             if ($item['type'] == ITEM_TYPE_IPMI) {
                 $hasIpmiItem = true;
             }
             if ($item['type'] == ITEM_TYPE_SNMPV1 || $item['type'] == ITEM_TYPE_SNMPV2C || $item['type'] == ITEM_TYPE_SNMPV3) {
                 $snmpItems[] = $item;
             }
         }
         // if a least one IPMI item exists on a host, create an IPMI interface
         if ($hasIpmiItem) {
             $ipmiInterface = ['type' => INTERFACE_TYPE_IPMI, 'useip' => INTERFACE_USE_IP, 'ip' => array_key_exists('ipmi_ip', $host) && $host['ipmi_ip'] !== '' ? $host['ipmi_ip'] : $host['ip'], 'dns' => '', 'port' => array_key_exists('ipmi_port', $host) && $host['ipmi_port'] !== '' ? $host['ipmi_port'] : '623', 'default' => INTERFACE_PRIMARY, 'interface_ref' => 'if' . $i];
             $interfaces[] = $ipmiInterface;
             $i++;
         }
         // if SNMP item exist, create an SNMP interface for each SNMP item port.
         if ($snmpItems) {
             $snmpInterfaces = [];
             foreach ($snmpItems as $item) {
                 if (!isset($item['snmp_port']) || isset($snmpInterfaces[$item['snmp_port']])) {
                     continue;
                 }
                 $snmpInterface = ['type' => INTERFACE_TYPE_SNMP, 'useip' => $host['useip'], 'ip' => $host['ip'], 'dns' => $host['dns'], 'port' => $item['snmp_port'], 'default' => count($snmpInterfaces) ? INTERFACE_SECONDARY : INTERFACE_PRIMARY, 'interface_ref' => 'if' . $i];
                 $snmpInterfaces[$item['snmp_port']] = $snmpInterface;
                 $interfaces[] = $snmpInterface;
                 $i++;
             }
         }
     }
     if ($interfaces) {
         $host['interfaces'] = $interfaces;
     }
     // map items to new interfaces
     if (isset($host['items']) && $host['items']) {
         foreach ($host['items'] as &$item) {
             if (!isset($item['type'])) {
                 continue;
             }
             $interfaceType = itemTypeInterface($item['type']);
             switch ($interfaceType) {
                 case INTERFACE_TYPE_AGENT:
                 case INTERFACE_TYPE_ANY:
                     $item['interface_ref'] = $agentInterface['interface_ref'];
                     break;
                 case INTERFACE_TYPE_IPMI:
                     $item['interface_ref'] = $ipmiInterface['interface_ref'];
                     break;
                 case INTERFACE_TYPE_SNMP:
                     if (isset($item['snmp_port'])) {
                         $item['interface_ref'] = $snmpInterfaces[$item['snmp_port']]['interface_ref'];
                     }
                     break;
             }
         }
         unset($item);
     }
     return $host;
 }