Search in sources :

Example 51 with Node

use of org.graylog2.indexer.cluster.Node in project graylog2-server by Graylog2.

the class GelfCodec method decode.

@Nullable
@Override
public Message decode(@Nonnull final RawMessage rawMessage) {
    final GELFMessage gelfMessage = new GELFMessage(rawMessage.getPayload(), rawMessage.getRemoteAddress());
    final String json = gelfMessage.getJSON(decompressSizeLimit);
    final JsonNode node;
    try {
        node = objectMapper.readTree(json);
        if (node == null) {
            throw new IOException("null result");
        }
    } catch (final Exception e) {
        log.error("Could not parse JSON, first 400 characters: " + StringUtils.abbreviate(json, 403), e);
        throw new IllegalStateException("JSON is null/could not be parsed (invalid JSON)", e);
    }
    try {
        validateGELFMessage(node, rawMessage.getId(), rawMessage.getRemoteAddress());
    } catch (IllegalArgumentException e) {
        log.trace("Invalid GELF message <{}>", node);
        throw e;
    }
    // Timestamp.
    final double messageTimestamp = timestampValue(node);
    final DateTime timestamp;
    if (messageTimestamp <= 0) {
        timestamp = rawMessage.getTimestamp();
    } else {
        // we treat this as a unix timestamp
        timestamp = Tools.dateTimeFromDouble(messageTimestamp);
    }
    final Message message = new Message(stringValue(node, "short_message"), stringValue(node, "host"), timestamp);
    message.addField(Message.FIELD_FULL_MESSAGE, stringValue(node, "full_message"));
    final String file = stringValue(node, "file");
    if (file != null && !file.isEmpty()) {
        message.addField("file", file);
    }
    final long line = longValue(node, "line");
    if (line > -1) {
        message.addField("line", line);
    }
    // Level is set by server if not specified by client.
    final int level = intValue(node, "level");
    if (level > -1) {
        message.addField("level", level);
    }
    // Facility is set by server if not specified by client.
    final String facility = stringValue(node, "facility");
    if (facility != null && !facility.isEmpty()) {
        message.addField("facility", facility);
    }
    // Add additional data if there is some.
    final Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
    while (fields.hasNext()) {
        final Map.Entry<String, JsonNode> entry = fields.next();
        String key = entry.getKey();
        // Do not index useless GELF "version" field.
        if ("version".equals(key)) {
            continue;
        }
        // Don't include GELF syntax underscore in message field key.
        if (key.startsWith("_") && key.length() > 1) {
            key = key.substring(1);
        }
        // We already set short_message and host as message and source. Do not add as fields again.
        if ("short_message".equals(key) || "host".equals(key)) {
            continue;
        }
        // Skip standard or already set fields.
        if (message.getField(key) != null || Message.RESERVED_FIELDS.contains(key) && !Message.RESERVED_SETTABLE_FIELDS.contains(key)) {
            continue;
        }
        // Convert JSON containers to Strings, and pick a suitable number representation.
        final JsonNode value = entry.getValue();
        final Object fieldValue;
        if (value.isContainerNode()) {
            fieldValue = value.toString();
        } else if (value.isFloatingPointNumber()) {
            fieldValue = value.asDouble();
        } else if (value.isIntegralNumber()) {
            fieldValue = value.asLong();
        } else if (value.isNull()) {
            log.debug("Field [{}] is NULL. Skipping.", key);
            continue;
        } else if (value.isTextual()) {
            fieldValue = value.asText();
        } else {
            log.debug("Field [{}] has unknown value type. Skipping.", key);
            continue;
        }
        message.addField(key, fieldValue);
    }
    return message;
}
Also used : RawMessage(org.graylog2.plugin.journal.RawMessage) GELFMessage(org.graylog2.inputs.codecs.gelf.GELFMessage) Message(org.graylog2.plugin.Message) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) GELFMessage(org.graylog2.inputs.codecs.gelf.GELFMessage) Map(java.util.Map) Nullable(javax.annotation.Nullable)

Example 52 with Node

use of org.graylog2.indexer.cluster.Node in project graylog2-server by Graylog2.

the class NodePingThread method doRun.

@Override
public synchronized // This method is "synchronized" because we are also calling it directly in AutomaticLeaderElectionService
void doRun() {
    final boolean isLeader = leaderElectionService.isLeader();
    try {
        Node node = nodeService.byNodeId(serverStatus.getNodeId());
        nodeService.markAsAlive(node, isLeader, httpConfiguration.getHttpPublishUri().resolve(HttpConfiguration.PATH_API));
    } catch (NodeNotFoundException e) {
        LOG.warn("Did not find meta info of this node. Re-registering.");
        nodeService.registerServer(serverStatus.getNodeId().toString(), isLeader, httpConfiguration.getHttpPublishUri().resolve(HttpConfiguration.PATH_API), Tools.getLocalCanonicalHostname());
    }
    try {
        // Remove old nodes that are no longer running. (Just some housekeeping)
        nodeService.dropOutdated();
        // Check that we still have a leader node in the cluster, if not, warn the user.
        if (nodeService.isAnyLeaderPresent()) {
            if (fixNoLeaderNotification()) {
                activityWriter.write(new Activity("Notification condition [" + NotificationImpl.Type.NO_LEADER + "] " + "has been fixed.", NodePingThread.class));
            }
        } else {
            Notification notification = notificationService.buildNow().addNode(serverStatus.getNodeId().toString()).addType(Notification.Type.NO_LEADER).addSeverity(Notification.Severity.URGENT);
            notificationService.publishIfFirst(notification);
        }
    } catch (Exception e) {
        LOG.warn("Caught exception during node ping.", e);
    }
}
Also used : NodeNotFoundException(org.graylog2.cluster.NodeNotFoundException) Node(org.graylog2.cluster.Node) Activity(org.graylog2.shared.system.activities.Activity) Notification(org.graylog2.notifications.Notification) NodeNotFoundException(org.graylog2.cluster.NodeNotFoundException)

Example 53 with Node

use of org.graylog2.indexer.cluster.Node in project graylog2-server by Graylog2.

the class ClusterSystemResource method threadDump.

@GET
@Timed
@ApiOperation(value = "Get a thread dump of the given node")
@RequiresPermissions(RestPermissions.THREADS_DUMP)
@Path("{nodeId}/threaddump")
public SystemThreadDumpResponse threadDump(@ApiParam(name = "nodeId", value = "The id of the node to get a thread dump.", required = true) @PathParam("nodeId") String nodeId) throws IOException, NodeNotFoundException {
    final Node targetNode = nodeService.byNodeId(nodeId);
    final RemoteSystemResource remoteSystemResource = remoteInterfaceProvider.get(targetNode, this.authenticationToken, RemoteSystemResource.class);
    final Response<SystemThreadDumpResponse> response = remoteSystemResource.threadDump().execute();
    if (response.isSuccessful()) {
        return response.body();
    } else {
        LOG.warn("Unable to get thread dump on node {}: {}", nodeId, response.message());
        throw new WebApplicationException(response.message(), BAD_GATEWAY);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RemoteSystemResource(org.graylog2.shared.rest.resources.system.RemoteSystemResource) Node(org.graylog2.cluster.Node) SystemThreadDumpResponse(org.graylog2.rest.models.system.responses.SystemThreadDumpResponse) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 54 with Node

use of org.graylog2.indexer.cluster.Node in project graylog2-server by Graylog2.

the class ClusterSystemResource method processBufferDump.

@GET
@Timed
@ApiOperation(value = "Get a process buffer dump of the given node")
@RequiresPermissions(RestPermissions.PROCESSBUFFER_DUMP)
@Path("{nodeId}/processbufferdump")
public SystemProcessBufferDumpResponse processBufferDump(@ApiParam(name = "nodeId", value = "The id of the node to get a process buffer dump.", required = true) @PathParam("nodeId") String nodeId) throws IOException, NodeNotFoundException {
    final Node targetNode = nodeService.byNodeId(nodeId);
    final RemoteSystemResource remoteSystemResource = remoteInterfaceProvider.get(targetNode, this.authenticationToken, RemoteSystemResource.class);
    final Response<SystemProcessBufferDumpResponse> response = remoteSystemResource.processBufferDump().execute();
    if (response.isSuccessful()) {
        return response.body();
    } else {
        LOG.warn("Unable to get process buffer dump on node {}: {}", nodeId, response.message());
        throw new WebApplicationException(response.message(), BAD_GATEWAY);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RemoteSystemResource(org.graylog2.shared.rest.resources.system.RemoteSystemResource) Node(org.graylog2.cluster.Node) SystemProcessBufferDumpResponse(org.graylog2.rest.models.system.responses.SystemProcessBufferDumpResponse) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 55 with Node

use of org.graylog2.indexer.cluster.Node in project graylog2-server by Graylog2.

the class ClusterDeflectorResource method getDeflectorResource.

private RemoteDeflectorResource getDeflectorResource() {
    final Node leader = findLeaderNode();
    final Function<String, Optional<RemoteDeflectorResource>> remoteInterfaceProvider = createRemoteInterfaceProvider(RemoteDeflectorResource.class);
    final Optional<RemoteDeflectorResource> deflectorResource = remoteInterfaceProvider.apply(leader.getNodeId());
    return deflectorResource.orElseThrow(() -> new InternalServerErrorException("Unable to get remote deflector resource."));
}
Also used : RemoteDeflectorResource(org.graylog2.shared.rest.resources.system.RemoteDeflectorResource) Optional(java.util.Optional) Node(org.graylog2.cluster.Node) InternalServerErrorException(javax.ws.rs.InternalServerErrorException)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)29 ApiOperation (io.swagger.annotations.ApiOperation)28 MessageInput (org.graylog2.plugin.inputs.MessageInput)23 ApiResponses (io.swagger.annotations.ApiResponses)19 Path (javax.ws.rs.Path)19 Input (org.graylog2.inputs.Input)15 AuditEvent (org.graylog2.audit.jersey.AuditEvent)14 Test (org.junit.Test)14 GET (javax.ws.rs.GET)13 Produces (javax.ws.rs.Produces)12 Node (org.graylog2.cluster.Node)12 Map (java.util.Map)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 PUT (javax.ws.rs.PUT)6 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)6 Notification (org.graylog2.notifications.Notification)6 Activity (org.graylog2.shared.system.activities.Activity)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 EventBus (com.google.common.eventbus.EventBus)5 URI (java.net.URI)5