use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.
the class IndexerSetupService method startUp.
@Override
protected void startUp() throws Exception {
Tools.silenceUncaughtExceptionsInThisThread();
LOG.debug("Starting indexer");
node.start();
final Client client = node.client();
try {
/* try to determine the cluster health. if this times out we could not connect and try to determine if there's
anything listening at all. if that happens this usually has these reasons:
1. cluster.name is different
2. network.publish_host is not reachable
3. wrong address configured
4. multicast in use but broken in this environment
we handle a red cluster state differently because if we can get that result it means the cluster itself
is reachable, which is a completely different problem from not being able to join at all.
*/
final ClusterHealthRequest atLeastRed = client.admin().cluster().prepareHealth().setWaitForStatus(ClusterHealthStatus.RED).request();
final ClusterHealthResponse health = client.admin().cluster().health(atLeastRed).actionGet(configuration.getClusterDiscoveryTimeout(), MILLISECONDS);
// we don't get here if we couldn't join the cluster. just check for red cluster state
if (ClusterHealthStatus.RED.equals(health.getStatus())) {
final Notification notification = notificationService.buildNow().addSeverity(Notification.Severity.URGENT).addType(Notification.Type.ES_CLUSTER_RED);
notificationService.publishIfFirst(notification);
LOG.warn("The Elasticsearch cluster state is RED which means shards are unassigned.");
LOG.info("This usually indicates a crashed and corrupt cluster and needs to be investigated. Graylog will write into the local disk journal.");
LOG.info("See {} for details.", DocsHelper.PAGE_ES_CONFIGURATION);
}
if (ClusterHealthStatus.GREEN.equals(health.getStatus())) {
notificationService.fixed(Notification.Type.ES_CLUSTER_RED);
}
notificationService.fixed(Notification.Type.ES_UNAVAILABLE);
} catch (ElasticsearchTimeoutException e) {
final String hosts = node.settings().get("discovery.zen.ping.unicast.hosts");
if (!isNullOrEmpty(hosts)) {
final Iterable<String> hostList = Splitter.on(',').omitEmptyStrings().trimResults().split(hosts);
for (String host : hostList) {
final URI esUri = URI.create("http://" + HostAndPort.fromString(host).getHostText() + ":9200/");
LOG.info("Checking Elasticsearch HTTP API at {}", esUri);
// Try the HTTP API endpoint
final Request request = new Request.Builder().get().url(esUri.resolve("/_nodes").toString()).build();
try (final Response response = httpClient.newCall(request).execute()) {
if (response.isSuccessful()) {
final JsonNode resultTree = objectMapper.readTree(response.body().byteStream());
final JsonNode nodesList = resultTree.get("nodes");
if (!configuration.isDisableVersionCheck()) {
final Iterator<String> nodes = nodesList.fieldNames();
while (nodes.hasNext()) {
final String id = nodes.next();
final Version clusterVersion = Version.fromString(nodesList.get(id).get("version").textValue());
checkClusterVersion(clusterVersion);
}
}
final String clusterName = resultTree.get("cluster_name").textValue();
checkClusterName(clusterName);
} else {
LOG.error("Could not connect to Elasticsearch at " + esUri + ". Is it running?");
}
} catch (IOException ioException) {
LOG.error("Could not connect to Elasticsearch: {}", ioException.getMessage());
}
}
}
final Notification notification = notificationService.buildNow().addSeverity(Notification.Severity.URGENT).addType(Notification.Type.ES_UNAVAILABLE);
notificationService.publishIfFirst(notification);
LOG.warn("Could not connect to Elasticsearch");
LOG.info("If you're using multicast, check that it is working in your network and that Elasticsearch is accessible. Also check that the cluster name setting is correct.");
LOG.info("See {} for details.", DocsHelper.PAGE_ES_CONFIGURATION);
}
}
use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.
the class LegacyMongoIndexRangeService method findAll.
@Override
public SortedSet<IndexRange> findAll() {
final BasicDBList subQueries = new BasicDBList();
subQueries.add(new BasicDBObject(FIELD_INDEX, new BasicDBObject("$exists", true)));
subQueries.add(new BasicDBObject(FIELD_START, new BasicDBObject("$exists", true)));
final DBObject query = new BasicDBObject("$and", subQueries);
final List<DBObject> result = query(query, COLLECTION_NAME);
final ImmutableSortedSet.Builder<IndexRange> indexRanges = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR);
for (DBObject dbo : result) {
try {
indexRanges.add(buildIndexRange(dbo));
} catch (Exception e) {
LOG.debug("Couldn't add index range to result set: " + dbo, e);
}
}
return indexRanges.build();
}
use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.
the class SizeBasedRotationStrategy method shouldRotate.
@Nullable
@Override
protected Result shouldRotate(final String index, IndexSet indexSet) {
if (!(indexSet.getConfig().rotationStrategy() instanceof SizeBasedRotationStrategyConfig)) {
throw new IllegalStateException("Invalid rotation strategy config <" + indexSet.getConfig().rotationStrategy().getClass().getCanonicalName() + "> for index set <" + indexSet.getConfig().id() + ">");
}
final SizeBasedRotationStrategyConfig config = (SizeBasedRotationStrategyConfig) indexSet.getConfig().rotationStrategy();
final IndexStatistics indexStats = indices.getIndexStats(index);
if (indexStats == null) {
return null;
}
final long sizeInBytes = indexStats.primaries().getStore().getSizeInBytes();
final boolean shouldRotate = sizeInBytes > config.maxSize();
return new Result() {
public final MessageFormat ROTATE = new MessageFormat("Storage size for index <{0}> is {1} bytes, exceeding the maximum of {2} bytes. Rotating index.", Locale.ENGLISH);
public final MessageFormat NOT_ROTATE = new MessageFormat("Storage size for index <{0}> is {1} bytes, below the maximum of {2} bytes. Not doing anything.", Locale.ENGLISH);
@Override
public String getDescription() {
MessageFormat format = shouldRotate() ? ROTATE : NOT_ROTATE;
return format.format(new Object[] { index, sizeInBytes, config.maxSize() });
}
@Override
public boolean shouldRotate() {
return shouldRotate;
}
};
}
use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.
the class SearchResponseCsvWriter method writeTo.
@Override
public void writeTo(SearchResponse searchResponse, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
final CSVWriter csvWriter = new CSVWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
final ImmutableSortedSet<String> sortedFields = ImmutableSortedSet.copyOf(Iterables.concat(searchResponse.fields(), Lists.newArrayList("source", "timestamp")));
// write field headers
csvWriter.writeNext(sortedFields.toArray(new String[sortedFields.size()]));
// write result set in same order as the header row
final String[] fieldValues = new String[sortedFields.size()];
for (ResultMessageSummary message : searchResponse.messages()) {
int idx = 0;
// first collect all values from the current message
for (String fieldName : sortedFields) {
final Object val = message.message().get(fieldName);
fieldValues[idx++] = ((val == null) ? null : val.toString().replaceAll("\n", "\\\\n"));
fieldValues[idx++] = ((val == null) ? null : val.toString().replaceAll("\r", "\\\\r"));
}
// write the complete line, some fields might not be present in the message, so there might be null values
csvWriter.writeNext(fieldValues);
}
if (csvWriter.checkError()) {
LOG.error("Encountered unspecified error when writing message result as CSV, result is likely malformed.");
}
csvWriter.close();
}
use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.
the class DashboardWidgetsResource method addWidget.
@POST
@Timed
@ApiOperation(value = "Add a widget to a dashboard")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "Dashboard not found."), @ApiResponse(code = 400, message = "Validation error."), @ApiResponse(code = 400, message = "No such widget type.") })
@AuditEvent(type = AuditEventTypes.DASHBOARD_WIDGET_CREATE)
public Response addWidget(@ApiParam(name = "dashboardId", required = true) @PathParam("dashboardId") String dashboardId, @ApiParam(name = "JSON body", required = true) AddWidgetRequest awr) throws ValidationException, NotFoundException {
checkPermission(RestPermissions.DASHBOARDS_EDIT, dashboardId);
// Bind to streams for reader users and check stream permission.
if (awr.config().containsKey("stream_id")) {
checkPermission(RestPermissions.STREAMS_READ, (String) awr.config().get("stream_id"));
} else {
checkPermission(RestPermissions.SEARCHES_ABSOLUTE);
checkPermission(RestPermissions.SEARCHES_RELATIVE);
checkPermission(RestPermissions.SEARCHES_KEYWORD);
}
final DashboardWidget widget;
try {
widget = dashboardWidgetCreator.fromRequest(awr, getCurrentUser().getName());
final Dashboard dashboard = dashboardService.load(dashboardId);
dashboardService.addWidget(dashboard, widget);
} catch (DashboardWidget.NoSuchWidgetTypeException e2) {
LOG.debug("No such widget type.", e2);
throw new BadRequestException("No such widget type.", e2);
} catch (InvalidRangeParametersException e3) {
LOG.debug("Invalid timerange parameters provided.", e3);
throw new BadRequestException("Invalid timerange parameters provided.", e3);
} catch (InvalidWidgetConfigurationException e4) {
LOG.debug("Invalid widget configuration.", e4);
throw new BadRequestException("Invalid widget configuration.", e4);
}
final Map<String, String> result = ImmutableMap.of("widget_id", widget.getId());
final URI widgetUri = getUriBuilderToSelf().path(DashboardWidgetsResource.class, "getWidget").build(dashboardId, widget.getId());
return Response.created(widgetUri).entity(result).build();
}
Aggregations