TECHZEN Zenoss User Community ARCHIVE  

can't bind templates to a device via JSON API,please give me a hand!

Subject: can't bind templates to a device via JSON API,please give me a hand!
Author: [Not Specified]
Posted: 2015-12-27 21:28

Hi

I'm new to zenoss , I'm trying to use JOSN API to do some settings to zenoss,here is the java codes to bind templates to a device:

1******************************JsonApi_Set.java

public class JsonApi_Set {

private final static String ZENOSS_INSTANCE = "http://192.168.1.70:8080";

private final static String ZENOSS_USERNAME = "admin";

private final static String ZENOSS_PASSWORD = "zenoss";

private final static HashMap ROUTERS = new HashMap();

static {

ROUTERS.put("MessagingRouter", "messaging");

ROUTERS.put("EventsRouter", "evconsole");

ROUTERS.put("ProcessRouter", "process");

ROUTERS.put("ServiceRouter", "service");

ROUTERS.put("DeviceRouter", "device");

ROUTERS.put("NetworkRouter", "messaging");

ROUTERS.put("TemplateRouter", "template");

ROUTERS.put("DetailNavRouter", "detailnav");

ROUTERS.put("ReportRouter", "report");

ROUTERS.put("MibRouter", "mib");

ROUTERS.put("ZenPackRouter", "zenpack");

ROUTERS.put("NetworkRouter", "network");

ROUTERS.put("JobsRouter", "jobs");

}

private DefaultHttpClient httpclient = new DefaultHttpClient();

private ResponseHandler responseHandler = new BasicResponseHandler();

private JSONParser jsonParser = new JSONParser();

private int reqCount = 1;

/**

*

* @throws Exception

*/

// Constructor logs in to the Zenoss instance (getting the auth cookie)

public JsonApi_Set() throws Exception {

HttpPost httpost = new HttpPost(ZENOSS_INSTANCE +

"/zport/acl_users/cookieAuthHelper/login");

List nvps = new ArrayList ();

nvps.add(new BasicNameValuePair("__ac_name", ZENOSS_USERNAME));

nvps.add(new BasicNameValuePair("__ac_password", ZENOSS_PASSWORD));

nvps.add(new BasicNameValuePair("submitted", "true"));

nvps.add(new BasicNameValuePair("came_from", ZENOSS_INSTANCE +

"/zport/dmd"));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

// Response from POST not needed, just the cookie

HttpResponse response = httpclient.execute(httpost);

// Consume so we can reuse httpclient

response.getEntity().consumeContent();

}

// routerRequest is the main method used to communicate with the Zenoss instance

/**

*

* @param String router

* @param String method

* @param HashMap data

* @return

* @throws Exception

*/

private JSONObject routerRequest(String router, String method, HashMap data)

throws Exception {

// Construct standard URL for requests

HttpPost httpost = new HttpPost(ZENOSS_INSTANCE + "/zport/dmd/" +

ROUTERS.get(router) + "_router");

// Content-type MUST be set to 'application/json'

httpost.addHeader("Content-type", "application/json; charset=utf-8");

ArrayList packagedData = new ArrayList();

packagedData.add(data);

HashMap reqData = new HashMap();

reqData.put("action", router);

reqData.put("method", method);

reqData.put("data", packagedData);

reqData.put("type", "rpc");

// Increment the request count ('tid'). More important if sending multiple

// calls in a single request

reqData.put("tid", String.valueOf(this.reqCount++));

// Set the POST content to be a JSON-serialized version of request data

httpost.setEntity(new StringEntity(JSONValue.toJSONString(reqData)));

// Execute the request, and return the JSON-deserialized data

String response = httpclient.execute(httpost, responseHandler);

return (JSONObject)jsonParser.parse(response);

}

public JSONObject setBoundTemplates(String uid, ArrayList templateIds) throws Exception {//List templateIds

HashMap data = new HashMap();

data.put("uid", uid);

data.put("templateIds", templateIds);

return (JSONObject) this.routerRequest("DeviceRouter","setBoundTemplates", data);

}

}

2**********************main.java

public static void main(String[] args) throws Exception {

JsonApi_Set ja7 = new JsonApi_Set();//70

JSONObject devObject = ja7.getDevices("/zport/dmd/Devices/Server");

JSONArray devArray = (JSONArray) devObject.get("devices");

JSONObject dev0 = (JSONObject) devArray.get(0);

String dev0uid = (String) dev0.get("uid");//uidsetBoundTemplatesuid

JSONArray tempArray= ja7.getTemplate("/zport/dmd/Devices");

ArrayList templateIds = new ArrayList();

for(int i=0;i

JSONObject temp = (JSONObject) tempArray.get(i);

String uid = (String) temp.get("uid");

templateIds.add(uid);//uidsetBoundTemplatestemplateIds

}

System.out.println(ja7.setBoundTemplates(dev0uid, templateIds));//zenoss

ja7.close();

}

}

3************************************Result of Console

{"result":{"success":true},"action":"DeviceRouter","method":"setBoundTemplates","tid":"4","uuid":"412eff37-8a75-4db2-b1f5-bf02cec2bf88","type":"rpc"}

4***********Question:

From the result of Console I think the method was excute sucessfully ,and the tempalte should have been binded to the device.but in actual, it is not, it turns out that not only the target templates haven't been binded to the device,but also it makes the templates which the device possese before dissapeared . Do you know how to solve or debug this problem please do me a favor ! thank you!



Subject: I don't develop in JAVA, but
Author: Andrew Kirch
Posted: 2015-12-30 12:24

I don't develop in JAVA, but there's an API guide for both 4.x and 5.x here:
http://www.zenoss.com/resources/documentation
Additionally, there is a wiki guide to using the JSON API on our wiki:
http://wiki.zenoss.org/Working_with_the_JSON_API

Andrew Kirch

akirch@gvit.com

Need Zenoss support, consulting or custom development Look no further. Email or PM me!

Ready for Distributed Topology (collectors) for Zenoss 5 Coming May 1st from GoVanguard



Subject: Having the same issues
Author: [Not Specified]
Posted: 2016-03-17 00:48

Hi Jenny5w and team,

Was just writing to find out whether a solution for this is known

I am having the same exact issue. I'm trying to add a template via JSON.
I tried using:
* zProperties, zDeviceTemplates to add a new template, when adding the device, but this did not work and resulted in the templates attached to the device class becoming unbound,
* DeviceRouter's bindOrUnbindTemplate and
* DeviceRouter's setBoundTemplates but neither of which worked, but the DirectResponse returned a success value.

I haven't found anything useful in the documentation to help with this.

Please assist. Thanks.



< Previous
Quirky disabling behaviour with PythonCollector ZenPack 1.7.3
  Next
Error modeling Windows Server
>