Tuesday, July 08, 2008

Experimental Snmp trapd as event source for RHQ (+ comments about Event polling)

I have just committed an experimental SNMP trapd plugin for RHQ. Currently it is only able to listen for V1 traps at a fixed address, but I am sure this will change :)
Incoming traps will be forwarded as Events into the events subsystem, so you can view them in the GUI and even define alarms on them (that trigger SNMP traps :-)

The plugin has its own page within the RHQ plugin community pages. This page also shows the location in SVN. The plugin is marked as experimental, meaning that it is not linked in the parent pom. To build it, you need to go into the plugin base directory and build from there.

Events processing

This plugin is also an example for processing of Events. In addition to the three components that you already know from my plugin development series, you need an EventPoller - that is a class with a method that gets called at regular intervals and which pulls the event data in. Lets have a look at the Component class, the plugin descriptor and the Poller. As the discovery component more or less follows what you have seen in part 3 of the series, I am not going to show this again.

Plugin Descriptor

The plugin descriptor is mostly as we know it. There is now one new element:

<event name="SnmpTrap" description="One single incoming trap"/>

The important part here is the name attribute, as we will need its content later again. The name is the key into the EventDefinition object.

Plugin Component

In the plugin component, we are using start() and stop() to start and stop polling for events:

public void start(ResourceContext context) throws
InvalidPluginConfigurationException, Exception {
eventContext = context.getEventContext();
snmpTrapEventPoller = new SnmpTrapEventPoller();
eventContext.registerEventPoller(snmpTrapEventPoller, 60);

So first we are getting an EventContext from the passed ResourceContext, Instantiate an EventPoller and register this Poller with the EventContext (60 is the number of seconds between polls).
The plugin container will start its timer when this registration is done.

In stop() we just unregister the poller again:


TRAP_TYPE is the ResourceType name as String - we will see this again in a second.

The remainder of this class is nothing special and if you have read the plugin development series, it should actually be no news at all.

Event Poller

This class is the only real new piece in the game.

public class SnmpTrapEventPoller implements EventPoller {

Implementing EventPoller means to implement two methods:

public String getEventType() {
return SnmpTrapdComponent.TRAP_TYPE;

Here we return the content of the name attribute from the <event> tag of the plugin descriptor. The plugin will not start if they don't match.

The other method to implement is poll():

public Set<Event> poll() {
Set<Event> eventSet = new HashSet<Event>();
return eventSet;

To create one Event object you just instantiate it. The needed type can just be obtained by a call to getEventType().

That's all

Well, that's all. Source is in the RHQ subversion repository - go and check out the sources yourself.

Feedback is always appreciated.

Technorati Tags:
, ,

No comments: