Search in sources :

Example 76 with JsonArray

use of org.json.simple.JsonArray in project opennms by OpenNMS.

the class MemosChangeNotificationClient method sendDbNotification.

@Override
public void sendDbNotification(DbNotification dbNotification) {
    try {
        String payload = dbNotification.getPayload();
        JSONObject memoJsonObject = null;
        JSONObject alarmIdJsonObject = null;
        String alarmId = null;
        String body = null;
        String author = null;
        String reductionkey = null;
        try {
            JSONParser parser = new JSONParser();
            Object obj;
            obj = parser.parse(payload);
            JSONArray jsonArray = (JSONArray) obj;
            if (LOG.isDebugEnabled())
                LOG.debug("payload memo jsonArray.toString():" + jsonArray.toString());
            memoJsonObject = (JSONObject) jsonArray.get(0);
            memoJsonObject = jsonMemoTimeNormaliser(memoJsonObject);
            alarmIdJsonObject = (JSONObject) jsonArray.get(1);
            alarmId = (alarmIdJsonObject.get("alarmid") == null) ? null : alarmIdJsonObject.get("alarmid").toString();
            body = (memoJsonObject.get("body") == null) ? null : memoJsonObject.get("body").toString();
            author = (memoJsonObject.get("author") == null) ? null : memoJsonObject.get("author").toString();
            reductionkey = (memoJsonObject.get("reductionkey") == null) ? null : memoJsonObject.get("reductionkey").toString();
        } catch (ParseException e1) {
            throw new RuntimeException("cannot parse notification payload to json object. payload=" + payload, e1);
        }
        if (!memoJsonObject.isEmpty()) {
            // sticky note event
            if ("Memo".equals(memoJsonObject.get("type").toString())) {
                if (LOG.isDebugEnabled())
                    LOG.debug("sticky memo updated=" + memoJsonObject.get("id"));
                EventBuilder eb = new EventBuilder(AlarmChangeEventConstants.STICKY_MEMO_EVENT, EVENT_SOURCE_NAME);
                // copy in all values as json in params
                eb.addParam(AlarmChangeEventConstants.MEMO_VALUES_PARAM, memoJsonObject.toString());
                eb.addParam(AlarmChangeEventConstants.MEMO_ALARMID_PARAM, alarmId);
                eb.addParam(AlarmChangeEventConstants.MEMO_BODY_PARAM, body);
                eb.addParam(AlarmChangeEventConstants.MEMO_AUTHOR_PARAM, author);
                sendEvent(eb.getEvent());
            } else if ("ReductionKeyMemo".equals(memoJsonObject.get("type").toString())) {
                if (LOG.isDebugEnabled())
                    LOG.debug("reduction key memo updated=" + memoJsonObject.get("id"));
                EventBuilder eb = new EventBuilder(AlarmChangeEventConstants.JOURNAL_MEMO_EVENT, EVENT_SOURCE_NAME);
                // copy in all values as json in params
                eb.addParam(AlarmChangeEventConstants.MEMO_VALUES_PARAM, memoJsonObject.toString());
                eb.addParam(AlarmChangeEventConstants.MEMO_ALARMID_PARAM, alarmId);
                eb.addParam(AlarmChangeEventConstants.MEMO_BODY_PARAM, body);
                eb.addParam(AlarmChangeEventConstants.MEMO_AUTHOR_PARAM, author);
                eb.addParam(AlarmChangeEventConstants.MEMO_REDUCTIONKEY_PARAM, reductionkey);
                sendEvent(eb.getEvent());
            }
        }
    } catch (Exception e) {
        LOG.error("problem creating opennms alarm change event from database notification", e);
    }
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) ParseException(org.json.simple.parser.ParseException) EventProxyException(org.opennms.netmgt.events.api.EventProxyException)

Example 77 with JsonArray

use of org.json.simple.JsonArray 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 78 with JsonArray

use of org.json.simple.JsonArray in project zookeeper by apache.

the class Throughput method handleRequest.

public String handleRequest(JsonRequest request) throws Exception {
    long starttime = 0;
    long endtime = 0;
    long period = 0;
    long scale = 0;
    starttime = request.getNumber("start", 0);
    endtime = request.getNumber("end", 0);
    period = request.getNumber("period", 0);
    if (starttime == 0) {
        starttime = source.getStartTime();
    }
    if (endtime == 0) {
        if (period > 0) {
            endtime = starttime + period;
        } else {
            endtime = source.getEndTime();
        }
    }
    String scalestr = request.getString("scale", "minutes");
    if (scalestr.equals("seconds")) {
        scale = MS_PER_SEC;
    } else if (scalestr.equals("hours")) {
        scale = MS_PER_HOUR;
    } else {
        scale = MS_PER_MIN;
    }
    LogIterator iter = source.iterator(starttime, endtime);
    long current = 0;
    long currentms = 0;
    Set<Long> zxids_ms = new HashSet<Long>();
    long zxidcount = 0;
    JSONArray events = new JSONArray();
    while (iter.hasNext()) {
        LogEntry e = iter.next();
        if (e.getType() != LogEntry.Type.TXN) {
            continue;
        }
        TransactionEntry cxn = (TransactionEntry) e;
        long ms = cxn.getTimestamp();
        long inscale = ms / scale;
        if (currentms != ms && currentms != 0) {
            zxidcount += zxids_ms.size();
            zxids_ms.clear();
        }
        if (inscale != current && current != 0) {
            JSONObject o = new JSONObject();
            o.put("time", current * scale);
            o.put("count", zxidcount);
            events.add(o);
            zxidcount = 0;
        }
        current = inscale;
        currentms = ms;
        zxids_ms.add(cxn.getZxid());
    }
    JSONObject o = new JSONObject();
    o.put("time", current * scale);
    o.put("count", zxidcount);
    events.add(o);
    iter.close();
    return JSONValue.toJSONString(events);
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) HashSet(java.util.HashSet)

Example 79 with JsonArray

use of org.json.simple.JsonArray in project metron by apache.

the class ElasticsearchSearchIntegrationTest method loadTestData.

@Override
protected void loadTestData() throws ParseException, IOException, ExecutionException, InterruptedException {
    ElasticSearchComponent es = (ElasticSearchComponent) indexComponent;
    es.getClient().admin().indices().prepareCreate("bro_index_2017.01.01.01").addMapping("bro_doc", broTypeMappings).addMapping("bro_doc_default", broDefaultStringMappings).get();
    es.getClient().admin().indices().prepareCreate("snort_index_2017.01.01.02").addMapping("snort_doc", snortTypeMappings).get();
    BulkRequestBuilder bulkRequest = es.getClient().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
    JSONArray broArray = (JSONArray) new JSONParser().parse(broData);
    for (Object o : broArray) {
        JSONObject jsonObject = (JSONObject) o;
        IndexRequestBuilder indexRequestBuilder = es.getClient().prepareIndex("bro_index_2017.01.01.01", "bro_doc");
        indexRequestBuilder = indexRequestBuilder.setId((String) jsonObject.get("guid"));
        indexRequestBuilder = indexRequestBuilder.setSource(jsonObject.toJSONString());
        indexRequestBuilder = indexRequestBuilder.setTimestamp(jsonObject.get("timestamp").toString());
        bulkRequest.add(indexRequestBuilder);
    }
    JSONArray snortArray = (JSONArray) new JSONParser().parse(snortData);
    for (Object o : snortArray) {
        JSONObject jsonObject = (JSONObject) o;
        IndexRequestBuilder indexRequestBuilder = es.getClient().prepareIndex("snort_index_2017.01.01.02", "snort_doc");
        indexRequestBuilder = indexRequestBuilder.setId((String) jsonObject.get("guid"));
        indexRequestBuilder = indexRequestBuilder.setSource(jsonObject.toJSONString());
        indexRequestBuilder = indexRequestBuilder.setTimestamp(jsonObject.get("timestamp").toString());
        bulkRequest.add(indexRequestBuilder);
    }
    BulkResponse bulkResponse = bulkRequest.execute().actionGet();
    if (bulkResponse.hasFailures()) {
        throw new RuntimeException("Failed to index test data");
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ElasticSearchComponent(org.apache.metron.elasticsearch.integration.components.ElasticSearchComponent) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 80 with JsonArray

use of org.json.simple.JsonArray in project metron by apache.

the class BasicBroParser method replaceKeyArray.

private boolean replaceKeyArray(JSONObject payload, String toKey, String[] fromKeys) {
    for (String fromKey : fromKeys) {
        if (payload.containsKey(fromKey)) {
            JSONArray value = (JSONArray) payload.remove(fromKey);
            if (value != null && !value.isEmpty()) {
                payload.put(toKey, value.get(0));
                _LOG.trace("[Metron] Added {} to {}", toKey, payload);
                return true;
            }
        }
    }
    return false;
}
Also used : JSONArray(org.json.simple.JSONArray)

Aggregations

JSONArray (org.json.simple.JSONArray)267 JSONObject (org.json.simple.JSONObject)238 JSONParser (org.json.simple.parser.JSONParser)73 Test (org.junit.Test)40 ParseException (org.json.simple.parser.ParseException)23 ArrayList (java.util.ArrayList)21 HttpClient (org.apache.commons.httpclient.HttpClient)21 IOException (java.io.IOException)17 HashMap (java.util.HashMap)17 GetMethod (org.apache.commons.httpclient.methods.GetMethod)17 Transaction (org.xel.Transaction)12 File (java.io.File)11 List (java.util.List)10 HttpResponse (org.apache.http.HttpResponse)10 BlockchainTest (org.xel.BlockchainTest)10 APICall (org.xel.http.APICall)10 Block (org.xel.Block)9 MapLayer (au.org.emii.portal.menu.MapLayer)8 Map (java.util.Map)8 FileReader (java.io.FileReader)6