Search in sources :

Example 1 with Activity

use of org.graylog2.shared.system.activities.Activity in project graylog2-server by Graylog2.

the class MongoIndexSet method cycle.

@Override
public void cycle() {
    if (!getConfig().isWritable()) {
        LOG.debug("Not cycling non-writable index set <{}> ({})", getConfig().id(), getConfig().title());
        return;
    }
    int oldTargetNumber;
    try {
        oldTargetNumber = getNewestIndexNumber();
    } catch (NoTargetIndexException ex) {
        oldTargetNumber = -1;
    }
    final int newTargetNumber = oldTargetNumber + 1;
    final String newTarget = buildIndexName(newTargetNumber);
    final String oldTarget = buildIndexName(oldTargetNumber);
    if (oldTargetNumber == -1) {
        LOG.info("Cycling from <none> to <{}>.", newTarget);
    } else {
        LOG.info("Cycling from <{}> to <{}>.", oldTarget, newTarget);
    }
    // Create new index.
    LOG.info("Creating target index <{}>.", newTarget);
    if (!indices.create(newTarget, this)) {
        throw new RuntimeException("Could not create new target index <" + newTarget + ">.");
    }
    LOG.info("Waiting for allocation of index <{}>.", newTarget);
    final HealthStatus healthStatus = indices.waitForRecovery(newTarget);
    checkIfHealthy(healthStatus, (status) -> new RuntimeException("New target index did not become healthy (target index: <" + newTarget + ">)"));
    LOG.debug("Health status of index <{}>: {}", newTarget, healthStatus);
    addDeflectorIndexRange(newTarget);
    LOG.info("Index <{}> has been successfully allocated.", newTarget);
    // Point deflector to new index.
    final String indexAlias = getWriteIndexAlias();
    LOG.info("Pointing index alias <{}> to new index <{}>.", indexAlias, newTarget);
    final Activity activity = new Activity(IndexSet.class);
    if (oldTargetNumber == -1) {
        // Only pointing, not cycling.
        pointTo(newTarget);
        activity.setMessage("Cycled index alias <" + indexAlias + "> from <none> to <" + newTarget + ">.");
    } else {
        // Re-pointing from existing old index to the new one.
        LOG.debug("Switching over index alias <{}>.", indexAlias);
        pointTo(newTarget, oldTarget);
        setIndexReadOnlyAndCalculateRange(oldTarget);
        activity.setMessage("Cycled index alias <" + indexAlias + "> from <" + oldTarget + "> to <" + newTarget + ">.");
    }
    LOG.info("Successfully pointed index alias <{}> to index <{}>.", indexAlias, newTarget);
    activityWriter.write(activity);
    auditEventSender.success(AuditActor.system(nodeId), ES_WRITE_INDEX_UPDATE, ImmutableMap.of("indexName", newTarget));
}
Also used : HealthStatus(org.graylog2.indexer.indices.HealthStatus) Activity(org.graylog2.shared.system.activities.Activity)

Example 2 with Activity

use of org.graylog2.shared.system.activities.Activity in project graylog2-server by Graylog2.

the class AbstractIndexCountBasedRetentionStrategy method runRetention.

private void runRetention(IndexSet indexSet, Map<String, Set<String>> deflectorIndices, int removeCount) {
    final Set<String> orderedIndices = Arrays.stream(indexSet.getManagedIndices()).filter(indexName -> !indices.isReopened(indexName)).filter(indexName -> !(deflectorIndices.getOrDefault(indexName, Collections.emptySet()).contains(indexSet.getWriteIndexAlias()))).sorted((indexName1, indexName2) -> indexSet.extractIndexNumber(indexName2).orElse(0).compareTo(indexSet.extractIndexNumber(indexName1).orElse(0))).collect(Collectors.toCollection(LinkedHashSet::new));
    LinkedList<String> orderedIndicesDescending = new LinkedList<>();
    orderedIndices.stream().skip(orderedIndices.size() - removeCount).collect(Collectors.toCollection(LinkedList::new)).descendingIterator().forEachRemaining(orderedIndicesDescending::add);
    String indexNamesAsString = String.join(", ", orderedIndicesDescending);
    final String strategyName = this.getClass().getCanonicalName();
    final String msg = "Running retention strategy [" + strategyName + "] for indices <" + indexNamesAsString + ">";
    LOG.info(msg);
    activityWriter.write(new Activity(msg, IndexRetentionThread.class));
    retain(orderedIndicesDescending, indexSet);
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) RetentionStrategy(org.graylog2.plugin.indexer.retention.RetentionStrategy) Collectors(java.util.stream.Collectors) ActivityWriter(org.graylog2.shared.system.activities.ActivityWriter) List(java.util.List) IndexRetentionThread(org.graylog2.periodical.IndexRetentionThread) Indices(org.graylog2.indexer.indices.Indices) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) IndexSet(org.graylog2.indexer.IndexSet) LinkedList(java.util.LinkedList) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) Activity(org.graylog2.shared.system.activities.Activity) IndexRetentionThread(org.graylog2.periodical.IndexRetentionThread) Activity(org.graylog2.shared.system.activities.Activity) LinkedList(java.util.LinkedList)

Example 3 with Activity

use of org.graylog2.shared.system.activities.Activity in project graylog2-server by Graylog2.

the class AbstractIndexCountBasedRetentionStrategy method retain.

@Override
public void retain(IndexSet indexSet) {
    final Map<String, Set<String>> deflectorIndices = indexSet.getAllIndexAliases();
    final int indexCount = (int) deflectorIndices.keySet().stream().filter(indexName -> !indices.isReopened(indexName)).count();
    final Optional<Integer> maxIndices = getMaxNumberOfIndices(indexSet);
    if (!maxIndices.isPresent()) {
        LOG.warn("No retention strategy configuration found, not running index retention!");
        return;
    }
    // Do we have more indices than the configured maximum?
    if (indexCount <= maxIndices.get()) {
        LOG.debug("Number of indices ({}) lower than limit ({}). Not performing any retention actions.", indexCount, maxIndices.get());
        return;
    }
    // We have more indices than the configured maximum! Remove as many as needed.
    final int removeCount = indexCount - maxIndices.get();
    final String msg = "Number of indices (" + indexCount + ") higher than limit (" + maxIndices.get() + "). " + "Running retention for " + removeCount + " indices.";
    LOG.info(msg);
    activityWriter.write(new Activity(msg, IndexRetentionThread.class));
    runRetention(indexSet, deflectorIndices, removeCount);
}
Also used : Set(java.util.Set) IndexSet(org.graylog2.indexer.IndexSet) LinkedHashSet(java.util.LinkedHashSet) IndexRetentionThread(org.graylog2.periodical.IndexRetentionThread) Activity(org.graylog2.shared.system.activities.Activity)

Example 4 with Activity

use of org.graylog2.shared.system.activities.Activity in project graylog2-server by Graylog2.

the class InputStateListener method inputStateChanged.

@Subscribe
public void inputStateChanged(IOStateChangedEvent<MessageInput> event) {
    final IOState<MessageInput> state = event.changedState();
    final MessageInput input = state.getStoppable();
    switch(event.newState()) {
        case FAILED:
            activityWriter.write(new Activity(state.getDetailedMessage(), InputRegistry.class));
            Notification notification = notificationService.buildNow();
            notification.addType(Notification.Type.INPUT_FAILED_TO_START).addSeverity(Notification.Severity.NORMAL);
            notification.addNode(serverStatus.getNodeId().toString());
            notification.addDetail("input_id", input.getId());
            notification.addDetail("reason", state.getDetailedMessage());
            notificationService.publishIfFirst(notification);
            break;
        case RUNNING:
            notificationService.fixed(Notification.Type.NO_INPUT_RUNNING);
        // fall through
        default:
            final String msg = "Input [" + input.getName() + "/" + input.getId() + "] is now " + event.newState().toString();
            activityWriter.write(new Activity(msg, InputStateListener.class));
            break;
    }
    LOG.debug("Input State of [{}/{}] changed: {} -> {}", input.getTitle(), input.getId(), event.oldState(), event.newState());
    LOG.info("Input [{}/{}] is now {}", input.getName(), input.getId(), event.newState());
}
Also used : MessageInput(org.graylog2.plugin.inputs.MessageInput) Activity(org.graylog2.shared.system.activities.Activity) InputRegistry(org.graylog2.shared.inputs.InputRegistry) Notification(org.graylog2.notifications.Notification) Subscribe(com.google.common.eventbus.Subscribe)

Example 5 with Activity

use of org.graylog2.shared.system.activities.Activity in project graylog2-server by Graylog2.

the class ServerBootstrap method startCommand.

@Override
protected void startCommand() {
    final AuditEventSender auditEventSender = injector.getInstance(AuditEventSender.class);
    final NodeId nodeId = injector.getInstance(NodeId.class);
    final String systemInformation = Tools.getSystemInformation();
    final Map<String, Object> auditEventContext = ImmutableMap.of("version", version.toString(), "java", systemInformation, "node_id", nodeId.toString());
    auditEventSender.success(AuditActor.system(nodeId), NODE_STARTUP_INITIATE, auditEventContext);
    final OS os = OS.getOs();
    LOG.info("Graylog {} {} starting up", commandName, version);
    LOG.info("JRE: {}", systemInformation);
    LOG.info("Deployment: {}", configuration.getInstallationSource());
    LOG.info("OS: {}", os.getPlatformName());
    LOG.info("Arch: {}", os.getArch());
    try {
        if (configuration.isLeader() && configuration.runMigrations()) {
            runMigrations();
        }
    } catch (Exception e) {
        LOG.warn("Exception while running migrations", e);
        System.exit(1);
    }
    final ServerStatus serverStatus = injector.getInstance(ServerStatus.class);
    serverStatus.initialize();
    startNodeRegistration(injector);
    final ActivityWriter activityWriter;
    final ServiceManager serviceManager;
    final Service leaderElectionService;
    try {
        activityWriter = injector.getInstance(ActivityWriter.class);
        serviceManager = injector.getInstance(ServiceManager.class);
        leaderElectionService = injector.getInstance(Key.get(Service.class, Names.named("LeaderElectionService")));
    } catch (ProvisionException e) {
        LOG.error("Guice error", e);
        annotateProvisionException(e);
        auditEventSender.failure(AuditActor.system(nodeId), NODE_STARTUP_INITIATE, auditEventContext);
        System.exit(-1);
        return;
    } catch (Exception e) {
        LOG.error("Unexpected exception", e);
        auditEventSender.failure(AuditActor.system(nodeId), NODE_STARTUP_INITIATE, auditEventContext);
        System.exit(-1);
        return;
    }
    Runtime.getRuntime().addShutdownHook(new Thread(injector.getInstance(shutdownHook())));
    // propagate default size to input plugins
    MessageInput.setDefaultRecvBufferSize(configuration.getUdpRecvBufferSizes());
    // Start services.
    final ServiceManagerListener serviceManagerListener = injector.getInstance(ServiceManagerListener.class);
    serviceManager.addListener(serviceManagerListener, MoreExecutors.directExecutor());
    try {
        leaderElectionService.startAsync().awaitRunning();
        serviceManager.startAsync().awaitHealthy();
    } catch (Exception e) {
        try {
            serviceManager.stopAsync().awaitStopped(configuration.getShutdownTimeout(), TimeUnit.MILLISECONDS);
        } catch (TimeoutException timeoutException) {
            LOG.error("Unable to shutdown properly on time. {}", serviceManager.servicesByState());
        }
        LOG.error("Graylog startup failed. Exiting. Exception was:", e);
        auditEventSender.failure(AuditActor.system(nodeId), NODE_STARTUP_INITIATE, auditEventContext);
        System.exit(-1);
    }
    LOG.info("Services started, startup times in ms: {}", serviceManager.startupTimes());
    activityWriter.write(new Activity("Started up.", Main.class));
    LOG.info("Graylog " + commandName + " up and running.");
    auditEventSender.success(AuditActor.system(nodeId), NODE_STARTUP_COMPLETE, auditEventContext);
    // Block forever.
    try {
        Thread.currentThread().join();
    } catch (InterruptedException e) {
        return;
    }
}
Also used : OS(org.jsoftbiz.utils.OS) PreflightCheckService(org.graylog2.bootstrap.preflight.PreflightCheckService) Service(com.google.common.util.concurrent.Service) Activity(org.graylog2.shared.system.activities.Activity) TimeoutException(java.util.concurrent.TimeoutException) ProvisionException(com.google.inject.ProvisionException) ProvisionException(com.google.inject.ProvisionException) AuditEventSender(org.graylog2.audit.AuditEventSender) ServiceManager(com.google.common.util.concurrent.ServiceManager) ServerStatus(org.graylog2.plugin.ServerStatus) NodeId(org.graylog2.plugin.system.NodeId) ServiceManagerListener(org.graylog2.shared.initializers.ServiceManagerListener) ActivityWriter(org.graylog2.shared.system.activities.ActivityWriter) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Activity (org.graylog2.shared.system.activities.Activity)23 Timed (com.codahale.metrics.annotation.Timed)9 ApiOperation (io.swagger.annotations.ApiOperation)9 AuditEvent (org.graylog2.audit.jersey.AuditEvent)9 ApiResponses (io.swagger.annotations.ApiResponses)7 Path (javax.ws.rs.Path)7 Produces (javax.ws.rs.Produces)7 MessageInput (org.graylog2.plugin.inputs.MessageInput)6 Input (org.graylog2.inputs.Input)5 Consumes (javax.ws.rs.Consumes)4 DELETE (javax.ws.rs.DELETE)4 POST (javax.ws.rs.POST)4 IndexSet (org.graylog2.indexer.IndexSet)4 Notification (org.graylog2.notifications.Notification)4 BadRequestException (javax.ws.rs.BadRequestException)3 NotFoundException (org.graylog2.database.NotFoundException)3 Extractor (org.graylog2.plugin.inputs.Extractor)3 ActivityWriter (org.graylog2.shared.system.activities.ActivityWriter)3 URI (java.net.URI)2 LinkedHashSet (java.util.LinkedHashSet)2