function delete_graph($graphids) { zbx_value2array($graphids); $result = true; $graphs = array(); $host_list = array(); foreach ($graphids as $id => $graphid) { $graphs[$graphid] = get_graph_by_graphid($graphid); $host_list[$graphid] = array(); $db_hosts = get_hosts_by_graphid($graphid); while ($db_host = DBfetch($db_hosts)) { $host_list[$graphid] = '"' . $db_host['host'] . '"'; } } // first remove child graphs $del_chd_graphs = array(); $chd_graphs = get_graphs_by_templateid($graphids); while ($chd_graph = DBfetch($chd_graphs)) { /* recursion */ $del_chd_graphs[$chd_graph['graphid']] = $chd_graph['graphid']; } if (!empty($del_chd_graphs)) { $result &= delete_graph($del_chd_graphs); } DBexecute('DELETE FROM screens_items WHERE ' . DBcondition('resourceid', $graphids) . ' AND resourcetype=' . SCREEN_RESOURCE_GRAPH); // delete graph DBexecute('DELETE FROM graphs_items WHERE ' . DBcondition('graphid', $graphids)); DBexecute("DELETE FROM profiles WHERE idx='web.favorite.graphids' AND source='graphid' AND " . DBcondition('value_id', $graphids)); $result = DBexecute('DELETE FROM graphs WHERE ' . DBcondition('graphid', $graphids)); if ($result) { foreach ($graphs as $graphid => $graph) { if (isset($host_list[$graphid])) { info('Graph "' . $graph['name'] . '" deleted from hosts ' . implode(',', $host_list)); } } } return $result; }
/** * Delete graph items * * @static * @param array $items * @return boolean */ public static function deleteItems($item_list, $force = false) { $error = 'Unknown ZABBIX internal error'; $result = true; $graphid = $item_list['graphid']; $items = $item_list['items']; if (!$force) { // check if graph is templated graph, then items cannot be deleted $graph = CGraph::getById(array('graphid' => $graphid)); if ($graph['templateid'] != 0) { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Cannot edit templated graph : ' . $graph['name']); return false; } } $chd_graphs = get_graphs_by_templateid($graphid); while ($chd_graph = DBfetch($chd_graphs)) { $item_list['graphid'] = $chd_graph['graphid']; $result = self::deleteItems($item_list, true); if (!$result) { return false; } } $sql = 'SELECT curr.itemid FROM graphs_items gi, items curr, items src WHERE gi.graphid=' . $graphid . ' AND gi.itemid=curr.itemid AND curr.key_=src.key_ AND ' . DBcondition('src.itemid', $items); $db_items = DBselect($sql); $gitems = array(); while ($curr_item = DBfetch($db_items)) { $gitems[$curr_item['itemid']] = $curr_item['itemid']; } $sql = 'DELETE FROM graphs_items WHERE graphid=' . $graphid . ' AND ' . DBcondition('itemid', $gitems); $result = DBselect($sql); return $result; }
function delete_graph($graphids) { zbx_value2array($graphids); $result = true; $graphs = array(); $host_list = array(); foreach ($graphids as $id => $graphid) { $graphs[$graphid] = get_graph_by_graphid($graphid); $host_list[$graphid] = array(); $db_hosts = get_hosts_by_graphid($graphid); while ($db_host = DBfetch($db_hosts)) { if (!isset($host_list[$graphid][$db_host['host']])) { $host_list[$graphid][$db_host['host']] = true; } } } // first remove child graphs $del_chd_graphs = array(); $chd_graphs = get_graphs_by_templateid($graphids); while ($chd_graph = DBfetch($chd_graphs)) { /* recursion */ $del_chd_graphs[$chd_graph['graphid']] = $chd_graph['graphid']; } if (!empty($del_chd_graphs)) { $result &= delete_graph($del_chd_graphs); } DBexecute('DELETE FROM screens_items WHERE ' . DBcondition('resourceid', $graphids) . ' AND resourcetype=' . SCREEN_RESOURCE_GRAPH); // delete graph DBexecute('DELETE FROM graphs_items WHERE ' . DBcondition('graphid', $graphids)); DBexecute("DELETE FROM profiles WHERE idx='web.favorite.graphids' AND source='graphid' AND " . DBcondition('value_id', $graphids)); $result = DBexecute('DELETE FROM graphs WHERE ' . DBcondition('graphid', $graphids)); if ($result) { foreach ($graphs as $graphid => $graph) { if (isset($host_list[$graphid])) { info(S_GRAPH_DELETED_FROM_HOSTS_PART1 . SPACE . $graph['name'] . SPACE . S_GRAPH_DELETED_FROM_HOSTS_PART2 . SPACE . (count($host_list[$graphid]) > 1 ? 's' : '') . SPACE . S_GRAPH_DELETED_FROM_HOSTS_PART3 . ':' . SPACE . '"' . implode('","', array_keys($host_list[$graphid])) . '"'); } } } return $result; }