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; }
function insert_graph_form() { $frmGraph = new CFormTable(S_GRAPH, null, 'post'); $frmGraph->setName('frm_graph'); //$frmGraph->setHelp("web.graphs.graph.php"); $items = get_request('items', array()); if (isset($_REQUEST['graphid'])) { $frmGraph->addVar('graphid', $_REQUEST['graphid']); $options = array('graphids' => $_REQUEST['graphid'], 'extendoutput' => 1); $graphs = CGraph::get($options); $row = reset($graphs); $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']; $options = array('graphids' => $_REQUEST['graphid'], 'sortfield' => 'sortorder', 'extendoutput' => 1); $items = CGraphItem::get($options); } 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) { unset($items[$gid]); } } } $items = array_values($items); $icount = count($items); for ($i = 0; $i < $icount - 1;) { // check if we deletd an item $next = $i + 1; while (!isset($items[$next]) && $next < $icount - 1) { $next++; } if (isset($items[$next]) && $items[$i]['sortorder'] == $items[$next]['sortorder']) { for ($j = $next; $j < $icount; $j++) { if ($items[$j - 1]['sortorder'] >= $items[$j]['sortorder']) { $items[$j]['sortorder']++; } } } $i = $next; } asort_by_key($items, 'sortorder'); $items = array_values($items); $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)); $frmGraph->addRow(S_WIDTH, new CNumericBox('width', $width, 5)); $frmGraph->addRow(S_HEIGHT, new CNumericBox('height', $height, 5)); $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); // items beforehead, to get only_hostid for miny maxy items $only_hostid = null; $monitored_hosts = null; if (count($items)) { $frmGraph->addVar('items', $items); $keys = array_keys($items); $first = reset($keys); $last = end($keys); $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']); } if ($gid == $first) { $do_up = null; } else { $do_up = new CSpan(S_UP, 'link'); $do_up->onClick("return create_var('" . $frmGraph->getName() . "','move_up'," . $gid . ", true);"); } if ($gid == $last) { $do_down = null; } else { $do_down = new CSpan(S_DOWN, 'link'); $do_down->onClick("return create_var('" . $frmGraph->getName() . "','move_down'," . $gid . ", true);"); } $description = new CSpan($host['host'] . ': ' . item_description($item), 'link'); $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])), $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])), $description, graph_item_calc_fnc2str($gitem["calc_fnc"], $gitem["type"]), graph_item_type2str($gitem['type'], $gitem["periods_cnt"]), $gitem['yaxisside'] == GRAPH_YAXIS_SIDE_LEFT ? S_LEFT : S_RIGHT, graph_item_drawtype2str($gitem["drawtype"], $gitem["type"]), $color, array($do_up, !is_null($do_up) && !is_null($do_down) ? SPACE . "|" . SPACE : '', $do_down))); } } $dedlete_button = new CButton('delete_item', S_DELETE_SELECTED); } else { $items_table = $dedlete_button = null; } // $frmGraph->addRow(S_SHOW_LEGEND, new CCheckBox('legend',$legend, null, 1)); 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->setAttribute('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->setAttribute('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); } if (count($items)) { $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() . url_param($only_hostid, false, 'only_hostid') . url_param($monitored_hosts, false, 'monitored_hosts') . "&dstfld1=ymin_itemid" . "&dstfld2=ymin_name" . "&srctbl=items" . "&srcfld1=itemid" . "&srcfld2=description',0,0,'zbx_popup_item');"); } else { $yaxis_min[] = SPACE . S_ADD_GRAPH_ITEMS; } } 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); } if (count($items)) { $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() . url_param($only_hostid, false, 'only_hostid') . url_param($monitored_hosts, false, 'monitored_hosts') . "&dstfld1=ymax_itemid" . "&dstfld2=ymax_name" . "&srctbl=items" . "&srcfld1=itemid" . "&srcfld2=description',0,0,'zbx_popup_item');"); } else { $yaxis_max[] = SPACE . S_ADD_GRAPH_ITEMS; } } else { $frmGraph->addVar('yaxismax', $yaxismax); } } $frmGraph->addRow(S_YAXIS_MAX_VALUE, $yaxis_max); } else { $frmGraph->addRow(S_SHOW_LEGEND, new CCheckBox('legend', $legend, null, 1)); $frmGraph->addRow(S_3D_VIEW, new CCheckBox('graph3d', $graph3d, null, 1)); } $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); $preView = new CButton('preview', S_PREVIEW); //$preView->setAttribute('style', 'float: left;'); $frmGraph->addItemToBottomRow($preView); $frmGraph->addItemToBottomRow(SPACE); $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(); }
protected static function inherit($graph, $hostids = null) { $options = array('itemids' => zbx_objectValues($graph['gitems'], 'itemid'), 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => 1); $graph_templates = CTemplate::get($options); if (empty($graph_templates)) { return true; } //----- $graphTemplate = reset($graph_templates); $options = array('templateids' => $graphTemplate['templateid'], 'output' => array('hostid', 'host'), 'preservekeys' => 1, 'hostids' => $hostids, 'nopermissions' => 1, 'templated_hosts' => 1); $chd_hosts = CHost::get($options); $options = array('graphids' => $graph['graphid'], 'nopermissions' => 1, 'select_items' => API_OUTPUT_EXTEND, 'select_graph_items' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND); $graph = self::get($options); $graph = reset($graph); foreach ($chd_hosts as $chd_host) { $tmp_graph = $graph; $tmp_graph['templateid'] = $graph['graphid']; if (!($tmp_graph['gitems'] = get_same_graphitems_for_host($tmp_graph['gitems'], $chd_host['hostid']))) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Graph [ ' . $tmp_graph['name'] . ' ]: cannot inherit. No required items on [ ' . $chd_host['host'] . ' ]'); } if ($tmp_graph['ymax_itemid'] > 0) { $ymax_itemid = get_same_graphitems_for_host(array(array('itemid' => $tmp_graph['ymax_itemid'])), $chd_host['hostid']); if (!$ymax_itemid) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Graph [ ' . $tmp_graph['name'] . ' ]: cannot inherit. No required items on [ ' . $chd_host['host'] . ' ] (Ymax value item)'); } $ymax_itemid = reset($ymax_itemid); $tmp_graph['ymax_itemid'] = $ymax_itemid['itemid']; } if ($tmp_graph['ymin_itemid'] > 0) { $ymin_itemid = get_same_graphitems_for_host(array(array('itemid' => $tmp_graph['ymin_itemid'])), $chd_host['hostid']); if (!$ymin_itemid) { self::exception(ZBX_API_ERROR_PARAMETERS, 'Graph [ ' . $tmp_graph['name'] . ' ]: cannot inherit. No required items on [ ' . $chd_host['host'] . ' ] (Ymin value item)'); } $ymin_itemid = reset($ymin_itemid); $tmp_graph['ymin_itemid'] = $ymin_itemid['itemid']; } // check if templated graph exists $chd_graph = self::get(array('filter' => array('templateid' => $tmp_graph['graphid']), 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1, 'hostids' => $chd_host['hostid'])); if ($chd_graph = reset($chd_graph)) { if (zbx_strtolower($tmp_graph['name']) != zbx_strtolower($chd_graph['name']) && self::exists(array('name' => $tmp_graph['name'], 'hostids' => $chd_host['hostid']))) { self::exception(ZBX_API_ERROR_PARAMETERS, sprintf(S_GRAPH_ALREADY_EXISTS_ON, $tmp_graph['name'], $chd_host['host'])); } $tmp_graph['graphid'] = $chd_graph['graphid']; self::updateReal($tmp_graph); } else { $options = array('filter' => array('name' => $tmp_graph['name']), 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1, 'nopermissions' => 1, 'hostids' => $chd_host['hostid']); $chd_graph = self::get($options); if ($chd_graph = reset($chd_graph)) { if ($chd_graph['templateid'] != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, sprintf(S_GRAPH_ALREADY_EXISTS_ON, $tmp_graph['name'], $chd_host['host']) . SPACE . S_INHERITED_FROM_ANOTHER_TEMPLATE); } $options = array('graphids' => $chd_graph['graphid'], 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1, 'expandData' => 1, 'nopermissions' => 1); $chd_graph_items = CGraphItem::get($options); if (count($chd_graph_items) == count($tmp_graph['gitems'])) { foreach ($tmp_graph['gitems'] as $gitem) { foreach ($chd_graph_items as $chd_item) { if ($gitem['key_'] == $chd_item['key_'] && bccomp($chd_host['hostid'], $chd_item['hostid']) == 0) { continue 2; } } self::exception(ZBX_API_ERROR_PARAMETERS, sprintf(S_GRAPH_ALREADY_EXISTS_ON, $tmp_graph['name'], $chd_host['host']) . SPACE . S_ITEMS_ARE_NOT_IDENTICAL); } $tmp_graph['graphid'] = $chd_graph['graphid']; self::updateReal($tmp_graph); } else { self::exception(ZBX_API_ERROR_PARAMETERS, sprintf(S_GRAPH_ALREADY_EXISTS_ON, $tmp_graph['name'], $chd_host['host']) . SPACE . S_ITEMS_ARE_NOT_IDENTICAL); } } else { $graphid = self::createReal($tmp_graph); $tmp_graph['graphid'] = $graphid; } } self::inherit($tmp_graph); } }
$hostids = get_request('hosts', array()); if ($EXPORT_DATA) { // SELECT HOSTS $params = array('hostids' => $hostids, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1, 'select_profile' => 1); $hosts = CHost::get($params); order_result($hosts, 'host'); // SELECT HOST GROUPS $params = array('hostids' => $hostids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $groups = CHostGroup::get($params); // SELECT GRAPHS $params = array('hostids' => $hostids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $graphs = CGraph::get($params); // SELECT GRAPH ITEMS $graphids = zbx_objectValues($graphs, 'graphid'); $params = array('graphids' => $graphids, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => 1, 'expandData' => 1); $gitems = CGraphItem::get($params); foreach ($gitems as $gnum => $gitem) { $gitems[$gitem['gitemid']]['host_key_'] = $gitem['host'] . ':' . $gitem['key_']; } // SELECT TEMPLATES $params = array('hostids' => $hostids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $templates = CTemplate::get($params); // SELECT MACROS $params = array('hostids' => $hostids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $macros = CUserMacro::get($params); // SELECT ITEMS $params = array('hostids' => $hostids, 'preservekeys' => 1, 'output' => API_OUTPUT_EXTEND); $items = CItem::get($params); // SELECT APPLICATIONS $itemids = zbx_objectValues($items, 'itemid'); //sdii($itemids);