From eebf8121ea8f616317d67f04bf577e4c5bdca0d1 Mon Sep 17 00:00:00 2001 From: artem Date: Fri, 1 Jun 2007 12:02:22 +0000 Subject: - added availability to export/import templates for hosts (Artem) - added permissions for hosts on import (Artem) git-svn-id: svn://svn.zabbix.com/trunk@4220 97f52cf1-0a1b-0410-bd0e-c28be96e8082 --- frontends/php/include/config.inc.php | 4 +- frontends/php/include/defines.inc.php | 2 + frontends/php/include/export.inc.php | 781 +++++++-------- frontends/php/include/hosts.inc.php | 26 + frontends/php/include/import.inc.php | 1412 ++++++++++++++------------- frontends/php/include/locales/en_gb.inc.php | 4 +- 6 files changed, 1159 insertions(+), 1070 deletions(-) (limited to 'frontends/php/include') diff --git a/frontends/php/include/config.inc.php b/frontends/php/include/config.inc.php index d808aacd..100504b4 100644 --- a/frontends/php/include/config.inc.php +++ b/frontends/php/include/config.inc.php @@ -682,8 +682,8 @@ else if($msg_count > $msg_show) $msg_count = $msg_show; - $msg_count = ($msg_count * $msg_font_size *2); - $lst_error->AddOption('style',' font-size: '.$msg_font_size.'pt; height: '.$msg_count.';'); + $msg_count = ($msg_count * $msg_font_size *2) + 2; + $lst_error->AddOption('style','font-size: '.$msg_font_size.'pt; height: '.$msg_count.';'); //--- $lst_error->Show(); diff --git a/frontends/php/include/defines.inc.php b/frontends/php/include/defines.inc.php index 24209da4..3558c34d 100644 --- a/frontends/php/include/defines.inc.php +++ b/frontends/php/include/defines.inc.php @@ -28,6 +28,8 @@ define('XML_TAG_APPLICATION', 'application'); define('XML_TAG_ITEMS', 'items'); define('XML_TAG_ITEM', 'item'); + define('XML_TAG_TEMPLATES', 'templates'); + define('XML_TAG_TEMPLATE', 'template'); define('XML_TAG_TRIGGERS', 'triggers'); define('XML_TAG_TRIGGER', 'trigger'); define('XML_TAG_GRAPHS', 'graphs'); diff --git a/frontends/php/include/export.inc.php b/frontends/php/include/export.inc.php index 1c3acb81..68609b90 100644 --- a/frontends/php/include/export.inc.php +++ b/frontends/php/include/export.inc.php @@ -1,381 +1,400 @@ - - array( - 'attribures' => array( - 'host' => 'name'), - 'elements' => array( - 'useip' => '', - 'dns' => '', - 'ip' => '', - 'port' => '', - 'status' => '') - ), - XML_TAG_ITEM => array( - 'attribures' => array( - 'type' => '', - 'key_' => 'key', - 'value_type' => ''), - 'elements' => array( - 'description' => '', - 'delay' => '', - 'history' => '', - 'trends' => '', - 'status' => '', - 'units' => '', - 'multiplier' => '', - 'delta' => '', - 'formula' => '', - 'lastlogsize' => '', - 'logtimefmt' => '', - 'delay_flex' => '', - 'trapper_hosts' => '', - 'snmp_community' => '', - 'snmp_oid' => '', - 'snmp_port' => '', - 'snmpv3_securityname' => '', - 'snmpv3_securitylevel' => '', - 'snmpv3_authpassphrase' => '', - 'snmpv3_privpassphrase' => '') - ), - XML_TAG_TRIGGER => array( - 'attribures' => array(), - 'elements' => array( - 'description' => '', - 'expression' => '', - 'url' => '', - 'status' => '', - 'priority' => '', - 'comments' => '') - ), - XML_TAG_GRAPH => array( - 'attribures' => array( - 'name' => '', - 'width' => '', - 'height' => ''), - 'elements' => array( - 'yaxistype' => '', - 'show_work_period' => '', - 'show_triggers' => '', - 'graphtype' => '', - 'yaxismin' => '', - 'yaxismax' => '') - ), - XML_TAG_GRAPH_ELEMENT => array( - 'attribures' => array( - 'item' => ''), - 'elements' => array( - 'drawtype' => '', - 'sortorder' => '', - 'color' => '', - 'yaxisside' => '', - 'calc_fnc' => '', - 'type' => '', - 'periods_cnt' => '') - ) - ); - - function zbx_xmlwriter_open_memory() - { - return array('tabs' => 0, 'tag'=>array()); - } - - function zbx_xmlwriter_set_indent($mem, $val) - { - return true; - } - - 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) - { - echo ' '.$name.'="'.htmlspecialchars($val).'"'; - return true; - } - - 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) - { - echo '>'."\n".str_repeat("\t",$mem['tabs']).'<'.$name.'>'.htmlspecialchars($val).' $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) - { - if(empty($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']))) - { - 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)) - { - zbx_xmlwriter_start_element ($memory,XML_TAG_APPLICATIONS); - do { - zbx_xmlwriter_write_element ($memory, XML_TAG_APPLICATION, $application['name']); - } while($application = DBfetch($db_applications)); - zbx_xmlwriter_end_element($memory); // XML_TAG_APPLICATIONS - } - - zbx_xmlwriter_end_element($memory); // XML_TAG_ITEM - } - - function export_trigger(&$memory, $triggerid) - { - global $ZBX_EXPORT_MAP; - - $data = DBfetch(DBselect('select * from triggers where triggerid='.$triggerid)); - if(!$data) return false; - - $data['expression'] = explode_exp($data["expression"],0,true); - - zbx_xmlwriter_start_element ($memory,XML_TAG_TRIGGER); - - $map =& $ZBX_EXPORT_MAP[XML_TAG_TRIGGER]; - - 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) - { - 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_TRIGGER - } - - 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)); - if(!$data) return false; - - $data['item'] = '{HOSTNAME}:'.$data['key_']; - - zbx_xmlwriter_start_element ($memory,XML_TAG_GRAPH_ELEMENT); - - $map =& $ZBX_EXPORT_MAP[XML_TAG_GRAPH_ELEMENT]; - - 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) - { - 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_GRAPH_ELEMENT - } - - function export_graph(&$memory, $graphid) - { - global $ZBX_EXPORT_MAP; - - $data = DBfetch(DBselect('select * from graphs where graphid='.$graphid)); - if(!$data) return false; - - zbx_xmlwriter_start_element ($memory,XML_TAG_GRAPH); - - $map =& $ZBX_EXPORT_MAP[XML_TAG_GRAPH]; - - 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) - { - 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_start_element ($memory,XML_TAG_GRAPH_ELEMENTS); - $db_elements = DBselect('select gitemid from graphs_items where graphid='.$graphid); - while($element = DBfetch($db_elements)) - { - $this->export_graph_element($memory, $element['gitemid']); - } - - zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPH_ELEMENTS - zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPH - } - - function export_host(&$memory, $hostid, $export_items, $export_triggers, $export_graphs) - { - global $ZBX_EXPORT_MAP; - - $data = DBfetch(DBselect('select * from hosts where hostid='.$hostid)); - if(!$data) return false; - - zbx_xmlwriter_start_element ($memory,XML_TAG_HOST); - - $map =& $ZBX_EXPORT_MAP[XML_TAG_HOST]; - - 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) - { - if(empty($data[$db_name])) continue; - if(empty($xml_name)) $xml_name = $db_name; - zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]); - } - - if($db_groups = DBselect('select g.name from groups g, hosts_groups hg'. - ' where g.groupid=hg.groupid and hg.hostid='.$hostid)) - { - zbx_xmlwriter_start_element ($memory,XML_TAG_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_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)) - { - $this->export_item($memory, $item_id['itemid']); - } - zbx_xmlwriter_end_element($memory); // XML_TAG_ITEMS - } - 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)) - { - if($trigger['hostid'] != $hostid || $trigger['cnt']!=1) continue; - $this->export_trigger($memory, $trigger['triggerid']); - } - zbx_xmlwriter_end_element($memory); // XML_TAG_TRIGGERS - } - 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)) - { - if($graph['hostid'] != $hostid || $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(&$hosts, &$items, &$triggers, &$graphs) - { - $memory = zbx_xmlwriter_open_memory(); - zbx_xmlwriter_set_indent($memory, true); - zbx_xmlwriter_start_document($memory,'1.0'); - zbx_xmlwriter_start_element ($memory,XML_TAG_ZABBIX_EXPORT); - 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) - { - $this->export_host( - $memory, - $hostid, - isset($items[$hostid]), - isset($triggers[$hostid]), - isset($graphs[$hostid]) - ); - } - zbx_xmlwriter_end_element($memory); // XML_TAG_HOSTS - zbx_xmlwriter_end_element($memory); // XML_TAG_ZABBIX_EXPORT - die(zbx_xmlwriter_output_memory($memory,true)); - } - } -?> + + array( + 'attribures' => array( + 'host' => 'name'), + 'elements' => array( + 'useip' => '', + 'dns' => '', + 'ip' => '', + 'port' => '', + 'status' => '') + ), + XML_TAG_ITEM => array( + 'attribures' => array( + 'type' => '', + 'key_' => 'key', + 'value_type' => ''), + 'elements' => array( + 'description' => '', + 'delay' => '', + 'history' => '', + 'trends' => '', + 'status' => '', + 'units' => '', + 'multiplier' => '', + 'delta' => '', + 'formula' => '', + 'lastlogsize' => '', + 'logtimefmt' => '', + 'delay_flex' => '', + 'trapper_hosts' => '', + 'snmp_community' => '', + 'snmp_oid' => '', + 'snmp_port' => '', + 'snmpv3_securityname' => '', + 'snmpv3_securitylevel' => '', + 'snmpv3_authpassphrase' => '', + 'snmpv3_privpassphrase' => '') + ), + XML_TAG_TRIGGER => array( + 'attribures' => array(), + 'elements' => array( + 'description' => '', + 'expression' => '', + 'url' => '', + 'status' => '', + 'priority' => '', + 'comments' => '') + ), + XML_TAG_GRAPH => array( + 'attribures' => array( + 'name' => '', + 'width' => '', + 'height' => ''), + 'elements' => array( + 'yaxistype' => '', + 'show_work_period' => '', + 'show_triggers' => '', + 'graphtype' => '', + 'yaxismin' => '', + 'yaxismax' => '') + ), + XML_TAG_GRAPH_ELEMENT => array( + 'attribures' => array( + 'item' => ''), + 'elements' => array( + 'drawtype' => '', + 'sortorder' => '', + 'color' => '', + 'yaxisside' => '', + 'calc_fnc' => '', + 'type' => '', + 'periods_cnt' => '') + ) + ); + + function zbx_xmlwriter_open_memory() + { + return array('tabs' => 0, 'tag'=>array()); + } + + function zbx_xmlwriter_set_indent($mem, $val) + { + return true; + } + + 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) + { + echo ' '.$name.'="'.htmlspecialchars($val).'"'; + return true; + } + + 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) + { + echo '>'."\n".str_repeat("\t",$mem['tabs']).'<'.$name.'>'.htmlspecialchars($val).' $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) + { + if(empty($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']))) + { + 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)) + { + zbx_xmlwriter_start_element ($memory,XML_TAG_APPLICATIONS); + do { + zbx_xmlwriter_write_element ($memory, XML_TAG_APPLICATION, $application['name']); + } while($application = DBfetch($db_applications)); + zbx_xmlwriter_end_element($memory); // XML_TAG_APPLICATIONS + } + + zbx_xmlwriter_end_element($memory); // XML_TAG_ITEM + } + + + function export_trigger(&$memory, $triggerid) + { + global $ZBX_EXPORT_MAP; + + $data = DBfetch(DBselect('select * from triggers where triggerid='.$triggerid)); + if(!$data) return false; + + $data['expression'] = explode_exp($data["expression"],0,true); + + zbx_xmlwriter_start_element ($memory,XML_TAG_TRIGGER); + + $map =& $ZBX_EXPORT_MAP[XML_TAG_TRIGGER]; + + 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) + { + 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_TRIGGER + } + + 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)); + if(!$data) return false; + + $data['item'] = '{HOSTNAME}:'.$data['key_']; + + zbx_xmlwriter_start_element ($memory,XML_TAG_GRAPH_ELEMENT); + + $map =& $ZBX_EXPORT_MAP[XML_TAG_GRAPH_ELEMENT]; + + 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) + { + 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_GRAPH_ELEMENT + } + + function export_graph(&$memory, $graphid) + { + global $ZBX_EXPORT_MAP; + + $data = DBfetch(DBselect('select * from graphs where graphid='.$graphid)); + if(!$data) return false; + + zbx_xmlwriter_start_element ($memory,XML_TAG_GRAPH); + + $map =& $ZBX_EXPORT_MAP[XML_TAG_GRAPH]; + + 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) + { + 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_start_element ($memory,XML_TAG_GRAPH_ELEMENTS); + $db_elements = DBselect('select gitemid from graphs_items where graphid='.$graphid); + while($element = DBfetch($db_elements)) + { + $this->export_graph_element($memory, $element['gitemid']); + } + + zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPH_ELEMENTS + zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPH + } + + 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)); + if(!$data) return false; + + zbx_xmlwriter_start_element ($memory,XML_TAG_HOST); + + $map =& $ZBX_EXPORT_MAP[XML_TAG_HOST]; + + 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) + { + if(empty($data[$db_name])) continue; + if(empty($xml_name)) $xml_name = $db_name; + zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]); + } + + if($db_groups = DBselect('select g.name from groups g, hosts_groups hg'. + ' where g.groupid=hg.groupid and hg.hostid='.$hostid)) + { + zbx_xmlwriter_start_element ($memory,XML_TAG_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; + + if($db_templates = DBselect($query)){ + 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) + { + zbx_xmlwriter_start_element ($memory,XML_TAG_ITEMS); + $db_items=DBselect('select itemid from items where hostid='.$hostid); + while($item_id = DBfetch($db_items)) + { + $this->export_item($memory, $item_id['itemid']); + } + zbx_xmlwriter_end_element($memory); // XML_TAG_ITEMS + } + + + 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)) + { + if($trigger['hostid'] != $hostid || $trigger['cnt']!=1) continue; + $this->export_trigger($memory, $trigger['triggerid']); + } + zbx_xmlwriter_end_element($memory); // XML_TAG_TRIGGERS + } + 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)) + { + if($graph['hostid'] != $hostid || $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(&$hosts, &$templates, &$items, &$triggers, &$graphs) + { + $memory = zbx_xmlwriter_open_memory(); + zbx_xmlwriter_set_indent($memory, true); + zbx_xmlwriter_start_document($memory,'1.0'); + zbx_xmlwriter_start_element ($memory,XML_TAG_ZABBIX_EXPORT); + 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) + { + $this->export_host( + $memory, + $hostid, + isset($templates[$hostid]), + isset($items[$hostid]), + isset($triggers[$hostid]), + isset($graphs[$hostid]) + ); + } + zbx_xmlwriter_end_element($memory); // XML_TAG_HOSTS + 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 5ccae673..52f8b3ef 100644 --- a/frontends/php/include/hosts.inc.php +++ b/frontends/php/include/hosts.inc.php @@ -915,4 +915,30 @@ require_once "include/items.inc.php"; return $result; } + + function host_status2str($status) + { + switch($status) + { + case HOST_STATUS_MONITORED: $status = S_MONITORED; break; + case HOST_STATUS_NOT_MONITORED: $status = S_NOT_MONITORED; break; + case HOST_STATUS_TEMPLATE: $status = S_TEMPLATE; break; + case HOST_STATUS_DELETED: $status = S_DELETED; break; + default: + $status = S_UNKNOWN; break; + } + return $status; + } + + function host_status2style($status) + { + switch($status) + { + case HOST_STATUS_MONITORED: $status = 'off'; break; + case HOST_STATUS_NOT_MONITORED: $status = 'on'; break; + default: + $status = 'unknown'; break; + } + return $status; + } ?> diff --git a/frontends/php/include/import.inc.php b/frontends/php/include/import.inc.php index 38e53ac5..6a36a968 100644 --- a/frontends/php/include/import.inc.php +++ b/frontends/php/include/import.inc.php @@ -1,686 +1,726 @@ - -main_node= null; - $this->sub_node = null; - $this->data = null; - $this->host = array('exist' => 0, 'missed' => 0); - $this->item = array('exist' => 0, 'missed' => 0); - $this->trigger = array('exist' => 0, 'missed' => 0); - $this->graph = array('exist' => 0, 'missed' => 0); - - $this->accessible_groups = get_accessible_groups_by_user($USER_DETAILS, - PERM_READ_WRITE, null, PERM_RES_IDS_ARRAY, $ZBX_CURNODEID); - } - - function CharacterData($parser, $data) - { - $this->element_data .= html_entity_decode($data); - } - - function StartElement($parser, $name, $attrs) - { - $this->element_data = ''; - - if(!isset($this->root)) - { - if($name == XML_TAG_ZABBIX_EXPORT) - if(isset($attrs['version'])) - if($attrs['version'] == '1.0') - { - $this->root = true; - return; - } - else - { - error('Unsupported version of imported data'); - } - error('Unsupported file format'); - $this->root = false; - } - elseif(!$this->root) - { - return false; - } - - global $ZBX_CURNODEID; - - $data = &$this->data[$name]; - - foreach($attrs as $id => $val) - $attrs[$id] = html_entity_decode($val); - - switch($name) - { - case XML_TAG_HOST: - $this->main_node= array($name); - $this->sub_node = null; - $data = $attrs; - $data['groups'] = array(); - $data['skip'] = false; - - if($host_data = DBfetch(DBselect('select hostid from hosts'. - ' where host='.zbx_dbstr($data['name']). - ' and '.DBid2nodeid('hostid').'='.$ZBX_CURNODEID))) - { /* exist */ - if($this->host['exist']==1) /* skip */ - { - $data['skip'] = true; - info('Host ['.$data['name'].'] skipped - user rule'); - break; // case - } - - $data['hostid'] = $host_data['hostid']; - $data['templates'] = get_templates_by_hostid($host_data['hostid']); - } - else - { /* missed */ - if($this->host['missed']==1) /* skip */ - { - $data['skip'] = true; - info('Host ['.$data['name'].'] skipped - user rule'); - break; // case - } - - $data['templates'] = array(); - $data['hostid'] = add_host( - $data['name'], - 10050, - HOST_STATUS_TEMPLATE, - 0, /* useip */ - "", /* dns */ - "", /* ip */ - array(), - null, - array()); - } - break; // case - case XML_TAG_GRAPH: - $data = $attrs; - $data['items'] = array(); - $this->sub_node = null; - array_push($this->main_node, $name); - break; // case - case XML_TAG_ITEM: - case XML_TAG_TRIGGER: - case XML_TAG_GRAPH_ELEMENT: - /*case XML_TAG_SCREEN: - case XML_TAG_SCREEN_ELEMENT:*/ - $data = $attrs; - $this->sub_node = null; - array_push($this->main_node, $name); - break; // case - case XML_TAG_HOSTS: - case XML_TAG_GROUPS: - case XML_TAG_ZABBIX_EXPORT: - case XML_TAG_APPLICATIONS: - case XML_TAG_ITEMS: - case XML_TAG_TRIGGERS: - case XML_TAG_GRAPHS: - /* case XML_TAG_SCREENS:*/ - $this->sub_node = null; - break; // case - case XML_TAG_GROUP: - case XML_TAG_APPLICATION: - default: - $this->sub_node = $name; - break; // case - } - } - - function EndElement($parser, $name) - { - if(!$this->root) - { - return false; - } - - global $USER_DETAILS; - global $ZBX_CURNODEID; - - $data = &$this->data[$name]; - - switch($name) - { - case XML_TAG_HOST: - if($data['skip'] || !$data['hostid']) - break; // case - - if(!isset($data['port'])) $data['port'] = 10050; - if(!isset($data['status'])) $data['status'] = 0; - if(!isset($data['useip'])) $data['useip'] = 0; - if(!isset($data['dns'])) $data['dns'] = ""; - if(!isset($data['ip'])) $data['ip'] = ""; - - if(update_host($data['hostid'], $data['name'], $data['port'], $data['status'], - $data['useip'], $data['dns'], $data['ip'], $data['templates'], null, $data['groups'])) - { - info('Host ['.$data['name'].'] updated'); - } - - break; // case - 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 '.DBid2nodeid('groupid').'='.$ZBX_CURNODEID. - ' and name='.zbx_dbstr($this->element_data))))) - { - error('Missed group ['.$this->element_data.']'); - break; // case - } - - if(!in_array($group["groupid"], $this->accessible_groups)) - { - error('Group ['.$this->element_data.'] skipped - Access deny.'); - break; // case - } - - array_push($this->data[XML_TAG_HOST]['groups'], $group["groupid"]); - - break; // case - case XML_TAG_APPLICATION: - if(!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) - break; //case - - if(!isset($this->data[XML_TAG_ITEM])) - break; //case - - if(!($application = DBfetch(DBselect('select applicationid from applications'. - ' where '.DBid2nodeid('applicationid').'='.$ZBX_CURNODEID. - ' and name='.zbx_dbstr($this->element_data). - ' and hostid='.$this->data[XML_TAG_HOST]['hostid'])))) - { - $applicationid = add_application($this->element_data, $this->data[XML_TAG_HOST]['hostid']); - } - else - { - $applicationid = $application['applicationid']; - } - - $this->data[XML_TAG_ITEM]['applications'][] = $applicationid; - - break; // case - case XML_TAG_ITEM: - 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']) - { - info('Item ['.$data['description'].'] skipped - user rule for host'); - break; // case - } - - error('Item ['.$data['description'].'] skipped - missed host'); - break; // case - } - - if(!isset($data['description'])) $data['description'] = ""; - 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['multiplier'])) $data['multiplier'] = 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'] = ''; - if(!isset($data['delay_flex'])) $data['delay_flex'] = ''; - if(!isset($data['trapper_hosts'])) $data['trapper_hosts'] = ''; - if(!isset($data['snmp_community'])) $data['snmp_community'] = ''; - if(!isset($data['snmp_oid'])) $data['snmp_oid'] = ''; - if(!isset($data['snmp_port'])) $data['snmp_port'] = 161; - if(!isset($data['snmpv3_securityname'])) $data['snmpv3_securityname'] = ''; - 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['applications'])) $data['applications'] = array(); - - if(!empty($data['valuemap'])) - { - if( $valuemap = DBfetch(DBselect('select valuemapid from valuemaps '. - ' where '.DBid2nodeid('valuemapid').'='.$ZBX_CURNODEID. - ' and name='.zbx_dbstr($data['valuemap']))) ) - { - $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 '. - DBid2nodeid('itemid').'='.$ZBX_CURNODEID))) - { /* exist */ - if($this->item['exist']==1) /* skip */ - { - info('Item ['.$data['description'].'] skipped - user rule'); - break; - } - - if( !isset($data['valuemapid']) ) - $data['valuemapid'] = $item['valuemapid']; - - update_item( - $item['itemid'], - $data['description'], - $data['key'], - $this->data[XML_TAG_HOST]['hostid'], - $data['delay'], - $data['history'], - $data['status'], - $data['type'], - $data['snmp_community'], - $data['snmp_oid'], - $data['value_type'], - $data['trapper_hosts'], - $data['snmp_port'], - $data['units'], - $data['multiplier'], - $data['delta'], - $data['snmpv3_securityname'], - $data['snmpv3_securitylevel'], - $data['snmpv3_authpassphrase'], - $data['snmpv3_privpassphrase'], - $data['formula'], - $data['trends'], - $data['logtimefmt'], - $data['valuemapid'], - $data['delay_flex'], - array_unique(array_merge( - $data['applications'], - get_applications_by_itemid($item['itemid']) - )), - $item['templateid']); - } - else - { /* missed */ - if($this->item['missed']==1) /* skip */ - { - info('Item ['.$data['description'].'] skipped - user rule'); - break; // case - } - - if( !isset($data['valuemapid']) ) - $data['valuemapid'] = 0; - - add_item( - $data['description'], - $data['key'], - $this->data[XML_TAG_HOST]['hostid'], - $data['delay'], - $data['history'], - $data['status'], - $data['type'], - $data['snmp_community'], - $data['snmp_oid'], - $data['value_type'], - $data['trapper_hosts'], - $data['snmp_port'], - $data['units'], - $data['multiplier'], - $data['delta'], - $data['snmpv3_securityname'], - $data['snmpv3_securitylevel'], - $data['snmpv3_authpassphrase'], - $data['snmpv3_privpassphrase'], - $data['formula'], - $data['trends'], - $data['logtimefmt'], - $data['valuemapid'], - $data['delay_flex'], - $data['applications']); - } - - break; // case - case XML_TAG_TRIGGER: - if(!isset($data['expression'])) $data['expression'] = ''; - if(!isset($data['description'])) $data['description'] = ''; - if(!isset($data['priority'])) $data['priority'] = 0; - if(!isset($data['status'])) $data['status'] = 0; - if(!isset($data['comments'])) $data['comments'] = ''; - if(!isset($data['url'])) $data['url'] = ''; - - 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']) - { - info('Trigger ['.$data['description'].'] skipped - user rule for host'); - break; // case - } - if(strstr($data['expression'],'{HOSTNAME}')) - { - error('Trigger ['.$data['description'].'] skipped - missed host'); - break; // case - } - } - else - { - $data['expression'] = str_replace('{HOSTNAME}', - $this->data[XML_TAG_HOST]['name'], - $data['expression']); - - if($trigger = DBfetch(DBselect('select distinct t.triggerid,t.templateid '. - ' from triggers t,functions f,items i '. - ' where t.triggerid=f.triggerid and f.itemid=i.itemid'. - ' and i.hostid='.$this->data[XML_TAG_HOST]['hostid']. - ' and t.description='.zbx_dbstr($data['description'])))) - { /* exist */ - if($this->trigger['exist']==1) /* skip */ - { - info('Trigger ['.$data['description'].'] skipped - user rule'); - break; // case - } - - update_trigger( - $trigger['triggerid'], - $data['expression'], - $data['description'], - $data['priority'], - $data['status'], - $data['comments'], - $data['url'], - get_trigger_dependences_by_triggerid($trigger['triggerid']), - $trigger['templateid']); - - break; // case - } - else /* missed */ - { - // continue [add_trigger] - } - } - - if($this->trigger['missed']==1) /* skip */ - { - info('Trigger ['.$data['description'].'] skipped - user rule'); - break; // case - } - - add_trigger( - $data['expression'], - $data['description'], - $data['priority'], - $data['status'], - $data['comments'], - $data['url']); - - break; // case - case XML_TAG_GRAPH: - if(isset($data['error'])) - { - error('Graph ['.$data['name'].'] skipped - error occured'); - break; // case - } - - if(!isset($data['yaxistype'])) $data['yaxistype'] = 0; - if(!isset($data['show_work_period'])) $data['show_work_period'] = 1; - if(!isset($data['show_triggers'])) $data['show_triggers'] = 1; - if(!isset($data['graphtype'])) $data['graphtype'] = 0; - if(!isset($data['yaxismin'])) $data['yaxismin'] = 0; - if(!isset($data['yaxismax'])) $data['yaxismax'] = 0; - if(!isset($data['items'])) $data['items'] = array(); - - 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']) - { - info('Graph ['.$data['name'].'] skipped - user rule for host'); - break; // case - } - foreach($data['items'] as $id) - - if(strstr($data['name'],'{HOSTNAME}')) - { - error('Graph ['.$data['name'].'] skipped - missed host'); - break; // case - } - } - else - { - if($graph = DBfetch(DBselect('select distinct g.graphid, g.templateid'. - ' from graphs g, graphs_items gi, items i'. - ' where g.graphid=gi.graphid and gi.itemid=i.itemid'. - ' and g.name='.zbx_dbstr($data['name']). - ' and i.hostid='.$this->data[XML_TAG_HOST]['hostid']))) - { /* exist */ - if($this->graph['exist']==1) /* skip */ - { - info('Graph ['.$data['name'].'] skipped - user rule'); - break; // case - } - - $data['graphid'] = $graph['graphid']; - - update_graph( - $data['graphid'], - $data['name'], - $data['width'], - $data['height'], - $data['yaxistype'], - $data['yaxismin'], - $data['yaxismax'], - $data['show_work_period'], - $data['show_triggers'], - $data['graphtype'], - $graph['templateid']); - - DBexecute('delete from graphs_items where graphid='.$data['graphid']); - } - else - { /* missed */ - // continue [add_group] - } - } - - if(!isset($data['graphid'])) - { - if($this->graph['missed']==1) /* skip */ - { - info('Graph ['.$data['name'].'] skipped - user rule'); - break; // case - } - - $data['graphid'] = add_graph( - $data['name'], - $data['width'], - $data['height'], - $data['yaxistype'], - $data['yaxismin'], - $data['yaxismax'], - $data['show_work_period'], - $data['show_triggers'], - $data['graphtype']); - } - - foreach($data['items'] as $item) - { - add_item_to_graph( - $data['graphid'], - $item['itemid'], - $item['color'], - $item['drawtype'], - $item['sortorder'], - $item['yaxisside'], - $item['calc_fnc'], - $item['type'], - $item['periods_cnt']); - } - break; // case - case XML_TAG_GRAPH_ELEMENT: - if(!isset($this->data[XML_TAG_GRAPH])) - break; // case - - $data['key'] = explode(':', $data['item']); - if(count($data['key']) < 2) - { - $this->data[XML_TAG_GRAPH]['error'] = true; - error('Incorrect element for graph ['.$data['name'].']'); - break; // case - } - - $data['host'] = array_shift($data['key']); - $data['key'] = implode(':', $data['key']); - - if(isset($this->data[XML_TAG_HOST]['name'])) - { - $data['host'] = str_replace('{HOSTNAME}',$this->data[XML_TAG_HOST]['name'],$data['host']); - } - - if(!isset($data['drawtype'])) $data['drawtype'] = 0; - if(!isset($data['sortorder'])) $data['sortorder'] = 0; - if(!isset($data['color'])) $data['color'] = 'Dark Green'; - if(!isset($data['yaxisside'])) $data['yaxisside'] = 1; - if(!isset($data['calc_fnc'])) $data['calc_fnc'] = 2; - if(!isset($data['type'])) $data['type'] = 0; - if(!isset($data['periods_cnt'])) $data['periods_cnt'] = 5; - - if(!($item = DBfetch(DBselect('select i.itemid from items i,hosts h'. - ' where h.hostid=i.hostid and i.key_='.zbx_dbstr($data['key']). - ' and h.host='.zbx_dbstr($data['host']))))) - { - $this->data[XML_TAG_GRAPH]['error'] = true; - - error('Missed item ['.$data['key'].'] for host ['.$data['host'].']'); - break; // case - } - - $data['itemid'] = $item['itemid']; - - array_push($this->data[XML_TAG_GRAPH]['items'], $data); - - break; // case - /*case XML_TAG_SCREEN: - case XML_TAG_SCREEN_ELEMENT: - break; // case*/ - default: - if(isset($this->sub_node) && isset($this->main_node)) - { - $main_node = array_pop($this->main_node); - $this->data[$main_node][$this->sub_node] = $this->element_data; - array_push($this->main_node, $main_node); - } - $this->sub_node = null; - return; - } - - unset($this->data[$name], $data); - - array_pop($this->main_node); - } - - function Parse($file) - { - $this->main_node = null; - $this->sub_node = null; - $this->data = null; - - $xml_parser = xml_parser_create(); - - xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); - - xml_set_element_handler($xml_parser, array(&$this, "StartElement"), array(&$this, "EndElement")); - - xml_set_character_data_handler($xml_parser, array(&$this, "characterData")); - - if (!($fp = fopen($file, "r"))) - { - error("could not open XML input"); - xml_parser_free($xml_parser); - return false; - } - else - { - while ($data = fread($fp, 4096)) - { - if (!xml_parse($xml_parser, $data, feof($fp))) - { - error( sprintf("XML error: %s at line %d", - xml_error_string(xml_get_error_code($xml_parser)), - xml_get_current_line_number($xml_parser)) - ); - fclose($fp); - xml_parser_free($xml_parser); - return false; - } - } - fclose($fp); - } - xml_parser_free($xml_parser); - - $this->main_node = null; - $this->sub_node = null; - $this->data = null; - - return true; - } - - function SetRules($host, $item, $trigger, $graph) - { - $this->host = $host; - $this->item = $item; - $this->trigger = $trigger; - $this->graph = $graph; - } - } - - class CZabbixHostImport - { - function CZabbixHostImport() - { - } - - function Parse($file) - { - if (!($fp = fopen($file, "r"))) - { - error("could not open XML input"); - xml_parser_free($xml_parser); - return false; - } - else - { - while(!feof($fp)) - { - $len = fgets($fp); - echo $len.'
'."\n"; - } - fclose($fp); - } - - info('Underconstructor!'); - return true; - } - } -?> + +main_node= null; + $this->sub_node = null; + $this->data = null; + $this->host = array('exist' => 0, 'missed' => 0); + $this->template = array('exist' => 0, 'missed' => 0); + $this->item = array('exist' => 0, 'missed' => 0); + $this->trigger = array('exist' => 0, 'missed' => 0); + $this->graph = array('exist' => 0, 'missed' => 0); + + $this->accessible_groups = get_accessible_groups_by_user($USER_DETAILS, + PERM_READ_WRITE, null, PERM_RES_IDS_ARRAY, $ZBX_CURNODEID); + + $this->accessible_hosts = get_accessible_hosts_by_user($USER_DETAILS, + PERM_READ_WRITE, null, PERM_RES_IDS_ARRAY, $ZBX_CURNODEID); + + $this->accessible_nodes = get_accessible_nodes_by_user($USER_DETAILS, + PERM_READ_WRITE, null, PERM_RES_IDS_ARRAY, $ZBX_CURNODEID); + } + + function CharacterData($parser, $data) + { + $this->element_data .= html_entity_decode($data); + } + + function StartElement($parser, $name, $attrs) + { + $this->element_data = ''; + + if(!isset($this->root)) + { + if($name == XML_TAG_ZABBIX_EXPORT) + if(isset($attrs['version'])) + if($attrs['version'] == '1.0') + { + $this->root = true; + return; + } + else + { + error(S_UNSUPPORTED_VERSION_OF_IMPORTED_DATA); + } + error(S_UNSUPPORTED_FILE_FORMAT); + $this->root = false; + } + elseif(!$this->root) + { + return false; + } + + global $ZBX_CURNODEID; + + $data = &$this->data[$name]; + + foreach($attrs as $id => $val) + $attrs[$id] = html_entity_decode($val); + + switch($name) + { + case XML_TAG_HOST: + $this->main_node= array($name); + $this->sub_node = null; + $data = $attrs; + $data['groups'] = array(); + $data['skip'] = false; + + if($host_data = DBfetch(DBselect('select hostid from hosts'. + ' where host='.zbx_dbstr($data['name']). + ' and '.DBid2nodeid('hostid').'='.$ZBX_CURNODEID))) + { /* exist */ + if($this->host['exist']==1) /* skip */ + { + $data['skip'] = true; + info('Host ['.$data['name'].'] skipped - user rule'); + break; // case + } + if(!in_array($host_data['hostid'], $this->accessible_hosts)){ + error('Host ['.$data['name'].'] skipped - Access deny.'); + break; // case + } + + $data['hostid'] = $host_data['hostid']; + $data['templates'] = get_templates_by_hostid($host_data['hostid']); + } + else + { /* missed */ + if($this->host['missed']==1) /* skip */ + { + $data['skip'] = true; + info('Host ['.$data['name'].'] skipped - user rule'); + break; // case + } + + if(!in_array($ZBX_CURNODEID, $this->accessible_nodes)){ + error('Host ['.$data['name'].'] skipped - Access deny.'); + break; // case + } + + $data['templates'] = array(); + $data['hostid'] = add_host( + $data['name'], + 10050, + HOST_STATUS_TEMPLATE, + 0, /* useip */ + "", /* dns */ + "", /* ip */ + array(), + null, + array()); + } + break; // case + case XML_TAG_GRAPH: + $data = $attrs; + $data['items'] = array(); + $this->sub_node = null; + array_push($this->main_node, $name); + break; // case + case XML_TAG_TEMPLATE: + case XML_TAG_ITEM: + case XML_TAG_TRIGGER: + case XML_TAG_GRAPH_ELEMENT: + /*case XML_TAG_SCREEN: + case XML_TAG_SCREEN_ELEMENT:*/ + $data = $attrs; + $this->sub_node = null; + array_push($this->main_node, $name); + break; // case + case XML_TAG_HOSTS: + case XML_TAG_GROUPS: + case XML_TAG_ZABBIX_EXPORT: + case XML_TAG_APPLICATIONS: + case XML_TAG_TEMPLATES: + case XML_TAG_ITEMS: + case XML_TAG_TRIGGERS: + case XML_TAG_GRAPHS: + /* case XML_TAG_SCREENS:*/ + $this->sub_node = null; + break; // case + case XML_TAG_GROUP: + case XML_TAG_APPLICATION: + default: + $this->sub_node = $name; + break; // case + } + } + + function EndElement($parser, $name) + { + if(!$this->root) + { + return false; + } + + global $USER_DETAILS; + global $ZBX_CURNODEID; + + $data = &$this->data[$name]; + + switch($name) + { + case XML_TAG_HOST: + if($data['skip'] || !isset($data['hostid']) || !$data['hostid']) + break; // case + + if(!isset($data['port'])) $data['port'] = 10050; + if(!isset($data['status'])) $data['status'] = 0; + if(!isset($data['useip'])) $data['useip'] = 0; + if(!isset($data['dns'])) $data['dns'] = ""; + if(!isset($data['ip'])) $data['ip'] = ""; + + if(update_host($data['hostid'], $data['name'], $data['port'], $data['status'], + $data['useip'], $data['dns'], $data['ip'], $data['templates'], null, $data['groups'])) + { + info('Host ['.$data['name'].'] updated'); + } + + break; // case + 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 '.DBid2nodeid('groupid').'='.$ZBX_CURNODEID. + ' and name='.zbx_dbstr($this->element_data))))) + { + error('Missed group ['.$this->element_data.']'); + break; // case + } + + if(!in_array($group["groupid"], $this->accessible_groups)) + { + error('Group ['.$this->element_data.'] skipped - Access deny.'); + break; // case + } + + array_push($this->data[XML_TAG_HOST]['groups'], $group["groupid"]); + + break; // case + case XML_TAG_APPLICATION: + if(!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) + break; //case + + if(!isset($this->data[XML_TAG_ITEM])) + break; //case + + if(!($application = DBfetch(DBselect('select applicationid from applications'. + ' where '.DBid2nodeid('applicationid').'='.$ZBX_CURNODEID. + ' and name='.zbx_dbstr($this->element_data). + ' and hostid='.$this->data[XML_TAG_HOST]['hostid'])))) + { + $applicationid = add_application($this->element_data, $this->data[XML_TAG_HOST]['hostid']); + } + else + { + $applicationid = $application['applicationid']; + } + + $this->data[XML_TAG_ITEM]['applications'][] = $applicationid; + + break; // case + case XML_TAG_TEMPLATE: + if(!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) + break; //case + + if(!($template = DBfetch(DBselect('SELECT DISTINCT host, hostid '. + ' FROM hosts'. + ' WHERE '.DBid2nodeid('hostid').'='.$ZBX_CURNODEID. + ' AND host='.zbx_dbstr($this->element_data))))) + { + error('Missed template ['.$this->element_data.']'); + break; // case + } + + if(!in_array($template["hostid"], $this->accessible_hosts)) + { + error('Template ['.$this->element_data.'] skipped - Access deny.'); + break; // case + } + + $this->data[XML_TAG_HOST]['templates'][$template["hostid"]] = $template['host']; + break; // case + case XML_TAG_ITEM: + 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']) + { + info('Item ['.$data['description'].'] skipped - user rule for host'); + break; // case + } + + error('Item ['.$data['description'].'] skipped - missed host'); + break; // case + } + + if(!isset($data['description'])) $data['description'] = ""; + 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['multiplier'])) $data['multiplier'] = 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'] = ''; + if(!isset($data['delay_flex'])) $data['delay_flex'] = ''; + if(!isset($data['trapper_hosts'])) $data['trapper_hosts'] = ''; + if(!isset($data['snmp_community'])) $data['snmp_community'] = ''; + if(!isset($data['snmp_oid'])) $data['snmp_oid'] = ''; + if(!isset($data['snmp_port'])) $data['snmp_port'] = 161; + if(!isset($data['snmpv3_securityname'])) $data['snmpv3_securityname'] = ''; + 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['applications'])) $data['applications'] = array(); + + if(!empty($data['valuemap'])) + { + if( $valuemap = DBfetch(DBselect('select valuemapid from valuemaps '. + ' where '.DBid2nodeid('valuemapid').'='.$ZBX_CURNODEID. + ' and name='.zbx_dbstr($data['valuemap']))) ) + { + $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 '. + DBid2nodeid('itemid').'='.$ZBX_CURNODEID))) + { /* exist */ + if($this->item['exist']==1) /* skip */ + { + info('Item ['.$data['description'].'] skipped - user rule'); + break; + } + + if( !isset($data['valuemapid']) ) + $data['valuemapid'] = $item['valuemapid']; + + update_item( + $item['itemid'], + $data['description'], + $data['key'], + $this->data[XML_TAG_HOST]['hostid'], + $data['delay'], + $data['history'], + $data['status'], + $data['type'], + $data['snmp_community'], + $data['snmp_oid'], + $data['value_type'], + $data['trapper_hosts'], + $data['snmp_port'], + $data['units'], + $data['multiplier'], + $data['delta'], + $data['snmpv3_securityname'], + $data['snmpv3_securitylevel'], + $data['snmpv3_authpassphrase'], + $data['snmpv3_privpassphrase'], + $data['formula'], + $data['trends'], + $data['logtimefmt'], + $data['valuemapid'], + $data['delay_flex'], + array_unique(array_merge( + $data['applications'], + get_applications_by_itemid($item['itemid']) + )), + $item['templateid']); + } + else + { /* missed */ + if($this->item['missed']==1) /* skip */ + { + info('Item ['.$data['description'].'] skipped - user rule'); + break; // case + } + + if( !isset($data['valuemapid']) ) + $data['valuemapid'] = 0; + + add_item( + $data['description'], + $data['key'], + $this->data[XML_TAG_HOST]['hostid'], + $data['delay'], + $data['history'], + $data['status'], + $data['type'], + $data['snmp_community'], + $data['snmp_oid'], + $data['value_type'], + $data['trapper_hosts'], + $data['snmp_port'], + $data['units'], + $data['multiplier'], + $data['delta'], + $data['snmpv3_securityname'], + $data['snmpv3_securitylevel'], + $data['snmpv3_authpassphrase'], + $data['snmpv3_privpassphrase'], + $data['formula'], + $data['trends'], + $data['logtimefmt'], + $data['valuemapid'], + $data['delay_flex'], + $data['applications']); + } + + break; // case + case XML_TAG_TRIGGER: + if(!isset($data['expression'])) $data['expression'] = ''; + if(!isset($data['description'])) $data['description'] = ''; + if(!isset($data['priority'])) $data['priority'] = 0; + if(!isset($data['status'])) $data['status'] = 0; + if(!isset($data['comments'])) $data['comments'] = ''; + if(!isset($data['url'])) $data['url'] = ''; + + 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']) + { + info('Trigger ['.$data['description'].'] skipped - user rule for host'); + break; // case + } + if(strstr($data['expression'],'{HOSTNAME}')) + { + error('Trigger ['.$data['description'].'] skipped - missed host'); + break; // case + } + } + else + { + $data['expression'] = str_replace('{HOSTNAME}', + $this->data[XML_TAG_HOST]['name'], + $data['expression']); + + if($trigger = DBfetch(DBselect('select distinct t.triggerid,t.templateid '. + ' from triggers t,functions f,items i '. + ' where t.triggerid=f.triggerid and f.itemid=i.itemid'. + ' and i.hostid='.$this->data[XML_TAG_HOST]['hostid']. + ' and t.description='.zbx_dbstr($data['description'])))) + { /* exist */ + if($this->trigger['exist']==1) /* skip */ + { + info('Trigger ['.$data['description'].'] skipped - user rule'); + break; // case + } + + update_trigger( + $trigger['triggerid'], + $data['expression'], + $data['description'], + $data['priority'], + $data['status'], + $data['comments'], + $data['url'], + get_trigger_dependences_by_triggerid($trigger['triggerid']), + $trigger['templateid']); + + break; // case + } + else /* missed */ + { + // continue [add_trigger] + } + } + + if($this->trigger['missed']==1) /* skip */ + { + info('Trigger ['.$data['description'].'] skipped - user rule'); + break; // case + } + + add_trigger( + $data['expression'], + $data['description'], + $data['priority'], + $data['status'], + $data['comments'], + $data['url']); + + break; // case + case XML_TAG_GRAPH: + if(isset($data['error'])) + { + error('Graph ['.$data['name'].'] skipped - error occured'); + break; // case + } + + if(!isset($data['yaxistype'])) $data['yaxistype'] = 0; + if(!isset($data['show_work_period'])) $data['show_work_period'] = 1; + if(!isset($data['show_triggers'])) $data['show_triggers'] = 1; + if(!isset($data['graphtype'])) $data['graphtype'] = 0; + if(!isset($data['yaxismin'])) $data['yaxismin'] = 0; + if(!isset($data['yaxismax'])) $data['yaxismax'] = 0; + if(!isset($data['items'])) $data['items'] = array(); + + 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']) + { + info('Graph ['.$data['name'].'] skipped - user rule for host'); + break; // case + } + foreach($data['items'] as $id) + + if(strstr($data['name'],'{HOSTNAME}')) + { + error('Graph ['.$data['name'].'] skipped - missed host'); + break; // case + } + } + else + { + if($graph = DBfetch(DBselect('select distinct g.graphid, g.templateid'. + ' from graphs g, graphs_items gi, items i'. + ' where g.graphid=gi.graphid and gi.itemid=i.itemid'. + ' and g.name='.zbx_dbstr($data['name']). + ' and i.hostid='.$this->data[XML_TAG_HOST]['hostid']))) + { /* exist */ + if($this->graph['exist']==1) /* skip */ + { + info('Graph ['.$data['name'].'] skipped - user rule'); + break; // case + } + + $data['graphid'] = $graph['graphid']; + + update_graph( + $data['graphid'], + $data['name'], + $data['width'], + $data['height'], + $data['yaxistype'], + $data['yaxismin'], + $data['yaxismax'], + $data['show_work_period'], + $data['show_triggers'], + $data['graphtype'], + $graph['templateid']); + + DBexecute('delete from graphs_items where graphid='.$data['graphid']); + } + else + { /* missed */ + // continue [add_group] + } + } + + if(!isset($data['graphid'])) + { + if($this->graph['missed']==1) /* skip */ + { + info('Graph ['.$data['name'].'] skipped - user rule'); + break; // case + } + + $data['graphid'] = add_graph( + $data['name'], + $data['width'], + $data['height'], + $data['yaxistype'], + $data['yaxismin'], + $data['yaxismax'], + $data['show_work_period'], + $data['show_triggers'], + $data['graphtype']); + } + + foreach($data['items'] as $item) + { + add_item_to_graph( + $data['graphid'], + $item['itemid'], + $item['color'], + $item['drawtype'], + $item['sortorder'], + $item['yaxisside'], + $item['calc_fnc'], + $item['type'], + $item['periods_cnt']); + } + break; // case + case XML_TAG_GRAPH_ELEMENT: + if(!isset($this->data[XML_TAG_GRAPH])) + break; // case + + $data['key'] = explode(':', $data['item']); + if(count($data['key']) < 2) + { + $this->data[XML_TAG_GRAPH]['error'] = true; + error('Incorrect element for graph ['.$data['name'].']'); + break; // case + } + + $data['host'] = array_shift($data['key']); + $data['key'] = implode(':', $data['key']); + + if(isset($this->data[XML_TAG_HOST]['name'])) + { + $data['host'] = str_replace('{HOSTNAME}',$this->data[XML_TAG_HOST]['name'],$data['host']); + } + + if(!isset($data['drawtype'])) $data['drawtype'] = 0; + if(!isset($data['sortorder'])) $data['sortorder'] = 0; + if(!isset($data['color'])) $data['color'] = 'Dark Green'; + if(!isset($data['yaxisside'])) $data['yaxisside'] = 1; + if(!isset($data['calc_fnc'])) $data['calc_fnc'] = 2; + if(!isset($data['type'])) $data['type'] = 0; + if(!isset($data['periods_cnt'])) $data['periods_cnt'] = 5; + + if(!($item = DBfetch(DBselect('select i.itemid from items i,hosts h'. + ' where h.hostid=i.hostid and i.key_='.zbx_dbstr($data['key']). + ' and h.host='.zbx_dbstr($data['host']))))) + { + $this->data[XML_TAG_GRAPH]['error'] = true; + + error('Missed item ['.$data['key'].'] for host ['.$data['host'].']'); + break; // case + } + + $data['itemid'] = $item['itemid']; + + array_push($this->data[XML_TAG_GRAPH]['items'], $data); + + break; // case + /*case XML_TAG_SCREEN: + case XML_TAG_SCREEN_ELEMENT: + break; // case*/ + default: + if(isset($this->sub_node) && isset($this->main_node)) + { + $main_node = array_pop($this->main_node); + $this->data[$main_node][$this->sub_node] = $this->element_data; + array_push($this->main_node, $main_node); + } + $this->sub_node = null; + return; + } + + unset($this->data[$name], $data); + + array_pop($this->main_node); + } + + function Parse($file) + { + $this->main_node = null; + $this->sub_node = null; + $this->data = null; + + $xml_parser = xml_parser_create(); + + xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); + + xml_set_element_handler($xml_parser, array(&$this, "StartElement"), array(&$this, "EndElement")); + + xml_set_character_data_handler($xml_parser, array(&$this, "characterData")); + + if (!($fp = fopen($file, "r"))) + { + error("could not open XML input"); + xml_parser_free($xml_parser); + return false; + } + else + { + while ($data = fread($fp, 4096)) + { + if (!xml_parse($xml_parser, $data, feof($fp))) + { + error( sprintf("XML error: %s at line %d", + xml_error_string(xml_get_error_code($xml_parser)), + xml_get_current_line_number($xml_parser)) + ); + fclose($fp); + xml_parser_free($xml_parser); + return false; + } + } + fclose($fp); + } + xml_parser_free($xml_parser); + + $this->main_node = null; + $this->sub_node = null; + $this->data = null; + + return true; + } + + function SetRules($host, $template, $item, $trigger, $graph) + { + $this->host = $host; + $this->template = $template; + $this->item = $item; + $this->trigger = $trigger; + $this->graph = $graph; + } + } + + class CZabbixHostImport + { + function CZabbixHostImport() + { + } + + function Parse($file) + { + if (!($fp = fopen($file, "r"))) + { + error("could not open XML input"); + xml_parser_free($xml_parser); + return false; + } + else + { + while(!feof($fp)) + { + $len = fgets($fp); + echo $len.'
'."\n"; + } + fclose($fp); + } + + info('Underconstructor!'); + return true; + } + } +?> diff --git a/frontends/php/include/locales/en_gb.inc.php b/frontends/php/include/locales/en_gb.inc.php index c3518f06..16d3c185 100644 --- a/frontends/php/include/locales/en_gb.inc.php +++ b/frontends/php/include/locales/en_gb.inc.php @@ -1307,6 +1307,8 @@ // Errors 'S_INCORRECT_DESCRIPTION'=> 'Incorrect description', - 'S_CANT_FORMAT_TREE'=> 'Can\'t format Tree' + 'S_CANT_FORMAT_TREE'=> 'Can\'t format Tree', + 'S_UNSUPPORTED_FILE_FORMAT'=> 'Unsupported file format', + 'S_UNSUPPORTED_VERSION_OF_IMPORTED_DATA'=> 'Unsupported version of imported data' ); ?> -- cgit