function slideshow_accessible($slideshowid, $perm)
{
    $result = false;
    $sql = 'SELECT s.slideshowid' . ' FROM slideshows s' . ' WHERE s.slideshowid=' . zbx_dbstr($slideshowid) . ' AND ' . DBin_node('s.slideshowid', get_current_nodeid(null, $perm));
    if (DBselect($sql)) {
        $result = true;
        $screenids = array();
        $db_screens = DBselect('SELECT DISTINCT s.screenid' . ' FROM slides s' . ' WHERE s.slideshowid=' . zbx_dbstr($slideshowid));
        while ($slide_data = DBfetch($db_screens)) {
            $screenids[$slide_data['screenid']] = $slide_data['screenid'];
        }
        $options = array('screenids' => $screenids);
        if ($perm == PERM_READ_WRITE) {
            $options['editable'] = true;
        }
        $screens = API::Screen()->get($options);
        $screens = zbx_toHash($screens, 'screenid');
        foreach ($screenids as $screenid) {
            if (!isset($screens[$screenid])) {
                return false;
            }
        }
    }
    return $result;
}
function updateRegexp(array $regexp, array $expressions)
{
    try {
        $regexpId = $regexp['regexpid'];
        unset($regexp['regexpid']);
        // check existence
        if (!getRegexp($regexpId)) {
            throw new Exception(_('Regular expression does not exist.'));
        }
        // check required fields
        $dbFields = array('name' => null);
        if (!check_db_fields($dbFields, $regexp)) {
            throw new Exception(_('Incorrect arguments passed to function') . ' [updateRegexp]');
        }
        // check duplicate name
        $dbRegexp = DBfetch(DBselect('SELECT re.regexpid ' . 'FROM regexps re ' . 'WHERE re.name=' . zbx_dbstr($regexp['name']) . ' AND ' . DBin_node('re.regexpid')));
        if ($dbRegexp && bccomp($regexpId, $dbRegexp['regexpid']) != 0) {
            throw new Exception(_s('Regular expression "%s" already exists.', $regexp['name']));
        }
        rewriteRegexpExpressions($regexpId, $expressions);
        DB::update('regexps', array('values' => $regexp, 'where' => array('regexpid' => $regexpId)));
    } catch (Exception $e) {
        error($e->getMessage());
        return false;
    }
    return true;
}
예제 #3
0
function show_report2_header($config, &$PAGE_GROUPS, &$PAGE_HOSTS)
{
    global $USER_DETAILS;
    $available_groups = $PAGE_GROUPS['groupids'];
    $available_hosts = $PAGE_HOSTS['hostids'];
    //	$available_groups = $PAGE_GROUPS['groupids'];
    //	$available_hosts = $PAGE_HOSTS['hostids'];
    $r_form = new CForm();
    $r_form->setMethod('get');
    $cmbConf = new CComboBox('config', $config, 'submit()');
    $cmbConf->addItem(0, S_BY_HOST);
    $cmbConf->addItem(1, S_BY_TRIGGER_TEMPLATE);
    $r_form->addItem(array(S_MODE . SPACE, $cmbConf, SPACE));
    $cmbGroups = new CComboBox('groupid', $PAGE_GROUPS['selected'], 'javascript: submit();');
    $cmbHosts = new CComboBox('hostid', $PAGE_HOSTS['selected'], 'javascript: submit();');
    foreach ($PAGE_GROUPS['groups'] as $groupid => $name) {
        $cmbGroups->addItem($groupid, get_node_name_by_elid($groupid) . $name);
    }
    foreach ($PAGE_HOSTS['hosts'] as $hostid => $name) {
        $cmbHosts->addItem($hostid, get_node_name_by_elid($hostid) . $name);
    }
    $r_form->addItem(array(S_GROUP . SPACE, $cmbGroups));
    $r_form->addItem(array(SPACE . S_HOST . SPACE, $cmbHosts));
    if (1 == $config) {
        $cmbTrigs = new CComboBox('tpl_triggerid', get_request('tpl_triggerid', 0), 'submit()');
        $cmbHGrps = new CComboBox('hostgroupid', get_request('hostgroupid', 0), 'submit()');
        $cmbTrigs->addItem(0, S_ALL_SMALL);
        $cmbHGrps->addItem(0, S_ALL_SMALL);
    }
    if (0 == $config) {
        show_table_header(S_AVAILABILITY_REPORT_BIG, $r_form);
    } else {
        $sql_cond = ' AND h.hostid=ht.hostid ';
        if ($_REQUEST['hostid'] > 0) {
            $sql_cond .= ' AND ht.templateid=' . $_REQUEST['hostid'];
        }
        if (isset($_REQUEST['tpl_triggerid']) && $_REQUEST['tpl_triggerid'] > 0) {
            $sql_cond .= ' AND t.templateid=' . $_REQUEST['tpl_triggerid'];
        }
        $result = DBselect('SELECT DISTINCT g.groupid,g.name ' . ' FROM triggers t,hosts h,items i,functions f, hosts_templates ht, groups g, hosts_groups hg ' . ' WHERE f.itemid=i.itemid ' . ' AND h.hostid=i.hostid ' . ' AND hg.hostid=h.hostid' . ' AND g.groupid=hg.groupid ' . ' AND ' . DBcondition('h.hostid', $available_hosts) . ' AND t.status=' . TRIGGER_STATUS_ENABLED . ' AND t.triggerid=f.triggerid ' . ' AND ' . DBin_node('t.triggerid') . ' AND i.status=' . ITEM_STATUS_ACTIVE . ' AND h.status=' . HOST_STATUS_MONITORED . $sql_cond . ' ORDER BY g.name');
        while ($row = DBfetch($result)) {
            $cmbHGrps->addItem($row['groupid'], get_node_name_by_elid($row['groupid']) . $row['name']);
        }
        $sql_cond = $_REQUEST['hostid'] > 0 ? ' AND h.hostid=' . $_REQUEST['hostid'] : ' AND ' . DBcondition('h.hostid', $available_hosts);
        $sql = 'SELECT DISTINCT t.triggerid,t.description ' . ' FROM triggers t,hosts h,items i,functions f ' . ' WHERE f.itemid=i.itemid ' . ' AND h.hostid=i.hostid ' . ' AND t.status=' . TRIGGER_STATUS_ENABLED . ' AND t.triggerid=f.triggerid ' . ' AND h.status=' . HOST_STATUS_TEMPLATE . ' AND ' . DBin_node('t.triggerid') . ' AND i.status=' . ITEM_STATUS_ACTIVE . $sql_cond . ' ORDER BY t.description';
        $result = DBselect($sql);
        while ($row = DBfetch($result)) {
            $cmbTrigs->addItem($row['triggerid'], get_node_name_by_elid($row['triggerid']) . expand_trigger_description($row['triggerid']));
        }
        $rr_form = new CForm();
        $rr_form->setMethod('get');
        $rr_form->addVar('config', $config);
        $rr_form->addVar('groupid', $_REQUEST['groupid']);
        $rr_form->addVar('hostid', $_REQUEST['hostid']);
        $rr_form->addItem(array(S_TRIGGER . SPACE, $cmbTrigs, BR(), S_FILTER, SPACE, S_HOST_GROUP . SPACE, $cmbHGrps));
        show_table_header(S_AVAILABILITY_REPORT_BIG, array($r_form, $rr_form));
    }
}
예제 #4
0
 public function bodyToString()
 {
     global $USER_DETAILS;
     $this->cleanItems();
     $total = 0;
     $accessible_hosts = get_accessible_hosts_by_user($USER_DETAILS, PERM_READ_ONLY, PERM_RES_IDS_ARRAY, get_current_nodeid(true));
     $cond_from = '';
     if (remove_nodes_from_id($this->groupid) > 0) {
         $cond_from = ', hosts_groups hg ';
         $cond_where = 'AND hg.hostid=h.hostid AND hg.groupid=' . $this->groupid;
     } else {
         $cond_where = ' AND ' . DBin_node('h.hostid', $this->nodeid);
     }
     $db_host_cnt = DBselect('SELECT COUNT(DISTINCT h.hostid) as cnt ' . ' FROM hosts h' . $cond_from . ' WHERE h.available=' . HOST_AVAILABLE_TRUE . ' AND h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ') ' . ' AND ' . DBcondition('h.hostid', $accessible_hosts) . $cond_where);
     $host_cnt = DBfetch($db_host_cnt);
     $avail = $host_cnt['cnt'];
     $total += $host_cnt['cnt'];
     $db_host_cnt = DBselect('SELECT COUNT(DISTINCT h.hostid) as cnt ' . ' FROM hosts h' . $cond_from . ' WHERE h.available=' . HOST_AVAILABLE_FALSE . ' AND h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ') ' . ' AND ' . DBcondition('h.hostid', $accessible_hosts) . $cond_where);
     $host_cnt = DBfetch($db_host_cnt);
     $notav = $host_cnt['cnt'];
     $total += $host_cnt['cnt'];
     $db_host_cnt = DBselect('SELECT COUNT(DISTINCT h.hostid) as cnt ' . ' FROM hosts h' . $cond_from . ' WHERE h.available=' . HOST_AVAILABLE_UNKNOWN . ' AND h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ') ' . ' AND ' . DBcondition('h.hostid', $accessible_hosts) . $cond_where);
     $host_cnt = DBfetch($db_host_cnt);
     $uncn = $host_cnt['cnt'];
     $total += $host_cnt['cnt'];
     $node = get_node_by_nodeid($this->nodeid);
     $header_str = S_HOSTS_INFO . SPACE;
     $header_str .= S_FOR_GROUP_SMALL . SPACE . '"';
     if ($node > 0) {
         $header_str .= '(' . $node['name'] . ')' . SPACE;
     }
     if (remove_nodes_from_id($this->groupid) > 0) {
         $group = get_hostgroup_by_groupid($this->groupid);
         $header_str .= $group['name'] . '"';
     } else {
         $header_str .= S_ALL_S . '"';
     }
     $header = new CCol($header_str, "header");
     if ($this->style == STYLE_HORISONTAL) {
         $header->SetColspan(4);
     }
     $this->addRow($header);
     $avail = new CCol($avail . '  ' . S_AVAILABLE, 'avail');
     $notav = new CCol($notav . '  ' . S_NOT_AVAILABLE, 'notav');
     $uncn = new CCol($uncn . '  ' . S_UNKNOWN, 'uncn');
     $total = new CCol($total . '  ' . S_TOTAL, 'total');
     if ($this->style == STYLE_HORISONTAL) {
         $this->addRow(array($avail, $notav, $uncn, $total));
     } else {
         $this->addRow($avail);
         $this->addRow($notav);
         $this->addRow($uncn);
         $this->addRow($total);
     }
     return parent::bodyToString();
 }
 public function bodyToString()
 {
     $this->cleanItems();
     $total = 0;
     // fetch accessible host ids
     $hosts = API::Host()->get(array('nodeids' => get_current_nodeid(true), 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => true));
     $hostIds = array_keys($hosts);
     $cond_from = '';
     if (remove_nodes_from_id($this->groupid) > 0) {
         $cond_from = ', hosts_groups hg ';
         $cond_where = 'AND hg.hostid=h.hostid AND hg.groupid=' . zbx_dbstr($this->groupid);
     } else {
         $cond_where = ' AND ' . DBin_node('h.hostid', $this->nodeid);
     }
     $db_host_cnt = DBselect('SELECT COUNT(DISTINCT h.hostid) AS cnt' . ' FROM hosts h' . $cond_from . ' WHERE h.available=' . HOST_AVAILABLE_TRUE . ' AND h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')' . ' AND ' . dbConditionInt('h.hostid', $hostIds) . $cond_where);
     $host_cnt = DBfetch($db_host_cnt);
     $avail = $host_cnt['cnt'];
     $total += $host_cnt['cnt'];
     $db_host_cnt = DBselect('SELECT COUNT(DISTINCT h.hostid) AS cnt' . ' FROM hosts h' . $cond_from . ' WHERE h.available=' . HOST_AVAILABLE_FALSE . ' AND h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')' . ' AND ' . dbConditionInt('h.hostid', $hostIds) . $cond_where);
     $host_cnt = DBfetch($db_host_cnt);
     $notav = $host_cnt['cnt'];
     $total += $host_cnt['cnt'];
     $db_host_cnt = DBselect('SELECT COUNT(DISTINCT h.hostid) AS cnt' . ' FROM hosts h' . $cond_from . ' WHERE h.available=' . HOST_AVAILABLE_UNKNOWN . ' AND h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')' . ' AND ' . dbConditionInt('h.hostid', $hostIds) . $cond_where);
     $host_cnt = DBfetch($db_host_cnt);
     $uncn = $host_cnt['cnt'];
     $total += $host_cnt['cnt'];
     $node = get_node_by_nodeid($this->nodeid);
     $header_str = _('Hosts info') . SPACE;
     if ($node > 0) {
         $header_str .= '(' . $node['name'] . ')' . SPACE;
     }
     if (remove_nodes_from_id($this->groupid) > 0) {
         $group = get_hostgroup_by_groupid($this->groupid);
         $header_str .= _('Group') . SPACE . '"' . $group['name'] . '"';
     } else {
         $header_str .= _('All groups');
     }
     $header = new CCol($header_str, 'header');
     if ($this->style == STYLE_HORISONTAL) {
         $header->setColspan(4);
     }
     $this->addRow($header);
     $avail = new CCol($avail . '  ' . _('Available'), 'avail');
     $notav = new CCol($notav . '  ' . _('Not available'), 'notav');
     $uncn = new CCol($uncn . '  ' . _('Unknown'), 'uncn');
     $total = new CCol($total . '  ' . _('Total'), 'total');
     if ($this->style == STYLE_HORISONTAL) {
         $this->addRow(array($avail, $notav, $uncn, $total));
     } else {
         $this->addRow($avail);
         $this->addRow($notav);
         $this->addRow($uncn);
         $this->addRow($total);
     }
     return parent::bodyToString();
 }
function get_userid_by_usrgrpid($usrgrpids)
{
    zbx_value2array($usrgrpids);
    $userids = array();
    $db_users = DBselect('SELECT DISTINCT u.userid' . ' FROM users u,users_groups ug' . ' WHERE u.userid=ug.userid' . ' AND ' . dbConditionInt('ug.usrgrpid', $usrgrpids) . ' AND ' . DBin_node('ug.usrgrpid', false));
    while ($user = DBFetch($db_users)) {
        $userids[$user['userid']] = $user['userid'];
    }
    return $userids;
}
예제 #7
0
function get_default_image($image = false, $imagetype = IMAGE_TYPE_ICON)
{
    if ($image) {
        $image = imagecreate(50, 50);
        $color = imagecolorallocate($image, 250, 50, 50);
        imagefill($image, 0, 0, $color);
    } else {
        $sql = 'SELECT i.imageid ' . ' FROM images i ' . ' WHERE ' . DBin_node('i.imageid', false) . ' AND imagetype=' . $imagetype . ' ORDER BY name ASC';
        $result = DBselect($sql, 1);
        if ($image = DBfetch($result)) {
            return $image;
        } else {
            $image = array();
            $image['imageid'] = 0;
        }
    }
    return $image;
}
예제 #8
0
function sysmap_accessible($sysmapid, $perm)
{
    global $USER_DETAILS;
    $nodes = get_current_nodeid(null, $perm);
    $result = (bool) count($nodes);
    $sql = 'SELECT * ' . ' FROM sysmaps_elements ' . ' WHERE sysmapid=' . $sysmapid . ' AND ' . DBin_node('sysmapid', $nodes);
    $db_result = DBselect($sql);
    $available_hosts = get_accessible_hosts_by_user($USER_DETAILS, $perm, PERM_RES_IDS_ARRAY, get_current_nodeid(true));
    //SDI($available_hosts);
    while (($se_data = DBfetch($db_result)) && $result) {
        switch ($se_data['elementtype']) {
            case SYSMAP_ELEMENT_TYPE_HOST:
                if (!isset($available_hosts[$se_data['elementid']])) {
                    $result = false;
                }
                break;
            case SYSMAP_ELEMENT_TYPE_MAP:
                $result = sysmap_accessible($se_data['elementid'], $perm);
                break;
            case SYSMAP_ELEMENT_TYPE_TRIGGER:
                $available_triggers = get_accessible_triggers($perm, array(), PERM_RES_IDS_ARRAY);
                if (!isset($available_triggers[$se_data['elementid']])) {
                    $result = false;
                }
                break;
            case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
                $available_groups = get_accessible_groups_by_user($USER_DETAILS, $perm);
                if (!isset($available_groups[$se_data['elementid']])) {
                    $result = false;
                }
                break;
        }
    }
    //SDI($se_data['elementid']);
    return $result;
}
예제 #9
0
 /**
  * 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;
 }
예제 #10
0
function update_user_group($usrgrpid, $name, $users_status, $gui_access, $api_access, $debug_mode, $users = array(), $rights = array())
{
    global $USER_DETAILS;
    $sql = 'SELECT * ' . ' FROM usrgrp ' . ' WHERE name=' . zbx_dbstr($name) . ' AND usrgrpid<>' . $usrgrpid . ' AND ' . DBin_node('usrgrpid', get_current_nodeid(false));
    if (DBfetch(DBselect($sql))) {
        error("Group '{$name}' already exists");
        return 0;
    }
    $result = DBexecute('UPDATE usrgrp SET name=' . zbx_dbstr($name) . ' WHERE usrgrpid=' . $usrgrpid);
    if (!$result) {
        return $result;
    }
    // must come before adding user to group
    $result &= change_group_status($usrgrpid, $users_status);
    $result &= change_group_gui_access($usrgrpid, $gui_access);
    $result &= change_group_api_access($usrgrpid, $api_access);
    $result &= change_group_debug_mode($usrgrpid, $debug_mode);
    if (!$result) {
        return $result;
    }
    //-------
    $grant = true;
    if ($gui_access == GROUP_GUI_ACCESS_DISABLED || $users_status == GROUP_STATUS_DISABLED) {
        $grant = !uint_in_array($USER_DETAILS['userid'], $users);
    }
    if ($grant) {
        $result = DBexecute('DELETE FROM users_groups WHERE usrgrpid=' . $usrgrpid);
        foreach ($users as $userid => $name) {
            $result = add_user_to_group($userid, $usrgrpid);
            if (!$result) {
                return $result;
            }
        }
    } else {
        error(S_USER_CANNOT_DISABLE_ITSELF);
        return false;
    }
    $result = DBexecute('DELETE FROM rights WHERE groupid=' . $usrgrpid);
    foreach ($rights as $right) {
        $id = get_dbid('rights', 'rightid');
        $result = DBexecute('INSERT INTO rights (rightid,groupid,permission,id)' . ' VALUES (' . $id . ',' . $usrgrpid . ',' . $right['permission'] . ',' . $right['id'] . ')');
        if (!$result) {
            return $result;
        }
    }
    return $result;
}
예제 #11
0
}
$form = new CForm();
$form->SetMethod('get');
$form->AddItem(new CButton("form", S_CREATE_MEDIA_TYPE));
$row_count = 0;
$numrows = new CSpan(null, 'info');
$numrows->addOption('name', 'numrows');
$header = get_table_header(array(S_CONFIGURATION_OF_MEDIA_TYPES_BIG, new CSpan(SPACE . SPACE . '|' . SPACE . SPACE, 'divider'), S_FOUND . ': ', $numrows));
show_table_header($header, $form);
if (isset($_REQUEST["form"])) {
    echo SBR;
    insert_media_type_form();
} else {
    $table = new CTableInfo(S_NO_MEDIA_TYPES_DEFINED);
    $table->setHeader(array(make_sorting_link(S_TYPE, 'mt.type'), make_sorting_link(S_DESCRIPTION, 'mt.description'), S_DETAILS));
    $result = DBselect('SELECT mt.* ' . ' FROM media_type mt' . ' WHERE ' . DBin_node('mt.mediatypeid') . order_by('mt.type,mt.description'));
    while ($row = DBfetch($result)) {
        switch ($row['type']) {
            case MEDIA_TYPE_EMAIL:
                $details = S_SMTP_SERVER . ": '" . $row['smtp_server'] . "', " . S_SMTP_HELO . ": '" . $row['smtp_helo'] . "', " . S_SMTP_EMAIL . ": '" . $row['smtp_email'] . "'";
                break;
            case MEDIA_TYPE_EXEC:
                $details = S_SCRIPT_NAME . ": '" . $row['exec_path'] . "'";
                break;
            case MEDIA_TYPE_SMS:
                $details = S_GSM_MODEM . ": '" . $row['gsm_modem'] . "'";
                break;
            case MEDIA_TYPE_JABBER:
                $details = S_JABBER_IDENTIFIER . ": '" . $row['username'] . "'";
                break;
            default:
예제 #12
0
//		VAR			TYPE	OPTIONAL FLAGS	VALIDATION	EXCEPTION
$fields = array("msg" => array(T_ZBX_STR, O_OPT, null, null, NULL), 'favobj' => array(T_ZBX_STR, O_OPT, P_ACT, IN("'hat'"), NULL), 'favid' => array(T_ZBX_STR, O_OPT, P_ACT, NOT_EMPTY, 'isset({favobj})'), 'state' => array(T_ZBX_INT, O_OPT, P_ACT, NOT_EMPTY, 'isset({favobj})'));
check_fields($fields);
/* AJAX */
if (isset($_REQUEST['favobj'])) {
    if ('hat' == $_REQUEST['favobj']) {
        update_profile('web.services.hats.' . $_REQUEST['favid'] . '.state', $_REQUEST['state'], PROFILE_TYPE_INT);
    }
}
if (PAGE_TYPE_JS == $page['type'] || PAGE_TYPE_HTML_BLOCK == $page['type']) {
    exit;
}
//--------
//--------------------------------------------------------------------------
$available_triggers = get_accessible_triggers(PERM_READ_ONLY, array(), PERM_RES_IDS_ARRAY);
$query = 'SELECT DISTINCT s.serviceid, sl.servicedownid, sl_p.serviceupid as serviceupid, s.triggerid, ' . ' s.name as caption, s.algorithm, t.description, t.expression, s.sortorder, sl.linkid, s.showsla, s.goodsla, s.status ' . ' FROM services s ' . ' LEFT JOIN triggers t ON s.triggerid = t.triggerid ' . ' LEFT JOIN services_links sl ON  s.serviceid = sl.serviceupid and NOT(sl.soft=0) ' . ' LEFT JOIN services_links sl_p ON  s.serviceid = sl_p.servicedownid and sl_p.soft=0 ' . ' WHERE ' . DBin_node('s.serviceid') . ' AND (t.triggerid IS NULL OR ' . DBcondition('t.triggerid', $available_triggers) . ') ' . ' ORDER BY s.sortorder, sl_p.serviceupid, s.serviceid';
$result = DBSelect($query);
$services = array();
$row = array('id' => 0, 'serviceid' => 0, 'serviceupid' => 0, 'caption' => S_ROOT_SMALL, 'status' => SPACE, 'algorithm' => SPACE, 'description' => SPACE, 'soft' => 0, 'linkid' => '');
$services[0] = $row;
while ($row = DBFetch($result)) {
    $row['id'] = $row['serviceid'];
    empty($row['serviceupid']) ? $row['serviceupid'] = '0' : '';
    empty($row['triggerid']) ? $row['description'] = 'None' : ($row['description'] = expand_trigger_description($row['triggerid']));
    if (isset($services[$row['serviceid']])) {
        $services[$row['serviceid']] = array_merge($services[$row['serviceid']], $row);
    } else {
        $services[$row['serviceid']] = $row;
    }
    if (isset($row['serviceupid'])) {
        $services[$row['serviceupid']]['childs'][] = array('id' => $row['serviceid'], 'soft' => 0, 'linkid' => 0);
예제 #13
0
function update_config($configs)
{
    $update = array();
    if (isset($configs['work_period']) && !is_null($configs['work_period'])) {
        if (!validate_period($configs['work_period'])) {
            error(S_ICORRECT_WORK_PERIOD);
            return NULL;
        }
    }
    if (isset($configs['alert_usrgrpid']) && !is_null($configs['alert_usrgrpid'])) {
        if ($configs['alert_usrgrpid'] != 0 && !DBfetch(DBselect('select usrgrpid from usrgrp where usrgrpid=' . $configs['alert_usrgrpid']))) {
            error(S_INCORRECT_GROUP);
            return NULL;
        }
    }
    foreach ($configs as $key => $value) {
        if (!is_null($value)) {
            $update[] = $key . '=' . zbx_dbstr($value);
        }
    }
    if (count($update) == 0) {
        error(S_NOTHING_TO_DO);
        return NULL;
    }
    return DBexecute('update config set ' . implode(',', $update) . ' where ' . DBin_node('configid', get_current_nodeid(false)));
}
예제 #14
0
 $services[0] = $row;
 $now = time();
 while ($row = DBFetch($result)) {
     $row['id'] = $row['serviceid'];
     empty($row['serviceupid']) ? $row['serviceupid'] = '0' : '';
     empty($row['description']) ? $row['description'] = 'None' : '';
     $row['graph'] = new CLink(S_SHOW, "srv_status.php?serviceid=" . $row["serviceid"] . "&showgraph=1" . url_param('path'), "action");
     if (isset($row["triggerid"]) && !empty($row["triggerid"])) {
         $url = new CLink(expand_trigger_description($row['triggerid']), 'events.php?triggerid=' . $row['triggerid']);
         $row['caption'] = array($row['caption'] . ' [', $url, ']');
     }
     if ($row["status"] == 0 || isset($service) && bccomp($service["serviceid"], $row["serviceid"]) == 0) {
         $row['reason'] = '-';
     } else {
         $row['reason'] = '-';
         $result2 = DBselect('SELECT s.triggerid,s.serviceid ' . ' FROM services s, triggers t ' . ' WHERE s.status>0 ' . ' AND s.triggerid is not NULL ' . ' AND t.triggerid=s.triggerid ' . ' AND ' . DBcondition('t.triggerid', $available_triggers) . ' AND ' . DBin_node('s.serviceid') . ' ORDER BY s.status DESC, t.description');
         while ($row2 = DBfetch($result2)) {
             if (is_string($row['reason']) && $row['reason'] == '-') {
                 $row['reason'] = new CList(null, "itservices");
             }
             if (does_service_depend_on_the_service($row["serviceid"], $row2["serviceid"])) {
                 $row['reason']->AddItem(new CLink(expand_trigger_description($row2["triggerid"]), "events.php?triggerid=" . $row2["triggerid"]));
             }
         }
     }
     if ($row["showsla"] == 1) {
         $row['sla'] = new CLink(new CImg("chart_sla.php?serviceid=" . $row["serviceid"]), "report3.php?serviceid=" . $row["serviceid"] . "&year=" . date("Y"));
         $now = time(NULL);
         $period_start = $now - 7 * 24 * 3600;
         $period_end = $now;
         $stat = calculate_service_availability($row["serviceid"], $period_start, $period_end);
예제 #15
0
 $header = get_table_header(array(S_MAINTENANCE_PERIODS, new CSpan(SPACE . SPACE . '|' . SPACE . SPACE, 'divider'), S_FOUND . ': ', $numrows));
 show_table_header($header, $form);
 // ----
 $available_maintenances = get_accessible_maintenance_by_user(PERM_READ_ONLY);
 $sqls = array();
 $config = select_config();
 $maintenances = array();
 $maintenanceids = array();
 if ($_REQUEST['groupid'] > 0) {
     $sqls[] = 'SELECT m.* ' . ' FROM maintenances m, maintenances_groups mg ' . ' WHERE ' . DBin_node('m.maintenanceid') . ' AND ' . DBcondition('m.maintenanceid', $available_maintenances) . ' AND mg.groupid=' . $_REQUEST['groupid'] . ' AND m.maintenanceid=mg.maintenanceid ';
     ' ORDER BY m.name';
     $sqls[] = 'SELECT m.* ' . ' FROM maintenances m, maintenances_hosts mh, hosts_groups hg ' . ' WHERE ' . DBin_node('m.maintenanceid') . ' AND ' . DBcondition('m.maintenanceid', $available_maintenances) . ' AND hg.groupid=' . $_REQUEST['groupid'] . ' AND mh.hostid=hg.hostid ' . ' AND m.maintenanceid=mh.maintenanceid ';
     ' ORDER BY m.name';
 } else {
     if ($config['dropdown_first_entry'] == ZBX_DROPDOWN_FIRST_ALL) {
         $sqls[] = 'SELECT m.* ' . ' FROM maintenances m ' . ' WHERE ' . DBin_node('m.maintenanceid') . ' AND ' . DBcondition('m.maintenanceid', $available_maintenances) . ' ORDER BY m.name';
     }
 }
 foreach ($sqls as $num => $sql) {
     $db_maintenances = DBselect($sql);
     while ($maintenance = DBfetch($db_maintenances)) {
         $maintenances[$maintenance['maintenanceid']] = $maintenance;
         $maintenanceids[$maintenance['maintenanceid']] = $maintenance['maintenanceid'];
     }
 }
 $form = new CForm(null, 'post');
 $form->setName('maintenances');
 $table = new CTableInfo();
 $table->setHeader(array(new CCheckBox('all_maintenances', NULL, "checkAll('" . $form->GetName() . "','all_maintenances','maintenanceids');"), make_sorting_link(S_NAME, 'm.name'), S_TYPE, S_STATUS, S_DESCRIPTION));
 foreach ($maintenances as $maintenanceid => $maintenance) {
     if ($maintenance['active_till'] < time()) {
예제 #16
0
파일: sysmaps.php 프로젝트: phedders/zabbix
$form->SetMethod('get');
$form->AddItem(new CButton("form", S_CREATE_MAP));
show_table_header(S_CONFIGURATION_OF_NETWORK_MAPS, $form);
echo SBR;
if (isset($_REQUEST["form"])) {
    insert_map_form();
} else {
    $form = new CForm();
    $form->setName('frm_maps');
    $numrows = new CSpan(null, 'info');
    $numrows->setAttribute('name', 'numrows');
    $header = get_table_header(array(S_MAPS_BIG, new CSpan(SPACE . SPACE . '|' . SPACE . SPACE, 'divider'), S_FOUND . ': ', $numrows));
    show_table_header($header);
    $table = new CTableInfo(S_NO_MAPS_DEFINED);
    $table->SetHeader(array(new CCheckBox('all_maps', NULL, "checkAll('" . $form->getName() . "','all_maps','maps');"), make_sorting_link(S_NAME, 'sm.name'), make_sorting_link(S_WIDTH, 'sm.width'), make_sorting_link(S_HEIGHT, 'sm.height'), S_MAP));
    $result = DBselect('SELECT sm.sysmapid,sm.name,sm.width,sm.height ' . ' FROM sysmaps sm' . ' WHERE ' . DBin_node('sm.sysmapid') . order_by('sm.name,sm.width,sm.height', 'sm.sysmapid'));
    while ($row = DBfetch($result)) {
        if (!sysmap_accessible($row["sysmapid"], PERM_READ_WRITE)) {
            continue;
        }
        $table->AddRow(array(new CCheckBox('maps[' . $row['sysmapid'] . ']', NULL, NULL, $row['sysmapid']), new CLink($row["name"], "sysmaps.php?form=update" . "&sysmapid=" . $row["sysmapid"] . "#form", 'action'), $row["width"], $row["height"], new CLink(S_EDIT, "sysmap.php?sysmapid=" . $row["sysmapid"])));
    }
    //----- GO ------
    $goBox = new CComboBox('go');
    $goBox->addItem('delete', S_DELETE_SELECTED);
    // goButton name is necessary!!!
    $goButton = new CButton('goButton', S_GO . ' (0)');
    $goButton->setAttribute('id', 'goButton');
    zbx_add_post_js('chkbxRange.pageGoName = "maps";');
    $table->setFooter(new CCol(array($goBox, $goButton)));
    $form->addItem($table);
 /**
  * Get GraphPrototype data
  *
  * @param array $options
  * @return array
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('graphid', 'name', 'graphtype');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     $sqlParts = array('select' => array('graphs' => 'g.graphid'), 'from' => array('graphs' => 'graphs g'), 'where' => array('g.flags=' . ZBX_FLAG_DISCOVERY_CHILD), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'graphids' => null, 'itemids' => null, 'discoveryids' => null, 'type' => null, 'templated' => null, 'inherited' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectGroups' => null, 'selectTemplates' => null, 'selectHosts' => null, 'selectItems' => null, 'selectGraphItems' => null, 'selectDiscoveryRule' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['graphs']);
         $dbTable = DB::getSchema('graphs');
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 'g.' . $field;
             }
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM graphs_items gi,items i,hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE g.graphid=gi.graphid' . ' AND gi.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY gi.graphid' . ' HAVING MIN(r.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) {
             $sqlParts['select']['groupid'] = 'hg.groupid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
         $sqlParts['where'][] = 'hg.hostid=i.hostid';
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['hostid'] = 'i.hostid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['where'][] = dbConditionInt('i.hostid', $options['hostids']);
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         $sqlParts['where'][] = dbConditionInt('g.graphid', $options['graphids']);
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['itemid'] = 'gi.itemid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where'][] = dbConditionInt('gi.itemid', $options['itemids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['gi'] = 'gi.itemid';
         }
     }
     // discoveryids
     if (!is_null($options['discoveryids'])) {
         zbx_value2array($options['discoveryids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['itemid'] = 'id.parent_itemid';
         }
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['item_discovery'] = 'item_discovery id';
         $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
         $sqlParts['where']['giid'] = 'gi.itemid=id.itemid';
         $sqlParts['where'][] = dbConditionInt('id.parent_itemid', $options['discoveryids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['id'] = 'id.parent_itemid';
         }
     }
     // type
     if (!is_null($options['type'])) {
         $sqlParts['where'][] = 'g.type=' . zbx_dbstr($options['type']);
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['graphs_items'] = 'graphs_items gi';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
         $sqlParts['where']['ggi'] = 'g.graphid=gi.graphid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 'g.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 'g.templateid IS NULL';
         }
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['graphs'] = 'g.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('count(DISTINCT g.graphid) as rowscount');
         // groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('graphs g', $options, $sqlParts);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('graphs g', $options, $sqlParts);
         if (isset($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['graphs_items'] = 'graphs_items gi';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
             $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['host'] = dbConditionString('h.host', $options['filter']['host']);
         }
         if (isset($options['filter']['hostid'])) {
             zbx_value2array($options['filter']['hostid']);
             $sqlParts['from']['graphs_items'] = 'graphs_items gi';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['where']['gig'] = 'gi.graphid=g.graphid';
             $sqlParts['where']['igi'] = 'i.itemid=gi.itemid';
             $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['filter']['hostid']);
         }
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'g');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $graphids = array();
     $sqlParts['select'] = array_unique($sqlParts['select']);
     $sqlParts['from'] = array_unique($sqlParts['from']);
     $sqlParts['where'] = array_unique($sqlParts['where']);
     $sqlParts['group'] = array_unique($sqlParts['group']);
     $sqlParts['order'] = array_unique($sqlParts['order']);
     $sqlSelect = '';
     $sqlFrom = '';
     $sqlWhere = '';
     $sqlGroup = '';
     $sqlOrder = '';
     if (!empty($sqlParts['select'])) {
         $sqlSelect .= implode(',', $sqlParts['select']);
     }
     if (!empty($sqlParts['from'])) {
         $sqlFrom .= implode(',', $sqlParts['from']);
     }
     if (!empty($sqlParts['where'])) {
         $sqlWhere .= ' AND ' . implode(' AND ', $sqlParts['where']);
     }
     if (!empty($sqlParts['group'])) {
         $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']);
     }
     if (!empty($sqlParts['order'])) {
         $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']);
     }
     $sqlLimit = $sqlParts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('g.graphid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder;
     $dbRes = DBselect($sql, $sqlLimit);
     while ($graph = DBfetch($dbRes)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $graph;
             } else {
                 $result = $graph['rowscount'];
             }
         } else {
             $graphids[$graph['graphid']] = $graph['graphid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$graph['graphid']] = array('graphid' => $graph['graphid']);
             } else {
                 if (!isset($result[$graph['graphid']])) {
                     $result[$graph['graphid']] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$graph['graphid']]['hosts'])) {
                     $result[$graph['graphid']]['hosts'] = array();
                 }
                 if (!is_null($options['selectGraphItems']) && !isset($result[$graph['graphid']]['gitems'])) {
                     $result[$graph['graphid']]['gitems'] = array();
                 }
                 if (!is_null($options['selectTemplates']) && !isset($result[$graph['graphid']]['templates'])) {
                     $result[$graph['graphid']]['templates'] = array();
                 }
                 if (!is_null($options['selectItems']) && !isset($result[$graph['graphid']]['items'])) {
                     $result[$graph['graphid']]['items'] = array();
                 }
                 if (!is_null($options['selectDiscoveryRule']) && !isset($result[$graph['graphid']]['discoveryRule'])) {
                     $result[$graph['graphid']]['discoveryRule'] = array();
                 }
                 // hostids
                 if (isset($graph['hostid']) && is_null($options['selectHosts'])) {
                     if (!isset($result[$graph['graphid']]['hosts'])) {
                         $result[$graph['graphid']]['hosts'] = array();
                     }
                     $result[$graph['graphid']]['hosts'][] = array('hostid' => $graph['hostid']);
                     unset($graph['hostid']);
                 }
                 // itemids
                 if (isset($graph['itemid']) && is_null($options['selectItems'])) {
                     if (!isset($result[$graph['graphid']]['items'])) {
                         $result[$graph['graphid']]['items'] = array();
                     }
                     $result[$graph['graphid']]['items'][] = array('itemid' => $graph['itemid']);
                     unset($graph['itemid']);
                 }
                 $result[$graph['graphid']] += $graph;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // adding GraphItems
     if (!is_null($options['selectGraphItems']) && str_in_array($options['selectGraphItems'], $subselectsAllowedOutputs)) {
         $gitems = API::GraphItem()->get(array('nodeids' => $nodeids, 'output' => $options['selectGraphItems'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
         foreach ($gitems as $gitem) {
             $ggraphs = $gitem['graphs'];
             unset($gitem['graphs']);
             foreach ($ggraphs as $graph) {
                 $result[$graph['graphid']]['gitems'][$gitem['gitemid']] = $gitem;
             }
         }
     }
     // adding Hostgroups
     if (!is_null($options['selectGroups'])) {
         if (is_array($options['selectGroups']) || str_in_array($options['selectGroups'], $subselectsAllowedOutputs)) {
             $groups = API::HostGroup()->get(array('nodeids' => $nodeids, 'output' => $options['selectGroups'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
             foreach ($groups as $group) {
                 $ggraphs = $group['graphs'];
                 unset($group['graphs']);
                 foreach ($ggraphs as $graph) {
                     $result[$graph['graphid']]['groups'][] = $group;
                 }
             }
         }
     }
     // adding Hosts
     if (!is_null($options['selectHosts'])) {
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) {
             $hosts = API::Host()->get(array('nodeids' => $nodeids, 'output' => $options['selectHosts'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
             foreach ($hosts as $host) {
                 $hgraphs = $host['graphs'];
                 unset($host['graphs']);
                 foreach ($hgraphs as $graph) {
                     $result[$graph['graphid']]['hosts'][] = $host;
                 }
             }
         }
     }
     // adding Templates
     if (!is_null($options['selectTemplates']) && str_in_array($options['selectTemplates'], $subselectsAllowedOutputs)) {
         $templates = API::Template()->get(array('nodeids' => $nodeids, 'output' => $options['selectTemplates'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
         foreach ($templates as $template) {
             $tgraphs = $template['graphs'];
             unset($template['graphs']);
             foreach ($tgraphs as $graph) {
                 $result[$graph['graphid']]['templates'][] = $template;
             }
         }
     }
     // adding Items
     if (!is_null($options['selectItems']) && str_in_array($options['selectItems'], $subselectsAllowedOutputs)) {
         $items = API::Item()->get(array('nodeids' => $nodeids, 'output' => $options['selectItems'], 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true, 'filter' => array('flags' => null)));
         foreach ($items as $item) {
             $igraphs = $item['graphs'];
             unset($item['graphs']);
             foreach ($igraphs as $graph) {
                 $result[$graph['graphid']]['items'][] = $item;
             }
         }
     }
     // adding discoveryRule
     if (!is_null($options['selectDiscoveryRule'])) {
         $ruleids = $ruleMap = array();
         $dbRules = DBselect('SELECT id.parent_itemid,gi.graphid' . ' FROM item_discovery id,graphs_items gi' . ' WHERE ' . dbConditionInt('gi.graphid', $graphids) . ' AND gi.itemid=id.itemid');
         while ($rule = DBfetch($dbRules)) {
             $ruleids[$rule['parent_itemid']] = $rule['parent_itemid'];
             $ruleMap[$rule['graphid']] = $rule['parent_itemid'];
         }
         $objParams = array('nodeids' => $nodeids, 'itemids' => $ruleids, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectDiscoveryRule']) || str_in_array($options['selectDiscoveryRule'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDiscoveryRule'];
             $discoveryRules = API::DiscoveryRule()->get($objParams);
             foreach ($result as $graphid => $graph) {
                 if (isset($ruleMap[$graphid]) && isset($discoveryRules[$ruleMap[$graphid]])) {
                     $result[$graphid]['discoveryRule'] = $discoveryRules[$ruleMap[$graphid]];
                 }
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
예제 #18
0
파일: users.php 프로젝트: rennhak/zabbix
        set_users_jsmenu_array();
    }
} else {
    if ($_REQUEST['config'] == 1) {
        // USER GROUPS
        if (isset($_REQUEST['form'])) {
            insert_usergroups_form();
        } else {
            $numrows = new CSpan(null, 'info');
            $numrows->addOption('name', 'numrows');
            $header = get_table_header(array(S_USER_GROUPS_BIG, new CSpan(SPACE . SPACE . '|' . SPACE . SPACE, 'divider'), S_FOUND . ': ', $numrows));
            show_table_header($header);
            $form = new CForm();
            $table = new CTableInfo(S_NO_USER_GROUPS_DEFINED);
            $table->setHeader(array(S_USERS_STATUS, S_GUI_ACCESS, array(new CCheckBox('all_groups', NULL, "CheckAll('" . $form->GetName() . "','all_groups');"), make_sorting_link(S_NAME, 'ug.name')), S_MEMBERS));
            $result = DBselect('SELECT ug.usrgrpid, ug.name, ug.users_status, ug.gui_access ' . ' FROM usrgrp ug' . ' WHERE ' . DBin_node('ug.usrgrpid') . order_by('ug.name'));
            while ($row = DBfetch($result)) {
                $users = array();
                $users_id = array();
                $db_users = DBselect('SELECT DISTINCT u.alias,u.userid ' . ' FROM users u,users_groups ug ' . ' WHERE u.userid=ug.userid ' . ' AND ug.usrgrpid=' . $row['usrgrpid'] . ' ORDER BY u.alias');
                while ($db_user = DBfetch($db_users)) {
                    if (!empty($users)) {
                        $users[$db_user['userid']][] = ', ';
                    } else {
                        $users[$db_user['userid']] = array();
                    }
                    $users[$db_user['userid']][] = new Clink($db_user['alias'], 'users.php?form=update&config=0&userid=' . $db_user['userid'] . '#form');
                }
                $gui_access = user_auth_type2str($row['gui_access']);
                $users_status = $row['users_status'] == GROUP_STATUS_ENABLED ? S_ENABLED : S_DISABLED;
                if (granted2update_group($row['usrgrpid'])) {
예제 #19
0
function get_viewed_hosts($perm, $groupid = 0, $options = array(), $nodeid = null, $sql = array())
{
    global $USER_DETAILS;
    global $page;
    $userid = $USER_DETAILS['userid'];
    $def_sql = array('select' => array('h.hostid', 'h.host'), 'from' => array('hosts h'), 'where' => array(), 'order' => array());
    $def_options = array('deny_all' => 0, 'allow_all' => 0, 'select_first_host' => 0, 'select_first_host_if_empty' => 0, 'select_host_on_group_switch' => 0, 'do_not_select' => 0, 'do_not_select_if_empty' => 0, 'monitored_hosts' => 0, 'templated_hosts' => 0, 'real_hosts' => 0, 'not_proxy_hosts' => 0, 'with_items' => 0, 'with_monitored_items' => 0, 'with_historical_items' => 0, 'with_triggers' => 0, 'with_monitored_triggers' => 0, 'with_httptests' => 0, 'with_monitored_httptests' => 0, 'with_graphs' => 0, 'only_current_node' => 0);
    $def_options = zbx_array_merge($def_options, $options);
    $config = select_config();
    $dd_first_entry = $config['dropdown_first_entry'];
    if ($def_options['allow_all']) {
        $dd_first_entry = ZBX_DROPDOWN_FIRST_ALL;
    }
    if ($def_options['deny_all']) {
        $dd_first_entry = ZBX_DROPDOWN_FIRST_NONE;
    }
    if ($dd_first_entry == ZBX_DROPDOWN_FIRST_ALL) {
        $def_options['select_host_on_group_switch'] = 1;
    }
    $result = array('original' => -1, 'selected' => 0, 'hosts' => array(), 'hostids' => array());
    $hosts =& $result['hosts'];
    $hostids =& $result['hostids'];
    $first_entry = $dd_first_entry == ZBX_DROPDOWN_FIRST_NONE ? S_NOT_SELECTED_SMALL : S_ALL_SMALL;
    $hosts['0'] = $first_entry;
    if (!is_array($groupid) && $groupid == 0) {
        if ($dd_first_entry == ZBX_DROPDOWN_FIRST_NONE) {
            return $result;
        }
    } else {
        zbx_value2array($groupid);
        $def_sql['from'][] = 'hosts_groups hg';
        $def_sql['where'][] = DBcondition('hg.groupid', $groupid);
        $def_sql['where'][] = 'hg.hostid=h.hostid';
    }
    $_REQUEST['hostid'] = $result['original'] = get_request('hostid', -1);
    //-----
    if (is_null($nodeid)) {
        if (!$def_options['only_current_node']) {
            $nodeid = get_current_nodeid();
        } else {
            $nodeid = get_current_nodeid(false);
        }
    }
    //$nodeid = is_null($nodeid)?get_current_nodeid($opt):$nodeid;
    //$available_hosts = get_accessible_hosts_by_user($USER_DETAILS,$perm,PERM_RES_IDS_ARRAY,$nodeid,AVAILABLE_NOCACHE);
    if (USER_TYPE_SUPER_ADMIN != $USER_DETAILS['type']) {
        $def_sql['from']['hg'] = 'hosts_groups hg';
        $def_sql['from']['r'] = 'rights r';
        $def_sql['from']['ug'] = 'users_groups ug';
        $def_sql['where']['hgh'] = 'hg.hostid=h.hostid';
        $def_sql['where'][] = 'r.id=hg.groupid ';
        $def_sql['where'][] = 'r.groupid=ug.usrgrpid';
        $def_sql['where'][] = 'ug.userid=' . $userid;
        $def_sql['where'][] = 'r.permission>=' . $perm;
        $def_sql['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<' . $perm . ')';
    }
    // nodes
    if (ZBX_DISTRIBUTED) {
        $def_sql['select'][] = 'n.name';
        $def_sql['from'][] = 'nodes n';
        $def_sql['where'][] = 'n.nodeid=' . DBid2nodeid('h.hostid');
        $def_sql['order'][] = 'n.name';
    }
    // hosts
    if ($def_options['monitored_hosts']) {
        $def_sql['where'][] = 'h.status=' . HOST_STATUS_MONITORED;
    } else {
        if ($def_options['real_hosts']) {
            $def_sql['where'][] = 'h.status IN(' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')';
        } else {
            if ($def_options['templated_hosts']) {
                $def_sql['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
            } else {
                if ($def_options['not_proxy_hosts']) {
                    $def_sql['where'][] = 'h.status<>' . HOST_STATUS_PROXY;
                }
            }
        }
    }
    // items
    if ($def_options['with_items']) {
        $def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid )';
    } else {
        if ($def_options['with_monitored_items']) {
            $def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND i.status=' . ITEM_STATUS_ACTIVE . ')';
        } else {
            if ($def_options['with_historical_items']) {
                $def_sql['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)';
            }
        }
    }
    // triggers
    if ($def_options['with_triggers']) {
        $def_sql['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 ($def_options['with_monitored_triggers']) {
            $def_sql['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 . ')';
        }
    }
    // httptests
    if ($def_options['with_httptests']) {
        $def_sql['where'][] = 'EXISTS( SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=h.hostid ' . ' AND ht.applicationid=a.applicationid)';
    } else {
        if ($def_options['with_monitored_httptests']) {
            $def_sql['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 . ')';
        }
    }
    // graphs
    if ($def_options['with_graphs']) {
        $def_sql['where'][] = 'EXISTS( SELECT DISTINCT i.itemid ' . ' FROM items i, graphs_items gi ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=gi.itemid)';
    }
    //------
    $def_sql['order'][] = 'h.host';
    foreach ($sql as $key => $value) {
        zbx_value2array($value);
        if (isset($def_sql[$key])) {
            $def_sql[$key] = zbx_array_merge($def_sql[$key], $value);
        } else {
            $def_sql[$key] = $value;
        }
    }
    $def_sql['select'] = array_unique($def_sql['select']);
    $def_sql['from'] = array_unique($def_sql['from']);
    $def_sql['where'] = array_unique($def_sql['where']);
    $def_sql['order'] = array_unique($def_sql['order']);
    $sql_select = '';
    $sql_from = '';
    $sql_where = '';
    $sql_order = '';
    if (!empty($def_sql['select'])) {
        $sql_select .= implode(',', $def_sql['select']);
    }
    if (!empty($def_sql['from'])) {
        $sql_from .= implode(',', $def_sql['from']);
    }
    if (!empty($def_sql['where'])) {
        $sql_where .= ' AND ' . implode(' AND ', $def_sql['where']);
    }
    if (!empty($def_sql['order'])) {
        $sql_order .= implode(',', $def_sql['order']);
    }
    $sql = 'SELECT DISTINCT ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('h.hostid', $nodeid) . $sql_where . ' ORDER BY ' . $sql_order;
    $res = DBselect($sql);
    while ($host = DBfetch($res)) {
        $hosts[$host['hostid']] = $host['host'];
        $hostids[$host['hostid']] = $host['hostid'];
        if (bccomp($_REQUEST['hostid'], $host['hostid']) == 0) {
            $result['selected'] = $host['hostid'];
        }
    }
    $profile_hostid = CProfile::get('web.' . $page['menu'] . '.hostid');
    //-----
    if ($def_options['do_not_select']) {
        $_REQUEST['hostid'] = $result['selected'] = 0;
    } else {
        if ($def_options['do_not_select_if_empty'] && $_REQUEST['hostid'] == -1) {
            $_REQUEST['hostid'] = $result['selected'] = 0;
        } else {
            if ($def_options['select_first_host'] || $def_options['select_first_host_if_empty'] && $_REQUEST['hostid'] == -1 && is_null($profile_hostid) || $def_options['select_host_on_group_switch'] && $_REQUEST['hostid'] != -1 && bccomp($_REQUEST['hostid'], $result['selected']) != 0) {
                $first_hostid = next($hostids);
                reset($hostids);
                if ($first_hostid !== FALSE) {
                    $_REQUEST['hostid'] = $result['selected'] = $first_hostid;
                } else {
                    $_REQUEST['hostid'] = $result['selected'] = 0;
                }
            } else {
                if ($config['dropdown_first_remember']) {
                    if ($_REQUEST['hostid'] == -1) {
                        $_REQUEST['hostid'] = is_null($profile_hostid) ? '0' : $profile_hostid;
                    }
                    if (isset($hostids[$_REQUEST['hostid']])) {
                        $result['selected'] = $_REQUEST['hostid'];
                    } else {
                        $_REQUEST['hostid'] = $result['selected'];
                    }
                } else {
                    $_REQUEST['hostid'] = $result['selected'];
                }
            }
        }
    }
    return $result;
}
예제 #20
0
        }
    }
} else {
    $severity = get_request('severity', array(0, 1, 2, 3, 4, 5));
}
$media = get_request('media', -1);
$sendto = get_request('sendto', '');
$mediatypeid = get_request('mediatypeid', 0);
$active = get_request('active', 0);
$period = get_request('period', '1-7,00:00-23:59');
$frmMedia = new CFormTable(S_NEW_MEDIA);
$frmMedia->SetHelp("web.media.php");
$frmMedia->AddVar("media", $media);
$frmMedia->AddVar("dstfrm", $_REQUEST["dstfrm"]);
$cmbType = new CComboBox("mediatypeid", $mediatypeid);
$types = DBselect('SELECT mediatypeid,description ' . ' FROM media_type' . ' WHERE ' . DBin_node('mediatypeid') . ' ORDER BY type');
while ($type = DBfetch($types)) {
    $cmbType->AddItem($type["mediatypeid"], get_node_name_by_elid($type["mediatypeid"]) . $type["description"]);
}
$frmMedia->AddRow(S_TYPE, $cmbType);
$frmMedia->AddRow(S_SEND_TO, new CTextBox("sendto", $sendto, 20));
$frmMedia->AddRow(S_WHEN_ACTIVE, new CTextBox("period", $period, 48));
$frm_row = array();
for ($i = 0; $i <= 5; $i++) {
    array_push($frm_row, array(new CCheckBox("severity[{$i}]", str_in_array($i, $severity) ? 'yes' : 'no', null, $i), get_severity_description($i)), BR());
}
$frmMedia->AddRow(S_USE_IF_SEVERITY, $frm_row);
$cmbStat = new CComboBox("active", $active);
$cmbStat->AddItem(0, S_ENABLED);
$cmbStat->AddItem(1, S_DISABLED);
$frmMedia->AddRow("Status", $cmbStat);
예제 #21
0
function make_discovery_status()
{
    $drules = array();
    $db_drules = DBselect('select distinct * from drules where ' . DBin_node('druleid') . ' order by name');
    while ($drule_data = DBfetch($db_drules)) {
        $drules[$drule_data['druleid']] = $drule_data;
        $drules[$drule_data['druleid']]['up'] = 0;
        $drules[$drule_data['druleid']]['down'] = 0;
    }
    $db_dhosts = DBselect('SELECT d.* ' . ' FROM dhosts d ' . ' ORDER BY d.dhostid,d.status,d.ip');
    $services = array();
    $discovery_info = array();
    while ($drule_data = DBfetch($db_dhosts)) {
        if (DHOST_STATUS_DISABLED == $drule_data['status']) {
            $drules[$drule_data['druleid']]['down']++;
        } else {
            $drules[$drule_data['druleid']]['up']++;
        }
    }
    $header = array(is_show_all_nodes() ? new CCol(S_NODE, 'center') : null, new CCol(S_DISCOVERY_RULE, 'center'), new CCol(S_UP), new CCol(S_DOWN));
    $table = new CTableInfo();
    $table->setHeader($header, 'vertical_header');
    foreach ($drules as $druleid => $drule) {
        $table->addRow(array(get_node_name_by_elid($druleid), new CLink(get_node_name_by_elid($drule['druleid']) . $drule['name'], 'discovery.php?druleid=' . $druleid), new CSpan($drule['up'], 'green'), new CSpan($drule['down'], $drule['down'] > 0 ? 'red' : 'green')));
    }
    $table->setFooter(new CCol(S_UPDATED . ': ' . date("H:i:s", time())));
    return $table;
}
예제 #22
0
         }
         /*				if(!$group = get_hostgroup_by_groupid($db_group['groupid'])) continue;*/
         $result &= delete_host_group($db_group['groupid']);
         /*				if($result){
         					add_audit(AUDIT_ACTION_DELETE,AUDIT_RESOURCE_HOST_GROUP,
         					S_HOST_GROUP.' ['.$group['name'].' ] ['.$group['groupid'].']');
         				}*/
     }
     $result = DBend($result);
     show_messages(true, S_GROUP_DELETED, S_CANNOT_DELETE_GROUP);
 } else {
     if (str_in_array($_REQUEST['go'], array('activate', 'disable'))) {
         $result = true;
         $status = $_REQUEST['go'] == 'activate' ? HOST_STATUS_MONITORED : HOST_STATUS_NOT_MONITORED;
         $groups = get_request('groups', array());
         $db_hosts = DBselect('select h.hostid, hg.groupid ' . ' from hosts_groups hg, hosts h' . ' where h.hostid=hg.hostid ' . ' and h.status in (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')' . ' and ' . DBin_node('h.hostid'));
         DBstart();
         while ($db_host = DBfetch($db_hosts)) {
             if (!uint_in_array($db_host['groupid'], $groups)) {
                 continue;
             }
             $host = get_host_by_hostid($db_host['hostid']);
             $result &= update_host_status($db_host['hostid'], $status);
             /*			add_audit(AUDIT_ACTION_UPDATE,AUDIT_RESOURCE_HOST,
             				'Old status ['.$host['status'].'] '.'New status ['.$status.']');*/
         }
         $result = DBend($result);
         show_messages($result, S_HOST_STATUS_UPDATED, S_CANNOT_UPDATE_HOST);
         unset($_REQUEST['activate']);
     }
 }
예제 #23
0
파일: queue.php 프로젝트: rennhak/zabbix
<?php 
$_REQUEST["show"] = get_request("show", 0);
$form = new CForm();
$form->SetMethod('get');
$cmbMode = new CComboBox("show", $_REQUEST["show"], "submit();");
$cmbMode->AddItem(0, S_OVERVIEW);
$cmbMode->AddItem(1, S_OVERVIEW_BY_PROXY);
$cmbMode->AddItem(2, S_DETAILS);
$form->AddItem($cmbMode);
show_table_header(S_QUEUE_OF_ITEMS_TO_BE_UPDATED_BIG, $form);
?>

<?php 
$now = time();
$item_types = array(ITEM_TYPE_ZABBIX, ITEM_TYPE_ZABBIX_ACTIVE, ITEM_TYPE_SNMPV1, ITEM_TYPE_SNMPV2C, ITEM_TYPE_SNMPV3, ITEM_TYPE_SIMPLE, ITEM_TYPE_INTERNAL, ITEM_TYPE_AGGREGATE, ITEM_TYPE_EXTERNAL);
$result = DBselect('SELECT i.itemid,i.nextcheck,i.description,i.key_,i.type,h.host,h.hostid,h.proxy_hostid ' . ' FROM items i,hosts h ' . ' WHERE i.status=' . ITEM_STATUS_ACTIVE . ' AND i.type in (' . implode(',', $item_types) . ') ' . ' AND ((h.status=' . HOST_STATUS_MONITORED . ' AND h.available != ' . HOST_AVAILABLE_FALSE . ') ' . ' OR (h.status=' . HOST_STATUS_MONITORED . ' AND h.available=' . HOST_AVAILABLE_FALSE . ' AND h.disable_until<=' . $now . ')) ' . ' AND i.hostid=h.hostid ' . ' AND i.nextcheck + 5 <' . $now . ' AND i.key_ NOT IN (' . zbx_dbstr('status') . ',' . zbx_dbstr('icmpping') . ',' . zbx_dbstr('icmppingsec') . ',' . zbx_dbstr('zabbix[log]') . ') ' . ' AND i.value_type not in (' . ITEM_VALUE_TYPE_LOG . ') ' . ' AND ' . DBcondition('h.hostid', $available_hosts) . ' AND ' . DBin_node('h.hostid', get_current_nodeid()) . ' ORDER BY i.nextcheck,h.host,i.description,i.key_');
$table = new CTableInfo(S_THE_QUEUE_IS_EMPTY);
if ($_REQUEST["show"] == 0) {
    foreach ($item_types as $type) {
        $sec_10[$type] = 0;
        $sec_30[$type] = 0;
        $sec_60[$type] = 0;
        $sec_300[$type] = 0;
        $sec_600[$type] = 0;
        $sec_rest[$type] = 0;
    }
    while ($row = DBfetch($result)) {
        if ($now - $row["nextcheck"] <= 10) {
            $sec_10[$row["type"]]++;
        } else {
            if ($now - $row["nextcheck"] <= 30) {
예제 #24
0
$fields = array('triggerid' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID, PAGE_TYPE_HTML . '==' . $page['type']), 'eventid' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID, PAGE_TYPE_HTML . '==' . $page['type']), 'fullscreen' => array(T_ZBX_INT, O_OPT, P_SYS, IN('0,1'), NULL), "save" => array(T_ZBX_STR, O_OPT, P_ACT | P_SYS, null, null), "cancel" => array(T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null), 'favobj' => array(T_ZBX_STR, O_OPT, P_ACT, IN("'filter','hat'"), NULL), 'favid' => array(T_ZBX_STR, O_OPT, P_ACT, NOT_EMPTY, 'isset({favobj})'), 'state' => array(T_ZBX_INT, O_OPT, P_ACT, NOT_EMPTY, 'isset({favobj})'));
check_fields($fields);
/* AJAX */
if (isset($_REQUEST['favobj'])) {
    if ('hat' == $_REQUEST['favobj']) {
        update_profile('web.tr_events.hats.' . $_REQUEST['favid'] . '.state', $_REQUEST['state'], PROFILE_TYPE_INT);
    }
}
if (PAGE_TYPE_JS == $page['type'] || PAGE_TYPE_HTML_BLOCK == $page['type']) {
    exit;
}
//--------
if (!check_right_on_trigger_by_triggerid(PERM_READ_ONLY, $_REQUEST["triggerid"])) {
    access_deny();
}
$trigger_data = DBfetch(DBselect('SELECT h.host, t.* ' . ' FROM hosts h, items i, functions f, triggers t ' . ' WHERE i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid ' . ' AND t.triggerid=' . $_REQUEST["triggerid"] . ' AND h.hostid=i.hostid ' . ' AND ' . DBin_node('t.triggerid')));
$p_elements = array();
//Header
$trigger_data['exp_expr'] = explode_exp($trigger_data["expression"], 1);
$trigger_data['exp_desc'] = expand_trigger_description_by_data($trigger_data);
$text = array(S_EVENTS_BIG . ': "' . $trigger_data['exp_desc'] . '"');
$url = '?fullscreen=' . ($_REQUEST['fullscreen'] ? '0' : '1') . url_param('triggerid') . url_param('eventid');
$fs_icon = new CDiv(SPACE, 'fullscreen');
$fs_icon->addOption('title', $_REQUEST['fullscreen'] ? S_NORMAL . ' ' . S_VIEW : S_FULLSCREEN);
$fs_icon->addAction('onclick', new CScript("javascript: document.location = '" . $url . "';"));
//-------
$left_tab = new CTable();
$left_tab->setCellPadding(3);
$left_tab->setCellSpacing(3);
$left_tab->addOption('border', 0);
$left_tab->addRow(create_hat(S_EVENT . SPACE . S_SOURCE . SPACE . S_DETAILS, make_trigger_details($_REQUEST['triggerid'], $trigger_data), null, 'hat_triggerdetails'));
예제 #25
0
        case AUDIT_ACTION_ENABLE:
            $action = _('Enabled');
            break;
        case AUDIT_ACTION_DISABLE:
            $action = _('Disabled');
            break;
        default:
            $action = _('Unknown action');
    }
    $audit['action'] = $action;
    $audit['resourcetype'] = audit_resource2str($audit['resourcetype']);
    if (empty($audit['details'])) {
        $audit['details'] = DBfetchArray(DBselect('SELECT ad.table_name,ad.field_name,ad.oldvalue,ad.newvalue' . ' FROM auditlog_details ad' . ' WHERE ad.auditid=' . $audit['auditid']));
    }
    $data['actions'][$audit['auditid']] = $audit;
}
if (!empty($data['actions'])) {
    order_result($data['actions'], 'clock', ZBX_SORT_DOWN);
}
// get paging
$data['paging'] = getPagingLine($data['actions']);
// get timeline
unset($sqlWhere['from'], $sqlWhere['till']);
$sql = 'SELECT a.auditid,a.clock' . ' FROM auditlog a,users u' . ' WHERE u.userid=a.userid' . implode('', $sqlWhere) . ' AND ' . DBin_node('u.userid', get_current_nodeid(null, PERM_READ_ONLY)) . ' ORDER BY a.clock';
$firstAudit = DBfetch(DBselect($sql, $config['search_limit'] + 1));
$data['timeline'] = array('period' => $effectivePeriod, 'starttime' => date('YmdHis', !empty($firstAudit) ? $firstAudit['clock'] : null), 'usertime' => isset($_REQUEST['stime']) ? date('YmdHis', zbxDateToTime($data['stime']) + $effectivePeriod) : null);
// render view
$auditView = new CView('administration.auditlogs.list', $data);
$auditView->render();
$auditView->show();
require_once dirname(__FILE__) . '/include/page_footer.php';
예제 #26
0
function validate_expression($expression)
{
    global $ZBX_TR_EXPR_ALLOWED_MACROS, $ZBX_TR_EXPR_REPLACE_TO, $ZBX_TR_EXPR_ALLOWED_FUNCTIONS;
    if (empty($expression)) {
        error('Expression can\'t be empty');
    }
    $expr = $expression;
    $h_status = array();
    /* Replace all {server:key.function(param)} and {MACRO} with '$ZBX_TR_EXPR_REPLACE_TO' */
    while (ereg(ZBX_EREG_EXPRESSION_TOKEN_FORMAT, $expr, $arr)) {
        if ($arr[ZBX_EXPRESSION_MACRO_ID] && !isset($ZBX_TR_EXPR_ALLOWED_MACROS[$arr[ZBX_EXPRESSION_MACRO_ID]])) {
            error('Unknown macro [' . $arr[ZBX_EXPRESSION_MACRO_ID] . ']');
            return false;
        } else {
            if (!$arr[ZBX_EXPRESSION_MACRO_ID]) {
                $host =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_HOST_ID];
                $key =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_KEY_ID];
                $function =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_NAME_ID];
                $parameter =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_PARAM_ID];
                /* Check host */
                $sql = 'SELECT COUNT(*) as cnt,min(status) as status,min(hostid) as hostid ' . ' FROM hosts h ' . ' WHERE h.host=' . zbx_dbstr($host) . ' AND ' . DBin_node('h.hostid', get_current_nodeid(false)) . ' AND status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ') ';
                $row = DBfetch(DBselect($sql));
                if ($row['cnt'] == 0) {
                    error('No such host (' . $host . ')');
                    return false;
                } else {
                    if ($row['cnt'] != 1) {
                        error('Too many hosts (' . $host . ')');
                        return false;
                    }
                }
                $h_status[$row['status']][$row['hostid']] = $row['cnt'];
                /* Check key */
                $sql = 'SELECT i.itemid,i.value_type ' . ' FROM hosts h,items i ' . ' WHERE h.host=' . zbx_dbstr($host) . ' AND i.key_=' . zbx_dbstr($key) . ' AND h.hostid=i.hostid ' . ' AND ' . DBin_node('h.hostid', get_current_nodeid(false));
                if (!($item = DBfetch(DBselect($sql)))) {
                    error('No such monitored parameter (' . $key . ') for host (' . $host . ')');
                    return false;
                }
                /* Check function */
                if (!isset($ZBX_TR_EXPR_ALLOWED_FUNCTIONS[$function])) {
                    error('Unknown function [' . $function . ']');
                    return false;
                }
                $fnc_valid =& $ZBX_TR_EXPR_ALLOWED_FUNCTIONS[$function];
                if (is_array($fnc_valid['item_types']) && !uint_in_array($item['value_type'], $fnc_valid['item_types'])) {
                    $allowed_types = array();
                    foreach ($fnc_valid['item_types'] as $type) {
                        $allowed_types[] = item_value_type2str($type);
                    }
                    info('Function (' . $function . ') available only for items with value types [' . implode(',', $allowed_types) . ']');
                    error('Incorrect value type [' . item_value_type2str($item['value_type']) . '] for function (' . $function . ') of key (' . $host . ':' . $key . ')');
                    return false;
                }
                if (!is_null($fnc_valid['args'])) {
                    $parameter = zbx_get_params($parameter);
                    if (!is_array($fnc_valid['args'])) {
                        $fnc_valid['args'] = array($fnc_valid['args']);
                    }
                    foreach ($fnc_valid['args'] as $pid => $params) {
                        if (!isset($parameter[$pid])) {
                            if (!isset($params['mandat'])) {
                                continue;
                            } else {
                                error('Missed mandatory parameter for function (' . $function . ')');
                                return false;
                            }
                        }
                        if ('sec' == $params['type'] && validate_float($parameter[$pid]) != 0) {
                            error('[' . $parameter[$pid] . '] is not a float for function (' . $function . ')');
                            return false;
                        }
                        if ('sec_num' == $params['type'] && validate_ticks($parameter[$pid]) != 0) {
                            error('[' . $parameter[$pid] . '] is not a float or counter for function (' . $function . ')');
                            return false;
                        }
                    }
                }
            }
        }
        $expr = $arr[ZBX_EXPRESSION_LEFT_ID] . $ZBX_TR_EXPR_REPLACE_TO . $arr[ZBX_EXPRESSION_RIGHT_ID];
    }
    if (isset($h_status[HOST_STATUS_TEMPLATE]) && (count($h_status) > 1 || count($h_status[HOST_STATUS_TEMPLATE]) > 1)) {
        error("Incorrect trigger expression. You can't use template hosts" . " in mixed expressions.");
        return false;
    }
    /* Replace all calculations and numbers with '$ZBX_TR_EXPR_REPLACE_TO' */
    $expt_number = '(' . $ZBX_TR_EXPR_REPLACE_TO . '|' . ZBX_EREG_NUMBER . ')';
    $expt_term = '((\\(' . $expt_number . '\\))|(' . $expt_number . '))';
    $expr_format = '((' . $expt_term . ZBX_EREG_SPACES . ZBX_EREG_SIGN . ZBX_EREG_SPACES . $expt_term . ')|(\\(' . $expt_term . '\\)))';
    $expr_full_format = '((\\(' . $expr_format . '\\))|(' . $expr_format . '))';
    while ($res = ereg($expr_full_format . '([[:print:]]*)$', $expr, $arr)) {
        $expr = substr($expr, 0, strpos($expr, $arr[1])) . $ZBX_TR_EXPR_REPLACE_TO . $arr[58];
    }
    if ($ZBX_TR_EXPR_REPLACE_TO != $expr) {
        error('Incorrect trigger expression. [' . str_replace($ZBX_TR_EXPR_REPLACE_TO, ' ... ', $expr) . ']');
        return false;
    }
    return true;
}
예제 #27
0
 /**
  * Get history data
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8.3
  * @version 1.3
  *
  * @param array $options
  * @param array $options['itemids']
  * @param boolean $options['editable']
  * @param string $options['pattern']
  * @param int $options['limit']
  * @param string $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $nodeCheck = false;
     $result = array();
     $sort_columns = array('itemid', 'clock');
     // 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('history' => 'h.itemid'), 'from' => array(), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('history' => ITEM_VALUE_TYPE_UINT64, 'nodeids' => null, 'hostids' => null, 'itemids' => null, 'triggerids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'time_from' => null, 'time_till' => null, 'output' => API_OUTPUT_REFER, 'countOutput' => null, 'groupCount' => null, 'groupOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     switch ($options['history']) {
         case ITEM_VALUE_TYPE_LOG:
             $sql_parts['from']['history'] = 'history_log h';
             $sort_columns[] = 'id';
             break;
         case ITEM_VALUE_TYPE_TEXT:
             $sql_parts['from']['history'] = 'history_text h';
             $sort_columns[] = 'id';
             break;
         case ITEM_VALUE_TYPE_STR:
             $sql_parts['from']['history'] = 'history_str h';
             break;
         case ITEM_VALUE_TYPE_UINT64:
             $sql_parts['from']['history'] = 'history_uint h';
             break;
         case ITEM_VALUE_TYPE_FLOAT:
         default:
             $sql_parts['from']['history'] = 'history h';
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $USER_DETAILS['type'] || $options['nopermissions']) {
     } else {
         $itemOptions = array('editable' => $options['editable'], 'preservekeys' => 1);
         if (!is_null($options['itemids'])) {
             $itemOptions['itemids'] = $options['itemids'];
         }
         $items = CItem::get($itemOptions);
         $options['itemids'] = array_keys($items);
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sql_parts['where']['itemid'] = DBcondition('h.itemid', $options['itemids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('h.itemid', $nodeids);
         }
     }
     // 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']['items'] = 'items i';
         $sql_parts['where']['i'] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['hi'] = 'h.itemid=i.itemid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('i.hostid', $nodeids);
         }
     }
     // node check !!!!!
     // should be last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('h.itemid', $nodeids);
     }
     // time_from
     if (!is_null($options['time_from'])) {
         $sql_parts['select']['clock'] = 'h.clock';
         $sql_parts['where']['clock_from'] = 'h.clock>=' . $options['time_from'];
     }
     // time_till
     if (!is_null($options['time_till'])) {
         $sql_parts['select']['clock'] = 'h.clock';
         $sql_parts['where']['clock_till'] = 'h.clock<=' . $options['time_till'];
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter($sql_parts['from']['history'], $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search($sql_parts['from']['history'], $options, $sql_parts);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         unset($sql_parts['select']['clock']);
         $sql_parts['select']['history'] = '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;
             }
         }
     }
     // groupOutput
     $groupOutput = false;
     if (!is_null($options['groupOutput'])) {
         if (str_in_array('h.' . $options['groupOutput'], $sql_parts['select']) || str_in_array('h.*', $sql_parts['select'])) {
             $groupOutput = true;
         }
     }
     // 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;
         if ($options['sortfield'] == 'clock') {
             $sql_parts['order']['itemid'] = 'h.itemid ' . $sortorder;
         }
         $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'];
     }
     //---------------
     $itemids = array();
     $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['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 .= 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 ' . $sql_where . $sql_order;
     $db_res = DBselect($sql, $sql_limit);
     //SDI($sql);
     $count = 0;
     $group = array();
     while ($data = DBfetch($db_res)) {
         if ($options['countOutput']) {
             $result = $data;
         } else {
             $itemids[$data['itemid']] = $data['itemid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$count] = array('itemid' => $data['itemid']);
             } else {
                 $result[$count] = array();
                 // hostids
                 if (isset($data['hostid'])) {
                     if (!isset($result[$count]['hosts'])) {
                         $result[$count]['hosts'] = array();
                     }
                     $result[$count]['hosts'][] = array('hostid' => $data['hostid']);
                     unset($data['hostid']);
                 }
                 // triggerids
                 if (isset($data['triggerid'])) {
                     if (!isset($result[$count]['triggers'])) {
                         $result[$count]['triggers'] = array();
                     }
                     $result[$count]['triggers'][] = array('triggerid' => $data['triggerid']);
                     unset($data['triggerid']);
                 }
                 // itemids
                 //					if(isset($data['itemid']) && !is_null($options['itemids'])){
                 //						if(!isset($result[$count]['items'])) $result[$count]['items'] = array();
                 //						$result[$count]['items'][] = array('itemid' => $data['itemid']);
                 //					}
                 $result[$count] += $data;
                 // grouping
                 if ($groupOutput) {
                     $dataid = $data[$options['groupOutput']];
                     if (!isset($group[$dataid])) {
                         $group[$dataid] = array();
                     }
                     $group[$dataid][] = $result[$count];
                 }
                 $count++;
             }
         }
     }
     COpt::memoryPick();
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
 /**
  * Get Applications 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 boolean $options['status']
  * @param boolean $options['editable']
  * @param boolean $options['count']
  * @param string $options['pattern']
  * @param int $options['limit']
  * @param string $options['order']
  * @return array|int item data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sortColumns = array('applicationid', 'name');
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     $sqlParts = array('select' => array('apps' => 'a.applicationid'), 'from' => array('applications' => 'applications a'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'itemids' => null, 'applicationids' => null, 'templated' => null, 'editable' => null, 'inherited' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'exludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'expandData' => null, 'selectHosts' => null, 'selectItems' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE a.hostid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.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) {
             $sqlParts['select']['groupid'] = 'hg.groupid';
         }
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where']['ahg'] = 'a.hostid=hg.hostid';
         $sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sqlParts['select']['hostid'] = 'a.hostid';
         }
         $sqlParts['where']['hostid'] = dbConditionInt('a.hostid', $options['hostids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hostid'] = 'a.hostid';
         }
     }
     // expandData
     if (!is_null($options['expandData'])) {
         $sqlParts['select']['host'] = 'h.host';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['ah'] = 'a.hostid=h.hostid';
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['itemid'] = 'ia.itemid';
         }
         $sqlParts['from']['items_applications'] = 'items_applications ia';
         $sqlParts['where'][] = dbConditionInt('ia.itemid', $options['itemids']);
         $sqlParts['where']['aia'] = 'a.applicationid=ia.applicationid';
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['applicationid'] = 'a.applicationid';
         }
         $sqlParts['where'][] = dbConditionInt('a.applicationid', $options['applicationids']);
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['ah'] = 'a.hostid=h.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 'a.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 'a.templateid IS NULL';
         }
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['apps'] = 'a.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('count(DISTINCT a.applicationid) as rowscount');
         // groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('applications a', $options, $sqlParts);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('applications a', $options, $sqlParts);
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'a');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $applicationids = array();
     $sqlParts['select'] = array_unique($sqlParts['select']);
     $sqlParts['from'] = array_unique($sqlParts['from']);
     $sqlParts['where'] = array_unique($sqlParts['where']);
     $sqlParts['group'] = array_unique($sqlParts['group']);
     $sqlParts['order'] = array_unique($sqlParts['order']);
     $sqlSelect = '';
     $sqlFrom = '';
     $sqlWhere = '';
     $sqlGroup = '';
     $sqlOrder = '';
     if (!empty($sqlParts['select'])) {
         $sqlSelect .= implode(',', $sqlParts['select']);
     }
     if (!empty($sqlParts['from'])) {
         $sqlFrom .= implode(',', $sqlParts['from']);
     }
     if (!empty($sqlParts['where'])) {
         $sqlWhere .= ' AND ' . implode(' AND ', $sqlParts['where']);
     }
     if (!empty($sqlParts['group'])) {
         $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']);
     }
     if (!empty($sqlParts['order'])) {
         $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']);
     }
     $sqlLimit = $sqlParts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('a.applicationid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder;
     $res = DBselect($sql, $sqlLimit);
     while ($application = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $application;
             } else {
                 $result = $application['rowscount'];
             }
         } else {
             $applicationids[$application['applicationid']] = $application['applicationid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$application['applicationid']] = array('applicationid' => $application['applicationid']);
             } else {
                 if (!isset($result[$application['applicationid']])) {
                     $result[$application['applicationid']] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$application['applicationid']]['hosts'])) {
                     $result[$application['applicationid']]['hosts'] = array();
                 }
                 if (!is_null($options['selectItems']) && !isset($result[$application['applicationid']]['items'])) {
                     $result[$application['applicationid']]['items'] = array();
                 }
                 // hostids
                 if (isset($application['hostid']) && is_null($options['selectHosts'])) {
                     if (!isset($result[$application['applicationid']]['hosts'])) {
                         $result[$application['applicationid']]['hosts'] = array();
                     }
                     $result[$application['applicationid']]['hosts'][] = array('hostid' => $application['hostid']);
                 }
                 // itemids
                 if (isset($application['itemid']) && is_null($options['selectItems'])) {
                     if (!isset($result[$application['applicationid']]['items'])) {
                         $result[$application['applicationid']]['items'] = array();
                     }
                     $result[$application['applicationid']]['items'][] = array('itemid' => $application['itemid']);
                     unset($application['itemid']);
                 }
                 $result[$application['applicationid']] += $application;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // adding objects
     // adding hosts
     if ($options['selectHosts'] !== null && (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs))) {
         $objParams = array('output' => $options['selectHosts'], 'applicationids' => $applicationids, 'nopermissions' => 1, 'templated_hosts' => true, 'preservekeys' => 1);
         $hosts = API::Host()->get($objParams);
         foreach ($hosts as $hostid => $host) {
             $iapplications = $host['applications'];
             unset($host['applications']);
             foreach ($iapplications as $application) {
                 $result[$application['applicationid']]['hosts'][] = $host;
             }
         }
     }
     // adding objects
     // adding items
     if (!is_null($options['selectItems']) && str_in_array($options['selectItems'], $subselectsAllowedOutputs)) {
         $objParams = array('output' => $options['selectItems'], 'applicationids' => $applicationids, 'nopermissions' => true, 'preservekeys' => true);
         $items = API::Item()->get($objParams);
         foreach ($items as $itemid => $item) {
             $iapplications = $item['applications'];
             unset($item['applications']);
             foreach ($iapplications as $application) {
                 $result[$application['applicationid']]['items'][] = $item;
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
예제 #29
0
 /**
  * Get Service data
  *
  * @param _array $options
  * @param array $options['nodeids'] Node IDs
  * @param array $options['groupids'] ServiceGroup IDs
  * @param array $options['hostids'] Service IDs
  * @param boolean $options['monitored_hosts'] only monitored Services
  * @param boolean $options['templated_hosts'] include templates in result
  * @param boolean $options['with_items'] only with items
  * @param boolean $options['with_historical_items'] only with historical items
  * @param boolean $options['with_triggers'] only with triggers
  * @param boolean $options['with_httptests'] only with http tests
  * @param boolean $options['with_graphs'] only with graphs
  * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins
  * @param boolean $options['selectGroups'] select ServiceGroups
  * @param boolean $options['selectTemplates'] select Templates
  * @param boolean $options['selectItems'] select Items
  * @param boolean $options['selectTriggers'] select Triggers
  * @param boolean $options['selectGraphs'] select Graphs
  * @param boolean $options['selectApplications'] select Applications
  * @param boolean $options['selectMacros'] select Macros
  * @param int $options['count'] count Services, returned column name is rowscount
  * @param string $options['pattern'] search hosts by pattern in Service name
  * @param string $options['extendPattern'] search hosts by pattern in Service 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 Service data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $nodeCheck = false;
     $userType = self::$userData['type'];
     // allowed columns for sorting
     $sortColumns = array('dserviceid', 'dhostid', 'ip');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     $sqlParts = array('select' => array('dservices' => 'ds.dserviceid'), 'from' => array('dservices' => 'dservices ds'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'dserviceids' => null, 'dhostids' => null, 'dcheckids' => null, 'druleids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectDRules' => null, 'selectDHosts' => null, 'selectDChecks' => null, 'selectHosts' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['dservices']);
         $dbTable = DB::getSchema('dservices');
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 's.' . $field;
             }
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $userType) {
     } elseif (is_null($options['editable']) && self::$userData['type'] == USER_TYPE_ZABBIX_ADMIN) {
     } elseif (!is_null($options['editable']) && self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
         return array();
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // dserviceids
     if (!is_null($options['dserviceids'])) {
         zbx_value2array($options['dserviceids']);
         $sqlParts['where']['dserviceid'] = dbConditionInt('ds.dserviceid', $options['dserviceids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('ds.dserviceid', $nodeids);
         }
     }
     // dhostids
     if (!is_null($options['dhostids'])) {
         zbx_value2array($options['dhostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['dhostid'] = 'ds.dhostid';
         }
         $sqlParts['where'][] = dbConditionInt('ds.dhostid', $options['dhostids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['dhostid'] = 'ds.dhostid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('ds.dhostid', $nodeids);
         }
     }
     // dcheckids
     if (!is_null($options['dcheckids'])) {
         zbx_value2array($options['dcheckids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['dcheckid'] = 'dc.dcheckid';
         }
         $sqlParts['from']['dhosts'] = 'dhosts dh';
         $sqlParts['from']['dchecks'] = 'dchecks dc';
         $sqlParts['where'][] = dbConditionInt('dc.dcheckid', $options['dcheckids']);
         $sqlParts['where']['dhds'] = 'dh.hostid=ds.hostid';
         $sqlParts['where']['dcdh'] = 'dc.druleid=dh.druleid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['dcheckid'] = 'dc.dcheckid';
         }
     }
     // druleids
     if (!is_null($options['druleids'])) {
         zbx_value2array($options['druleids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['druleid'] = 'dh.druleid';
         }
         $sqlParts['from']['dhosts'] = 'dhosts dh';
         $sqlParts['where']['druleid'] = dbConditionInt('dh.druleid', $options['druleids']);
         $sqlParts['where']['dhds'] = 'dh.dhostid=ds.dhostid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['druleid'] = 'dh.druleid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sqlParts['where'][] = DBin_node('dh.druleid', $nodeids);
         }
     }
     // node check !!!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sqlParts['where'][] = DBin_node('ds.dserviceid', $nodeids);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['dservices'] = 'ds.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('count(DISTINCT ds.dserviceid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('dservices ds', $options, $sqlParts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('dservices ds', $options, $sqlParts);
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 'ds');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     //-------
     $dserviceids = array();
     $sqlParts['select'] = array_unique($sqlParts['select']);
     $sqlParts['from'] = array_unique($sqlParts['from']);
     $sqlParts['where'] = array_unique($sqlParts['where']);
     $sqlParts['group'] = array_unique($sqlParts['group']);
     $sqlParts['order'] = array_unique($sqlParts['order']);
     $sqlSelect = '';
     $sqlFrom = '';
     $sqlWhere = '';
     $sqlGroup = '';
     $sqlOrder = '';
     if (!empty($sqlParts['select'])) {
         $sqlSelect .= implode(',', $sqlParts['select']);
     }
     if (!empty($sqlParts['from'])) {
         $sqlFrom .= implode(',', $sqlParts['from']);
     }
     if (!empty($sqlParts['where'])) {
         $sqlWhere .= implode(' AND ', $sqlParts['where']);
     }
     if (!empty($sqlParts['group'])) {
         $sqlWhere .= ' GROUP BY ' . implode(',', $sqlParts['group']);
     }
     if (!empty($sqlParts['order'])) {
         $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']);
     }
     $sqlLimit = $sqlParts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . $sqlWhere . $sqlGroup . $sqlOrder;
     //SDI($sql);
     $res = DBselect($sql, $sqlLimit);
     while ($dservice = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $dservice;
             } else {
                 $result = $dservice['rowscount'];
             }
         } else {
             $dserviceids[$dservice['dserviceid']] = $dservice['dserviceid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$dservice['dserviceid']] = array('dserviceid' => $dservice['dserviceid']);
             } else {
                 if (!isset($result[$dservice['dserviceid']])) {
                     $result[$dservice['dserviceid']] = array();
                 }
                 if (!is_null($options['selectDRules']) && !isset($result[$dservice['dserviceid']]['drules'])) {
                     $result[$dservice['dserviceid']]['drules'] = array();
                 }
                 if (!is_null($options['selectDHosts']) && !isset($result[$dservice['dserviceid']]['dhosts'])) {
                     $result[$dservice['dserviceid']]['dhosts'] = array();
                 }
                 if (!is_null($options['selectDChecks']) && !isset($result[$dservice['dserviceid']]['dchecks'])) {
                     $result[$dservice['dserviceid']]['dchecks'] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$dservice['dserviceid']]['hosts'])) {
                     $result[$dservice['dserviceid']]['hosts'] = array();
                 }
                 // druleids
                 if (isset($dservice['druleid']) && is_null($options['selectDRules'])) {
                     if (!isset($result[$dservice['dserviceid']]['drules'])) {
                         $result[$dservice['dserviceid']]['drules'] = array();
                     }
                     $result[$dservice['dserviceid']]['drules'][] = array('druleid' => $dservice['druleid']);
                 }
                 // dhostids
                 if (isset($dservice['dhostid']) && is_null($options['selectDHosts'])) {
                     if (!isset($result[$dservice['dserviceid']]['dhosts'])) {
                         $result[$dservice['dserviceid']]['dhosts'] = array();
                     }
                     $result[$dservice['dserviceid']]['dhosts'][] = array('dhostid' => $dservice['dhostid']);
                 }
                 // dcheckids
                 if (isset($dservice['dcheckid']) && is_null($options['selectDChecks'])) {
                     if (!isset($result[$dservice['dserviceid']]['dchecks'])) {
                         $result[$dservice['dserviceid']]['dchecks'] = array();
                     }
                     $result[$dservice['dserviceid']]['dchecks'][] = array('dcheckid' => $dservice['dcheckid']);
                 }
                 $result[$dservice['dserviceid']] += $dservice;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // Adding Objects
     // select_drules
     if (!is_null($options['selectDRules'])) {
         $objParams = array('nodeids' => $nodeids, 'dserviceids' => $dserviceids, 'preservekeys' => 1);
         if (is_array($options['selectDRules']) || str_in_array($options['selectDRules'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDRules'];
             $drules = API::DRule()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($drules, 'name');
             }
             foreach ($drules as $druleid => $drule) {
                 unset($drules[$druleid]['dservices']);
                 $count = array();
                 foreach ($drule['dservices'] as $dnum => $dservice) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dservice['dserviceid']])) {
                             $count[$dservice['dserviceid']] = 0;
                         }
                         $count[$dservice['dserviceid']]++;
                         if ($count[$dservice['dserviceid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dservice['dserviceid']]['drules'][] =& $drules[$druleid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectDRules']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $drules = API::DRule()->get($objParams);
             $drules = zbx_toHash($drules, 'dserviceid');
             foreach ($result as $dserviceid => $dservice) {
                 if (isset($drules[$dserviceid])) {
                     $result[$dserviceid]['drules'] = $drules[$dserviceid]['rowscount'];
                 } else {
                     $result[$dserviceid]['drules'] = 0;
                 }
             }
         }
     }
     // selectDHosts
     if (!is_null($options['selectDHosts'])) {
         $objParams = array('nodeids' => $nodeids, 'dserviceids' => $dserviceids, 'preservekeys' => 1);
         if (is_array($options['selectDHosts']) || str_in_array($options['selectDHosts'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDHosts'];
             $dhosts = API::DHost()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($dhosts, 'dhostid');
             }
             foreach ($dhosts as $dhostid => $dhost) {
                 unset($dhosts[$dhostid]['dservices']);
                 foreach ($dhost['dservices'] as $snum => $dservice) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dservice['dserviceid']])) {
                             $count[$dservice['dserviceid']] = 0;
                         }
                         $count[$dservice['dserviceid']]++;
                         if ($count[$dservice['dserviceid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dservice['dserviceid']]['dhosts'][] =& $dhosts[$dhostid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectDHosts']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $dhosts = API::DHost()->get($objParams);
             $dhosts = zbx_toHash($dhosts, 'dhostid');
             foreach ($result as $dserviceid => $dservice) {
                 if (isset($dhosts[$dserviceid])) {
                     $result[$dserviceid]['dhosts'] = $dhosts[$dserviceid]['rowscount'];
                 } else {
                     $result[$dserviceid]['dhosts'] = 0;
                 }
             }
         }
     }
     // selectHosts
     if (!is_null($options['selectHosts'])) {
         $objParams = array('nodeids' => $nodeids, 'dserviceids' => $dserviceids, 'preservekeys' => 1, 'sortfield' => 'status');
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectHosts'];
             $hosts = API::Host()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'hostid');
             }
             foreach ($hosts as $hostid => $host) {
                 unset($hosts[$hostid]['dservices']);
                 foreach ($host['dservices'] as $dnum => $dservice) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dservice['dserviceid']])) {
                             $count[$dservice['dserviceid']] = 0;
                         }
                         $count[$dservice['dserviceid']]++;
                         if ($count[$dservice['dserviceid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dservice['dserviceid']]['hosts'][] =& $hosts[$hostid];
                 }
             }
         } elseif (API_OUTPUT_COUNT == $options['selectHosts']) {
             $objParams['countOutput'] = 1;
             $objParams['groupCount'] = 1;
             $hosts = API::Host()->get($objParams);
             $hosts = zbx_toHash($hosts, 'hostid');
             foreach ($result as $dserviceid => $dservice) {
                 if (isset($hosts[$dserviceid])) {
                     $result[$dserviceid]['hosts'] = $hosts[$dserviceid]['rowscount'];
                 } else {
                     $result[$dserviceid]['hosts'] = 0;
                 }
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
예제 #30
0
function get_regexp_by_regexpid($regexpid)
{
    $sql = 'SELECT re.* ' . ' FROM regexps re ' . ' WHERE ' . DBin_node('re.regexpid') . ' AND regexpid=' . $regexpid;
    $db_regexp = DBfetch(DBselect($sql));
    return $db_regexp;
}