Ad Widget

Collapse

PHP script from import xml dumps manually

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mkein
    Junior Member
    • Dec 2011
    • 7

    #1

    PHP script from import xml dumps manually

    Hello all,

    im trying to get a little php script to import xml templates generated using a modified perl script i found on this forum, and would like you guys to point to some api doc, or some advice to get this to work.


    At this time, i started taking the part ot the hosts.php that does the import and seems to be working but nothing is added on the zabbix manager. If i add the xml file through the web its working, so theres no issue with the xml.

    IF i enter bad chrs on the xml, the script complaints about it and failed to end, so seems like the basic functionality is ok, this i miss some parameters to get to the db maybe ?


    PHP Code:
    #!/usr/bin/php

    <?php
    # Zabbix
    # Copyright (C) 2000-2011 Zabbix SIA
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; either version 2 of the License, or
    # (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    ?>

    <?php
            
    require_once('config2.inc.php');
            require_once(
    'include/db.inc.php');
            require_once(
    'include/export.inc.php');
            
    $rules = array(T_ZBX_STRO_OPTnullDB_IDnull);

            if (
    $argc != || in_array($argv[1], array('--help''-help''-h''-?'))) {
                    print 
    "PHP script to import xml item templates to zabbix from commandline\n";
                    print 
    "Syntax: import.php <xml_file>\n\n";
                    exit (
    1);
            } else {
                    
    $file$argv[1];
            }


            print 
    "[+]  Started import from file $file\n";
            
    DBstart();
            
    $result zbxXML::import($file);
            if(
    $result) {
                    print 
    "[+]  Parging XML file $file\n";
                    
    $result zbxXML::parseMain($rules);
                    if (
    $result) {
                            
    $result DBend($result);
                            if (
    $result) print "[+]  Data imported and commited on DB\n";
                    } else {
                            print 
    "\n[-] Failed while parsing $file\n";

                    }
            } else {
                    print 
    "\n[-] Failed while loading XML file $file\n";
            }
            print 
    "\n";

    ?>

    Thank you all
    Javi
  • mkein
    Junior Member
    • Dec 2011
    • 7

    #2
    I added a modified config.inc.php just to get the error function to avoid being redirected to the index auth page.

    I removed the following (diff output)

    PHP Code:
    > if (!defined('ZBX_PAGE_NO_AUTHORIZATION') && !defined('ZBX_RPC_REQUEST')) {
    >       if (!
    CWebUser::checkAuthentication(get_cookie('zbx_sessionid'))) {
    >               require_once(
    'include/locales/en_gb.inc.php');
    >               
    process_locales();

    >               include(
    'index.php');
    >               exit();
    >       }
    260a269,282
    > }
    > else {
    >       
    CWebUser::$data = array(
    >               
    'alias' => ZBX_GUEST_USER,
    >               
    'userid'=> 0,
    >               
    'lang'  => 'en_gb',
    >               
    'type'  => '0',
    >               
    'node'  => array(
    >                       
    'name'  => '- unknown -',
    >                       
    'nodeid'=> 0)
    >               );

    >       
    $USER_DETAILS CWebUser::$data;
    > } 

    The data vars seems to be related to the web access, but maybe are used somehow during the import.
    I usually removed the update Template linkage on the import webpage, how can i set this value ?


    Thank you
    Javi

    Comment

    Working...