Ad Widget

Collapse

Guest user login via API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaragunde
    Junior Member
    • Jan 2011
    • 13

    #1

    Guest user login via API

    Hi,

    I'm building a custom web application on top of Zabbix to show logged data in special ways. To access Zabbix, I'm using its JSON API.

    In my setup, guest user will have permissions on some data, and I'm trying to retrieve those data through the API, but I don't know exactly how to do that.

    If I try to access a resource directly, without specifying auth, I get this:
    request:
    Code:
    {
    "id":0,
    "auth":"",
    "jsonrpc":"2.0",
    "method":"hostgroup.get",
    "params":{"output":"extend"}
    }
    response:
    Code:
    {
    "jsonrpc":"2.0",
    "error":{
        "code":-32602,
        "message":"Invalid params.",
        "data":"Not authorized"
    },
    "id":0
    }
    And if I try to use the login operation with user 'guest' or empty, I get this:

    request:
    Code:
    {
    "id":0,
    "auth": null,
    "jsonrpc":"2.0",
    "method":"user.authenticate",
    "params":{"user":"guest","password":""}
    }
    response:
    Code:
    {
    "jsonrpc":"2.0",
    "error":{
        "code":-32602,
        "message":"Invalid params.",
        "data":"No API access"
    },
    "id":0
    }
    And BTW, the documentation about the login operation of the api (http://www.zabbix.com/documentation/1.8/api/user/login) is empty :S .

    Any help would be appreciated. Thank you!
  • Aly
    ZABBIX developer
    • May 2007
    • 1126

    #2
    Does guest user have enabled access to API?
    Zabbix | ex GUI developer

    Comment

    • jaragunde
      Junior Member
      • Jan 2011
      • 13

      #3
      You are right. API access for Guests users group was still disabled. The solution for my problem was enabling it and trying to log in with 'guest' user name, without a password:
      request:
      Code:
      {
      "id":0,
      "auth":null,
      "jsonrpc":"2.0",
      "method":"user.authenticate",
      "params":{"user":"guest","password":""}
      response:
      Code:
      {
      "jsonrpc":"2.0",
      "result":"d13cf7a7affe8d39cc7413b94729a200",
      "id":0
      }
      With the result of this operation, I can access normally to other API operations, like hostgroup.get, without any problem .

      Thank you very much!

      Comment

      • s12k34
        Junior Member
        • Jan 2011
        • 13

        #4
        how to

        Could you explain how to do run this command?
        I am new in Zabbix and want to use API in java application.
        Please give some detail about using json rpc API in Zabbix.
        Thanks...

        Comment

        • jaragunde
          Junior Member
          • Jan 2011
          • 13

          #5
          To use the API, you'll have to use a library to make HTTP requests to query the URL http://your-zabbix-server/api_jsonrpc.php. You'll have to do a POST request with some JSON parameters according to the operations specified in http://www.zabbix.com/documentation/1.8/api.

          I don't have any examples with Java, but I can post you some PHP code. SimpleHttpRequest is a custom class to manage HTTP requests easily, it uses cURL internally.

          Code:
                  $data = array(
                      'id' => 0,
                      'auth' => $authToken,
                      'jsonrpc' => '2.0',
                      'method' => $method,
                      'params' => $parameters);
          
                  $dataJson = json_encode($data);
          
                  require_once('SimpleHttpRequest.php');
                  $request = new SimpleHttpRequest($zabbixUrl . '/api_jsonrpc.php');
                  $request->setupPost($dataJson);
                  $request->addHeader("Content-type", "application/json");
                  $request->init();
          
                  $response = $request->doRequest();
                  if(!$response) {
                      error_log($request->getError());
                      throw new Exception($request->getError());
                  }
                  $request->clearPost();
                  $request->close();
          
                  return json_decode($response);

          Comment

          • s12k34
            Junior Member
            • Jan 2011
            • 13

            #6
            Thank you very much jaragunde.
            I implemented java solution and send this site also.

            Comment

            Working...