$wd = $wd == 0 ? 6 : $wd - 1; $minTime = $time - $wd * SEC_PER_DAY; $dateFormat = DATE_TIME_FORMAT; array_unshift($header, _('From'), _('Till')); $max = $year == $currentYear ? date('W') - 1 : 52; for ($i = 0; $i <= $max; $i++) { $intervals[strtotime('+' . $i . ' week', $minTime)] = strtotime('+' . ($i + 1) . ' week', $minTime); } break; } // time till $maxTime = $year == $currentYear ? time() : mktime(0, 0, 0, 1, 1, $year + 1); // fetch alerts $alerts = []; foreach (eventSourceObjects() as $sourceObject) { $alerts = array_merge($alerts, API::Alert()->get(['output' => ['mediatypeid', 'userid', 'clock'], 'eventsource' => $sourceObject['source'], 'eventobject' => $sourceObject['object'], 'mediatypeids' => getRequest('media_type') ? getRequest('media_type') : null, 'time_from' => $minTime, 'time_till' => $maxTime])); } // sort alerts in chronological order so we could easily iterate through them later CArrayHelper::sort($alerts, ['clock']); $table->setHeader($header); foreach ($intervals as $from => $till) { // interval start $row = [zbx_date2str($dateFormat, $from)]; // interval end, displayed only for week intervals if ($period == 'weekly') { $row[] = zbx_date2str($dateFormat, min($till, time())); } // counting alert count for each user and media type $summary = []; foreach ($users as $userid) { $summary[$userid] = [];
protected function addRelatedObjects(array $options, array $result) { $result = parent::addRelatedObjects($options, $result); $eventIds = array_keys($result); // adding hosts if ($options['selectHosts'] !== null && $options['selectHosts'] != API_OUTPUT_COUNT) { // trigger events if ($options['object'] == EVENT_OBJECT_TRIGGER) { $query = DBselect('SELECT e.eventid,i.hostid' . ' FROM events e,functions f,items i' . ' WHERE ' . dbConditionInt('e.eventid', $eventIds) . ' AND e.objectid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND e.object=' . zbx_dbstr($options['object']) . ' AND e.source=' . zbx_dbstr($options['source'])); } elseif ($options['object'] == EVENT_OBJECT_ITEM || $options['object'] == EVENT_OBJECT_LLDRULE) { $query = DBselect('SELECT e.eventid,i.hostid' . ' FROM events e,items i' . ' WHERE ' . dbConditionInt('e.eventid', $eventIds) . ' AND e.objectid=i.itemid' . ' AND e.object=' . zbx_dbstr($options['object']) . ' AND e.source=' . zbx_dbstr($options['source'])); } $relationMap = new CRelationMap(); while ($relation = DBfetch($query)) { $relationMap->addRelation($relation['eventid'], $relation['hostid']); } $hosts = API::Host()->get(array('output' => $options['selectHosts'], 'hostids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true)); $result = $relationMap->mapMany($result, $hosts, 'hosts'); } // adding the related object if ($options['selectRelatedObject'] !== null && $options['selectRelatedObject'] != API_OUTPUT_COUNT && $options['object'] != EVENT_OBJECT_AUTOREGHOST) { $relationMap = new CRelationMap(); foreach ($result as $event) { $relationMap->addRelation($event['eventid'], $event['objectid']); } switch ($options['object']) { case EVENT_OBJECT_TRIGGER: $api = API::Trigger(); break; case EVENT_OBJECT_DHOST: $api = API::DHost(); break; case EVENT_OBJECT_DSERVICE: $api = API::DService(); break; case EVENT_OBJECT_ITEM: $api = API::Item(); break; case EVENT_OBJECT_LLDRULE: $api = API::DiscoveryRule(); break; } $objects = $api->get(array('output' => $options['selectRelatedObject'], $api->pkOption() => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true)); $result = $relationMap->mapOne($result, $objects, 'relatedObject'); } // adding alerts if ($options['select_alerts'] !== null && $options['select_alerts'] != API_OUTPUT_COUNT) { $relationMap = $this->createRelationMap($result, 'eventid', 'alertid', 'alerts'); $alerts = API::Alert()->get(array('output' => $options['select_alerts'], 'selectMediatypes' => API_OUTPUT_EXTEND, 'alertids' => $relationMap->getRelatedIds(), 'nopermissions' => true, 'preservekeys' => true, 'sortfield' => 'clock', 'sortorder' => ZBX_SORT_DOWN)); $result = $relationMap->mapMany($result, $alerts, 'alerts'); } // adding acknowledges if ($options['select_acknowledges'] !== null) { if ($options['select_acknowledges'] != API_OUTPUT_COUNT) { // create the base query $sqlParts = API::getApiService()->createSelectQueryParts('acknowledges', 'a', array('output' => $this->outputExtend($options['select_acknowledges'], array('acknowledgeid', 'eventid', 'clock')), 'filter' => array('eventid' => $eventIds))); $sqlParts['order'][] = 'a.clock DESC'; // if the user data is requested via extended output or specified fields, join the users table $userFields = array('alias', 'name', 'surname'); $requestUserData = array(); foreach ($userFields as $userField) { if ($this->outputIsRequested($userField, $options['select_acknowledges'])) { $requestUserData[] = $userField; } } if ($requestUserData) { foreach ($requestUserData as $userField) { $sqlParts = $this->addQuerySelect('u.' . $userField, $sqlParts); } $sqlParts['from'][] = 'users u'; $sqlParts['where'][] = 'a.userid=u.userid'; } $acknowledges = DBFetchArrayAssoc(DBselect($this->createSelectQueryFromParts($sqlParts)), 'acknowledgeid'); $relationMap = $this->createRelationMap($acknowledges, 'eventid', 'acknowledgeid'); $acknowledges = $this->unsetExtraFields($acknowledges, array('eventid', 'acknowledgeid', 'clock'), $options['select_acknowledges']); $result = $relationMap->mapMany($result, $acknowledges, 'acknowledges'); } else { $acknowledges = DBFetchArrayAssoc(DBselect('SELECT COUNT(a.acknowledgeid) AS rowscount,a.eventid' . ' FROM acknowledges a' . ' WHERE ' . dbConditionInt('a.eventid', $eventIds) . ' GROUP BY a.eventid'), 'eventid'); foreach ($result as &$event) { if (isset($acknowledges[$event['eventid']])) { $event['acknowledges'] = $acknowledges[$event['eventid']]['rowscount']; } else { $event['acknowledges'] = 0; } } unset($event); } } return $result; }
$till = $from + $effectivePeriod; $user = null; $queryData = true; $firstAlert = null; if ($data['alias']) { $user = API::User()->get(array('output' => array('userid'), 'filter' => array('alias' => $data['alias']))); if ($user) { $user = reset($user); } else { $queryData = false; } } // fetch alerts for different objects and sources and combine them in a single stream if ($queryData) { foreach (eventSourceObjects() as $eventSource) { $data['alerts'] = array_merge($data['alerts'], API::Alert()->get(array('output' => API_OUTPUT_EXTEND, 'selectMediatypes' => API_OUTPUT_EXTEND, 'userids' => $data['alias'] ? $user['userid'] : null, 'time_from' => $from, 'time_till' => $till, 'eventsource' => $eventSource['source'], 'eventobject' => $eventSource['object'], 'limit' => $config['search_limit'] + 1))); } CArrayHelper::sort($data['alerts'], array(array('field' => 'alertid', 'order' => ZBX_SORT_DOWN))); $data['alerts'] = array_slice($data['alerts'], 0, $config['search_limit'] + 1); // get first alert if ($user) { $firstAlert = DBfetch(DBselect('SELECT MIN(a.clock) AS clock' . ' FROM alerts a' . ' WHERE a.userid=' . zbx_dbstr($user['userid']))); } } // padding $data['paging'] = getPagingLine($data['alerts']); // timeline $data['timeline'] = array('period' => $effectivePeriod, 'starttime' => date(TIMESTAMP_FORMAT, $firstAlert ? $firstAlert['clock'] : ZBX_MAX_PERIOD), 'usertime' => isset($data['stime']) ? date(TIMESTAMP_FORMAT, zbxDateToTime($data['stime']) + $effectivePeriod) : null); // render view $auditView = new CView('administration.auditacts.list', $data); $auditView->render();
$effectivePeriod = navigation_bar_calc('web.auditacts.timeline', 0, true); $data = array('stime' => getRequest('stime'), 'alias' => CProfile::get('web.auditacts.filter.alias', ''), 'users' => array(), 'alerts' => array(), 'paging' => null); $userId = null; if ($data['alias']) { $data['users'] = API::User()->get(array('output' => array('userid', 'alias', 'name', 'surname'), 'filter' => array('alias' => $data['alias']), 'preservekeys' => true)); if ($data['users']) { $user = reset($data['users']); $userId = $user['userid']; } } if (!$data['alias'] || $data['users']) { $from = zbxDateToTime($data['stime']); $till = $from + $effectivePeriod; // fetch alerts for different objects and sources and combine them in a single stream foreach (eventSourceObjects() as $eventSource) { $data['alerts'] = array_merge($data['alerts'], API::Alert()->get(array('output' => array('alertid', 'actionid', 'userid', 'clock', 'sendto', 'subject', 'message', 'status', 'retries', 'error', 'alerttype'), 'selectMediatypes' => array('mediatypeid', 'description'), 'userids' => $userId, 'time_from' => $from, 'time_till' => $till, 'eventsource' => $eventSource['source'], 'eventobject' => $eventSource['object'], 'sortfield' => 'alertid', 'sortorder' => ZBX_SORT_DOWN, 'limit' => $config['search_limit'] + 1))); } CArrayHelper::sort($data['alerts'], array(array('field' => 'alertid', 'order' => ZBX_SORT_DOWN))); $data['alerts'] = array_slice($data['alerts'], 0, $config['search_limit'] + 1); // paging $data['paging'] = getPagingLine($data['alerts'], ZBX_SORT_DOWN); // get users if (!$data['alias']) { $data['users'] = API::User()->get(array('output' => array('userid', 'alias', 'name', 'surname'), 'userids' => zbx_objectValues($data['alerts'], 'userid'), 'preservekeys' => true)); } } // get first alert clock $firstAlert = null; if ($userId) { $firstAlert = DBfetch(DBselect('SELECT MIN(a.clock) AS clock' . ' FROM alerts a' . ' WHERE a.userid=' . zbx_dbstr($userId))); } elseif ($data['alias'] === '') {
if (isset($_REQUEST['filter_set']) || isset($_REQUEST['filter_rst'])) { CProfile::update('web.auditacts.filter.alias', $_REQUEST['alias'], PROFILE_TYPE_STR); } /* * Display */ $effectivePeriod = navigation_bar_calc('web.auditacts.timeline', 0, true); $data = array('stime' => get_request('stime'), 'alias' => get_request('alias'), 'alerts' => array()); $from = zbxDateToTime($data['stime']); $till = $from + $effectivePeriod; // get alerts $options = array('time_from' => $from, 'time_till' => $till, 'output' => API_OUTPUT_EXTEND, 'selectMediatypes' => API_OUTPUT_EXTEND, 'sortfield' => 'alertid', 'sortorder' => ZBX_SORT_DOWN, 'limit' => $config['search_limit'] + 1); if (!empty($data['alias'])) { $users = API::User()->get(array('filter' => array('alias' => $data['alias']))); $options['userids'] = zbx_objectValues($users, 'userid'); } $data['alerts'] = API::Alert()->get($options); // get paging $data['paging'] = getPagingLine($data['alerts']); // get timeline unset($options['userids'], $options['time_from'], $options['time_till'], $options['selectMediatypes']); $options['limit'] = 1; $options['sortorder'] = ZBX_SORT_UP; $firstAlert = API::Alert()->get($options); $firstAlert = reset($firstAlert); $data['timeline'] = array('period' => $effectivePeriod, 'starttime' => date('YmdHis', !empty($firstAlert) ? $firstAlert['clock'] : time() - SEC_PER_HOUR), 'usertime' => isset($data['stime']) ? date('YmdHis', zbxDateToTime($data['stime']) + $effectivePeriod) : null); // render view $auditView = new CView('administration.auditacts.list', $data); $auditView->render(); $auditView->show(); require_once dirname(__FILE__) . '/include/page_footer.php';
/** * Get events data. * * @param _array $options * @param array $options['itemids'] * @param array $options['hostids'] * @param array $options['groupids'] * @param array $options['eventids'] * @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($options = array()) { $result = array(); $nodeCheck = array(); $userType = self::$userData['type']; $userid = self::$userData['userid']; // allowed columns for sorting $sortColumns = array('eventid', 'object', 'objectid'); // allowed output options for [ select_* ] params $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND); $sqlParts = array('select' => array($this->fieldId('eventid')), 'from' => array('events' => 'events e'), 'where' => array(), 'order' => array(), 'group' => array(), 'limit' => null); $defOptions = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'triggerids' => null, 'eventids' => null, 'editable' => null, 'object' => null, 'source' => null, 'acknowledged' => null, 'nopermissions' => null, 'showUnknown' => null, 'value' => null, 'time_from' => null, 'time_till' => null, 'eventid_from' => null, 'eventid_till' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectHosts' => null, 'selectItems' => null, 'selectTriggers' => null, 'select_alerts' => null, 'select_acknowledges' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null); $options = zbx_array_merge($defOptions, $options); // editable + PERMISSION CHECK if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) { if (is_null($options['source']) && is_null($options['object'])) { $options['object'] = EVENT_OBJECT_TRIGGER; } if ($options['object'] !== null && $options['object'] == EVENT_OBJECT_TRIGGER || $options['source'] !== null && $options['source'] == EVENT_SOURCE_TRIGGERS) { if (!is_null($options['triggerids'])) { $triggers = API::Trigger()->get(array('triggerids' => $options['triggerids'], 'editable' => $options['editable'])); $options['triggerids'] = zbx_objectValues($triggers, 'triggerid'); } else { $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 e.objectid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' AND e.object=' . EVENT_OBJECT_TRIGGER . ' GROUP BY f.triggerid' . ' HAVING MIN(r.permission)>=' . $permission . ')'; } } } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // eventids if (!is_null($options['eventids'])) { zbx_value2array($options['eventids']); $sqlParts['where'][] = dbConditionInt('e.eventid', $options['eventids']); if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'][] = DBin_node('e.objectid', $nodeids); } } // triggerids if (!is_null($options['triggerids']) && $options['object'] == EVENT_OBJECT_TRIGGER) { zbx_value2array($options['triggerids']); $sqlParts['where'][] = dbConditionInt('e.objectid', $options['triggerids']); if (!is_null($options['groupCount'])) { $sqlParts['group']['objectid'] = 'e.objectid'; } if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'][] = DBin_node('e.objectid', $nodeids); } } // groupids if (!is_null($options['groupids'])) { zbx_value2array($options['groupids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts = $this->addQuerySelect('hg.groupid', $sqlParts); } $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['from']['hosts_groups'] = 'hosts_groups hg'; $sqlParts['where']['hg'] = dbConditionInt('hg.groupid', $options['groupids']); $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid'; $sqlParts['where']['fe'] = 'f.triggerid=e.objectid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sqlParts = $this->addQuerySelect('i.hostid', $sqlParts); } $sqlParts['from']['functions'] = 'functions f'; $sqlParts['from']['items'] = 'items i'; $sqlParts['where']['i'] = dbConditionInt('i.hostid', $options['hostids']); $sqlParts['where']['ft'] = 'f.triggerid=e.objectid'; $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; } // should last, after all ****IDS checks if (!$nodeCheck) { $nodeCheck = true; $sqlParts['where'][] = DBin_node('e.eventid', $nodeids); } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sqlParts = $this->addQuerySelect('e.*', $sqlParts); } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sqlParts['select'] = array('COUNT(DISTINCT e.eventid) AS rowscount'); // groupCount if (!is_null($options['groupCount'])) { foreach ($sqlParts['group'] as $key => $fields) { $sqlParts['select'][$key] = $fields; } } } // object if (!is_null($options['object'])) { $sqlParts['where']['o'] = 'e.object=' . zbx_dbstr($options['object']); } // source if (!is_null($options['source'])) { $sqlParts['where'][] = 'e.source=' . zbx_dbstr($options['source']); } // acknowledged if (!is_null($options['acknowledged'])) { $sqlParts['where'][] = 'e.acknowledged=' . ($options['acknowledged'] ? 1 : 0); } // showUnknown if (!is_null($options['showUnknown'])) { if (is_null($options['filter'])) { $options['filter'] = array(); } $options['filter']['value_changed'] = null; } // time_from if (!is_null($options['time_from'])) { $sqlParts['where'][] = 'e.clock>=' . zbx_dbstr($options['time_from']); } // time_till if (!is_null($options['time_till'])) { $sqlParts['where'][] = 'e.clock<=' . zbx_dbstr($options['time_till']); } // eventid_from if (!is_null($options['eventid_from'])) { $sqlParts['where'][] = 'e.eventid>=' . zbx_dbstr($options['eventid_from']); } // eventid_till if (!is_null($options['eventid_till'])) { $sqlParts['where'][] = 'e.eventid<=' . zbx_dbstr($options['eventid_till']); } // value if (!is_null($options['value'])) { zbx_value2array($options['value']); $sqlParts['where'][] = dbConditionInt('e.value', $options['value']); } // search if (is_array($options['search'])) { zbx_db_search('events e', $options, $sqlParts); } // filter if (is_array($options['filter'])) { $this->dbFilter('events e', $options, $sqlParts); } // sorting zbx_db_sorting($sqlParts, $options, $sortColumns, $this->tableAlias()); // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sqlParts['limit'] = $options['limit']; } // selectHosts, selectTriggers, selectItems if ($options['output'] != API_OUTPUT_EXTEND && (!is_null($options['selectHosts']) || !is_null($options['selectTriggers']) || !is_null($options['selectItems']))) { $sqlParts = $this->addQuerySelect($this->fieldId('object'), $sqlParts); $sqlParts = $this->addQuerySelect($this->fieldId('objectid'), $sqlParts); } $eventids = array(); $triggerids = array(); // event fields $sqlParts['select'] = array_unique($sqlParts['select']); $sqlParts['from'] = array_unique($sqlParts['from']); $sqlParts['where'] = array_unique($sqlParts['where']); $sqlParts['order'] = array_unique($sqlParts['order']); $sqlParts['group'] = array_unique($sqlParts['group']); $sqlSelect = ''; $sqlFrom = ''; $sqlWhere = ''; $sqlOrder = ''; $sqlGroup = ''; if (!empty($sqlParts['select'])) { $sqlSelect .= implode(',', $sqlParts['select']); } if (!empty($sqlParts['from'])) { $sqlFrom .= implode(',', $sqlParts['from']); } if (!empty($sqlParts['where'])) { $sqlWhere .= implode(' AND ', $sqlParts['where']); } if (!empty($sqlParts['order'])) { $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']); } if (!empty($sqlParts['group'])) { $sqlGroup .= ' GROUP BY ' . implode(',', $sqlParts['group']); } $sqlLimit = $sqlParts['limit']; $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere . $sqlGroup . $sqlOrder; $dbRes = DBselect($sql, $sqlLimit); while ($event = DBfetch($dbRes)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $event; } else { $result = $event['rowscount']; } } else { $eventids[$event['eventid']] = $event['eventid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$event['eventid']] = array('eventid' => $event['eventid']); } else { if (isset($event['object']) && $event['object'] == EVENT_OBJECT_TRIGGER) { $triggerids[$event['objectid']] = $event['objectid']; } if (!isset($result[$event['eventid']])) { $result[$event['eventid']] = array(); } if (!is_null($options['selectHosts']) && !isset($result[$event['eventid']]['hosts'])) { $result[$event['eventid']]['hosts'] = array(); } if (!is_null($options['selectTriggers']) && !isset($result[$event['eventid']]['triggers'])) { $result[$event['eventid']]['triggers'] = array(); } if (!is_null($options['selectItems']) && !isset($result[$event['eventid']]['items'])) { $result[$event['eventid']]['items'] = array(); } if (!is_null($options['select_alerts']) && !isset($result[$event['eventid']]['alerts'])) { $result[$event['eventid']]['alerts'] = array(); } if (!is_null($options['select_acknowledges']) && !isset($result[$event['eventid']]['acknowledges'])) { $result[$event['eventid']]['acknowledges'] = array(); } // hostids if (isset($event['hostid']) && is_null($options['selectHosts'])) { if (!isset($result[$event['eventid']]['hosts'])) { $result[$event['eventid']]['hosts'] = array(); } $result[$event['eventid']]['hosts'][] = array('hostid' => $event['hostid']); unset($event['hostid']); } // triggerids if (isset($event['triggerid']) && is_null($options['selectTriggers'])) { if (!isset($result[$event['eventid']]['triggers'])) { $result[$event['eventid']]['triggers'] = array(); } $result[$event['eventid']]['triggers'][] = array('triggerid' => $event['triggerid']); unset($event['triggerid']); } // itemids if (isset($event['itemid']) && is_null($options['selectItems'])) { if (!isset($result[$event['eventid']]['items'])) { $result[$event['eventid']]['items'] = array(); } $result[$event['eventid']]['items'][] = array('itemid' => $event['itemid']); unset($event['itemid']); } $result[$event['eventid']] += $event; } } } if (!is_null($options['countOutput'])) { return $result; } /* * Adding objects */ // adding hosts if (!is_null($options['selectHosts']) && str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) { $hosts = API::Host()->get(array('nodeids' => $nodeids, 'output' => $options['selectHosts'], 'triggerids' => $triggerids, 'nopermissions' => true, 'preservekeys' => true)); $triggers = array(); foreach ($hosts as $hostid => $host) { $htriggers = $host['triggers']; unset($host['triggers']); foreach ($htriggers as $tnum => $trigger) { $triggerid = $trigger['triggerid']; if (!isset($triggers[$triggerid])) { $triggers[$triggerid] = array('hosts' => array()); } $triggers[$triggerid]['hosts'][] = $host; } } foreach ($result as $eventid => $event) { if (isset($triggers[$event['objectid']])) { $result[$eventid]['hosts'] = $triggers[$event['objectid']]['hosts']; } else { $result[$eventid]['hosts'] = array(); } } } // adding triggers if (!is_null($options['selectTriggers']) && str_in_array($options['selectTriggers'], $subselectsAllowedOutputs)) { $triggers = API::Trigger()->get(array('nodeids' => $nodeids, 'output' => $options['selectTriggers'], 'triggerids' => $triggerids, 'nopermissions' => true, 'preservekeys' => true)); foreach ($result as $eventid => $event) { if (isset($triggers[$event['objectid']])) { $result[$eventid]['triggers'][] = $triggers[$event['objectid']]; } else { $result[$eventid]['triggers'] = array(); } } } // adding items if (!is_null($options['selectItems']) && str_in_array($options['selectItems'], $subselectsAllowedOutputs)) { $dbItems = API::Item()->get(array('nodeids' => $nodeids, 'output' => $options['selectItems'], 'triggerids' => $triggerids, 'webitems' => true, 'nopermissions' => true, 'preservekeys' => true)); $items = array(); foreach ($dbItems as $itemid => $item) { $itriggers = $item['triggers']; unset($item['triggers']); foreach ($itriggers as $trigger) { if (!isset($items[$trigger['triggerid']])) { $items[$trigger['triggerid']] = array(); } $items[$trigger['triggerid']][] = $item; } } foreach ($result as $eventid => $event) { if (isset($items[$event['objectid']])) { $result[$eventid]['items'] = $items[$event['objectid']]; } else { $result[$eventid]['items'] = array(); } } } // adding alerts if (!is_null($options['select_alerts']) && str_in_array($options['select_alerts'], $subselectsAllowedOutputs)) { $dbAlerts = API::Alert()->get(array('output' => $options['select_alerts'], 'selectMediatypes' => API_OUTPUT_EXTEND, 'nodeids' => $nodeids, 'eventids' => $eventids, 'nopermissions' => true, 'preservekeys' => true, 'sortfield' => 'clock', 'sortorder' => ZBX_SORT_DOWN)); foreach ($dbAlerts as $alert) { $result[$alert['eventid']]['alerts'][] = $alert; } } // adding acknowledges if (!is_null($options['select_acknowledges'])) { if (is_array($options['select_acknowledges']) || str_in_array($options['select_acknowledges'], $subselectsAllowedOutputs)) { $res = DBselect('SELECT a.*,u.alias' . ' FROM acknowledges a' . ' LEFT JOIN users u ON u.userid=a.userid' . ' WHERE ' . dbConditionInt('a.eventid', $eventids) . ' ORDER BY a.clock DESC'); while ($ack = DBfetch($res)) { $result[$ack['eventid']]['acknowledges'][] = $ack; } } elseif ($options['select_acknowledges'] == API_OUTPUT_COUNT) { $res = DBselect('SELECT COUNT(a.acknowledgeid) AS rowscount,a.eventid' . ' FROM acknowledges a' . ' WHERE ' . dbConditionInt('a.eventid', $eventids) . ' GROUP BY a.eventid'); while ($ack = DBfetch($res)) { $result[$ack['eventid']]['acknowledges'] = $ack['rowscount']; } } elseif ($options['select_acknowledges'] == API_OUTPUT_EXTEND) { $res = DBselect('SELECT a.*' . ' FROM acknowledges a' . ' WHERE ' . dbConditionInt('a.eventid', $eventids) . ' ORDER BY a.clock DESC'); while ($ack = DBfetch($res)) { $result[$ack['eventid']]['acknowledges'] = $ack['rowscount']; } } } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
for ($t = $from; $t <= $to; $t++) { if (($start = get_time($t)) > time()) { break; } if (($end = get_time($t + 1)) > time()) { $end = time(); } $table_row = array(format_time($start), format_time2($end)); // getting all alerts in this period of time $options = array('output' => array('mediatypeid', 'userid'), 'time_from' => $start, 'time_till' => $end); // if we must get only specific media type, no need to select the other ones if ($media_type > 0) { $options['mediatypeids'] = $media_type; } // getting data through API $alert_info = API::Alert()->get($options); // counting alert count for each user and media type $summary = array(); foreach ($users as $userid => $alias) { $summary[$userid] = array(); $summary[$userid]['total'] = 0; $summary[$userid]['medias'] = array(); foreach ($media_types as $media_type_nr => $mt) { $summary[$userid]['medias'][$media_type_nr] = 0; } } foreach ($alert_info as $ai) { if (!isset($summary[$ai['userid']])) { continue; } $summary[$ai['userid']]['total']++;