/** * Process screen. * * @return CDiv (screen inside container) */ public function get() { // if screen is defined in template, then 'real_resourceid' is defined and should be used if (!empty($this->screenitem['real_resourceid'])) { $this->screenitem['resourceid'] = $this->screenitem['real_resourceid']; } if ($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM && !empty($this->hostid)) { $newitemid = get_same_item_for_host($this->screenitem['resourceid'], $this->hostid); $this->screenitem['resourceid'] = !empty($newitemid) ? $newitemid : 0; } if ($this->screenitem['resourceid'] == 0) { $table = (new CTableInfo())->setHeader([_('Timestamp'), _('Item')]); return $this->getOutput($table); } $items = CMacrosResolverHelper::resolveItemNames([get_item_by_itemid($this->screenitem['resourceid'])]); $item = reset($items); switch ($item['value_type']) { case ITEM_VALUE_TYPE_TEXT: case ITEM_VALUE_TYPE_LOG: $orderField = 'id'; break; case ITEM_VALUE_TYPE_FLOAT: case ITEM_VALUE_TYPE_UINT64: default: $orderField = ['itemid', 'clock']; } $host = get_host_by_itemid($this->screenitem['resourceid']); $table = (new CTableInfo())->setHeader([_('Timestamp'), _('Value')]); $stime = zbxDateToTime($this->timeline['stime']); $histories = API::History()->get(['history' => $item['value_type'], 'itemids' => $this->screenitem['resourceid'], 'output' => API_OUTPUT_EXTEND, 'sortorder' => ZBX_SORT_DOWN, 'sortfield' => $orderField, 'limit' => $this->screenitem['elements'], 'time_from' => $stime, 'time_till' => $stime + $this->timeline['period']]); foreach ($histories as $history) { switch ($item['value_type']) { case ITEM_VALUE_TYPE_FLOAT: sscanf($history['value'], '%f', $value); break; case ITEM_VALUE_TYPE_TEXT: case ITEM_VALUE_TYPE_STR: case ITEM_VALUE_TYPE_LOG: $value = $this->screenitem['style'] ? new CJsScript($history['value']) : $history['value']; break; default: $value = $history['value']; break; } if ($item['valuemapid'] > 0) { $value = applyValueMap($value, $item['valuemapid']); } if ($this->screenitem['style'] == 0) { $value = new CPre($value); } $table->addRow([zbx_date2str(DATE_TIME_FORMAT_SECONDS, $history['clock']), $value]); } $footer = (new CList())->addItem(_s('Updated: %s', zbx_date2str(TIME_FORMAT_SECONDS)))->addClass(ZBX_STYLE_DASHBRD_WIDGET_FOOT); return $this->getOutput((new CUiWidget(uniqid(), [$table, $footer]))->setHeader($host['name'] . NAME_DELIMITER . $item['name_expanded'])); }
/** * Process screen. * * @return CDiv (screen inside container) */ public function get() { // if screen is defined in template, then 'real_resourceid' is defined and should be used if (!empty($this->screenitem['real_resourceid'])) { $this->screenitem['resourceid'] = $this->screenitem['real_resourceid']; } if ($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM && !empty($this->hostid)) { $newitemid = get_same_item_for_host($this->screenitem['resourceid'], $this->hostid); $this->screenitem['resourceid'] = !empty($newitemid) ? $newitemid : 0; } if ($this->screenitem['resourceid'] == 0) { $table = new CTableInfo(_('No values found.')); $table->setHeader(array(_('Timestamp'), _('Item'))); return $this->getOutput($table); } $items = CMacrosResolverHelper::resolveItemNames(array(get_item_by_itemid($this->screenitem['resourceid']))); $item = reset($items); switch ($item['value_type']) { case ITEM_VALUE_TYPE_TEXT: case ITEM_VALUE_TYPE_LOG: $orderField = 'id'; break; case ITEM_VALUE_TYPE_FLOAT: case ITEM_VALUE_TYPE_UINT64: default: $orderField = array('itemid', 'clock'); } $host = get_host_by_itemid($this->screenitem['resourceid']); $table = new CTableInfo(_('No values found.')); $table->setHeader(array(_('Timestamp'), $host['name'] . NAME_DELIMITER . $item['name_expanded'])); $stime = zbxDateToTime($this->timeline['stime']); $histories = API::History()->get(array('history' => $item['value_type'], 'itemids' => $this->screenitem['resourceid'], 'output' => API_OUTPUT_EXTEND, 'sortorder' => ZBX_SORT_DOWN, 'sortfield' => $orderField, 'limit' => $this->screenitem['elements'], 'time_from' => $stime, 'time_till' => $stime + $this->timeline['period'])); foreach ($histories as $history) { switch ($item['value_type']) { case ITEM_VALUE_TYPE_FLOAT: sscanf($history['value'], '%f', $value); break; case ITEM_VALUE_TYPE_TEXT: case ITEM_VALUE_TYPE_STR: case ITEM_VALUE_TYPE_LOG: $value = $this->screenitem['style'] ? new CJsScript($history['value']) : $history['value']; break; default: $value = $history['value']; break; } if ($item['valuemapid'] > 0) { $value = applyValueMap($value, $item['valuemapid']); } $class = $this->screenitem['style'] ? null : 'pre'; $table->addRow(array(zbx_date2str(DATE_TIME_FORMAT_SECONDS, $history['clock']), new CCol($value, $class))); } return $this->getOutput($table); }
function insert_graph_form() { $frmGraph = new CFormTable(S_GRAPH, null, 'post'); $frmGraph->SetName('frm_graph'); $frmGraph->SetHelp("web.graphs.graph.php"); $frmGraph->SetMethod('post'); $items = get_request('items', array()); if (isset($_REQUEST['graphid'])) { $frmGraph->addVar('graphid', $_REQUEST['graphid']); $result = DBselect('SELECT * FROM graphs WHERE graphid=' . $_REQUEST['graphid']); $row = DBfetch($result); $frmGraph->SetTitle(S_GRAPH . ' "' . $row['name'] . '"'); } if (isset($_REQUEST['graphid']) && !isset($_REQUEST['form_refresh'])) { $name = $row['name']; $width = $row['width']; $height = $row['height']; $ymin_type = $row["ymin_type"]; $ymax_type = $row["ymax_type"]; $yaxismin = $row['yaxismin']; $yaxismax = $row['yaxismax']; $ymin_itemid = $row["ymin_itemid"]; $ymax_itemid = $row["ymax_itemid"]; $showworkperiod = $row['show_work_period']; $showtriggers = $row['show_triggers']; $graphtype = $row['graphtype']; $legend = $row['show_legend']; $graph3d = $row['show_3d']; $percent_left = $row['percent_left']; $percent_right = $row['percent_right']; $db_items = DBselect('SELECT * FROM graphs_items WHERE graphid=' . $_REQUEST['graphid']); while ($item = DBfetch($db_items)) { array_push($items, array('itemid' => $item['itemid'], 'drawtype' => $item['drawtype'], 'sortorder' => $item['sortorder'], 'color' => $item['color'], 'yaxisside' => $item['yaxisside'], 'calc_fnc' => $item['calc_fnc'], 'type' => $item['type'], 'periods_cnt' => $item['periods_cnt'])); } } else { $name = get_request('name', ''); $graphtype = get_request('graphtype', GRAPH_TYPE_NORMAL); if ($graphtype == GRAPH_TYPE_PIE || $graphtype == GRAPH_TYPE_EXPLODED) { $width = get_request('width', 400); $height = get_request('height', 300); } else { $width = get_request('width', 900); $height = get_request('height', 200); } $ymin_type = get_request("ymin_type", GRAPH_YAXIS_TYPE_CALCULATED); $ymax_type = get_request("ymax_type", GRAPH_YAXIS_TYPE_CALCULATED); $yaxismin = get_request("yaxismin", 0.0); $yaxismax = get_request("yaxismax", 100.0); $ymin_itemid = get_request("ymin_itemid", 0); $ymax_itemid = get_request("ymax_itemid", 0); $showworkperiod = get_request('showworkperiod', 0); $showtriggers = get_request('showtriggers', 0); $legend = get_request('legend', 0); $graph3d = get_request('graph3d', 0); $visible = get_request('visible'); $percent_left = 0; $percent_right = 0; if (isset($visible['percent_left'])) { $percent_left = get_request('percent_left', 0); } if (isset($visible['percent_right'])) { $percent_right = get_request('percent_right', 0); } } /* reinit $_REQUEST */ $_REQUEST['items'] = $items; $_REQUEST['name'] = $name; $_REQUEST['width'] = $width; $_REQUEST['height'] = $height; $_REQUEST['ymin_type'] = $ymin_type; $_REQUEST['ymax_type'] = $ymax_type; $_REQUEST['yaxismin'] = $yaxismin; $_REQUEST['yaxismax'] = $yaxismax; $_REQUEST['ymin_itemid'] = $ymin_itemid; $_REQUEST['ymax_itemid'] = $ymax_itemid; $_REQUEST['showworkperiod'] = $showworkperiod; $_REQUEST['showtriggers'] = $showtriggers; $_REQUEST['graphtype'] = $graphtype; $_REQUEST['legend'] = $legend; $_REQUEST['graph3d'] = $graph3d; $_REQUEST['percent_left'] = $percent_left; $_REQUEST['percent_right'] = $percent_right; /********************/ if ($graphtype != GRAPH_TYPE_NORMAL) { foreach ($items as $gid => $gitem) { if ($gitem['type'] != GRAPH_ITEM_AGGREGATED) { continue; } unset($items[$gid]); } } asort_by_key($items, 'sortorder'); $group_gid = get_request('group_gid', array()); $frmGraph->addVar('ymin_itemid', $ymin_itemid); $frmGraph->addVar('ymax_itemid', $ymax_itemid); $frmGraph->addRow(S_NAME, new CTextBox('name', $name, 32)); $g_width = new CNumericBox('width', $width, 5); $g_width->addoption('onblur', 'javascript: submit();'); $frmGraph->addRow(S_WIDTH, $g_width); $g_height = new CNumericBox('height', $height, 5); $g_height->addoption('onblur', 'javascript: submit();'); $frmGraph->addRow(S_HEIGHT, $g_height); $cmbGType = new CComboBox('graphtype', $graphtype, 'graphs.submit(this)'); $cmbGType->addItem(GRAPH_TYPE_NORMAL, S_NORMAL); $cmbGType->addItem(GRAPH_TYPE_STACKED, S_STACKED); $cmbGType->addItem(GRAPH_TYPE_PIE, S_PIE); $cmbGType->addItem(GRAPH_TYPE_EXPLODED, S_EXPLODED); zbx_add_post_js('graphs.graphtype = ' . $graphtype . ";\n"); $frmGraph->addRow(S_GRAPH_TYPE, $cmbGType); if ($graphtype == GRAPH_TYPE_NORMAL || $graphtype == GRAPH_TYPE_STACKED) { $frmGraph->addRow(S_SHOW_WORKING_TIME, new CCheckBox('showworkperiod', $showworkperiod, null, 1)); $frmGraph->addRow(S_SHOW_TRIGGERS, new CCheckBox('showtriggers', $showtriggers, null, 1)); if ($graphtype == GRAPH_TYPE_NORMAL) { $percent_left = sprintf("%2.2f", $percent_left); $percent_right = sprintf("%2.2f", $percent_right); $pr_left_input = new CTextBox('percent_left', $percent_left, '5'); $pr_left_chkbx = new CCheckBox('visible[percent_left]', 1, "javascript: ShowHide('percent_left');", 1); if ($percent_left == 0) { $pr_left_input->addOption('style', 'display: none;'); $pr_left_chkbx->SetChecked(0); } $pr_right_input = new CTextBox('percent_right', $percent_right, '5'); $pr_right_chkbx = new CCheckBox('visible[percent_right]', 1, "javascript: ShowHide('percent_right');", 1); if ($percent_right == 0) { $pr_right_input->addOption('style', 'display: none;'); $pr_right_chkbx->SetChecked(0); } $frmGraph->addRow(S_PERCENTILE_LINE . ' (' . S_LEFT . ')', array($pr_left_chkbx, $pr_left_input)); $frmGraph->addRow(S_PERCENTILE_LINE . ' (' . S_RIGHT . ')', array($pr_right_chkbx, $pr_right_input)); } $yaxis_min = array(); $cmbYType = new CComboBox('ymin_type', $ymin_type, 'javascript: submit();'); $cmbYType->addItem(GRAPH_YAXIS_TYPE_CALCULATED, S_CALCULATED); $cmbYType->addItem(GRAPH_YAXIS_TYPE_FIXED, S_FIXED); $cmbYType->addItem(GRAPH_YAXIS_TYPE_ITEM_VALUE, S_ITEM); $yaxis_min[] = $cmbYType; if ($ymin_type == GRAPH_YAXIS_TYPE_FIXED) { $yaxis_min[] = new CTextBox("yaxismin", $yaxismin, 9); } else { if ($ymin_type == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $frmGraph->addVar('yaxismin', $yaxismin); $ymin_name = ''; if ($ymin_itemid > 0) { $min_host = get_host_by_itemid($ymin_itemid); $min_item = get_item_by_itemid($ymin_itemid); $ymin_name = $min_host['host'] . ':' . item_description($min_item); } $yaxis_min[] = new CTextBox("ymin_name", $ymin_name, 80, 'yes'); $yaxis_min[] = new CButton('yaxis_min', S_SELECT, 'javascript: ' . "return PopUp('popup.php?dstfrm=" . $frmGraph->getName() . "&dstfld1=ymin_itemid" . "&dstfld2=ymin_name" . "&srctbl=items" . "&srcfld1=itemid" . "&srcfld2=description',0,0,'zbx_popup_item');"); } else { $frmGraph->addVar('yaxismin', $yaxismin); } } $frmGraph->addRow(S_YAXIS_MIN_VALUE, $yaxis_min); $yaxis_max = array(); $cmbYType = new CComboBox("ymax_type", $ymax_type, "submit()"); $cmbYType->addItem(GRAPH_YAXIS_TYPE_CALCULATED, S_CALCULATED); $cmbYType->addItem(GRAPH_YAXIS_TYPE_FIXED, S_FIXED); $cmbYType->addItem(GRAPH_YAXIS_TYPE_ITEM_VALUE, S_ITEM); $yaxis_max[] = $cmbYType; if ($ymax_type == GRAPH_YAXIS_TYPE_FIXED) { $yaxis_max[] = new CTextBox("yaxismax", $yaxismax, 9); } else { if ($ymax_type == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $frmGraph->addVar('yaxismax', $yaxismax); $ymax_name = ''; if ($ymax_itemid > 0) { $max_host = get_host_by_itemid($ymax_itemid); $max_item = get_item_by_itemid($ymax_itemid); $ymax_name = $max_host['host'] . ':' . item_description($max_item); } $yaxis_max[] = new CTextBox("ymax_name", $ymax_name, 80, 'yes'); $yaxis_max[] = new CButton('yaxis_max', S_SELECT, 'javascript: ' . "return PopUp('popup.php?dstfrm=" . $frmGraph->getName() . "&dstfld1=ymax_itemid" . "&dstfld2=ymax_name" . "&srctbl=items" . "&srcfld1=itemid" . "&srcfld2=description',0,0,'zbx_popup_item');"); } else { $frmGraph->addVar('yaxismax', $yaxismax); } } $frmGraph->addRow(S_YAXIS_MAX_VALUE, $yaxis_max); } else { $frmGraph->addRow(S_3D_VIEW, new CCheckBox('graph3d', $graph3d, 'javascript: graphs.submit(this);', 1)); $frmGraph->addRow(S_LEGEND, new CCheckBox('legend', $legend, 'javascript: graphs.submit(this);', 1)); } $only_hostid = null; $monitored_hosts = null; if (count($items)) { $frmGraph->addVar('items', $items); $items_table = new CTableInfo(); foreach ($items as $gid => $gitem) { if ($graphtype == GRAPH_TYPE_STACKED && $gitem['type'] == GRAPH_ITEM_AGGREGATED) { continue; } $host = get_host_by_itemid($gitem['itemid']); $item = get_item_by_itemid($gitem['itemid']); if ($host['status'] == HOST_STATUS_TEMPLATE) { $only_hostid = $host['hostid']; } else { $monitored_hosts = 1; } if ($gitem['type'] == GRAPH_ITEM_AGGREGATED) { $color = '-'; } else { $color = new CColorCell(null, $gitem['color']); } $do_up = new CLink(S_UP, '#', 'action'); $do_up->OnClick("return create_var('" . $frmGraph->GetName() . "','move_up'," . $gid . ", true);"); $do_down = new CLink(S_DOWN, '#', 'action'); $do_down->OnClick("return create_var('" . $frmGraph->GetName() . "','move_down'," . $gid . ", true);"); $description = new CLink($host['host'] . ': ' . item_description($item), '#', 'action'); $description->OnClick('return PopUp("popup_gitem.php?list_name=items&dstfrm=' . $frmGraph->GetName() . url_param($only_hostid, false, 'only_hostid') . url_param($monitored_hosts, false, 'monitored_hosts') . url_param($graphtype, false, 'graphtype') . url_param($gitem, false) . url_param($gid, false, 'gid') . url_param(get_request('graphid', 0), false, 'graphid') . '",550,400,"graph_item_form");'); if ($graphtype == GRAPH_TYPE_PIE || $graphtype == GRAPH_TYPE_EXPLODED) { $items_table->addRow(array(new CCheckBox('group_gid[' . $gid . ']', isset($group_gid[$gid])), $gitem['sortorder'], $description, graph_item_calc_fnc2str($gitem["calc_fnc"], $gitem["type"]), graph_item_type2str($gitem['type'], $gitem["periods_cnt"]), $color, array($do_up, SPACE . "|" . SPACE, $do_down))); } else { $items_table->addRow(array(new CCheckBox('group_gid[' . $gid . ']', isset($group_gid[$gid])), $gitem['sortorder'], $description, graph_item_calc_fnc2str($gitem["calc_fnc"], $gitem["type"]), graph_item_type2str($gitem['type'], $gitem["periods_cnt"]), graph_item_drawtype2str($gitem["drawtype"], $gitem["type"]), $color, array($do_up, SPACE . "|" . SPACE, $do_down))); } } $dedlete_button = new CButton('delete_item', S_DELETE_SELECTED); } else { $items_table = $dedlete_button = null; } $frmGraph->addRow(S_ITEMS, array($items_table, new CButton('add_item', S_ADD, "return PopUp('popup_gitem.php?dstfrm=" . $frmGraph->GetName() . url_param($only_hostid, false, 'only_hostid') . url_param($monitored_hosts, false, 'monitored_hosts') . url_param($graphtype, false, 'graphtype') . "',550,400,'graph_item_form');"), $dedlete_button)); unset($items_table, $dedlete_button); $frmGraph->addItemToBottomRow(new CButton("save", S_SAVE)); if (isset($_REQUEST["graphid"])) { $frmGraph->addItemToBottomRow(SPACE); $frmGraph->addItemToBottomRow(new CButton("clone", S_CLONE)); $frmGraph->addItemToBottomRow(SPACE); $frmGraph->addItemToBottomRow(new CButtonDelete(S_DELETE_GRAPH_Q, url_param("graphid") . url_param('groupid') . url_param("hostid"))); } $frmGraph->addItemToBottomRow(SPACE); $frmGraph->addItemToBottomRow(new CButtonCancel(url_param('groupid') . url_param("hostid"))); $frmGraph->Show(); }
/** * Search items by same key in destination host. * * @param array $gitems * @param string $destinationHostId * @param bool $error if false error won't be thrown when item does not exist * @param array $flags * * @return array|bool */ function getSameGraphItemsForHost($gitems, $destinationHostId, $error = true, array $flags = array()) { $result = array(); $flagsSql = $flags ? ' AND ' . dbConditionInt('dest.flags', $flags) : ''; foreach ($gitems as $gitem) { $dbItem = DBfetch(DBselect('SELECT dest.itemid,src.key_' . ' FROM items dest,items src' . ' WHERE dest.key_=src.key_' . ' AND dest.hostid=' . zbx_dbstr($destinationHostId) . ' AND src.itemid=' . zbx_dbstr($gitem['itemid']) . $flagsSql)); if ($dbItem) { $gitem['itemid'] = $dbItem['itemid']; $gitem['key_'] = $dbItem['key_']; } elseif ($error) { $item = get_item_by_itemid($gitem['itemid']); $host = get_host_by_hostid($destinationHostId); error(_s('Missing key "%1$s" for host "%2$s".', $item['key_'], $host['host'])); return false; } else { continue; } $result[] = $gitem; } return $result; }
/** * Gets all item data from DB by itemid * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @static * @param int $item_data * @param int $item_data['itemid'] * @return array|boolean item data || false if error */ public static function getById($item_data) { $item = get_item_by_itemid($item_data['itemid']); $result = $item ? true : false; if ($result) { return $item; } else { self::$error = array('error' => ZBX_API_ERROR_NO_HOST, 'data' => 'Item with id: ' . $itemid . ' doesn\'t exists.'); return false; } }
protected function selectData() { $this->data = array(); $now = time(null); if (isset($this->stime)) { $this->from_time = $this->stime; $this->to_time = $this->stime + $this->period; } else { $this->to_time = $now - SEC_PER_HOUR * $this->from; $this->from_time = $this->to_time - $this->period; } $strvaluelength = 0; // we need to know how long in px will be our legend for ($i = 0; $i < $this->num; $i++) { $real_item = get_item_by_itemid($this->items[$i]['itemid']); $type = $this->items[$i]['calc_type']; $from_time = $this->from_time; $to_time = $this->to_time; $sql_arr = array(); if (ZBX_HISTORY_DATA_UPKEEP > -1) { $real_item['history'] = ZBX_HISTORY_DATA_UPKEEP; } if ($real_item['history'] * SEC_PER_DAY > time() - ($from_time + $this->period / 2) && $this->period / $this->sizeX <= ZBX_MAX_TREND_DIFF / ZBX_GRAPH_MAX_SKIP_CELL) { // is reasonable to take data from history? $this->dataFrom = 'history'; array_push($sql_arr, 'SELECT h.itemid,' . 'AVG(h.value) AS avg,MIN(h.value) AS min,' . 'MAX(h.value) AS max,MAX(h.clock) AS clock,' . 'MAX(i.lastvalue) AS lst' . ' FROM history h' . ' LEFT JOIN items i ON h.itemid=i.itemid' . ' WHERE h.itemid=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND h.clock>=' . zbx_dbstr($from_time) . ' AND h.clock<=' . zbx_dbstr($to_time) . ' GROUP BY h.itemid', 'SELECT hu.itemid,' . 'AVG(hu.value) AS avg,MIN(hu.value) AS min,' . 'MAX(hu.value) AS max,MAX(hu.clock) AS clock,' . 'MAX(i.lastvalue) AS lst' . ' FROM history_uint hu' . ' LEFT JOIN items i ON hu.itemid=i.itemid' . ' WHERE hu.itemid=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND hu.clock>=' . zbx_dbstr($from_time) . ' AND hu.clock<=' . zbx_dbstr($to_time) . ' GROUP BY hu.itemid'); } else { $this->dataFrom = 'trends'; array_push($sql_arr, 'SELECT t.itemid,' . 'AVG(t.value_avg) AS avg,MIN(t.value_min) AS min,' . 'MAX(t.value_max) AS max,MAX(t.clock) AS clock,' . 'MAX(i.lastvalue) AS lst' . ' FROM trends t' . ' LEFT JOIN items i ON t.itemid=i.itemid' . ' WHERE t.itemid=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND t.clock>=' . zbx_dbstr($from_time) . ' AND t.clock<=' . zbx_dbstr($to_time) . ' GROUP BY t.itemid', 'SELECT t.itemid,' . 'AVG(t.value_avg) AS avg,MIN(t.value_min) AS min,' . 'MAX(t.value_max) AS max,MAX(t.clock) AS clock,' . 'MAX(i.lastvalue) AS lst' . ' FROM trends_uint t' . ' LEFT JOIN items i ON t.itemid=i.itemid' . ' WHERE t.itemid=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND t.clock>=' . zbx_dbstr($from_time) . ' AND t.clock<=' . zbx_dbstr($to_time) . ' GROUP BY t.itemid'); } $curr_data =& $this->data[$this->items[$i]['itemid']][$type]; $curr_data->min = null; $curr_data->max = null; $curr_data->avg = null; $curr_data->clock = null; foreach ($sql_arr as $sql) { $result = DBselect($sql); while ($row = DBfetch($result)) { $curr_data->min = $row['min']; $curr_data->max = $row['max']; $curr_data->avg = $row['avg']; $curr_data->lst = $row['lst']; $curr_data->clock = $row['clock']; $curr_data->shift_min = 0; $curr_data->shift_max = 0; $curr_data->shift_avg = 0; } unset($row); } switch ($this->items[$i]['calc_fnc']) { case CALC_FNC_MIN: $item_value = abs($curr_data->min); break; case CALC_FNC_MAX: $item_value = abs($curr_data->max); break; case CALC_FNC_LST: $item_value = abs($curr_data->lst); break; case CALC_FNC_AVG: default: $item_value = abs($curr_data->avg); } if ($type == GRAPH_ITEM_SUM) { $this->background = $i; $graph_sum = $item_value; } $this->sum += $item_value; $strvaluelength = max($strvaluelength, zbx_strlen(convert_units($item_value, $this->items[$i]['unit']))); } if (isset($graph_sum)) { $this->sum = $graph_sum; } $this->shiftlegendright += $strvaluelength * 7; }
protected function calculateMaxY($side) { if ($this->ymax_type == GRAPH_YAXIS_TYPE_FIXED) { return $this->yaxismax; } else { if ($this->ymax_type == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $item = get_item_by_itemid($this->ymax_itemid); if ($item && isset($item['lastvalue']) && !is_null($item['lastvalue'])) { return $item['lastvalue']; } } } $maxY = null; for ($i = 0; $i < $this->num; $i++) { if ($this->items[$i]['axisside'] != $side) { continue; } foreach (array(GRAPH_ITEM_SIMPLE, GRAPH_ITEM_AGGREGATED) as $type) { if (!isset($this->data[$this->items[$i]['itemid']][$type])) { continue; } $data =& $this->data[$this->items[$i]['itemid']][$type]; if (!isset($data)) { continue; } if ($type == GRAPH_ITEM_AGGREGATED) { $calc_fnc = CALC_FNC_ALL; } else { $calc_fnc = $this->items[$i]['calc_fnc']; } switch ($calc_fnc) { case CALC_FNC_ALL: /* use max */ /* use max */ case CALC_FNC_MAX: $val = $data['max']; $shift_val = $data['shift_max']; break; case CALC_FNC_MIN: $val = $data['min']; $shift_val = $data['shift_min']; break; case CALC_FNC_AVG: default: $val = $data['avg']; $shift_val = $data['shift_avg']; } if (!isset($val)) { continue; } for ($ci = 0; $ci < min(count($val), count($shift_val)); $ci++) { if ($data['count'][$ci] == 0) { continue; } $val[$ci] = bcadd($shift_val[$ci], $val[$ci]); } if (!isset($maxY)) { if (isset($val) && count($val) > 0) { $maxY = max($val); } } else { $maxY = max($maxY, max($val)); } } } return $maxY; }
} } DBstart(); $go_result = copyItemsToHosts($_REQUEST['group_itemid'], $hosts_ids); $go_result = DBend($go_result); show_messages($go_result, _('Items copied'), _('Cannot copy items')); $_REQUEST['go'] = 'none2'; } else { show_error_message(_('No target selected.')); } } elseif ($_REQUEST['go'] == 'clean_history' && isset($_REQUEST['group_itemid'])) { DBstart(); $go_result = delete_history_by_itemid($_REQUEST['group_itemid']); DBexecute('UPDATE items SET lastvalue=null,lastclock=null,prevvalue=null WHERE ' . dbConditionInt('itemid', $_REQUEST['group_itemid'])); foreach ($_REQUEST['group_itemid'] as $id) { if (!($item = get_item_by_itemid($id))) { continue; } $host = get_host_by_hostid($item['hostid']); add_audit(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_ITEM, _('Item') . ' [' . $item['key_'] . '] [' . $id . '] ' . _('Host') . ' [' . $host['host'] . '] ' . _('History cleared')); } $go_result = DBend($go_result); show_messages($go_result, _('History cleared'), $go_result); } elseif ($_REQUEST['go'] == 'delete' && isset($_REQUEST['group_itemid'])) { DBstart(); $group_itemid = $_REQUEST['group_itemid']; $itemsToDelete = API::Item()->get(array('output' => array('key_', 'itemid'), 'selectHosts' => array('name'), 'itemids' => $group_itemid, 'preservekeys' => true)); $go_result = API::Item()->delete($group_itemid); if ($go_result) { foreach ($itemsToDelete as $item) { $host = reset($item['hosts']);
function construct_expression($itemid, $expressions) { $complite_expr = ''; $item = get_item_by_itemid($itemid); $host = get_host_by_itemid($itemid); $prefix = $host['host'] . ':' . $item['key_'] . '.'; if (empty($expressions)) { error(_('Expression cannot be empty')); return false; } $ZBX_PREG_EXPESSION_FUNC_FORMAT = '^([' . ZBX_PREG_PRINT . ']*)([&|]{1})[(]*(([a-zA-Z_.\\$]{6,7})(\\(([' . ZBX_PREG_PRINT . ']+?){0,1}\\)))([' . ZBX_PREG_PRINT . ']*)$'; $functions = array('regexp' => 1, 'iregexp' => 1); $expr_array = array(); $cexpor = 0; $startpos = -1; foreach ($expressions as $expression) { $expression['value'] = preg_replace('/\\s+(AND){1,2}\\s+/U', '&', $expression['value']); $expression['value'] = preg_replace('/\\s+(OR){1,2}\\s+/U', '|', $expression['value']); if ($expression['type'] == REGEXP_INCLUDE) { if (!empty($complite_expr)) { $complite_expr .= ' | '; } if ($cexpor == 0) { $startpos = zbx_strlen($complite_expr); } $cexpor++; $eq_global = '#0'; } else { if ($cexpor > 1 & $startpos >= 0) { $head = substr($complite_expr, 0, $startpos); $tail = substr($complite_expr, $startpos); $complite_expr = $head . '(' . $tail . ')'; } $cexpor = 0; $eq_global = '=0'; if (!empty($complite_expr)) { $complite_expr .= ' & '; } } $expr = '&' . $expression['value']; $expr = preg_replace('/\\s+(\\&|\\|){1,2}\\s+/U', '$1', $expr); $expr_array = array(); $sub_expr_count = 0; $sub_expr = ''; $multi = preg_match('/.+(&|\\|).+/', $expr); while (preg_match('/' . $ZBX_PREG_EXPESSION_FUNC_FORMAT . '/i', $expr, $arr)) { $arr[4] = zbx_strtolower($arr[4]); if (!isset($functions[$arr[4]])) { error(_('Incorrect function is used') . '. [' . $expression['value'] . ']'); return false; } $expr_array[$sub_expr_count]['eq'] = trim($arr[2]); $expr_array[$sub_expr_count]['regexp'] = zbx_strtolower($arr[4]) . $arr[5]; $sub_expr_count++; $expr = $arr[1]; } if (empty($expr_array)) { error(_('Incorrect trigger expression') . '. [' . $expression['value'] . ']'); return false; } $expr_array[$sub_expr_count - 1]['eq'] = ''; $sub_eq = ''; if ($multi > 0) { $sub_eq = $eq_global; } foreach ($expr_array as $id => $expr) { if ($multi > 0) { $sub_expr = $expr['eq'] . '({' . $prefix . $expr['regexp'] . '})' . $sub_eq . $sub_expr; } else { $sub_expr = $expr['eq'] . '{' . $prefix . $expr['regexp'] . '}' . $sub_eq . $sub_expr; } } if ($multi > 0) { $complite_expr .= '(' . $sub_expr . ')'; } else { $complite_expr .= '((' . $sub_expr . ')' . $eq_global . ')'; } } if ($cexpor > 1 & $startpos >= 0) { $head = substr($complite_expr, 0, $startpos); $tail = substr($complite_expr, $startpos); $complite_expr = $head . '(' . $tail . ')'; } return $complite_expr; }
* Actions */ $result = false; if (isset($_REQUEST['add_delay_flex']) && isset($_REQUEST['new_delay_flex'])) { $timePeriodValidator = new CTimePeriodValidator(array('allowMultiple' => false)); $_REQUEST['delay_flex'] = getRequest('delay_flex', array()); if ($timePeriodValidator->validate($_REQUEST['new_delay_flex']['period'])) { array_push($_REQUEST['delay_flex'], $_REQUEST['new_delay_flex']); unset($_REQUEST['new_delay_flex']); } else { error($timePeriodValidator->getError()); show_messages(false, null, _('Invalid time period')); } } elseif (isset($_REQUEST['delete']) && isset($_REQUEST['itemid'])) { $result = false; if ($item = get_item_by_itemid($_REQUEST['itemid'])) { $result = API::Item()->delete(array(getRequest('itemid'))); } if ($result) { uncheckTableRows(getRequest('hostid')); } unset($_REQUEST['itemid'], $_REQUEST['form']); show_messages($result, _('Item deleted'), _('Cannot delete item')); } elseif (isset($_REQUEST['clone']) && isset($_REQUEST['itemid'])) { unset($_REQUEST['itemid']); $_REQUEST['form'] = 'clone'; } elseif (hasRequest('add') || hasRequest('update')) { $delay_flex = getRequest('delay_flex', array()); $db_delay_flex = ''; foreach ($delay_flex as $value) { $db_delay_flex .= $value['delay'] . '/' . $value['period'] . ';';
/** * Replace items for specified host. * * @param $gitems * @param $dest_hostid * @param bool $error if false error won't be thrown when item does not exist * @return array|bool */ function get_same_graphitems_for_host($gitems, $dest_hostid, $error = true) { $result = array(); foreach ($gitems as $gitem) { $dbItem = DBfetch(DBselect('SELECT dest.itemid,src.key_' . ' FROM items dest,items src' . ' WHERE dest.key_=src.key_' . ' AND dest.hostid=' . zbx_dbstr($dest_hostid) . ' AND src.itemid=' . zbx_dbstr($gitem['itemid']))); if ($dbItem) { $gitem['itemid'] = $dbItem['itemid']; $gitem['key_'] = $dbItem['key_']; } elseif ($error) { $item = get_item_by_itemid($gitem['itemid']); $host = get_host_by_hostid($dest_hostid); error(_s('Missing key "%1$s" for host "%2$s".', $item['key_'], $host['host'])); return false; } else { continue; } $result[] = $gitem; } return $result; }
protected function selectData() { $this->data = array(); $now = time(NULL); if (isset($this->stime)) { $this->from_time = $this->stime; $this->to_time = $this->stime + $this->period; } else { $this->to_time = $now - 3600 * $this->from; $this->from_time = $this->to_time - $this->period; } $p = $this->to_time - $this->from_time; // graph size in time $z = $p - $this->from_time % $p; //<strong></strong> $x = $this->sizeX; // graph size in px $strvaluelength = 0; // we need to know how long in px will be our legend for ($i = 0; $i < $this->num; $i++) { $real_item = get_item_by_itemid($this->items[$i]['itemid']); $type = $this->items[$i]['calc_type']; $from_time = $this->from_time; $to_time = $this->to_time; $sql_arr = array(); if ($real_item['history'] * 86400 > time() - ($from_time + $this->period / 2) && $this->period / $this->sizeX <= ZBX_MAX_TREND_DIFF / ZBX_GRAPH_MAX_SKIP_CELL) { array_push($sql_arr, 'SELECT h.itemid, ' . ' avg(h.value) AS avg,min(h.value) AS min, ' . ' max(h.value) AS max,max(h.clock) AS clock, max(i.lastvalue) as lst ' . ' FROM history h ' . ' LEFT JOIN items i ON h.itemid = i.itemid' . ' WHERE h.itemid=' . $this->items[$i]['itemid'] . ' AND h.clock>=' . $from_time . ' AND h.clock<=' . $to_time . ' GROUP BY h.itemid', 'SELECT hu.itemid, ' . ' avg(hu.value) AS avg,min(hu.value) AS min,' . ' max(hu.value) AS max,max(hu.clock) AS clock, max(i.lastvalue) as lst' . ' FROM history_uint hu ' . ' LEFT JOIN items i ON hu.itemid = i.itemid' . ' WHERE hu.itemid=' . $this->items[$i]['itemid'] . ' AND hu.clock>=' . $from_time . ' AND hu.clock<=' . $to_time . ' GROUP BY hu.itemid'); } else { array_push($sql_arr, 'SELECT t.itemid, ' . ' avg(t.value_avg) AS avg,min(t.value_min) AS min,' . ' max(t.value_max) AS max,max(t.clock) AS clock, max(i.lastvalue) as lst' . ' FROM trends t ' . ' LEFT JOIN items i ON t.itemid = i.itemid' . ' WHERE t.itemid=' . $this->items[$i]['itemid'] . ' AND t.clock>=' . $from_time . ' AND t.clock<=' . $to_time . ' GROUP BY t.itemid', 'SELECT t.itemid, ' . ' avg(t.value_avg) AS avg,min(t.value_min) AS min,' . ' max(t.value_max) AS max,max(t.clock) AS clock, max(i.lastvalue) as lst' . ' FROM trends_uint t ' . ' LEFT JOIN items i ON t.itemid = i.itemid' . ' WHERE t.itemid=' . $this->items[$i]['itemid'] . ' AND t.clock>=' . $from_time . ' AND t.clock<=' . $to_time . ' GROUP BY t.itemid'); } $curr_data =& $this->data[$this->items[$i]['itemid']][$type]; $curr_data->min = NULL; $curr_data->max = NULL; $curr_data->avg = NULL; $curr_data->clock = NULL; foreach ($sql_arr as $sql) { $result = DBselect($sql); while ($row = DBfetch($result)) { $curr_data->min = $row['min']; $curr_data->max = $row['max']; $curr_data->avg = $row['avg']; $curr_data->lst = $row['lst']; $curr_data->clock = $row['clock']; $curr_data->shift_min = 0; $curr_data->shift_max = 0; $curr_data->shift_avg = 0; } unset($row); } switch ($this->items[$i]['calc_fnc']) { case CALC_FNC_MIN: $item_value = abs($curr_data->min); break; case CALC_FNC_MAX: $item_value = abs($curr_data->max); break; case CALC_FNC_LST: $item_value = abs($curr_data->lst); break; case CALC_FNC_AVG: default: $item_value = abs($curr_data->avg); } if ($type == GRAPH_ITEM_SUM) { $this->background = $i; $graph_sum = $item_value; } $this->sum += $item_value; $strvaluelength = max($strvaluelength, strlen(convert_units($item_value, $this->items[$i]['unit']))); } if (isset($graph_sum)) { $this->sum = $graph_sum; } $this->shiftlegendright += $strvaluelength * 7; }
/** * Create report bar for for "Compare values for multiple periods" * * @return object $reportForm */ function valueComparisonFormForMultiplePeriods() { $config = get_request('config', 1); $title = get_request('title', _('Report 3')); $xlabel = get_request('xlabel', ''); $ylabel = get_request('ylabel', ''); $scaletype = get_request('scaletype', TIMEPERIOD_TYPE_WEEKLY); $avgperiod = get_request('avgperiod', TIMEPERIOD_TYPE_DAILY); $report_timesince = get_request('report_timesince', date(TIMESTAMP_FORMAT_ZERO_TIME, time() - SEC_PER_DAY)); $report_timetill = get_request('report_timetill', date(TIMESTAMP_FORMAT_ZERO_TIME)); $itemId = get_request('itemid', 0); $hostids = get_request('hostids', array()); $hostids = zbx_toHash($hostids); $showlegend = get_request('showlegend', 0); $palette = get_request('palette', 0); $palettetype = get_request('palettetype', 0); $reportForm = new CFormTable(null, null, 'get'); $reportForm->setAttribute('name', 'zbx_report'); $reportForm->setAttribute('id', 'zbx_report'); if (isset($_REQUEST['report_show']) && $itemId) { $reportForm->addVar('report_show', 'show'); } $reportForm->addVar('config', $config); $reportForm->addVar('report_timesince', date(TIMESTAMP_FORMAT, $report_timesince)); $reportForm->addVar('report_timetill', date(TIMESTAMP_FORMAT, $report_timetill)); $reportForm->addRow(_('Title'), new CTextBox('title', $title, 40)); $reportForm->addRow(_('X label'), new CTextBox('xlabel', $xlabel, 40)); $reportForm->addRow(_('Y label'), new CTextBox('ylabel', $ylabel, 40)); $reportForm->addRow(_('Legend'), new CCheckBox('showlegend', $showlegend, null, 1)); $reportForm->addVar('sortorder', 0); $groupids = get_request('groupids', array()); $group_tb = new CTweenBox($reportForm, 'groupids', $groupids, 10); $options = array('real_hosts' => true, 'output' => 'extend'); $db_groups = API::HostGroup()->get($options); order_result($db_groups, 'name'); foreach ($db_groups as $gnum => $group) { $groupids[$group['groupid']] = $group['groupid']; $group_tb->addItem($group['groupid'], $group['name']); } $reportForm->addRow(_('Groups'), $group_tb->Get(_('Selected groups'), _('Other groups'))); $groupid = get_request('groupid', 0); $cmbGroups = new CComboBox('groupid', $groupid, 'submit()'); $cmbGroups->addItem(0, _('All')); foreach ($db_groups as $gnum => $group) { $cmbGroups->addItem($group['groupid'], $group['name']); } $td_groups = new CCol(array(_('Group'), SPACE, $cmbGroups)); $td_groups->setAttribute('style', 'text-align: right;'); $host_tb = new CTweenBox($reportForm, 'hostids', $hostids, 10); $options = array('real_hosts' => true, 'output' => array('hostid', 'name')); if ($groupid > 0) { $options['groupids'] = $groupid; } $db_hosts = API::Host()->get($options); $db_hosts = zbx_toHash($db_hosts, 'hostid'); order_result($db_hosts, 'name'); foreach ($db_hosts as $hnum => $host) { $host_tb->addItem($host['hostid'], $host['name']); } $options = array('real_hosts' => true, 'output' => array('hostid', 'name'), 'hostids' => $hostids); $db_hosts2 = API::Host()->get($options); order_result($db_hosts2, 'name'); foreach ($db_hosts2 as $hnum => $host) { if (!isset($db_hosts[$host['hostid']])) { $host_tb->addItem($host['hostid'], $host['name']); } } $reportForm->addRow(_('Hosts'), $host_tb->Get(_('Selected hosts'), array(_('Other hosts | Group') . SPACE, $cmbGroups))); $reporttimetab = new CTable(null, 'calendar'); $timeSinceRow = createDateSelector('report_timesince', $report_timesince, 'report_timetill'); array_unshift($timeSinceRow, _('From')); $reporttimetab->addRow($timeSinceRow); $timeTillRow = createDateSelector('report_timetill', $report_timetill, 'report_timesince'); array_unshift($timeTillRow, _('Till')); $reporttimetab->addRow($timeTillRow); $reportForm->addRow(_('Period'), $reporttimetab); $scale = new CComboBox('scaletype', $scaletype); $scale->addItem(TIMEPERIOD_TYPE_HOURLY, _('Hourly')); $scale->addItem(TIMEPERIOD_TYPE_DAILY, _('Daily')); $scale->addItem(TIMEPERIOD_TYPE_WEEKLY, _('Weekly')); $scale->addItem(TIMEPERIOD_TYPE_MONTHLY, _('Monthly')); $scale->addItem(TIMEPERIOD_TYPE_YEARLY, _('Yearly')); $reportForm->addRow(_('Scale'), $scale); $avgcmb = new CComboBox('avgperiod', $avgperiod); $avgcmb->addItem(TIMEPERIOD_TYPE_HOURLY, _('Hourly')); $avgcmb->addItem(TIMEPERIOD_TYPE_DAILY, _('Daily')); $avgcmb->addItem(TIMEPERIOD_TYPE_WEEKLY, _('Weekly')); $avgcmb->addItem(TIMEPERIOD_TYPE_MONTHLY, _('Monthly')); $avgcmb->addItem(TIMEPERIOD_TYPE_YEARLY, _('Yearly')); $reportForm->addRow(_('Average by'), $avgcmb); $itemName = ''; if ($itemId) { $itemName = get_item_by_itemid($itemId); $itemName = itemName($itemName); } $itemidVar = new CVar('itemid', $itemId, 'itemid'); $reportForm->addItem($itemidVar); $txtCondVal = new CTextBox('item_name', $itemName, 50, 'yes'); $txtCondVal->setAttribute('id', 'item_name'); $btnSelect = new CButton('btn1', _('Select'), 'return PopUp("popup.php?dstfrm=' . $reportForm->GetName() . '&dstfld1=itemid' . '&dstfld2=item_name' . '&srctbl=items' . '&srcfld1=itemid' . '&srcfld2=name' . '&monitored_hosts=1");', 'T'); $reportForm->addRow(_('Item'), array($txtCondVal, $btnSelect)); $paletteCmb = new CComboBox('palette', $palette); $paletteCmb->addItem(0, _s('Palette #%1$s', 1)); $paletteCmb->addItem(1, _s('Palette #%1$s', 2)); $paletteCmb->addItem(2, _s('Palette #%1$s', 3)); $paletteCmb->addItem(3, _s('Palette #%1$s', 4)); $paletteTypeCmb = new CComboBox('palettetype', $palettetype); $paletteTypeCmb->addItem(0, _('Middle')); $paletteTypeCmb->addItem(1, _('Darken')); $paletteTypeCmb->addItem(2, _('Brighten')); $reportForm->addRow(_('Palette'), array($paletteCmb, $paletteTypeCmb)); $reportForm->addItemToBottomRow(new CSubmit('report_show', _('Show'))); $reset = new CButton('reset', _('Reset')); $reset->setType('reset'); $reportForm->addItemToBottomRow($reset); return $reportForm; }
protected function selectData() { $this->data = array(); $now = time(NULL); if (isset($this->stime)) { $this->from_time = $this->stime; $this->to_time = $this->stime + $this->period; } else { $this->to_time = $now - 3600 * $this->from; $this->from_time = $this->to_time - $this->period; } $p = $this->to_time - $this->from_time; // graph size in time $z = $p - $this->from_time % $p; // graphsize - mod(from_time,p) for Oracle... $x = $this->sizeX; // graph size in px for ($i = 0; $i < $this->num; $i++) { $real_item = get_item_by_itemid($this->items[$i]['itemid']); if (!isset($this->axis_valuetype[$this->items[$i]['axisside']])) { $this->axis_valuetype[$this->items[$i]['axisside']] = $real_item['value_type']; } else { if ($this->axis_valuetype[$this->items[$i]['axisside']] != $real_item['value_type']) { $this->axis_valuetype[$this->items[$i]['axisside']] = ITEM_VALUE_TYPE_FLOAT; } } $type = $this->items[$i]['calc_type']; if ($type == GRAPH_ITEM_AGGREGATED) { /* skip current period */ $from_time = $this->from_time - $this->period * $this->items[$i]['periods_cnt']; $to_time = $this->from_time; } else { $from_time = $this->from_time; $to_time = $this->to_time; } $calc_field = 'round(' . $x . '*(mod(' . zbx_dbcast_2bigint('clock') . '+' . $z . ',' . $p . '))/(' . $p . '),0)'; /* required for 'group by' support of Oracle */ $sql_arr = array(); if ($real_item['history'] * 86400 > time() - ($this->from_time + $this->period / 2) && $this->period / $this->sizeX <= ZBX_MAX_TREND_DIFF / ZBX_GRAPH_MAX_SKIP_CELL) { $this->dataFrom = 'history'; array_push($sql_arr, 'SELECT itemid,' . $calc_field . ' as i,' . ' count(*) as count,avg(value) as avg,min(value) as min,' . ' max(value) as max,max(clock) as clock' . ' FROM history ' . ' WHERE itemid=' . $this->items[$i]['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field, 'SELECT itemid,' . $calc_field . ' as i,' . ' count(*) as count,avg(value) as avg,min(value) as min,' . ' max(value) as max,max(clock) as clock' . ' FROM history_uint ' . ' WHERE itemid=' . $this->items[$i]['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field); } else { $this->dataFrom = 'trends'; array_push($sql_arr, 'SELECT itemid,' . $calc_field . ' as i,' . ' sum(num) as count,avg(value_avg) as avg,min(value_min) as min,' . ' max(value_max) as max,max(clock) as clock' . ' FROM trends ' . ' WHERE itemid=' . $this->items[$i]['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field, 'SELECT itemid,' . $calc_field . ' as i,' . ' sum(num) as count,avg(value_avg) as avg,min(value_min) as min,' . ' max(value_max) as max,max(clock) as clock' . ' FROM trends_uint ' . ' WHERE itemid=' . $this->items[$i]['itemid'] . ' AND clock>=' . $from_time . ' AND clock<=' . $to_time . ' GROUP BY itemid,' . $calc_field); $this->items[$i]['delay'] = max($this->items[$i]['delay'], 3600); } //SDI($sql_arr); $curr_data =& $this->data[$this->items[$i]['itemid']][$type]; $curr_data->count = NULL; $curr_data->min = NULL; $curr_data->max = NULL; $curr_data->avg = NULL; $curr_data->clock = NULL; foreach ($sql_arr as $sql) { $result = DBselect($sql); while ($row = DBfetch($result)) { $idx = $row['i'] - 1; if ($idx < 0) { continue; } /* -------------------------------------------------- We are taking graph on 1px more than we need, and here we are skiping first px, because of MOD (in SELECT), it combines prelast point (it would be last point if not that 1px in begining) and first point, but we still losing prelast point :( but now we've got the first point. --------------------------------------------------*/ $curr_data->count[$idx] = $row['count']; $curr_data->min[$idx] = $row['min']; $curr_data->max[$idx] = $row['max']; $curr_data->avg[$idx] = $row['avg']; $curr_data->clock[$idx] = $row['clock']; $curr_data->shift_min[$idx] = 0; $curr_data->shift_max[$idx] = 0; $curr_data->shift_avg[$idx] = 0; if ($this->type == GRAPH_TYPE_STACKED) { $this->CheckGraphOrientation($curr_data->min[$idx]); } } unset($row); } /* calculate missed points */ $first_idx = 0; /* first_idx - last existed point ci - current index cj - count of missed in onetime dx - offset to first value (count to last existed point) //*/ for ($ci = 0, $cj = 0; $ci < $this->sizeX; $ci++) { if (!isset($curr_data->count[$ci]) || $curr_data->count[$ci] == 0) { $curr_data->count[$ci] = 0; $curr_data->shift_min[$ci] = 0; $curr_data->shift_max[$ci] = 0; $curr_data->shift_avg[$ci] = 0; $cj++; } else { if ($cj > 0) { $dx = $cj + 1; $first_idx = $ci - $dx; if ($first_idx < 0) { $first_idx = $ci; } // if no data FROM start of graph get current data as first data for (; $cj > 0; $cj--) { if ($dx < $this->sizeX / 20 && $this->type == GRAPH_TYPE_STACKED) { $curr_data->count[$ci - ($dx - $cj)] = 1; } foreach (array('clock', 'min', 'max', 'avg') as $var_name) { $var =& $curr_data->{$var_name}; if ($first_idx == $ci && $var_name == 'clock') { $var[$ci - ($dx - $cj)] = $var[$first_idx] - $p / $this->sizeX * ($dx - $cj); continue; } $dy = $var[$ci] - $var[$first_idx]; $var[$ci - ($dx - $cj)] = $var[$first_idx] + $cj * $dy / $dx; } } } } } if ($cj > 0 && $ci > $cj) { $dx = $cj + 1; $first_idx = $ci - $dx; for (; $cj > 0; $cj--) { // if($dx < ($this->sizeX/20)) //($this->type == GRAPH_TYPE_STACKED) // $curr_data->count[$first_idx + ($dx - $cj)] = 1; foreach (array('clock', 'min', 'max', 'avg') as $var_name) { $var =& $curr_data->{$var_name}; if ($var_name == 'clock') { $var[$first_idx + ($dx - $cj)] = $var[$first_idx] + $p / $this->sizeX * ($dx - $cj); continue; } $var[$first_idx + ($dx - $cj)] = $var[$first_idx]; } } } /* end of missed points calculation */ } /* calculte shift for stacked graphs */ if ($this->type == GRAPH_TYPE_STACKED) { for ($i = 1; $i < $this->num; $i++) { $curr_data =& $this->data[$this->items[$i]['itemid']][$this->items[$i]['calc_type']]; if (!isset($curr_data)) { continue; } for ($j = $i - 1; $j >= 0; $j--) { if ($this->items[$j]['axisside'] != $this->items[$i]['axisside']) { continue; } $prev_data =& $this->data[$this->items[$j]['itemid']][$this->items[$j]['calc_type']]; if (!isset($prev_data)) { continue; } for ($ci = 0; $ci < $this->sizeX; $ci++) { foreach (array('min', 'max', 'avg') as $var_name) { $shift_var_name = 'shift_' . $var_name; $curr_shift =& $curr_data->{$shift_var_name}; $curr_var =& $curr_data->{$var_name}; $prev_shift =& $prev_data->{$shift_var_name}; $prev_var =& $prev_data->{$var_name}; $curr_shift[$ci] = $prev_var[$ci] + $prev_shift[$ci]; } } break; } } } /* end calculation of stacked graphs */ }
function get_resource_name($permission, $id) { $res = '-'; if ($permission == 'Graph') { if (isset($id) && $id != 0) { if ($graph = get_graph_by_graphid($id)) { $res = $graph['name']; } } else { if (!isset($id) || $id == 0) { $res = 'All graphs'; } } } else { if ($permission == 'Host') { if (isset($id) && $id != 0) { if ($host = get_host_by_hostid($id)) { $res = $host['host']; } } else { if (!isset($id) || $id == 0) { $res = 'All hosts'; } } } else { if ($permission == 'Screen') { if (isset($id) && $id != 0) { if ($screen = get_screen_by_screenid($id)) { $res = $screen['name']; } } else { if (!isset($id) || $id == 0) { $res = 'All screens'; } } } else { if ($permission == 'Item') { if (isset($id) && $id != 0) { if ($item = get_item_by_itemid($id)) { if ($host = get_host_by_hostid($item['hostid'])) { $res = $host['host'] . ':' . $item['description']; } } } else { if (!isset($id) || $id == 0) { $res = 'All items'; } } } else { if ($permission == 'User') { if (isset($id) && $id != 0) { if ($user = get_user_by_userid($id)) { $res = $user['alias']; } } else { if (!isset($id) || $id == 0) { $res = 'All users'; } } } else { if ($permission == 'Network map') { if (isset($id) && $id != 0) { if ($user = get_sysmap_by_sysmapid($id)) { $res = $user['name']; } } else { if (!isset($id) || $id == 0) { $res = 'All maps'; } } } else { if ($permission == 'Application') { if (isset($id) && $id > 0) { if ($app = get_application_by_applicationid($id)) { $res = $app['name']; } } else { if (!isset($id) || $id == 0) { $res = 'All applications'; } } } else { if ($permission == 'Service') { if (isset($id) && $id > 0) { if ($service = get_service_by_serviceid($id)) { $res = $service['name']; } } else { if (!isset($id) || $id == 0) { $res = 'All services'; } } } } } } } } } } if ($res == '-' && isset($id) && $id > 0) { $res = $id; } return $res; }
function get_screen_plaintext($itemid, $elements, $style = 0) { if ($itemid == 0) { $table = new CTableInfo(S_ITEM_DOES_NOT_EXIST); $table->setHeader(array(S_TIMESTAMP, S_ITEM)); return $table; } $item = get_item_by_itemid($itemid); switch ($item['value_type']) { case ITEM_VALUE_TYPE_TEXT: case ITEM_VALUE_TYPE_LOG: $order_field = 'id'; break; case ITEM_VALUE_TYPE_FLOAT: case ITEM_VALUE_TYPE_UINT64: default: $order_field = 'clock'; } $host = get_host_by_itemid($itemid); $table = new CTableInfo(); $table->setHeader(array(S_TIMESTAMP, $host['host'] . ': ' . item_description($item))); $options = array('history' => $item['value_type'], 'itemids' => $itemid, 'output' => API_OUTPUT_EXTEND, 'sortorder' => ZBX_SORT_DOWN, 'sortfield' => $order_field, 'limit' => $elements); $hData = CHistory::get($options); foreach ($hData as $hnum => $data) { switch ($item['value_type']) { case ITEM_VALUE_TYPE_TEXT: /* do not use break */ /* do not use break */ case ITEM_VALUE_TYPE_STR: if ($style) { $value = new CJSscript($data['value']); } else { $value = $data['value']; } break; case ITEM_VALUE_TYPE_LOG: if ($style) { $value = new CJSscript($data['value']); } else { $value = $data['value']; } break; default: $value = $data['value']; break; } if ($item['valuemapid'] > 0) { $value = replace_value_by_map($value, $item['valuemapid']); } $table->addRow(array(zbx_date2str(S_SCREENS_PLAIN_TEXT_DATE_FORMAT, $data['clock']), new CCol($value, 'pre'))); } return $table; }
require_once dirname(__FILE__) . '/include/config.inc.php'; require_once dirname(__FILE__) . '/include/triggers.inc.php'; require_once dirname(__FILE__) . '/include/js.inc.php'; $dstfrm = getRequest('dstfrm', 0); $page['title'] = _('Graph item'); $page['file'] = 'popup_bitem.php'; define('ZBX_PAGE_NO_MENU', 1); require_once dirname(__FILE__) . '/include/page_header.php'; // VAR TYPE OPTIONAL FLAGS VALIDATION EXCEPTION $fields = array('dstfrm' => array(T_ZBX_STR, O_MAND, P_SYS, NOT_EMPTY, null), 'config' => array(T_ZBX_INT, O_OPT, P_SYS, IN('0,1,2,3'), null), 'gid' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID . '({} != 0)', null), 'list_name' => array(T_ZBX_STR, O_OPT, P_SYS, NOT_EMPTY, '(isset({add}) || isset({update})) && isset({gid})'), 'caption' => array(T_ZBX_STR, O_OPT, null, null, null), 'itemid' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID . '({} != 0)', 'isset({add}) || isset({update})', _('Parameter')), 'color' => array(T_ZBX_CLR, O_OPT, null, null, 'isset({add}) || isset({update})', _('Colour')), 'calc_fnc' => array(T_ZBX_INT, O_OPT, null, IN('0,1,2,4,7,9'), 'isset({add}) || isset({update})'), 'axisside' => array(T_ZBX_INT, O_OPT, null, IN(GRAPH_YAXIS_SIDE_LEFT . ',' . GRAPH_YAXIS_SIDE_RIGHT), null), 'add' => array(T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null), 'update' => array(T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null), 'form' => array(T_ZBX_STR, O_OPT, P_SYS, null, null), 'form_refresh' => array(T_ZBX_INT, O_OPT, null, null, null), 'host' => array(T_ZBX_STR, O_OPT, null, null, null), 'name' => array(T_ZBX_STR, O_OPT, null, null, null), 'name_expanded' => array(T_ZBX_STR, O_OPT, null, null, null)); check_fields($fields); $caption = getRequest('caption', ''); $autoCaption = ''; $_REQUEST['axisside'] = getRequest('axisside', GRAPH_YAXIS_SIDE_LEFT); if (getRequest('itemid') > 0) { $items = CMacrosResolverHelper::resolveItemNames(array(get_item_by_itemid(getRequest('itemid')))); $item = reset($items); $autoCaption = $item['name_expanded']; if (!hasRequest('caption') || getRequest('caption') === $item['name']) { $caption = $item['name_expanded']; } } insert_js_function('add_bitem'); insert_js_function('update_bitem'); if (hasRequest('add') && !hasRequest('gid')) { insert_js("add_bitem(" . zbx_jsvalue(getRequest('dstfrm')) . "," . zbx_jsvalue($caption) . ",'" . getRequest('itemid') . "','" . getRequest('color') . "'," . getRequest('calc_fnc') . "," . getRequest('axisside') . ");\n"); } if (hasRequest('update') && hasRequest('gid')) { insert_js("update_bitem(" . zbx_jsvalue(getRequest('dstfrm')) . "," . zbx_jsvalue(getRequest('list_name')) . ",'" . getRequest('gid') . "'," . zbx_jsvalue($caption) . ",'" . getRequest('itemid') . "','" . getRequest('color') . "'," . getRequest('calc_fnc') . "," . getRequest('axisside') . ");\n"); } else { echo BR();
function cmp_graphitems(&$gitem1, &$gitem2) { if ($gitem1['drawtype'] != $gitem2['drawtype']) { return 1; } if ($gitem1['sortorder'] != $gitem2['sortorder']) { return 2; } if ($gitem1['color'] != $gitem2['color']) { return 3; } if ($gitem1['yaxisside'] != $gitem2['yaxisside']) { return 4; } $item1 = get_item_by_itemid($gitem1['itemid']); $item2 = get_item_by_itemid($gitem2['itemid']); if ($item1['key_'] != $item2['key_']) { return 5; } return 0; }
function bar_report_form3() { global $USER_DETAILS; $available_hosts = get_accessible_hosts_by_user($USER_DETAILS, PERM_READ_ONLY); $config = get_request('config', 1); $title = get_request('title', S_REPORT . ' 3'); $xlabel = get_request('xlabel', ''); $ylabel = get_request('ylabel', ''); $sorttype = get_request('sorttype', 0); $scaletype = get_request('scaletype', TIMEPERIOD_TYPE_WEEKLY); $avgperiod = get_request('avgperiod', TIMEPERIOD_TYPE_DAILY); $report_timesince = get_request('report_timesince', date('YmdHis', time() - 86400)); $report_timetill = get_request('report_timetill', date('YmdHis')); $captions = get_request('captions', array()); $items = get_request('items', array()); $hostids = get_request('hostids', array()); $hostids = zbx_toHash($hostids); $showlegend = get_request('showlegend', 0); $palette = get_request('palette', 0); $palettetype = get_request('palettetype', 0); $reportForm = new CFormTable(null, null, 'get'); //,'events.php?report_set=1','POST',null,'sform'); $reportForm->setAttribute('name', 'zbx_report'); $reportForm->setAttribute('id', 'zbx_report'); // $reportForm->setMethod('post'); if (isset($_REQUEST['report_show']) && !empty($items)) { $reportForm->addVar('report_show', 'show'); } $reportForm->addVar('config', $config); $reportForm->addVar('report_timesince', date('YmdHis', $report_timesince)); $reportForm->addVar('report_timetill', date('YmdHis', $report_timetill)); // $reportForm->addVar('items',$items); //params are set later!! // $reportForm->addVar('periods',$periods); $reportForm->addRow(S_TITLE, new CTextBox('title', $title, 40)); $reportForm->addRow(S_X . SPACE . S_LABEL, new CTextBox('xlabel', $xlabel, 40)); $reportForm->addRow(S_Y . SPACE . S_LABEL, new CTextBox('ylabel', $ylabel, 40)); $reportForm->addRow(S_LEGEND, new CCheckBox('showlegend', $showlegend, null, 1)); $reportForm->addVar('sortorder', 0); // GROUPS $groupids = get_request('groupids', array()); $group_tb = new CTweenBox($reportForm, 'groupids', $groupids, 10); $options = array('real_hosts' => 1, 'output' => 'extend'); $db_groups = CHostGroup::get($options); order_result($db_groups, 'name'); foreach ($db_groups as $gnum => $group) { $groupids[$group['groupid']] = $group['groupid']; $group_tb->addItem($group['groupid'], $group['name']); } $reportForm->addRow(S_GROUPS, $group_tb->Get(S_SELECTED_GROUPS, S_OTHER . SPACE . S_GROUPS)); // ---------- // HOSTS // validate_group(PERM_READ_ONLY,array('real_hosts'),'web.last.conf.groupid'); $groupid = get_request('groupid', 0); $cmbGroups = new CComboBox('groupid', $groupid, 'submit()'); $cmbGroups->addItem(0, S_ALL_S); foreach ($db_groups as $gnum => $group) { $cmbGroups->addItem($group['groupid'], $group['name']); } $td_groups = new CCol(array(S_GROUP, SPACE, $cmbGroups)); $td_groups->setAttribute('style', 'text-align: right;'); $host_tb = new CTweenBox($reportForm, 'hostids', $hostids, 10); $options = array('real_hosts' => 1, 'output' => array('hostid', 'host')); if ($groupid > 0) { $options['groupids'] = $groupid; } $db_hosts = CHost::get($options); $db_hosts = zbx_toHash($db_hosts, 'hostid'); order_result($db_hosts, 'host'); foreach ($db_hosts as $hnum => $host) { $host_tb->addItem($host['hostid'], $host['host']); } $options = array('real_hosts' => 1, 'output' => array('hostid', 'host'), 'hostids' => $hostids); $db_hosts2 = CHost::get($options); order_result($db_hosts2, 'host'); foreach ($db_hosts2 as $hnum => $host) { if (!isset($db_hosts[$host['hostid']])) { $host_tb->addItem($host['hostid'], $host['host']); } } $reportForm->addRow(S_HOSTS, $host_tb->Get(S_SELECTED_HOSTS, array(S_OTHER . SPACE . S_HOSTS . SPACE . '|' . SPACE . S_GROUP . SPACE, $cmbGroups))); // ---------- //*/ // PERIOD $clndr_icon = new CImg('images/general/bar/cal.gif', 'calendar', 16, 12, 'pointer'); $clndr_icon->addAction('onclick', 'javascript: ' . 'var pos = getPosition(this); ' . 'pos.top+=10; ' . 'pos.left+=16; ' . "CLNDR['avail_report_since'].clndr.clndrshow(pos.top,pos.left);"); $reporttimetab = new CTable(null, 'calendar'); $reporttimetab->setAttribute('width', '10%'); $reporttimetab->setCellPadding(0); $reporttimetab->setCellSpacing(0); $reporttimetab->addRow(array(S_FROM, new CNumericBox('report_since_day', $report_timesince > 0 ? date('d', $report_timesince) : '', 2), '/', new CNumericBox('report_since_month', $report_timesince > 0 ? date('m', $report_timesince) : '', 2), '/', new CNumericBox('report_since_year', $report_timesince > 0 ? date('Y', $report_timesince) : '', 4), SPACE, new CNumericBox('report_since_hour', $report_timesince > 0 ? date('H', $report_timesince) : '', 2), ':', new CNumericBox('report_since_minute', $report_timesince > 0 ? date('i', $report_timesince) : '', 2), $clndr_icon)); zbx_add_post_js('create_calendar(null,' . '["report_since_day","report_since_month","report_since_year","report_since_hour","report_since_minute"],' . '"avail_report_since",' . '"report_timesince");'); $clndr_icon->addAction('onclick', 'javascript: ' . 'var pos = getPosition(this); ' . 'pos.top+=10; ' . 'pos.left+=16; ' . "CLNDR['avail_report_till'].clndr.clndrshow(pos.top,pos.left);"); $reporttimetab->addRow(array(S_TILL, new CNumericBox('report_till_day', $report_timetill > 0 ? date('d', $report_timetill) : '', 2), '/', new CNumericBox('report_till_month', $report_timetill > 0 ? date('m', $report_timetill) : '', 2), '/', new CNumericBox('report_till_year', $report_timetill > 0 ? date('Y', $report_timetill) : '', 4), SPACE, new CNumericBox('report_till_hour', $report_timetill > 0 ? date('H', $report_timetill) : '', 2), ':', new CNumericBox('report_till_minute', $report_timetill > 0 ? date('i', $report_timetill) : '', 2), $clndr_icon)); zbx_add_post_js('create_calendar(null,' . '["report_till_day","report_till_month","report_till_year","report_till_hour","report_till_minute"],' . '"avail_report_till",' . '"report_timetill");'); zbx_add_post_js('addListener($("filter_icon"),' . '"click",' . 'CLNDR[\'avail_report_since\'].clndr.clndrhide.bindAsEventListener(CLNDR[\'avail_report_since\'].clndr));' . 'addListener($("filter_icon"),' . '"click",' . 'CLNDR[\'avail_report_till\'].clndr.clndrhide.bindAsEventListener(CLNDR[\'avail_report_till\'].clndr));'); $reportForm->addRow(S_PERIOD, $reporttimetab); //----------- $scale = new CComboBox('scaletype', $scaletype); $scale->addItem(TIMEPERIOD_TYPE_HOURLY, S_HOURLY); $scale->addItem(TIMEPERIOD_TYPE_DAILY, S_DAILY); $scale->addItem(TIMEPERIOD_TYPE_WEEKLY, S_WEEKLY); $scale->addItem(TIMEPERIOD_TYPE_MONTHLY, S_MONTHLY); $scale->addItem(TIMEPERIOD_TYPE_YEARLY, S_YEARLY); $reportForm->addRow(S_SCALE, $scale); $avgcmb = new CComboBox('avgperiod', $avgperiod); $avgcmb->addItem(TIMEPERIOD_TYPE_HOURLY, S_HOURLY); $avgcmb->addItem(TIMEPERIOD_TYPE_DAILY, S_DAILY); $avgcmb->addItem(TIMEPERIOD_TYPE_WEEKLY, S_WEEKLY); $avgcmb->addItem(TIMEPERIOD_TYPE_MONTHLY, S_MONTHLY); $avgcmb->addItem(TIMEPERIOD_TYPE_YEARLY, S_YEARLY); $reportForm->addRow(S_AVERAGE_BY, $avgcmb); // ITEMS $itemid = 0; $description = ''; if (count($items) && $items[0]['itemid'] > 0) { $itemid = $items[0]['itemid']; $description = get_item_by_itemid($itemid); $description = item_description($description); } $reportForm->addVar('items[0][itemid]', $itemid); $txtCondVal = new CTextBox('items[0][description]', $description, 50, 'yes'); $btnSelect = new CButton('btn1', S_SELECT, "return PopUp('popup.php?dstfrm=" . $reportForm->GetName() . "&dstfld1=items[0][itemid]&dstfld2=items[0][description]&" . "srctbl=items&srcfld1=itemid&srcfld2=description&monitored_hosts=1');", 'T'); $reportForm->addRow(S_ITEM, array($txtCondVal, $btnSelect)); $paletteCmb = new CComboBox('palette', $palette); $paletteCmb->addItem(0, S_PALETTE . ' #1'); $paletteCmb->addItem(1, S_PALETTE . ' #2'); $paletteCmb->addItem(2, S_PALETTE . ' #3'); $paletteCmb->addItem(3, S_PALETTE . ' #4'); $paletteTypeCmb = new CComboBox('palettetype', $palettetype); $paletteTypeCmb->addItem(0, S_MIDDLE); $paletteTypeCmb->addItem(1, S_DARKEN); $paletteTypeCmb->addItem(2, S_BRIGHTEN); $reportForm->addRow(S_PALETTE, array($paletteCmb, $paletteTypeCmb)); //-------------- $reportForm->addItemToBottomRow(new CButton('report_show', S_SHOW)); $reset = new CButton('reset', S_RESET); $reset->setType('reset'); $reportForm->addItemToBottomRow($reset); return $reportForm; }
} $graphFormList->addRow(_('Y axis MIN value'), $yaxisMinData); $yaxisMaxData = array(); $yTypeComboBox = new CComboBox('ymax_type', $this->data['ymax_type'], 'submit()'); $yTypeComboBox->addItem(GRAPH_YAXIS_TYPE_CALCULATED, _('Calculated')); $yTypeComboBox->addItem(GRAPH_YAXIS_TYPE_FIXED, _('Fixed')); $yTypeComboBox->addItem(GRAPH_YAXIS_TYPE_ITEM_VALUE, _('Item')); $yaxisMaxData[] = $yTypeComboBox; if ($this->data['ymax_type'] == GRAPH_YAXIS_TYPE_FIXED) { $yaxisMaxData[] = new CTextBox('yaxismax', $this->data['yaxismax'], 7); } elseif ($this->data['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $graphForm->addVar('yaxismax', $this->data['yaxismax']); $ymax_name = ''; if (!empty($this->data['ymax_itemid'])) { $max_host = get_host_by_itemid($this->data['ymax_itemid']); $max_item = get_item_by_itemid($this->data['ymax_itemid']); $ymax_name = $max_host['name'] . NAME_DELIMITER . itemName($max_item); } $yaxisMaxData[] = new CTextBox('ymax_name', $ymax_name, 36, 'yes'); $yaxisMaxData[] = new CButton('yaxis_max', _('Select'), 'javascript: ' . 'return PopUp("popup.php?dstfrm=' . $graphForm->getName() . '&dstfld1=ymax_itemid' . '&dstfld2=ymax_name' . '&srctbl=items' . '&srcfld1=itemid' . '&srcfld2=name' . '&numeric=1' . '&writeonly=1" + getOnlyHostParam(), 0, 0, "zbx_popup_item");', 'formlist'); // select prototype button if (!empty($this->data['parent_discoveryid'])) { $yaxisMaxData[] = new CButton('yaxis_max_prototype', _('Select prototype'), 'javascript: ' . 'return PopUp("popup.php?dstfrm=' . $graphForm->getName() . '&parent_discoveryid=' . $this->data['parent_discoveryid'] . '&dstfld1=ymax_itemid' . '&dstfld2=ymax_name' . '&srctbl=prototypes' . '&srcfld1=itemid' . '&srcfld2=name' . '&numeric=1", 0, 0, "zbx_popup_item");', 'formlist'); } } else { $graphForm->addVar('yaxismax', $this->data['yaxismax']); } $graphFormList->addRow(_('Y axis MAX value'), $yaxisMaxData); } else { $graphFormList->addRow(_('3D view'), new CCheckBox('show_3d', $this->data['show_3d'], null, 1)); }
function get_realrule_by_itemid_and_hostid($itemid, $hostid) { $item = get_item_by_itemid($itemid); if (bccomp($hostid, $item['hostid']) == 0) { return $item['itemid']; } if ($item['templateid'] != 0) { return get_realrule_by_itemid_and_hostid($item['templateid'], $hostid); } return $item['itemid']; }
} $graphFormList->addRow(_('Y axis MIN value'), $yaxisMinData); $yaxisMaxData = array(); $yTypeComboBox = new CComboBox('ymax_type', $this->data['ymax_type']); $yTypeComboBox->addItem(GRAPH_YAXIS_TYPE_CALCULATED, _('Calculated')); $yTypeComboBox->addItem(GRAPH_YAXIS_TYPE_FIXED, _('Fixed')); $yTypeComboBox->addItem(GRAPH_YAXIS_TYPE_ITEM_VALUE, _('Item')); $yaxisMaxData[] = $yTypeComboBox; if ($this->data['ymax_type'] == GRAPH_YAXIS_TYPE_FIXED) { $yaxisMaxData[] = new CTextBox('yaxismax', $this->data['yaxismax'], 7); } elseif ($this->data['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $graphForm->addVar('yaxismax', $this->data['yaxismax']); $ymax_name = ''; if (!empty($this->data['ymax_itemid'])) { $max_host = get_host_by_itemid($this->data['ymax_itemid']); $maxItems = CMacrosResolverHelper::resolveItemNames(array(get_item_by_itemid($this->data['ymax_itemid']))); $maxItem = reset($maxItems); $ymax_name = $max_host['name'] . NAME_DELIMITER . $maxItem['name_expanded']; } $yaxisMaxData[] = new CTextBox('ymax_name', $ymax_name, 36, true); $yaxisMaxData[] = new CButton('yaxis_max', _('Select'), 'javascript: ' . 'return PopUp("popup.php?dstfrm=' . $graphForm->getName() . '&dstfld1=ymax_itemid' . '&dstfld2=ymax_name' . '&srctbl=items' . '&srcfld1=itemid' . '&srcfld2=name' . '&numeric=1' . '&writeonly=1" + getOnlyHostParam(), 0, 0, "zbx_popup_item");', 'formlist'); // select prototype button if (!empty($this->data['parent_discoveryid'])) { $yaxisMaxData[] = new CButton('yaxis_max_prototype', _('Select prototype'), 'javascript: ' . 'return PopUp("popup.php?dstfrm=' . $graphForm->getName() . '&parent_discoveryid=' . $this->data['parent_discoveryid'] . '&dstfld1=ymax_itemid' . '&dstfld2=ymax_name' . '&srctbl=item_prototypes' . '&srcfld1=itemid' . '&srcfld2=name' . '&numeric=1", 0, 0, "zbx_popup_item");', 'formlist'); } } else { $graphForm->addVar('yaxismax', $this->data['yaxismax']); } $graphFormList->addRow(_('Y axis MAX value'), $yaxisMaxData); } else { $graphFormList->addRow(_('3D view'), new CCheckBox('show_3d', $this->data['show_3d'], null, 1));
function cmp_triggers($triggerid1, $triggerid2) { // compare EXPRESSION !!! $trig1 = get_trigger_by_triggerid($triggerid1); $trig2 = get_trigger_by_triggerid($triggerid2); $trig_fnc1 = get_functions_by_triggerid($triggerid1); $expr1 = $trig1["expression"]; while ($fnc1 = DBfetch($trig_fnc1)) { $trig_fnc2 = get_functions_by_triggerid($triggerid2); while ($fnc2 = DBfetch($trig_fnc2)) { if (strcmp($fnc1["function"], $fnc2["function"])) { continue; } if ($fnc1["parameter"] != $fnc2["parameter"]) { continue; } $item1 = get_item_by_itemid($fnc1["itemid"]); $item2 = get_item_by_itemid($fnc2["itemid"]); if (strcmp($item1["key_"], $item2["key_"])) { continue; } $expr1 = str_replace("{" . $fnc1["functionid"] . "}", "{" . $fnc2["functionid"] . "}", $expr1); break; } } return strcmp($expr1, $trig2["expression"]); }
function cmp_graphitems(&$gitem1, &$gitem2) { if ($gitem1["drawtype"] != $gitem2["drawtype"]) { return 1; } if ($gitem1["sortorder"] != $gitem2["sortorder"]) { return 2; } if ($gitem1["color"] != $gitem2["color"]) { return 3; } if ($gitem1["yaxisside"] != $gitem2["yaxisside"]) { return 4; } $item1 = get_item_by_itemid($gitem1["itemid"]); $item2 = get_item_by_itemid($gitem2["itemid"]); if ($item1["key_"] != $item2["key_"]) { return 5; } return 0; }
protected function selectData() { $this->data = array(); $now = time(null); if (isset($this->stime)) { $this->from_time = $this->stime; $this->to_time = $this->stime + $this->period; } else { $this->to_time = $now - SEC_PER_HOUR * $this->from; $this->from_time = $this->to_time - $this->period; } $strvaluelength = 0; // we need to know how long in px will be our legend // fetch values for items with the "last" function $lastValueItems = array(); foreach ($this->items as $item) { if ($item['calc_fnc'] == CALC_FNC_LST) { $lastValueItems[] = $item; } } if ($lastValueItems) { $history = Manager::History()->getLast($lastValueItems); } $config = select_config(); for ($i = 0; $i < $this->num; $i++) { $item = get_item_by_itemid($this->items[$i]['itemid']); $type = $this->items[$i]['calc_type']; $from_time = $this->from_time; $to_time = $this->to_time; $sql_arr = array(); // override item history setting with housekeeping settings if ($config['hk_history_global']) { $item['history'] = $config['hk_history']; } $trendsEnabled = $config['hk_trends_global'] ? $config['hk_trends'] > 0 : $item['trends'] > 0; if (!$trendsEnabled || $item['history'] * SEC_PER_DAY > time() - ($from_time + $this->period / 2)) { $this->dataFrom = 'history'; array_push($sql_arr, 'SELECT h.itemid,' . 'AVG(h.value) AS avg,MIN(h.value) AS min,' . 'MAX(h.value) AS max,MAX(h.clock) AS clock' . ' FROM history h' . ' WHERE h.itemid=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND h.clock>=' . zbx_dbstr($from_time) . ' AND h.clock<=' . zbx_dbstr($to_time) . ' GROUP BY h.itemid', 'SELECT hu.itemid,' . 'AVG(hu.value) AS avg,MIN(hu.value) AS min,' . 'MAX(hu.value) AS max,MAX(hu.clock) AS clock' . ' FROM history_uint hu' . ' WHERE hu.itemid=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND hu.clock>=' . zbx_dbstr($from_time) . ' AND hu.clock<=' . zbx_dbstr($to_time) . ' GROUP BY hu.itemid'); } else { $this->dataFrom = 'trends'; array_push($sql_arr, 'SELECT t.itemid,' . 'AVG(t.value_avg) AS avg,MIN(t.value_min) AS min,' . 'MAX(t.value_max) AS max,MAX(t.clock) AS clock' . ' FROM trends t' . ' WHERE t.itemid=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND t.clock>=' . zbx_dbstr($from_time) . ' AND t.clock<=' . zbx_dbstr($to_time) . ' GROUP BY t.itemid', 'SELECT t.itemid,' . 'AVG(t.value_avg) AS avg,MIN(t.value_min) AS min,' . 'MAX(t.value_max) AS max,MAX(t.clock) AS clock' . ' FROM trends_uint t' . ' WHERE t.itemid=' . zbx_dbstr($this->items[$i]['itemid']) . ' AND t.clock>=' . zbx_dbstr($from_time) . ' AND t.clock<=' . zbx_dbstr($to_time) . ' GROUP BY t.itemid'); } $this->data[$this->items[$i]['itemid']][$type]['last'] = isset($history[$item['itemid']]) ? $history[$item['itemid']][0]['value'] : null; $this->data[$this->items[$i]['itemid']][$type]['shift_min'] = 0; $this->data[$this->items[$i]['itemid']][$type]['shift_max'] = 0; $this->data[$this->items[$i]['itemid']][$type]['shift_avg'] = 0; foreach ($sql_arr as $sql) { $result = DBselect($sql); while ($row = DBfetch($result)) { $this->data[$this->items[$i]['itemid']][$type]['min'] = $row['min']; $this->data[$this->items[$i]['itemid']][$type]['max'] = $row['max']; $this->data[$this->items[$i]['itemid']][$type]['avg'] = $row['avg']; $this->data[$this->items[$i]['itemid']][$type]['clock'] = $row['clock']; } unset($row); } switch ($this->items[$i]['calc_fnc']) { case CALC_FNC_MIN: $fncName = 'min'; break; case CALC_FNC_MAX: $fncName = 'max'; break; case CALC_FNC_LST: $fncName = 'last'; break; case CALC_FNC_AVG: default: $fncName = 'avg'; } $item_value = empty($this->data[$this->items[$i]['itemid']][$type][$fncName]) ? 0 : abs($this->data[$this->items[$i]['itemid']][$type][$fncName]); if ($type == GRAPH_ITEM_SUM) { $this->background = $i; $graph_sum = $item_value; } $this->sum += $item_value; $convertedUnit = zbx_strlen(convert_units(array('value' => $item_value, 'units' => $this->items[$i]['units']))); $strvaluelength = max($strvaluelength, $convertedUnit); } if (isset($graph_sum)) { $this->sum = $graph_sum; } $this->shiftlegendright += $strvaluelength * 7; }
protected function calculateMaxY($side) { if ($this->ymax_type == GRAPH_YAXIS_TYPE_FIXED) { return $this->yaxismax; } if ($this->ymax_type == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $item = get_item_by_itemid($this->ymax_itemid); $history = Manager::History()->getLast(array($item)); if (isset($history[$item['itemid']])) { return $history[$item['itemid']][0]['value']; } } $maxY = null; for ($i = 0; $i < $this->num; $i++) { if ($this->items[$i]['axisside'] != $side) { continue; } if (!isset($this->data[$this->items[$i]['itemid']][GRAPH_ITEM_SIMPLE])) { continue; } $data =& $this->data[$this->items[$i]['itemid']][GRAPH_ITEM_SIMPLE]; if (!isset($data)) { continue; } $calc_fnc = $this->items[$i]['calc_fnc']; switch ($calc_fnc) { case CALC_FNC_ALL: case CALC_FNC_MAX: $val = $data['max']; $shift_val = $data['shift_max']; break; case CALC_FNC_MIN: $val = $data['min']; $shift_val = $data['shift_min']; break; case CALC_FNC_AVG: default: $val = $data['avg']; $shift_val = $data['shift_avg']; } if (!isset($val)) { continue; } for ($ci = 0; $ci < min(count($val), count($shift_val)); $ci++) { if ($data['count'][$ci] == 0) { continue; } $val[$ci] = bcadd($shift_val[$ci], $val[$ci]); } if (!isset($maxY)) { if (isset($val) && count($val) > 0) { $maxY = max($val); } } else { $maxY = max($maxY, max($val)); } } return $maxY; }
function make_graph_submenu() { $graphids = array(); $fav_graphs = get_favorites('web.favorite.graphids'); foreach ($fav_graphs as $key => $favorite) { $source = $favorite['source']; $sourceid = $favorite['value']; if ('itemid' == $source) { if (!($item = get_item_by_itemid($sourceid))) { continue; } $item_added = true; $host = get_host_by_itemid($sourceid); $item["description"] = item_description($item); $graphids[] = array('name' => $host['host'] . ':' . $item['description'], 'favobj' => 'itemid', 'favid' => $sourceid, 'action' => 'remove'); } else { if (!($graph = get_graph_by_graphid($sourceid))) { continue; } $graph_added = true; $result = get_hosts_by_graphid($sourceid); $ghost = DBFetch($result); $graphids[] = array('name' => $ghost['host'] . ':' . $graph['name'], 'favobj' => 'graphid', 'favid' => $sourceid, 'action' => 'remove'); } } if (isset($graph_added)) { $graphids[] = array('name' => S_REMOVE . SPACE . S_ALL_S . SPACE . S_GRAPHS, 'favobj' => 'graphid', 'favid' => 0, 'action' => 'remove'); } if (isset($item_added)) { $graphids[] = array('name' => S_REMOVE . SPACE . S_ALL_S . SPACE . S_SIMPLE_GRAPHS, 'favobj' => 'itemid', 'favid' => 0, 'action' => 'remove'); } return $graphids; }
} } $db_values[$count][$itemid] = is_null($item_data) ? 0 : $item_data; $tmp_color = get_next_palette($palette, $palettetype); if (!isset($graph_data['colors'][$count])) { $graph_data['colors'][$count] = rgb2hex($tmp_color); } $date_caption = $scaletype == TIMEPERIOD_TYPE_HOURLY ? CHARTBAR_HOURLY_DATE_FORMAT : CHARTBAR_DAILY_DATE_FORMAT; $graph_data['legend'][$count] = zbx_date2str($date_caption, $start); $count++; } } foreach ($db_values as $item_data) { $graph->addSeries($item_data); } if (isset($itemid) && ($db_item = get_item_by_itemid($itemid))) { $graph->setUnits($db_item['units']); $graph->setSideValueType($db_item['value_type']); } } if (!isset($graph_data['captions'])) { $graph_data['captions'] = array(); } if (!isset($graph_data['legend'])) { $graph_data['legend'] = ''; } $graph->setSeriesLegend($graph_data['legend']); $graph->setPeriodCaption($graph_data['captions']); $graph->setHeader($title); $graph->setPeriod($scaleperiod); $graph->setXLabel($xlabel);
$drawtype = get_request("drawtype", 0); $sortorder = get_request("sortorder", 0); $yaxisside = get_request("yaxisside", 1); $calc_fnc = get_request("calc_fnc", 2); $type = get_request("type", 0); $periods_cnt = get_request("periods_cnt", 5); $only_hostid = get_request("only_hostid", null); $monitored_hosts = get_request('monitored_hosts', null); $caption = $itemid ? S_UPD_ITEM_FOR_THE_GRAPH : S_NEW_ITEM_FOR_THE_GRAPH; $frmGItem = new CFormTable($caption); $frmGItem->setName('graph_item'); $frmGItem->setHelp("web.graph.item.php"); $frmGItem->addVar('dstfrm', $_REQUEST['dstfrm']); $description = ''; if ($itemid > 0) { $description = get_item_by_itemid($itemid); $description = item_description($description); } $frmGItem->addVar('graphid', $graphid); $frmGItem->addVar('gid', $gid); $frmGItem->addVar('list_name', $list_name); $frmGItem->addVar('itemid', $itemid); $frmGItem->addVar('graphtype', $graphtype); $frmGItem->addVar('only_hostid', $only_hostid); $txtCondVal = new CTextBox('description', $description, 50, 'yes'); $host_condition = ''; if (isset($only_hostid)) { // graph for template must use only one host $host_condition = "&only_hostid=" . $only_hostid; } else { if (isset($monitored_hosts)) {
public function getItemListFormat($list = array()) { $itemList = $list['list_item']; $parame = $list['parame']; $search_key = $parame['item_name_search']; $order_result_list = array(); $search = 0; if (empty($search_key)) { $search_key_list = array(); } else { $search_key_list = explode(",", $search_key); } if (count($search_key_list) >= 1) { $search = 1; } foreach ($itemList as $each_item) { $each_item = (array) $each_item; $item = get_item_by_itemid($each_item['itemid']); $newItem = $item; // $newItem['name'] = itemName($item); $newItem['name'] = $item['name']; $newItem['delay'] = getItemDelay($item['delay'], $item['delay_flex']); $host = get_host_by_hostid($item['hostid']); $newItem['hostname'] = $host['name']; if (strpos($item['units'], ',') !== false) { list($newItem['units'], $newItem['unitsLong']) = explode(',', $item['units']); } else { $newItem['unitsLong'] = ''; } if ($search == 0) { $newItem['graphid'] = $each_item['graphid']; $order_result_list[$newItem['graphid']] = array("graphid" => $newItem['graphid'], "itemname" => $newItem['name'], "lastvalue" => 0, 'min' => 0, 'avg' => 0, 'max' => 0, 'hostname' => $newItem['hostname'], 'chazhi' => 0); } else { foreach ($search_key_list as $each_search_key) { $each_search_key = trim($each_search_key); //按关键字进行筛选 if (strpos(strtolower($newItem['name']), strtolower($each_search_key)) === false) { //echo "bupipei<br>"; } else { $newItem['calc_fnc'] = is_null($each_item['calc_fnc']) ? CALC_FNC_AVG : $each_item->calc_fnc; $newItem['calc_type'] = GRAPH_ITEM_SIMPLE; $newItem['graphid'] = $each_item['graphid']; $item_info_new = array_merge($newItem, $parame); $data = self::GetItemValues($item_info_new); if (isset($data) && isset($data['min'])) { $lastvalue = convert_units(self::getLastValueBy($data), $newItem['units'], ITEM_CONVERT_NO_UNITS); $min = convert_units(min($data['min']), $newItem['units'], ITEM_CONVERT_NO_UNITS); $avg = convert_units($data['avg_orig'], $newItem['units'], ITEM_CONVERT_NO_UNITS); $max = convert_units(max($data['max']), $newItem['units'], ITEM_CONVERT_NO_UNITS); //return array("lastvalue"=>$lastvalue,'min'=>$min,'avg'=>$avg,'max'=>$max); $order_result_list[$each_search_key][$newItem['graphid']] = array("graphid" => $newItem['graphid'], "itemname" => $newItem['name'], "lastvalue" => self::getLastValueBy($data), 'min' => min($data['min']), 'avg' => $data['avg_orig'], 'max' => max($data['max']), 'hostname' => $newItem['hostname'], 'chazhi' => max($data['max']) - min($data['min'])); } else { $order_result_list[$each_search_key][$newItem['graphid']] = array("graphid" => $newItem['graphid'], "itemname" => $newItem['name'], "lastvalue" => 0, 'min' => 0, 'avg' => 0, 'max' => 0, 'hostname' => $newItem['hostname'], 'chazhi' => 0); } break; } } } } return $order_result_list; }