SetHeader(array(S_TIMESTAMP,$item["description"]));
// table_begin();
// table_header(array(S_TIMESTAMP,$item["description"]));
// $col=0;
while($row=DBfetch($result))
{
$table->AddRow(array(
date(S_DATE_FORMAT_YMDHMS,$row["clock"]),
$row["value"])
);
// table_row(array(
// date(S_DATE_FORMAT_YMDHMS,$row["clock"]),
// $row["value"],
// ),$col++);
}
// table_end();
return $table;
}
# Show values in plain text
function show_plaintext($itemid, $from, $till)
{
$item=get_item_by_itemid($itemid);
if($item["value_type"]==ITEM_VALUE_TYPE_FLOAT)
{
$sql="select clock,value from history where itemid=$itemid and clock>$from and clock<$till order by clock";
}
else if($item["value_type"]==ITEM_VALUE_TYPE_UINT64)
{
$sql="select clock,value from history_uint where itemid=$itemid and clock>$from and clock<$till order by clock";
}
else if($item["value_type"]==ITEM_VALUE_TYPE_LOG)
{
$sql="select clock,value from history_log where itemid=$itemid and clock>$from and clock<$till order by clock";
}
else
{
$sql="select clock,value from history_str where itemid=$itemid and clock>$from and clock<$till order by clock";
}
$result=DBselect($sql);
echo "
\n";
while($row=DBfetch($result))
{
$clock=$row["clock"];
$value=$row["value"];
echo date("Y-m-d H:i:s",$clock);
echo "\t$clock\t$value\n";
}
}
# Translate {10}>10 to something like localhost:procload.last(0)>10
function explode_exp ($expression, $html)
{
# echo "EXPRESSION:",$expression," ";
$functionid='';
$exp='';
$state='';
for($i=0;$i".$row1["host"].":".$row1["key_"].".".$row1["function"]."(".$row1["parameter"].")}";
}
else
{
$exp=$exp."{".$row1["host"].":".$row1["key_"].".".$row1["function"]."(".$row1["parameter"].")}";
}
}
continue;
}
if($state == "FUNCTIONID")
{
$functionid=$functionid.$expression[$i];
continue;
}
$exp=$exp.$expression[$i];
}
# echo "EXP:",$exp," ";
return $exp;
}
# Translate localhost:procload.last(0)>10 to {12}>10
function implode_exp ($expression, $triggerid)
{
// echo "Expression:$expression ";
$exp='';
$state="";
for($i=0;$i";
// echo "KEY:$key ";
// echo "FUNCTION:$function ";
// echo "PARAMETER:$parameter ";
$state='';
$sql="select i.itemid from items i,hosts h where i.key_='$key' and h.host='$host' and h.hostid=i.hostid";
# echo $sql," ";
$res=DBselect($sql);
$row=DBfetch($res);
$itemid=$row["itemid"];
# echo "ITEMID:$itemid ";
$sql="insert into functions (itemid,triggerid,function,parameter) values ($itemid,$triggerid,'$function','$parameter')";
# echo $sql," ";
$res=DBexecute($sql);
if(!$res)
{
# echo "ERROR ";
return $res;
}
$functionid=DBinsert_id($res,"functions","functionid");
$exp=$exp.'{'.$functionid.'}';
continue;
}
if($expression[$i] == '(')
{
if($state == "FUNCTION")
{
$state='PARAMETER';
continue;
}
}
if($expression[$i] == ')')
{
if($state == "PARAMETER")
{
$state='';
continue;
}
}
if(($expression[$i] == ':') && ($state == "HOST"))
{
$state="KEY";
continue;
}
if($expression[$i] == '.')
{
if($state == "KEY")
{
$state="FUNCTION";
continue;
}
// Support for '.' in KEY
if($state == "FUNCTION")
{
$state="FUNCTION";
$key=$key.".".$function;
$function="";
continue;
}
}
if($state == "HOST")
{
$host=$host.$expression[$i];
continue;
}
if($state == "KEY")
{
$key=$key.$expression[$i];
continue;
}
if($state == "FUNCTION")
{
$function=$function.$expression[$i];
continue;
}
if($state == "PARAMETER")
{
$parameter=$parameter.$expression[$i];
continue;
}
$exp=$exp.$expression[$i];
}
return $exp;
}
function add_image($name,$imagetype,$files)
{
if(isset($files))
{
if($files["image"]["error"]==0)
if($files["image"]["size"]<1024*1024)
{
$image=zbx_ads(fread(fopen($files["image"]["tmp_name"],"r"),filesize($files["image"]["tmp_name"])));
$sql="insert into images (name,imagetype,image) values ('$name',$imagetype,'$image')";
return DBexecute($sql);
}
else
{
error("Image size must be less than 1Mb");
return FALSE;
}
}
else
{
error("Select image to download");
return FALSE;
}
}
function update_image($imageid,$name,$imagetype,$files)
{
if(isset($files))
{
if($files["image"]["error"]==0)
if($files["image"]["size"]<1024*1024)
{
$image=zbx_ads(fread(fopen($files["image"]["tmp_name"],"r"),filesize($files["image"]["tmp_name"])));
$sql="update images set name='$name',imagetype='$imagetype',image='$image' where imageid='$imageid'";
return DBexecute($sql);
}
else
{
error("Image size must be less than 1Mb");
return FALSE;
}
}
else
{
error("Select image to download");
return FALSE;
}
}
function delete_image($imageid)
{
$sql="delete from images where imageid=$imageid";
return DBexecute($sql);
}
# Delete Alert by actionid
function delete_alert_by_actionid( $actionid )
{
$sql="delete from alerts where actionid=$actionid";
return DBexecute($sql);
}
function delete_rights_by_userid($userid )
{
$sql="delete from rights where userid=$userid";
return DBexecute($sql);
}
# Delete from History
function delete_history_by_itemid( $itemid )
{
$sql="delete from history_str where itemid=$itemid";
DBexecute($sql);
$sql="delete from history where itemid=$itemid";
return DBexecute($sql);
}
# Delete from Trends
function delete_trends_by_itemid( $itemid )
{
$sql="delete from trends where itemid=$itemid";
return DBexecute($sql);
}
# Add alarm
function add_alarm($triggerid,$value)
{
$sql="select max(clock) from alarms where triggerid=$triggerid";
$result=DBselect($sql);
$row=DBfetch($result);
if($row[0]!="")
{
$sql="select value from alarms where triggerid=$triggerid and clock=".$row[0];
$result=DBselect($sql);
if(DBnum_rows($result) == 1)
{
$row=DBfetch($result);
if($row["value"] == $value)
{
return 0;
}
}
}
$now=time();
$sql="insert into alarms(triggerid,clock,value) values($triggerid,$now,$value)";
return DBexecute($sql);
}
# Reset nextcheck for related items
function reset_items_nextcheck($triggerid)
{
$sql="select itemid from functions where triggerid=$triggerid";
$result=DBselect($sql);
while($row=DBfetch($result))
{
$sql="update items set nextcheck=0 where itemid=".$row["itemid"];
DBexecute($sql);
}
}
# Add host-template linkage
function add_template_linkage($hostid,$templateid,$items,$triggers,$graphs)
{
$sql="insert into hosts_templates (hostid,templateid,items,triggers,graphs) values ($hostid,$templateid,$items,$triggers,$graphs)";
return DBexecute($sql);
}
# Update host-template linkage
function update_template_linkage($hosttemplateid,$hostid,$templateid,$items,$triggers,$graphs)
{
$sql="update hosts_templates set hostid=$hostid,templateid=$templateid,items=$items,triggers=$triggers,graphs=$graphs where hosttemplateid=$hosttemplateid";
return DBexecute($sql);
}
# Delete host-template linkage
function delete_template_linkage($hosttemplateid)
{
$sql="delete from hosts_templates where hosttemplateid=$hosttemplateid";
return DBexecute($sql);
}
function add_group_to_host($hostid,$newgroup)
{
$sql="insert into groups (groupid,name) values (NULL,'$newgroup')";
$result=DBexecute($sql);
if(!$result)
{
return $result;
}
$groupid=DBinsert_id($result,"groupd","groupid");
$sql="insert into hosts_groups (hostid,groupid) values ($hostid,$groupid)";
$result=DBexecute($sql);
return $result;
}
function update_host_groups_by_groupid($groupid,$hosts)
{
$count=count($hosts);
$sql="delete from hosts_groups where groupid=$groupid";
DBexecute($sql);
for($i=0;$i<$count;$i++)
{
$sql="insert into hosts_groups (hostid,groupid) values (".$hosts[$i].",$groupid)";
DBexecute($sql);
}
}
function update_host_groups($hostid,$groups)
{
$count=count($groups);
$sql="delete from hosts_groups where hostid=$hostid";
DBexecute($sql);
for($i=0;$i<$count;$i++)
{
$sql="insert into hosts_groups (hostid,groupid) values ($hostid,".$groups[$i].")";
DBexecute($sql);
}
}
function add_host_group($name,$hosts)
{
// if(!check_right("Host","A",0))
// {
// error("Insufficient permissions");
// return 0;
// }
$sql="select * from groups where name='$name'";
$result=DBexecute($sql);
if(DBnum_rows($result)>0)
{
error("Group '$name' already exists");
return 0;
}
$sql="insert into groups (name) values ('$name')";
$result=DBexecute($sql);
if(!$result)
{
return $result;
}
$groupid=DBinsert_id($result,"groups","groupid");
update_host_groups_by_groupid($groupid,$hosts);
return $result;
}
function update_host_group($groupid,$name,$users)
{
// if(!check_right("Host","U",0))
// {
// error("Insufficient permissions");
// return 0;
// }
$sql="select * from groups where name='$name' and groupid<>$groupid";
$result=DBexecute($sql);
if(DBnum_rows($result)>0)
{
error("Group '$name' already exists");
return 0;
}
$sql="update groups set name='$name' where groupid=$groupid";
$result=DBexecute($sql);
if(!$result)
{
return $result;
}
update_host_groups_by_groupid($groupid,$users);
return $result;
}
# Sync host with hard-linked templates
function sync_host_with_templates($hostid)
{
$sql="select * from hosts_templates where hostid=$hostid";
$result=DBselect($sql);
while($row=DBfetch($result))
{
sync_host_with_template($hostid,$row["templateid"],$row["items"],$row["triggers"],
$row["graphs"]);
}
}
# Sync host with hard-linked template
function sync_host_with_template($hostid,$templateid,$items,$triggers,$graphs)
{
if(!isset($templateid)||($templateid==0))
{
error("Select template first");
return 0;
}
// Sync items
$sql="select itemid from items where hostid=$templateid";
$result=DBselect($sql);
while($row=DBfetch($result))
{
add_item_to_linked_hosts($row["itemid"],$hostid);
}
// Sync triggers
$sql="select distinct t.triggerid from hosts h, items i,triggers t,functions f where h.hostid=$templateid and h.hostid=i.hostid and t.triggerid=f.triggerid and i.itemid=f.itemid";
$result=DBselect($sql);
while($row=DBfetch($result))
{
add_trigger_to_linked_hosts($row["triggerid"],$hostid);
}
// Sync graphs
$sql="select distinct gi.gitemid from graphs g,graphs_items gi,items i where i.itemid=gi.itemid and i.hostid=$templateid and g.graphid=gi.graphid";
$result=DBselect($sql);
while($row=DBfetch($result))
{
// Hmm WHere is this function?
// add_graph_item_to_linked_hosts($row["gitemid"],$hostid);
}
return TRUE;
}
# Delete Media definition by mediatypeid
function delete_media_by_mediatypeid($mediatypeid)
{
$sql="delete from media where mediatypeid=$mediatypeid";
return DBexecute($sql);
}
# Delete alrtes by mediatypeid
function delete_alerts_by_mediatypeid($mediatypeid)
{
$sql="delete from alerts where mediatypeid=$mediatypeid";
return DBexecute($sql);
}
function get_mediatype_by_mediatypeid($mediatypeid)
{
$sql="select * from media_type where mediatypeid=$mediatypeid";
$result=DBselect($sql);
if(DBnum_rows($result) == 1)
{
return DBfetch($result);
}
else
{
error("No media type with with mediatypeid=[$mediatypeid]");
}
return $item;
}
# Delete media type
function delete_mediatype($mediatypeid)
{
delete_media_by_mediatypeid($mediatypeid);
delete_alerts_by_mediatypeid($mediatypeid);
$sql="delete from media_type where mediatypeid=$mediatypeid";
return DBexecute($sql);
}
# Update media type
function update_mediatype($mediatypeid,$type,$description,$smtp_server,$smtp_helo,$smtp_email,$exec_path)
{
$ret = 0;
$sql="select * from media_type where description='".zbx_ads($description)."' and mediatypeid!=".zbx_ads($mediatypeid);
$result=DBexecute($sql);
if(DBnum_rows($result)>0)
{
error("An action type with description '$description' already exists.");
}
else
{
$sql="update media_type set type=".zbx_ads($type).",description='".zbx_ads($description)."',smtp_server='".zbx_ads($smtp_server)."',smtp_helo='".zbx_ads($smtp_helo)."',smtp_email='".zbx_ads($smtp_email)."',exec_path='".zbx_ads($exec_path)."' where mediatypeid=".zbx_ads($mediatypeid);
$ret = DBexecute($sql);
}
return $ret;
}
# Add Media type
function add_mediatype($type,$description,$smtp_server,$smtp_helo,$smtp_email,$exec_path)
{
$ret = 0;
if($description==""){
error(S_INCORRECT_DESCRIPTION);
return 0;
}
$description=zbx_ads($description);
$sql="select * from media_type where description='$description'";
$result=DBexecute($sql);
if(DBnum_rows($result)>0)
{
error("An action type with description '$description' already exists.");
}
else
{
$sql="insert into media_type (type,description,smtp_server,smtp_helo,smtp_email,exec_path) values ($type,'$description','$smtp_server','$smtp_helo','$smtp_email','$exec_path')";
$ret = DBexecute($sql);
}
return $ret;
}
# Add Media definition
function add_media( $userid, $mediatypeid, $sendto, $severity, $active, $period)
{
$c=count($severity);
$s=0;
for($i=0;$i<$c;$i++)
{
$s=$s|pow(2,(int)$severity[$i]);
}
$sql="insert into media (userid,mediatypeid,sendto,active,severity,period) values ($userid,'$mediatypeid','$sendto',$active,$s,'$period')";
return DBexecute($sql);
}
# Update Media definition
function update_media($mediaid, $userid, $mediatypeid, $sendto, $severity, $active, $period)
{
$c=count($severity);
$s=0;
for($i=0;$i<$c;$i++)
{
$s=$s|pow(2,(int)$severity[$i]);
}
$sql="update media set userid=$userid, mediatypeid=$mediatypeid, sendto='$sendto', active=$active,severity=$s,period='$period' where mediaid=$mediaid";
return DBexecute($sql);
}
# Delete Media definition
function delete_media($mediaid)
{
$sql="delete from media where mediaid=$mediaid";
return DBexecute($sql);
}
# Delete Media definition by userid
function delete_media_by_userid($userid)
{
$sql="delete from media where userid=$userid";
return DBexecute($sql);
}
function delete_profiles_by_userid($userid)
{
$sql="delete from profiles where userid=$userid";
return DBexecute($sql);
}
# Update configuration
// function update_config($smtp_server,$smtp_helo,$smtp_email,$alarm_history,$alert_history)
function update_config($alarm_history,$alert_history,$refresh_unsupported)
{
if(!check_right("Configuration of Zabbix","U",0))
{
error("Insufficient permissions");
return 0;
}
// $sql="update config set smtp_server='$smtp_server',smtp_helo='$smtp_helo',smtp_email='$smtp_email',alarm_history=$alarm_history,alert_history=$alert_history";
$sql="update config set alarm_history=$alarm_history,alert_history=$alert_history,refresh_unsupported=$refresh_unsupported";
return DBexecute($sql);
}
# Activate Media
function activate_media($mediaid)
{
$sql="update media set active=0 where mediaid=$mediaid";
return DBexecute($sql);
}
# Disactivate Media
function disactivate_media($mediaid)
{
$sql="update media set active=1 where mediaid=$mediaid";
return DBexecute($sql);
}
# Delete User permission
function delete_permission($rightid)
{
$sql="delete from rights where rightid=$rightid";
return DBexecute($sql);
}
# Delete User definition
function delete_user($userid)
{
$sql="select * from users where userid=$userid and alias='guest'";
$result=DBselect($sql);
if(DBnum_rows($result) == 1)
{
error("Cannot delete user 'guest'");
return 0;
}
delete_media_by_userid($userid);
delete_actions_by_userid($userid);
delete_rights_by_userid($userid);
delete_profiles_by_userid($userid);
$sql="delete from users_groups where userid=$userid";
DBexecute($sql);
$sql="delete from users where userid=$userid";
return DBexecute($sql);
}
function show_table_h_delimiter()
{
// echo "";
cr();
echo "";
cr();
echo "