Ad Widget

Collapse

Can't get Host inventory from Zabbix Api.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EdwardK
    Junior Member
    • Nov 2016
    • 18

    #1

    Can't get Host inventory from Zabbix Api.

    Hey Guys!

    Beend working with Zabbix Api lately,managed to succesfully connect to it and auth. token(Using David Webb Java plugin).

    The problem is,i need to get host latitude ang longitude,but i'm strugling to write JSON request to get this data.

    Used this to connect to Zabbix(Succesfully)

    Code:
    import org.json.JSONObject;
    import org.json.JSONException;
    import com.goebl.david.Webb;
     
    public class Zabbix_auth {
     
       public static void main(String[] args) throws JSONException {
     
         try {
     
           JSONObject mainJObj = new JSONObject();
           JSONObject paramJObj = new JSONObject();
     
           mainJObj.put("jsonrpc", "2.0");
           mainJObj.put("method", "user.login");
     
           paramJObj.put("user", "username");
           paramJObj.put("password", "password"); 
     
           mainJObj.put("params", paramJObj);
           mainJObj.put("id", "1");
           
           
           
         
     
           Webb webb = Webb.create();
     
           System.out.println("Data to send: " + mainJObj.toString());
     
           JSONObject result = webb.post("http://localhost/api_jsonrpc.php")
                                        .header("Content-Type", "application/json")
                                        .useCaches(false)
                                        .body(mainJObj)
                                        .ensureSuccess()
                                        .asJsonObject()
                                        .getBody();
     
           System.out.println("Authentication token: " + result.getString("result"));
           
           
           
     
         } catch (JSONException je) {
     
           System.out.println("Error creating JSON request to Zabbix API..." + je.getMessage());
           
            }
         
        
         
         
         
     
      }
     
    }
    Trying to use this,but no luck lately,getting only host ID's.

    Code:
    import org.json.JSONObject;
    import org.json.JSONException;
    import com.goebl.david.Webb;
     
    public class Host_info_get {
     
       public static void main(String[] args) throws JSONException {
     
         try {
     
           JSONObject mainJObj = new JSONObject();
           JSONObject paramJObj = new JSONObject();
     
           mainJObj.put("jsonrpc", "2.0");
           mainJObj.put("method", "host.get");
          
           paramJObj.put("output", "location_lat");
          
           
           mainJObj.put("params", paramJObj);
           mainJObj.put("id", "2");
           mainJObj.put("auth", "TOKEN");
           
           Webb webb = Webb.create();
     
           System.out.println("Data to send: " + mainJObj.toString());
     
           JSONObject result = webb.post("http://localhost/api_jsonrpc.php")
                                        .header("Content-Type", "application/json")
                                        .useCaches(false)
                                         .body(mainJObj)
                                        .ensureSuccess()
                                        .asJsonObject()
                                        .getBody();
     
           System.out.print("Host info: " + result.getString("result"));
           
           
           
     
         } catch (JSONException je) {
     
           System.out.println("Unable to get to host" + je.getMessage());
           
            }
         
     
      }
     
    }
    Any ideas how i can retrive host inventory?

    Thanks)
  • jan.garaj
    Senior Member
    Zabbix Certified Specialist
    • Jan 2010
    • 506

    #2
    You will need more params, at least (check Zabbix API doc for your Zabbix version):
    Code:
    "selectInventory":["location_lat"]
    Zapix query:
    Devops Monitoring Expert advice: Dockerize/automate/monitor all the things.
    My DevOps stack: Docker / Kubernetes / Mesos / ECS / Terraform / Elasticsearch / Zabbix / Grafana / Puppet / Ansible / Vagrant

    Comment

    • EdwardK
      Junior Member
      • Nov 2016
      • 18

      #3
      Thanks for your response.

      I've updated my code :

      Code:
      import org.json.JSONObject;
      import org.json.JSONException;
      import com.goebl.david.Webb;
       
      public class Host_info_get {
       
         public static void main(String[] args) throws JSONException {
       
           try {
       
             JSONObject mainJObj = new JSONObject();
             JSONObject paramJObj = new JSONObject();
       
             mainJObj.put("jsonrpc", "2.0");
             mainJObj.put("method", "host.get");
             mainJObj.put("inventory_mode", "1");
             
             
             paramJObj.put("output", "searchInventory");
             paramJObj.put("output", "selectInventory");
             paramJObj.put("output", "location_lat");
             paramJObj.put("selectInventory", "location_lat");
             paramJObj.put("selectInventory", "location_lon");
            
             paramJObj.put("object", "searchInventory");
             paramJObj.put("object", "selectInventory");
             mainJObj.put("params", paramJObj);
             mainJObj.put("id", "2");
             mainJObj.put("auth", "34f3c3dtghfhgfhrt380537245de");
             
             Webb webb = Webb.create();
       
             System.out.println("Data to send: " + mainJObj.toString());
       
             JSONObject result = webb.post("http://localhost/api_jsonrpc.php")
                                          .header("Content-Type", "application/json")
                                          .useCaches(false)
                                           .body(mainJObj)
                                          .ensureSuccess()
                                          .asJsonObject()
                                          .getBody();
       
             System.out.print("Host info: " + result.getString("result"));
             
             
             
       
           } catch (JSONException je) {
       
             System.out.println("Unable to get to host" + je.getMessage());
             
              }
           
       
        }
      And that's what im getting in console :

      Code:
      Host info: [{"hostid":"10151","inventory":{"hostid":"10151"}},{"hostid":"10152","inventory":{"hostid":"10152"}},.......
      I guess i've reached inventory,but cant get parameters i need....

      Comment

      Working...