Ad Widget

Collapse

Import Device XML command line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ataylo13
    Senior Member
    • Feb 2007
    • 122

    #1

    Import Device XML command line

    Is it possible to import the xml file from the command line? I am looking at a way to automate the management of our device list.
    Version : 1.8.8
    Current Configuration 1 Master and 3 Child Nodes
  • jean
    Member
    • Apr 2006
    • 85

    #2
    Hi !

    I'm also interested by this functionnality...

    Comment

    • NOB
      Senior Member
      Zabbix Certified Specialist
      • Mar 2007
      • 469

      #3
      Originally posted by jean
      Hi !

      I'm also interested by this functionnality...
      It's on the list of ToDos for version 1.6, anyway.
      XML import/export in the list means via command-line !

      Regards

      Norbert.

      Comment

      • ataylo13
        Senior Member
        • Feb 2007
        • 122

        #4
        Are you interpreting XML backup/restore as import/export. Because depending on how it is implemented can have dramatically different results.

        I would like to be able to use a script to insert an XML file with all the hosts I want monitored and it will update/add any differences.

        Does that clear it up at all?
        Version : 1.8.8
        Current Configuration 1 Master and 3 Child Nodes

        Comment

        • Niels
          Senior Member
          • May 2007
          • 239

          #5
          I'm also doubtful. I'm pretty sure I've heard that this import/export is simply a way of making a back-up of the configuration -- hosts, items and all that. Which would be very helpfull in that the only way to do that right now is a full DB bump.

          But since it's at 0% the exact meaning isn't all that important...

          Comment

          • stephen.soltesz
            Junior Member
            • Oct 2008
            • 9

            #6
            using curl to import xml

            Originally posted by ataylo13
            Is it possible to import the xml file from the command line? I am looking at a way to automate the management of our device list.
            I accomplished this using the commands below. substitute your hostname and admin password, and the appropriate path to your zabbix install.

            Code:
             curl -s --cookie cookiejar.txt --cookie-jar cookiejar.txt \
            		--form "enter=Enter" \
            		--form "name=Admin" \
            		--form "password=yourpassword" \
            		'http://your.hostname.com/zabbix/index.php?login=1'
            	# 0 - update , 1 - skip, 0 - add
            curl -s --cookie cookiejar.txt --cookie-jar cookiejar.txt \
            		--form "config=1" \
            		--form "import_file=@/root/zabbix_templates.xml"  \
            		--form "rules[host][exist]=0"  \
            		--form "rules[host][missed]=0"  \
            		--form "rules[template][exist]=0"  \
            		--form "rules[template][missed]=1"  \
            		--form "rules[item][exist]=0"  \
            		--form "rules[item][missed]=0"  \
            		--form "rules[trigger][exist]=0"  \
            		--form "rules[trigger][missed]=0"  \
            		--form "rules[graph][exist]=0"  \
            		--form "rules[graph][missed]=0"  \
            		--form "import=Import"  \
            		'http://your.hostname.com/zabbix/exp_imp.php'

            Comment

            • jean
              Member
              • Apr 2006
              • 85

              #7
              Nice, thank you ! It work's very well !

              Do you have a similar script for deleting all hosts before to import the new one ?

              Because, today, I have a script that make a global xml file to import the hosts, but before I need manually delete all hosts.

              Comment

              • stephen.soltesz
                Junior Member
                • Oct 2008
                • 9

                #8
                Originally posted by jean
                Do you have a similar script for deleting all hosts before to import the new one ?
                No, I don't. I'm guessing that you would need to know the hostid to do this. If you'd like to try, you can use the commands above as a template, and the following firefox plugin to expose the form information on any page. This is how I derived the curl commands above.



                Let us know if you find a way to do this.

                Thank you.

                Comment

                • lopatich
                  Junior Member
                  • Jul 2009
                  • 2

                  #9
                  Hi.
                  (sorry for english)
                  I try your script, but get "<strong>ERROR: Invalid URL parameters</strong>".
                  Zabbix 1.6.5

                  Comment

                  • lopatich
                    Junior Member
                    • Jul 2009
                    • 2

                    #10
                    Originally posted by lopatich
                    Hi.
                    (sorry for english)
                    I try your script, but get "<strong>ERROR: Invalid URL parameters</strong>".
                    Zabbix 1.6.5
                    FIXED
                    field "sid" required

                    Code:
                    #!/bin/sh
                    
                    zabbix_url="http://your.host.com/zabbix"
                    xml="$1"
                    
                    if [ "$xml" == "" ]; then
                            echo "Usage: $0 <xml>"
                            exit
                    fi
                    
                    curl -s --cookie /tmp/cookiejar.txt --cookie-jar /tmp/cookiejar.txt \
                                    --form "enter=Enter" \
                                    --form "name=admin" \
                                    --form "password=zabbix" \
                                    "${zabbix_url}/index.php?login=1" >/dev/null 2>&1
                    sid=$(curl -s --cookie /tmp/cookiejar.txt --cookie-jar /tmp/cookiejar.txt \
                                    "${zabbix_url}/index.php?login=1" \
                                    |grep sid |head -n 1  \
                                    |awk -Fsid= '{print $2}' \
                                    |awk -F\" '{print $1}')
                            # 0 - update , 1 - skip, 0 - add
                    curl -s --cookie /tmp/cookiejar.txt --cookie-jar /tmp/cookiejar.txt \
                                    --form "sid=${sid}" \
                                    --form "config=1" \
                                    --form "import_file=@${xml}" \
                                    --form "rules[host][exist]=0" \
                                    --form "rules[host][missed]=0" \
                                    --form "rules[template][exist]=0" \
                                    --form "rules[template][missed]=1" \
                                    --form "rules[item][exist]=0" \
                                    --form "rules[item][missed]=0" \
                                    --form "rules[trigger][exist]=0" \
                                    --form "rules[trigger][missed]=0" \
                                    --form "rules[graph][exist]=0" \
                                    --form "rules[graph][missed]=0" \
                                    --form "import=Import" \
                                    "${zabbix_url}/exp_imp.php"

                    Comment

                    • sebastianzenobio
                      Junior Member
                      • Aug 2011
                      • 4

                      #11
                      FIXED add cut -c -16 for the sid

                      FIXED

                      Add cut -c -16 for the sid


                      Originally posted by lopatich
                      FIXED
                      field "sid" required

                      Code:
                      #!/bin/sh
                      
                      zabbix_url="http://your.host.com/zabbix"
                      xml="$1"
                      
                      if [ "$xml" == "" ]; then
                              echo "Usage: $0 <xml>"
                              exit
                      fi
                      
                      curl -s --cookie /tmp/cookiejar.txt --cookie-jar /tmp/cookiejar.txt \
                                      --form "enter=Enter" \
                                      --form "name=admin" \
                                      --form "password=zabbix" \
                                      "${zabbix_url}/index.php?login=1" >/dev/null 2>&1
                      sid=$(curl -s --cookie /tmp/cookiejar.txt --cookie-jar /tmp/cookiejar.txt \
                                      "${zabbix_url}/index.php?login=1" \
                                      |grep sid |head -n 1  \
                                      |awk -Fsid= '{print $2}' \
                                      |awk -F\" '{print $1}'
                                      |cut -c -16 )
                      
                              # 0 - update , 1 - skip, 0 - add
                      curl -s --cookie /tmp/cookiejar.txt --cookie-jar /tmp/cookiejar.txt \
                                      --form "sid=${sid}" \
                                      --form "config=1" \
                                      --form "import_file=@${xml}" \
                                      --form "rules[host][exist]=0" \
                                      --form "rules[host][missed]=0" \
                                      --form "rules[template][exist]=0" \
                                      --form "rules[template][missed]=1" \
                                      --form "rules[item][exist]=0" \
                                      --form "rules[item][missed]=0" \
                                      --form "rules[trigger][exist]=0" \
                                      --form "rules[trigger][missed]=0" \
                                      --form "rules[graph][exist]=0" \
                                      --form "rules[graph][missed]=0" \
                                      --form "import=Import" \
                                      "${zabbix_url}/exp_imp.php"

                      Comment

                      • sebastianzenobio
                        Junior Member
                        • Aug 2011
                        • 4

                        #12
                        Fixed

                        Add cut -c -16 fot the sid

                        sid=$(curl -s --cookie /tmp/cookiejar.txt --cookie-jar /tmp/cookiejar.txt \
                        "${zabbix_url}/index.php?login=1" \
                        |grep sid |head -n 1 \
                        |awk -Fsid= '{print $2}' \
                        |awk -F\" '{print $1}'
                        |cut -c -16 )

                        Comment

                        Working...