use of org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage in project opennms by OpenNMS.
the class TelemetrySinkModule method getAggregationPolicy.
@Override
public AggregationPolicy<TelemetryMessage, TelemetryProtos.TelemetryMessageLog, TelemetryProtos.TelemetryMessageLog.Builder> getAggregationPolicy() {
final String systemId = distPollerDao.whoami().getId();
final String systemLocation = distPollerDao.whoami().getLocation();
return new AggregationPolicy<TelemetryMessage, TelemetryProtos.TelemetryMessageLog, TelemetryProtos.TelemetryMessageLog.Builder>() {
@Override
public int getCompletionSize() {
return protocol.getBatchSize().orElse(DEFAULT_BATCH_SIZE);
}
@Override
public int getCompletionIntervalMs() {
return protocol.getBatchIntervalMs().orElse(DEFAULT_BATCH_INTERVAL_MS);
}
@Override
public Object key(TelemetryMessage telemetryMessage) {
return telemetryMessage.getSource();
}
@Override
public TelemetryProtos.TelemetryMessageLog.Builder aggregate(TelemetryProtos.TelemetryMessageLog.Builder accumulator, TelemetryMessage message) {
if (accumulator == null) {
accumulator = TelemetryProtos.TelemetryMessageLog.newBuilder().setLocation(systemLocation).setSystemId(systemId).setSourceAddress(message.getSource().getHostString()).setSourcePort(message.getSource().getPort());
}
final TelemetryProtos.TelemetryMessage messageDto = TelemetryProtos.TelemetryMessage.newBuilder().setTimestamp(message.getReceivedAt().getTime()).setBytes(ByteString.copyFrom(message.getBuffer())).build();
// Append
accumulator.addMessage(messageDto);
return accumulator;
}
@Override
public TelemetryProtos.TelemetryMessageLog build(TelemetryProtos.TelemetryMessageLog.Builder accumulator) {
return accumulator.build();
}
};
}
use of org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage in project opennms by OpenNMS.
the class Telemetryd method start.
@Override
public synchronized void start() throws Exception {
if (consumers.size() > 0) {
throw new IllegalStateException(NAME + " is already started.");
}
LOG.info("{} is starting.", NAME);
final TelemetrydConfiguration config = telemetrydConfigDao.getContainer().getObject();
final AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
for (Protocol protocol : config.getProtocols()) {
if (!protocol.getEnabled()) {
LOG.debug("Skipping disabled protocol: {}", protocol.getName());
continue;
}
LOG.debug("Setting up protocol: {}", protocol.getName());
// Create a Sink module using the protocol definition.
// This allows for protocol to each have their respective queues and thread
// related settings to help limit the impact of one protocol on another.
final TelemetrySinkModule sinkModule = new TelemetrySinkModule(protocol);
beanFactory.autowireBean(sinkModule);
beanFactory.initializeBean(sinkModule, "sinkModule");
// Create the consumer, but don't start it yet
final TelemetryMessageConsumer consumer = new TelemetryMessageConsumer(protocol, sinkModule);
beanFactory.autowireBean(consumer);
beanFactory.initializeBean(consumer, "consumer");
consumers.add(consumer);
// Build the dispatcher, and all of
final AsyncDispatcher<TelemetryMessage> dispatcher = messageDispatcherFactory.createAsyncDispatcher(sinkModule);
dispatchers.add(dispatcher);
for (org.opennms.netmgt.telemetry.config.model.Listener listenerDef : protocol.getListeners()) {
listeners.add(ListenerFactory.buildListener(listenerDef, dispatcher));
}
}
// Start the consumers
for (TelemetryMessageConsumer consumer : consumers) {
LOG.info("Starting consumer for {} protocol.", consumer.getProtocol().getName());
messageConsumerManager.registerConsumer(consumer);
}
// Start the listeners
for (Listener listener : listeners) {
LOG.info("Starting {} listener.", listener.getName());
listener.start();
}
LOG.info("{} is started.", NAME);
}
use of org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage in project opennms by OpenNMS.
the class ListenerManager method deleted.
@Override
public void deleted(String pid) {
final Listener listener = listenersByPid.remove(pid);
if (listener != null) {
LOG.info("Stopping listener for pid: {}", pid);
try {
listener.stop();
} catch (InterruptedException e) {
LOG.error("Error occured while stopping listener for pid: {}", pid, e);
}
}
final AsyncDispatcher<TelemetryMessage> dispatcher = dispatchersByPid.remove(pid);
if (dispatcher != null) {
LOG.info("Closing dispatcher for pid: {}", pid);
try {
dispatcher.close();
} catch (Exception e) {
LOG.error("Error occured while closing dispatcher for pid: {}", pid, e);
}
}
}
use of org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage in project opennms by OpenNMS.
the class UdpListener method start.
public void start() throws InterruptedException {
bossGroup = new NioEventLoopGroup();
final Bootstrap b = new Bootstrap().group(bossGroup).channel(NioDatagramChannel.class).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_RCVBUF, Integer.MAX_VALUE).option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(maxPacketSize)).handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new MessageToMessageDecoder<DatagramPacket>() {
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
// Wrap the contents of the packet in a ByteBuffer, referencing
// the underlying byte array if possible
final ByteBuffer buffer = wrapContentsWithNioByteBuffer(packet);
// Build the message to dispatch via the Sink API
final TelemetryMessage msg = new TelemetryMessage(packet.sender(), buffer);
// Dispatch and retain a reference to the packet
// in the case that we are sharing the underlying byte array
final CompletableFuture<TelemetryMessage> future = dispatcher.send(msg);
packet.retain();
future.whenComplete((res, ex) -> packet.release());
}
});
}
});
future = b.bind(host, port).await();
}
use of org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage in project opennms by OpenNMS.
the class ListenerManager method updated.
@Override
public void updated(String pid, Dictionary<String, ?> properties) {
final Listener existingListener = listenersByPid.get(pid);
if (existingListener != null) {
LOG.info("Updating existing listener/dispatcher for pid: {}", pid);
deleted(pid);
} else {
LOG.info("Creating new listener/dispatcher for pid: {}", pid);
}
// Convert the dictionary to a map
final Map<String, String> parameters = MapUtils.fromDict(properties);
// Build the protocol and listener definitions
final MapBasedProtocolDef protocolDef = new MapBasedProtocolDef(parameters);
final MapBasedListenerDef listenerDef = new MapBasedListenerDef(parameters);
final TelemetrySinkModule sinkModule = new TelemetrySinkModule(protocolDef);
sinkModule.setDistPollerDao(distPollerDao);
final AsyncDispatcher<TelemetryMessage> dispatcher = messageDispatcherFactory.createAsyncDispatcher(sinkModule);
try {
final Listener listener = ListenerFactory.buildListener(listenerDef, dispatcher);
listener.start();
listenersByPid.put(pid, listener);
dispatchersByPid.put(pid, dispatcher);
} catch (Exception e) {
LOG.error("Failed to build listener.", e);
try {
dispatcher.close();
} catch (Exception ee) {
LOG.error("Failed to close dispatcher.", e);
}
}
LOG.info("Successfully started listener/dispatcher for pid: {}", pid);
}
Aggregations