use of org.opennms.plugins.elasticsearch.rest.IndexNameFunction in project opennms by OpenNMS.
the class AlarmEventToIndexTest method jestClientAlarmToESTest.
/**
* simple test to create an alarm change event which will create a new alarm in the alarm index
* and create an alarm change event in the alarm change index
*/
@Test
public void jestClientAlarmToESTest() {
LOG.debug("***************** start of test jestClientAlarmToESTest");
EventToIndex eventToIndex = new EventToIndex();
JestClient jestClient = null;
try {
// Get Jest client
String esusername = "";
String espassword = "";
String elasticsearchUrl = "http://localhost:9200";
RestClientFactory restClientFactory = new RestClientFactory(elasticsearchUrl, esusername, espassword);
IndexNameFunction indexNameFunction = new IndexNameFunction("yyyy.MM");
NodeCache nodeCache = new MockNodeCache();
eventToIndex.setRestClientFactory(restClientFactory);
eventToIndex.setNodeCache(nodeCache);
eventToIndex.setIndexNameFunction(indexNameFunction);
eventToIndex.setLogEventDescription(true);
eventToIndex.setArchiveRawEvents(true);
eventToIndex.setArchiveAlarms(true);
eventToIndex.setArchiveAlarmChangeEvents(true);
eventToIndex.setArchiveOldAlarmValues(true);
eventToIndex.setArchiveNewAlarmValues(true);
// create an alarm change event
EventBuilder eb = new EventBuilder(ALARM_ACKNOWLEDGED_EVENT, EVENT_SOURCE_NAME);
// copy in all values as json in params
eb.addParam("oldalarmvalues", TEST_ALARM_JSON_1);
eb.addParam("newalarmvalues", TEST_ALARM_JSON_1);
Event event = eb.getEvent();
event.setDbid(100);
event.setNodeid((long) 34);
// forward event to Elasticsearch
eventToIndex.forwardEvents(Collections.singletonList(event));
// waiting INDEX_WAIT_SECONDS seconds for index
try {
TimeUnit.SECONDS.sleep(INDEX_WAIT_SECONDS);
} catch (InterruptedException e) {
}
// send query to check that alarm has been created
jestClient = restClientFactory.getJestClient();
// search for resulting alarm
String query = "{\n" + "\n \"query\": {" + "\n \"match\": {" + "\n \"alarmid\": \"807\"" + "\n }" + "\n }" + "\n }";
LOG.debug("alarm check search query: " + query);
Search search = new Search.Builder(query).addIndex("opennms-*").build();
SearchResult sresult = jestClient.execute(search);
LOG.debug("received search sresult: " + sresult.getJsonString() + "\n response code:" + sresult.getResponseCode() + "\n error message: " + sresult.getErrorMessage());
assertEquals(200, sresult.getResponseCode());
JSONParser parser = new JSONParser();
Object obj = parser.parse(sresult.getJsonString());
JSONObject resultValues = (JSONObject) obj;
JSONObject hits = (JSONObject) resultValues.get("hits");
LOG.debug("search result hits:total=" + hits.get("total"));
assertEquals(Long.valueOf(1), hits.get("total"));
// waiting INDEX_WAIT_SECONDS seconds for index
try {
TimeUnit.SECONDS.sleep(INDEX_WAIT_SECONDS);
} catch (InterruptedException e) {
}
// search for resulting alarm change event
String eventquery = "{\n" + "\n \"query\": {" + "\n \"match\": {" + "\n \"id\": \"100\"" + "\n }" + "\n }" + "\n }";
LOG.debug("event check search query: " + eventquery);
Search eventsearch = new Search.Builder(eventquery).addIndex("opennms-*").build();
SearchResult eventsresult = jestClient.execute(eventsearch);
LOG.debug("received search eventsresult: " + eventsresult.getJsonString() + "\n response code:" + eventsresult.getResponseCode() + "\n error message: " + eventsresult.getErrorMessage());
assertEquals(200, eventsresult.getResponseCode());
Object obj2 = parser.parse(eventsresult.getJsonString());
JSONObject eventsresultValues = (JSONObject) obj2;
JSONObject eventhits = (JSONObject) eventsresultValues.get("hits");
LOG.debug("search result event hits:total=" + eventhits.get("total"));
assertEquals(Long.valueOf(1), eventhits.get("total"));
JSONArray eventhitsvalues = (JSONArray) eventhits.get("hits");
LOG.debug(" eventhitsvalues: " + eventhitsvalues.toJSONString());
JSONObject hitObj = (JSONObject) eventhitsvalues.get(0);
LOG.debug(" hitObj: " + hitObj.toJSONString());
String typeStr = hitObj.get("_type").toString();
LOG.debug("search result index type=" + typeStr);
assertEquals(EVENT_INDEX_TYPE, typeStr);
JSONObject sourceObj = (JSONObject) hitObj.get("_source");
LOG.debug(" sourceObj: " + sourceObj.toJSONString());
String eventUeiStr = sourceObj.get("eventuei").toString();
LOG.debug("search result event eventueistr=" + eventUeiStr);
assertEquals(ALARM_ACKNOWLEDGED_EVENT, eventUeiStr);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
} finally {
// shutdown client
if (jestClient != null)
jestClient.shutdownClient();
if (eventToIndex != null)
eventToIndex.close();
}
LOG.debug("***************** end of test jestClientAlarmToESTest");
}
use of org.opennms.plugins.elasticsearch.rest.IndexNameFunction in project opennms by OpenNMS.
the class ClientRecoveryTest method test.
@Test
public void test() {
LOG.debug("***************** start of test ClientRecoveryTest");
try {
IndexNameFunction indexNameFunction = new IndexNameFunction();
String rootIndexName = EventToIndex.INDEX_NAMES.get(EventToIndex.Indices.ALARMS);
String indexName = indexNameFunction.apply(rootIndexName, new Date());
// Get Jest client
HttpClientConfig clientConfig = new HttpClientConfig.Builder("http://localhost:9200").multiThreaded(true).build();
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(clientConfig);
JestClient jestClient = factory.getObject();
try {
String query = "{\n" + "\n \"query\": {" + "\n \"match\": {" + "\n \"alarmid\": \"1359\"" + "\n }" + "\n }" + "\n }";
Search search = new Search.Builder(query).addIndex(indexName).build();
SearchResult sresult = jestClient.execute(search);
LOG.debug("received search result: " + sresult.getJsonString() + "\n response code:" + sresult.getResponseCode() + "\n error message: " + sresult.getErrorMessage());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// shutdown client
jestClient.shutdownClient();
}
} catch (Exception ex) {
ex.printStackTrace();
}
LOG.debug("***************** end of test ClientRecoveryTest");
}
use of org.opennms.plugins.elasticsearch.rest.IndexNameFunction in project opennms by OpenNMS.
the class RawEventToIndexTest method jestClientRawEventToESTest.
/**
* simple test to create a raw event in the raw event index and test that the event is added
*/
@Test
public void jestClientRawEventToESTest() {
LOG.debug("***************** start of test jestClientRawEventToESTestt");
EventToIndex eventToIndex = new EventToIndex();
JestClient jestClient = null;
try {
// Get Jest client
String esusername = "";
String espassword = "";
String elasticsearchUrl = "http://localhost:9200";
RestClientFactory restClientFactory = new RestClientFactory(elasticsearchUrl, esusername, espassword);
IndexNameFunction indexNameFunction = new IndexNameFunction("yyyy.MM");
NodeCache nodeCache = new MockNodeCache();
eventToIndex.setRestClientFactory(restClientFactory);
eventToIndex.setNodeCache(nodeCache);
eventToIndex.setIndexNameFunction(indexNameFunction);
eventToIndex.setLogEventDescription(true);
eventToIndex.setArchiveRawEvents(true);
eventToIndex.setArchiveAlarms(true);
eventToIndex.setArchiveAlarmChangeEvents(true);
eventToIndex.setArchiveOldAlarmValues(true);
eventToIndex.setArchiveNewAlarmValues(true);
EventBuilder eb = new EventBuilder(NODE_LOST_SERVICE_EVENT, EVENT_SOURCE_NAME);
// raw json="{"alarmid":806,"eventuei":"uei.opennms.org/nodes/nodeLostService","nodeid":36,"ipaddr":"142.34.5.19","serviceid":2,"reductionkey":"uei.opennms.org/nodes/nodeLostService::36:142.34.5.19:HTTP","alarmtype":1,"counter":1,"severity":5,"lasteventid":7003,"firsteventtime":"2016-07-27 22:20:52.282+01","lasteventtime":"2016-07-27 22:20:52.282+01","firstautomationtime":null,"lastautomationtime":null,"description":"<p>A HTTP outage was identified on interface\n 142.34.5.19.</p> <p>A new Outage record has been\n created and service level availability calculations will be\n impacted until this outage is resolved.</p>","logmsg":"HTTP outage identified on interface 142.34.5.19 with reason code: Unknown.","operinstruct":null,"tticketid":null,"tticketstate":null,"mouseovertext":null,"suppresseduntil":"2016-07-27 22:20:52.282+01","suppresseduser":null,"suppressedtime":"2016-07-27 22:20:52.282+01","alarmackuser":null,"alarmacktime":null,"managedobjectinstance":null,"managedobjecttype":null,"applicationdn":null,"ossprimarykey":null,"x733alarmtype":null,"x733probablecause":0,"qosalarmstate":null,"clearkey":null,"ifindex":null,"eventparms":"eventReason=Unknown(string,text)","stickymemo":null,"systemid":"00000000-0000-0000-0000-000000000000"}";
eb.setUei("uei.opennms.org/nodes/nodeLostService");
eb.setNodeid(36);
InetAddress ipAddress = InetAddressUtils.getInetAddress("142.34.5.19");
eb.setInterface(ipAddress);
eb.setSource("mock event test");
eb.setHost("localhost");
eb.setLogDest("logndisplay");
eb.setLogMessage("this is a test log message");
eb.setDescription("this is a test description");
eb.setTime(new Date());
eb.setUuid("00000000-0000-0000-0000-000000000000");
Event event = eb.getEvent();
event.setDbid(101);
LOG.debug("ecreated node lost service event:" + event.toString());
// forward event to Elasticsearch
eventToIndex.forwardEvents(Collections.singletonList(event));
// waiting 5 seconds for index
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
}
// send query to check that event has been created
jestClient = restClientFactory.getJestClient();
// search for resulting event
String eventquery = "{\n" + "\n \"query\": {" + "\n \"match\": {" + "\n \"id\": \"101\"" + "\n }" + "\n }" + "\n }";
LOG.debug("event check search query: " + eventquery);
Search eventsearch = new Search.Builder(eventquery).addIndex("opennms-*").build();
SearchResult eventsresult = jestClient.execute(eventsearch);
LOG.debug("received search eventsresult: " + eventsresult.getJsonString() + "\n response code:" + eventsresult.getResponseCode() + "\n error message: " + eventsresult.getErrorMessage());
assertEquals(200, eventsresult.getResponseCode());
JSONParser parser = new JSONParser();
Object obj2 = parser.parse(eventsresult.getJsonString());
JSONObject eventsresultValues = (JSONObject) obj2;
JSONObject eventhits = (JSONObject) eventsresultValues.get("hits");
LOG.debug("search result event hits:total=" + eventhits.get("total"));
assertEquals(Long.valueOf(1), eventhits.get("total"));
JSONArray eventhitsvalues = (JSONArray) eventhits.get("hits");
LOG.debug(" eventhitsvalues: " + eventhitsvalues.toJSONString());
JSONObject hitObj = (JSONObject) eventhitsvalues.get(0);
LOG.debug(" hitsObj: " + hitObj.toJSONString());
String typeStr = hitObj.get("_type").toString();
LOG.debug("search result index type=" + typeStr);
assertEquals(EVENT_INDEX_TYPE, typeStr);
JSONObject sourceObj = (JSONObject) hitObj.get("_source");
LOG.debug(" sourceObj: " + sourceObj.toJSONString());
String eventUeiStr = sourceObj.get("eventuei").toString();
LOG.debug("search result event eventueistr=" + eventUeiStr);
assertEquals(NODE_LOST_SERVICE_EVENT, eventUeiStr);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
} finally {
// shutdown client
if (jestClient != null)
jestClient.shutdownClient();
if (eventToIndex != null)
eventToIndex.close();
}
LOG.debug("***************** end of test jestClientRawEventToESTest");
}
Aggregations