Ad Widget

Collapse

Zabbix API & Host creation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pierre-hoffmann
    Senior Member
    • Jan 2008
    • 133

    #1

    Zabbix API & Host creation

    Hi,

    I'm trying to use new Zabbix API to create an host with Zabbix php-class API but it doesn't work:
    (http://andrewfarley.com/php/zabbix-1...php-class-v1-0 )
    -> List hostGroup OK:
    Code:
    $five_hostgroup = ZabbixAPI::fetch_array('hostgroup','get',
         array('extendoutput'=>1, 'limit'=>5))
        or die('Unable to get hosts: '.print_r(ZabbixAPI::getLastError(),true));
    
    echo "<li>List hostgroups: <pre>"
          .print_r($five_hostgroup, true)."</pre></li>\n";
    Result:
    Code:
    Array
    (
        [0] => Array
            (
                [groupid] => 1
                [name] => Templates
                [internal] => 0
            )
    
        [1] => Array
            (
                [groupid] => 5
                [name] => O_NETWARE
                [internal] => 0
            )
    [...]
    -> Create host KO:
    Code:
    ZabbixAPI::query('host', 'add', array("host"=>toto,
                                     "groupids"=>array(0=>6,1=>5)
                                    )
    ) 
        or die('<li>Unable to create host: '.
        print_r(ZabbixAPI::getLastError(),true).'</li>');
    Result:
    Code:
    CURL Options: Array
    (
        [19913] => 1
        [13] => 30
        [78] => 5
        [81] => 
        [64] => 
        [52] => 1
        [74] => 1
        [10023] => Array
            (
                [0] => Content-Type: application/json-rpc
                [1] => User-Agent: ZabbixAPI v1.0 - 
               http://andrewfarley.com/zabbix_php_api
            )
    
        [10036] => POST
        [10015] => {"auth":"7667ee5f9f6d813d5a726898f4a5c799","method":"host.add",
                     "id":1,"params":{"host":"toto","groupids":[6,5]},
                     "jsonrpc":"2.0"}
        [10102] => gzip
    )
    Code:
    Got response from API: ({"jsonrpc":"2.0","error":{"code":-32602,
    "message":"Invalid params.", "data":"Action does not exists"},"id":1})
    
    Response decoded: (Array
    (
        [jsonrpc] => 2.0
        [error] => Array
            (
                code => -32602
                message => Invalid params.
                data => [COLOR="Red"]Action does not exists[/COLOR]
            )
    
        [id] => 1
    )
    What's my mistake ?

    Regards,
    Last edited by pierre-hoffmann; 25-01-2010, 10:36.
    P.Hoffmann
    System & Network Admin.
    __________________________
    Zabbix version 1.8.1
    Hosts monitored 1300
    OS Novell SLES 10 SP2
    __________________________
  • Aly
    ZABBIX developer
    • May 2007
    • 1126

    #2
    Replace "groupids" param with "groups" containing array of objects type of group.

    ZabbixAPI::query('host', 'create',
    array(
    "host"=>"toto",
    "groups"=>array(array("groupid"=>6), array("groupid"=>5))
    )
    )
    Last edited by Aly; 19-01-2010, 16:57.
    Zabbix | ex GUI developer

    Comment

    • nelsonab
      Senior Member
      Zabbix Certified SpecialistZabbix Certified Professional
      • Sep 2006
      • 1233

      #3
      Originally posted by pierre-hoffmann
      Code:
      Got response from API: ({"jsonrpc":"2.0","error":{"code":-32602,
      "message":"Invalid params.", "data":"Action does not exists"},"id":1})
      
      Response decoded: (Array
      (
          [jsonrpc] => 2.0
          [error] => Array
              (
                  code => -32602
                  message => Invalid params.
                  data => [COLOR=Red]Action does not exists[/COLOR]
              )
      
          [id] => 1
      )
      What's my mistake ?
      The error message is actually quite descriptive and correct on this one. :-)

      Host.add does not exist. However host.create does. Line 636 api/classes/class.chost.php from 1.8 release.

      However the documentation in the source is misleading. I'd open a bug to this effect and that one of the following should happen. 1) The documentation should be changed from add to create, or 2) the function name should be changed from create to add. Option one seems to have less impact.
      RHCE, author of zbxapi
      Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
      Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

      Comment

      • Aly
        ZABBIX developer
        • May 2007
        • 1126

        #4
        Yepp I missed this one, host.create should be. i'll fix my previous post.
        Zabbix | ex GUI developer

        Comment

        • nelsonab
          Senior Member
          Zabbix Certified SpecialistZabbix Certified Professional
          • Sep 2006
          • 1233

          #5
          Staring at the API code long before it was being used is starting to pay off I guess... :-D
          RHCE, author of zbxapi
          Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
          Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

          Comment

          • pierre-hoffmann
            Senior Member
            • Jan 2008
            • 133

            #6
            Hi,

            Thanks for your responses;

            i've used zabbix manuel but it seem buggy ??
            (http://www.zabbix.com/documentation/...jects/host#add)

            Code:
            ZabbixAPI::query('host', 'create', array("host"=>toto,
                                             "groups"=>array(array("group"=>6),array("group"=>5))
                                            )
            )
            now i've another error: (my api user have super Admin Zabbix right)
            Code:
            (
                code => -32500
                [message] => Application error.
                [data] => [ CHost::create ] You do not have enough rights for operation
            )
            i've see something here (http://www.zabbix.com/forum/showthread.php?p=57718)
            but i don't understand russian

            Regards,
            Last edited by pierre-hoffmann; 20-01-2010, 16:44.
            P.Hoffmann
            System & Network Admin.
            __________________________
            Zabbix version 1.8.1
            Hosts monitored 1300
            OS Novell SLES 10 SP2
            __________________________

            Comment

            • nelsonab
              Senior Member
              Zabbix Certified SpecialistZabbix Certified Professional
              • Sep 2006
              • 1233

              #7
              Originally posted by pierre-hoffmann

              Code:
              (
                  code => -32500
                  [message] => Application error.
                  [data] => [ CHost::create ] You do not have enough rights for operation
              )
              Two things, does the user you are using for the API call have permission to create a host in that group if you were to do it through the GUI? I'm also assuming you've got API privs enabled for this user anyway.
              RHCE, author of zbxapi
              Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
              Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

              Comment

              • Aly
                ZABBIX developer
                • May 2007
                • 1126

                #8
                group -> groupid!!!

                Code:
                ZabbixAPI::query('host', 'create', array("host"=>toto,
                                                 "groups"=>array(array("[U]groupid[/U]"=>6),array("[U]groupid[/U]"=>5))
                                                )
                )
                Zabbix | ex GUI developer

                Comment

                • pierre-hoffmann
                  Senior Member
                  • Jan 2008
                  • 133

                  #9
                  Originally posted by Aly
                  group -> groupid!!!

                  Code:
                  ZabbixAPI::query('host', 'create', array("host"=>toto,
                                                   "groups"=>array(array("[U]groupid[/U]"=>6),array("[U]groupid[/U]"=>5))
                                                  )
                  )
                  Oups now it Work ... Sorry

                  Regards,
                  P.Hoffmann
                  System & Network Admin.
                  __________________________
                  Zabbix version 1.8.1
                  Hosts monitored 1300
                  OS Novell SLES 10 SP2
                  __________________________

                  Comment

                  • pierre-hoffmann
                    Senior Member
                    • Jan 2008
                    • 133

                    #10
                    Hi,

                    Creation work great, but another question what's good syntaxe for host.delete :

                    Code:
                    ZabbixAPI::query('host', 'delete', array(0=>$host_id) )
                            or die('<li>Unable to delete host: '
                    .print_r(ZabbixAPI::getLastError(),true).'</li>');
                    echo "<li>Delete a host toto</li>\n"
                    
                    [10015] => {"auth":"66ad350dbb3385768bbfbdcddb673b65","method":
                    "host.delete","id":1,"params":["11498"],"jsonrpc":"2.0"}
                        [10102] => gzip
                    )
                    
                    [error] => Array
                    (
                                code => -32500
                                message => Application error.
                                data => [ CHost::delete ] [COLOR="red"]S_NO_PERMISSION[/COLOR]
                    )
                    Regards
                    P.Hoffmann
                    System & Network Admin.
                    __________________________
                    Zabbix version 1.8.1
                    Hosts monitored 1300
                    OS Novell SLES 10 SP2
                    __________________________

                    Comment

                    • pierre-hoffmann
                      Senior Member
                      • Jan 2008
                      • 133

                      #11
                      Oups i've find the solution:

                      Good syntaxe for host.delete was :
                      Code:
                      ZabbixAPI::query('host', 'delete', array(hostid=>$host_id) )
                      Debug:
                      Code:
                      {"auth":"7f3d4fbdb2d558b2434b6e847a2acaec",
                      "method":"host.delete","id":1,"params":{"hostid":"11503"},"jsonrpc":"2.0"}
                      
                      Got response from API:
                      [jsonrpc] => 2.0
                          [result] => Array
                              (
                                  [0] => Array
                                      (
                                          [hostid] => 11503
                                          [proxy_hostid] => 0
                                          [host] => toto
                                          [dns] => toto.mydomain.fr
                                          [useip] => 0
                                          [ip] => 0.0.0.0
                                          [port] => 0
                                          [status] => 0
                                          [disable_until] => 0
                                          [error] => 
                                          [available] => 0
                                          [errors_from] => 0
                                          [lastaccess] => 0
                                          [inbytes] => 0
                                          [outbytes] => 0
                                          [useipmi] => 0
                                          [ipmi_port] => 623
                                          [ipmi_authtype] => 0
                                          [ipmi_privilege] => 0
                                          [ipmi_username] => 
                                          [ipmi_password] => 
                                          [ipmi_disable_until] => 0
                                          [ipmi_available] => 0
                                          [snmp_disable_until] => 0
                                          [snmp_available] => 0
                                          [maintenanceid] => 0
                                          [maintenance_status] => 0
                                          [maintenance_type] => 0
                                          [maintenance_from] => 0
                                          [ipmi_ip] => 
                                          [ipmi_errors_from] => 0
                                          [snmp_errors_from] => 0
                                          [ipmi_error] => 
                                          [snmp_error] => 
                                      )
                      P.Hoffmann
                      System & Network Admin.
                      __________________________
                      Zabbix version 1.8.1
                      Hosts monitored 1300
                      OS Novell SLES 10 SP2
                      __________________________

                      Comment

                      • to6ko
                        Junior Member
                        Zabbix Certified Specialist
                        • Jun 2007
                        • 5

                        #12
                        What about host link with template

                        I'v try to link a host to a template.
                        Acording to documentation :
                        ( http://www.zabbix.com/documentation/...jects/template )
                        7. linkHosts Parameters: templateid (string) ;hostids (array)

                        I test this :

                        ZabbixAPI::query('template', 'linkhosts', array("hostids"=>array("100100000010629"), "templateid"=>"100100000010053"));

                        but the response was:

                        Got response from API: ({"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"Action (linkhosts) does not exist"},"id":1})

                        Any suggestions ?

                        Comment

                        • to6ko
                          Junior Member
                          Zabbix Certified Specialist
                          • Jun 2007
                          • 5

                          #13
                          just found it

                          ZabbixAPI::query('host', 'create', array("host"=>"test7",
                          "groups"=>array(array("groupid"=>"100100000000005" )),"ip"=>"10.10.3.2", "useip"=>"1","templates"=>"100100000010053"
                          ))

                          where 100100000010053 is the templateid of template you want to link the host to

                          Comment

                          • tof233
                            Member
                            • Nov 2010
                            • 94

                            #14
                            Hello,
                            I also have problems with creating applications :
                            Code:
                            $createappli=array("name"=>"Plone",
                            "hosts"=>array(array("hostid" =>10001))
                            );
                            $application_id = ZabbixAPI::query('application','create',$createappli)
                                or die('Unable to ADD Application: '.print_r(ZabbixAPI::getLastError(),true));
                            I got this error :
                            [CODE]
                            Unable to ADD Application: Array
                            (
                            Code:
                             => -32602
                                [message] => Invalid params.
                                [data] => [ CApplication::create ] Cannot create application
                            )

                            Comment

                            • tof233
                              Member
                              • Nov 2010
                              • 94

                              #15
                              Finally I fixed it with
                              Code:
                              $createappli=array("name"=>"Plone_".$plonename,"hostid" =>$template_id
                              );
                              $application = ZabbixAPI::fetch_array('application','create',$createappli)
                                  or die('Unable to ADD Application: '.print_r(ZabbixAPI::getLastError(),true));
                              $application_id=$application["applicationids"][0];

                              Comment

                              Working...