A "syslog event" is an entry written to a device's system log. You can subscribe to
receive data for syslog events from a device as they occur using the
api/space/device-management/devices/get-syslog-events
API, and unsubscribe using
the api/space/device-management/devices/stop-syslog-events
API.
get-syslog-events
relies on a HornetQ queue to make a long-running request
to subscribe to syslog events on a specified device. This page provides code examples showing
how to use these APIs.
The following code examples show how to use the two syslog Device Management APIs to receive and stop receiving syslog events from a device. The examples illustrate a typical sequence of use of the APIs in an application:
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(request_URL, clientExecutor);
String queueString = "<queue name=\"space\"><durable>true</durable></queue>";
request.body(mediatype, queueString);
ClientResponse<String> response = request.post(String.class);
int status = response.getStatus();
if (status == 201) // OK!
{
System.out.println("QUEUE created : " + response.getHeaders().get("Location"));
else {
System.out.println("QUEUE creation FAILED : ");
}
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......");
}
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());
}
}
Parse the asynchronous syslog messages received from the subscribed device by matching text-patterns. The following is an example of a received message.
<?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>lt;38>1 2010-12-08T11:01:28.849-08:00 blennies sshd 93873 - - subsystem request for netconf</sysLog>
</DeviceSysLog>
</data>
</progress-update>
Your code can parse the <data> tag to get the syslog entries.
Unsubscribe for syslog events using the stop-syslog-events
API, as shown below.
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......");
}