/** * Prepare map elements data. * Calculate problem triggers and priorities. Populate map elements with automatic icon mapping, acknowledging and * recent change markers. * * @param array $sysmap * @param int $options * @param int $options['severity_min'] Minimum trigger severity, default value is maximal (Disaster) * * @return array */ function getSelementsInfo($sysmap, array $options = array()) { if (!isset($options['severity_min'])) { $options['severity_min'] = TRIGGER_SEVERITY_NOT_CLASSIFIED; } $config = select_config(); $showUnacknowledged = $config['event_ack_enable'] ? $sysmap['show_unack'] : EXTACK_OPTION_ALL; $triggerIdToSelementIds = array(); $subSysmapTriggerIdToSelementIds = array(); $hostGroupIdToSelementIds = array(); $hostIdToSelementIds = array(); if ($sysmap['sysmapid']) { $iconMap = API::IconMap()->get(array('sysmapids' => $sysmap['sysmapid'], 'selectMappings' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND)); $iconMap = reset($iconMap); } $hostsToGetInventories = array(); $selements = $sysmap['selements']; $selementIdToSubSysmaps = array(); foreach ($selements as $selementId => &$selement) { $selement['hosts'] = array(); $selement['triggers'] = array(); switch ($selement['elementtype']) { case SYSMAP_ELEMENT_TYPE_MAP: $sysmapIds = array($selement['elementid']); while (!empty($sysmapIds)) { $subSysmaps = API::Map()->get(array('sysmapids' => $sysmapIds, 'output' => array('sysmapid'), 'selectSelements' => API_OUTPUT_EXTEND, 'nopermissions' => true, 'preservekeys' => true)); if (!isset($selementIdToSubSysmaps[$selementId])) { $selementIdToSubSysmaps[$selementId] = array(); } $selementIdToSubSysmaps[$selementId] += $subSysmaps; $sysmapIds = array(); foreach ($subSysmaps as $subSysmap) { foreach ($subSysmap['selements'] as $subSysmapSelement) { switch ($subSysmapSelement['elementtype']) { case SYSMAP_ELEMENT_TYPE_MAP: $sysmapIds[] = $subSysmapSelement['elementid']; break; case SYSMAP_ELEMENT_TYPE_HOST_GROUP: $hostGroupIdToSelementIds[$subSysmapSelement['elementid']][$selementId] = $selementId; break; case SYSMAP_ELEMENT_TYPE_HOST: $hostIdToSelementIds[$subSysmapSelement['elementid']][$selementId] = $selementId; break; case SYSMAP_ELEMENT_TYPE_TRIGGER: $subSysmapTriggerIdToSelementIds[$subSysmapSelement['elementid']][$selementId] = $selementId; break; } } } } break; case SYSMAP_ELEMENT_TYPE_HOST_GROUP: $hostGroupId = $selement['elementid']; $hostGroupIdToSelementIds[$hostGroupId][$selementId] = $selementId; break; case SYSMAP_ELEMENT_TYPE_HOST: $hostId = $selement['elementid']; $hostIdToSelementIds[$hostId][$selementId] = $selementId; // if we have icon map applied, we need to get inventories for all hosts, // where automatic icon selection is enabled. if ($sysmap['iconmapid'] && $selement['use_iconmap']) { $hostsToGetInventories[] = $hostId; } break; case SYSMAP_ELEMENT_TYPE_TRIGGER: $triggerId = $selement['elementid']; $triggerIdToSelementIds[$triggerId][$selementId] = $selementId; break; } } unset($selement); // get host inventories if ($sysmap['iconmapid']) { $hostInventories = API::Host()->get(array('hostids' => $hostsToGetInventories, 'output' => array('hostid'), 'nopermissions' => true, 'preservekeys' => true, 'selectInventory' => API_OUTPUT_EXTEND)); } $allHosts = array(); if (!empty($hostIdToSelementIds)) { $hosts = API::Host()->get(array('hostids' => array_keys($hostIdToSelementIds), 'output' => array('name', 'status', 'maintenance_status', 'maintenanceid'), 'nopermissions' => true, 'preservekeys' => true)); $allHosts = array_merge($allHosts, $hosts); foreach ($hosts as $hostId => $host) { foreach ($hostIdToSelementIds[$hostId] as $selementId) { $selements[$selementId]['hosts'][$hostId] = $hostId; } } } $hostsFromHostGroups = array(); if (!empty($hostGroupIdToSelementIds)) { $hostsFromHostGroups = API::Host()->get(array('groupids' => array_keys($hostGroupIdToSelementIds), 'output' => array('name', 'status', 'maintenance_status', 'maintenanceid'), 'selectGroups' => array('groupid'), 'nopermissions' => true, 'preservekeys' => true)); foreach ($hostsFromHostGroups as $hostId => $host) { foreach ($host['groups'] as $group) { $groupId = $group['groupid']; if (isset($hostGroupIdToSelementIds[$groupId])) { foreach ($hostGroupIdToSelementIds[$groupId] as $selementId) { $selement =& $selements[$selementId]; $selement['hosts'][$hostId] = $hostId; // add hosts to hosts_map for trigger selection; if (!isset($hostIdToSelementIds[$hostId])) { $hostIdToSelementIds[$hostId] = array(); } $hostIdToSelementIds[$hostId][$selementId] = $selementId; unset($selement); } } } } $allHosts = array_merge($allHosts, $hostsFromHostGroups); } $allHosts = zbx_toHash($allHosts, 'hostid'); // get triggers data, triggers from current map, select all $allTriggers = array(); if (!empty($triggerIdToSelementIds)) { $triggerOptions = array('output' => array('triggerid', 'status', 'value', 'priority', 'lastchange', 'description', 'expression'), 'triggerids' => array_keys($triggerIdToSelementIds), 'filter' => array('state' => null), 'nopermissions' => true, 'preservekeys' => true); if ($showUnacknowledged) { $triggerOptions['selectLastEvent'] = array('acknowledged'); } $triggers = API::Trigger()->get($triggerOptions); $allTriggers = array_merge($allTriggers, $triggers); foreach ($triggers as $triggerId => $trigger) { foreach ($triggerIdToSelementIds[$triggerId] as $selementId) { $selements[$selementId]['triggers'][$triggerId] = $triggerId; } } } // triggers from submaps, skip dependent if (!empty($subSysmapTriggerIdToSelementIds)) { $triggerOptions = array('output' => array('triggerid', 'status', 'value', 'priority', 'lastchange', 'description', 'expression'), 'triggerids' => array_keys($subSysmapTriggerIdToSelementIds), 'filter' => array('state' => null), 'skipDependent' => true, 'nopermissions' => true, 'preservekeys' => true, 'only_true' => true); if ($showUnacknowledged) { $triggerOptions['selectLastEvent'] = array('acknowledged'); } $triggers = API::Trigger()->get($triggerOptions); $allTriggers = array_merge($allTriggers, $triggers); foreach ($triggers as $triggerId => $trigger) { foreach ($subSysmapTriggerIdToSelementIds[$triggerId] as $selementId) { $selements[$selementId]['triggers'][$triggerId] = $triggerId; } } } $monitoredHostIds = array(); foreach ($allHosts as $hostId => $host) { if ($host['status'] == HOST_STATUS_MONITORED) { $monitoredHostIds[$hostId] = $hostId; } } // triggers from all hosts/hostgroups, skip dependent if ($monitoredHostIds) { $triggerOptions = array('output' => array('triggerid', 'status', 'value', 'priority', 'lastchange', 'description', 'expression'), 'selectHosts' => array('hostid'), 'selectItems' => array('itemid'), 'hostids' => $monitoredHostIds, 'filter' => array('state' => null), 'monitored' => true, 'skipDependent' => true, 'nopermissions' => true, 'preservekeys' => true, 'only_true' => true); if ($showUnacknowledged) { $triggerOptions['selectLastEvent'] = array('acknowledged'); } $triggersFromMonitoredHosts = API::Trigger()->get($triggerOptions); foreach ($triggersFromMonitoredHosts as $triggerId => $trigger) { foreach ($trigger['hosts'] as $host) { $hostId = $host['hostid']; if (isset($hostIdToSelementIds[$hostId])) { foreach ($hostIdToSelementIds[$hostId] as $selementId) { $selements[$selementId]['triggers'][$triggerId] = $triggerId; } } } } $subSysmapHostApplicationFilters = getSelementHostApplicationFilters($selements, $selementIdToSubSysmaps, $hostsFromHostGroups); $selements = filterSysmapTriggers($selements, $subSysmapHostApplicationFilters, $triggersFromMonitoredHosts, $subSysmapTriggerIdToSelementIds); $allTriggers = array_merge($allTriggers, $triggersFromMonitoredHosts); } $allTriggers = zbx_toHash($allTriggers, 'triggerid'); $info = array(); foreach ($selements as $selementId => $selement) { $i = array('disabled' => 0, 'maintenance' => 0, 'problem' => 0, 'problem_unack' => 0, 'priority' => 0, 'trigger_disabled' => 0, 'latelyChanged' => false, 'ack' => true); foreach ($selement['hosts'] as $hostId) { $host = $allHosts[$hostId]; $last_hostid = $hostId; if ($host['status'] == HOST_STATUS_NOT_MONITORED) { $i['disabled']++; } elseif ($host['maintenance_status'] == HOST_MAINTENANCE_STATUS_ON) { $i['maintenance']++; } } foreach ($selement['triggers'] as $triggerId) { $trigger = $allTriggers[$triggerId]; if ($options['severity_min'] <= $trigger['priority']) { if ($trigger['status'] == TRIGGER_STATUS_DISABLED) { $i['trigger_disabled']++; } else { if ($trigger['value'] == TRIGGER_VALUE_TRUE) { $i['problem']++; $lastProblemId = $triggerId; if ($i['priority'] < $trigger['priority']) { $i['priority'] = $trigger['priority']; } } if ($showUnacknowledged && $trigger['lastEvent'] && !$trigger['lastEvent']['acknowledged']) { $i['problem_unack']++; } $i['latelyChanged'] |= time() - $trigger['lastchange'] < $config['blink_period']; } } } $i['ack'] = (bool) (!$i['problem_unack']); if ($sysmap['expandproblem'] && $i['problem'] == 1) { if (!isset($lastProblemId)) { $lastProblemId = null; } $i['problem_title'] = CMacrosResolverHelper::resolveTriggerName($allTriggers[$lastProblemId]); } if ($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST && $i['maintenance'] == 1) { $mnt = get_maintenance_by_maintenanceid($allHosts[$last_hostid]['maintenanceid']); $i['maintenance_title'] = $mnt['name']; } // replace default icons if (!$selement['iconid_on']) { $selement['iconid_on'] = $selement['iconid_off']; } if (!$selement['iconid_maintenance']) { $selement['iconid_maintenance'] = $selement['iconid_off']; } if (!$selement['iconid_disabled']) { $selement['iconid_disabled'] = $selement['iconid_off']; } switch ($selement['elementtype']) { case SYSMAP_ELEMENT_TYPE_MAP: $info[$selementId] = getMapsInfo($selement, $i, $showUnacknowledged); break; case SYSMAP_ELEMENT_TYPE_HOST_GROUP: $info[$selementId] = getHostGroupsInfo($selement, $i, $showUnacknowledged); break; case SYSMAP_ELEMENT_TYPE_HOST: $info[$selementId] = getHostsInfo($selement, $i, $showUnacknowledged); if ($sysmap['iconmapid'] && $selement['use_iconmap']) { $info[$selementId]['iconid'] = getIconByMapping($iconMap, $hostInventories[$selement['elementid']]); } break; case SYSMAP_ELEMENT_TYPE_TRIGGER: $info[$selementId] = getTriggersInfo($selement, $i, $showUnacknowledged); break; case SYSMAP_ELEMENT_TYPE_IMAGE: $info[$selementId] = getImagesInfo($selement); break; } } if ($sysmap['label_format'] == SYSMAP_LABEL_ADVANCED_OFF) { $hlabel = $hglabel = $tlabel = $mlabel = $sysmap['label_type'] == MAP_LABEL_TYPE_NAME; } else { $hlabel = $sysmap['label_type_host'] == MAP_LABEL_TYPE_NAME; $hglabel = $sysmap['label_type_hostgroup'] == MAP_LABEL_TYPE_NAME; $tlabel = $sysmap['label_type_trigger'] == MAP_LABEL_TYPE_NAME; $mlabel = $sysmap['label_type_map'] == MAP_LABEL_TYPE_NAME; } // get names if needed $elems = separateMapElements($sysmap); if (!empty($elems['sysmaps']) && $mlabel) { $subSysmaps = API::Map()->get(array('sysmapids' => zbx_objectValues($elems['sysmaps'], 'elementid'), 'nopermissions' => true, 'output' => array('name'))); $subSysmaps = zbx_toHash($subSysmaps, 'sysmapid'); foreach ($elems['sysmaps'] as $elem) { $info[$elem['selementid']]['name'] = $subSysmaps[$elem['elementid']]['name']; } } if (!empty($elems['hostgroups']) && $hglabel) { $hostgroups = API::HostGroup()->get(array('groupids' => zbx_objectValues($elems['hostgroups'], 'elementid'), 'nopermissions' => true, 'output' => array('name'))); $hostgroups = zbx_toHash($hostgroups, 'groupid'); foreach ($elems['hostgroups'] as $elem) { $info[$elem['selementid']]['name'] = $hostgroups[$elem['elementid']]['name']; } } if (!empty($elems['triggers']) && $tlabel) { foreach ($elems['triggers'] as $elem) { $info[$elem['selementid']]['name'] = CMacrosResolverHelper::resolveTriggerName($allTriggers[$elem['elementid']]); } } if (!empty($elems['hosts']) && $hlabel) { foreach ($elems['hosts'] as $elem) { $info[$elem['selementid']]['name'] = $allHosts[$elem['elementid']]['name']; } } return $info; }
function prepareMapExport(&$exportMaps) { $sysmaps = array(); $hostgroups = array(); $hosts = array(); $triggers = array(); $images = array(); foreach ($exportMaps as $mnum => $sysmap) { $selements = separateMapElements($sysmap); $sysmaps += zbx_objectValues($selements['sysmaps'], 'elementid'); $hostgroups += zbx_objectValues($selements['hostgroups'], 'elementid'); $hosts += zbx_objectValues($selements['hosts'], 'elementid'); $triggers += zbx_objectValues($selements['triggers'], 'elementid'); $images += zbx_objectValues($selements['images'], 'elementid'); foreach ($sysmap['selements'] as $snum => $selement) { if ($selement['iconid_off'] > 0) { $images[$selement['iconid_off']] = $selement['iconid_off']; } if ($selement['iconid_on'] > 0) { $images[$selement['iconid_on']] = $selement['iconid_on']; } if ($selement['iconid_unknown'] > 0) { $images[$selement['iconid_unknown']] = $selement['iconid_unknown']; } if ($selement['iconid_disabled'] > 0) { $images[$selement['iconid_disabled']] = $selement['iconid_disabled']; } if ($selement['iconid_maintenance'] > 0) { $images[$selement['iconid_maintenance']] = $selement['iconid_maintenance']; } } $images[$sysmap['backgroundid']] = $sysmap['backgroundid']; foreach ($sysmap['links'] as $lnum => $link) { foreach ($link['linktriggers'] as $ltnum => $linktrigger) { array_push($triggers, $linktrigger['triggerid']); } } } $sysmaps = sysmapIdents($sysmaps); $hostgroups = hostgroupIdents($hostgroups); $hosts = hostIdents($hosts); $triggers = triggerIdents($triggers); $images = imageIdents($images); try { foreach ($exportMaps as $mnum => &$sysmap) { unset($sysmap['sysmapid']); $sysmap['backgroundid'] = $sysmap['backgroundid'] > 0 ? $images[$sysmap['backgroundid']] : ''; foreach ($sysmap['selements'] as $snum => &$selement) { unset($selement['sysmapid']); switch ($selement['elementtype']) { case SYSMAP_ELEMENT_TYPE_MAP: $selement['elementid'] = $sysmaps[$selement['elementid']]; break; case SYSMAP_ELEMENT_TYPE_HOST_GROUP: $selement['elementid'] = $hostgroups[$selement['elementid']]; break; case SYSMAP_ELEMENT_TYPE_HOST: $selement['elementid'] = $hosts[$selement['elementid']]; break; case SYSMAP_ELEMENT_TYPE_TRIGGER: $selement['elementid'] = $triggers[$selement['elementid']]; break; case SYSMAP_ELEMENT_TYPE_IMAGE: default: $selement['elementid'] = $images[$selement['elementid']]; } $selement['iconid_off'] = $selement['iconid_off'] > 0 ? $images[$selement['iconid_off']] : ''; $selement['iconid_on'] = $selement['iconid_on'] > 0 ? $images[$selement['iconid_on']] : ''; $selement['iconid_unknown'] = $selement['iconid_unknown'] > 0 ? $images[$selement['iconid_unknown']] : ''; $selement['iconid_disabled'] = $selement['iconid_disabled'] > 0 ? $images[$selement['iconid_disabled']] : ''; $selement['iconid_maintenance'] = $selement['iconid_maintenance'] > 0 ? $images[$selement['iconid_maintenance']] : ''; } unset($selement); foreach ($sysmap['links'] as $lnum => &$link) { unset($link['sysmapid']); unset($link['linkid']); foreach ($link['linktriggers'] as $ltnum => &$linktrigger) { unset($linktrigger['linktriggerid']); unset($linktrigger['linkid']); $linktrigger['triggerid'] = $triggers[$linktrigger['triggerid']]; } } unset($linktrigger); unset($link); } unset($sysmap); } catch (Exception $e) { throw new Exception($e->getMessage()); } //SDII($exportMaps); }