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();
}
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();
}
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);
}
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();
}
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;
}
Aggregations