summaryrefslogtreecommitdiffstats
path: root/frontends/php
diff options
context:
space:
mode:
authorosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2005-12-30 11:36:15 +0000
committerosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2005-12-30 11:36:15 +0000
commitac9c6ecc360df3e9347194fbb2924b2bf68954f4 (patch)
treea0f29bb7e8d470b6576adc7248e97cc91ea9da6b /frontends/php
parentd5dce47a8b216a4db6ba2d075edd0e2de7556b6a (diff)
downloadzabbix-ac9c6ecc360df3e9347194fbb2924b2bf68954f4.tar.gz
zabbix-ac9c6ecc360df3e9347194fbb2924b2bf68954f4.tar.xz
zabbix-ac9c6ecc360df3e9347194fbb2924b2bf68954f4.zip
- added Graphs synchronization for linked hosts (Eugene)
git-svn-id: svn://svn.zabbix.com/trunk@2458 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'frontends/php')
-rw-r--r--frontends/php/chart2.php2
-rw-r--r--frontends/php/chart_sla.php2
-rw-r--r--frontends/php/graph.php21
-rw-r--r--frontends/php/graphs.php10
-rw-r--r--frontends/php/image.php2
-rw-r--r--frontends/php/include/config.inc.php3
-rw-r--r--frontends/php/include/db.inc.php3
-rw-r--r--frontends/php/include/graphs.inc.php489
-rw-r--r--frontends/php/include/hosts.inc.php19
-rw-r--r--frontends/php/include/items.inc.php19
10 files changed, 453 insertions, 117 deletions
diff --git a/frontends/php/chart2.php b/frontends/php/chart2.php
index 2636d51d..6df46a4f 100644
--- a/frontends/php/chart2.php
+++ b/frontends/php/chart2.php
@@ -76,7 +76,5 @@
$graph->setColor($row["itemid"], $row["color"]);
$graph->setDrawtype($row["itemid"], $row["drawtype"]);
}
-
$graph->Draw();
?>
-
diff --git a/frontends/php/chart_sla.php b/frontends/php/chart_sla.php
index a07dccd8..04a11c5a 100644
--- a/frontends/php/chart_sla.php
+++ b/frontends/php/chart_sla.php
@@ -30,7 +30,7 @@
$sizeX=200;
$sizeY=15;
-// Header( "Content-type: text/html");
+# Header( "Content-type: text/html");
Header( "Content-type: image/png");
Header( "Expires: Mon, 17 Aug 1998 12:51:50 GMT");
diff --git a/frontends/php/graph.php b/frontends/php/graph.php
index db61ca77..5d1b85e4 100644
--- a/frontends/php/graph.php
+++ b/frontends/php/graph.php
@@ -25,12 +25,10 @@
show_header($page["title"],0,0);
insert_confirm_javascript();
?>
-
<?php
show_table_header(S_CONFIGURATION_OF_GRAPH_BIG);
echo "<br>";
?>
-
<?php
if(!check_right("Graph","R",$_REQUEST["graphid"]))
{
@@ -39,16 +37,17 @@
exit;
}
?>
-
<?php
+
if(isset($_REQUEST["register"]))
{
if($_REQUEST["register"]=="add")
{
+ add_graph_item_to_templates($_REQUEST["graphid"],$_REQUEST["itemid"],$_REQUEST["color"],$_REQUEST["drawtype"],$_REQUEST["sortorder"],$_REQUEST["yaxisside"]);
+
$gitemid=add_item_to_graph($_REQUEST["graphid"],$_REQUEST["itemid"],$_REQUEST["color"],$_REQUEST["drawtype"],$_REQUEST["sortorder"],$_REQUEST["yaxisside"]);
if($gitemid)
{
- add_graph_item_to_linked_hosts($gitemid);
$graph=get_graph_by_graphid($_REQUEST["graphid"]);
$item=get_item_by_itemid($_REQUEST["itemid"]);
add_audit(AUDIT_ACTION_ADD,AUDIT_RESOURCE_GRAPH_ELEMENT,"Graph ID [".$_REQUEST["graphid"]."] Name [".$graph["name"]."] Added [".$item["description"]."]");
@@ -57,6 +56,8 @@
}
if($_REQUEST["register"]=="update")
{
+ update_graph_item_from_templates($_REQUEST["gitemid"],$_REQUEST["itemid"],$_REQUEST["color"],$_REQUEST["drawtype"],$_REQUEST["sortorder"],$_REQUEST["yaxisside"]);
+
$result=update_graph_item($_REQUEST["gitemid"],$_REQUEST["itemid"],$_REQUEST["color"],$_REQUEST["drawtype"],$_REQUEST["sortorder"],$_REQUEST["yaxisside"]);
if($result)
{
@@ -83,22 +84,22 @@
}
if($_REQUEST["register"]=="up")
{
- $sql="update graphs_items set sortorder=sortorder-1 where sortorder>0 and gitemid=".$_REQUEST["gitemid"];
- $result=DBexecute($sql);
+ move_up_graph_item_from_templates($_REQUEST["gitemid"]);
+ $result = move_up_graph_item($_REQUEST["gitemid"]);
show_messages($result, S_SORT_ORDER_UPDATED, S_CANNOT_UPDATE_SORT_ORDER);
unset($_REQUEST["gitemid"]);
}
if($_REQUEST["register"]=="down")
{
- $sql="update graphs_items set sortorder=sortorder+1 where sortorder<100 and gitemid=".$_REQUEST["gitemid"];
- $result=DBexecute($sql);
+ move_down_graph_item_from_templates($_REQUEST["gitemid"]);
+ $result = move_down_graph_item($_REQUEST["gitemid"]);
show_messages($result, S_SORT_ORDER_UPDATED, S_CANNOT_UPDATE_SORT_ORDER);
unset($_REQUEST["gitemid"]);
}
}
?>
-
<?php
+
$result=DBselect("select name from graphs where graphid=".$_REQUEST["graphid"]);
$row=DBfetch($result);
show_table_header($row["name"]);
@@ -144,7 +145,6 @@
}
echo "</TABLE>";
?>
-
<?php
echo "<br>";
echo "<a name=\"form\"></a>";
@@ -247,7 +247,6 @@
show_table2_header_end();
?>
-
<?php
show_footer();
?>
diff --git a/frontends/php/graphs.php b/frontends/php/graphs.php
index b394da38..882ea298 100644
--- a/frontends/php/graphs.php
+++ b/frontends/php/graphs.php
@@ -26,11 +26,9 @@
show_header($page["title"],0,0);
insert_confirm_javascript();
?>
-
<?php
show_table_header(S_CONFIGURATION_OF_GRAPHS_BIG);
?>
-
<?php
if(!check_anyright("Graph","U"))
{
@@ -42,7 +40,6 @@
update_profile("web.latest.hostid",$_REQUEST["hostid"]);
update_profile("web.menu.config.last",$page["file"]);
?>
-
<?php
if(isset($_REQUEST["register"]))
{
@@ -57,6 +54,8 @@
}
if($_REQUEST["register"]=="update")
{
+ update_graph_from_templates($_REQUEST["graphid"],$_REQUEST["name"],$_REQUEST["width"],$_REQUEST["height"],$_REQUEST["yaxistype"],$_REQUEST["yaxismin"],$_REQUEST["yaxismax"]);
+
$result=update_graph($_REQUEST["graphid"],$_REQUEST["name"],$_REQUEST["width"],$_REQUEST["height"],$_REQUEST["yaxistype"],$_REQUEST["yaxismin"],$_REQUEST["yaxismax"]);
if($result)
{
@@ -66,6 +65,7 @@
}
if($_REQUEST["register"]=="delete")
{
+ delete_graph_from_templates($_REQUEST["graphid"]);
$graph=get_graph_by_graphid($_REQUEST["graphid"]);
$result=delete_graph($_REQUEST["graphid"]);
if($result)
@@ -77,7 +77,6 @@
}
}
?>
-
<?php
if(isset($_REQUEST["groupid"])&&($_REQUEST["groupid"]==0))
{
@@ -155,7 +154,6 @@
show_header2($h1,$h2,"<form name=\"form2\" method=\"get\" action=\"graphs.php\">","</form>");
?>
-
<?php
table_begin();
table_header(array(S_ID,S_NAME,S_WIDTH,S_HEIGHT,S_ACTIONS));
@@ -199,14 +197,12 @@
}
table_end();
?>
-
<?php
echo "<a name=\"form\"></a>";
insert_graph_form();
?>
-
<?php
show_footer();
?>
diff --git a/frontends/php/image.php b/frontends/php/image.php
index 16e3076b..22ff0ec8 100644
--- a/frontends/php/image.php
+++ b/frontends/php/image.php
@@ -25,7 +25,7 @@
# imageid
-# Header( "Content-type: text/html");
+ Header( "Content-type: text/html");
Header( "Content-type: image/png");
Header( "Expires: Mon, 17 Aug 1998 12:51:50 GMT");
diff --git a/frontends/php/include/config.inc.php b/frontends/php/include/config.inc.php
index 05fd5854..f648a75a 100644
--- a/frontends/php/include/config.inc.php
+++ b/frontends/php/include/config.inc.php
@@ -17,6 +17,9 @@
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
+
+function SDI($msg) { echo "DEBUG INFO: $msg <br>"; } # DEBUG INFO!!!
+
?>
<?php
// GLOBALS
diff --git a/frontends/php/include/db.inc.php b/frontends/php/include/db.inc.php
index 2b04fdae..26716087 100644
--- a/frontends/php/include/db.inc.php
+++ b/frontends/php/include/db.inc.php
@@ -97,6 +97,7 @@
$result=pg_exec($DB,$query);
return $result;
}
+ return FALSE;
}
function DBfetch($result)
@@ -113,6 +114,7 @@
$row=pg_fetch_array($result);
return $row;
}
+ return FALSE;
}
function get_field($result,$rownum,$fieldnum)
@@ -149,6 +151,7 @@
{
return pg_numrows($result);
}
+ return 0;
}
function DBinsert_id($result,$table,$field)
diff --git a/frontends/php/include/graphs.inc.php b/frontends/php/include/graphs.inc.php
index d27cb5eb..a06d80c0 100644
--- a/frontends/php/include/graphs.inc.php
+++ b/frontends/php/include/graphs.inc.php
@@ -19,6 +19,51 @@
**/
?>
<?php
+ function get_graphs_by_hostid($hostid)
+ {
+ $sql="select distinct g.* from graphs g, graphs_items gi, items i where g.graphid=gi.graphid and gi.itemid=i.itemid and i.hostid=$hostid";
+ $graphs=DBselect($sql);
+ return $graphs;
+ }
+
+ function get_hosts_by_graphid($graphid)
+ {
+ $sql="select distinct h.* from graphs_items gi, items i, hosts h where h.hostid=i.hostid and gi.itemid=i.itemid and gi.graphid=$graphid";
+ $graphs=DBselect($sql);
+ return $graphs;
+ }
+
+ function get_graphitems_by_graphid($graphid)
+ {
+ $sql="select * from graphs_items where graphid=$graphid order by itemid,drawtype,sortorder,color,yaxisside";
+ $result=DBselect($sql);
+ return $result;
+ }
+
+ function get_graphitem_by_gitemid($gitemid)
+ {
+ $sql="select * from graphs_items where gitemid=$gitemid";
+ $result=DBselect($sql);
+ if(DBnum_rows($result) == 1)
+ {
+ return DBfetch($result);
+ }
+ error("No graph item with gitemid=[$gitemid]");
+ return $result;
+ }
+
+ function get_graph_by_graphid($graphid)
+ {
+ $sql="select * from graphs where graphid=$graphid";
+ $result=DBselect($sql);
+ if(DBnum_rows($result) == 1)
+ {
+ return DBfetch($result);
+ }
+ error("No graph with graphid=[$graphid]");
+ return $result;
+ }
+
# Add Graph
function add_graph($name,$width,$height,$yaxistype,$yaxismin,$yaxismax)
@@ -34,19 +79,6 @@
return DBinsert_id($result,"graphs","graphid");
}
- function update_graph_item($gitemid,$itemid,$color,$drawtype,$sortorder,$yaxisside)
- {
- $sql="update graphs_items set itemid=$itemid,color='$color',drawtype=$drawtype,sortorder=$sortorder,yaxisside=$yaxisside where gitemid=$gitemid";
- return DBexecute($sql);
- }
-
- function add_item_to_graph($graphid,$itemid,$color,$drawtype,$sortorder,$yaxisside)
- {
- $sql="insert into graphs_items (graphid,itemid,color,drawtype,sortorder,yaxisside) values ($graphid,$itemid,'$color',$drawtype,$sortorder,$yaxisside)";
- $result=DBexecute($sql);
- return DBinsert_id($result,"graphs_items","gitemid");
- }
-
# Update Graph
function update_graph($graphid,$name,$width,$height,$yaxistype,$yaxismin,$yaxismax)
@@ -56,17 +88,10 @@
error("Insufficient permissions");
return 0;
}
-
$sql="update graphs set name='$name',width=$width,height=$height,yaxistype=$yaxistype,yaxismin=$yaxismin,yaxismax=$yaxismax where graphid=$graphid";
return DBexecute($sql);
}
-
- function delete_graphs_item($gitemid)
- {
- $sql="delete from graphs_items where gitemid=$gitemid";
- return DBexecute($sql);
- }
-
+
# Delete Graph
function delete_graph($graphid)
@@ -81,91 +106,299 @@
return DBexecute($sql);
}
- function get_graphitem_by_gitemid($gitemid)
+ function update_graph_from_templates($graphid,$name,$width,$height,$yaxistype,$yaxismin,$yaxismax)
{
- $sql="select * from graphs_items where gitemid=$gitemid";
- $result=DBselect($sql);
- if(DBnum_rows($result) == 1)
- {
- return DBfetch($result);
- }
- else
+ if($graphid<=0) return;
+
+ $hosts = get_hosts_by_graphid($graphid);
+ if(!$hosts) return;
+ if(DBnum_rows($hosts)!=1) return;
+ $host=DBfetch($hosts);
+
+ $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$host["hostid"];
+ $templates=DBselect($sql);
+ while($template=DBfetch($templates))
{
- error("No graph item with gitemid=[$gitemid]");
+ if($template["graphs"]&4 == 0) continue;
+
+ $graphs=get_graphs_by_hostid($template["hostid"]);
+ while($graph=DBfetch($graphs))
+ {
+ if(!cmp_graphs($graphid,$graph["graphid"])) continue;
+ if(!cmp_graph_by_item_key($graphid,$graph["graphid"])) continue;
+ update_graph($graph["graphid"],$name,$width,$height,$yaxistype,$yaxismin,$yaxismax);
+ $template_host=get_host_by_hostid($template["hostid"]);
+ info("Updated graph '".$graph["name"]."' from linked host '".$template_host["host"]."'");
+ }
}
- return $result;
}
- function get_graph_by_graphid($graphid)
+ function delete_graph_from_templates($graphid)
{
- $sql="select * from graphs where graphid=$graphid";
- $result=DBselect($sql);
- if(DBnum_rows($result) == 1)
+ if($graphid<=0) return;
+
+ $hosts = get_hosts_by_graphid($graphid);
+ if(!$hosts) return;
+ if(DBnum_rows($hosts)!=1) return;
+ $host=DBfetch($hosts);
+
+ $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$host["hostid"];
+ $templates=DBselect($sql);
+ while($template=DBfetch($templates))
{
- return DBfetch($result);
+ if($template["graphs"]&4 == 0) continue;
+
+ $graphs=get_graphs_by_hostid($template["hostid"]);
+ while($graph=DBfetch($graphs))
+ {
+ if(!cmp_graphs($graphid,$graph["graphid"])) continue;
+ if(!cmp_graph_by_item_key($graphid,$graph["graphid"])) continue;
+ delete_graph($graph["graphid"]);
+ $template_host=get_host_by_hostid($template["hostid"]);
+ info("Deleted graph '".$graph["name"]."' from linked host '".$template_host["host"]."'");
+ }
}
- else
+ }
+
+ function add_item_to_graph($graphid,$itemid,$color,$drawtype,$sortorder,$yaxisside)
+ {
+ $sql="insert into graphs_items (graphid,itemid,color,drawtype,sortorder,yaxisside) values ($graphid,$itemid,'$color',$drawtype,$sortorder,$yaxisside)";
+ $result=DBexecute($sql);
+ return DBinsert_id($result,"graphs_items","gitemid");
+ }
+
+ function update_graph_item($gitemid,$itemid,$color,$drawtype,$sortorder,$yaxisside)
+ {
+ $sql="update graphs_items set itemid=$itemid,color='$color',drawtype=$drawtype,sortorder=$sortorder,yaxisside=$yaxisside where gitemid=$gitemid";
+ return DBexecute($sql);
+ }
+
+ function delete_graphs_item($gitemid)
+ {
+ $sql="delete from graphs_items where gitemid=$gitemid";
+ return DBexecute($sql);
+ }
+
+ function move_up_graph_item($gitemid)
+ {
+ if($gitemid<=0) return;
+ $sql="update graphs_items set sortorder=sortorder-1 where sortorder>0 and gitemid=$gitemid";
+ return DBexecute($sql);
+
+ }
+
+ function move_down_graph_item($gitemid)
+ {
+ if($gitemid<=0) return;
+ $sql="update graphs_items set sortorder=sortorder+1 where sortorder<100 and gitemid=$gitemid";
+ return DBexecute($sql);
+
+ }
+
+ function copy_graphitems_for_host($src_graphid,$dist_graphid,$hostid)
+ {
+ $ret_code=0;
+ $src_graphitems=get_graphitems_by_graphid($src_graphid);
+ while($src_graphitem=DBfetch($src_graphitems))
{
- error("No graph with graphid=[$graphid]");
+ $src_item=get_item_by_itemid($src_graphitem["itemid"]);
+ $host_items=get_items_by_hostid($hostid);
+ $item_exist=0;
+ while($host_item=DBfetch($host_items))
+ {
+ if($src_item["key_"]==$host_item["key_"])
+ {
+ $item_exist=1;
+ $host_itemid=$host_item["itemid"];
+ break;
+ }
+ }
+ if($item_exist==0)
+ {
+ $ret_code|=1;
+ continue;
+ }
+ if(!add_item_to_graph($dist_graphid,$host_itemid,$src_graphitem["color"],$src_graphitem["drawtype"],$src_graphitem["sortorder"],$src_graphitem["yaxisside"]))
+ {
+ $ret_code|=2;
+ }
}
- return $result;
+ return $ret_code;
}
- function add_graph_item_to_linked_hosts($gitemid,$hostid=0)
+ function add_graph_item_to_templates($template_graphid,$template_itemid,$color,$drawtype,$sortorder,$yaxisside)
{
- if($gitemid<=0)
+ if($template_graphid<=0) return;
+ $template_hosts = get_hosts_by_graphid($template_graphid);
+ if(!$template_hosts) return;
+ $template_hosts_cnt=DBnum_rows($template_hosts);
+ if($template_hosts_cnt==0)
{
- return;
+ $template_host=get_host_by_itemid($template_itemid);
+ $template_hosts_cnt++;
}
+ else if($template_hosts_cnt==1)
+ {
+ $template_host=DBfetch($template_hosts);
+ $item_host=get_host_by_itemid($template_itemid);
+ if($template_host["hostid"]!=$item_host["hostid"])
+ $template_hosts_cnt++;
+ }
+ if($template_hosts_cnt!=1) return;
- $graph_item=get_graphitem_by_gitemid($gitemid);
- $graph=get_graph_by_graphid($graph_item["graphid"]);
- $item=get_item_by_itemid($graph_item["itemid"]);
+ $template_item=get_item_by_itemid($template_itemid);
- if($hostid==0)
+ $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$template_host["hostid"];
+ $hosts=DBselect($sql);
+ while($host=DBfetch($hosts))
{
- $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$item["hostid"];
+ if($host["graphs"]&2 == 0) continue;
+
+ $sql="select i.itemid from items i where i.key_='".$template_item["key_"]."' and i.hostid=".$host["hostid"];
+ $items=DBselect($sql);
+ if(DBnum_rows($items)==0) continue;
+ $item=DBfetch($items);
+
+ $find_graph=0;
+ $graphs=get_graphs_by_hostid($host["hostid"]);
+ while($graph=DBfetch($graphs))
+ {
+ if(!cmp_graphs($template_graphid,$graph["graphid"])) continue;
+ if(!cmp_graph_by_item_key($template_graphid,$graph["graphid"])) continue;
+ add_item_to_graph($graph["graphid"],$item["itemid"],$color,$drawtype,$sortorder,$yaxisside);
+ $host_info=get_host_by_hostid($host["hostid"]);
+ info("Added item to graph '".$graph["name"]."' from linked host '".$host_info["host"]."'");
+ remove_durpblicated_graphs($graph["graphid"]);
+ $find_graph=1;
+ }
+
+ if($find_graph==0)
+ {
+# duplicate graph for new host
+ $template_graph=get_graph_by_graphid($template_graphid);
+ $new_graphid=add_graph($template_graph["name"],$template_graph["width"],$template_graph["height"],$template_graph["yaxistype"],$template_graph["yaxismin"],$template_graph["yaxismax"]);
+ if(copy_graphitems_for_host($template_graphid,$new_graphid,$host["hostid"])!=0)
+ {
+ delete_graph($new_graphid);
+ }
+ else
+ {
+ add_item_to_graph($new_graphid,$item["itemid"],$color,$drawtype,$sortorder,$yaxisside);
+ $new_graph=get_graph_by_graphid($new_graphid);
+ $host_info=get_host_by_hostid($host["hostid"]);
+ info("Graph ".$new_graph["name"]." coped for linked host ".$host_info["host"]);
+ remove_durpblicated_graphs($new_graphid);
+ }
+ }
}
- else
+ }
+
+ function move_up_graph_item_from_templates($gitemid)
+ {
+ if($gitemid<=0) return;
+ $graph_item=get_graphitem_by_gitemid($gitemid);
+ $graph=get_graph_by_graphid($graph_item["graphid"]);
+ $item=get_item_by_itemid($graph_item["itemid"]);
+
+ $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$item["hostid"];
+ $result=DBselect($sql);
+ while($row=DBfetch($result))
{
- $sql="select hostid,templateid,graphs from hosts_templates where hostid=$hostid and templateid=".$item["hostid"];
+ if($row["graphs"]&2 == 0) continue;
+
+ $sql="select i.itemid from items i where i.key_='".$item["key_"]."' and i.hostid=".$row["hostid"];
+ $result2=DBselect($sql);
+ if(DBnum_rows($result2)==0) continue;
+ $row2=DBfetch($result2);
+
+ $sql="select distinct gi.gitemid,gi.graphid from graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=".$row["hostid"]." and i.itemid=".$row2["itemid"]." and gi.drawtype=".$graph_item["drawtype"]." and gi.sortorder=".$graph_item["sortorder"]." and gi.color='".$graph_item["color"]."' and gi.yaxisside= ".$graph_item["yaxisside"];
+ $result3=DBselect($sql);
+ if(DBnum_rows($result3)==0) continue;
+ $row3=DBfetch($result3);
+
+ $graph2=get_graph_by_graphid($row3["graphid"]);
+ if(!cmp_graphs($graph["graphid"],$graph2["graphid"])) continue;
+ if(!cmp_graph_by_item_key($graph["graphid"],$graph2["graphid"]))continue;
+
+ move_up_graph_item($row3["gitemid"]);
+ $host=get_host_by_hostid($row["hostid"]);
+ info("Updated graph element ".$item["key_"]." from linked host ".$host["host"]);
}
- $result=DBselect($sql);
- while($row=DBfetch($result))
+ }
+
+ function move_down_graph_item_from_templates($gitemid)
+ {
+ if($gitemid<=0) return;
+ $graph_item=get_graphitem_by_gitemid($gitemid);
+ $graph=get_graph_by_graphid($graph_item["graphid"]);
+ $item=get_item_by_itemid($graph_item["itemid"]);
+
+ $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$item["hostid"];
+ $result=DBselect($sql);
+ while($row=DBfetch($result))
{
- if($row["graphs"]&1 == 0) continue;
+ if($row["graphs"]&2 == 0) continue;
$sql="select i.itemid from items i where i.key_='".$item["key_"]."' and i.hostid=".$row["hostid"];
$result2=DBselect($sql);
if(DBnum_rows($result2)==0) continue;
$row2=DBfetch($result2);
- $itemid=$row2["itemid"];
- $sql="select distinct g.graphid from graphs g,graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=".$row["hostid"]." and g.graphid=gi.graphid and g.name='".addslashes($graph["name"])."'";
- $result2=DBselect($sql);
+ $sql="select distinct gi.gitemid,gi.graphid from graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=".$row["hostid"]." and i.itemid=".$row2["itemid"]." and gi.drawtype=".$graph_item["drawtype"]." and gi.sortorder=".$graph_item["sortorder"]." and gi.color='".$graph_item["color"]."' and gi.yaxisside= ".$graph_item["yaxisside"];
+ $result3=DBselect($sql);
+ if(DBnum_rows($result3)==0) continue;
+ $row3=DBfetch($result3);
+
+ $graph2=get_graph_by_graphid($row3["graphid"]);
+ if(!cmp_graphs($graph["graphid"],$graph2["graphid"])) continue;
+ if(!cmp_graph_by_item_key($graph["graphid"],$graph2["graphid"]))continue;
+
+ move_down_graph_item($row3["gitemid"]);
$host=get_host_by_hostid($row["hostid"]);
- while($row2=DBfetch($result2))
- {
- add_item_to_graph($row2["graphid"],$itemid,$graph_item["color"],$graph_item["drawtype"],$graph_item["sortorder"],$graph_item["taxisside"]);
- info("Added graph element to graph ".$graph["name"]." of linked host ".$host["host"]);
- }
- if(DBnum_rows($result2)==0)
- {
- $graphid=add_graph($graph["name"],$graph["width"],$graph["height"],$graph["yaxistype"],$graph["yaxismin"],$graph["yaxismax"]);
- info("Added graph ".$graph["name"]." of linked host ".$host["host"]);
- add_item_to_graph($graphid,$itemid,$graph_item["color"],$graph_item["drawtype"],$graph_item["sortorder"],$graph_item["yaxisside"]);
- info("Added graph element to graph ".$graph["name"]." of linked host ".$host["host"]);
- }
+ info("Updated graph element ".$item["key_"]." from linked host ".$host["host"]);
+
}
}
- function delete_graph_item_from_templates($gitemid)
+ function update_graph_item_from_templates($gitemid,$itemid,$color,$drawtype,$sortorder,$yaxisside)
{
- if($gitemid<=0)
+ if($gitemid<=0) return;
+ $graph_item=get_graphitem_by_gitemid($gitemid);
+ $graph=get_graph_by_graphid($graph_item["graphid"]);
+ $item=get_item_by_itemid($graph_item["itemid"]);
+
+ $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$item["hostid"];
+ $result=DBselect($sql);
+ while($row=DBfetch($result))
{
- return;
+ if($row["graphs"]&2 == 0) continue;
+
+ $sql="select i.itemid from items i where i.key_='".$item["key_"]."' and i.hostid=".$row["hostid"];
+ $result2=DBselect($sql);
+ if(DBnum_rows($result2)==0) continue;
+ $row2=DBfetch($result2);
+
+ $sql="select distinct gi.gitemid,gi.graphid from graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=".$row["hostid"]." and i.itemid=".$row2["itemid"]." and gi.drawtype=".$graph_item["drawtype"]." and gi.sortorder=".$graph_item["sortorder"]." and gi.color='".$graph_item["color"]."' and gi.yaxisside= ".$graph_item["yaxisside"];
+ $result3=DBselect($sql);
+ if(DBnum_rows($result3)==0) continue;
+ $row3=DBfetch($result3);
+
+ $graph2=get_graph_by_graphid($row3["graphid"]);
+ if(!cmp_graphs($graph["graphid"],$graph2["graphid"])) continue;
+ if(!cmp_graph_by_item_key($graph["graphid"],$graph2["graphid"]))continue;
+
+ update_graph_item($row3["gitemid"],$row2["itemid"],$color,$drawtype,$sortorder,$yaxisside);
+ $host=get_host_by_hostid($row["hostid"]);
+ info("Updated graph element ".$item["key_"]." from linked host ".$host["host"]);
+
}
+ }
+
+ function delete_graph_item_from_templates($gitemid)
+ {
+ if($gitemid<=0) return;
+
$graph_item=get_graphitem_by_gitemid($gitemid);
$graph=get_graph_by_graphid($graph_item["graphid"]);
$item=get_item_by_itemid($graph_item["itemid"]);
@@ -174,7 +407,7 @@
$result=DBselect($sql);
while($row=DBfetch($result))
{
- if($row["graphs"]&4 == 0) continue;
+ if($row["graphs"]&2 == 0) continue;
$sql="select i.itemid from items i where i.key_='".$item["key_"]."' and i.hostid=".$row["hostid"];
$result2=DBselect($sql);
@@ -182,22 +415,27 @@
$row2=DBfetch($result2);
$itemid=$row2["itemid"];
- $host=get_host_by_hostid($result["hostid"]);
+ $sql="select distinct gi.gitemid,gi.graphid from graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=".$row["hostid"]." and i.itemid=".$row2["itemid"]." and gi.drawtype=".$graph_item["drawtype"]." and gi.sortorder=".$graph_item["sortorder"]." and gi.color='".$graph_item["color"]."' and gi.yaxisside= ".$graph_item["yaxisside"];
+ $result3=DBselect($sql);
+ if(DBnum_rows($result3)==0) continue;
+ $row3=DBfetch($result3);
- $sql="select distinct gi.gitemid,gi.graphid from graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=".$row["hostid"]." and i.itemid=$itemid";
- $result2=DBselect($sql);
- while($row2=DBfetch($result2))
+ $graph2=get_graph_by_graphid($row3["graphid"]);
+ if(!cmp_graphs($graph["graphid"],$graph2["graphid"])) continue;
+ if(!cmp_graph_by_item_key($graph["graphid"],$graph2["graphid"]))continue;
+
+ delete_graphs_item($row3["gitemid"]);
+
+ $host=get_host_by_hostid($row["hostid"]);
+ info("Deleted graph element ".$item["key_"]." from linked host ".$host["host"]);
+
+ $sql="select count(*) as count from graphs_items where graphid=".$row3["graphid"];
+ $result4=DBselect($sql);
+ $row4=DBfetch($result4);
+ if($row4["count"]==0)
{
- delete_graphs_item($row2["gitemid"]);
- info("Deleted graph element ".$item["key_"]." from linked host ".$host["host"]);
- $sql="select count(*) as count from graphs_items where graphid=".$row2["graphid"];
- $result3=DBselect($sql);
- $row3=DBfetch($result3);
- if($row3["count"]==0)
- {
- delete_graph($row2["graphid"]);
- info("Deleted graph from linked host ".$host["host"]);
- }
+ delete_graph($row3["graphid"]);
+ info("Deleted graph from linked host ".$host["host"]);
}
}
}
@@ -453,4 +691,85 @@
echo "</TR>";
echo "</TABLE>";
}
+
+ function cmp_graphs($graphid1, $graphid2)
+ {
+
+ $graph1 = get_graph_by_graphid($graphid1);
+ if($graph1==FALSE) return FALSE;
+
+ $graph2 =get_graph_by_graphid($graphid2);
+ if($graph2==FALSE) return FALSE;
+
+ if($graph1["name"] !=$graph2["name"]) return FALSE;
+ if($graph1["width"] !=$graph2["width"]) return FALSE;
+ if($graph1["height"] !=$graph2["height"]) return FALSE;
+ if($graph1["yaxistype"] !=$graph2["yaxistype"]) return FALSE;
+ if($graph1["yaxismin"] !=$graph2["yaxismin"]) return FALSE;
+ if($graph1["yaxismax"] !=$graph2["yaxismax"]) return FALSE;
+
+ return TRUE;
+ }
+
+ function cmp_graph_by_graphs_items($graphid1, $graphid2)
+ {
+ $graph_items1 = get_graphitems_by_graphid($graphid1);
+ $graph_items2 = get_graphitems_by_graphid($graphid2);
+ if(DBnum_rows($graph_items1) != DBnum_rows($graph_items2)) return FALSE;
+
+ while(($graph_item1=DBfetch($graph_items1)) && ($graph_item2=DBfetch($graph_items2)))
+ {
+ if($graph_item1["itemid"] != $graph_item2["itemid"]) return FALSE;
+ if($graph_item1["drawtype"] != $graph_item2["drawtype"]) return FALSE;
+ if($graph_item1["sortorder"] != $graph_item2["sortorder"]) return FALSE;
+ if($graph_item1["color"] != $graph_item2["color"]) return FALSE;
+ if($graph_item1["yaxisside"] != $graph_item2["yaxisside"]) return FALSE;
+ }
+
+ return TRUE;
+ }
+
+ function cmp_graph_by_item_key($graphid1, $graphid2)
+ {
+ $graph_items1 = get_graphitems_by_graphid($graphid1);
+ $graph_items2 = get_graphitems_by_graphid($graphid2);
+ if(DBnum_rows($graph_items1) != DBnum_rows($graph_items2)) return FALSE;
+
+ while(($graph_item1=DBfetch($graph_items1)) && ($graph_item2=DBfetch($graph_items2)))
+ {
+ $item1 = get_item_by_itemid($graph_item1["itemid"]);
+ $item2 = get_item_by_itemid($graph_item2["itemid"]);
+ if($item1["key_"] != $item2["key_"]) return FALSE;
+ }
+
+ return TRUE;
+ }
+
+ function remove_durpblicated_graphs($graphid=0)
+ {
+ $sql="select graphid from graphs";
+ if($graphid!=0)
+ {
+ $sql.=" where graphid=$graphid";
+ }
+ $graphs = DBselect($sql);
+ if(DBnum_rows($graphs) == 0) return;
+
+ while($graphid=DBfetch($graphs))
+ {
+ $sql="select graphid from graphs";
+ $all_graphs = DBselect($sql);
+ if(DBnum_rows($all_graphs) == 0) return;
+ while($all_graphid=DBfetch($all_graphs))
+ {
+ if($graphid["graphid"] == $all_graphid["graphid"]) continue;
+ if(cmp_graphs($graphid["graphid"],$all_graphid["graphid"]) == FALSE) continue;
+ if(cmp_graph_by_graphs_items($graphid["graphid"],$all_graphid["graphid"]) == FALSE) continue;
+ $graph=get_graph_by_graphid($graphid["graphid"]);
+ delete_graph($graphid["graphid"]);
+ info("Deleted duplicated graph with name '".$graph["name"]."'");
+ }
+ }
+ }
+
?>
diff --git a/frontends/php/include/hosts.inc.php b/frontends/php/include/hosts.inc.php
index b215715f..8e268a83 100644
--- a/frontends/php/include/hosts.inc.php
+++ b/frontends/php/include/hosts.inc.php
@@ -188,19 +188,28 @@
return DBexecute($sql);
}
- function get_host_by_hostid($hostid)
+ function get_host_by_itemid($itemid)
{
- $sql="select * from hosts where hostid=$hostid";
+ $sql="select h.* from hosts h, items i where i.hostid=h.hostid and i.itemid=$itemid";
$result=DBselect($sql);
if(DBnum_rows($result) == 1)
{
return DBfetch($result);
}
- else
+ error("No host with itemid=[$itemid]");
+ return FALSE;
+ }
+
+ function get_host_by_hostid($hostid)
+ {
+ $sql="select * from hosts where hostid=$hostid";
+ $result=DBselect($sql);
+ if(DBnum_rows($result) == 1)
{
- error("No host with hostid=[$hostid]");
+ return DBfetch($result);
}
- return $host;
+ error("No host with hostid=[$hostid]");
+ return FALSE;
}
# Update Host status
diff --git a/frontends/php/include/items.inc.php b/frontends/php/include/items.inc.php
index 1db16857..b2e02deb 100644
--- a/frontends/php/include/items.inc.php
+++ b/frontends/php/include/items.inc.php
@@ -310,6 +310,18 @@
return DBexecute($sql);
}
+ function get_items_by_hostid($hostid)
+ {
+ $sql="select * from items where hostid=$hostid";
+ $result=DBselect($sql);
+ if(DBnum_rows($result) != 0)
+ {
+ return $result;
+ }
+ error("No items for hostid=[$hostid]");
+ return FALSE;
+ }
+
function get_item_by_itemid($itemid)
{
$sql="select * from items where itemid=$itemid";
@@ -318,11 +330,8 @@
{
return DBfetch($result);
}
- else
- {
- error("No item with itemid=[$itemid]");
- }
- return $item;
+ error("No item with itemid=[$itemid]");
+ return FALSE;
}
# Delete Item definition