protected function calcTriggers() { $this->triggers = array(); if ($this->m_showTriggers != 1) { return; } $max = 3; $cnt = 0; foreach ($this->items as $inum => $item) { $db_triggers = DBselect('SELECT DISTINCT h.host,tr.description,tr.triggerid,tr.expression,tr.priority,tr.value' . ' FROM triggers tr,functions f,items i,hosts h' . ' WHERE tr.triggerid=f.triggerid' . " AND f.function IN ('last','min','avg','max')" . ' AND tr.status=' . TRIGGER_STATUS_ENABLED . ' AND i.itemid=f.itemid' . ' AND h.hostid=i.hostid' . ' AND f.itemid=' . $item['itemid'] . ' ORDER BY tr.priority'); while (($trigger = DBfetch($db_triggers)) && $cnt < $max) { $db_fnc_cnt = DBselect('SELECT COUNT(*) AS cnt FROM functions f WHERE f.triggerid=' . $trigger['triggerid']); $fnc_cnt = DBfetch($db_fnc_cnt); if ($fnc_cnt['cnt'] != 1) { continue; } $trigger = API::UserMacro()->resolveTrigger($trigger); if (!preg_match('/^\\{([0-9]+)\\}\\s*?([\\<\\>\\=]{1})\\s*?([\\-0-9\\.]+)([TGMKsmhdw]?)$/', $trigger['expression'], $arr)) { continue; } $val = convert($arr[3] . $arr[4]); $minY = $this->m_minY[$this->items[$inum]['axisside']]; $maxY = $this->m_maxY[$this->items[$inum]['axisside']]; $this->triggers[] = array('skipdraw' => $val <= $minY || $val >= $maxY, 'y' => $this->sizeY - ($val - $minY) / ($maxY - $minY) * $this->sizeY + $this->shiftY, 'color' => getSeverityColor($trigger['priority']), 'description' => _('Trigger') . ': ' . CTriggerHelper::expandDescription($trigger), 'constant' => '[' . $arr[2] . ' ' . $arr[3] . $arr[4] . ']'); ++$cnt; } } }
function make_trigger_details($trigger) { $table = new CTableInfo(); if (is_show_all_nodes()) { $table->addRow(array(_('Node'), get_node_name_by_elid($trigger['triggerid']))); } $expression = explode_exp($trigger['expression'], true, true); $host = API::Host()->get(array('output' => array('name', 'hostid'), 'hostids' => $trigger['hosts'][0]['hostid'], 'selectScreens' => API_OUTPUT_COUNT, 'selectInventory' => array('hostid'), 'preservekeys' => true)); $host = reset($host); $hostScripts = API::Script()->getScriptsByHosts($host['hostid']); // host js link $hostSpan = new CSpan($host['name'], 'link_menu menu-host'); $scripts = $hostScripts[$host['hostid']]; $hostSpan->attr('data-menu', hostMenuData($host, $scripts)); // get visible name of the first host $table->addRow(array(_('Host'), $hostSpan)); $table->addRow(array(_('Trigger'), CTriggerHelper::expandDescription($trigger))); $table->addRow(array(_('Severity'), getSeverityCell($trigger['priority']))); $table->addRow(array(_('Expression'), $expression)); $table->addRow(array(_('Event generation'), _('Normal') . (TRIGGER_MULT_EVENT_ENABLED == $trigger['type'] ? SPACE . '+' . SPACE . _('Multiple PROBLEM events') : ''))); $table->addRow(array(_('Disabled'), TRIGGER_STATUS_ENABLED == $trigger['status'] ? new CCol(_('No'), 'off') : new CCol(_('Yes'), 'on'))); return $table; }
require_once dirname(__FILE__) . '/include/views/js/general.script.confirm.js.php'; // get triggers $options = array('triggerids' => $_REQUEST['triggerid'], 'expandData' => 1, 'selectHosts' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND); $triggers = API::Trigger()->get($options); if (empty($triggers)) { access_deny(); } $trigger = reset($triggers); // get events $options = array('eventids' => $_REQUEST['eventid'], 'triggerids' => $_REQUEST['triggerid'], 'select_alerts' => API_OUTPUT_EXTEND, 'select_acknowledges' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND, 'selectHosts' => API_OUTPUT_EXTEND); $events = API::Event()->get($options); $event = reset($events); $tr_event_wdgt = new CWidget(); $tr_event_wdgt->setClass('header'); // Main widget header $text = array(_('EVENTS') . ': "' . CTriggerHelper::expandDescription($trigger) . '"'); $fs_icon = get_icon('fullscreen', array('fullscreen' => $_REQUEST['fullscreen'])); $tr_event_wdgt->addHeader($text, $fs_icon); $left_col = array(); // tr details $triggerDetails = new CUIWidget('hat_triggerdetails', make_trigger_details($trigger)); $triggerDetails->setHeader(_('Event source details')); $left_col[] = $triggerDetails; // event details $eventDetails = new CUIWidget('hat_eventdetails', make_event_details($event, $trigger)); $eventDetails->setHeader(_('Event details')); $left_col[] = $eventDetails; $right_col = array(); // if acknowledges are not disabled in configuration, let's show them if ($config['event_ack_enable']) { $event_ack = new CUIWidget('hat_eventack', makeAckTab($event), CProfile::get('web.tr_events.hats.hat_eventack.state', 1));
$description = new CSpan($trigger['description'], 'link'); $trigger['description'] = $trigger['hostname'] . ': ' . $trigger['description']; if ($multiselect) { $js_action = 'addValue(' . zbx_jsvalue($reference) . ', ' . zbx_jsvalue($trigger['triggerid']) . ');'; } else { $values = array($dstfld1 => $trigger[$srcfld1], $dstfld2 => $trigger[$srcfld2]); if (isset($srcfld3)) { $values[$dstfld3] = $trigger[$srcfld3]; } $js_action = 'addValues(' . zbx_jsvalue($dstfrm) . ', ' . zbx_jsvalue($values) . '); return false;'; } $description->setAttribute('onclick', $js_action . ' jQuery(this).removeAttr("onclick");'); if (count($trigger['dependencies']) > 0) { $description = array($description, BR(), bold(_('Depends on')), BR()); foreach ($trigger['dependencies'] as $val) { $description[] = array(CTriggerHelper::expandDescription($val), BR()); } } switch ($trigger['status']) { case TRIGGER_STATUS_DISABLED: $status = new CSpan(_('Disabled'), 'disabled'); break; case TRIGGER_STATUS_ENABLED: $status = new CSpan(_('Enabled'), 'enabled'); break; } $table->addRow(array($multiselect ? new CCheckBox('triggers[' . zbx_jsValue($trigger[$srcfld1]) . ']', null, null, $trigger['triggerid']) : null, $description, getSeverityCell($trigger['priority']), $status)); // made to save memmory usage if ($multiselect) { $jsTriggers[$trigger['triggerid']] = array('triggerid' => $trigger['triggerid'], 'description' => $trigger['description'], 'expression' => $trigger['expression'], 'priority' => $trigger['priority'], 'status' => $trigger['status'], 'host' => $trigger['hostname']); }
$dep_table->setAttribute('style', 'width: 200px;'); $dep_table->addRow(bold(_('Depends on') . ':')); foreach ($trigger['dependencies'] as $dep) { $dep_table->addRow(' - ' . CTriggerHelper::expandDescriptionById($dep['triggerid'])); } $img = new Cimg('images/general/arrow_down2.png', 'DEP_UP'); $img->setAttribute('style', 'vertical-align: middle; border: 0px;'); $img->setHint($dep_table); $description = array($img, SPACE, $description); } $dependency = false; $dep_table = new CTableInfo(); $dep_table->setAttribute('style', 'width: 200px;'); $dep_table->addRow(bold(_('Dependent') . ':')); if (!empty($triggerids_down[$trigger['triggerid']])) { $depTriggers = CTriggerHelper::batchExpandDescriptionById($triggerids_down[$trigger['triggerid']]); foreach ($depTriggers as $depTrigger) { $dep_table->addRow(SPACE . '-' . SPACE . $depTrigger['description']); $dependency = true; } } if ($dependency) { $img = new Cimg('images/general/arrow_up2.png', 'DEP_UP'); $img->setAttribute('style', 'vertical-align: middle; border: 0px;'); $img->setHint($dep_table); $description = array($img, SPACE, $description); } unset($img, $dep_table, $dependency); // }}} DEPENDENCIES $tr_desc = new CSpan($description); // host JS menu {{{
break; case 'year': $period_start = mktime(0, 0, 0, 1, 1, date('Y')); break; case 24: case 24 * 7: case 24 * 30: case 24 * DAY_IN_YEAR: $period_start = $period_end - $period * 3600; break; } // fetch services $services = API::Service()->get(array('output' => array('name', 'serviceid', 'showsla', 'goodsla', 'algorithm'), 'selectParent' => array('serviceid'), 'selectDependencies' => array('servicedownid', 'soft', 'linkid'), 'selectTrigger' => array('description', 'triggerid', 'expression'), 'preservekeys' => true, 'sortfield' => 'sortorder', 'sortorder' => ZBX_SORT_UP)); // expand trigger descriptions $triggers = zbx_objectValues($services, 'trigger'); $triggers = CTriggerHelper::batchExpandDescription($triggers); foreach ($services as &$service) { if ($service['trigger']) { $service['trigger'] = $triggers[$service['trigger']['triggerid']]; } } unset($service); // fetch sla $slaData = API::Service()->getSla(array('intervals' => array(array('from' => $period_start, 'to' => $period_end)))); // expand problem trigger descriptions foreach ($slaData as &$serviceSla) { foreach ($serviceSla['problems'] as &$problemTrigger) { $problemTrigger['description'] = $triggers[$problemTrigger['triggerid']]['description']; } unset($problemTrigger); }
/** * Create CTriggerDescription object and store in static variable. * * @static */ private static function init() { if (self::$tDescription === null) { self::$tDescription = new CTriggerDescription(); } }
function getSelementsInfo($sysmap) { $config = select_config(); $show_unack = $config['event_ack_enable'] ? $sysmap['show_unack'] : EXTACK_OPTION_ALL; $triggers_map = array(); $triggers_map_submaps = array(); $hostgroups_map = array(); $hosts_map = 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 = zbx_toHash($sysmap['selements'], 'selementid'); foreach ($selements as $selementid => $selement) { $selements[$selementid]['hosts'] = array(); $selements[$selementid]['triggers'] = array(); switch ($selement['elementtype']) { case SYSMAP_ELEMENT_TYPE_MAP: $mapids = array($selement['elementid']); while (!empty($mapids)) { $maps = API::Map()->get(array('sysmapids' => $mapids, 'output' => API_OUTPUT_REFER, 'selectSelements' => API_OUTPUT_EXTEND, 'nopermissions' => true, 'nodeids' => get_current_nodeid(true))); $mapids = array(); foreach ($maps as $map) { foreach ($map['selements'] as $sel) { switch ($sel['elementtype']) { case SYSMAP_ELEMENT_TYPE_MAP: $mapids[] = $sel['elementid']; break; case SYSMAP_ELEMENT_TYPE_HOST_GROUP: $hostgroups_map[$sel['elementid']][$selementid] = $selementid; break; case SYSMAP_ELEMENT_TYPE_HOST: $hosts_map[$sel['elementid']][$selementid] = $selementid; break; case SYSMAP_ELEMENT_TYPE_TRIGGER: $triggers_map_submaps[$sel['elementid']][$selementid] = $selementid; break; } } } } break; case SYSMAP_ELEMENT_TYPE_HOST_GROUP: $hostgroups_map[$selement['elementid']][$selement['selementid']] = $selement['selementid']; break; case SYSMAP_ELEMENT_TYPE_HOST: $hosts_map[$selement['elementid']][$selement['selementid']] = $selement['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[] = $selement['elementid']; } break; case SYSMAP_ELEMENT_TYPE_TRIGGER: $triggers_map[$selement['elementid']][$selement['selementid']] = $selement['selementid']; break; } } // get host inventories if ($sysmap['iconmapid']) { $hostInventories = API::Host()->get(array('hostids' => $hostsToGetInventories, 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => true, 'preservekeys' => true, 'selectInventory' => API_OUTPUT_EXTEND)); } $all_hosts = array(); if (!empty($hosts_map)) { $hosts = API::Host()->get(array('hostids' => array_keys($hosts_map), 'output' => array('name', 'status', 'maintenance_status', 'maintenanceid'), 'nopermissions' => true, 'nodeids' => get_current_nodeid(true))); $all_hosts = array_merge($all_hosts, $hosts); foreach ($hosts as $host) { foreach ($hosts_map[$host['hostid']] as $belongs_to_sel) { $selements[$belongs_to_sel]['hosts'][$host['hostid']] = $host['hostid']; } } } if (!empty($hostgroups_map)) { $hosts = API::Host()->get(array('groupids' => array_keys($hostgroups_map), 'output' => array('name', 'status', 'maintenance_status', 'maintenanceid'), 'nopermissions' => true, 'nodeids' => get_current_nodeid(true))); $all_hosts = array_merge($all_hosts, $hosts); foreach ($hosts as $host) { foreach ($host['groups'] as $group) { if (isset($hostgroups_map[$group['groupid']])) { foreach ($hostgroups_map[$group['groupid']] as $belongs_to_sel) { $selements[$belongs_to_sel]['hosts'][$host['hostid']] = $host['hostid']; // add hosts to hosts_map for trigger selection; if (!isset($hosts_map[$host['hostid']])) { $hosts_map[$host['hostid']] = array(); } $hosts_map[$host['hostid']][$belongs_to_sel] = $belongs_to_sel; } } } } } $all_hosts = zbx_toHash($all_hosts, 'hostid'); $monitored_hostids = array(); foreach ($all_hosts as $hostid => $host) { if ($host['status'] == HOST_STATUS_MONITORED) { $monitored_hostids[$hostid] = $hostid; } } // get triggers data, triggers from current map, select all $all_triggers = array(); if (!empty($triggers_map)) { $triggers = API::Trigger()->get(array('nodeids' => get_current_nodeid(true), 'triggerids' => array_keys($triggers_map), 'filter' => array('value_flags' => null), 'output' => API_OUTPUT_EXTEND, 'nopermissions' => true)); $all_triggers = array_merge($all_triggers, $triggers); foreach ($triggers as $trigger) { foreach ($triggers_map[$trigger['triggerid']] as $belongs_to_sel) { $selements[$belongs_to_sel]['triggers'][$trigger['triggerid']] = $trigger['triggerid']; } } } // triggers from submaps, skip dependent if (!empty($triggers_map_submaps)) { $triggers = API::Trigger()->get(array('nodeids' => get_current_nodeid(true), 'triggerids' => array_keys($triggers_map_submaps), 'filter' => array('value_flags' => null), 'skipDependent' => true, 'output' => API_OUTPUT_EXTEND, 'nopermissions' => true)); $all_triggers = array_merge($all_triggers, $triggers); foreach ($triggers as $trigger) { foreach ($triggers_map_submaps[$trigger['triggerid']] as $belongs_to_sel) { $selements[$belongs_to_sel]['triggers'][$trigger['triggerid']] = $trigger['triggerid']; } } } // triggers from all hosts/hostgroups, skip dependent if (!empty($monitored_hostids)) { $triggers = API::Trigger()->get(array('hostids' => $monitored_hostids, 'output' => array('status', 'value', 'priority', 'lastchange', 'description', 'expression'), 'nopermissions' => true, 'filter' => array('value_flags' => null), 'nodeids' => get_current_nodeid(true), 'monitored' => true, 'skipDependent' => true)); $all_triggers = array_merge($all_triggers, $triggers); foreach ($triggers as $trigger) { foreach ($trigger['hosts'] as $host) { if (isset($hosts_map[$host['hostid']])) { foreach ($hosts_map[$host['hostid']] as $belongs_to_sel) { $selements[$belongs_to_sel]['triggers'][$trigger['triggerid']] = $trigger['triggerid']; } } } } } $all_triggers = zbx_toHash($all_triggers, 'triggerid'); $unack_triggerids = API::Trigger()->get(array('triggerids' => array_keys($all_triggers), 'withLastEventUnacknowledged' => true, 'output' => API_OUTPUT_SHORTEN, 'nodeids' => get_current_nodeid(true), 'nopermissions' => true, 'monitored' => true, 'filter' => array('value' => TRIGGER_VALUE_TRUE, 'value_flags' => null))); $unack_triggerids = zbx_toHash($unack_triggerids, 'triggerid'); $info = array(); foreach ($selements as $selementid => $selement) { $i = array('disabled' => 0, 'maintenance' => 0, 'problem' => 0, 'problem_unack' => 0, 'unknown' => 0, 'priority' => 0, 'trigger_disabled' => 0, 'latelyChanged' => false, 'ack' => true); foreach ($selement['hosts'] as $hostid) { $host = $all_hosts[$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 = $all_triggers[$triggerid]; if ($trigger['status'] == TRIGGER_STATUS_DISABLED) { $i['trigger_disabled']++; } else { if ($trigger['value'] == TRIGGER_VALUE_TRUE) { $i['problem']++; $last_problemid = $triggerid; if ($i['priority'] < $trigger['priority']) { $i['priority'] = $trigger['priority']; } } if (isset($unack_triggerids[$triggerid])) { $i['problem_unack']++; } $config = select_config(); $i['latelyChanged'] |= time() - $trigger['lastchange'] < $config['blink_period']; } } $i['ack'] = (bool) (!$i['problem_unack']); if ($sysmap['expandproblem'] && $i['problem'] == 1) { $i['problem_title'] = CTriggerHelper::expandDescription($all_triggers[$last_problemid]); } if ($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST && $i['maintenance'] == 1) { $mnt = get_maintenance_by_maintenanceid($all_hosts[$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, $show_unack); break; case SYSMAP_ELEMENT_TYPE_HOST_GROUP: $info[$selementid] = getHostGroupsInfo($selement, $i, $show_unack); break; case SYSMAP_ELEMENT_TYPE_HOST: $info[$selementid] = getHostsInfo($selement, $i, $show_unack); 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, $show_unack); 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) { $maps = API::Map()->get(array('sysmapids' => zbx_objectValues($elems['sysmaps'], 'elementid'), 'nopermissions' => true, 'output' => array('name'))); $maps = zbx_toHash($maps, 'sysmapid'); foreach ($elems['sysmaps'] as $elem) { $info[$elem['selementid']]['name'] = $maps[$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'] = CTriggerHelper::expandDescription($all_triggers[$elem['elementid']]); } } if (!empty($elems['hosts']) && $hlabel) { foreach ($elems['hosts'] as $elem) { $info[$elem['selementid']]['name'] = $all_hosts[$elem['elementid']]['name']; } } return $info; }
} if (isset($_REQUEST['saveandreturn'])) { ob_end_clean(); if ($_REQUEST['backurl'] == 'tr_events.php') { redirect($_REQUEST['backurl'] . '?eventid=' . $_REQUEST['eventid'] . '&triggerid=' . $_REQUEST['triggerid']); } elseif ($_REQUEST['backurl'] == 'screenedit.php') { redirect($_REQUEST['backurl'] . '?screenid=' . $_REQUEST['screenid']); } elseif ($_REQUEST['backurl'] == 'screens.php') { redirect($_REQUEST['backurl'] . '?elementid=' . $_REQUEST['screenid']); } else { redirect($_REQUEST['backurl']); } } } ob_end_flush(); $msg = $bulk ? ' BULK ACKNOWLEDGE ' : CTriggerHelper::expandDescription($event_trigger); show_table_header(array(_('ALARM ACKNOWLEDGES') . ': ', $msg)); echo SBR; if ($bulk) { $title = _('Acknowledge alarm by'); $btn_txt2 = _('Acknowledge and return'); } else { $db_acks = get_acknowledges_by_eventid($_REQUEST['eventid']); if ($db_acks) { $table = new CTable(null, 'ack_msgs'); $table->setAlign('center'); while ($db_ack = DBfetch($db_acks)) { $table->addRow(array(new CCol($db_ack['alias'], 'user'), new CCol(zbx_date2str(_('d M Y H:i:s'), $db_ack['clock']), 'time')), 'title'); $msgCol = new CCol(zbx_nl2br($db_ack['message'])); $msgCol->setColspan(2); $table->addRow($msgCol, 'msg');
/** * Get Triggers data * * @param array $options * @param array $options['itemids'] * @param array $options['hostids'] * @param array $options['groupids'] * @param array $options['triggerids'] * @param array $options['applicationids'] * @param array $options['status'] * @param array $options['editable'] * @param array $options['count'] * @param array $options['pattern'] * @param array $options['limit'] * @param array $options['order'] * * @return array|int item data as array or false if error */ public function get(array $options = array()) { $result = array(); $userType = self::$userData['type']; $userid = self::$userData['userid']; // allowed columns for sorting $sortColumns = array('triggerid', 'description', 'status', 'priority', 'lastchange', 'hostname'); // allowed output options for [ select_* ] params $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND); $fieldsToUnset = array(); $sqlParts = array('select' => array('triggers' => 't.triggerid'), 'from' => array('t' => 'triggers t'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'triggerids' => null, 'itemids' => null, 'applicationids' => null, 'functions' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'active' => null, 'maintenance' => null, 'withUnacknowledgedEvents' => null, 'withAcknowledgedEvents' => null, 'withLastEventUnacknowledged' => null, 'skipDependent' => null, 'nopermissions' => null, 'editable' => null, 'lastChangeSince' => null, 'lastChangeTill' => null, 'group' => null, 'host' => null, 'only_true' => null, 'min_severity' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'expandData' => null, 'expandDescription' => null, 'expandExpression' => null, 'output' => API_OUTPUT_REFER, 'selectGroups' => null, 'selectHosts' => null, 'selectItems' => null, 'selectFunctions' => null, 'selectDependencies' => null, 'selectDiscoveryRule' => null, 'selectLastEvent' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null); $options = zbx_array_merge($defOptions, $options); if (is_array($options['output'])) { unset($sqlParts['select']['triggers']); $dbTable = DB::getSchema('triggers'); $sqlParts['select']['triggerid'] = ' t.triggerid'; foreach ($options['output'] as $field) { if (isset($dbTable['fields'][$field])) { $sqlParts['select'][$field] = 't.' . $field; } } if (!is_null($options['expandDescription'])) { if (!str_in_array('description', $options['output'])) { $options['expandDescription'] = null; } else { if (!str_in_array('expression', $options['output'])) { $sqlParts['select']['expression'] = ' t.expression'; $fieldsToUnset[] = 'expression'; } } } // ignore the "expandExpression" parameter if the expression is not requested if ($options['expandExpression'] !== null && !str_in_array('expression', $options['output'])) { $options['expandExpression'] = null; } $options['output'] = API_OUTPUT_CUSTOM; } // editable + PERMISSION CHECK if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) { $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY; $userGroups = getUserGroupsByUserId($userid); $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY f.triggerid' . ' HAVING MIN(r.permission)>=' . $permission . ')'; } // groupids if (!is_null($options['groupids'])) { zbx_value2array($options['groupids']); sort($options['groupids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['groupid'] = 'hg.groupid'; } $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts_groups'] = 'hosts_groups hg'; $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid'; $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; $sqlParts['where']['groupid'] = dbConditionInt('hg.groupid', $options['groupids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['hg'] = 'hg.groupid'; } } // templateids if (!is_null($options['templateids'])) { zbx_value2array($options['templateids']); if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $options['hostids'] = array_merge($options['hostids'], $options['templateids']); } else { $options['hostids'] = $options['templateids']; } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['hostid'] = 'i.hostid'; } $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']); $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['i'] = 'i.hostid'; } } // triggerids if (!is_null($options['triggerids'])) { zbx_value2array($options['triggerids']); $sqlParts['where']['triggerid'] = dbConditionInt('t.triggerid', $options['triggerids']); } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['itemid'] = 'f.itemid'; } $sqlParts['from']['functions'] = 'functions f'; $sqlParts['where']['itemid'] = dbConditionInt('f.itemid', $options['itemids']); $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; if (!is_null($options['groupCount'])) { $sqlParts['group']['f'] = 'f.itemid'; } } // applicationids if (!is_null($options['applicationids'])) { zbx_value2array($options['applicationids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['applicationid'] = 'a.applicationid'; } $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['applications'] = 'applications a'; $sqlParts['where']['a'] = dbConditionInt('a.applicationid', $options['applicationids']); $sqlParts['where']['ia'] = 'i.hostid=a.hostid'; $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; } // functions if (!is_null($options['functions'])) { zbx_value2array($options['functions']); $sqlParts['from']['functions'] = 'functions f'; $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where'][] = dbConditionString('f.function', $options['functions']); } // monitored if (!is_null($options['monitored'])) { $sqlParts['where']['monitored'] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts h' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND (' . 'i.status<>' . ITEM_STATUS_ACTIVE . ' OR h.status<>' . HOST_STATUS_MONITORED . ')' . ')'; $sqlParts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED; } // active if (!is_null($options['active'])) { $sqlParts['where']['active'] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts h' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND h.status<>' . HOST_STATUS_MONITORED . ')'; $sqlParts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED; } // maintenance if (!is_null($options['maintenance'])) { $sqlParts['where'][] = ($options['maintenance'] == 0 ? 'NOT ' : '') . 'EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts h' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND h.maintenance_status=' . HOST_MAINTENANCE_STATUS_ON . ')'; $sqlParts['where'][] = 't.status=' . TRIGGER_STATUS_ENABLED; } // lastChangeSince if (!is_null($options['lastChangeSince'])) { $sqlParts['where']['lastchangesince'] = 't.lastchange>' . zbx_dbstr($options['lastChangeSince']); } // lastChangeTill if (!is_null($options['lastChangeTill'])) { $sqlParts['where']['lastchangetill'] = 't.lastchange<' . zbx_dbstr($options['lastChangeTill']); } // withUnacknowledgedEvents if (!is_null($options['withUnacknowledgedEvents'])) { $sqlParts['where']['unack'] = 'EXISTS (' . 'SELECT NULL' . ' FROM events e' . ' WHERE t.triggerid=e.objectid' . ' AND e.object=' . EVENT_OBJECT_TRIGGER . ' AND e.value_changed=' . TRIGGER_VALUE_CHANGED_YES . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=' . EVENT_NOT_ACKNOWLEDGED . ')'; } // withAcknowledgedEvents if (!is_null($options['withAcknowledgedEvents'])) { $sqlParts['where']['ack'] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM events e' . ' WHERE e.objectid=t.triggerid' . ' AND e.object=' . EVENT_OBJECT_TRIGGER . ' AND e.value_changed=' . TRIGGER_VALUE_CHANGED_YES . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=' . EVENT_NOT_ACKNOWLEDGED . ')'; } // templated if (!is_null($options['templated'])) { $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; if ($options['templated']) { $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE; } else { $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE; } } // inherited if (!is_null($options['inherited'])) { if ($options['inherited']) { $sqlParts['where'][] = 't.templateid IS NOT NULL'; } else { $sqlParts['where'][] = 't.templateid IS NULL'; } } // search if (is_array($options['search'])) { zbx_db_search('triggers t', $options, $sqlParts); } // filter if (is_null($options['filter'])) { $options['filter'] = array(); } if (is_array($options['filter'])) { if (!array_key_exists('flags', $options['filter'])) { $options['filter']['flags'] = array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED); } $this->dbFilter('triggers t', $options, $sqlParts); if (isset($options['filter']['host']) && !is_null($options['filter']['host'])) { zbx_value2array($options['filter']['host']); $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; $sqlParts['where']['host'] = dbConditionString('h.host', $options['filter']['host']); } if (isset($options['filter']['hostid']) && !is_null($options['filter']['hostid'])) { zbx_value2array($options['filter']['hostid']); $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['filter']['hostid']); } } // group if (!is_null($options['group'])) { if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['name'] = 'g.name'; } $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts_groups'] = 'hosts_groups hg'; $sqlParts['from']['groups'] = 'groups g'; $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid'; $sqlParts['where']['ghg'] = 'g.groupid = hg.groupid'; $sqlParts['where']['group'] = ' g.name=' . zbx_dbstr($options['group']); } // host if (!is_null($options['host'])) { if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts['select']['host'] = 'h.host'; } $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['i'] = dbConditionInt('i.hostid', $options['hostids']); $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; $sqlParts['where']['host'] = ' h.host=' . zbx_dbstr($options['host']); } // only_true if (!is_null($options['only_true'])) { $config = select_config(); $sqlParts['where']['ot'] = '((t.value=' . TRIGGER_VALUE_TRUE . ')' . ' OR ' . '((t.value=' . TRIGGER_VALUE_FALSE . ') AND (t.lastchange>' . (time() - $config['ok_period']) . ')))'; } // min_severity if (!is_null($options['min_severity'])) { $sqlParts['where'][] = 't.priority>=' . zbx_dbstr($options['min_severity']); } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sqlParts['select']['triggers'] = 't.*'; } // expandData if (!is_null($options['expandData'])) { $sqlParts['select']['hostname'] = 'h.name AS hostname'; $sqlParts['select']['host'] = 'h.host'; $sqlParts['select']['hostid'] = 'h.hostid'; $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; } // count or grouped counts via direct SQL count if (!is_null($options['countOutput']) && !$this->requiresPostSqlFiltering($options)) { $options['sortfield'] = ''; $sqlParts['select'] = array('COUNT(DISTINCT t.triggerid) as rowscount'); // groupCount if (!is_null($options['groupCount'])) { foreach ($sqlParts['group'] as $key => $fields) { $sqlParts['select'][$key] = $fields; } } } // sorting if (!zbx_empty($options['sortfield'])) { if (!is_array($options['sortfield'])) { $options['sortfield'] = array($options['sortfield']); } foreach ($options['sortfield'] as $i => $sortfield) { // validate sortfield if (!str_in_array($sortfield, $sortColumns)) { throw new APIException(ZBX_API_ERROR_INTERNAL, _s('Sorting by field "%s" not allowed.', $sortfield)); } // add sort field to order $sortorder = ''; if (is_array($options['sortorder'])) { if (!empty($options['sortorder'][$i])) { $sortorder = $options['sortorder'][$i] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ''; } } else { $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ''; } // we will be using lastchange for ordering in any case if (!str_in_array('t.lastchange', $sqlParts['select']) && !str_in_array('t.*', $sqlParts['select'])) { $sqlParts['select']['lastchange'] = 't.lastchange'; } switch ($sortfield) { case 'hostname': // the only way to sort by host name is to get it like this: // triggers -> functions -> items -> hosts $sqlParts['select']['hostname'] = 'h.name'; $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts'] = 'hosts h'; $sqlParts['where'][] = 't.triggerid = f.triggerid'; $sqlParts['where'][] = 'f.itemid = i.itemid'; $sqlParts['where'][] = 'i.hostid = h.hostid'; $sqlParts['order'][] = 'h.name ' . $sortorder; break; case 'lastchange': $sqlParts['order'][] = $sortfield . ' ' . $sortorder; break; default: // if lastchange is not used for ordering, it should be the second order criteria $sqlParts['order'][] = 't.' . $sortfield . ' ' . $sortorder; break; } // add sort field to select if distinct is used if (count($sqlParts['from']) > 1) { if (!str_in_array('t.' . $sortfield, $sqlParts['select']) && !str_in_array('t.*', $sqlParts['select'])) { $sqlParts['select'][$sortfield] = 't.' . $sortfield; } } } if (!empty($sqlParts['order'])) { $sqlParts['order'][] = 't.lastchange DESC'; } } // limit if (!zbx_ctype_digit($options['limit']) || !$options['limit']) { $options['limit'] = null; } $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts); // return count or grouped counts via direct SQL count if (!is_null($options['countOutput']) && !$this->requiresPostSqlFiltering($options)) { $dbRes = DBselect($this->createSelectQueryFromParts($sqlParts), $options['limit']); while ($trigger = DBfetch($dbRes)) { if (!is_null($options['groupCount'])) { $result[] = $trigger; } else { $result = $trigger['rowscount']; } } return $result; } $triggers = zbx_toHash($this->customFetch($this->createSelectQueryFromParts($sqlParts), $options), 'triggerid'); // return count for post SQL filtered result sets if (!is_null($options['countOutput'])) { return count($triggers); } $triggerids = array_keys($triggers); sort($triggerids); // format result array foreach ($triggers as $trigger) { if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$trigger['triggerid']] = array('triggerid' => $trigger['triggerid']); } else { if (!isset($result[$trigger['triggerid']])) { $result[$trigger['triggerid']] = array(); } if (!is_null($options['selectHosts']) && !isset($result[$trigger['triggerid']]['hosts'])) { $result[$trigger['triggerid']]['hosts'] = array(); } if (!is_null($options['selectItems']) && !isset($result[$trigger['triggerid']]['items'])) { $result[$trigger['triggerid']]['items'] = array(); } if (!is_null($options['selectFunctions']) && !isset($result[$trigger['triggerid']]['functions'])) { $result[$trigger['triggerid']]['functions'] = array(); } if (!is_null($options['selectDependencies']) && !isset($result[$trigger['triggerid']]['dependencies'])) { $result[$trigger['triggerid']]['dependencies'] = array(); } if (!is_null($options['selectDiscoveryRule']) && !isset($result[$trigger['triggerid']]['discoveryRule'])) { $result[$trigger['triggerid']]['discoveryRule'] = array(); } $result[$trigger['triggerid']] += $trigger; } } /* * Adding objects */ // adding last event if (!is_null($options['selectLastEvent']) && str_in_array($options['selectLastEvent'], $subselectsAllowedOutputs)) { $select = $options['selectLastEvent'] == API_OUTPUT_REFER ? 'e.eventid, e.objectid' : 'e.*'; $lastEvents = DBfetchArrayAssoc(DBselect('SELECT ' . $select . ' FROM events e JOIN (' . 'SELECT max(eventid) as lasteventid FROM events e' . ' WHERE ' . dbConditionInt('e.objectid', $triggerids) . ' AND ' . DBin_node('e.objectid') . ' AND e.object=' . EVENT_SOURCE_TRIGGERS . ' AND e.value_changed=' . TRIGGER_VALUE_CHANGED_YES . ' GROUP BY e.objectid' . ') ee ON e.eventid=ee.lasteventid'), 'objectid'); foreach ($result as $triggerId => $trigger) { $result[$triggerId]['lastEvent'] = isset($lastEvents[$triggerId]) ? $lastEvents[$triggerId] : array(); } } // adding trigger dependencies if (!is_null($options['selectDependencies']) && str_in_array($options['selectDependencies'], $subselectsAllowedOutputs)) { $deps = array(); $depids = array(); $dbDeps = DBselect('SELECT td.triggerid_up,td.triggerid_down' . ' FROM trigger_depends td' . ' WHERE ' . dbConditionInt('td.triggerid_down', $triggerids)); while ($dbDep = DBfetch($dbDeps)) { if (!isset($deps[$dbDep['triggerid_down']])) { $deps[$dbDep['triggerid_down']] = array(); } $deps[$dbDep['triggerid_down']][$dbDep['triggerid_up']] = $dbDep['triggerid_up']; $depids[] = $dbDep['triggerid_up']; } $objParams = array('triggerids' => $depids, 'output' => $options['selectDependencies'], 'expandData' => true, 'preservekeys' => true); $allowed = $this->get($objParams); // allowed triggerids foreach ($deps as $triggerid => $deptriggers) { foreach ($deptriggers as $deptriggerid) { if (isset($allowed[$deptriggerid])) { $result[$triggerid]['dependencies'][] = $allowed[$deptriggerid]; } } } } // adding groups if ($options['groupids'] !== null && $options['selectGroups'] === null) { $options['selectGroups'] = API_OUTPUT_REFER; } if (!is_null($options['selectGroups']) && str_in_array($options['selectGroups'], $subselectsAllowedOutputs)) { $objParams = array('nodeids' => $options['nodeids'], 'output' => $options['selectGroups'], 'triggerids' => $triggerids, 'preservekeys' => true); $groups = API::HostGroup()->get($objParams); foreach ($groups as $groupid => $group) { $gtriggers = $group['triggers']; unset($group['triggers']); foreach ($gtriggers as $trigger) { $result[$trigger['triggerid']]['groups'][] = $group; } } } // adding hosts if ($options['hostids'] !== null && $options['selectHosts'] === null) { $options['selectHosts'] = API_OUTPUT_REFER; } if (!is_null($options['selectHosts'])) { $objParams = array('nodeids' => $options['nodeids'], 'triggerids' => $triggerids, 'templated_hosts' => true, 'nopermissions' => true, 'preservekeys' => true); if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) { $objParams['output'] = $options['selectHosts']; $hosts = API::Host()->get($objParams); if (!is_null($options['limitSelects'])) { order_result($hosts, 'host'); } foreach ($hosts as $hostid => $host) { unset($hosts[$hostid]['triggers']); $count = array(); foreach ($host['triggers'] as $trigger) { if (!is_null($options['limitSelects'])) { if (!isset($count[$trigger['triggerid']])) { $count[$trigger['triggerid']] = 0; } $count[$trigger['triggerid']]++; if ($count[$trigger['triggerid']] > $options['limitSelects']) { continue; } } $result[$trigger['triggerid']]['hosts'][] =& $hosts[$hostid]; } } } else { if (API_OUTPUT_COUNT == $options['selectHosts']) { $objParams['countOutput'] = 1; $objParams['groupCount'] = 1; $hosts = API::Host()->get($objParams); $hosts = zbx_toHash($hosts, 'hostid'); foreach ($result as $triggerid => $trigger) { if (isset($hosts[$triggerid])) { $result[$triggerid]['hosts'] = $hosts[$triggerid]['rowscount']; } else { $result[$triggerid]['hosts'] = 0; } } } } } // adding functions if (!is_null($options['selectFunctions']) && str_in_array($options['selectFunctions'], $subselectsAllowedOutputs)) { if ($options['selectFunctions'] == API_OUTPUT_EXTEND) { $sqlSelect = 'f.*'; } else { $sqlSelect = 'f.functionid,f.triggerid'; } $res = DBselect('SELECT ' . $sqlSelect . ' FROM functions f' . ' WHERE ' . dbConditionInt('f.triggerid', $triggerids)); while ($function = DBfetch($res)) { $triggerid = $function['triggerid']; unset($function['triggerid']); $result[$triggerid]['functions'][] = $function; } } // adding items if ($options['itemids'] !== null && $options['selectItems'] === null) { $options['selectItems'] = API_OUTPUT_REFER; } if (!is_null($options['selectItems']) && (is_array($options['selectItems']) || str_in_array($options['selectItems'], $subselectsAllowedOutputs))) { $objParams = array('nodeids' => $options['nodeids'], 'output' => $options['selectItems'], 'triggerids' => $triggerids, 'webitems' => true, 'nopermissions' => true, 'preservekeys' => true); $items = API::Item()->get($objParams); foreach ($items as $item) { $itriggers = $item['triggers']; unset($item['triggers']); foreach ($itriggers as $trigger) { $result[$trigger['triggerid']]['items'][] = $item; } } } // adding discoveryrule if (!is_null($options['selectDiscoveryRule'])) { $ruleids = $ruleMap = array(); $dbRules = DBselect('SELECT id.parent_itemid,td.triggerid' . ' FROM trigger_discovery td,item_discovery id,functions f' . ' WHERE ' . dbConditionInt('td.triggerid', $triggerids) . ' AND td.parent_triggerid=f.triggerid' . ' AND f.itemid=id.itemid'); while ($rule = DBfetch($dbRules)) { $ruleids[$rule['parent_itemid']] = $rule['parent_itemid']; $ruleMap[$rule['triggerid']] = $rule['parent_itemid']; } $objParams = array('nodeids' => $options['nodeids'], 'itemids' => $ruleids, 'nopermissions' => true, 'preservekeys' => true); if (is_array($options['selectDiscoveryRule']) || str_in_array($options['selectDiscoveryRule'], $subselectsAllowedOutputs)) { $objParams['output'] = $options['selectDiscoveryRule']; $discoveryRules = API::DiscoveryRule()->get($objParams); foreach ($result as $triggerid => $trigger) { if (isset($ruleMap[$triggerid]) && isset($discoveryRules[$ruleMap[$triggerid]])) { $result[$triggerid]['discoveryRule'] = $discoveryRules[$ruleMap[$triggerid]]; } } } } // expandDescription if (!is_null($options['expandDescription']) && $result && array_key_exists('description', reset($result))) { $result = CTriggerHelper::batchExpandDescription($result); } // expand expression if ($options['expandExpression'] !== null) { foreach ($result as &$trigger) { if ($trigger['expression']) { $trigger['expression'] = explode_exp($trigger['expression'], false, true); } } unset($trigger); } if (!empty($fieldsToUnset)) { foreach ($result as $tnum => $trigger) { foreach ($fieldsToUnset as $fieldToUnset) { unset($result[$tnum][$fieldToUnset]); } } } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }