Ejemplo n.º 1
0
function hostIdents($hostids)
{
    $idents = array();
    $options = array('hostids' => $hostids, 'output' => API_OUTPUT_EXTEND, 'nodeids' => get_current_nodeid(true));
    $hosts = CHost::get($options);
    foreach ($hosts as $hnum => $host) {
        $idents[$host['hostid']] = array('node' => get_node_name_by_elid($host['hostid'], true), 'host' => $host['host']);
    }
    return $idents;
}
Ejemplo n.º 2
0
 /**
  * Delete Host
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8
  * @version 1
  *
  * @param array $hostids
  * @return boolean
  */
 public static function delete($hostids)
 {
     $result = delete_host($hostids, false);
     if ($result) {
         return $hostids;
     } else {
         self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Internal zabbix error');
         return false;
     }
 }
Ejemplo n.º 3
0
$right_tab->setCellPadding(3);
$right_tab->setCellSpacing(3);
$right_tab->setAttribute('border', 0);
// FIND Hosts
$params = array('nodeids' => get_current_nodeid(), 'extendoutput' => true, 'pattern' => $search, 'extend_pattern' => true, 'limit' => $rows_per_page, 'order' => 'host');
$db_hosts = CHost::get($params);
$hosts = selectByPattern($db_hosts, 'host', $search, $rows_per_page);
$hostids = array_keys($hosts);
$hostsgroups = array();
$sql = 'SELECT * FROM hosts_groups hg WHERE ' . DBcondition('hg.hostid', $hostids);
$res = DBselect($sql);
while ($hostgroup = DBfetch($res)) {
    $hostsgroups[$hostgroup['hostid']] = $hostgroup['groupid'];
}
$params = array('nodeids' => get_current_nodeid(), 'pattern' => $search, 'extend_pattern' => true, 'count' => 1);
$hosts_count = CHost::get($params);
$overalCount = $hosts_count['rowscount'];
$viewCount = count($hosts);
$header = array(is_show_all_nodes() ? new CCol(S_NODE) : null, new CCol(S_HOSTS), new CCol(S_IP), new CCol(S_DNS), new CCol(S_LATEST_DATA), new CCol(S_TRIGGERS), new CCol(S_EVENTS), $admin ? new CCol(S_EDIT, 'center') : null);
$table = new CTableInfo();
$table->setHeader($header);
foreach ($hosts as $num => $host) {
    $hostid = $host['hostid'];
    $groupid = isset($hostsgroups[$hostid]) ? $hostsgroups[$hostid] : 0;
    $link = 'groupid=' . $groupid . '&hostid=' . $hostid;
    if ($admin) {
        $pageBox = new CComboBox('hostpages_' . $hostid);
        $pageBox->addItem('hosts.php?form=update&config=0&' . $link, S_HOST);
        $pageBox->addItem('items.php?' . $link, S_ITEMS);
        $pageBox->addItem('triggers.php?' . $link, S_TRIGGERS);
        $pageBox->addItem('graphs.php?' . $link, S_GRAPHS);
Ejemplo n.º 4
0
 private function _initHosts($hostid, $options)
 {
     $this->data['hosts'] = array();
     if (!$this->groupsSelected) {
         $hostid = 0;
     } else {
         $def_options = array('nodeids' => $this->config['all_nodes'] ? get_current_nodeid() : null, 'output' => array('hostid', 'host'), 'groupids' => $this->groupid > 0 ? $this->groupid : null);
         $options = zbx_array_merge($def_options, $options);
         $hosts = CHost::get($options);
         order_result($hosts, 'host');
         foreach ($hosts as $host) {
             $this->data['hosts'][$host['hostid']] = $host['host'];
         }
         if (is_null($hostid)) {
             $hostid = $this->_profileIds['hostid'];
         }
         if (!isset($this->data['hosts'][$hostid]) && $hostid > 0 || is_null($hostid)) {
             if ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_NONE) {
                 $hostid = 0;
             } else {
                 if (is_null($this->_requestIds['hostid']) || $this->_requestIds['hostid'] > 0) {
                     $hostids = array_keys($this->data['hosts']);
                     $hostid = empty($hostids) ? 0 : reset($hostids);
                 }
             }
         }
     }
     if (!is_null($this->_requestIds['hostid'])) {
         CProfile::update($this->_profileIdx['hosts'], $hostid, PROFILE_TYPE_ID);
         CProfile::update(self::HOST_LATEST_IDX, $hostid, PROFILE_TYPE_ID);
     }
     $this->isSelected['hostsSelected'] = $this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_ALL && !empty($this->data['hosts']) || $hostid > 0;
     $this->isSelected['hostsAll'] = $this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_ALL && !empty($this->data['hosts']) && $hostid == 0;
     $this->ids['hostid'] = $hostid;
 }
Ejemplo n.º 5
0
 /**
  * Get graph data
  *
  * <code>
  * $options = array(
  *	array 'graphids'				=> array(graphid1, graphid2, ...),
  *	array 'itemids'					=> array(itemid1, itemid2, ...),
  *	array 'hostids'					=> array(hostid1, hostid2, ...),
  *	int 'type'					=> 'graph type, chart/pie'
  *	boolean 'templated_graphs'			=> 'only templated graphs',
  *	int 'count'					=> 'count',
  *	string 'pattern'				=> 'search hosts by pattern in graph names',
  *	integer 'limit'					=> 'limit selection',
  *	string 'order'					=> 'depricated parameter (for now)'
  * );
  * </code>
  *
  * @static
  * @param array $options
  * @return array|boolean host data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $result = array();
     $sort_columns = array('graphid');
     // allowed columns for sorting
     $sql_parts = array('select' => array('graphs' => 'g.graphid'), 'from' => array('graphs g'), 'where' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => 0, 'groupids' => 0, 'hostids' => 0, 'graphids' => 0, 'itemids' => 0, 'type' => 0, 'templated_graphs' => 0, 'editable' => 0, 'nopermission' => 0, 'select_hosts' => 0, 'select_templates' => 0, 'select_items' => 0, 'extendoutput' => 0, 'count' => 0, 'pattern' => '', 'limit' => 0, 'order' => '');
     $options = array_merge($def_options, $options);
     // editable + PERMISSION CHECK
     if (defined('ZBX_API_REQUEST')) {
         $options['nopermissions'] = false;
     }
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql_parts['from']['gi'] = 'graphs_items gi';
         $sql_parts['from']['i'] = 'items i';
         $sql_parts['from']['hg'] = 'hosts_groups hg';
         $sql_parts['from']['r'] = 'rights r';
         $sql_parts['from']['ug'] = 'users_groups ug';
         $sql_parts['where']['gig'] = 'gi.graphid=g.graphid';
         $sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where'][] = 'r.id=hg.groupid ';
         $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
         $sql_parts['where'][] = 'ug.userid=' . $userid;
         $sql_parts['where'][] = 'r.permission>=' . $permission;
         $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT gii.graphid ' . ' FROM graphs_items gii, items ii ' . ' WHERE gii.graphid=g.graphid ' . ' AND gii.itemid=ii.itemid ' . ' AND EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups ugg ' . ' WHERE ii.hostid=hgg.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=ugg.usrgrpid ' . ' AND ugg.userid=' . $userid . ' AND rr.permission<' . $permission . '))';
     }
     // nodeids
     $nodeids = $options['nodeids'] ? $options['nodeids'] : get_current_nodeid(false);
     // groupids
     if ($options['groupids'] != 0) {
         zbx_value2array($options['groupids']);
         if ($options['extendoutput'] != 0) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['gi'] = 'graphs_items gi';
         $sql_parts['from']['i'] = 'items i';
         $sql_parts['from']['hg'] = 'hosts_groups hg';
         $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']);
         $sql_parts['where'][] = 'hg.hostid=i.hostid';
         $sql_parts['where']['gig'] = 'gi.graphid=g.graphid';
         $sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
     }
     // hostids
     if ($options['hostids'] != 0) {
         zbx_value2array($options['hostids']);
         if ($options['extendoutput'] != 0) {
             $sql_parts['select']['hostid'] = 'i.hostid';
         }
         $sql_parts['from']['gi'] = 'graphs_items gi';
         $sql_parts['from']['i'] = 'items i';
         $sql_parts['where'][] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['gig'] = 'gi.graphid=g.graphid';
         $sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
     }
     // graphids
     if ($options['graphids'] != 0) {
         $sql_parts['where'][] = DBcondition('g.graphid', $options['graphids']);
     }
     // itemids
     if ($options['itemids'] != 0) {
         zbx_value2array($options['itemids']);
         if ($options['extendoutput'] != 0) {
             $sql_parts['select']['itemid'] = 'gi.itemid';
         }
         $sql_parts['from']['gi'] = 'graphs_items gi';
         $sql_parts['where']['gig'] = 'gi.graphid=g.graphid';
         $sql_parts['where'][] = DBcondition('gi.itemid', $options['itemids']);
     }
     // type
     if ($options['type'] != 0) {
         $sql_parts['where'][] = 'g.type=' . $options['type'];
     }
     // templated_graphs
     if ($options['templated_graphs'] != 0) {
         $sql_parts['where'][] = 'g.templateid<>0';
     }
     // extendoutput
     if ($options['extendoutput'] != 0) {
         $sql_parts['select']['graphs'] = 'g.*';
     }
     // count
     if ($options['count'] != 0) {
         $sql_parts['select']['graphs'] = 'count(g.graphid) as count';
     }
     // pattern
     if (!zbx_empty($options['pattern'])) {
         $sql_parts['where'][] = ' UPPER(g.name) LIKE ' . zbx_dbstr('%' . strtoupper($options['pattern']) . '%');
     }
     // order
     // restrict not allowed columns for sorting
     $options['order'] = in_array($options['order'], $sort_columns) ? $options['order'] : '';
     if (!zbx_empty($options['order'])) {
         $sql_parts['order'][] = 'g.' . $options['order'];
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //------------
     $graphids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('g.graphid', $nodeids) . $sql_where . $sql_order;
     $db_res = DBselect($sql, $sql_limit);
     while ($graph = DBfetch($db_res)) {
         if ($options['count']) {
             $result = $graph;
         } else {
             if (!$options['extendoutput']) {
                 $result[$graph['graphid']] = $graph['graphid'];
             } else {
                 $graphids[$graph['graphid']] = $graph['graphid'];
                 if (!isset($result[$graph['graphid']])) {
                     $result[$graph['graphid']] = array();
                 }
                 if ($options['select_hosts'] && !isset($result[$graph['graphid']]['hostids'])) {
                     $result[$graph['graphid']]['hostids'] = array();
                     $result[$graph['graphid']]['hosts'] = array();
                 }
                 if ($options['select_templates'] && !isset($result[$graph['graphid']]['templateids'])) {
                     $result[$graph['graphid']]['templateids'] = array();
                     $result[$graph['graphid']]['templates'] = array();
                 }
                 if ($options['select_items'] && !isset($result[$graph['graphid']]['itemids'])) {
                     $result[$graph['graphid']]['itemids'] = array();
                     $result[$graph['graphid']]['items'] = array();
                 }
                 // hostids
                 if (isset($graph['hostid'])) {
                     if (!isset($result[$graph['graphid']]['hostids'])) {
                         $result[$graph['graphid']]['hostids'] = array();
                     }
                     $result[$graph['graphid']]['hostids'][$graph['hostid']] = $graph['hostid'];
                     unset($graph['hostid']);
                 }
                 // itemids
                 if (isset($graph['itemid'])) {
                     if (!isset($result[$graph['graphid']]['itemid'])) {
                         $result[$graph['graphid']]['itemid'] = array();
                     }
                     $result[$graph['graphid']]['itemids'][$graph['itemid']] = $graph['itemid'];
                     unset($graph['itemid']);
                 }
                 $result[$graph['graphid']] += $graph;
             }
         }
     }
     // Adding Hosts
     if ($options['select_hosts']) {
         $obj_params = array('extendoutput' => 1, 'graphids' => $graphids, 'nopermissions' => 1);
         $hosts = CHost::get($obj_params);
         foreach ($hosts as $hostid => $host) {
             foreach ($host['graphids'] as $num => $graphid) {
                 $result[$graphid]['hostids'][$hostid] = $hostid;
                 $result[$graphid]['hosts'][$hostid] = $host;
             }
         }
     }
     // Adding Templates
     if ($options['select_templates']) {
         $obj_params = array('extendoutput' => 1, 'graphids' => $graphids, 'nopermissions' => 1);
         $templates = CTemplate::get($obj_params);
         foreach ($templates as $templateid => $template) {
             foreach ($template['graphids'] as $num => $graphid) {
                 $result[$graphid]['templateids'][$templateid] = $templateid;
                 $result[$graphid]['templates'][$templateid] = $template;
             }
         }
     }
     // Adding Items
     if ($options['select_items']) {
         $obj_params = array('extendoutput' => 1, 'graphids' => $graphids, 'nopermissions' => 1);
         $items = CItem::get($obj_params);
         foreach ($items as $itemid => $item) {
             foreach ($item['graphids'] as $num => $graphid) {
                 $result[$graphid]['itemids'][$itemid] = $itemid;
                 $result[$graphid]['items'][$itemid] = $item;
             }
         }
     }
     return $result;
 }
Ejemplo n.º 6
0
        $hostprof_wdgt->addItem(insert_host_profile_ext_form());
    } else {
        $hostprof_wdgt->addItem(insert_host_profile_form());
    }
} else {
    $sortfield = getPageSortField('host');
    $sortorder = getPageSortOrder();
    $options = array('extendoutput' => 1, 'sortfield' => $sortfield, 'sortorder' => $sortorder, 'select_profile' => 1, 'select_groups' => 1, 'limit' => $config['search_limit'] + 1);
    if ($pageFilter->groupsSelected) {
        if ($pageFilter->groupid > 0) {
            $options['groupids'] = $pageFilter->groupid;
        }
    } else {
        $options['groupids'] = array();
    }
    $hosts = CHost::get($options);
    // unset hosts without profiles, and copy some profile fileds to the uppers array level for sorting
    $pr = $_REQUEST['prof_type'] == 0 ? 'profile' : 'profile_ext';
    $profile = array();
    foreach ($hosts as $num => $host) {
        if (empty($host[$pr])) {
            unset($hosts[$num]);
        } else {
            if ($_REQUEST['prof_type'] == 0) {
                $hosts[$num]['pr_name'] = $host['profile']['name'];
                $hosts[$num]['pr_os'] = $host['profile']['os'];
                $hosts[$num]['pr_serialno'] = $host['profile']['serialno'];
                $hosts[$num]['pr_tag'] = $host['profile']['tag'];
                $hosts[$num]['pr_macaddress'] = $host['profile']['macaddress'];
            } else {
                $hosts[$num]['pre_device_os_short'] = $host['profile_ext']['device_os_short'];
Ejemplo n.º 7
0
 private static function host($action, $params)
 {
     CHost::$error = array();
     switch ($action) {
         case 'add':
             $result = CHost::add($params);
             break;
         case 'get':
             $result = CHost::get($params);
             break;
         case 'getById':
             $result = CHost::getById($params);
             break;
         case 'getId':
             $result = CHost::getId($params);
             break;
         case 'update':
             $result = CHost::update($params);
             break;
         case 'massUpdate':
             $result = CHost::massUpdate($params);
             break;
         case 'delete':
             $result = CHost::delete($params);
             break;
         default:
             self::$result = array('error' => ZBX_API_ERROR_NO_METHOD, 'data' => 'Method: "' . $action . '" doesn\'t exist.');
             return;
             //exit function
     }
     if ($result !== false) {
         self::$result = array('result' => $result);
     } else {
         self::$result = CHost::$error;
     }
 }
Ejemplo n.º 8
0
 /**
  * Add item
  *
  * @param array $items
  * @return array|boolean
  */
 public static function create($items)
 {
     $items = zbx_toArray($items);
     $itemids = array();
     try {
         self::BeginTransaction(__METHOD__);
         $dbHosts = CHost::get(array('output' => API_OUTPUT_SHORTEN, 'hostids' => zbx_objectValues($items, 'hostid'), 'templated_hosts' => true, 'editable' => true, 'preservekeys' => true));
         foreach ($items as $inum => $item) {
             if (!isset($dbHosts[$item['hostid']])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, S_NO_PERMISSIONS);
             }
             $result = add_item($item);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot create Item');
             }
             $itemids[] = $result;
         }
         self::EndTransaction(true, __METHOD__);
         return array('itemids' => $itemids);
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }
Ejemplo n.º 9
0
function make_webmon_overview($filter)
{
    $options = array('groupids' => $filter['groupids'], 'monitored_hosts' => 1, 'filter' => array('maintenance_status' => $filter['maintenance']));
    $available_hosts = CHost::get($options);
    $available_hosts = zbx_objectValues($available_hosts, 'hostid');
    $table = new CTableInfo();
    $table->setHeader(array(is_show_all_nodes() ? S_NODE : null, S_HOST_GROUP, S_OK, S_FAILED, S_IN_PROGRESS, S_UNKNOWN));
    $options = array('monitored_hosts' => 1, 'with_monitored_httptests' => 1, 'output' => API_OUTPUT_EXTEND);
    $groups = CHostGroup::get($options);
    foreach ($groups as $gnum => $group) {
        $showGroup = false;
        $apps['ok'] = 0;
        $apps['failed'] = 0;
        $apps[HTTPTEST_STATE_BUSY] = 0;
        $apps[HTTPTEST_STATE_UNKNOWN] = 0;
        $sql = 'SELECT DISTINCT ht.name, ht.httptestid, ht.curstate, ht.lastfailedstep ' . ' FROM httptest ht, applications a, hosts_groups hg, groups g ' . ' WHERE g.groupid=' . $group['groupid'] . ' AND ' . DBcondition('hg.hostid', $available_hosts) . ' AND hg.groupid=g.groupid ' . ' AND a.hostid=hg.hostid ' . ' AND ht.applicationid=a.applicationid ' . ' AND ht.status=' . HTTPTEST_STATUS_ACTIVE;
        $db_httptests = DBselect($sql);
        while ($httptest_data = DBfetch($db_httptests)) {
            $showGroup = true;
            if (HTTPTEST_STATE_BUSY == $httptest_data['curstate']) {
                $apps[HTTPTEST_STATE_BUSY]++;
            } else {
                if (HTTPTEST_STATE_IDLE == $httptest_data['curstate']) {
                    if ($httptest_data['lastfailedstep'] > 0) {
                        $apps['failed']++;
                    } else {
                        $apps['ok']++;
                    }
                } else {
                    $apps[HTTPTEST_STATE_UNKNOWN]++;
                }
            }
        }
        if (!$showGroup) {
            continue;
        }
        $table->addRow(array(is_show_all_nodes() ? get_node_name_by_elid($group['groupid']) : null, $group['name'], new CSpan($apps['ok'], 'off'), new CSpan($apps['failed'], $apps['failed'] ? 'on' : 'off'), new CSpan($apps[HTTPTEST_STATE_BUSY], $apps[HTTPTEST_STATE_BUSY] ? 'orange' : 'off'), new CSpan($apps[HTTPTEST_STATE_UNKNOWN], 'unknown')));
    }
    $table->setFooter(new CCol(S_UPDATED . ': ' . zbx_date2str(S_BLOCKS_WEBMON_TIME_FORMAT)));
    return $table;
}
Ejemplo n.º 10
0
 $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));
 $numrows = new CSpan(null, 'info');
 $numrows->setAttribute('name', 'numrows');
 $header = get_table_header(array(S_TRIGGERS_BIG, new CSpan(SPACE . SPACE . '|' . SPACE . SPACE, 'divider'), S_FOUND . ': ', $numrows));
 show_table_header($header, $r_form);
 // <<<--- SELECTED HOST HEADER INFORMATION --->>>
 if ($PAGE_HOSTS['selected'] > 0) {
     $header_host = CHost::get(array('hostids' => $PAGE_HOSTS['selected'], 'nopermissions' => 1, 'extendoutput' => 1, 'select_items' => 1, 'select_graphs' => 1));
     $header_host = array_pop($header_host);
     $description = array();
     if ($header_host['proxy_hostid']) {
         $proxy = get_host_by_hostid($header_host['proxy_hostid']);
         $description[] = $proxy['host'] . ':';
     }
     $description[] = new CLink($header_host['host'], 'hosts.php?form=update&hostid=' . $header_host['hostid'] . url_param('groupid'));
     $items = array(new CLink(S_ITEMS, 'items.php?groupid=' . $PAGE_GROUPS['selected'] . '&hostid=' . $header_host['hostid']), ' (' . count($header_host['itemids']) . ')');
     $graphs = array(new CLink(S_GRAPHS, 'graphs.php?groupid=' . $PAGE_GROUPS['selected'] . '&hostid=' . $header_host['hostid']), ' (' . count($header_host['graphids']) . ')');
     $dns = empty($header_host['dns']) ? '-' : $header_host['dns'];
     $ip = empty($header_host['ip']) ? '-' : $header_host['ip'];
     $port = empty($header_host['port']) ? '-' : $header_host['port'];
     if (1 == $header_host['useip']) {
         $ip = bold($ip);
     } else {
Ejemplo n.º 11
0
function validate_condition($conditiontype, $value)
{
    global $USER_DETAILS;
    switch ($conditiontype) {
        case CONDITION_TYPE_HOST_GROUP:
            $groups = CHostGroup::get(array('groupids' => $value, 'output' => API_OUTPUT_SHORTEN, 'nodeids' => get_current_nodeid(true)));
            if (empty($groups)) {
                error(S_INCORRECT_GROUP);
                return false;
            }
            break;
        case CONDITION_TYPE_HOST_TEMPLATE:
            $templates = CTemplate::get(array('templateids' => $value, 'output' => API_OUTPUT_SHORTEN, 'nodeids' => get_current_nodeid(true)));
            if (empty($templates)) {
                error(S_INCORRECT_HOST);
                return false;
            }
            break;
        case CONDITION_TYPE_TRIGGER:
            $triggers = CTrigger::get(array('triggerids' => $value, 'output' => API_OUTPUT_SHORTEN, 'nodeids' => get_current_nodeid(true)));
            if (empty($triggers)) {
                error(S_INCORRECT_TRIGGER);
                return false;
            }
            break;
        case CONDITION_TYPE_HOST:
            $hosts = CHost::get(array('hostids' => $value, 'output' => API_OUTPUT_SHORTEN, 'nodeids' => get_current_nodeid(true)));
            if (empty($hosts)) {
                error(S_INCORRECT_HOST);
                return false;
            }
            break;
        case CONDITION_TYPE_TIME_PERIOD:
            if (!validate_period($value)) {
                error(S_INCORRECT_PERIOD . ' [' . $value . ']');
                return false;
            }
            break;
        case CONDITION_TYPE_DHOST_IP:
            if (!validate_ip_range($value)) {
                error(S_INCORRECT_IP . ' [' . $value . ']');
                return false;
            }
            break;
        case CONDITION_TYPE_DSERVICE_TYPE:
            if (S_UNKNOWN == discovery_check_type2str($value)) {
                error(S_INCORRECT_DISCOVERY_CHECK);
                return false;
            }
            break;
        case CONDITION_TYPE_DSERVICE_PORT:
            if (!validate_port_list($value)) {
                error(S_INCORRECT_PORT . ' [' . $value . ']');
                return false;
            }
            break;
        case CONDITION_TYPE_DSTATUS:
            if (S_UNKNOWN == discovery_object_status2str($value)) {
                error(S_INCORRECT_DISCOVERY_STATUS);
                return false;
            }
            break;
        case CONDITION_TYPE_EVENT_ACKNOWLEDGED:
            if (S_UNKNOWN == condition_value2str($conditiontype, $value)) {
                error(S_INCORRECT_DISCOVERY_STATUS);
                return false;
            }
            break;
        case CONDITION_TYPE_TRIGGER_NAME:
        case CONDITION_TYPE_TRIGGER_VALUE:
        case CONDITION_TYPE_TRIGGER_SEVERITY:
        case CONDITION_TYPE_MAINTENANCE:
        case CONDITION_TYPE_NODE:
        case CONDITION_TYPE_DRULE:
        case CONDITION_TYPE_DCHECK:
        case CONDITION_TYPE_DOBJECT:
        case CONDITION_TYPE_PROXY:
        case CONDITION_TYPE_DUPTIME:
        case CONDITION_TYPE_DVALUE:
        case CONDITION_TYPE_APPLICATION:
        case CONDITION_TYPE_HOST_NAME:
            break;
        default:
            error(S_INCORRECT_CONDITION_TYPE);
            return false;
            break;
    }
    return true;
}
Ejemplo n.º 12
0
 public static function getMacros($macros, $options)
 {
     zbx_value2array($macros);
     $macros = array_unique($macros);
     $result = array();
     $obj_options = array('itemids' => isset($options['itemid']) ? $options['itemid'] : null, 'triggerids' => isset($options['triggerid']) ? $options['triggerid'] : null, 'nopermissions' => 1, 'preservekeys' => 1, 'output' => API_OUTPUT_SHORTEN, 'templated_hosts' => true);
     $hosts = CHost::get($obj_options);
     $hostids = array_keys($hosts);
     do {
         $obj_options = array('hostids' => $hostids, 'macros' => $macros, 'output' => API_OUTPUT_EXTEND, 'nopermissions' => 1, 'preservekeys' => 1);
         $host_macros = self::get($obj_options);
         order_result($host_macros, 'hostid');
         foreach ($macros as $mnum => $macro) {
             foreach ($host_macros as $hmnum => $hmacro) {
                 if ($macro == $hmacro['macro']) {
                     $result[$macro] = $hmacro['value'];
                     unset($host_macros[$hmnum], $macros[$mnum]);
                     break;
                 }
             }
         }
         if (!empty($macros)) {
             $obj_options = array('hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1, 'output' => API_OUTPUT_SHORTEN);
             $hosts = CTemplate::get($obj_options);
             $hostids = array_keys($hosts);
         }
     } while (!empty($macros) && !empty($hostids));
     if (!empty($macros)) {
         $obj_options = array('output' => API_OUTPUT_EXTEND, 'globalmacro' => 1, 'nopermissions' => 1, 'macros' => $macros);
         $gmacros = self::get($obj_options);
         foreach ($macros as $macro) {
             foreach ($gmacros as $mid => $gmacro) {
                 if ($macro == $gmacro['macro']) {
                     $result[$macro] = $gmacro['value'];
                     unset($gmacros[$mid]);
                     break;
                 }
             }
         }
     }
     return $result;
 }
Ejemplo n.º 13
0
    /**
     * Get HostGroups
     *
     * {@source}
     * @access public
     * @static
     * @since 1.8
     * @version 1
     *
     * @static
     * @param array $params
     * @return array
     */
    public static function get($params)
    {
        global $USER_DETAILS;
        $result = array();
        $user_type = $USER_DETAILS['type'];
        $userid = $USER_DETAILS['userid'];
        $sort_columns = array('groupid', 'name');
        // allowed columns for sorting
        $sql_parts = array('select' => array('groups' => 'g.groupid'), 'from' => array('groups g'), 'where' => array(), 'order' => array(), 'limit' => null);
        $def_options = array('nodeids' => 0, 'groupids' => 0, 'hostids' => 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, 'editable' => 0, 'nopermissions' => 0, 'select_hosts' => 0, 'extendoutput' => 0, 'count' => 0, 'pattern' => '', 'order' => '', 'limit' => 0);
        $options = array_merge($def_options, $params);
        // *** ????? *** //
        // nodes
        // disabled by false
        // TODO('check this ~106');
        if (false && ZBX_DISTRIBUTED) {
            $sql_parts['select'][] = 'n.name as node_name';
            $sql_parts['from'][] = 'nodes n';
            $sql_parts['where'][] = 'n.nodeid=' . DBid2nodeid('g.groupid');
            $sql_parts['order'][] = 'node_name';
        }
        // *** ????? *** //
        // editable + PERMISSION CHECK
        if (defined('ZBX_API_REQUEST')) {
            $options['nopermissions'] = false;
        }
        if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
        } else {
            $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
            $sql_parts['from']['r'] = 'rights r';
            $sql_parts['from']['ug'] = 'users_groups ug';
            $sql_parts['where'][] = 'r.id=g.groupid';
            $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
            $sql_parts['where'][] = 'ug.userid=' . $userid;
            $sql_parts['where'][] = 'r.permission>=' . $permission;
            $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT gg.groupid ' . ' FROM groups gg, rights rr, users_groups ugg ' . ' WHERE rr.id=g.groupid ' . ' AND rr.groupid=ugg.usrgrpid ' . ' AND ugg.userid=' . $userid . ' AND rr.permission<' . $permission . ')';
        }
        // nodeids
        $nodeids = $options['nodeids'] ? $options['nodeids'] : get_current_nodeid(false);
        // groupids
        if ($options['groupids'] != 0) {
            zbx_value2array($options['groupids']);
            $sql_parts['where'][] = DBcondition('g.groupid', $options['groupids']);
        }
        // hostids
        if ($options['hostids'] != 0) {
            zbx_value2array($options['hostids']);
            $sql_parts['from']['hg'] = 'hosts_groups hg';
            $sql_parts['where'][] = DBcondition('hg.hostid', $options['hostids']);
            $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
        }
        // monitored_hosts, real_hosts, templated_hosts, not_proxy_hosts
        if ($options['monitored_hosts'] != 0) {
            $sql_parts['from']['hg'] = 'hosts_groups hg';
            $sql_parts['from']['h'] = 'hosts h';
            $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
            $sql_parts['where'][] = 'h.hostid=hg.hostid';
            $sql_parts['where'][] = 'h.status=' . HOST_STATUS_MONITORED;
        } else {
            if ($options['real_hosts'] != 0) {
                $sql_parts['from']['hg'] = 'hosts_groups hg';
                $sql_parts['from']['h'] = 'hosts h';
                $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
                $sql_parts['where'][] = 'h.hostid=hg.hostid';
                $sql_parts['where'][] = 'h.status IN(' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')';
            } else {
                if ($options['templated_hosts'] != 0) {
                    $sql_parts['from']['hg'] = 'hosts_groups hg';
                    $sql_parts['from']['h'] = 'hosts h';
                    $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
                    $sql_parts['where'][] = 'h.hostid=hg.hostid';
                    $sql_parts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
                } else {
                    if ($options['not_proxy_hosts'] != 0) {
                        $sql_parts['from']['hg'] = 'hosts_groups hg';
                        $sql_parts['from']['h'] = 'hosts h';
                        $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
                        $sql_parts['where'][] = 'h.hostid=hg.hostid';
                        $sql_parts['where'][] = 'h.status<>' . HOST_STATUS_PROXY;
                    }
                }
            }
        }
        // with_items, with_monitored_items, with_historical_items
        if ($options['with_items'] != 0) {
            $sql_parts['from']['hg'] = 'hosts_groups hg';
            $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
            $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE hg.hostid=i.hostid )';
        } else {
            if ($options['with_monitored_items'] != 0) {
                $sql_parts['from']['hg'] = 'hosts_groups hg';
                $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
                $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE hg.hostid=i.hostid AND i.status=' . ITEM_STATUS_ACTIVE . ')';
            } else {
                if ($options['with_historical_items'] != 0) {
                    $sql_parts['from']['hg'] = 'hosts_groups hg';
                    $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
                    $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE hg.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 ($options['with_triggers'] != 0) {
            $sql_parts['from']['hg'] = 'hosts_groups hg';
            $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
            $sql_parts['where'][] = 'EXISTS( SELECT t.triggerid ' . ' FROM items i, functions f, triggers t' . ' WHERE i.hostid=hg.hostid ' . ' AND f.itemid=i.itemid ' . ' AND t.triggerid=f.triggerid)';
        } else {
            if ($options['with_monitored_triggers'] != 0) {
                $sql_parts['from']['hg'] = 'hosts_groups hg';
                $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
                $sql_parts['where'][] = 'EXISTS( SELECT t.triggerid ' . ' FROM items i, functions f, triggers t' . ' WHERE i.hostid=hg.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 ($options['with_httptests'] != 0) {
            $sql_parts['from']['hg'] = 'hosts_groups hg';
            $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
            $sql_parts['where'][] = 'EXISTS( SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=hg.hostid ' . ' AND ht.applicationid=a.applicationid)';
        } else {
            if ($options['with_monitored_httptests'] != 0) {
                $sql_parts['from']['hg'] = 'hosts_groups hg';
                $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
                $sql_parts['where'][] = 'EXISTS( SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=hg.hostid ' . ' AND ht.applicationid=a.applicationid ' . ' AND ht.status=' . HTTPTEST_STATUS_ACTIVE . ')';
            }
        }
        // with_graphs
        if ($options['with_graphs'] != 0) {
            $sql_parts['from']['hg'] = 'hosts_groups hg';
            $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
            $sql_parts['where'][] = 'EXISTS( SELECT DISTINCT i.itemid ' . ' FROM items i, graphs_items gi ' . ' WHERE i.hostid=hg.hostid ' . ' AND i.itemid=gi.itemid)';
        }
        // extendoutput
        if ($options['extendoutput'] != 0) {
            $sql_parts['select']['groups'] = 'g.*';
        }
        // count
        if ($options['count'] != 0) {
            $sql_parts['select']['groups'] = 'COUNT(g.groupid) as rowscount';
        }
        // pattern
        if (!zbx_empty($options['pattern'])) {
            $sql_parts['where'][] = ' UPPER(g.name) LIKE ' . zbx_dbstr('%' . strtoupper($options['pattern']) . '%');
        }
        // order
        // restrict not allowed columns for sorting
        $options['order'] = in_array($options['order'], $sort_columns) ? $options['order'] : '';
        if (!zbx_empty($options['order'])) {
            $sql_parts['order'][] = 'g.' . $options['order'];
            if (!str_in_array('g.' . $options['order'], $sql_parts['select'])) {
                $sql_parts['select'][] = 'g.' . $options['order'];
            }
        }
        // limit
        if (zbx_ctype_digit($options['limit']) && $options['limit']) {
            $sql_parts['limit'] = $options['limit'];
        }
        //-----------
        $groupids = array();
        $sql_parts['select'] = array_unique($sql_parts['select']);
        $sql_parts['from'] = array_unique($sql_parts['from']);
        $sql_parts['where'] = array_unique($sql_parts['where']);
        $sql_parts['order'] = array_unique($sql_parts['order']);
        $sql_select = '';
        $sql_from = '';
        $sql_where = '';
        $sql_order = '';
        if (!empty($sql_parts['select'])) {
            $sql_select .= implode(',', $sql_parts['select']);
        }
        if (!empty($sql_parts['from'])) {
            $sql_from .= implode(',', $sql_parts['from']);
        }
        if (!empty($sql_parts['where'])) {
            $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
        }
        if (!empty($sql_parts['order'])) {
            $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
        }
        $sql_limit = $sql_parts['limit'];
        $sql = 'SELECT ' . $sql_select . '
				FROM ' . $sql_from . '
				WHERE ' . DBin_node('g.groupid', $nodeids) . $sql_where . $sql_order;
        $res = DBselect($sql, $sql_limit);
        while ($group = DBfetch($res)) {
            if ($options['count']) {
                $result = $group;
            } else {
                if (!$options['extendoutput']) {
                    $result[$group['groupid']] = $group['groupid'];
                } else {
                    $groupids[$group['groupid']] = $group['groupid'];
                    if (!isset($result[$group['groupid']])) {
                        $result[$group['groupid']] = array();
                    }
                    if ($options['select_hosts'] && !isset($result[$group['groupid']]['hostids'])) {
                        $result[$group['groupid']]['hostids'] = array();
                        $result[$group['groupid']]['hosts'] = array();
                    }
                    $result[$group['groupid']] += $group;
                }
            }
        }
        // Adding hosts
        if ($options['select_hosts']) {
            $obj_params = array('extendoutput' => 1, 'groupids' => $groupids, 'templated_hosts' => 1);
            $hosts = CHost::get($obj_params);
            foreach ($hosts as $hostid => $host) {
                foreach ($host['groupids'] as $num => $groupid) {
                    $result[$groupid]['hostids'][$hostid] = $hostid;
                    $result[$groupid]['hosts'][$hostid] = $host;
                }
            }
        }
        return $result;
    }
Ejemplo n.º 14
0
 protected static function checkItems($screenitems)
 {
     $hostgroups = array();
     $hosts = array();
     $graphs = array();
     $items = array();
     $maps = array();
     $screens = array();
     $resources = array(SCREEN_RESOURCE_GRAPH, SCREEN_RESOURCE_SIMPLE_GRAPH, SCREEN_RESOURCE_PLAIN_TEXT, SCREEN_RESOURCE_MAP, SCREEN_RESOURCE_SCREEN, SCREEN_RESOURCE_TRIGGERS_OVERVIEW, SCREEN_RESOURCE_DATA_OVERVIEW);
     foreach ($screenitems as $item) {
         if (isset($item['resourcetype']) && !isset($item['resourceid']) || !isset($item['resourcetype']) && isset($item['resourceid'])) {
             self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
         }
         if (isset($item['resourceid']) && $item['resourceid'] == 0) {
             if (uint_in_array($item['resourcetype'], $resources)) {
                 throw new Exception(S_INCORRECT_RESOURCE_PROVIDED_FOR_SCREEN_ITEM);
             } else {
                 continue;
             }
         }
         switch ($item['resourcetype']) {
             case SCREEN_RESOURCE_HOSTS_INFO:
             case SCREEN_RESOURCE_TRIGGERS_INFO:
             case SCREEN_RESOURCE_TRIGGERS_OVERVIEW:
             case SCREEN_RESOURCE_DATA_OVERVIEW:
             case SCREEN_RESOURCE_HOSTGROUP_TRIGGERS:
                 $hostgroups[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_HOST_TRIGGERS:
                 $hosts[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_GRAPH:
                 $graphs[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_SIMPLE_GRAPH:
             case SCREEN_RESOURCE_PLAIN_TEXT:
                 $items[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_MAP:
                 $maps[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_SCREEN:
                 $screens[] = $item['resourceid'];
                 break;
         }
     }
     if (!empty($hostgroups)) {
         $result = CHostGroup::get(array('groupids' => $hostgroups, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($hostgroups as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Host group identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($hosts)) {
         $result = CHost::get(array('hostids' => $hosts, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($hosts as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Host identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($graphs)) {
         $result = CGraph::get(array('graphids' => $graphs, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($graphs as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Graph identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($items)) {
         $result = CItem::get(array('itemids' => $items, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1, 'webitems' => 1));
         foreach ($items as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Item identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($maps)) {
         $result = CMap::get(array('sysmapids' => $maps, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($maps as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Map identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($screens)) {
         $result = self::get(array('screenids' => $screens, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($screens as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Screen identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
 }
Ejemplo n.º 15
0
    /**
     * Get Proxy data
     *
     * {@source}
     * @access public
     * @static
     * @since 1.8
     * @version 1
     *
     * @param _array $options
     * @param array $options['nodeids']
     * @param array $options['proxyids']
     * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins
     * @param boolean $options['extendoutput'] return all fields
     * @param int $options['count'] returns value in rowscount
     * @param string $options['pattern']
     * @param int $options['limit']
     * @param string $options['sortfield']
     * @param string $options['sortorder']
     * @return array|boolean
     */
    public static function get($options = array())
    {
        global $USER_DETAILS;
        $result = array();
        $user_type = $USER_DETAILS['type'];
        $sort_columns = array('hostid', 'host', 'status');
        // allowed columns for sorting
        $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
        // allowed output options for [ select_* ] params
        $sql_parts = array('select' => array('hostid' => 'h.hostid'), 'from' => array('hosts' => 'hosts h'), 'where' => array('h.status IN (' . HOST_STATUS_PROXY_ACTIVE . ',' . HOST_STATUS_PROXY_PASSIVE . ')'), 'order' => array(), 'limit' => null);
        $def_options = array('nodeids' => null, 'proxyids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'extendoutput' => null, 'output' => API_OUTPUT_REFER, 'countOutput' => null, 'preservekeys' => null, 'select_hosts' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
        $options = zbx_array_merge($def_options, $options);
        if (!is_null($options['extendoutput'])) {
            $options['output'] = API_OUTPUT_EXTEND;
        }
        // editable + PERMISSION CHECK
        if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
        } else {
            $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
            if ($permission == PERM_READ_WRITE) {
                return array();
            }
        }
        // nodeids
        $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
        // proxyids
        if (!is_null($options['proxyids'])) {
            zbx_value2array($options['proxyids']);
            $sql_parts['where'][] = DBcondition('h.hostid', $options['proxyids']);
        }
        // filter
        if (is_array($options['filter'])) {
            zbx_db_filter('hosts h', $options, $sql_parts);
        }
        // search
        if (is_array($options['search'])) {
            zbx_db_search('hosts h', $options, $sql_parts);
        }
        // extendoutput
        if ($options['output'] == API_OUTPUT_EXTEND) {
            $sql_parts['select']['hostid'] = 'h.hostid';
            $sql_parts['select']['host'] = 'h.host';
            $sql_parts['select']['status'] = 'h.status';
            $sql_parts['select']['lastaccess'] = 'h.lastaccess';
        }
        // countOutput
        if (!is_null($options['countOutput'])) {
            $options['sortfield'] = '';
            $sql_parts['select'] = array('count(DISTINCT h.hostid) as rowscount');
        }
        // order
        // restrict not allowed columns for sorting
        $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
        if (!zbx_empty($options['sortfield'])) {
            $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
            $sql_parts['order'][] = 'h.' . $options['sortfield'] . ' ' . $sortorder;
            if (!str_in_array('h.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('h.*', $sql_parts['select'])) {
                $sql_parts['select'][] = 'h.' . $options['sortfield'];
            }
        }
        // limit
        if (zbx_ctype_digit($options['limit']) && $options['limit']) {
            $sql_parts['limit'] = $options['limit'];
        }
        //-------
        $proxyids = array();
        $sql_parts['select'] = array_unique($sql_parts['select']);
        $sql_parts['from'] = array_unique($sql_parts['from']);
        $sql_parts['where'] = array_unique($sql_parts['where']);
        $sql_parts['order'] = array_unique($sql_parts['order']);
        $sql_select = '';
        $sql_from = '';
        $sql_where = '';
        $sql_order = '';
        if (!empty($sql_parts['select'])) {
            $sql_select .= implode(',', $sql_parts['select']);
        }
        if (!empty($sql_parts['from'])) {
            $sql_from .= implode(',', $sql_parts['from']);
        }
        if (!empty($sql_parts['where'])) {
            $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
        }
        if (!empty($sql_parts['order'])) {
            $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
        }
        $sql_limit = $sql_parts['limit'];
        $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . '
				FROM ' . $sql_from . '
				WHERE ' . DBin_node('h.hostid', $nodeids) . $sql_where . $sql_order;
        // sdi($sql);
        $res = DBselect($sql, $sql_limit);
        while ($proxy = DBfetch($res)) {
            if ($options['countOutput']) {
                $result = $proxy['rowscount'];
            } else {
                $proxyids[$proxy['hostid']] = $proxy['hostid'];
                $proxy['proxyid'] = $proxy['hostid'];
                unset($proxy['hostid']);
                if ($options['output'] == API_OUTPUT_SHORTEN) {
                    $result[$proxy['proxyid']] = array('proxyid' => $proxy['proxyid']);
                } else {
                    if (!isset($result[$proxy['proxyid']])) {
                        $result[$proxy['proxyid']] = array();
                    }
                    if (!is_null($options['select_hosts']) && !isset($result[$proxy['proxyid']]['hosts'])) {
                        $result[$proxy['proxyid']]['hosts'] = array();
                    }
                    $result[$proxy['proxyid']] += $proxy;
                }
            }
        }
        if (!is_null($options['countOutput'])) {
            if (is_null($options['preservekeys'])) {
                $result = zbx_cleanHashes($result);
            }
            return $result;
        }
        // Adding Objects
        // select_hosts
        if (!empty($proxyids)) {
            if (!is_null($options['select_hosts']) && str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
                $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_hosts'], 'proxyids' => $proxyids, 'preservekeys' => 1);
                $hosts = CHost::get($obj_params);
                foreach ($hosts as $host) {
                    $result[$host['proxy_hostid']]['hosts'][] = $host;
                }
            }
        }
        // removing keys (hash -> array)
        if (is_null($options['preservekeys'])) {
            $result = zbx_cleanHashes($result);
        }
        return $result;
    }
Ejemplo n.º 16
0
 foreach ($all_groups as $group) {
     $cmbGroups->addItem($group['groupid'], $group['name']);
 }
 if (isset($_REQUEST['maintenanceid']) && !isset($_REQUEST['form_refresh'])) {
     $hostids = CHost::get(array('maintenanceids' => $_REQUEST['maintenanceid'], 'real_hosts' => 1, 'output' => API_OUTPUT_SHORTEN, 'editable' => 1));
     $hostids = zbx_objectValues($hostids, 'hostid');
 } else {
     $hostids = get_request('hostids', array());
 }
 $host_tb = new CTweenBox($frmMaintenance, 'hostids', $hostids, 10);
 // hosts from selected twb group
 $options = array('output' => API_OUTPUT_EXTEND, 'real_hosts' => 1, 'editable' => 1, 'groupids' => $twb_groupid);
 $hosts = CHost::get($options);
 // selected hosts
 $options = array('output' => API_OUTPUT_EXTEND, 'real_hosts' => 1, 'editable' => 1, 'hostids' => $hostids);
 $hosts_selected = CHost::get($options);
 $hosts = array_merge($hosts, $hosts_selected);
 $hosts = zbx_toHash($hosts, 'hostid');
 order_result($hosts, 'host');
 foreach ($hosts as $host) {
     $host_tb->addItem($host['hostid'], $host['host']);
 }
 $tblHlink = new CTable(null, 'formElementTable');
 $tblHlink->addRow($host_tb->Get(S_IN . SPACE . S_MAINTENANCE, array(S_OTHER . SPACE . S_HOSTS . SPACE . '|' . SPACE . S_GROUP . SPACE, $cmbGroups)));
 $right_tab->addRow(new CFormElement(S_HOSTS_IN_MAINTENANCE, $tblHlink));
 // }}} MAINTENANCE HOSTS
 // MAINTENANCE GROUPS {{{
 $tblGlink = new CTable(null, 'formElementTable');
 if (isset($_REQUEST['maintenanceid']) && !isset($_REQUEST['form_refresh'])) {
     $groupids = CHostGroup::get(array('maintenanceids' => $_REQUEST['maintenanceid'], 'real_hosts' => 1, 'output' => API_OUTPUT_SHORTEN, 'editable' => 1));
     $groupids = zbx_objectValues($groupids, 'groupid');
Ejemplo n.º 17
0
 /**
  * Get itemid by host.name and item.key
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8
  * @version 1
  *
  * @static
  * @param array $item_data
  * @param array $item_data['key_']
  * @param array $item_data['host']
  * @param array $item_data['hostid'] OPTIONAL
  * @return int|boolean
  */
 public static function getId($item_data)
 {
     if (isset($item_data['host'])) {
         $host = $item_data['host'];
     } else {
         $host = CHost::getById(array('hostid' => $item_data['hostid']));
         $host = $host['host'];
     }
     $item = get_item_by_key($item_data['key_'], $host);
     $result = $item ? true : false;
     if ($result) {
         return $item['itemid'];
     } else {
         self::$error = array('error' => ZBX_API_ERROR_NO_HOST, 'data' => 'Item doesn\'t exists.');
         return false;
     }
 }
Ejemplo n.º 18
0
 /**
  * Add Applications
  *
  * @param _array $applications
  * @param array $app_data['name']
  * @param array $app_data['hostid']
  * @return boolean
  */
 public static function create($applications)
 {
     $applications = zbx_toArray($applications);
     $applicationids = array();
     try {
         self::BeginTransaction(__METHOD__);
         $dbHosts = CHost::get(array('output' => API_OUTPUT_SHORTEN, 'hostids' => zbx_objectValues($applications, 'hostid'), 'templated_hosts' => true, 'editable' => true, 'preservekeys' => true));
         foreach ($applications as $anum => $application) {
             if (!isset($dbHosts[$application['hostid']])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, S_NO_PERMISSIONS);
             }
             $result = add_application($application['name'], $application['hostid']);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, S_CANNOT_CREATE_APPLICATION);
             }
             $applicationids[] = $result;
         }
         self::EndTransaction(true, __METHOD__);
         return array('applicationids' => $applicationids);
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }
Ejemplo n.º 19
0
function bar_report_form3()
{
    global $USER_DETAILS;
    $available_hosts = get_accessible_hosts_by_user($USER_DETAILS, PERM_READ_ONLY);
    $config = get_request('config', 1);
    $title = get_request('title', S_REPORT . ' 3');
    $xlabel = get_request('xlabel', '');
    $ylabel = get_request('ylabel', '');
    $sorttype = get_request('sorttype', 0);
    $scaletype = get_request('scaletype', TIMEPERIOD_TYPE_WEEKLY);
    $avgperiod = get_request('avgperiod', TIMEPERIOD_TYPE_DAILY);
    $report_timesince = get_request('report_timesince', date('YmdHis', time() - 86400));
    $report_timetill = get_request('report_timetill', date('YmdHis'));
    $captions = get_request('captions', array());
    $items = get_request('items', array());
    $hostids = get_request('hostids', array());
    $hostids = zbx_toHash($hostids);
    $showlegend = get_request('showlegend', 0);
    $palette = get_request('palette', 0);
    $palettetype = get_request('palettetype', 0);
    $reportForm = new CFormTable(null, null, 'get');
    //,'events.php?report_set=1','POST',null,'sform');
    $reportForm->setAttribute('name', 'zbx_report');
    $reportForm->setAttribute('id', 'zbx_report');
    //	$reportForm->setMethod('post');
    if (isset($_REQUEST['report_show']) && !empty($items)) {
        $reportForm->addVar('report_show', 'show');
    }
    $reportForm->addVar('config', $config);
    $reportForm->addVar('report_timesince', date('YmdHis', $report_timesince));
    $reportForm->addVar('report_timetill', date('YmdHis', $report_timetill));
    //	$reportForm->addVar('items',$items); 				//params are set later!!
    //	$reportForm->addVar('periods',$periods);
    $reportForm->addRow(S_TITLE, new CTextBox('title', $title, 40));
    $reportForm->addRow(S_X . SPACE . S_LABEL, new CTextBox('xlabel', $xlabel, 40));
    $reportForm->addRow(S_Y . SPACE . S_LABEL, new CTextBox('ylabel', $ylabel, 40));
    $reportForm->addRow(S_LEGEND, new CCheckBox('showlegend', $showlegend, null, 1));
    $reportForm->addVar('sortorder', 0);
    // GROUPS
    $groupids = get_request('groupids', array());
    $group_tb = new CTweenBox($reportForm, 'groupids', $groupids, 10);
    $options = array('real_hosts' => 1, 'output' => 'extend');
    $db_groups = CHostGroup::get($options);
    order_result($db_groups, 'name');
    foreach ($db_groups as $gnum => $group) {
        $groupids[$group['groupid']] = $group['groupid'];
        $group_tb->addItem($group['groupid'], $group['name']);
    }
    $reportForm->addRow(S_GROUPS, $group_tb->Get(S_SELECTED_GROUPS, S_OTHER . SPACE . S_GROUPS));
    // ----------
    // HOSTS
    //	validate_group(PERM_READ_ONLY,array('real_hosts'),'web.last.conf.groupid');
    $groupid = get_request('groupid', 0);
    $cmbGroups = new CComboBox('groupid', $groupid, 'submit()');
    $cmbGroups->addItem(0, S_ALL_S);
    foreach ($db_groups as $gnum => $group) {
        $cmbGroups->addItem($group['groupid'], $group['name']);
    }
    $td_groups = new CCol(array(S_GROUP, SPACE, $cmbGroups));
    $td_groups->setAttribute('style', 'text-align: right;');
    $host_tb = new CTweenBox($reportForm, 'hostids', $hostids, 10);
    $options = array('real_hosts' => 1, 'output' => array('hostid', 'host'));
    if ($groupid > 0) {
        $options['groupids'] = $groupid;
    }
    $db_hosts = CHost::get($options);
    $db_hosts = zbx_toHash($db_hosts, 'hostid');
    order_result($db_hosts, 'host');
    foreach ($db_hosts as $hnum => $host) {
        $host_tb->addItem($host['hostid'], $host['host']);
    }
    $options = array('real_hosts' => 1, 'output' => array('hostid', 'host'), 'hostids' => $hostids);
    $db_hosts2 = CHost::get($options);
    order_result($db_hosts2, 'host');
    foreach ($db_hosts2 as $hnum => $host) {
        if (!isset($db_hosts[$host['hostid']])) {
            $host_tb->addItem($host['hostid'], $host['host']);
        }
    }
    $reportForm->addRow(S_HOSTS, $host_tb->Get(S_SELECTED_HOSTS, array(S_OTHER . SPACE . S_HOSTS . SPACE . '|' . SPACE . S_GROUP . SPACE, $cmbGroups)));
    // ----------
    //*/
    // PERIOD
    $clndr_icon = new CImg('images/general/bar/cal.gif', 'calendar', 16, 12, 'pointer');
    $clndr_icon->addAction('onclick', 'javascript: ' . 'var pos = getPosition(this); ' . 'pos.top+=10; ' . 'pos.left+=16; ' . "CLNDR['avail_report_since'].clndr.clndrshow(pos.top,pos.left);");
    $reporttimetab = new CTable(null, 'calendar');
    $reporttimetab->setAttribute('width', '10%');
    $reporttimetab->setCellPadding(0);
    $reporttimetab->setCellSpacing(0);
    $reporttimetab->addRow(array(S_FROM, new CNumericBox('report_since_day', $report_timesince > 0 ? date('d', $report_timesince) : '', 2), '/', new CNumericBox('report_since_month', $report_timesince > 0 ? date('m', $report_timesince) : '', 2), '/', new CNumericBox('report_since_year', $report_timesince > 0 ? date('Y', $report_timesince) : '', 4), SPACE, new CNumericBox('report_since_hour', $report_timesince > 0 ? date('H', $report_timesince) : '', 2), ':', new CNumericBox('report_since_minute', $report_timesince > 0 ? date('i', $report_timesince) : '', 2), $clndr_icon));
    zbx_add_post_js('create_calendar(null,' . '["report_since_day","report_since_month","report_since_year","report_since_hour","report_since_minute"],' . '"avail_report_since",' . '"report_timesince");');
    $clndr_icon->addAction('onclick', 'javascript: ' . 'var pos = getPosition(this); ' . 'pos.top+=10; ' . 'pos.left+=16; ' . "CLNDR['avail_report_till'].clndr.clndrshow(pos.top,pos.left);");
    $reporttimetab->addRow(array(S_TILL, new CNumericBox('report_till_day', $report_timetill > 0 ? date('d', $report_timetill) : '', 2), '/', new CNumericBox('report_till_month', $report_timetill > 0 ? date('m', $report_timetill) : '', 2), '/', new CNumericBox('report_till_year', $report_timetill > 0 ? date('Y', $report_timetill) : '', 4), SPACE, new CNumericBox('report_till_hour', $report_timetill > 0 ? date('H', $report_timetill) : '', 2), ':', new CNumericBox('report_till_minute', $report_timetill > 0 ? date('i', $report_timetill) : '', 2), $clndr_icon));
    zbx_add_post_js('create_calendar(null,' . '["report_till_day","report_till_month","report_till_year","report_till_hour","report_till_minute"],' . '"avail_report_till",' . '"report_timetill");');
    zbx_add_post_js('addListener($("filter_icon"),' . '"click",' . 'CLNDR[\'avail_report_since\'].clndr.clndrhide.bindAsEventListener(CLNDR[\'avail_report_since\'].clndr));' . 'addListener($("filter_icon"),' . '"click",' . 'CLNDR[\'avail_report_till\'].clndr.clndrhide.bindAsEventListener(CLNDR[\'avail_report_till\'].clndr));');
    $reportForm->addRow(S_PERIOD, $reporttimetab);
    //-----------
    $scale = new CComboBox('scaletype', $scaletype);
    $scale->addItem(TIMEPERIOD_TYPE_HOURLY, S_HOURLY);
    $scale->addItem(TIMEPERIOD_TYPE_DAILY, S_DAILY);
    $scale->addItem(TIMEPERIOD_TYPE_WEEKLY, S_WEEKLY);
    $scale->addItem(TIMEPERIOD_TYPE_MONTHLY, S_MONTHLY);
    $scale->addItem(TIMEPERIOD_TYPE_YEARLY, S_YEARLY);
    $reportForm->addRow(S_SCALE, $scale);
    $avgcmb = new CComboBox('avgperiod', $avgperiod);
    $avgcmb->addItem(TIMEPERIOD_TYPE_HOURLY, S_HOURLY);
    $avgcmb->addItem(TIMEPERIOD_TYPE_DAILY, S_DAILY);
    $avgcmb->addItem(TIMEPERIOD_TYPE_WEEKLY, S_WEEKLY);
    $avgcmb->addItem(TIMEPERIOD_TYPE_MONTHLY, S_MONTHLY);
    $avgcmb->addItem(TIMEPERIOD_TYPE_YEARLY, S_YEARLY);
    $reportForm->addRow(S_AVERAGE_BY, $avgcmb);
    // ITEMS
    $itemid = 0;
    $description = '';
    if (count($items) && $items[0]['itemid'] > 0) {
        $itemid = $items[0]['itemid'];
        $description = get_item_by_itemid($itemid);
        $description = item_description($description);
    }
    $reportForm->addVar('items[0][itemid]', $itemid);
    $txtCondVal = new CTextBox('items[0][description]', $description, 50, 'yes');
    $btnSelect = new CButton('btn1', S_SELECT, "return PopUp('popup.php?dstfrm=" . $reportForm->GetName() . "&dstfld1=items[0][itemid]&dstfld2=items[0][description]&" . "srctbl=items&srcfld1=itemid&srcfld2=description&monitored_hosts=1');", 'T');
    $reportForm->addRow(S_ITEM, array($txtCondVal, $btnSelect));
    $paletteCmb = new CComboBox('palette', $palette);
    $paletteCmb->addItem(0, S_PALETTE . ' #1');
    $paletteCmb->addItem(1, S_PALETTE . ' #2');
    $paletteCmb->addItem(2, S_PALETTE . ' #3');
    $paletteCmb->addItem(3, S_PALETTE . ' #4');
    $paletteTypeCmb = new CComboBox('palettetype', $palettetype);
    $paletteTypeCmb->addItem(0, S_MIDDLE);
    $paletteTypeCmb->addItem(1, S_DARKEN);
    $paletteTypeCmb->addItem(2, S_BRIGHTEN);
    $reportForm->addRow(S_PALETTE, array($paletteCmb, $paletteTypeCmb));
    //--------------
    $reportForm->addItemToBottomRow(new CButton('report_show', S_SHOW));
    $reset = new CButton('reset', S_RESET);
    $reset->setType('reset');
    $reportForm->addItemToBottomRow($reset);
    return $reportForm;
}
Ejemplo n.º 20
0
function get_screen($screenid, $editmode, $effectiveperiod = NULL)
{
    global $USER_DETAILS;
    if ($screenid == 0) {
        return new CTableInfo(S_NO_SCREENS_DEFINED);
    }
    $r = CScreen::get(array('screenids' => $screenid, 'editable' => $editmode == 1 ? 1 : null, 'output' => API_OUTPUT_SHORTEN));
    if (empty($r)) {
        access_deny();
    }
    if (is_null($effectiveperiod)) {
        $effectiveperiod = ZBX_MIN_PERIOD;
    }
    $result = DBselect('SELECT name,hsize,vsize FROM screens WHERE screenid=' . $screenid);
    $row = DBfetch($result);
    if (!$row) {
        return new CTableInfo(S_NO_SCREENS_DEFINED);
    }
    $sql = 'SELECT * FROM screens_items WHERE screenid=' . $screenid;
    $iresult = DBSelect($sql);
    $skip_field = array();
    $irows = array();
    while ($irow = DBfetch($iresult)) {
        $irows[] = $irow;
        for ($i = 0; $i < $irow['rowspan'] || $i == 0; $i++) {
            for ($j = 0; $j < $irow['colspan'] || $j == 0; $j++) {
                if ($i != 0 || $j != 0) {
                    if (!isset($skip_field[$irow['y'] + $i])) {
                        $skip_field[$irow['y'] + $i] = array();
                    }
                    $skip_field[$irow['y'] + $i][$irow['x'] + $j] = 1;
                }
            }
        }
    }
    $table = new CTable(new CLink(S_NO_ROWS_IN_SCREEN . SPACE . $row['name'], 'screenconf.php?config=0&form=update&screenid=' . $screenid), $editmode == 0 || $editmode == 2 ? 'screen_view' : 'screen_edit');
    $table->setAttribute('id', 'iframe');
    if ($editmode == 1) {
        $new_cols = array(new Ccol(new Cimg('images/general/zero.gif', 'zero', 1, 1)));
        for ($c = 0; $c < $row['hsize'] + 1; $c++) {
            $add_icon = new Cimg('images/general/closed.gif', NULL, NULL, NULL, 'pointer');
            $add_icon->addAction('onclick', "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&add_col={$c}';");
            array_push($new_cols, new Ccol($add_icon));
        }
        $table->addRow($new_cols);
    }
    $empty_screen_col = array();
    for ($r = 0; $r < $row['vsize']; $r++) {
        $new_cols = array();
        $empty_screen_row = true;
        if ($editmode == 1) {
            $add_icon = new Cimg('images/general/closed.gif', NULL, NULL, NULL, 'pointer');
            $add_icon->addAction('onclick', "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&add_row={$r}';");
            array_push($new_cols, new Ccol($add_icon));
        }
        for ($c = 0; $c < $row['hsize']; $c++) {
            $item = array();
            if (isset($skip_field[$r][$c])) {
                continue;
            }
            $item_form = false;
            $irow = false;
            foreach ($irows as $tmprow) {
                if ($tmprow['x'] == $c && $tmprow['y'] == $r) {
                    $irow = $tmprow;
                    break;
                }
            }
            if ($irow) {
                $screenitemid = $irow['screenitemid'];
                $resourcetype = $irow['resourcetype'];
                $resourceid = $irow['resourceid'];
                $width = $irow['width'];
                $height = $irow['height'];
                $colspan = $irow['colspan'];
                $rowspan = $irow['rowspan'];
                $elements = $irow['elements'];
                $valign = $irow['valign'];
                $halign = $irow['halign'];
                $style = $irow['style'];
                $url = $irow['url'];
                $dynamic = $irow['dynamic'];
            } else {
                $screenitemid = 0;
                $resourcetype = 0;
                $resourceid = 0;
                $width = 0;
                $height = 0;
                $colspan = 0;
                $rowspan = 0;
                $elements = 0;
                $valign = VALIGN_DEFAULT;
                $halign = HALIGN_DEFAULT;
                $style = 0;
                $url = '';
                $dynamic = 0;
            }
            if ($screenitemid > 0) {
                $empty_screen_row = false;
                $empty_screen_col[$c] = 1;
            }
            if ($editmode == 1 && $screenitemid != 0) {
                $onclick_action = "ZBX_SCREENS['" . $_REQUEST['screenid'] . "'].screen.element_onclick('screenedit.php?form=update" . url_param('screenid') . '&screenitemid=' . $screenitemid . "#form');";
                $action = 'screenedit.php?form=update' . url_param('screenid') . '&screenitemid=' . $screenitemid . '#form';
            } else {
                if ($editmode == 1 && $screenitemid == 0) {
                    $onclick_action = "ZBX_SCREENS['" . $_REQUEST['screenid'] . "'].screen.element_onclick('screenedit.php?form=update" . url_param('screenid') . '&x=' . $c . '&y=' . $r . "#form');";
                    $action = 'screenedit.php?form=update' . url_param('screenid') . '&x=' . $c . '&y=' . $r . '#form';
                } else {
                    $action = NULL;
                }
            }
            if ($editmode == 1 && isset($_REQUEST['form']) && isset($_REQUEST['x']) && $_REQUEST['x'] == $c && isset($_REQUEST['y']) && $_REQUEST['y'] == $r) {
                // click on empty field
                $item = get_screen_item_form();
                $item_form = true;
            } else {
                if ($editmode == 1 && isset($_REQUEST['form']) && isset($_REQUEST['screenitemid']) && bccomp($_REQUEST['screenitemid'], $screenitemid) == 0) {
                    // click on element
                    $item = get_screen_item_form();
                    $item_form = true;
                } else {
                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_GRAPH) {
                        if ($editmode == 0) {
                            $action = 'charts.php?graphid=' . $resourceid . url_param('period') . url_param('stime');
                        }
                        // GRAPH & ZOOM features
                        $dom_graph_id = 'graph_' . $screenitemid . '_' . $resourceid;
                        $containerid = 'graph_cont_' . $screenitemid . '_' . $resourceid;
                        $graphDims = getGraphDims($resourceid);
                        $graphDims['graphHeight'] = $height;
                        $graphDims['width'] = $width;
                        $graph = get_graph_by_graphid($resourceid);
                        $graphid = $graph['graphid'];
                        $legend = $graph['show_legend'];
                        $graph3d = $graph['show_3d'];
                        //-------------
                        // Host feature
                        if ($dynamic == SCREEN_DYNAMIC_ITEM && isset($_REQUEST['hostid']) && $_REQUEST['hostid'] > 0) {
                            $options = array('hostids' => $_REQUEST['hostid'], 'output' => array('hostid', 'host'));
                            $hosts = CHost::get($options);
                            $host = reset($hosts);
                            $options = array('graphids' => $resourceid, 'output' => API_OUTPUT_EXTEND, 'select_hosts' => API_OUTPUT_REFER, 'select_graph_items' => API_OUTPUT_EXTEND);
                            $graph = CGraph::get($options);
                            $graph = reset($graph);
                            if (count($graph['hosts']) == 1) {
                                // if items from one host we change them, or set calculated if not exist on that host
                                if ($graph['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymax_itemid']) {
                                    $new_dinamic = get_same_graphitems_for_host(array(array('itemid' => $graph['ymax_itemid'])), $_REQUEST['hostid'], false);
                                    $new_dinamic = reset($new_dinamic);
                                    if (isset($new_dinamic['itemid']) && $new_dinamic['itemid'] > 0) {
                                        $graph['ymax_itemid'] = $new_dinamic['itemid'];
                                    } else {
                                        $graph['ymax_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
                                    }
                                }
                                if ($graph['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymin_itemid']) {
                                    $new_dinamic = get_same_graphitems_for_host(array(array('itemid' => $graph['ymin_itemid'])), $_REQUEST['hostid'], false);
                                    $new_dinamic = reset($new_dinamic);
                                    if (isset($new_dinamic['itemid']) && $new_dinamic['itemid'] > 0) {
                                        $graph['ymin_itemid'] = $new_dinamic['itemid'];
                                    } else {
                                        $graph['ymin_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
                                    }
                                }
                            }
                            $url = $graph['graphtype'] == GRAPH_TYPE_PIE || $graph['graphtype'] == GRAPH_TYPE_EXPLODED ? 'chart7.php' : 'chart3.php';
                            $url = new Curl($url);
                            foreach ($graph as $name => $value) {
                                if ($name == 'width' || $name == 'height') {
                                    continue;
                                }
                                $url->setArgument($name, $value);
                            }
                            $new_items = get_same_graphitems_for_host($graph['gitems'], $_REQUEST['hostid'], false);
                            foreach ($new_items as $gitem) {
                                unset($gitem['gitemid']);
                                unset($gitem['graphid']);
                                foreach ($gitem as $name => $value) {
                                    $url->setArgument('items[' . $gitem['itemid'] . '][' . $name . ']', $value);
                                }
                            }
                            $url->setArgument('name', $host['host'] . ': ' . $graph['name']);
                            $url = $url->getUrl();
                        }
                        //-------------
                        $objData = array('id' => $resourceid, 'domid' => $dom_graph_id, 'containerid' => $containerid, 'objDims' => $graphDims, 'loadSBox' => 0, 'loadImage' => 1, 'loadScroll' => 0, 'dynamic' => 0);
                        $default = false;
                        if ($graphDims['graphtype'] == GRAPH_TYPE_PIE || $graphDims['graphtype'] == GRAPH_TYPE_EXPLODED) {
                            if ($dynamic == SCREEN_SIMPLE_ITEM || empty($url)) {
                                $url = 'chart6.php?graphid=' . $resourceid;
                                $default = true;
                            }
                            $timeline = array();
                            $timeline['period'] = $effectiveperiod;
                            $timeline['starttime'] = date('YmdHis', get_min_itemclock_by_graphid($resourceid));
                            if (isset($_REQUEST['stime'])) {
                                $timeline['usertime'] = date('YmdHis', zbxDateToTime($_REQUEST['stime']) + $timeline['period']);
                            }
                            // $src = $url.'&width='.$width.'&height='.$height.'&legend='.$legend.'&graph3d='.$graph3d;
                            $src = $url . '&width=' . $width . '&height=' . $height . '&legend=' . $legend . '&graph3d=' . $graph3d . '&period=' . $effectiveperiod . url_param('stime');
                            $objData['src'] = $src;
                        } else {
                            if ($dynamic == SCREEN_SIMPLE_ITEM || empty($url)) {
                                $url = 'chart2.php?graphid=' . $resourceid;
                                $default = true;
                            }
                            $src = $url . '&width=' . $width . '&height=' . $height . '&period=' . $effectiveperiod . url_param('stime');
                            $timeline = array();
                            if (isset($graphid) && !is_null($graphid) && $editmode != 1) {
                                $timeline['period'] = $effectiveperiod;
                                $timeline['starttime'] = date('YmdHis', time() - ZBX_MAX_PERIOD);
                                //get_min_itemclock_by_graphid($graphid);
                                if (isset($_REQUEST['stime'])) {
                                    $timeline['usertime'] = date('YmdHis', zbxDateToTime($_REQUEST['stime']) + $timeline['period']);
                                }
                                $objData['loadSBox'] = 1;
                            }
                            $objData['src'] = $src;
                        }
                        if ($editmode || !$default) {
                            $item = new CDiv();
                        } else {
                            $item = new CLink(null, $action);
                        }
                        $item->setAttribute('id', $containerid);
                        $item = array($item);
                        if ($editmode == 1) {
                            $item[] = BR();
                            $item[] = new CLink(S_CHANGE, $action);
                        }
                        if ($editmode == 2) {
                            insert_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');');
                        } else {
                            zbx_add_post_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');');
                        }
                    } else {
                        if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SIMPLE_GRAPH) {
                            $dom_graph_id = 'graph_' . $screenitemid . '_' . $resourceid;
                            $containerid = 'graph_cont_' . $screenitemid . '_' . $resourceid;
                            $graphDims = getGraphDims();
                            $graphDims['graphHeight'] = $height;
                            $graphDims['width'] = $width;
                            $objData = array('id' => $resourceid, 'domid' => $dom_graph_id, 'containerid' => $containerid, 'objDims' => $graphDims, 'loadSBox' => 0, 'loadImage' => 1, 'loadScroll' => 0, 'dynamic' => 0);
                            // Host feature
                            if ($dynamic == SCREEN_DYNAMIC_ITEM && isset($_REQUEST['hostid']) && $_REQUEST['hostid'] > 0) {
                                if ($newitemid = get_same_item_for_host($resourceid, $_REQUEST['hostid'])) {
                                    $resourceid = $newitemid;
                                } else {
                                    $resourceid = '';
                                }
                            }
                            //-------------
                            if ($editmode == 0 && !empty($resourceid)) {
                                $action = 'history.php?action=showgraph&itemid=' . $resourceid . url_param('period') . url_param('stime');
                            }
                            $timeline = array();
                            $timeline['starttime'] = date('YmdHis', time() - ZBX_MAX_PERIOD);
                            if (!zbx_empty($resourceid) && $editmode != 1) {
                                $timeline['period'] = $effectiveperiod;
                                if (isset($_REQUEST['stime'])) {
                                    $timeline['usertime'] = date('YmdHis', zbxDateToTime($_REQUEST['stime']) + $timeline['period']);
                                }
                                $objData['loadSBox'] = 1;
                            }
                            $src = zbx_empty($resourceid) ? 'chart3.php?' : 'chart.php?itemid=' . $resourceid . '&';
                            $src .= $url . 'width=' . $width . '&height=' . $height;
                            $objData['src'] = $src;
                            if ($editmode) {
                                $item = new CDiv();
                            } else {
                                $item = new CLink(null, $action);
                            }
                            $item->setAttribute('id', $containerid);
                            $item = array($item);
                            if ($editmode == 1) {
                                $item[] = BR();
                                $item[] = new CLink(S_CHANGE, $action);
                            }
                            if ($editmode == 2) {
                                insert_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');');
                            } else {
                                zbx_add_post_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');');
                            }
                        } else {
                            if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_MAP) {
                                $image_map = new CImg("map.php?noedit=1&sysmapid={$resourceid}" . "&width={$width}&height={$height}&curtime=" . time());
                                if ($editmode == 0) {
                                    $options = array('sysmapids' => $resourceid, 'output' => API_OUTPUT_EXTEND, 'select_selements' => API_OUTPUT_EXTEND, 'nopermissions' => 1);
                                    $sysmaps = CMap::get($options);
                                    $sysmap = reset($sysmaps);
                                    $action_map = getActionMapBySysmap($sysmap);
                                    $image_map->setMap($action_map->getName());
                                    $item = array($action_map, $image_map);
                                } else {
                                    $item = $image_map;
                                    //						$item = new CLink($image_map, $action);
                                }
                            } else {
                                if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_PLAIN_TEXT) {
                                    // Host feature
                                    if ($dynamic == SCREEN_DYNAMIC_ITEM && isset($_REQUEST['hostid']) && $_REQUEST['hostid'] > 0) {
                                        if ($newitemid = get_same_item_for_host($resourceid, $_REQUEST['hostid'])) {
                                            $resourceid = $newitemid;
                                        } else {
                                            $resourceid = 0;
                                        }
                                    }
                                    //-------------
                                    $item = array(get_screen_plaintext($resourceid, $elements, $style));
                                    if ($editmode == 1) {
                                        array_push($item, new CLink(S_CHANGE, $action));
                                    }
                                } else {
                                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_HOSTGROUP_TRIGGERS) {
                                        $params = array('groupids' => null, 'hostids' => null, 'maintenance' => null, 'severity' => null, 'limit' => $elements);
                                        $tr_form = S_ALL_S;
                                        if ($resourceid > 0) {
                                            $options = array('groupids' => $resourceid, 'output' => API_OUTPUT_EXTEND);
                                            $hostgroups = CHostgroup::get($options);
                                            $hostgroup = reset($hostgroups);
                                            $tr_form = new CSpan(S_GROUP . ': ' . $hostgroup['name'], 'white');
                                            $params['groupids'] = $hostgroup['groupid'];
                                        } else {
                                            $groupid = get_request('tr_groupid', CProfile::get('web.screens.tr_groupid', 0));
                                            $hostid = get_request('tr_hostid', CProfile::get('web.screens.tr_hostid', 0));
                                            CProfile::update('web.screens.tr_groupid', $groupid, PROFILE_TYPE_ID);
                                            CProfile::update('web.screens.tr_hostid', $hostid, PROFILE_TYPE_ID);
                                            $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND);
                                            $groups = CHostGroup::get($options);
                                            order_result($groups, 'name');
                                            $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND);
                                            if ($groupid > 0) {
                                                $options['groupids'] = $groupid;
                                            }
                                            $hosts = CHost::get($options);
                                            $hosts = zbx_toHash($hosts, 'hostid');
                                            order_result($hosts, 'host');
                                            if (!isset($hosts[$hostid])) {
                                                $hostid = 0;
                                            }
                                            $tr_form = new CForm();
                                            $cmbGroup = new CComboBox('tr_groupid', $groupid, 'submit()');
                                            $cmbHosts = new CComboBox('tr_hostid', $hostid, 'submit()');
                                            $cmbGroup->addItem(0, S_ALL_SMALL);
                                            $cmbHosts->addItem(0, S_ALL_SMALL);
                                            foreach ($groups as $gnum => $group) {
                                                $cmbGroup->addItem($group['groupid'], get_node_name_by_elid($group['groupid'], null, ': ') . $group['name']);
                                            }
                                            foreach ($hosts as $hnum => $host) {
                                                $cmbHosts->addItem($host['hostid'], get_node_name_by_elid($host['hostid'], null, ': ') . $host['host']);
                                            }
                                            $tr_form->addItem(array(S_GROUP . SPACE, $cmbGroup));
                                            $tr_form->addItem(array(SPACE . S_HOST . SPACE, $cmbHosts));
                                            if ($groupid > 0) {
                                                $params['groupids'] = $groupid;
                                            }
                                            if ($hostid > 0) {
                                                $params['hostids'] = $hostid;
                                            }
                                        }
                                        $item = array(get_table_header(array(S_STATUS_OF_TRIGGERS_BIG, SPACE, zbx_date2str(S_SCREENS_TRIGGER_FORM_DATE_FORMAT)), $tr_form));
                                        $item[] = make_latest_issues($params);
                                        if ($editmode == 1) {
                                            array_push($item, new CLink(S_CHANGE, $action));
                                        }
                                        ///-----------------------
                                    } else {
                                        if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_HOST_TRIGGERS) {
                                            $params = array('groupids' => null, 'hostids' => null, 'maintenance' => null, 'severity' => null, 'limit' => $elements);
                                            $tr_form = S_ALL_S;
                                            if ($resourceid > 0) {
                                                $options = array('hostids' => $resourceid, 'output' => API_OUTPUT_EXTEND);
                                                $hosts = CHost::get($options);
                                                $host = reset($hosts);
                                                $tr_form = new CSpan(S_HOST . ': ' . $host['host'], 'white');
                                                $params['hostids'] = $host['hostid'];
                                            } else {
                                                $groupid = get_request('tr_groupid', CProfile::get('web.screens.tr_groupid', 0));
                                                $hostid = get_request('tr_hostid', CProfile::get('web.screens.tr_hostid', 0));
                                                CProfile::update('web.screens.tr_groupid', $groupid, PROFILE_TYPE_ID);
                                                CProfile::update('web.screens.tr_hostid', $hostid, PROFILE_TYPE_ID);
                                                $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND);
                                                $groups = CHostGroup::get($options);
                                                order_result($groups, 'name');
                                                $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND);
                                                if ($groupid > 0) {
                                                    $options['groupids'] = $groupid;
                                                }
                                                $hosts = CHost::get($options);
                                                $hosts = zbx_toHash($hosts, 'hostid');
                                                order_result($hosts, 'host');
                                                if (!isset($hosts[$hostid])) {
                                                    $hostid = 0;
                                                }
                                                $tr_form = new CForm();
                                                $cmbGroup = new CComboBox('tr_groupid', $groupid, 'submit()');
                                                $cmbHosts = new CComboBox('tr_hostid', $hostid, 'submit()');
                                                $cmbGroup->addItem(0, S_ALL_SMALL);
                                                $cmbHosts->addItem(0, S_ALL_SMALL);
                                                foreach ($groups as $gnum => $group) {
                                                    $cmbGroup->addItem($group['groupid'], get_node_name_by_elid($group['groupid'], null, ': ') . $group['name']);
                                                }
                                                foreach ($hosts as $hnum => $host) {
                                                    $cmbHosts->addItem($host['hostid'], get_node_name_by_elid($host['hostid'], null, ': ') . $host['host']);
                                                }
                                                $tr_form->addItem(array(S_GROUP . SPACE, $cmbGroup));
                                                $tr_form->addItem(array(SPACE . S_HOST . SPACE, $cmbHosts));
                                                if ($groupid > 0) {
                                                    $params['groupids'] = $groupid;
                                                }
                                                if ($hostid > 0) {
                                                    $params['hostids'] = $hostid;
                                                }
                                            }
                                            ///-----------------------
                                            $item = array(get_table_header(array(S_STATUS_OF_TRIGGERS_BIG, SPACE, zbx_date2str(S_SCREENS_TRIGGER_FORM_DATE_FORMAT)), $tr_form));
                                            $item[] = make_latest_issues($params);
                                            if ($editmode == 1) {
                                                array_push($item, new CLink(S_CHANGE, $action));
                                            }
                                        } else {
                                            if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SYSTEM_STATUS) {
                                                $params = array('groupids' => null, 'hostids' => null, 'maintenance' => null, 'severity' => null, 'limit' => null, 'extAck' => 0);
                                                $item = array(get_table_header(array(S_SYSTEM_STATUS, SPACE, zbx_date2str(S_SCREENS_TRIGGER_FORM_DATE_FORMAT))));
                                                $item[] = make_system_status($params);
                                                if ($editmode == 1) {
                                                    array_push($item, new CLink(S_CHANGE, $action));
                                                }
                                            } else {
                                                if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_HOSTS_INFO) {
                                                    $item = array(new CHostsInfo($resourceid, $style));
                                                    if ($editmode == 1) {
                                                        array_push($item, new CLink(S_CHANGE, $action));
                                                    }
                                                } else {
                                                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_TRIGGERS_INFO) {
                                                        $item = new CTriggersInfo($resourceid, null, $style);
                                                        $item = array($item);
                                                        if ($editmode == 1) {
                                                            array_push($item, new CLink(S_CHANGE, $action));
                                                        }
                                                    } else {
                                                        if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SERVER_INFO) {
                                                            //					$item = array(get_table_header(S_STATUS_OF_ZABBIX_BIG),make_status_of_zbx());
                                                            $item = array(new CServerInfo());
                                                            if ($editmode == 1) {
                                                                array_push($item, new CLink(S_CHANGE, $action));
                                                            }
                                                        } else {
                                                            if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_CLOCK) {
                                                                $item = new CFlashClock($width, $height, $style, $action);
                                                            } else {
                                                                if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SCREEN) {
                                                                    $item = array(get_screen($resourceid, 2, $effectiveperiod));
                                                                    if ($editmode == 1) {
                                                                        array_push($item, new CLink(S_CHANGE, $action));
                                                                    }
                                                                } else {
                                                                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_TRIGGERS_OVERVIEW) {
                                                                        $hostids = array();
                                                                        $res = DBselect('SELECT DISTINCT hg.hostid FROM hosts_groups hg WHERE hg.groupid=' . $resourceid);
                                                                        while ($tmp_host = DBfetch($res)) {
                                                                            $hostids[$tmp_host['hostid']] = $tmp_host['hostid'];
                                                                        }
                                                                        $item = array(get_triggers_overview($hostids, $style));
                                                                        if ($editmode == 1) {
                                                                            array_push($item, new CLink(S_CHANGE, $action));
                                                                        }
                                                                    } else {
                                                                        if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_DATA_OVERVIEW) {
                                                                            $hostids = array();
                                                                            $res = DBselect('SELECT DISTINCT hg.hostid FROM hosts_groups hg WHERE hg.groupid=' . $resourceid);
                                                                            while ($tmp_host = DBfetch($res)) {
                                                                                $hostids[$tmp_host['hostid']] = $tmp_host['hostid'];
                                                                            }
                                                                            $item = array(get_items_data_overview($hostids, $style));
                                                                            if ($editmode == 1) {
                                                                                array_push($item, new CLink(S_CHANGE, $action));
                                                                            }
                                                                        } else {
                                                                            if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_URL) {
                                                                                $item = array(new CIFrame($url, $width, $height, "auto"));
                                                                                if ($editmode == 1) {
                                                                                    array_push($item, BR(), new CLink(S_CHANGE, $action));
                                                                                }
                                                                            } else {
                                                                                if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_ACTIONS) {
                                                                                    $item = array(get_history_of_actions($elements));
                                                                                    if ($editmode == 1) {
                                                                                        array_push($item, new CLink(S_CHANGE, $action));
                                                                                    }
                                                                                } else {
                                                                                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_EVENTS) {
                                                                                        $options = array('monitored' => 1, 'value' => array(TRIGGER_VALUE_TRUE, TRIGGER_VALUE_FALSE), 'limit' => $elements);
                                                                                        $hide_unknown = CProfile::get('web.events.filter.hide_unknown', 0);
                                                                                        if ($hide_unknown) {
                                                                                            $options['value'] = array(TRIGGER_VALUE_TRUE, TRIGGER_VALUE_FALSE);
                                                                                        }
                                                                                        $item = new CTableInfo(S_NO_EVENTS_FOUND);
                                                                                        $item->SetHeader(array(S_TIME, is_show_all_nodes() ? S_NODE : null, S_HOST, S_DESCRIPTION, S_VALUE, S_SEVERITY));
                                                                                        $events = getLastEvents($options);
                                                                                        foreach ($events as $enum => $event) {
                                                                                            $trigger = $event['trigger'];
                                                                                            $host = $event['host'];
                                                                                            $value = new CCol(trigger_value2str($event['value']), get_trigger_value_style($event['value']));
                                                                                            //						$row = zbx_array_merge($triggers[$row['triggerid']],$row);
                                                                                            //						if((1 == $hide_unknown) && (!event_initial_time($row,$hide_unknown))) continue;
                                                                                            $item->addRow(array(zbx_date2str(S_EVENTS_TRIGGERS_EVENTS_HISTORY_LIST_DATE_FORMAT, $event['clock']), get_node_name_by_elid($event['objectid']), $host['host'], new CLink($trigger['description'], 'tr_events.php?triggerid=' . $event['objectid'] . '&eventid=' . $event['eventid']), $value, new CCol(get_severity_description($trigger['priority']), get_severity_style($trigger['priority']))));
                                                                                        }
                                                                                        $item = array($item);
                                                                                        if ($editmode == 1) {
                                                                                            array_push($item, new CLink(S_CHANGE, $action));
                                                                                        }
                                                                                    } else {
                                                                                        $item = array(SPACE);
                                                                                        if ($editmode == 1) {
                                                                                            array_push($item, BR(), new CLink(S_CHANGE, $action));
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $str_halign = 'def';
            if ($halign == HALIGN_CENTER) {
                $str_halign = 'cntr';
            }
            if ($halign == HALIGN_LEFT) {
                $str_halign = 'left';
            }
            if ($halign == HALIGN_RIGHT) {
                $str_halign = 'right';
            }
            $str_valign = 'def';
            if ($valign == VALIGN_MIDDLE) {
                $str_valign = 'mdl';
            }
            if ($valign == VALIGN_TOP) {
                $str_valign = 'top';
            }
            if ($valign == VALIGN_BOTTOM) {
                $str_valign = 'bttm';
            }
            if ($editmode == 1 && !$item_form) {
                $item = new CDiv($item, 'draggable');
                $item->setAttribute('id', 'position_' . $r . '_' . $c);
                if ($editmode == 1) {
                    $item->setAttribute('onclick', 'javascript: ' . $onclick_action);
                }
            }
            $new_col = new CCol($item, $str_halign . '_' . $str_valign);
            if ($colspan) {
                $new_col->SetColSpan($colspan);
            }
            if ($rowspan) {
                $new_col->SetRowSpan($rowspan);
            }
            array_push($new_cols, $new_col);
        }
        if ($editmode == 1) {
            $rmv_icon = new Cimg('images/general/opened.gif', NULL, NULL, NULL, 'pointer');
            if ($empty_screen_row) {
                $rmv_row_link = "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_row={$r}';";
            } else {
                $rmv_row_link = "javascript: if(Confirm('" . S_THIS_SCREEN_ROW_NOT_EMPTY . '. ' . S_DELETE_IT_Q . "')){" . " location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_row={$r}';}";
            }
            $rmv_icon->addAction('onclick', $rmv_row_link);
            array_push($new_cols, new Ccol($rmv_icon));
        }
        $table->addRow(new CRow($new_cols));
    }
    if ($editmode == 1) {
        $add_icon = new Cimg('images/general/closed.gif', NULL, NULL, NULL, 'pointer');
        $add_icon->addAction('onclick', "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&add_row={$row['vsize']}';");
        $new_cols = array(new Ccol($add_icon));
        for ($c = 0; $c < $row['hsize']; $c++) {
            $rmv_icon = new Cimg('images/general/opened.gif', NULL, NULL, NULL, 'pointer');
            if (isset($empty_screen_col[$c])) {
                $rmv_col_link = "javascript: if(Confirm('" . S_THIS_SCREEN_COLUMN_NOT_EMPTY . '. ' . S_DELETE_IT_Q . "')){" . " location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_col={$c}';}";
            } else {
                $rmv_col_link = "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_col={$c}';";
            }
            $rmv_icon->addAction('onclick', $rmv_col_link);
            array_push($new_cols, new Ccol($rmv_icon));
        }
        array_push($new_cols, new Ccol(new Cimg('images/general/zero.gif', 'zero', 1, 1)));
        $table->addRow($new_cols);
    }
    return $table;
}
Ejemplo n.º 21
0
 /**
  * Get Alerts data
  *
  * @param _array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['alertids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['extendoutput']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('alertid', 'clock', 'eventid', 'status');
     // 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('alerts' => 'a.alertid'), 'from' => array('alerts' => 'alerts a'), 'where' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'alertids' => null, 'triggerids' => null, 'eventids' => null, 'actionids' => null, 'mediatypeids' => null, 'userids' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'time_from' => null, 'time_till' => null, 'extendoutput' => null, 'output' => API_OUTPUT_REFER, 'select_mediatypes' => null, 'select_users' => null, 'select_hosts' => null, 'countOutput' => null, 'preservekeys' => null, 'editable' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_mediatypes'])) {
             $options['select_mediatypes'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_users'])) {
             $options['select_users'] = API_OUTPUT_EXTEND;
         }
     }
     if (is_array($options['output'])) {
         unset($sql_parts['select']['alerts']);
         $sql_parts['select']['alertid'] = ' a.alertid';
         foreach ($options['output'] as $key => $field) {
             $sql_parts['select'][$field] = ' a.' . $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['from']['events'] = 'events e';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['rights'] = 'rights r';
         $sql_parts['from']['users_groups'] = 'users_groups ug';
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['where']['ae'] = 'a.eventid=e.eventid';
         $sql_parts['where']['e'] = 'e.object=' . EVENT_OBJECT_TRIGGER;
         $sql_parts['where']['ef'] = 'e.objectid=f.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where'][] = 'r.id=hg.groupid ';
         $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
         $sql_parts['where'][] = 'ug.userid=' . $userid;
         $sql_parts['where'][] = 'r.permission>=' . $permission;
         $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT ff.triggerid ' . ' FROM functions ff, items ii ' . ' WHERE ff.triggerid=e.objectid ' . ' AND ff.itemid=ii.itemid ' . ' AND EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=ii.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . '))';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where']['e'] = 'e.object=' . EVENT_OBJECT_TRIGGER;
         $sql_parts['where']['ef'] = 'e.objectid=f.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hg'] = DBcondition('hg.groupid', $options['groupids']);
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['hostid'] = 'i.hostid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where']['i'] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['e'] = 'e.object=' . EVENT_OBJECT_TRIGGER;
         $sql_parts['where']['ef'] = 'e.objectid=f.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // alertids
     if (!is_null($options['alertids'])) {
         zbx_value2array($options['alertids']);
         $sql_parts['where'][] = DBcondition('a.alertid', $options['alertids']);
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['actionid'] = 'a.actionid';
         }
         $sql_parts['where']['ae'] = 'a.eventid=e.eventid';
         $sql_parts['where']['e'] = 'e.object=' . EVENT_OBJECT_TRIGGER;
         $sql_parts['where'][] = DBcondition('e.objectid', $options['triggerids']);
     }
     // eventids
     if (!is_null($options['eventids'])) {
         zbx_value2array($options['eventids']);
         $sql_parts['where'][] = DBcondition('a.eventid', $options['eventids']);
     }
     // actionids
     if (!is_null($options['actionids'])) {
         zbx_value2array($options['actionids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['actionid'] = 'a.actionid';
         }
         $sql_parts['where'][] = DBcondition('a.actionid', $options['actionids']);
     }
     // userids
     if (!is_null($options['userids'])) {
         zbx_value2array($options['userids']);
         // if(is_null($options['extendoutput'])){
         // $sql_parts['select']['userid'] = 'a.userid';
         // }
         $field = 'a.userid';
         if (!is_null($options['time_from']) || !is_null($options['time_till'])) {
             $field = '(a.userid+0)';
         }
         $sql_parts['where'][] = DBcondition($field, $options['userids']);
     }
     // mediatypeids
     if (!is_null($options['mediatypeids'])) {
         zbx_value2array($options['mediatypeids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['mediatypeid'] = 'a.mediatypeid';
         }
         $sql_parts['where'][] = DBcondition('a.mediatypeid', $options['mediatypeids']);
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('alerts a', $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('alerts a', $options, $sql_parts);
     }
     // time_from
     if (!is_null($options['time_from'])) {
         $sql_parts['where'][] = 'a.clock>' . $options['time_from'];
     }
     // time_till
     if (!is_null($options['time_till'])) {
         $sql_parts['where'][] = 'a.clock<' . $options['time_till'];
     }
     // extendoutput
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['alerts'] = 'a.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('COUNT(DISTINCT a.alertid) as rowscount');
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][] = 'a.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('a.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('a.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 'a.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //---------------
     $alertids = array();
     $userids = array();
     $hostids = array();
     $mediatypeids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('a.alertid', $nodeids) . $sql_where . $sql_order;
     $db_res = DBselect($sql, $sql_limit);
     while ($alert = DBfetch($db_res)) {
         if ($options['countOutput']) {
             $result = $alert['rowscount'];
         } else {
             $alertids[$alert['alertid']] = $alert['alertid'];
             if (isset($alert['userid'])) {
                 $userids[$alert['userid']] = $alert['userid'];
             }
             if (isset($alert['hostid'])) {
                 $hostids[$alert['hostid']] = $alert['hostid'];
             }
             if (isset($alert['mediatypeid'])) {
                 $mediatypeids[$alert['mediatypeid']] = $alert['mediatypeid'];
             }
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$alert['alertid']] = array('alertid' => $alert['alertid']);
             } else {
                 if (!isset($result[$alert['alertid']])) {
                     $result[$alert['alertid']] = array();
                 }
                 if (!is_null($options['select_mediatypes']) && !isset($result[$alert['alertid']]['mediatypes'])) {
                     $result[$alert['alertid']]['mediatypes'] = array();
                 }
                 if (!is_null($options['select_users']) && !isset($result[$alert['alertid']]['users'])) {
                     $result[$alert['alertid']]['users'] = array();
                 }
                 // hostids
                 if (isset($alert['hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$alert['alertid']]['hosts'])) {
                         $result[$alert['alertid']]['hosts'] = array();
                     }
                     $result[$alert['alertid']]['hosts'][] = array('hostid' => $alert['hostid']);
                     //						unset($alert['hostid']);
                 }
                 // userids
                 if (isset($alert['userid']) && is_null($options['select_users'])) {
                     if (!isset($result[$alert['alertid']]['users'])) {
                         $result[$alert['alertid']]['users'] = array();
                     }
                     $result[$alert['alertid']]['users'][] = array('userid' => $alert['userid']);
                 }
                 // mediatypeids
                 if (isset($alert['mediatypeid']) && is_null($options['select_mediatypes'])) {
                     if (!isset($result[$alert['alertid']]['mediatypes'])) {
                         $result[$alert['alertid']]['mediatypes'] = array();
                     }
                     $result[$alert['alertid']]['mediatypes'][] = array('mediatypeid' => $alert['mediatypeid']);
                 }
                 $result[$alert['alertid']] += $alert;
             }
         }
     }
     COpt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     $hosts = array();
     $users = array();
     $mediatypes = array();
     // Adding hosts
     if (!is_null($options['select_hosts']) && str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
         $obj_params = array('output' => $options['select_hosts'], 'hostids' => $hostids, 'preservekeys' => 1);
         $hosts = CHost::get($obj_params);
     }
     // Adding Users
     if (!is_null($options['select_users']) && str_in_array($options['select_users'], $subselects_allowed_outputs)) {
         $obj_params = array('output' => $options['select_users'], 'userids' => $userids, 'preservekeys' => 1);
         $users = CUser::get($obj_params);
     }
     // Adding MediaTypes
     if (!is_null($options['select_mediatypes']) && str_in_array($options['select_mediatypes'], $subselects_allowed_outputs)) {
         $sql = 'SELECT mt.* FROM media_type mt WHERE ' . DBcondition('mt.mediatypeid', $mediatypeids);
         $res = DBselect($sql);
         while ($media = DBfetch($res)) {
             $mediatypes[$media['mediatypeid']] = $media;
         }
     }
     foreach ($result as $alertid => $alert) {
         if (isset($alert['hostid']) && isset($hosts[$alert['hostid']])) {
             $result[$alertid]['hosts'][] = $hosts[$alert['hostid']];
         }
         if (isset($mediatypes[$alert['mediatypeid']])) {
             $result[$alertid]['mediatypes'][] = $mediatypes[$alert['mediatypeid']];
         }
         if (isset($users[$alert['userid']])) {
             $result[$alertid]['users'][] = $users[$alert['userid']];
         }
     }
     COpt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Ejemplo n.º 22
0
 /**
  * Update HostGroups with new Hosts (rewrite)
  *
  * @param array $data
  * @param array $data['groups']
  * @param array $data['hosts']
  * @param array $data['templates']
  * @return boolean
  */
 public static function massUpdate($data)
 {
     $groups = zbx_toArray($data['groups']);
     $hosts = isset($data['hosts']) ? zbx_toArray($data['hosts']) : null;
     $templates = isset($data['templates']) ? zbx_toArray($data['templates']) : null;
     $groupids = zbx_objectValues($groups, 'groupid');
     $hostids = zbx_objectValues($hosts, 'hostid');
     $templateids = zbx_objectValues($templates, 'templateid');
     try {
         self::BeginTransaction(__METHOD__);
         $hosts_to_unlink = $hosts_to_link = array();
         $options = array('groupids' => $groupids, 'preservekeys' => 1);
         if (!is_null($hosts)) {
             $groups_hosts = CHost::get($options);
             $hosts_to_unlink = array_diff(array_keys($groups_hosts), $hostids);
             $hosts_to_link = array_diff($hostids, array_keys($groups_hosts));
         }
         $templates_to_unlink = $templates_to_link = array();
         if (!is_null($templates)) {
             $groups_templates = CTemplate::get($options);
             $templates_to_unlink = array_diff(array_keys($groups_templates), $templateids);
             $templates_to_link = array_diff($templateids, array_keys($groups_templates));
         }
         $objectids_to_link = array_merge($hosts_to_link, $templates_to_link);
         $objectids_to_unlink = array_merge($hosts_to_unlink, $templates_to_unlink);
         // PERMISSION {{{
         $options = array('groupids' => $groupids, 'editable' => 1, 'preservekeys' => 1);
         $allowed_groups = self::get($options);
         foreach ($groups as $num => $group) {
             if (!isset($allowed_groups[$group['groupid']])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
             }
         }
         if (!is_null($hosts)) {
             $hosts_to_check = array_merge($hosts_to_link, $hosts_to_unlink);
             $options = array('hostids' => $hosts_to_check, 'editable' => 1, 'preservekeys' => 1);
             $allowed_hosts = CHost::get($options);
             foreach ($hosts_to_check as $num => $hostid) {
                 if (!isset($allowed_hosts[$hostid])) {
                     self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
                 }
             }
         }
         if (!is_null($templates)) {
             $templates_to_check = array_merge($templates_to_link, $templates_to_unlink);
             $options = array('templateids' => $templates_to_check, 'editable' => 1, 'preservekeys' => 1);
             $allowed_templates = CTemplate::get($options);
             foreach ($templates_to_check as $num => $templateid) {
                 if (!isset($allowed_templates[$templateid])) {
                     self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
                 }
             }
         }
         // }}} PERMISSION
         $unlinkable = getUnlinkableHosts($groupids, $objectids_to_unlink);
         if (count($objectids_to_unlink) != count($unlinkable)) {
             self::exception(ZBX_API_ERROR_PARAMETERS, 'One of the Objects is left without Hostgroup');
         }
         $sql = 'DELETE FROM hosts_groups WHERE ' . DBcondition('groupid', $groupids) . ' AND ' . DBcondition('hostid', $objectids_to_unlink);
         if (!DBexecute($sql)) {
             self::exception(ZBX_API_ERROR_PARAMETERS, 'DBerror');
         }
         foreach ($groupids as $gnum => $groupid) {
             foreach ($objectids_to_link as $objectid) {
                 $hostgroupid = get_dbid('hosts_groups', 'hostgroupid');
                 $result = DBexecute("INSERT INTO hosts_groups (hostgroupid, hostid, groupid) VALUES ({$hostgroupid}, {$objectid}, {$groupid})");
                 if (!$result) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, 'DBerror');
                 }
             }
         }
         self::EndTransaction(true, __METHOD__);
         return array('groupids' => $groupids);
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }
Ejemplo n.º 23
0
$left_tab->setAttribute('border', 0);
$right_tab = new CTable();
$right_tab->setCellPadding(3);
$right_tab->setCellSpacing(3);
$right_tab->setAttribute('border', 0);
// FIND Hosts
$params = array('nodeids' => get_current_nodeid(true), 'search' => array('host' => $search, 'dns' => $search, 'ip' => $search), 'limit' => $rows_per_page, 'select_groups' => API_OUTPUT_EXTEND, 'select_items' => API_OUTPUT_COUNT, 'select_triggers' => API_OUTPUT_COUNT, 'select_graphs' => API_OUTPUT_COUNT, 'select_applications' => API_OUTPUT_COUNT, 'output' => API_OUTPUT_EXTEND);
$db_hosts = CHost::get($params);
order_result($db_hosts, 'host');
$hosts = selectByPattern($db_hosts, 'host', $search, $rows_per_page);
$hostids = zbx_objectValues($hosts, 'hostid');
$params = array('nodeids' => get_current_nodeid(true), 'hostids' => $hostids, 'editable' => 1);
$rw_hosts = CHost::get($params);
$rw_hosts = zbx_toHash($rw_hosts, 'hostid');
$params = array('nodeids' => get_current_nodeid(true), 'search' => array('host' => $search, 'dns' => $search, 'ip' => $search), 'countOutput' => 1);
$overalCount = CHost::get($params);
$viewCount = count($hosts);
$header = array(ZBX_DISTRIBUTED ? new CCol(S_NODE) : null, new CCol(S_HOSTS), new CCol(S_IP), new CCol(S_DNS), new CCol(S_LATEST_DATA), new CCol(S_TRIGGERS), new CCol(S_EVENTS), new CCol(S_APPLICATIONS), new CCol(S_ITEMS), new CCol(S_TRIGGERS), new CCol(S_GRAPHS));
$table = new CTableInfo();
$table->setHeader($header);
foreach ($hosts as $hnum => $host) {
    $hostid = $host['hostid'];
    switch ($host['status']) {
        case HOST_STATUS_NOT_MONITORED:
            $style = 'on';
            break;
        default:
            $style = null;
            break;
    }
    $group = reset($host['groups']);
Ejemplo n.º 24
0
$data = $json->decode($data, true);
if (!is_array($data)) {
    fatal_error('Wrong RPC call to JS RPC');
}
if (!isset($data['method']) || !isset($data['params'])) {
    fatal_error('Wrong RPC call to JS RPC');
}
if (!is_array($data['params'])) {
    fatal_error('Wrong RPC call to JS RPC');
}
$result = array();
switch ($data['method']) {
    case 'host.get':
        $search = $data['params']['search'];
        $options = array('startSearch' => 1, 'search' => $search, 'output' => array('hostid', 'host'), 'sortfield' => 'host', 'limit' => 15);
        $result = CHost::get($options);
        break;
    case 'message.mute':
        $msgsettings = getMessageSettings();
        $msgsettings['sounds.mute'] = 1;
        updateMessageSettings($msgsettings);
        break;
    case 'message.unmute':
        $msgsettings = getMessageSettings();
        $msgsettings['sounds.mute'] = 0;
        updateMessageSettings($msgsettings);
        break;
    case 'message.settings':
        $result = getMessageSettings();
        break;
    case 'message.get':
Ejemplo n.º 25
0
 $rep2_wdgt->addHeader(S_REPORT_BIG, array(S_MODE . SPACE, $r_form));
 // FILTER
 $filterForm = get_report2_filter($config, $PAGE_GROUPS, $PAGE_HOSTS);
 $rep2_wdgt->addFlicker($filterForm, CProfile::get('web.avail_report.filter.state', 0));
 //-------
 $options = array('output' => array('triggerid', 'description', 'expression', 'value'), 'expandDescription' => true, 'expandData' => true, 'monitored' => true, 'filter' => array());
 if (0 == $config) {
     if ($_REQUEST['groupid'] > 0) {
         $options['groupids'] = $_REQUEST['groupid'];
     }
     if ($_REQUEST['hostid'] > 0) {
         $options['hostids'] = $_REQUEST['hostid'];
     }
 } else {
     if ($_REQUEST['hostid'] > 0) {
         $hosts = CHost::get(array('templateids' => $_REQUEST['hostid']));
         $options['hostids'] = zbx_objectValues($hosts, 'hostid');
     }
     if (isset($_REQUEST['tpl_triggerid']) && $_REQUEST['tpl_triggerid'] > 0) {
         $options['filter']['templateid'] = $_REQUEST['tpl_triggerid'];
     }
 }
 $triggers = CTrigger::get($options);
 morder_result($triggers, array('host', 'description'));
 $table = new CTableInfo();
 $table->setHeader(array(is_show_all_nodes() ? S_NODE : null, $_REQUEST['hostid'] == 0 || 1 == $config ? S_HOST : NULL, S_NAME, S_PROBLEMS, S_OK, S_UNKNOWN, S_GRAPH));
 foreach ($triggers as $trigger) {
     $availability = calculate_availability($trigger['triggerid'], $_REQUEST['filter_timesince'], $_REQUEST['filter_timetill']);
     $true = new CSpan(sprintf('%.4f%%', $availability['true']), 'on');
     $false = new CSpan(sprintf('%.4f%%', $availability['false']), 'off');
     $unknown = new CSpan(sprintf('%.4f%%', $availability['unknown']), 'unknown');
Ejemplo n.º 26
0
 /**
  * Get events data
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8
  * @version 1
  *
  * @param _array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['eventids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['extendoutput']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $nodeCheck = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('eventid', '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('events' => array('e.eventid')), 'from' => array('events' => 'events e'), 'where' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'triggerids' => null, 'eventids' => null, 'editable' => null, 'object' => null, 'source' => null, 'acknowledged' => null, 'nopermissions' => null, 'hide_unknown' => null, 'value' => null, 'time_from' => null, 'time_till' => null, 'eventid_from' => null, 'eventid_till' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_hosts' => null, 'select_items' => null, 'select_triggers' => null, 'countOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_hosts'])) {
             $options['select_hosts'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_triggers'])) {
             $options['select_triggers'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_items'])) {
             $options['select_items'] = API_OUTPUT_EXTEND;
         }
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         if (is_null($options['source']) && is_null($options['object'])) {
             $options['object'] = EVENT_OBJECT_TRIGGER;
         }
         if ($options['object'] == EVENT_OBJECT_TRIGGER || $options['source'] == EVENT_SOURCE_TRIGGER) {
             if (!is_null($options['triggerids'])) {
                 $triggerOptions = array('triggerids' => $options['triggerids'], 'editable' => $options['editable']);
                 $triggers = CTrigger::get($triggerOptions);
                 $options['triggerids'] = zbx_objectValues($triggers, 'triggerid');
             } else {
                 $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
                 $sql_parts['from']['functions'] = 'functions f';
                 $sql_parts['from']['items'] = 'items i';
                 $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
                 $sql_parts['from']['rights'] = 'rights r';
                 $sql_parts['from']['users_groups'] = 'users_groups ug';
                 $sql_parts['where']['e'] = 'e.object=' . EVENT_OBJECT_TRIGGER;
                 $sql_parts['where']['fe'] = 'f.triggerid=e.objectid';
                 $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
                 $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
                 $sql_parts['where'][] = 'r.id=hg.groupid ';
                 $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
                 $sql_parts['where'][] = 'ug.userid=' . $userid;
                 $sql_parts['where'][] = 'r.permission>=' . $permission;
                 $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT ff.triggerid ' . ' FROM functions ff, items ii ' . ' WHERE ff.triggerid=e.objectid ' . ' AND ff.itemid=ii.itemid ' . ' AND EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=ii.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . '))';
             }
         }
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // eventids
     if (!is_null($options['eventids'])) {
         zbx_value2array($options['eventids']);
         $sql_parts['where'][] = DBcondition('e.eventid', $options['eventids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('e.objectid', $nodeids);
         }
     }
     // triggerids
     if (!is_null($options['triggerids']) && $options['object'] == EVENT_OBJECT_TRIGGER) {
         zbx_value2array($options['triggerids']);
         $sql_parts['where'][] = DBcondition('e.objectid', $options['triggerids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('e.objectid', $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']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where']['hg'] = DBcondition('hg.groupid', $options['groupids']);
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where']['fe'] = 'f.triggerid=e.objectid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['hostid'] = 'i.hostid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where']['i'] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['ft'] = 'f.triggerid=e.objectid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // node check !!!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('e.eventid', $nodeids);
     }
     // object
     if (!is_null($options['object'])) {
         $sql_parts['where']['o'] = 'e.object=' . $options['object'];
     }
     // source
     if (!is_null($options['source'])) {
         $sql_parts['where'][] = 'e.source=' . $options['source'];
     }
     // acknowledged
     if (!is_null($options['acknowledged'])) {
         $sql_parts['where'][] = 'e.acknowledged=' . ($options['acknowledged'] ? 1 : 0);
     }
     // hide_unknown
     if (!is_null($options['hide_unknown'])) {
         $sql_parts['where'][] = 'e.value<>' . TRIGGER_VALUE_UNKNOWN;
     }
     // time_from
     if (!is_null($options['time_from'])) {
         $sql_parts['where'][] = 'e.clock>=' . $options['time_from'];
     }
     // time_till
     if (!is_null($options['time_till'])) {
         $sql_parts['where'][] = 'e.clock<=' . $options['time_till'];
     }
     // eventid_from
     if (!is_null($options['eventid_from'])) {
         $sql_parts['where'][] = 'e.eventid>=' . $options['eventid_from'];
     }
     // eventid_till
     if (!is_null($options['eventid_till'])) {
         $sql_parts['where'][] = 'e.eventid<=' . $options['eventid_till'];
     }
     // value
     if (!is_null($options['value'])) {
         zbx_value2array($options['value']);
         $sql_parts['where'][] = DBcondition('e.value', $options['value']);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['events'] = array('e.*');
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('events' => array('COUNT(DISTINCT e.eventid) as rowscount'));
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][] = 'e.' . $options['sortfield'] . ' ' . $sortorder;
         if (!is_null($options['triggerids']) && $options['sortfield'] == 'clock') {
             $sql_parts['where']['o'] = '(e.object-0)=' . EVENT_OBJECT_TRIGGER;
         }
         $eventFields = $sql_parts['select']['events'];
         if (!str_in_array('e.' . $options['sortfield'], $eventFields) && !str_in_array('e.*', $eventFields)) {
             $sql_parts['select']['events'][] = 'e.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     // select_********
     if ($options['output'] != API_OUTPUT_EXTEND && (!is_null($options['select_hosts']) || !is_null($options['select_triggers']) || !is_null($options['select_items']))) {
         $sql_parts['select']['events'][] = 'e.object';
         $sql_parts['select']['events'][] = 'e.objectid';
     }
     //---------------
     $eventids = array();
     $triggerids = array();
     // Event fields
     $sql_parts['select']['events'] = implode(',', array_unique($sql_parts['select']['events']));
     $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 ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_order;
     $db_res = DBselect($sql, $sql_limit);
     //SDI($sql);
     while ($event = DBfetch($db_res)) {
         if ($options['countOutput']) {
             $result = $event['rowscount'];
         } else {
             $eventids[$event['eventid']] = $event['eventid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$event['eventid']] = array('eventid' => $event['eventid']);
             } else {
                 if (isset($event['object']) && $event['object'] == EVENT_OBJECT_TRIGGER) {
                     $triggerids[$event['objectid']] = $event['objectid'];
                 }
                 if (!isset($result[$event['eventid']])) {
                     $result[$event['eventid']] = array();
                 }
                 if (!is_null($options['select_hosts']) && !isset($result[$event['eventid']]['hosts'])) {
                     $result[$event['eventid']]['hosts'] = array();
                 }
                 if (!is_null($options['select_triggers']) && !isset($result[$event['eventid']]['triggers'])) {
                     $result[$event['eventid']]['triggers'] = array();
                 }
                 if (!is_null($options['select_items']) && !isset($result[$event['eventid']]['items'])) {
                     $result[$event['eventid']]['items'] = array();
                 }
                 // hostids
                 if (isset($event['hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$event['eventid']]['hosts'])) {
                         $result[$event['eventid']]['hosts'] = array();
                     }
                     $result[$event['eventid']]['hosts'][] = array('hostid' => $event['hostid']);
                     unset($event['hostid']);
                 }
                 // triggerids
                 if (isset($event['triggerid']) && is_null($options['select_triggers'])) {
                     if (!isset($result[$event['eventid']]['triggers'])) {
                         $result[$event['eventid']]['triggers'] = array();
                     }
                     $result[$event['eventid']]['triggers'][] = array('triggerid' => $event['triggerid']);
                     unset($event['triggerid']);
                 }
                 // itemids
                 if (isset($event['itemid']) && is_null($options['select_items'])) {
                     if (!isset($result[$event['eventid']]['items'])) {
                         $result[$event['eventid']]['items'] = array();
                     }
                     $result[$event['eventid']]['items'][] = array('itemid' => $event['itemid']);
                     unset($event['itemid']);
                 }
                 $result[$event['eventid']] += $event;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding hosts
     if (!is_null($options['select_hosts']) && str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_hosts'], 'triggerids' => $triggerids, 'nopermissions' => 1, 'preservekeys' => 1);
         $hosts = CHost::get($obj_params);
         $triggers = array();
         foreach ($hosts as $hostid => $host) {
             $htriggers = $host['triggers'];
             unset($host['triggers']);
             foreach ($htriggers as $tnum => $trigger) {
                 $triggerid = $trigger['triggerid'];
                 if (!isset($triggers[$triggerid])) {
                     $triggers[$triggerid] = array('hosts' => array());
                 }
                 $triggers[$triggerid]['hosts'][] = $host;
             }
         }
         foreach ($result as $eventid => $event) {
             if (isset($triggers[$event['objectid']])) {
                 $result[$eventid]['hosts'] = $triggers[$event['objectid']]['hosts'];
             } else {
                 $result[$eventid]['hosts'] = array();
             }
         }
     }
     // Adding triggers
     if (!is_null($options['select_triggers']) && str_in_array($options['select_triggers'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_triggers'], 'triggerids' => $triggerids, 'nopermissions' => 1, 'preservekeys' => 1);
         $triggers = CTrigger::get($obj_params);
         foreach ($result as $eventid => $event) {
             if (isset($triggers[$event['objectid']])) {
                 $result[$eventid]['triggers'][] = $triggers[$event['objectid']];
             } else {
                 $result[$eventid]['triggers'] = array();
             }
         }
     }
     // Adding items
     if (!is_null($options['select_items']) && str_in_array($options['select_items'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_items'], 'triggerids' => $triggerids, 'webitems' => 1, 'nopermissions' => 1, 'preservekeys' => 1);
         $db_items = CItem::get($obj_params);
         $items = array();
         foreach ($db_items as $itemid => $item) {
             $itriggers = $item['triggers'];
             unset($item['triggers']);
             foreach ($itriggers as $trigger) {
                 if (!isset($items[$trigger['triggerid']])) {
                     $items[$trigger['triggerid']] = array();
                 }
                 $items[$trigger['triggerid']][] = $item;
             }
         }
         foreach ($result as $eventid => $event) {
             if (isset($items[$event['objectid']])) {
                 $result[$eventid]['items'] = $items[$event['objectid']];
             } else {
                 $result[$eventid]['items'] = array();
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Ejemplo n.º 27
0
 foreach ($db_groups as $groupid => $row) {
     $cmbGroups->addItem($groupid, $row['name']);
 }
 $cmbHosts = new CTweenBox($frmHostG, 'hosts', $hosts, 25);
 // get hosts from selected twb_groupid combo
 $params = array('groupids' => $twb_groupid, 'templated_hosts' => 1, 'order' => 'host', 'editable' => 1, 'extendoutput' => 1);
 $db_hosts = CHost::get($params);
 foreach ($db_hosts as $hostid => $db_host) {
     // add all except selected hosts
     if (!isset($hosts[$hostid])) {
         $cmbHosts->addItem($hostid, get_node_name_by_elid($hostid) . $db_host['host']);
     }
 }
 // select selected hosts and add them
 $params = array('hostids' => $hosts, 'templated_hosts' => 1, 'order' => 'host', 'editable' => 1, 'extendoutput' => 1);
 $db_hosts = CHost::get($params);
 foreach ($db_hosts as $hostid => $db_host) {
     $cmbHosts->addItem($hostid, get_node_name_by_elid($hostid) . $db_host['host']);
 }
 $frmHostG->addRow(S_HOSTS, $cmbHosts->Get(S_HOSTS . SPACE . S_IN, array(S_OTHER . SPACE . S_HOSTS . SPACE . '|' . SPACE . S_GROUP . SPACE, $cmbGroups)));
 $frmHostG->addItemToBottomRow(new CButton('save', S_SAVE));
 if ($groupid > 0) {
     $frmHostG->addItemToBottomRow(SPACE);
     $frmHostG->addItemToBottomRow(new CButton('clone', S_CLONE));
     $frmHostG->addItemToBottomRow(SPACE);
     $dltButton = new CButtonDelete('Delete selected group?', url_param('form') . url_param('config') . url_param('groupid'));
     $dlt_groups = getDeletableHostGroups($_REQUEST['groupid']);
     if (empty($dlt_groups)) {
         $dltButton->setAttribute('disabled', 'disabled');
     }
     $frmHostG->addItemToBottomRow($dltButton);
Ejemplo n.º 28
0
 public static function get($options)
 {
     global $USER_DETAILS;
     $result = array();
     $nodeCheck = false;
     $user_type = $USER_DETAILS['type'];
     $sort_columns = array('dcheckid', 'druleid');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('dchecks' => 'dc.dcheckid'), 'from' => array('dchecks' => 'dchecks dc'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'dcheckids' => null, 'druleids' => null, 'dhostids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'selectDRules' => null, 'selectDHosts' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($def_options, $options);
     if (is_array($options['output'])) {
         unset($sql_parts['select']['dchecks']);
         foreach ($options['output'] as $key => $field) {
             $sql_parts['select'][$field] = ' dc.' . $field;
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type) {
     } else {
         if (is_null($options['editable']) && $USER_DETAILS['type'] == USER_TYPE_ZABBIX_ADMIN) {
         } else {
             if (!is_null($options['editable']) && $USER_DETAILS['type'] != USER_TYPE_SUPER_ADMIN) {
                 return array();
             }
         }
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // dcheckids
     if (!is_null($options['dcheckids'])) {
         zbx_value2array($options['dcheckids']);
         $sql_parts['where']['dcheckid'] = DBcondition('dc.dcheckid', $options['dcheckids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('ds.dcheckid', $nodeids);
         }
     }
     // druleids
     if (!is_null($options['druleids'])) {
         zbx_value2array($options['druleids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['druleid'] = 'dc.druleid';
         }
         $sql_parts['where'][] = DBcondition('dc.druleid', $options['druleids']);
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['druleid'] = 'dc.druleid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('dc.druleid', $nodeids);
         }
     }
     // dhostids
     if (!is_null($options['dhostids'])) {
         zbx_value2array($options['dhostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['dservices'] = 'dh.dhostid';
         }
         $sql_parts['from']['dhosts'] = 'dhosts dh';
         $sql_parts['where']['dh'] = DBcondition('dh.dhostid', $options['dhostids']);
         $sql_parts['where']['dcdh'] = 'dc.druleid=dh.druleid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dhostid'] = 'dh.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']['dhosts'] = 'dhosts dh';
         $sql_parts['from']['dservices'] = 'dservices ds';
         $sql_parts['where']['ds'] = DBcondition('ds.dserviceid', $options['dserviceids']);
         $sql_parts['where']['dcdh'] = 'dc.druleid=dh.druleid';
         $sql_parts['where']['dhds'] = 'dh.hostid=ds.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dserviceid'] = 'ds.dserviceid';
         }
     }
     // node check !!!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('dc.dcheckid', $nodeids);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['dchecks'] = 'dc.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT dc.dcheckid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('dchecks dc', $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('dchecks dc', $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']] = 'dc.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('dc.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('dc.*', $sql_parts['select'])) {
             $sql_parts['select'][$options['sortfield']] = 'dc.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //-------
     $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 ($dcheck = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $dcheck;
             } else {
                 $result = $dcheck['rowscount'];
             }
         } else {
             $dcheckids[$dcheck['dcheckid']] = $dcheck['dcheckid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$dcheck['dcheckid']] = array('dcheckid' => $dcheck['dcheckid']);
             } else {
                 if (!isset($result[$dcheck['dcheckid']])) {
                     $result[$dcheck['dcheckid']] = array();
                 }
                 if (!is_null($options['selectDRules']) && !isset($result[$dcheck['dcheckid']]['drules'])) {
                     $result[$dcheck['dcheckid']]['drules'] = array();
                 }
                 if (!is_null($options['selectDHosts']) && !isset($result[$dcheck['dcheckid']]['dhosts'])) {
                     $result[$dcheck['dcheckid']]['dhosts'] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$dcheck['dcheckid']]['hosts'])) {
                     $result[$dcheck['dcheckid']]['hosts'] = array();
                 }
                 // druleids
                 if (isset($dcheck['druleid']) && is_null($options['selectDRules'])) {
                     if (!isset($result[$dcheck['dcheckid']]['drules'])) {
                         $result[$dcheck['dcheckid']]['drules'] = array();
                     }
                     $result[$dcheck['dcheckid']]['drules'][] = array('druleid' => $dcheck['druleid']);
                 }
                 // dhostids
                 if (isset($dcheck['dhostid']) && is_null($options['selectDHosts'])) {
                     if (!isset($result[$dcheck['dcheckid']]['dhosts'])) {
                         $result[$dcheck['dcheckid']]['dhosts'] = array();
                     }
                     $result[$dcheck['dcheckid']]['dhosts'][] = array('dhostid' => $dcheck['dhostid']);
                 }
                 $result[$dcheck['dcheckid']] += $dcheck;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // select_drules
     if (!is_null($options['selectDRules'])) {
         $obj_params = array('nodeids' => $nodeids, 'dcheckids' => $dcheckids, 'preservekeys' => 1);
         if (is_array($options['selectDRules']) || str_in_array($options['selectDRules'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectDRules'];
             $drules = CDRule::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($drules, 'name');
             }
             foreach ($drules as $druleid => $drule) {
                 unset($drules[$druleid]['dchecks']);
                 $count = array();
                 foreach ($drule['dchecks'] as $dnum => $dcheck) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dcheck['dcheckid']])) {
                             $count[$dcheck['dcheckid']] = 0;
                         }
                         $count[$dcheck['dcheckid']]++;
                         if ($count[$dcheck['dcheckid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dcheck['dcheckid']]['drules'][] =& $drules[$druleid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectDRules']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $drules = CDRule::get($obj_params);
                 $drules = zbx_toHash($drules, 'dcheckid');
                 foreach ($result as $dcheckid => $dcheck) {
                     if (isset($drules[$dcheckid])) {
                         $result[$dcheckid]['drules'] = $drules[$dcheckid]['rowscount'];
                     } else {
                         $result[$dcheckid]['drules'] = 0;
                     }
                 }
             }
         }
     }
     // selectDHosts
     if (!is_null($options['selectDHosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'dcheckids' => $dcheckids, 'preservekeys' => 1);
         if (is_array($options['selectDHosts']) || str_in_array($options['selectDHosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectDHosts'];
             $dhosts = CDHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($dhosts, 'dhostid');
             }
             foreach ($dhosts as $dhostid => $dhost) {
                 unset($dhosts[$dhostid]['dchecks']);
                 foreach ($dhost['dchecks'] as $snum => $dcheck) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dcheck['dcheckid']])) {
                             $count[$dcheck['dcheckid']] = 0;
                         }
                         $count[$dcheck['dcheckid']]++;
                         if ($count[$dcheck['dcheckid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dcheck['dcheckid']]['dhosts'][] =& $dhosts[$dhostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectDHosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $dhosts = CDHost::get($obj_params);
                 $dhosts = zbx_toHash($dhosts, 'dhostid');
                 foreach ($result as $dcheckid => $dcheck) {
                     if (isset($dhosts[$dcheckid])) {
                         $result[$dcheckid]['dhosts'] = $dhosts[$dcheckid]['rowscount'];
                     } else {
                         $result[$dcheckid]['dhosts'] = 0;
                     }
                 }
             }
         }
     }
     // select_hosts
     if (!is_null($options['selectHosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'dcheckids' => $dcheckids, 'preservekeys' => 1, 'sortfield' => 'status');
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectHosts'];
             $hosts = CHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'hostid');
             }
             foreach ($hosts as $hostid => $host) {
                 unset($hosts[$hostid]['dchecks']);
                 foreach ($host['dchecks'] as $dnum => $dcheck) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dcheck['dcheckid']])) {
                             $count[$dcheck['dcheckid']] = 0;
                         }
                         $count[$dcheck['dcheckid']]++;
                         if ($count[$dcheck['dcheckid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dcheck['dcheckid']]['hosts'][] =& $hosts[$hostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectHosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $hosts = CHost::get($obj_params);
                 $hosts = zbx_toHash($hosts, 'hostid');
                 foreach ($result as $dcheckid => $dcheck) {
                     if (isset($hosts[$dcheckid])) {
                         $result[$dcheckid]['hosts'] = $hosts[$dcheckid]['rowscount'];
                     } else {
                         $result[$dcheckid]['hosts'] = 0;
                     }
                 }
             }
         }
     }
     Copt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Ejemplo n.º 29
0
 /**
  * Add Host
  *
  * @param _array $hosts multidimensional array with Hosts data
  * @param string $hosts['host'] Host name.
  * @param array $hosts['groups'] array of HostGroup objects with IDs add Host to.
  * @param int $hosts['port'] Port. OPTIONAL
  * @param int $hosts['status'] Host Status. OPTIONAL
  * @param int $hosts['useip'] Use IP. OPTIONAL
  * @param string $hosts['dns'] DNS. OPTIONAL
  * @param string $hosts['ip'] IP. OPTIONAL
  * @param int $hosts['proxy_hostid'] Proxy Host ID. OPTIONAL
  * @param int $hosts['useipmi'] Use IPMI. OPTIONAL
  * @param string $hosts['ipmi_ip'] IPMAI IP. OPTIONAL
  * @param int $hosts['ipmi_port'] IPMI port. OPTIONAL
  * @param int $hosts['ipmi_authtype'] IPMI authentication type. OPTIONAL
  * @param int $hosts['ipmi_privilege'] IPMI privilege. OPTIONAL
  * @param string $hosts['ipmi_username'] IPMI username. OPTIONAL
  * @param string $hosts['ipmi_password'] IPMI password. OPTIONAL
  * @return boolean
  */
 public static function create($hosts)
 {
     $hosts = zbx_toArray($hosts);
     $hostids = array();
     $groupids = array();
     try {
         self::BeginTransaction(__METHOD__);
         // BASIC VALIDATION {{{
         foreach ($hosts as $hnum => $host) {
             // CHECK IF HOSTS HAVE AT LEAST 1 GROUP
             if (empty($host['groups'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, sprintf(S_NO_GROUPS_FOR_HOST, $host['host']));
             }
             // Check if host name isn't longer then 64 chars
             if (zbx_strlen($host['host']) > 64) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, sprintf(S_HOST_NAME_MUST_BE_LONGER, 64, $host['host'], zbx_strlen($host['host'])));
             }
             $hosts[$hnum]['groups'] = zbx_toArray($hosts[$hnum]['groups']);
             foreach ($hosts[$hnum]['groups'] as $gnum => $group) {
                 $groupids[$group['groupid']] = $group['groupid'];
             }
         }
         // }}}
         // PERMISSIONS {{{
         $upd_groups = CHostGroup::get(array('groupids' => $groupids, 'editable' => 1, 'preservekeys' => 1));
         foreach ($groupids as $gnum => $groupid) {
             if (!isset($upd_groups[$groupid])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSIONS);
             }
         }
         // }}} PERMISSIONS
         foreach ($hosts as $num => $host) {
             $host_db_fields = array('host' => null, 'port' => 0, 'status' => 0, 'useip' => 0, 'dns' => '', 'ip' => '0.0.0.0', 'proxy_hostid' => 0, 'useipmi' => 0, 'ipmi_ip' => '', 'ipmi_port' => 623, 'ipmi_authtype' => 0, 'ipmi_privilege' => 0, 'ipmi_username' => '', 'ipmi_password' => '');
             if (!check_db_fields($host_db_fields, $host)) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Wrong fields for host [ ' . $host['host'] . ' ]');
             }
             if (!preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/i', $host['host'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Incorrect characters used for Hostname [ ' . $host['host'] . ' ]');
             }
             if (!empty($host['dns']) && !preg_match('/^' . ZBX_PREG_DNS_FORMAT . '$/i', $host['dns'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Incorrect characters used for DNS [ ' . $host['dns'] . ' ]');
             }
             if (self::exists(array('host' => $host['host']))) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, S_HOST . ' [ ' . $host['host'] . ' ] ' . S_ALREADY_EXISTS_SMALL);
             }
             if (CTemplate::exists(array('host' => $host['host']))) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, S_TEMPLATE . ' [ ' . $host['host'] . ' ] ' . S_ALREADY_EXISTS_SMALL);
             }
             $hostid = get_dbid('hosts', 'hostid');
             $hostids[] = $hostid;
             $result = DBexecute('INSERT INTO hosts (hostid, proxy_hostid, host, port, status, useip, dns, ip, disable_until, available,' . 'useipmi,ipmi_port,ipmi_authtype,ipmi_privilege,ipmi_username,ipmi_password,ipmi_ip) VALUES (' . $hostid . ',' . $host['proxy_hostid'] . ',' . zbx_dbstr($host['host']) . ',' . $host['port'] . ',' . $host['status'] . ',' . $host['useip'] . ',' . zbx_dbstr($host['dns']) . ',' . zbx_dbstr($host['ip']) . ',0,' . HOST_AVAILABLE_UNKNOWN . ',' . $host['useipmi'] . ',' . $host['ipmi_port'] . ',' . $host['ipmi_authtype'] . ',' . $host['ipmi_privilege'] . ',' . zbx_dbstr($host['ipmi_username']) . ',' . zbx_dbstr($host['ipmi_password']) . ',' . zbx_dbstr($host['ipmi_ip']) . ')');
             if (!$result) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'DBerror');
             }
             foreach ($host['groups'] as $group) {
                 $hostgroupid = get_dbid('hosts_groups', 'hostgroupid');
                 $result = DBexecute("INSERT INTO hosts_groups (hostgroupid, hostid, groupid) VALUES ({$hostgroupid}, {$hostid}, {$group['groupid']})");
                 if (!$result) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, 'DBerror');
                 }
             }
             $host['hostid'] = $hostid;
             $options = array();
             $options['hosts'] = $host;
             if (isset($host['templates']) && !is_null($host['templates'])) {
                 $options['templates'] = $host['templates'];
             }
             if (isset($host['macros']) && !is_null($host['macros'])) {
                 $options['macros'] = $host['macros'];
             }
             $result = CHost::massAdd($options);
             if (!$result) {
                 self::exception();
             }
             if (isset($host['profile']) && !empty($host['extendedProfile'])) {
                 $fields = array_keys($host['profile']);
                 $fields = implode(', ', $fields);
                 $values = array_map('zbx_dbstr', $host['profile']);
                 $values = implode(', ', $values);
                 DBexecute('INSERT INTO hosts_profiles (hostid, ' . $fields . ') VALUES (' . $hostid . ', ' . $values . ')');
             }
             if (isset($host['extendedProfile']) && !empty($host['extendedProfile'])) {
                 $fields = array_keys($host['extendedProfile']);
                 $fields = implode(', ', $fields);
                 $values = array_map('zbx_dbstr', $host['extendedProfile']);
                 $values = implode(', ', $values);
                 DBexecute('INSERT INTO hosts_profiles_ext (hostid, ' . $fields . ') VALUES (' . $hostid . ', ' . $values . ')');
             }
         }
         self::EndTransaction(true, __METHOD__);
         return array('hostids' => $hostids);
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }
Ejemplo n.º 30
0
 /**
  * Inherit template graphs from template to host
  *
  * params: templateids, hostids
  *
  * @param array $data
  * @return boolean
  */
 public static function syncTemplates($data)
 {
     try {
         self::BeginTransaction(__METHOD__);
         $data['templateids'] = zbx_toArray($data['templateids']);
         $data['hostids'] = zbx_toArray($data['hostids']);
         $options = array('hostids' => $data['hostids'], 'editable' => 1, 'preservekeys' => 1, 'templated_hosts' => 1, 'output' => API_OUTPUT_SHORTEN);
         $allowedHosts = CHost::get($options);
         foreach ($data['hostids'] as $hostid) {
             if (!isset($allowedHosts[$hostid])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
             }
         }
         $options = array('templateids' => $data['templateids'], 'preservekeys' => 1, 'output' => API_OUTPUT_SHORTEN);
         $allowedTemplates = CTemplate::get($options);
         foreach ($data['templateids'] as $templateid) {
             if (!isset($allowedTemplates[$templateid])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
             }
         }
         $sql = 'SELECT hostid, templateid' . ' FROM hosts_templates' . ' WHERE ' . DBcondition('hostid', $data['hostids']) . ' AND ' . DBcondition('templateid', $data['templateids']);
         $db_links = DBSelect($sql);
         $linkage = array();
         while ($link = DBfetch($db_links)) {
             if (!isset($linkage[$link['templateid']])) {
                 $linkage[$link['templateid']] = array();
             }
             $linkage[$link['templateid']][$link['hostid']] = 1;
         }
         $options = array('hostids' => $data['templateids'], 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND, 'select_graph_items' => API_OUTPUT_EXTEND);
         $graphs = self::get($options);
         foreach ($graphs as $graph) {
             foreach ($data['hostids'] as $hostid) {
                 if (isset($linkage[$graph['hosts'][0]['hostid']][$hostid])) {
                     self::inherit($graph, $hostid);
                 }
             }
         }
         self::EndTransaction(true, __METHOD__);
         return true;
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }