/** * Get mapping for value. * * @param int|float|string $value Value that mapping should be applied to. * @param string $valuemapid Value map ID which should be used. * * @return string|bool If there is no mapping return false, return mapped value otherwise. */ function getMappedValue($value, $valuemapid) { static $valuemaps = []; if ($valuemapid == 0) { return false; } if (!array_key_exists($valuemapid, $valuemaps)) { $valuemaps[$valuemapid] = []; $db_valuemaps = API::ValueMap()->get(['output' => [], 'selectMappings' => ['value', 'newvalue'], 'valuemapids' => [$valuemapid]]); if ($db_valuemaps) { foreach ($db_valuemaps[0]['mappings'] as $mapping) { $valuemaps[$valuemapid][$mapping['value']] = $mapping['newvalue']; } } } return array_key_exists((string) $value, $valuemaps[$valuemapid]) ? $valuemaps[$valuemapid][(string) $value] : false; }
$items = API::Item()->get(['itemids' => zbx_objectValues($data['hosts']['items'], 'itemid'), 'output' => ['itemid', 'type']]); $usedInterfacesTypes = []; foreach ($items as $item) { $usedInterfacesTypes[$item['type']] = itemTypeInterface($item['type']); } $initialItemType = min(array_keys($usedInterfacesTypes)); $data['type'] = getRequest('type') !== null ? $data['type'] : $initialItemType; $data['initial_item_type'] = $initialItemType; $data['multiple_interface_types'] = count(array_unique($usedInterfacesTypes)) > 1; } } // item types $data['itemTypes'] = item_type2str(); unset($data['itemTypes'][ITEM_TYPE_HTTPTEST]); // valuemap $data['valuemaps'] = API::ValueMap()->get(['output' => ['valuemapid', 'name']]); CArrayHelper::sort($data['valuemaps'], ['name']); if (!$data['delay_flex']) { $data['delay_flex'][] = ['delay' => '', 'period' => '', 'type' => ITEM_DELAY_FLEX_TYPE_FLEXIBLE]; } // render view $itemView = new CView('configuration.item.massupdate', $data); $itemView->render(); $itemView->show(); } elseif (hasRequest('action') && getRequest('action') == 'item.masscopyto' && hasRequest('group_itemid')) { // render view $data = getCopyElementsFormData('group_itemid', _('Items')); $data['action'] = 'item.masscopyto'; $graphView = new CView('configuration.copy.elements', $data); $graphView->render(); $graphView->show();
/** * Get value maps for export builder from database. * * @param array $valuemapids * * return array */ protected function gatherValueMaps(array $valuemapids) { $this->data['valueMaps'] = API::ValueMap()->get(['output' => ['valuemapid', 'name'], 'selectMappings' => ['value', 'newvalue'], 'valuemapids' => $valuemapids, 'preservekeys' => true]); }
/** * 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']; $data['hostid'] = $data['item']['hostid']; $data['params'] = $data['item']['params']; $data['snmpv3_contextname'] = $data['item']['snmpv3_contextname']; $data['snmpv3_securityname'] = $data['item']['snmpv3_securityname']; $data['snmpv3_securitylevel'] = $data['item']['snmpv3_securitylevel']; $data['snmpv3_authprotocol'] = $data['item']['snmpv3_authprotocol']; $data['snmpv3_authpassphrase'] = $data['item']['snmpv3_authpassphrase']; $data['snmpv3_privprotocol'] = $data['item']['snmpv3_privprotocol']; $data['snmpv3_privpassphrase'] = $data['item']['snmpv3_privpassphrase']; $data['ipmi_sensor'] = $data['item']['ipmi_sensor']; $data['authtype'] = $data['item']['authtype']; $data['username'] = $data['item']['username']; $data['password'] = $data['item']['password']; $data['publickey'] = $data['item']['publickey']; $data['privatekey'] = $data['item']['privatekey']; $data['logtimefmt'] = $data['item']['logtimefmt']; $data['new_application'] = getRequest('new_application', ''); if ($data['parent_discoveryid'] != 0) { $data['new_application_prototype'] = getRequest('new_application_prototype', ''); } if (!$data['is_discovery_rule']) { $data['formula'] = $data['item']['formula']; } if (!$data['limited'] || !isset($_REQUEST['form_refresh'])) { $data['delay'] = $data['item']['delay']; if (($data['type'] == ITEM_TYPE_TRAPPER || $data['type'] == ITEM_TYPE_SNMPTRAP) && $data['delay'] == 0) { $data['delay'] = ZBX_ITEM_DELAY_DEFAULT; } $data['history'] = $data['item']['history']; $data['status'] = $data['item']['status']; $data['delta'] = $data['item']['delta']; $data['trends'] = $data['item']['trends']; $parser = new CItemDelayFlexParser($data['item']['delay_flex']); if ($parser->isValid()) { foreach ($parser->getIntervals() as $interval) { if ($interval['type'] == ITEM_DELAY_FLEX_TYPE_FLEXIBLE) { $interval_parts = explode('/', $interval['interval']); $data['delay_flex'][] = ['delay' => $interval_parts[0], 'period' => $interval_parts[1], 'type' => ITEM_DELAY_FLEX_TYPE_FLEXIBLE]; } else { $data['delay_flex'][] = ['schedule' => $interval['interval'], 'type' => ITEM_DELAY_FLEX_TYPE_SCHEDULING]; } } } $data['applications'] = array_unique(zbx_array_merge($data['applications'], get_applications_by_itemid($data['itemid']))); if ($data['parent_discoveryid'] != 0) { /* * Get a list of application prototypes assigned to item prototype. Don't select distinct names, * since database can be accidentally created case insensitive. */ $application_prototypes = DBfetchArray(DBselect('SELECT ap.name' . ' FROM application_prototype ap,item_application_prototype iap' . ' WHERE ap.application_prototypeid=iap.application_prototypeid' . ' AND ap.itemid=' . zbx_dbstr($data['parent_discoveryid']) . ' AND iap.itemid=' . zbx_dbstr($data['itemid']))); // Merge form submitted data with data existing in DB to find diff and correctly display ListBox. $data['application_prototypes'] = array_unique(zbx_array_merge($data['application_prototypes'], zbx_objectValues($application_prototypes, 'name'))); } } } if (!$data['delay_flex']) { $data['delay_flex'][] = ['delay' => '', 'period' => '', 'type' => ITEM_DELAY_FLEX_TYPE_FLEXIBLE]; } // applications if (count($data['applications']) == 0) { array_push($data['applications'], 0); } $data['db_applications'] = DBfetchArray(DBselect('SELECT DISTINCT a.applicationid,a.name' . ' FROM applications a' . ' WHERE a.hostid=' . zbx_dbstr($data['hostid']) . ($data['parent_discoveryid'] ? ' AND a.flags=' . ZBX_FLAG_DISCOVERY_NORMAL : ''))); order_result($data['db_applications'], 'name'); if ($data['parent_discoveryid'] != 0) { // Make the application prototype list no appearing empty, but filling it with "-None-" as first element. if (count($data['application_prototypes']) == 0) { $data['application_prototypes'][] = 0; } // Get a list of application prototypes by discovery rule. $data['db_application_prototypes'] = DBfetchArray(DBselect('SELECT ap.application_prototypeid,ap.name' . ' FROM application_prototype ap' . ' WHERE ap.itemid=' . zbx_dbstr($data['parent_discoveryid']))); order_result($data['db_application_prototypes'], 'name'); } // interfaces $data['interfaces'] = API::HostInterface()->get(['hostids' => $data['hostid'], 'output' => API_OUTPUT_EXTEND]); // valuemapid if ($data['limited']) { if ($data['valuemapid'] != 0) { $valuemaps = API::ValueMap()->get(['output' => ['name'], 'valuemapids' => [$data['valuemapid']]]); if ($valuemaps) { $data['valuemaps'] = $valuemaps[0]['name']; } } } else { $data['valuemaps'] = API::ValueMap()->get(['output' => ['valemapid', 'name']]); CArrayHelper::sort($data['valuemaps'], ['name']); } // possible host inventories if (empty($data['parent_discoveryid'])) { $data['possibleHostInventories'] = getHostInventories(); // get already populated fields by other items $data['alreadyPopulated'] = API::item()->get(['output' => ['inventory_link'], 'filter' => ['hostid' => $data['hostid']], 'nopermissions' => true]); $data['alreadyPopulated'] = zbx_toHash($data['alreadyPopulated'], 'inventory_link'); } // unset snmpv3 fields if ($data['type'] != ITEM_TYPE_SNMPV3) { $data['snmpv3_contextname'] = ''; $data['snmpv3_securityname'] = ''; $data['snmpv3_securitylevel'] = ITEM_SNMPV3_SECURITYLEVEL_NOAUTHNOPRIV; $data['snmpv3_authprotocol'] = ITEM_AUTHPROTOCOL_MD5; $data['snmpv3_authpassphrase'] = ''; $data['snmpv3_privprotocol'] = ITEM_PRIVPROTOCOL_DES; $data['snmpv3_privpassphrase'] = ''; } // unset ssh auth fields if ($data['type'] != ITEM_TYPE_SSH) { $data['authtype'] = ITEM_AUTHTYPE_PASSWORD; $data['publickey'] = ''; $data['privatekey'] = ''; } return $data; }
/** * Select value map IDs for previously added value map names. */ protected function selectValueMaps() { if ($this->valueMaps) { $this->valueMapsRefs = []; $valuemaps = API::ValueMap()->get(['output' => ['valeumapid', 'name'], 'filter' => ['name' => $this->valueMaps]]); foreach ($valuemaps as $valuemap) { $this->valueMapsRefs[$valuemap['name']] = $valuemap['valuemapid']; } $this->valueMaps = []; } }
/** * Import value maps. */ protected function processValueMaps() { if (!$this->options['valueMaps']['createMissing'] && !$this->options['valueMaps']['updateExisting']) { return; } $all_valuemaps = $this->getFormattedValueMaps(); if (!$all_valuemaps) { return; } $valuemaps_to_create = []; $valuemaps_to_update = []; foreach ($all_valuemaps as $valuemap) { $valuemapid = $this->referencer->resolveValueMap($valuemap['name']); if ($valuemapid) { $valuemap['valuemapid'] = $valuemapid; $valuemaps_to_update[] = $valuemap; } else { $valuemaps_to_create[] = $valuemap; } } if ($this->options['valueMaps']['createMissing'] && $valuemaps_to_create) { $valuemapids = API::ValueMap()->create($valuemaps_to_create); foreach ($valuemaps_to_create as $key => $valuemap) { $this->referencer->addValueMapRef($valuemap['name'], $valuemapids['valuemapids'][$key]); } } if ($this->options['valueMaps']['updateExisting'] && $valuemaps_to_update) { API::ValueMap()->update($valuemaps_to_update); } }
} else { $data['name'] = getRequest('name', ''); $data['mappings'] = getRequest('mappings', []); } if ($data['valuemapid'] != 0) { $data['valuemap_count'] += API::Item()->get(['countOutput' => true, 'webitems' => true, 'filter' => ['valuemapid' => $data['valuemapid']]]); $data['valuemap_count'] += API::ItemPrototype()->get(['countOutput' => true, 'filter' => ['valuemapid' => $data['valuemapid']]]); } if (!$data['mappings']) { $data['mappings'][] = ['value' => '', 'newvalue' => '']; } $view = new CView('administration.general.valuemapping.edit', $data); } else { $sortfield = getRequest('sort', CProfile::get('web.' . $page['file'] . '.sort', 'name')); $sortorder = getRequest('sortorder', CProfile::get('web.' . $page['file'] . '.sortorder', ZBX_SORT_UP)); CProfile::update('web.' . $page['file'] . '.sort', $sortfield, PROFILE_TYPE_STR); CProfile::update('web.' . $page['file'] . '.sortorder', $sortorder, PROFILE_TYPE_STR); $data = ['sort' => $sortfield, 'sortorder' => $sortorder]; $data['valuemaps'] = API::ValueMap()->get(['output' => ['valuemapid', 'name'], 'selectMappings' => ['value', 'newvalue'], 'sortfield' => $sortfield, 'limit' => $config['search_limit'] + 1]); order_result($data['valuemaps'], $sortfield, $sortorder); $data['paging'] = getPagingLine($data['valuemaps'], $sortorder, new CUrl('adm.valuemapping.php')); foreach ($data['valuemaps'] as &$valuemap) { order_result($valuemap['mappings'], 'value'); $valuemap['used_in_items'] = (bool) API::Item()->get(['output' => [], 'webitems' => true, 'filter' => ['valuemapid' => $valuemap['valuemapid']], 'limit' => 1]) || (bool) API::ItemPrototype()->get(['output' => [], 'filter' => ['valuemapid' => $valuemap['valuemapid']], 'limit' => 1]); } unset($valuemap); $view = new CView('administration.general.valuemapping.list', $data); } $view->render(); $view->show(); require_once dirname(__FILE__) . '/include/page_footer.php';