function update_graph_with_items($graphid, $name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $gitems = array(), $templateid = 0) { $result = false; if (!is_array($gitems) || count($gitems) < 1) { error('Missing items for graph "' . $name . '"'); return $result; } /* check items for template graph */ $tmp_hosts = get_hosts_by_graphid($graphid); $host = DBfetch($tmp_hosts); if ($host["status"] == HOST_STATUS_TEMPLATE) { unset($new_hostid); $itemid = array(0); foreach ($gitems as $gitem) { $itemid[] = $gitem['itemid']; } $db_item_hosts = DBselect('SELECT DISTINCT hostid from items where itemid in (' . implode(',', $itemid) . ')'); while ($db_item = DBfetch($db_item_hosts)) { if (isset($new_hostid)) { error('Can not use multiple host items for template graph "' . $name . '"'); return $result; } $new_hostid = $db_item['hostid']; } if (bccomp($host['hostid'], $new_hostid) != 0) { error('You must use items only from host "' . $host['host'] . '" for template graph "' . $name . '"'); return $result; } } /* firstly update child graphs */ $chd_graphs = get_graphs_by_templateid($graphid); while ($chd_graph = DBfetch($chd_graphs)) { $tmp_hosts = get_hosts_by_graphid($chd_graph['graphid']); $chd_host = DBfetch($tmp_hosts); if (!($new_gitems = get_same_graphitems_for_host($gitems, $chd_host['hostid']))) { /* skip host with missing items */ error('Can not update graph "' . $name . '" for host "' . $chd_host['host'] . '"'); return $result; } if (!($result = update_graph_with_items($chd_graph['graphid'], $name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $new_gitems, $graphid))) { return $result; } } DBexecute('DELETE FROM graphs_items WHERE graphid=' . $graphid); foreach ($gitems as $gitem) { if (!($result = add_item_to_graph($graphid, $gitem['itemid'], $gitem['color'], $gitem['drawtype'], $gitem['sortorder'], $gitem['yaxisside'], $gitem['calc_fnc'], $gitem['type'], $gitem['periods_cnt']))) { return $result; } } if ($result = update_graph($graphid, $name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $templateid)) { $host_list = array(); $db_hosts = get_hosts_by_graphid($graphid); while ($db_host = DBfetch($db_hosts)) { $host_list[] = '"' . $db_host["host"] . '"'; } info('Graph "' . $name . '" updated for hosts ' . implode(',', $host_list)); } return $result; }
/** * Update graphs * * @static * @param array $graphs multidimensional array with graphs data * @return boolean */ public static function update($graphs) { $result_ids = array(); $result = false; DBstart(false); foreach ($graphs as $graph) { $host_db_fields = self::getById(array('graphid' => $graph['graphid'])); if (!$host_db_fields) { $result = false; break; } if (!check_db_fields($host_db_fields, $graph)) { $result = false; break; } $result = update_graph($graph['graphid'], $graph['name'], $graph['width'], $graph['height'], $graph['ymin_type'], $graph['ymax_type'], $graph['yaxismin'], $graph['yaxismax'], $graph['ymin_itemid'], $graph['ymax_itemid'], $graph['show_work_period'], $graph['show_triggers'], $graph['graphtype'], $graph['show_legend'], $graph['show_3d'], $graph['percent_left'], $graph['percent_right'], $graph['templateid']); if (!$result) { break; } $result_ids[$graph['graphid']] = $result; } $result = DBend($result); if ($result) { return $result_ids; } else { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Internal zabbix error'); return false; } }
function update_graph_with_items($graphid, $name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $gitems = array(), $templateid = 0) { $result = false; if (!is_array($gitems) || count($gitems) < 1) { error(S_MISSING_ITEMS_FOR_GRAPH . SPACE . '"' . $name . '"'); return $result; } // check items for template graph $tmp_hosts = get_hosts_by_graphid($graphid); $host = DBfetch($tmp_hosts); if ($host["status"] == HOST_STATUS_TEMPLATE) { unset($new_hostid); $itemid = array(0); foreach ($gitems as $gitem) { $itemid[] = $gitem['itemid']; } $db_item_hosts = DBselect('SELECT DISTINCT hostid from items where itemid in (' . implode(',', $itemid) . ')'); while ($db_item = DBfetch($db_item_hosts)) { if (isset($new_hostid)) { error(S_CANNOT_USE_MULTIPLE_HOST_ITEMS_TEMPLATE_GRAPH . SPACE . '"' . $name . '"'); return $result; } $new_hostid = $db_item['hostid']; } if (bccomp($host['hostid'], $new_hostid) != 0) { error(S_MUST_USE_ITEMS_ONLY_FROM_HOST . SPACE . '"' . $host['host'] . '"' . SPACE . S_FOR_TEMPLATE_GRAPH_SMALL . SPACE . '"' . $name . '"'); return $result; } } // firstly update child graphs $chd_graphs = get_graphs_by_templateid($graphid); while ($chd_graph = DBfetch($chd_graphs)) { $tmp_hosts = get_hosts_by_graphid($chd_graph['graphid']); $chd_host = DBfetch($tmp_hosts); if (!($new_gitems = get_same_graphitems_for_host($gitems, $chd_host['hostid']))) { /* skip host with missing items */ error(S_CANNOT_UPDATE_GRAPH . SPACE . '"' . $name . '"' . SPACE . S_FOR_HOST_SMALL . SPACE . '"' . $chd_host['host'] . '"'); return $result; } if (!($result = update_graph_with_items($chd_graph['graphid'], $name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $new_gitems, $graphid))) { return $result; } } DBexecute('DELETE FROM graphs_items WHERE graphid=' . $graphid); foreach ($gitems as $gitem) { if (!($result = add_item_to_graph($graphid, $gitem['itemid'], $gitem['color'], $gitem['drawtype'], $gitem['sortorder'], $gitem['yaxisside'], $gitem['calc_fnc'], $gitem['type'], $gitem['periods_cnt']))) { return $result; } } if ($result = update_graph($graphid, $name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $templateid)) { $host_list = array(); $db_hosts = get_hosts_by_graphid($graphid); while ($db_host = DBfetch($db_hosts)) { $host_list[] = '"' . $db_host["host"] . '"'; } info(S_GRAPH . SPACE . '"' . $name . '"' . SPACE . S_UPDATED_FOR_HOSTS . SPACE . implode(',', $host_list)); } return $result; }