0) { $ret=$t."y"; $value=$value-$t*(365*24*3600); } $t=floor($value/(30*24*3600)); if($t>0) { $ret=$ret.$t."m"; $value=$value-$t*(30*24*3600); } $t=floor($value/(24*3600)); if($t>0) { $ret=$ret.$t."d"; $value=$value-$t*(24*3600); } $t=floor($value/(3600)); if($t>0) { $ret=$ret.$t."h"; $value=$value-$t*(3600); } $t=floor($value/(60)); if($t>0) { $ret=$ret.$t."m"; $value=$value-$t*(60); } $ret=$ret.$value."s"; return $ret; } $u=""; // Special processing for bits (kilo=1000, not 1024 for bits) if( ($units=="b") || ($units=="bps")) { $abs=abs($value); if($abs<1000) { $u=""; } else if($abs<1000*1000) { $u="K"; $value=$value/1000; } else if($abs<1000*1000*1000) { $u="M"; $value=$value/(1000*1000); } else { $u="G"; $value=$value/(1000*1000*1000); } if(round($value)==$value) { $s=sprintf("%.0f",$value); } else { $s=sprintf("%.2f",$value); } return "$s $u$units"; } if($units=="") { if(round($value)==$value) { return sprintf("%.0f",$value); } else { return sprintf("%.2f",$value); } } $abs=abs($value); if($abs<1024) { $u=""; } else if($abs<1024*1024) { $u="K"; $value=$value/1024; } else if($abs<1024*1024*1024) { $u="M"; $value=$value/(1024*1024); } else { $u="G"; $value=$value/(1024*1024*1024); } if(round($value)==$value) { $s=sprintf("%.0f",$value); } else { $s=sprintf("%.2f",$value); } return "$s $u$units"; } function get_template_permission_str($num) { $str=" "; if(($num&1)==1) $str=$str.S_ADD." "; if(($num&2)==2) $str=$str.S_UPDATE." "; if(($num&4)==4) $str=$str.S_DELETE." "; return $str; } function get_media_count_by_userid($userid) { $sql="select count(*) as cnt from media where userid=$userid"; $result=DBselect($sql); $row=DBfetch($result); return $row["cnt"]; } function get_action_count_by_triggerid($triggerid) { $cnt=0; $sql="select count(*) as cnt from actions where triggerid=$triggerid and scope=0"; $result=DBselect($sql); $row=DBfetch($result); $cnt=$cnt+$row["cnt"]; $sql="select count(*) as cnt from actions where scope=2"; $result=DBselect($sql); $row=DBfetch($result); $cnt=$cnt+$row["cnt"]; $sql="select distinct h.hostid from hosts h,items i,triggers t,functions f where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and t.triggerid=$triggerid"; $result=DBselect($sql); while($row=DBfetch($result)) { $sql="select count(*) as cnt from actions a,hosts h,items i,triggers t,functions f where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=t.triggerid and a.triggerid=".$row["hostid"]." and a.scope=1"; $result2=DBselect($sql); $row2=DBfetch($result2); $cnt=$cnt+$row2["cnt"]; } return $cnt; } function check_anyright($right,$permission) { global $USER_DETAILS; $sql="select permission from rights where name='Default permission' and userid=".$USER_DETAILS["userid"]; $result=DBselect($sql); $default_permission="H"; if(DBnum_rows($result)>0) { $default_permission=""; while($row=DBfetch($result)) { $default_permission=$default_permission.$row["permission"]; } } # default_permission $sql="select permission from rights where name='$right' and id!=0 and userid=".$USER_DETAILS["userid"]; $result=DBselect($sql); $all_permissions=""; if(DBnum_rows($result)>0) { while($row=DBfetch($result)) { $all_permissions=$all_permissions.$row["permission"]; } } # all_permissions // echo "$all_permissions|$default_permission
"; switch ($permission) { case 'A': if(strstr($all_permissions,"A")) { return 1; } if(strstr($default_permission,"H")) { return 0; } else if(strstr($default_permission,"A")) { return 1; } break; case 'R': if(strstr($all_permissions,"R")) { return 1; } else if(strstr($all_permissions,"U")) { return 1; } if(strstr($default_permission,"H")) { return 0; } else if(strstr($default_permission,"R")) { return 1; } else if(strstr($default_permission,"U")) { return 1; } break; case 'U': if(strstr($all_permissions,"U")) { return 1; } if(strstr($default_permission,"H")) { return 0; } else if(strstr($default_permission,"U")) { return 1; } break; default: return 0; } return 0; } function check_right($right,$permission,$id) { global $USER_DETAILS; $sql="select permission from rights where name='Default permission' and userid=".$USER_DETAILS["userid"]; $result=DBselect($sql); $default_permission="H"; if(DBnum_rows($result)>0) { $default_permission=""; while($row=DBfetch($result)) { $default_permission=$default_permission.$row["permission"]; } } # default_permission $sql="select permission from rights where name='$right' and id=0 and userid=".$USER_DETAILS["userid"]; $result=DBselect($sql); $group_permission=""; if(DBnum_rows($result)>0) { while($row=DBfetch($result)) { $group_permission=$group_permission.$row["permission"]; } } # group_permission $id_permission=""; if($id!=0) { $sql="select permission from rights where name='$right' and id=$id and userid=".$USER_DETAILS["userid"]; $result=DBselect($sql); if(DBnum_rows($result)>0) { while($row=DBfetch($result)) { $id_permission=$id_permission.$row["permission"]; } } } # id_permission // echo "$id_permission|$group_permission|$default_permission
"; switch ($permission) { case 'A': if(strstr($id_permission,"H")) { return 0; } else if(strstr($id_permission,"A")) { return 1; } if(strstr($group_permission,"H")) { return 0; } else if(strstr($group_permission,"A")) { return 1; } if(strstr($default_permission,"H")) { return 0; } else if(strstr($default_permission,"A")) { return 1; } break; case 'R': if(strstr($id_permission,"H")) { return 0; } else if(strstr($id_permission,"R")) { return 1; } else if(strstr($id_permission,"U")) { return 1; } if(strstr($group_permission,"H")) { return 0; } else if(strstr($group_permission,"R")) { return 1; } else if(strstr($group_permission,"U")) { return 1; } if(strstr($default_permission,"H")) { return 0; } else if(strstr($default_permission,"R")) { return 1; } else if(strstr($default_permission,"U")) { return 1; } break; case 'U': if(strstr($id_permission,"H")) { return 0; } else if(strstr($id_permission,"U")) { return 1; } if(strstr($group_permission,"H")) { return 0; } else if(strstr($group_permission,"U")) { return 1; } if(strstr($default_permission,"H")) { return 0; } else if(strstr($default_permission,"U")) { return 1; } break; default: return 0; } return 0; } /* function check_right($right,$permission,$id) { global $USER_DETAILS; if($id!=0) { $sql="select * from rights where name='$right' and permission in ('H') and id=$id and userid=".$USER_DETAILS["userid"]; $result=DBselect($sql); if(DBnum_rows($result)>0) { return 0; } } $sql="select permission from rights where name='Default permission' and userid=".$USER_DETAILS["userid"]; $result=DBselect($sql); $default_permission="H"; if(DBnum_rows($result)>0) { $default_permission=""; while($row=DBfetch($result)) { $default_permission=$default_permission.$row["permission"]; } } if($permission=='R') { $cond="'R','U'"; } else { $cond="'".$permission."'"; } $sql="select * from rights where name='$right' and permission in ($cond) and (id=$id or id=0) and userid=".$USER_DETAILS["userid"]; // echo $sql; $result=DBselect($sql); if(DBnum_rows($result)>0) { return 1; } else { if(strstr($default_permission,"A")&&($permission=="A")) { return 1; } if(strstr($default_permission,"R")&&($permission=="R")) { return 1; } if(strstr($default_permission,"U")&&($permission=="R")) { return 1; } if(strstr($default_permission,"U")&&($permission=="U")) { return 1; } return 0; } } */ function get_scope_description($scope) { $desc="Unknown"; if($scope==2) { $desc="All"; } elseif($scope==1) { $desc="Host"; } elseif($scope==0) { $desc="Trigger"; } return $desc; } // The hash has form , function calc_trigger_hash() { $priorities=0; for($i=0;$i<=5;$i++) { $result=DBselect("select count(*) from triggers t,hosts h,items i,functions f where t.value=1 and f.itemid=i.itemid and h.hostid=i.hostid and t.triggerid=f.triggerid and i.status=0 and t.priority=$i"); // $priorities+=(1000^$i)*DBget_field($result,0,0); $priorities+=pow(100,$i)*DBget_field($result,0,0); // echo "$i $priorities ",DBget_field($result,0,0),"
"; // echo pow(100,5)*13; } $triggerids=""; $result=DBselect("select t.triggerid from triggers t,hosts h,items i,functions f where t.value=1 and f.itemid=i.itemid and h.hostid=i.hostid and t.triggerid=f.triggerid and i.status=0"); for($i=0;$i"; echo ""; echo "[$msg]"; echo ""; echo "

"; if(is_array($INFO_MSG)) { echo "

"; while(list($key, $val)=each($INFO_MSG)) { echo $val."
"; } echo "

"; } if(is_array($ERROR_MSG)) { echo "

"; while(list($key, $val)=each($ERROR_MSG)) { echo $val."
"; } echo "

"; } } function show_message($msg) { show_messages(TRUE,$msg,''); } function show_error_message($msg) { show_messages(FALSE,'',$msg); } function validate_float($str) { // echo "Validating float:$str
"; if (eregi('^([0-9]+)((\.)?)([0-9]*[KMG]{0,1})$', $str, &$arr)) { return 0; } else { return -1; } } // Does expression match server:key.function(param) ? function validate_simple_expression($expression) { // echo "Validating simple:$expression
"; // Before str() // if (eregi('^\{([0-9a-zA-Z[.-.]\_\.]+)\:([]\[0-9a-zA-Z\_\/\.\,]+)\.((diff)|(min)|(max)|(last)|(prev))\(([0-9\.]+)\)\}$', $expression, &$arr)) // if (eregi('^\{([0-9a-zA-Z[.-.]\_\.]+)\:([]\[0-9a-zA-Z\_\/\.\,]+)\.((diff)|(min)|(max)|(last)|(prev)|(str))\(([0-9a-zA-Z\.\_\/\,]+)\)\}$', $expression, &$arr)) if (eregi('^\{([0-9a-zA-Z\_\.-]+)\:([]\[0-9a-zA-Z\_\/\.\,\:\(\) -]+)\.([a-z]{3,9})\(([0-9a-zA-Z\_\/\.\,]+)\)\}$', $expression, &$arr)) { $host=$arr[1]; $key=$arr[2]; $function=$arr[3]; $parameter=$arr[4]; // echo $host,"
"; // echo $key,"
"; // echo $function,"
"; // echo $parameter,"
"; $sql="select count(*) from hosts h,items i where h.host='$host' and i.key_='$key' and h.hostid=i.hostid"; $result=DBselect($sql); if(DBget_field($result,0,0)!=1) { error("No such host ($host) or monitored parameter ($key)"); return -1; } if( ($function!="last")&& ($function!="diff")&& ($function!="min") && ($function!="max") && ($function!="avg") && ($function!="sum") && ($function!="count") && ($function!="prev")&& ($function!="delta")&& ($function!="change")&& ($function!="abschange")&& ($function!="nodata")&& ($function!="time")&& ($function!="dayofweek")&& ($function!="date")&& ($function!="now")&& ($function!="str")) { error("Unknown function [$function]"); return -1; } if(( $function!="str") && (validate_float($parameter)!=0) ) { error("[$parameter] is not a float"); return -1; } } else { error("Expression [$expression] does not match to [server:key.func(param)]"); return -1; } return 0; } function validate_expression($expression) { // echo "Validating expression: $expression
"; $ok=0; // Replace all {server:key.function(param)} with 0 while($ok==0) { // echo "Expression:$expression
"; $arr=""; if (eregi('^((.)*)(\{((.)*)\})((.)*)$', $expression, &$arr)) { // for($i=0;$i<20;$i++) // { // if($arr[$i]) // echo " $i: ",$arr[$i],"
"; // } if(validate_simple_expression($arr[3])!=0) { return -1; } $expression=$arr[1]."0".$arr[6]; } else { $ok=1; } } // echo "Result:$expression

"; $ok=0; while($ok==0) { // Replace all with 0 // echo "Expression:$expression
"; $arr=""; if (eregi('^((.)*)([0-9\.]+[A-Z]{0,1})([\&\|\>\<\=\+\-\*\/\#]{1})([0-9\.]+[A-Z]{0,1})((.)*)$', $expression, &$arr)) { // echo "OK
"; // for($i=0;$i<50;$i++) // { // if($arr[$i]!="") // echo " $i: ",$arr[$i],"
"; // } if(validate_float($arr[3])!=0) { error("[".$arr[3]."] is not a float"); return -1; } if(validate_float($arr[5])!=0) { error("[".$arr[5]."] is not a float"); return -1; } $expression=$arr[1]."(0)".$arr[6]; } else { $ok=1; } // Replace all (float) with 0 // echo "Expression2:[$expression]
"; $arr=""; if (eregi('^((.)*)(\(([0-9\.]+)\))((.)*)$', $expression, &$arr)) { // echo "OK
"; // for($i=0;$i<30;$i++) // { // if($arr[$i]!="") // echo " $i: ",$arr[$i],"
"; // } if(validate_float($arr[4])!=0) { error("[".$arr[4]."] is not a float"); return -1; } $expression=$arr[1]."0".$arr[5]; $ok=0; } else { $ok=1; } } // echo "Result:$expression

"; if($expression=="0") { return 0; } return 1; } function cr() { echo "\n"; } function check_authorisation() { global $page; global $PHP_AUTH_USER,$PHP_AUTH_PW; global $USER_DETAILS; global $_COOKIE; global $_GET; // global $sessionid; if(isset($_COOKIE["sessionid"])) { $sessionid=$_COOKIE["sessionid"]; } else { unset($sessionid); } if(isset($sessionid)) { $sql="select u.userid,u.alias,u.name,u.surname from sessions s,users u where s.sessionid='$sessionid' and s.userid=u.userid and s.lastaccess+900>".time(); $result=DBselect($sql); if(DBnum_rows($result)==1) { // setcookie("sessionid",$sessionid,time()+3600); setcookie("sessionid",$sessionid); $sql="update sessions set lastaccess=".time()." where sessionid='$sessionid'"; DBexecute($sql); $USER_DETAILS["userid"]=DBget_field($result,0,0); $USER_DETAILS["alias"]=DBget_field($result,0,1); $USER_DETAILS["name"]=DBget_field($result,0,2); $USER_DETAILS["surname"]=DBget_field($result,0,3); return; } else { setcookie("sessionid",$sessionid,time()-3600); unset($sessionid); } } $sql="select u.userid,u.alias,u.name,u.surname from users u where u.alias='guest'"; $result=DBselect($sql); if(DBnum_rows($result)==1) { $USER_DETAILS["userid"]=DBget_field($result,0,0); $USER_DETAILS["alias"]=DBget_field($result,0,1); $USER_DETAILS["name"]=DBget_field($result,0,2); $USER_DETAILS["surname"]=DBget_field($result,0,3); return; } if($page["file"]!="index.php") { echo ""; } show_special_header("Login",0,1,1); show_error_message("Login name or password is incorrect"); insert_login_form(); show_footer(); exit; } # Header for HTML pages function show_header($title,$refresh,$nomenu) { show_special_header($title,$refresh,$nomenu,0); } function show_special_header($title,$refresh,$nomenu,$noauth) { global $page; global $USER_DETAILS; if($noauth!=1) { check_authorisation(); } ?> \n"; echo "$title [refreshed every $refresh sec]\n"; } else { echo "$title\n"; } echo ""; ?> array( "label"=>"View", "pages"=>array("overview.php","latest.php","tr_status.php","queue.php","latestalarms.php","alerts.php","maps.php","charts.php","screens.php","srv_status.php","alarms.php","history.php","tr_comments.php","report3.php"), "level2"=>array( array("label"=>"Overview","url"=>"overview.php"), array("label"=>"Latest data","url"=>"latest.php"), array("label"=>"Triggers","url"=>"tr_status.php?onlytrue=true&noactions=true&compact=true"), array("label"=>"Queue","url"=>"queue.php"), array("label"=>"Events","url"=>"latestalarms.php"), array("label"=>"Actions","url"=>"alerts.php"), array("label"=>"Maps","url"=>"maps.php"), array("label"=>"Graphs","url"=>"charts.php"), array("label"=>"Screens","url"=>"screens.php"), array("label"=>"IT Services","url"=>"srv_status.php") ) ), "reports"=>array( "label"=>"Reports", "pages"=>array("report1.php","report2.php"), "level2"=>array( array("label"=>"Status of ZABBIX","url"=>"report1.php"), array("label"=>"Availability report","url"=>"report2.php") ) ), "configuration"=>array( "label"=>"Configuration", "pages"=>array("config.php","users.php","audit.php","hosts.php","items.php","triggers.php","sysmaps.php","graphs.php","screenconf.php","services.php","sysmap.php","media.php","screenedit.php","actions.php","graph.php"), "level2"=>array( array("label"=>"General","url"=>"config.php"), array("label"=>"Users","url"=>"users.php"), array("label"=>"Audit","url"=>"audit.php"), array("label"=>"Hosts","url"=>"hosts.php"), array("label"=>"Items","url"=>"items.php"), array("label"=>"Triggers","url"=>"triggers.php"), array("label"=>"Maps","url"=>"sysmaps.php"), array("label"=>"Graphs","url"=>"graphs.php"), array("label"=>"Screens","url"=>"screenconf.php"), array("label"=>"IT Services","url"=>"services.php") ) ), "login"=>array( "label"=>"Login", "pages"=>array("index.php"), "level2"=>array( array("label"=>"Login","url"=>"index.php"), ) ), ); ?>
ZABBIX |Help|
$sub) { // Check permissions if($label=="configuration") { if( !check_anyright("Configuration of Zabbix","U") &&!check_anyright("User","U") &&!check_anyright("Host","U") &&!check_anyright("Graph","U") &&!check_anyright("Screen","U") &&!check_anyright("Network map","U") &&!check_anyright("Service","U") ) { continue; } if( !check_anyright("Default permission","R") &&!check_anyright("Host","R") ) { continue; } } // End of check permissions $active=0; foreach($sub["pages"] as $label2) { if($page["file"]==$label2) { $active=1; $active_level1=$label; } } if($active==1) echo ""; else echo ""; $i++; } ?>

$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";
		for($i=0;$i10 to something like localhost:procload.last(0)>10

	function	explode_exp ($expression, $html)
	{
#		echo "EXPRESSION:",$expression,"
"; $functionid=''; $exp=''; $state=''; for($i=0;$i".DBget_field($res1,0,0).":".DBget_field($res1,0,1).".".DBget_field($res1,0,2)."(".DBget_field($res1,0,3).")}"; } else { $exp=$exp."{".DBget_field($res1,0,0).":".DBget_field($res1,0,1).".".DBget_field($res1,0,2)."(".DBget_field($res1,0,3).")}"; } } 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); $itemid=DBget_field($res,0,0); # echo "ITEMID:$itemid
"; # $sql="select functionid,count(*) from functions where function='$function' and parameter=$parameter group by 1"; # echo $sql,"
"; # $res=DBselect($sql); # # if(DBget_field($res,0,1)>0) # { # $functionid=DBget_field($res,0,0); # } # else # { $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"); # } # echo "FUNCTIONID:$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; } # Update Host status function update_host_status($hostid,$status) { if(!check_right("Host","U",0)) { error("Insufficient permissions"); return 0; } $sql="select status from hosts where hostid=$hostid"; $result=DBselect($sql); $old_status=DBget_field($result,0,0); 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; return DBexecute($sql); } else { return 1; } } function add_image($name,$imagetype,$files) { if(isset($files)) { if($files["image"]["error"]==0) if($files["image"]["size"]<1024*1024) { $image=addslashes(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 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); for($i=0;$i"; $result4=DBexecute($sql); $actionid=DBinsert_id($result4,"actions","actionid"); } } } return TRUE; } 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 add_user_group($name,$users) { if(!check_right("Host","A",0)) { error("Insufficient permissions"); return 0; } $sql="select * from usrgrp where name='$name'"; $result=DBexecute($sql); if(DBnum_rows($result)>0) { error("Group '$name' already exists"); return 0; } $sql="insert into usrgrp (name) values ('$name')"; $result=DBexecute($sql); if(!$result) { return $result; } $usrgrpid=DBinsert_id($result,"usrgrp","usrgrpid"); update_user_groups($usrgrpid,$users); 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; } function update_user_group($usrgrpid,$name,$users) { if(!check_right("Host","U",0)) { error("Insufficient permissions"); return 0; } $sql="select * from usrgrp where name='$name' and usrgrpid<>$usrgrpid"; $result=DBexecute($sql); if(DBnum_rows($result)>0) { error("Group '$name' already exists"); return 0; } $sql="update usrgrp set name='$name' where usrgrpid=$usrgrpid"; $result=DBexecute($sql); if(!$result) { return $result; } update_user_groups($usrgrpid,$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["actions"], $row["graphs"],$row["screens"]); } } # Sync host with hard-linked template function sync_host_with_template($hostid,$templateid,$items,$triggers,$actions,$graphs,$screens) { if(!isset($templateid)||($templateid==0)) { error("Select template first"); return 0; } $host=get_host_by_hostid($hostid); $sql="select itemid from items where hostid=$templateid"; $result=DBselect($sql); while($row=DBfetch($result)) { $item=get_item_by_itemid($row["itemid"]); $itemid=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["snmp_port"],$item["units"],$item["multiplier"],$item["delta"],$item["snmpv3_securityname"],$item["snmpv3_securitylevel"],$item["snmpv3_authpassphrase"],$item["snmpv3_privpassphrase"],$item["formula"],$item["trends"]); $sql="select distinct t.triggerid from triggers t,functions f where f.itemid=".$row["itemid"]." and f.triggerid=t.triggerid"; $result2=DBselect($sql); while($row2=DBfetch($result2)) { $trigger=get_trigger_by_triggerid($row2["triggerid"]); // Cannot use add_trigger here $description=$trigger["description"]; # $description=str_replace("%s",$host["host"],$description); $sql="insert into triggers (description,priority,status,comments,url,value) values ('".addslashes($description)."',".$trigger["priority"].",".$trigger["status"].",'".addslashes($trigger["comments"])."','".addslashes($trigger["url"])."',2)"; $result4=DBexecute($sql); $triggerid=DBinsert_id($result4,"triggers","triggerid"); $sql="select functionid from functions where triggerid=".$row2["triggerid"]." and itemid=".$row["itemid"]; $result3=DBselect($sql); while($row3=DBfetch($result3)) { $function=get_function_by_functionid($row3["functionid"]); $sql="insert into functions (itemid,triggerid,function,parameter) values ($itemid,$triggerid,'".$function["function"]."','".$function["parameter"]."')"; $result4=DBexecute($sql); $functionid=DBinsert_id($result4,"functions","functionid"); $sql="update triggers set expression='".$trigger["expression"]."' where triggerid=$triggerid"; DBexecute($sql); $trigger["expression"]=str_replace("{".$row3["functionid"]."}","{".$functionid."}",$trigger["expression"]); $sql="update triggers set expression='".$trigger["expression"]."' where triggerid=$triggerid"; DBexecute($sql); } # Add actions $sql="select actionid from actions where scope=0 and triggerid=".$row2["triggerid"]; $result3=DBselect($sql); while($row3=DBfetch($result3)) { $action=get_action_by_actionid($row3["actionid"]); $userid=$action["userid"]; $scope=$action["scope"]; $severity=$action["severity"]; $good=$action["good"]; $delay=$action["delay"]; $subject=addslashes($action["subject"]); $message=addslashes($action["message"]); $recipient=$action["recipient"]; $sql="insert into actions (triggerid, userid, scope, severity, good, delay, subject, message,recipient) values ($triggerid,$userid,$scope,$severity,$good,$delay,'$subject','$message',$recipient)"; // echo "$sql
"; $result4=DBexecute($sql); $actionid=DBinsert_id($result4,"actions","actionid"); } } } # Add graphs $sql="select distinct g.graphid from graphs g,items i, hosts h,graphs_items gi where h.hostid=$templateid and i.hostid=h.hostid and gi.itemid=i.itemid and g.graphid=gi.graphid"; $result=DBselect($sql); while($row=DBfetch($result)) { $graph=get_graph_by_graphid($row["graphid"]); $graphid=add_graph($graph["name"],$graph["width"],$graph["height"],$graph["yaxistype"],$graph["yaxismin"],$graph["yaxismax"]); $sql="select distinct gi.gitemid from graphs_items gi,graphs g,items i,hosts h where gi.itemid=i.itemid and h.hostid=$templateid and i.hostid=h.hostid and g.graphid=gi.graphid and gi.itemid=i.itemid and g.graphid=".$graph["graphid"]; $result2=DBselect($sql); while($row=DBfetch($result2)) { $gitem=get_graphitem_by_gitemid($row["gitemid"]); $item=get_item_by_itemid($gitem["itemid"]); $sql="select * from items where key_='".$item["key_"]."' and hostid=$hostid"; $result3=DBselect($sql); if(DBnum_rows($result3)==1) { $row2=DBfetch($result3); add_item_to_graph($graphid,$row2["itemid"],$gitem["color"],$gitem["drawtype"],$gitem["sortorder"]); } } } 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) { $description=addslashes($description); $sql="update media_type set type=$type,description='$description',smtp_server='$smtp_server',smtp_helo='$smtp_helo',smtp_email='$smtp_email',exec_path='$exec_path' where mediatypeid=$mediatypeid"; return DBexecute($sql); } # Add Media type function add_mediatype($type,$description,$smtp_server,$smtp_helo,$smtp_email,$exec_path) { $description=addslashes($description); $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')"; return DBexecute($sql); } # Add Media definition function add_media( $userid, $mediatypeid, $sendto, $severity, $active) { $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) values ($userid,'$mediatypeid','$sendto',$active,$s)"; return DBexecute($sql); } # Update Media definition function update_media($mediaid, $userid, $mediatypeid, $sendto, $severity, $active) { $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 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) { 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"; 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); } function delete_user_group($usrgrpid) { $sql="delete from users_groups where usrgrpid=$usrgrpid"; DBexecute($sql); $sql="delete from usrgrp where usrgrpid=$usrgrpid"; 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 ""; cr(); // echo " "; cr(); } function show_table2_h_delimiter() { // echo ""; cr(); echo ""; cr(); // echo ""; echo ""; cr(); // echo " "; cr(); } function show_table3_h_delimiter($width=10) { ?> "; cr(); echo ""; cr(); cr(); */ } function show_table_v_delimiter($colspan=1) { // echo ""; cr(); echo ""; cr(); echo ""; cr(); echo ""; cr(); echo ""; cr(); // echo ""; cr(); } function show_table2_v_delimiter($rownum=0) { // echo ""; cr(); echo ""; cr(); echo ""; cr(); if($rownum%2 == 1) { echo ""; } else { echo ""; } cr(); // echo ""; echo ""; cr(); // echo ""; cr(); } function show_table3_v_delimiter() { ?> "; cr(); echo ""; cr(); echo ""; cr(); echo ""; cr(); cr();*/ } function show_table2_v_delimiter2() { // echo ""; cr(); echo ""; cr(); echo ""; cr(); echo ""; cr(); // echo ""; echo ""; cr(); // echo ""; cr(); } // function show_table2_header_begin() function show_form_begin($help="") { ?>

"; echo ""; echo "
\"?\""; } } function show_table_header_begin() { echo ""; cr(); echo ""; cr(); echo ""; cr(); echo ""; cr(); echo "
"; cr(); echo ""; cr(); echo ""; cr(); // echo ""; cr(); echo ""; cr(); echo "
"; echo ""; cr(); // echo " "; cr(); } function show_header2($h1, $h2, $h2_form1, $h2_form2) { ?> "; } function show_table2_header_end() { // cr(); // echo ""; // cr(); // echo ""; // cr(); // echo ""; cr(); echo "
"; // echo "

"; cr(); } function show_table_header_end() { /* cr(); echo "
"; cr();*/ echo "
"; } function show_table_header($title) { ?> "; echo ""; echo ""; echo "Year"; echo ""; echo "Month"; echo ""; echo "Day"; echo ""; echo "Hour"; echo ""; echo "Period:"; echo ""; echo ""; echo ""; } # Show History Graph function show_history($itemid,$from,$period,$diff) { if (!isset($from)) { $from=0; $till="NOW"; } else { $till=time(NULL)-$from*3600; $till=date(S_DATE_FORMAT_YMDHMS,$till); } if (!isset($period)) { $period=3600; show_table_header("TILL $till (LAST HOUR)"); } else { $tmp=$period/3600; show_table_header("TILL $till ($tmp HOURs)"); } // echo("


"); echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
"; if($diff==0) { echo ""; } else { echo ""; } echo "
"; echo "
"; echo("
"); insert_time_navigator($itemid,$period,$from); echo("
"); } # Show history function show_freehist($itemid,$period) { show_form_begin("history.period"); echo "Choose period"; show_table2_v_delimiter(); echo "
"; echo ""; echo "Period in seconds"; show_table2_h_delimiter(); echo ""; show_table2_v_delimiter(); echo "From (in hours)"; show_table2_h_delimiter(); echo ""; show_table2_v_delimiter2(); echo "Press "; echo " to see values in plain text"; show_table2_header_end(); show_footer(); } # Show in plain text function show_plaintxt($itemid,$period) { show_form_begin("history.plaim"); echo "Data in plain text format"; show_table2_v_delimiter(); echo ""; echo ""; echo ""; echo "From: (yyyy/mm/dd - HH:MM)"; show_table2_h_delimiter(); echo "/"; echo "/"; echo " - "; echo ":"; echo ""; show_table2_v_delimiter(); echo "Till: (yyyy/mm/dd - HH:MM)"; show_table2_h_delimiter(); echo "/"; echo "/"; echo " - "; echo ":"; echo ""; show_table2_v_delimiter2(); echo "Press to see data in "; echo ""; show_table2_header_end(); show_footer(); } function show_footer() { global $USER_DETAILS; ?>

=$period_start and clock<=$period_end"; } // echo $sql,"
"; $result=DBselect($sql); if(DBget_field($result,0,0)>0) { $min=DBget_field($result,0,1); $max=DBget_field($result,0,2); } else { if(($period_start==0)&&($period_end==0)) { $max=time(); $min=$max-24*3600; } else { $ret["true_time"]=0; $ret["false_time"]=0; $ret["unknown_time"]=0; $ret["true"]=0; $ret["false"]=0; $ret["unknown"]=100; return $ret; } } $sql="select clock,value from alarms where triggerid=$triggerid and clock>=$min and clock<=$max"; // echo " $sql
"; $result=DBselect($sql); // echo $sql,"
"; // -1,0,1 $state=-1; $true_time=0; $false_time=0; $unknown_time=0; $time=$min; if(($period_start==0)&&($period_end==0)) { $max=time(); } for($i=0;$i"; echo "function Confirm(msg)"; echo "{"; echo " if(confirm( msg))"; echo " {"; echo " return true;"; echo " }"; echo " else"; echo " {"; echo " return false;"; echo " }"; echo "}"; echo ""; } /* Use ImageSetStyle+ImageLIne instead of bugged ImageDashedLine */ if(function_exists("imagesetstyle")) { function DashedLine($image,$x1,$y1,$x2,$y2,$color) { // Style for dashed lines // $style = array($color, $color, $color, $color, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT); $style = array($color, $color, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT); ImageSetStyle($image, $style); ImageLine($image,$x1,$y1,$x2,$y2,IMG_COLOR_STYLED); } } else { function DashedLine($image,$x1,$y1,$x2,$y2,$color) { ImageDashedLine($image,$x1,$y1,$x2,$y2,$color); } } function time_navigator($resource="graphid",$id) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
"; echo "
"; echo "".S_PERIOD.": "; $hour=3600; $a=array(S_1H=>3600,S_2H=>2*3600,S_4H=>4*3600,S_8H=>8*3600,S_12H=>12*3600, S_24H=>24*3600,S_WEEK_SMALL=>7*24*3600,S_MONTH_SMALL=>31*24*3600,S_YEAR_SMALL=>365*24*3600); foreach($a as $label=>$sec) { echo "["; if($_GET["period"]>$sec) { $tmp=$_GET["period"]-$sec; echo("-"); } else { echo "-"; } echo(""); echo($label.""); $tmp=$_GET["period"]+$sec; echo("+"); echo "] "; } echo("
"); echo "
"; echo "".nbsp(S_KEEP_PERIOD).": "; if($_GET["keep"] == 1) { echo("[".S_ON_C."]"); } else { echo("[".S_OFF_C."]"); } echo "
"; if(isset($_GET["stime"])) { echo "
" ; echo "".S_MOVE.": " ; $day=24; // $a already defined // $a=array("1h"=>1,"2h"=>2,"4h"=>4,"8h"=>8,"12h"=>12, // "24h"=>24,"week"=>7*24,"month"=>31*24,"year"=>365*24); foreach($a as $label=>$hours) { echo "["; $stime=$_GET["stime"]; $tmp=mktime(substr($stime,8,2),substr($stime,10,2),0,substr($stime,4,2),substr($stime,6,2),substr($stime,0,4)); $tmp=$tmp-3600*$hours; $tmp=date("YmdHi",$tmp); echo("-"); echo($label); $stime=$_GET["stime"]; $tmp=mktime(substr($stime,8,2),substr($stime,10,2),0,substr($stime,4,2),substr($stime,6,2),substr($stime,0,4)); $tmp=$tmp+3600*$hours; $tmp=date("YmdHi",$tmp); echo("+"); echo "] "; } echo("
"); } else { echo "
"; echo "".S_MOVE.": "; $day=24; // $a already defined // $a=array("1h"=>1,"2h"=>2,"4h"=>4,"8h"=>8,"12h"=>12, // "24h"=>24,"week"=>7*24,"month"=>31*24,"year"=>365*24); foreach($a as $label=>$hours) { echo "["; $tmp=$_GET["from"]+$hours; echo("-"); echo($label); if($_GET["from"]>=$hours) { $tmp=$_GET["from"]-$hours; echo("+"); } else { echo "+"; } echo "] "; } echo("
"); } echo "
"; // echo("
"); echo ""; echo ""; echo ""; if(isset($_GET["stime"])) { echo ""; } else { echo ""; } echo " "; echo ""; echo ""; // echo("
"); echo "
"; } function ImageOut($image) { // ImageJPEG($image); ImagePNG($image); } function update_user_groups($usrgrpid,$users) { $count=count($users); $sql="delete from users_groups where usrgrpid=$usrgrpid"; DBexecute($sql); for($i=0;$i<$count;$i++) { $sql="insert into users_groups (usrgrpid,userid) values ($usrgrpid,".$users[$i].")"; DBexecute($sql); } } ?>