Search in sources :

Example 1 with Sidecar

use of org.graylog.plugins.sidecar.rest.models.Sidecar in project graylog2-server by Graylog2.

the class ConfigurationResource method renderConfiguration.

@GET
@Timed
@Path("/render/{sidecarId}/{configurationId}")
@Produces(MediaType.APPLICATION_JSON)
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_READ)
@ApiOperation(value = "Render configuration template")
public Response renderConfiguration(@Context HttpHeaders httpHeaders, @ApiParam(name = "sidecarId", required = true) @PathParam("sidecarId") String sidecarId, @ApiParam(name = "configurationId", required = true) @PathParam("configurationId") String configurationId) throws RenderTemplateException {
    String ifNoneMatch = httpHeaders.getHeaderString("If-None-Match");
    Boolean etagCached = false;
    Response.ResponseBuilder builder = Response.noContent();
    // check if client is up to date with a known valid etag
    if (ifNoneMatch != null) {
        EntityTag etag = new EntityTag(ifNoneMatch.replaceAll("\"", ""));
        if (etagService.isPresent(etag.toString())) {
            etagCached = true;
            builder = Response.notModified();
            builder.tag(etag);
        }
    }
    // fetch configuration from database if client is outdated
    if (!etagCached) {
        Sidecar sidecar = sidecarService.findByNodeId(sidecarId);
        if (sidecar == null) {
            throw new NotFoundException("Couldn't find Sidecar by ID: " + sidecarId);
        }
        Configuration configuration = configurationService.find(configurationId);
        if (configuration == null) {
            throw new NotFoundException("Couldn't find configuration by ID: " + configurationId);
        }
        Configuration collectorConfiguration = this.configurationService.renderConfigurationForCollector(sidecar, configuration);
        // add new etag to cache
        String etagString = configurationToEtag(collectorConfiguration);
        EntityTag collectorConfigurationEtag = new EntityTag(etagString);
        builder = Response.ok(collectorConfiguration);
        builder.tag(collectorConfigurationEtag);
        etagService.put(collectorConfigurationEtag.toString());
    }
    // set cache control
    CacheControl cacheControl = new CacheControl();
    cacheControl.setNoTransform(true);
    cacheControl.setPrivate(true);
    builder.cacheControl(cacheControl);
    return builder.build();
}
Also used : ConfigurationSidecarsResponse(org.graylog.plugins.sidecar.rest.responses.ConfigurationSidecarsResponse) Response(javax.ws.rs.core.Response) ConfigurationPreviewRenderResponse(org.graylog.plugins.sidecar.rest.responses.ConfigurationPreviewRenderResponse) CollectorUploadListResponse(org.graylog.plugins.sidecar.rest.responses.CollectorUploadListResponse) ConfigurationListResponse(org.graylog.plugins.sidecar.rest.responses.ConfigurationListResponse) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) NotFoundException(javax.ws.rs.NotFoundException) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with Sidecar

use of org.graylog.plugins.sidecar.rest.models.Sidecar in project graylog2-server by Graylog2.

the class SidecarResource method assignConfiguration.

@PUT
@Timed
@Path("/configurations")
@ApiOperation(value = "Assign configurations to sidecars")
@RequiresPermissions(SidecarRestPermissions.SIDECARS_UPDATE)
@AuditEvent(type = SidecarAuditEventTypes.SIDECAR_UPDATE)
public Response assignConfiguration(@ApiParam(name = "JSON body", required = true) @Valid @NotNull NodeConfigurationRequest request) throws NotFoundException {
    List<String> nodeIdList = request.nodes().stream().filter(distinctByKey(NodeConfiguration::nodeId)).map(NodeConfiguration::nodeId).collect(Collectors.toList());
    for (String nodeId : nodeIdList) {
        List<ConfigurationAssignment> nodeRelations = request.nodes().stream().filter(a -> a.nodeId().equals(nodeId)).flatMap(a -> a.assignments().stream()).collect(Collectors.toList());
        try {
            Sidecar sidecar = sidecarService.assignConfiguration(nodeId, nodeRelations);
            sidecarService.save(sidecar);
        } catch (org.graylog2.database.NotFoundException e) {
            throw new NotFoundException(e.getMessage());
        }
    }
    return Response.accepted().build();
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) CollectorActions(org.graylog.plugins.sidecar.rest.models.CollectorActions) Produces(javax.ws.rs.Produces) SidecarService(org.graylog.plugins.sidecar.services.SidecarService) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) NodeConfiguration(org.graylog.plugins.sidecar.rest.models.NodeConfiguration) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) SidecarConfiguration(org.graylog.plugins.sidecar.system.SidecarConfiguration) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) Consumes(javax.ws.rs.Consumes) SearchQueryField(org.graylog2.search.SearchQueryField) Map(java.util.Map) PluginRestResource(org.graylog2.plugin.rest.PluginRestResource) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) BadRequestException(javax.ws.rs.BadRequestException) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) CollectorAction(org.graylog.plugins.sidecar.rest.models.CollectorAction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) Timed(com.codahale.metrics.annotation.Timed) RegistrationRequest(org.graylog.plugins.sidecar.rest.requests.RegistrationRequest) List(java.util.List) SidecarSummary(org.graylog.plugins.sidecar.rest.models.SidecarSummary) Response(javax.ws.rs.core.Response) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) ConfigurationAssignment(org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment) PathParam(javax.ws.rs.PathParam) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) SearchQueryParser(org.graylog2.search.SearchQueryParser) GET(javax.ws.rs.GET) SidecarRegistrationConfiguration(org.graylog.plugins.sidecar.rest.models.SidecarRegistrationConfiguration) NodeConfigurationRequest(org.graylog.plugins.sidecar.rest.requests.NodeConfigurationRequest) ApiResponses(io.swagger.annotations.ApiResponses) Inject(javax.inject.Inject) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RegistrationResponse(org.graylog.plugins.sidecar.rest.responses.RegistrationResponse) AuditEvent(org.graylog2.audit.jersey.AuditEvent) Api(io.swagger.annotations.Api) SidecarRestPermissions(org.graylog.plugins.sidecar.permissions.SidecarRestPermissions) SearchQuery(org.graylog2.search.SearchQuery) ActiveSidecarFilter(org.graylog.plugins.sidecar.filter.ActiveSidecarFilter) DateTime(org.joda.time.DateTime) SidecarAuditEventTypes(org.graylog.plugins.sidecar.audit.SidecarAuditEventTypes) RestResource(org.graylog2.shared.rest.resources.RestResource) SidecarListResponse(org.graylog.plugins.sidecar.rest.responses.SidecarListResponse) ActionService(org.graylog.plugins.sidecar.services.ActionService) ApiResponse(io.swagger.annotations.ApiResponse) NotEmpty(org.hibernate.validator.constraints.NotEmpty) PUT(javax.ws.rs.PUT) SidecarStatusMapper(org.graylog.plugins.sidecar.mapper.SidecarStatusMapper) ConfigurationAssignment(org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment) NotFoundException(javax.ws.rs.NotFoundException) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 3 with Sidecar

use of org.graylog.plugins.sidecar.rest.models.Sidecar in project graylog2-server by Graylog2.

the class SidecarResource method sidecars.

@GET
@Timed
@ApiOperation(value = "Lists existing Sidecar registrations using pagination")
@RequiresPermissions(SidecarRestPermissions.SIDECARS_READ)
public SidecarListResponse sidecars(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "title,description,name,id") @DefaultValue(Sidecar.FIELD_NODE_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order, @ApiParam(name = "only_active") @QueryParam("only_active") @DefaultValue("false") boolean onlyActive) {
    final String mappedQuery = sidecarStatusMapper.replaceStringStatusSearchQuery(query);
    SearchQuery searchQuery;
    try {
        searchQuery = searchQueryParser.parse(mappedQuery);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
    }
    final PaginatedList<Sidecar> sidecars = onlyActive ? sidecarService.findPaginated(searchQuery, activeSidecarFilter, page, perPage, sort, order) : sidecarService.findPaginated(searchQuery, page, perPage, sort, order);
    final List<SidecarSummary> collectorSummaries = sidecarService.toSummaryList(sidecars, activeSidecarFilter);
    final long total = sidecarService.count();
    return SidecarListResponse.create(query, sidecars.pagination(), total, onlyActive, sort, order, collectorSummaries);
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) SidecarSummary(org.graylog.plugins.sidecar.rest.models.SidecarSummary) BadRequestException(javax.ws.rs.BadRequestException) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 4 with Sidecar

use of org.graylog.plugins.sidecar.rest.models.Sidecar in project graylog2-server by Graylog2.

the class SidecarResource method register.

@PUT
@Timed
@Path("/{sidecarId}")
@ApiOperation(value = "Create/update a Sidecar registration", notes = "This is a stateless method which upserts a Sidecar registration")
@ApiResponses(value = { @ApiResponse(code = 400, message = "The supplied request is not valid.") })
@RequiresPermissions(SidecarRestPermissions.SIDECARS_UPDATE)
@NoAuditEvent("this is only a ping from Sidecars, and would overflow the audit log")
public Response register(@ApiParam(name = "sidecarId", value = "The id this Sidecar is registering as.", required = true) @PathParam("sidecarId") @NotEmpty String sidecarId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull RegistrationRequest request, @HeaderParam(value = "X-Graylog-Sidecar-Version") @NotEmpty String sidecarVersion) {
    final Sidecar newSidecar;
    final Sidecar oldSidecar = sidecarService.findByNodeId(sidecarId);
    List<ConfigurationAssignment> assignments = null;
    if (oldSidecar != null) {
        assignments = oldSidecar.assignments();
        newSidecar = oldSidecar.toBuilder().nodeName(request.nodeName()).nodeDetails(request.nodeDetails()).sidecarVersion(sidecarVersion).lastSeen(DateTime.now(DateTimeZone.UTC)).build();
    } else {
        newSidecar = sidecarService.fromRequest(sidecarId, request, sidecarVersion);
    }
    sidecarService.save(newSidecar);
    final CollectorActions collectorActions = actionService.findActionBySidecar(sidecarId, true);
    List<CollectorAction> collectorAction = null;
    if (collectorActions != null) {
        collectorAction = collectorActions.action();
    }
    RegistrationResponse sidecarRegistrationResponse = RegistrationResponse.create(SidecarRegistrationConfiguration.create(this.sidecarConfiguration.sidecarUpdateInterval().toStandardDuration().getStandardSeconds(), this.sidecarConfiguration.sidecarSendStatus()), this.sidecarConfiguration.sidecarConfigurationOverride(), collectorAction, assignments);
    return Response.accepted(sidecarRegistrationResponse).build();
}
Also used : CollectorActions(org.graylog.plugins.sidecar.rest.models.CollectorActions) ConfigurationAssignment(org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment) CollectorAction(org.graylog.plugins.sidecar.rest.models.CollectorAction) RegistrationResponse(org.graylog.plugins.sidecar.rest.responses.RegistrationResponse) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 5 with Sidecar

use of org.graylog.plugins.sidecar.rest.models.Sidecar in project graylog2-server by Graylog2.

the class SidecarService method markExpired.

public int markExpired(Period period, String message) {
    final DateTime threshold = DateTime.now(DateTimeZone.UTC).minus(period);
    int count;
    try (final Stream<Sidecar> collectorStream = streamAll()) {
        count = collectorStream.mapToInt(collector -> {
            if (collector.nodeDetails().statusList() == null) {
                return 0;
            }
            final CollectorStatusList sidecarStatus = collector.nodeDetails().statusList();
            if (collector.lastSeen().isBefore(threshold) && Sidecar.Status.RUNNING.equals(Sidecar.Status.fromStatusCode(sidecarStatus.status()))) {
                NodeDetails nodeDetails = collector.nodeDetails();
                ImmutableSet.Builder<CollectorStatus> collectorStatuses = ImmutableSet.builder();
                for (CollectorStatus collectorStatus : sidecarStatus.collectors()) {
                    collectorStatuses.add(CollectorStatus.create(collectorStatus.collectorId(), Sidecar.Status.UNKNOWN.getStatusCode(), message, ""));
                }
                CollectorStatusList statusListToSave = CollectorStatusList.create(Sidecar.Status.UNKNOWN.getStatusCode(), message, collectorStatuses.build());
                NodeDetails nodeDetailsToSave = NodeDetails.create(nodeDetails.operatingSystem(), nodeDetails.ip(), nodeDetails.metrics(), nodeDetails.logFileList(), statusListToSave);
                Sidecar toSave = collector.toBuilder().nodeDetails(nodeDetailsToSave).build();
                save(toSave);
                return 1;
            }
            return 0;
        }).sum();
    }
    return count;
}
Also used : CollectorStatus(org.graylog.plugins.sidecar.rest.models.CollectorStatus) ImmutableSet(com.google.common.collect.ImmutableSet) CollectorStatusList(org.graylog.plugins.sidecar.rest.models.CollectorStatusList) NodeDetails(org.graylog.plugins.sidecar.rest.models.NodeDetails) DateTime(org.joda.time.DateTime) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar)

Aggregations

Sidecar (org.graylog.plugins.sidecar.rest.models.Sidecar)15 Timed (com.codahale.metrics.annotation.Timed)5 ApiOperation (io.swagger.annotations.ApiOperation)5 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)5 Path (javax.ws.rs.Path)4 SidecarSummary (org.graylog.plugins.sidecar.rest.models.SidecarSummary)4 Test (org.junit.Test)4 ApiResponses (io.swagger.annotations.ApiResponses)3 BadRequestException (javax.ws.rs.BadRequestException)3 GET (javax.ws.rs.GET)3 PUT (javax.ws.rs.PUT)3 Produces (javax.ws.rs.Produces)3 Response (javax.ws.rs.core.Response)3 ActiveSidecarFilter (org.graylog.plugins.sidecar.filter.ActiveSidecarFilter)3 CollectorAction (org.graylog.plugins.sidecar.rest.models.CollectorAction)3 CollectorActions (org.graylog.plugins.sidecar.rest.models.CollectorActions)3 Configuration (org.graylog.plugins.sidecar.rest.models.Configuration)3 ConfigurationAssignment (org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment)3 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)3 SearchQuery (org.graylog2.search.SearchQuery)3