From b205072539c6f5a77b7fdc69bd3b8387314576da Mon Sep 17 00:00:00 2001 From: artem Date: Tue, 1 Jul 2008 14:29:51 +0000 Subject: - [DEV-184] added trigger dependencies in export/import (Artem) git-svn-id: svn://svn.zabbix.com/trunk@5808 97f52cf1-0a1b-0410-bd0e-c28be96e8082 --- frontends/php/include/defines.inc.php | 3 + frontends/php/include/export.inc.php | 236 +++++++++++++++++++++------------ frontends/php/include/hosts.inc.php | 13 +- frontends/php/include/import.inc.php | 129 +++++++++++++----- frontends/php/include/triggers.inc.php | 97 ++++++++------ 5 files changed, 320 insertions(+), 158 deletions(-) (limited to 'frontends') diff --git a/frontends/php/include/defines.inc.php b/frontends/php/include/defines.inc.php index 2b11a011..122fbb2b 100644 --- a/frontends/php/include/defines.inc.php +++ b/frontends/php/include/defines.inc.php @@ -534,6 +534,9 @@ else{ define('XML_TAG_SCREEN', 'screen'); define('XML_TAG_SCREEN_ELEMENT', 'screen_element'); define('XML_TAG_SCREEN_ELEMENTS', 'screen_elements'); + define('XML_TAG_DEPENDENCIES', 'dependencies'); + define('XML_TAG_DEPENDENCY', 'dependency'); + define('XML_TAG_DEPENDS', 'depends'); /* Support for PHP5. PHP5 does not have $HTTP_..._VARS */ if (!function_exists('version_compare')) diff --git a/frontends/php/include/export.inc.php b/frontends/php/include/export.inc.php index 84b0c52d..a4e3649c 100644 --- a/frontends/php/include/export.inc.php +++ b/frontends/php/include/export.inc.php @@ -34,6 +34,12 @@ 'port' => '', 'status' => '') ), + XML_TAG_DEPENDENCY => array( + 'attribures' => array( + 'dependency' => 'description'), + 'elements' => array( + 'depends' => '') + ), // based on mod by scricca XML_TAG_HOSTPROFILE => array( 'attribures' => array(), @@ -118,58 +124,50 @@ ) ); - function zbx_xmlwriter_open_memory() - { + function zbx_xmlwriter_open_memory(){ return array('tabs' => 0, 'tag'=>array()); } - function zbx_xmlwriter_set_indent($mem, $val) - { + function zbx_xmlwriter_set_indent($mem, $val){ return true; } - function zbx_xmlwriter_start_document(&$mem, $ver) - { + function zbx_xmlwriter_start_document(&$mem, $ver){ echo ''."\n".str_repeat("\t",$mem['tabs']).'<'.$tag; $mem['tabs']++; return true; } - function zbx_xmlwriter_write_attribute(&$mem, $name, $val) - { + + function zbx_xmlwriter_write_attribute(&$mem, $name, $val){ echo ' '.$name.'="'.htmlspecialchars($val).'"'; return true; } - function zbx_xmlwriter_end_element(&$mem) - { + function zbx_xmlwriter_end_element(&$mem){ $teg = array_pop($mem['tag']); $mem['tabs']--; echo '>'."\n".str_repeat("\t",$mem['tabs']).''; } - function zbx_xmlwriter_write_element(&$mem, $name, $val) - { + function zbx_xmlwriter_write_element(&$mem, $name, $val){ echo '>'."\n".str_repeat("\t",$mem['tabs']).'<'.$name.'>'.htmlspecialchars($val).' $xml_name) - { + foreach($map['attribures'] as $db_name => $xml_name){ if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]); } - foreach($map['elements'] as $db_name => $xml_name) - { + + foreach($map['elements'] as $db_name => $xml_name){ if(!isset($data[$db_name])) continue; if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]); } - if( !empty($data['valuemapid']) && $valuemap = DBfetch(DBselect('select name from valuemaps where valuemapid='.$data['valuemapid']))) - { + if( !empty($data['valuemapid']) && $valuemap = DBfetch(DBselect('select name from valuemaps where valuemapid='.$data['valuemapid']))){ zbx_xmlwriter_write_element($memory, 'valuemap', $valuemap['name']); } $db_applications=DBselect('select distinct a.name from applications a,items_applications ia '. ' where ia.applicationid=a.applicationid and ia.itemid='.$itemid); - if ($application = DBfetch($db_applications)) - { + if($application = DBfetch($db_applications)){ zbx_xmlwriter_start_element ($memory,XML_TAG_APPLICATIONS); do { zbx_xmlwriter_write_element ($memory, XML_TAG_APPLICATION, $application['name']); @@ -211,8 +206,7 @@ } - function export_trigger(&$memory, $triggerid) - { + function export_trigger(&$memory, $triggerid){ global $ZBX_EXPORT_MAP; $data = DBfetch(DBselect('select * from triggers where triggerid='.$triggerid)); @@ -224,13 +218,12 @@ $map =& $ZBX_EXPORT_MAP[XML_TAG_TRIGGER]; - foreach($map['attribures'] as $db_name => $xml_name) - { + foreach($map['attribures'] as $db_name => $xml_name){ if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]); } - foreach($map['elements'] as $db_name => $xml_name) - { + + foreach($map['elements'] as $db_name => $xml_name){ if(!isset($data[$db_name])) continue; if(empty($xml_name)) $xml_name = $db_name; @@ -239,12 +232,16 @@ zbx_xmlwriter_end_element($memory); // XML_TAG_TRIGGER } - function export_graph_element(&$memory, $gitemid) - { + function export_graph_element(&$memory, $gitemid){ global $ZBX_EXPORT_MAP; - $data = DBfetch(DBselect('select gi.*,i.key_,h.host from graphs_items gi, items i, hosts h'. - ' where h.hostid=i.hostid and i.itemid=gi.itemid and gi.gitemid='.$gitemid)); + $sql = 'select gi.*,i.key_,h.host '. + ' from graphs_items gi, items i, hosts h'. + ' where h.hostid=i.hostid '. + ' and i.itemid=gi.itemid '. + ' and gi.gitemid='.$gitemid; + + $data = DBfetch(DBselect($sql)); if(!$data) return false; $data['item'] = '{HOSTNAME}:'.$data['key_']; @@ -253,13 +250,12 @@ $map =& $ZBX_EXPORT_MAP[XML_TAG_GRAPH_ELEMENT]; - foreach($map['attribures'] as $db_name => $xml_name) - { + foreach($map['attribures'] as $db_name => $xml_name){ if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]); } - foreach($map['elements'] as $db_name => $xml_name) - { + + foreach($map['elements'] as $db_name => $xml_name){ if(!isset($data[$db_name])) continue; if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]); @@ -267,8 +263,7 @@ zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPH_ELEMENT } - function export_graph(&$memory, $graphid) - { + function export_graph(&$memory, $graphid){ global $ZBX_EXPORT_MAP; $data = DBfetch(DBselect('select * from graphs where graphid='.$graphid)); @@ -278,21 +273,21 @@ $map =& $ZBX_EXPORT_MAP[XML_TAG_GRAPH]; - foreach($map['attribures'] as $db_name => $xml_name) - { + foreach($map['attribures'] as $db_name => $xml_name){ if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]); } - foreach($map['elements'] as $db_name => $xml_name) - { + + foreach($map['elements'] as $db_name => $xml_name){ if(!isset($data[$db_name])) continue; if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]); } + zbx_xmlwriter_start_element ($memory,XML_TAG_GRAPH_ELEMENTS); + $db_elements = DBselect('select gitemid from graphs_items where graphid='.$graphid); - while($element = DBfetch($db_elements)) - { + while($element = DBfetch($db_elements)){ $this->export_graph_element($memory, $element['gitemid']); } @@ -300,8 +295,7 @@ zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPH } - function export_host(&$memory, $hostid, $export_templates, $export_items, $export_triggers, $export_graphs) - { + function export_host(&$memory, $hostid, $export_templates, $export_items, $export_triggers, $export_graphs){ global $ZBX_EXPORT_MAP; $data = DBfetch(DBselect('select * from hosts where hostid='.$hostid)); @@ -311,13 +305,12 @@ $map =& $ZBX_EXPORT_MAP[XML_TAG_HOST]; - foreach($map['attribures'] as $db_name => $xml_name) - { + foreach($map['attribures'] as $db_name => $xml_name){ if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]); } - foreach($map['elements'] as $db_name => $xml_name) - { + + foreach($map['elements'] as $db_name => $xml_name){ if(!isset($data[$db_name])) continue; if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]); @@ -335,86 +328,133 @@ if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]); } + foreach($map['elements'] as $db_name => $xml_name){ if(empty($data[$db_name])) continue; if(empty($xml_name)) $xml_name = $db_name; zbx_xmlwriter_write_element($memory, $xml_name, $data[$db_name]); } + zbx_xmlwriter_end_element($memory); // XML_TAG_HOSTPROFILE } //-- - - if($db_groups = DBselect('select g.name from groups g, hosts_groups hg'. - ' where g.groupid=hg.groupid and hg.hostid='.$hostid)) - { + $sql = 'select g.name '. + ' from groups g, hosts_groups hg '. + ' where g.groupid=hg.groupid '. + ' and hg.hostid='.$hostid; + if($db_groups = DBselect()){ zbx_xmlwriter_start_element ($memory,XML_TAG_GROUPS); - while($group = DBfetch($db_groups)) - { + + while($group = DBfetch($db_groups)){ zbx_xmlwriter_write_element ($memory, XML_TAG_GROUP, $group['name']); } + zbx_xmlwriter_end_element($memory); // XML_TAG_GROUP } if($export_templates){ - $query = 'SELECT t.host '. - ' FROM hosts t, hosts_templates ht'. - ' WHERE t.hostid=ht.templateid AND ht.hostid='.$hostid; + $sql = 'SELECT t.host '. + ' FROM hosts t, hosts_templates ht '. + ' WHERE t.hostid=ht.templateid '. + ' AND ht.hostid='.$hostid; - if($db_templates = DBselect($query)){ + if($db_templates = DBselect($sql)){ zbx_xmlwriter_start_element ($memory,XML_TAG_TEMPLATES); while($template = DBfetch($db_templates)){ zbx_xmlwriter_write_element ($memory, XML_TAG_TEMPLATE, $template['host']); } } + zbx_xmlwriter_end_element($memory); // XML_TAG_TEMPLATES } - if($export_items) - { + if($export_items){ zbx_xmlwriter_start_element ($memory,XML_TAG_ITEMS); + $db_items=DBselect('select itemid from items where hostid='.$hostid); - while($item_id = DBfetch($db_items)) - { + while($item_id = DBfetch($db_items)){ $this->export_item($memory, $item_id['itemid']); } + zbx_xmlwriter_end_element($memory); // XML_TAG_ITEMS } - if($export_triggers) - { + if($export_triggers){ zbx_xmlwriter_start_element ($memory,XML_TAG_TRIGGERS); + $db_triggers = DBselect('select f.triggerid, i.hostid, count(distinct i.hostid) as cnt '. ' from functions f, items i '. - ' where f.itemid=i.itemid group by f.triggerid, i.hostid'); - while($trigger = DBfetch($db_triggers)) - { + ' where f.itemid=i.itemid '. + ' group by f.triggerid, i.hostid'); + while($trigger = DBfetch($db_triggers)){ if((bccomp($trigger['hostid'] , $hostid) != 0) || $trigger['cnt']!=1) continue; $this->export_trigger($memory, $trigger['triggerid']); } + zbx_xmlwriter_end_element($memory); // XML_TAG_TRIGGERS } - if($export_graphs) - { + if($export_graphs){ zbx_xmlwriter_start_element ($memory, XML_TAG_GRAPHS); + $db_graphs = DBselect('select gi.graphid, i.hostid, count(distinct i.hostid) as cnt '. ' from graphs_items gi, items i '. - ' where gi.itemid=i.itemid group by gi.graphid, i.hostid'); - while($graph = DBfetch($db_graphs)) - { + ' where gi.itemid=i.itemid '. + ' group by gi.graphid, i.hostid'); + while($graph = DBfetch($db_graphs)){ if((bccomp($graph['hostid'] , $hostid) != 0) || $graph['cnt']!=1) continue; $this->export_graph($memory, $graph['graphid']); } + zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPHS } /* export screens */ zbx_xmlwriter_end_element($memory); // XML_TAG_HOST return true; } + + function export_dependency(&$memory, $triggerid, $host, $description){ + global $ZBX_EXPORT_MAP; + if(!$triggerid) return false; + + $data['dependency'] = $host.':'.$description; + + zbx_xmlwriter_start_element ($memory,XML_TAG_DEPENDENCY); + + $map =& $ZBX_EXPORT_MAP[XML_TAG_DEPENDENCY]; + + foreach($map['attribures'] as $db_name => $xml_name){ + if(empty($xml_name)) $xml_name = $db_name; + zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]); + } + + $sql = 'SELECT t.triggerid, t.description, h.host'. + ' FROM trigger_depends td, triggers t, items i, hosts h, functions f '. + ' WHERE td.triggerid_down='.$triggerid. + ' AND t.triggerid=td.triggerid_up '. + ' AND f.triggerid=t.triggerid '. + ' AND i.itemid=f.itemid '. + ' AND h.hostid=i.hostid '. + ' GROUP BY t.triggerid, t.description'; + + $res = DBselect($sql); + while($data = DBfetch($res)){ + $data['depends'] = $data['host'].':'.$data['description']; + foreach($map['elements'] as $db_name => $xml_name){ + if(!isset($data[$db_name])) continue; + if(empty($xml_name)) $xml_name = $db_name; + zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]); + } + } +//-- + /* export screens */ + zbx_xmlwriter_end_element($memory); // XML_TAG_DEPENDENCY + return true; + } - function Export(&$hosts, &$templates, &$items, &$triggers, &$graphs) - { + function Export(&$hosts, &$templates, &$items, &$triggers, &$graphs){ + $memory = zbx_xmlwriter_open_memory(); zbx_xmlwriter_set_indent($memory, true); zbx_xmlwriter_start_document($memory,'1.0'); @@ -422,9 +462,9 @@ zbx_xmlwriter_write_attribute($memory, 'version', '1.0'); zbx_xmlwriter_write_attribute($memory, 'date', date('d.m.y')); zbx_xmlwriter_write_attribute($memory, 'time', date('h.i')); + zbx_xmlwriter_start_element ($memory,XML_TAG_HOSTS); - foreach(array_keys($hosts) as $hostid) - { + foreach($hosts as $hostid => $val){ $this->export_host( $memory, $hostid, @@ -435,6 +475,34 @@ ); } zbx_xmlwriter_end_element($memory); // XML_TAG_HOSTS + + if(!empty($triggers)){ + + zbx_xmlwriter_start_element ($memory,XML_TAG_DEPENDENCIES); + foreach($triggers as $hostid => $val){ + + $sql = 'SELECT h.host, t.description, t.triggerid '. + ' FROM trigger_depends td, triggers t, functions f, items i, hosts h '. + ' WHERE h.hostid='.$hostid. + ' AND i.hostid=h.hostid '. + ' AND f.itemid=i.itemid '. + ' AND t.triggerid=f.triggerid '. + ' AND td.triggerid_down=t.triggerid '. + ' GROUP BY t.triggerid, t.description'; + + $dependent_triggers = DBselect($sql); + while($deps = DBfetch($dependent_triggers)){ + $this->export_dependency( + $memory, + $deps['triggerid'], + $deps['host'], + $deps['description'] + ); + } + } + zbx_xmlwriter_end_element($memory); // XML_TAG_DEPENDENCIES + } + zbx_xmlwriter_end_element($memory); // XML_TAG_ZABBIX_EXPORT die(zbx_xmlwriter_output_memory($memory,true)); } diff --git a/frontends/php/include/hosts.inc.php b/frontends/php/include/hosts.inc.php index 9791b6b4..b30763e5 100644 --- a/frontends/php/include/hosts.inc.php +++ b/frontends/php/include/hosts.inc.php @@ -582,18 +582,21 @@ require_once "include/httptest.inc.php"; return false; } - function get_hosts_by_templateid($templateid){ - return DBselect("select h.* from hosts h, hosts_templates ht where h.hostid=ht.hostid and ht.templateid=$templateid"); + function get_hosts_by_templateid($templateid){ + return DBselect('SELECT h.* '. + ' FROM hosts h, hosts_templates ht '. + ' WHERE h.hostid=ht.hostid '. + ' AND ht.templateid='.$templateid); } # Update Host status - function update_host_status($hostid,$status){ + function update_host_status($hostid,$status){ $row=DBfetch(DBselect("select status,host from hosts where hostid=$hostid")); $old_status=$row["status"]; - if($status != $old_status) - { + + if($status != $old_status){ update_trigger_value_to_unknown_by_hostid($hostid); info("Updated status of host ".$row["host"]); return DBexecute('update hosts set status='.$status.' where hostid='.$hostid. diff --git a/frontends/php/include/import.inc.php b/frontends/php/include/import.inc.php index 84389754..41bf60ba 100644 --- a/frontends/php/include/import.inc.php +++ b/frontends/php/include/import.inc.php @@ -64,7 +64,7 @@ else if(!$this->root){ return false; } - + $data = &$this->data[$name]; foreach($attrs as $id => $val) @@ -128,10 +128,29 @@ $this->sub_node = null; array_push($this->main_node, $name); break; // case + case XML_TAG_DEPENDENCY: + // checks if trigger has been skipped + if(str_in_array($attrs['description'], $this->data[XML_TAG_DEPENDENCIES]['skip'])){ + info('Trigger ['.$attrs['description'].'] dependency update skipped - user rule'); + break; + } + + // searches trigger by host name & trigger description + if(!$trigger_down = get_trigger_by_description($attrs['description'])){ + error('Trigger ['.$attrs['description'].'] dependency update skipped - trigger not found'); + break; + } + + $data['triggerid_down'] = $trigger_down['triggerid']; + $data['triggerid_up'] = array(); + $this->sub_node = null; + array_push($this->main_node, $name); + break; case XML_TAG_HOSTPROFILE: case XML_TAG_TEMPLATE: case XML_TAG_ITEM: case XML_TAG_TRIGGER: + case XML_TAG_DEPENDS: case XML_TAG_GRAPH_ELEMENT: /*case XML_TAG_SCREEN: case XML_TAG_SCREEN_ELEMENT:*/ @@ -140,8 +159,11 @@ array_push($this->main_node, $name); break; // case case XML_TAG_HOSTS: - case XML_TAG_GROUPS: + $this->data[XML_TAG_DEPENDENCIES]['skip'] = array(); + break; + case XML_TAG_DEPENDENCIES: case XML_TAG_ZABBIX_EXPORT: + case XML_TAG_GROUPS: case XML_TAG_APPLICATIONS: case XML_TAG_TEMPLATES: case XML_TAG_ITEMS: @@ -215,11 +237,13 @@ case XML_TAG_GROUP: if(!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) break; //case - - if(!($group = DBfetch(DBselect('select groupid, name from groups'. - ' where '.DBin_node('groupid',get_current_nodeid(false)). - ' and name='.zbx_dbstr($this->element_data))))) - { + + $sql = 'SELECT groupid, name '. + ' FROM groups'. + ' WHERE '.DBin_node('groupid',get_current_nodeid(false)). + ' AND name='.zbx_dbstr($this->element_data); + + if(!$group = DBfetch(DBselect($sql))){ error('Missed group ['.$this->element_data.']'); break; // case } @@ -231,6 +255,25 @@ array_push($this->data[XML_TAG_HOST]['groups'], $group["groupid"]); + break; // case + case XML_TAG_DEPENDENCY: + if(!isset($data['triggerid_down']) || !$data['triggerid_down']) + break; // case + + update_trigger($data['triggerid_down'], + null,null,null, + null,null,null,null, + $data['triggerid_up'], null); + + break; // case + case XML_TAG_DEPENDS: + if(!isset($this->data[XML_TAG_DEPENDENCY]['triggerid_down']) || !$this->data[XML_TAG_DEPENDENCY]['triggerid_down']) + break; //case + + if(!$trigger_up = get_trigger_by_description($this->element_data)) break; + + array_push($this->data[XML_TAG_DEPENDENCY]['triggerid_up'], $trigger_up['triggerid']); + break; // case case XML_TAG_APPLICATION: if(!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) @@ -239,11 +282,12 @@ if(!isset($this->data[XML_TAG_ITEM])) break; //case - if(!($application = DBfetch(DBselect('select applicationid from applications'. - ' where '.DBin_node('applicationid',get_current_nodeid(false)). - ' and name='.zbx_dbstr($this->element_data). - ' and hostid='.$this->data[XML_TAG_HOST]['hostid'])))) - { + $sql= 'SELECT applicationid '. + ' FROM applications'. + ' WHERE '.DBin_node('applicationid',get_current_nodeid(false)). + ' AND name='.zbx_dbstr($this->element_data). + ' AND hostid='.$this->data[XML_TAG_HOST]['hostid']; + if(!$application = DBfetch(DBselect($sql))){ $applicationid = add_application($this->element_data, $this->data[XML_TAG_HOST]['hostid']); } else{ @@ -257,10 +301,11 @@ if(!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) break; //case - if(!($template = DBfetch(DBselect('SELECT DISTINCT host, hostid '. + $sql= 'SELECT DISTINCT host, hostid '. ' FROM hosts'. ' WHERE '.DBin_node('hostid'). - ' AND host='.zbx_dbstr($this->element_data))))){ + ' AND host='.zbx_dbstr($this->element_data); + if(!$template = DBfetch(DBselect($sql))){ error('Missed template ['.$this->element_data.']'); break; // case } @@ -284,13 +329,13 @@ } if(!isset($data['description'])) $data['description'] = ''; - if(!isset($data['delay'])) $data['delay'] = 30; + if(!isset($data['delay'])) $data['delay'] = 30; if(!isset($data['history'])) $data['history'] = 90; - if(!isset($data['trends'])) $data['trends'] = 365; - if(!isset($data['status'])) $data['status'] = 0; - if(!isset($data['units'])) $data['units'] = ''; + if(!isset($data['trends'])) $data['trends'] = 365; + if(!isset($data['status'])) $data['status'] = 0; + if(!isset($data['units'])) $data['units'] = ''; if(!isset($data['multiplier'])) $data['multiplier'] = 0; - if(!isset($data['delta'])) $data['delta'] = 0; + if(!isset($data['delta'])) $data['delta'] = 0; if(!isset($data['formula'])) $data['formula'] = ''; if(!isset($data['lastlogsize'])) $data['lastlogsize'] = 0; if(!isset($data['logtimefmt'])) $data['logtimefmt'] = ''; @@ -303,27 +348,31 @@ if(!isset($data['snmpv3_securitylevel'])) $data['snmpv3_securitylevel'] = 0; if(!isset($data['snmpv3_authpassphrase'])) $data['snmpv3_authpassphrase'] = ''; if(!isset($data['snmpv3_privpassphrase'])) $data['snmpv3_privpassphrase'] = ''; - if(!isset($data['valuemap'])) $data['valuemap'] = ''; - if(!isset($data['params'])) $data['params'] = ''; - if(!isset($data['applications'])) $data['applications'] = array(); + if(!isset($data['valuemap'])) $data['valuemap'] = ''; + if(!isset($data['params'])) $data['params'] = ''; + if(!isset($data['applications'])) $data['applications'] = array(); if(!empty($data['valuemap'])){ - if( $valuemap = DBfetch(DBselect('select valuemapid from valuemaps '. - ' where '.DBin_node('valuemapid', get_current_nodeid(false)). - ' and name='.zbx_dbstr($data['valuemap']))) ) - { + $sql = 'SELECT valuemapid '. + ' FROM valuemaps '. + ' WHERE '.DBin_node('valuemapid', get_current_nodeid(false)). + ' AND name='.zbx_dbstr($data['valuemap']); + + if( $valuemap = DBfetch(DBselect($sql))){ $data['valuemapid'] = $valuemap['valuemapid']; } else{ $data['valuemapid'] = add_valuemap($data['valuemap'],array()); } } - - if($item = DBfetch(DBselect('select itemid,valuemapid,templateid from items'. - ' where key_='.zbx_dbstr($data['key']). - ' and hostid='.$this->data[XML_TAG_HOST]['hostid'].' and '. - DBin_node('itemid', get_current_nodeid(false))))) - { /* exist */ + + $sql = 'SELECT itemid,valuemapid,templateid '. + ' FROM items '. + ' WHERE key_='.zbx_dbstr($data['key']). + ' AND hostid='.$this->data[XML_TAG_HOST]['hostid']. + ' AND '.DBin_node('itemid', get_current_nodeid(false)); + + if($item = DBfetch(DBselect($sql))){ /* exist */ if($this->item['exist']==1) /* skip */{ info('Item ['.$data['description'].'] skipped - user rule'); break; @@ -415,10 +464,18 @@ if(!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']){ if(isset($this->data[XML_TAG_HOST]['skip']) && $this->data[XML_TAG_HOST]['skip']){ + +// remember skipped triggers for dependencies + $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'].':'.$data['description']; + info('Trigger ['.$data['description'].'] skipped - user rule for host'); break; // case } if(zbx_strstr($data['expression'],'{HOSTNAME}')){ + +// remember skipped triggers for dependencies + $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'].':'.$data['description']; + error('Trigger ['.$data['description'].'] skipped - missed host'); break; // case } @@ -443,6 +500,10 @@ if(!empty($trigger)){ /* exist */ if($this->trigger['exist']==1){ /* skip */ + +// remember skipped triggers for dependencies + $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'].':'.$data['description']; + info('Trigger ['.$data['description'].'] skipped - user rule'); break; // case } @@ -467,6 +528,10 @@ } if($this->trigger['missed']==1) /* skip */{ + +// remember skipped triggers for dependencies + $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'].':'.$data['description']; + info('Trigger ['.$data['description'].'] skipped - user rule'); break; // case } diff --git a/frontends/php/include/triggers.inc.php b/frontends/php/include/triggers.inc.php index 3936f408..3ecb20c9 100644 --- a/frontends/php/include/triggers.inc.php +++ b/frontends/php/include/triggers.inc.php @@ -407,8 +407,11 @@ 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) @@ -416,18 +419,18 @@ return DBselect('select * from functions where triggerid='.$triggerid); } - /* - * Function: get_triggers_by_hostid - * - * Description: - * retrive selection of triggers by hostid - * - * Author: - * Aly - * - * Comments: - * - */ +/* + * Function: get_triggers_by_hostid + * + * Description: + * retrive selection of triggers by hostid + * + * Author: + * Aly + * + * Comments: + * + */ function get_triggers_by_hostid($hostid){ $db_triggers = DBselect('SELECT DISTINCT t.* '. ' FROM triggers t, functions f, items i '. @@ -436,9 +439,37 @@ ' AND f.triggerid=t.triggerid'); return $db_triggers; } + - function get_triggers_by_templateid($triggerid) - { +/* + * Function: get_trigger_by_description + * + * Description: + * retrive triggerid by description + * + * Author: + * Aly + * + * Comments: + * description - host-name:trigger-description. Example( "unix server:low free disk space") + */ + + function get_trigger_by_description($desc){ + list($host_name, $trigger_description) = explode(':',$desc); + + $sql = 'SELECT t.* '. + ' FROM triggers t, items i, functions f, hosts h '. + ' WHERE h.host='.zbx_dbstr($host_name). + ' AND i.hostid=h.hostid '. + ' AND f.itemid=i.itemid '. + ' AND t.triggerid=f.triggerid '. + ' AND t.description='.zbx_dbstr($trigger_description). + ' ORDER BY t.triggerid DESC'; + $trigger = DBfetch(DBselect($sql,1)); + return $trigger; + } + + function get_triggers_by_templateid($triggerid){ return DBselect('select * from triggers where templateid='.$triggerid); } @@ -1119,7 +1150,7 @@ * replcae $1-9 macros * */ - function expand_trigger_description_constants($description, $row){ + function expand_trigger_description_constants($description, $row){ if($row && isset($row['expression'])){ $numbers = extract_numbers(ereg_replace('(\{[0-9]+\})', 'function', $row['expression'])); $description = $row["description"]; @@ -1348,7 +1379,7 @@ * Comments: !!! Don't forget sync code with C !!! * * * ******************************************************************************/ - function update_trigger($triggerid,$expression=NULL,$description=NULL,$type=NULL,$priority=NULL,$status=NULL, + function update_trigger($triggerid,$expression=NULL,$description=NULL,$type=NULL,$priority=NULL,$status=NULL, $comments=NULL,$url=NULL,$deps=array(),$templateid=0) { $trigger = get_trigger_by_triggerid($triggerid); @@ -1370,17 +1401,14 @@ $exp_hosts = get_hosts_by_expression($expression); - if( $exp_hosts ) - { + if( $exp_hosts ){ $chd_hosts = get_hosts_by_templateid($trig_host["hostid"]); - if(DBfetch($chd_hosts)) - { + if(DBfetch($chd_hosts)){ $exp_host = DBfetch($exp_hosts); $db_chd_triggers = get_triggers_by_templateid($triggerid); - while($db_chd_trigger = DBfetch($db_chd_triggers)) - { + 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); @@ -1406,8 +1434,7 @@ } $result=delete_function_by_triggerid($triggerid); - if(!$result) - { + if(!$result){ return $result; } @@ -1450,8 +1477,7 @@ return $result; } - function check_right_on_trigger_by_triggerid($permission,$triggerid) - { + function check_right_on_trigger_by_triggerid($permission,$triggerid){ $trigger_data = DBfetch(DBselect('select expression from triggers where triggerid='.$triggerid)); if(!$trigger_data) return false; @@ -1459,7 +1485,7 @@ return check_right_on_trigger_by_expression($permission, explode_exp($trigger_data['expression'], 0)); } - function check_right_on_trigger_by_expression($permission,$expression){ + function check_right_on_trigger_by_expression($permission,$expression){ global $USER_DETAILS; $available_hosts = get_accessible_hosts_by_user($USER_DETAILS, $permission, PERM_RES_IDS_ARRAY); @@ -1477,12 +1503,10 @@ * Comments: !!! Don't forget sync code with C !!! * * * ******************************************************************************/ - function delete_dependencies_by_triggerid($triggerid) - { + function delete_dependencies_by_triggerid($triggerid){ $db_deps = DBselect('select triggerid_up, triggerid_down from trigger_depends'. ' where triggerid_down='.$triggerid); - while($db_dep = DBfetch($db_deps)) - { + while($db_dep = DBfetch($db_deps)){ DBexecute('update triggers set dep_level=dep_level-1 where triggerid='.$db_dep['triggerid_up']); DBexecute('delete from trigger_depends'. ' where triggerid_up='.$db_dep['triggerid_up']. @@ -1496,13 +1520,12 @@ * Comments: !!! Don't forget sync code with C !!! * * * ******************************************************************************/ - function insert_dependency($triggerid_down,$triggerid_up) - { + function insert_dependency($triggerid_down,$triggerid_up){ + $triggerdepid = get_dbid("trigger_depends","triggerdepid"); $result=DBexecute("insert into trigger_depends (triggerdepid,triggerid_down,triggerid_up)". " values ($triggerdepid,$triggerid_down,$triggerid_up)"); - if(!$result) - { + if(!$result){ return $result; } return DBexecute("update triggers set dep_level=dep_level+1 where triggerid=$triggerid_up"); -- cgit