Esempio n. 1
0
 public function __construct($type = GRAPH_TYPE_PIE)
 {
     parent::__construct($type);
     $this->background = false;
     $this->sum = false;
     $this->exploderad = 1;
     $this->exploderad3d = 3;
     $this->graphheight3d = 12;
     $this->shiftlegendright = 17 * 7 + 7 + 10;
     // count of static chars * px/char + for color rectangle + space
     $this->drawlegendallow = 1;
 }
Esempio n. 2
0
 public function __construct($type = GRAPH_TYPE_COLUMN)
 {
     parent::__construct($type);
     $this->background = false;
     $this->opacity = 30;
     // bar/column opacity
     $this->sum = false;
     $this->shiftlegendright = 0;
     // count of static chars * px/char + for color rectangle + space
     $this->shiftCaption = 0;
     $this->maxCaption = 0;
     $this->drawlegendallow = 0;
     $this->series = array();
     $this->periodCaption = array();
     $this->seriesLegend = array();
     $this->seriesColor = array();
     $this->seriesCount = 0;
     $this->columnWidth = 10;
     // bar/column width per serie
     $this->seriesWidth = 10;
     // overal per serie bar/column width
     $this->seriesDistance = 10;
     $this->axisSideLeft = false;
     // Does values for axis left/top persist
     $this->axisSideRight = false;
     // Does values for axis right/bottom persist
     $this->xLabel = null;
     $this->yLabel = null;
     $this->yaxismin = array(GRAPH_YAXIS_SIDE_LEFT => 0, GRAPH_YAXIS_SIDE_RIGHT => 0);
     $this->yaxismax = array(GRAPH_YAXIS_SIDE_LEFT => 100, GRAPH_YAXIS_SIDE_RIGHT => 100);
     $this->minValue = array(GRAPH_YAXIS_SIDE_LEFT => 0, GRAPH_YAXIS_SIDE_RIGHT => 0);
     $this->maxValue = array(GRAPH_YAXIS_SIDE_LEFT => null, GRAPH_YAXIS_SIDE_RIGHT => null);
     $this->gridLinesCount = NULL;
     // How many grids to draw
     $this->gridPixels = 40;
     // optimal grid size
     $this->gridStep = array(GRAPH_YAXIS_SIDE_LEFT => null, GRAPH_YAXIS_SIDE_RIGHT => null);
     // seted value
     $this->side_values = array(GRAPH_YAXIS_SIDE_LEFT => ITEM_VALUE_TYPE_UINT64, GRAPH_YAXIS_SIDE_RIGHT => ITEM_VALUE_TYPE_UINT64);
     // 0 - float, 3 - uint
     $this->column = null;
     $this->units = array(GRAPH_YAXIS_SIDE_LEFT => '', GRAPH_YAXIS_SIDE_RIGHT => '');
     // Units for values
 }
Esempio n. 3
0
 public function __construct($type = GRAPH_TYPE_NORMAL)
 {
     parent::__construct($type);
     $this->yaxismin = null;
     $this->yaxismax = null;
     $this->triggers = array();
     $this->ymin_type = GRAPH_YAXIS_TYPE_CALCULATED;
     $this->ymax_type = GRAPH_YAXIS_TYPE_CALCULATED;
     $this->yaxisright = 0;
     $this->yaxisleft = 0;
     $this->ymin_itemid = 0;
     $this->ymax_itemid = 0;
     $this->percentile = array('left' => array('percent' => 0, 'value' => 0), 'right' => array('percent' => 0, 'value' => 0));
     $this->m_showWorkPeriod = 1;
     $this->m_showTriggers = 1;
     $this->zero = array();
     $this->graphorientation = '';
     $this->gridLinesCount = NULL;
     // How many grids to draw
     $this->gridPixels = 40;
     // optimal grid size
 }
Esempio n. 4
0
     $options = array('editable' => 1, 'hostids' => $templateid, 'extendoutput' => 1);
     $template_triggers = CTrigger::get($options);
     if (empty($template_triggers)) {
         $trig_lbx->setAttribute('style', 'width: 200px;');
     } else {
         foreach ($template_triggers as $tnum => $ttrigger) {
             $trigger_description = expand_trigger_description($ttrigger['triggerid']);
             $trig_lbx->addItem($ttrigger['triggerid'], $trigger_description);
         }
     }
     $template_tbl->addRow(array(S_TRIGGERS, $trig_lbx));
     // FORM ITEM : Host graphs
     $graphs_lbx = new CListBox('graphs', null, 8);
     $graphs_lbx->setAttribute('disabled', 'disabled');
     $options = array('editable' => 1, 'hostids' => $templateid, 'extendoutput' => 1);
     $template_graphs = CGraph::get($options);
     if (empty($template_graphs)) {
         $graphs_lbx->setAttribute('style', 'width: 200px;');
     } else {
         foreach ($template_graphs as $tnum => $tgraph) {
             $graphs_lbx->addItem($tgraph['graphid'], $tgraph['name']);
         }
     }
     $template_tbl->addRow(array(S_GRAPHS, $graphs_lbx));
 }
 // FULL CLONE }
 $host_footer = array();
 $host_footer[] = new CButton('save', S_SAVE);
 if ($templateid > 0 && $_REQUEST['form'] != 'full_clone') {
     $host_footer[] = SPACE;
     $host_footer[] = new CButton('clone', S_CLONE);
Esempio n. 5
0
function copy_template_elements($hostid, $templateid = null, $copy_mode = false)
{
    $result = true;
    copy_template_applications($hostid, $templateid, $copy_mode);
    copy_template_items($hostid, $templateid, $copy_mode);
    copy_template_triggers($hostid, $templateid, $copy_mode);
    // razvilka $copy
    if ($copy_mode) {
        copy_template_graphs($hostid, $templateid, $copy_mode);
    } else {
        $result = CGraph::syncTemplates(array('hostids' => $hostid, 'templateids' => $templateid));
    }
    return $result;
}
Esempio n. 6
0
 // ===============================================================
 // Last 7 days stored Bytes graph
 // ===============================================================
 $graph = new CGraph("backupjobreport-graph01.jpg");
 foreach ($days as $day) {
     $stored_bytes = Jobs_Model::getStoredBytes($dbSql->db_link, array($day['start'], $day['end']), $backupjob_name);
     $days_stored_bytes[] = array(date("m-d", $day['start']), $stored_bytes);
 }
 $graph->SetData($days_stored_bytes, 'bars', true);
 // Graph rendering
 $view->assign('graph_stored_bytes', $graph->Render());
 unset($graph);
 // ===============================================================
 // Getting last 7 days stored files graph
 // ===============================================================
 $graph = new CGraph("backupjobreport-graph02.jpg");
 foreach ($days as $day) {
     $stored_files = Jobs_Model::getStoredFiles($dbSql->db_link, array($day['start'], $day['end']), $backupjob_name);
     $days_stored_files[] = array(date("m-d", $day['start']), $stored_files);
 }
 $graph->SetData($days_stored_files, 'bars');
 $graph->SetYTitle("Files");
 // Graph rendering
 $view->assign('graph_stored_files', $graph->Render());
 unset($graph);
 // Get last 10 jobs list
 $query = "SELECT JobId, Level, JobFiles, JobBytes, ReadBytes, JobStatus, StartTime, EndTime, Name ";
 $query .= "FROM Job ";
 $query .= "WHERE Name = '{$backupjob_name}' ";
 $query .= "ORDER BY EndTime DESC ";
 $query .= "LIMIT 7 ";
 private function _initGraphs($graphid, $options)
 {
     $this->data['graphs'] = array();
     if (!$this->hostsSelected) {
         $graphid = 0;
     } else {
         $def_ptions = array('nodeids' => $this->config['all_nodes'] ? get_current_nodeid() : null, 'output' => API_OUTPUT_EXTEND, 'groupids' => $this->groupid > 0 && $this->hostid == 0 ? $this->groupid : null, 'hostids' => $this->hostid > 0 ? $this->hostid : null);
         $options = zbx_array_merge($def_ptions, $options);
         $graphs = CGraph::get($options);
         order_result($graphs, 'name');
         foreach ($graphs as $graph) {
             $this->data['graphs'][$graph['graphid']] = $graph['name'];
         }
         if (is_null($graphid)) {
             $graphid = $this->_profileIds['graphid'];
         }
         $graphid = isset($this->data['graphs'][$graphid]) ? $graphid : 0;
     }
     if (!is_null($this->_requestIds['graphid'])) {
         CProfile::update($this->_profileIdx['graphs'], $graphid, PROFILE_TYPE_ID);
         CProfile::update(self::GRAPH_LATEST_IDX, $graphid, PROFILE_TYPE_ID);
     }
     $this->isSelected['graphsSelected'] = $graphid > 0;
     $this->ids['graphid'] = $graphid;
 }
Esempio n. 8
0
 /**
  * Delete graphs
  *
  * @static
  * @param array $graphids
  * @return boolean
  */
 public static function delete($graphids)
 {
     $result = delete_graph($graphids);
     if ($result) {
         return true;
     } else {
         self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Internal zabbix error');
         return false;
     }
 }
Esempio n. 9
0
<?php

include "restore_confs.php";
error_reporting(E_PARSE);
if ($BILLEVEL < 3 && $action != "topurl") {
    die($CURRENT_USER["level"] . "");
} elseif ($BILLEVEL <= 1) {
    die("");
}
$BILL = new CBilling($GV["dbhost"], $GV["dbname"], $GV["dblogin"], $GV["dbpassword"]);
include "graph_class.php";
$gr = new CGraph();
//настройки графика
$params = array();
$params[0] = 600;
$params[1] = 500;
$params[2] = 255;
$params[3] = 255;
$params[4] = 255;
$params[5] = 0;
$params[6] = 0;
$params[7] = 0;
$params[8] = 155;
$params[9] = 155;
$params[10] = 155;
$params[11] = 50;
$params[12] = 50;
$params[13] = "./GOST_A.TTF";
$params[15] = "";
$params[16] = "";
$params[17] = 6;
Esempio n. 10
0
    /**
     * Get items data
     *
     * {@source}
     * @access public
     * @static
     * @since 1.8
     * @version 1
     *
     * @static
     * @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');
        // allowed columns for sorting
        $sql_parts = array('select' => array('items' => 'i.itemid'), 'from' => array('items i'), 'where' => array('i.type<>9'), 'order' => array(), 'limit' => null);
        $def_options = array('nodeids' => 0, 'groupids' => 0, 'hostids' => 0, 'itemids' => 0, 'graphids' => 0, 'triggerids' => 0, 'applicationids' => 0, 'status' => 0, 'templated_items' => 0, 'editable' => 0, 'nopermissions' => 0, 'select_triggers' => 0, 'select_hosts' => 0, 'select_graphs' => 0, 'extendoutput' => 0, 'count' => 0, 'pattern' => '', 'limit' => 0, 'order' => '');
        $options = array_merge($def_options, $options);
        // editable + PERMISSION CHECK
        if (defined('ZBX_API_REQUEST')) {
            $options['nopermissions'] = false;
        }
        if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
        } else {
            $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
            $sql_parts['from']['hg'] = 'hosts_groups hg';
            $sql_parts['from']['r'] = 'rights r';
            $sql_parts['from']['ug'] = '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 = $options['nodeids'] ? $options['nodeids'] : get_current_nodeid(false);
        // groupids
        if ($options['groupids'] != 0) {
            zbx_value2array($options['groupids']);
            if ($options['extendoutput'] != 0) {
                $sql_parts['select']['groupid'] = 'hg.groupid';
            }
            $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']);
            $sql_parts['where'][] = 'hg.hostid=i.hostid';
            $sql_parts['from']['hg'] = 'hosts_groups hg';
        }
        // hostids
        if ($options['hostids'] != 0) {
            zbx_value2array($options['hostids']);
            if ($options['extendoutput'] != 0) {
                $sql_parts['select']['hostid'] = 'i.hostid';
            }
            $sql_parts['where'][] = DBcondition('i.hostid', $options['hostids']);
        }
        // itemids
        if ($options['itemids'] != 0) {
            $sql_parts['where'][] = DBcondition('i.itemid', $options['itemids']);
        }
        // triggerids
        if ($options['triggerids'] != 0) {
            zbx_value2array($options['triggerids']);
            if ($options['extendoutput'] != 0) {
                $sql_parts['select']['triggerid'] = 'f.triggerid';
            }
            $sql_parts['where'][] = DBcondition('f.triggerid', $options['triggerids']);
            $sql_parts['where'][] = 'i.itemid=f.itemid';
            $sql_parts['from'][] = 'functions f';
        }
        // applicationids
        if ($options['applicationids'] != 0) {
            $sql_parts['where'][] = DBcondition('a.applicationid', $options['applicationids']);
            $sql_parts['where'][] = 'i.hostid=a.hostid';
            $sql_parts['from'][] = 'applications a';
        }
        // graphids
        if ($options['graphids'] != 0) {
            zbx_value2array($options['graphids']);
            if ($options['extendoutput'] != 0) {
                $sql_parts['select']['graphid'] = 'gi.graphid';
            }
            $sql_parts['from']['gi'] = 'graphs_items gi';
            $sql_parts['where'][] = DBcondition('gi.graphid', $options['graphids']);
            $sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
        }
        // status
        if ($options['status'] != 0) {
            $sql_parts['where'][] = 'i.status=' . $options['status'];
        }
        // templated_items
        if ($options['templated_items'] != 0) {
            $sql_parts['where'][] = 'i.templateid<>0';
        }
        // extendoutput
        if ($options['extendoutput'] != 0) {
            $sql_parts['select']['items'] = 'i.*';
        }
        // count
        if ($options['count'] != 0) {
            $sql_parts['select']['items'] = 'count(i.itemid) as count';
        }
        // pattern
        if (!zbx_empty($options['pattern'])) {
            $sql_parts['where'][] = ' UPPER(i.description) LIKE ' . zbx_dbstr('%' . strtoupper($options['pattern']) . '%');
        }
        // order
        // restrict not allowed columns for sorting
        $options['order'] = in_array($options['order'], $sort_columns) ? $options['order'] : '';
        if (!zbx_empty($options['order'])) {
            $sql_parts['order'][] = 'i.' . $options['order'];
            if (!str_in_array('i.' . $options['order'], $sql_parts['select'])) {
                $sql_parts['select'][] = 'i.' . $options['order'];
            }
        }
        // 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['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('i.itemid', $nodeids) . $sql_where . $sql_order;
        $res = DBselect($sql, $sql_limit);
        while ($item = DBfetch($res)) {
            if ($options['count']) {
                $result = $item;
            } else {
                $itemids[$item['itemid']] = $item['itemid'];
                if ($options['extendoutput'] == 0) {
                    $result[$item['itemid']] = $item['itemid'];
                } else {
                    if (!isset($result[$item['itemid']])) {
                        $result[$item['itemid']] = array();
                    }
                    if ($options['select_triggers'] && !isset($result[$item['itemid']]['triggerids'])) {
                        $result[$item['itemid']]['triggerids'] = array();
                        $result[$item['itemid']]['triggers'] = array();
                    }
                    if ($options['select_graphs'] && !isset($result[$item['itemid']]['graphids'])) {
                        $result[$item['itemid']]['graphids'] = array();
                        $result[$item['itemid']]['graphs'] = array();
                    }
                    if ($options['select_hosts'] && !isset($result[$item['itemid']]['hostids'])) {
                        $result[$item['itemid']]['hostids'] = array();
                        $result[$item['itemid']]['hosts'] = array();
                    }
                    // hostids
                    if (isset($item['hostid'])) {
                        if (!isset($result[$item['itemid']]['hostids'])) {
                            $result[$item['itemid']]['hostids'] = array();
                        }
                        $result[$item['itemid']]['hostids'][$item['hostid']] = $item['hostid'];
                        unset($item['hostid']);
                    }
                    // triggerids
                    if (isset($item['triggerid'])) {
                        if (!isset($result[$item['itemid']]['triggerids'])) {
                            $result[$item['itemid']]['triggerids'] = array();
                        }
                        $result[$item['itemid']]['triggerids'][$item['triggerid']] = $item['triggerid'];
                        unset($item['triggerid']);
                    }
                    // graphids
                    if (isset($item['graphid'])) {
                        if (!isset($result[$item['itemid']]['graphids'])) {
                            $result[$item['itemid']]['graphids'] = array();
                        }
                        $result[$item['itemid']]['graphids'][$item['graphid']] = $item['graphid'];
                        unset($item['graphid']);
                    }
                    $result[$item['itemid']] += $item;
                }
            }
        }
        // Adding Objects
        // Adding hosts
        if ($options['select_hosts']) {
            $obj_params = array('extendoutput' => 1, 'itemids' => $itemids, 'nopermissions' => 1);
            $hosts = CHost::get($obj_params);
            foreach ($hosts as $hostid => $host) {
                foreach ($host['itemids'] as $num => $itemid) {
                    $result[$itemid]['hostids'][$hostid] = $hostid;
                    $result[$itemid]['hosts'][$hostid] = $host;
                }
            }
        }
        // Adding triggers
        if ($options['select_triggers']) {
            $obj_params = array('extendoutput' => 1, 'itemids' => $itemids);
            $triggers = CTrigger::get($obj_params);
            foreach ($triggers as $triggerid => $trigger) {
                foreach ($trigger['itemids'] as $num => $itemid) {
                    $result[$itemid]['triggerids'][$triggerid] = $triggerid;
                    $result[$itemid]['triggers'][$triggerid] = $trigger;
                }
            }
        }
        // Adding graphs
        if ($options['select_graphs']) {
            $obj_params = array('extendoutput' => 1, 'itemids' => $itemids);
            $graphs = CGraph::get($obj_params);
            foreach ($graphs as $graphid => $graph) {
                foreach ($graph['itemids'] as $num => $itemid) {
                    $result[$itemid]['graphids'][$graphid] = $graphid;
                    $result[$itemid]['graphs'][$graphid] = $graph;
                }
            }
        }
        return $result;
    }
Esempio n. 11
0
    /**
     * Get Host data
     *
     * {@source}
     * @access public
     * @static
     * @since 1.8
     * @version 1
     *
     * @param _array $options
     * @param array $options['nodeids'] Node IDs
     * @param array $options['groupids'] HostGroup IDs
     * @param array $options['hostids'] Host IDs
     * @param boolean $options['monitored_hosts'] only monitored Hosts
     * @param boolean $options['templated_hosts'] include templates in result
     * @param boolean $options['with_items'] only with items
     * @param boolean $options['with_monitored_items'] only with monitored items
     * @param boolean $options['with_historical_items'] only with historical items
     * @param boolean $options['with_triggers'] only with triggers
     * @param boolean $options['with_monitored_triggers'] only with monitores triggers
     * @param boolean $options['with_httptests'] only with http tests
     * @param boolean $options['with_monitored_httptests'] only with monitores 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['order'] depricated parametr (for now)
     * @return array|boolean Host data as array or false if error
     */
    public static function get($options = array())
    {
        global $USER_DETAILS;
        $result = array();
        $user_type = $USER_DETAILS['type'];
        $userid = $USER_DETAILS['userid'];
        $sort_columns = array('hostid', 'host');
        // allowed columns for sorting
        $sql_parts = array('select' => array('hosts' => 'h.hostid'), 'from' => array('hosts h'), 'where' => array(), 'order' => array(), 'limit' => null);
        $def_options = array('nodeids' => 0, 'groupids' => 0, 'hostids' => 0, 'templateids' => 0, 'triggerids' => 0, 'graphids' => 0, 'monitored_hosts' => 0, 'templated_hosts' => 0, 'with_items' => 0, 'with_monitored_items' => 0, 'with_historical_items' => 0, 'with_triggers' => 0, 'with_monitored_triggers' => 0, 'with_httptests' => 0, 'with_monitored_httptests' => 0, 'with_graphs' => 0, 'editable' => 0, 'nopermissions' => 0, 'extendoutput' => 0, 'select_templates' => 0, 'select_items' => 0, 'select_triggers' => 0, 'select_graphs' => 0, 'count' => 0, 'pattern' => '', 'extend_pattern' => 0, 'order' => '', 'limit' => 0);
        $options = array_merge($def_options, $options);
        // editable + PERMISSION CHECK
        if (defined('ZBX_API_REQUEST')) {
            $options['nopermissions'] = false;
        }
        if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
        } else {
            $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
            $sql_parts['from']['hg'] = 'hosts_groups hg';
            $sql_parts['from']['r'] = 'rights r';
            $sql_parts['from']['ug'] = 'users_groups ug';
            $sql_parts['where']['hgh'] = '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 = $options['nodeids'] ? $options['nodeids'] : get_current_nodeid(false);
        // groupids
        if ($options['groupids'] != 0) {
            zbx_value2array($options['groupids']);
            if ($options['extendoutput'] != 0) {
                $sql_parts['select']['groupid'] = 'hg.groupid';
            }
            $sql_parts['from']['hg'] = 'hosts_groups hg';
            $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']);
            $sql_parts['where']['hgh'] = 'hg.hostid=h.hostid';
        }
        // hostids
        if ($options['hostids'] != 0) {
            zbx_value2array($options['hostids']);
            $sql_parts['where'][] = DBcondition('h.hostid', $options['hostids']);
        }
        // templateids
        if ($options['templateids'] != 0) {
            zbx_value2array($options['templateids']);
            if ($options['extendoutput'] != 0) {
                $sql_parts['select']['templateid'] = 'ht.templateid';
            }
            $sql_parts['from']['ht'] = 'hosts_templates ht';
            $sql_parts['where'][] = DBcondition('ht.templateid', $options['templateids']);
            $sql_parts['where']['hht'] = 'h.hostid=ht.hostid';
        }
        // triggerids
        if ($options['triggerids'] != 0) {
            zbx_value2array($options['triggerids']);
            if ($options['extendoutput'] != 0) {
                $sql_parts['select']['triggerid'] = 'f.triggerid';
            }
            $sql_parts['from']['f'] = 'functions f';
            $sql_parts['from']['i'] = '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';
        }
        // graphids
        if ($options['graphids'] != 0) {
            zbx_value2array($options['graphids']);
            if ($options['extendoutput'] != 0) {
                $sql_parts['select']['graphid'] = 'gi.graphid';
            }
            $sql_parts['from']['gi'] = 'graphs_items gi';
            $sql_parts['from']['i'] = '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';
        }
        // monitored_hosts, templated_hosts
        if ($options['monitored_hosts'] != 0) {
            $sql_parts['where'][] = 'h.status=' . HOST_STATUS_MONITORED;
        } else {
            if ($options['templated_hosts'] != 0) {
                $sql_parts['where'][] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ')';
            } else {
                $sql_parts['where'][] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')';
            }
        }
        // with_items, with_monitored_items, with_historical_items
        if ($options['with_items'] != 0) {
            $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid )';
        } else {
            if ($options['with_monitored_items'] != 0) {
                $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND i.status=' . ITEM_STATUS_ACTIVE . ')';
            } else {
                if ($options['with_historical_items']) {
                    $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND (i.status=' . ITEM_STATUS_ACTIVE . ' OR i.status=' . ITEM_STATUS_NOTSUPPORTED . ') AND i.lastvalue IS NOT NULL)';
                }
            }
        }
        // with_triggers, with_monitored_triggers
        if ($options['with_triggers'] != 0) {
            $sql_parts['where'][] = 'EXISTS( 
					SELECT i.itemid
					FROM items i, functions f, triggers t
					WHERE i.hostid=h.hostid 
						AND i.itemid=f.itemid 
						AND f.triggerid=t.triggerid)';
        } else {
            if ($options['with_monitored_triggers'] != 0) {
                $sql_parts['where'][] = 'EXISTS( 
					SELECT i.itemid 
					FROM items i, functions f, triggers t
					WHERE i.hostid=h.hostid 
						AND i.status=' . ITEM_STATUS_ACTIVE . '
						AND i.itemid=f.itemid 
						AND f.triggerid=t.triggerid 
						AND t.status=' . TRIGGER_STATUS_ENABLED . ')';
            }
        }
        // with_httptests, with_monitored_httptests
        if ($options['with_httptests'] != 0) {
            $sql_parts['where'][] = 'EXISTS( 
					SELECT a.applicationid 
					FROM applications a, httptest ht 
					WHERE a.hostid=h.hostid 
						AND ht.applicationid=a.applicationid)';
        } else {
            if ($options['with_monitored_httptests'] != 0) {
                $sql_parts['where'][] = 'EXISTS( 
					SELECT a.applicationid 	
					FROM applications a, httptest ht 	
					WHERE a.hostid=h.hostid	
						AND ht.applicationid=a.applicationid 	
						AND ht.status=' . HTTPTEST_STATUS_ACTIVE . ')';
            }
        }
        // with_graphs
        if ($options['with_graphs'] != 0) {
            $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['extendoutput'] != 0) {
            $sql_parts['select']['hosts'] = 'h.*';
        }
        // count
        if ($options['count'] != 0) {
            $options['select_templates'] = 0;
            $options['select_items'] = 0;
            $options['select_triggers'] = 0;
            $options['select_graphs'] = 0;
            $sql_parts['select'] = array('count(h.hostid) as rowscount');
        }
        // pattern
        if (!zbx_empty($options['pattern'])) {
            if ($options['extend_pattern']) {
                $sql_parts['where'][] = ' ( ' . 'UPPER(h.host) LIKE ' . zbx_dbstr('%' . strtoupper($options['pattern']) . '%') . ' OR ' . 'h.ip LIKE ' . zbx_dbstr('%' . $options['pattern'] . '%') . ' OR ' . 'UPPER(h.dns) LIKE ' . zbx_dbstr('%' . strtoupper($options['pattern']) . '%') . ' ) ';
            } else {
                $sql_parts['where'][] = ' UPPER(h.host) LIKE ' . zbx_dbstr('%' . strtoupper($options['pattern']) . '%');
            }
        }
        // order
        // restrict not allowed columns for sorting
        $options['order'] = str_in_array($options['order'], $sort_columns) ? $options['order'] : '';
        if (!zbx_empty($options['order'])) {
            $sql_parts['order'][] = 'h.' . $options['order'];
            if (!str_in_array('h.' . $options['order'], $sql_parts['select'])) {
                $sql_parts['select'][] = 'h.' . $options['order'];
            }
        }
        // limit
        if (zbx_ctype_digit($options['limit']) && $options['limit']) {
            $sql_parts['limit'] = $options['limit'];
        }
        //-------
        $hostids = array();
        $sql_parts['select'] = array_unique($sql_parts['select']);
        $sql_parts['from'] = array_unique($sql_parts['from']);
        $sql_parts['where'] = array_unique($sql_parts['where']);
        $sql_parts['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('h.hostid', $nodeids) . $sql_where . $sql_order;
        $res = DBselect($sql, $sql_limit);
        while ($host = DBfetch($res)) {
            if ($options['count']) {
                $result = $host;
            } else {
                $hostids[$host['hostid']] = $host['hostid'];
                if ($options['extendoutput'] == 0) {
                    $result[$host['hostid']] = $host['hostid'];
                } else {
                    if (!isset($result[$host['hostid']])) {
                        $result[$host['hostid']] = array();
                    }
                    if ($options['select_templates'] && !isset($result[$host['hostid']]['templateids'])) {
                        $result[$host['hostid']]['templateids'] = array();
                        $result[$host['hostid']]['templates'] = array();
                    }
                    if ($options['select_items'] && !isset($result[$host['hostid']]['itemids'])) {
                        $result[$host['hostid']]['itemids'] = array();
                        $result[$host['hostid']]['items'] = array();
                    }
                    if ($options['select_triggers'] && !isset($result[$host['hostid']]['triggerids'])) {
                        $result[$host['hostid']]['triggerids'] = array();
                        $result[$host['hostid']]['triggers'] = array();
                    }
                    if ($options['select_graphs'] && !isset($result[$host['hostid']]['graphids'])) {
                        $result[$host['hostid']]['graphids'] = array();
                        $result[$host['hostid']]['graphs'] = array();
                    }
                    // groupids
                    if (isset($host['groupid'])) {
                        if (!isset($result[$host['hostid']]['groupids'])) {
                            $result[$host['hostid']]['groupids'] = array();
                        }
                        $result[$host['hostid']]['groupids'][$host['groupid']] = $host['groupid'];
                        unset($host['groupid']);
                    }
                    // templateids
                    if (isset($host['templateid'])) {
                        if (!isset($result[$host['hostid']]['templateids'])) {
                            $result[$host['hostid']]['templateids'] = array();
                        }
                        $result[$host['hostid']]['templateids'][$host['templateid']] = $host['templateid'];
                        unset($host['templateid']);
                    }
                    // triggerids
                    if (isset($host['triggerid'])) {
                        if (!isset($result[$host['hostid']]['triggerids'])) {
                            $result[$host['hostid']]['triggerids'] = array();
                        }
                        $result[$host['hostid']]['triggerids'][$host['triggerid']] = $host['triggerid'];
                        unset($host['triggerid']);
                    }
                    // itemids
                    if (isset($host['itemid'])) {
                        if (!isset($result[$host['hostid']]['itemids'])) {
                            $result[$host['hostid']]['itemids'] = array();
                        }
                        $result[$host['hostid']]['itemids'][$host['itemid']] = $host['itemid'];
                        unset($host['itemid']);
                    }
                    // graphids
                    if (isset($host['graphid'])) {
                        if (!isset($result[$host['hostid']]['graphids'])) {
                            $result[$host['hostid']]['graphids'] = array();
                        }
                        $result[$host['hostid']]['graphids'][$host['graphid']] = $host['graphid'];
                        unset($host['graphid']);
                    }
                    $result[$host['hostid']] += $host;
                }
            }
        }
        // Adding Objects
        // Adding Templates
        if ($options['select_templates']) {
            $obj_params = array('extendoutput' => 1, 'hostids' => $hostids);
            $templates = CTemplate::get($obj_params);
            foreach ($templates as $templateid => $template) {
                foreach ($template['hostids'] as $num => $hostid) {
                    $result[$hostid]['templateids'][$templateid] = $templateid;
                    $result[$hostid]['templates'][$templateid] = $template;
                }
            }
        }
        // Adding Items
        if ($options['select_items']) {
            $obj_params = array('extendoutput' => 1, 'hostids' => $hostids, 'nopermissions' => 1);
            $items = CItem::get($obj_params);
            foreach ($items as $itemid => $item) {
                foreach ($item['hostids'] as $num => $hostid) {
                    $result[$hostid]['itemids'][$itemid] = $itemid;
                    $result[$hostid]['items'][$itemid] = $item;
                }
            }
        }
        // Adding triggers
        if ($options['select_triggers']) {
            $obj_params = array('extendoutput' => 1, 'hostids' => $hostids);
            $triggers = CTrigger::get($obj_params);
            foreach ($triggers as $triggerid => $trigger) {
                foreach ($trigger['hostids'] as $num => $hostid) {
                    $result[$hostid]['triggerids'][$triggerid] = $triggerid;
                    $result[$hostid]['triggers'][$triggerid] = $trigger;
                }
            }
        }
        // Adding graphs
        if ($options['select_graphs']) {
            $obj_params = array('extendoutput' => 1, 'hostids' => $hostids);
            $graphs = CGraph::get($obj_params);
            foreach ($graphs as $graphid => $graph) {
                foreach ($graph['hostids'] as $num => $hostid) {
                    $result[$hostid]['graphids'][$graphid] = $graphid;
                    $result[$hostid]['graphs'][$graphid] = $graph;
                }
            }
        }
        return $result;
    }
Esempio n. 12
0
 private static function graph($action, $params)
 {
     CGraph::$error = array();
     switch ($action) {
         default:
             $result = call_user_func(array('CGraph', $action), $params);
     }
     self::$result = $result;
 }
Esempio n. 13
0
function insert_host_form()
{
    global $USER_DETAILS;
    $host_groups = get_request('groups', array());
    if (isset($_REQUEST['groupid']) && $_REQUEST['groupid'] > 0 && empty($host_groups)) {
        array_push($host_groups, $_REQUEST['groupid']);
    }
    $newgroup = get_request('newgroup', '');
    $host = get_request('host', '');
    $port = get_request('port', CProfile::get('HOST_PORT', 10050));
    $status = get_request('status', HOST_STATUS_MONITORED);
    $useip = get_request('useip', 1);
    $dns = get_request('dns', '');
    $ip = get_request('ip', '0.0.0.0');
    $proxy_hostid = get_request('proxy_hostid', '');
    $useipmi = get_request('useipmi', 'no');
    $ipmi_ip = get_request('ipmi_ip', '');
    $ipmi_port = get_request('ipmi_port', 623);
    $ipmi_authtype = get_request('ipmi_authtype', -1);
    $ipmi_privilege = get_request('ipmi_privilege', 2);
    $ipmi_username = get_request('ipmi_username', '');
    $ipmi_password = get_request('ipmi_password', '');
    $useprofile = get_request('useprofile', 'no');
    $devicetype = get_request('devicetype', '');
    $name = get_request('name', '');
    $os = get_request('os', '');
    $serialno = get_request('serialno', '');
    $tag = get_request('tag', '');
    $macaddress = get_request('macaddress', '');
    $hardware = get_request('hardware', '');
    $software = get_request('software', '');
    $contact = get_request('contact', '');
    $location = get_request('location', '');
    $notes = get_request('notes', '');
    $_REQUEST['hostid'] = get_request('hostid', 0);
    // BEGIN: HOSTS PROFILE EXTENDED Section
    $useprofile_ext = get_request('useprofile_ext', 'no');
    $ext_host_profiles = get_request('ext_host_profiles', array());
    // END:   HOSTS PROFILE EXTENDED Section
    $templates = get_request('templates', array());
    $clear_templates = get_request('clear_templates', array());
    $frm_title = S_HOST;
    if ($_REQUEST['hostid'] > 0) {
        $db_host = get_host_by_hostid($_REQUEST['hostid']);
        $frm_title .= SPACE . ' [' . $db_host['host'] . ']';
        $original_templates = get_templates_by_hostid($_REQUEST['hostid']);
    } else {
        $original_templates = array();
    }
    if ($_REQUEST['hostid'] > 0 && !isset($_REQUEST['form_refresh'])) {
        $proxy_hostid = $db_host['proxy_hostid'];
        $host = $db_host['host'];
        $port = $db_host['port'];
        $status = $db_host['status'];
        $useip = $db_host['useip'];
        $useipmi = $db_host['useipmi'] ? 'yes' : 'no';
        $ip = $db_host['ip'];
        $dns = $db_host['dns'];
        $ipmi_ip = $db_host['ipmi_ip'];
        $ipmi_port = $db_host['ipmi_port'];
        $ipmi_authtype = $db_host['ipmi_authtype'];
        $ipmi_privilege = $db_host['ipmi_privilege'];
        $ipmi_username = $db_host['ipmi_username'];
        $ipmi_password = $db_host['ipmi_password'];
        // add groups
        $options = array('hostids' => $_REQUEST['hostid']);
        $host_groups = CHostGroup::get($options);
        $host_groups = zbx_objectValues($host_groups, 'groupid');
        // read profile
        $db_profiles = DBselect('SELECT * FROM hosts_profiles WHERE hostid=' . $_REQUEST['hostid']);
        $useprofile = 'no';
        $db_profile = DBfetch($db_profiles);
        if ($db_profile) {
            $useprofile = 'yes';
            $devicetype = $db_profile['devicetype'];
            $name = $db_profile['name'];
            $os = $db_profile['os'];
            $serialno = $db_profile['serialno'];
            $tag = $db_profile['tag'];
            $macaddress = $db_profile['macaddress'];
            $hardware = $db_profile['hardware'];
            $software = $db_profile['software'];
            $contact = $db_profile['contact'];
            $location = $db_profile['location'];
            $notes = $db_profile['notes'];
        }
        // BEGIN: HOSTS PROFILE EXTENDED Section
        $useprofile_ext = 'no';
        $db_profiles_alt = DBselect('SELECT * FROM hosts_profiles_ext WHERE hostid=' . $_REQUEST['hostid']);
        if ($ext_host_profiles = DBfetch($db_profiles_alt)) {
            $useprofile_ext = 'yes';
        } else {
            $ext_host_profiles = array();
        }
        // END:   HOSTS PROFILE EXTENDED Section
        $templates = $original_templates;
    }
    $ext_profiles_fields = array('device_alias' => S_DEVICE_ALIAS, 'device_type' => S_DEVICE_TYPE, 'device_chassis' => S_DEVICE_CHASSIS, 'device_os' => S_DEVICE_OS, 'device_os_short' => S_DEVICE_OS_SHORT, 'device_hw_arch' => S_DEVICE_HW_ARCH, 'device_serial' => S_DEVICE_SERIAL, 'device_model' => S_DEVICE_MODEL, 'device_tag' => S_DEVICE_TAG, 'device_vendor' => S_DEVICE_VENDOR, 'device_contract' => S_DEVICE_CONTRACT, 'device_who' => S_DEVICE_WHO, 'device_status' => S_DEVICE_STATUS, 'device_app_01' => S_DEVICE_APP_01, 'device_app_02' => S_DEVICE_APP_02, 'device_app_03' => S_DEVICE_APP_03, 'device_app_04' => S_DEVICE_APP_04, 'device_app_05' => S_DEVICE_APP_05, 'device_url_1' => S_DEVICE_URL_1, 'device_url_2' => S_DEVICE_URL_2, 'device_url_3' => S_DEVICE_URL_3, 'device_networks' => S_DEVICE_NETWORKS, 'device_notes' => S_DEVICE_NOTES, 'device_hardware' => S_DEVICE_HARDWARE, 'device_software' => S_DEVICE_SOFTWARE, 'ip_subnet_mask' => S_IP_SUBNET_MASK, 'ip_router' => S_IP_ROUTER, 'ip_macaddress' => S_IP_MACADDRESS, 'oob_ip' => S_OOB_IP, 'oob_subnet_mask' => S_OOB_SUBNET_MASK, 'oob_router' => S_OOB_ROUTER, 'date_hw_buy' => S_DATE_HW_BUY, 'date_hw_install' => S_DATE_HW_INSTALL, 'date_hw_expiry' => S_DATE_HW_EXPIRY, 'date_hw_decomm' => S_DATE_HW_DECOMM, 'site_street_1' => S_SITE_STREET_1, 'site_street_2' => S_SITE_STREET_2, 'site_street_3' => S_SITE_STREET_3, 'site_city' => S_SITE_CITY, 'site_state' => S_SITE_STATE, 'site_country' => S_SITE_COUNTRY, 'site_zip' => S_SITE_ZIP, 'site_rack' => S_SITE_RACK, 'site_notes' => S_SITE_NOTES, 'poc_1_name' => S_POC_1_NAME, 'poc_1_email' => S_POC_1_EMAIL, 'poc_1_phone_1' => S_POC_1_PHONE_1, 'poc_1_phone_2' => S_POC_1_PHONE_2, 'poc_1_cell' => S_POC_1_CELL, 'poc_1_screen' => S_POC_1_SCREEN, 'poc_1_notes' => S_POC_1_NOTES, 'poc_2_name' => S_POC_2_NAME, 'poc_2_email' => S_POC_2_EMAIL, 'poc_2_phone_1' => S_POC_2_PHONE_1, 'poc_2_phone_2' => S_POC_2_PHONE_2, 'poc_2_cell' => S_POC_2_CELL, 'poc_2_screen' => S_POC_2_SCREEN, 'poc_2_notes' => S_POC_2_NOTES);
    foreach ($ext_profiles_fields as $field => $caption) {
        if (!isset($ext_host_profiles[$field])) {
            $ext_host_profiles[$field] = '';
        }
    }
    $clear_templates = array_intersect($clear_templates, array_keys($original_templates));
    $clear_templates = array_diff($clear_templates, array_keys($templates));
    natcasesort($templates);
    $frmHost = new CForm('hosts.php', 'post');
    $frmHost->setName('web.hosts.host.php.');
    //		$frmHost->setHelp('web.hosts.host.php');
    //		$frmHost->addVar('config',get_request('config',0));
    $frmHost->addVar('form', get_request('form', 1));
    $from_rfr = get_request('form_refresh', 0);
    $frmHost->addVar('form_refresh', $from_rfr + 1);
    $frmHost->addVar('clear_templates', $clear_templates);
    // HOST WIDGET {
    $host_tbl = new CTable('', 'tablestripped');
    $host_tbl->setOddRowClass('form_odd_row');
    $host_tbl->setEvenRowClass('form_even_row');
    if ($_REQUEST['hostid'] > 0) {
        $frmHost->addVar('hostid', $_REQUEST['hostid']);
    }
    if ($_REQUEST['groupid'] > 0) {
        $frmHost->addVar('groupid', $_REQUEST['groupid']);
    }
    $host_tbl->addRow(array(S_NAME, new CTextBox('host', $host, 54)));
    $grp_tb = new CTweenBox($frmHost, 'groups', $host_groups, 10);
    $all_groups = CHostGroup::get(array('editable' => 1, 'extendoutput' => 1));
    order_result($all_groups, 'name');
    foreach ($all_groups as $group) {
        $grp_tb->addItem($group['groupid'], $group['name']);
    }
    $host_tbl->addRow(array(S_GROUPS, $grp_tb->get(S_IN_GROUPS, S_OTHER_GROUPS)));
    $host_tbl->addRow(array(S_NEW_GROUP, new CTextBox('newgroup', $newgroup)));
    // onchange does not work on some browsers: MacOS, KDE browser
    $host_tbl->addRow(array(S_DNS_NAME, new CTextBox('dns', $dns, '40')));
    if (defined('ZBX_HAVE_IPV6')) {
        $host_tbl->addRow(array(S_IP_ADDRESS, new CTextBox('ip', $ip, '39')));
    } else {
        $host_tbl->addRow(array(S_IP_ADDRESS, new CTextBox('ip', $ip, '15')));
    }
    $cmbConnectBy = new CComboBox('useip', $useip);
    $cmbConnectBy->addItem(0, S_DNS_NAME);
    $cmbConnectBy->addItem(1, S_IP_ADDRESS);
    $host_tbl->addRow(array(S_CONNECT_TO, $cmbConnectBy));
    $host_tbl->addRow(array(S_AGENT_PORT, new CNumericBox('port', $port, 5)));
    //Proxy
    $cmbProxy = new CComboBox('proxy_hostid', $proxy_hostid);
    $cmbProxy->addItem(0, S_NO_PROXY);
    $options = array('extendoutput' => 1);
    $db_proxies = CProxy::get($options);
    order_result($db_proxies, 'host');
    foreach ($db_proxies as $proxy) {
        $cmbProxy->addItem($proxy['proxyid'], $proxy['host']);
    }
    $host_tbl->addRow(array(S_MONITORED_BY_PROXY, $cmbProxy));
    //----------
    $cmbStatus = new CComboBox('status', $status);
    $cmbStatus->addItem(HOST_STATUS_MONITORED, S_MONITORED);
    $cmbStatus->addItem(HOST_STATUS_NOT_MONITORED, S_NOT_MONITORED);
    $host_tbl->addRow(array(S_STATUS, $cmbStatus));
    $host_tbl->addRow(array(S_USEIPMI, new CCheckBox('useipmi', $useipmi, 'submit()')));
    if ($useipmi == 'yes') {
        $host_tbl->addRow(array(S_IPMI_IP_ADDRESS, new CTextBox('ipmi_ip', $ipmi_ip, defined('ZBX_HAVE_IPV6') ? 39 : 15)));
        $host_tbl->addRow(array(S_IPMI_PORT, new CNumericBox('ipmi_port', $ipmi_port, 5)));
        $cmbIPMIAuthtype = new CComboBox('ipmi_authtype', $ipmi_authtype);
        $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_DEFAULT, S_AUTHTYPE_DEFAULT);
        $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_NONE, S_AUTHTYPE_NONE);
        $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_MD2, S_AUTHTYPE_MD2);
        $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_MD5, S_AUTHTYPE_MD5);
        $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_STRAIGHT, S_AUTHTYPE_STRAIGHT);
        $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_OEM, S_AUTHTYPE_OEM);
        $cmbIPMIAuthtype->addItem(IPMI_AUTHTYPE_RMCP_PLUS, S_AUTHTYPE_RMCP_PLUS);
        $host_tbl->addRow(array(S_IPMI_AUTHTYPE, $cmbIPMIAuthtype));
        $cmbIPMIPrivilege = new CComboBox('ipmi_privilege', $ipmi_privilege);
        $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_CALLBACK, S_PRIVILEGE_CALLBACK);
        $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_USER, S_PRIVILEGE_USER);
        $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_OPERATOR, S_PRIVILEGE_OPERATOR);
        $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_ADMIN, S_PRIVILEGE_ADMIN);
        $cmbIPMIPrivilege->addItem(IPMI_PRIVILEGE_OEM, S_PRIVILEGE_OEM);
        $host_tbl->addRow(array(S_IPMI_PRIVILEGE, $cmbIPMIPrivilege));
        $host_tbl->addRow(array(S_IPMI_USERNAME, new CTextBox('ipmi_username', $ipmi_username, 16)));
        $host_tbl->addRow(array(S_IPMI_PASSWORD, new CTextBox('ipmi_password', $ipmi_password, 20)));
    } else {
        $frmHost->addVar('ipmi_ip', $ipmi_ip);
        $frmHost->addVar('ipmi_port', $ipmi_port);
        $frmHost->addVar('ipmi_authtype', $ipmi_authtype);
        $frmHost->addVar('ipmi_privilege', $ipmi_privilege);
        $frmHost->addVar('ipmi_username', $ipmi_username);
        $frmHost->addVar('ipmi_password', $ipmi_password);
    }
    if ($_REQUEST['form'] == 'full_clone') {
        // Host items
        $options = array('inherited' => 0, 'hostids' => $_REQUEST['hostid'], 'output' => API_OUTPUT_EXTEND, 'webitems' => 1);
        $host_items = CItem::get($options);
        if (!empty($host_items)) {
            $items_lbx = new CListBox('items', null, 8);
            $items_lbx->setAttribute('disabled', 'disabled');
            order_result($host_items, 'description');
            foreach ($host_items as $hitem) {
                $items_lbx->addItem($hitem['itemid'], item_description($hitem));
            }
            $host_tbl->addRow(array(S_ITEMS, $items_lbx));
        }
        // Host triggers
        $options = array('inherited' => 0, 'hostids' => $_REQUEST['hostid'], 'output' => API_OUTPUT_EXTEND, 'expandDescription' => true);
        $host_triggers = CTrigger::get($options);
        if (!empty($host_triggers)) {
            $trig_lbx = new CListBox('triggers', null, 8);
            $trig_lbx->setAttribute('disabled', 'disabled');
            order_result($host_triggers, 'description');
            foreach ($host_triggers as $htrigger) {
                $trig_lbx->addItem($htrigger['triggerid'], $htrigger['description']);
            }
            $host_tbl->addRow(array(S_TRIGGERS, $trig_lbx));
        }
        // Host graphs
        $options = array('inherited' => 0, 'hostids' => $_REQUEST['hostid'], 'select_hosts' => API_OUTPUT_REFER, 'output' => API_OUTPUT_EXTEND);
        $host_graphs = CGraph::get($options);
        if (!empty($host_graphs)) {
            $graphs_lbx = new CListBox('graphs', null, 8);
            $graphs_lbx->setAttribute('disabled', 'disabled');
            order_result($host_graphs, 'name');
            foreach ($host_graphs as $hgraph) {
                if (count($hgraph['hosts']) > 1) {
                    continue;
                }
                $graphs_lbx->addItem($hgraph['graphid'], $hgraph['name']);
            }
            if ($graphs_lbx->ItemsCount() > 1) {
                $host_tbl->addRow(array(S_GRAPHS, $graphs_lbx));
            }
        }
    }
    $host_footer = array();
    $host_footer[] = new CButton('save', S_SAVE);
    if ($_REQUEST['hostid'] > 0 && $_REQUEST['form'] != 'full_clone') {
        array_push($host_footer, SPACE, new CButton('clone', S_CLONE), SPACE, new CButton('full_clone', S_FULL_CLONE), SPACE, new CButtonDelete(S_DELETE_SELECTED_HOST_Q, url_param('form') . url_param('hostid') . url_param('groupid')));
    }
    array_push($host_footer, SPACE, new CButtonCancel(url_param('groupid')));
    $host_footer = new CCol($host_footer);
    $host_footer->setColSpan(2);
    $host_tbl->setFooter($host_footer);
    $host_wdgt = new CWidget();
    $host_wdgt->setClass('header');
    $host_wdgt->addHeader($frm_title);
    $host_wdgt->addItem($host_tbl);
    // } HOST WIDGET
    // TEMPLATES{
    $template_tbl = new CTableInfo(S_NO_TEMPLATES_LINKED, 'tablestripped');
    $template_tbl->setOddRowClass('form_odd_row');
    $template_tbl->setEvenRowClass('form_even_row');
    foreach ($templates as $id => $temp_name) {
        $frmHost->addVar('templates[' . $id . ']', $temp_name);
        $template_tbl->addRow(new CCol(array(new CCheckBox('templates_rem[' . $id . ']', 'no', null, $id), $temp_name)));
    }
    $footer = new CCol(array(new CButton('add_template', S_ADD, "return PopUp('popup.php?dstfrm=" . $frmHost->getName() . "&dstfld1=new_template&srctbl=templates&srcfld1=hostid&srcfld2=host" . url_param($templates, false, 'existed_templates') . "',450,450)", 'T'), SPACE, new CButton('unlink', S_UNLINK), SPACE, new CButton('unlink_and_clear', S_UNLINK_AND_CLEAR)));
    //$footer->setColSpan(2);
    $template_tbl->setFooter($footer);
    $template_wdgt = new CWidget();
    $template_wdgt->setClass('header');
    $template_wdgt->addHeader(S_LINKED_TEMPLATES);
    $template_wdgt->addItem($template_tbl);
    // } TEMPLATES
    // MACROS WIDGET {
    $macros_wdgt = get_macros_widget($_REQUEST['hostid']);
    // } MACROS WIDGET
    // PROFILE WIDGET {
    $profile_tbl = new CTable('', 'tablestripped');
    $profile_tbl->setOddRowClass('form_odd_row');
    $profile_tbl->setEvenRowClass('form_even_row');
    $profile_tbl->addRow(array(S_USE_PROFILE, new CCheckBox('useprofile', $useprofile, 'submit()')));
    if ($useprofile == 'yes') {
        $profile_tbl->addRow(array(S_DEVICE_TYPE, new CTextBox('devicetype', $devicetype, 61)));
        $profile_tbl->addRow(array(S_NAME, new CTextBox('name', $name, 61)));
        $profile_tbl->addRow(array(S_OS, new CTextBox('os', $os, 61)));
        $profile_tbl->addRow(array(S_SERIALNO, new CTextBox('serialno', $serialno, 61)));
        $profile_tbl->addRow(array(S_TAG, new CTextBox('tag', $tag, 61)));
        $profile_tbl->addRow(array(S_MACADDRESS, new CTextBox('macaddress', $macaddress, 61)));
        $profile_tbl->addRow(array(S_HARDWARE, new CTextArea('hardware', $hardware, 60, 4)));
        $profile_tbl->addRow(array(S_SOFTWARE, new CTextArea('software', $software, 60, 4)));
        $profile_tbl->addRow(array(S_CONTACT, new CTextArea('contact', $contact, 60, 4)));
        $profile_tbl->addRow(array(S_LOCATION, new CTextArea('location', $location, 60, 4)));
        $profile_tbl->addRow(array(S_NOTES, new CTextArea('notes', $notes, 60, 4)));
    } else {
        $frmHost->addVar('devicetype', $devicetype);
        $frmHost->addVar('name', $name);
        $frmHost->addVar('os', $os);
        $frmHost->addVar('serialno', $serialno);
        $frmHost->addVar('tag', $tag);
        $frmHost->addVar('macaddress', $macaddress);
        $frmHost->addVar('hardware', $hardware);
        $frmHost->addVar('software', $software);
        $frmHost->addVar('contact', $contact);
        $frmHost->addVar('location', $location);
        $frmHost->addVar('notes', $notes);
    }
    $profile_wdgt = new CWidget();
    $profile_wdgt->setClass('header');
    $profile_wdgt->addHeader(S_PROFILE);
    $profile_wdgt->addItem($profile_tbl);
    // } PROFILE WIDGET
    // EXT PROFILE WIDGET {
    $ext_profile_tbl = new CTable('', 'tablestripped');
    $ext_profile_tbl->setOddRowClass('form_odd_row');
    $ext_profile_tbl->setEvenRowClass('form_even_row');
    $ext_profile_tbl->addRow(array(S_USE_EXTENDED_PROFILE, new CCheckBox('useprofile_ext', $useprofile_ext, 'submit()', 'yes')));
    foreach ($ext_profiles_fields as $prof_field => $caption) {
        if ($useprofile_ext == 'yes') {
            $ext_profile_tbl->addRow(array($caption, new CTextBox('ext_host_profiles[' . $prof_field . ']', $ext_host_profiles[$prof_field], 40)));
        } else {
            $frmHost->addVar('ext_host_profiles[' . $prof_field . ']', $ext_host_profiles[$prof_field]);
        }
    }
    $ext_profile_wdgt = new CWidget();
    $ext_profile_wdgt->setClass('header');
    $ext_profile_wdgt->addHeader(S_EXTENDED_HOST_PROFILE);
    $ext_profile_wdgt->addItem($ext_profile_tbl);
    // } EXT PROFILE WIDGET
    $left_table = new CTable();
    $left_table->setCellPadding(4);
    $left_table->setCellSpacing(4);
    $left_table->addRow($host_wdgt);
    $right_table = new CTable();
    $right_table->setCellPadding(4);
    $right_table->setCellSpacing(4);
    $right_table->addRow($template_wdgt);
    $right_table->addRow($macros_wdgt);
    $right_table->addRow($profile_wdgt);
    $right_table->addRow($ext_profile_wdgt);
    $td_l = new CCol($left_table);
    $td_l->setAttribute('valign', 'top');
    $td_r = new CCol($right_table);
    $td_r->setAttribute('valign', 'top');
    $outer_table = new CTable();
    $outer_table->addRow(array($td_l, $td_r));
    $frmHost->addItem($outer_table);
    return $frmHost;
}
Esempio n. 14
0
 public static function parseMain($rules)
 {
     $triggers_for_dependencies = array();
     try {
         if (isset($rules['host']['exist']) || isset($rules['host']['missed'])) {
             $xpath = new DOMXPath(self::$xml);
             $hosts = $xpath->query('hosts/host');
             foreach ($hosts as $hnum => $host) {
                 $host_db = self::mapXML2arr($host, XML_TAG_HOST);
                 if (!isset($host_db['status'])) {
                     $host_db['status'] = HOST_STATUS_TEMPLATE;
                 }
                 $current_host = $host_db['status'] == HOST_STATUS_TEMPLATE ? CTemplate::exists($host_db) : CHost::exists($host_db);
                 if (!$current_host && !isset($rules['host']['missed'])) {
                     info('Host [' . $host_db['host'] . '] skipped - user rule');
                     continue;
                     // break if update nonexist
                 }
                 if ($current_host && !isset($rules['host']['exist'])) {
                     info('Host [' . $host_db['host'] . '] skipped - user rule');
                     continue;
                     // break if not update exist
                 }
                 if (isset($host_db['proxy_hostid'])) {
                     $proxy_exists = CProxy::get(array('proxyids' => $host_db['proxy_hostid']));
                     if (empty($proxy_exists)) {
                         $host_db['proxy_hostid'] = 0;
                     }
                 }
                 if ($current_host) {
                     $options = array('filter' => array('host' => $host_db['host']), 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                     if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                         $current_host = CTemplate::get($options);
                     } else {
                         $current_host = CHost::get($options);
                     }
                     if (empty($current_host)) {
                         throw new APIException(1, 'No permission for host [' . $host_db['host'] . ']');
                     } else {
                         $current_host = reset($current_host);
                     }
                 }
                 // HOST GROUPS {{{
                 $groups = $xpath->query('groups/group', $host);
                 $host_db['groups'] = array();
                 $groups_to_parse = array();
                 foreach ($groups as $gnum => $group) {
                     $groups_to_parse[] = array('name' => $group->nodeValue);
                 }
                 if (empty($groups_to_parse)) {
                     $groups_to_parse[] = array('name' => ZBX_DEFAULT_IMPORT_HOST_GROUP);
                 }
                 foreach ($groups_to_parse as $group) {
                     $current_group = CHostGroup::exists($group);
                     if ($current_group) {
                         $options = array('filter' => $group, 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                         $current_group = CHostGroup::get($options);
                         if (empty($current_group)) {
                             throw new APIException(1, 'No permissions for group ' . $group['name']);
                         }
                         $host_db['groups'][] = reset($current_group);
                     } else {
                         $result = CHostGroup::create($group);
                         if (!$result) {
                             throw new APIException(1, CHostGroup::resetErrors());
                         }
                         $options = array('groupids' => $result['groupids'], 'output' => API_OUTPUT_EXTEND);
                         $new_group = CHostgroup::get($options);
                         $host_db['groups'][] = reset($new_group);
                     }
                 }
                 // }}} HOST GROUPS
                 // MACROS
                 $macros = $xpath->query('macros/macro', $host);
                 $host_db['macros'] = array();
                 if ($macros->length > 0) {
                     foreach ($macros as $macro) {
                         $host_db['macros'][] = self::mapXML2arr($macro, XML_TAG_MACRO);
                     }
                 }
                 // }}} MACROS
                 // TEMPLATES {{{
                 if (isset($rules['template']['exist'])) {
                     $templates = $xpath->query('templates/template', $host);
                     $host_db['templates'] = array();
                     foreach ($templates as $tnum => $template) {
                         $options = array('filter' => array('host' => $template->nodeValue), 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                         $current_template = CTemplate::get($options);
                         if (empty($current_template)) {
                             throw new APIException(1, 'No permission for Template [' . $template->nodeValue . ']');
                         }
                         $current_template = reset($current_template);
                         if (!$current_template && !isset($rules['template']['missed'])) {
                             info('Template [' . $template->nodeValue . '] skipped - user rule');
                             continue;
                             // break if update nonexist
                         }
                         if ($current_template && !isset($rules['template']['exist'])) {
                             info('Template [' . $template->nodeValue . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         $host_db['templates'][] = $current_template;
                     }
                 }
                 // }}} TEMPLATES
                 // HOSTS
                 if ($current_host && isset($rules['host']['exist'])) {
                     if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                         $host_db['templateid'] = $current_host['hostid'];
                         $result = CTemplate::update($host_db);
                         if (!$result) {
                             throw new APIException(1, CTemplate::resetErrors());
                         }
                         $options = array('templateids' => $result['templateids'], 'output' => API_OUTPUT_EXTEND);
                         $current_host = CTemplate::get($options);
                     } else {
                         $host_db['hostid'] = $current_host['hostid'];
                         $result = CHost::update($host_db);
                         if (!$result) {
                             throw new APIException(1, CHost::resetErrors());
                         }
                         $options = array('hostids' => $result['hostids'], 'output' => API_OUTPUT_EXTEND);
                         $current_host = CHost::get($options);
                     }
                     if ($current_host === false) {
                         throw new APIException(1, $host_db['status'] == HOST_STATUS_TEMPLATE ? CTemplate::resetErrors() : CHost::resetErrors());
                     }
                 }
                 if (!$current_host && isset($rules['host']['missed'])) {
                     if ($host_db['status'] == HOST_STATUS_TEMPLATE) {
                         $result = CTemplate::create($host_db);
                         if (!$result) {
                             throw new APIException(1, CTemplate::resetErrors());
                         }
                         $options = array('templateids' => $result['templateids'], 'output' => API_OUTPUT_EXTEND);
                         $current_host = CTemplate::get($options);
                     } else {
                         $result = CHost::create($host_db);
                         if (!$result) {
                             throw new APIException(1, CHost::resetErrors());
                         }
                         $options = array('hostids' => $result['hostids'], 'output' => API_OUTPUT_EXTEND);
                         $current_host = CHost::get($options);
                     }
                 }
                 $current_host = reset($current_host);
                 // HOST PROFILES {{{
                 $profile_node = $xpath->query('host_profile/*', $host);
                 if ($profile_node->length > 0) {
                     $profile = array();
                     foreach ($profile_node as $num => $field) {
                         $profile[$field->nodeName] = $field->nodeValue;
                     }
                     delete_host_profile($current_host['hostid']);
                     add_host_profile($current_host['hostid'], $profile['devicetype'], $profile['name'], $profile['os'], $profile['serialno'], $profile['tag'], $profile['macaddress'], $profile['hardware'], $profile['software'], $profile['contact'], $profile['location'], $profile['notes']);
                 }
                 $profile_ext_node = $xpath->query('host_profiles_ext/*', $host);
                 if ($profile_ext_node->length > 0) {
                     $profile_ext = array();
                     foreach ($profile_ext_node as $num => $field) {
                         $profile_ext[$field->nodeName] = $field->nodeValue;
                     }
                     delete_host_profile_ext($current_host['hostid']);
                     add_host_profile_ext($current_host['hostid'], $profile_ext);
                 }
                 // }}} HOST PROFILES
                 // ITEMS {{{
                 if (isset($rules['item']['exist']) || isset($rules['item']['missed'])) {
                     $items = $xpath->query('items/item', $host);
                     foreach ($items as $inum => $item) {
                         $item_db = self::mapXML2arr($item, XML_TAG_ITEM);
                         $item_db['hostid'] = $current_host['hostid'];
                         if ($current_item = CItem::exists($item_db)) {
                             $options = array('filter' => array('hostid' => $item_db['hostid'], 'key_' => $item_db['key_']), 'webitems' => 1, 'output' => API_OUTPUT_EXTEND, 'editable' => 1);
                             $current_item = CItem::get($options);
                             if (empty($current_item)) {
                                 throw new APIException(1, 'No permission for Item [' . $item_db['key_'] . ']');
                             }
                             $current_item = reset($current_item);
                         }
                         if (!$current_item && !isset($rules['item']['missed'])) {
                             info('Item [' . $item_db['key_'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         if ($current_item && !isset($rules['item']['exist'])) {
                             info('Item [' . $item_db['key_'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         // ITEM APPLICATIONS {{{
                         $applications = $xpath->query('applications/application', $item);
                         $item_applications = array();
                         $applications_to_add = array();
                         foreach ($applications as $application) {
                             $application_db = array('name' => $application->nodeValue, 'hostid' => $current_host['hostid']);
                             if ($current_application = CApplication::exists($application_db)) {
                                 $current_application = CApplication::get(array('filter' => $application_db, 'output' => API_OUTPUT_EXTEND));
                                 if (empty($current_application)) {
                                     throw new APIException(1, 'No permission for Application [' . $application_db['name'] . ']');
                                 }
                             }
                             if ($current_application) {
                                 $item_applications = array_merge($item_applications, $current_application);
                             } else {
                                 $applications_to_add[] = $application_db;
                             }
                         }
                         if (!empty($applications_to_add)) {
                             $result = CApplication::create($applications_to_add);
                             if (!$result) {
                                 throw new APIException(1, CApplication::resetErrors());
                             }
                             $options = array('applicationids' => $result['applicationids'], 'output' => API_OUTPUT_EXTEND);
                             $new_applications = CApplication::get($options);
                             $item_applications = array_merge($item_applications, $new_applications);
                         }
                         // }}} ITEM APPLICATIONS
                         if ($current_item && isset($rules['item']['exist'])) {
                             $item_db['itemid'] = $current_item['itemid'];
                             $result = CItem::update($item_db);
                             if (!$result) {
                                 throw new APIException(1, CItem::resetErrors());
                             }
                             $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND);
                             $current_item = CItem::get($options);
                         }
                         if (!$current_item && isset($rules['item']['missed'])) {
                             $result = CItem::create($item_db);
                             if (!$result) {
                                 throw new APIException(1, CItem::resetErrors());
                             }
                             $options = array('itemids' => $result['itemids'], 'webitems' => 1, 'output' => API_OUTPUT_EXTEND);
                             $current_item = CItem::get($options);
                         }
                         $r = CApplication::massAdd(array('applications' => $item_applications, 'items' => $current_item));
                         if ($r === false) {
                             throw new APIException(1, CApplication::resetErrors());
                         }
                     }
                 }
                 // }}} ITEMS
                 // TRIGGERS {{{
                 if (isset($rules['trigger']['exist']) || isset($rules['trigger']['missed'])) {
                     $triggers = $xpath->query('triggers/trigger', $host);
                     $triggers_to_add = array();
                     $triggers_to_upd = array();
                     foreach ($triggers as $trigger) {
                         $trigger_db = self::mapXML2arr($trigger, XML_TAG_TRIGGER);
                         $trigger_db['expression'] = str_replace('{{HOSTNAME}:', '{' . $host_db['host'] . ':', $trigger_db['expression']);
                         $trigger_db['hostid'] = $current_host['hostid'];
                         if ($current_trigger = CTrigger::exists($trigger_db)) {
                             $ctriggers = CTrigger::get(array('filter' => array('description' => $trigger_db['description']), 'hostids' => $current_host['hostid'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1));
                             $current_trigger = false;
                             foreach ($ctriggers as $tnum => $ct) {
                                 $tmp_exp = explode_exp($ct['expression'], false);
                                 if (strcmp($trigger_db['expression'], $tmp_exp) == 0) {
                                     $current_trigger = $ct;
                                     break;
                                 }
                             }
                             if (!$current_trigger) {
                                 throw new APIException(1, 'No permission for Trigger [' . $trigger_db['description'] . ']');
                             }
                         }
                         if (!$current_trigger && !isset($rules['trigger']['missed'])) {
                             info('Trigger [' . $trigger_db['description'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         if ($current_trigger && !isset($rules['trigger']['exist'])) {
                             info('Trigger [' . $trigger_db['description'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         if ($current_trigger && isset($rules['trigger']['exist'])) {
                             $trigger_db['triggerid'] = $current_trigger['triggerid'];
                             $triggers_to_upd[] = $trigger_db;
                         }
                         if (!$current_trigger && isset($rules['trigger']['missed'])) {
                             $triggers_to_add[] = $trigger_db;
                         }
                     }
                     if (!empty($triggers_to_upd)) {
                         $result = CTrigger::update($triggers_to_upd);
                         if (!$result) {
                             throw new APIException(1, CTrigger::resetErrors());
                         }
                         $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND);
                         $r = CTrigger::get($options);
                         $triggers_for_dependencies = array_merge($triggers_for_dependencies, $r);
                     }
                     if (!empty($triggers_to_add)) {
                         $result = CTrigger::create($triggers_to_add);
                         if (!$result) {
                             throw new APIException(1, CTrigger::resetErrors());
                         }
                         $options = array('triggerids' => $result['triggerids'], 'output' => API_OUTPUT_EXTEND);
                         $r = CTrigger::get($options);
                         $triggers_for_dependencies = array_merge($triggers_for_dependencies, $r);
                     }
                 }
                 // }}} TRIGGERS
                 // GRAPHS {{{
                 if (isset($rules['graph']['exist']) || isset($rules['graph']['missed'])) {
                     $graphs = $xpath->query('graphs/graph', $host);
                     $graphs_to_add = array();
                     $graphs_to_upd = array();
                     foreach ($graphs as $gnum => $graph) {
                         // GRAPH ITEMS {{{
                         $gitems = $xpath->query('graph_elements/graph_element', $graph);
                         $graph_hostids = array();
                         $graph_items = array();
                         foreach ($gitems as $ginum => $gitem) {
                             $gitem_db = self::mapXML2arr($gitem, XML_TAG_GRAPH_ELEMENT);
                             $data = explode(':', $gitem_db['host_key_']);
                             $gitem_host = array_shift($data);
                             $gitem_db['host'] = $gitem_host == '{HOSTNAME}' ? $host_db['host'] : $gitem_host;
                             $gitem_db['key_'] = implode(':', $data);
                             if ($current_item = CItem::exists($gitem_db)) {
                                 $current_item = CItem::get(array('filter' => array('key_' => $gitem_db['key_']), 'webitems' => 1, 'host' => $gitem_db['host'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1));
                                 if (empty($current_item)) {
                                     throw new APIException(1, 'No permission for Item [' . $gitem_db['key_'] . ']');
                                 }
                                 $current_item = reset($current_item);
                                 $graph_hostids[] = $current_item['hostid'];
                                 $gitem_db['itemid'] = $current_item['itemid'];
                                 $graph_items[] = $gitem_db;
                             } else {
                                 throw new APIException(1, 'Item [' . $gitem_db['host_key_'] . '] does not exists');
                             }
                         }
                         // }}} GRAPH ITEMS
                         $graph_db = self::mapXML2arr($graph, XML_TAG_GRAPH);
                         $graph_db['hostids'] = $graph_hostids;
                         if ($current_graph = CGraph::exists($graph_db)) {
                             $current_graph = CGraph::get(array('filter' => array('name' => $graph_db['name']), 'hostids' => $graph_db['hostids'], 'output' => API_OUTPUT_EXTEND, 'editable' => 1));
                             if (empty($current_graph)) {
                                 throw new APIException(1, 'No permission for Graph [' . $graph_db['name'] . ']');
                             }
                             $current_graph = reset($current_graph);
                         }
                         if (!$current_graph && !isset($rules['graph']['missed'])) {
                             info('Graph [' . $graph_db['name'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         if ($current_graph && !isset($rules['graph']['exist'])) {
                             info('Graph [' . $graph_db['name'] . '] skipped - user rule');
                             continue;
                             // break if not update exist
                         }
                         if ($graph_db['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
                             $item_data = explode(':', $graph_db['ymin_item_key'], 2);
                             if (count($item_data) < 2) {
                                 throw new APIException(1, 'Incorrect y min item for graph [' . $graph_db['name'] . ']');
                             }
                             if (!($item = get_item_by_key($item_data[1], $item_data[0]))) {
                                 throw new APIException(1, 'Missing item [' . $graph_db['ymin_item_key'] . '] for host [' . $host_db['host'] . ']');
                             }
                             $graph_db['ymin_itemid'] = $item['itemid'];
                         }
                         if ($graph_db['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) {
                             $item_data = explode(':', $graph_db['ymax_item_key'], 2);
                             if (count($item_data) < 2) {
                                 throw new APIException(1, 'Incorrect y max item for graph [' . $graph_db['name'] . ']');
                             }
                             if (!($item = get_item_by_key($item_data[1], $item_data[0]))) {
                                 throw new APIException(1, 'Missing item [' . $graph_db['ymax_item_key'] . '] for host [' . $host_db['host'] . ']');
                             }
                             $graph_db['ymax_itemid'] = $item['itemid'];
                         }
                         $graph_db['gitems'] = $graph_items;
                         if ($current_graph) {
                             $graph_db['graphid'] = $current_graph['graphid'];
                             $graphs_to_upd[] = $graph_db;
                         } else {
                             $graphs_to_add[] = $graph_db;
                         }
                     }
                     if (!empty($graphs_to_add)) {
                         $r = CGraph::create($graphs_to_add);
                         if ($r === false) {
                             throw new APIException(1, CGraph::resetErrors());
                         }
                     }
                     if (!empty($graphs_to_upd)) {
                         $r = CGraph::update($graphs_to_upd);
                         if ($r === false) {
                             throw new APIException(1, CGraph::resetErrors());
                         }
                     }
                 }
             }
             // DEPENDENCIES
             $dependencies = $xpath->query('dependencies/dependency');
             if ($dependencies->length > 0) {
                 $triggers_for_dependencies = zbx_objectValues($triggers_for_dependencies, 'triggerid');
                 $triggers_for_dependencies = array_flip($triggers_for_dependencies);
                 foreach ($dependencies as $dependency) {
                     $triggers_to_add_dep = array();
                     $trigger_description = $dependency->getAttribute('description');
                     $current_triggerid = get_trigger_by_description($trigger_description);
                     // sdi('<b><u>Trigger Description: </u></b>'.$trigger_description.' | <b>Current_triggerid: </b>'. $current_triggerid['triggerid']);
                     if ($current_triggerid && isset($triggers_for_dependencies[$current_triggerid['triggerid']])) {
                         $depends_on_list = $xpath->query('depends', $dependency);
                         foreach ($depends_on_list as $depends_on) {
                             $depends_triggerid = get_trigger_by_description($depends_on->nodeValue);
                             // sdi('<b>depends on description: </b>'.$depends_on->nodeValue.' | <b>depends_triggerid: </b>'. $depends_triggerid['triggerid']);
                             if ($depends_triggerid['triggerid']) {
                                 $triggers_to_add_dep[] = $depends_triggerid['triggerid'];
                             }
                         }
                         $r = update_trigger($current_triggerid['triggerid'], null, $current_triggerid['description'], null, null, null, null, null, $triggers_to_add_dep, null);
                         if ($r === false) {
                             throw new APIException();
                         }
                     }
                 }
             }
         }
         return true;
     } catch (APIException $e) {
         error($e->getErrors());
         return false;
     }
 }
Esempio n. 15
0
 function DrawGraph($file = "")
 {
     $this->im = imagecreatetruecolor($this->graph_width, $this->graph_height);
     $this->axis_positions = array(0, 0, 0, 0);
     $this->axis_xscalevisible = 0;
     $this->axis_yscalevisible = 0;
     $this->axis_gridlines = 0;
     CGraph::DrawGraph();
     if ($this->pie_total == 0) {
         foreach ($this->data as $name => $value) {
             $this->pie_total += $value;
         }
     }
     // deepness
     for ($i = $this->pie_deepness; $i > 0; $i--) {
         $offset = 0;
         $p = 0;
         foreach ($this->data as $n => $value) {
             if (!$this->im_pie_deepnesscolor[$p]) {
                 $this->__AllocateColor("im_pie_deepnesscolor", $this->pie_deepnesscolor[$p], 0, $p);
             }
             $from = round($this->pie_startoffset - $offset * 360 / $this->pie_total);
             $to = round($this->pie_startoffset - ($value + $offset) * 360 / $this->pie_total);
             if ($from < 0) {
                 $from += 360;
             }
             if ($to < 0) {
                 $to += 360;
             }
             imagefilledarc($this->im, round($this->graph_width / 2), round($this->graph_height / 2) + $i, $this->graph_areawidth, $this->graph_areaheight, $to, $from, $this->im_pie_deepnesscolor[$p], IMG_ARC_PIE);
             $offset += $value;
             $p++;
         }
     }
     $offset = 0;
     $p = 0;
     foreach ($this->data as $n => $value) {
         $this->__AllocateColor("im_pie_color", $this->pie_color[$p], 0, $p);
         $from = round($this->pie_startoffset - $offset * 360 / $this->pie_total);
         $to = round($this->pie_startoffset - ($value + $offset) * 360 / $this->pie_total);
         if ($from < 0) {
             $from += 360;
         }
         if ($to < 0) {
             $to += 360;
         }
         imagefilledarc($this->im, round($this->graph_width / 2), round($this->graph_height / 2), $this->graph_areawidth, $this->graph_areaheight, $to, $from, $this->im_pie_color[$p], IMG_ARC_PIE);
         $offset += $value;
         $p++;
     }
     CGraph::DrawGraph2();
     if (strlen($file)) {
         $ret = imagepng($this->im, $file);
     } else {
         header("Content-Type: image/png");
         // thanks to Marcin G. :)
         imagepng($this->im);
         $ret = true;
     }
     imagedestroy($this->im);
     return $ret;
 }
Esempio n. 16
0
 protected static function checkItems($screenitems)
 {
     $hostgroups = array();
     $hosts = array();
     $graphs = array();
     $items = array();
     $maps = array();
     $screens = array();
     $resources = array(SCREEN_RESOURCE_GRAPH, SCREEN_RESOURCE_SIMPLE_GRAPH, SCREEN_RESOURCE_PLAIN_TEXT, SCREEN_RESOURCE_MAP, SCREEN_RESOURCE_SCREEN, SCREEN_RESOURCE_TRIGGERS_OVERVIEW, SCREEN_RESOURCE_DATA_OVERVIEW);
     foreach ($screenitems as $item) {
         if (isset($item['resourcetype']) && !isset($item['resourceid']) || !isset($item['resourcetype']) && isset($item['resourceid'])) {
             self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSION);
         }
         if (isset($item['resourceid']) && $item['resourceid'] == 0) {
             if (uint_in_array($item['resourcetype'], $resources)) {
                 throw new Exception(S_INCORRECT_RESOURCE_PROVIDED_FOR_SCREEN_ITEM);
             } else {
                 continue;
             }
         }
         switch ($item['resourcetype']) {
             case SCREEN_RESOURCE_HOSTS_INFO:
             case SCREEN_RESOURCE_TRIGGERS_INFO:
             case SCREEN_RESOURCE_TRIGGERS_OVERVIEW:
             case SCREEN_RESOURCE_DATA_OVERVIEW:
             case SCREEN_RESOURCE_HOSTGROUP_TRIGGERS:
                 $hostgroups[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_HOST_TRIGGERS:
                 $hosts[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_GRAPH:
                 $graphs[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_SIMPLE_GRAPH:
             case SCREEN_RESOURCE_PLAIN_TEXT:
                 $items[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_MAP:
                 $maps[] = $item['resourceid'];
                 break;
             case SCREEN_RESOURCE_SCREEN:
                 $screens[] = $item['resourceid'];
                 break;
         }
     }
     if (!empty($hostgroups)) {
         $result = CHostGroup::get(array('groupids' => $hostgroups, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($hostgroups as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Host group identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($hosts)) {
         $result = CHost::get(array('hostids' => $hosts, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($hosts as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Host identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($graphs)) {
         $result = CGraph::get(array('graphids' => $graphs, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($graphs as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Graph identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($items)) {
         $result = CItem::get(array('itemids' => $items, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1, 'webitems' => 1));
         foreach ($items as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Item identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($maps)) {
         $result = CMap::get(array('sysmapids' => $maps, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($maps as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Map identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
     if (!empty($screens)) {
         $result = self::get(array('screenids' => $screens, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => 1));
         foreach ($screens as $id) {
             if (!isset($result[$id])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, 'Incorrect Screen identity "' . $id . '" provided for Screens item resource');
             }
         }
     }
 }
 /**
 * Get GraphItems data
 *
 * @static
 * @param array $options
 * @return array|boolean
 */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $result = array();
     $sort_columns = array('gitemid');
     // 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('gitems' => 'gi.gitemid'), 'from' => array('graphs_items' => 'graphs_items gi'), 'where' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'graphids' => null, 'itemids' => null, 'type' => null, 'editable' => null, 'nopermissions' => null, 'select_graphs' => null, 'output' => API_OUTPUT_REFER, 'expandData' => null, 'extendoutput' => 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;
     }
     // 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']['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']['igi'] = 'i.itemid=gi.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where'][] = 'r.id=hg.groupid ';
         $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
         $sql_parts['where'][] = 'ug.userid=' . $userid;
         $sql_parts['where'][] = 'r.permission>=' . $permission;
         $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups ugg ' . ' WHERE i.hostid=hgg.hostid ' . ' AND rr.id=hgg.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();
     // 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'] = 'graphs g';
         $sql_parts['where']['gig'] = 'gi.graphid=g.graphid';
         $sql_parts['where'][] = DBcondition('g.graphid', $options['graphids']);
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['itemid'] = 'gi.itemid';
         }
         $sql_parts['where'][] = DBcondition('gi.itemid', $options['itemids']);
     }
     // type
     if (!is_null($options['type'])) {
         $sql_parts['where'][] = 'gi.type=' . $options['type'];
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['gitems'] = 'gi.*';
     }
     // expandData
     if (!is_null($options['expandData'])) {
         $sql_parts['select']['key'] = 'i.key_';
         $sql_parts['select']['hostid'] = 'i.hostid';
         $sql_parts['select']['host'] = 'h.host';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['gii'] = 'gi.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 gi.gitemid) 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'][] = 'gi.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('gi.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('gi.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 'gi.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //------------
     $gitemids = 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('gi.gitemid', $nodeids) . $sql_where . $sql_order;
     //SDI($sql);
     $db_res = DBselect($sql, $sql_limit);
     while ($gitem = DBfetch($db_res)) {
         if (!is_null($options['countOutput'])) {
             $result = $gitem['rowscount'];
         } else {
             $gitemids[$gitem['gitemid']] = $gitem['gitemid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$gitem['gitemid']] = array('gitemid' => $gitem['gitemid']);
             } else {
                 if (!isset($result[$gitem['gitemid']])) {
                     $result[$gitem['gitemid']] = array();
                 }
                 // graphids
                 if (isset($gitem['graphid']) && is_null($options['select_graphs'])) {
                     if (!isset($result[$gitem['gitemid']]['graphs'])) {
                         $result[$gitem['gitemid']]['graphs'] = array();
                     }
                     $result[$gitem['gitemid']]['graphs'][] = array('graphid' => $gitem['graphid']);
                     //						unset($gitem['graphid']);
                 }
                 $result[$gitem['gitemid']] += $gitem;
             }
         }
     }
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // 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'], 'gitemids' => $gitemids, 'preservekeys' => 1);
         $graphs = CGraph::get($obj_params);
         foreach ($graphs as $graphid => $graph) {
             $gitems = $graph['gitems'];
             unset($graph['gitems']);
             foreach ($gitems as $inum => $item) {
                 $result[$gitem['gitemid']]['graphs'][] = $graph;
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Esempio n. 18
0
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
require_once 'include/config.inc.php';
require_once 'include/graphs.inc.php';
$page['file'] = 'chart6.php';
// $page['title']	= "S_CHART";
$page['type'] = PAGE_TYPE_IMAGE;
include_once 'include/page_header.php';
//		VAR			TYPE	OPTIONAL FLAGS	VALIDATION	EXCEPTION
$fields = array('graphid' => array(T_ZBX_INT, O_MAND, P_SYS, DB_ID, null), 'period' => array(T_ZBX_INT, O_OPT, P_NZERO, BETWEEN(ZBX_MIN_PERIOD, ZBX_MAX_PERIOD), null), 'stime' => array(T_ZBX_STR, O_OPT, P_SYS, null, null), 'border' => array(T_ZBX_INT, O_OPT, P_NZERO, IN('0,1'), null), 'width' => array(T_ZBX_INT, O_OPT, P_NZERO, '{}>0', null), 'height' => array(T_ZBX_INT, O_OPT, P_NZERO, '{}>0', null), 'graph3d' => array(T_ZBX_INT, O_OPT, P_NZERO, IN('0,1'), null), 'legend' => array(T_ZBX_INT, O_OPT, P_NZERO, IN('0,1'), null));
check_fields($fields);
if (!DBfetch(DBselect('select graphid from graphs where graphid=' . $_REQUEST['graphid']))) {
    show_error_message(S_NO_GRAPH_DEFINED);
}
$options = array('graphids' => $_REQUEST['graphid'], 'select_hosts' => 1, 'extendoutput' => 1);
$db_data = CGraph::get($options);
if (empty($db_data)) {
    access_deny();
} else {
    $db_data = reset($db_data);
}
$host = reset($db_data['hosts']);
$effectiveperiod = navigation_bar_calc();
$graph = new CPie($db_data['graphtype']);
if (isset($_REQUEST['period'])) {
    $graph->setPeriod($_REQUEST['period']);
}
if (isset($_REQUEST['stime'])) {
    $graph->setSTime($_REQUEST['stime']);
}
if (isset($_REQUEST['border'])) {
Esempio n. 19
0
 /**
  * Delete items
  *
  * @param array $itemids
  * @return
  */
 public static function delete($itemids)
 {
     if (empty($itemids)) {
         return true;
     }
     $itemids = zbx_toArray($itemids);
     $insert = array();
     try {
         self::BeginTransaction(__METHOD__);
         $options = array('itemids' => $itemids, 'editable' => 1, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND);
         $del_items = self::get($options);
         foreach ($itemids as $itemid) {
             if (!isset($del_items[$itemid])) {
                 self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSIONS);
             }
             if ($del_items[$itemid]['templateid'] != 0) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot delete templated items');
             }
             if ($del_items[$itemid]['type'] == ITEM_TYPE_HTTPTEST) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot delete web items');
             }
         }
         // first delete child items
         $parent_itemids = $itemids;
         do {
             $db_items = DBselect('SELECT itemid FROM items WHERE ' . DBcondition('templateid', $parent_itemids));
             $parent_itemids = array();
             while ($db_item = DBfetch($db_items)) {
                 $parent_itemids[] = $db_item['itemid'];
                 $itemids[] = $db_item['itemid'];
             }
         } while (!empty($parent_itemids));
         // delete triggers
         $result = delete_triggers_by_itemid($itemids);
         if (!$result) {
             self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot delete item');
         }
         // delete graphs
         $del_graphs = array();
         $sql = 'SELECT gi.graphid' . ' FROM graphs_items gi' . ' WHERE ' . DBcondition('gi.itemid', $itemids) . ' AND NOT EXISTS (' . ' SELECT gii.gitemid' . ' FROM graphs_items gii' . ' WHERE gii.graphid=gi.graphid' . ' AND ' . DBcondition('gii.itemid', $itemids, true, false) . ' )';
         $db_graphs = DBselect($sql);
         while ($db_graph = DBfetch($db_graphs)) {
             $del_graphs[$db_graph['graphid']] = $db_graph['graphid'];
         }
         if (!empty($del_graphs)) {
             $result = CGraph::delete($del_graphs);
             if (!$result) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, 'Cannot delete item');
             }
         }
         //--
         $itemids_condition = DBcondition('itemid', $itemids);
         DB::delete('graphs_items', array($itemids_condition));
         DB::delete('screens_items', array(DBcondition('resourceid', $itemids), DBcondition('resourcetype', array(SCREEN_RESOURCE_SIMPLE_GRAPH, SCREEN_RESOURCE_PLAIN_TEXT))));
         DB::delete('items_applications', array($itemids_condition));
         DB::delete('items', array($itemids_condition));
         DB::delete('profiles', array('idx=' . zbx_dbstr('web.favorite.graphids'), 'source=' . zbx_dbstr('itemid'), DBcondition('value_id', $itemids)));
         $item_data_tables = array('trends', 'trends_uint', 'history_text', 'history_log', 'history_uint', 'history_str', 'history');
         foreach ($itemids as $id => $itemid) {
             foreach ($item_data_tables as $table) {
                 $insert[] = array('tablename' => $table, 'field' => 'itemid', 'value' => $itemid);
             }
         }
         DB::insert('housekeeper', $insert);
         self::EndTransaction(true, __METHOD__);
         return array('itemids' => $itemids);
     } catch (APIException $e) {
         self::EndTransaction(false, __METHOD__);
         $error = $e->getErrors();
         $error = reset($error);
         self::setError(__METHOD__, $e->getCode(), $error);
         return false;
     }
 }
Esempio n. 20
0
 private static function graph($action, $params)
 {
     CGraph::$error = array();
     switch ($action) {
         case 'add':
             $result = CGraph::add($params);
             break;
         case 'get':
             $result = CGraph::get($params);
             break;
         case 'getById':
             $result = CGraph::getById($params);
             break;
         case 'getId':
             $result = CGraph::getId($params);
             break;
         case 'update':
             $result = CGraph::update($params);
             break;
         case 'addItems':
             $result = CGraph::addItems($params);
             break;
         case 'deleteItems':
             $result = CGraph::deleteItems($params);
             break;
         case 'delete':
             $result = CGraph::delete($params);
             break;
         default:
             self::$result = array('error' => ZBX_API_ERROR_NO_METHOD, 'data' => 'Method: "' . $action . '" doesn\'t exist.');
             return;
             //exit function
     }
     if ($result !== false) {
         self::$result = array('result' => $result);
     } else {
         self::$result = CGraph::$error;
     }
 }
Esempio n. 21
0
     if ($pageFilter->hostid > 0) {
         $options['hostids'] = $pageFilter->hostid;
     } else {
         if ($pageFilter->groupid > 0) {
             $options['groupids'] = $pageFilter->groupid;
         }
     }
     $graphs = CGraph::get($options);
 }
 // sorting && paging
 order_result($graphs, $sortfield, $sortorder);
 $paging = getPagingLine($graphs);
 //----
 $graphids = zbx_objectValues($graphs, 'graphid');
 $options = array('graphids' => $graphids, 'output' => API_OUTPUT_EXTEND, 'select_hosts' => API_OUTPUT_EXTEND, 'select_templates' => API_OUTPUT_EXTEND);
 $graphs = CGraph::get($options);
 // Change graphtype from numbers to names, for correct sorting
 foreach ($graphs as $gnum => $graph) {
     switch ($graph['graphtype']) {
         case GRAPH_TYPE_STACKED:
             $graphtype = S_STACKED;
             break;
         case GRAPH_TYPE_PIE:
             $graphtype = S_PIE;
             break;
         case GRAPH_TYPE_EXPLODED:
             $graphtype = S_EXPLODED;
             break;
         default:
             $graphtype = S_NORMAL;
             break;
Esempio n. 22
0
function graphIdents($graphids)
{
    $idents = array();
    $options = array('graphids' => $graphids, 'select_hosts' => array('hostid', 'host'), 'output' => API_OUTPUT_EXTEND, 'nodeids' => get_current_nodeid(true));
    $graphs = CGraph::get($options);
    foreach ($graphs as $inum => $graph) {
        $host = reset($graph['hosts']);
        $idents[$graph['graphid']] = array('node' => get_node_name_by_elid($graph['graphid'], true), 'host' => $host['host'], 'name' => $graph['name']);
    }
    return $idents;
}
Esempio n. 23
0
function copy_template_graphs($hostid, $templateid = null, $copy_mode = false)
{
    if ($templateid == null) {
        $templateid = get_templates_by_hostid($hostid);
        $templateid = array_keys($templateid);
    }
    if (is_array($templateid)) {
        foreach ($templateid as $key => $id) {
            copy_template_graphs($hostid, $id, $copy_mode);
        }
        // attention recursion
        return;
    }
    $db_graphs = get_graphs_by_hostid($templateid);
    if ($copy_mode) {
        while ($db_graph = DBfetch($db_graphs)) {
            copy_graph_to_host($db_graph["graphid"], $hostid, $copy_mode);
        }
    } else {
        while ($db_graph = DBfetch($db_graphs)) {
            $gitems = CGraphItem::get(array('graphids' => $db_graph['graphid'], 'output' => API_OUTPUT_EXTEND));
            $filter = array('name' => $db_graph['name'], 'hostids' => $hostid);
            if (CGraph::exists($filter)) {
                $db_graph['gitems'] = $gitems;
                $res = CGraph::update($db_graph);
            } else {
                $db_graph['templateid'] = $db_graph['graphid'];
                $db_graph['gitems'] = get_same_graphitems_for_host($gitems, $hostid);
                $res = CGraph::create($db_graph);
            }
            if ($res === false) {
                return false;
            }
        }
    }
    return true;
}
Esempio n. 24
0
        case 'db-connection':
            $check['check_result'] = $icon_result[CDBUtils::isConnected($dbSql->db_link)];
            break;
        case 'php-timezone':
            $timezone = ini_get('date.timezone');
            if (!empty($timezone)) {
                $check['check_result'] = $icon_result[true];
            } else {
                $check['check_result'] = $icon_result[false];
            }
            break;
    }
}
// Testing graph capabilities
$data = array(array('test', 100), array('test1', 150), array('test1', 180), array('test1', 456));
// Pie graph
$pie_graph = new CGraph("testpage-graph03.jpg");
$pie_graph->SetData($data, 'pie');
$view->assign('pie_graph', $pie_graph->Render());
unset($pie_graph);
// Bar graph
$bar_graph = new CGraph("testpage-graph04.jpg");
$bar_graph->SetData($data, 'bars');
$view->assign('bar_graph', $bar_graph->Render());
unset($bar_graph);
// Set page name
$current_page = 'Test page';
$view->assign('page_name', $current_page);
// Template rendering
$view->assign('checks', $check_list);
$view->display('test.tpl');
Esempio n. 25
0
function make_graph_submenu()
{
    $graphids = array();
    $itemids = array();
    $fav_graphs = get_favorites('web.favorite.graphids');
    foreach ($fav_graphs as $key => $favorite) {
        if ('itemid' == $favorite['source']) {
            $itemids[$favorite['value']] = $favorite['value'];
        } else {
            $graphids[$favorite['value']] = $favorite['value'];
        }
    }
    $options = array('graphids' => $graphids, 'select_hosts' => array('hostid', 'host'), 'output' => API_OUTPUT_EXTEND);
    $graphs = CGraph::get($options);
    $graphs = zbx_toHash($graphs, 'graphid');
    $options = array('itemids' => $itemids, 'select_hosts' => array('hostid', 'host'), 'output' => API_OUTPUT_EXTEND, 'webitems' => 1);
    $items = CItem::get($options);
    $items = zbx_toHash($items, 'itemid');
    $favGraphs = array();
    foreach ($fav_graphs as $key => $favorite) {
        $source = $favorite['source'];
        $sourceid = $favorite['value'];
        if ('itemid' == $source) {
            if (!isset($items[$sourceid])) {
                continue;
            }
            $item_added = true;
            $item = $items[$sourceid];
            $host = reset($item['hosts']);
            $item['description'] = item_description($item);
            $favGraphs[] = array('name' => $host['host'] . ':' . $item['description'], 'favobj' => 'itemid', 'favid' => $sourceid, 'action' => 'remove');
        } else {
            if (!isset($graphs[$sourceid])) {
                continue;
            }
            $graph_added = true;
            $graph = $graphs[$sourceid];
            $ghost = reset($graph['hosts']);
            $favGraphs[] = array('name' => $ghost['host'] . ':' . $graph['name'], 'favobj' => 'graphid', 'favid' => $sourceid, 'action' => 'remove');
        }
    }
    if (isset($graph_added)) {
        $favGraphs[] = array('name' => S_REMOVE . SPACE . S_ALL_S . SPACE . S_GRAPHS, 'favobj' => 'graphid', 'favid' => 0, 'action' => 'remove');
    }
    if (isset($item_added)) {
        $favGraphs[] = array('name' => S_REMOVE . SPACE . S_ALL_S . SPACE . S_SIMPLE_GRAPHS, 'favobj' => 'itemid', 'favid' => 0, 'action' => 'remove');
    }
    return $favGraphs;
}
Esempio n. 26
0
 function DrawGraph($file = "")
 {
     $this->graph_width = $this->graph_padding['left'] + count($this->data) * ($this->bar_width + $this->bar_padding * 2) + $this->graph_padding['right'];
     $this->axis_deepness = $this->bar_height;
     $this->im = imagecreatetruecolor($this->graph_width, $this->graph_height);
     $this->axis_frontgridlines = 0;
     $this->axis_xscalevisible = 0;
     $this->axis_positions = array($this->axis_positions[0], 0, 0, 0);
     CGraph::DrawGraph();
     if ($this->axis_minY > 0) {
         $this->axis_minY = 0;
     }
     if ($this->axis_maxY < 0) {
         $this->axis_maxY = 0;
     }
     $this->__Draw_LeftBottom_Axis();
     $p = 0;
     foreach ($this->data as $name => $value) {
         $p++;
         $this->__DrawBarText($p, $name);
         $this->__DrawBar($p, $value);
     }
     $this->__Draw_TopRight_Axis();
     CGraph::DrawGraph2();
     if (strlen($file)) {
         $ret = imagepng($this->im, $file);
     } else {
         header("Content-Type: image/png");
         // thanks to Marcin G. :)
         imagepng($this->im);
         $ret = true;
     }
     imagedestroy($this->im);
     return $ret;
 }
Esempio n. 27
0
function get_screen($screenid, $editmode, $effectiveperiod = NULL)
{
    global $USER_DETAILS;
    if ($screenid == 0) {
        return new CTableInfo(S_NO_SCREENS_DEFINED);
    }
    $r = CScreen::get(array('screenids' => $screenid, 'editable' => $editmode == 1 ? 1 : null, 'output' => API_OUTPUT_SHORTEN));
    if (empty($r)) {
        access_deny();
    }
    if (is_null($effectiveperiod)) {
        $effectiveperiod = ZBX_MIN_PERIOD;
    }
    $result = DBselect('SELECT name,hsize,vsize FROM screens WHERE screenid=' . $screenid);
    $row = DBfetch($result);
    if (!$row) {
        return new CTableInfo(S_NO_SCREENS_DEFINED);
    }
    $sql = 'SELECT * FROM screens_items WHERE screenid=' . $screenid;
    $iresult = DBSelect($sql);
    $skip_field = array();
    $irows = array();
    while ($irow = DBfetch($iresult)) {
        $irows[] = $irow;
        for ($i = 0; $i < $irow['rowspan'] || $i == 0; $i++) {
            for ($j = 0; $j < $irow['colspan'] || $j == 0; $j++) {
                if ($i != 0 || $j != 0) {
                    if (!isset($skip_field[$irow['y'] + $i])) {
                        $skip_field[$irow['y'] + $i] = array();
                    }
                    $skip_field[$irow['y'] + $i][$irow['x'] + $j] = 1;
                }
            }
        }
    }
    $table = new CTable(new CLink(S_NO_ROWS_IN_SCREEN . SPACE . $row['name'], 'screenconf.php?config=0&form=update&screenid=' . $screenid), $editmode == 0 || $editmode == 2 ? 'screen_view' : 'screen_edit');
    $table->setAttribute('id', 'iframe');
    if ($editmode == 1) {
        $new_cols = array(new Ccol(new Cimg('images/general/zero.gif', 'zero', 1, 1)));
        for ($c = 0; $c < $row['hsize'] + 1; $c++) {
            $add_icon = new Cimg('images/general/closed.gif', NULL, NULL, NULL, 'pointer');
            $add_icon->addAction('onclick', "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&add_col={$c}';");
            array_push($new_cols, new Ccol($add_icon));
        }
        $table->addRow($new_cols);
    }
    $empty_screen_col = array();
    for ($r = 0; $r < $row['vsize']; $r++) {
        $new_cols = array();
        $empty_screen_row = true;
        if ($editmode == 1) {
            $add_icon = new Cimg('images/general/closed.gif', NULL, NULL, NULL, 'pointer');
            $add_icon->addAction('onclick', "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&add_row={$r}';");
            array_push($new_cols, new Ccol($add_icon));
        }
        for ($c = 0; $c < $row['hsize']; $c++) {
            $item = array();
            if (isset($skip_field[$r][$c])) {
                continue;
            }
            $item_form = false;
            $irow = false;
            foreach ($irows as $tmprow) {
                if ($tmprow['x'] == $c && $tmprow['y'] == $r) {
                    $irow = $tmprow;
                    break;
                }
            }
            if ($irow) {
                $screenitemid = $irow['screenitemid'];
                $resourcetype = $irow['resourcetype'];
                $resourceid = $irow['resourceid'];
                $width = $irow['width'];
                $height = $irow['height'];
                $colspan = $irow['colspan'];
                $rowspan = $irow['rowspan'];
                $elements = $irow['elements'];
                $valign = $irow['valign'];
                $halign = $irow['halign'];
                $style = $irow['style'];
                $url = $irow['url'];
                $dynamic = $irow['dynamic'];
            } else {
                $screenitemid = 0;
                $resourcetype = 0;
                $resourceid = 0;
                $width = 0;
                $height = 0;
                $colspan = 0;
                $rowspan = 0;
                $elements = 0;
                $valign = VALIGN_DEFAULT;
                $halign = HALIGN_DEFAULT;
                $style = 0;
                $url = '';
                $dynamic = 0;
            }
            if ($screenitemid > 0) {
                $empty_screen_row = false;
                $empty_screen_col[$c] = 1;
            }
            if ($editmode == 1 && $screenitemid != 0) {
                $onclick_action = "ZBX_SCREENS['" . $_REQUEST['screenid'] . "'].screen.element_onclick('screenedit.php?form=update" . url_param('screenid') . '&screenitemid=' . $screenitemid . "#form');";
                $action = 'screenedit.php?form=update' . url_param('screenid') . '&screenitemid=' . $screenitemid . '#form';
            } else {
                if ($editmode == 1 && $screenitemid == 0) {
                    $onclick_action = "ZBX_SCREENS['" . $_REQUEST['screenid'] . "'].screen.element_onclick('screenedit.php?form=update" . url_param('screenid') . '&x=' . $c . '&y=' . $r . "#form');";
                    $action = 'screenedit.php?form=update' . url_param('screenid') . '&x=' . $c . '&y=' . $r . '#form';
                } else {
                    $action = NULL;
                }
            }
            if ($editmode == 1 && isset($_REQUEST['form']) && isset($_REQUEST['x']) && $_REQUEST['x'] == $c && isset($_REQUEST['y']) && $_REQUEST['y'] == $r) {
                // click on empty field
                $item = get_screen_item_form();
                $item_form = true;
            } else {
                if ($editmode == 1 && isset($_REQUEST['form']) && isset($_REQUEST['screenitemid']) && bccomp($_REQUEST['screenitemid'], $screenitemid) == 0) {
                    // click on element
                    $item = get_screen_item_form();
                    $item_form = true;
                } else {
                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_GRAPH) {
                        if ($editmode == 0) {
                            $action = 'charts.php?graphid=' . $resourceid . url_param('period') . url_param('stime');
                        }
                        // GRAPH & ZOOM features
                        $dom_graph_id = 'graph_' . $screenitemid . '_' . $resourceid;
                        $containerid = 'graph_cont_' . $screenitemid . '_' . $resourceid;
                        $graphDims = getGraphDims($resourceid);
                        $graphDims['graphHeight'] = $height;
                        $graphDims['width'] = $width;
                        $graph = get_graph_by_graphid($resourceid);
                        $graphid = $graph['graphid'];
                        $legend = $graph['show_legend'];
                        $graph3d = $graph['show_3d'];
                        //-------------
                        // Host feature
                        if ($dynamic == SCREEN_DYNAMIC_ITEM && isset($_REQUEST['hostid']) && $_REQUEST['hostid'] > 0) {
                            $options = array('hostids' => $_REQUEST['hostid'], 'output' => array('hostid', 'host'));
                            $hosts = CHost::get($options);
                            $host = reset($hosts);
                            $options = array('graphids' => $resourceid, 'output' => API_OUTPUT_EXTEND, 'select_hosts' => API_OUTPUT_REFER, 'select_graph_items' => API_OUTPUT_EXTEND);
                            $graph = CGraph::get($options);
                            $graph = reset($graph);
                            if (count($graph['hosts']) == 1) {
                                // if items from one host we change them, or set calculated if not exist on that host
                                if ($graph['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymax_itemid']) {
                                    $new_dinamic = get_same_graphitems_for_host(array(array('itemid' => $graph['ymax_itemid'])), $_REQUEST['hostid'], false);
                                    $new_dinamic = reset($new_dinamic);
                                    if (isset($new_dinamic['itemid']) && $new_dinamic['itemid'] > 0) {
                                        $graph['ymax_itemid'] = $new_dinamic['itemid'];
                                    } else {
                                        $graph['ymax_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
                                    }
                                }
                                if ($graph['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymin_itemid']) {
                                    $new_dinamic = get_same_graphitems_for_host(array(array('itemid' => $graph['ymin_itemid'])), $_REQUEST['hostid'], false);
                                    $new_dinamic = reset($new_dinamic);
                                    if (isset($new_dinamic['itemid']) && $new_dinamic['itemid'] > 0) {
                                        $graph['ymin_itemid'] = $new_dinamic['itemid'];
                                    } else {
                                        $graph['ymin_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
                                    }
                                }
                            }
                            $url = $graph['graphtype'] == GRAPH_TYPE_PIE || $graph['graphtype'] == GRAPH_TYPE_EXPLODED ? 'chart7.php' : 'chart3.php';
                            $url = new Curl($url);
                            foreach ($graph as $name => $value) {
                                if ($name == 'width' || $name == 'height') {
                                    continue;
                                }
                                $url->setArgument($name, $value);
                            }
                            $new_items = get_same_graphitems_for_host($graph['gitems'], $_REQUEST['hostid'], false);
                            foreach ($new_items as $gitem) {
                                unset($gitem['gitemid']);
                                unset($gitem['graphid']);
                                foreach ($gitem as $name => $value) {
                                    $url->setArgument('items[' . $gitem['itemid'] . '][' . $name . ']', $value);
                                }
                            }
                            $url->setArgument('name', $host['host'] . ': ' . $graph['name']);
                            $url = $url->getUrl();
                        }
                        //-------------
                        $objData = array('id' => $resourceid, 'domid' => $dom_graph_id, 'containerid' => $containerid, 'objDims' => $graphDims, 'loadSBox' => 0, 'loadImage' => 1, 'loadScroll' => 0, 'dynamic' => 0);
                        $default = false;
                        if ($graphDims['graphtype'] == GRAPH_TYPE_PIE || $graphDims['graphtype'] == GRAPH_TYPE_EXPLODED) {
                            if ($dynamic == SCREEN_SIMPLE_ITEM || empty($url)) {
                                $url = 'chart6.php?graphid=' . $resourceid;
                                $default = true;
                            }
                            $timeline = array();
                            $timeline['period'] = $effectiveperiod;
                            $timeline['starttime'] = date('YmdHis', get_min_itemclock_by_graphid($resourceid));
                            if (isset($_REQUEST['stime'])) {
                                $timeline['usertime'] = date('YmdHis', zbxDateToTime($_REQUEST['stime']) + $timeline['period']);
                            }
                            // $src = $url.'&width='.$width.'&height='.$height.'&legend='.$legend.'&graph3d='.$graph3d;
                            $src = $url . '&width=' . $width . '&height=' . $height . '&legend=' . $legend . '&graph3d=' . $graph3d . '&period=' . $effectiveperiod . url_param('stime');
                            $objData['src'] = $src;
                        } else {
                            if ($dynamic == SCREEN_SIMPLE_ITEM || empty($url)) {
                                $url = 'chart2.php?graphid=' . $resourceid;
                                $default = true;
                            }
                            $src = $url . '&width=' . $width . '&height=' . $height . '&period=' . $effectiveperiod . url_param('stime');
                            $timeline = array();
                            if (isset($graphid) && !is_null($graphid) && $editmode != 1) {
                                $timeline['period'] = $effectiveperiod;
                                $timeline['starttime'] = date('YmdHis', time() - ZBX_MAX_PERIOD);
                                //get_min_itemclock_by_graphid($graphid);
                                if (isset($_REQUEST['stime'])) {
                                    $timeline['usertime'] = date('YmdHis', zbxDateToTime($_REQUEST['stime']) + $timeline['period']);
                                }
                                $objData['loadSBox'] = 1;
                            }
                            $objData['src'] = $src;
                        }
                        if ($editmode || !$default) {
                            $item = new CDiv();
                        } else {
                            $item = new CLink(null, $action);
                        }
                        $item->setAttribute('id', $containerid);
                        $item = array($item);
                        if ($editmode == 1) {
                            $item[] = BR();
                            $item[] = new CLink(S_CHANGE, $action);
                        }
                        if ($editmode == 2) {
                            insert_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');');
                        } else {
                            zbx_add_post_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');');
                        }
                    } else {
                        if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SIMPLE_GRAPH) {
                            $dom_graph_id = 'graph_' . $screenitemid . '_' . $resourceid;
                            $containerid = 'graph_cont_' . $screenitemid . '_' . $resourceid;
                            $graphDims = getGraphDims();
                            $graphDims['graphHeight'] = $height;
                            $graphDims['width'] = $width;
                            $objData = array('id' => $resourceid, 'domid' => $dom_graph_id, 'containerid' => $containerid, 'objDims' => $graphDims, 'loadSBox' => 0, 'loadImage' => 1, 'loadScroll' => 0, 'dynamic' => 0);
                            // Host feature
                            if ($dynamic == SCREEN_DYNAMIC_ITEM && isset($_REQUEST['hostid']) && $_REQUEST['hostid'] > 0) {
                                if ($newitemid = get_same_item_for_host($resourceid, $_REQUEST['hostid'])) {
                                    $resourceid = $newitemid;
                                } else {
                                    $resourceid = '';
                                }
                            }
                            //-------------
                            if ($editmode == 0 && !empty($resourceid)) {
                                $action = 'history.php?action=showgraph&itemid=' . $resourceid . url_param('period') . url_param('stime');
                            }
                            $timeline = array();
                            $timeline['starttime'] = date('YmdHis', time() - ZBX_MAX_PERIOD);
                            if (!zbx_empty($resourceid) && $editmode != 1) {
                                $timeline['period'] = $effectiveperiod;
                                if (isset($_REQUEST['stime'])) {
                                    $timeline['usertime'] = date('YmdHis', zbxDateToTime($_REQUEST['stime']) + $timeline['period']);
                                }
                                $objData['loadSBox'] = 1;
                            }
                            $src = zbx_empty($resourceid) ? 'chart3.php?' : 'chart.php?itemid=' . $resourceid . '&';
                            $src .= $url . 'width=' . $width . '&height=' . $height;
                            $objData['src'] = $src;
                            if ($editmode) {
                                $item = new CDiv();
                            } else {
                                $item = new CLink(null, $action);
                            }
                            $item->setAttribute('id', $containerid);
                            $item = array($item);
                            if ($editmode == 1) {
                                $item[] = BR();
                                $item[] = new CLink(S_CHANGE, $action);
                            }
                            if ($editmode == 2) {
                                insert_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');');
                            } else {
                                zbx_add_post_js('timeControl.addObject("' . $dom_graph_id . '",' . zbx_jsvalue($timeline) . ',' . zbx_jsvalue($objData) . ');');
                            }
                        } else {
                            if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_MAP) {
                                $image_map = new CImg("map.php?noedit=1&sysmapid={$resourceid}" . "&width={$width}&height={$height}&curtime=" . time());
                                if ($editmode == 0) {
                                    $options = array('sysmapids' => $resourceid, 'output' => API_OUTPUT_EXTEND, 'select_selements' => API_OUTPUT_EXTEND, 'nopermissions' => 1);
                                    $sysmaps = CMap::get($options);
                                    $sysmap = reset($sysmaps);
                                    $action_map = getActionMapBySysmap($sysmap);
                                    $image_map->setMap($action_map->getName());
                                    $item = array($action_map, $image_map);
                                } else {
                                    $item = $image_map;
                                    //						$item = new CLink($image_map, $action);
                                }
                            } else {
                                if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_PLAIN_TEXT) {
                                    // Host feature
                                    if ($dynamic == SCREEN_DYNAMIC_ITEM && isset($_REQUEST['hostid']) && $_REQUEST['hostid'] > 0) {
                                        if ($newitemid = get_same_item_for_host($resourceid, $_REQUEST['hostid'])) {
                                            $resourceid = $newitemid;
                                        } else {
                                            $resourceid = 0;
                                        }
                                    }
                                    //-------------
                                    $item = array(get_screen_plaintext($resourceid, $elements, $style));
                                    if ($editmode == 1) {
                                        array_push($item, new CLink(S_CHANGE, $action));
                                    }
                                } else {
                                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_HOSTGROUP_TRIGGERS) {
                                        $params = array('groupids' => null, 'hostids' => null, 'maintenance' => null, 'severity' => null, 'limit' => $elements);
                                        $tr_form = S_ALL_S;
                                        if ($resourceid > 0) {
                                            $options = array('groupids' => $resourceid, 'output' => API_OUTPUT_EXTEND);
                                            $hostgroups = CHostgroup::get($options);
                                            $hostgroup = reset($hostgroups);
                                            $tr_form = new CSpan(S_GROUP . ': ' . $hostgroup['name'], 'white');
                                            $params['groupids'] = $hostgroup['groupid'];
                                        } else {
                                            $groupid = get_request('tr_groupid', CProfile::get('web.screens.tr_groupid', 0));
                                            $hostid = get_request('tr_hostid', CProfile::get('web.screens.tr_hostid', 0));
                                            CProfile::update('web.screens.tr_groupid', $groupid, PROFILE_TYPE_ID);
                                            CProfile::update('web.screens.tr_hostid', $hostid, PROFILE_TYPE_ID);
                                            $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND);
                                            $groups = CHostGroup::get($options);
                                            order_result($groups, 'name');
                                            $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND);
                                            if ($groupid > 0) {
                                                $options['groupids'] = $groupid;
                                            }
                                            $hosts = CHost::get($options);
                                            $hosts = zbx_toHash($hosts, 'hostid');
                                            order_result($hosts, 'host');
                                            if (!isset($hosts[$hostid])) {
                                                $hostid = 0;
                                            }
                                            $tr_form = new CForm();
                                            $cmbGroup = new CComboBox('tr_groupid', $groupid, 'submit()');
                                            $cmbHosts = new CComboBox('tr_hostid', $hostid, 'submit()');
                                            $cmbGroup->addItem(0, S_ALL_SMALL);
                                            $cmbHosts->addItem(0, S_ALL_SMALL);
                                            foreach ($groups as $gnum => $group) {
                                                $cmbGroup->addItem($group['groupid'], get_node_name_by_elid($group['groupid'], null, ': ') . $group['name']);
                                            }
                                            foreach ($hosts as $hnum => $host) {
                                                $cmbHosts->addItem($host['hostid'], get_node_name_by_elid($host['hostid'], null, ': ') . $host['host']);
                                            }
                                            $tr_form->addItem(array(S_GROUP . SPACE, $cmbGroup));
                                            $tr_form->addItem(array(SPACE . S_HOST . SPACE, $cmbHosts));
                                            if ($groupid > 0) {
                                                $params['groupids'] = $groupid;
                                            }
                                            if ($hostid > 0) {
                                                $params['hostids'] = $hostid;
                                            }
                                        }
                                        $item = array(get_table_header(array(S_STATUS_OF_TRIGGERS_BIG, SPACE, zbx_date2str(S_SCREENS_TRIGGER_FORM_DATE_FORMAT)), $tr_form));
                                        $item[] = make_latest_issues($params);
                                        if ($editmode == 1) {
                                            array_push($item, new CLink(S_CHANGE, $action));
                                        }
                                        ///-----------------------
                                    } else {
                                        if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_HOST_TRIGGERS) {
                                            $params = array('groupids' => null, 'hostids' => null, 'maintenance' => null, 'severity' => null, 'limit' => $elements);
                                            $tr_form = S_ALL_S;
                                            if ($resourceid > 0) {
                                                $options = array('hostids' => $resourceid, 'output' => API_OUTPUT_EXTEND);
                                                $hosts = CHost::get($options);
                                                $host = reset($hosts);
                                                $tr_form = new CSpan(S_HOST . ': ' . $host['host'], 'white');
                                                $params['hostids'] = $host['hostid'];
                                            } else {
                                                $groupid = get_request('tr_groupid', CProfile::get('web.screens.tr_groupid', 0));
                                                $hostid = get_request('tr_hostid', CProfile::get('web.screens.tr_hostid', 0));
                                                CProfile::update('web.screens.tr_groupid', $groupid, PROFILE_TYPE_ID);
                                                CProfile::update('web.screens.tr_hostid', $hostid, PROFILE_TYPE_ID);
                                                $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND);
                                                $groups = CHostGroup::get($options);
                                                order_result($groups, 'name');
                                                $options = array('monitored_hosts' => 1, 'output' => API_OUTPUT_EXTEND);
                                                if ($groupid > 0) {
                                                    $options['groupids'] = $groupid;
                                                }
                                                $hosts = CHost::get($options);
                                                $hosts = zbx_toHash($hosts, 'hostid');
                                                order_result($hosts, 'host');
                                                if (!isset($hosts[$hostid])) {
                                                    $hostid = 0;
                                                }
                                                $tr_form = new CForm();
                                                $cmbGroup = new CComboBox('tr_groupid', $groupid, 'submit()');
                                                $cmbHosts = new CComboBox('tr_hostid', $hostid, 'submit()');
                                                $cmbGroup->addItem(0, S_ALL_SMALL);
                                                $cmbHosts->addItem(0, S_ALL_SMALL);
                                                foreach ($groups as $gnum => $group) {
                                                    $cmbGroup->addItem($group['groupid'], get_node_name_by_elid($group['groupid'], null, ': ') . $group['name']);
                                                }
                                                foreach ($hosts as $hnum => $host) {
                                                    $cmbHosts->addItem($host['hostid'], get_node_name_by_elid($host['hostid'], null, ': ') . $host['host']);
                                                }
                                                $tr_form->addItem(array(S_GROUP . SPACE, $cmbGroup));
                                                $tr_form->addItem(array(SPACE . S_HOST . SPACE, $cmbHosts));
                                                if ($groupid > 0) {
                                                    $params['groupids'] = $groupid;
                                                }
                                                if ($hostid > 0) {
                                                    $params['hostids'] = $hostid;
                                                }
                                            }
                                            ///-----------------------
                                            $item = array(get_table_header(array(S_STATUS_OF_TRIGGERS_BIG, SPACE, zbx_date2str(S_SCREENS_TRIGGER_FORM_DATE_FORMAT)), $tr_form));
                                            $item[] = make_latest_issues($params);
                                            if ($editmode == 1) {
                                                array_push($item, new CLink(S_CHANGE, $action));
                                            }
                                        } else {
                                            if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SYSTEM_STATUS) {
                                                $params = array('groupids' => null, 'hostids' => null, 'maintenance' => null, 'severity' => null, 'limit' => null, 'extAck' => 0);
                                                $item = array(get_table_header(array(S_SYSTEM_STATUS, SPACE, zbx_date2str(S_SCREENS_TRIGGER_FORM_DATE_FORMAT))));
                                                $item[] = make_system_status($params);
                                                if ($editmode == 1) {
                                                    array_push($item, new CLink(S_CHANGE, $action));
                                                }
                                            } else {
                                                if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_HOSTS_INFO) {
                                                    $item = array(new CHostsInfo($resourceid, $style));
                                                    if ($editmode == 1) {
                                                        array_push($item, new CLink(S_CHANGE, $action));
                                                    }
                                                } else {
                                                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_TRIGGERS_INFO) {
                                                        $item = new CTriggersInfo($resourceid, null, $style);
                                                        $item = array($item);
                                                        if ($editmode == 1) {
                                                            array_push($item, new CLink(S_CHANGE, $action));
                                                        }
                                                    } else {
                                                        if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SERVER_INFO) {
                                                            //					$item = array(get_table_header(S_STATUS_OF_ZABBIX_BIG),make_status_of_zbx());
                                                            $item = array(new CServerInfo());
                                                            if ($editmode == 1) {
                                                                array_push($item, new CLink(S_CHANGE, $action));
                                                            }
                                                        } else {
                                                            if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_CLOCK) {
                                                                $item = new CFlashClock($width, $height, $style, $action);
                                                            } else {
                                                                if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_SCREEN) {
                                                                    $item = array(get_screen($resourceid, 2, $effectiveperiod));
                                                                    if ($editmode == 1) {
                                                                        array_push($item, new CLink(S_CHANGE, $action));
                                                                    }
                                                                } else {
                                                                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_TRIGGERS_OVERVIEW) {
                                                                        $hostids = array();
                                                                        $res = DBselect('SELECT DISTINCT hg.hostid FROM hosts_groups hg WHERE hg.groupid=' . $resourceid);
                                                                        while ($tmp_host = DBfetch($res)) {
                                                                            $hostids[$tmp_host['hostid']] = $tmp_host['hostid'];
                                                                        }
                                                                        $item = array(get_triggers_overview($hostids, $style));
                                                                        if ($editmode == 1) {
                                                                            array_push($item, new CLink(S_CHANGE, $action));
                                                                        }
                                                                    } else {
                                                                        if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_DATA_OVERVIEW) {
                                                                            $hostids = array();
                                                                            $res = DBselect('SELECT DISTINCT hg.hostid FROM hosts_groups hg WHERE hg.groupid=' . $resourceid);
                                                                            while ($tmp_host = DBfetch($res)) {
                                                                                $hostids[$tmp_host['hostid']] = $tmp_host['hostid'];
                                                                            }
                                                                            $item = array(get_items_data_overview($hostids, $style));
                                                                            if ($editmode == 1) {
                                                                                array_push($item, new CLink(S_CHANGE, $action));
                                                                            }
                                                                        } else {
                                                                            if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_URL) {
                                                                                $item = array(new CIFrame($url, $width, $height, "auto"));
                                                                                if ($editmode == 1) {
                                                                                    array_push($item, BR(), new CLink(S_CHANGE, $action));
                                                                                }
                                                                            } else {
                                                                                if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_ACTIONS) {
                                                                                    $item = array(get_history_of_actions($elements));
                                                                                    if ($editmode == 1) {
                                                                                        array_push($item, new CLink(S_CHANGE, $action));
                                                                                    }
                                                                                } else {
                                                                                    if ($screenitemid != 0 && $resourcetype == SCREEN_RESOURCE_EVENTS) {
                                                                                        $options = array('monitored' => 1, 'value' => array(TRIGGER_VALUE_TRUE, TRIGGER_VALUE_FALSE), 'limit' => $elements);
                                                                                        $hide_unknown = CProfile::get('web.events.filter.hide_unknown', 0);
                                                                                        if ($hide_unknown) {
                                                                                            $options['value'] = array(TRIGGER_VALUE_TRUE, TRIGGER_VALUE_FALSE);
                                                                                        }
                                                                                        $item = new CTableInfo(S_NO_EVENTS_FOUND);
                                                                                        $item->SetHeader(array(S_TIME, is_show_all_nodes() ? S_NODE : null, S_HOST, S_DESCRIPTION, S_VALUE, S_SEVERITY));
                                                                                        $events = getLastEvents($options);
                                                                                        foreach ($events as $enum => $event) {
                                                                                            $trigger = $event['trigger'];
                                                                                            $host = $event['host'];
                                                                                            $value = new CCol(trigger_value2str($event['value']), get_trigger_value_style($event['value']));
                                                                                            //						$row = zbx_array_merge($triggers[$row['triggerid']],$row);
                                                                                            //						if((1 == $hide_unknown) && (!event_initial_time($row,$hide_unknown))) continue;
                                                                                            $item->addRow(array(zbx_date2str(S_EVENTS_TRIGGERS_EVENTS_HISTORY_LIST_DATE_FORMAT, $event['clock']), get_node_name_by_elid($event['objectid']), $host['host'], new CLink($trigger['description'], 'tr_events.php?triggerid=' . $event['objectid'] . '&eventid=' . $event['eventid']), $value, new CCol(get_severity_description($trigger['priority']), get_severity_style($trigger['priority']))));
                                                                                        }
                                                                                        $item = array($item);
                                                                                        if ($editmode == 1) {
                                                                                            array_push($item, new CLink(S_CHANGE, $action));
                                                                                        }
                                                                                    } else {
                                                                                        $item = array(SPACE);
                                                                                        if ($editmode == 1) {
                                                                                            array_push($item, BR(), new CLink(S_CHANGE, $action));
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $str_halign = 'def';
            if ($halign == HALIGN_CENTER) {
                $str_halign = 'cntr';
            }
            if ($halign == HALIGN_LEFT) {
                $str_halign = 'left';
            }
            if ($halign == HALIGN_RIGHT) {
                $str_halign = 'right';
            }
            $str_valign = 'def';
            if ($valign == VALIGN_MIDDLE) {
                $str_valign = 'mdl';
            }
            if ($valign == VALIGN_TOP) {
                $str_valign = 'top';
            }
            if ($valign == VALIGN_BOTTOM) {
                $str_valign = 'bttm';
            }
            if ($editmode == 1 && !$item_form) {
                $item = new CDiv($item, 'draggable');
                $item->setAttribute('id', 'position_' . $r . '_' . $c);
                if ($editmode == 1) {
                    $item->setAttribute('onclick', 'javascript: ' . $onclick_action);
                }
            }
            $new_col = new CCol($item, $str_halign . '_' . $str_valign);
            if ($colspan) {
                $new_col->SetColSpan($colspan);
            }
            if ($rowspan) {
                $new_col->SetRowSpan($rowspan);
            }
            array_push($new_cols, $new_col);
        }
        if ($editmode == 1) {
            $rmv_icon = new Cimg('images/general/opened.gif', NULL, NULL, NULL, 'pointer');
            if ($empty_screen_row) {
                $rmv_row_link = "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_row={$r}';";
            } else {
                $rmv_row_link = "javascript: if(Confirm('" . S_THIS_SCREEN_ROW_NOT_EMPTY . '. ' . S_DELETE_IT_Q . "')){" . " location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_row={$r}';}";
            }
            $rmv_icon->addAction('onclick', $rmv_row_link);
            array_push($new_cols, new Ccol($rmv_icon));
        }
        $table->addRow(new CRow($new_cols));
    }
    if ($editmode == 1) {
        $add_icon = new Cimg('images/general/closed.gif', NULL, NULL, NULL, 'pointer');
        $add_icon->addAction('onclick', "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&add_row={$row['vsize']}';");
        $new_cols = array(new Ccol($add_icon));
        for ($c = 0; $c < $row['hsize']; $c++) {
            $rmv_icon = new Cimg('images/general/opened.gif', NULL, NULL, NULL, 'pointer');
            if (isset($empty_screen_col[$c])) {
                $rmv_col_link = "javascript: if(Confirm('" . S_THIS_SCREEN_COLUMN_NOT_EMPTY . '. ' . S_DELETE_IT_Q . "')){" . " location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_col={$c}';}";
            } else {
                $rmv_col_link = "javascript: location.href = 'screenedit.php?config=1&screenid={$screenid}&rmv_col={$c}';";
            }
            $rmv_icon->addAction('onclick', $rmv_col_link);
            array_push($new_cols, new Ccol($rmv_icon));
        }
        array_push($new_cols, new Ccol(new Cimg('images/general/zero.gif', 'zero', 1, 1)));
        $table->addRow($new_cols);
    }
    return $table;
}
Esempio n. 28
0
 /**
  * Get Host data
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8
  * @version 1
  *
  * @param _array $options
  * @param array $options['nodeids'] Node IDs
  * @param array $options['groupids'] HostGroup IDs
  * @param array $options['hostids'] Host IDs
  * @param boolean $options['monitored_hosts'] only monitored Hosts
  * @param boolean $options['templated_hosts'] include templates in result
  * @param boolean $options['with_items'] only with items
  * @param boolean $options['with_monitored_items'] only with monitored items
  * @param boolean $options['with_historical_items'] only with historical items
  * @param boolean $options['with_triggers'] only with triggers
  * @param boolean $options['with_monitored_triggers'] only with monitored triggers
  * @param boolean $options['with_httptests'] only with http tests
  * @param boolean $options['with_monitored_httptests'] only with monitored http tests
  * @param boolean $options['with_graphs'] only with graphs
  * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins
  * @param int $options['extendoutput'] return all fields for Hosts
  * @param boolean $options['select_groups'] select HostGroups
  * @param boolean $options['select_templates'] select Templates
  * @param boolean $options['select_items'] select Items
  * @param boolean $options['select_triggers'] select Triggers
  * @param boolean $options['select_graphs'] select Graphs
  * @param boolean $options['select_applications'] select Applications
  * @param boolean $options['select_macros'] select Macros
  * @param boolean $options['select_profile'] select Profile
  * @param int $options['count'] count Hosts, returned column name is rowscount
  * @param string $options['pattern'] search hosts by pattern in Host name
  * @param string $options['extendPattern'] search hosts by pattern in Host name, ip and DNS
  * @param int $options['limit'] limit selection
  * @param string $options['sortfield'] field to sort by
  * @param string $options['sortorder'] sort order
  * @return array|boolean Host data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $nodeCheck = false;
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('hostid', 'host', 'status', 'dns', 'ip');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('hosts' => 'h.hostid'), 'from' => array('hosts' => 'hosts h'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'proxyids' => null, 'templateids' => null, 'itemids' => null, 'triggerids' => null, 'maintenanceids' => null, 'graphids' => null, 'dhostids' => null, 'dserviceids' => null, 'monitored_hosts' => null, 'templated_hosts' => null, 'proxy_hosts' => null, 'with_items' => null, 'with_monitored_items' => null, 'with_historical_items' => null, 'with_triggers' => null, 'with_monitored_triggers' => null, 'with_httptests' => null, 'with_monitored_httptests' => null, 'with_graphs' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'selectParentTemplates' => null, 'select_items' => null, 'select_triggers' => null, 'select_graphs' => null, 'select_dhosts' => null, 'select_dservices' => null, 'select_applications' => null, 'select_macros' => null, 'select_profile' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_groups'])) {
             $options['select_groups'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['selectParentTemplates'])) {
             $options['selectParentTemplates'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_items'])) {
             $options['select_items'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_triggers'])) {
             $options['select_triggers'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_graphs'])) {
             $options['select_graphs'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_applications'])) {
             $options['select_applications'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_macros'])) {
             $options['select_macros'] = API_OUTPUT_EXTEND;
         }
     }
     if (is_array($options['output'])) {
         unset($sql_parts['select']['hosts']);
         $sql_parts['select']['hostid'] = ' h.hostid';
         foreach ($options['output'] as $key => $field) {
             $sql_parts['select'][$field] = ' h.' . $field;
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql_parts['where'][] = 'EXISTS (' . ' SELECT hh.hostid ' . ' FROM hosts hh, hosts_groups hgg, rights r, users_groups ug ' . ' WHERE hh.hostid=h.hostid ' . ' AND hh.hostid=hgg.hostid ' . ' AND r.id=hgg.groupid ' . ' AND r.groupid=ug.usrgrpid ' . ' AND ug.userid=' . $userid . ' AND r.permission>=' . $permission . ' AND NOT EXISTS( ' . ' SELECT hggg.groupid ' . ' FROM hosts_groups hggg, rights rr, users_groups gg ' . ' WHERE hggg.hostid=hgg.hostid ' . ' AND rr.id=hggg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . ' )) ';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sql_parts['where']['hostid'] = DBcondition('h.hostid', $options['hostids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('h.hostid', $nodeids);
         }
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']);
         $sql_parts['where']['hgh'] = 'hg.hostid=h.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['groupid'] = 'hg.groupid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('hg.groupid', $nodeids);
         }
     }
     // proxyids
     if (!is_null($options['proxyids'])) {
         zbx_value2array($options['proxyids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['proxy_hostid'] = 'h.proxy_hostid';
         }
         $sql_parts['where'][] = DBcondition('h.proxy_hostid', $options['proxyids']);
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['templateid'] = 'ht.templateid';
         }
         $sql_parts['from']['hosts_templates'] = 'hosts_templates ht';
         $sql_parts['where'][] = DBcondition('ht.templateid', $options['templateids']);
         $sql_parts['where']['hht'] = 'h.hostid=ht.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['templateid'] = 'ht.templateid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('ht.templateid', $nodeids);
         }
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['itemid'] = 'i.itemid';
         }
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('i.itemid', $options['itemids']);
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('i.itemid', $nodeids);
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['triggerid'] = 'f.triggerid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('f.triggerid', $options['triggerids']);
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('f.triggerid', $nodeids);
         }
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['graphid'] = 'gi.graphid';
         }
         $sql_parts['from']['graphs_items'] = 'graphs_items gi';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('gi.graphid', $options['graphids']);
         $sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('gi.graphid', $nodeids);
         }
     }
     // dhostids
     if (!is_null($options['dhostids'])) {
         zbx_value2array($options['dhostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['dhostid'] = 'ds.dhostid';
         }
         $sql_parts['from']['dservices'] = 'dservices ds';
         $sql_parts['where'][] = DBcondition('ds.dhostid', $options['dhostids']);
         $sql_parts['where']['dsh'] = 'ds.ip=h.ip';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dhostid'] = 'ds.dhostid';
         }
     }
     // dserviceids
     if (!is_null($options['dserviceids'])) {
         zbx_value2array($options['dserviceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['dserviceid'] = 'ds.dserviceid';
         }
         $sql_parts['from']['dservices'] = 'dservices ds';
         $sql_parts['where'][] = DBcondition('ds.dserviceid', $options['dserviceids']);
         $sql_parts['where']['dsh'] = 'ds.ip=h.ip';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dserviceid'] = 'ds.dserviceid';
         }
     }
     // maintenanceids
     if (!is_null($options['maintenanceids'])) {
         zbx_value2array($options['maintenanceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['maintenanceid'] = 'mh.maintenanceid';
         }
         $sql_parts['from']['maintenances_hosts'] = 'maintenances_hosts mh';
         $sql_parts['where'][] = DBcondition('mh.maintenanceid', $options['maintenanceids']);
         $sql_parts['where']['hmh'] = 'h.hostid=mh.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['maintenanceid'] = 'mh.maintenanceid';
         }
     }
     // node check !!!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('h.hostid', $nodeids);
     }
     // monitored_hosts, templated_hosts
     if (!is_null($options['monitored_hosts'])) {
         $sql_parts['where']['status'] = 'h.status=' . HOST_STATUS_MONITORED;
     } else {
         if (!is_null($options['templated_hosts'])) {
             $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ')';
         } else {
             if (!is_null($options['proxy_hosts'])) {
                 $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_PROXY_ACTIVE . ',' . HOST_STATUS_PROXY_PASSIVE . ')';
             } else {
                 $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')';
             }
         }
     }
     // with_items, with_monitored_items, with_historical_items
     if (!is_null($options['with_items'])) {
         $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid )';
     } else {
         if (!is_null($options['with_monitored_items'])) {
             $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND i.status=' . ITEM_STATUS_ACTIVE . ')';
         } else {
             if (!is_null($options['with_historical_items'])) {
                 $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND (i.status=' . ITEM_STATUS_ACTIVE . ' OR i.status=' . ITEM_STATUS_NOTSUPPORTED . ') AND i.lastvalue IS NOT NULL)';
             }
         }
     }
     // with_triggers, with_monitored_triggers
     if (!is_null($options['with_triggers'])) {
         $sql_parts['where'][] = 'EXISTS( ' . ' SELECT i.itemid ' . ' FROM items i, functions f, triggers t ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid)';
     } else {
         if (!is_null($options['with_monitored_triggers'])) {
             $sql_parts['where'][] = 'EXISTS( ' . ' SELECT i.itemid ' . ' FROM items i, functions f, triggers t ' . ' WHERE i.hostid=h.hostid ' . ' AND i.status=' . ITEM_STATUS_ACTIVE . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid ' . ' AND t.status=' . TRIGGER_STATUS_ENABLED . ')';
         }
     }
     // with_httptests, with_monitored_httptests
     if (!is_null($options['with_httptests'])) {
         $sql_parts['where'][] = 'EXISTS( ' . ' SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=h.hostid ' . ' AND ht.applicationid=a.applicationid)';
     } else {
         if (!is_null($options['with_monitored_httptests'])) {
             $sql_parts['where'][] = 'EXISTS( ' . ' SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=h.hostid ' . ' AND ht.applicationid=a.applicationid ' . ' AND ht.status=' . HTTPTEST_STATUS_ACTIVE . ')';
         }
     }
     // with_graphs
     if (!is_null($options['with_graphs'])) {
         $sql_parts['where'][] = 'EXISTS( ' . ' SELECT DISTINCT i.itemid ' . ' FROM items i, graphs_items gi ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=gi.itemid)';
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['hosts'] = 'h.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT h.hostid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('hosts h', $options, $sql_parts);
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('hosts h', $options, $sql_parts);
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][$options['sortfield']] = 'h.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('h.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('h.*', $sql_parts['select'])) {
             $sql_parts['select'][$options['sortfield']] = 'h.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //-------
     $hostids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_group . $sql_order;
     //SDI($sql);
     $res = DBselect($sql, $sql_limit);
     while ($host = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $host;
             } else {
                 $result = $host['rowscount'];
             }
         } else {
             $hostids[$host['hostid']] = $host['hostid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$host['hostid']] = array('hostid' => $host['hostid']);
             } else {
                 if (!isset($result[$host['hostid']])) {
                     $result[$host['hostid']] = array();
                 }
                 if (!is_null($options['select_groups']) && !isset($result[$host['hostid']]['groups'])) {
                     $result[$host['hostid']]['groups'] = array();
                 }
                 if (!is_null($options['selectParentTemplates']) && !isset($result[$host['hostid']]['parentTemplates'])) {
                     $result[$host['hostid']]['parentTemplates'] = array();
                 }
                 if (!is_null($options['select_items']) && !isset($result[$host['hostid']]['items'])) {
                     $result[$host['hostid']]['items'] = array();
                 }
                 if (!is_null($options['select_profile']) && !isset($result[$host['hostid']]['profile'])) {
                     $result[$host['hostid']]['profile'] = array();
                     $result[$host['hostid']]['profile_ext'] = array();
                 }
                 if (!is_null($options['select_triggers']) && !isset($result[$host['hostid']]['triggers'])) {
                     $result[$host['hostid']]['triggers'] = array();
                 }
                 if (!is_null($options['select_graphs']) && !isset($result[$host['hostid']]['graphs'])) {
                     $result[$host['hostid']]['graphs'] = array();
                 }
                 if (!is_null($options['select_dhosts']) && !isset($result[$host['hostid']]['dhosts'])) {
                     $result[$host['hostid']]['dhosts'] = array();
                 }
                 if (!is_null($options['select_dservices']) && !isset($result[$host['hostid']]['dservices'])) {
                     $result[$host['hostid']]['dservices'] = array();
                 }
                 if (!is_null($options['select_applications']) && !isset($result[$host['hostid']]['applications'])) {
                     $result[$host['hostid']]['applications'] = array();
                 }
                 if (!is_null($options['select_macros']) && !isset($result[$host['hostid']]['macros'])) {
                     $result[$host['hostid']]['macros'] = array();
                 }
                 //					if(!is_null($options['select_maintenances']) && !isset($result[$host['hostid']]['maintenances'])){
                 //						$result[$host['hostid']]['maintenances'] = array();
                 //					}
                 // groupids
                 if (isset($host['groupid']) && is_null($options['select_groups'])) {
                     if (!isset($result[$host['hostid']]['groups'])) {
                         $result[$host['hostid']]['groups'] = array();
                     }
                     $result[$host['hostid']]['groups'][] = array('groupid' => $host['groupid']);
                     unset($host['groupid']);
                 }
                 // templateids
                 if (isset($host['templateid'])) {
                     if (!isset($result[$host['hostid']]['templates'])) {
                         $result[$host['hostid']]['templates'] = array();
                     }
                     $result[$host['hostid']]['templates'][] = array('templateid' => $host['templateid']);
                     unset($host['templateid']);
                 }
                 // triggerids
                 if (isset($host['triggerid']) && is_null($options['select_triggers'])) {
                     if (!isset($result[$host['hostid']]['triggers'])) {
                         $result[$host['hostid']]['triggers'] = array();
                     }
                     $result[$host['hostid']]['triggers'][] = array('triggerid' => $host['triggerid']);
                     unset($host['triggerid']);
                 }
                 // itemids
                 if (isset($host['itemid']) && is_null($options['select_items'])) {
                     if (!isset($result[$host['hostid']]['items'])) {
                         $result[$host['hostid']]['items'] = array();
                     }
                     $result[$host['hostid']]['items'][] = array('itemid' => $host['itemid']);
                     unset($host['itemid']);
                 }
                 // graphids
                 if (isset($host['graphid']) && is_null($options['select_graphs'])) {
                     if (!isset($result[$host['hostid']]['graphs'])) {
                         $result[$host['hostid']]['graphs'] = array();
                     }
                     $result[$host['hostid']]['graphs'][] = array('graphid' => $host['graphid']);
                     unset($host['graphid']);
                 }
                 // dhostids
                 if (isset($host['dhostid']) && is_null($options['select_dhosts'])) {
                     if (!isset($result[$host['hostid']]['dhosts'])) {
                         $result[$host['hostid']]['dhosts'] = array();
                     }
                     $result[$host['hostid']]['dhosts'][] = array('dhostid' => $host['dhostid']);
                     unset($host['dhostid']);
                 }
                 // dserviceids
                 if (isset($host['dserviceid']) && is_null($options['select_dservices'])) {
                     if (!isset($result[$host['hostid']]['dservices'])) {
                         $result[$host['hostid']]['dservices'] = array();
                     }
                     $result[$host['hostid']]['dservices'][] = array('dserviceid' => $host['dserviceid']);
                     unset($host['dserviceid']);
                 }
                 // maintenanceids
                 if (isset($host['maintenanceid'])) {
                     if (!isset($result[$host['hostid']]['maintenanceid'])) {
                         $result[$host['hostid']]['maintenances'] = array();
                     }
                     $result[$host['hostid']]['maintenances'][] = array('maintenanceid' => $host['maintenanceid']);
                     //						unset($host['maintenanceid']);
                 }
                 //---
                 $result[$host['hostid']] += $host;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding Groups
     if (!is_null($options['select_groups']) && str_in_array($options['select_groups'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_groups'], 'hostids' => $hostids, 'preservekeys' => 1);
         $groups = CHostgroup::get($obj_params);
         foreach ($groups as $groupid => $group) {
             $ghosts = $group['hosts'];
             unset($group['hosts']);
             foreach ($ghosts as $num => $host) {
                 $result[$host['hostid']]['groups'][] = $group;
             }
         }
     }
     // Adding Profiles
     if (!is_null($options['select_profile'])) {
         $sql = 'SELECT hp.* ' . ' FROM hosts_profiles hp ' . ' WHERE ' . DBcondition('hp.hostid', $hostids);
         $db_profile = DBselect($sql);
         while ($profile = DBfetch($db_profile)) {
             $result[$profile['hostid']]['profile'] = $profile;
         }
         $sql = 'SELECT hpe.* ' . ' FROM hosts_profiles_ext hpe ' . ' WHERE ' . DBcondition('hpe.hostid', $hostids);
         $db_profile_ext = DBselect($sql);
         while ($profile_ext = DBfetch($db_profile_ext)) {
             $result[$profile_ext['hostid']]['profile_ext'] = $profile_ext;
         }
     }
     // Adding Templates
     if (!is_null($options['selectParentTemplates'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'preservekeys' => 1);
         if (is_array($options['selectParentTemplates']) || str_in_array($options['selectParentTemplates'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectParentTemplates'];
             $templates = CTemplate::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($templates, 'host');
             }
             foreach ($templates as $templateid => $template) {
                 unset($templates[$templateid]['hosts']);
                 $count = array();
                 foreach ($template['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['parentTemplates'][] =& $templates[$templateid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectParentTemplates']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $templates = CTemplate::get($obj_params);
                 $templates = zbx_toHash($templates, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($templates[$hostid])) {
                         $result[$hostid]['templates'] = $templates[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['templates'] = 0;
                     }
                 }
             }
         }
     }
     // Adding Items
     if (!is_null($options['select_items'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_items']) || str_in_array($options['select_items'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_items'];
             $items = CItem::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($items, 'description');
             }
             foreach ($items as $itemid => $item) {
                 unset($items[$itemid]['hosts']);
                 foreach ($item['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['items'][] =& $items[$itemid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_items']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $items = CItem::get($obj_params);
                 $items = zbx_toHash($items, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($items[$hostid])) {
                         $result[$hostid]['items'] = $items[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['items'] = 0;
                     }
                 }
             }
         }
     }
     // Adding triggers
     if (!is_null($options['select_triggers'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_triggers']) || str_in_array($options['select_triggers'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_triggers'];
             $triggers = CTrigger::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             foreach ($triggers as $triggerid => $trigger) {
                 unset($triggers[$triggerid]['hosts']);
                 foreach ($trigger['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['triggers'][] =& $triggers[$triggerid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_triggers']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $triggers = CTrigger::get($obj_params);
                 $triggers = zbx_toHash($triggers, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($triggers[$hostid])) {
                         $result[$hostid]['triggers'] = $triggers[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['triggers'] = 0;
                     }
                 }
             }
         }
     }
     // Adding graphs
     if (!is_null($options['select_graphs'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_graphs']) || str_in_array($options['select_graphs'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_graphs'];
             $graphs = CGraph::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             foreach ($graphs as $graphid => $graph) {
                 unset($graphs[$graphid]['hosts']);
                 foreach ($graph['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['graphs'][] =& $graphs[$graphid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_graphs']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $graphs = CGraph::get($obj_params);
                 $graphs = zbx_toHash($graphs, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($graphs[$hostid])) {
                         $result[$hostid]['graphs'] = $graphs[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['graphs'] = 0;
                     }
                 }
             }
         }
     }
     // Adding discovery hosts
     if (!is_null($options['select_dhosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_dhosts']) || str_in_array($options['select_dhosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_dhosts'];
             $dhosts = CDHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($dhosts, 'dhostid');
             }
             foreach ($dhosts as $dhostid => $dhost) {
                 unset($dhosts[$dhostid]['hosts']);
                 foreach ($dhost['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['dhosts'][] =& $dhosts[$dhostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_dhosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $dhosts = CDHost::get($obj_params);
                 $dhosts = zbx_toHash($dhosts, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($dhosts[$hostid])) {
                         $result[$hostid]['dhosts'] = $dhosts[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['dhosts'] = 0;
                     }
                 }
             }
         }
     }
     // Adding applications
     if (!is_null($options['select_applications'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_applications']) || str_in_array($options['select_applications'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_applications'];
             $applications = CApplication::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($applications, 'name');
             }
             foreach ($applications as $applicationid => $application) {
                 unset($applications[$applicationid]['hosts']);
                 foreach ($application['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['applications'][] =& $applications[$applicationid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_applications']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $applications = CApplication::get($obj_params);
                 $applications = zbx_toHash($applications, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($applications[$hostid])) {
                         $result[$hostid]['applications'] = $applications[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['applications'] = 0;
                     }
                 }
             }
         }
     }
     // Adding macros
     if (!is_null($options['select_macros']) && str_in_array($options['select_macros'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_macros'], 'hostids' => $hostids, 'preservekeys' => 1);
         $macros = CUserMacro::get($obj_params);
         foreach ($macros as $macroid => $macro) {
             $mhosts = $macro['hosts'];
             unset($macro['hosts']);
             foreach ($mhosts as $num => $host) {
                 $result[$host['hostid']]['macros'][] = $macro;
             }
         }
     }
     Copt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Esempio n. 29
0
 /**
  * Graph::DrawGraph()
  * Draw all the graph: bg, axis, bars, text.. and output it
  * Optional file parameter turns output to file, and bool on success
  **/
 function DrawGraph($file = "")
 {
     $this->im = imagecreatetruecolor($this->graph_width, $this->graph_height);
     CGraph::DrawGraph();
     $this->__Draw_LeftBottom_Axis();
     $arrki = array_keys($this->data);
     if (is_array($this->data[$arrki[0]])) {
         // more than 1 line
         if (!is_array($this->line_color)) {
             $this->line_color = array($this->line_color);
         }
         if (!is_array($this->line_bgcolor)) {
             $this->line_bgcolor = array($this->line_bgcolor);
         }
         for ($i = 0; $i < count($arrki); $i++) {
             $this->__AllocateColor("im_line_color", $this->line_color[$i], $this->graph_transparencylevel, $i);
             if ($this->axis_deepness > 0) {
                 $this->__AllocateColor("im_line_bgcolor", $this->line_bgcolor[$i], $this->graph_transparencylevel, $i);
             }
             $arrkj = array_keys($this->data[$arrki[$i]]);
             for ($j = 1; $j < count($arrkj); $j++) {
                 $this->__DrawLine(array($arrkj[$j - 1], $arrkj[$j], $this->data[$arrki[$i]][$arrkj[$j - 1]], $this->data[$arrki[$i]][$arrkj[$j]]), $this->im_line_color[$i], $this->im_line_bgcolor[$i]);
             }
         }
     } else {
         $this->__AllocateColor("im_line_color", $this->line_color, $this->graph_transparencylevel);
         $this->__AllocateColor("im_line_bgcolor", $this->line_bgcolor, $this->graph_transparencylevel);
         for ($i = 1; $i < count($arrki); $i++) {
             $this->__DrawLine(array($arrki[$i - 1], $arrki[$i], $this->data[$arrki[$i - 1]], $this->data[$arrki[$i]]), $this->im_line_color, $this->im_line_bgcolor);
         }
     }
     $this->__Draw_TopRight_Axis();
     CGraph::DrawGraph2();
     if (strlen($file)) {
         $ret = imagepng($this->im, $file);
     } else {
         header("Content-Type: image/png");
         // thanks to Marcin G. :)
         imagepng($this->im);
         $ret = true;
     }
     imagedestroy($this->im);
     return $ret;
 }