Sample Code: Get Syslog Events
- Create a Queue with "queueName":
public String createQueue(String queueName) throws Exception {
String mediatype = "application/hornetq.jms.queue+xml";
Credentials credentials = new UsernamePasswordCredentials("uname", "password");
HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY, credentials);
httpClient.getParams().setAuthenticationPreemptive(true);
ClientExecutor clientExecutor = new ApacheHttpClientExecutor(httpClient);
ClientRequest request = new ClientRequest("/api/space/hornet-q/queues", clientExecutor);
String requestBody = "<queue name=\"" + queueName + "\">" +
"<durable>true</durable>" +
"</queue>";
request.body(mediatype, requestBody);
ClientResponse<String> response = request.post(String.class);
int status = response.getStatus();
String queueURL = response.getHeaders().get("Location");
if (status == 201) // OK!
System.out.println("QUEUE created : " + queueURL);
else
throw new InternalServerException("QUEUE creation failed!");
return queueURL;
}
- Subscribing for get-syslog-events createQueue():
Credentials credentials = new
UsernamePasswordCredentials("uname", "password"); HttpClient
httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY,
credentials);
httpClient.getParams().setAuthenticationPreemptive(true);
ClientExecutor clientExecutor = new
ApacheHttpClientExecutor(httpClient); // request_URL will
point to
/api/space/device-management/devices/stop-syslog-events/{id} ,
where id should be valid SyslogTask Id String request_URL =
"http://<<Server_IP>>/api/space/device-management/devices/get-syslog-events?queue=http://<<Server_IP>>/api/hornet-q/queues/jms.queue.space";
String
mediatype =
"application/vnd.net.juniper.space.device-management.get-syslog-events+xml;version=1;charset=UTF-8";
ClientRequest
request = new ClientRequest(request_URL, clientExecutor);
request.header("Content-Type", mediatype); String input =
"<get-syslog-events> <devices>
<device>/api/space/device-management/devices/12345</device>
</devices> <text-patterns>
<text-pattern>netconf subsystem</text-pattern>
</text-patterns> </get-syslog-events>";
request.body(mediatype, input);
ClientResponse<SyslogTask> response =
request.post(SyslogTask.class); int status =
response.getStatus(); System.out.println(" Status=" + status);
if (status == 200) // OK! { System.out.println("SyslogTask ID
: " + response.getEntity().getId()); } else {
System.out.println("get-syslog-events failed......"); }
(iii)
Polling the Queue ClientRequest request = new
ClientRequest(request_URL); ClientResponse res =
request.get(); String pullConsumers = (String) ((ArrayList)
res.getHeaders().get("msg-pull-consumers")).get(0); request =
new ClientRequest(pullConsumers); res = request.post(); String
ackNext = (String) ((ArrayList)
res.getHeaders().get("msg-consume-next")).get(0); while (true)
{ request = new ClientRequest(ackNext); res =
request.header("Accept-Wait", "10").post(); if
(res.getStatus() == 503) { ackNext = (String) ((ArrayList)
res.getHeaders().get("msg-consume-next")).get(0); } else if
(res.getStatus() == 200) { String result = (String)
res.getEntity(String.class);
if(result.contains("<data>") { // Parse the data tag.
Can use XPath break; } ackNext = (String) ((ArrayList)
res.getHeaders().get("msg-consume-next")).get(0); } else {
throw new RuntimeException("Failure! " + res.getStatus()); } }
(iv) Parse the Result: There would asynchronous syslog
messages matching the text-patterns that would be generated
from device. Following is the Sample output of the messages
generated. <?xml version="1.0" encoding="UTF-8"
standalone="yes"?> <progress-update
xsi:type="percentage-complete"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<taskId>100</taskId>
<state>INPROGRESS</state> <data>
<DeviceSysLog> <deviceID>1234</deviceID>
<sysLog> <38&>1 2010-12-08T11:01:28.849-08:00
blennies sshd 93873 - - subsystem request for netconf
</sysLog> </DeviceSysLog> </data>
</progress-update> User can parse the "<data>" tag
to get the syslogs.
(v) UnSubscribing syslog using
stop-syslog-events API Credentials credentials = new
UsernamePasswordCredentials("uname", "password"); HttpClient
httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY,
credentials);
httpClient.getParams().setAuthenticationPreemptive(true);
ClientExecutor clientExecutor = new
ApacheHttpClientExecutor(httpClient); String request_URL =
"http://<<Server_IP>>:8080/api/space/device-management/devices/stop-syslog-events/<<SyslogTaskId>>";
ClientRequest
request = new ClientRequest(request_URL, clientExecutor);
ClientResponse response = request.post(); int status =
response.getStatus(); if (status == 200) // OK! {
System.out.println("stop-syslog-events successful...."); }
else { System.out.println("stop-syslog-events failed......");
}