Пример #1
0
//------------------------------------- <HISTORY> ---------------------------------------
if (isset($page['hist_arg']) && $USER_DETAILS['alias'] != ZBX_GUEST_USER && $page['type'] == PAGE_TYPE_HTML && !defined('ZBX_PAGE_NO_MENU')) {
    add_user_history($page);
}
//------------------------------------- </HISTORY> --------------------------------------
show_messages();
$post_script = '';
if ($page['type'] == PAGE_TYPE_HTML) {
    $post_script .= 'function zbxCallPostScripts(){' . "\n";
    if (isset($ZBX_PAGE_POST_JS)) {
        foreach ($ZBX_PAGE_POST_JS as $num => $script) {
            $post_script .= $script . "\n";
        }
    }
    $post_script .= '}' . "\n";
    //		$post_script.= 'try{ chkbxRange.init(); } catch(e){ throw("Checkbox extension failed!");}';
    $post_script .= 'chkbxRange.init();';
    insert_js($post_script);
    if (!defined('ZBX_PAGE_NO_MENU') && !defined('ZBX_PAGE_NO_FOOTER')) {
        $table = new CTable(NULL, "page_footer");
        $table->setCellSpacing(0);
        $table->setCellPadding(1);
        $table->AddRow(array(new CCol(new CLink(S_ZABBIX . SPACE . ZABBIX_VERSION . SPACE . S_COPYRIGHT_BY . SPACE . S_SIA_ZABBIX, 'http://www.zabbix.com', 'highlight', null, true), 'page_footer_l'), new CCol(array(new CSpan(SPACE . SPACE . '|' . SPACE . SPACE, 'divider'), new CSpan($USER_DETAILS['userid'] == 0 ? S_NOT_CONNECTED : S_CONNECTED_AS . SPACE . "'" . $USER_DETAILS['alias'] . "'" . (ZBX_DISTRIBUTED ? SPACE . S_FROM_SMALL . SPACE . "'" . $USER_DETAILS['node']['name'] . "'" : ''), 'footer_sign')), "page_footer_r")));
        $table->Show();
    }
    COpt::profiling_stop("script");
    print '<!--' . "\n" . 'SELECTS: ' . $DB['SELECT_COUNT'] . "\n" . 'EXECUTE: ' . $DB['EXECUTE_COUNT'] . "\n" . 'TOTAL: ' . ($DB['EXECUTE_COUNT'] + $DB['SELECT_COUNT']) . "\n" . '-->';
    echo "</body>\n";
    echo "</html>\n";
}
exit;
Пример #2
0
 /**
  * Get items data
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8
  * @version 1
  *
  * @param array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['triggerids']
  * @param array $options['applicationids']
  * @param boolean $options['status']
  * @param boolean $options['templated_items']
  * @param boolean $options['editable']
  * @param boolean $options['count']
  * @param string $options['pattern']
  * @param int $options['limit']
  * @param string $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('itemid', 'description', 'key_', 'delay', 'history', 'trends', 'type', 'status');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('items' => 'i.itemid'), 'from' => array('items' => 'items i'), 'where' => array('webtype' => 'i.type<>9'), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'proxyids' => null, 'itemids' => null, 'graphids' => null, 'triggerids' => null, 'applicationids' => null, 'webitems' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'group' => null, 'host' => null, 'application' => null, 'belongs' => null, 'with_triggers' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_hosts' => null, 'select_triggers' => null, 'select_graphs' => null, 'select_applications' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_hosts'])) {
             $options['select_hosts'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_triggers'])) {
             $options['select_triggers'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_graphs'])) {
             $options['select_graphs'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_applications'])) {
             $options['select_applications'] = API_OUTPUT_EXTEND;
         }
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['rights'] = 'rights r';
         $sql_parts['from']['users_groups'] = 'users_groups ug';
         $sql_parts['where'][] = 'hg.hostid=i.hostid';
         $sql_parts['where'][] = 'r.id=hg.groupid ';
         $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
         $sql_parts['where'][] = 'ug.userid=' . $userid;
         $sql_parts['where'][] = 'r.permission>=' . $permission;
         $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=hg.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . ')';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']);
         $sql_parts['where'][] = 'hg.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sql_parts['select']['hostid'] = 'i.hostid';
         }
         $sql_parts['where']['hostid'] = DBcondition('i.hostid', $options['hostids']);
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['i'] = 'i.hostid';
         }
     }
     // proxyids
     if (!is_null($options['proxyids'])) {
         zbx_value2array($options['proxyids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sql_parts['select']['proxyid'] = 'h.proxy_hostid';
         }
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where'][] = DBcondition('h.proxy_hostid', $options['proxyids']);
         $sql_parts['where'][] = 'h.hostid=i.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['h'] = 'h.proxy_hostid';
         }
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sql_parts['where']['itemid'] = DBcondition('i.itemid', $options['itemids']);
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['triggerid'] = 'f.triggerid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['where'][] = DBcondition('f.triggerid', $options['triggerids']);
         $sql_parts['where']['if'] = 'i.itemid=f.itemid';
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['applicationid'] = 'ia.applicationid';
         }
         $sql_parts['from']['items_applications'] = 'items_applications ia';
         $sql_parts['where'][] = DBcondition('ia.applicationid', $options['applicationids']);
         $sql_parts['where']['ia'] = 'ia.itemid=i.itemid';
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['graphid'] = 'gi.graphid';
         }
         $sql_parts['from']['graphs_items'] = 'graphs_items gi';
         $sql_parts['where'][] = DBcondition('gi.graphid', $options['graphids']);
         $sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
     }
     // webitems
     if (!is_null($options['webitems'])) {
         unset($sql_parts['where']['webtype']);
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sql_parts['where'][] = 'i.templateid>0';
         } else {
             $sql_parts['where'][] = 'i.templateid=0';
         }
     }
     // templated
     if (!is_null($options['templated'])) {
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sql_parts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sql_parts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['monitored']) {
             $sql_parts['where'][] = 'h.status=' . HOST_STATUS_MONITORED;
             $sql_parts['where'][] = 'i.status=' . ITEM_STATUS_ACTIVE;
         } else {
             $sql_parts['where'][] = '(h.status<>' . HOST_STATUS_MONITORED . ' OR i.status<>' . ITEM_STATUS_ACTIVE . ')';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('items i', $options, $sql_parts);
     }
     // --- FILTER ---
     if (is_array($options['filter'])) {
         zbx_db_filter('items i', $options, $sql_parts);
         if (isset($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sql_parts['from']['hosts'] = 'hosts h';
             $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
             $sql_parts['where']['h'] = DBcondition('h.host', $options['filter']['host'], false, true);
         }
     }
     // group
     if (!is_null($options['group'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['name'] = 'g.name';
         }
         $sql_parts['from']['groups'] = 'groups g';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where']['ghg'] = 'g.groupid = hg.groupid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where'][] = ' UPPER(g.name)=' . zbx_dbstr(zbx_strtoupper($options['group']));
     }
     // host
     if (!is_null($options['host'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['host'] = 'h.host';
         }
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         $sql_parts['where'][] = ' UPPER(h.host)=' . zbx_dbstr(zbx_strtoupper($options['host']));
     }
     // application
     if (!is_null($options['application'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['application'] = 'a.name as application';
         }
         $sql_parts['from']['applications'] = 'applications a';
         $sql_parts['from']['items_applications'] = 'items_applications ia';
         $sql_parts['where']['aia'] = 'a.applicationid = ia.applicationid';
         $sql_parts['where']['iai'] = 'ia.itemid=i.itemid';
         $sql_parts['where'][] = ' UPPER(a.name)=' . zbx_dbstr(zbx_strtoupper($options['application']));
     }
     // with_triggers
     if (!is_null($options['with_triggers'])) {
         if ($options['with_triggers'] == 1) {
             $sql_parts['where'][] = ' EXISTS ( SELECT functionid FROM functions ff WHERE ff.itemid=i.itemid )';
         } else {
             $sql_parts['where'][] = 'NOT EXISTS ( SELECT functionid FROM functions ff WHERE ff.itemid=i.itemid )';
         }
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['items'] = 'i.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT i.itemid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][] = 'i.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('i.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('i.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 'i.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //----------
     $itemids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('i.itemid', $nodeids) . $sql_where . $sql_group . $sql_order;
     $res = DBselect($sql, $sql_limit);
     while ($item = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $item;
             } else {
                 $result = $item['rowscount'];
             }
         } else {
             $itemids[$item['itemid']] = $item['itemid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$item['itemid']] = array('itemid' => $item['itemid']);
             } else {
                 if (!isset($result[$item['itemid']])) {
                     $result[$item['itemid']] = array();
                 }
                 if (!is_null($options['select_hosts']) && !isset($result[$item['itemid']]['hosts'])) {
                     $result[$item['itemid']]['hosts'] = array();
                 }
                 if (!is_null($options['select_triggers']) && !isset($result[$item['itemid']]['triggers'])) {
                     $result[$item['itemid']]['triggers'] = array();
                 }
                 if (!is_null($options['select_graphs']) && !isset($result[$item['itemid']]['graphs'])) {
                     $result[$item['itemid']]['graphs'] = array();
                 }
                 if (!is_null($options['select_applications']) && !isset($result[$item['itemid']]['applications'])) {
                     $result[$item['itemid']]['applications'] = array();
                 }
                 // hostids
                 if (isset($item['hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$item['itemid']]['hosts'])) {
                         $result[$item['itemid']]['hosts'] = array();
                     }
                     $result[$item['itemid']]['hosts'][] = array('hostid' => $item['hostid']);
                     //						unset($item['hostid']);
                 }
                 // triggerids
                 if (isset($item['triggerid']) && is_null($options['select_triggers'])) {
                     if (!isset($result[$item['itemid']]['triggers'])) {
                         $result[$item['itemid']]['triggers'] = array();
                     }
                     $result[$item['itemid']]['triggers'][] = array('triggerid' => $item['triggerid']);
                     unset($item['triggerid']);
                 }
                 // graphids
                 if (isset($item['graphid']) && is_null($options['select_graphs'])) {
                     if (!isset($result[$item['itemid']]['graphs'])) {
                         $result[$item['itemid']]['graphs'] = array();
                     }
                     $result[$item['itemid']]['graphs'][] = array('graphid' => $item['graphid']);
                     unset($item['graphid']);
                 }
                 // applicationids
                 if (isset($item['applicationid']) && is_null($options['select_applications'])) {
                     if (!isset($result[$item['itemid']]['applications'])) {
                         $result[$item['itemid']]['applications'] = array();
                     }
                     $result[$item['itemid']]['applications'][] = array('applicationid' => $item['applicationid']);
                     unset($item['applicationid']);
                 }
                 $result[$item['itemid']] += $item;
             }
         }
     }
     COpt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding hosts
     if (!is_null($options['select_hosts'])) {
         if (is_array($options['select_hosts']) || str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
             $obj_params = array('nodeids' => $nodeids, 'itemids' => $itemids, 'templated_hosts' => 1, 'output' => $options['select_hosts'], 'nopermissions' => 1, 'preservekeys' => 1);
             $hosts = CHost::get($obj_params);
             foreach ($hosts as $hostid => $host) {
                 $hitems = $host['items'];
                 unset($host['items']);
                 foreach ($hitems as $inum => $item) {
                     $result[$item['itemid']]['hosts'][] = $host;
                 }
             }
             $templates = CTemplate::get($obj_params);
             foreach ($templates as $templateid => $template) {
                 $titems = $template['items'];
                 unset($template['items']);
                 foreach ($titems as $inum => $item) {
                     $result[$item['itemid']]['hosts'][] = $template;
                 }
             }
         }
     }
     // Adding triggers
     if (!is_null($options['select_triggers']) && str_in_array($options['select_triggers'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_triggers'], 'itemids' => $itemids, 'preservekeys' => 1);
         $triggers = CTrigger::get($obj_params);
         foreach ($triggers as $triggerid => $trigger) {
             $titems = $trigger['items'];
             unset($trigger['items']);
             foreach ($titems as $inum => $item) {
                 $result[$item['itemid']]['triggers'][] = $trigger;
             }
         }
     }
     // Adding graphs
     if (!is_null($options['select_graphs']) && str_in_array($options['select_graphs'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_graphs'], 'itemids' => $itemids, 'preservekeys' => 1);
         $graphs = CGraph::get($obj_params);
         foreach ($graphs as $graphid => $graph) {
             $gitems = $graph['items'];
             unset($graph['items']);
             foreach ($gitems as $inum => $item) {
                 $result[$item['itemid']]['graphs'][] = $graph;
             }
         }
     }
     // Adding applications
     if (!is_null($options['select_applications']) && str_in_array($options['select_applications'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_applications'], 'itemids' => $itemids, 'preservekeys' => 1);
         $applications = CApplication::get($obj_params);
         foreach ($applications as $applicationid => $application) {
             $aitems = $application['items'];
             unset($application['items']);
             foreach ($aitems as $inum => $item) {
                 $result[$item['itemid']]['applications'][] = $application;
             }
         }
     }
     COpt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
 /**
  * Get Applications data
  *
  * @param array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['triggerids']
  * @param array $options['applicationids']
  * @param boolean $options['status']
  * @param boolean $options['editable']
  * @param boolean $options['count']
  * @param string $options['pattern']
  * @param int $options['limit']
  * @param string $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('applicationid', 'name');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('apps' => 'a.applicationid'), 'from' => array('applications' => 'applications a'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'itemids' => null, 'applicationids' => null, 'templated' => null, 'editable' => null, 'inherited' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'exludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'expandData' => null, 'select_hosts' => null, 'select_items' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_items'])) {
             $options['select_items'] = API_OUTPUT_EXTEND;
         }
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['rights'] = 'rights r';
         $sql_parts['from']['users_groups'] = 'users_groups ug';
         $sql_parts['where'][] = 'hg.hostid=a.hostid';
         $sql_parts['where'][] = 'r.id=hg.groupid ';
         $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
         $sql_parts['where'][] = 'ug.userid=' . $userid;
         $sql_parts['where'][] = 'r.permission>=' . $permission;
         $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=hg.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . ')';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where']['ahg'] = 'a.hostid=hg.hostid';
         $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']);
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sql_parts['select']['hostid'] = 'a.hostid';
         }
         $sql_parts['where']['hostid'] = DBcondition('a.hostid', $options['hostids']);
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['hostid'] = 'a.hostid';
         }
     }
     // expandData
     if (!is_null($options['expandData'])) {
         $sql_parts['select']['host'] = 'h.host';
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['ah'] = 'a.hostid=h.hostid';
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['itemid'] = 'ia.itemid';
         }
         $sql_parts['from']['items_applications'] = 'items_applications ia';
         $sql_parts['where'][] = DBcondition('ia.itemid', $options['itemids']);
         $sql_parts['where']['aia'] = 'a.applicationid=ia.applicationid';
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['applicationid'] = 'a.applicationid';
         }
         $sql_parts['where'][] = DBcondition('a.applicationid', $options['applicationids']);
     }
     // templated
     if (!is_null($options['templated'])) {
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['ah'] = 'a.hostid=h.hostid';
         if ($options['templated']) {
             $sql_parts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sql_parts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sql_parts['where'][] = 'a.templateid<>0';
         } else {
             $sql_parts['where'][] = 'a.templateid=0';
         }
     }
     // extendoutput
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['apps'] = 'a.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(a.applicationid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('applications a', $options, $sql_parts);
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('applications a', $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'][] = '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'];
     }
     //----------
     $applicationids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('a.applicationid', $nodeids) . $sql_where . $sql_group . $sql_order;
     $res = DBselect($sql, $sql_limit);
     while ($application = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $application;
             } else {
                 $result = $application['rowscount'];
             }
         } else {
             $applicationids[$application['applicationid']] = $application['applicationid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$application['applicationid']] = array('applicationid' => $application['applicationid']);
             } else {
                 if (!isset($result[$application['applicationid']])) {
                     $result[$application['applicationid']] = array();
                 }
                 if (!is_null($options['select_hosts']) && !isset($result[$application['applicationid']]['hosts'])) {
                     $result[$application['applicationid']]['hosts'] = array();
                 }
                 if (!is_null($options['select_items']) && !isset($result[$application['applicationid']]['items'])) {
                     $result[$application['applicationid']]['items'] = array();
                 }
                 // hostids
                 if (isset($application['hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$application['applicationid']]['hosts'])) {
                         $result[$application['applicationid']]['hosts'] = array();
                     }
                     $result[$application['applicationid']]['hosts'][] = array('hostid' => $application['hostid']);
                     unset($application['hostid']);
                 }
                 // itemids
                 if (isset($application['itemid']) && is_null($options['select_items'])) {
                     if (!isset($result[$application['applicationid']]['items'])) {
                         $result[$application['applicationid']]['items'] = array();
                     }
                     $result[$application['applicationid']]['items'][] = array('itemid' => $application['itemid']);
                     unset($application['itemid']);
                 }
                 $result[$application['applicationid']] += $application;
             }
         }
     }
     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('output' => $options['select_hosts'], 'applicationids' => $applicationids, 'nopermissions' => 1, 'preservekeys' => 1);
         $hosts = CHost::get($obj_params);
         foreach ($hosts as $hostid => $host) {
             $iapplications = $host['applications'];
             unset($host['applications']);
             foreach ($iapplications as $num => $application) {
                 $result[$application['applicationid']]['hosts'][] = $host;
             }
         }
     }
     // Adding Objects
     // Adding items
     if (!is_null($options['select_items']) && str_in_array($options['select_items'], $subselects_allowed_outputs)) {
         $obj_params = array('output' => $options['select_items'], 'applicationids' => $applicationids, 'nopermissions' => 1, 'preservekeys' => 1);
         $items = CItem::get($obj_params);
         foreach ($items as $itemid => $item) {
             $iapplications = $item['applications'];
             unset($item['applications']);
             foreach ($iapplications as $num => $application) {
                 $result[$application['applicationid']]['items'][] = $item;
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Пример #4
0
 /**
 * Get drule data
 *
 * @static
 * @param array $options
 * @return array
 */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $nodeCheck = false;
     $user_type = $USER_DETAILS['type'];
     $result = array();
     $sort_columns = array('druleid', 'name');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('drules' => 'dr.druleid'), 'from' => array('drules' => 'drules dr'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'druleids' => null, 'dhostids' => null, 'dserviceids' => null, 'dcheckids' => null, 'editable' => null, 'selectDHosts' => null, 'selectDServices' => null, 'selectDChecks' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($def_options, $options);
     // 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();
     // druleids
     if (!is_null($options['druleids'])) {
         zbx_value2array($options['druleids']);
         $sql_parts['where']['druleid'] = DBcondition('dr.druleid', $options['druleids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('dr.druleid', $nodeids);
         }
     }
     // dhostids
     if (!is_null($options['dhostids'])) {
         zbx_value2array($options['dhostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['dhostid'] = 'dh.dhostid';
         }
         $sql_parts['from']['dhosts'] = 'dhosts dh';
         $sql_parts['where']['dhostid'] = DBcondition('dh.dhostid', $options['dhostids']);
         $sql_parts['where']['dhdr'] = 'dh.druleid=dr.druleid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dhostid'] = 'dh.dhostid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('dh.dhostid', $nodeids);
         }
     }
     // 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']['dserviceid'] = DBcondition('ds.dserviceid', $options['dserviceids']);
         $sql_parts['where']['dhdr'] = 'dh.druleid=dr.druleid';
         $sql_parts['where']['dhds'] = 'dh.dhostid=ds.dhostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dserviceid'] = 'ds.dserviceid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('ds.dserviceid', $nodeids);
         }
     }
     // node check !!!!!
     // should be last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('dr.druleid', $nodeids);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['drules'] = 'dr.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT dr.druleid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // search
     if (!is_null($options['search'])) {
         zbx_db_search('drules dr', $options, $sql_parts);
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('drules dr', $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('drules dr', $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'][] = 'dr.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('dr.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('dr.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 'dr.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //------------
     $druleids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_group . $sql_order;
     $db_res = DBselect($sql, $sql_limit);
     while ($drule = DBfetch($db_res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $drule;
             } else {
                 $result = $drule['rowscount'];
             }
         } else {
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$drule['druleid']] = array('druleid' => $drule['druleid']);
             } else {
                 $druleids[$drule['druleid']] = $drule['druleid'];
                 if (!is_null($options['selectDHosts']) && !isset($result[$drule['druleid']]['dhosts'])) {
                     $result[$drule['druleid']]['dhosts'] = array();
                 }
                 if (!is_null($options['selectDChecks']) && !isset($result[$drule['druleid']]['dchecks'])) {
                     $result[$drule['druleid']]['dchecks'] = array();
                 }
                 if (!is_null($options['selectDServices']) && !isset($result[$drule['druleid']]['dservices'])) {
                     $result[$drule['druleid']]['dservices'] = array();
                 }
                 // dhostids
                 if (isset($drule['dhostid']) && is_null($options['selectDHosts'])) {
                     if (!isset($result[$drule['druleid']]['dhosts'])) {
                         $result[$drule['druleid']]['dhosts'] = array();
                     }
                     $result[$drule['druleid']]['dhosts'][] = array('dhostid' => $drule['dhostid']);
                     unset($drule['dhostid']);
                 }
                 // dchecks
                 if (isset($drule['dcheckid']) && is_null($options['selectDChecks'])) {
                     if (!isset($result[$drule['druleid']]['dchecks'])) {
                         $result[$drule['druleid']]['dchecks'] = array();
                     }
                     $result[$drule['druleid']]['dchecks'][] = array('dcheckid' => $drule['dcheckid']);
                     unset($drule['dcheckid']);
                 }
                 // dservices
                 if (isset($drule['dserviceid']) && is_null($options['selectDServices'])) {
                     if (!isset($result[$drule['druleid']]['dservices'])) {
                         $result[$drule['druleid']]['dservices'] = array();
                     }
                     $result[$drule['druleid']]['dservices'][] = array('dserviceid' => $drule['dserviceid']);
                     unset($drule['dserviceid']);
                 }
                 if (!isset($result[$drule['druleid']])) {
                     $result[$drule['druleid']] = array();
                 }
                 $result[$drule['druleid']] += $drule;
             }
         }
     }
     COpt::memoryPick();
     if ($options['output'] != API_OUTPUT_EXTEND || !is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding Discovery Checks
     if (!is_null($options['selectDChecks'])) {
         $obj_params = array('nodeids' => $nodeids, 'dhostids' => $dhostids, 'preservekeys' => 1);
         if (is_array($options['selectDChecks']) || str_in_array($options['selectDChecks'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectDChecks'];
             $dchecks = CDCheck::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($dchecks, 'name');
             }
             foreach ($dchecks as $dcheckid => $dcheck) {
                 unset($dchecks[$dcheckid]['dhosts']);
                 $count = array();
                 foreach ($dcheck['dhosts'] as $dnum => $dhost) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dhost['dhostid']])) {
                             $count[$dhost['dhostid']] = 0;
                         }
                         $count[$dhost['dhostid']]++;
                         if ($count[$dhost['dhostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dhost['dhostid']]['dchecks'][] =& $dchecks[$dcheckid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectDChecks']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $dchecks = CDCheck::get($obj_params);
                 $dchecks = zbx_toHash($dchecks, 'druleid');
                 foreach ($result as $dhostid => $dhost) {
                     if (isset($dchecks[$druleid])) {
                         $result[$dhostid]['dchecks'] = $dchecks[$druleid]['rowscount'];
                     } else {
                         $result[$dhostid]['dchecks'] = 0;
                     }
                 }
             }
         }
     }
     // Adding Discovery Hosts
     if (!is_null($options['selectDHosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'druleids' => $druleids, '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, 'name');
             }
             foreach ($dhosts as $dhostid => $dhost) {
                 unset($dhosts[$dhostid]['drules']);
                 foreach ($dhost['drules'] as $dnum => $drule) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$drule['druleid']])) {
                             $count[$drule['druleid']] = 0;
                         }
                         $count[$drule['druleid']]++;
                         if ($count[$drule['druleid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$drule['druleid']]['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, 'druleid');
                 foreach ($result as $druleid => $drule) {
                     if (isset($dhosts[$druleid])) {
                         $result[$druleid]['dhosts'] = $dhosts[$druleid]['rowscount'];
                     } else {
                         $result[$druleid]['dhosts'] = 0;
                     }
                 }
             }
         }
     }
     COpt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Пример #5
0
function DBexecute($query, $skip_error_messages = 0)
{
    global $DB;
    $result = false;
    $time_start = microtime(true);
    if (isset($DB['DB']) && !empty($DB['DB'])) {
        $DB['EXECUTE_COUNT']++;
        // WRONG FOR ORACLE!!
        //SDI('SQL xec: '.$query);
        switch ($DB['TYPE']) {
            case 'MYSQL':
                $result = mysql_query($query, $DB['DB']);
                if (!$result) {
                    error('Error in query [' . $query . '] [' . mysql_error() . ']');
                }
                break;
            case 'POSTGRESQL':
                if (!($result = pg_query($DB['DB'], $query))) {
                    error('Error in query [' . $query . '] [' . pg_last_error() . ']');
                }
                break;
            case 'ORACLE':
                $stid = OCIParse($DB['DB'], $query);
                if (!$stid) {
                    $e = @ocierror();
                    error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']');
                }
                $result = @OCIExecute($stid, $DB['TRANSACTIONS'] ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS);
                if (!$result) {
                    $e = ocierror($stid);
                    error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']');
                } else {
                    $result = $stid;
                }
                break;
            case 'SQLITE3':
                if (!$DB['TRANSACTIONS']) {
                    lock_db_access();
                }
                $result = sqlite3_exec($DB['DB'], $query);
                if (!$result) {
                    error('Error in query [' . $query . '] [' . sqlite3_error($DB['DB']) . ']');
                }
                if (!$DB['TRANSACTIONS']) {
                    unlock_db_access();
                }
                break;
        }
        if ($DB['TRANSACTIONS'] && !$result) {
            $DB['TRANSACTION_STATE'] &= $result;
            //			SDI($query);
            //			SDI($DB['TRANSACTION_STATE']);
        }
    }
    COpt::savesqlrequest(microtime(true) - $time_start, $query);
    return $result;
}
Пример #6
0
// Header
$url = 'overview.php?fullscreen=' . ($_REQUEST['fullscreen'] ? '0' : '1');
$fs_icon = new CDiv(SPACE, 'fullscreen');
$fs_icon->setAttribute('title', $_REQUEST['fullscreen'] ? S_NORMAL . ' ' . S_VIEW : S_FULLSCREEN);
$fs_icon->addAction('onclick', new CScript("javascript: document.location = '" . $url . "';"));
$over_wdgt->addHeader(S_OVERVIEW_BIG, array($fs_icon, $help));
// 2nd heder
$form_l = new CForm();
$form_l->setMethod('get');
$form_l->addVar('groupid', $_REQUEST['groupid']);
$cmbStyle = new CComboBox('view_style', $_REQUEST['view_style'], 'submit()');
$cmbStyle->addItem(STYLE_TOP, S_TOP);
$cmbStyle->addItem(STYLE_LEFT, S_LEFT);
$form_l->additem(array(S_HOSTS_LOCATION . SPACE, $cmbStyle));
$over_wdgt->addHeader($form_l, $form);
//	show_table_header(S_OVERVIEW_BIG,$form);
//-------------
if ($_REQUEST['type'] == SHOW_DATA) {
    COpt::profiling_start('get_items_data_overview');
    $table = get_items_data_overview($PAGE_HOSTS['hostids'], $_REQUEST['view_style']);
    COpt::profiling_stop('get_items_data_overview');
} else {
    if ($_REQUEST['type'] == SHOW_TRIGGERS) {
        COpt::profiling_start('get_triggers_overview');
        $table = get_triggers_overview($PAGE_HOSTS['hostids'], $_REQUEST['view_style']);
        COpt::profiling_stop('get_triggers_overview');
    }
}
$over_wdgt->addItem($table);
$over_wdgt->show();
include_once 'include/page_footer.php';
Пример #7
0
 /**
  * Get history data
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8.3
  * @version 1.3
  *
  * @param array $options
  * @param array $options['itemids']
  * @param boolean $options['editable']
  * @param string $options['pattern']
  * @param int $options['limit']
  * @param string $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $nodeCheck = false;
     $result = array();
     $sort_columns = array('itemid', 'clock');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('history' => 'h.itemid'), 'from' => array(), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('history' => ITEM_VALUE_TYPE_UINT64, 'nodeids' => null, 'hostids' => null, 'itemids' => null, 'triggerids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'time_from' => null, 'time_till' => null, 'output' => API_OUTPUT_REFER, 'countOutput' => null, 'groupCount' => null, 'groupOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     switch ($options['history']) {
         case ITEM_VALUE_TYPE_LOG:
             $sql_parts['from']['history'] = 'history_log h';
             $sort_columns[] = 'id';
             break;
         case ITEM_VALUE_TYPE_TEXT:
             $sql_parts['from']['history'] = 'history_text h';
             $sort_columns[] = 'id';
             break;
         case ITEM_VALUE_TYPE_STR:
             $sql_parts['from']['history'] = 'history_str h';
             break;
         case ITEM_VALUE_TYPE_UINT64:
             $sql_parts['from']['history'] = 'history_uint h';
             break;
         case ITEM_VALUE_TYPE_FLOAT:
         default:
             $sql_parts['from']['history'] = 'history h';
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $USER_DETAILS['type'] || $options['nopermissions']) {
     } else {
         $itemOptions = array('editable' => $options['editable'], 'preservekeys' => 1);
         if (!is_null($options['itemids'])) {
             $itemOptions['itemids'] = $options['itemids'];
         }
         $items = CItem::get($itemOptions);
         $options['itemids'] = array_keys($items);
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         $sql_parts['where']['itemid'] = DBcondition('h.itemid', $options['itemids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('h.itemid', $nodeids);
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['hostid'] = 'i.hostid';
         }
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where']['i'] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['hi'] = 'h.itemid=i.itemid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('i.hostid', $nodeids);
         }
     }
     // node check !!!!!
     // should be last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('h.itemid', $nodeids);
     }
     // time_from
     if (!is_null($options['time_from'])) {
         $sql_parts['select']['clock'] = 'h.clock';
         $sql_parts['where']['clock_from'] = 'h.clock>=' . $options['time_from'];
     }
     // time_till
     if (!is_null($options['time_till'])) {
         $sql_parts['select']['clock'] = 'h.clock';
         $sql_parts['where']['clock_till'] = 'h.clock<=' . $options['time_till'];
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter($sql_parts['from']['history'], $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search($sql_parts['from']['history'], $options, $sql_parts);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         unset($sql_parts['select']['clock']);
         $sql_parts['select']['history'] = 'h.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT h.hostid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // groupOutput
     $groupOutput = false;
     if (!is_null($options['groupOutput'])) {
         if (str_in_array('h.' . $options['groupOutput'], $sql_parts['select']) || str_in_array('h.*', $sql_parts['select'])) {
             $groupOutput = true;
         }
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         if ($options['sortfield'] == 'clock') {
             $sql_parts['order']['itemid'] = 'h.itemid ' . $sortorder;
         }
         $sql_parts['order'][$options['sortfield']] = 'h.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('h.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('h.*', $sql_parts['select'])) {
             $sql_parts['select'][$options['sortfield']] = 'h.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //---------------
     $itemids = array();
     $triggerids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_order;
     $db_res = DBselect($sql, $sql_limit);
     //SDI($sql);
     $count = 0;
     $group = array();
     while ($data = DBfetch($db_res)) {
         if ($options['countOutput']) {
             $result = $data;
         } else {
             $itemids[$data['itemid']] = $data['itemid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$count] = array('itemid' => $data['itemid']);
             } else {
                 $result[$count] = array();
                 // hostids
                 if (isset($data['hostid'])) {
                     if (!isset($result[$count]['hosts'])) {
                         $result[$count]['hosts'] = array();
                     }
                     $result[$count]['hosts'][] = array('hostid' => $data['hostid']);
                     unset($data['hostid']);
                 }
                 // triggerids
                 if (isset($data['triggerid'])) {
                     if (!isset($result[$count]['triggers'])) {
                         $result[$count]['triggers'] = array();
                     }
                     $result[$count]['triggers'][] = array('triggerid' => $data['triggerid']);
                     unset($data['triggerid']);
                 }
                 // itemids
                 //					if(isset($data['itemid']) && !is_null($options['itemids'])){
                 //						if(!isset($result[$count]['items'])) $result[$count]['items'] = array();
                 //						$result[$count]['items'][] = array('itemid' => $data['itemid']);
                 //					}
                 $result[$count] += $data;
                 // grouping
                 if ($groupOutput) {
                     $dataid = $data[$options['groupOutput']];
                     if (!isset($group[$dataid])) {
                         $group[$dataid] = array();
                     }
                     $group[$dataid][] = $result[$count];
                 }
                 $count++;
             }
         }
     }
     COpt::memoryPick();
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Пример #8
0
                }
                if (!isset($_REQUEST["plaintext"])) {
                    $table->ShowRow($new_row);
                } else {
                    echo date("Y-m-d H:i:s", $row["clock"]);
                    echo "\t" . $row["clock"] . "\t" . htmlspecialchars($row["value"]) . "\n";
                }
            }
            if (!isset($_REQUEST["plaintext"])) {
                $table->ShowEnd();
                // to solve memory leak we call 'Show' method by steps
                echo SBR;
            } else {
                echo "</pre>";
            }
            COpt::profiling_stop("history");
        }
    }
}
if (!isset($_REQUEST["plaintext"])) {
    if (str_in_array($_REQUEST["action"], array("showvalues", "showgraph"))) {
        $stime = get_min_itemclock_by_itemid($_REQUEST["itemid"]);
        $stime = is_null($stime) ? 0 : $stime;
        $bstime = time() - $effectiveperiod;
        if (isset($_REQUEST['stime'])) {
            $bstime = $_REQUEST['stime'];
            $bstime = mktime(substr($bstime, 8, 2), substr($bstime, 10, 2), 0, substr($bstime, 4, 2), substr($bstime, 6, 2), substr($bstime, 0, 4));
        }
        $script = 'scrollinit(0,' . $effectiveperiod . ',' . $stime . ',0,' . $bstime . ');
						showgraphmenu("graph");';
        if (isset($dom_graph_id)) {
Пример #9
0
    /**
     * Get Map data
     *
     * @param _array $options
     * @param array $options['nodeids'] Node IDs
     * @param array $options['groupids'] HostGroup IDs
     * @param array $options['hostids'] Host IDs
     * @param boolean $options['monitored_hosts'] only monitored Hosts
     * @param boolean $options['templated_hosts'] include templates in result
     * @param boolean $options['with_items'] only with items
     * @param boolean $options['with_monitored_items'] only with monitored items
     * @param boolean $options['with_historical_items'] only with historical items
     * @param boolean $options['with_triggers'] only with triggers
     * @param boolean $options['with_monitored_triggers'] only with monitored triggers
     * @param boolean $options['with_httptests'] only with http tests
     * @param boolean $options['with_monitored_httptests'] only with monitored http tests
     * @param boolean $options['with_graphs'] only with graphs
     * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins
     * @param int $options['extendoutput'] return all fields for Hosts
     * @param int $options['count'] count Hosts, returned column name is rowscount
     * @param string $options['pattern'] search hosts by pattern in host names
     * @param int $options['limit'] limit selection
     * @param string $options['sortorder']
     * @param string $options['sortfield']
     * @return array|boolean Host data as array or false if error
     */
    public static function get($options = array())
    {
        global $USER_DETAILS;
        $result = array();
        $user_type = $USER_DETAILS['type'];
        $sort_columns = array('name');
        // allowed columns for sorting
        $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
        // allowed output options for [ select_* ] params
        $sql_parts = array('select' => array('sysmaps' => 's.sysmapid'), 'from' => array('sysmaps' => 'sysmaps s'), 'where' => array(), 'order' => array(), 'limit' => null);
        $def_options = array('nodeids' => null, 'sysmapids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'extendoutput' => null, 'output' => API_OUTPUT_REFER, 'select_selements' => null, 'select_links' => 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_selements'])) {
                $options['select_selements'] = API_OUTPUT_EXTEND;
            }
            if (!is_null($options['select_links'])) {
                $options['select_links'] = API_OUTPUT_EXTEND;
            }
        }
        // editable + PERMISSION CHECK
        // nodeids
        $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
        // sysmapids
        if (!is_null($options['sysmapids'])) {
            zbx_value2array($options['sysmapids']);
            $sql_parts['where']['sysmapid'] = DBcondition('s.sysmapid', $options['sysmapids']);
        }
        // search
        if (!is_null($options['search'])) {
            zbx_db_search('sysmaps s', $options, $sql_parts);
        }
        // filter
        if (!is_null($options['filter'])) {
            zbx_value2array($options['filter']);
            if (isset($options['filter']['sysmapid']) && !is_null($options['filter']['sysmapid'])) {
                zbx_value2array($options['filter']['sysmapid']);
                $sql_parts['where']['sysmapid'] = DBcondition('s.sysmapid', $options['filter']['sysmapid']);
            }
            if (isset($options['filter']['name']) && !is_null($options['filter']['name'])) {
                zbx_value2array($options['filter']['name']);
                $sql_parts['where']['name'] = DBcondition('s.name', $options['filter']['name'], false, true);
            }
        }
        // output
        if ($options['output'] == API_OUTPUT_EXTEND) {
            $sql_parts['select']['sysmaps'] = 's.*';
        }
        // countOutput
        if (!is_null($options['countOutput'])) {
            $options['sortfield'] = '';
            $sql_parts['select'] = array('count(DISTINCT s.sysmapid) 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'][] = 's.' . $options['sortfield'] . ' ' . $sortorder;
            if (!str_in_array('s.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('s.*', $sql_parts['select'])) {
                $sql_parts['select'][] = 's.' . $options['sortfield'];
            }
        }
        // limit
        if (zbx_ctype_digit($options['limit']) && $options['limit']) {
            $sql_parts['limit'] = $options['limit'];
        }
        //-------
        $sysmapids = array();
        $sql_parts['select'] = array_unique($sql_parts['select']);
        $sql_parts['from'] = array_unique($sql_parts['from']);
        $sql_parts['where'] = array_unique($sql_parts['where']);
        $sql_parts['order'] = array_unique($sql_parts['order']);
        $sql_select = '';
        $sql_from = '';
        $sql_where = '';
        $sql_order = '';
        if (!empty($sql_parts['select'])) {
            $sql_select .= implode(',', $sql_parts['select']);
        }
        if (!empty($sql_parts['from'])) {
            $sql_from .= implode(',', $sql_parts['from']);
        }
        if (!empty($sql_parts['where'])) {
            $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
        }
        if (!empty($sql_parts['order'])) {
            $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
        }
        $sql_limit = $sql_parts['limit'];
        $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . '
				FROM ' . $sql_from . '
				WHERE ' . DBin_node('s.sysmapid', $nodeids) . $sql_where . $sql_order;
        $res = DBselect($sql, $sql_limit);
        while ($sysmap = DBfetch($res)) {
            if ($options['countOutput']) {
                $result = $sysmap['rowscount'];
            } else {
                $sysmapids[$sysmap['sysmapid']] = $sysmap['sysmapid'];
                if ($options['output'] == API_OUTPUT_SHORTEN) {
                    $result[$sysmap['sysmapid']] = array('sysmapid' => $sysmap['sysmapid']);
                } else {
                    if (!isset($result[$sysmap['sysmapid']])) {
                        $result[$sysmap['sysmapid']] = array();
                    }
                    if (!is_null($options['select_selements']) && !isset($result[$sysmap['sysmapid']]['selements'])) {
                        $result[$sysmap['sysmapid']]['selements'] = array();
                    }
                    if (!is_null($options['select_links']) && !isset($result[$sysmap['sysmapid']]['links'])) {
                        $result[$sysmap['sysmapid']]['links'] = array();
                    }
                    if (isset($sysmap['highlight'])) {
                        $sysmap['expandproblem'] = $sysmap['highlight'] & ZBX_MAP_EXPANDPROBLEM ? 0 : 1;
                        $sysmap['markelements'] = $sysmap['highlight'] & ZBX_MAP_MARKELEMENTS ? 1 : 0;
                        if (($sysmap['highlight'] & ZBX_MAP_EXTACK_SEPARATED) == ZBX_MAP_EXTACK_SEPARATED) {
                            $sysmap['show_unack'] = EXTACK_OPTION_BOTH;
                        } else {
                            if ($sysmap['highlight'] & ZBX_MAP_EXTACK_UNACK) {
                                $sysmap['show_unack'] = EXTACK_OPTION_UNACK;
                            } else {
                                $sysmap['show_unack'] = EXTACK_OPTION_ALL;
                            }
                        }
                        $sysmap['highlight'] = $sysmap['highlight'] & ZBX_MAP_HIGHLIGHT ? 1 : 0;
                    }
                    $result[$sysmap['sysmapid']] += $sysmap;
                }
            }
        }
        if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
        } else {
            if (!empty($result)) {
                $link_triggers = array();
                $sql = 'SELECT slt.triggerid, sl.sysmapid' . ' FROM sysmaps_link_triggers slt, sysmaps_links sl' . ' WHERE ' . DBcondition('sl.sysmapid', $sysmapids) . ' AND sl.linkid=slt.linkid';
                $db_link_triggers = DBselect($sql);
                while ($link_trigger = DBfetch($db_link_triggers)) {
                    $link_triggers[$link_trigger['sysmapid']] = $link_trigger['triggerid'];
                }
                if (!empty($link_triggers)) {
                    $all_triggers = CTrigger::get(array('triggerids' => $link_triggers, 'editable' => $options['editable'], 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
                    foreach ($link_triggers as $id => $triggerid) {
                        if (!isset($all_triggers[$triggerid])) {
                            unset($result[$id], $sysmapids[$id]);
                        }
                    }
                }
                $hosts_to_check = array();
                $maps_to_check = array();
                $triggers_to_check = array();
                $host_groups_to_check = array();
                $selements = array();
                $db_selements = DBselect('SELECT * FROM sysmaps_elements WHERE ' . DBcondition('sysmapid', $sysmapids));
                while ($selement = DBfetch($db_selements)) {
                    $selements[$selement['selementid']] = $selement;
                    switch ($selement['elementtype']) {
                        case SYSMAP_ELEMENT_TYPE_HOST:
                            $hosts_to_check[$selement['elementid']] = $selement['elementid'];
                            break;
                        case SYSMAP_ELEMENT_TYPE_MAP:
                            $maps_to_check[$selement['elementid']] = $selement['elementid'];
                            break;
                        case SYSMAP_ELEMENT_TYPE_TRIGGER:
                            $triggers_to_check[$selement['elementid']] = $selement['elementid'];
                            break;
                        case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
                            $host_groups_to_check[$selement['elementid']] = $selement['elementid'];
                            break;
                    }
                }
                // sdi($hosts_to_check);
                // sdi($maps_to_check);
                // sdi($triggers_to_check);
                // sdi($host_groups_to_check);
                $nodeids = get_current_nodeid(true);
                if (!empty($hosts_to_check)) {
                    $host_options = array('hostids' => $hosts_to_check, 'nodeids' => $nodeids, 'editable' => $options['editable'], 'preservekeys' => 1, 'output' => API_OUTPUT_SHORTEN);
                    $allowed_hosts = CHost::get($host_options);
                    foreach ($hosts_to_check as $elementid) {
                        if (!isset($allowed_hosts[$elementid])) {
                            foreach ($selements as $selementid => $selement) {
                                if ($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST && $selement['elementid'] == $elementid) {
                                    unset($result[$selement['sysmapid']], $selements[$selementid]);
                                }
                            }
                        }
                    }
                }
                if (!empty($maps_to_check)) {
                    $map_options = array('sysmapids' => $maps_to_check, 'nodeids' => $nodeids, 'editable' => $options['editable'], 'preservekeys' => 1, 'output' => API_OUTPUT_SHORTEN);
                    $allowed_maps = self::get($map_options);
                    foreach ($maps_to_check as $elementid) {
                        if (!isset($allowed_maps[$elementid])) {
                            foreach ($selements as $selementid => $selement) {
                                if ($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_MAP && $selement['elementid'] == $elementid) {
                                    unset($result[$selement['sysmapid']], $selements[$selementid]);
                                }
                            }
                        }
                    }
                }
                if (!empty($triggers_to_check)) {
                    $trigger_options = array('triggerids' => $triggers_to_check, 'nodeids' => $nodeids, 'editable' => $options['editable'], 'preservekeys' => 1, 'output' => API_OUTPUT_SHORTEN);
                    $allowed_triggers = CTrigger::get($trigger_options);
                    foreach ($triggers_to_check as $elementid) {
                        if (!isset($allowed_triggers[$elementid])) {
                            foreach ($selements as $selementid => $selement) {
                                if ($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_TRIGGER && $selement['elementid'] == $elementid) {
                                    unset($result[$selement['sysmapid']], $selements[$selementid]);
                                }
                            }
                        }
                    }
                }
                if (!empty($host_groups_to_check)) {
                    $hostgroup_options = array('groupids' => $host_groups_to_check, 'nodeids' => $nodeids, 'editable' => $options['editable'], 'preservekeys' => 1, 'output' => API_OUTPUT_SHORTEN);
                    $allowed_host_groups = CHostGroup::get($hostgroup_options);
                    foreach ($host_groups_to_check as $elementid) {
                        if (!isset($allowed_host_groups[$elementid])) {
                            foreach ($selements as $selementid => $selement) {
                                if ($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST_GROUP && $selement['elementid'] == $elementid) {
                                    unset($result[$selement['sysmapid']], $selements[$selementid]);
                                }
                            }
                        }
                    }
                }
            }
        }
        COpt::memoryPick();
        if (!is_null($options['countOutput'])) {
            if (is_null($options['preservekeys'])) {
                $result = zbx_cleanHashes($result);
            }
            return $result;
        }
        // Adding Elements
        if (!is_null($options['select_selements']) && str_in_array($options['select_selements'], $subselects_allowed_outputs)) {
            if (!isset($map_selements)) {
                $map_selements = array();
                $sql = 'SELECT se.* ' . ' FROM sysmaps_elements se ' . ' WHERE ' . DBcondition('se.sysmapid', $sysmapids);
                $db_selements = DBselect($sql);
                while ($selement = DBfetch($db_selements)) {
                    $map_selements[$selement['selementid']] = $selement;
                }
            }
            foreach ($map_selements as $num => $selement) {
                if (!isset($result[$selement['sysmapid']]['selements'])) {
                    $result[$selement['sysmapid']]['selements'] = array();
                }
                $result[$selement['sysmapid']]['selements'][] = $selement;
            }
        }
        // Adding Links
        if (!is_null($options['select_links']) && str_in_array($options['select_links'], $subselects_allowed_outputs)) {
            if (!isset($map_links)) {
                $linkids = array();
                $map_links = array();
                $sql = 'SELECT sl.* FROM sysmaps_links sl WHERE ' . DBcondition('sl.sysmapid', $sysmapids);
                $db_links = DBselect($sql);
                while ($link = DBfetch($db_links)) {
                    $link['linktriggers'] = array();
                    $map_links[$link['linkid']] = $link;
                    $linkids[$link['linkid']] = $link['linkid'];
                }
                $sql = 'SELECT DISTINCT slt.* FROM sysmaps_link_triggers slt WHERE ' . DBcondition('slt.linkid', $linkids);
                $db_link_triggers = DBselect($sql);
                while ($link_trigger = DBfetch($db_link_triggers)) {
                    $map_links[$link_trigger['linkid']]['linktriggers'][] = $link_trigger;
                }
            }
            foreach ($map_links as $num => $link) {
                if (!isset($result[$link['sysmapid']]['links'])) {
                    $result[$link['sysmapid']]['links'] = array();
                }
                $result[$link['sysmapid']]['links'][] = $link;
            }
        }
        COpt::memoryPick();
        // removing keys (hash -> array)
        if (is_null($options['preservekeys'])) {
            $result = zbx_cleanHashes($result);
        }
        return $result;
    }
Пример #10
0
 /**
  * Get Template data
  *
  * @param array $options
  * @return array|boolean Template data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $nodeCheck = false;
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('hostid', 'host');
     // 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('templates' => 'h.hostid'), 'from' => array('hosts' => 'hosts h'), 'where' => array('h.status=' . HOST_STATUS_TEMPLATE), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'parentTemplateids' => null, 'hostids' => null, 'graphids' => null, 'itemids' => null, 'triggerids' => null, 'with_items' => null, 'with_triggers' => null, 'with_graphs' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => '', 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'select_hosts' => null, 'select_templates' => null, 'selectParentTemplates' => null, 'select_items' => null, 'select_triggers' => null, 'select_graphs' => null, 'select_applications' => null, 'select_macros' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_groups'])) {
             $options['select_groups'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_templates'])) {
             $options['select_templates'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['selectParentTemplates'])) {
             $options['selectParentTemplates'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_hosts'])) {
             $options['select_hosts'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_items'])) {
             $options['select_items'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_triggers'])) {
             $options['select_triggers'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_graphs'])) {
             $options['select_graphs'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_applications'])) {
             $options['select_applications'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_macros'])) {
             $options['select_macros'] = API_OUTPUT_EXTEND;
         }
     }
     if (is_array($options['output'])) {
         $sql_parts['select']['hosts'] = ' h.' . implode(',h.', $options['output']);
         $options['output'] = API_OUTPUT_REFER;
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['rights'] = 'rights r';
         $sql_parts['from']['users_groups'] = 'users_groups ug';
         $sql_parts['where'][] = 'hg.hostid=h.hostid';
         $sql_parts['where'][] = 'r.id=hg.groupid ';
         $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
         $sql_parts['where'][] = 'ug.userid=' . $userid;
         $sql_parts['where'][] = 'r.permission>=' . $permission;
         $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=hg.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . ')';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']);
         $sql_parts['where']['hgh'] = 'hg.hostid=h.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['hg'] = 'hg.groupid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('hg.groupid', $nodeids);
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         $sql_parts['where']['templateid'] = DBcondition('h.hostid', $options['templateids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('h.hostid', $nodeids);
         }
     }
     // parentTemplateids
     if (!is_null($options['parentTemplateids'])) {
         zbx_value2array($options['parentTemplateids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['parentTemplateid'] = 'ht.templateid as parentTemplateid';
         }
         $sql_parts['from']['hosts_templates'] = 'hosts_templates ht';
         $sql_parts['where'][] = DBcondition('ht.templateid', $options['parentTemplateids']);
         $sql_parts['where']['hht'] = 'h.hostid=ht.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['templateid'] = 'ht.templateid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('ht.templateid', $nodeids);
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['linked_hostid'] = 'ht.hostid as linked_hostid';
         }
         $sql_parts['from']['hosts_templates'] = 'hosts_templates ht';
         $sql_parts['where'][] = DBcondition('ht.hostid', $options['hostids']);
         $sql_parts['where']['hht'] = 'h.hostid=ht.templateid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['ht'] = 'ht.hostid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('ht.hostid', $nodeids);
         }
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['itemid'] = 'i.itemid';
         }
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('i.itemid', $options['itemids']);
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('i.itemid', $nodeids);
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['triggerid'] = 'f.triggerid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('f.triggerid', $options['triggerids']);
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('f.triggerid', $nodeids);
         }
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['graphid'] = 'gi.graphid';
         }
         $sql_parts['from']['graphs_items'] = 'graphs_items gi';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('gi.graphid', $options['graphids']);
         $sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('gi.graphid', $nodeids);
         }
     }
     // node check !!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('h.hostid', $nodeids);
     }
     // with_items
     if (!is_null($options['with_items'])) {
         $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid )';
     }
     // with_triggers
     if (!is_null($options['with_triggers'])) {
         $sql_parts['where'][] = 'EXISTS( ' . ' SELECT i.itemid ' . ' FROM items i, functions f, triggers t ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid)';
     }
     // with_graphs
     if (!is_null($options['with_graphs'])) {
         $sql_parts['where'][] = 'EXISTS(' . 'SELECT DISTINCT i.itemid ' . ' FROM items i, graphs_items gi ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=gi.itemid)';
     }
     // extendoutput
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['templates'] = 'h.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT h.hostid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // 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);
     }
     // 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'];
     }
     //-------------
     $templateids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('h.hostid', $nodeids) . $sql_where . $sql_group . $sql_order;
     $res = DBselect($sql, $sql_limit);
     while ($template = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $template;
             } else {
                 $result = $template['rowscount'];
             }
         } else {
             $template['templateid'] = $template['hostid'];
             $templateids[$template['templateid']] = $template['templateid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$template['templateid']] = array('templateid' => $template['templateid']);
             } else {
                 if (!isset($result[$template['templateid']])) {
                     $result[$template['templateid']] = array();
                 }
                 if (!is_null($options['select_groups']) && !isset($result[$template['templateid']]['groups'])) {
                     $template['groups'] = array();
                 }
                 if (!is_null($options['select_templates']) && !isset($result[$template['templateid']]['templates'])) {
                     $template['templates'] = array();
                 }
                 if (!is_null($options['select_hosts']) && !isset($result[$template['templateid']]['hosts'])) {
                     $template['hosts'] = array();
                 }
                 if (!is_null($options['selectParentTemplates']) && !isset($result[$template['templateid']]['parentTemplates'])) {
                     $template['parentTemplates'] = array();
                 }
                 if (!is_null($options['select_items']) && !isset($result[$template['templateid']]['items'])) {
                     $template['items'] = array();
                 }
                 if (!is_null($options['select_triggers']) && !isset($result[$template['templateid']]['triggers'])) {
                     $template['triggers'] = array();
                 }
                 if (!is_null($options['select_graphs']) && !isset($result[$template['templateid']]['graphs'])) {
                     $template['graphs'] = array();
                 }
                 if (!is_null($options['select_applications']) && !isset($result[$template['templateid']]['applications'])) {
                     $template['applications'] = array();
                 }
                 if (!is_null($options['select_macros']) && !isset($result[$template['hostid']]['macros'])) {
                     $template['macros'] = array();
                 }
                 // groupids
                 if (isset($template['groupid']) && is_null($options['select_groups'])) {
                     if (!isset($result[$template['templateid']]['groups'])) {
                         $result[$template['templateid']]['groups'] = array();
                     }
                     $result[$template['templateid']]['groups'][] = array('groupid' => $template['groupid']);
                     unset($template['groupid']);
                 }
                 // hostids
                 if (isset($template['linked_hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$template['templateid']]['hosts'])) {
                         $result[$template['templateid']]['hosts'] = array();
                     }
                     $result[$template['templateid']]['hosts'][] = array('hostid' => $template['linked_hostid']);
                     unset($template['linked_hostid']);
                 }
                 // parentTemplateids
                 if (isset($template['parentTemplateid']) && is_null($options['selectParentTemplates'])) {
                     if (!isset($result[$template['templateid']]['parentTemplates'])) {
                         $result[$template['templateid']]['parentTemplates'] = array();
                     }
                     $result[$template['templateid']]['parentTemplates'][] = array('templateid' => $template['parentTemplateid']);
                     unset($template['parentTemplateid']);
                 }
                 // itemids
                 if (isset($template['itemid']) && is_null($options['select_items'])) {
                     if (!isset($result[$template['templateid']]['items'])) {
                         $result[$template['templateid']]['items'] = array();
                     }
                     $result[$template['templateid']]['items'][] = array('itemid' => $template['itemid']);
                     unset($template['itemid']);
                 }
                 // triggerids
                 if (isset($template['triggerid']) && is_null($options['select_triggers'])) {
                     if (!isset($result[$template['hostid']]['triggers'])) {
                         $result[$template['hostid']]['triggers'] = array();
                     }
                     $result[$template['hostid']]['triggers'][] = array('triggerid' => $template['triggerid']);
                     unset($template['triggerid']);
                 }
                 // graphids
                 if (isset($template['graphid']) && is_null($options['select_graphs'])) {
                     if (!isset($result[$template['templateid']]['graphs'])) {
                         $result[$template['templateid']]['graphs'] = array();
                     }
                     $result[$template['templateid']]['graphs'][] = array('graphid' => $template['graphid']);
                     unset($template['graphid']);
                 }
                 $result[$template['templateid']] += $template;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding Groups
     if (!is_null($options['select_groups']) && str_in_array($options['select_groups'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_groups'], 'hostids' => $templateids, 'preservekeys' => 1);
         $groups = CHostgroup::get($obj_params);
         foreach ($groups as $groupid => $group) {
             $ghosts = $group['hosts'];
             unset($group['hosts']);
             foreach ($ghosts as $hnum => $template) {
                 $result[$template['hostid']]['groups'][] = $group;
             }
         }
     }
     // Adding Templates
     if (!is_null($options['select_templates'])) {
         $obj_params = array('nodeids' => $nodeids, 'parentTemplateids' => $templateids, 'preservekeys' => 1);
         if (is_array($options['select_templates']) || str_in_array($options['select_templates'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_templates'];
             $templates = CTemplate::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($templates, 'host');
             }
             foreach ($templates as $templateid => $template) {
                 unset($templates[$templateid]['parentTemplates']);
                 if (isset($template['parentTemplates']) && is_array($template['parentTemplates'])) {
                     $count = array();
                     foreach ($template['parentTemplates'] as $hnum => $parentTemplate) {
                         if (!is_null($options['limitSelects'])) {
                             if (!isset($count[$parentTemplate['templateid']])) {
                                 $count[$parentTemplate['templateid']] = 0;
                             }
                             $count[$parentTemplate['hostid']]++;
                             if ($count[$parentTemplate['templateid']] > $options['limitSelects']) {
                                 continue;
                             }
                         }
                         $result[$parentTemplate['templateid']]['templates'][] =& $templates[$templateid];
                     }
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_templates']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $templates = CTemplate::get($obj_params);
                 $templates = zbx_toHash($templates, 'hostid');
                 foreach ($result as $templateid => $template) {
                     if (isset($templates[$groupid])) {
                         $result[$templateid]['templates'] = $templates[$templateid]['rowscount'];
                     } else {
                         $result[$templateid]['templates'] = 0;
                     }
                 }
             }
         }
     }
     // Adding Hosts
     if (!is_null($options['select_hosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'templateids' => $templateids, 'preservekeys' => 1);
         if (is_array($options['select_hosts']) || str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_hosts'];
             $hosts = CHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'host');
             }
             foreach ($hosts as $hostid => $host) {
                 unset($hosts[$hostid]['templates']);
                 foreach ($host['templates'] as $tnum => $template) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$template['templateid']])) {
                             $count[$template['templateid']] = 0;
                         }
                         $count[$template['templateid']]++;
                         if ($count[$template['templateid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$template['templateid']]['hosts'][] =& $hosts[$hostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_hosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $hosts = CHost::get($obj_params);
                 $hosts = zbx_toHash($hosts, 'hostid');
                 foreach ($result as $templateid => $template) {
                     if (isset($hosts[$templateid])) {
                         $result[$templateid]['hosts'] = $hosts[$templateid]['rowscount'];
                     } else {
                         $result[$templateid]['hosts'] = 0;
                     }
                 }
             }
         }
     }
     // Adding parentTemplates
     if (!is_null($options['selectParentTemplates'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $templateids, 'preservekeys' => 1);
         if (is_array($options['selectParentTemplates']) || str_in_array($options['selectParentTemplates'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectParentTemplates'];
             $templates = CTemplate::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($templates, 'host');
             }
             foreach ($templates as $templateid => $template) {
                 unset($templates[$templateid]['hosts']);
                 foreach ($template['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['parentTemplates'][] =& $templates[$templateid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_templates']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $templates = CTemplate::get($obj_params);
                 $templates = zbx_toHash($templates, 'hostid');
                 foreach ($result as $templateid => $template) {
                     if (isset($templates[$groupid])) {
                         $result[$templateid]['parentTemplates'] = $templates[$templateid]['rowscount'];
                     } else {
                         $result[$templateid]['parentTemplates'] = 0;
                     }
                 }
             }
         }
     }
     // Adding Items
     if (!is_null($options['select_items'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $templateids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_items']) || str_in_array($options['select_items'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_items'];
             $items = CItem::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($items, 'description');
             }
             foreach ($items as $itemid => $item) {
                 unset($items[$itemid]['hosts']);
                 foreach ($item['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['items'][] =& $items[$itemid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_items']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $items = CItem::get($obj_params);
                 $items = zbx_toHash($items, 'hostid');
                 foreach ($result as $templateid => $template) {
                     if (isset($items[$templateid])) {
                         $result[$templateid]['items'] = $items[$templateid]['rowscount'];
                     } else {
                         $result[$templateid]['items'] = 0;
                     }
                 }
             }
         }
     }
     // Adding triggers
     if (!is_null($options['select_triggers'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $templateids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_triggers']) || str_in_array($options['select_triggers'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_triggers'];
             $triggers = CTrigger::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             foreach ($triggers as $triggerid => $trigger) {
                 unset($trigger[$triggerid]['hosts']);
                 foreach ($trigger['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['triggers'][] =& $trigger[$triggerid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_triggers']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $triggers = CTrigger::get($obj_params);
                 $triggers = zbx_toHash($triggers, 'hostid');
                 foreach ($result as $templateid => $template) {
                     if (isset($triggers[$templateid])) {
                         $result[$templateid]['triggers'] = $triggers[$templateid]['rowscount'];
                     } else {
                         $result[$templateid]['triggers'] = 0;
                     }
                 }
             }
         }
     }
     // Adding graphs
     if (!is_null($options['select_graphs'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $templateids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_graphs']) || str_in_array($options['select_graphs'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_graphs'];
             $graphs = CGraph::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             foreach ($graphs as $graphid => $graph) {
                 unset($graph[$graphid]['hosts']);
                 foreach ($graph['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['graphs'][] =& $graph[$graphid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_graphs']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $graphs = CGraph::get($obj_params);
                 $graphs = zbx_toHash($graphs, 'hostid');
                 foreach ($result as $templateid => $template) {
                     if (isset($graphs[$templateid])) {
                         $result[$templateid]['graphs'] = $graphs[$templateid]['rowscount'];
                     } else {
                         $result[$templateid]['graphs'] = 0;
                     }
                 }
             }
         }
     }
     // Adding applications
     if (!is_null($options['select_applications'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $templateids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_applications']) || str_in_array($options['select_applications'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_applications'];
             $applications = CApplication::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($applications, 'name');
             }
             foreach ($applications as $applicationid => $application) {
                 unset($application[$applicationid]['hosts']);
                 foreach ($application['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['applications'][] =& $application[$applicationid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_applications']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $applications = CApplication::get($obj_params);
                 $applications = zbx_toHash($applications, 'hostid');
                 foreach ($result as $templateid => $template) {
                     if (isset($applications[$templateid])) {
                         $result[$templateid]['applications'] = $applications[$templateid]['rowscount'];
                     } else {
                         $result[$templateid]['applications'] = 0;
                     }
                 }
             }
         }
     }
     // Adding macros
     if (!is_null($options['select_macros']) && str_in_array($options['select_macros'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_macros'], 'hostids' => $templateids, 'preservekeys' => 1);
         $macros = CUserMacro::get($obj_params);
         foreach ($macros as $macroid => $macro) {
             $mhosts = $macro['hosts'];
             unset($macro['hosts']);
             foreach ($mhosts as $hnum => $host) {
                 $result[$host['hostid']]['macros'][] = $macro;
             }
         }
     }
     COpt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Пример #11
0
                }
                if (!isset($_REQUEST['plaintext'])) {
                    $table->ShowRow($new_row);
                } else {
                    echo date('Y-m-d H:i:s', $row['clock']);
                    echo "\t" . $row['clock'] . "\t" . htmlspecialchars($row['value']) . "\n";
                }
            }
            if (!isset($_REQUEST['plaintext'])) {
                $table->ShowEnd();
                // to solve memory leak we call 'Show' method by steps
                echo SBR;
            } else {
                echo '</pre></span>';
            }
            COpt::profiling_stop('history');
        }
    }
}
if (!isset($_REQUEST['plaintext'])) {
    if (str_in_array($_REQUEST['action'], array('showvalues', 'showgraph'))) {
        $stime = get_min_itemclock_by_itemid($_REQUEST['itemid']);
        $stime = is_null($stime) ? 0 : $stime;
        $bstime = time() - $effectiveperiod;
        if (isset($_REQUEST['stime'])) {
            $bstime = $_REQUEST['stime'];
            $bstime = mktime(substr($bstime, 8, 2), substr($bstime, 10, 2), 0, substr($bstime, 4, 2), substr($bstime, 6, 2), substr($bstime, 0, 4));
        }
        $script = 'scrollinit(0,' . $effectiveperiod . ',' . $stime . ',0,' . $bstime . '); showgraphmenu("graph");';
        if (isset($dom_graph_id)) {
            $script .= 'graph_zoom_init("' . $dom_graph_id . '",' . $bstime . ',' . $effectiveperiod . ',ZBX_G_WIDTH, 200, true);';
Пример #12
0
 /**
  * Get Triggers data
  *
  * @param _array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['triggerids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['extendoutput']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('triggerid', 'description', 'status', 'priority', 'lastchange');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('triggers' => 't.triggerid'), 'from' => array('t' => 'triggers t'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'triggerids' => null, 'itemids' => null, 'applicationids' => null, 'functions' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'active' => null, 'maintenance' => null, 'withUnacknowledgedEvents' => null, 'withAcknowledgedEvents' => null, 'withLastEventUnacknowledged' => null, 'skipDependent' => null, 'nopermissions' => null, 'editable' => null, 'lastChangeSince' => null, 'lastChangeTill' => null, 'group' => null, 'host' => null, 'only_true' => null, 'min_severity' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'expandData' => null, 'expandDescription' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'select_hosts' => null, 'select_items' => null, 'select_functions' => null, 'select_dependencies' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_hosts'])) {
             $options['select_hosts'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_items'])) {
             $options['select_items'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_dependencies'])) {
             $options['select_dependencies'] = API_OUTPUT_EXTEND;
         }
     }
     if (is_array($options['output'])) {
         unset($sql_parts['select']['triggers']);
         foreach ($options['output'] as $key => $field) {
             $sql_parts['select'][$field] = ' t.' . $field;
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         /*/
         			$sql_parts['where'][] = ' EXISTS(  '.
         						' SELECT tt.triggerid  '.
         						' FROM triggers tt,functions ff,items ii,hosts_groups hgg,rights rr,users_groups ugg '.
         						' WHERE t.triggerid=tt.triggerid  '.
         							' AND ff.triggerid=tt.triggerid  '.
         							' AND ff.itemid=ii.itemid  '.
         							' AND hgg.hostid=ii.hostid  '.
         							' AND rr.id=hgg.groupid  '.
         							' AND rr.groupid=ugg.usrgrpid  '.
         							' AND ugg.userid='.$userid.
         							' AND rr.permission>='.$permission.
         							' AND NOT EXISTS(  '.
         								' SELECT fff.triggerid  '.
         								' FROM functions fff, items iii  '.
         								' WHERE fff.triggerid=tt.triggerid '.
         									' AND fff.itemid=iii.itemid '.		'    '.
         									' AND EXISTS( '.
         										' SELECT hggg.groupid '.
         										' FROM hosts_groups hggg, rights rrr, users_groups uggg '.
         										' WHERE hggg.hostid=iii.hostid '.
         											' AND rrr.id=hggg.groupid '.
         											' AND rrr.groupid=uggg.usrgrpid '.
         											' AND uggg.userid='.$userid.
         											' AND rrr.permission<'.$permission.
         										' ) '.
         								' ) '.
         						' ) ';
         //*/
         //*/
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['rights'] = 'rights r';
         $sql_parts['from']['users_groups'] = 'users_groups ug';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where'][] = 'r.id=hg.groupid ';
         $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
         $sql_parts['where'][] = 'ug.userid=' . $userid;
         $sql_parts['where'][] = 'r.permission>=' . $permission;
         $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT ff.triggerid ' . ' FROM functions ff, items ii ' . ' WHERE ff.triggerid=t.triggerid ' . ' AND ff.itemid=ii.itemid ' . ' AND EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=ii.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . '))';
         //*/
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['groupid'] = DBcondition('hg.groupid', $options['groupids']);
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['hostid'] = 'i.hostid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where']['hostid'] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['i'] = 'i.hostid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         $sql_parts['where']['triggerid'] = DBcondition('t.triggerid', $options['triggerids']);
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['itemid'] = 'f.itemid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['where']['itemid'] = DBcondition('f.itemid', $options['itemids']);
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['applicationid'] = 'a.applicationid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['applications'] = 'applications a';
         $sql_parts['where']['a'] = DBcondition('a.applicationid', $options['applicationids']);
         $sql_parts['where']['ia'] = 'i.hostid=a.hostid';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // functions
     if (!is_null($options['functions'])) {
         zbx_value2array($options['functions']);
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where'][] = DBcondition('f.function', $options['functions'], false, true);
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sql_parts['where']['monitored'] = '' . ' NOT EXISTS (' . ' SELECT ff.functionid' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT ii.itemid' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND (' . ' ii.status<>' . ITEM_STATUS_ACTIVE . ' OR hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )' . ' )';
         $sql_parts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // active
     if (!is_null($options['active'])) {
         $sql_parts['where']['active'] = '' . ' NOT EXISTS (' . ' SELECT ff.functionid' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT ii.itemid' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND  hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )';
         $sql_parts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // maintenance
     if (!is_null($options['maintenance'])) {
         $sql_parts['where'][] = ($options['maintenance'] == 0 ? ' NOT ' : '') . ' EXISTS (' . ' SELECT ff.functionid' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT ii.itemid' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND hh.maintenance_status=1' . ' )' . ' )';
         $sql_parts['where'][] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // lastChangeSince
     if (!is_null($options['lastChangeSince'])) {
         $sql_parts['where']['lastchangesince'] = 't.lastchange>' . $options['lastChangeSince'];
     }
     // lastChangeTill
     if (!is_null($options['lastChangeTill'])) {
         $sql_parts['where']['lastchangetill'] = 't.lastchange<' . $options['lastChangeTill'];
     }
     // withUnacknowledgedEvents
     if (!is_null($options['withUnacknowledgedEvents'])) {
         $sql_parts['where']['unack'] = ' EXISTS(' . ' SELECT e.eventid' . ' FROM events e' . ' WHERE e.objectid=t.triggerid' . ' AND e.object=0' . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=0)';
     }
     // withAcknowledgedEvents
     if (!is_null($options['withAcknowledgedEvents'])) {
         $sql_parts['where']['ack'] = 'NOT EXISTS(' . ' SELECT e.eventid' . ' FROM events e' . ' WHERE e.objectid=t.triggerid' . ' AND e.object=0' . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=0)';
     }
     // templated
     if (!is_null($options['templated'])) {
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sql_parts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sql_parts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sql_parts['where'][] = 't.templateid<>0';
         } else {
             $sql_parts['where'][] = 't.templateid=0';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('triggers t', $options, $sql_parts);
     }
     // --- FILTER ---
     if (is_array($options['filter'])) {
         zbx_db_filter('triggers t', $options, $sql_parts);
         if (isset($options['filter']['host']) && !is_null($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sql_parts['from']['functions'] = 'functions f';
             $sql_parts['from']['items'] = 'items i';
             $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
             $sql_parts['from']['hosts'] = 'hosts h';
             $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
             $sql_parts['where']['host'] = DBcondition('h.host', $options['filter']['host'], false, true);
         }
         if (isset($options['filter']['hostid']) && !is_null($options['filter']['hostid'])) {
             zbx_value2array($options['filter']['hostid']);
             $sql_parts['from']['functions'] = 'functions f';
             $sql_parts['from']['items'] = 'items i';
             $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
             $sql_parts['where']['hostid'] = DBcondition('i.hostid', $options['filter']['hostid']);
         }
     }
     // group
     if (!is_null($options['group'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['name'] = 'g.name';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['groups'] = 'groups g';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where']['ghg'] = 'g.groupid = hg.groupid';
         $sql_parts['where']['group'] = ' UPPER(g.name)=' . zbx_dbstr(zbx_strtoupper($options['group']));
     }
     // host
     if (!is_null($options['host'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['host'] = 'h.host';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['i'] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         $sql_parts['where']['host'] = ' UPPER(h.host)=' . zbx_dbstr(zbx_strtoupper($options['host']));
     }
     // only_true
     if (!is_null($options['only_true'])) {
         $sql_parts['where']['ot'] = '((t.value=' . TRIGGER_VALUE_TRUE . ')' . ' OR ' . '((t.value=' . TRIGGER_VALUE_FALSE . ') AND (t.lastchange>' . (time() - TRIGGER_FALSE_PERIOD) . ')))';
     }
     // min_severity
     if (!is_null($options['min_severity'])) {
         $sql_parts['where'][] = 't.priority>=' . $options['min_severity'];
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['triggers'] = 't.*';
     }
     // expandData
     if (!is_null($options['expandData'])) {
         $sql_parts['select']['host'] = 'h.host';
         $sql_parts['select']['hostid'] = 'h.hostid';
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('COUNT(DISTINCT t.triggerid) as rowscount');
         // groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][] = 't.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('t.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('t.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 't.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //---------------
     $triggerids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('t.triggerid', $nodeids) . $sql_where . $sql_group . $sql_order;
     //SDI($sql);
     $db_res = DBselect($sql, $sql_limit);
     while ($trigger = DBfetch($db_res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $trigger;
             } else {
                 $result = $trigger['rowscount'];
             }
         } else {
             $triggerids[$trigger['triggerid']] = $trigger['triggerid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$trigger['triggerid']] = array('triggerid' => $trigger['triggerid']);
             } else {
                 if (!isset($result[$trigger['triggerid']])) {
                     $result[$trigger['triggerid']] = array();
                 }
                 if (!is_null($options['select_hosts']) && !isset($result[$trigger['triggerid']]['hosts'])) {
                     $result[$trigger['triggerid']]['hosts'] = array();
                 }
                 if (!is_null($options['select_items']) && !isset($result[$trigger['triggerid']]['items'])) {
                     $result[$trigger['triggerid']]['items'] = array();
                 }
                 if (!is_null($options['select_functions']) && !isset($result[$trigger['triggerid']]['functions'])) {
                     $result[$trigger['triggerid']]['functions'] = array();
                 }
                 if (!is_null($options['select_dependencies']) && !isset($result[$trigger['triggerid']]['dependencies'])) {
                     $result[$trigger['triggerid']]['dependencies'] = array();
                 }
                 // groups
                 if (isset($trigger['groupid']) && is_null($options['select_groups'])) {
                     if (!isset($result[$trigger['triggerid']]['groups'])) {
                         $result[$trigger['triggerid']]['groups'] = array();
                     }
                     $result[$trigger['triggerid']]['groups'][] = array('groupid' => $trigger['groupid']);
                     unset($trigger['groupid']);
                 }
                 // hostids
                 if (isset($trigger['hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$trigger['triggerid']]['hosts'])) {
                         $result[$trigger['triggerid']]['hosts'] = array();
                     }
                     $result[$trigger['triggerid']]['hosts'][] = array('hostid' => $trigger['hostid']);
                     if (is_null($options['expandData'])) {
                         unset($trigger['hostid']);
                     }
                 }
                 // itemids
                 if (isset($trigger['itemid']) && is_null($options['select_items'])) {
                     if (!isset($result[$trigger['triggerid']]['items'])) {
                         $result[$trigger['triggerid']]['items'] = array();
                     }
                     $result[$trigger['triggerid']]['items'][] = array('itemid' => $trigger['itemid']);
                     unset($trigger['itemid']);
                 }
                 $result[$trigger['triggerid']] += $trigger;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // skipDependent
     if (!is_null($options['skipDependent'])) {
         $tids = $triggerids;
         $map = array();
         do {
             $sql = 'SELECT d.triggerid_down, d.triggerid_up, t.value ' . ' FROM trigger_depends d, triggers t ' . ' WHERE ' . DBcondition('d.triggerid_down', $tids) . ' AND d.triggerid_up=t.triggerid';
             $db_result = DBselect($sql);
             $tids = array();
             while ($row = DBfetch($db_result)) {
                 if (TRIGGER_VALUE_TRUE == $row['value']) {
                     if (isset($map[$row['triggerid_down']])) {
                         foreach ($map[$row['triggerid_down']] as $triggerid => $state) {
                             unset($result[$triggerid]);
                             unset($triggerids[$triggerid]);
                         }
                     } else {
                         unset($result[$row['triggerid_down']]);
                         unset($triggerids[$row['triggerid_down']]);
                     }
                 } else {
                     if (isset($map[$row['triggerid_down']])) {
                         if (!isset($map[$row['triggerid_up']])) {
                             $map[$row['triggerid_up']] = array();
                         }
                         $map[$row['triggerid_up']] += $map[$row['triggerid_down']];
                     } else {
                         if (!isset($map[$row['triggerid_up']])) {
                             $map[$row['triggerid_up']] = array();
                         }
                         $map[$row['triggerid_up']][$row['triggerid_down']] = 1;
                     }
                     $tids[] = $row['triggerid_up'];
                 }
             }
         } while (!empty($tids));
     }
     // withLastEventUnacknowledged
     if (!is_null($options['withLastEventUnacknowledged'])) {
         $eventids = array();
         $sql = 'SELECT max(e.eventid) as eventid, e.objectid' . ' FROM events e ' . ' WHERE e.object=' . EVENT_OBJECT_TRIGGER . ' AND ' . DBcondition('e.objectid', $triggerids) . ' AND ' . DBcondition('e.value', array(TRIGGER_VALUE_TRUE)) . ' GROUP BY e.objectid';
         $events_db = DBselect($sql);
         while ($event = DBfetch($events_db)) {
             $eventids[] = $event['eventid'];
         }
         $correct_triggerids = array();
         $sql = 'SELECT e.objectid' . ' FROM events e ' . ' WHERE ' . DBcondition('e.eventid', $eventids) . ' AND e.acknowledged=0';
         $triggers_db = DBselect($sql);
         while ($trigger = DBfetch($triggers_db)) {
             $correct_triggerids[$trigger['objectid']] = $trigger['objectid'];
         }
         foreach ($result as $triggerid => $trigger) {
             if (!isset($correct_triggerids[$triggerid])) {
                 unset($result[$triggerid]);
                 unset($triggerids[$triggerid]);
             }
         }
     }
     // Adding Objects
     // Adding trigger dependencies
     if (!is_null($options['select_dependencies']) && str_in_array($options['select_dependencies'], $subselects_allowed_outputs)) {
         $deps = array();
         $depids = array();
         $sql = 'SELECT triggerid_up, triggerid_down ' . ' FROM trigger_depends ' . ' WHERE ' . DBcondition('triggerid_down', $triggerids);
         $db_deps = DBselect($sql);
         while ($db_dep = DBfetch($db_deps)) {
             if (!isset($deps[$db_dep['triggerid_down']])) {
                 $deps[$db_dep['triggerid_down']] = array();
             }
             $deps[$db_dep['triggerid_down']][$db_dep['triggerid_up']] = $db_dep['triggerid_up'];
             $depids[] = $db_dep['triggerid_up'];
         }
         $obj_params = array('triggerids' => $depids, 'output' => $options['select_dependencies'], 'expandData' => 1, 'preservekeys' => 1);
         $allowed = self::get($obj_params);
         //allowed triggerids
         foreach ($deps as $triggerid => $deptriggers) {
             foreach ($deptriggers as $num => $deptriggerid) {
                 if (isset($allowed[$deptriggerid])) {
                     $result[$triggerid]['dependencies'][] = $allowed[$deptriggerid];
                 }
             }
         }
     }
     // Adding groups
     if (!is_null($options['select_groups']) && str_in_array($options['select_groups'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_groups'], 'triggerids' => $triggerids, 'preservekeys' => 1);
         $groups = CHostgroup::get($obj_params);
         foreach ($groups as $groupid => $group) {
             $gtriggers = $group['triggers'];
             unset($group['triggers']);
             foreach ($gtriggers as $num => $trigger) {
                 $result[$trigger['triggerid']]['groups'][] = $group;
             }
         }
     }
     // Adding hosts
     if (!is_null($options['select_hosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'triggerids' => $triggerids, 'templated_hosts' => 1, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_hosts']) || str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_hosts'];
             $hosts = CHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'host');
             }
             foreach ($hosts as $hostid => $host) {
                 unset($hosts[$hostid]['triggers']);
                 $count = array();
                 foreach ($host['triggers'] as $tnum => $trigger) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$trigger['triggerid']])) {
                             $count[$trigger['triggerid']] = 0;
                         }
                         $count[$trigger['triggerid']]++;
                         if ($count[$trigger['triggerid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$trigger['triggerid']]['hosts'][] =& $hosts[$hostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_hosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $hosts = CHost::get($obj_params);
                 $hosts = zbx_toHash($hosts, 'hostid');
                 foreach ($result as $triggerid => $trigger) {
                     if (isset($hosts[$triggerid])) {
                         $result[$triggerid]['hosts'] = $hosts[$triggerid]['rowscount'];
                     } else {
                         $result[$triggerid]['hosts'] = 0;
                     }
                 }
             }
         }
     }
     // Adding Functions
     if (!is_null($options['select_functions']) && str_in_array($options['select_functions'], $subselects_allowed_outputs)) {
         if ($options['select_functions'] == API_OUTPUT_EXTEND) {
             $sql_select = 'f.*';
         } else {
             $sql_select = 'f.functionid, f.triggerid';
         }
         $sql = 'SELECT ' . $sql_select . ' FROM functions f ' . ' WHERE ' . DBcondition('f.triggerid', $triggerids);
         $res = DBselect($sql);
         while ($function = DBfetch($res)) {
             $triggerid = $function['triggerid'];
             unset($function['triggerid']);
             $result[$triggerid]['functions'][] = $function;
         }
     }
     // Adding Items
     if (!is_null($options['select_items']) && str_in_array($options['select_items'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_items'], 'triggerids' => $triggerids, 'webitems' => 1, 'nopermissions' => 1, 'preservekeys' => 1);
         $items = CItem::get($obj_params);
         foreach ($items as $itemid => $item) {
             $itriggers = $item['triggers'];
             unset($item['triggers']);
             foreach ($itriggers as $num => $trigger) {
                 $result[$trigger['triggerid']]['items'][] = $item;
             }
         }
     }
     // expandDescription
     if (!is_null($options['expandDescription'])) {
         // Function compare values {{{
         foreach ($result as $tnum => $trigger) {
             preg_match_all('/\\$([1-9])/u', $trigger['description'], $numbers);
             preg_match_all('~{[0-9]+}[+\\-\\*/<>=#]?[\\(]*(?P<val>[+\\-0-9]+)[\\)]*~u', $trigger['expression'], $matches);
             foreach ($numbers[1] as $i) {
                 $rep = isset($matches['val'][$i - 1]) ? $matches['val'][$i - 1] : '';
                 $result[$tnum]['description'] = str_replace('$' . $i, $rep, $result[$tnum]['description']);
             }
         }
         // }}}
         $functionids = array();
         $triggers_to_expand_hosts = array();
         $triggers_to_expand_items = array();
         $triggers_to_expand_items2 = array();
         foreach ($result as $tnum => $trigger) {
             preg_match_all('/{HOSTNAME([1-9]?)}/u', $trigger['description'], $hnums);
             if (!empty($hnums[1])) {
                 preg_match_all('/{([0-9]+)}/u', $trigger['expression'], $funcs);
                 $funcs = $funcs[1];
                 foreach ($hnums[1] as $fnum) {
                     $fnum = $fnum ? $fnum : 1;
                     if (isset($funcs[$fnum - 1])) {
                         $functionid = $funcs[$fnum - 1];
                         $functionids[$functionid] = $functionid;
                         $triggers_to_expand_hosts[$trigger['triggerid']][$functionid] = $fnum;
                     }
                 }
             }
             preg_match_all('/{ITEM.LASTVALUE([1-9]?)}/u', $trigger['description'], $inums);
             if (!empty($inums[1])) {
                 preg_match_all('/{([0-9]+)}/u', $trigger['expression'], $funcs);
                 $funcs = $funcs[1];
                 foreach ($inums[1] as $fnum) {
                     $fnum = $fnum ? $fnum : 1;
                     if (isset($funcs[$fnum - 1])) {
                         $functionid = $funcs[$fnum - 1];
                         $functionids[$functionid] = $functionid;
                         $triggers_to_expand_items[$trigger['triggerid']][$functionid] = $fnum;
                     }
                 }
             }
             preg_match_all('/{ITEM.VALUE([1-9]?)}/u', $trigger['description'], $inums);
             if (!empty($inums[1])) {
                 preg_match_all('/{([0-9]+)}/u', $trigger['expression'], $funcs);
                 $funcs = $funcs[1];
                 foreach ($inums[1] as $fnum) {
                     $fnum = $fnum ? $fnum : 1;
                     if (isset($funcs[$fnum - 1])) {
                         $functionid = $funcs[$fnum - 1];
                         $functionids[$functionid] = $functionid;
                         $triggers_to_expand_items2[$trigger['triggerid']][$functionid] = $fnum;
                     }
                 }
             }
         }
         if (!empty($functionids)) {
             $sql = 'SELECT DISTINCT f.triggerid, f.functionid, h.host, i.lastvalue' . ' FROM functions f,items i,hosts h' . ' WHERE f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND h.status<>' . HOST_STATUS_TEMPLATE . ' AND ' . DBcondition('f.functionid', $functionids);
             $db_funcs = DBselect($sql);
             while ($func = DBfetch($db_funcs)) {
                 if (isset($triggers_to_expand_hosts[$func['triggerid']][$func['functionid']])) {
                     $fnum = $triggers_to_expand_hosts[$func['triggerid']][$func['functionid']];
                     if ($fnum == 1) {
                         $result[$func['triggerid']]['description'] = str_replace('{HOSTNAME}', $func['host'], $result[$func['triggerid']]['description']);
                     }
                     $result[$func['triggerid']]['description'] = str_replace('{HOSTNAME' . $fnum . '}', $func['host'], $result[$func['triggerid']]['description']);
                 }
                 if (isset($triggers_to_expand_items[$func['triggerid']][$func['functionid']])) {
                     $fnum = $triggers_to_expand_items[$func['triggerid']][$func['functionid']];
                     if ($fnum == 1) {
                         $result[$func['triggerid']]['description'] = str_replace('{ITEM.LASTVALUE}', $func['lastvalue'], $result[$func['triggerid']]['description']);
                     }
                     $result[$func['triggerid']]['description'] = str_replace('{ITEM.LASTVALUE' . $fnum . '}', $func['lastvalue'], $result[$func['triggerid']]['description']);
                 }
                 if (isset($triggers_to_expand_items2[$func['triggerid']][$func['functionid']])) {
                     $fnum = $triggers_to_expand_items2[$func['triggerid']][$func['functionid']];
                     if ($fnum == 1) {
                         $result[$func['triggerid']]['description'] = str_replace('{ITEM.VALUE}', $func['lastvalue'], $result[$func['triggerid']]['description']);
                     }
                     $result[$func['triggerid']]['description'] = str_replace('{ITEM.VALUE' . $fnum . '}', $func['lastvalue'], $result[$func['triggerid']]['description']);
                 }
             }
         }
         foreach ($result as $tnum => $trigger) {
             if ($res = preg_match_all('/' . ZBX_PREG_EXPRESSION_USER_MACROS . '/', $trigger['description'], $arr)) {
                 $macros = CUserMacro::getMacros($arr[1], array('triggerid' => $trigger['triggerid']));
                 $search = array_keys($macros);
                 $values = array_values($macros);
                 $result[$tnum]['description'] = str_replace($search, $values, $trigger['description']);
             }
         }
     }
     COpt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Пример #13
0
function DBexecute($query, $skip_error_messages = 0)
{
    global $DB;
    $result = false;
    $time_start = microtime(true);
    if (isset($DB['DB']) && !empty($DB['DB'])) {
        $DB['EXECUTE_COUNT']++;
        //SDI('SQL xec: '.$query);
        switch ($DB['TYPE']) {
            case 'MYSQL':
                $result = mysql_query($query, $DB['DB']);
                if (!$result) {
                    error('Error in query [' . $query . '] [' . mysql_error() . ']');
                }
                break;
            case 'POSTGRESQL':
                $result = (bool) pg_query($DB['DB'], $query);
                if (!$result) {
                    error('Error in query [' . $query . '] [' . pg_last_error() . ']');
                }
                break;
            case 'ORACLE':
                $result = OCIParse($DB['DB'], $query);
                if (!$result) {
                    $e = @ocierror();
                    error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']');
                } else {
                    if (!@OCIExecute($result, $DB['TRANSACTIONS'] ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS)) {
                        $e = ocierror($result);
                        error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']');
                    } else {
                        /* It should be here. The function must return boolean */
                        $result = true;
                    }
                }
                break;
            case 'IBM_DB2':
                $options = array();
                if (!($result = db2_prepare($DB['DB'], $query))) {
                    $e = @db2_stmt_errormsg($result);
                    error('SQL error [' . $query . '] in [' . $e . ']');
                } else {
                    if (true !== @db2_execute($result)) {
                        $e = @db2_stmt_errormsg($result);
                        error('SQL error [' . $query . '] in [' . $e . ']');
                    } else {
                        /* It should be here. The function must return boolean */
                        $result = true;
                    }
                }
                break;
            case 'SQLITE3':
                if (!$DB['TRANSACTIONS']) {
                    lock_db_access();
                }
                $result = sqlite3_exec($DB['DB'], $query);
                if (!$result) {
                    error('Error in query [' . $query . '] [' . sqlite3_error($DB['DB']) . ']');
                }
                if (!$DB['TRANSACTIONS']) {
                    unlock_db_access();
                }
                break;
        }
        if ($DB['TRANSACTIONS'] && !$result) {
            $DB['TRANSACTION_STATE'] &= $result;
        }
    }
    COpt::savesqlrequest(microtime(true) - $time_start, $query);
    return (bool) $result;
}
Пример #14
0
 public static function profiling_stop($type = NULL)
 {
     global $starttime;
     global $memorystamp;
     global $sqlrequests;
     global $sqlmark;
     global $perf_counter;
     global $var_list;
     global $USER_DETAILS;
     global $DB;
     if (isset($USER_DETAILS['debug_mode']) && $USER_DETAILS['debug_mode'] == GROUP_DEBUG_MODE_DISABLED) {
         return;
     }
     $endtime = COpt::getmicrotime();
     $memory = COpt::getmemoryusage();
     if (is_null($type)) {
         $type = 'global';
     }
     $debug_str = '';
     $debug_str .= '<a name="debug"></a>';
     $debug_str .= "******************* Stats for {$type} *************************" . OBR;
     if (defined('USE_TIME_PROF')) {
         $time = $endtime - $starttime[$type];
         if ($time < TOTAL_TIME) {
             $debug_str .= 'Total time: ' . round($time, 6) . OBR;
         } else {
             $debug_str .= '<b>Total time: ' . round($time, 6) . '</b>' . OBR;
         }
     }
     if (defined('USE_MEM_PROF')) {
         $debug_str .= 'Memory limit	 : ' . ini_get('memory_limit') . OBR;
         $debug_str .= 'Memory usage	 : ' . mem2str($memorystamp[$type]) . ' - ' . mem2str($memory) . OBR;
         $debug_str .= 'Memory leak	 : ' . mem2str($memory - $memorystamp[$type]) . OBR;
     }
     if (defined('USE_VAR_MON')) {
         $curr_var_list = isset($GLOBALS) ? array_keys($GLOBALS) : array();
         $var_diff = array_diff($curr_var_list, $var_list[$type]);
         $debug_str .= ' Undeleted vars : ' . count($var_diff) . ' [';
         print_r(implode(', ', $var_diff));
         $debug_str .= ']' . OBR;
     }
     if (defined('USE_COUNTER_PROF')) {
         if (isset($perf_counter[$type])) {
             ksort($perf_counter[$type]);
             foreach ($perf_counter[$type] as $name => $value) {
                 $debug_str .= 'Counter "' . $name . '" : ' . $value . OBR;
             }
         }
     }
     if (defined('USE_SQLREQUEST_PROF')) {
         if (defined('SHOW_SQLREQUEST_DETAILS')) {
             $requests_cnt = count($sqlrequests);
             $debug_str .= 'SQL selects count: ' . $DB['SELECT_COUNT'] . OBR;
             $debug_str .= 'SQL executes count: ' . $DB['EXECUTE_COUNT'] . OBR;
             $debug_str .= 'SQL requests count: ' . ($requests_cnt - $sqlmark[$type]) . OBR;
             $sql_time = 0;
             for ($i = $sqlmark[$type]; $i < $requests_cnt; $i++) {
                 $time = $sqlrequests[$i][0];
                 $sql_time += $time;
                 if ($time < LONG_QUERY) {
                     $debug_str .= 'Time:' . round($time, 8) . ' SQL:&nbsp;' . $sqlrequests[$i][1] . OBR;
                 } else {
                     $debug_str .= '<b>Time:' . round($time, 8) . ' LONG SQL:&nbsp;' . $sqlrequests[$i][1] . '</b>' . OBR;
                 }
             }
         } else {
             $debug_str .= 'SQL requests count: ' . ($sqlrequests - $sqlmark[$type]) . OBR;
         }
         if ($sql_time < QUERY_TOTAL_TIME) {
             $debug_str .= 'Total time spent on SQL: ' . round($sql_time, 8) . OBR;
         } else {
             $debug_str .= '<b>Total time spent on SQL: ' . round($sql_time, 8) . '</b>' . OBR;
         }
     }
     $debug_str .= "******************** End of {$type} ***************************" . OBR;
     // DEBUG of ZBX FrontEnd
     $zbx_debug = new CWidget('debug_hat', new CSpan(new CScript($debug_str), 'textcolorstyles'));
     $zbx_debug->addHeader(S_DEBUG);
     $debud = new CDiv(array(BR(), $zbx_debug));
     $debud->setAttribute('id', 'zbx_gebug_info');
     $debud->setAttribute('style', 'display: none;');
     $debud->show();
     //----------------
 }
Пример #15
0
 /**
  * Get HostGroups
  *
  * @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
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('groups' => 'g.groupid'), 'from' => array('groups' => 'groups g'), 'where' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'templateids' => null, 'graphids' => null, 'triggerids' => null, 'maintenanceids' => null, 'monitored_hosts' => null, 'templated_hosts' => null, 'real_hosts' => null, 'not_proxy_hosts' => null, 'with_items' => null, 'with_monitored_items' => null, 'with_historical_items' => null, 'with_triggers' => null, 'with_monitored_triggers' => null, 'with_httptests' => null, 'with_monitored_httptests' => null, 'with_graphs' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_hosts' => null, 'select_templates' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($def_options, $params);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_hosts'])) {
             $options['select_hosts'] = API_OUTPUT_EXTEND;
         }
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql_parts['from']['rights'] = 'rights r';
         $sql_parts['from']['users_groups'] = '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 = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         $sql_parts['where']['groupid'] = DBcondition('g.groupid', $options['groupids']);
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['hostid'] = 'hg.hostid';
         }
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where'][] = DBcondition('hg.hostid', $options['hostids']);
         $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['triggerid'] = 'f.triggerid';
         }
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('f.triggerid', $options['triggerids']);
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
         /*
         			if(!$nodeCheck){
         				$nodeCheck = true;
         				$sql_parts['where'][] = DBin_node('f.triggerid', $nodeids);
         			}
         //*/
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['graphid'] = 'gi.graphid';
         }
         $sql_parts['from']['gi'] = 'graphs_items gi';
         $sql_parts['from']['i'] = 'items i';
         $sql_parts['from']['hg'] = 'hosts_groups hg';
         $sql_parts['where'][] = DBcondition('gi.graphid', $options['graphids']);
         $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
         $sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
     }
     // maintenanceids
     if (!is_null($options['maintenanceids'])) {
         zbx_value2array($options['maintenanceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['maintenanceid'] = 'mg.maintenanceid';
         }
         $sql_parts['from']['maintenances_groups'] = 'maintenances_groups mg';
         $sql_parts['where'][] = DBcondition('mg.maintenanceid', $options['maintenanceids']);
         $sql_parts['where']['hmh'] = 'g.groupid=mg.groupid';
     }
     // monitored_hosts, real_hosts, templated_hosts, not_proxy_hosts
     if (!is_null($options['monitored_hosts'])) {
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['hosts'] = '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 (!is_null($options['real_hosts'])) {
             $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
             $sql_parts['from']['hosts'] = '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 (!is_null($options['templated_hosts'])) {
                 $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
                 $sql_parts['from']['hosts'] = '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 (!is_null($options['not_proxy_hosts'])) {
                     $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
                     $sql_parts['from']['hosts'] = 'hosts h';
                     $sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
                     $sql_parts['where'][] = 'h.hostid=hg.hostid';
                     $sql_parts['where'][] = 'NOT h.status IN (' . HOST_STATUS_PROXY_ACTIVE . ',' . HOST_STATUS_PROXY_PASSIVE . ')';
                 }
             }
         }
     }
     // with_items, with_monitored_items, with_historical_items
     if (!is_null($options['with_items'])) {
         $sql_parts['from']['hosts_groups'] = '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 (!is_null($options['with_monitored_items'])) {
             $sql_parts['from']['hosts_groups'] = '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 (!is_null($options['with_historical_items'])) {
                 $sql_parts['from']['hosts_groups'] = '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 (!is_null($options['with_triggers'])) {
         $sql_parts['from']['hosts_groups'] = '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 (!is_null($options['with_monitored_triggers'])) {
             $sql_parts['from']['hosts_groups'] = '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 (!is_null($options['with_httptests'])) {
         $sql_parts['from']['hosts_groups'] = '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 (!is_null($options['with_monitored_httptests'])) {
             $sql_parts['from']['hosts_groups'] = '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 (!is_null($options['with_graphs'])) {
         $sql_parts['from']['hosts_groups'] = '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)';
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['groups'] = 'g.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('COUNT(DISTINCT g.groupid) 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('groups g', $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('groups g', $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'][] = 'g.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('g.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('g.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 'g.' . $options['sortfield'];
         }
     }
     // 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 ' . zbx_db_distinct($sql_parts) . ' ' . $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 (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $group;
             } else {
                 $result = $group['rowscount'];
             }
         } else {
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$group['groupid']] = array('groupid' => $group['groupid']);
             } else {
                 $groupids[$group['groupid']] = $group['groupid'];
                 if (!isset($result[$group['groupid']])) {
                     $result[$group['groupid']] = array();
                 }
                 if (!is_null($options['select_templates']) && !isset($result[$group['groupid']]['templates'])) {
                     $result[$group['groupid']]['templates'] = array();
                 }
                 if (!is_null($options['select_hosts']) && !isset($result[$group['groupid']]['hosts'])) {
                     $result[$group['groupid']]['hosts'] = array();
                 }
                 // hostids
                 if (isset($group['hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$group['groupid']]['hosts'])) {
                         $result[$group['groupid']]['hosts'] = array();
                     }
                     $result[$group['groupid']]['hosts'][] = array('hostid' => $group['hostid']);
                     unset($group['hostid']);
                 }
                 //graphids
                 if (isset($group['graphid'])) {
                     if (!isset($result[$group['groupid']]['graphs'])) {
                         $result[$group['groupid']]['graphs'] = array();
                     }
                     $result[$group['groupid']]['graphs'][] = array('graphid' => $group['graphid']);
                     unset($group['hostid']);
                 }
                 // maintenanceids
                 if (isset($group['maintenanceid'])) {
                     if (!isset($result[$group['groupid']]['maintenanceid'])) {
                         $result[$group['groupid']]['maintenances'] = array();
                     }
                     $result[$group['groupid']]['maintenances'][] = array('maintenanceid' => $group['maintenanceid']);
                     unset($group['maintenanceid']);
                 }
                 // triggerids
                 if (isset($group['triggerid'])) {
                     if (!isset($result[$group['groupid']]['triggers'])) {
                         $result[$group['groupid']]['triggers'] = array();
                     }
                     $result[$group['groupid']]['triggers'][] = array('triggerid' => $group['triggerid']);
                     unset($group['triggerid']);
                 }
                 $result[$group['groupid']] += $group;
             }
         }
     }
     COpt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding hosts
     if (!is_null($options['select_hosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'groupids' => $groupids, 'preservekeys' => 1);
         if (is_array($options['select_hosts']) || str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_hosts'];
             $hosts = CHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'host');
             }
             $count = array();
             foreach ($hosts as $hostid => $host) {
                 $hgroups = $host['groups'];
                 unset($host['groups']);
                 foreach ($hgroups as $num => $group) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$group['groupid']])) {
                             $count[$group['groupid']] = 0;
                         }
                         $count[$group['groupid']]++;
                         if ($count[$group['groupid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$group['groupid']]['hosts'][] = $hosts[$hostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_hosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $hosts = CHost::get($obj_params);
                 $hosts = zbx_toHash($hosts, 'groupid');
                 foreach ($result as $groupid => $group) {
                     if (isset($hosts[$groupid])) {
                         $result[$groupid]['hosts'] = $hosts[$groupid]['rowscount'];
                     } else {
                         $result[$groupid]['hosts'] = 0;
                     }
                 }
             }
         }
     }
     // Adding templates
     if (!is_null($options['select_templates'])) {
         $obj_params = array('nodeids' => $nodeids, 'groupids' => $groupids, 'preservekeys' => 1);
         if (is_array($options['select_templates']) || str_in_array($options['select_templates'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_templates'];
             $templates = CTemplate::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($templates, 'host');
             }
             $count = array();
             foreach ($templates as $templateid => $template) {
                 $hgroups = $template['groups'];
                 unset($template['groups']);
                 foreach ($hgroups as $num => $group) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$group['groupid']])) {
                             $count[$group['groupid']] = 0;
                         }
                         $count[$group['groupid']]++;
                         if ($count[$group['groupid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$group['groupid']]['templates'][] = $templates[$templateid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_templates']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $templates = CTemplate::get($obj_params);
                 $templates = zbx_toHash($templates, 'groupid');
                 foreach ($result as $groupid => $group) {
                     if (isset($templates[$groupid])) {
                         $result[$groupid]['templates'] = $templates[$groupid]['rowscount'];
                     } else {
                         $result[$groupid]['templates'] = 0;
                     }
                 }
             }
         }
     }
     COpt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Пример #16
0
function get_items_data_overview($hostids, $view_style = null)
{
    if (is_null($view_style)) {
        $view_style = get_profile('web.overview.view.style', STYLE_TOP);
    }
    $table = new CTableInfo(S_NO_ITEMS_DEFINED);
    COpt::profiling_start('prepare data');
    $result = DBselect('SELECT DISTINCT h.hostid, h.host,i.itemid, i.key_, i.value_type, i.lastvalue, i.units, ' . ' i.description, t.priority, i.valuemapid, t.value as tr_value, t.triggerid ' . ' FROM hosts h, items i ' . ' LEFT JOIN functions f on f.itemid=i.itemid ' . ' LEFT JOIN triggers t on t.triggerid=f.triggerid ' . ' WHERE ' . DBcondition('h.hostid', $hostids) . ' AND h.status=' . HOST_STATUS_MONITORED . ' AND h.hostid=i.hostid ' . ' AND i.status=' . ITEM_STATUS_ACTIVE . ' ORDER BY i.description,i.itemid');
    unset($items);
    unset($hosts);
    // get rid of warnings about $triggers undefined
    $items = array();
    while ($row = DBfetch($result)) {
        $descr = item_description($row);
        $row['host'] = get_node_name_by_elid($row['hostid']) . $row['host'];
        $hosts[strtolower($row['host'])] = $row['host'];
        // A little tricky check for attempt to overwrite active trigger (value=1) with
        // inactive or active trigger with lower priority.
        if (!isset($items[$descr][$row['host']]) || ($items[$descr][$row['host']]['tr_value'] == TRIGGER_VALUE_FALSE && $row['tr_value'] == TRIGGER_VALUE_TRUE || ($items[$descr][$row['host']]['tr_value'] == TRIGGER_VALUE_FALSE || $row['tr_value'] == TRIGGER_VALUE_TRUE) && $row['priority'] > $items[$descr][$row['host']]['severity'])) {
            $items[$descr][$row['host']] = array('itemid' => $row['itemid'], 'value_type' => $row['value_type'], 'lastvalue' => $row['lastvalue'], 'units' => $row['units'], 'description' => $row['description'], 'valuemapid' => $row['valuemapid'], 'severity' => $row['priority'], 'tr_value' => $row['tr_value'], 'triggerid' => $row['triggerid']);
        }
    }
    if (!isset($hosts)) {
        return $table;
    }
    ksort($hosts);
    COpt::profiling_stop('prepare data');
    COpt::profiling_start('prepare table');
    if ($view_style == STYLE_TOP) {
        $header = array(new CCol(S_ITEMS, 'center'));
        foreach ($hosts as $hostname) {
            $header = array_merge($header, array(new CImg('vtext.php?text=' . $hostname)));
        }
        $table->SetHeader($header, 'vertical_header');
        $curr_rime = time();
        foreach ($items as $descr => $ithosts) {
            $table_row = array(nbsp($descr));
            foreach ($hosts as $hostname) {
                $table_row = get_item_data_overview_cells($table_row, $ithosts, $hostname);
            }
            $table->AddRow($table_row);
        }
    } else {
        $header = array(new CCol(S_HOSTS, 'center'));
        foreach ($items as $descr => $ithosts) {
            $header = array_merge($header, array(new CImg('vtext.php?text=' . $descr)));
        }
        $table->SetHeader($header, 'vertical_header');
        $curr_rime = time();
        foreach ($hosts as $hostname) {
            $table_row = array(nbsp($hostname));
            foreach ($items as $descr => $ithosts) {
                $table_row = get_item_data_overview_cells($table_row, $ithosts, $hostname);
            }
            $table->AddRow($table_row);
        }
    }
    COpt::profiling_stop('prepare table');
    return $table;
}
Пример #17
0
function get_accessible_groups_by_user($user_data, $perm, $perm_res = null, $nodeid = null)
{
    global $ZBX_LOCALNODEID;
    if (is_null($perm_res)) {
        $perm_res = PERM_RES_IDS_ARRAY;
    }
    if (is_null($nodeid)) {
        $nodeid = get_current_nodeid();
    }
    $result = array();
    $userid =& $user_data['userid'];
    if (!isset($userid)) {
        fatal_error('Incorrect user data in "get_accessible_groups_by_user"');
    }
    $user_type =& $user_data['type'];
    COpt::counter_up('perm_group[' . $userid . ',' . $perm . ',' . $perm_res . ',' . $nodeid . ']');
    COpt::counter_up('perm');
    $processed = array();
    $where = array();
    if (!is_null($nodeid)) {
        array_push($where, DBin_node('hg.groupid', $nodeid));
    }
    $where = count($where) ? ' WHERE ' . implode(' AND ', $where) : '';
    $sql = 'SELECT n.nodeid as nodeid,n.name as node_name,hg.groupid,hg.name,min(r.permission) as permission,g.userid' . ' FROM groups hg ' . ' LEFT JOIN rights r ON r.id=hg.groupid ' . ' LEFT JOIN users_groups g ON r.groupid=g.usrgrpid AND g.userid=' . $userid . ' LEFT JOIN nodes n ON ' . DBid2nodeid('hg.groupid') . '=n.nodeid ' . $where . ' GROUP BY n.nodeid, n.name, hg.groupid, hg.name, g.userid, g.userid ' . ' ORDER BY node_name, hg.name, permission ';
    $db_groups = DBselect($sql);
    while ($group_data = DBfetch($db_groups)) {
        if (zbx_empty($group_data['nodeid'])) {
            $group_data['nodeid'] = id2nodeid($group_data['groupid']);
        }
        /* deny if no rights defined */
        if (USER_TYPE_SUPER_ADMIN == $user_type) {
            $group_data['permission'] = PERM_MAX;
        } else {
            if (zbx_empty($group_data['permission']) || zbx_empty($group_data['userid'])) {
                continue;
            }
            if (isset($processed[$group_data['groupid']])) {
                if (PERM_DENY == $group_data['permission']) {
                    unset($result[$group_data['groupid']]);
                } else {
                    if ($processed[$group_data['groupid']] > $group_data['permission']) {
                        unset($processed[$group_data['groupid']]);
                    } else {
                        continue;
                    }
                }
            }
        }
        $processed[$group_data['groupid']] = $group_data['permission'];
        if ($group_data['permission'] < $perm) {
            continue;
        }
        switch ($perm_res) {
            case PERM_RES_DATA_ARRAY:
                $result[$group_data['groupid']] = $group_data;
                break;
            default:
                $result[$group_data['groupid']] = $group_data["groupid"];
                break;
        }
    }
    unset($processed, $group_data, $db_groups);
    if ($perm_res == PERM_RES_STRING_LINE) {
        if (count($result) == 0) {
            $result = '-1';
        } else {
            $result = implode(',', $result);
        }
    }
    return $result;
}
Пример #18
0
 function profiling_stop($type = NULL)
 {
     global $starttime;
     global $memorystamp;
     global $sqlrequests;
     global $sqlmark;
     global $perf_counter;
     global $var_list;
     $endtime = COpt::getmicrotime();
     $memory = COpt::getmemoryusage();
     if (is_null($type)) {
         $type = 'global';
     }
     echo OBR;
     if (defined('USE_TIME_PROF')) {
         echo '(' . $type . ') Time to execute: ' . round($endtime - $starttime[$type], 6) . ' seconds!' . OBR;
     }
     if (defined('USE_MEM_PROF')) {
         echo '(' . $type . ') Memory limit	 : ' . ini_get('memory_limit') . OBR;
         echo '(' . $type . ') Memory usage	 : ' . mem2str($memorystamp[$type]) . ' - ' . mem2str($memory) . OBR;
         echo '(' . $type . ') Memory leak	 : ' . mem2str($memory - $memorystamp[$type]) . OBR;
     }
     if (defined('USE_VAR_MON')) {
         $curr_var_list = isset($GLOBALS) ? array_keys($GLOBALS) : array();
         $var_diff = array_diff($curr_var_list, $var_list[$type]);
         echo '(' . $type . ') Undeleted vars : ' . count($var_diff) . ' [';
         print_r(implode(', ', $var_diff));
         echo ']' . OBR;
     }
     if (defined('USE_COUNTER_PROF')) {
         if (isset($perf_counter[$type])) {
             ksort($perf_counter[$type]);
             foreach ($perf_counter[$type] as $name => $value) {
                 echo '(' . $type . ') Counter "' . $name . '" : ' . $value . OBR;
             }
         }
     }
     if (defined('USE_SQLREQUEST_PROF')) {
         if (defined('SHOW_SQLREQUEST_DETAILS')) {
             $requests_cnt = count($sqlrequests);
             echo '(' . $type . ') SQL requests count: ' . ($requests_cnt - $sqlmark[$type]) . OBR;
             for ($i = $sqlmark[$type]; $i < $requests_cnt; $i++) {
                 echo '(' . $type . ') SQL request    : ' . $sqlrequests[$i] . OBR;
             }
         } else {
             echo '(' . $type . ') SQL requests count: ' . ($sqlrequests - $sqlmark[$type]) . OBR;
         }
     }
 }
Пример #19
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;
 }
Пример #20
0
 /**
  * Get Actions data
  *
  * @param _array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['actionids']
  * @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('actionid', 'name');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('actions' => 'a.actionid'), 'from' => array('actions' => 'actions a'), 'where' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'actionids' => null, 'triggerids' => null, 'mediatypeids' => null, 'userids' => null, 'nopermissions' => null, 'editable' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'extendoutput' => null, 'output' => API_OUTPUT_REFER, 'select_conditions' => null, 'select_operations' => 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_conditions'])) {
             $options['select_conditions'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_operations'])) {
             $options['select_operations'] = API_OUTPUT_EXTEND;
         }
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || !is_null($options['nopermissions'])) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql_parts['from']['conditions'] = 'conditions c';
         $sql_parts['where']['ac'] = 'a.actionid=c.actionid';
         // condition hostgroup
         $sql_parts['where'][] = ' NOT EXISTS(' . ' SELECT cc.conditionid' . ' FROM conditions cc' . ' WHERE cc.conditiontype=' . CONDITION_TYPE_HOST_GROUP . ' AND cc.actionid=c.actionid' . ' AND (' . ' NOT EXISTS(' . ' SELECT rr.id' . ' FROM rights rr, users_groups ug' . ' WHERE rr.id=' . zbx_dbcast_2bigint('cc.value') . ' AND rr.groupid=ug.usrgrpid' . ' AND ug.userid=' . $userid . ' AND rr.permission>=' . $permission . ' )' . ' OR EXISTS(' . ' SELECT rr.id' . ' FROM rights rr, users_groups ugg' . ' WHERE rr.id=' . zbx_dbcast_2bigint('cc.value') . ' AND rr.groupid=ugg.usrgrpid' . ' AND ugg.userid=' . $userid . ' AND rr.permission<' . $permission . ' )' . ' )' . ' )';
         // condition host or template
         $sql_parts['where'][] = ' NOT EXISTS(' . ' SELECT cc.conditionid' . ' FROM conditions cc' . ' WHERE (cc.conditiontype=' . CONDITION_TYPE_HOST . ' OR cc.conditiontype=' . CONDITION_TYPE_HOST_TEMPLATE . ')' . ' AND cc.actionid=c.actionid' . ' AND (' . ' NOT EXISTS(' . ' SELECT hgg.hostid' . ' FROM hosts_groups hgg, rights r,users_groups ug' . ' WHERE hgg.hostid=' . zbx_dbcast_2bigint('cc.value') . ' AND r.id=hgg.groupid' . ' AND ug.userid=' . $userid . ' AND r.permission>=' . $permission . ' AND r.groupid=ug.usrgrpid)' . ' OR EXISTS(' . ' SELECT hgg.hostid' . ' FROM hosts_groups hgg, rights rr, users_groups gg' . ' WHERE hgg.hostid=' . zbx_dbcast_2bigint('cc.value') . ' AND rr.id=hgg.groupid' . ' AND rr.groupid=gg.usrgrpid' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . ')' . ' )' . ' )';
         // condition trigger
         $sql_parts['where'][] = ' NOT EXISTS(' . ' SELECT cc.conditionid ' . ' FROM conditions cc ' . ' WHERE cc.conditiontype=' . CONDITION_TYPE_TRIGGER . ' AND cc.actionid=c.actionid' . ' AND (' . ' NOT EXISTS(' . ' SELECT f.triggerid' . ' FROM functions f, items i,hosts_groups hg, rights r, users_groups ug' . ' WHERE ug.userid=' . $userid . ' AND r.groupid=ug.usrgrpid' . ' AND r.permission>=' . $permission . ' AND hg.groupid=r.id' . ' AND i.hostid=hg.hostid' . ' AND f.itemid=i.itemid' . ' AND f.triggerid=' . zbx_dbcast_2bigint('cc.value') . ')' . ' OR EXISTS(' . ' SELECT ff.functionid' . ' FROM functions ff, items ii' . ' WHERE ff.triggerid=' . zbx_dbcast_2bigint('cc.value') . ' AND ii.itemid=ff.itemid' . ' AND EXISTS(' . ' SELECT hgg.groupid' . ' FROM hosts_groups hgg, rights rr, users_groups ugg' . ' WHERE hgg.hostid=ii.hostid' . ' AND rr.id=hgg.groupid' . ' AND rr.groupid=ugg.usrgrpid' . ' AND ugg.userid=' . $userid . ' AND rr.permission<' . $permission . '))' . ' )' . ' )';
         // condition users
         $sql_parts['where'][] = ' NOT EXISTS(' . ' SELECT o.operationid ' . ' FROM operations o ' . ' WHERE o.operationtype=' . OPERATION_TYPE_MESSAGE . ' AND o.actionid=a.actionid' . ' AND ((' . ' o.object=' . OPERATION_OBJECT_USER . ' AND o.objectid NOT IN (' . ' SELECT DISTINCT ug.userid' . ' FROM users_groups ug' . ' WHERE ug.usrgrpid IN (' . ' SELECT uug.usrgrpid' . ' FROM users_groups uug' . ' WHERE uug.userid=' . $USER_DETAILS['userid'] . ' )' . ' )' . ' ) OR (' . ' o.object=' . OPERATION_OBJECT_GROUP . ' AND o.objectid NOT IN (' . ' SELECT ug.usrgrpid' . ' FROM users_groups ug' . ' WHERE ug.userid=' . $USER_DETAILS['userid'] . ' )' . ' )' . ' )' . ' )';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // 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']);
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupids'] = 'c.value';
         }
         $sql_parts['from']['conditions'] = 'conditions c';
         $sql_parts['where'][] = DBcondition('c.value', $options['groupids'], false, true);
         $sql_parts['where']['c'] = 'c.conditiontype=' . CONDITION_TYPE_HOST_GROUP;
         $sql_parts['where']['ac'] = 'a.actionid=c.actionid';
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['hostids'] = 'c.value';
         }
         $sql_parts['from']['conditions'] = 'conditions c';
         $sql_parts['where'][] = DBcondition('c.value', $options['hostids'], false, true);
         $sql_parts['where']['c'] = 'c.conditiontype=' . CONDITION_TYPE_HOST;
         $sql_parts['where']['ac'] = 'a.actionid=c.actionid';
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['triggerids'] = 'c.value';
         }
         $sql_parts['from']['conditions'] = 'conditions c';
         $sql_parts['where'][] = DBcondition('c.value', $options['triggerids'], false, true);
         $sql_parts['where']['c'] = 'c.conditiontype=' . CONDITION_TYPE_TRIGGER;
         $sql_parts['where']['ac'] = 'a.actionid=c.actionid';
     }
     // mediatypeids
     if (!is_null($options['mediatypeids'])) {
         zbx_value2array($options['mediatypeids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['mediatypeid'] = 'opm.mediatypeid';
         }
         $sql_parts['from']['opmediatypes'] = 'opmediatypes opm';
         $sql_parts['from']['operations'] = 'operations o';
         $sql_parts['where'][] = DBcondition('opm.mediatypeid', $options['mediatypeids']);
         $sql_parts['where']['oopm'] = 'opm.operationid=o.operationid';
         $sql_parts['where']['ao'] = 'a.actionid=o.actionid';
     }
     // userids
     if (!is_null($options['userids'])) {
         zbx_value2array($options['userids']);
         // TODO:
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('actions a', $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('actions a', $options, $sql_parts);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['actions'] = 'a.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('COUNT(DISTINCT a.actionid) 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'];
     }
     //---------------
     $actionids = 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('a.actionid', $nodeids) . $sql_where . $sql_order;
     //SDI($sql);
     $db_res = DBselect($sql, $sql_limit);
     while ($action = DBfetch($db_res)) {
         if ($options['countOutput']) {
             $result = $action['rowscount'];
         } else {
             $actionids[$action['actionid']] = $action['actionid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$action['actionid']] = array('actionid' => $action['actionid']);
             } else {
                 if (!isset($result[$action['actionid']])) {
                     $result[$action['actionid']] = array();
                 }
                 if (!is_null($options['select_conditions']) && !isset($result[$action['actionid']]['conditions'])) {
                     $result[$action['actionid']]['conditions'] = array();
                 }
                 if (!is_null($options['select_operations']) && !isset($result[$action['actionid']]['operations'])) {
                     $result[$action['actionid']]['operations'] = array();
                 }
                 $result[$action['actionid']] += $action;
             }
         }
     }
     COpt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding Conditions
     if (!is_null($options['select_conditions']) && str_in_array($options['select_conditions'], $subselects_allowed_outputs)) {
         $sql = 'SELECT c.* FROM conditions c WHERE ' . DBcondition('c.actionid', $actionids);
         $res = DBselect($sql);
         while ($condition = DBfetch($res)) {
             $result[$condition['actionid']]['conditions'][] = $condition;
         }
     }
     // Adding Operations
     if (!is_null($options['select_operations']) && str_in_array($options['select_operations'], $subselects_allowed_outputs)) {
         $operations = array();
         $operationids = array();
         $sql = 'SELECT o.* ' . ' FROM operations o ' . ' WHERE ' . DBcondition('o.actionid', $actionids);
         $res = DBselect($sql);
         while ($operation = DBfetch($res)) {
             $operation['opconditions'] = array();
             $operation['opmediatypes'] = array();
             $operations[$operation['operationid']] = $operation;
             $operationids[$operation['operationid']] = $operation['operationid'];
         }
         $sql = 'SELECT op.* FROM opconditions op WHERE ' . DBcondition('op.operationid', $operationids);
         $res = DBselect($sql);
         while ($opcondition = DBfetch($res)) {
             $operations[$opcondition['operationid']]['opconditions'][] = $opcondition;
         }
         $sql = 'SELECT op.* FROM opmediatypes op WHERE ' . DBcondition('op.operationid', $operationids);
         $res = DBselect($sql);
         while ($opmediatype = DBfetch($res)) {
             $operations[$opmediatype['operationid']]['opmediatypes'][] = $opmediatype;
         }
         foreach ($operations as $num => $operation) {
             $result[$operation['actionid']]['operations'][] = $operation;
         }
     }
     COpt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Пример #21
0
</head>
<body onload="zbxCallPostScripts();">
<?php 
}
define('PAGE_HEADER_LOADED', 1);
if (isset($_REQUEST['print'])) {
    define('ZBX_PAGE_NO_MENU', 1);
    $req = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], 'print') - 1);
    $link = new CLink(bold('&laquo;' . S_BACK_BIG), $req, 'small_font');
    $link->setAttribute('style', 'padding-left: 10px;');
    $printview = new CDiv($link, 'printless');
    $printview->setAttribute('style', 'border: 1px #333 dotted;');
    $printview->Show();
}
if (!defined('ZBX_PAGE_NO_MENU')) {
    COpt::compare_files_with_menu($ZBX_MENU);
    $help = new CLink(S_HELP, 'http://www.zabbix.com/documentation.php', 'small_font', null, 'nosid');
    $help->setTarget('_blank');
    $support = new CLink(S_GET_SUPPORT, 'http://www.zabbix.com/support.php', 'small_font', null, 'nosid');
    $support->setTarget('_blank');
    $printview = new CLink(S_PRINT, $_SERVER['REQUEST_URI'] . (empty($_GET) ? '?' : '&') . 'print=1', 'small_font', null, 'nosid');
    $page_header_r_col = array($help, '|', $support, '|', $printview);
    if ($USER_DETAILS['alias'] != ZBX_GUEST_USER) {
        $page_header_r_col[] = array('|');
        array_push($page_header_r_col, new CLink(S_PROFILE, 'profile.php', 'small_font', null, 'nosid'), '|');
        if ($USER_DETAILS['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
            $debug = new CLink(S_DEBUG, '#debug', 'small_font', null, 'nosid');
            $debug->setAttribute('onclick', "javascript: ShowHide('zbx_gebug_info');");
            array_push($page_header_r_col, $debug, '|');
        }
        array_push($page_header_r_col, new CLink(S_LOGOUT, 'index.php?reconnect=1', 'small_font', null, 'nosid'));
Пример #22
0
if (!defined('ZBX_PAGE_NO_MENU') && $page['file'] != 'profile.php') {
    CProfile::update('web.paging.lastpage', $page['file'], PROFILE_TYPE_STR);
}
CProfile::flush();
// END Transactions if havn't been -----------------
if (isset($DB) && isset($DB['TRANSACTIONS']) && $DB['TRANSACTIONS'] != 0) {
    error(S_TRANSACTION_HAVE_NOT_BEEN_CLOSED_ABORTING);
    DBend(false);
}
//--------------------------------------------------
show_messages();
$post_script = '';
if (uint_in_array($page['type'], array(PAGE_TYPE_HTML_BLOCK, PAGE_TYPE_HTML))) {
    if (!is_null($USER_DETAILS) && isset($USER_DETAILS['debug_mode']) && $USER_DETAILS['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
        COpt::profiling_stop('script');
        COpt::show();
    }
}
if ($page['type'] == PAGE_TYPE_HTML) {
    $post_script .= 'var page_refresh = null;' . "\n";
    if (isset($JS_TRANSLATE)) {
        $post_script .= 'var newLocale = ' . zbx_jsvalue($JS_TRANSLATE) . "\n";
        $post_script .= 'var locale = (typeof(locale) == "undefined" ? {} : locale);' . "\n";
        $post_script .= 'for(key in newLocale){locale[key] = newLocale[key];}' . "\n";
    }
    $post_script .= 'function zbxCallPostScripts(){' . "\n";
    if (isset($ZBX_PAGE_POST_JS)) {
        foreach ($ZBX_PAGE_POST_JS as $num => $script) {
            $post_script .= $script . "\n";
        }
    }