function add_graph_with_items($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 */ unset($new_host_is_template); $host_list = array(); $itemid = array(0); foreach ($gitems as $gitem) { $itemid[] = $gitem['itemid']; } $db_item_hosts = DBselect('SELECT DISTINCT h.hostid,h.host,h.status ' . ' FROM items i, hosts h ' . ' WHERE h.hostid=i.hostid ' . ' AND ' . DBcondition('i.itemid', $itemid)); while ($db_item_host = DBfetch($db_item_hosts)) { $host_list[] = '"' . $db_item_host['host'] . '"'; if (HOST_STATUS_TEMPLATE == $db_item_host['status']) { $new_host_is_template = true; } } if (isset($new_host_is_template) && count($host_list) > 1) { error('Graph "' . $name . '" with template host can not contain items from other hosts.'); return $result; } if ($graphid = add_graph($name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $templateid)) { $result = true; 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']))) { break; } } } if ($result) { info('Graph "' . $name . '" added to hosts ' . implode(',', $host_list)); /* add graphs for child hosts */ $tmp_hosts = get_hosts_by_graphid($graphid); $host = DBfetch($tmp_hosts); $chd_hosts = get_hosts_by_templateid($host['hostid']); while ($chd_host = DBfetch($chd_hosts)) { copy_graph_to_host($graphid, $chd_host['hostid'], false); } } if (!$result && $graphid) { delete_graph($graphid); $graphid = false; } return $graphid; }
/** * Add graph * * <code> * $graphs = array( * *string 'name' => null, * int 'width' => 900, * int 'height' => 200, * int 'ymin_type' => 0, * int 'ymax_type' => 0, * int 'yaxismin' => 0, * int 'yaxismax' => 100, * int 'ymin_itemid' => 0, * int 'ymax_itemid' => 0, * int 'show_work_period' => 1, * int 'show_triggers' => 1, * int 'graphtype' => 0, * int 'show_legend' => 0, * int 'show_3d' => 0, * int 'percent_left' => 0, * int 'percent_right' => 0 * ); * </code> * * @static * @param array $graphs multidimensional array with graphs data * @return boolean */ public static function add($graphs) { $error = 'Unknown ZABBIX internal error'; $result_ids = array(); $result = false; DBstart(false); foreach ($graphs as $graph) { $graph_db_fields = array('name' => null, 'width' => 900, 'height' => 200, 'ymin_type' => 0, 'ymax_type' => 0, 'yaxismin' => 0, 'yaxismax' => 100, 'ymin_itemid' => 0, 'ymax_itemid' => 0, 'showworkperiod' => 1, 'showtriggers' => 1, 'graphtype' => 0, 'legend' => 0, 'graph3d' => 0, 'percent_left' => 0, 'percent_right' => 0, 'templateid' => 0); if (!check_db_fields($graph_db_fields, $graph)) { $result = false; $error = 'Wrong fields for graph [ ' . $graph['name'] . ' ]'; break; } $result = add_graph($graph['name'], $graph['width'], $graph['height'], $graph['ymin_type'], $graph['ymax_type'], $graph['yaxismin'], $graph['yaxismax'], $graph['ymin_itemid'], $graph['ymax_itemid'], $graph['showworkperiod'], $graph['showtriggers'], $graph['graphtype'], $graph['legend'], $graph['graph3d'], $graph['percent_left'], $graph['percent_right'], $graph['templateid']); if (!$result) { break; } $result_ids[$result] = $result; } $result = DBend($result); if ($result) { return $result_ids; } else { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => $error); //'Internal zabbix error'); return false; } }
function add_graph_with_items($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 */ unset($new_host_is_template); $host_list = array(); $itemid = array(0); foreach ($gitems as $gitem) { $itemid[] = $gitem['itemid']; } $db_item_hosts = DBselect('SELECT DISTINCT h.hostid,h.host,h.status ' . ' FROM items i, hosts h ' . ' WHERE h.hostid=i.hostid ' . ' AND ' . DBcondition('i.itemid', $itemid)); $graph_hostids = array(); while ($db_item_host = DBfetch($db_item_hosts)) { $host_list[] = '"' . $db_item_host['host'] . '"'; $graph_hostids[] = $db_item_host['hostid']; if (HOST_STATUS_TEMPLATE == $db_item_host['status']) { $new_host_is_template = true; } } if (isset($new_host_is_template) && count($host_list) > 1) { error(S_GRAPH . SPACE . '"' . $name . '"' . SPACE . S_GRAPH_TEMPLATE_HOST_CANNOT_OTHER_ITEMS_HOSTS_SMALL); return $result; } // $filter = array( // 'name' => $name, // 'hostids' => $graph_hostids // ); // if(CGraph::exists($filter)){ // error('Graph already exists [ '.$name.' ]'); // return false; // } if ($graphid = add_graph($name, $width, $height, $ymin_type, $ymax_type, $yaxismin, $yaxismax, $ymin_itemid, $ymax_itemid, $showworkperiod, $showtriggers, $graphtype, $legend, $graph3d, $percent_left, $percent_right, $templateid)) { $result = true; 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']))) { break; } } } if ($result) { info('Graph "' . $name . '" added to hosts ' . implode(',', $host_list)); /* add graphs for child hosts */ $tmp_hosts = get_hosts_by_graphid($graphid); $host = DBfetch($tmp_hosts); $chd_hosts = get_hosts_by_templateid($host['hostid']); while ($chd_host = DBfetch($chd_hosts)) { copy_graph_to_host($graphid, $chd_host['hostid'], false); } } if (!$result && $graphid) { delete_graph($graphid); $graphid = false; } return $graphid; }