Пример #1
0
 /**
  * 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 = [])
 {
     $result = [];
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sqlParts = ['select' => ['triggers' => 't.triggerid'], 'from' => ['t' => 'triggers t'], 'where' => [], 'group' => [], 'order' => [], 'limit' => null];
     $defOptions = ['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, 'expandDescription' => null, 'expandComment' => null, 'expandExpression' => null, 'output' => API_OUTPUT_EXTEND, '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);
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts_groups hgg' . ' LEFT 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 i.hostid' . ' HAVING MAX(permission)<' . zbx_dbstr($permission) . ' OR MIN(permission) IS NULL' . ' OR MIN(permission)=' . PERM_DENY . ')';
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         sort($options['groupids']);
         $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']);
         $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']);
         $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']);
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items_applications'] = 'items_applications ia';
         $sqlParts['where']['a'] = dbConditionInt('ia.applicationid', $options['applicationids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fia'] = 'f.itemid=ia.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.source=' . EVENT_SOURCE_TRIGGERS . ' AND e.object=' . EVENT_OBJECT_TRIGGER . ' 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.source=' . EVENT_SOURCE_TRIGGERS . ' AND e.object=' . EVENT_OBJECT_TRIGGER . ' 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'] = [];
     }
     if (is_array($options['filter'])) {
         if (!array_key_exists('flags', $options['filter'])) {
             $options['filter']['flags'] = [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'])) {
         $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'])) {
         $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';
         $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']);
     }
     // limit
     if (!zbx_ctype_digit($options['limit']) || !$options['limit']) {
         $options['limit'] = null;
     }
     $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQuerySortOptions($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;
     }
     $result = zbx_toHash($this->customFetch($this->createSelectQueryFromParts($sqlParts), $options), 'triggerid');
     // return count for post SQL filtered result sets
     if (!is_null($options['countOutput'])) {
         return count($result);
     }
     if ($result) {
         $result = $this->addRelatedObjects($options, $result);
     }
     // expandDescription
     if (!is_null($options['expandDescription']) && $result && array_key_exists('description', reset($result))) {
         $result = CMacrosResolverHelper::resolveTriggerNames($result);
     }
     // expandComment
     if (!is_null($options['expandComment']) && $result && array_key_exists('comments', reset($result))) {
         $result = CMacrosResolverHelper::resolveTriggerDescriptions($result);
     }
     // expand expression
     if ($options['expandExpression'] !== null && $result && array_key_exists('expression', reset($result))) {
         $result = CMacrosResolverHelper::resolveTriggerExpressions($result, ['resolve_usermacros' => true, 'resolve_macros' => true]);
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     $result = $this->unsetExtraFields($result, ['state', 'expression'], $options['output']);
     return $result;
 }