Ad Widget

Collapse

Zabbix API Login

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diegodias
    Junior Member
    • Oct 2010
    • 9

    #1

    Zabbix API Login

    Hi,
    I am having trouble logging in with the Zabbix (JSON-rpc) API. I am not using the python or ruby clients, since I will build my own (JAVA) client.
    This is what I am trying to do.

    curl -i -X POST -H 'Content-Type: application/json' -d '{"params": {"password": "pass", "user": "user"}, "jsonrpc": "2.0", "method": "user.authenticate"}' http://<ip>/api_jsonrpc.php

    And I get the return

    HTTP/1.1 200 OK
    Date: Mon, 18 Oct 2010 15:07:56 GMT
    Server: Apache/2.2.3 (CentOS)
    X-Powered-By: PHP/5.1.6
    Content-Length: 2
    Connection: close
    Content-Type: text/html; charset=UTF-8

    []

    Can anyone help me with the correct params?
  • gescheit
    Senior Member
    • Jul 2007
    • 156

    #2
    curl -i -X POST -H 'Content-Type: application/json-rpc' -d '{"params": {"password": "pass", "user": "user"}, "jsonrpc": "2.0", "method": "user.authenticate","auth": "", "id": 0}' http://<ip>/api_jsonrpc.php

    Comment

    • diegodias
      Junior Member
      • Oct 2010
      • 9

      #3
      I solved it yesterday...
      Actually the problem was not with the call (I did include the auth and id). The problem was on the zabbix configuration... my user didnt have api access.

      Comment

      • s12k34
        Junior Member
        • Jan 2011
        • 13

        #4
        how

        how did you build your own java for using zabbix api?
        could you give detail information?
        I want to do the same thing too.
        thanks...

        Comment

        • diegodias
          Junior Member
          • Oct 2010
          • 9

          #5
          I am using the gson lib to convert classes into Strings on json format and vice-versa.

          Also using HttpClient to make the calls to the zabbix api. Following its documentation.

          Comment

          • angelhc
            Senior Member
            Zabbix Certified Specialist
            • Nov 2009
            • 226

            #6
            Hello,
            I'm so interested on this issue, please can you paste some example?
            What I want to do is a php web page to the system administrators when they can add host, items and so on, to managed the monitoring environment without any understanding of zabbix system.

            Thanks.
            Number of hosts 1600,Number of items +90k,Number of triggers +22k, Number of users +100, New values per second +1270

            http://zabbixes.wordpress.com/

            Comment

            • s12k34
              Junior Member
              • Jan 2011
              • 13

              #7
              You can find Java solution, PHP users may put the PHP solution...

              Code:
              import java.io.ByteArrayInputStream;
              import java.io.FileNotFoundException;
              import java.io.IOException;
              import java.io.InputStream;
              import java.io.UnsupportedEncodingException;
              
              import org.apache.commons.httpclient.HttpClient;
              import org.apache.commons.httpclient.HttpException;
              import org.apache.commons.httpclient.methods.PutMethod;
              import org.json.JSONException;
              import org.json.JSONObject;
              
              public class ZabbixApiClient {
              	private static String ZABBIX_API_URL = "http://1.2.3.4/zabbix/api_jsonrpc.php"; // 1.2.3.4 is your zabbix_server_ip
              	
              	public static void testClient() throws JSONException, FileNotFoundException, UnsupportedEncodingException {
              	
              		HttpClient client = new HttpClient();
              		
              		PutMethod putMethod = new PutMethod(ZABBIX_API_URL);
              		putMethod.setRequestHeader("Content-Type", "application/json-rpc"); // content-type is controlled in api_jsonrpc.php, so set it like this
              		
              		// create json object for apiinfo.version 
              		JSONObject jsonObj=new JSONObject("{\"jsonrpc\":\"2.0\",\"method\":\"apiinfo.version\",\"params\":[],\"auth\":\"a6e895b98fde40f4f7badf112fd983bf\",\"id\":2}");
              		
              		putMethod.setRequestBody(fromString(jsonObj.toString())); // put the json object as input stream into request body 
              		
              		System.out.println("jsonObj:\n"+jsonObj);
              		
              		String loginResponse = "";
              		
              		try {
              			client.executeMethod(putMethod); // send to request to the zabbix api
              			
              			loginResponse = putMethod.getResponseBodyAsString(); // read the result of the response
              			
              			System.out.println("loginResponse: \n"+loginResponse); // print the result of the response
              			
              			// Work with the data using methods like...
              			JSONObject obj = new JSONObject(loginResponse); 
              			String id = obj.getString("id");
              			String result = obj.getString("result");
              			System.out.println("id:"+id);
              			System.out.println("result:"+result);
              			
              		} catch (HttpException e) {
              			e.printStackTrace();
              		} catch (IOException e) {
              			e.printStackTrace();
              		}		
              	}
              	
              	public static InputStream fromString(String str) throws UnsupportedEncodingException {
              		byte[] bytes = str.getBytes("UTF-8");
              		return new ByteArrayInputStream(bytes);
              	}
              
              	
              	public static void main(String[] args) {
              		try {
              			testClient();
              		} catch (JSONException e) {
              			e.printStackTrace();
              		} catch (FileNotFoundException e) {
              			e.printStackTrace();
              		} catch (UnsupportedEncodingException e) {
              			e.printStackTrace();
              		}
              	}
              
              }
              After the execution of above code, output is like this:

              Code:
              jsonObj:
              {"id":2,"method":"apiinfo.version","params":[],"jsonrpc":"2.0","auth":"a6e895b98fde40f4f7badf112fd983bf"}
              loginResponse: 
              {"jsonrpc":"2.0","result":"1.1","id":2}
              id:2
              result:1.1

              Comment

              • Alexander Fedulov
                Junior Member
                • Sep 2010
                • 2

                #8
                Originally posted by diegodias
                Hi,
                I am having trouble logging in with the Zabbix (JSON-rpc) API. I am not using the python or ruby clients, since I will build my own (JAVA) client.
                ...
                Hi, diegodias!

                I was recently using python zabbix api calls through Jython out of my Java programm, but this is definitely not the best solution. Did you finish your API? It would be great to have at least some reference implementation to spare time implementing my own. Thanks.

                Comment

                • diegodias
                  Junior Member
                  • Oct 2010
                  • 9

                  #9
                  Hi Alexander,

                  Yes, I have finished it.
                  I can probably make the library as a shared library later so the Zabbix community can use it and update it as fast as the Zabbix API is being updated.
                  Here is an example of authentication:

                  Code:
                   private String authenticate(){
                  		
                  		Authenticate authenticate = new Authenticate(zabbixUser,zabbixPassword);
                  		ZabbixCallAPI<Authenticate> zabbixCallAPI = new ZabbixCallAPI<Authenticate>();
                  		zabbixCallAPI.setAuth(null);
                  		zabbixCallAPI.setMethod("user.authenticate");
                  		zabbixCallAPI.setParams(authenticate);
                  		String json = gson.toJson(zabbixCallAPI,new TypeToken<ZabbixCallAPI<Authenticate>>() {}.getType());
                  		String response = this.callPost(json);
                  		ZabbixResult<String> zabbixResult  = new ZabbixResult<String>();
                  		zabbixResult = gson.fromJson(response, new TypeToken<ZabbixResult<String>>() {}.getType());
                  		return zabbixResult.getResponse();
                  	}
                  Code:
                  public class ZabbixCallAPI<T> {
                  
                  	private String jsonrpc = "2.0";
                  	
                  	private String auth;
                  	
                  	private String method;
                  
                  	private T params;
                  	
                  	private int id = 0;
                  	
                  	public ZabbixCallAPI(String auth,String method,T params){
                  		this.auth = auth;
                  		this.method = method;
                  		this.params = params;
                  	}
                  	public ZabbixCallAPI(){
                  	}
                  	public int getId() {
                  		return id;
                  	}
                  
                  	public void setId(int id) {
                  		this.id = id;
                  	}
                  
                  	public T getParams() {
                  		return params;
                  	}
                  
                  	public void setParams(T params) {
                  		this.params = params;
                  	}
                  
                  	public String getJsonrpc() {
                  		return jsonrpc;
                  	}
                  
                  	public void setJsonrpc(String jsonrpc) {
                  		this.jsonrpc = jsonrpc;
                  	}
                  
                  	public String getAuth() {
                  		return auth;
                  	}
                  
                  	public void setAuth(String auth) {
                  		this.auth = auth;
                  	}
                  
                  	public String getMethod() {
                  		return method;
                  	}
                  
                  	public void setMethod(String method) {
                  		this.method = method;
                  	}
                  Code:
                  public class ZabbixResult<T>{
                  
                  	private String jsonrpc = "2.0";
                  	
                  	private T result;
                  	
                  	private String id;
                  	
                  	public ZabbixResult(){
                  		
                  	}
                  	
                  	public ZabbixResult(T result){
                  		this.result = result;
                  	}
                  	
                  	public String getId() {
                  		return id;
                  	}
                  
                  	public void setId(String id) {
                  		this.id = id;
                  	}
                  
                  	public T getResponse() {
                  		return result;
                  	}
                  
                  	public void setResponse(T response) {
                  		this.result = response;
                  	}
                  
                  	public String getJsonrpc() {
                  		return jsonrpc;
                  	}
                  
                  	public void setJsonrpc(String jsonrpc) {
                  		this.jsonrpc = jsonrpc;
                  	}

                  Code:
                   private String callPost(String content) {
                  		HttpClient httpClient = new HttpClient();
                  		PostMethod postMethod = new PostMethod(zabbixAPIURL));
                  
                  		postMethod.addRequestHeader("Content-Type", "application/json-rpc");
                  		String response = null;
                  		try {
                  			StringRequestEntity stringRequestEntity = new StringRequestEntity(content,"application/json", "UTF-8");
                  			postMethod.setRequestEntity(stringRequestEntity);
                  			httpClient.executeMethod(postMethod);
                  			response = postMethod.getResponseBodyAsString();
                  		} catch (Exception e) {
                  			e.printStackTrace();
                  		} 
                  
                  		postMethod.releaseConnection();
                  		return response;
                  	}
                  I hope this helps!

                  The other actions u can do with the json-rpc Zabbix API goes similar to the authenticate one.

                  Comment

                  • Alexander Fedulov
                    Junior Member
                    • Sep 2010
                    • 2

                    #10
                    Hi diegodias!

                    thank you for your prompt reply. It would be very useful for me, and I am sure for many other members of the community if you could publish your Java Zabbix API library. Many thanks in advance!

                    Comment

                    Working...