Ad Widget

Collapse

Zabbix 2.0.4 api json host.create

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Evan.Anderson
    Member
    • Jun 2009
    • 42

    #1

    Zabbix 2.0.4 api json host.create

    I was using this perl script to generate an xml, but I've updated it and been trying to use the api via perl script to add hosts, but no luck so far. I can get the api to do a host.get and return successfully, so I believe that it's just my json object format. Could someone please look at this format and tell me if I'm missing required data or a bracket or brace.
    Okay so this part works:
    my $client = new JSON::RPC::Client;
    my $url = 'http://localhost/zabbix/api_jsonrpc.php';
    my $authID;
    my $response;

    my $json = {
    jsonrpc => "2.0",
    method => "user.login",
    params => {
    user => "admin",
    password => "zabbix"
    },
    id => 1
    };
    $response = $client->call($url, $json);

    # Check if response was successful
    die "Authentication failed\n" unless $response->content->{'result'};

    $authID = $response->content->{'result'};
    print "Authentication successful. Auth ID: " . $authID . "\n";

    I can get the authID and can pass it to the next step.
    Next I try this
    $json = {
    jsonrpc => "2.0",
    method => "host.create",
    macros => [],
    inventory_mode => 1,
    inventory => {
    notes => localtime(time)
    },
    params => {
    host => "$results",
    interfaces => [
    {
    type => 1,
    main => 1,
    useip => 1,
    ip => "$ipaddress",
    dns => "$results",
    port => "10050"
    }
    ],
    groups => [
    {
    groupid => "9"
    }
    ],
    templates => [
    {
    templateid => "10089",
    templateid => "10092"
    }
    ],
    },
    auth => "$authID",
    id => 2
    };
    $response = $client->call($url, $json);
    # Check if response was successful
    die "Host creation failed: Dumper($response);\n" unless $response->content->{'result'};

    So I'm also wondering about the id part of the object, should it match the user.login id or should it be sequential?

    Please let me know if you need more information.
  • BDiE8VNy
    Senior Member
    • Apr 2010
    • 680

    #2
    I think via the ID it's possible to match a response to its corresponding request.
    What's the output of:
    Code:
    print Dumper($json);
    Edit:
    Please put the output between the code tags provided for such non-structured text.
    Last edited by BDiE8VNy; 01-03-2013, 18:47.

    Comment

    • Pavels
      Member
      • Oct 2011
      • 83

      #3
      You are using an incorrect JSON request, please refer to the API docs for an example of how the request should look



      I think via the ID it's possible to match a response to its corresponding request.
      Correct.

      Comment

      • Evan.Anderson
        Member
        • Jun 2009
        • 42

        #4
        Originally posted by BDiE8VNy
        I think via the ID it's possible to match a response to its corresponding request.
        What's the output of:
        Code:
        print Dumper($json);
        Edit:
        Please put the output between the code tags provided for such non-structured text.
        Code:
        $VAR1 = {
                  'inventory' => {
                                   '4' => 2,
                                   'notes' => 30,
                                   '62' => 0,
                                   '58' => 8,
                                   '113' => 1
                                 },
                  'inventory_mode' => 1,
                  'jsonrpc' => '2.0',
                  'version' => '1.1',
                  'macros' => [],
                  'params' => {
                                'interfaces' => [
                                                  {
                                                    'dns' => 'dns.fqdn.com',
                                                    'useip' => 1,
                                                    'ip' => 'x.x.x.x',
                                                    'type' => 1,
                                                    'port' => '10050',
                                                    'main' => 1
                                                  }
                                                ],
                                'groups' => [
                                              {
                                                'groupid' => '9'
                                              }
                                            ],
                                'templates' => [
                                                 {
                                                   'templateid' => '10092'
                                                 }
                                               ],
                                'host' => 'dns.fqdn.com'
                              },
                  'auth' => '179c331eb7be992b3c34bd2069c38a07',
                  'method' => 'host.create',
                  'id' => 2
                };
        First time posting code, hopefully I've done it correctly, I just dabble in coding, so bear with me. I've change the ip address and host fields, but the rest is unedited. I've tried to follow the api suggestions for host create, hopefully you can help get this working, after which I'll post my code so everyone can use it.
        Last edited by Evan.Anderson; 04-03-2013, 17:06. Reason: typo

        Comment

        • Evan.Anderson
          Member
          • Jun 2009
          • 42

          #5
          Just bumpin' still looking for help.

          Also, can someone elaborate on the proper use of auth and id? Is auth static after login? Is id static after login? Or interative?
          Last edited by Evan.Anderson; 09-03-2013, 23:45.

          Comment

          • BDiE8VNy
            Senior Member
            • Apr 2010
            • 680

            #6
            As mentioned before the ID is used to associate responses to their requests and are only relevant if honored by your application.

            Do you already have tried to use the 'property names' instead of the unique IDs for inventory fields?
            What is the output of:
            Code:
            print Dumper($response);

            Comment

            • Evan.Anderson
              Member
              • Jun 2009
              • 42

              #7
              Originally posted by BDiE8VNy
              As mentioned before the ID is used to associate responses to their requests and are only relevant if honored by your application.

              Do you already have tried to use the 'property names' instead of the unique IDs for inventory fields?
              What is the output of:
              Code:
              print Dumper($response);
              My bad, I thought we were looking for $json, here is $response:

              Code:
              $VAR1 = bless( {
                               'version' => 0,
                               'content' => {
                                              'jsonrpc' => '2.0',
                                              'error' => {
                                                           'data' => 'Host with the same name "as-n5k-2.nmmc.com" already exists.',
                                                           'message' => 'Invalid params.',
                                                           'code' => -32602
                                                         },
                                              'id' => 2
                                            },
                               'jsontext' => '{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"Host with the same name \\"as-n5k-2.nmmc.com\\" already exists."},"id":2}',
                               'is_success' => 0
                             }, 'JSON::RPC::ReturnObject' );
              Host creation failed:  Dumper(JSON::RPC::ReturnObject=HASH(0x8c404e8));
              From that, it's quite obvious what going on, the host already exists...indeed it does already exist, so I'll figure out why my script is adding when it should be passing it by, thanks. I'll post my finished script for use, once I figure out this last part...

              Comment

              • BDiE8VNy
                Senior Member
                • Apr 2010
                • 680

                #8
                Originally posted by Evan.Anderson
                [..]I thought we were looking for $json [...]
                The aim to look for $json first was to see the json object in the form it was passed to LWP::UserAgent.

                Comment

                • Evan.Anderson
                  Member
                  • Jun 2009
                  • 42

                  #9
                  Thank you for your help, i've just added 49 hosts via cdp discovery, do you think I should post my script in this existing post, or create a new post under zabbix cookbook and point to it?

                  Comment

                  • BDiE8VNy
                    Senior Member
                    • Apr 2010
                    • 680

                    #10
                    I would suggest to create a new post in cookbooks and refer that in this post.

                    Comment

                    • Evan.Anderson
                      Member
                      • Jun 2009
                      • 42

                      #11
                      Thank you, if your curious what this was all about check it out here: https://www.zabbix.com/forum/showthread.php?t=39845

                      Comment

                      Working...