Search in sources :

Example 1 with MetaScores

use of org.apache.metron.indexing.dao.metaalert.MetaScores in project metron by apache.

the class ElasticsearchMetaAlertDao method calculateMetaScores.

/**
 * Calculate the meta alert scores for a Document.
 * @param metaAlert The Document containing scores
 * @return Set of score statistics
 */
@SuppressWarnings("unchecked")
protected void calculateMetaScores(Document metaAlert) {
    MetaScores metaScores = new MetaScores(new ArrayList<>());
    List<Object> alertsRaw = ((List<Object>) metaAlert.getDocument().get(ALERT_FIELD));
    if (alertsRaw != null && !alertsRaw.isEmpty()) {
        ArrayList<Double> scores = new ArrayList<>();
        for (Object alertRaw : alertsRaw) {
            Map<String, Object> alert = (Map<String, Object>) alertRaw;
            Double scoreNum = parseThreatField(alert.get(threatTriageField));
            if (scoreNum != null) {
                scores.add(scoreNum);
            }
        }
        metaScores = new MetaScores(scores);
    }
    // add a summary (max, min, avg, ...) of all the threat scores from the child alerts
    metaAlert.getDocument().putAll(metaScores.getMetaScores());
    // add the overall threat score for the metaalert; one of the summary aggregations as defined by `threatSort`
    Object threatScore = metaScores.getMetaScores().get(threatSort);
    // add the threat score as a float; type needs to match the threat score field from each of the sensor indices
    metaAlert.getDocument().put(threatTriageField, ConversionUtils.convert(threatScore, Float.class));
}
Also used : MetaScores(org.apache.metron.indexing.dao.metaalert.MetaScores)

Example 2 with MetaScores

use of org.apache.metron.indexing.dao.metaalert.MetaScores in project metron by apache.

the class InMemoryMetaAlertDao method createMetaAlert.

@SuppressWarnings("unchecked")
@Override
public MetaAlertCreateResponse createMetaAlert(MetaAlertCreateRequest request) throws InvalidCreateException, IOException {
    List<GetRequest> alertRequests = request.getAlerts();
    if (alertRequests.isEmpty()) {
        MetaAlertCreateResponse response = new MetaAlertCreateResponse();
        response.setCreated(false);
        return response;
    }
    // Build meta alert json.  Give it a reasonable GUID
    JSONObject metaAlert = new JSONObject();
    String metaAlertGuid = "meta_" + (InMemoryDao.BACKING_STORE.get(MetaAlertDao.METAALERTS_INDEX).size() + 1);
    metaAlert.put(GUID, metaAlertGuid);
    JSONArray groupsArray = new JSONArray();
    groupsArray.addAll(request.getGroups());
    metaAlert.put(MetaAlertDao.GROUPS_FIELD, groupsArray);
    // Retrieve the alert for each guid
    // For the purpose of testing, we're just using guids for the alerts field and grabbing the scores.
    JSONArray alertArray = new JSONArray();
    List<Double> threatScores = new ArrayList<>();
    Collection<String> alertGuids = new ArrayList<>();
    for (GetRequest alertRequest : alertRequests) {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.setIndices(ImmutableList.of(alertRequest.getIndex().get()));
        searchRequest.setQuery("guid:" + alertRequest.getGuid());
        try {
            SearchResponse searchResponse = search(searchRequest);
            List<SearchResult> searchResults = searchResponse.getResults();
            if (searchResults.size() > 1) {
                throw new InvalidCreateException("Found more than one result for: " + alertRequest.getGuid() + ". Values: " + searchResults);
            }
            if (searchResults.size() == 1) {
                SearchResult result = searchResults.get(0);
                alertArray.add(result.getSource());
                Double threatScore = Double.parseDouble(result.getSource().getOrDefault(THREAT_FIELD_DEFAULT, "0").toString());
                threatScores.add(threatScore);
            }
        } catch (InvalidSearchException e) {
            throw new InvalidCreateException("Unable to find guid: " + alertRequest.getGuid(), e);
        }
        alertGuids.add(alertRequest.getGuid());
    }
    metaAlert.put(MetaAlertDao.ALERT_FIELD, alertArray);
    metaAlert.putAll(new MetaScores(threatScores).getMetaScores());
    metaAlert.put(STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString());
    // Add the alert to the store, but make sure not to overwrite existing results
    InMemoryDao.BACKING_STORE.get(MetaAlertDao.METAALERTS_INDEX).add(metaAlert.toJSONString());
    METAALERT_STORE.put(metaAlertGuid, new HashSet<>(alertGuids));
    MetaAlertCreateResponse createResponse = new MetaAlertCreateResponse();
    createResponse.setGuid(metaAlertGuid);
    createResponse.setCreated(true);
    return createResponse;
}
Also used : SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) MetaScores(org.apache.metron.indexing.dao.metaalert.MetaScores) JSONArray(org.json.simple.JSONArray) ArrayList(java.util.ArrayList) MetaAlertCreateResponse(org.apache.metron.indexing.dao.metaalert.MetaAlertCreateResponse) SearchResult(org.apache.metron.indexing.dao.search.SearchResult) SearchResponse(org.apache.metron.indexing.dao.search.SearchResponse) InvalidSearchException(org.apache.metron.indexing.dao.search.InvalidSearchException) JSONObject(org.json.simple.JSONObject) GetRequest(org.apache.metron.indexing.dao.search.GetRequest) InvalidCreateException(org.apache.metron.indexing.dao.search.InvalidCreateException)

Aggregations

MetaScores (org.apache.metron.indexing.dao.metaalert.MetaScores)2 ArrayList (java.util.ArrayList)1 MetaAlertCreateResponse (org.apache.metron.indexing.dao.metaalert.MetaAlertCreateResponse)1 GetRequest (org.apache.metron.indexing.dao.search.GetRequest)1 InvalidCreateException (org.apache.metron.indexing.dao.search.InvalidCreateException)1 InvalidSearchException (org.apache.metron.indexing.dao.search.InvalidSearchException)1 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)1 SearchResponse (org.apache.metron.indexing.dao.search.SearchResponse)1 SearchResult (org.apache.metron.indexing.dao.search.SearchResult)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1