Search in sources :

Example 1 with SearchResult

use of io.searchbox.core.SearchResult 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");
}
Also used : IndexNameFunction(org.opennms.plugins.elasticsearch.rest.IndexNameFunction) NodeCache(org.opennms.plugins.elasticsearch.rest.NodeCache) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONArray(org.json.simple.JSONArray) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONObject(org.json.simple.JSONObject) RestClientFactory(org.opennms.plugins.elasticsearch.rest.RestClientFactory) Search(io.searchbox.core.Search) Event(org.opennms.netmgt.xml.event.Event) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) EventToIndex(org.opennms.plugins.elasticsearch.rest.EventToIndex) Test(org.junit.Test)

Example 2 with SearchResult

use of io.searchbox.core.SearchResult in project opennms by OpenNMS.

the class AbstractSyslogTestCase method pollForElasticsearchEventsUsingJest.

protected static void pollForElasticsearchEventsUsingJest(Supplier<InetSocketAddress> esTransportAddr, int numMessages) {
    with().pollInterval(15, SECONDS).await().atMost(5, MINUTES).until(() -> {
        JestClient client = null;
        try {
            JestClientFactory factory = new JestClientFactory();
            factory.setHttpClientConfig(new HttpClientConfig.Builder(String.format("http://%s:%d", esTransportAddr.get().getHostString(), esTransportAddr.get().getPort())).multiThreaded(true).build());
            client = factory.getObject();
            SearchResult response = client.execute(new Search.Builder(new SearchSourceBuilder().query(QueryBuilders.matchQuery("eventuei", "uei.opennms.org/vendor/cisco/syslog/SEC-6-IPACCESSLOGP/aclDeniedIPTraffic")).toString()).addIndex("opennms*").build());
            LOG.debug("SEARCH RESPONSE: {}", response.toString());
            // Sometimes, the first warm-up message is successful so treat both message counts as valid
            assertTrue("ES search hits was not equal to " + numMessages + ": " + response.getTotal(), (numMessages == response.getTotal()));
        //assertEquals("Event UEI did not match", "uei.opennms.org/vendor/cisco/syslog/SEC-6-IPACCESSLOGP/aclDeniedIPTraffic", response.getHits().getAt(0).getSource().get("eventuei"));
        //assertEquals("Event IP address did not match", "4.2.2.2", response.getHits().getAt(0).getSource().get("ipaddr"));
        } catch (Throwable e) {
            LOG.warn(e.getMessage(), e);
            return false;
        } finally {
            if (client != null) {
                client.shutdownClient();
            }
        }
        return true;
    });
}
Also used : HttpClientConfig(io.searchbox.client.config.HttpClientConfig) Search(io.searchbox.core.Search) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) JestClientFactory(io.searchbox.client.JestClientFactory) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 3 with SearchResult

use of io.searchbox.core.SearchResult in project spring-boot by spring-projects.

the class ElasticsearchJestHealthIndicatorTests method createJestResult.

private static JestResult createJestResult(int shards, int failedShards) {
    String json = String.format("{_shards: {\n" + "total: %s,\n" + "successful: %s,\n" + "failed: %s\n" + "}}", shards, shards - failedShards, failedShards);
    SearchResult searchResult = new SearchResult(new Gson());
    searchResult.setJsonString(json);
    searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());
    return searchResult;
}
Also used : Gson(com.google.gson.Gson) SearchResult(io.searchbox.core.SearchResult) JsonParser(com.google.gson.JsonParser)

Example 4 with SearchResult

use of io.searchbox.core.SearchResult 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");
}
Also used : IndexNameFunction(org.opennms.plugins.elasticsearch.rest.IndexNameFunction) NodeCache(org.opennms.plugins.elasticsearch.rest.NodeCache) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONArray(org.json.simple.JSONArray) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) Date(java.util.Date) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONObject(org.json.simple.JSONObject) RestClientFactory(org.opennms.plugins.elasticsearch.rest.RestClientFactory) Search(io.searchbox.core.Search) Event(org.opennms.netmgt.xml.event.Event) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) InetAddress(java.net.InetAddress) EventToIndex(org.opennms.plugins.elasticsearch.rest.EventToIndex) Test(org.junit.Test)

Example 5 with SearchResult

use of io.searchbox.core.SearchResult 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");
}
Also used : IndexNameFunction(org.opennms.plugins.elasticsearch.rest.IndexNameFunction) HttpClientConfig(io.searchbox.client.config.HttpClientConfig) Search(io.searchbox.core.Search) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) JestClientFactory(io.searchbox.client.JestClientFactory) Date(java.util.Date) Test(org.junit.Test)

Aggregations

SearchResult (io.searchbox.core.SearchResult)5 JestClient (io.searchbox.client.JestClient)4 Search (io.searchbox.core.Search)4 Test (org.junit.Test)3 IndexNameFunction (org.opennms.plugins.elasticsearch.rest.IndexNameFunction)3 JestClientFactory (io.searchbox.client.JestClientFactory)2 HttpClientConfig (io.searchbox.client.config.HttpClientConfig)2 Date (java.util.Date)2 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 JSONParser (org.json.simple.parser.JSONParser)2 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)2 Event (org.opennms.netmgt.xml.event.Event)2 EventToIndex (org.opennms.plugins.elasticsearch.rest.EventToIndex)2 NodeCache (org.opennms.plugins.elasticsearch.rest.NodeCache)2 RestClientFactory (org.opennms.plugins.elasticsearch.rest.RestClientFactory)2 Gson (com.google.gson.Gson)1 JsonParser (com.google.gson.JsonParser)1 InetAddress (java.net.InetAddress)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1