function check_permission_for_action_conditions($conditions) { global $USER_DETAILS; if (USER_TYPE_SUPER_ADMIN == $USER_DETAILS['type']) { return true; } $groupids = array(); $hostids = array(); $triggerids = array(); foreach ($conditions as $ac_data) { if ($ac_data['operator'] != 0) { continue; } switch ($ac_data['type']) { case CONDITION_TYPE_HOST_GROUP: $groupids[$ac_data['value']] = $ac_data['value']; break; case CONDITION_TYPE_HOST: case CONDITION_TYPE_HOST_TEMPLATE: $hostids[$ac_data['value']] = $ac_data['value']; break; case CONDITION_TYPE_TRIGGER: $triggerids[$ac_data['value']] = $ac_data['value']; break; } } $options = array('groupids' => $groupids, 'editable' => 1); try { $groups = CHostgroup::get($options); $groups = zbx_toHash($groups, 'groupid'); foreach ($groupids as $hgnum => $groupid) { if (!isset($groups[$groupid])) { throw new Exception(S_INCORRECT_GROUP); } } $options = array('hostids' => $hostids, 'editable' => 1); $hosts = CHost::get($options); $hosts = zbx_toHash($hosts, 'hostid'); foreach ($hostids as $hnum => $hostid) { if (!isset($hosts[$hostid])) { throw new Exception(S_INCORRECT_HOST); } } $options = array('triggerids' => $triggerids, 'editable' => 1); $triggers = CTrigger::get($options); $triggers = zbx_toHash($triggers, 'triggerid'); foreach ($triggerids as $hnum => $triggerid) { if (!isset($triggers[$triggerid])) { throw new Exception(S_INCORRECT_TRIGGER); } } } catch (Exception $e) { // throw new Exception($e->getMessage()); // error($e->getMessage()); return false; } return true; }
function hostgroupIdents($groupids) { $idents = array(); $options = array('groupids' => $groupids, 'output' => API_OUTPUT_EXTEND, 'nodeids' => get_current_nodeid(true)); $groups = CHostgroup::get($options); foreach ($groups as $gnum => $group) { $idents[$group['groupid']] = array('node' => get_node_name_by_elid($group['groupid'], true), 'name' => $group['name']); } return $idents; }
/** * Get Host data * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param _array $options * @param array $options['nodeids'] Node IDs * @param array $options['groupids'] HostGroup IDs * @param array $options['hostids'] Host IDs * @param boolean $options['monitored_hosts'] only monitored Hosts * @param boolean $options['templated_hosts'] include templates in result * @param boolean $options['with_items'] only with items * @param boolean $options['with_monitored_items'] only with monitored items * @param boolean $options['with_historical_items'] only with historical items * @param boolean $options['with_triggers'] only with triggers * @param boolean $options['with_monitored_triggers'] only with monitored triggers * @param boolean $options['with_httptests'] only with http tests * @param boolean $options['with_monitored_httptests'] only with monitored http tests * @param boolean $options['with_graphs'] only with graphs * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins * @param int $options['extendoutput'] return all fields for Hosts * @param boolean $options['select_groups'] select HostGroups * @param boolean $options['select_templates'] select Templates * @param boolean $options['select_items'] select Items * @param boolean $options['select_triggers'] select Triggers * @param boolean $options['select_graphs'] select Graphs * @param boolean $options['select_applications'] select Applications * @param boolean $options['select_macros'] select Macros * @param boolean $options['select_profile'] select Profile * @param int $options['count'] count Hosts, returned column name is rowscount * @param string $options['pattern'] search hosts by pattern in Host name * @param string $options['extendPattern'] search hosts by pattern in Host name, ip and DNS * @param int $options['limit'] limit selection * @param string $options['sortfield'] field to sort by * @param string $options['sortorder'] sort order * @return array|boolean Host data as array or false if error */ public static function get($options = array()) { global $USER_DETAILS; $result = array(); $nodeCheck = false; $user_type = $USER_DETAILS['type']; $userid = $USER_DETAILS['userid']; $sort_columns = array('hostid', 'host', 'status', 'dns', 'ip'); // allowed columns for sorting $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM); // allowed output options for [ select_* ] params $sql_parts = array('select' => array('hosts' => 'h.hostid'), 'from' => array('hosts' => 'hosts h'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'proxyids' => null, 'templateids' => null, 'itemids' => null, 'triggerids' => null, 'maintenanceids' => null, 'graphids' => null, 'dhostids' => null, 'dserviceids' => null, 'monitored_hosts' => null, 'templated_hosts' => null, 'proxy_hosts' => null, 'with_items' => null, 'with_monitored_items' => null, 'with_historical_items' => null, 'with_triggers' => null, 'with_monitored_triggers' => null, 'with_httptests' => null, 'with_monitored_httptests' => null, 'with_graphs' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'selectParentTemplates' => null, 'select_items' => null, 'select_triggers' => null, 'select_graphs' => null, 'select_dhosts' => null, 'select_dservices' => null, 'select_applications' => null, 'select_macros' => null, 'select_profile' => 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_groups'])) { $options['select_groups'] = API_OUTPUT_EXTEND; } if (!is_null($options['selectParentTemplates'])) { $options['selectParentTemplates'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_items'])) { $options['select_items'] = 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; } if (!is_null($options['select_macros'])) { $options['select_macros'] = API_OUTPUT_EXTEND; } } if (is_array($options['output'])) { unset($sql_parts['select']['hosts']); $sql_parts['select']['hostid'] = ' h.hostid'; foreach ($options['output'] as $key => $field) { $sql_parts['select'][$field] = ' h.' . $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 hh.hostid ' . ' FROM hosts hh, hosts_groups hgg, rights r, users_groups ug ' . ' WHERE hh.hostid=h.hostid ' . ' AND hh.hostid=hgg.hostid ' . ' AND r.id=hgg.groupid ' . ' AND r.groupid=ug.usrgrpid ' . ' AND ug.userid=' . $userid . ' AND r.permission>=' . $permission . ' AND NOT EXISTS( ' . ' SELECT hggg.groupid ' . ' FROM hosts_groups hggg, rights rr, users_groups gg ' . ' WHERE hggg.hostid=hgg.hostid ' . ' AND rr.id=hggg.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(); // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $sql_parts['where']['hostid'] = DBcondition('h.hostid', $options['hostids']); if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('h.hostid', $nodeids); } } // 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']['hgh'] = 'hg.hostid=h.hostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['groupid'] = 'hg.groupid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('hg.groupid', $nodeids); } } // proxyids if (!is_null($options['proxyids'])) { zbx_value2array($options['proxyids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['proxy_hostid'] = 'h.proxy_hostid'; } $sql_parts['where'][] = DBcondition('h.proxy_hostid', $options['proxyids']); } // templateids if (!is_null($options['templateids'])) { zbx_value2array($options['templateids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['templateid'] = 'ht.templateid'; } $sql_parts['from']['hosts_templates'] = 'hosts_templates ht'; $sql_parts['where'][] = DBcondition('ht.templateid', $options['templateids']); $sql_parts['where']['hht'] = 'h.hostid=ht.hostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['templateid'] = 'ht.templateid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('ht.templateid', $nodeids); } } // itemids if (!is_null($options['itemids'])) { zbx_value2array($options['itemids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['itemid'] = 'i.itemid'; } $sql_parts['from']['items'] = 'items i'; $sql_parts['where'][] = DBcondition('i.itemid', $options['itemids']); $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('i.itemid', $nodeids); } } // 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['from']['items'] = 'items i'; $sql_parts['where'][] = DBcondition('f.triggerid', $options['triggerids']); $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; $sql_parts['where']['fi'] = 'f.itemid=i.itemid'; if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('f.triggerid', $nodeids); } } // 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['from']['items'] = 'items i'; $sql_parts['where'][] = DBcondition('gi.graphid', $options['graphids']); $sql_parts['where']['igi'] = 'i.itemid=gi.itemid'; $sql_parts['where']['hi'] = 'h.hostid=i.hostid'; if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('gi.graphid', $nodeids); } } // dhostids if (!is_null($options['dhostids'])) { zbx_value2array($options['dhostids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['dhostid'] = 'ds.dhostid'; } $sql_parts['from']['dservices'] = 'dservices ds'; $sql_parts['where'][] = DBcondition('ds.dhostid', $options['dhostids']); $sql_parts['where']['dsh'] = 'ds.ip=h.ip'; if (!is_null($options['groupCount'])) { $sql_parts['group']['dhostid'] = 'ds.dhostid'; } } // dserviceids if (!is_null($options['dserviceids'])) { zbx_value2array($options['dserviceids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['dserviceid'] = 'ds.dserviceid'; } $sql_parts['from']['dservices'] = 'dservices ds'; $sql_parts['where'][] = DBcondition('ds.dserviceid', $options['dserviceids']); $sql_parts['where']['dsh'] = 'ds.ip=h.ip'; if (!is_null($options['groupCount'])) { $sql_parts['group']['dserviceid'] = 'ds.dserviceid'; } } // maintenanceids if (!is_null($options['maintenanceids'])) { zbx_value2array($options['maintenanceids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['maintenanceid'] = 'mh.maintenanceid'; } $sql_parts['from']['maintenances_hosts'] = 'maintenances_hosts mh'; $sql_parts['where'][] = DBcondition('mh.maintenanceid', $options['maintenanceids']); $sql_parts['where']['hmh'] = 'h.hostid=mh.hostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['maintenanceid'] = 'mh.maintenanceid'; } } // node check !!!!! // should last, after all ****IDS checks if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('h.hostid', $nodeids); } // monitored_hosts, templated_hosts if (!is_null($options['monitored_hosts'])) { $sql_parts['where']['status'] = 'h.status=' . HOST_STATUS_MONITORED; } else { if (!is_null($options['templated_hosts'])) { $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ')'; } else { if (!is_null($options['proxy_hosts'])) { $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_PROXY_ACTIVE . ',' . HOST_STATUS_PROXY_PASSIVE . ')'; } else { $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')'; } } } // with_items, with_monitored_items, with_historical_items if (!is_null($options['with_items'])) { $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid )'; } else { if (!is_null($options['with_monitored_items'])) { $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND i.status=' . ITEM_STATUS_ACTIVE . ')'; } else { if (!is_null($options['with_historical_items'])) { $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND (i.status=' . ITEM_STATUS_ACTIVE . ' OR i.status=' . ITEM_STATUS_NOTSUPPORTED . ') AND i.lastvalue IS NOT NULL)'; } } } // with_triggers, with_monitored_triggers if (!is_null($options['with_triggers'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT i.itemid ' . ' FROM items i, functions f, triggers t ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid)'; } else { if (!is_null($options['with_monitored_triggers'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT i.itemid ' . ' FROM items i, functions f, triggers t ' . ' WHERE i.hostid=h.hostid ' . ' AND i.status=' . ITEM_STATUS_ACTIVE . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid ' . ' AND t.status=' . TRIGGER_STATUS_ENABLED . ')'; } } // with_httptests, with_monitored_httptests if (!is_null($options['with_httptests'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=h.hostid ' . ' AND ht.applicationid=a.applicationid)'; } else { if (!is_null($options['with_monitored_httptests'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=h.hostid ' . ' AND ht.applicationid=a.applicationid ' . ' AND ht.status=' . HTTPTEST_STATUS_ACTIVE . ')'; } } // with_graphs if (!is_null($options['with_graphs'])) { $sql_parts['where'][] = 'EXISTS( ' . ' SELECT DISTINCT i.itemid ' . ' FROM items i, graphs_items gi ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=gi.itemid)'; } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sql_parts['select']['hosts'] = 'h.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sql_parts['select'] = array('count(DISTINCT h.hostid) as rowscount'); //groupCount if (!is_null($options['groupCount'])) { foreach ($sql_parts['group'] as $key => $fields) { $sql_parts['select'][$key] = $fields; } } } // search if (is_array($options['search'])) { zbx_db_search('hosts h', $options, $sql_parts); } // filter if (is_array($options['filter'])) { zbx_db_filter('hosts h', $options, $sql_parts); } // 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'][$options['sortfield']] = 'h.' . $options['sortfield'] . ' ' . $sortorder; if (!str_in_array('h.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('h.*', $sql_parts['select'])) { $sql_parts['select'][$options['sortfield']] = 'h.' . $options['sortfield']; } } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sql_parts['limit'] = $options['limit']; } //------- $hostids = 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 .= 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 ' . $sql_where . $sql_group . $sql_order; //SDI($sql); $res = DBselect($sql, $sql_limit); while ($host = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $host; } else { $result = $host['rowscount']; } } else { $hostids[$host['hostid']] = $host['hostid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$host['hostid']] = array('hostid' => $host['hostid']); } else { if (!isset($result[$host['hostid']])) { $result[$host['hostid']] = array(); } if (!is_null($options['select_groups']) && !isset($result[$host['hostid']]['groups'])) { $result[$host['hostid']]['groups'] = array(); } if (!is_null($options['selectParentTemplates']) && !isset($result[$host['hostid']]['parentTemplates'])) { $result[$host['hostid']]['parentTemplates'] = array(); } if (!is_null($options['select_items']) && !isset($result[$host['hostid']]['items'])) { $result[$host['hostid']]['items'] = array(); } if (!is_null($options['select_profile']) && !isset($result[$host['hostid']]['profile'])) { $result[$host['hostid']]['profile'] = array(); $result[$host['hostid']]['profile_ext'] = array(); } if (!is_null($options['select_triggers']) && !isset($result[$host['hostid']]['triggers'])) { $result[$host['hostid']]['triggers'] = array(); } if (!is_null($options['select_graphs']) && !isset($result[$host['hostid']]['graphs'])) { $result[$host['hostid']]['graphs'] = array(); } if (!is_null($options['select_dhosts']) && !isset($result[$host['hostid']]['dhosts'])) { $result[$host['hostid']]['dhosts'] = array(); } if (!is_null($options['select_dservices']) && !isset($result[$host['hostid']]['dservices'])) { $result[$host['hostid']]['dservices'] = array(); } if (!is_null($options['select_applications']) && !isset($result[$host['hostid']]['applications'])) { $result[$host['hostid']]['applications'] = array(); } if (!is_null($options['select_macros']) && !isset($result[$host['hostid']]['macros'])) { $result[$host['hostid']]['macros'] = array(); } // if(!is_null($options['select_maintenances']) && !isset($result[$host['hostid']]['maintenances'])){ // $result[$host['hostid']]['maintenances'] = array(); // } // groupids if (isset($host['groupid']) && is_null($options['select_groups'])) { if (!isset($result[$host['hostid']]['groups'])) { $result[$host['hostid']]['groups'] = array(); } $result[$host['hostid']]['groups'][] = array('groupid' => $host['groupid']); unset($host['groupid']); } // templateids if (isset($host['templateid'])) { if (!isset($result[$host['hostid']]['templates'])) { $result[$host['hostid']]['templates'] = array(); } $result[$host['hostid']]['templates'][] = array('templateid' => $host['templateid']); unset($host['templateid']); } // triggerids if (isset($host['triggerid']) && is_null($options['select_triggers'])) { if (!isset($result[$host['hostid']]['triggers'])) { $result[$host['hostid']]['triggers'] = array(); } $result[$host['hostid']]['triggers'][] = array('triggerid' => $host['triggerid']); unset($host['triggerid']); } // itemids if (isset($host['itemid']) && is_null($options['select_items'])) { if (!isset($result[$host['hostid']]['items'])) { $result[$host['hostid']]['items'] = array(); } $result[$host['hostid']]['items'][] = array('itemid' => $host['itemid']); unset($host['itemid']); } // graphids if (isset($host['graphid']) && is_null($options['select_graphs'])) { if (!isset($result[$host['hostid']]['graphs'])) { $result[$host['hostid']]['graphs'] = array(); } $result[$host['hostid']]['graphs'][] = array('graphid' => $host['graphid']); unset($host['graphid']); } // dhostids if (isset($host['dhostid']) && is_null($options['select_dhosts'])) { if (!isset($result[$host['hostid']]['dhosts'])) { $result[$host['hostid']]['dhosts'] = array(); } $result[$host['hostid']]['dhosts'][] = array('dhostid' => $host['dhostid']); unset($host['dhostid']); } // dserviceids if (isset($host['dserviceid']) && is_null($options['select_dservices'])) { if (!isset($result[$host['hostid']]['dservices'])) { $result[$host['hostid']]['dservices'] = array(); } $result[$host['hostid']]['dservices'][] = array('dserviceid' => $host['dserviceid']); unset($host['dserviceid']); } // maintenanceids if (isset($host['maintenanceid'])) { if (!isset($result[$host['hostid']]['maintenanceid'])) { $result[$host['hostid']]['maintenances'] = array(); } $result[$host['hostid']]['maintenances'][] = array('maintenanceid' => $host['maintenanceid']); // unset($host['maintenanceid']); } //--- $result[$host['hostid']] += $host; } } } Copt::memoryPick(); if (!is_null($options['countOutput'])) { if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; } // Adding Objects // 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'], 'hostids' => $hostids, 'preservekeys' => 1); $groups = CHostgroup::get($obj_params); foreach ($groups as $groupid => $group) { $ghosts = $group['hosts']; unset($group['hosts']); foreach ($ghosts as $num => $host) { $result[$host['hostid']]['groups'][] = $group; } } } // Adding Profiles if (!is_null($options['select_profile'])) { $sql = 'SELECT hp.* ' . ' FROM hosts_profiles hp ' . ' WHERE ' . DBcondition('hp.hostid', $hostids); $db_profile = DBselect($sql); while ($profile = DBfetch($db_profile)) { $result[$profile['hostid']]['profile'] = $profile; } $sql = 'SELECT hpe.* ' . ' FROM hosts_profiles_ext hpe ' . ' WHERE ' . DBcondition('hpe.hostid', $hostids); $db_profile_ext = DBselect($sql); while ($profile_ext = DBfetch($db_profile_ext)) { $result[$profile_ext['hostid']]['profile_ext'] = $profile_ext; } } // Adding Templates if (!is_null($options['selectParentTemplates'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'preservekeys' => 1); if (is_array($options['selectParentTemplates']) || str_in_array($options['selectParentTemplates'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['selectParentTemplates']; $templates = CTemplate::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($templates, 'host'); } foreach ($templates as $templateid => $template) { unset($templates[$templateid]['hosts']); $count = array(); foreach ($template['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['parentTemplates'][] =& $templates[$templateid]; } } } else { if (API_OUTPUT_COUNT == $options['selectParentTemplates']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $templates = CTemplate::get($obj_params); $templates = zbx_toHash($templates, 'hostid'); foreach ($result as $hostid => $host) { if (isset($templates[$hostid])) { $result[$hostid]['templates'] = $templates[$hostid]['rowscount']; } else { $result[$hostid]['templates'] = 0; } } } } } // Adding Items if (!is_null($options['select_items'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_items']) || str_in_array($options['select_items'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_items']; $items = CItem::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($items, 'description'); } foreach ($items as $itemid => $item) { unset($items[$itemid]['hosts']); foreach ($item['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['items'][] =& $items[$itemid]; } } } else { if (API_OUTPUT_COUNT == $options['select_items']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $items = CItem::get($obj_params); $items = zbx_toHash($items, 'hostid'); foreach ($result as $hostid => $host) { if (isset($items[$hostid])) { $result[$hostid]['items'] = $items[$hostid]['rowscount']; } else { $result[$hostid]['items'] = 0; } } } } } // Adding triggers if (!is_null($options['select_triggers'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_triggers']) || str_in_array($options['select_triggers'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_triggers']; $triggers = CTrigger::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($triggers, 'description'); } foreach ($triggers as $triggerid => $trigger) { unset($triggers[$triggerid]['hosts']); foreach ($trigger['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['triggers'][] =& $triggers[$triggerid]; } } } else { if (API_OUTPUT_COUNT == $options['select_triggers']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $triggers = CTrigger::get($obj_params); $triggers = zbx_toHash($triggers, 'hostid'); foreach ($result as $hostid => $host) { if (isset($triggers[$hostid])) { $result[$hostid]['triggers'] = $triggers[$hostid]['rowscount']; } else { $result[$hostid]['triggers'] = 0; } } } } } // Adding graphs if (!is_null($options['select_graphs'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_graphs']) || str_in_array($options['select_graphs'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_graphs']; $graphs = CGraph::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($graphs, 'name'); } foreach ($graphs as $graphid => $graph) { unset($graphs[$graphid]['hosts']); foreach ($graph['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['graphs'][] =& $graphs[$graphid]; } } } else { if (API_OUTPUT_COUNT == $options['select_graphs']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $graphs = CGraph::get($obj_params); $graphs = zbx_toHash($graphs, 'hostid'); foreach ($result as $hostid => $host) { if (isset($graphs[$hostid])) { $result[$hostid]['graphs'] = $graphs[$hostid]['rowscount']; } else { $result[$hostid]['graphs'] = 0; } } } } } // Adding discovery hosts if (!is_null($options['select_dhosts'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_dhosts']) || str_in_array($options['select_dhosts'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_dhosts']; $dhosts = CDHost::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($dhosts, 'dhostid'); } foreach ($dhosts as $dhostid => $dhost) { unset($dhosts[$dhostid]['hosts']); foreach ($dhost['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['dhosts'][] =& $dhosts[$dhostid]; } } } else { if (API_OUTPUT_COUNT == $options['select_dhosts']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $dhosts = CDHost::get($obj_params); $dhosts = zbx_toHash($dhosts, 'hostid'); foreach ($result as $hostid => $host) { if (isset($dhosts[$hostid])) { $result[$hostid]['dhosts'] = $dhosts[$hostid]['rowscount']; } else { $result[$hostid]['dhosts'] = 0; } } } } } // Adding applications if (!is_null($options['select_applications'])) { $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1); if (is_array($options['select_applications']) || str_in_array($options['select_applications'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['select_applications']; $applications = CApplication::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($applications, 'name'); } foreach ($applications as $applicationid => $application) { unset($applications[$applicationid]['hosts']); foreach ($application['hosts'] as $hnum => $host) { if (!is_null($options['limitSelects'])) { if (!isset($count[$host['hostid']])) { $count[$host['hostid']] = 0; } $count[$host['hostid']]++; if ($count[$host['hostid']] > $options['limitSelects']) { continue; } } $result[$host['hostid']]['applications'][] =& $applications[$applicationid]; } } } else { if (API_OUTPUT_COUNT == $options['select_applications']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $applications = CApplication::get($obj_params); $applications = zbx_toHash($applications, 'hostid'); foreach ($result as $hostid => $host) { if (isset($applications[$hostid])) { $result[$hostid]['applications'] = $applications[$hostid]['rowscount']; } else { $result[$hostid]['applications'] = 0; } } } } } // Adding macros if (!is_null($options['select_macros']) && str_in_array($options['select_macros'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_macros'], 'hostids' => $hostids, 'preservekeys' => 1); $macros = CUserMacro::get($obj_params); foreach ($macros as $macroid => $macro) { $mhosts = $macro['hosts']; unset($macro['hosts']); foreach ($mhosts as $num => $host) { $result[$host['hostid']]['macros'][] = $macro; } } } Copt::memoryPick(); // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
function get_screen($screenid, $editmode, $effectiveperiod = NULL) { global $USER_DETAILS; if ($screenid == 0) { return new CTableInfo(S_NO_SCREENS_DEFINED); } $r = CScreen::get(array('screenids' => $screenid, 'editable' => $editmode == 1 ? 1 : null, 'output' => API_OUTPUT_SHORTEN)); if (empty($r)) { access_deny(); } if (is_null($effectiveperiod)) { $effectiveperiod = ZBX_MIN_PERIOD; } $result = DBselect('SELECT name,hsize,vsize FROM screens WHERE screenid=' . $screenid); $row = DBfetch($result); if (!$row) { return new CTableInfo(S_NO_SCREENS_DEFINED); } $sql = 'SELECT * FROM screens_items WHERE screenid=' . $screenid; $iresult = DBSelect($sql); $skip_field = array(); $irows = array(); while ($irow = DBfetch($iresult)) { $irows[] = $irow; for ($i = 0; $i < $irow['rowspan'] || $i == 0; $i++) { for ($j = 0; $j < $irow['colspan'] || $j == 0; $j++) { if ($i != 0 || $j != 0) { if (!isset($skip_field[$irow['y'] + $i])) { $skip_field[$irow['y'] + $i] = array(); } $skip_field[$irow['y'] + $i][$irow['x'] + $j] = 1; } } } } $table = new CTable(new CLink(S_NO_ROWS_IN_SCREEN . SPACE . $row['name'], 'screenconf.php?config=0&form=update&screenid=' . $screenid), $editmode == 0 || $editmode == 2 ? 'screen_view' : 'screen_edit'); $table->setAttribute('id', 'iframe'); if ($editmode == 1) { $new_cols = array(new Ccol(new Cimg('images/general/zero.gif', 'zero', 1, 1))); for ($c = 0; $c < $row['hsize'] + 1; $c++) { $add_icon = new Cimg('images/general/closed.gif', NULL, NULL, NULL, 'pointer'); $add_icon->addAction('onclick', "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&add_col={$c}';"); array_push($new_cols, new Ccol($add_icon)); } $table->addRow($new_cols); } $empty_screen_col = array(); for ($r = 0; $r < $row['vsize']; $r++) { $new_cols = array(); $empty_screen_row = true; if ($editmode == 1) { $add_icon = new Cimg('images/general/closed.gif', NULL, NULL, NULL, 'pointer'); $add_icon->addAction('onclick', "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&add_row={$r}';"); array_push($new_cols, new Ccol($add_icon)); } for ($c = 0; $c < $row['hsize']; $c++) { $item = array(); if (isset($skip_field[$r][$c])) { continue; } $item_form = false; $irow = false; foreach ($irows as $tmprow) { if ($tmprow['x'] == $c && $tmprow['y'] == $r) { $irow = $tmprow; break; } } if ($irow) { $screenitemid = $irow['screenitemid']; $resourcetype = $irow['resourcetype']; $resourceid = $irow['resourceid']; $width = $irow['width']; $height = $irow['height']; $colspan = $irow['colspan']; $rowspan = $irow['rowspan']; $elements = $irow['elements']; $valign = $irow['valign']; $halign = $irow['halign']; $style = $irow['style']; $url = $irow['url']; $dynamic = $irow['dynamic']; } else { $screenitemid = 0; $resourcetype = 0; $resourceid = 0; $width = 0; $height = 0; $colspan = 0; $rowspan = 0; $elements = 0; $valign = VALIGN_DEFAULT; $halign = HALIGN_DEFAULT; $style = 0; $url = ''; $dynamic = 0; } if ($screenitemid > 0) { $empty_screen_row = false; $empty_screen_col[$c] = 1; } if ($editmode == 1 && $screenitemid != 0) { $onclick_action = "ZBX_SCREENS['" . $_REQUEST['screenid'] . "'].screen.element_onclick('screenedit.php?form=update" . url_param('screenid') . '&screenitemid=' . $screenitemid . "#form');"; $action = 'screenedit.php?form=update' . url_param('screenid') . '&screenitemid=' . $screenitemid . '#form'; } else { if ($editmode == 1 && $screenitemid == 0) { $onclick_action = "ZBX_SCREENS['" . $_REQUEST['screenid'] . "'].screen.element_onclick('screenedit.php?form=update" . url_param('screenid') . '&x=' . $c . '&y=' . $r . "#form');"; $action = 'screenedit.php?form=update' . url_param('screenid') . '&x=' . $c . '&y=' . $r . '#form'; } else { $action = NULL; } } if ($editmode == 1 && isset($_REQUEST['form']) && isset($_REQUEST['x']) && $_REQUEST['x'] == $c && isset($_REQUEST['y']) && $_REQUEST['y'] == $r) { // click on empty field $item = get_screen_item_form(); $item_form = true; } else { if ($editmode == 1 && isset($_REQUEST['form']) && isset($_REQUEST['screenitemid']) && bccomp($_REQUEST['screenitemid'], $screenitemid) == 0) { // click on element $item = get_screen_item_form(); $item_form = true; } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_GRAPH) { if ($editmode == 0) { $action = 'charts.php?graphid=' . $resourceid . url_param('period') . url_param('stime'); } // GRAPH & ZOOM features $dom_graph_id = 'graph_' . $screenitemid . '_' . $resourceid; $containerid = 'graph_cont_' . $screenitemid . '_' . $resourceid; $graphDims = getGraphDims($resourceid); $graphDims['graphHeight'] = $height; $graphDims['width'] = $width; $graph = get_graph_by_graphid($resourceid); $graphid = $graph['graphid']; $legend = $graph['show_legend']; $graph3d = $graph['show_3d']; //------------- // Host feature if ($dynamic == SCREEN_DYNAMIC_ITEM && isset($_REQUEST['hostid']) && $_REQUEST['hostid'] > 0) { $options = array('hostids' => $_REQUEST['hostid'], 'output' => array('hostid', 'host')); $hosts = CHost::get($options); $host = reset($hosts); $options = array('graphids' => $resourceid, 'output' => API_OUTPUT_EXTEND, 'select_hosts' => API_OUTPUT_REFER, 'select_graph_items' => API_OUTPUT_EXTEND); $graph = CGraph::get($options); $graph = reset($graph); if (count($graph['hosts']) == 1) { // if items from one host we change them, or set calculated if not exist on that host if ($graph['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymax_itemid']) { $new_dinamic = get_same_graphitems_for_host(array(array('itemid' => $graph['ymax_itemid'])), $_REQUEST['hostid'], false); $new_dinamic = reset($new_dinamic); if (isset($new_dinamic['itemid']) && $new_dinamic['itemid'] > 0) { $graph['ymax_itemid'] = $new_dinamic['itemid']; } else { $graph['ymax_type'] = GRAPH_YAXIS_TYPE_CALCULATED; } } if ($graph['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymin_itemid']) { $new_dinamic = get_same_graphitems_for_host(array(array('itemid' => $graph['ymin_itemid'])), $_REQUEST['hostid'], false); $new_dinamic = reset($new_dinamic); if (isset($new_dinamic['itemid']) && $new_dinamic['itemid'] > 0) { $graph['ymin_itemid'] = $new_dinamic['itemid']; } else { $graph['ymin_type'] = GRAPH_YAXIS_TYPE_CALCULATED; } } } $url = $graph['graphtype'] == GRAPH_TYPE_PIE || $graph['graphtype'] == GRAPH_TYPE_EXPLODED ? 'chart7.php' : 'chart3.php'; $url = new Curl($url); foreach ($graph as $name => $value) { if ($name == 'width' || $name == 'height') { continue; } $url->setArgument($name, $value); } $new_items = get_same_graphitems_for_host($graph['gitems'], $_REQUEST['hostid'], false); foreach ($new_items as $gitem) { unset($gitem['gitemid']); unset($gitem['graphid']); foreach ($gitem as $name => $value) { $url->setArgument('items[' . $gitem['itemid'] . '][' . $name . ']', $value); } } $url->setArgument('name', $host['host'] . ': ' . $graph['name']); $url = $url->getUrl(); } //------------- $objData = array('id' => $resourceid, 'domid' => $dom_graph_id, 'containerid' => $containerid, 'objDims' => $graphDims, 'loadSBox' => 0, 'loadImage' => 1, 'loadScroll' => 0, 'dynamic' => 0); $default = false; if ($graphDims['graphtype'] == GRAPH_TYPE_PIE || $graphDims['graphtype'] == GRAPH_TYPE_EXPLODED) { if ($dynamic == SCREEN_SIMPLE_ITEM || empty($url)) { $url = 'chart6.php?graphid=' . $resourceid; $default = true; } $timeline = array(); $timeline['period'] = $effectiveperiod; $timeline['starttime'] = date('YmdHis', get_min_itemclock_by_graphid($resourceid)); if (isset($_REQUEST['stime'])) { $timeline['usertime'] = date('YmdHis', zbxDateToTime($_REQUEST['stime']) + $timeline['period']); } // $src = $url.'&width='.$width.'&height='.$height.'&legend='.$legend.'&graph3d='.$graph3d; $src = $url . '&width=' . $width . '&height=' . $height . '&legend=' . $legend . '&graph3d=' . $graph3d . '&period=' . $effectiveperiod . url_param('stime'); $objData['src'] = $src; } else { if ($dynamic == SCREEN_SIMPLE_ITEM || empty($url)) { $url = 'chart2.php?graphid=' . $resourceid; $default = true; } $src = $url . '&width=' . $width . '&height=' . $height . '&period=' . $effectiveperiod . url_param('stime'); $timeline = array(); if (isset($graphid) && !is_null($graphid) && $editmode != 1) { $timeline['period'] = $effectiveperiod; $timeline['starttime'] = date('YmdHis', time() - ZBX_MAX_PERIOD); //get_min_itemclock_by_graphid($graphid); if (isset($_REQUEST['stime'])) { $timeline['usertime'] = date('YmdHis', zbxDateToTime($_REQUEST['stime']) + $timeline['period']); } $objData['loadSBox'] = 1; } $objData['src'] = $src; } if ($editmode || !$default) { $item = new CDiv(); } else { $item = new CLink(null, $action); } $item->setAttribute('id', $containerid); $item = array($item); if ($editmode == 1) { $item[] = BR(); $item[] = new CLink(S_CHANGE, $action); } if ($editmode == 2) { insert_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');'); } else { zbx_add_post_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');'); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SIMPLE_GRAPH) { $dom_graph_id = 'graph_' . $screenitemid . '_' . $resourceid; $containerid = 'graph_cont_' . $screenitemid . '_' . $resourceid; $graphDims = getGraphDims(); $graphDims['graphHeight'] = $height; $graphDims['width'] = $width; $objData = array('id' => $resourceid, 'domid' => $dom_graph_id, 'containerid' => $containerid, 'objDims' => $graphDims, 'loadSBox' => 0, 'loadImage' => 1, 'loadScroll' => 0, 'dynamic' => 0); // Host feature if ($dynamic == SCREEN_DYNAMIC_ITEM && isset($_REQUEST['hostid']) && $_REQUEST['hostid'] > 0) { if ($newitemid = get_same_item_for_host($resourceid, $_REQUEST['hostid'])) { $resourceid = $newitemid; } else { $resourceid = ''; } } //------------- if ($editmode == 0 && !empty($resourceid)) { $action = 'history.php?action=showgraph&itemid=' . $resourceid . url_param('period') . url_param('stime'); } $timeline = array(); $timeline['starttime'] = date('YmdHis', time() - ZBX_MAX_PERIOD); if (!zbx_empty($resourceid) && $editmode != 1) { $timeline['period'] = $effectiveperiod; if (isset($_REQUEST['stime'])) { $timeline['usertime'] = date('YmdHis', zbxDateToTime($_REQUEST['stime']) + $timeline['period']); } $objData['loadSBox'] = 1; } $src = zbx_empty($resourceid) ? 'chart3.php?' : 'chart.php?itemid=' . $resourceid . '&'; $src .= $url . 'width=' . $width . '&height=' . $height; $objData['src'] = $src; if ($editmode) { $item = new CDiv(); } else { $item = new CLink(null, $action); } $item->setAttribute('id', $containerid); $item = array($item); if ($editmode == 1) { $item[] = BR(); $item[] = new CLink(S_CHANGE, $action); } if ($editmode == 2) { insert_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');'); } else { zbx_add_post_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');'); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_MAP) { $image_map = new CImg("map.php?noedit=1&sysmapid={$resourceid}" . "&width={$width}&height={$height}&curtime=" . time()); if ($editmode == 0) { $options = array('sysmapids' => $resourceid, 'output' => API_OUTPUT_EXTEND, 'select_selements' => API_OUTPUT_EXTEND, 'nopermissions' => 1); $sysmaps = CMap::get($options); $sysmap = reset($sysmaps); $action_map = getActionMapBySysmap($sysmap); $image_map->setMap($action_map->getName()); $item = array($action_map, $image_map); } else { $item = $image_map; // $item = new CLink($image_map, $action); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_PLAIN_TEXT) { // Host feature if ($dynamic == SCREEN_DYNAMIC_ITEM && isset($_REQUEST['hostid']) && $_REQUEST['hostid'] > 0) { if ($newitemid = get_same_item_for_host($resourceid, $_REQUEST['hostid'])) { $resourceid = $newitemid; } else { $resourceid = 0; } } //------------- $item = array(get_screen_plaintext($resourceid, $elements, $style)); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_HOSTGROUP_TRIGGERS) { $params = array('groupids' => null, 'hostids' => null, 'maintenance' => null, 'severity' => null, 'limit' => $elements); $tr_form = S_ALL_S; if ($resourceid > 0) { $options = array('groupids' => $resourceid, 'output' => API_OUTPUT_EXTEND); $hostgroups = CHostgroup::get($options); $hostgroup = reset($hostgroups); $tr_form = new CSpan(S_GROUP . ': ' . $hostgroup['name'], 'white'); $params['groupids'] = $hostgroup['groupid']; } else { $groupid = get_request('tr_groupid', CProfile::get('web.screens.tr_groupid', 0)); $hostid = get_request('tr_hostid', CProfile::get('web.screens.tr_hostid', 0)); CProfile::update('web.screens.tr_groupid', $groupid, PROFILE_TYPE_ID); CProfile::update('web.screens.tr_hostid', $hostid, PROFILE_TYPE_ID); $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND); $groups = CHostGroup::get($options); order_result($groups, 'name'); $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND); if ($groupid > 0) { $options['groupids'] = $groupid; } $hosts = CHost::get($options); $hosts = zbx_toHash($hosts, 'hostid'); order_result($hosts, 'host'); if (!isset($hosts[$hostid])) { $hostid = 0; } $tr_form = new CForm(); $cmbGroup = new CComboBox('tr_groupid', $groupid, 'submit()'); $cmbHosts = new CComboBox('tr_hostid', $hostid, 'submit()'); $cmbGroup->addItem(0, S_ALL_SMALL); $cmbHosts->addItem(0, S_ALL_SMALL); foreach ($groups as $gnum => $group) { $cmbGroup->addItem($group['groupid'], get_node_name_by_elid($group['groupid'], null, ': ') . $group['name']); } foreach ($hosts as $hnum => $host) { $cmbHosts->addItem($host['hostid'], get_node_name_by_elid($host['hostid'], null, ': ') . $host['host']); } $tr_form->addItem(array(S_GROUP . SPACE, $cmbGroup)); $tr_form->addItem(array(SPACE . S_HOST . SPACE, $cmbHosts)); if ($groupid > 0) { $params['groupids'] = $groupid; } if ($hostid > 0) { $params['hostids'] = $hostid; } } $item = array(get_table_header(array(S_STATUS_OF_TRIGGERS_BIG, SPACE, zbx_date2str(S_SCREENS_TRIGGER_FORM_DATE_FORMAT)), $tr_form)); $item[] = make_latest_issues($params); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } ///----------------------- } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_HOST_TRIGGERS) { $params = array('groupids' => null, 'hostids' => null, 'maintenance' => null, 'severity' => null, 'limit' => $elements); $tr_form = S_ALL_S; if ($resourceid > 0) { $options = array('hostids' => $resourceid, 'output' => API_OUTPUT_EXTEND); $hosts = CHost::get($options); $host = reset($hosts); $tr_form = new CSpan(S_HOST . ': ' . $host['host'], 'white'); $params['hostids'] = $host['hostid']; } else { $groupid = get_request('tr_groupid', CProfile::get('web.screens.tr_groupid', 0)); $hostid = get_request('tr_hostid', CProfile::get('web.screens.tr_hostid', 0)); CProfile::update('web.screens.tr_groupid', $groupid, PROFILE_TYPE_ID); CProfile::update('web.screens.tr_hostid', $hostid, PROFILE_TYPE_ID); $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND); $groups = CHostGroup::get($options); order_result($groups, 'name'); $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND); if ($groupid > 0) { $options['groupids'] = $groupid; } $hosts = CHost::get($options); $hosts = zbx_toHash($hosts, 'hostid'); order_result($hosts, 'host'); if (!isset($hosts[$hostid])) { $hostid = 0; } $tr_form = new CForm(); $cmbGroup = new CComboBox('tr_groupid', $groupid, 'submit()'); $cmbHosts = new CComboBox('tr_hostid', $hostid, 'submit()'); $cmbGroup->addItem(0, S_ALL_SMALL); $cmbHosts->addItem(0, S_ALL_SMALL); foreach ($groups as $gnum => $group) { $cmbGroup->addItem($group['groupid'], get_node_name_by_elid($group['groupid'], null, ': ') . $group['name']); } foreach ($hosts as $hnum => $host) { $cmbHosts->addItem($host['hostid'], get_node_name_by_elid($host['hostid'], null, ': ') . $host['host']); } $tr_form->addItem(array(S_GROUP . SPACE, $cmbGroup)); $tr_form->addItem(array(SPACE . S_HOST . SPACE, $cmbHosts)); if ($groupid > 0) { $params['groupids'] = $groupid; } if ($hostid > 0) { $params['hostids'] = $hostid; } } ///----------------------- $item = array(get_table_header(array(S_STATUS_OF_TRIGGERS_BIG, SPACE, zbx_date2str(S_SCREENS_TRIGGER_FORM_DATE_FORMAT)), $tr_form)); $item[] = make_latest_issues($params); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SYSTEM_STATUS) { $params = array('groupids' => null, 'hostids' => null, 'maintenance' => null, 'severity' => null, 'limit' => null, 'extAck' => 0); $item = array(get_table_header(array(S_SYSTEM_STATUS, SPACE, zbx_date2str(S_SCREENS_TRIGGER_FORM_DATE_FORMAT)))); $item[] = make_system_status($params); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_HOSTS_INFO) { $item = array(new CHostsInfo($resourceid, $style)); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_TRIGGERS_INFO) { $item = new CTriggersInfo($resourceid, null, $style); $item = array($item); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SERVER_INFO) { // $item = array(get_table_header(S_STATUS_OF_ZABBIX_BIG),make_status_of_zbx()); $item = array(new CServerInfo()); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_CLOCK) { $item = new CFlashClock($width, $height, $style, $action); } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SCREEN) { $item = array(get_screen($resourceid, 2, $effectiveperiod)); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_TRIGGERS_OVERVIEW) { $hostids = array(); $res = DBselect('SELECT DISTINCT hg.hostid FROM hosts_groups hg WHERE hg.groupid=' . $resourceid); while ($tmp_host = DBfetch($res)) { $hostids[$tmp_host['hostid']] = $tmp_host['hostid']; } $item = array(get_triggers_overview($hostids, $style)); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_DATA_OVERVIEW) { $hostids = array(); $res = DBselect('SELECT DISTINCT hg.hostid FROM hosts_groups hg WHERE hg.groupid=' . $resourceid); while ($tmp_host = DBfetch($res)) { $hostids[$tmp_host['hostid']] = $tmp_host['hostid']; } $item = array(get_items_data_overview($hostids, $style)); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_URL) { $item = array(new CIFrame($url, $width, $height, "auto")); if ($editmode == 1) { array_push($item, BR(), new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_ACTIONS) { $item = array(get_history_of_actions($elements)); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_EVENTS) { $options = array('monitored' => 1, 'value' => array(TRIGGER_VALUE_TRUE, TRIGGER_VALUE_FALSE), 'limit' => $elements); $hide_unknown = CProfile::get('web.events.filter.hide_unknown', 0); if ($hide_unknown) { $options['value'] = array(TRIGGER_VALUE_TRUE, TRIGGER_VALUE_FALSE); } $item = new CTableInfo(S_NO_EVENTS_FOUND); $item->SetHeader(array(S_TIME, is_show_all_nodes() ? S_NODE : null, S_HOST, S_DESCRIPTION, S_VALUE, S_SEVERITY)); $events = getLastEvents($options); foreach ($events as $enum => $event) { $trigger = $event['trigger']; $host = $event['host']; $value = new CCol(trigger_value2str($event['value']), get_trigger_value_style($event['value'])); // $row = zbx_array_merge($triggers[$row['triggerid']],$row); // if((1 == $hide_unknown) && (!event_initial_time($row,$hide_unknown))) continue; $item->addRow(array(zbx_date2str(S_EVENTS_TRIGGERS_EVENTS_HISTORY_LIST_DATE_FORMAT, $event['clock']), get_node_name_by_elid($event['objectid']), $host['host'], new CLink($trigger['description'], 'tr_events.php?triggerid=' . $event['objectid'] . '&eventid=' . $event['eventid']), $value, new CCol(get_severity_description($trigger['priority']), get_severity_style($trigger['priority'])))); } $item = array($item); if ($editmode == 1) { array_push($item, new CLink(S_CHANGE, $action)); } } else { $item = array(SPACE); if ($editmode == 1) { array_push($item, BR(), new CLink(S_CHANGE, $action)); } } } } } } } } } } } } } } } } } } } } $str_halign = 'def'; if ($halign == HALIGN_CENTER) { $str_halign = 'cntr'; } if ($halign == HALIGN_LEFT) { $str_halign = 'left'; } if ($halign == HALIGN_RIGHT) { $str_halign = 'right'; } $str_valign = 'def'; if ($valign == VALIGN_MIDDLE) { $str_valign = 'mdl'; } if ($valign == VALIGN_TOP) { $str_valign = 'top'; } if ($valign == VALIGN_BOTTOM) { $str_valign = 'bttm'; } if ($editmode == 1 && !$item_form) { $item = new CDiv($item, 'draggable'); $item->setAttribute('id', 'position_' . $r . '_' . $c); if ($editmode == 1) { $item->setAttribute('onclick', 'javascript: ' . $onclick_action); } } $new_col = new CCol($item, $str_halign . '_' . $str_valign); if ($colspan) { $new_col->SetColSpan($colspan); } if ($rowspan) { $new_col->SetRowSpan($rowspan); } array_push($new_cols, $new_col); } if ($editmode == 1) { $rmv_icon = new Cimg('images/general/opened.gif', NULL, NULL, NULL, 'pointer'); if ($empty_screen_row) { $rmv_row_link = "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_row={$r}';"; } else { $rmv_row_link = "javascript: if(Confirm('" . S_THIS_SCREEN_ROW_NOT_EMPTY . '. ' . S_DELETE_IT_Q . "')){" . " location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_row={$r}';}"; } $rmv_icon->addAction('onclick', $rmv_row_link); array_push($new_cols, new Ccol($rmv_icon)); } $table->addRow(new CRow($new_cols)); } if ($editmode == 1) { $add_icon = new Cimg('images/general/closed.gif', NULL, NULL, NULL, 'pointer'); $add_icon->addAction('onclick', "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&add_row={$row['vsize']}';"); $new_cols = array(new Ccol($add_icon)); for ($c = 0; $c < $row['hsize']; $c++) { $rmv_icon = new Cimg('images/general/opened.gif', NULL, NULL, NULL, 'pointer'); if (isset($empty_screen_col[$c])) { $rmv_col_link = "javascript: if(Confirm('" . S_THIS_SCREEN_COLUMN_NOT_EMPTY . '. ' . S_DELETE_IT_Q . "')){" . " location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_col={$c}';}"; } else { $rmv_col_link = "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_col={$c}';"; } $rmv_icon->addAction('onclick', $rmv_col_link); array_push($new_cols, new Ccol($rmv_icon)); } array_push($new_cols, new Ccol(new Cimg('images/general/zero.gif', 'zero', 1, 1))); $table->addRow($new_cols); } return $table; }
/** * Get UserMacros data * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param _array $options * @param array $options['nodeids'] Node IDs * @param array $options['groupids'] UserMacrosGroup IDs * @param array $options['macroids'] UserMacros IDs * @param boolean $options['monitored_macros'] only monitored UserMacros * @param boolean $options['templated_macros'] include templates in result * @param boolean $options['with_items'] only with items * @param boolean $options['with_monitored_items'] only with monitored items * @param boolean $options['with_historical_items'] only with historical items * @param boolean $options['with_triggers'] only with triggers * @param boolean $options['with_monitored_triggers'] only with monitored triggers * @param boolean $options['with_httptests'] only with http tests * @param boolean $options['with_monitored_httptests'] only with monitored http tests * @param boolean $options['with_graphs'] only with graphs * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins * @param int $options['extendoutput'] return all fields for UserMacros * @param int $options['count'] count UserMacros, returned column name is rowscount * @param string $options['pattern'] search macros by pattern in macro names * @param int $options['limit'] limit selection * @param string $options['order'] deprecated parameter (for now) * @return array|boolean UserMacros 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('macro'); // 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('macros' => 'hm.hostmacroid'), 'from' => array('hostmacro hm'), 'where' => array(), 'order' => array(), 'limit' => null); $sql_parts_global = array('select' => array('macros' => 'gm.globalmacroid'), 'from' => array('globalmacro gm'), 'where' => array(), 'order' => array(), 'limit' => null); $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'hostmacroids' => null, 'globalmacroids' => null, 'templateids' => null, 'globalmacro' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'select_hosts' => null, 'select_templates' => null, 'countOutput' => 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_groups'])) { $options['select_groups'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_hosts'])) { $options['select_hosts'] = API_OUTPUT_EXTEND; } if (!is_null($options['select_templates'])) { $options['select_templates'] = API_OUTPUT_EXTEND; } } // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) { } else { if (!is_null($options['editable']) && !is_null($options['globalmacro'])) { return array(); } 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']['hgh'] = 'hg.hostid=hm.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(); // Global Macro if (!is_null($options['globalmacro'])) { $options['groupids'] = null; $options['hostmacroids'] = null; $options['triggerids'] = null; $options['hostids'] = null; $options['itemids'] = null; $options['select_groups'] = null; $options['select_templates'] = null; $options['select_hosts'] = null; } // globalmacroids if (!is_null($options['globalmacroids'])) { zbx_value2array($options['globalmacroids']); $sql_parts_global['where'][] = DBcondition('gm.globalmacroid', $options['globalmacroids']); } // hostmacroids if (!is_null($options['hostmacroids'])) { zbx_value2array($options['hostmacroids']); $sql_parts['where'][] = DBcondition('hm.hostmacroid', $options['hostmacroids']); } // 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']['hgh'] = 'hg.hostid=hm.hostid'; } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); $sql_parts['where'][] = DBcondition('hm.hostid', $options['hostids']); } // templateids if (!is_null($options['templateids'])) { zbx_value2array($options['templateids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['templateid'] = 'ht.templateid'; } $sql_parts['from']['macros_templates'] = 'macros_templates ht'; $sql_parts['where'][] = DBcondition('ht.templateid', $options['templateids']); $sql_parts['where']['hht'] = 'hm.hostid=ht.macroid'; } // search if (is_array($options['search'])) { zbx_db_search('hostmacro hm', $options, $sql_parts); zbx_db_search('globalmacro gm', $options, $sql_parts_global); } // filter if (is_array($options['filter'])) { if (isset($options['filter']['macro'])) { zbx_value2array($options['filter']['macro']); $sql_parts['where'][] = DBcondition('hm.macro', $options['filter']['macro'], null, true); $sql_parts_global['where'][] = DBcondition('gm.macro', $options['filter']['macro'], null, true); } } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sql_parts['select']['macros'] = 'hm.*'; $sql_parts_global['select']['macros'] = 'gm.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sql_parts['select'] = array('count(DISTINCT hm.hostmacroid) as rowscount'); $sql_parts_global['select'] = array('count(DISTINCT gm.globalmacroid) as rowscount'); } // 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'][] = 'hm.' . $options['sortfield'] . ' ' . $sortorder; $sql_parts_global['order'][] = 'gm.' . $options['sortfield'] . ' ' . $sortorder; if (!str_in_array('hm.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('hm.*', $sql_parts['select'])) { $sql_parts['select'][] = 'hm.' . $options['sortfield']; } if (!str_in_array('gm.' . $options['sortfield'], $sql_parts_global['select']) && !str_in_array('gm.*', $sql_parts_global['select'])) { $sql_parts_global['select'][] = 'gm.' . $options['sortfield']; } } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sql_parts['limit'] = $options['limit']; $sql_parts_global['limit'] = $options['limit']; } //------- // init GLOBALS if (!is_null($options['globalmacro'])) { $sql_parts_global['select'] = array_unique($sql_parts_global['select']); $sql_parts_global['from'] = array_unique($sql_parts_global['from']); $sql_parts_global['where'] = array_unique($sql_parts_global['where']); $sql_parts_global['order'] = array_unique($sql_parts_global['order']); $sql_select = ''; $sql_from = ''; $sql_where = ''; $sql_order = ''; if (!empty($sql_parts_global['select'])) { $sql_select .= implode(',', $sql_parts_global['select']); } if (!empty($sql_parts_global['from'])) { $sql_from .= implode(',', $sql_parts_global['from']); } if (!empty($sql_parts_global['where'])) { $sql_where .= ' AND ' . implode(' AND ', $sql_parts_global['where']); } if (!empty($sql_parts_global['order'])) { $sql_order .= ' ORDER BY ' . implode(',', $sql_parts_global['order']); } $sql_limit = $sql_parts_global['limit']; $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('gm.globalmacroid', $nodeids) . $sql_where . $sql_order; $res = DBselect($sql, $sql_limit); while ($macro = DBfetch($res)) { if ($options['countOutput']) { $result = $macro['rowscount']; } else { $globalmacroids[$macro['globalmacroid']] = $macro['globalmacroid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$macro['globalmacroid']] = array('globalmacroid' => $macro['globalmacroid']); } else { if (!isset($result[$macro['globalmacroid']])) { $result[$macro['globalmacroid']] = array(); } $result[$macro['globalmacroid']] += $macro; } } } } else { $hostids = 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['order'] = array_unique($sql_parts['order']); $sql_select = ''; $sql_from = ''; $sql_where = ''; $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['order'])) { $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']); } $sql_limit = $sql_parts['limit']; $sql = 'SELECT ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('hm.hostmacroid', $nodeids) . $sql_where . $sql_order; //SDI($sql); $res = DBselect($sql, $sql_limit); while ($macro = DBfetch($res)) { if ($options['countOutput']) { $result = $macro['rowscount']; } else { $hostmacroids[$macro['hostmacroid']] = $macro['hostmacroid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$macro['hostmacroid']] = $macro['hostmacroid']; } else { $hostids[$macro['hostid']] = $macro['hostid']; if (!isset($result[$macro['hostmacroid']])) { $result[$macro['hostmacroid']] = array(); } // Groups if ($options['select_groups'] && !isset($result[$macro['hostmacroid']]['groups'])) { $result[$macro['hostmacroid']]['groups'] = array(); } // Templates if ($options['select_templates'] && !isset($result[$macro['hostmacroid']]['templates'])) { $result[$macro['hostmacroid']]['templates'] = array(); } // Hosts if ($options['select_hosts'] && !isset($result[$macro['hostmacroid']]['hosts'])) { $result[$macro['hostmacroid']]['hosts'] = array(); } // groupids if (isset($macro['groupid'])) { if (!isset($result[$macro['hostmacroid']]['groups'])) { $result[$macro['hostmacroid']]['groups'] = array(); } $result[$macro['hostmacroid']]['groups'][] = array('groupid' => $macro['groupid']); unset($macro['groupid']); } // templateids if (isset($macro['templateid'])) { if (!isset($result[$macro['hostmacroid']]['templates'])) { $result[$macro['hostmacroid']]['templates'] = array(); } $result[$macro['hostmacroid']]['templates'][] = array('templateid' => $macro['templateid']); unset($macro['templateid']); } // hostids if (isset($macro['hostid'])) { if (!isset($result[$macro['hostmacroid']]['hosts'])) { $result[$macro['hostmacroid']]['hosts'] = array(); } $result[$macro['hostmacroid']]['hosts'][] = array('hostid' => $macro['hostid']); } $result[$macro['hostmacroid']] += $macro; } } } } if (!is_null($options['countOutput'])) { if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; } // Adding Objects // Adding Groups if (!is_null($options['select_groups']) && str_in_array($options['select_groups'], $subselects_allowed_outputs)) { $obj_params = array('output' => $options['select_groups'], 'hostids' => $hostids, 'preservekeys' => 1); $groups = CHostgroup::get($obj_params); foreach ($groups as $groupid => $group) { $ghosts = $group['hosts']; unset($group['hosts']); foreach ($ghosts as $num => $host) { foreach ($result as $macroid => $macro) { if ($macro['hostid'] == $host['hostid']) { $result[$macroid]['groups'][] = $group; } } } } } // Adding Templates if (!is_null($options['select_templates']) && str_in_array($options['select_templates'], $subselects_allowed_outputs)) { $obj_params = array('output' => $options['select_templates'], 'hostids' => $hostids, 'preservekeys' => 1); $templates = CTemplate::get($obj_params); foreach ($templates as $templateid => $template) { $thosts = $template['hosts']; unset($template['hosts']); foreach ($thosts as $num => $host) { foreach ($result as $macroid => $macro) { if ($macro['hostid'] == $host['hostid']) { $result[$macroid]['templates'][] = $template; } } } } } // Adding Hosts if (!is_null($options['select_hosts']) && str_in_array($options['select_hosts'], $subselects_allowed_outputs)) { $obj_params = array('output' => $options['select_hosts'], 'hostids' => $hostids, 'preservekeys' => 1); $hosts = CHost::get($obj_params); foreach ($hosts as $hostid => $host) { foreach ($result as $macroid => $macro) { if ($macro['hostid'] == $hostid) { $result[$macroid]['hosts'][] = $host; } } } } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
/** * Get Screen data * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param _array $options * @param array $options['nodeids'] Node IDs * @param boolean $options['with_items'] only with items * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins * @param int $options['extendoutput'] return all fields for Hosts * @param int $options['count'] count Hosts, returned column name is rowscount * @param string $options['pattern'] search hosts by pattern in host names * @param int $options['limit'] limit selection * @param string $options['order'] deprecated parameter (for now) * @return array|boolean Host data as array or false if error */ public static function get($options = array()) { global $USER_DETAILS; $result = array(); $user_type = $USER_DETAILS['type']; $sort_columns = array('screenid', 'name'); // 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('screens' => 's.screenid'), 'from' => array('screens' => 'screens s'), 'where' => array(), 'order' => array(), 'limit' => null); $def_options = array('nodeids' => null, 'screenids' => null, 'screenitemids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'extendoutput' => null, 'output' => API_OUTPUT_REFER, 'select_screenitems' => null, 'countOutput' => 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_screenitems'])) { $options['select_screenitems'] = API_OUTPUT_EXTEND; } } // editable + PERMISSION CHECK // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // screenids if (!is_null($options['screenids'])) { zbx_value2array($options['screenids']); $sql_parts['where'][] = DBcondition('s.screenid', $options['screenids']); } // screenitemids if (!is_null($options['screenitemids'])) { zbx_value2array($options['screenitemids']); if ($options['output'] != API_OUTPUT_EXTEND) { $sql_parts['select']['screenitemid'] = 'si.screenitemid'; } $sql_parts['from']['screens_items'] = 'screens_items si'; $sql_parts['where']['ssi'] = 'si.screenid=s.screenid'; $sql_parts['where'][] = DBcondition('si.screenitemid', $options['screenitemids']); } // extendoutput if ($options['output'] == API_OUTPUT_EXTEND) { $sql_parts['select']['screens'] = 's.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sql_parts['select'] = array('count(DISTINCT s.screenid) as rowscount'); } // filter if (is_array($options['filter'])) { zbx_db_filter('screens s', $options, $sql_parts); } // search if (is_array($options['search'])) { zbx_db_search('screens s', $options, $sql_parts); } // 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'][] = 's.' . $options['sortfield'] . ' ' . $sortorder; if (!str_in_array('s.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('s.*', $sql_parts['select'])) { $sql_parts['select'][] = 's.' . $options['sortfield']; } } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sql_parts['limit'] = $options['limit']; } //------- $screenids = 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['order'] = array_unique($sql_parts['order']); $sql_select = ''; $sql_from = ''; $sql_where = ''; $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['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('s.screenid', $nodeids) . $sql_where . $sql_order; $res = DBselect($sql, $sql_limit); while ($screen = DBfetch($res)) { if (!is_null($options['countOutput'])) { $result = $screen['rowscount']; } else { $screenids[$screen['screenid']] = $screen['screenid']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$screen['screenid']] = array('screenid' => $screen['screenid']); } else { if (!isset($result[$screen['screenid']])) { $result[$screen['screenid']] = array(); } if (!is_null($options['select_screenitems']) && !isset($result[$screen['screenid']]['screenitems'])) { $result[$screen['screenid']]['screenitems'] = array(); } if (isset($screen['screenitemid']) && is_null($options['select_screenitems'])) { if (!isset($result[$screen['screenid']]['screenitems'])) { $result[$screen['screenid']]['screenitems'] = array(); } $result[$screen['screenid']]['screenitems'][] = array('screenitemid' => $screen['screenitemid']); unset($screen['screenitemid']); } $result[$screen['screenid']] += $screen; } } } if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) { } else { if (!empty($result)) { $groups_to_check = array(); $hosts_to_check = array(); $graphs_to_check = array(); $items_to_check = array(); $maps_to_check = array(); $screens_to_check = array(); $screens_items = array(); $db_sitems = DBselect('SELECT * FROM screens_items WHERE ' . DBcondition('screenid', $screenids)); while ($sitem = DBfetch($db_sitems)) { if ($sitem['resourceid'] == 0) { continue; } $screens_items[$sitem['screenitemid']] = $sitem; switch ($sitem['resourcetype']) { case SCREEN_RESOURCE_HOSTS_INFO: case SCREEN_RESOURCE_TRIGGERS_INFO: case SCREEN_RESOURCE_TRIGGERS_OVERVIEW: case SCREEN_RESOURCE_DATA_OVERVIEW: case SCREEN_RESOURCE_HOSTGROUP_TRIGGERS: $groups_to_check[] = $sitem['resourceid']; break; case SCREEN_RESOURCE_HOST_TRIGGERS: $hosts_to_check[] = $sitem['resourceid']; break; case SCREEN_RESOURCE_GRAPH: $graphs_to_check[] = $sitem['resourceid']; break; case SCREEN_RESOURCE_SIMPLE_GRAPH: case SCREEN_RESOURCE_PLAIN_TEXT: $items_to_check[] = $sitem['resourceid']; break; case SCREEN_RESOURCE_MAP: $maps_to_check[] = $sitem['resourceid']; break; case SCREEN_RESOURCE_SCREEN: $screens_to_check[] = $sitem['resourceid']; break; } } $groups_to_check = array_unique($groups_to_check); $hosts_to_check = array_unique($hosts_to_check); $graphs_to_check = array_unique($graphs_to_check); $items_to_check = array_unique($items_to_check); $maps_to_check = array_unique($maps_to_check); $screens_to_check = array_unique($screens_to_check); /* sdii($graphs_to_check); sdii($items_to_check); sdii($maps_to_check); sdii($screens_to_check); //*/ // group $group_options = array('nodeids' => $nodeids, 'groupids' => $groups_to_check, 'editable' => $options['editable']); $allowed_groups = CHostgroup::get($group_options); $allowed_groups = zbx_objectValues($allowed_groups, 'groupid'); // host $host_options = array('nodeids' => $nodeids, 'hostids' => $hosts_to_check, 'editable' => $options['editable']); $allowed_hosts = CHost::get($host_options); $allowed_hosts = zbx_objectValues($allowed_hosts, 'hostid'); // graph $graph_options = array('nodeids' => $nodeids, 'graphids' => $graphs_to_check, 'editable' => $options['editable']); $allowed_graphs = CGraph::get($graph_options); $allowed_graphs = zbx_objectValues($allowed_graphs, 'graphid'); // item $item_options = array('nodeids' => $nodeids, 'itemids' => $items_to_check, 'webitems' => 1, 'editable' => $options['editable']); $allowed_items = CItem::get($item_options); $allowed_items = zbx_objectValues($allowed_items, 'itemid'); // map $map_options = array('nodeids' => $nodeids, 'sysmapids' => $maps_to_check, 'editable' => $options['editable']); $allowed_maps = CMap::get($map_options); $allowed_maps = zbx_objectValues($allowed_maps, 'sysmapid'); // screen $screens_options = array('nodeids' => $nodeids, 'screenids' => $screens_to_check, 'editable' => $options['editable']); $allowed_screens = CScreen::get($screens_options); $allowed_screens = zbx_objectValues($allowed_screens, 'screenid'); $restr_groups = array_diff($groups_to_check, $allowed_groups); $restr_hosts = array_diff($hosts_to_check, $allowed_hosts); $restr_graphs = array_diff($graphs_to_check, $allowed_graphs); $restr_items = array_diff($items_to_check, $allowed_items); $restr_maps = array_diff($maps_to_check, $allowed_maps); $restr_screens = array_diff($screens_to_check, $allowed_screens); /* SDI('---------------------------------------'); SDII($restr_graphs); SDII($restr_items); SDII($restr_maps); SDII($restr_screens); SDI('/////////////////////////////////'); //*/ // group foreach ($restr_groups as $resourceid) { foreach ($screens_items as $screen_itemid => $screen_item) { if ($screen_item['resourceid'] == $resourceid && uint_in_array($screen_item['resourcetype'], array(SCREEN_RESOURCE_HOSTS_INFO, SCREEN_RESOURCE_TRIGGERS_INFO, SCREEN_RESOURCE_TRIGGERS_OVERVIEW, SCREEN_RESOURCE_DATA_OVERVIEW, SCREEN_RESOURCE_HOSTGROUP_TRIGGERS))) { unset($result[$screen_item['screenid']]); unset($screens_items[$screen_itemid]); } } } // host foreach ($restr_hosts as $resourceid) { foreach ($screens_items as $screen_itemid => $screen_item) { if ($screen_item['resourceid'] == $resourceid && uint_in_array($screen_item['resourcetype'], array(SCREEN_RESOURCE_HOST_TRIGGERS))) { unset($result[$screen_item['screenid']]); unset($screens_items[$screen_itemid]); } } } // graph foreach ($restr_graphs as $resourceid) { foreach ($screens_items as $screen_itemid => $screen_item) { if ($screen_item['resourceid'] == $resourceid && $screen_item['resourcetype'] == SCREEN_RESOURCE_GRAPH) { unset($result[$screen_item['screenid']]); unset($screens_items[$screen_itemid]); } } } // item foreach ($restr_items as $resourceid) { foreach ($screens_items as $screen_itemid => $screen_item) { if ($screen_item['resourceid'] == $resourceid && uint_in_array($screen_item['resourcetype'], array(SCREEN_RESOURCE_SIMPLE_GRAPH, SCREEN_RESOURCE_PLAIN_TEXT))) { unset($result[$screen_item['screenid']]); unset($screens_items[$screen_itemid]); } } } // map foreach ($restr_maps as $resourceid) { foreach ($screens_items as $screen_itemid => $screen_item) { if ($screen_item['resourceid'] == $resourceid && $screen_item['resourcetype'] == SCREEN_RESOURCE_MAP) { unset($result[$screen_item['screenid']]); unset($screens_items[$screen_itemid]); } } } // screen foreach ($restr_screens as $resourceid) { foreach ($screens_items as $screen_itemid => $screen_item) { if ($screen_item['resourceid'] == $resourceid && $screen_item['resourcetype'] == SCREEN_RESOURCE_SCREEN) { unset($result[$screen_item['screenid']]); unset($screens_items[$screen_itemid]); } } } } } if (!is_null($options['countOutput'])) { if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; } // Adding ScreenItems if (!is_null($options['select_screenitems']) && str_in_array($options['select_screenitems'], $subselects_allowed_outputs)) { if (!isset($screens_items)) { $screens_items = array(); $db_sitems = DBselect('SELECT * FROM screens_items WHERE ' . DBcondition('screenid', $screenids)); while ($sitem = DBfetch($db_sitems)) { $screens_items[$sitem['screenitemid']] = $sitem; } } foreach ($screens_items as $snum => $sitem) { if (!isset($result[$sitem['screenid']]['screenitems'])) { $result[$sitem['screenid']]['screenitems'] = array(); } $result[$sitem['screenid']]['screenitems'][] = $sitem; } } // removing keys (hash -> array) if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; }
/** * Get Host data * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param _array $options * @param array $options['nodeids'] Node IDs * @param array $options['groupids'] HostGroup IDs * @param array $options['hostids'] Host IDs * @param boolean $options['monitored_hosts'] only monitored Hosts * @param boolean $options['templated_hosts'] include templates in result * @param boolean $options['with_items'] only with items * @param boolean $options['with_monitored_items'] only with monitored items * @param boolean $options['with_historical_items'] only with historical items * @param boolean $options['with_triggers'] only with triggers * @param boolean $options['with_monitored_triggers'] only with monitored triggers * @param boolean $options['with_httptests'] only with http tests * @param boolean $options['with_monitored_httptests'] only with monitored http tests * @param boolean $options['with_graphs'] only with graphs * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins * @param int $options['extendoutput'] return all fields for Hosts * @param boolean $options['select_groups'] select HostGroups * @param boolean $options['select_templates'] select Templates * @param boolean $options['select_items'] select Items * @param boolean $options['select_triggers'] select Triggers * @param boolean $options['select_graphs'] select Graphs * @param boolean $options['select_applications'] select Applications * @param boolean $options['select_macros'] select Macros * @param boolean $options['select_profile'] select Profile * @param int $options['count'] count Hosts, returned column name is rowscount * @param string $options['pattern'] search hosts by pattern in Host name * @param string $options['extendPattern'] search hosts by pattern in Host name, ip and DNS * @param int $options['limit'] limit selection * @param string $options['sortfield'] field to sort by * @param string $options['sortorder'] sort order * @return array|boolean Host data as array or false if error */ public static function get($options = array()) { global $USER_DETAILS; $result = array(); $nodeCheck = false; $user_type = $USER_DETAILS['type']; $sort_columns = array('dhostid', 'druleid'); // allowed columns for sorting $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM); // allowed output options for [ select_* ] params $sql_parts = array('select' => array('dhosts' => 'dh.dhostid'), 'from' => array('dhosts' => 'dhosts dh'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null); $def_options = array('nodeids' => null, 'druleids' => null, 'dhostids' => null, 'dserviceids' => null, 'groupids' => null, 'hostids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'selectDRules' => null, 'selectDServices' => null, 'selectGroups' => null, 'selectHosts' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null); $options = zbx_array_merge($def_options, $options); if (is_array($options['output'])) { unset($sql_parts['select']['dhosts']); foreach ($options['output'] as $key => $field) { $sql_parts['select'][$field] = ' dh.' . $field; } $options['output'] = API_OUTPUT_CUSTOM; } // editable + PERMISSION CHECK if (USER_TYPE_SUPER_ADMIN == $user_type) { } else { if (is_null($options['editable']) && $USER_DETAILS['type'] == USER_TYPE_ZABBIX_ADMIN) { } else { if (!is_null($options['editable']) && $USER_DETAILS['type'] != USER_TYPE_SUPER_ADMIN) { return array(); } } } // nodeids $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid(); // dhostids if (!is_null($options['dhostids'])) { zbx_value2array($options['dhostids']); $sql_parts['where']['dhostid'] = DBcondition('dh.dhostid', $options['dhostids']); if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('dh.dhostid', $nodeids); } } // druleids if (!is_null($options['druleids'])) { zbx_value2array($options['druleids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['druleid'] = 'dh.druleid'; } $sql_parts['where']['druleid'] = DBcondition('dh.druleid', $options['druleids']); if (!is_null($options['groupCount'])) { $sql_parts['group']['druleid'] = 'dh.druleid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('dh.druleid', $nodeids); } } // dserviceids if (!is_null($options['dserviceids'])) { zbx_value2array($options['dserviceids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['dserviceids'] = 'ds.dserviceids'; } $sql_parts['from']['dservices'] = 'dservices ds'; $sql_parts['where'][] = DBcondition('ds.dserviceid', $options['dserviceids']); $sql_parts['where']['dhds'] = 'dh.dhostid=ds.dhostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['dserviceids'] = 'ds.dserviceid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('ds.dserviceid', $nodeids); } } // 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'] = 'hosts h'; $sql_parts['from']['hosts_groups'] = 'hosts_groups hg'; $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']); $sql_parts['where']['dhh'] = 'h.ip=dh.ip'; $sql_parts['where']['hgh'] = 'hg.hostid=h.hostid'; if (!is_null($options['groupCount'])) { $sql_parts['group']['groupid'] = 'hg.groupid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('hg.groupid', $nodeids); } } // hostids if (!is_null($options['hostids'])) { zbx_value2array($options['hostids']); if ($options['output'] != API_OUTPUT_SHORTEN) { $sql_parts['select']['hostid'] = 'h.hostid'; } $sql_parts['from']['hosts'] = 'hosts h'; $sql_parts['where'][] = DBcondition('h.hostid', $options['hostids']); $sql_parts['where']['dhh'] = 'h.ip=dh.ip'; if (!is_null($options['groupCount'])) { $sql_parts['group']['hostid'] = 'h.hostid'; } if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('h.hostid', $nodeids); } } // node check !!!!! // should be last, after all ****IDS checks if (!$nodeCheck) { $nodeCheck = true; $sql_parts['where'][] = DBin_node('dh.dhostid', $nodeids); } // output if ($options['output'] == API_OUTPUT_EXTEND) { $sql_parts['select']['dhosts'] = 'dh.*'; } // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; $sql_parts['select'] = array('count(DISTINCT dh.dhostid) as rowscount'); //groupCount if (!is_null($options['groupCount'])) { foreach ($sql_parts['group'] as $key => $fields) { $sql_parts['select'][$key] = $fields; } } } // filter if (is_array($options['filter'])) { zbx_db_filter('dhosts dh', $options, $sql_parts); } // search if (is_array($options['search'])) { zbx_db_search('dhosts dh', $options, $sql_parts); } // 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'][$options['sortfield']] = 'dh.' . $options['sortfield'] . ' ' . $sortorder; if (!str_in_array('dh.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('dh.*', $sql_parts['select'])) { $sql_parts['select'][$options['sortfield']] = 'dh.' . $options['sortfield']; } } // limit if (zbx_ctype_digit($options['limit']) && $options['limit']) { $sql_parts['limit'] = $options['limit']; } //------- $dhostids = 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 .= 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 ' . $sql_where . $sql_group . $sql_order; //SDI($sql); $res = DBselect($sql, $sql_limit); while ($dhost = DBfetch($res)) { if (!is_null($options['countOutput'])) { if (!is_null($options['groupCount'])) { $result[] = $dhost; } else { $result = $dhost['rowscount']; } } else { $dhostids[$dhost['dhostid']] = $dhost['dhostid']; // $dips[$dhost['ip']] = $dhost['ip']; if ($options['output'] == API_OUTPUT_SHORTEN) { $result[$dhost['dhostid']] = array('dhostid' => $dhost['dhostid']); } else { if (!isset($result[$dhost['dhostid']])) { $result[$dhost['dhostid']] = array(); } if (!is_null($options['selectDRules']) && !isset($result[$dhost['dhostid']]['drules'])) { $result[$dhost['dhostid']]['drules'] = array(); } if (!is_null($options['selectDServices']) && !isset($result[$dhost['dhostid']]['dservices'])) { $result[$dhost['dhostid']]['dservices'] = array(); } if (!is_null($options['selectGroups']) && !isset($result[$dhost['dhostid']]['groups'])) { $result[$dhost['dhostid']]['groups'] = array(); } if (!is_null($options['selectHosts']) && !isset($result[$dhost['dhostid']]['hosts'])) { $result[$dhost['dhostid']]['hosts'] = array(); } // druleids if (isset($dhost['druleid']) && is_null($options['selectDRules'])) { if (!isset($result[$dhost['dhostid']]['drules'])) { $result[$dhost['dhostid']]['drules'] = array(); } $result[$dhost['dhostid']]['drules'][] = array('druleid' => $dhost['druleid']); } // dserviceids if (isset($dhost['dserviceid']) && is_null($options['selectDServices'])) { if (!isset($result[$dhost['dhostid']]['dservices'])) { $result[$dhost['dhostid']]['dservices'] = array(); } $result[$dhost['dhostid']]['dservices'][] = array('dserviceid' => $dhost['dserviceid']); unset($dhost['dserviceid']); } // groupids if (isset($dhost['groupid']) && is_null($options['selectGroups'])) { if (!isset($result[$dhost['dhostid']]['groups'])) { $result[$dhost['dhostid']]['groups'] = array(); } $result[$dhost['dhostid']]['groups'][] = array('groupid' => $dhost['groupid']); unset($dhost['groupid']); } // hostids if (isset($dhost['hostid']) && is_null($options['selectHosts'])) { if (!isset($result[$dhost['hostid']]['hosts'])) { $result[$dhost['dhostid']]['hosts'] = array(); } $result[$dhost['dhostid']]['hosts'][] = array('hostid' => $dhost['hostid']); unset($dhost['hostid']); } $result[$dhost['dhostid']] += $dhost; } } } Copt::memoryPick(); if (!is_null($options['countOutput'])) { if (is_null($options['preservekeys'])) { $result = zbx_cleanHashes($result); } return $result; } // Adding Objects // select_drules if (!is_null($options['selectDRules'])) { $obj_params = array('nodeids' => $nodeids, 'dhostids' => $dhostids, 'preservekeys' => 1); if (is_array($options['selectDRules']) || str_in_array($options['selectDRules'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['selectDRules']; $drules = CDRule::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($drules, 'name'); } foreach ($drules as $druleid => $drule) { unset($drules[$druleid]['dhosts']); $count = array(); foreach ($drule['dhosts'] as $dnum => $dhost) { if (!is_null($options['limitSelects'])) { if (!isset($count[$dhost['dhostid']])) { $count[$dhost['dhostid']] = 0; } $count[$dhost['dhostid']]++; if ($count[$dhost['dhostid']] > $options['limitSelects']) { continue; } } $result[$dhost['dhostid']]['drules'][] =& $drules[$druleid]; } } } else { if (API_OUTPUT_COUNT == $options['selectDRules']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $drules = CDRule::get($obj_params); $drules = zbx_toHash($drules, 'dhostid'); foreach ($result as $dhostid => $dhost) { if (isset($drules[$dhostid])) { $result[$dhostid]['drules'] = $drules[$dhostid]['rowscount']; } else { $result[$dhostid]['drules'] = 0; } } } } } // selectDServices if (!is_null($options['selectDServices'])) { $obj_params = array('nodeids' => $nodeids, 'dhostids' => $dhostids, 'preservekeys' => 1); if (is_array($options['selectDServices']) || str_in_array($options['selectDServices'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['selectDServices']; $dservices = CDService::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($dservices, 'name'); } foreach ($dservices as $dserviceid => $dservice) { unset($dservices[$dserviceid]['dhosts']); foreach ($dservice['dhosts'] as $dnum => $dhost) { if (!is_null($options['limitSelects'])) { if (!isset($count[$dhost['dhostid']])) { $count[$dhost['dhostid']] = 0; } $count[$dhost['dhostid']]++; if ($count[$dhost['dhostid']] > $options['limitSelects']) { continue; } } $result[$dhost['dhostid']]['dservices'][] =& $dservices[$dserviceid]; } } } else { if (API_OUTPUT_COUNT == $options['selectDServices']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $dservices = CDService::get($obj_params); $dservices = zbx_toHash($dservices, 'dhostid'); foreach ($result as $dhostid => $dhost) { if (isset($dservices[$dhostid])) { $result[$dhostid]['dservices'] = $dservices[$dhostid]['rowscount']; } else { $result[$dhostid]['dservices'] = 0; } } } } } // TODO :select_groups if (!is_null($options['selectGroups']) && str_in_array($options['selectGroups'], $subselects_allowed_outputs)) { $obj_params = array('nodeids' => $nodeids, 'output' => $options['selectGroups'], 'hostids' => $dhostids, 'preservekeys' => 1); $groups = CHostgroup::get($obj_params); foreach ($groups as $groupid => $group) { $ghosts = $group['hosts']; unset($group['hosts']); foreach ($ghosts as $num => $dhost) { $result[$dhost['hostid']]['groups'][] = $group; } } } // select_hosts if (!is_null($options['selectHosts'])) { $obj_params = array('nodeids' => $nodeids, 'dhostids' => $dhostids, 'preservekeys' => 1); if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselects_allowed_outputs)) { $obj_params['output'] = $options['selectHosts']; $hosts = CHost::get($obj_params); if (!is_null($options['limitSelects'])) { order_result($hosts, 'host'); } foreach ($hosts as $hostid => $host) { unset($hosts[$hostid]['dhosts']); foreach ($host['dhosts'] as $dnum => $dhost) { if (!is_null($options['limitSelects'])) { if (!isset($count[$dhost['dhostid']])) { $count[$dhost['dhostid']] = 0; } $count[$dhost['dhostid']]++; if ($count[$dhost['dhostid']] > $options['limitSelects']) { continue; } } $result[$dhost['dhostid']]['hosts'][] =& $hosts[$hostid]; } } } else { if (API_OUTPUT_COUNT == $options['selectHosts']) { $obj_params['countOutput'] = 1; $obj_params['groupCount'] = 1; $hosts = CHost::get($obj_params); $hosts = zbx_toHash($hosts, 'hostid'); foreach ($result as $dhostid => $dhost) { if (isset($hosts[$dhostid])) { $result[$dhostid]['hosts'] = $hosts[$dhostid]['rowscount']; } else { $result[$dhostid]['hosts'] = 0; } } } } } 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; }
public static function parseMain($rules) { $triggers_for_dependencies = array(); try { if (isset($rules['host']['exist']) || isset($rules['host']['missed'])) { $xpath = new DOMXPath(self::$xml); $hosts = $xpath->query('hosts/host'); foreach ($hosts as $hnum => $host) { $host_db = self::mapXML2arr($host, XML_TAG_HOST); if (!isset($host_db['status'])) { $host_db['status'] = HOST_STATUS_TEMPLATE; } $current_host = $host_db['status'] == HOST_STATUS_TEMPLATE ? CTemplate::exists($host_db) : CHost::exists($host_db); if (!$current_host && !isset($rules['host']['missed'])) { info('Host [' . $host_db['host'] . '] skipped - user rule'); continue; // break if update nonexist } if ($current_host && !isset($rules['host']['exist'])) { info('Host [' . $host_db['host'] . '] skipped - user rule'); continue; // break if not update exist } if (isset($host_db['proxy_hostid'])) { $proxy_exists = CProxy::get(array('proxyids' => $host_db['proxy_hostid'])); if (empty($proxy_exists)) { $host_db['proxy_hostid'] = 0; } } if ($current_host) { $options = array('filter' => array('host' => $host_db['host']), 'output' => API_OUTPUT_EXTEND, 'editable' => 1); if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $current_host = CTemplate::get($options); } else { $current_host = CHost::get($options); } if (empty($current_host)) { throw new APIException(1, 'No permission for host [' . $host_db['host'] . ']'); } else { $current_host = reset($current_host); } } // HOST GROUPS {{{ $groups = $xpath->query('groups/group', $host); $host_db['groups'] = array(); $groups_to_parse = array(); foreach ($groups as $gnum => $group) { $groups_to_parse[] = array('name' => $group->nodeValue); } if (empty($groups_to_parse)) { $groups_to_parse[] = array('name' => ZBX_DEFAULT_IMPORT_HOST_GROUP); } foreach ($groups_to_parse as $group) { $current_group = CHostGroup::exists($group); if ($current_group) { $options = array('filter' => $group, 'output' => API_OUTPUT_EXTEND, 'editable' => 1); $current_group = CHostGroup::get($options); if (empty($current_group)) { throw new APIException(1, 'No permissions for group ' . $group['name']); } $host_db['groups'][] = reset($current_group); } else { $result = CHostGroup::create($group); if (!$result) { throw new APIException(1, CHostGroup::resetErrors()); } $options = array('groupids' => $result['groupids'], 'output' => API_OUTPUT_EXTEND); $new_group = CHostgroup::get($options); $host_db['groups'][] = reset($new_group); } } // }}} HOST GROUPS // MACROS $macros = $xpath->query('macros/macro', $host); $host_db['macros'] = array(); if ($macros->length > 0) { foreach ($macros as $macro) { $host_db['macros'][] = self::mapXML2arr($macro, XML_TAG_MACRO); } } // }}} MACROS // TEMPLATES {{{ if (isset($rules['template']['exist'])) { $templates = $xpath->query('templates/template', $host); $host_db['templates'] = array(); foreach ($templates as $tnum => $template) { $options = array('filter' => array('host' => $template->nodeValue), 'output' => API_OUTPUT_EXTEND, 'editable' => 1); $current_template = CTemplate::get($options); if (empty($current_template)) { throw new APIException(1, 'No permission for Template [' . $template->nodeValue . ']'); } $current_template = reset($current_template); if (!$current_template && !isset($rules['template']['missed'])) { info('Template [' . $template->nodeValue . '] skipped - user rule'); continue; // break if update nonexist } if ($current_template && !isset($rules['template']['exist'])) { info('Template [' . $template->nodeValue . '] skipped - user rule'); continue; // break if not update exist } $host_db['templates'][] = $current_template; } } // }}} TEMPLATES // HOSTS if ($current_host && isset($rules['host']['exist'])) { if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $host_db['templateid'] = $current_host['hostid']; $result = CTemplate::update($host_db); if (!$result) { throw new APIException(1, CTemplate::resetErrors()); } $options = array('templateids' => $result['templateids'], 'output' => API_OUTPUT_EXTEND); $current_host = CTemplate::get($options); } else { $host_db['hostid'] = $current_host['hostid']; $result = CHost::update($host_db); if (!$result) { throw new APIException(1, CHost::resetErrors()); } $options = array('hostids' => $result['hostids'], 'output' => API_OUTPUT_EXTEND); $current_host = CHost::get($options); } if ($current_host === false) { throw new APIException(1, $host_db['status'] == HOST_STATUS_TEMPLATE ? CTemplate::resetErrors() : CHost::resetErrors()); } } if (!$current_host && isset($rules['host']['missed'])) { if ($host_db['status'] == HOST_STATUS_TEMPLATE) { $result = CTemplate::create($host_db); if (!$result) { throw new APIException(1, CTemplate::resetErrors()); } $options = array('templateids' => $result['templateids'], 'output' => API_OUTPUT_EXTEND); $current_host = CTemplate::get($options); } else { $result = CHost::create($host_db); if (!$result) { throw new APIException(1, CHost::resetErrors()); } $options = array('hostids' => $result['hostids'], 'output' => API_OUTPUT_EXTEND); $current_host = CHost::get($options); } } $current_host = reset($current_host); // HOST PROFILES {{{ $profile_node = $xpath->query('host_profile/*', $host); if ($profile_node->length > 0) { $profile = array(); foreach ($profile_node as $num => $field) { $profile[$field->nodeName] = $field->nodeValue; } delete_host_profile($current_host['hostid']); add_host_profile($current_host['hostid'], $profile['devicetype'], $profile['name'], $profile['os'], $profile['serialno'], $profile['tag'], $profile['macaddress'], $profile['hardware'], $profile['software'], $profile['contact'], $profile['location'], $profile['notes']); } $profile_ext_node = $xpath->query('host_profiles_ext/*', $host); if ($profile_ext_node->length > 0) { $profile_ext = array(); foreach ($profile_ext_node as $num => $field) { $profile_ext[$field->nodeName] = $field->nodeValue; } delete_host_profile_ext($current_host['hostid']); add_host_profile_ext($current_host['hostid'], $profile_ext); } // }}} HOST PROFILES // ITEMS {{{ if (isset($rules['item']['exist']) || isset($rules['item']['missed'])) { $items = $xpath->query('items/item', $host); foreach ($items as $inum => $item) { $item_db = self::mapXML2arr($item, XML_TAG_ITEM); $item_db['hostid'] = $current_host['hostid']; if ($current_item = CItem::exists($item_db)) { $options = array('filter' => array('hostid' => $item_db['hostid'], 'key_' => $item_db['key_']), 'webitems' => 1, 'output' => API_OUTPUT_EXTEND, 'editable' => 1); $current_item = CItem::get($options); if (empty($current_item)) { throw new APIException(1, 'No permission for Item [' . $item_db['key_'] . ']'); } $current_item = reset($current_item); } if (!$current_item && !isset($rules['item']['missed'])) { info('Item [' . $item_db['key_'] . '] skipped - user rule'); continue; // break if not update exist } if ($current_item && !isset($rules['item']['exist'])) { info('Item [' . $item_db['key_'] . '] skipped - user rule'); continue; // break if not update exist } // ITEM APPLICATIONS {{{ $applications = $xpath->query('applications/application', $item); $item_applications = array(); $applications_to_add = array(); foreach ($applications as $application) { $application_db = array('name' => $application->nodeValue, 'hostid' => $current_host['hostid']); if ($current_application = CApplication::exists($application_db)) { $current_application = CApplication::get(array('filter' => $application_db, 'output' => API_OUTPUT_EXTEND)); if (empty($current_application)) { throw new APIException(1, 'No permission for Application [' . $application_db['name'] . ']'); } } if ($current_application) { $item_applications = array_merge($item_applications, $current_application); } else { $applications_to_add[] = $application_db; } } if (!empty($applications_to_add)) { $result = CApplication::create($applications_to_add); if (!$result) { throw new APIException(1, CApplication::resetErrors()); } $options = array('applicationids' => $result['applicationids'], 'output' => API_OUTPUT_EXTEND); $new_applications = CApplication::get($options); $item_applications = array_merge($item_applications, $new_applications); } // }}} ITEM APPLICATIONS if ($current_item && isset($rules['item']['exist'])) { $item_db['itemid'] = $current_item['itemid']; $result = CItem::update($item_db); if (!$result) { throw new APIException(1, CItem::resetErrors()); } $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND); $current_item = CItem::get($options); } if (!$current_item && isset($rules['item']['missed'])) { $result = CItem::create($item_db); if (!$result) { throw new APIException(1, CItem::resetErrors()); } $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND); $current_item = CItem::get($options); } $r = CApplication::massAdd(array('applications' => $item_applications, 'items' => $current_item)); if ($r === false) { throw new APIException(1, CApplication::resetErrors()); } } } // }}} ITEMS // TRIGGERS {{{ if (isset($rules['trigger']['exist']) || isset($rules['trigger']['missed'])) { $triggers = $xpath->query('triggers/trigger', $host); $triggers_to_add = array(); $triggers_to_upd = array(); foreach ($triggers as $trigger) { $trigger_db = self::mapXML2arr($trigger, XML_TAG_TRIGGER); $trigger_db['expression'] = str_replace('{{HOSTNAME}:', '{' . $host_db['host'] . ':', $trigger_db['expression']); $trigger_db['hostid'] = $current_host['hostid']; if ($current_trigger = CTrigger::exists($trigger_db)) { $ctriggers = CTrigger::get(array('filter' => array('description' => $trigger_db['description']), 'hostids' => $current_host['hostid'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1)); $current_trigger = false; foreach ($ctriggers as $tnum => $ct) { $tmp_exp = explode_exp($ct['expression'], false); if (strcmp($trigger_db['expression'], $tmp_exp) == 0) { $current_trigger = $ct; break; } } if (!$current_trigger) { throw new APIException(1, 'No permission for Trigger [' . $trigger_db['description'] . ']'); } } if (!$current_trigger && !isset($rules['trigger']['missed'])) { info('Trigger [' . $trigger_db['description'] . '] skipped - user rule'); continue; // break if not update exist } if ($current_trigger && !isset($rules['trigger']['exist'])) { info('Trigger [' . $trigger_db['description'] . '] skipped - user rule'); continue; // break if not update exist } if ($current_trigger && isset($rules['trigger']['exist'])) { $trigger_db['triggerid'] = $current_trigger['triggerid']; $triggers_to_upd[] = $trigger_db; } if (!$current_trigger && isset($rules['trigger']['missed'])) { $triggers_to_add[] = $trigger_db; } } if (!empty($triggers_to_upd)) { $result = CTrigger::update($triggers_to_upd); if (!$result) { throw new APIException(1, CTrigger::resetErrors()); } $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND); $r = CTrigger::get($options); $triggers_for_dependencies = array_merge($triggers_for_dependencies, $r); } if (!empty($triggers_to_add)) { $result = CTrigger::create($triggers_to_add); if (!$result) { throw new APIException(1, CTrigger::resetErrors()); } $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND); $r = CTrigger::get($options); $triggers_for_dependencies = array_merge($triggers_for_dependencies, $r); } } // }}} TRIGGERS // GRAPHS {{{ if (isset($rules['graph']['exist']) || isset($rules['graph']['missed'])) { $graphs = $xpath->query('graphs/graph', $host); $graphs_to_add = array(); $graphs_to_upd = array(); foreach ($graphs as $gnum => $graph) { // GRAPH ITEMS {{{ $gitems = $xpath->query('graph_elements/graph_element', $graph); $graph_hostids = array(); $graph_items = array(); foreach ($gitems as $ginum => $gitem) { $gitem_db = self::mapXML2arr($gitem, XML_TAG_GRAPH_ELEMENT); $data = explode(':', $gitem_db['host_key_']); $gitem_host = array_shift($data); $gitem_db['host'] = $gitem_host == '{HOSTNAME}' ? $host_db['host'] : $gitem_host; $gitem_db['key_'] = implode(':', $data); if ($current_item = CItem::exists($gitem_db)) { $current_item = CItem::get(array('filter' => array('key_' => $gitem_db['key_']), 'webitems' => 1, 'host' => $gitem_db['host'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1)); if (empty($current_item)) { throw new APIException(1, 'No permission for Item [' . $gitem_db['key_'] . ']'); } $current_item = reset($current_item); $graph_hostids[] = $current_item['hostid']; $gitem_db['itemid'] = $current_item['itemid']; $graph_items[] = $gitem_db; } else { throw new APIException(1, 'Item [' . $gitem_db['host_key_'] . '] does not exists'); } } // }}} GRAPH ITEMS $graph_db = self::mapXML2arr($graph, XML_TAG_GRAPH); $graph_db['hostids'] = $graph_hostids; if ($current_graph = CGraph::exists($graph_db)) { $current_graph = CGraph::get(array('filter' => array('name' => $graph_db['name']), 'hostids' => $graph_db['hostids'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1)); if (empty($current_graph)) { throw new APIException(1, 'No permission for Graph [' . $graph_db['name'] . ']'); } $current_graph = reset($current_graph); } if (!$current_graph && !isset($rules['graph']['missed'])) { info('Graph [' . $graph_db['name'] . '] skipped - user rule'); continue; // break if not update exist } if ($current_graph && !isset($rules['graph']['exist'])) { info('Graph [' . $graph_db['name'] . '] skipped - user rule'); continue; // break if not update exist } if ($graph_db['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $item_data = explode(':', $graph_db['ymin_item_key'], 2); if (count($item_data) < 2) { throw new APIException(1, 'Incorrect y min item for graph [' . $graph_db['name'] . ']'); } if (!($item = get_item_by_key($item_data[1], $item_data[0]))) { throw new APIException(1, 'Missing item [' . $graph_db['ymin_item_key'] . '] for host [' . $host_db['host'] . ']'); } $graph_db['ymin_itemid'] = $item['itemid']; } if ($graph_db['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $item_data = explode(':', $graph_db['ymax_item_key'], 2); if (count($item_data) < 2) { throw new APIException(1, 'Incorrect y max item for graph [' . $graph_db['name'] . ']'); } if (!($item = get_item_by_key($item_data[1], $item_data[0]))) { throw new APIException(1, 'Missing item [' . $graph_db['ymax_item_key'] . '] for host [' . $host_db['host'] . ']'); } $graph_db['ymax_itemid'] = $item['itemid']; } $graph_db['gitems'] = $graph_items; if ($current_graph) { $graph_db['graphid'] = $current_graph['graphid']; $graphs_to_upd[] = $graph_db; } else { $graphs_to_add[] = $graph_db; } } if (!empty($graphs_to_add)) { $r = CGraph::create($graphs_to_add); if ($r === false) { throw new APIException(1, CGraph::resetErrors()); } } if (!empty($graphs_to_upd)) { $r = CGraph::update($graphs_to_upd); if ($r === false) { throw new APIException(1, CGraph::resetErrors()); } } } } // DEPENDENCIES $dependencies = $xpath->query('dependencies/dependency'); if ($dependencies->length > 0) { $triggers_for_dependencies = zbx_objectValues($triggers_for_dependencies, 'triggerid'); $triggers_for_dependencies = array_flip($triggers_for_dependencies); foreach ($dependencies as $dependency) { $triggers_to_add_dep = array(); $trigger_description = $dependency->getAttribute('description'); $current_triggerid = get_trigger_by_description($trigger_description); // sdi('<b><u>Trigger Description: </u></b>'.$trigger_description.' | <b>Current_triggerid: </b>'. $current_triggerid['triggerid']); if ($current_triggerid && isset($triggers_for_dependencies[$current_triggerid['triggerid']])) { $depends_on_list = $xpath->query('depends', $dependency); foreach ($depends_on_list as $depends_on) { $depends_triggerid = get_trigger_by_description($depends_on->nodeValue); // sdi('<b>depends on description: </b>'.$depends_on->nodeValue.' | <b>depends_triggerid: </b>'. $depends_triggerid['triggerid']); if ($depends_triggerid['triggerid']) { $triggers_to_add_dep[] = $depends_triggerid['triggerid']; } } $r = update_trigger($current_triggerid['triggerid'], null, $current_triggerid['description'], null, null, null, null, null, $triggers_to_add_dep, null); if ($r === false) { throw new APIException(); } } } } } return true; } catch (APIException $e) { error($e->getErrors()); return false; } }