diff options
| author | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2006-02-17 14:16:25 +0000 |
|---|---|---|
| committer | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2006-02-17 14:16:25 +0000 |
| commit | eeb19c96867c981d71e977813e0536f6e90f1adc (patch) | |
| tree | c128a1dc44937620541c426d37b99ab1f0f0a38d /frontends/php/include/graphs.inc.php | |
| parent | 42a2ee7629091fc45c463d28cb560c8bdd15d960 (diff) | |
| download | zabbix-eeb19c96867c981d71e977813e0536f6e90f1adc.tar.gz zabbix-eeb19c96867c981d71e977813e0536f6e90f1adc.tar.xz zabbix-eeb19c96867c981d71e977813e0536f6e90f1adc.zip | |
- improved templates logic for graphs (Eugene)
- improved templates logic for triggers (Eugene)
git-svn-id: svn://svn.zabbix.com/trunk@2650 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'frontends/php/include/graphs.inc.php')
| -rw-r--r-- | frontends/php/include/graphs.inc.php | 767 |
1 files changed, 329 insertions, 438 deletions
diff --git a/frontends/php/include/graphs.inc.php b/frontends/php/include/graphs.inc.php index 709b37ab..fffeee25 100644 --- a/frontends/php/include/graphs.inc.php +++ b/frontends/php/include/graphs.inc.php @@ -19,31 +19,44 @@ **/ ?> <?php + function get_graph_by_gitemid($gitemid) + { + $db_graphs = DBselect("select distinct g.* from graphs g, graphs_items gi". + " where g.graphid=gi.graphid and gi.gitemid=$gitemid"); + return DBfetch($db_graphs); + + } + 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; + return DBselect("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"); + } + + function get_realhosts_by_graphid($graphid) + { + $graph = get_graph_by_graphid($graphid); + if($graph["templateid"] != 0) + return get_realhosts_by_graphid($graph["templateid"]); + + return get_hosts_by_graphid($graphid); } 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; + return DBselect("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"); } 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; + return DBselect("select * from graphs_items where graphid=$graphid". + " order by itemid,drawtype,sortorder,color,yaxisside"); } function get_graphitem_by_gitemid($gitemid) { - $sql="select * from graphs_items where gitemid=$gitemid"; - $result=DBselect($sql); + $result=DBselect("select * from graphs_items where gitemid=$gitemid"); if(DBnum_rows($result) == 1) { return DBfetch($result); @@ -54,8 +67,8 @@ function get_graph_by_graphid($graphid) { - $sql="select * from graphs where graphid=$graphid"; - $result=DBselect($sql); + + $result=DBselect("select * from graphs where graphid=$graphid"); if(DBnum_rows($result) == 1) { return DBfetch($result); @@ -64,9 +77,14 @@ return $result; } + function get_graphs_by_templateid($templateid) + { + return DBselect("select * from graphs where templateid=$templateid"); + } + # Add Graph - function add_graph($name,$width,$height,$yaxistype,$yaxismin,$yaxismax) + function add_graph($name,$width,$height,$yaxistype,$yaxismin,$yaxismax,$templateid=0) { if(!check_right("Graph","A",0)) { @@ -74,390 +92,391 @@ return 0; } - $sql="insert into graphs (name,width,height,yaxistype,yaxismin,yaxismax) values (".zbx_dbstr($name).",$width,$height,$yaxistype,$yaxismin,$yaxismax)"; - $result=DBexecute($sql); - return DBinsert_id($result,"graphs","graphid"); + $result=DBexecute("insert into graphs". + " (name,width,height,yaxistype,yaxismin,yaxismax,templateid)". + " values (".zbx_dbstr($name).",$width,$height,$yaxistype,$yaxismin,". + " $yaxismax,$templateid)"); + $graphid = DBinsert_id($result,"graphs","graphid"); + if($graphid) + { + info("Graph '$name' added"); + } + return $graphid; } # Update Graph - function update_graph($graphid,$name,$width,$height,$yaxistype,$yaxismin,$yaxismax) + function update_graph($graphid,$name,$width,$height,$yaxistype,$yaxismin,$yaxismax,$templateid=0) { if(!check_right("Graph","U",0)) { error("Insufficient permissions"); return 0; } - $sql="update graphs set name=".zbx_dbstr($name).",width=$width,height=$height,yaxistype=$yaxistype,yaxismin=$yaxismin,yaxismax=$yaxismax where graphid=$graphid"; - return DBexecute($sql); + + $g_graph = get_graph_by_graphid($graphid); + + $graphs = get_graphs_by_templateid($graphid); + while($graph = DBfetch($graphs)) + { + $result = update_graph($graph["graphid"],$name,$width, + $height,$yaxistype,$yaxismin,$yaxismax,$graphid); + if(!$result) + return $result; + } + + $result = DBexecute("update graphs set name=".zbx_dbstr($name).",width=$width,height=$height,". + "yaxistype=$yaxistype,yaxismin=$yaxismin,yaxismax=$yaxismax,templateid=$templateid". + " where graphid=$graphid"); + if($result) + { + info("Graph '".$g_graph["name"]."' updated"); + } + return $result; } # Delete Graph function delete_graph($graphid) { - $sql="delete from graphs_items where graphid=$graphid"; - $result=DBexecute($sql); - if(!$result) + if(!check_right("Graph","U",0)) { - return $result; + error("Insufficient permissions"); + return 0; } - $sql="delete from graphs where graphid=$graphid"; - return DBexecute($sql); - } - function update_graph_from_templates($graphid,$name,$width,$height,$yaxistype,$yaxismin,$yaxismax) - { - if($graphid<=0) return; - - $hosts = get_hosts_by_graphid($graphid); - if(!$hosts) return; - if(DBnum_rows($hosts)!=1) return; - $host=DBfetch($hosts); + $graph = get_graph_by_graphid($graphid); - $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$host["hostid"]; - $templates=DBselect($sql); - while($template=DBfetch($templates)) - { - 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"]."'"); - } + $chd_graphs = get_graphs_by_templateid($graphid); + while($chd_graph = DBfetch($chd_graphs)) + {// recursion + $result = delete_graph($chd_graph["graphid"]); + if(!$result) + return $result; } - } - - function delete_graph_from_templates($graphid) - { - 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)) - { - if($template["graphs"]&4 == 0) continue; + // delete graph + $result=DBexecute("delete from graphs_items where graphid=$graphid"); + if(!$result) + return $result; - $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"]."'"); - } + $result = DBexecute("delete from graphs where graphid=$graphid"); + if($result) + { + info("Graph '".$graph["name"]."' deleted"); } - } - - 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,".zbx_dbstr($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=".zbx_dbstr($color).",drawtype=$drawtype,sortorder=$sortorder,yaxisside=$yaxisside where gitemid=$gitemid"; - return DBexecute($sql); + return $result; } - function delete_graphs_item($gitemid) + function cmp_graphitems(&$gitem1, &$gitem2) { - $sql="delete from graphs_items where gitemid=$gitemid"; - return DBexecute($sql); - } + if($gitem1["drawtype"] != $gitem2["drawtype"]) return 1; + if($gitem1["sortorder"] != $gitem2["sortorder"]) return 2; + if($gitem1["color"] != $gitem2["color"]) return 3; + if($gitem1["yaxisside"] != $gitem2["yaxisside"]) return 4; - 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); + $item1 = get_item_by_itemid($gitem1["itemid"]); + $item2 = get_item_by_itemid($gitem2["itemid"]); - } - - 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); + if($item1["key_"] != $item2["key_"]) return 5; + return 0; } - function copy_graphitems_for_host($src_graphid,$dist_graphid,$hostid) + function add_item_to_graph($graphid,$itemid,$color,$drawtype,$sortorder,$yaxisside) { - $ret_code=0; - $src_graphitems=get_graphitems_by_graphid($src_graphid); - while($src_graphitem=DBfetch($src_graphitems)) - { - $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_"]) + $result = DBexecute("insert into graphs_items". + " (graphid,itemid,color,drawtype,sortorder,yaxisside)". + " values ($graphid,$itemid,".zbx_dbstr($color).",$drawtype,$sortorder,$yaxisside)"); + + $gitemid = DBinsert_id($result,"graphs_items","gitemid"); + + $item = get_item_by_itemid($itemid); + $graph = get_graph_by_graphid($graphid); + + $host = get_host_by_itemid($itemid); + if($gitemid && $host["status"]==HOST_STATUS_TEMPLATE) + {// add to child graphs + $gitems = get_graphitems_by_graphid($graphid); + if(DBnum_rows($gitems)==1) + {// create graphs for childs with item + $chd_hosts = get_hosts_by_templateid($host["hostid"]); + while($chd_host = DBfetch($chd_hosts)) { - $item_exist=1; - $host_itemid=$host_item["itemid"]; - break; + $new_graphid = add_graph($graph["name"],$graph["width"],$graph["height"], + $graph["yaxistype"],$graph["yaxismin"],$graph["yaxismax"], + $graph["graphid"]); + + if(!$new_graphid) + { + $result = $new_graphid; + break; + } + $db_items = DBselect("select itemid from items". + " where key_=".$item["key_"]. + " and hostid=".$chd_host["hostid"]); + if(DBnum_rows($db_items)==0) + { + $result = FALSE; + break; + } + $db_item = DBfetch($db_items); + // recursion + $result = add_item_to_graph($new_graphid,$db_item["itemid"], + $color,$drawtype,$sortorder,$yaxisside); + + if(!$result) + break; + } } - if($item_exist==0) - { - $ret_code|=1; - continue; + else + {// copy items to childs + $childs = get_graphs_by_templateid($graphid); + while($child = DBfetch($childs)) + { + $chd_hosts = get_hosts_by_graphid($child["graphid"]); + $chd_host = DBfetch($chd_hosts); + $db_items = DBselect("select itemid from items". + " where key_=".$item["key_"]. + " and hostid=".$chd_host["hostid"]); + if(DBnum_rows($db_items)==0) + { + $result = FALSE; + break; + } + $db_item = DBfetch($db_items); + // recursion + $result = add_item_to_graph($child["graphid"],$db_item["itemid"], + $color,$drawtype,$sortorder,$yaxisside); + if(!$result) + break; + } + } - if(!add_item_to_graph($dist_graphid,$host_itemid,$src_graphitem["color"],$src_graphitem["drawtype"],$src_graphitem["sortorder"],$src_graphitem["yaxisside"])) - { - $ret_code|=2; + if(!$result && $graph["templateid"]==0) + {// remove only main graph item + delete_graph_item($gitemid); + return $result; } } - return $ret_code; - } + if($result) + { + info("Added Item '".$item["description"]."' for graph '".$graph["name"]."'"); + } - function add_graph_item_to_templates( - $template_graphid,$template_itemid, - $color,$drawtype,$sortorder,$yaxisside) + return $gitemid; + } + + function update_graph_item($gitemid,$itemid,$color,$drawtype,$sortorder,$yaxisside) { - if($template_graphid<=0) - return; + $gitem = get_graphitem_by_gitemid($gitemid); -// get host count by graph - $template_hosts = get_hosts_by_graphid($template_graphid); + $item = get_item_by_itemid($itemid); + $graph = get_graph_by_gitemid($gitemid); - if(!$template_hosts) - return; + $childs = get_graphs_by_templateid($graph["graphid"]); + while($child = DBfetch($childs)) + { + $chd_hosts = get_hosts_by_graphid($child["graphid"]); + $chd_host = DBfetch($chd_hosts); + $db_items = DBselect("select itemid from items". + " where key_=".$item["key_"]. + " and hostid=".$chd_host["hostid"]); + if(DBnum_rows($db_items)==0) + return FALSE; + $db_item = DBfetch($db_items); - $template_hosts_cnt=DBnum_rows($template_hosts); + $chd_gitems = get_graphitems_by_graphid($child["graphid"]); + while($chd_gitem = DBfetch($chd_gitems)) + { + if(cmp_graphitems($gitem, $chd_gitem)) continue; - if($template_hosts_cnt==0) - { - $template_host=get_host_by_itemid($template_itemid); - $template_hosts_cnt++; + // recursion + $result = update_graph_item($chd_gitem["gitemid"],$db_item["itemid"], + $color,$drawtype,$sortorder,$yaxisside); + if(!$result) + return $reslut; + break; + } } - else if($template_hosts_cnt==1) + $result = DBexecute("update graphs_items set itemid=$itemid,color=".zbx_dbstr($color).",". + "drawtype=$drawtype,sortorder=$sortorder,yaxisside=$yaxisside where gitemid=$gitemid"); + if($result) { - $template_host=DBfetch($template_hosts); - $item_host=get_host_by_itemid($template_itemid); - if($template_host["hostid"]!=$item_host["hostid"]) - $template_hosts_cnt++; + $host = get_host_by_itemid($item["itemid"]); + info("Graph item '".$host["host"].": ".$item["description"]. + " for graph '".$graph["name"]."' updated"); } - if($template_hosts_cnt!=1) return; -// end host counting + return $result; + } - $template_item=get_item_by_itemid($template_itemid); + function delete_graph_item($gitemid) + { + + $gitem = get_graphitem_by_gitemid($gitemid); - $hosts=DBselect("select hostid,templateid,graphs from hosts_templates". - " where templateid=".$template_host["hostid"]); - while($host=DBfetch($hosts)) + $graph = get_graph_by_gitemid($gitemid); + $childs = get_graphs_by_templateid($graph["graphid"]); + while($child = DBfetch($childs)) { - if($host["graphs"]&2 == 0) continue; - - $items=DBselect("select i.itemid from items i". - " where i.key_=".zbx_dbstr($template_item["key_"]). - " and i.hostid=".$host["hostid"]); - if(DBnum_rows($items)==0) continue; - $item=DBfetch($items); - - $find_graph=0; - $graphs=get_graphs_by_hostid($host["hostid"]); - while($graph=DBfetch($graphs)) + $chd_gitems = get_graphitems_by_graphid($child["graphid"]); + while($chd_gitem = DBfetch($chd_gitems)) { - 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"]."'"); + if(cmp_graphitems($gitem, $chd_gitem)) continue; - remove_duplicated_graphs($graph["graphid"]); - $find_graph=1; + // recursion + $result = delete_graph_item($chd_gitem["gitemid"]); + if(!$result) + return $reslut; + break; } + } - 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"]); + $result = DBexecute("delete from graphs_items where gitemid=$gitemid"); + if($result) + { + $item = get_item_by_itemid($gitem["itemid"]); + info("Item '".$item["description"]."' deleted from graph '".$graph["name"]."'"); - 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_duplicated_graphs($new_graphid); - } + $graph_items = get_graphitems_by_graphid($graph["graphid"]); + if($graph["templateid"]>0 && DBnum_rows($graph_items) < 1) + { + return delete_graph($graph["graphid"]); } } + return $result; } - function move_up_graph_item_from_templates($gitemid) + function move_up_graph_item($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"]&2 == 0) continue; - - $sql="select i.itemid from items i where i.key_=".zbx_dbstr($item["key_"])." and i.hostid=".$row["hostid"]; - $result2=DBselect($sql); - if(DBnum_rows($result2)==0) continue; - $row2=DBfetch($result2); + $gitem = get_graphitem_by_gitemid($gitemid); - $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=".zbx_dbstr($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; + $graph = get_graph_by_gitemid($gitemid); + $childs = get_graphs_by_templateid($graph["graphid"]); + while($child = DBfetch($childs)) + { + $chd_gitems = get_graphitems_by_graphid($child["graphid"]); + while($chd_gitem = DBfetch($chd_gitems)) + { + if(cmp_graphitems($gitem, $chd_gitem)) 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"]); + // recursion + $result = move_up_graph_item($chd_gitem["gitemid"]); + if(!$result) + return $reslut; + break; + } } - } + $result = DBexecute("update graphs_items set sortorder=sortorder-1". + " where sortorder>0 and gitemid=$gitemid"); + if($result) + { + $item = get_item_by_itemid($gitem["itemid"]); + info("Sort order updated for item '".$item["description"]."'". + " in graph '".$graph["name"]."'"); + } + return $result; - function move_down_graph_item_from_templates($gitemid) + } + + function move_down_graph_item($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"]&2 == 0) continue; - $sql="select i.itemid from items i where i.key_=".zbx_dbstr($item["key_"])." and i.hostid=".$row["hostid"]; - $result2=DBselect($sql); - if(DBnum_rows($result2)==0) continue; - $row2=DBfetch($result2); + $gitem = get_graphitem_by_gitemid($gitemid); - $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=".zbx_dbstr($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; + $graph = get_graph_by_gitemid($gitemid); + $childs = get_graphs_by_templateid($graph["graphid"]); + while($child = DBfetch($childs)) + { + $chd_gitems = get_graphitems_by_graphid($child["graphid"]); + while($chd_gitem = DBfetch($chd_gitems)) + { + if(cmp_graphitems($gitem, $chd_gitem)) continue; - move_down_graph_item($row3["gitemid"]); - $host=get_host_by_hostid($row["hostid"]); - info("Updated graph element ".$item["key_"]." from linked host ".$host["host"]); - + // recursion + $result = move_down_graph_item($chd_gitem["gitemid"]); + if(!$result) + return $reslut; + break; + } } + $result = DBexecute("update graphs_items set sortorder=sortorder+1". + " where sortorder<100 and gitemid=$gitemid"); + if($result) + { + $item = get_item_by_itemid($gitem["itemid"]); + info("Sort order updated for item '".$item["description"]."'". + " in graph '".$graph["name"]."'"); + } + return $result; } - function update_graph_item_from_templates($gitemid,$itemid,$color,$drawtype,$sortorder,$yaxisside) + function delete_template_graphs_by_hostid($hostid) { - 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)) + $db_graphs = get_graphs_by_hostid($hostid); + while($db_graph = DBfetch($db_graphs)) { - if($row["graphs"]&2 == 0) continue; - - $sql="select i.itemid from items i where i.key_=".zbx_dbstr($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=".zbx_dbstr($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"]); - + if($db_graph["templateid"] == 0) continue; + delete_graph($db_graph["graphid"]); } - } - - function delete_graph_item_from_templates($gitemid) + + function sync_graphs_with_templates($hostid) { - if($gitemid<=0) return; + $host = get_host_by_hostid($hostid); + $db_graphs = get_graphs_by_hostid($host["templateid"]); + while($db_graph = DBfetch($db_graphs)) + { + copy_graph_to_host($db_graph["graphid"], $hostid); + } + } - $graph_item=get_graphitem_by_gitemid($gitemid); - $graph=get_graph_by_graphid($graph_item["graphid"]); - $item=get_item_by_itemid($graph_item["itemid"]); + function copy_graph_to_host($graphid, $hostid) + { + $db_graph = get_graph_by_graphid($graphid); + $new_graphid = add_graph($db_graph["name"],$db_graph["width"],$db_graph["height"], + $db_graph["yaxistype"],$db_graph["yaxismin"],$db_graph["yaxismax"],$graphid); + if(!$new_graphid) + return $new_graphid; - $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$item["hostid"]; - $result=DBselect($sql); - while($row=DBfetch($result)) + $result = copy_graphitems_for_host($graphid, $new_graphid, $hostid); + if(!$result) { - if($row["graphs"]&2 == 0) continue; + delete_graph($graphid); + } + return $result; + } - $sql="select i.itemid from items i where i.key_=".zbx_dbstr($item["key_"])." and i.hostid=".$row["hostid"]; - $result2=DBselect($sql); - if(DBnum_rows($result2)==0) continue; - $row2=DBfetch($result2); - $itemid=$row2["itemid"]; + function copy_graphitems_for_host($src_graphid,$dist_graphid,$hostid) + { + $src_graphitems=get_graphitems_by_graphid($src_graphid); + while($src_graphitem=DBfetch($src_graphitems)) + { + $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_"]) continue; - $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=".zbx_dbstr($graph_item["color"])." and gi.yaxisside= ".$graph_item["yaxisside"]; - $result3=DBselect($sql); - if(DBnum_rows($result3)==0) continue; - $row3=DBfetch($result3); + $item_exist=1; + $host_itemid=$host_item["itemid"]; - $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_graph($row3["graphid"]); - info("Deleted graph from linked host ".$host["host"]); + $result = add_item_to_graph($dist_graphid,$host_itemid,$src_graphitem["color"], + $src_graphitem["drawtype"],$src_graphitem["sortorder"], + $src_graphitem["yaxisside"]); + if(!$result) + return $result; + break; } + if($item_exist==0) + return FALSE; } + return TRUE; } function navigation_bar_calc() @@ -711,132 +730,4 @@ 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_duplicated_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"]."'"); - } - } - } - - function add_graph_item_to_linked_hosts($gitemid,$hostid=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"]); - - if($hostid==0) - { - $sql="select hostid,templateid,graphs from hosts_templates where templateid=".$item["hostid"]; - } - else - { - $sql="select hostid,templateid,graphs from hosts_templates where hostid=$hostid and templateid=".$item["hostid"]; - } - $result=DBselect($sql); - while($row=DBfetch($result)) - { - if($row["graphs"]&1 == 0) continue; - - $sql="select i.itemid from items i where i.key_=".zbx_dbstr($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=".zbx_dbstr($graph["name"]); - $result2=DBselect($sql); - $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["yaxisside"]); - 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"]); - } - } - } ?> |
