/** * Check if 'dbversion' table exists. * * @return boolean */ protected function checkDbVersionTable() { global $DB; $tabSchema = zbx_dbstr(!empty($DB['SCHEMA']) ? $DB['SCHEMA'] : zbx_strtoupper($DB['USER'])); $tableExists = DBfetch(DBselect('SELECT 1 FROM SYSCAT.TABLES' . " WHERE TABNAME='DBVERSION'" . " AND TABSCHEMA=" . $tabSchema)); if (!$tableExists) { $this->setError(_('The frontend does not match Zabbix database.')); return false; } return true; }
$result = true; if (isset($_REQUEST['save'])) { try { DBstart(); $globalMacros = API::UserMacro()->get(array('globalmacro' => true, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true)); $newMacros = get_request('macros', array()); // remove empty new macro lines foreach ($newMacros as $number => $newMacro) { if (!isset($newMacro['globalmacroid']) && zbx_empty($newMacro['macro']) && zbx_empty($newMacro['value'])) { unset($newMacros[$number]); } } $duplicatedMacros = array(); foreach ($newMacros as $number => $newMacro) { // transform macros to uppercase {$aaa} => {$AAA} $newMacros[$number]['macro'] = zbx_strtoupper($newMacro['macro']); } // update $macrosToUpdate = array(); foreach ($newMacros as $number => $newMacro) { if (isset($newMacro['globalmacroid']) && isset($globalMacros[$newMacro['globalmacroid']])) { $dbGlobalMacro = $globalMacros[$newMacro['globalmacroid']]; // remove item from new macros array unset($newMacros[$number]); unset($globalMacros[$newMacro['globalmacroid']]); // if the macro is unchanged - skip it if ($dbGlobalMacro == $newMacro) { continue; } $macrosToUpdate[$newMacro['globalmacroid']] = $newMacro; }
} $interfaceTypes = array(INTERFACE_TYPE_AGENT, INTERFACE_TYPE_SNMP, INTERFACE_TYPE_JMX, INTERFACE_TYPE_IPMI); foreach ($interfaceTypes as $type) { if (isset($_REQUEST['mainInterfaces'][$type])) { $interfaces[$_REQUEST['mainInterfaces'][$type]]['main'] = '1'; } } // ignore empty new macros, i.e., macros rows that have not been filled foreach ($macros as $key => $macro) { if (!isset($macro['hostmacroid']) && zbx_empty($macro['macro']) && zbx_empty($macro['value'])) { unset($macros[$key]); } } foreach ($macros as $key => $macro) { // transform macros to uppercase {$aaa} => {$AAA} $macros[$key]['macro'] = zbx_strtoupper($macro['macro']); } // create new group if (!zbx_empty($_REQUEST['newgroup'])) { if (!($newGroup = API::HostGroup()->create(array('name' => $_REQUEST['newgroup'])))) { throw new Exception(); } $groups[] = reset($newGroup['groupids']); } $groups = zbx_toObject($groups, 'groupid'); $host = array('host' => $_REQUEST['host'], 'name' => $_REQUEST['visiblename'], 'status' => $_REQUEST['status'], 'proxy_hostid' => get_request('proxy_hostid', 0), 'ipmi_authtype' => get_request('ipmi_authtype'), 'ipmi_privilege' => get_request('ipmi_privilege'), 'ipmi_username' => get_request('ipmi_username'), 'ipmi_password' => get_request('ipmi_password'), 'groups' => $groups, 'templates' => $templates, 'interfaces' => $interfaces, 'macros' => $macros, 'inventory' => get_request('inventory_mode') != HOST_INVENTORY_DISABLED ? get_request('host_inventory', array()) : null, 'inventory_mode' => get_request('inventory_mode')); if (!$createNew) { $host['templates_clear'] = zbx_toObject(get_request('clear_templates', array()), 'templateid'); } } ##############################################################################################
function selectByPattern(&$table, $column, $pattern, $limit) { $chunk_size = $limit; $rsTable = array(); foreach ($table as $num => $row) { if (zbx_strtoupper($row[$column]) == zbx_strtoupper($pattern)) { $rsTable = array($num => $row) + $rsTable; } elseif ($limit > 0) { $rsTable[$num] = $row; } else { continue; } $limit--; } if (!empty($rsTable)) { $rsTable = array_chunk($rsTable, $chunk_size, true); $rsTable = $rsTable[0]; } return $rsTable; }
} if ($templateid) { $msg_ok = _('Template updated'); $msg_fail = _('Cannot update template'); } else { $msg_ok = _('Template added'); $msg_fail = _('Cannot add template'); } foreach ($macros as $mnum => $macro) { if (zbx_empty($macro['macro']) && zbx_empty($macro['value'])) { unset($macros[$mnum]); } } foreach ($macros as $mnum => $macro) { // transform macros to uppercase {$aaa} => {$AAA} $macros[$mnum]['macro'] = zbx_strtoupper($macro['macro']); } // Create new group $groups = zbx_toObject($groups, 'groupid'); if (!zbx_empty($newgroup)) { $result = API::HostGroup()->create(array('name' => $newgroup)); if (!$result) { throw new Exception(); } $newgroup = API::HostGroup()->get(array('groupids' => $result['groupids'], 'output' => API_OUTPUT_EXTEND)); if ($newgroup) { $groups = array_merge($groups, $newgroup); } else { throw new Exception(); } }
/** * Retrieve an HTTP Header * @param string Case-Insensitive HTTP Header Name (eg: "User-Agent") */ public function header($name) { $name = zbx_strtoupper($name); return isset($this->headers[$name]) ? $this->headers[$name] : false; }
** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ ?> <?php global $TRANSLATION; $TRANSLATION=array(' . "\n\n"; foreach ($_REQUEST['langTo'] as $key => $value) { $value = str_replace('\\"', '"', addslashes($value)); // $value = preg_replace("/([^\\\])\'/uU", "$1\\\'", $value); if (!empty($value)) { $output .= "\t'" . zbx_strtoupper($key) . "'=>\t\t\t'" . $value . "',\n"; } } $output .= ' ); ?> '; print $output; die; } } if (isset($_REQUEST['next'])) { $help = new CHelp('web.view.php'); $help_table = new CTableInfo(); $help_table->setAttribute('style', 'width: 600px;'); if ($_REQUEST['extlang'] == 'new') {
function zbx_db_search($table, $options, &$sql_parts) { list($table, $tableShort) = explode(' ', $table); $tableSchema = DB::getSchema($table); if (!$tableSchema) { info(_s('Error in search request for table "%1$s".', $table)); } $start = is_null($options['startSearch']) ? '%' : ''; $exclude = is_null($options['excludeSearch']) ? '' : ' NOT '; $search = array(); foreach ($options['search'] as $field => $pattern) { if (!isset($tableSchema['fields'][$field]) || zbx_empty($pattern)) { continue; } if ($tableSchema['fields'][$field]['type'] != DB::FIELD_TYPE_CHAR && $tableSchema['fields'][$field]['type'] != DB::FIELD_TYPE_TEXT) { continue; } // escaping parameter that is about to be used in LIKE statement $pattern = str_replace("!", "!!", $pattern); $pattern = str_replace("%", "!%", $pattern); $pattern = str_replace("_", "!_", $pattern); if (empty($options['searchWildcardsEnabled'])) { $search[$field] = ' UPPER(' . $tableShort . '.' . $field . ') ' . $exclude . ' LIKE ' . zbx_dbstr($start . zbx_strtoupper($pattern) . '%') . " ESCAPE '!'"; } else { $pattern = str_replace("*", "%", $pattern); $search[$field] = ' UPPER(' . $tableShort . '.' . $field . ') ' . $exclude . ' LIKE ' . zbx_dbstr(zbx_strtoupper($pattern)) . " ESCAPE '!'"; } } if (!empty($search)) { if (isset($sql_parts['where']['search'])) { $search[] = $sql_parts['where']['search']; } $glue = is_null($options['searchByAny']) || $options['searchByAny'] === false ? ' AND ' : ' OR '; $sql_parts['where']['search'] = '( ' . implode($glue, $search) . ' )'; return true; } return false; }
/** * Get items data * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param array $options * @param array $options['itemids'] * @param array $options['hostids'] * @param array $options['groupids'] * @param array $options['triggerids'] * @param array $options['applicationids'] * @param boolean $options['status'] * @param boolean $options['templated_items'] * @param boolean $options['editable'] * @param boolean $options['count'] * @param string $options['pattern'] * @param int $options['limit'] * @param string $options['order'] * @return array|int item data as array or false if error */ public static function get($options = array()) { global $USER_DETAILS; $result = array(); $user_type = $USER_DETAILS['type']; $userid = $USER_DETAILS['userid']; $sort_columns = array('itemid', 'description', 'key_', 'delay', 'history', 'trends', 'type', 'status'); // allowed columns for sorting $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND); // allowed output options for [ select_* ] params $sql_parts = array('select' => array('items' => 'i.itemid'), 'from' => array('items' => 'items i'), 'where' => array('webtype' => 'i.type<>9'), 'group' => array(), 'order' => array(), 'limit' => null); $def_options = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'proxyids' => null, 'itemids' => null, 'graphids' => null, 'triggerids' => null, 'applicationids' => null, 'webitems' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'group' => null, 'host' => null, 'application' => null, 'belongs' => null, 'with_triggers' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_hosts' => null, 'select_triggers' => null, 'select_graphs' => null, 'select_applications' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null); $options = zbx_array_merge($def_options, $options); if (!is_null($options['extendoutput'])) { $options['output'] = API_OUTPUT_EXTEND; if (!is_null($options['select_hosts'])) { $options['select_hosts'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_triggers'])) { $options['select_triggers'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_graphs'])) { $options['select_graphs'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_applications'])) { $options['select_applications'] = API_OUTPUT_EXTEND; } } // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) { } else { $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY; $sql_parts['from']['hosts_groups'] = 'hosts_groups hg'; $sql_parts['from']['rights'] = 'rights r'; $sql_parts['from']['users_groups'] = 'users_groups ug'; $sql_parts['where'][] = 'hg.hostid=i.hostid'; $sql_parts['where'][] = 'r.id=hg.groupid '; $sql_parts['where'][] = 'r.groupid=ug.usrgrpid'; $sql_parts['where'][] = 'ug.userid=' . $userid; $sql_parts['where'][] = 'r.permission>=' . $permission; $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=hg.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . ')'; } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // groupids if (!is_null($options['groupids'])) { zbx_value2array($options['groupids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['groupid'] = 'hg.groupid'; } $sql_parts['from']['hosts_groups'] = 'hosts_groups hg'; $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']); $sql_parts['where'][] = 'hg.hostid=i.hostid'; if (!is_null($options['groupCount'])) { $sql_parts['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_EXTEND) { $sql_parts['select']['hostid'] = 'i.hostid'; } $sql_parts['where']['hostid'] = DBcondition('i.hostid', $options['hostids']); if (!is_null($options['groupCount'])) { $sql_parts['group']['i'] = 'i.hostid'; } } // proxyids if (!is_null($options['proxyids'])) { zbx_value2array($options['proxyids']); if ($options['output'] != API_OUTPUT_EXTEND) { $sql_parts['select']['proxyid'] = 'h.proxy_hostid'; } $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where'][] = DBcondition('h.proxy_hostid', $options['proxyids']); $sql_parts['where'][] = 'h.hostid=i.hostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['h'] = 'h.proxy_hostid'; } } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); $sql_parts['where']['itemid'] = DBcondition('i.itemid', $options['itemids']); } // triggerids if (!is_null($options['triggerids'])) { zbx_value2array($options['triggerids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['triggerid'] = 'f.triggerid'; } $sql_parts['from']['functions'] = 'functions f'; $sql_parts['where'][] = DBcondition('f.triggerid', $options['triggerids']); $sql_parts['where']['if'] = 'i.itemid=f.itemid'; } // applicationids if (!is_null($options['applicationids'])) { zbx_value2array($options['applicationids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['applicationid'] = 'ia.applicationid'; } $sql_parts['from']['items_applications'] = 'items_applications ia'; $sql_parts['where'][] = DBcondition('ia.applicationid', $options['applicationids']); $sql_parts['where']['ia'] = 'ia.itemid=i.itemid'; } // graphids if (!is_null($options['graphids'])) { zbx_value2array($options['graphids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['graphid'] = 'gi.graphid'; } $sql_parts['from']['graphs_items'] = 'graphs_items gi'; $sql_parts['where'][] = DBcondition('gi.graphid', $options['graphids']); $sql_parts['where']['igi'] = 'i.itemid=gi.itemid'; } // webitems if (!is_null($options['webitems'])) { unset($sql_parts['where']['webtype']); } // inherited if (!is_null($options['inherited'])) { if ($options['inherited']) { $sql_parts['where'][] = 'i.templateid>0'; } else { $sql_parts['where'][] = 'i.templateid=0'; } } // templated if (!is_null($options['templated'])) { $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; if ($options['templated']) { $sql_parts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE; } else { $sql_parts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE; } } // monitored if (!is_null($options['monitored'])) { $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; if ($options['monitored']) { $sql_parts['where'][] = 'h.status=' . HOST_STATUS_MONITORED; $sql_parts['where'][] = 'i.status=' . ITEM_STATUS_ACTIVE; } else { $sql_parts['where'][] = '(h.status<>' . HOST_STATUS_MONITORED . ' OR i.status<>' . ITEM_STATUS_ACTIVE . ')'; } } // search if (is_array($options['search'])) { zbx_db_search('items i', $options, $sql_parts); } // --- FILTER --- if (is_array($options['filter'])) { zbx_db_filter('items i', $options, $sql_parts); if (isset($options['filter']['host'])) { zbx_value2array($options['filter']['host']); $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; $sql_parts['where']['h'] = DBcondition('h.host', $options['filter']['host'], false, true); } } // group if (!is_null($options['group'])) { if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['name'] = 'g.name'; } $sql_parts['from']['groups'] = 'groups g'; $sql_parts['from']['hosts_groups'] = 'hosts_groups hg'; $sql_parts['where']['ghg'] = 'g.groupid = hg.groupid'; $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid'; $sql_parts['where'][] = ' UPPER(g.name)=' . zbx_dbstr(zbx_strtoupper($options['group'])); } // host if (!is_null($options['host'])) { if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['host'] = 'h.host'; } $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; $sql_parts['where'][] = ' UPPER(h.host)=' . zbx_dbstr(zbx_strtoupper($options['host'])); } // application if (!is_null($options['application'])) { if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['application'] = 'a.name as application'; } $sql_parts['from']['applications'] = 'applications a'; $sql_parts['from']['items_applications'] = 'items_applications ia'; $sql_parts['where']['aia'] = 'a.applicationid = ia.applicationid'; $sql_parts['where']['iai'] = 'ia.itemid=i.itemid'; $sql_parts['where'][] = ' UPPER(a.name)=' . zbx_dbstr(zbx_strtoupper($options['application'])); } // with_triggers if (!is_null($options['with_triggers'])) { if ($options['with_triggers'] == 1) { $sql_parts['where'][] = ' EXISTS ( SELECT functionid FROM functions ff WHERE ff.itemid=i.itemid )'; } else { $sql_parts['where'][] = 'NOT EXISTS ( SELECT functionid FROM functions ff WHERE ff.itemid=i.itemid )'; } } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sql_parts['select']['items'] = 'i.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sql_parts['select'] = array('count(DISTINCT i.itemid) as rowscount'); //groupCount if (!is_null($options['groupCount'])) { foreach ($sql_parts['group'] as $key => $fields) { $sql_parts['select'][$key] = $fields; } } } // order // restrict not allowed columns for sorting $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : ''; if (!zbx_empty($options['sortfield'])) { $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP; $sql_parts['order'][] = 'i.' . $options['sortfield'] . ' ' . $sortorder; if (!str_in_array('i.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('i.*', $sql_parts['select'])) { $sql_parts['select'][] = 'i.' . $options['sortfield']; } } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sql_parts['limit'] = $options['limit']; } //---------- $itemids = array(); $sql_parts['select'] = array_unique($sql_parts['select']); $sql_parts['from'] = array_unique($sql_parts['from']); $sql_parts['where'] = array_unique($sql_parts['where']); $sql_parts['group'] = array_unique($sql_parts['group']); $sql_parts['order'] = array_unique($sql_parts['order']); $sql_select = ''; $sql_from = ''; $sql_where = ''; $sql_group = ''; $sql_order = ''; if (!empty($sql_parts['select'])) { $sql_select .= implode(',', $sql_parts['select']); } if (!empty($sql_parts['from'])) { $sql_from .= implode(',', $sql_parts['from']); } if (!empty($sql_parts['where'])) { $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']); } if (!empty($sql_parts['group'])) { $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']); } if (!empty($sql_parts['order'])) { $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']); } $sql_limit = $sql_parts['limit']; $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('i.itemid', $nodeids) . $sql_where . $sql_group . $sql_order; $res = DBselect($sql, $sql_limit); while ($item = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $item; } else { $result = $item['rowscount']; } } else { $itemids[$item['itemid']] = $item['itemid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$item['itemid']] = array('itemid' => $item['itemid']); } else { if (!isset($result[$item['itemid']])) { $result[$item['itemid']] = array(); } if (!is_null($options['select_hosts']) && !isset($result[$item['itemid']]['hosts'])) { $result[$item['itemid']]['hosts'] = array(); } if (!is_null($options['select_triggers']) && !isset($result[$item['itemid']]['triggers'])) { $result[$item['itemid']]['triggers'] = array(); } if (!is_null($options['select_graphs']) && !isset($result[$item['itemid']]['graphs'])) { $result[$item['itemid']]['graphs'] = array(); } if (!is_null($options['select_applications']) && !isset($result[$item['itemid']]['applications'])) { $result[$item['itemid']]['applications'] = array(); } // hostids if (isset($item['hostid']) && is_null($options['select_hosts'])) { if (!isset($result[$item['itemid']]['hosts'])) { $result[$item['itemid']]['hosts'] = array(); } $result[$item['itemid']]['hosts'][] = array('hostid' => $item['hostid']); // unset($item['hostid']); } // triggerids if (isset($item['triggerid']) && is_null($options['select_triggers'])) { if (!isset($result[$item['itemid']]['triggers'])) { $result[$item['itemid']]['triggers'] = array(); } $result[$item['itemid']]['triggers'][] = array('triggerid' => $item['triggerid']); unset($item['triggerid']); } // graphids if (isset($item['graphid']) && is_null($options['select_graphs'])) { if (!isset($result[$item['itemid']]['graphs'])) { $result[$item['itemid']]['graphs'] = array(); } $result[$item['itemid']]['graphs'][] = array('graphid' => $item['graphid']); unset($item['graphid']); } // applicationids if (isset($item['applicationid']) && is_null($options['select_applications'])) { if (!isset($result[$item['itemid']]['applications'])) { $result[$item['itemid']]['applications'] = array(); } $result[$item['itemid']]['applications'][] = array('applicationid' => $item['applicationid']); unset($item['applicationid']); } $result[$item['itemid']] += $item; } } } COpt::memoryPick(); if (!is_null($options['countOutput'])) { if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; } // Adding Objects // Adding hosts if (!is_null($options['select_hosts'])) { if (is_array($options['select_hosts']) || str_in_array($options['select_hosts'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'itemids' => $itemids, 'templated_hosts' => 1, 'output' => $options['select_hosts'], 'nopermissions' => 1, 'preservekeys' => 1); $hosts = CHost::get($obj_params); foreach ($hosts as $hostid => $host) { $hitems = $host['items']; unset($host['items']); foreach ($hitems as $inum => $item) { $result[$item['itemid']]['hosts'][] = $host; } } $templates = CTemplate::get($obj_params); foreach ($templates as $templateid => $template) { $titems = $template['items']; unset($template['items']); foreach ($titems as $inum => $item) { $result[$item['itemid']]['hosts'][] = $template; } } } } // Adding triggers if (!is_null($options['select_triggers']) && str_in_array($options['select_triggers'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_triggers'], 'itemids' => $itemids, 'preservekeys' => 1); $triggers = CTrigger::get($obj_params); foreach ($triggers as $triggerid => $trigger) { $titems = $trigger['items']; unset($trigger['items']); foreach ($titems as $inum => $item) { $result[$item['itemid']]['triggers'][] = $trigger; } } } // Adding graphs if (!is_null($options['select_graphs']) && str_in_array($options['select_graphs'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_graphs'], 'itemids' => $itemids, 'preservekeys' => 1); $graphs = CGraph::get($obj_params); foreach ($graphs as $graphid => $graph) { $gitems = $graph['items']; unset($graph['items']); foreach ($gitems as $inum => $item) { $result[$item['itemid']]['graphs'][] = $graph; } } } // Adding applications if (!is_null($options['select_applications']) && str_in_array($options['select_applications'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_applications'], 'itemids' => $itemids, 'preservekeys' => 1); $applications = CApplication::get($obj_params); foreach ($applications as $applicationid => $application) { $aitems = $application['items']; unset($application['items']); foreach ($aitems as $inum => $item) { $result[$item['itemid']]['applications'][] = $application; } } } COpt::memoryPick(); // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
/** * 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['extendoutput'] * @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 static function get($options = array()) { global $USER_DETAILS; $result = array(); $user_type = $USER_DETAILS['type']; $userid = $USER_DETAILS['userid']; $sort_columns = array('triggerid', 'description', 'status', 'priority', 'lastchange'); // allowed columns for sorting $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND); // allowed output options for [ select_* ] params $sql_parts = array('select' => array('triggers' => 't.triggerid'), 'from' => array('t' => 'triggers t'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $def_options = 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, 'startSearch' => null, 'excludeSearch' => null, 'expandData' => null, 'expandDescription' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'select_hosts' => null, 'select_items' => null, 'select_functions' => null, 'select_dependencies' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null); $options = zbx_array_merge($def_options, $options); if (!is_null($options['extendoutput'])) { $options['output'] = API_OUTPUT_EXTEND; if (!is_null($options['select_hosts'])) { $options['select_hosts'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_items'])) { $options['select_items'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_dependencies'])) { $options['select_dependencies'] = API_OUTPUT_EXTEND; } } if (is_array($options['output'])) { unset($sql_parts['select']['triggers']); foreach ($options['output'] as $key => $field) { $sql_parts['select'][$field] = ' t.' . $field; } $options['output'] = API_OUTPUT_CUSTOM; } // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) { } else { $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY; /*/ $sql_parts['where'][] = ' EXISTS( '. ' SELECT tt.triggerid '. ' FROM triggers tt,functions ff,items ii,hosts_groups hgg,rights rr,users_groups ugg '. ' WHERE t.triggerid=tt.triggerid '. ' AND ff.triggerid=tt.triggerid '. ' AND ff.itemid=ii.itemid '. ' AND hgg.hostid=ii.hostid '. ' AND rr.id=hgg.groupid '. ' AND rr.groupid=ugg.usrgrpid '. ' AND ugg.userid='.$userid. ' AND rr.permission>='.$permission. ' AND NOT EXISTS( '. ' SELECT fff.triggerid '. ' FROM functions fff, items iii '. ' WHERE fff.triggerid=tt.triggerid '. ' AND fff.itemid=iii.itemid '. ' '. ' AND EXISTS( '. ' SELECT hggg.groupid '. ' FROM hosts_groups hggg, rights rrr, users_groups uggg '. ' WHERE hggg.hostid=iii.hostid '. ' AND rrr.id=hggg.groupid '. ' AND rrr.groupid=uggg.usrgrpid '. ' AND uggg.userid='.$userid. ' AND rrr.permission<'.$permission. ' ) '. ' ) '. ' ) '; //*/ //*/ $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['from']['hosts_groups'] = 'hosts_groups hg'; $sql_parts['from']['rights'] = 'rights r'; $sql_parts['from']['users_groups'] = 'users_groups ug'; $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid'; $sql_parts['where'][] = 'r.id=hg.groupid '; $sql_parts['where'][] = 'r.groupid=ug.usrgrpid'; $sql_parts['where'][] = 'ug.userid=' . $userid; $sql_parts['where'][] = 'r.permission>=' . $permission; $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT ff.triggerid ' . ' FROM functions ff, items ii ' . ' WHERE ff.triggerid=t.triggerid ' . ' AND ff.itemid=ii.itemid ' . ' AND EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=ii.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . '))'; //*/ } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // groupids if (!is_null($options['groupids'])) { zbx_value2array($options['groupids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['groupid'] = 'hg.groupid'; } $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['from']['hosts_groups'] = 'hosts_groups hg'; $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid'; $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; $sql_parts['where']['groupid'] = DBcondition('hg.groupid', $options['groupids']); if (!is_null($options['groupCount'])) { $sql_parts['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) { $sql_parts['select']['hostid'] = 'i.hostid'; } $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['where']['hostid'] = DBcondition('i.hostid', $options['hostids']); $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['i'] = 'i.hostid'; } } // triggerids if (!is_null($options['triggerids'])) { zbx_value2array($options['triggerids']); $sql_parts['where']['triggerid'] = DBcondition('t.triggerid', $options['triggerids']); } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['itemid'] = 'f.itemid'; } $sql_parts['from']['functions'] = 'functions f'; $sql_parts['where']['itemid'] = DBcondition('f.itemid', $options['itemids']); $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; } // applicationids if (!is_null($options['applicationids'])) { zbx_value2array($options['applicationids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['applicationid'] = 'a.applicationid'; } $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['from']['applications'] = 'applications a'; $sql_parts['where']['a'] = DBcondition('a.applicationid', $options['applicationids']); $sql_parts['where']['ia'] = 'i.hostid=a.hostid'; $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; } // functions if (!is_null($options['functions'])) { zbx_value2array($options['functions']); $sql_parts['from']['functions'] = 'functions f'; $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where'][] = DBcondition('f.function', $options['functions'], false, true); } // monitored if (!is_null($options['monitored'])) { $sql_parts['where']['monitored'] = '' . ' NOT EXISTS (' . ' SELECT ff.functionid' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT ii.itemid' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND (' . ' ii.status<>' . ITEM_STATUS_ACTIVE . ' OR hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )' . ' )'; $sql_parts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED; } // active if (!is_null($options['active'])) { $sql_parts['where']['active'] = '' . ' NOT EXISTS (' . ' SELECT ff.functionid' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT ii.itemid' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )'; $sql_parts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED; } // maintenance if (!is_null($options['maintenance'])) { $sql_parts['where'][] = ($options['maintenance'] == 0 ? ' NOT ' : '') . ' EXISTS (' . ' SELECT ff.functionid' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT ii.itemid' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND hh.maintenance_status=1' . ' )' . ' )'; $sql_parts['where'][] = 't.status=' . TRIGGER_STATUS_ENABLED; } // lastChangeSince if (!is_null($options['lastChangeSince'])) { $sql_parts['where']['lastchangesince'] = 't.lastchange>' . $options['lastChangeSince']; } // lastChangeTill if (!is_null($options['lastChangeTill'])) { $sql_parts['where']['lastchangetill'] = 't.lastchange<' . $options['lastChangeTill']; } // withUnacknowledgedEvents if (!is_null($options['withUnacknowledgedEvents'])) { $sql_parts['where']['unack'] = ' EXISTS(' . ' SELECT e.eventid' . ' FROM events e' . ' WHERE e.objectid=t.triggerid' . ' AND e.object=0' . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=0)'; } // withAcknowledgedEvents if (!is_null($options['withAcknowledgedEvents'])) { $sql_parts['where']['ack'] = 'NOT EXISTS(' . ' SELECT e.eventid' . ' FROM events e' . ' WHERE e.objectid=t.triggerid' . ' AND e.object=0' . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=0)'; } // templated if (!is_null($options['templated'])) { $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; if ($options['templated']) { $sql_parts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE; } else { $sql_parts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE; } } // inherited if (!is_null($options['inherited'])) { if ($options['inherited']) { $sql_parts['where'][] = 't.templateid<>0'; } else { $sql_parts['where'][] = 't.templateid=0'; } } // search if (is_array($options['search'])) { zbx_db_search('triggers t', $options, $sql_parts); } // --- FILTER --- if (is_array($options['filter'])) { zbx_db_filter('triggers t', $options, $sql_parts); if (isset($options['filter']['host']) && !is_null($options['filter']['host'])) { zbx_value2array($options['filter']['host']); $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; $sql_parts['where']['host'] = DBcondition('h.host', $options['filter']['host'], false, true); } if (isset($options['filter']['hostid']) && !is_null($options['filter']['hostid'])) { zbx_value2array($options['filter']['hostid']); $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; $sql_parts['where']['hostid'] = DBcondition('i.hostid', $options['filter']['hostid']); } } // group if (!is_null($options['group'])) { if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['name'] = 'g.name'; } $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['from']['hosts_groups'] = 'hosts_groups hg'; $sql_parts['from']['groups'] = 'groups g'; $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid'; $sql_parts['where']['ghg'] = 'g.groupid = hg.groupid'; $sql_parts['where']['group'] = ' UPPER(g.name)=' . zbx_dbstr(zbx_strtoupper($options['group'])); } // host if (!is_null($options['host'])) { if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['host'] = 'h.host'; } $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where']['i'] = DBcondition('i.hostid', $options['hostids']); $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; $sql_parts['where']['host'] = ' UPPER(h.host)=' . zbx_dbstr(zbx_strtoupper($options['host'])); } // only_true if (!is_null($options['only_true'])) { $sql_parts['where']['ot'] = '((t.value=' . TRIGGER_VALUE_TRUE . ')' . ' OR ' . '((t.value=' . TRIGGER_VALUE_FALSE . ') AND (t.lastchange>' . (time() - TRIGGER_FALSE_PERIOD) . ')))'; } // min_severity if (!is_null($options['min_severity'])) { $sql_parts['where'][] = 't.priority>=' . $options['min_severity']; } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sql_parts['select']['triggers'] = 't.*'; } // expandData if (!is_null($options['expandData'])) { $sql_parts['select']['host'] = 'h.host'; $sql_parts['select']['hostid'] = 'h.hostid'; $sql_parts['from']['functions'] = 'functions f'; $sql_parts['from']['items'] = 'items i'; $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sql_parts['select'] = array('COUNT(DISTINCT t.triggerid) as rowscount'); // groupCount if (!is_null($options['groupCount'])) { foreach ($sql_parts['group'] as $key => $fields) { $sql_parts['select'][$key] = $fields; } } } // order // restrict not allowed columns for sorting $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : ''; if (!zbx_empty($options['sortfield'])) { $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP; $sql_parts['order'][] = 't.' . $options['sortfield'] . ' ' . $sortorder; if (!str_in_array('t.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('t.*', $sql_parts['select'])) { $sql_parts['select'][] = 't.' . $options['sortfield']; } } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sql_parts['limit'] = $options['limit']; } //--------------- $triggerids = array(); $sql_parts['select'] = array_unique($sql_parts['select']); $sql_parts['from'] = array_unique($sql_parts['from']); $sql_parts['where'] = array_unique($sql_parts['where']); $sql_parts['group'] = array_unique($sql_parts['group']); $sql_parts['order'] = array_unique($sql_parts['order']); $sql_select = ''; $sql_from = ''; $sql_where = ''; $sql_group = ''; $sql_order = ''; if (!empty($sql_parts['select'])) { $sql_select .= implode(',', $sql_parts['select']); } if (!empty($sql_parts['from'])) { $sql_from .= implode(',', $sql_parts['from']); } if (!empty($sql_parts['where'])) { $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']); } if (!empty($sql_parts['group'])) { $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']); } if (!empty($sql_parts['order'])) { $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']); } $sql_limit = $sql_parts['limit']; $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('t.triggerid', $nodeids) . $sql_where . $sql_group . $sql_order; //SDI($sql); $db_res = DBselect($sql, $sql_limit); while ($trigger = DBfetch($db_res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $trigger; } else { $result = $trigger['rowscount']; } } else { $triggerids[$trigger['triggerid']] = $trigger['triggerid']; 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['select_hosts']) && !isset($result[$trigger['triggerid']]['hosts'])) { $result[$trigger['triggerid']]['hosts'] = array(); } if (!is_null($options['select_items']) && !isset($result[$trigger['triggerid']]['items'])) { $result[$trigger['triggerid']]['items'] = array(); } if (!is_null($options['select_functions']) && !isset($result[$trigger['triggerid']]['functions'])) { $result[$trigger['triggerid']]['functions'] = array(); } if (!is_null($options['select_dependencies']) && !isset($result[$trigger['triggerid']]['dependencies'])) { $result[$trigger['triggerid']]['dependencies'] = array(); } // groups if (isset($trigger['groupid']) && is_null($options['select_groups'])) { if (!isset($result[$trigger['triggerid']]['groups'])) { $result[$trigger['triggerid']]['groups'] = array(); } $result[$trigger['triggerid']]['groups'][] = array('groupid' => $trigger['groupid']); unset($trigger['groupid']); } // hostids if (isset($trigger['hostid']) && is_null($options['select_hosts'])) { if (!isset($result[$trigger['triggerid']]['hosts'])) { $result[$trigger['triggerid']]['hosts'] = array(); } $result[$trigger['triggerid']]['hosts'][] = array('hostid' => $trigger['hostid']); if (is_null($options['expandData'])) { unset($trigger['hostid']); } } // itemids if (isset($trigger['itemid']) && is_null($options['select_items'])) { if (!isset($result[$trigger['triggerid']]['items'])) { $result[$trigger['triggerid']]['items'] = array(); } $result[$trigger['triggerid']]['items'][] = array('itemid' => $trigger['itemid']); unset($trigger['itemid']); } $result[$trigger['triggerid']] += $trigger; } } } Copt::memoryPick(); if (!is_null($options['countOutput'])) { if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; } // skipDependent if (!is_null($options['skipDependent'])) { $tids = $triggerids; $map = array(); do { $sql = 'SELECT d.triggerid_down, d.triggerid_up, t.value ' . ' FROM trigger_depends d, triggers t ' . ' WHERE ' . DBcondition('d.triggerid_down', $tids) . ' AND d.triggerid_up=t.triggerid'; $db_result = DBselect($sql); $tids = array(); while ($row = DBfetch($db_result)) { if (TRIGGER_VALUE_TRUE == $row['value']) { if (isset($map[$row['triggerid_down']])) { foreach ($map[$row['triggerid_down']] as $triggerid => $state) { unset($result[$triggerid]); unset($triggerids[$triggerid]); } } else { unset($result[$row['triggerid_down']]); unset($triggerids[$row['triggerid_down']]); } } else { if (isset($map[$row['triggerid_down']])) { if (!isset($map[$row['triggerid_up']])) { $map[$row['triggerid_up']] = array(); } $map[$row['triggerid_up']] += $map[$row['triggerid_down']]; } else { if (!isset($map[$row['triggerid_up']])) { $map[$row['triggerid_up']] = array(); } $map[$row['triggerid_up']][$row['triggerid_down']] = 1; } $tids[] = $row['triggerid_up']; } } } while (!empty($tids)); } // withLastEventUnacknowledged if (!is_null($options['withLastEventUnacknowledged'])) { $eventids = array(); $sql = 'SELECT max(e.eventid) as eventid, e.objectid' . ' FROM events e ' . ' WHERE e.object=' . EVENT_OBJECT_TRIGGER . ' AND ' . DBcondition('e.objectid', $triggerids) . ' AND ' . DBcondition('e.value', array(TRIGGER_VALUE_TRUE)) . ' GROUP BY e.objectid'; $events_db = DBselect($sql); while ($event = DBfetch($events_db)) { $eventids[] = $event['eventid']; } $correct_triggerids = array(); $sql = 'SELECT e.objectid' . ' FROM events e ' . ' WHERE ' . DBcondition('e.eventid', $eventids) . ' AND e.acknowledged=0'; $triggers_db = DBselect($sql); while ($trigger = DBfetch($triggers_db)) { $correct_triggerids[$trigger['objectid']] = $trigger['objectid']; } foreach ($result as $triggerid => $trigger) { if (!isset($correct_triggerids[$triggerid])) { unset($result[$triggerid]); unset($triggerids[$triggerid]); } } } // Adding Objects // Adding trigger dependencies if (!is_null($options['select_dependencies']) && str_in_array($options['select_dependencies'], $subselects_allowed_outputs)) { $deps = array(); $depids = array(); $sql = 'SELECT triggerid_up, triggerid_down ' . ' FROM trigger_depends ' . ' WHERE ' . DBcondition('triggerid_down', $triggerids); $db_deps = DBselect($sql); while ($db_dep = DBfetch($db_deps)) { if (!isset($deps[$db_dep['triggerid_down']])) { $deps[$db_dep['triggerid_down']] = array(); } $deps[$db_dep['triggerid_down']][$db_dep['triggerid_up']] = $db_dep['triggerid_up']; $depids[] = $db_dep['triggerid_up']; } $obj_params = array('triggerids' => $depids, 'output' => $options['select_dependencies'], 'expandData' => 1, 'preservekeys' => 1); $allowed = self::get($obj_params); //allowed triggerids foreach ($deps as $triggerid => $deptriggers) { foreach ($deptriggers as $num => $deptriggerid) { if (isset($allowed[$deptriggerid])) { $result[$triggerid]['dependencies'][] = $allowed[$deptriggerid]; } } } } // Adding groups if (!is_null($options['select_groups']) && str_in_array($options['select_groups'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_groups'], 'triggerids' => $triggerids, 'preservekeys' => 1); $groups = CHostgroup::get($obj_params); foreach ($groups as $groupid => $group) { $gtriggers = $group['triggers']; unset($group['triggers']); foreach ($gtriggers as $num => $trigger) { $result[$trigger['triggerid']]['groups'][] = $group; } } } // Adding hosts if (!is_null($options['select_hosts'])) { $obj_params = array('nodeids' => $nodeids, 'triggerids' => $triggerids, 'templated_hosts' => 1, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_hosts']) || str_in_array($options['select_hosts'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_hosts']; $hosts = CHost::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($hosts, 'host'); } foreach ($hosts as $hostid => $host) { unset($hosts[$hostid]['triggers']); $count = array(); foreach ($host['triggers'] as $tnum => $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['select_hosts']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $hosts = CHost::get($obj_params); $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['select_functions']) && str_in_array($options['select_functions'], $subselects_allowed_outputs)) { if ($options['select_functions'] == API_OUTPUT_EXTEND) { $sql_select = 'f.*'; } else { $sql_select = 'f.functionid, f.triggerid'; } $sql = 'SELECT ' . $sql_select . ' FROM functions f ' . ' WHERE ' . DBcondition('f.triggerid', $triggerids); $res = DBselect($sql); while ($function = DBfetch($res)) { $triggerid = $function['triggerid']; unset($function['triggerid']); $result[$triggerid]['functions'][] = $function; } } // Adding Items if (!is_null($options['select_items']) && str_in_array($options['select_items'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_items'], 'triggerids' => $triggerids, 'webitems' => 1, 'nopermissions' => 1, 'preservekeys' => 1); $items = CItem::get($obj_params); foreach ($items as $itemid => $item) { $itriggers = $item['triggers']; unset($item['triggers']); foreach ($itriggers as $num => $trigger) { $result[$trigger['triggerid']]['items'][] = $item; } } } // expandDescription if (!is_null($options['expandDescription'])) { // Function compare values {{{ foreach ($result as $tnum => $trigger) { preg_match_all('/\\$([1-9])/u', $trigger['description'], $numbers); preg_match_all('~{[0-9]+}[+\\-\\*/<>=#]?[\\(]*(?P<val>[+\\-0-9]+)[\\)]*~u', $trigger['expression'], $matches); foreach ($numbers[1] as $i) { $rep = isset($matches['val'][$i - 1]) ? $matches['val'][$i - 1] : ''; $result[$tnum]['description'] = str_replace('$' . $i, $rep, $result[$tnum]['description']); } } // }}} $functionids = array(); $triggers_to_expand_hosts = array(); $triggers_to_expand_items = array(); $triggers_to_expand_items2 = array(); foreach ($result as $tnum => $trigger) { preg_match_all('/{HOSTNAME([1-9]?)}/u', $trigger['description'], $hnums); if (!empty($hnums[1])) { preg_match_all('/{([0-9]+)}/u', $trigger['expression'], $funcs); $funcs = $funcs[1]; foreach ($hnums[1] as $fnum) { $fnum = $fnum ? $fnum : 1; if (isset($funcs[$fnum - 1])) { $functionid = $funcs[$fnum - 1]; $functionids[$functionid] = $functionid; $triggers_to_expand_hosts[$trigger['triggerid']][$functionid] = $fnum; } } } preg_match_all('/{ITEM.LASTVALUE([1-9]?)}/u', $trigger['description'], $inums); if (!empty($inums[1])) { preg_match_all('/{([0-9]+)}/u', $trigger['expression'], $funcs); $funcs = $funcs[1]; foreach ($inums[1] as $fnum) { $fnum = $fnum ? $fnum : 1; if (isset($funcs[$fnum - 1])) { $functionid = $funcs[$fnum - 1]; $functionids[$functionid] = $functionid; $triggers_to_expand_items[$trigger['triggerid']][$functionid] = $fnum; } } } preg_match_all('/{ITEM.VALUE([1-9]?)}/u', $trigger['description'], $inums); if (!empty($inums[1])) { preg_match_all('/{([0-9]+)}/u', $trigger['expression'], $funcs); $funcs = $funcs[1]; foreach ($inums[1] as $fnum) { $fnum = $fnum ? $fnum : 1; if (isset($funcs[$fnum - 1])) { $functionid = $funcs[$fnum - 1]; $functionids[$functionid] = $functionid; $triggers_to_expand_items2[$trigger['triggerid']][$functionid] = $fnum; } } } } if (!empty($functionids)) { $sql = 'SELECT DISTINCT f.triggerid, f.functionid, h.host, i.lastvalue' . ' FROM functions f,items i,hosts h' . ' WHERE f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND h.status<>' . HOST_STATUS_TEMPLATE . ' AND ' . DBcondition('f.functionid', $functionids); $db_funcs = DBselect($sql); while ($func = DBfetch($db_funcs)) { if (isset($triggers_to_expand_hosts[$func['triggerid']][$func['functionid']])) { $fnum = $triggers_to_expand_hosts[$func['triggerid']][$func['functionid']]; if ($fnum == 1) { $result[$func['triggerid']]['description'] = str_replace('{HOSTNAME}', $func['host'], $result[$func['triggerid']]['description']); } $result[$func['triggerid']]['description'] = str_replace('{HOSTNAME' . $fnum . '}', $func['host'], $result[$func['triggerid']]['description']); } if (isset($triggers_to_expand_items[$func['triggerid']][$func['functionid']])) { $fnum = $triggers_to_expand_items[$func['triggerid']][$func['functionid']]; if ($fnum == 1) { $result[$func['triggerid']]['description'] = str_replace('{ITEM.LASTVALUE}', $func['lastvalue'], $result[$func['triggerid']]['description']); } $result[$func['triggerid']]['description'] = str_replace('{ITEM.LASTVALUE' . $fnum . '}', $func['lastvalue'], $result[$func['triggerid']]['description']); } if (isset($triggers_to_expand_items2[$func['triggerid']][$func['functionid']])) { $fnum = $triggers_to_expand_items2[$func['triggerid']][$func['functionid']]; if ($fnum == 1) { $result[$func['triggerid']]['description'] = str_replace('{ITEM.VALUE}', $func['lastvalue'], $result[$func['triggerid']]['description']); } $result[$func['triggerid']]['description'] = str_replace('{ITEM.VALUE' . $fnum . '}', $func['lastvalue'], $result[$func['triggerid']]['description']); } } } foreach ($result as $tnum => $trigger) { if ($res = preg_match_all('/' . ZBX_PREG_EXPRESSION_USER_MACROS . '/', $trigger['description'], $arr)) { $macros = CUserMacro::getMacros($arr[1], array('triggerid' => $trigger['triggerid'])); $search = array_keys($macros); $values = array_values($macros); $result[$tnum]['description'] = str_replace($search, $values, $trigger['description']); } } } COpt::memoryPick(); // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
function zbx_db_search($table, $options, &$sql_parts) { list($table, $tableShort) = explode(' ', $table); $tableSchema = DB::getSchema($table); if (!$tableSchema) { info('Error in search request for table [' . $table . ']'); } $start = is_null($options['startSearch']) ? '%' : ''; $exclude = is_null($options['excludeSearch']) ? '' : ' NOT '; $search = array(); foreach ($options['search'] as $field => $pattern) { if (!isset($tableSchema['fields'][$field]) || zbx_empty($pattern)) { continue; } if ($tableSchema['fields'][$field]['type'] != DB::FIELD_TYPE_CHAR) { continue; } // escaping parameter that is about to be used in LIKE statement $pattern = str_replace("!", "!!", $pattern); $pattern = str_replace("%", "!%", $pattern); $pattern = str_replace("_", "!_", $pattern); $search[$field] = ' UPPER(' . $tableShort . '.' . $field . ') ' . $exclude . ' LIKE ' . zbx_dbstr($start . zbx_strtoupper($pattern) . '%') . " ESCAPE '!'"; } if (!empty($search)) { $sql_parts['where']['search'] = '( ' . implode(' OR ', $search) . ' )'; } }