Ad Widget

Collapse

host.get fails under 1.8 api?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cirrhus9.com
    Member
    • Feb 2012
    • 58

    #1

    host.get fails under 1.8 api?

    Hello:
    I am just starting to explore some basic remote information gathering from our zbx 1.8.10 host.

    I found an example on the blog at Getting started with Zabbix API and I can authenticate fine.
    so I move on to Getting list of hosts on the same page but it returns no hosts.
    PHP Code:
    #!/usr/bin/perl

    use 5.010;
    use 
    strict;
    use 
    warnings;
    use 
    JSON::RPC::Client;
    use 
    Data::Dumper;

    # Authenticate yourself
    my $client = new JSON::RPC::Client;
    my $url 'http://some.ip.address/zabbix/api_jsonrpc.php';
    my $authID;
    my $response;

    my $json = {
    jsonrpc => "2.0",
    method => "user.login",
    params => {
    user => "user",
    password => "secret"
    },
    id => 1
    };

    $response $client->call($url$json);

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

    $authID $response->content->{'result'};

    # Get list of all hosts using authID

    $json = {
    jsonrpc=> '2.0',
    method => 'host.get',
    params =>
    {
    output => ['hostid','name'],# get only host id and host name
    sortfield => 'name',        # sort by host name
    },
    id => 2,
    auth => "$authID",
    };
    $response $client->call($url$json);

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

    print 
    "List of hosts\n-----------------------------\n";
    foreach 
    my $host (@{$response->content->{result}}) {
    print 
    "Host ID: ".$host->{hostid}." Host: ".$host->{name}."\n";

    I do get
    Code:
    List of hosts
    -----------------------------
    Can someone help me out and show me the error of my ways?

    Thank you.

    Edit: The topic is not accurate, it does NOT fail, it simply returns no hosts.
    Sorry about that.
    Last edited by cirrhus9.com; 23-07-2013, 21:04.
  • cirrhus9.com
    Member
    • Feb 2012
    • 58

    #2
    is bump allowed?

    Comment

    • Pada
      Senior Member
      • Apr 2012
      • 236

      #3
      I don't know the API really, but it seems like the request options are invalid for 1.8 - or at the very least they don't return anything for me on 1.8.11.

      When I changed the request to the following, I got lots of info back:
      Code:
      $json = {
      jsonrpc=> '2.0',
      method => 'host.get',
      params =>
      {
      output => 'extend', #['hostid','name'],
      sortfield => 'host',   # 'name'
      },
      id => 2,
      auth => "$authID",
      };
      If you want to print all of the response, use the following line:
      Code:
      print Dumper($response->content);

      Comment

      • cirrhus9.com
        Member
        • Feb 2012
        • 58

        #4
        Pada:

        Thank you!
        Code:
        $json = {
        jsonrpc=> '2.0',
        method => 'host.get',
        params =>
        {
        output => 'extend', #['hostid','name'],
        sortfield => 'hostid'.'host',   # 'name'
        },
        id => 2,
        auth => "$authID",
        };
        $response = $client->call($url, $json);
        Dumper($response->content);
        
        # Check if response was successful
        die "host.get failed\n" unless $response->content->{result};
        
        print "List of hosts\n-----------------------------\n";
        foreach my $host (@{$response->content->{result}}) {
        print "Host ID: ".$host->{hostid}." Host: ".$host->{host}."\n";
        }
        works from here.

        Comment

        Working...