geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r408108 - in /geronimo/xbean/trunk: ./ xbean-jmx/ xbean-jmx/src/main/java/org/apache/xbean/jmx/ xbean-jmx/src/main/java/org/apache/xbean/snmp/ xbean-jmx/src/test/java/org/apache/xbean/snmp/
Date Sat, 20 May 2006 23:27:08 GMT
Author: adc
Date: Sat May 20 16:27:07 2006
New Revision: 408108

URL: http://svn.apache.org/viewvc?rev=408108&view=rev
Log:
XBEAN-11 JMX notification to SNMP converter

Added:
    geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/jmx/Heartbeat.java   (with
props)
    geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/
    geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpBinding.java  
(with props)
    geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpEmitter.java  
(with props)
    geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpServer.java   (with
props)
    geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/
    geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/AbstractJMXSNMPTest.java
  (with props)
    geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/HeartbeatTest.java
  (with props)
    geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/MemoryUsageExampleTest.java
  (with props)
Modified:
    geronimo/xbean/trunk/pom.xml
    geronimo/xbean/trunk/xbean-jmx/pom.xml

Modified: geronimo/xbean/trunk/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/pom.xml?rev=408108&r1=408107&r2=408108&view=diff
==============================================================================
--- geronimo/xbean/trunk/pom.xml (original)
+++ geronimo/xbean/trunk/pom.xml Sat May 20 16:27:07 2006
@@ -4,18 +4,22 @@
     <groupId>org.apache</groupId>
     <version>1</version>
   </parent>
+
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.apache.xbean</groupId>
   <artifactId>xbean</artifactId>
   <packaging>pom</packaging>
+
   <name>XBean</name>
   <version>2.4-SNAPSHOT</version>
   <description>XBean is a plugin based server architecture.</description>
   <url>http://xbean.org</url>
+
   <issueManagement>
     <system>jira</system>
     <url>http://issues.apache.org/jira/browse/XBEAN</url>
   </issueManagement>
+
   <ciManagement>
     <system>continuum</system>
     <notifiers>
@@ -26,6 +30,7 @@
       </notifier>
     </notifiers>
   </ciManagement>
+
   <mailingLists>
     <mailingList>
       <name>xbean developers</name>
@@ -43,6 +48,7 @@
       <unsubscribe>mailto:scm-unsubscribe@xbean.org</unsubscribe>
     </mailingList>
   </mailingLists>
+
   <developers>
     <developer>
       <id>chirino</id>
@@ -103,11 +109,13 @@
       <timezone>+1</timezone>
     </developer>
   </developers>
+
   <scm>
     <connection>scm:svn:http://svn.apache.org/repos/asf/geronimo/xbean/tags/xbean-2.3</connection>
     <developerConnection>scm:svn:https://${maven.username}@svn.apache.org/repos/asf/geronimo/xbean/tags/xbean-2.3</developerConnection>
     <url>http://svn.apache.org/repos/asf/geronimo/xbean/tags/xbean-2.3</url>
   </scm>
+
   <build>
     <extensions>
       <extension>
@@ -142,8 +150,10 @@
       </plugin>
     </plugins>
   </build>
+
   <modules>
     <module>xbean-classpath</module>
+    <module>xbean-jmx</module>
     <module>xbean-kernel</module>
     <module>xbean-osgi</module>
     <module>xbean-reflect</module>
@@ -153,6 +163,7 @@
     <module>xbean-tiger</module>
     <module>maven-xbean-plugin</module>
   </modules>
+
   <dependencies>
     <dependency>
       <groupId>junit</groupId>
@@ -161,6 +172,7 @@
       <scope>test</scope>
     </dependency>
   </dependencies>
+
   <reporting>
     <plugins>
       <plugin>
@@ -176,6 +188,7 @@
       </plugin>
     </plugins>
   </reporting>
+
   <dependencyManagement>
     <dependencies>
       <dependency>
@@ -209,9 +222,9 @@
         <version>1.0-jsr-03</version>
       </dependency>
       <dependency>
-        <groupId>org.livetribe</groupId>
-        <artifactId>livetribe-jmx</artifactId>
-        <version>1.0-SNAPSHOT</version>
+        <groupId>lingo</groupId>
+        <artifactId>sca-api</artifactId>
+        <version>SNAPSHOT</version>
       </dependency>
       <dependency>
         <groupId>mx4j</groupId>
@@ -224,9 +237,14 @@
         <version>3.1.1</version>
       </dependency>
       <dependency>
-        <groupId>qdox</groupId>
-        <artifactId>qdox</artifactId>
-        <version>1.5</version>
+        <groupId>org.livetribe</groupId>
+        <artifactId>livetribe-jmx</artifactId>
+        <version>1.0-SNAPSHOT</version>
+      </dependency>
+      <dependency>
+        <groupId>org.snmp4j</groupId>
+        <artifactId>snmp4j</artifactId>
+        <version>1.7.1</version>
       </dependency>
       <dependency>
         <groupId>org.springframework</groupId>
@@ -234,11 +252,6 @@
         <version>1.2.4</version>
       </dependency>
       <dependency>
-        <groupId>stax</groupId>
-        <artifactId>stax-api</artifactId>
-        <version>1.0</version>
-      </dependency>
-      <dependency>
         <groupId>org.apache.xbean</groupId>
         <artifactId>xbean-kernel</artifactId>
         <version>${version}</version>
@@ -249,16 +262,23 @@
         <version>${version}</version>
       </dependency>
       <dependency>
-        <groupId>lingo</groupId>
-        <artifactId>sca-api</artifactId>
-        <version>SNAPSHOT</version>
+        <groupId>qdox</groupId>
+        <artifactId>qdox</artifactId>
+        <version>1.5</version>
+      </dependency>
+      <dependency>
+        <groupId>stax</groupId>
+        <artifactId>stax-api</artifactId>
+        <version>1.0</version>
       </dependency>
     </dependencies>
   </dependencyManagement>
+
   <distributionManagement>
     <site>
       <id>xbean-website</id>
       <url>scpexe://xbean.org/home/projects/xbean/public_html/maven</url>
     </site>
   </distributionManagement>
+
 </project>

Modified: geronimo/xbean/trunk/xbean-jmx/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-jmx/pom.xml?rev=408108&r1=408107&r2=408108&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-jmx/pom.xml (original)
+++ geronimo/xbean/trunk/xbean-jmx/pom.xml Sat May 20 16:27:07 2006
@@ -23,7 +23,7 @@
   <parent>
     <groupId>org.apache.xbean</groupId>
     <artifactId>xbean</artifactId>
-    <version>2.3-SNAPSHOT</version>
+    <version>2.4-SNAPSHOT</version>
   </parent>
 
   <artifactId>xbean-jmx</artifactId>
@@ -34,7 +34,7 @@
       <plugin>
         <groupId>org.apache.xbean</groupId>
         <artifactId>maven-xbean-plugin</artifactId>
-        <version>${pom.version}</version>
+        <version>2.3</version>
         <executions>
           <execution>
             <goals>
@@ -73,6 +73,10 @@
     <dependency>
       <groupId>org.apache.xbean</groupId>
       <artifactId>xbean-spring</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.snmp4j</groupId>
+      <artifactId>snmp4j</artifactId>
     </dependency>
   </dependencies>
   

Added: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/jmx/Heartbeat.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/jmx/Heartbeat.java?rev=408108&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/jmx/Heartbeat.java (added)
+++ geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/jmx/Heartbeat.java Sat May
20 16:27:07 2006
@@ -0,0 +1,107 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.jmx;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectInstance;
+import javax.management.ObjectName;
+import java.util.Date;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * Creates JMX heartbeat notifications for the specified timer and at the
+ * specified interval.
+ *
+ * @version $Rev$ $Date$
+ */
+public class Heartbeat
+{
+    private static final Log log = LogFactory.getLog(Heartbeat.class);
+
+    public static final String HEARTBEAT_TYPE = "heartbeat";
+    public static final String HEARTBEAT_MESSAGE = "heartbeat message";
+
+    private long interval = 10000;
+    private ObjectName timerName;
+    private MBeanServer server;
+    private Integer timerId;
+
+    public Heartbeat(MBeanServer server, ObjectName timerName, long interval)
+    {
+        this.interval = interval;
+        this.timerName = timerName;
+        this.server = server;
+    }
+
+    public void start()
+    {
+        Set lBeans = server.queryMBeans(timerName, null);
+
+        if (lBeans.isEmpty())
+        {
+            throw new RuntimeException("Cannot find Timer MBean!");
+        }
+
+        ObjectInstance lTimer = (ObjectInstance) lBeans.iterator().next();
+
+        try
+        {
+            log.debug("Adding heartbeat notification.");
+            timerId = (Integer) server.invoke(lTimer.getObjectName(),
+                                              "addNotification",
+                                              new Object[]{
+                                                      HEARTBEAT_TYPE,
+                                                      HEARTBEAT_MESSAGE,
+                                                      null, // No user object
+                                                      new Date(), // start now
+                                                      new Long(interval), // with this interval
+                                                      new Long(0) // forever
+                                              },
+                                              new String[]{
+                                                      String.class.getName(),
+                                                      String.class.getName(),
+                                                      Object.class.getName(),
+                                                      Date.class.getName(),
+                                                      Long.TYPE.getName(),
+                                                      Long.TYPE.getName()
+                                              });
+        }
+        catch (Exception e)
+        {
+            log.error("Could not register Timer notification!", e);
+        }
+    }
+
+    public void stop()
+    {
+        try
+        {
+            server.invoke(timerName,
+                          "removeNotification",
+                          new Object[]{timerId},
+                          new String[]{Integer.class.getName()});
+        }
+        catch (Exception e)
+        {
+            log.error("Could not remove Timer notification!", e);
+        }
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/jmx/Heartbeat.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/jmx/Heartbeat.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpBinding.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpBinding.java?rev=408108&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpBinding.java (added)
+++ geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpBinding.java Sat
May 20 16:27:07 2006
@@ -0,0 +1,62 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.snmp;
+
+/**
+ * Specifies a binding between a JMX notification and an SNMP message.
+ *
+ * @version $Rev$ $Date$
+ */
+public class SnmpBinding
+{
+    private String oid;
+    private String payload;
+    private String type;
+
+    /**
+     * The Type of Notification that this VarBinding applies to.
+     */
+    public String getType()
+    {
+        return type;
+    }
+
+    public void setType(String type)
+    {
+        this.type = type;
+    }
+
+    public String getOid()
+    {
+        return oid;
+    }
+
+    public void setOid(String oid)
+    {
+        this.oid = oid;
+    }
+
+    public String getPayload()
+    {
+        return payload;
+    }
+
+    public void setPayload(String payload)
+    {
+        this.payload = payload;
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpBinding.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpBinding.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpEmitter.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpEmitter.java?rev=408108&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpEmitter.java (added)
+++ geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpEmitter.java Sat
May 20 16:27:07 2006
@@ -0,0 +1,274 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.snmp;
+
+import javax.management.Notification;
+import javax.management.NotificationListener;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.snmp4j.CommunityTarget;
+import org.snmp4j.PDU;
+import org.snmp4j.Session;
+import org.snmp4j.Snmp;
+import org.snmp4j.TransportMapping;
+import org.snmp4j.event.ResponseEvent;
+import org.snmp4j.event.ResponseListener;
+import org.snmp4j.mp.SnmpConstants;
+import org.snmp4j.smi.Address;
+import org.snmp4j.smi.Counter64;
+import org.snmp4j.smi.GenericAddress;
+import org.snmp4j.smi.Null;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.OctetString;
+import org.snmp4j.smi.Variable;
+import org.snmp4j.smi.VariableBinding;
+import org.snmp4j.transport.DefaultUdpTransportMapping;
+
+
+/**
+ * Listens for JMX notifications and then emits them as SNMP traps.
+ * The class is configured with the SnmpServer and VarBinding POJOs.
+ * <pre>
+ * // Create an SnmpServer which describes where the messages are going to go
+ * SnmpServer ss = new SnmpServer();
+ * ss.setHost("127.0.0.1");
+ * ss.setPort(162);
+ * <p/>
+ * // Create a binding from a notification to the SNMP trap message
+ * VarBinding binding = new VarBinding();
+ * binding.setOid("1.3.6.1.2.1.1.1");
+ * binding.setPayload(SnmpEmitter.PAYLOAD_NOTIFICATION_MESSAGE);
+ * ss.getVarBindings().add(binding);
+ * <p/>
+ * // Create the SNMP emitter
+ * SnmpEmitter snmpEmitter = new SnmpEmitter();
+ * snmpEmitter.getServers().add(ss);
+ * snmpEmitter.start();
+ * <p/>
+ * // Register the emitter as a JMX listener
+ * MBeanServer server = ....;
+ * server.addNotificationListener(objectName,
+ *                                snmpEmitter,
+ *                                null, // No filter
+ *                                null // No object handback necessary);
+ * </pre>
+ * VarBindings specify what data to send to the SNMP server. The OID is the
+ * object ID that identifies the message on the SNMP server. The payload is
+ * what you desire to send to the server. It can be one of the following items:
+ * <ol>
+ * <li>PAYLOAD_NOTIFICATION_MESSAGE: The notification message.</li>
+ * <li>PAYLOAD_NOTIFICATION_TIMESTAMP: The notification timestamp.</li>
+ * <li>PAYLOAD_NOTIFICATION_TYPE: The notification type.</li>
+ * <li>PAYLOAD_NOTIFICATION_SEQUENCE: The notification sequence number.
+ * <li>UserData: If the payload is not one of the above, the SnmpEmitter w</li>ill
+ * see if the Notification's UserData is a Map. If so it will use the payload
+ * as the key to lookup data from the Map and send the result. Otherwise, Null
+ * will be sent.
+ * </li>
+ * </ol>
+ *
+ * @version $Rev$ $Date$
+ */
+public class SnmpEmitter implements NotificationListener
+{
+
+    private static final Log log = LogFactory.getLog(SnmpEmitter.class);
+
+    public static final String PAYLOAD_NOTIFICATION_MESSAGE = "notification-message";
+    public static final String PAYLOAD_NOTIFICATION_TIMESTAMP = "notification-date";
+    public static final String PAYLOAD_NOTIFICATION_TYPE = "notification-type";
+    public static final String PAYLOAD_NOTIFICATION_SEQUENCE = "notification-sequence";
+
+    private Set servers = new HashSet();
+
+    private Map sessions = new HashMap();
+
+    public Set getServers()
+    {
+        return servers;
+    }
+
+    public void setServers(Set servers)
+    {
+        this.servers = servers;
+    }
+
+    public void start() throws Exception
+    {
+        log.info("Starting SnmpEmitter");
+        for (Iterator itr = servers.iterator(); itr.hasNext();)
+        {
+            SnmpServer server = (SnmpServer) itr.next();
+
+            Snmp sess = createSession(server);
+            sessions.put(server, sess);
+        }
+    }
+
+    public void stop()
+    {
+        for (Iterator itr = sessions.values().iterator(); itr.hasNext();)
+        {
+            Session session = (Session) itr.next();
+            try
+            {
+                session.close();
+            }
+            catch (IOException e)
+            {
+                log.error("Could not close session.", e);
+            }
+            itr.remove();
+        }
+    }
+
+    public void handleNotification(Notification n, Object o)
+    {
+        log.debug("Received JMX notification");
+        for (Iterator itr = servers.iterator(); itr.hasNext();)
+        {
+            SnmpServer server = (SnmpServer) itr.next();
+            Snmp session = getSession(server);
+            Address targetAddress = GenericAddress.parse("udp:" + server.getHost() + "/"
+ server.getPort());
+
+            // setting up target
+            CommunityTarget target = new CommunityTarget();
+            target.setCommunity(new OctetString("public"));
+            target.setAddress(targetAddress);
+            target.setRetries(server.getRetries());
+            target.setTimeout(server.getTimeout());
+            target.setVersion(SnmpConstants.version2c);
+
+            // creating PDU
+            PDU pdu = new PDU();
+
+            addVarBindings(n, server, pdu);
+
+            pdu.setType(PDU.TRAP);
+
+            // sending request
+            ResponseListener listener = new ResponseListener()
+            {
+                public void onResponse(ResponseEvent event)
+                {
+                    // Always cancel async request when response has been received
+                    // otherwise a memory leak is created! Not canceling a request
+                    // immediately can be useful when sending a request to a broadcast
+                    // address.
+                    ((Snmp) event.getSource()).cancel(event.getRequest(), this);
+                    log.debug("Received response PDU is: " + event.getResponse());
+                }
+            };
+            try
+            {
+                log.debug("Sending SNMP trap to " + targetAddress);
+                session.send(pdu, target, null, listener);
+            }
+            catch (IOException e)
+            {
+                log.error("Could not send SNMP trap!", e);
+            }
+        }
+    }
+
+    protected void addVarBindings(Notification n, SnmpServer server, PDU pdu)
+    {
+        for (Iterator itr = server.getVarBindings().iterator(); itr.hasNext();)
+        {
+            addVarBinding(n, (SnmpBinding) itr.next(), pdu);
+        }
+    }
+
+    protected void addVarBinding(Notification n, SnmpBinding binding, PDU pdu)
+    {
+        String type = binding.getType();
+        // See if this VarBinding applies to this notification
+        if (type != null && !type.equals(n.getType())) return;
+
+        pdu.add(new VariableBinding(new OID(binding.getOid()), getVariable(n, binding)));
+    }
+
+    protected Variable getVariable(Notification n, SnmpBinding binding)
+    {
+        String payload = binding.getPayload();
+        if (payload.equals(PAYLOAD_NOTIFICATION_TIMESTAMP))
+        {
+            return new Counter64(n.getTimeStamp());
+        }
+        else if (payload.equals(PAYLOAD_NOTIFICATION_MESSAGE))
+        {
+            return new OctetString(n.getMessage());
+        }
+        else if (payload.equals(PAYLOAD_NOTIFICATION_SEQUENCE))
+        {
+            return new Counter64(n.getSequenceNumber());
+        }
+        else if (payload.equals(PAYLOAD_NOTIFICATION_TYPE))
+        {
+            return new OctetString(n.getType());
+        }
+        else
+        {
+            // This isn't a payload that we recognize, so attempt to pull it
+            // out of a mMap in the UserData.
+            Object data = n.getUserData();
+            if (data instanceof Map)
+            {
+                Object pdata = ((Map) data).get(payload);
+                if (pdata != null)
+                    return new OctetString(pdata.toString());
+                else
+                    return new Null();
+            }
+            else if (data instanceof CompositeDataSupport)
+            {
+                CompositeData cdata = (CompositeData) data;
+                if (cdata.containsKey(payload))
+                    return new OctetString(cdata.get(payload).toString());
+                else
+                    return new Null();
+            }
+            else
+            {
+                return new Null();
+            }
+        }
+    }
+
+    private Snmp getSession(SnmpServer server)
+    {
+        return (Snmp) sessions.get(server);
+    }
+
+    private Snmp createSession(SnmpServer server) throws IOException
+    {
+        TransportMapping transport = new DefaultUdpTransportMapping();
+        Snmp snmp = new Snmp(transport);
+        transport.listen();
+
+        return snmp;
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpEmitter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpEmitter.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpServer.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpServer.java?rev=408108&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpServer.java (added)
+++ geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpServer.java Sat
May 20 16:27:07 2006
@@ -0,0 +1,83 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.snmp;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class SnmpServer
+{
+    private String host;
+    private int port;
+    private int timeout = 1000;
+    private int retries = 0;
+    private List varBindings = new ArrayList();
+
+    public int getRetries()
+    {
+        return retries;
+    }
+
+    public void setRetries(int retries)
+    {
+        this.retries = retries;
+    }
+
+    public List getVarBindings()
+    {
+        return varBindings;
+    }
+
+    public void setVarBindings(List varBindings)
+    {
+        this.varBindings = varBindings;
+    }
+
+    public String getHost()
+    {
+        return host;
+    }
+
+    public void setHost(String host)
+    {
+        this.host = host;
+    }
+
+    public int getPort()
+    {
+        return port;
+    }
+
+    public void setPort(int port)
+    {
+        this.port = port;
+    }
+
+    public int getTimeout()
+    {
+        return timeout;
+    }
+
+    public void setTimeout(int timeout)
+    {
+        this.timeout = timeout;
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpServer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-jmx/src/main/java/org/apache/xbean/snmp/SnmpServer.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/AbstractJMXSNMPTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/AbstractJMXSNMPTest.java?rev=408108&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/AbstractJMXSNMPTest.java
(added)
+++ geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/AbstractJMXSNMPTest.java
Sat May 20 16:27:07 2006
@@ -0,0 +1,90 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.snmp;
+
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.ObjectInstance;
+import javax.management.ObjectName;
+
+import junit.framework.TestCase;
+import org.snmp4j.CommandResponder;
+import org.snmp4j.CommandResponderEvent;
+import org.snmp4j.PDU;
+import org.snmp4j.Snmp;
+import org.snmp4j.TransportMapping;
+import org.snmp4j.smi.Address;
+import org.snmp4j.smi.GenericAddress;
+import org.snmp4j.smi.UdpAddress;
+import org.snmp4j.transport.DefaultUdpTransportMapping;
+
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class AbstractJMXSNMPTest extends TestCase
+{
+    protected final int SNMP_TEST_PORT = 1620;
+    protected static int count = 0;
+    private Snmp snmp;
+
+    public void setUp() throws Exception
+    {
+        createServer();
+        createSNMPServer();
+    }
+
+    protected void createServer() throws Exception
+    {
+        MBeanServer server = MBeanServerFactory.createMBeanServer();
+
+        ObjectName timerName = new ObjectName("DefaultDomain", "service", "timer");
+        ObjectInstance lTimer = server.createMBean("javax.management.timer.Timer", timerName);
+
+        server.invoke(lTimer.getObjectName(), "start", new Object[]{}, new String[]{});
+    }
+
+    public void createSNMPServer() throws Exception
+    {
+        Address targetAddress = GenericAddress.parse("udp:0.0.0.0/" + SNMP_TEST_PORT);
+        TransportMapping transport = new DefaultUdpTransportMapping((UdpAddress) targetAddress);
+        snmp = new Snmp(transport);
+
+        snmp.listen();
+
+        CommandResponder trapPrinter = new CommandResponder()
+        {
+            public synchronized void processPdu(CommandResponderEvent e)
+            {
+                PDU command = e.getPDU();
+                if (command != null)
+                {
+                    count++;
+                    System.out.println(command.toString());
+                }
+            }
+        };
+        snmp.addCommandResponder(trapPrinter);
+    }
+
+    public void tearDown() throws Exception
+    {
+        super.tearDown();
+
+        snmp.close();
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/AbstractJMXSNMPTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/AbstractJMXSNMPTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/HeartbeatTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/HeartbeatTest.java?rev=408108&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/HeartbeatTest.java
(added)
+++ geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/HeartbeatTest.java
Sat May 20 16:27:07 2006
@@ -0,0 +1,101 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.snmp;
+
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.NotificationEmitter;
+import javax.management.ObjectInstance;
+import javax.management.ObjectName;
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryMXBean;
+import java.lang.management.MemoryPoolMXBean;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.xbean.jmx.Heartbeat;
+
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class HeartbeatTest extends AbstractJMXSNMPTest
+{
+    public void testHB() throws Exception
+    {
+        // Create SNMP stuff
+        SnmpEmitter em = new SnmpEmitter();
+
+        SnmpServer ss = new SnmpServer();
+        ss.setHost("127.0.0.1");
+        ss.setPort(SNMP_TEST_PORT);
+
+        ss.setVarBindings(new ArrayList());
+
+        SnmpBinding binding = new SnmpBinding();
+        binding.setOid("1.3.6.1.2.1.1.1");
+        binding.setPayload(SnmpEmitter.PAYLOAD_NOTIFICATION_MESSAGE);
+        ss.getVarBindings().add(binding);
+
+        binding = new SnmpBinding();
+        binding.setOid("1.3.6.1.2.1.1.2");
+        binding.setPayload(SnmpEmitter.PAYLOAD_NOTIFICATION_TYPE);
+        ss.getVarBindings().add(binding);
+
+        binding = new SnmpBinding();
+        binding.setOid("1.3.6.1.2.1.1.3");
+        binding.setPayload("foo");
+        binding.setType("asdf");
+        ss.getVarBindings().add(binding);
+
+
+        Set servers = new HashSet();
+        servers.add(ss);
+        em.setServers(servers);
+
+        em.start();
+
+        MBeanServer lServer = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
+        Set lBeans = lServer.queryMBeans(new ObjectName("DefaultDomain", "service", "timer"),
null);
+        ObjectInstance lTimer = (ObjectInstance) lBeans.iterator().next();
+
+        lServer.addNotificationListener(lTimer.getObjectName(), em,
+                                        // No filter
+                                        null,
+                                        // No object handback necessary
+                                        null);
+
+        MemoryPoolMXBean pool = (MemoryPoolMXBean) ManagementFactory.getMemoryPoolMXBeans().iterator().next();
+
+        MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
+
+        NotificationEmitter memEm = (NotificationEmitter) memBean;
+        memEm.addNotificationListener(em, null, null);
+
+        pool.setUsageThreshold(1000);
+        Heartbeat hb = new Heartbeat(lServer, new ObjectName("DefaultDomain", "service",
"timer"), 1000);
+        hb.start();
+
+        Thread.sleep(1000);
+
+        if (count == 0) fail("Didn't receive any SNMP traps!");
+
+        hb.stop();
+    }
+
+}
\ No newline at end of file

Propchange: geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/HeartbeatTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/HeartbeatTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/MemoryUsageExampleTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/MemoryUsageExampleTest.java?rev=408108&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/MemoryUsageExampleTest.java
(added)
+++ geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/MemoryUsageExampleTest.java
Sat May 20 16:27:07 2006
@@ -0,0 +1,76 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.snmp;
+
+import javax.management.NotificationEmitter;
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryMXBean;
+import java.lang.management.MemoryPoolMXBean;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Set;
+
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class MemoryUsageExampleTest extends AbstractJMXSNMPTest
+{
+    public void testHB() throws Exception
+    {
+        // Create SNMP stuff
+        SnmpEmitter em = new SnmpEmitter();
+
+        SnmpServer ss = new SnmpServer();
+        ss.setHost("127.0.0.1");
+        ss.setPort(SNMP_TEST_PORT);
+
+        ss.setVarBindings(new ArrayList());
+
+        /**
+         *  Bind the JMX notification type "java.management.memory.threshold.exceeded"
+         * to the specified OID. The payload of the snmp message will be the usage
+         * attribute.
+         */
+        SnmpBinding binding = new SnmpBinding();
+        binding.setOid("1.3.6.1.2.1.1.3");
+        binding.setPayload("usage");
+        binding.setType("java.management.memory.threshold.exceeded");
+        ss.getVarBindings().add(binding);
+
+        Set servers = new HashSet();
+        servers.add(ss);
+        em.setServers(servers);
+
+        em.start();
+
+        MemoryPoolMXBean pool = (MemoryPoolMXBean) ManagementFactory.getMemoryPoolMXBeans().iterator().next();
+
+        MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
+
+        // Add our SNMP emitter as a notification listener
+        NotificationEmitter memEm = (NotificationEmitter) memBean;
+        memEm.addNotificationListener(em, null, null);
+
+        // Set the memory usage threshold real low to trigger an event
+        pool.setUsageThreshold(1000);
+
+        Thread.sleep(1000);
+
+        if (count == 0) fail("Didn't receive any SNMP traps!");
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/MemoryUsageExampleTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-jmx/src/test/java/org/apache/xbean/snmp/MemoryUsageExampleTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message