Tommy 碎碎念

Tommy Wu's blog

« 上一篇 | 下一篇 »

解決 cacti export graph 時, 無法看到網頁的問題
post by tommy @ 18 五月, 2006 12:02
利用用 cacti 來取代原本 mrtg, 雖然 cacti 有個 export 的功能, 可以每隔一段時間, 產生與原本 mrtg 類似的網頁, 以方便瀏覽. 不過, 有個小問題就是: cacti 在 export 之前, 會把該目錄下的所有檔案先刪除, 然後再產生新的檔案. 這時, 如果有人在瀏覽該網頁, 就會發現少了一些圖, 或者是完全看不到東西.

所以動手改了 cacti 這部份的運作. 先建立一個暫存目錄, 然後把網頁與圖檔都產生在這個暫存目錄下, 等全部產生後, 再搬到原本的目錄去覆蓋. 這樣子, 用來避免前面所說的問題.

這個 patch 如下:

--- graph_export.old.php	2006-05-18 11:48:54.241173819 +0800
+++ graph_export.new.php 2006-05-18 11:55:00.362298171 +0800
@@ -308,13 +308,18 @@
}

/* delete all files and directories in the cacti_export_path */
- del_directory($cacti_export_path, false);
+ //del_directory($cacti_export_path, false);

/* test how will the export will be made */
if (read_config_option('export_presentation') == 'tree') {
+ /* delete all files and directories in the cacti_export_path */
+ del_directory($cacti_export_path, false);
export_log("Running graph export with tree organization");
$total_graphs_created = tree_export();
}else {
+ /* delete all files and directories in the cacti_export_path */
+ del_directory($cacti_export_path.'/tmp', false);
+ @mkdir($cacti_export_path.'/tmp');
/* copy the css/images on the first time */
if (file_exists("$cacti_export_path/main.css") == false) {
copy("$cacti_root_path/include/main.css", "$cacti_export_path/main.css");
@@ -325,10 +330,12 @@
}

/* if the index file already exists, delete it */
- check_remove($cacti_export_path . "/index.html");
+ //check_remove($cacti_export_path . "/index.html");
+ check_remove($cacti_export_path . "/tmp/index.html");

/* open pointer to the new index file */
- $fp_index = fopen($cacti_export_path . "/index.html", "w");
+ //$fp_index = fopen($cacti_export_path . "/index.html", "w");
+ $fp_index = fopen($cacti_export_path . "/tmp/index.html", "w");

/* get a list of all graphs that need exported */
$graphs = db_fetch_assoc("select
@@ -364,20 +371,24 @@
$i = 0; $k = 0;
if ((sizeof($graphs) > 0) && (sizeof($rras) > 0)) {
foreach ($graphs as $graph) {
- check_remove($cacti_export_path . "/thumb_" . $graph["local_graph_id"] . ".png");
- check_remove($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html");
+ //check_remove($cacti_export_path . "/thumb_" . $graph["local_graph_id"] . ".png");
+ //check_remove($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html");
+ check_remove($cacti_export_path . "/tmp/thumb_" . $graph["local_graph_id"] . ".png");
+ check_remove($cacti_export_path . "/tmp/graph_" . $graph["local_graph_id"] . ".html");

/* 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"] = "thumb_" . $graph["local_graph_id"] . ".png";
+ //$graph_data_array["export_filename"] = "thumb_" . $graph["local_graph_id"] . ".png";
+ $graph_data_array["export_filename"] = "tmp/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 . "/graph_" . $graph["local_graph_id"] . ".html", "w");
+ //$fp_graph_index = fopen($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html", "w");
+ $fp_graph_index = fopen($cacti_export_path . "/tmp/graph_" . $graph["local_graph_id"] . ".html", "w");

fwrite($fp_graph_index, HTML_HEADER);
fwrite($fp_graph_index, HTML_GRAPH_HEADER_ONE);
@@ -392,7 +403,8 @@
/* generate graphs for each rra */
foreach ($rras as $rra) {
$graph_data_array["export"] = true;
- $graph_data_array["export_filename"] = "graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";
+ //$graph_data_array["export_filename"] = "graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";
+ $graph_data_array["export_filename"] = "tmp/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++;
@@ -428,6 +440,18 @@
fwrite($fp_index, HTML_GRAPH_FOOTER);
fwrite($fp_index, HTML_FOOTER);
fclose($fp_index);
+ // move file from tmp to realworld
+ if ($dh = opendir($cacti_export_path.'/tmp')) {
+ while (($file = readdir($dh)) !== false) {
+ if ($file == '.' || $file == '..') continue;
+ if (is_dir($cacti_export_path.'/tmp/'.$file)) continue;
+ $new_file = $cacti_export_path.'/'.$file;
+ $old_file = $cacti_export_path.'/tmp/'.$file;
+ @unlink($new_file);
+ @rename($old_file, $new_file);
+ }
+ closedir($dh);
+ }
}

return $total_graphs_created;

也可以直接由這兒抓取: http://www.teatime.com.tw/~tommy/mypatch/graph_export.patch 

 

Del.icio.us Furl HEMiDEMi Technorati MyShare
迴響
暱稱:
標題:
個人網頁:
電子郵件:
authimage

迴響

  

Bad Behavior 已經阻擋了 97 個過去 7 天試圖闖關的垃圾迴響與引用。
Power by LifeType. Template design by JamesHuang. Valid XHTML and CSS