$groupid"); if(DBnum_rows($result)>0) { error("Group '$name' already exists"); return FALSE; } if($groupid==NULL) return DBexecute("insert into groups (name) values (".zbx_dbstr($name).")"); else return DBexecute("update groups set name=".zbx_dbstr($name)." where groupid=$groupid"); } function add_group_to_host($hostid,$newgroup="") { if($newgroup == "" || $newgroup == NULL) return TRUE; $result = db_save_group($newgroup); if(!$result) return $result; $groupid = DBinsert_id($result,"groupd","groupid"); return add_host_to_group($hostid, $groupid); } function update_host_groups_by_groupid($groupid,$hosts=array()) { DBexecute("delete from hosts_groups where groupid=$groupid"); foreach($hosts as $hostid) { add_host_to_group($hostid, $groupid); } } function update_host_groups($hostid,$groups=array()) { DBexecute("delete from hosts_groups where hostid=$hostid"); foreach($groups as $groupid) { add_host_to_group($hostid, $groupid); } } function add_host_group($name,$hosts=array()) { // if(!check_right("Host","A",0)) // { // error("Insufficient permissions"); // return FLASE; // } $result = db_save_group($name); if(!$result) return $result; $groupid = DBinsert_id($result,"groups","groupid"); update_host_groups_by_groupid($groupid,$hosts); return $groupid; } function update_host_group($groupid,$name,$hosts) { // if(!check_right("Host","U",0)) // { // error("Insufficient permissions"); // return 0; // } $result = db_save_group($name,$groupid); if(!$result) return $result; update_host_groups_by_groupid($groupid,$hosts); return $result; } /* HOST finction */ function check_circle_host_link($hostid, $templateid) { if($templateid <= 0) return FALSE; if($hostid == $templateid) return TRUE; $template = get_host_by_hostid($templateid); if($template["templateid"] > 0) return check_circle_host_link($hostid, $template["templateid"]); return FALSE; } function db_save_host($host,$port,$status,$useip,$ip,$templateid,$hostid=NULL) { if (!eregi('^([0-9a-zA-Z\_\.-]+)$', $host)) { error("Hostname should contain 0-9a-zA-Z_.- characters only"); return FALSE; } if($hostid==NULL) $result=DBexecute("select * from hosts where host=".zbx_dbstr($host)); else $result=DBexecute("select * from hosts where host=".zbx_dbstr($host). " and hostid<>$hostid"); if(DBnum_rows($result)>0) { error("Host '$host' already exists"); return FALSE; } if($useip=="on" || $useip=="yes" || $useip==1) $useip=1; else $useip=0; if($hostid==NULL) { $result = DBexecute("insert into hosts". " (host,port,status,useip,ip,disable_until,available,templateid)". " values (".zbx_dbstr($host).",$port,$status,$useip,".zbx_dbstr($ip).",0," .HOST_AVAILABLE_UNKNOWN.",$templateid)"); } else { if(check_circle_host_link($hostid, $templateid)) { error("Circle link can't be created"); return FALSE; } $result = DBexecute("update hosts set host=".zbx_dbstr($host).",". "port=$port,useip=$useip,ip=".zbx_dbstr($ip).",templateid=$templateid". " where hostid=$hostid"); update_host_status($hostid, $status); } return $result; } function add_host($host,$port,$status,$useip,$ip,$templateid,$newgroup,$groups) { if(!check_right("Host","A",0)) { error("Insufficient permissions"); return FALSE; } $result = db_save_host($host,$port,$status,$useip,$ip,$templateid); if(!$result) return $result; $hostid = DBinsert_id($result,"hosts","hostid"); update_host_groups($hostid,$groups); add_group_to_host($hostid,$newgroup); sync_host_with_templates($hostid); update_profile("HOST_PORT",$port); return $hostid; } function update_host($hostid,$host,$port,$status,$useip,$ip,$templateid,$newgroup,$groups) { if(!check_right("Host","U",$hostid)) { error("Insufficient permissions"); return FALSE; } $old_host = get_host_by_hostid($hostid); $result = db_save_host($host,$port,$status,$useip,$ip,$templateid,$hostid); if(!$result) return $result; update_host_groups($hostid, $groups); add_group_to_host($hostid,$newgroup); if($old_host["templateid"] != $templateid) sync_host_with_templates($hostid); return $result; } # Sync host with linked template function sync_host_with_templates($hostid) { $host = get_host_by_hostid($hostid); delete_template_graphs_by_hostid($hostid); delete_template_triggers_by_hostid($hostid); delete_template_items_by_hostid($hostid); if($host["templateid"] > 0) { // start host syncing sync_items_with_template($hostid); sync_triggers_with_template($hostid); sync_graphs_with_templates($hostid); // end host syncing } } function delete_groups_by_hostid($hostid) { $sql="select groupid from hosts_groups where hostid=$hostid"; $result=DBselect($sql); while($row=DBfetch($result)) { $sql="delete from hosts_groups where hostid=$hostid and groupid=".$row["groupid"]; DBexecute($sql); $sql="select count(*) as count from hosts_groups where groupid=".$row["groupid"]; $result2=DBselect($sql); $row2=DBfetch($result2); if($row2["count"]==0) { $sql="delete from groups where groupid=".$row["groupid"]; DBexecute($sql); } } } # Delete Host function delete_host($hostid) { global $DB_TYPE; $ret = FALSE; // delete items -> triggers -> graphs $db_items = get_items_by_hostid($hostid); while($db_item = DBfetch($db_items)) { delete_item($db_item["itemid"]); } // delete host from maps delete_sysmaps_elements_with_hostid($hostid); // delete host from group DBexecute("delete from hosts_groups where hostid=$hostid"); // unlink child hosts $db_childs = get_hosts_by_templateid($hostid); while($db_child = DBfetch($db_childs)) { DBexecute("update hosts set templateid=0 where hostid=".$db_child["hostid"]); sync_host_with_templates($hostid); } // delete host profile delete_host_profile($hostid); // delete host return DBexecute("delete from hosts where hostid=$hostid"); } function delete_host_group($groupid) { $sql="delete from hosts_groups where groupid=$groupid"; DBexecute($sql); $sql="delete from groups where groupid=$groupid"; return DBexecute($sql); } function get_hostgroup_by_groupid($groupid) { $result=DBselect("select * from groups where groupid=".$groupid); if(DBnum_rows($result) == 1) { return DBfetch($result); } error("No host groups with groupid=[$groupid]"); return FALSE; } function get_host_by_itemid($itemid) { $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); } 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) { return DBfetch($result); } error("No host with hostid=[$hostid]"); return FALSE; } function get_hosts_by_templateid($templateid) { return DBselect("select * from hosts where templateid=$templateid"); } # Update Host status function update_host_status($hostid,$status) { if(!check_right("Host","U",0)) { error("Insufficient permissions"); return 0; } $sql="select status,host from hosts where hostid=$hostid"; $result=DBselect($sql); $row=DBfetch($result); $old_status=$row["status"]; if($status != $old_status) { update_trigger_value_to_unknown_by_hostid($hostid); info("Updated status of host ".$row["host"]); return DBexecute("update hosts set status=$status". " where hostid=$hostid and status!=".HOST_STATUS_DELETED); } else { return 1; } } function get_template_path($hostid) { $host = get_host_by_hostid($hostid); if ($host["templateid"]==0) return "/"; $tmp_host = get_host_by_hostid($host["templateid"]); return get_template_path($tmp_host["hostid"]).$tmp_host["host"]."/"; } function get_correct_group_and_host($a_groupid=NULL, $a_hostid=NULL, $right="U", $options = array()) { if(!is_array($options)) { error("Incorrest options for get_correct_group_and_host"); show_page_footer(); exit; } $first_hostig_in_group = 0; $allow_all_hosts = (in_array("allow_all_hosts",$options)) ? 1 : 0; if(in_array("monitored_hosts",$options)) $with_host_status = " and h.status=".HOST_STATUS_MONITORED; else $with_host_status = ""; if(in_array("with_monitored_items",$options)){ $item_table = ",items i"; $with_items = " and h.hostid=i.hostid and i.status=".ITEM_STATUS_ACTIVE; }elseif(in_array("with_items",$options)){ $item_table = ",items i"; $with_items = " and h.hostid=i.hostid"; } else { $item_table = ""; $with_items = ""; } if(is_null($a_groupid)) { $groupid = 0; } else { $groupid = $a_groupid; if($groupid > 0) if(DBnum_rows(DBselect("select hg.groupid from hosts_groups hg". " where hg.groupid=".$groupid." group by hg.groupid")) != 1) $groupid = 0; if($groupid > 0) { // Check if at least one host with read permission exists for this group $sql = "select h.hostid,h.host from hosts h,hosts_groups hg".$item_table. " where hg.groupid=".$groupid." and hg.hostid=h.hostid and". " h.status<>".HOST_STATUS_DELETED.$with_host_status.$with_items. " group by h.hostid order by h.host"; $db_hosts = DBselect($sql); while($db_host = DBfetch($db_hosts)) { if(!check_right("Host",$right,$db_host["hostid"])) continue; $first_hostig_in_group = $db_host["hostid"]; break; } if($first_hostig_in_group == 0) $groupid = 0; } } if(is_null($a_hostid)) { $hostid = 0; } else { $hostid = $a_hostid; if($groupid == 0) { $sql = "select h.hostid,h.host from hosts h".$item_table. " where h.status<>".HOST_STATUS_DELETED.$with_host_status.$with_items. " group by h.hostid order by h.host"; $db_hosts = DBselect($sql); while($db_host = DBfetch($db_hosts)) { if(!check_right("Host",$right,$db_host["hostid"])) continue; $first_hostig_in_group = $db_host["hostid"]; break; } if($first_hostig_in_group == 0) $hostid = 0; } if($groupid > 0) { if(DBnum_rows(DBselect("select hg.hostid from hosts_groups hg". " where hg.groupid=".$groupid." and hg.hostid=".$hostid)) != 1) $hostid = 0; } if(!check_right("Host",$right,$hostid)) $hostid = 0; if($hostid > 0) { if(DBnum_rows(DBselect("select distinct h.hostid from hosts h".$item_table. " where h.status<>".HOST_STATUS_DELETED.$with_host_status.$with_items. " and h.hostid=".$hostid)) != 1) $hostid = 0; } if(($hostid < 0) || ($hostid == 0 && !($allow_all_hosts ==1 && $groupid==0))) { $hostid = $first_hostig_in_group; } } $host_correct = ($hostid == $a_hostid) ? 1 : 0; $group_correct = ($groupid == $a_groupid) ? 1 : 0; $correct = ($group_correct && $host_correct) ? 1 : 0; $result = array( "groupid" => $groupid, "group_correct" => $group_correct, "hostid" => $hostid, "host_correct" => $host_correct, "correct" => $correct ); return $result; } function validate_group_with_host($right, $options = array(),$group_var=NULL,$host_var=NULL) { if(is_null($group_var)) $group_var = "web.latest.groupid"; if(is_null($host_var)) $host_var = "web.latest.hostid"; $_REQUEST["groupid"] = get_request("groupid",get_profile($group_var,0)); $_REQUEST["hostid"] = get_request("hostid",get_profile($host_var, (in_array("always_select_first_host",$options)) ? -1 : 0)); $result = get_correct_group_and_host($_REQUEST["groupid"],$_REQUEST["hostid"], $right, $options); $_REQUEST["groupid"] = $result["groupid"]; $_REQUEST["hostid"] = $result["hostid"]; update_profile($host_var,$_REQUEST["hostid"]); update_profile($group_var,$_REQUEST["groupid"]); } function validate_group($right, $options = array(),$group_var=NULL) { if(is_null($group_var)) $group_var = "web.latest.groupid"; $_REQUEST["groupid"] = get_request("groupid",get_profile($group_var,0)); $result = get_correct_group_and_host($_REQUEST["groupid"],NULL,$right,$options); $_REQUEST["groupid"] = $result["groupid"]; update_profile($group_var,$_REQUEST["groupid"]); } ?>