summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/items.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'frontends/php/include/items.inc.php')
-rw-r--r--frontends/php/include/items.inc.php281
1 files changed, 209 insertions, 72 deletions
diff --git a/frontends/php/include/items.inc.php b/frontends/php/include/items.inc.php
index cdcdb6c4..8cdd3d26 100644
--- a/frontends/php/include/items.inc.php
+++ b/frontends/php/include/items.inc.php
@@ -47,11 +47,12 @@
return 0;
}
- $sql="select i.itemid from hosts_groups hg,items i where hg.groupid=$groupid and i.key_=".zbx_dbstr($item["key_"])." and hg.hostid=i.hostid";
+ $sql="select i.itemid from hosts_groups hg,items i".
+ " where hg.groupid=$groupid and i.key_=".zbx_dbstr($item["key_"]).
+ " and hg.hostid=i.hostid";
$result=DBexecute($sql);
while($row=DBfetch($result))
{
- delete_item_from_templates($row["itemid"]);
delete_item($row["itemid"]);
}
return 1;
@@ -72,35 +73,30 @@
# Add Item definition
- function add_item($description,$key,$hostid,$delay,$history,$status,$type,$snmp_community,$snmp_oid,$value_type,$trapper_hosts,$snmp_port,$units,$multiplier,$delta,$snmpv3_securityname,$snmpv3_securitylevel,$snmpv3_authpassphrase,$snmpv3_privpassphrase,$formula,$trends,$logtimefmt)
+ function add_item(
+ $description,$key,$hostid,$delay,$history,$status,$type,$snmp_community,$snmp_oid,
+ $value_type,$trapper_hosts,$snmp_port,$units,$multiplier,$delta,$snmpv3_securityname,
+ $snmpv3_securitylevel,$snmpv3_authpassphrase,$snmpv3_privpassphrase,$formula,$trends,$logtimefmt,
+ $templateid=0)
{
- if(!check_right("Item","A",0))
- {
- error("Insufficient permissions");
- return 0;
- }
-
$host=get_host_by_hostid($hostid);
- $sql="select count(*) as cnt from items where hostid=$hostid and key_=".zbx_dbstr($key);
- $result=DBexecute($sql);
- $row = DBfetch($result);
- if($row["cnt"]>0)
+ if(!check_right("Item","A",0))
{
- error("An item with the same Key already exists for host ".$host["host"].". The key must be unique.");
- return 0;
+ error("Insufficient permissions to item '".$host["host"].":$key'");
+ return FALSE;
}
if($delay<1)
{
error("Delay cannot be less than 1 second");
- return 0;
+ return FALSE;
}
if( ($snmp_port<1)||($snmp_port>65535))
{
error("Invalid SNMP port");
- return 0;
+ return FALSE;
}
if($value_type == ITEM_VALUE_TYPE_STR)
@@ -108,14 +104,70 @@
$delta=0;
}
- $sql="insert into items (description,key_,hostid,delay,history,nextcheck,status,type,snmp_community,snmp_oid,value_type,trapper_hosts,snmp_port,units,multiplier,delta,snmpv3_securityname,snmpv3_securitylevel,snmpv3_authpassphrase,snmpv3_privpassphrase,formula,trends,logtimefmt) values (".zbx_dbstr($description).",".zbx_dbstr($key).",$hostid,$delay,$history,0,$status,$type,".zbx_dbstr($snmp_community).",".zbx_dbstr($snmp_oid).",$value_type,".zbx_dbstr($trapper_hosts).",$snmp_port,".zbx_dbstr($units).",$multiplier,$delta,".zbx_dbstr($snmpv3_securityname).",$snmpv3_securitylevel,".zbx_dbstr($snmpv3_authpassphrase).",".zbx_dbstr($snmpv3_privpassphrase).",".zbx_dbstr($formula).",$trends,".zbx_dbstr($logtimefmt).")";
- $result=DBexecute($sql);
- if($result)
+ $db_items = DBexecute("select itemid,hostid from items".
+ " where hostid=$hostid and key_=".zbx_dbstr($key));
+ if(DBnum_rows($db_items) > 0 && $templateid == 0)
+ {
+ error("An item with the same Key already exists for host ".$host["host"].".".
+ " The key must be unique.");
+ return FALSE;
+ } elseif (DBnum_rows($db_items) > 0 && $templateid != 0){
+ $db_item = DBfetch($db_items);
+
+ $result = update_item(
+ $db_item["itemid"], $description, $key, $db_item["hostid"],
+ $delay, $history, $status, $type, $snmp_community, $snmp_oid,
+ $value_type, $trapper_hosts, $snmp_port, $units, $multiplier,
+ $delta, $snmpv3_securityname, $snmpv3_securitylevel,
+ $snmpv3_authpassphrase, $snmpv3_privpassphrase, $formula,
+ $trends, $logtimefmt, $itemid);
+
+ return $result;
+ }
+
+ // first add mother item
+ $result=DBexecute("insert into items".
+ " (description,key_,hostid,delay,history,nextcheck,status,type,".
+ "snmp_community,snmp_oid,value_type,trapper_hosts,snmp_port,units,multiplier,".
+ "delta,snmpv3_securityname,snmpv3_securitylevel,snmpv3_authpassphrase,".
+ "snmpv3_privpassphrase,formula,trends,logtimefmt,templateid)".
+ " values (".zbx_dbstr($description).",".zbx_dbstr($key).",$hostid,$delay,$history,0,
+ $status,$type,".zbx_dbstr($snmp_community).",".zbx_dbstr($snmp_oid).",$value_type,".
+ zbx_dbstr($trapper_hosts).",$snmp_port,".zbx_dbstr($units).",$multiplier,$delta,".
+ zbx_dbstr($snmpv3_securityname).",$snmpv3_securitylevel,".
+ zbx_dbstr($snmpv3_authpassphrase).",".zbx_dbstr($snmpv3_privpassphrase).",".
+ zbx_dbstr($formula).",$trends,".zbx_dbstr($logtimefmt).",$templateid)");
+
+
+ if(!$result)
+ return $result;
+
+ $itemid = DBinsert_id($result,"items","itemid");
+ info("Added new item ".$host["host"].":$key");
+
+// add items to child hosts
+
+ $db_hosts = DBselect("select hostid from hosts where templateid=".$host["hostid"]);
+ while($db_host = DBfetch($db_hosts))
{
- $host=get_host_by_hostid($hostid);
- info("Added new item ".$host["host"].":$key");
+ // recursion
+ $result = add_item($description, $key, $db_host["hostid"],
+ $delay, $history, $status, $type, $snmp_community, $snmp_oid,
+ $value_type, $trapper_hosts, $snmp_port, $units, $multiplier,
+ $delta, $snmpv3_securityname, $snmpv3_securitylevel,
+ $snmpv3_authpassphrase, $snmpv3_privpassphrase, $formula,
+ $trends, $logtimefmt, $itemid);
+ if(!$result)
+ break;
}
- return DBinsert_id($result,"items","itemid");
+ if($result)
+ return $itemid;
+
+ if($templateid == 0){
+ delete_item($itemid);
+ }
+
+ return $result;
}
# Update Item status
@@ -140,23 +192,26 @@
function update_item($itemid,$description,$key,$hostid,$delay,$history,$status,$type,
$snmp_community,$snmp_oid,$value_type,$trapper_hosts,$snmp_port,$units,$multiplier,$delta,
$snmpv3_securityname,$snmpv3_securitylevel,$snmpv3_authpassphrase,$snmpv3_privpassphrase,
- $formula,$trends,$logtimefmt)
+ $formula,$trends,$logtimefmt,$templateid=0)
{
+ $host = get_host_by_hostid($hostid);
+
if(!check_right("Item","U",$itemid))
{
- error("Insufficient permissions");
- return 0;
+ error("Insufficient permissions to item '".$host["host"].":$key'");
+ return FALSE;
}
+
if($delay<1)
{
error("Delay cannot be less than 1 second");
- return 0;
+ return FALSE;
}
if( ($snmp_port<1)||($snmp_port>65535))
{
error("Invalid SNMP port");
- return 0;
+ return FALSE;
}
if($value_type == ITEM_VALUE_TYPE_STR)
@@ -164,10 +219,52 @@
$delta=0;
}
+ $db_items = DBexecute("select itemid as cnt from items".
+ " where hostid=$hostid and itemid<>$itemid and key_=".zbx_dbstr($key));
+ if(DBnum_rows($db_items) > 0 && $templateid == 0)
+ {
+ error("An item with the same Key already exists for host ".$host["host"].".".
+ " The key must be unique.");
+ return FALSE;
+ }
+
+ // first update child items
+ $db_tmp_items = DBselect("select itemid, hostid from items where templateid=$itemid");
+ while($db_tmp_item = DBfetch($db_tmp_items))
+ {
+ // recursion
+ $result = update_item(
+ $db_tmp_item["itemid"], $description, $key, $db_tmp_item["hostid"],
+ $delay, $history, $status, $type, $snmp_community, $snmp_oid,
+ $value_type, $trapper_hosts, $snmp_port, $units, $multiplier,
+ $delta, $snmpv3_securityname, $snmpv3_securitylevel,
+ $snmpv3_authpassphrase, $snmpv3_privpassphrase, $formula,
+ $trends, $logtimefmt, $itemid);
+
+ if(!$result)
+ return $result;
+ }
+
+ if(DBnum_rows($db_items) > 0 && $templateid != 0)
+ {
+ $result = delete_item($itemid);
+ if(!$result) {
+ error("Can't update item '".$host["host"].":$key'");
+ return FALSE;
+ }
+ $db_item = DBfetch($db_items);
+ $itemid = $db_item("itemid");
+ }
+
DBexecute("update items set lastlogsize=0 where itemid=$itemid and key_<>".zbx_dbstr($key));
- $sql="update items set description=".zbx_dbstr($description).",key_=".zbx_dbstr($key).",".
- "hostid=$hostid,delay=$delay,history=$history,nextcheck=0,status=$status,type=$type,".
+ if($templateid==0){
+ update_item_status($itemid, $status);
+ }
+
+ $result=DBexecute(
+ "update items set description=".zbx_dbstr($description).",key_=".zbx_dbstr($key).",".
+ "hostid=$hostid,delay=$delay,history=$history,nextcheck=0,type=$type,".
"snmp_community=".zbx_dbstr($snmp_community).",snmp_oid=".zbx_dbstr($snmp_oid).",".
"value_type=$value_type,trapper_hosts=".zbx_dbstr($trapper_hosts).",".
"snmp_port=$snmp_port,units=".zbx_dbstr($units).",multiplier=$multiplier,delta=$delta,".
@@ -176,35 +273,63 @@
"snmpv3_authpassphrase=".zbx_dbstr($snmpv3_authpassphrase).",".
"snmpv3_privpassphrase=".zbx_dbstr($snmpv3_privpassphrase).",".
"formula=".zbx_dbstr($formula).",trends=$trends,logtimefmt=".zbx_dbstr($logtimefmt).
- " where itemid=$itemid";
- $result=DBexecute($sql);
+ ",templateid=$templateid where itemid=$itemid");
if($result)
{
- $host=get_host_by_hostid($hostid);
- info("Item ".$host["host"].":$key updated");
+ info("Item '".$host["host"].":$key' updated");
+
}
return $result;
}
- function sync_items_with_template_host($hostid,$host_templateid)
+ function delete_template_items_by_hostid($hostid)
{
- $sql="select itemid from items where hostid=$host_templateid";
- $result=DBselect($sql);
- while($row=DBfetch($result))
+ $db_items = DBselect("select itemid from items where hostid=$hostid and templateid<>0");
+ while($db_item = DBfetch($db_items))
{
- $item=get_item_by_itemid($row["itemid"]);
+ delete_item($db_item["itemid"]);
+ }
+ }
- $sql="select itemid from items where key_=".zbx_dbstr($item["key_"])." and hostid=$hostid";
- $result2=DBselect($sql);
- if(DBnum_rows($result2)==0)
- {
- add_item($item["description"],$item["key_"],$hostid,$item["delay"],$item["history"],$item["status"],$item["type"],$item["snmp_community"],$item["snmp_oid"],$item["value_type"],$item["trapper_hosts"],$item["snmp_port"],$item["units"],$item["multiplier"],$item["delta"],$item["snmpv3_securityname"],$item["snmpv3_securitylevel"],$item["snmpv3_authpassphrase"],$item["snmpv3_privpassphrase"],$item["formula"],$item["trends"],$item["logtimefmt"]);
- }
+ function sync_items_with_template($hostid)
+ {
+ $host = get_host_by_hostid($hostid);
+
+//SDI("sync host: ".$host['host']);
+
+ $db_tmp_items = DBselect("select * from items where hostid=".$host["templateid"]);
+
+ while($db_tmp_item = DBfetch($db_tmp_items))
+ {
+ add_item(
+ $db_tmp_item["description"],
+ $db_tmp_item["key_"],
+ $hostid,
+ $db_tmp_item["delay"],
+ $db_tmp_item["history"],
+ $db_tmp_item["status"],
+ $db_tmp_item["type"],
+ $db_tmp_item["snmp_community"],
+ $db_tmp_item["snmp_oid"],
+ $db_tmp_item["value_type"],
+ $db_tmp_item["trapper_hosts"],
+ $db_tmp_item["snmp_port"],
+ $db_tmp_item["units"],
+ $db_tmp_item["multiplier"],
+ $db_tmp_item["delta"],
+ $db_tmp_item["snmpv3_securityname"],
+ $db_tmp_item["snmpv3_securitylevel"],
+ $db_tmp_item["snmpv3_authpassphrase"],
+ $db_tmp_item["snmpv3_privpassphrase"],
+ $db_tmp_item["formula"],
+ $db_tmp_item["trends"],
+ $db_tmp_item["logtimefmt"],
+ $db_tmp_item["itemid"]);
}
}
# Add item to hardlinked hosts
-
+/*
function add_item_to_linked_hosts($itemid,$hostid=0)
{
if($itemid<=0)
@@ -244,9 +369,9 @@
}
}
}
-
+*/
# Add item to hardlinked hosts
-
+/*
function delete_item_from_templates($itemid)
{
if($itemid<=0)
@@ -270,9 +395,9 @@
}
}
}
-
+*/
# Update item in hardlinked hosts
-
+/*
function update_item_in_templates($itemid,$description,$key,$hostid,$delay,$history,$status,$type,
$snmp_community,$snmp_oid,$value_type,$trapper_hosts,$snmp_port,$units,$multiplier,$delta,
$snmpv3_securityname,$snmpv3_securitylevel,$snmpv3_authpassphrase,$snmpv3_privpassphrase,
@@ -304,7 +429,7 @@
}
}
}
-
+*/
# Activate Item
function activate_item($itemid)
@@ -349,30 +474,33 @@
function delete_item($itemid)
{
- $result=delete_triggers_by_itemid($itemid);
- if(!$result)
- {
- return $result;
- }
- $result=delete_trends_by_itemid($itemid);
- $result=delete_history_by_itemid($itemid);
- $sql="delete from graphs_items where itemid=$itemid";
- if(!$result)
- {
- return $result;
- }
- $result=DBexecute($sql);
- if(!$result)
- {
- return $result;
+ $item = get_item_by_itemid($itemid);
+ $host = get_host_by_itemid($itemid);
+
+ // first delete child items
+ $db_items = DBselect("select itemid from items where templateid=$itemid");
+ while($db_item = DBfetch($db_items))
+ {// recursion
+ $result = delete_item($db_item["itemid"]);
+ if(!$result) return $result;
}
- $item=get_item_by_itemid($itemid);
- $host=get_host_by_hostid($item["hostid"]);
- $sql="delete from items where itemid=$itemid";
- $result=DBexecute($sql);
+
+ $result = delete_triggers_by_itemid($itemid);
+ if(!$result) return $result;
+
+ $result = delete_trends_by_itemid($itemid);
+ if(!$result) return $result;
+
+ $result = delete_history_by_itemid($itemid);
+ if(!$result) return $result;
+
+ $result = DBexecute("delete from graphs_items where itemid=$itemid");
+ if(!$result) return $result;
+
+ $result = DBexecute("delete from items where itemid=$itemid");
if($result)
{
- info("Item ".$host["host"].":".$item["key_"]." deleted");
+ info("Item '".$host["host"].":".$item["key_"]."' deleted");
}
return $result;
}
@@ -405,4 +533,13 @@
return $descr;
}
+
+ function get_realhost_by_itemid($itemid)
+ {
+ $itme = get_item_by_itemid($itemid);
+ if($itme["templateid"] <> 0)
+ return get_realhost_by_itemid($itme["templateid"]);
+
+ return get_host_by_itemid($itemid);
+ }
?>