Search in sources :

Example 11 with Collector

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

the class V20180212165000_AddDefaultCollectors method ensureCollector.

@Nullable
private String ensureCollector(String collectorName, String serviceType, String nodeOperatingSystem, String executablePath, String executeParameters, String validationCommand, String defaultTemplate) {
    Collector collector = null;
    try {
        collector = collectorService.findByNameAndOs(collectorName, nodeOperatingSystem);
        if (collector == null) {
            final String msg = "Couldn't find collector '{} on {}' fixing it.";
            LOG.error(msg, collectorName, nodeOperatingSystem);
            throw new IllegalArgumentException();
        }
    } catch (IllegalArgumentException ignored) {
        LOG.info("{} collector on {} is missing, adding it.", collectorName, nodeOperatingSystem);
        final Collector newCollector;
        newCollector = Collector.create(null, collectorName, serviceType, nodeOperatingSystem, executablePath, executeParameters, validationCommand, defaultTemplate);
        try {
            return collectorService.save(newCollector).id();
        } catch (Exception e) {
            LOG.error("Can't save collector " + collectorName + ", please restart Graylog to fix this.", e);
        }
    }
    if (collector == null) {
        LOG.error("Unable to access fixed " + collectorName + " collector, please restart Graylog to fix this.");
        return null;
    }
    return collector.id();
}
Also used : Collector(org.graylog.plugins.sidecar.rest.models.Collector) Nullable(javax.annotation.Nullable)

Example 12 with Collector

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

the class CollectorResource method listSummary.

@GET
@Path("/summary")
@RequiresPermissions(SidecarRestPermissions.COLLECTORS_READ)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List a summary of all collectors")
public CollectorSummaryResponse listSummary(@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 = "name,id,collector_id") @DefaultValue(Collector.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
    final SearchQuery searchQuery = searchQueryParser.parse(query);
    final PaginatedList<Collector> collectors = this.collectorService.findPaginated(searchQuery, page, perPage, sort, order);
    final long total = this.collectorService.count();
    final List<CollectorSummary> summaries = collectors.stream().map(CollectorSummary::create).collect(Collectors.toList());
    return CollectorSummaryResponse.create(query, collectors.pagination(), total, sort, order, summaries);
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) Collector(org.graylog.plugins.sidecar.rest.models.Collector) CollectorSummary(org.graylog.plugins.sidecar.rest.models.CollectorSummary) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 13 with Collector

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

the class CollectorResource method updateCollector.

@PUT
@Path("/{id}")
@RequiresPermissions(SidecarRestPermissions.COLLECTORS_UPDATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update a collector")
@AuditEvent(type = SidecarAuditEventTypes.COLLECTOR_UPDATE)
public Response updateCollector(@ApiParam(name = "id", required = true) @PathParam("id") String id, @ApiParam(name = "JSON body", required = true) @Valid @NotNull Collector request) throws BadRequestException {
    Collector collector = collectorService.fromRequest(id, request);
    final ValidationResult validationResult = validate(collector);
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    etagService.invalidateAll();
    return Response.ok().entity(collectorService.save(collector)).build();
}
Also used : Collector(org.graylog.plugins.sidecar.rest.models.Collector) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 14 with Collector

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

the class CollectorResource method listCollectors.

@GET
@Timed
@RequiresPermissions(SidecarRestPermissions.COLLECTORS_READ)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List all collectors")
public Response listCollectors(@Context HttpHeaders httpHeaders) {
    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 collector list from database if client is outdated
    if (!etagCached) {
        final List<Collector> result = this.collectorService.all();
        CollectorListResponse collectorListResponse = CollectorListResponse.create(result.size(), result);
        // add new etag to cache
        String etagString = collectorsToEtag(collectorListResponse);
        EntityTag collectorsEtag = new EntityTag(etagString);
        builder = Response.ok(collectorListResponse);
        builder.tag(collectorsEtag);
        etagService.put(collectorsEtag.toString());
    }
    // set cache control
    CacheControl cacheControl = new CacheControl();
    cacheControl.setNoTransform(true);
    cacheControl.setPrivate(true);
    builder.cacheControl(cacheControl);
    return builder.build();
}
Also used : CollectorListResponse(org.graylog.plugins.sidecar.rest.responses.CollectorListResponse) Response(javax.ws.rs.core.Response) CollectorSummaryResponse(org.graylog.plugins.sidecar.rest.responses.CollectorSummaryResponse) CollectorListResponse(org.graylog.plugins.sidecar.rest.responses.CollectorListResponse) Collector(org.graylog.plugins.sidecar.rest.models.Collector) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) 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 15 with Collector

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

the class AdministrationResource method setAction.

@PUT
@Timed
@Path("/action")
@RequiresPermissions(SidecarRestPermissions.SIDECARS_UPDATE)
@ApiOperation(value = "Set collector actions in bulk")
@ApiResponses(value = { @ApiResponse(code = 400, message = "The supplied action is not valid.") })
@AuditEvent(type = SidecarAuditEventTypes.ACTION_UPDATE)
public Response setAction(@ApiParam(name = "JSON body", required = true) @Valid @NotNull BulkActionsRequest request) {
    for (BulkActionRequest bulkActionRequest : request.collectors()) {
        final List<CollectorAction> actions = bulkActionRequest.collectorIds().stream().map(collectorId -> CollectorAction.create(collectorId, request.action())).collect(Collectors.toList());
        final CollectorActions collectorActions = actionService.fromRequest(bulkActionRequest.sidecarId(), actions);
        actionService.saveAction(collectorActions);
    }
    return Response.accepted().build();
}
Also used : 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) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) AdministrationFilter(org.graylog.plugins.sidecar.filter.AdministrationFilter) MediaType(javax.ws.rs.core.MediaType) CollectorService(org.graylog.plugins.sidecar.services.CollectorService) SidecarConfiguration(org.graylog.plugins.sidecar.system.SidecarConfiguration) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) Consumes(javax.ws.rs.Consumes) Locale(java.util.Locale) Map(java.util.Map) PluginRestResource(org.graylog2.plugin.rest.PluginRestResource) BadRequestException(javax.ws.rs.BadRequestException) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) Predicate(java.util.function.Predicate) CollectorAction(org.graylog.plugins.sidecar.rest.models.CollectorAction) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) BulkActionRequest(org.graylog.plugins.sidecar.rest.requests.BulkActionRequest) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) SidecarSummary(org.graylog.plugins.sidecar.rest.models.SidecarSummary) Response(javax.ws.rs.core.Response) BulkActionsRequest(org.graylog.plugins.sidecar.rest.requests.BulkActionsRequest) Optional(java.util.Optional) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) ConfigurationService(org.graylog.plugins.sidecar.services.ConfigurationService) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) SearchQueryParser(org.graylog2.search.SearchQueryParser) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) ImmutableList(com.google.common.collect.ImmutableList) AdministrationFiltersFactory(org.graylog.plugins.sidecar.filter.AdministrationFiltersFactory) AuditEvent(org.graylog2.audit.jersey.AuditEvent) Api(io.swagger.annotations.Api) SidecarRestPermissions(org.graylog.plugins.sidecar.permissions.SidecarRestPermissions) SearchQuery(org.graylog2.search.SearchQuery) AdministrationRequest(org.graylog.plugins.sidecar.rest.requests.AdministrationRequest) POST(javax.ws.rs.POST) ActiveSidecarFilter(org.graylog.plugins.sidecar.filter.ActiveSidecarFilter) 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) Collector(org.graylog.plugins.sidecar.rest.models.Collector) PUT(javax.ws.rs.PUT) SidecarStatusMapper(org.graylog.plugins.sidecar.mapper.SidecarStatusMapper) BulkActionRequest(org.graylog.plugins.sidecar.rest.requests.BulkActionRequest) CollectorActions(org.graylog.plugins.sidecar.rest.models.CollectorActions) CollectorAction(org.graylog.plugins.sidecar.rest.models.CollectorAction) 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) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Collector (org.graylog.plugins.sidecar.rest.models.Collector)18 ApiOperation (io.swagger.annotations.ApiOperation)7 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)7 Produces (javax.ws.rs.Produces)6 Path (javax.ws.rs.Path)5 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)5 SidecarCollectorEntity (org.graylog2.contentpacks.model.entities.SidecarCollectorEntity)5 Test (org.junit.Test)5 POST (javax.ws.rs.POST)4 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)4 Timed (com.codahale.metrics.annotation.Timed)3 PUT (javax.ws.rs.PUT)3 Response (javax.ws.rs.core.Response)3 ValidationResult (org.graylog2.plugin.rest.ValidationResult)3 ImmutableList (com.google.common.collect.ImmutableList)2 Api (io.swagger.annotations.Api)2 ApiParam (io.swagger.annotations.ApiParam)2 ApiResponse (io.swagger.annotations.ApiResponse)2 ApiResponses (io.swagger.annotations.ApiResponses)2