Ad Widget

Collapse

HOST.CREATE with API (Perl+JSON)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spider87
    Junior Member
    • Apr 2013
    • 4

    #1

    HOST.CREATE with API (Perl+JSON)

    #!/usr/bin/perl

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

    my $client = new JSON::RPC::Client;
    my $uri = 'http://*.*.*.*/zabbix/api_jsonrpc.php';

    my $json_obj = {
    jsonrpc => "2.0",
    method => "user.login",
    params => {
    user => "root",
    password => "password"
    },
    id => 1
    };
    my $response = $client->call($uri, $json_obj);
    my $authHash;
    if ($response->content->{'result'}) {
    $authHash = $response->content->{'result'};
    print "Auth successful - ID hash: " . $authHash . "\n";
    }
    else {
    print "sorry, incorrect auth\n";
    exit;
    }
    $client = new JSON::RPC::Client;

    $json_obj = {
    jsonrpc => '2.0',
    method => 'host.create',
    params =>
    {
    host => 'test_host',
    interfaces =>
    {
    type => '1',
    main => '1',
    useip => 1,
    ip => '1.2.3.4',
    dns => 'teszt123',
    port => '10050',
    },
    groups =>
    {
    groupid => '52',
    }
    },
    id => 2,
    auth => "$authHash"
    };
    $response = $client->call($uri, $json_obj);
    print Dumper($response);


    This was the response: "'data' => 'No permissions to referred object or it does not exist!',
    'message' = > 'Application error.',
    'code' => -32500

    what's the problem?
    The authentication part of the script is run as well.
    The API access was enabled for root user.
  • Pavels
    Member
    • Oct 2011
    • 83

    #2
    It's likely that group "52" doesn't exist or you don't have write permission to it.

    Comment

    • spider87
      Junior Member
      • Apr 2013
      • 4

      #3
      re.

      Originally posted by Pavels
      It's likely that group "52" doesn't exist or you don't have write permission to it.

      groupid "52" is exists
      maybe I don't have write permission...
      I use zabbix frontend username and password for the API login in perl script
      but the database user is anouther user
      could this be a fault?

      Comment

      • Pavels
        Member
        • Oct 2011
        • 83

        #4
        You can try and login in with the API user to the frontend and make sure he has all the required permissions.

        Comment

        • spider87
          Junior Member
          • Apr 2013
          • 4

          #5
          Originally posted by Pavels
          You can try and login in with the API user to the frontend and make sure he has all the required permissions.
          root user is also API user with all permission
          I delegated all permission what I could on the frontend (for root user)
          but this perl script doesn't work

          I have only one postgresql user. (not on the frontend)
          This user's name isn't root. (postgresql user's name is zabbix)

          could this be a fault?

          Comment

          • Pavels
            Member
            • Oct 2011
            • 83

            #6
            It doesn't matter what your postgres user name is. Try to login to the forntend with the root user and see if you can access the group.

            Comment

            • spider87
              Junior Member
              • Apr 2013
              • 4

              #7
              Originally posted by Pavels
              It doesn't matter what your postgres user name is. Try to login to the forntend with the root user and see if you can access the group.
              well, I can login with root user, and I delegated every permission for root user.
              I ran the perl script again and I got this message:

              Authentication successful. Auth ID: e2d91d7b6ece9040c8efb91c8763900b
              Host creation failed: Dumper(JSON::RPC::ReturnObject=HASH(0x9bbdb10));



              my perl script now:


              #!/usr/bin/perl
              use 5.010;
              use strict;
              use warnings;
              use JSON::RPC::Client;
              use Data:umper;

              my $client = new JSON::RPC::Client;
              my $url = 'http://*.*.*.*/zabbix/api_jsonrpc.php';
              my $authID;
              my $response;

              my $json = {
              jsonrpc => '2.0',
              method => 'user.login',
              params => {
              user => 'root',
              password => '*******'
              },
              id => 1
              };

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

              die "Authentication failed\n" unless $response->content->{'result'};

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

              $json = {
              jsonrpc=> '2.0',
              method => 'host.create',
              params =>
              {
              host => 'JSON Test3',
              interfaces => [{
              type => 1,
              main => 1,
              ip => '192.168.0.3',
              dns => '',
              port => '10050',
              }],
              groups =>{
              groupid =>' 52',
              },
              templates =>{
              templateid => '273',
              },
              },
              auth => "$authID",
              id => 2
              };
              $response = $client->call($url, $json);

              die "Host creation failed: Dumper($response);\n" unless $response->content->{'result'};

              foreach my $host (@{$response->content->{result}}) {
              }



              My perl script what I used it first, sent this message again:

              This was the response: "'data' => 'No permissions to referred object or it does not exist!',
              'message' = > 'Application error.',
              'code' => -32500
              Last edited by spider87; 10-04-2013, 10:58.

              Comment

              • Pavels
                Member
                • Oct 2011
                • 83

                #8
                This turned out to be a weird API bug. It has problems when groups are passed as a single object instead of an array of objects. Change "groups =>{groupid =>'52'}" to "groups =>[{groupid =>'52'}]" and it should work.

                Comment

                • fizwit
                  Junior Member
                  • Jan 2014
                  • 3

                  #9
                  host.create Pytyon

                  Zabbix 2.0
                  API 1.4
                  Python 2.6.6
                  Example Python API host.create with Python that works. groups needs to be a list

                  #!/usr/bin/env python
                  from pyzabbix import ZabbixAPI

                  zapi = ZabbixAPI( zabbix_url )
                  zapi.login(username, password )

                  result = zapi.host.create(
                  host='newHost',
                  status=1,
                  interfaces={'type':1,
                  'main':1,
                  'useip':1,
                  'port':10050,
                  'ip':'192.168.0.1',
                  'dns':'newHost-e1'},
                  groups=[{'groupid':35}], # adding the Brackets is mandatory!
                  templates={'templateid':11115}
                  )

                  print result

                  >>>{u'hostids': [u'11117']}

                  Comment

                  Working...