diff options
| author | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-07-05 09:22:34 +0000 |
|---|---|---|
| committer | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-07-05 09:22:34 +0000 |
| commit | 0f93047219d365318ef9feb7005403b1c3155bfa (patch) | |
| tree | fa08172aad7936d7ab9834f8252432e5f5e59d94 /frontends/php/include | |
| parent | 51edf7af1a0eee6ddfe22e62d846abeb8c43bcc3 (diff) | |
| download | zabbix-0f93047219d365318ef9feb7005403b1c3155bfa.tar.gz zabbix-0f93047219d365318ef9feb7005403b1c3155bfa.tar.xz zabbix-0f93047219d365318ef9feb7005403b1c3155bfa.zip | |
- added frontend-side support of quotes and triger expression validation (Eugene)
- merged rev. 4305:4307 [improved trigger expression syntax and validation]
- merged rev. 4329:4331 [more support for quotes in key parameters]
- merged rev. 4344:4346 [minor improovments]
- merged rev. 4431:4432 [added availability of using ":," character for key]
git-svn-id: svn://svn.zabbix.com/trunk@4437 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'frontends/php/include')
| -rw-r--r-- | frontends/php/include/acknow.inc.php | 2 | ||||
| -rw-r--r-- | frontends/php/include/defines.inc.php | 60 | ||||
| -rw-r--r-- | frontends/php/include/graphs.inc.php | 33 | ||||
| -rw-r--r-- | frontends/php/include/hosts.inc.php | 327 | ||||
| -rw-r--r-- | frontends/php/include/httptest.inc.php | 2 | ||||
| -rw-r--r-- | frontends/php/include/items.inc.php | 171 | ||||
| -rw-r--r-- | frontends/php/include/nodes.inc.php | 20 | ||||
| -rw-r--r-- | frontends/php/include/triggers.inc.php | 910 |
8 files changed, 1081 insertions, 444 deletions
diff --git a/frontends/php/include/acknow.inc.php b/frontends/php/include/acknow.inc.php index e4dcfa18..36ffdb5a 100644 --- a/frontends/php/include/acknow.inc.php +++ b/frontends/php/include/acknow.inc.php @@ -28,7 +28,7 @@ return $event_data; } - function get_acknowledges_by_eventid($eventid) + function &get_acknowledges_by_eventid($eventid) { return DBselect("select * from acknowledges where eventid=$eventid"); } diff --git a/frontends/php/include/defines.inc.php b/frontends/php/include/defines.inc.php index 59043c76..37ed61ab 100644 --- a/frontends/php/include/defines.inc.php +++ b/frontends/php/include/defines.inc.php @@ -217,14 +217,14 @@ define('ACTION_STATUS_ENABLED',0); define('ACTION_STATUS_DISABLED',1); - define('OPERATION_TYPE_MESSAGE', 0); /* !!!!! */ - define('OPERATION_TYPE_COMMAND', 1); /* !!!!! */ - define('OPERATION_TYPE_HOST_ADD', 2); /* TODO!!! */ - define('OPERATION_TYPE_HOST_REMOVE', 3); /* TODO!!! */ - define('OPERATION_TYPE_GROUP_ADD', 4); /* TODO!!! */ - define('OPERATION_TYPE_GROUP_REMOVE', 5); /* TODO!!! */ - define('OPERATION_TYPE_TEMPLATE_ADD', 6); /* TODO!!! */ - define('OPERATION_TYPE_TEMPLATE_REMOVE',7); /* TODO!!! */ + define('OPERATION_TYPE_MESSAGE', 0); + define('OPERATION_TYPE_COMMAND', 1); + define('OPERATION_TYPE_HOST_ADD', 2); + define('OPERATION_TYPE_HOST_REMOVE', 3); + define('OPERATION_TYPE_GROUP_ADD', 4); + define('OPERATION_TYPE_GROUP_REMOVE', 5); + define('OPERATION_TYPE_TEMPLATE_ADD', 6); + define('OPERATION_TYPE_TEMPLATE_REMOVE',7); define('ACTION_EVAL_TYPE_AND_OR',0); define('ACTION_EVAL_TYPE_AND',1); @@ -383,17 +383,39 @@ define('SPACE', ' '); define('RARR', '⇒'); - define('ZBX_EREG_HOST_FORMAT', '([0-9a-zA-Z\_\.[:space:][.-.]\$]+)'); - define('ZBX_EREG_NODE_FORMAT', '([0-9a-zA-Z\_\.[:space:][.-.]\$]+)'); - define('ZBX_EREG_ITEM_KEY_FORMAT', '([]\[\'0-9a-zA-Z!\_\*\&/\.\,\:\(\)\+ [.-.]\$%]+)'); - - define('ZBX_EREG_SIMPLE_EXPRESSION_FORMAT', - '^\{'.ZBX_EREG_HOST_FORMAT.'\:'.ZBX_EREG_ITEM_KEY_FORMAT.'\.([a-z]{3,11})\(([#0-9a-zA-Z\_\/\.\,[:space:]]+)\)\}$'); - - define('ZBX_SIMPLE_EXPRESSION_HOST_ID', 1); - define('ZBX_SIMPLE_EXPRESSION_KEY_ID', 2); - define('ZBX_SIMPLE_EXPRESSION_FUNCTION_ID', 3); - define('ZBX_SIMPLE_EXPRESSION_PARAMETER_ID', 4); + define('ZBX_EREG_INTERNAL_NAMES', '([0-9a-zA-Z_.[:space:][.-.]$]+)'); + define('ZBX_EREG_KEY_NAME', '([0-9a-zA-Z_.,[.:.][:space:][.-.]$]+)'); + define('ZBX_EREG_PARAMS', '([[:print:]]+){0,1}'); + define('ZBX_EREG_SIGN', '([&|><=+*/#[.-.]])'); + define('ZBX_EREG_NUMBER', '([[.-.]+]*[0-9]+[.]{0,1}[0-9]*[A-Z]{0,1})'); + + /* Character '-' must be last in the list of symbols, otherwise it won't be accepted */ + define('ZBX_EREG_DNS_FORMAT', '([0-9a-zA-Z\_\.\$[.-.]]+)'); + define('ZBX_EREG_HOST_FORMAT', ZBX_EREG_INTERNAL_NAMES); + define('ZBX_EREG_NODE_FORMAT', ZBX_EREG_INTERNAL_NAMES); + define('ZBX_EREG_ITEM_KEY_FORMAT', '('.ZBX_EREG_KEY_NAME.'(\['.ZBX_EREG_PARAMS.'\]){0,1})'); + define('ZBX_KEY_ID', 1); + define('ZBX_KEY_NAME_ID', 2); + define('ZBX_KEY_PARAM_ID', 4); + define('ZBX_EREG_FUNCTION_FORMAT', '('.ZBX_EREG_INTERNAL_NAMES.'(\('.ZBX_EREG_PARAMS.'\)))'); + + define('ZBX_EREG_SIMPLE_EXPRESSION_FORMAT','(\{'.ZBX_EREG_HOST_FORMAT.'\:'.ZBX_EREG_ITEM_KEY_FORMAT.'\.'.ZBX_EREG_FUNCTION_FORMAT.'\})'); + define('ZBX_EREG_MACRO_NAME_FORMAT', '(\{[A-Z\.]+\})'); + + define('ZBX_SIMPLE_EXPRESSION_HOST_ID', 2); + define('ZBX_SIMPLE_EXPRESSION_KEY_ID', 2 + ZBX_KEY_ID); + define('ZBX_SIMPLE_EXPRESSION_KEY_NAME_ID', 2 + ZBX_KEY_NAME_ID); + define('ZBX_SIMPLE_EXPRESSION_KEY_PARAM_ID', 2 + ZBX_KEY_PARAM_ID); + define('ZBX_SIMPLE_EXPRESSION_FUNCTION_ID', 7); + define('ZBX_SIMPLE_EXPRESSION_FUNCTION_NAME_ID', 8); + define('ZBX_SIMPLE_EXPRESSION_FUNCTION_PARAM_ID', 10); + + define('ZBX_EREG_EXPRESSION_TOKEN_FORMAT', '^([[:print:]]*)('.ZBX_EREG_SIMPLE_EXPRESSION_FORMAT.'|'.ZBX_EREG_MACRO_NAME_FORMAT.')([[:print:]]*)$'); + + define('ZBX_EXPRESSION_LEFT_ID', 1); + define('ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID', 2); + define('ZBX_EXPRESSION_MACRO_ID', 13); + define('ZBX_EXPRESSION_RIGHT_ID', 14); define('ZBX_MIN_PERIOD', 3600); define('ZBX_MAX_PERIOD', 12*31*24*3600); diff --git a/frontends/php/include/graphs.inc.php b/frontends/php/include/graphs.inc.php index ce1596fe..85e9ac8e 100644 --- a/frontends/php/include/graphs.inc.php +++ b/frontends/php/include/graphs.inc.php @@ -80,13 +80,13 @@ } - function get_graphs_by_hostid($hostid) + function &get_graphs_by_hostid($hostid) { return DBselect("select distinct g.* from graphs g, graphs_items gi, items i". " where g.graphid=gi.graphid and gi.itemid=i.itemid and i.hostid=$hostid"); } - function get_realhosts_by_graphid($graphid) + function &get_realhosts_by_graphid($graphid) { $graph = get_graph_by_graphid($graphid); if($graph["templateid"] != 0) @@ -95,13 +95,13 @@ return get_hosts_by_graphid($graphid); } - function get_hosts_by_graphid($graphid) + function &get_hosts_by_graphid($graphid) { return DBselect("select distinct h.* from graphs_items gi, items i, hosts h". " where h.hostid=i.hostid and gi.itemid=i.itemid and gi.graphid=$graphid"); } - function get_graphitems_by_graphid($graphid) + function &get_graphitems_by_graphid($graphid) { return DBselect("select * from graphs_items where graphid=$graphid". " order by itemid,drawtype,sortorder,color,yaxisside"); @@ -143,7 +143,7 @@ return false; } - function get_graphs_by_templateid($templateid) + function &get_graphs_by_templateid($templateid) { return DBselect("select * from graphs where templateid=$templateid"); } @@ -446,7 +446,7 @@ * Comments: !!! Don't forget sync code with C !!! * * * ******************************************************************************/ - function delete_template_graphs($hostid, $templateid = null /* array format 'arr[id]=name' */, $unlink_mode = false) + function delete_template_graphs($hostid, $templateid = null, $unlink_mode = false) { $db_graphs = get_graphs_by_hostid($hostid); while($db_graph = DBfetch($db_graphs)) @@ -454,16 +454,13 @@ if($db_graph["templateid"] == 0) continue; - if($templateid != null) + if( !is_null($templateid) ) { - $hosts = get_hosts_by_graphid($db_graph["templateid"]); - $tmp_host = DBfetch($hosts); - if(is_array($templateid)) - { - if(!isset($templateid[$tmp_host["hostid"]])) - continue; - } - elseif($tmp_host["hostid"] != $templateid) + if( !is_array($templateid) ) $templateid=array($templateid); + + $tmp_host = DBfetch(get_hosts_by_graphid($db_graph["templateid"])); + + if( !in_array($tmp_host["hostid"], $templateid)) continue; } @@ -486,16 +483,16 @@ * Comments: !!! Don't forget sync code with C !!! * * * ******************************************************************************/ - function copy_template_graphs($hostid, $templateid = null /* array format 'arr[id]=name' */, $copy_mode = false) + function copy_template_graphs($hostid, $templateid = null, $copy_mode = false) { if($templateid == null) { - $templateid = get_templates_by_hostid($hostid); + $templateid = array_keys(get_templates_by_hostid($hostid)); } if(is_array($templateid)) { - foreach($templateid as $id => $name) + foreach($templateid as $id) copy_template_graphs($hostid, $id, $copy_mode); // attention recursion return; } diff --git a/frontends/php/include/hosts.inc.php b/frontends/php/include/hosts.inc.php index 04fa2422..ed015f75 100644 --- a/frontends/php/include/hosts.inc.php +++ b/frontends/php/include/hosts.inc.php @@ -47,6 +47,18 @@ require_once "include/items.inc.php"; return DBexecute('delete from hosts_groups where hostid='.$hostid.' and groupid='.$groupid); } + /* + * Function: db_save_group + * + * Description: + * Add new or update host group + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function db_save_group($name,$groupid=null) { if(!is_string($name)){ @@ -131,7 +143,20 @@ require_once "include/items.inc.php"; return $result; } -/* HOST finction */ + /* + * Function: check_circle_host_link + * + * Description: + * Check templates linage circeling + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + * NOTE: templates = array(id => name, id2 => name2, ...) + * + */ function check_circle_host_link($hostid, $templates) { if(count($templates) == 0) return false; @@ -143,20 +168,33 @@ require_once "include/items.inc.php"; return false; } + /* + * Function: db_save_host + * + * Description: + * Add or update host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * if hostid is NULL add new host, in other cases update + * + * NOTE: templates = array(id => name, id2 => name2, ...) + */ function db_save_host($host,$port,$status,$useip,$dns,$ip,$templates,$hostid=null) { global $ZBX_CURNODEID; if( !eregi('^'.ZBX_EREG_HOST_FORMAT.'$', $host) ) { - error("Hostname should contain '0-9a-zA-Z_. $'- characters only"); + error("Incorrect characters used for Hostname"); return false; } -/* Character '-' must be last in the list of symbols, otherwise it won't be accepted */ - if ( !empty($dns) && !eregi('^([0-9a-zA-Z\_\.\$[.-.]]+)$', $dns)) + if ( !empty($dns) && !eregi('^'.ZBX_EREG_DNS_FORMAT.'$', $dns)) { - error("DNS should contain '0-9a-zA-Z_.$'- characters only"); + error("Incorrect characters used for DNS"); return false; } @@ -205,6 +243,19 @@ require_once "include/items.inc.php"; return $result; } + /* + * Function: add_host + * + * Description: + * Add new host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + * NOTE: templates = array(id => name, id2 => name2, ...) + */ function add_host($host,$port,$status,$useip,$dns,$ip,$templates,$newgroup,$groups) { $hostid = db_save_host($host,$port,$status,$useip,$dns,$ip,$templates); @@ -224,6 +275,19 @@ require_once "include/items.inc.php"; return $hostid; } + /* + * Function: update_host + * + * Description: + * Update host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + * NOTE: templates = array(id => name, id2 => name2, ...) + */ function update_host($hostid,$host,$port,$status,$useip,$dns,$ip,$templates,$newgroup,$groups) { $old_templates = get_templates_by_hostid($hostid); @@ -247,17 +311,24 @@ require_once "include/items.inc.php"; if(count($new_templates) > 0) { - sync_host_with_templates($hostid,$new_templates); + sync_host_with_templates($hostid,array_keys($new_templates)); } return $result; } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: unlink_template + * + * Description: + * Unlink elements from host by template + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function unlink_template($hostid, $templateid, $unlink_mode = true) { if( !is_numeric($templateid) ) fatal_error('Not supported type for [templateid] in [unlink_template] - ['.$templateid.']'); @@ -266,11 +337,18 @@ require_once "include/items.inc.php"; DBexecute("delete from hosts_templates where hostid=".$hostid.' and templateid='.$templateid); } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: delete_template_elements + * + * Description: + * Delete all elements from host by template + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function delete_template_elements($hostid, $templateid = null, $unlink_mode = false) { delete_template_graphs($hostid, $templateid, $unlink_mode); @@ -279,11 +357,18 @@ require_once "include/items.inc.php"; delete_template_applications($hostid, $templateid, $unlink_mode); } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: copy_template_elements + * + * Description: + * Copy all elements from template to host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function copy_template_elements($hostid, $templateid = null, $copy_mode = false) { copy_template_applications($hostid, $templateid, $copy_mode); @@ -292,12 +377,18 @@ require_once "include/items.inc.php"; copy_template_graphs($hostid, $templateid, $copy_mode); } -# Sync host with linked template - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: sync_host_with_templates + * + * Description: + * Synchronize template elements with host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function sync_host_with_templates($hostid, $templateid = null) { delete_template_elements($hostid, $templateid); @@ -323,13 +414,18 @@ require_once "include/items.inc.php"; } } - # Delete Host - - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: delete_host + * + * Description: + * Delete host with all elements and relations + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function delete_host($hostid, $unlink_mode = false) { global $DB_TYPE; @@ -451,6 +547,18 @@ require_once "include/items.inc.php"; } } + /* + * Function: get_templates_by_hostid + * + * Description: + * Retrive templates for specified host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function get_templates_by_hostid($hostid) { $resuilt = array(); @@ -464,6 +572,18 @@ require_once "include/items.inc.php"; return $resuilt; } + /* + * Function: get_correct_group_and_host + * + * Description: + * Retrive correct relations for group and host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function get_correct_group_and_host($a_groupid=null, $a_hostid=null, $perm=PERM_READ_WRITE, $options = array()) { if(!is_array($options)) @@ -587,6 +707,19 @@ require_once "include/items.inc.php"; ); } + /* + * Function: validate_group_with_host + * + * Description: + * Check available groups and host by user permission + * and check current group an host relations + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function validate_group_with_host($perm, $options = array(),$group_var=null,$host_var=null) { if(is_null($group_var)) $group_var = "web.latest.groupid"; @@ -618,6 +751,18 @@ require_once "include/items.inc.php"; update_profile($group_var,$_REQUEST["groupid"]); } + /* + * Function: validate_group + * + * Description: + * Check available groups by user permisions + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function validate_group($perm, $options = array(),$group_var=null) { if(is_null($group_var)) $group_var = "web.latest.groupid"; @@ -633,11 +778,18 @@ require_once "include/items.inc.php"; /* APPLICATIONS */ - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: db_save_application + * + * Description: + * Add or update application + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * If applicationid is NULL add application, in other cases update + */ function db_save_application($name,$hostid,$applicationid=null,$templateid=0) { if(!is_string($name)){ @@ -716,21 +868,49 @@ require_once "include/items.inc.php"; return false; } + + /* + * Function: add_application + * + * Description: + * Add application + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + */ function add_application($name,$hostid,$templateid=0) { return db_save_application($name,$hostid,null,$templateid); } + /* + * Function: update_application + * + * Description: + * Update application + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + */ function update_application($applicationid,$name,$hostid,$templateid=0) { return db_save_application($name,$hostid,$applicationid,$templateid); } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: delete_application + * + * Description: + * Delete application with all linkages + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function delete_application($applicationid) { $app = get_application_by_applicationid($applicationid); @@ -816,14 +996,23 @@ require_once "include/items.inc.php"; function &get_applications_by_hostid($hostid) { - return DBselect("select * from applications where hostid=$hostid"); + return DBselect('select * from applications where hostid='.$hostid); } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: delete_template_applications + * + * Description: + * Delete applicatios from host by templates + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + * $templateid can be numeric or numeric array + * + */ function delete_template_applications($hostid, $templateid = null, $unlink_mode = false) { $db_apps = get_applications_by_hostid($hostid); @@ -864,21 +1053,30 @@ require_once "include/items.inc.php"; } } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ - function copy_template_applications($hostid, $templateid = null /* array format 'arr[id]=name' */, $copy_mode = false) + /* + * Function: copy_template_applications + * + * Description: + * Copy applicatios from templates to host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + * $templateid can be numeric or numeric array + * + */ + function copy_template_applications($hostid, $templateid = null, $copy_mode = false) { if(null == $templateid) { - $templateid = get_templates_by_hostid($hostid); + $templateid = array_keys(get_templates_by_hostid($hostid)); } if(is_array($templateid)) { - foreach($templateid as $id => $name) + foreach($templateid as $id) copy_template_applications($hostid, $id, $copy_mode); // attention recursion return; } @@ -894,6 +1092,19 @@ require_once "include/items.inc.php"; } } + /* + * Function: validate_templates + * + * Description: + * Check collisions between templates + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * $templateid_list can be numeric or numeric array + * + */ function validate_templates($templateid_list) { if(is_numeric($templateid_list))return true; diff --git a/frontends/php/include/httptest.inc.php b/frontends/php/include/httptest.inc.php index 728eef06..2d2415fa 100644 --- a/frontends/php/include/httptest.inc.php +++ b/frontends/php/include/httptest.inc.php @@ -389,7 +389,7 @@ return DBfetch(DBselect('select * from httptest where httptestid='.$httptestid)); } - function get_httpsteps_by_httptestid($httptestid) + function &get_httpsteps_by_httptestid($httptestid) { return DBselect('select * from httpstep where httptestid='.$httptestid); } diff --git a/frontends/php/include/items.inc.php b/frontends/php/include/items.inc.php index cb9eab8e..495056ed 100644 --- a/frontends/php/include/items.inc.php +++ b/frontends/php/include/items.inc.php @@ -20,6 +20,18 @@ ?> <?php + /* + * Function: item_type2str + * + * Description: + * Represent integer value of item type as string + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function item_type2str($type) { switch($type) @@ -41,6 +53,18 @@ return $type; } + /* + * Function: item_value_type2str + * + * Description: + * Represent integer value of item value type as string + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function item_value_type2str($value_type) { switch($value_type) @@ -55,6 +79,18 @@ return $value_type; } + /* + * Function: item_value_type2str + * + * Description: + * Represent integer value of item status as string + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function item_status2str($status) { switch($status) @@ -68,6 +104,18 @@ return $status; } + /* + * Function: item_status2style + * + * Description: + * Represent integer value of item status as CSS style name + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function item_status2style($status) { switch($status) @@ -151,7 +199,7 @@ if( !eregi('^'.ZBX_EREG_ITEM_KEY_FORMAT.'$', $key) ) { - error("Key should contain '[]0-9a-zA-Z!_,:()+.*\ $'- characters only"); + error("Incorrect key format 'key_name[param1,param2,...]'"); return false; } @@ -312,6 +360,12 @@ if(($i = array_search(0,$applications)) !== FALSE) unset($applications[$i]); + if( !eregi('^'.ZBX_EREG_ITEM_KEY_FORMAT.'$', $key) ) + { + error("Incorrect key format 'key_name[param1,param2,...]'"); + return false; + } + if($delay<1) { error("Delay cannot be less than 1 second"); @@ -397,6 +451,18 @@ return $result; } + /* + * Function: smart_update_item + * + * Description: + * Update specified fields of item + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function smart_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, @@ -458,12 +524,19 @@ $item_data['templateid']); } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ - function delete_template_items($hostid, $templateid = null /* array format 'arr[id]=name' */, $unlink_mode = false) + /* + * Function: delete_template_items + * + * Description: + * Delete items from host by templateid + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ + function delete_template_items($hostid, $templateid = null, $unlink_mode = false) { $db_items = get_items_by_hostid($hostid); while($db_item = DBfetch($db_items)) @@ -471,18 +544,14 @@ if($db_item["templateid"] == 0) continue; - if($templateid != null) + if( !is_null($templateid)) { + if ( !is_array($templateid) ) $templateid = array($templateid); + $db_tmp_item = get_item_by_itemid($db_item["templateid"]); - if(is_array($templateid)) - { - if(!isset($templateid[$db_tmp_item["hostid"]])) - continue; - } - elseif($db_tmp_item["hostid"] != $templateid) - { + + if ( !in_array($db_tmp_item["hostid"], $templateid) ) continue; - } } if($unlink_mode) @@ -499,6 +568,18 @@ } } + /* + * Function: copy_item_to_host + * + * Description: + * Copy specified item to the host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function copy_item_to_host($itemid, $hostid, $copy_mode = false) { $db_tmp_item = get_item_by_itemid($itemid); @@ -532,21 +613,28 @@ $copy_mode ? 0 : $db_tmp_item["itemid"]); } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ - function copy_template_items($hostid, $templateid = null /* array format 'arr[id]=name' */, $copy_mode = false) + /* + * Function: copy_template_items + * + * Description: + * Copy items from template to the host + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ + function copy_template_items($hostid, $templateid = null, $copy_mode = false) { if($templateid == null) { - $templateid = get_templates_by_hostid($hostid); + $templateid = array_keys(get_templates_by_hostid($hostid)); } if(is_array($templateid)) { - foreach($templateid as $id => $name) + foreach($templateid as $id) copy_template_items($hostid, $id, $copy_mode); // attention recursion return; } @@ -679,15 +767,32 @@ return $result; } + /* + * Function: get_n_param + * + * Description: + * Return key parameter by index + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: indexes between 1-x + * + */ function get_n_param($key, $num) { $param=""; - $params = preg_split('/[\]\[,]/', $key); + $num--; - if(isset($params[$num])) + if( ereg('^'.ZBX_EREG_ITEM_KEY_FORMAT.'$', $key, $arr) ) { - $param = $params[$num]; + $params = zbx_get_params($arr[ZBX_KEY_PARAM_ID]); + + if(isset($params[$num])) + { + $param = $params[$num]; + } } return $param; @@ -714,6 +819,18 @@ return get_host_by_itemid($itemid); } + /* + * Function: get_items_data_overview + * + * Description: + * Retrive overview table object for items + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function get_items_data_overview($groupid, $nodeid) { global $USER_DETAILS; diff --git a/frontends/php/include/nodes.inc.php b/frontends/php/include/nodes.inc.php index c1e8d6b2..b8e14035 100644 --- a/frontends/php/include/nodes.inc.php +++ b/frontends/php/include/nodes.inc.php @@ -50,11 +50,11 @@ { global $ZBX_CURNODEID, $ZBX_CURMASTERID; - if( !eregi('^'.ZBX_EREG_NODE_FORMAT.'$', $name) ) - { - error("Incorrect characters used for Node name"); - return false; - } + if( !eregi('^'.ZBX_EREG_NODE_FORMAT.'$', $name) ) + { + error("Incorrect characters used for Node name"); + return false; + } switch($node_type) { @@ -103,11 +103,11 @@ function update_node($nodeid,$new_nodeid,$name,$timezone,$ip,$port,$slave_history,$slave_trends) { - if( !eregi('^'.ZBX_EREG_NODE_FORMAT.'$', $name) ) - { - error("Incorrect characters used for Node name"); - return false; - } + if( !eregi('^'.ZBX_EREG_NODE_FORMAT.'$', $name) ) + { + error("Incorrect characters used for Node name"); + return false; + } $result = DBexecute('update nodes set nodeid='.$new_nodeid.',name='.zbx_dbstr($name).','. 'timezone='.$timezone.',ip='.zbx_dbstr($ip).',port='.$port.','. diff --git a/frontends/php/include/triggers.inc.php b/frontends/php/include/triggers.inc.php index d3761b16..f1d22e39 100644 --- a/frontends/php/include/triggers.inc.php +++ b/frontends/php/include/triggers.inc.php @@ -22,6 +22,162 @@ require_once "maps.inc.php"; require_once "acknow.inc.php"; + /* + * Function: INIT_TRIGGER_EXPRESSION_STRUCTURES + * + * Description: + * initialize structures for trigger expression + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ + function INIT_TRIGGER_EXPRESSION_STRUCTURES() + { + if ( defined('TRIGGER_EXPRESSION_STRUCTURES_OK') ) return; + define('TRIGGER_EXPRESSION_STRUCTURES_OK', 1); + + global $ZBX_TR_EXPR_ALLOWED_MACROS, $ZBX_TR_EXPR_REPLACE_TO, $ZBX_TR_EXPR_ALLOWED_FUNCTIONS; + + $ZBX_TR_EXPR_ALLOWED_MACROS['{TRIGGER.VALUE}'] = '{TRIGGER.VALUE}'; + + $ZBX_TR_EXPR_REPLACE_TO = 'zbx_expr_ok'; + + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['abschange'] = array('args' => null, + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64, + ITEM_VALUE_TYPE_STR, + ITEM_VALUE_TYPE_TEXT + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['avg'] = array('args' => array( 0 => array('type' => 'sec_num','mandat' => true) ), + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64 + ), + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['delta'] = array('args' => array( 0 => array('type' => 'sec_num','mandat' => true) ), + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64 + ), + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['change'] = array('args' => null, + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64, + ITEM_VALUE_TYPE_STR, + ITEM_VALUE_TYPE_TEXT + ), + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['count'] = array('args' => array( 0 => array('type' => 'sec','mandat' => true), 1 => array('type' => 'str') ), + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64, + ITEM_VALUE_TYPE_LOG, + ITEM_VALUE_TYPE_STR, + ITEM_VALUE_TYPE_TEXT + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['date'] = array('args' => null, 'item_types' => null ); + + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['dayofweek']= array('args' => null, 'item_types' => null ); + + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['diff'] = array('args' => null, + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64, + ITEM_VALUE_TYPE_STR, + ITEM_VALUE_TYPE_TEXT + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['fuzzytime'] = array('args' => null, + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64 + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['last'] = array('args' => null, + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64, + ITEM_VALUE_TYPE_STR, + ITEM_VALUE_TYPE_TEXT + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['max'] = array('args' => array( 0 => array('type' => 'sec_num','mandat' => true) ), + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64 + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['min'] = array('args' => array( 0 => array('type' => 'sec_num','mandat' => true) ), + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64 + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['nodata']= array('args' => array( 0 => array('type' => 'sec','mandat' => true) ), 'item_types' => null ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['now'] = array('args' => null, 'item_types' => null ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['prev'] = array('args' => null, + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64, + ITEM_VALUE_TYPE_STR, + ITEM_VALUE_TYPE_TEXT + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['str'] = array('args' => array( 0 => array('type' => 'str','mandat' => true) ), + 'item_types' => array( + ITEM_VALUE_TYPE_STR, + ITEM_VALUE_TYPE_LOG + ) + ); + + + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['sum'] = array('args' => array( 0 => array('type' => 'sec_num','mandat' => true) ), + 'item_types' => array( + ITEM_VALUE_TYPE_FLOAT, + ITEM_VALUE_TYPE_UINT64 + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['logseverity']= array('args' => null, + 'item_types' => array( + ITEM_VALUE_TYPE_LOG + ) + ); + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['logsource']= array('args' => array( 0=> array('type' => 'str','mandat' => true) ), + 'item_types' => array( + ITEM_VALUE_TYPE_LOG + ) + ); + + $ZBX_TR_EXPR_ALLOWED_FUNCTIONS['regexp']= array('args' => array( 0 => array('type' => 'str','mandat' => true) ), + 'item_types' => array( + ITEM_VALUE_TYPE_STR, + ITEM_VALUE_TYPE_LOG + ) + ); + } + + INIT_TRIGGER_EXPRESSION_STRUCTURES(); + + + /* + * Function: get_severity_style + * + * Description: + * convert severity constant in to the CSS style name + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function get_severity_style($severity) { if($severity == TRIGGER_SEVERITY_INFORMATION) return 'information'; @@ -33,6 +189,18 @@ return ''; } + /* + * Function: get_severity_description + * + * Description: + * convert severity constant in to the string representation + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function get_severity_description($severity) { if($severity == TRIGGER_SEVERITY_NOT_CLASSIFIED) return S_NOT_CLASSIFIED; @@ -45,6 +213,18 @@ return S_UNKNOWN; } + /* + * Function: get_trigger_value_style + * + * Description: + * convert trigger value in to the CSS style name + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function get_trigger_value_style($value) { $str_val[TRIGGER_VALUE_FALSE] = 'off'; @@ -57,6 +237,18 @@ return ''; } + /* + * Function: trigger_value2str + * + * Description: + * convert trigger value in to the string representation + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function trigger_value2str($value) { $str_val[TRIGGER_VALUE_FALSE] = S_FALSE_BIG; @@ -69,11 +261,23 @@ return S_UNKNOWN; } + /* + * Function: get_realhosts_by_triggerid + * + * Description: + * retrive real host for trigger + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function get_realhosts_by_triggerid($triggerid) { $trigger = get_trigger_by_triggerid($triggerid); - if($trigger["templateid"] > 0) - return get_realhosts_by_triggerid($trigger["templateid"]); + if($trigger['templateid'] > 0) + return get_realhosts_by_triggerid($trigger['templateid']); return get_hosts_by_triggerid($triggerid); } @@ -91,18 +295,30 @@ return FALSE; } - function get_hosts_by_triggerid($triggerid) + function &get_hosts_by_triggerid($triggerid) { - return DBselect("select distinct h.* from hosts h, functions f, items i". - " where i.itemid=f.itemid and h.hostid=i.hostid and f.triggerid=$triggerid"); + return DBselect('select distinct h.* from hosts h, functions f, items i'. + ' where i.itemid=f.itemid and h.hostid=i.hostid and f.triggerid='.$triggerid); } - function get_functions_by_triggerid($triggerid) + function &get_functions_by_triggerid($triggerid) { - return DBselect("select * from functions where triggerid=$triggerid"); + return DBselect('select * from functions where triggerid='.$triggerid); } - function get_triggers_by_hostid($hostid, $show_mixed = "yes") + /* + * Function: get_triggers_by_hostid + * + * Description: + * retrive selection of triggers by hostid + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ + function &get_triggers_by_hostid($hostid, $show_mixed = "yes") { $db_triggers = DBselect("select distinct t.* from triggers t, functions f, items i". " where i.hostid=$hostid and f.itemid=i.itemid and f.triggerid=t.triggerid"); @@ -127,40 +343,44 @@ return DBselect($sql); } - function get_triggers_by_templateid($triggerid) + function &get_triggers_by_templateid($triggerid) { - return DBselect("select * from triggers where templateid=$triggerid"); + return DBselect('select * from triggers where templateid='.$triggerid); } - function get_hosts_by_expression($expression) + /* + * Function: get_hosts_by_expression + * + * Description: + * retrive selection of hosts by trigger expression + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ + function &get_hosts_by_expression($expression) { - global $ZBX_CURNODEID; + global $ZBX_CURNODEID, $ZBX_TR_EXPR_ALLOWED_MACROS, $ZBX_TR_EXPR_REPLACE_TO; - $state=""; - $host=""; - $hosts=array(); - for($i=0,$max=strlen($expression); $i<$max; $i++) - { - if($expression[$i] == '{' && $state=="") - { - $host=""; - $state='HOST'; - continue; - } + $expr = $expression; - if($expression[$i] == ':' && $state == "HOST") + $hosts = array(); + + /* Replace all {server:key.function(param)} and {MACRO} with '$ZBX_TR_EXPR_REPLACE_TO' */ + while(ereg(ZBX_EREG_EXPRESSION_TOKEN_FORMAT, $expr, $arr)) + { + if ( $arr[ZBX_EXPRESSION_MACRO_ID] && !isset($ZBX_TR_EXPR_ALLOWED_MACROS[$arr[ZBX_EXPRESSION_MACRO_ID]]) ) { - $state=""; - $hosts[$host] = '\''.$host.'\''; - $host = ''; - continue; + $hosts = array('0'); + break; } - - if($state == "HOST") + else if( !$arr[ZBX_EXPRESSION_MACRO_ID] ) { - $host .= $expression[$i]; - continue; + $hosts[] = zbx_dbstr($arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_HOST_ID]); } + $expr = $arr[ZBX_EXPRESSION_LEFT_ID].$ZBX_TR_EXPR_REPLACE_TO.$arr[ZBX_EXPRESSION_RIGHT_ID]; } if(count($hosts) == 0) $hosts = array('0'); @@ -169,231 +389,246 @@ ' and host in ('.implode(',',$hosts).')'); } -// Does expression match server:key.function(param) ? - function validate_simple_expression($expression) + /* + * Function: zbx_unquote_param + * + * Description: + * unquote string and unescape cahrs + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * Double quotes used only. + * Unquote string only if value directly in quotes. + * Unescape only '\\' and '\"' combination + * + */ + function zbx_unquote_param($value) { - global $ZBX_CURNODEID; - - $allowed_functions['min'] = 'ticks'; - $allowed_functions['max'] = 'ticks'; - $allowed_functions['delta'] = 'ticks'; - $allowed_functions['avg'] = 'ticks'; - $allowed_functions['sum'] = 'ticks'; - $allowed_functions['last'] = 'float'; - $allowed_functions['diff'] = 'float'; - $allowed_functions['count'] = array('float', ''); - $allowed_functions['prev'] = 'float'; - $allowed_functions['change'] = 'float'; - $allowed_functions['abschange'] = 'float'; - $allowed_functions['nodata'] = 'float'; - $allowed_functions['time'] = 'float'; - $allowed_functions['dayofweek'] = 'float'; - $allowed_functions['date'] = 'float'; - $allowed_functions['now'] = 'float'; - $allowed_functions['fuzzytime'] = 'float'; - $allowed_functions['str'] = ''; - $allowed_functions['logseverity'] = ''; - $allowed_functions['logsource'] = ''; - $allowed_functions['regexp'] = ''; - -// echo "Validating simple:$expression<br>"; - if (eregi(ZBX_EREG_SIMPLE_EXPRESSION_FORMAT, $expression, $arr)) - { - $host = &$arr[ZBX_SIMPLE_EXPRESSION_HOST_ID]; - $key = &$arr[ZBX_SIMPLE_EXPRESSION_KEY_ID]; - $function = &$arr[ZBX_SIMPLE_EXPRESSION_FUNCTION_ID]; - $parameter = &$arr[ZBX_SIMPLE_EXPRESSION_PARAMETER_ID]; - - - $row=DBfetch(DBselect('select count(*) as cnt from hosts h where h.host='.zbx_dbstr($host). - ' and '.DBid2nodeid('h.hostid').'='.$ZBX_CURNODEID - )); - if($row["cnt"]==0) - { - error('No such host ('.$host.')'); - return -1; - } - elseif($row["cnt"]!=1) - { - error('Too many hosts ('.$host.')'); - return -1; - } + $value = trim($value); + if ( !empty($value) && '"' == $value[0] ) + { /* open quotes and unescape chars */ + $value = substr($value, 1, strlen($value)-2); - $row=DBfetch(DBselect('select count(*) as cnt from hosts h,items i where h.host='.zbx_dbstr($host). - ' and i.key_='.zbx_dbstr($key).' and h.hostid=i.hostid '. - ' and '.DBid2nodeid('h.hostid').'='.$ZBX_CURNODEID - )); - if($row["cnt"]==0) - { - error('No such monitored parameter ('.$key.') for host ('.$host.')'); - return -1; - } - elseif($row["cnt"]!=1) + $new_val = ''; + for ( $i=0, $max=strlen($value); $i < $max; $i++) { - error('Too many monitored parameter ('.$key.') for host ('.$host.')'); - return -1; - } - - if( !isset($allowed_functions[$function]) ) - { - error('Unknown function ['.$function.']'); - return -1; + if ( $i+1 < $max && $value[$i] == '\\' && ($value[$i+1] == '\\' || $value[$i+1] == '"') ) + $new_val .= $value[++$i]; + else + $new_val .= $value[$i]; } + $value = $new_val; + } + return $value; + } - if( !is_array($allowed_functions[$function]) ) - $allowed_functions[$function] = array($allowed_functions[$function]); - - $parameter = split(',', $parameter, count($allowed_functions[$function])); + /* + * Function: zbx_get_params + * + * Description: + * parse list of quoted parameters + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * Double quotes used only. + * + */ + function zbx_get_params($string) + { + $params = array(); + $quoted = false; - foreach($allowed_functions[$function] as $pid => $params) + for( $param_s = $i = 0, $len = strlen($string); $i < $len; $i++) + { + switch ( $string[$i] ) { - if(!isset($parameter[$pid])) continue; - - if( 'float' == $params - && (validate_float($parameter[$pid])!=0) ) - { - error('['.$parameter[$pid].'] is not a float'); - return -1; - } - - if( 'ticks' == $params - && (validate_ticks($parameter[$pid])!=0) ) - { - error('['.$parameter[$pid].'] is not a float'); - return -1; - } + case '"': + $quoted = !$quoted; + break; + case ',': + if ( !$quoted ) + { + $params[] = zbx_unquote_param(substr($string, $param_s, $i - $param_s)); + $param_s = $i+1; + } + break; + case '\\': + if ( $quoted && $i+1 < $len && ($string[$i+1] == '\\' || $string[$i+1] == '"')) + $i++; + break; } } - # Process macros - else if($expression!="{TRIGGER.VALUE}") - + + if( $quoted ) { - error('Expression ['.$expression.'] does not match to [server:key.func(param)]'); - return -1; + error('Incorrect usage of quotes. ['.$string.']'); + return null; } - return 0; + + if( $i > $param_s ) + { + $params[] = zbx_unquote_param(substr($string, $param_s, $i - $param_s)); + } + + return $params; } + /* + * Function: validate_expression + * + * Description: + * check trigger expression syntax and validate values + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: + * + */ function validate_expression($expression) { -// echo "Validating expression: $expression<br>"; - $exp_hosts = get_hosts_by_expression($expression); + global $ZBX_CURNODEID, $ZBX_TR_EXPR_ALLOWED_MACROS, $ZBX_TR_EXPR_REPLACE_TO, $ZBX_TR_EXPR_ALLOWED_FUNCTIONS; - $ok=0; -// Replace all {server:key.function(param)} with 0 - while($ok==0) + if( empty($expression) ) { -// echo "Expression:$expression<br>"; - $arr=""; - if (eregi('^((.)*)[ ]*(\{((.)*)\})[ ]*((.)*)$', $expression, $arr)) - { - if(validate_simple_expression($arr[3])!=0) - { - return -1; - } - $expression=$arr[1]."0".$arr[6]; - } - else - { - $ok=1; - } + error('Expression can\'t be empty'); } -// echo "Result:$expression<br><hr>"; + + $expr = $expression; + $h_status = array(); - $ok=0; - while($ok==0) + /* Replace all {server:key.function(param)} and {MACRO} with '$ZBX_TR_EXPR_REPLACE_TO' */ + while(ereg(ZBX_EREG_EXPRESSION_TOKEN_FORMAT, $expr, $arr)) { -// Replace all <float> <sign> <float> <K|M|G> with 0 -// echo "Expression:$expression<br>"; - $arr=""; - if (eregi('^((.)*)([0-9\.]+[A-Z]{0,1})[ ]*([\&\|\>\<\=\+\*\/\#[.-.]]{1})[ ]*([0-9\.]+[A-Z]{0,1})((.)*)$', $expression, $arr)) + if ( $arr[ZBX_EXPRESSION_MACRO_ID] && !isset($ZBX_TR_EXPR_ALLOWED_MACROS[$arr[ZBX_EXPRESSION_MACRO_ID]]) ) { -// echo "OK<br>"; -// for($i=0;$i<50;$i++) -// { -// if($arr[$i]!="") -// echo " $i: ",$arr[$i],"<br>"; -// } - if(validate_float($arr[3])!=0) + error('Unknown macro ['.$arr[ZBX_EXPRESSION_MACRO_ID].']'); + return false; + } + else if( !$arr[ZBX_EXPRESSION_MACRO_ID] ) + { + $host = &$arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_HOST_ID]; + $key = &$arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_KEY_ID]; + $function = &$arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_NAME_ID]; + $parameter = &$arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_PARAM_ID]; + + /* Check host */ + $row=DBfetch(DBselect('select count(*) as cnt,min(status) as status,min(hostid) as hostid from hosts h where h.host='.zbx_dbstr($host). + ' and '.DBid2nodeid('h.hostid').'='.$ZBX_CURNODEID + )); + if($row['cnt']==0) { - error("[".$arr[3]."] is not a float"); - return -1; + error('No such host ('.$host.')'); + return false; } - if(validate_float($arr[5])!=0) + elseif($row['cnt']!=1) { - error("[".$arr[5]."] is not a float"); - return -1; + error('Too many hosts ('.$host.')'); + return false; } - $expression=$arr[1]."(0)".$arr[6]; - } - else - { - $ok=1; - } + $h_status[$row['status']][$row['hostid']] = $row['cnt']; -// Replace all (float) with 0 -// echo "Expression2:[$expression]<br>"; - $arr=""; - if (eregi('^((.)*)(\(([ 0-9\.]+)\))((.)*)$', $expression, $arr)) - { -// echo "OK<br>"; -// for($i=0;$i<30;$i++) -// { -// if($arr[$i]!="") -// echo " $i: ",$arr[$i],"<br>"; -// } - if(validate_float($arr[4])!=0) + /* Check key */ + if ( !($item = DBfetch(DBselect('select i.itemid,i.value_type from hosts h,items i where h.host='.zbx_dbstr($host). + ' and i.key_='.zbx_dbstr($key).' and h.hostid=i.hostid '. + ' and '.DBid2nodeid('h.hostid').'='.$ZBX_CURNODEID + ))) ) { - error("[".$arr[4]."] is not a float"); - return -1; + error('No such monitored parameter ('.$key.') for host ('.$host.')'); + return false; + } + + /* Check function */ + if( !isset($ZBX_TR_EXPR_ALLOWED_FUNCTIONS[$function]) ) + { + error('Unknown function ['.$function.']'); + return false; } - $expression=$arr[1]."0".$arr[5]; - $ok=0; - } - else - { - $ok=1; - } + $fnc_valid = &$ZBX_TR_EXPR_ALLOWED_FUNCTIONS[$function]; + if ( is_array($fnc_valid['item_types']) && + !in_array($item['value_type'], $fnc_valid['item_types'])) + { + $allowed_types = array(); + foreach($fnc_valid['item_types'] as $type) + $allowed_types[] = item_value_type2str($type); + info('Function ('.$function.') available only for items with value types ['.implode(',',$allowed_types).']'); + error('Incorrect value type ['.item_value_type2str($item['value_type']).'] for function ('.$function.') of key ('.$host.':'.$key.')'); + return false; + } + + if( !is_null($fnc_valid['args']) ) + { + $parameter = zbx_get_params($parameter); + + if( !is_array($fnc_valid['args']) ) + $fnc_valid['args'] = array($fnc_valid['args']); + + foreach($fnc_valid['args'] as $pid => $params) + { + if(!isset($parameter[$pid])) + { + if( !isset($params['mandat']) ) + { + continue; + } + else + { + error('Missed mandatory parameter for function ('.$function.')'); + return false; + } + } + + if( 'sec' == $params['type'] + && (validate_float($parameter[$pid])!=0) ) + { + error('['.$parameter[$pid].'] is not a float for function ('.$function.')'); + return false; + } + if( 'sec_num' == $params['type'] + && (validate_ticks($parameter[$pid])!=0) ) + { + error('['.$parameter[$pid].'] is not a float or counter for function ('.$function.')'); + return false; + } + } + } + } + $expr = $arr[ZBX_EXPRESSION_LEFT_ID].$ZBX_TR_EXPR_REPLACE_TO.$arr[ZBX_EXPRESSION_RIGHT_ID]; } - $exp_host = DBfetch($exp_hosts); - if(!$exp_host) + if ( isset($h_status[HOST_STATUS_TEMPLATE]) && ( count($h_status) > 1 || count($h_status[HOST_STATUS_TEMPLATE]) > 1 )) { - error("Incorrect trigger expression. Incorrect host is used."); - return 1; + error("Incorrect trigger expression. You can't use template hosts". + " in mixed expressions."); + return false; } - else - { - $rows=0; - unset($fail); - do - { - if($exp_host["status"]==HOST_STATUS_TEMPLATE) - { - $fail=1; - } - $rows++; - } while($exp_host = DBfetch($exp_hosts)); - if(isset($fail) && ($rows>1)) - { - error("Incorrect trigger expression. You can't use template hosts". - " in mixed expressions."); - return 1; - } + /* Replace all calculations and numbers with '$ZBX_TR_EXPR_REPLACE_TO' */ + $expt_number = '('.$ZBX_TR_EXPR_REPLACE_TO.'|'.ZBX_EREG_NUMBER.')'; + $expt_term = '((\('.$expt_number.'\))|('.$expt_number.'))'; + $expr_format = '(('.$expt_term.ZBX_EREG_SIGN.$expt_term.')|(\('.$expt_term.'\)))'; + $expr_full_format = '((\('.$expr_format.'\))|('.$expr_format.'))'; + + while($res = ereg($expr_full_format.'([[:print:]]*)$', $expr, $arr)) + { + $expr = substr($expr, 0, strpos($expr, $arr[1])).$ZBX_TR_EXPR_REPLACE_TO.$arr[54]; } - if($expression=="0") + if ( $ZBX_TR_EXPR_REPLACE_TO != $expr ) { - return 0; + error('Incorrect trigger expression. ['.str_replace($ZBX_TR_EXPR_REPLACE_TO, ' ... ', $expr).']'); + return false; } - error("Incorrect trigger expression '$expression'"); - return 1; + return true; } @@ -401,7 +636,8 @@ $expression, $description, $priority, $status, $comments, $url, $deps=array(), $templateid=0) { - if(!is_null($expression)) if(validate_expression($expression)) return FALSE; + if( !validate_expression($expression) ) + return false; $triggerid=get_dbid("triggers","triggerid"); @@ -667,49 +903,62 @@ return $exp; } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: implode_exp + * + * Description: + * Translate localhost:procload.last(0)>10 to {12}>10 + * And create database representation. + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function implode_exp ($expression, $triggerid) - # Translate localhost:procload.last(0)>10 to {12}>10 { + global $ZBX_TR_EXPR_ALLOWED_MACROS, $ZBX_TR_EXPR_REPLACE_TO; + $expr = $expression; $short_exp = $expression; - for($pos=0,$max=strlen($expression); $pos<$max; ) + /* Replace all {server:key.function(param)} and {MACRO} with '$ZBX_TR_EXPR_REPLACE_TO' */ + /* build short expression {12}>10 */ + while(ereg(ZBX_EREG_EXPRESSION_TOKEN_FORMAT, $expr, $arr)) { - if ( false === ($simple_start = strpos($expression, '{', $pos)) ) break; - if ( false === ($simple_end = strpos($expression, '}', $simple_start)) ) break; - - $pos = $simple_end; - - $simple_exp = substr($expression, $simple_start, $simple_end - $simple_start + 1); - - if (!eregi(ZBX_EREG_SIMPLE_EXPRESSION_FORMAT, $simple_exp, $arr)) continue; - - $host = &$arr[ZBX_SIMPLE_EXPRESSION_HOST_ID]; - $key = &$arr[ZBX_SIMPLE_EXPRESSION_KEY_ID]; - $function = &$arr[ZBX_SIMPLE_EXPRESSION_FUNCTION_ID]; - $parameter = &$arr[ZBX_SIMPLE_EXPRESSION_PARAMETER_ID]; - - $row=DBfetch(DBselect("select i.itemid from items i,hosts h". - " where i.key_=".zbx_dbstr($key). - " and h.host=".zbx_dbstr($host). - " and h.hostid=i.hostid")); - - $itemid=$row["itemid"]; + if ( $arr[ZBX_EXPRESSION_MACRO_ID] && !isset($ZBX_TR_EXPR_ALLOWED_MACROS[$arr[ZBX_EXPRESSION_MACRO_ID]]) ) + { + error('[ie] Unknown macro ['.$arr[ZBX_EXPRESSION_MACRO_ID].']'); + return false; + } + else if( !$arr[ZBX_EXPRESSION_MACRO_ID] ) + { + $s_expr = &$arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID]; + $host = &$arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_HOST_ID]; + $key = &$arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_KEY_ID]; + $function = &$arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_NAME_ID]; + $parameter = &$arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_PARAM_ID]; - unset($row); + $item = DBfetch(DBselect('select i.itemid from items i,hosts h'. + ' where i.key_='.zbx_dbstr($key). + ' and h.host='.zbx_dbstr($host). + ' and h.hostid=i.hostid')); - $functionid = get_dbid("functions","functionid"); - if ( !DBexecute("insert into functions (functionid,itemid,triggerid,function,parameter)". - " values ($functionid,$itemid,$triggerid,".zbx_dbstr($function).",". - zbx_dbstr($parameter).")")) return NULL; + $item = $item["itemid"]; - $short_exp = str_replace($simple_exp, '{'.$functionid.'}', $short_exp); + $functionid = get_dbid("functions","functionid"); - unset($simple_exp); + if ( !DBexecute('insert into functions (functionid,itemid,triggerid,function,parameter)'. + ' values ('.$functionid.','.$item.','.$triggerid.','.zbx_dbstr($function).','. + zbx_dbstr($parameter).')')) + { + return null; + } + $short_exp = str_replace($s_expr,'{'.$functionid.'}',$short_exp); + $expr = str_replace($s_expr,$ZBX_TR_EXPR_REPLACE_TO,$expr); + continue; + } + $expr = $arr[ZBX_EXPRESSION_LEFT_ID].$ZBX_TR_EXPR_REPLACE_TO.$arr[ZBX_EXPRESSION_RIGHT_ID]; } return $short_exp; @@ -911,41 +1160,43 @@ if(is_null($expression)) { + /* Restore expression */ $expression = explode_exp($trigger["expression"],0); } - else - { - if(validate_expression($expression)) - return FALSE; - } - $exp_hosts = get_hosts_by_expression($expression); - $chd_hosts = get_hosts_by_templateid($trig_host["hostid"]); + if ( !validate_expression($expression) ) + return false; - if(DBfetch($chd_hosts)) + $exp_hosts = get_hosts_by_expression($expression); + if( $exp_hosts ) { - $exp_host = DBfetch($exp_hosts); - $db_chd_triggers = get_triggers_by_templateid($triggerid); - while($db_chd_trigger = DBfetch($db_chd_triggers)) + $chd_hosts = get_hosts_by_templateid($trig_host["hostid"]); + + if(DBfetch($chd_hosts)) { - $chd_trig_hosts = get_hosts_by_triggerid($db_chd_trigger["triggerid"]); - $chd_trig_host = DBfetch($chd_trig_hosts); - - $newexpression = str_replace( - "{".$exp_host["host"].":", - "{".$chd_trig_host["host"].":", - $expression); - // recursion - update_trigger( - $db_chd_trigger["triggerid"], - $newexpression, - $description, - $priority, - NULL, // status - $comments, - $url, - replace_template_dependences($deps, $chd_trig_host['hostid']), - $triggerid); + $exp_host = DBfetch($exp_hosts); + $db_chd_triggers = get_triggers_by_templateid($triggerid); + while($db_chd_trigger = DBfetch($db_chd_triggers)) + { + $chd_trig_hosts = get_hosts_by_triggerid($db_chd_trigger["triggerid"]); + $chd_trig_host = DBfetch($chd_trig_hosts); + + $newexpression = str_replace( + "{".$exp_host["host"].":", + "{".$chd_trig_host["host"].":", + $expression); + // recursion + update_trigger( + $db_chd_trigger["triggerid"], + $newexpression, + $description, + $priority, + NULL, // status + $comments, + $url, + replace_template_dependences($deps, $chd_trig_host['hostid']), + $triggerid); + } } } @@ -1120,11 +1371,18 @@ return TRUE; } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: cmp_triggers + * + * Description: + * compate triggers by expression + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function cmp_triggers($triggerid1, $triggerid2) // compare EXPRESSION !!! { $trig1 = get_trigger_by_triggerid($triggerid1); @@ -1155,12 +1413,19 @@ return strcmp($expr1,$trig2["expression"]); } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ - function delete_template_triggers($hostid, $templateid = null /* array format 'arr[id]=name' */, $unlink_mode = false) + /* + * Function: delete_template_triggers + * + * Description: + * Delete template triggers + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ + function delete_template_triggers($hostid, $templateid = null, $unlink_mode = false) { $triggers = get_triggers_by_hostid($hostid); while($trigger = DBfetch($triggers)) @@ -1169,15 +1434,14 @@ if($templateid != null) { + if( !is_array($templateid)) + $templateid = array($templateid); + $db_tmp_hosts = get_hosts_by_triggerid($trigger["templateid"]); $tmp_host = DBfetch($db_tmp_hosts); - if(is_array($templateid)) - { - if(!isset($templateid[$tmp_host["hostid"]])) - continue; - } - elseif($tmp_host["hostid"] != $templateid) - continue; + + if( !in_array($tmp_host["hostid"], $templateid) ) + continue; } if($unlink_mode) @@ -1196,21 +1460,28 @@ return TRUE; } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ - function copy_template_triggers($hostid, $templateid = null /* array format 'arr[id]=name' */, $copy_mode = false) + /* + * Function: copy_template_triggers + * + * Description: + * Copy triggers from template + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ + function copy_template_triggers($hostid, $templateid = null, $copy_mode = false) { if(null == $templateid) { - $templateid = get_templates_by_hostid($hostid);; + $templateid = array_keys(get_templates_by_hostid($hostid)); } if(is_array($templateid)) { - foreach($templateid as $id => $name) + foreach($templateid as $id) copy_template_triggers($hostid, $id, $copy_mode); // attention recursion return; } @@ -1224,11 +1495,18 @@ update_template_dependences_for_host($hostid); } - /****************************************************************************** - * * - * Comments: !!! Don't forget sync code with C !!! * - * * - ******************************************************************************/ + /* + * Function: update_template_dependences_for_host + * + * Description: + * Update template triggers + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function update_template_dependences_for_host($hostid) { $db_triggers = get_triggers_by_hostid($hostid); @@ -1251,6 +1529,18 @@ } } + /* + * Function: get_triggers_overview + * + * Description: + * Retrive table with overview of triggers + * + * Author: + * Eugene Grigorjev (eugene.grigorjev@zabbix.com) + * + * Comments: !!! Don't forget sync code with C !!! + * + */ function get_triggers_overview($groupid, $nodeid) { global $USER_DETAILS; |
