Example #1
0
function export_build_graphs($fp, $path, $tree_id, $parent_tree_item_id)  {
	/* start the count of graphs */
	$total_graphs_created = 0;

	$cacti_export_path = read_config_option("path_html_export");

	$req="";
	if (get_tree_item_type($parent_tree_item_id)=="host")  {
		$req="select distinct
				graph_templates_graph.id,
				graph_templates_graph.local_graph_id,
				graph_templates_graph.height,
				graph_templates_graph.width,
				graph_templates_graph.title_cache,
				graph_templates.name,
				graph_local.host_id
			from graph_templates_graph
				left join graph_templates on (graph_templates_graph.graph_template_id=graph_templates.id)
			    left join graph_local on (graph_templates_graph.local_graph_id=graph_local.id)
			where graph_local.host_id=".get_host_id($parent_tree_item_id)."
			  and graph_templates_graph.local_graph_id!=0
			  and graph_templates_graph.export='on'
			order by graph_templates_graph.title_cache";
	}else {
		/* searching for the graph_tree_items of the tree_id which are graphs */
		$req="select distinct
				graph_templates_graph.id,
				graph_templates_graph.local_graph_id,
				graph_templates_graph.height,
				graph_templates_graph.width,
				graph_templates_graph.title_cache,
				graph_templates.name,
				graph_local.host_id,
				graph_tree_items.id as gtid
			from graph_templates_graph
				left join graph_tree_items on (graph_templates_graph.local_graph_id=graph_tree_items.local_graph_id)
			    left join graph_templates on (graph_templates_graph.graph_template_id=graph_templates.id)
			    left join graph_local on (graph_templates_graph.local_graph_id=graph_local.id)
			where graph_tree_items.graph_tree_id =".$tree_id."
			  and graph_templates_graph.local_graph_id!=0
			  and graph_templates_graph.export='on'
			order by graph_templates_graph.title_cache";
	}

	$graphs=db_fetch_assoc($req);
	$rras = db_fetch_assoc("select
		rra.id,
		rra.name
		from rra
		order by timespan");

	/* open a pipe to rrdtool for writing */
	$rrdtool_pipe = rrd_init();

	/* for each graph... */
	$i = 0;
	foreach($graphs as $graph)  {
		/* this test gives us the graph_tree_items which are just under the parent_graph_tree_item */
		if (((get_tree_item_type($parent_tree_item_id)=="header") || ($parent_tree_item_id == 0)) && (get_parent_id($graph["gtid"], "graph_tree_items","graph_tree_id = ".$tree_id) != $parent_tree_item_id))  {
			/* do nothing */
		}else {
			/* settings for preview graphs */
			$graph_data_array["graph_height"] = "100";
			$graph_data_array["graph_width"] = "300";
			$graph_data_array["graph_nolegend"] = true;
			$graph_data_array["export"] = true;
			$graph_data_array["export_filename"] = "'".$path."'/thumb_".$graph["local_graph_id"].".png";

			rrdtool_function_graph($graph["local_graph_id"], 0, $graph_data_array, $rrdtool_pipe);
			$total_graphs_created++;

			/* generate html files for each graph */
			$fp_graph_index = fopen($cacti_export_path."/".$path."/graph_".$graph["local_graph_id"].".html", "w");
			fwrite($fp_graph_index, HTML_HEADER_TREE);
			draw_html_left_tree($fp_graph_index,$tree_id);
			fwrite($fp_graph_index, HTML_GRAPH_HEADER_ONE_TREE);
			fwrite($fp_graph_index, "<strong>Graph - " . $graph["title_cache"] . "</strong>");
			fwrite($fp_graph_index, HTML_GRAPH_HEADER_TWO);
			fwrite($fp_graph_index, "<td>");

			/* reset vars for actual graph image creation */
			reset($rras);
			unset($graph_data_array);

			/* generate graphs for each rra */
			foreach ($rras as $rra) {
				$graph_data_array["export"] = true;
				$graph_data_array["export_filename"] = "'".$path."'/graph_".$graph["local_graph_id"]."_".$rra["id"].".png";

				rrdtool_function_graph($graph["local_graph_id"], $rra["id"], $graph_data_array, $rrdtool_pipe);
				$total_graphs_created++;

				/* write image related html */
				fwrite($fp_graph_index, "<div align=center><img src='graph_".$graph["local_graph_id"]."_".$rra["id"].".png' border=0></div>\n
					<div align=center><strong>".$rra["name"]."</strong></div><br>");
			}

			fwrite($fp_graph_index, "</tr></table>");
			fwrite($fp_graph_index, HTML_FOOTER_TREE);
			fclose($fp_graph_index);

			/* main graph page html */
			fwrite($fp, "<td align='center' width='\" . (98 / 2) . \"%'><a href='graph_" . $graph["local_graph_id"] . ".html'><img src='thumb_" . $graph["local_graph_id"] . ".png' border='0' alt='" . $graph["title_cache"] . "'></a></td>\n");
			$i++;
			if (($i == 2)) {
				$i = 0;
				fwrite($fp, "</tr><tr>");
			}
		}
	}

	/* close the rrdtool pipe */
	rrd_close($rrdtool_pipe);

	return $total_graphs_created;
}
Example #2
0
function export_tree_graphs_and_graph_html($path, $tree_id)
{
    global $colors, $config;
    include_once $config["library_path"] . "/tree.php";
    include_once $config["library_path"] . "/data_query.php";
    /* start the count of graphs */
    $total_graphs_created = 0;
    $exported_files = array();
    $cacti_export_path = read_config_option("path_html_export");
    /* auth check for hosts on the trees */
    $current_user = db_fetch_row("SELECT * FROM user_auth WHERE id=" . read_config_option("export_user_id"));
    if (!export_is_tree_allowed($tree_id)) {
        return 0;
    }
    $sql_join = "LEFT JOIN graph_local ON (graph_templates_graph.local_graph_id=graph_local.id)\n\t\tLEFT JOIN graph_templates ON (graph_templates.id=graph_local.graph_template_id)\n\t\tLEFT JOIN host ON (host.id=graph_local.host_id)\n\t\tLEFT JOIN user_auth_perms ON ((graph_templates_graph.local_graph_id=user_auth_perms.item_id and user_auth_perms.type=1 AND user_auth_perms.user_id=" . $current_user["id"] . ") OR (host.id=user_auth_perms.item_id AND user_auth_perms.type=3 AND user_auth_perms.user_id=" . $current_user["id"] . ") OR (graph_templates.id=user_auth_perms.item_id AND user_auth_perms.type=4 AND user_auth_perms.user_id=" . $current_user["id"] . "))";
    $sql_where = get_graph_permissions_sql($current_user["policy_graphs"], $current_user["policy_hosts"], $current_user["policy_graph_templates"]);
    $sql_where = empty($sql_where) ? "" : "AND {$sql_where}";
    $graphs = array();
    if ($tree_id == 0) {
        $hosts = db_fetch_assoc("SELECT DISTINCT host_id FROM graph_tree_items");
    } else {
        $hosts = db_fetch_assoc("SELECT DISTINCT host_id FROM graph_tree_items WHERE graph_tree_id=" . $tree_id);
    }
    /* get a list of host graphs first */
    if (sizeof($hosts)) {
        foreach ($hosts as $host) {
            $hosts_sql = "SELECT DISTINCT\n\t\t\tgraph_templates_graph.id,\n\t\t\tgraph_templates_graph.local_graph_id,\n\t\t\tgraph_templates_graph.height,\n\t\t\tgraph_templates_graph.width,\n\t\t\tgraph_templates_graph.title_cache,\n\t\t\tgraph_templates.name,\n\t\t\tgraph_local.host_id\n\t\t\tFROM (graph_tree_items, graph_templates_graph)\n\t\t\t{$sql_join}\n\t\t\tWHERE ((graph_templates_graph.local_graph_id<>0)\n\t\t\t{$sql_where}\n\t\t\tAND (graph_local.host_id=" . $host["host_id"] . ")\n\t\t\tAND (graph_templates_graph.export='on'))\n\t\t\tORDER BY graph_templates_graph.title_cache";
            $host_graphs = db_fetch_assoc($hosts_sql);
            if (sizeof($host_graphs)) {
                if (sizeof($graphs)) {
                    $graphs = array_merge($host_graphs, $graphs);
                } else {
                    $graphs = $host_graphs;
                }
            }
        }
    }
    /* now get the list of graphs placed within the tree */
    if ($tree_id == 0) {
        $sql_where = "WHERE graph_templates_graph.local_graph_id!=0\n\t\t\t{$sql_where}\n\t\t\tAND graph_templates_graph.export='on'";
    } else {
        $sql_where = "WHERE graph_tree_items.graph_tree_id =" . $tree_id . "\n\t\t\t{$sql_where}\n\t\t\tAND graph_templates_graph.local_graph_id!=0\n\t\t\tAND graph_templates_graph.export='on'";
    }
    $non_host_sql = "SELECT\n\t\tgraph_templates_graph.id,\n\t\tgraph_templates_graph.local_graph_id,\n\t\tgraph_templates_graph.height,\n\t\tgraph_templates_graph.width,\n\t\tgraph_templates_graph.title_cache,\n\t\tgraph_templates.name,\n\t\tgraph_local.host_id,\n\t\tgraph_tree_items.id AS gtid\n\t\tFROM (graph_tree_items, graph_templates_graph)\n\t\t{$sql_join}\n\t\t{$sql_where}\n\t\tAND graph_tree_items.local_graph_id = graph_templates_graph.local_graph_id\n\t\tAND graph_templates_graph.export='on'\n\t\tORDER BY graph_templates_graph.title_cache";
    $non_host_graphs = db_fetch_assoc($non_host_sql);
    if (sizeof($non_host_graphs)) {
        if (sizeof($graphs)) {
            $graphs = array_merge($non_host_graphs, $graphs);
        } else {
            $graphs = $non_host_graphs;
        }
    }
    /* open a pipe to rrdtool for writing */
    $rrdtool_pipe = rrd_init();
    /* for each graph... */
    $i = 0;
    if (sizeof($graphs) > 0) {
        foreach ($graphs as $graph) {
            $rras = get_associated_rras($graph["local_graph_id"]);
            /* settings for preview graphs */
            $graph_data_array["graph_height"] = read_config_option("export_default_height");
            $graph_data_array["graph_width"] = read_config_option("export_default_width");
            $graph_data_array["graph_nolegend"] = true;
            $graph_data_array["export"] = true;
            if (read_config_option("export_tree_isolation") == "on") {
                $graph_data_array["export_filename"] = "/" . $path . "/graphs/thumb_" . $graph["local_graph_id"] . ".png";
                $export_filename = $cacti_export_path . "/" . $path . "/graphs/thumb_" . $graph["local_graph_id"] . ".png";
            } else {
                $graph_data_array["export_filename"] = "/graphs/thumb_" . $graph["local_graph_id"] . ".png";
                $export_filename = $cacti_export_path . "/graphs/thumb_" . $graph["local_graph_id"] . ".png";
            }
            if (!array_search($export_filename, $exported_files)) {
                /* add the graph to the exported list */
                array_push($exported_files, $export_filename);
                export_log("Creating Graph '" . $cacti_export_path . $graph_data_array["export_filename"] . "'");
                /* generate the graph */
                rrdtool_function_graph($graph["local_graph_id"], 0, $graph_data_array, $rrdtool_pipe);
                $total_graphs_created++;
                /* generate html files for each graph */
                if (read_config_option("export_tree_isolation") == "on") {
                    export_log("Creating File  '" . $cacti_export_path . "/" . $path . "/graph_" . $graph["local_graph_id"] . ".html'");
                    $fp_graph_index = fopen($cacti_export_path . "/" . $path . "/graph_" . $graph["local_graph_id"] . ".html", "w");
                } else {
                    export_log("Creating File  '" . $cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html'");
                    $fp_graph_index = fopen($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html", "w");
                }
                fwrite($fp_graph_index, HTML_HEADER_TREE);
                /* write the code for the tree at the left */
                draw_html_left_tree($fp_graph_index, $tree_id);
                fwrite($fp_graph_index, HTML_GRAPH_HEADER_ONE_TREE);
                fwrite($fp_graph_index, "<strong>Graph - " . $graph["title_cache"] . "</strong></td></tr>");
                fwrite($fp_graph_index, HTML_GRAPH_HEADER_TWO_TREE);
                fwrite($fp_graph_index, "<td>");
                /* reset vars for actual graph image creation */
                reset($rras);
                unset($graph_data_array);
                /* generate graphs for each rra */
                foreach ($rras as $rra) {
                    $graph_data_array["export"] = true;
                    if (read_config_option("export_tree_isolation") == "on") {
                        $graph_data_array["export_filename"] = "/" . $path . "/graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";
                    } else {
                        $graph_data_array["export_filename"] = "/graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";
                    }
                    export_log("Creating Graph '" . $cacti_export_path . $graph_data_array["export_filename"] . "'");
                    rrdtool_function_graph($graph["local_graph_id"], $rra["id"], $graph_data_array, $rrdtool_pipe);
                    $total_graphs_created++;
                    /* write image related html */
                    if (read_config_option("export_tree_isolation") == "off") {
                        fwrite($fp_graph_index, "<div align=center><img src='graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png' border=0></div>\n\n\t\t\t\t\t\t<div align=center><strong>" . $rra["name"] . "</strong></div><br>");
                    } else {
                        fwrite($fp_graph_index, "<div align=center><img src='" . "graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png' border=0></div>\n\n\t\t\t\t\t\t<div align=center><strong>" . $rra["name"] . "</strong></div><br>");
                    }
                }
                fwrite($fp_graph_index, "</td></tr></table></td></tr></table>");
                fwrite($fp_graph_index, HTML_FOOTER_TREE);
                fclose($fp_graph_index);
            }
        }
    }
    /* close the rrdtool pipe */
    rrd_close($rrdtool_pipe);
    return $total_graphs_created;
}