diff options
| author | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2006-02-10 12:53:34 +0000 |
|---|---|---|
| committer | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2006-02-10 12:53:34 +0000 |
| commit | f54860dfdd8d24de1dc26a530d95a23343756dc2 (patch) | |
| tree | 76234c5a861862aee2170b41cf7a517e82633e93 /frontends/php/include/hosts.inc.php | |
| parent | 455a64dfe19d7700fa1fe10edecb1ea82fa5dcce (diff) | |
| download | zabbix-f54860dfdd8d24de1dc26a530d95a23343756dc2.tar.gz zabbix-f54860dfdd8d24de1dc26a530d95a23343756dc2.tar.xz zabbix-f54860dfdd8d24de1dc26a530d95a23343756dc2.zip | |
- improved Templates logic (Eugene)
- speed improvement for proc.num of Tru64 (Eugene)
git-svn-id: svn://svn.zabbix.com/trunk@2631 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'frontends/php/include/hosts.inc.php')
| -rw-r--r-- | frontends/php/include/hosts.inc.php | 261 |
1 files changed, 192 insertions, 69 deletions
diff --git a/frontends/php/include/hosts.inc.php b/frontends/php/include/hosts.inc.php index c874c318..3a547d46 100644 --- a/frontends/php/include/hosts.inc.php +++ b/frontends/php/include/hosts.inc.php @@ -19,122 +19,234 @@ **/ ?> <?php - # Add Host definition - function add_host($host,$port,$status,$useip,$ip,$host_templateid,$newgroup,$groups) +/* HOST GROUP functions */ + function add_host_to_group($hostid, $groupid) { - if(!check_right("Host","A",0)) - { - error("Insufficient permissions"); - return 0; + if(!is_numeric($hostid) || !is_numeric($groupid)){ + error("incorrect parameters for 'add_host_to_group'"); + return FALSE; } - - if (!eregi('^([0-9a-zA-Z\_\.-]+)$', $host, $arr)) - { - error("Hostname should contain 0-9a-zA-Z_.- characters only"); - return 0; + return DBexecute("insert into hosts_groups (hostid,groupid) values ($hostid,$groupid)"); + } + function db_save_group($name,$groupid=NULL) + { + if(!is_string($name)){ + error("incorrect parameters for 'db_save_group'"); + return FALSE; } - - $sql="select * from hosts where host=".zbx_dbstr($host); - $result=DBexecute($sql); + + if($groupid==NULL) + $result = DBexecute("select * from groups where name=".zbx_dbstr($name)); + else + $result = DBexecute("select * from groups where name=".zbx_dbstr($name). + " and groupid<>$groupid"); + if(DBnum_rows($result)>0) { - error("Host '$host' already exists"); - return 0; - } - - if($useip=="on" || $useip=="yes" || $useip==1) - { - $useip=1; + error("Group '$name' already exists"); + return FALSE; } + if($groupid==NULL) + return DBexecute("insert into groups (name) values (".zbx_dbstr($name).")"); else - { - $useip=0; - } + 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; - $sql="insert into hosts (host,port,status,useip,ip,disable_until,available) values (".zbx_dbstr($host).",$port,$status,$useip,".zbx_dbstr($ip).",0,".HOST_AVAILABLE_UNKNOWN.")"; - $result=DBexecute($sql); + $result = db_save_group($newgroup); if(!$result) - { return $result; - } - $hostid=DBinsert_id($result,"hosts","hostid"); + $groupid = DBinsert_id($result,"groupd","groupid"); - if($host_templateid != 0) - { - add_templates_to_host($hostid,$host_templateid); - sync_host_with_templates($hostid); - } - if($groups != "") + 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) { - update_host_groups($hostid,$groups); + add_host_to_group($hostid, $groupid); } - if($newgroup != "") + } + + function update_host_groups($hostid,$groups=array()) + { + DBexecute("delete from hosts_groups where hostid=$hostid"); + + foreach($groups as $groupid) { - add_group_to_host($hostid,$newgroup); + add_host_to_group($hostid, $groupid); } + } - update_profile("HOST_PORT",$port); + function add_host_group($name,$hosts) + { +// if(!check_right("Host","A",0)) +// { +// error("Insufficient permissions"); +// return FLASE; +// } + + $result = db_save_group($name); + if(!$result) + return $result; - return $result; + $groupid = DBinsert_id($result,"groups","groupid"); + + update_host_groups_by_groupid($groupid,$hosts); + + return $groupid; } - function update_host($hostid,$host,$port,$status,$useip,$ip,$newgroup,$groups) + function update_host_group($groupid,$name,$hosts) { - if(!check_right("Host","U",$hostid)) - { - error("Insufficient permissions"); - return 0; - } +// if(!check_right("Host","U",0)) +// { +// error("Insufficient permissions"); +// return 0; +// } - if (!eregi('^([0-9a-zA-Z\_\.-]+)$', $host, $arr)) + + $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 0; + return FALSE; } - $sql="select * from hosts where host=".zbx_dbstr($host)." and hostid<>$hostid"; - $result=DBexecute($sql); + 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 0; + return FALSE; } + if($useip=="on" || $useip=="yes" || $useip==1) $useip=1; + else $useip=0; - if($useip=="on" || $useip=="yes" || $useip==1) + if($hostid==NULL) { - $useip=1; + $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 { - $useip=0; + 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; + } - $sql="update hosts set host=".zbx_dbstr($host).",port=$port,useip=$useip,ip=".zbx_dbstr($ip)." where hostid=$hostid"; - $result=DBexecute($sql); + 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_status($hostid, $status); update_host_groups($hostid,$groups); - if($newgroup != "") + + 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)) { - add_group_to_host($hostid,$newgroup); + 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; } - # Add templates linked to template host to the host - - function add_templates_to_host($hostid,$host_templateid) +# Sync host with linked template + function sync_host_with_templates($hostid) { - $sql="select * from hosts_templates where hostid=$host_templateid"; - $result=DBselect($sql); - while($row=DBfetch($result)) + $host = get_host_by_hostid($hostid); + delete_template_items_by_hostid($hostid); +// TODO delete_template_triggers_by_hostid($hostid); +// TODO delete_template_hosts_by_hostid($hostid); + + if($host["templateid"] > 0) { - add_template_linkage($hostid,$row["templateid"],$row["items"],$row["triggers"], - $row["graphs"]); +// start host syncing + sync_items_with_template($hostid); +// TODO sync_triggers_with_template($hostid); +// TODO sync_hosts_with_teplates($hostid); +// end host syncing } } @@ -243,13 +355,24 @@ if($status != $old_status) { update_trigger_value_to_unknown_by_hostid($hostid); - $sql="update hosts set status=$status where hostid=$hostid and status!=".HOST_STATUS_DELETED; info("Updated status of host ".$row["host"]); - return DBexecute($sql); + 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"]."/"; + } ?> |
