Search in sources :

Example 1 with TelemetryMessage

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();
        }
    };
}
Also used : ByteString(com.google.protobuf.ByteString) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage) AggregationPolicy(org.opennms.core.ipc.sink.api.AggregationPolicy)

Example 2 with TelemetryMessage

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);
}
Also used : TelemetrySinkModule(org.opennms.netmgt.telemetry.ipc.TelemetrySinkModule) Listener(org.opennms.netmgt.telemetry.listeners.api.Listener) EventListener(org.opennms.netmgt.events.api.annotations.EventListener) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage) TelemetrydConfiguration(org.opennms.netmgt.telemetry.config.model.TelemetrydConfiguration) Protocol(org.opennms.netmgt.telemetry.config.model.Protocol)

Example 3 with TelemetryMessage

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);
        }
    }
}
Also used : Listener(org.opennms.netmgt.telemetry.listeners.api.Listener) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage)

Example 4 with TelemetryMessage

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();
}
Also used : DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuffer(java.nio.ByteBuffer) ChannelPipeline(io.netty.channel.ChannelPipeline) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage) MessageToMessageDecoder(io.netty.handler.codec.MessageToMessageDecoder) DatagramPacket(io.netty.channel.socket.DatagramPacket) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) Bootstrap(io.netty.bootstrap.Bootstrap) List(java.util.List) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 5 with TelemetryMessage

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);
}
Also used : TelemetrySinkModule(org.opennms.netmgt.telemetry.ipc.TelemetrySinkModule) Listener(org.opennms.netmgt.telemetry.listeners.api.Listener) TelemetryMessage(org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage)

Aggregations

TelemetryMessage (org.opennms.netmgt.telemetry.listeners.api.TelemetryMessage)5 Listener (org.opennms.netmgt.telemetry.listeners.api.Listener)3 TelemetrySinkModule (org.opennms.netmgt.telemetry.ipc.TelemetrySinkModule)2 ByteString (com.google.protobuf.ByteString)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 FixedRecvByteBufAllocator (io.netty.channel.FixedRecvByteBufAllocator)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 DatagramChannel (io.netty.channel.socket.DatagramChannel)1 DatagramPacket (io.netty.channel.socket.DatagramPacket)1 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)1 MessageToMessageDecoder (io.netty.handler.codec.MessageToMessageDecoder)1 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 AggregationPolicy (org.opennms.core.ipc.sink.api.AggregationPolicy)1 EventListener (org.opennms.netmgt.events.api.annotations.EventListener)1 Protocol (org.opennms.netmgt.telemetry.config.model.Protocol)1 TelemetrydConfiguration (org.opennms.netmgt.telemetry.config.model.TelemetrydConfiguration)1 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)1