├── .gitignore ├── README.md ├── pom.xml └── src ├── main └── java │ └── org │ └── vafer │ └── jmx2snmp │ ├── jmx │ ├── JmxAttribute.java │ ├── JmxIndex.java │ ├── JmxMib.java │ ├── JmxServer.java │ ├── RMIClientSocketFactoryImpl.java │ └── RMIServerSocketFactoryImpl.java │ └── snmp │ └── SnmpBridge.java └── test ├── java └── org │ └── vafer │ └── jmx2snmp │ ├── beans │ ├── TestBean.java │ └── TestRow.java │ ├── jmx │ ├── JmxMibTestCase.java │ └── JmxServerTestCase.java │ ├── jmxutils │ ├── JmxutilsTestCase.java │ └── beans │ │ └── TestBeanImpl.java │ ├── snmp │ └── SnmpBridgeTestCase.java │ └── spring │ ├── SpringTestCase.java │ └── beans │ └── TestBeanImpl.java └── resources └── org └── vafer └── jmx2snmp ├── mapping.properties └── spring └── beans.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .classpath 3 | .project 4 | .fatjar 5 | target 6 | eclipse 7 | old 8 | bin 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bridge 2 | 3 | // export JMX beans either through Spring, Guice or jmxutils 4 | final MBeanExporter exporter = new MBeanExporter(ManagementFactory.getPlatformMBeanServer()); 5 | exporter.export("bean:name=test1", new TestBeanImpl()); 6 | 7 | // fire up JMX 8 | final JmxServer jmxServer = new JmxServer(InetAddress.getByName("localhost")); 9 | jmxServer.start(); 10 | 11 | // load mib mapping 12 | final URL url = JmxutilsTestCase.class.getResource("/org/vafer/jmx2snmp/mapping.properties"); 13 | final JmxMib jmxMib = new JmxMib(); 14 | jmxMib.load(new FileReader(url.getFile())); 15 | 16 | // build MBean index 17 | final JmxIndex = new JmxIndex(); 18 | 19 | // fire up SNMP bridge 20 | final SnmpBridge snmpBridge = new SnmpBridge(InetAddress.getByName("localhost"), 1161, jmxIndex, jmxMib); 21 | snmpBridge.start(); 22 | 23 | // show inconsistencies in the mapping (if there are) 24 | snmpBridge.report(); 25 | 26 | # Mapping 27 | 28 | 1.3.6.1.4.1.27305 = 29 | 1.3.6.1.4.1.27305.12 = test1 30 | 1.3.6.1.4.1.27305.12.1 = AnotherInt 31 | 1.3.6.1.4.1.27305.12.2 = Blue 32 | 1.3.6.1.4.1.27305.12.3 = Boolean 33 | 1.3.6.1.4.1.27305.12.4 = BooleanArray 34 | 1.3.6.1.4.1.27305.12.5 = IntArray 35 | 1.3.6.1.4.1.27305.12.6 = IntegerArray 36 | 1.3.6.1.4.1.27305.12.7 = Long 37 | 1.3.6.1.4.1.27305.12.8 = SomeInt 38 | 1.3.6.1.4.1.27305.12.9 = SomeObjects 39 | 1.3.6.1.4.1.27305.12.10 = SomethingMapped 40 | 1.3.6.1.4.1.27305.12.11 = SomethingUnique 41 | 1.3.6.1.4.1.27305.12.12 = String 42 | 1.3.6.1.4.1.27305.12.13 = StringArray 43 | 1.3.6.1.4.1.27305.12.14 = StringTable 44 | 1.3.6.1.4.1.27305.12.15 = TestRowArray 45 | 46 | # Test 47 | 48 | snmpwalk -On -c public -v 1 localhost:1161 1.3.6.1.4.1.27305.12 49 | snmpget -On -c public -v 1 localhost:1161 1.3.6.1.4.1.27305.12.8 50 | 51 | # Integration with snmpd 52 | 53 | proxy -v 1 -c public localhost:1161 .1.3.6.1.4.1.27305 54 | 55 | # License 56 | 57 | Licensed under the Apache License, Version 2.0 (the "License") 58 | You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 59 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | org.vafer 7 | maven-pom 8 | 1.0 9 | 10 | 11 | org.vafer 12 | jmx2snmp 13 | jmx2snmp 14 | 1.0-SNAPSHOT 15 | 16 | 17 | http://github.com/tcurdt/jmx2snmp 18 | 19 | 20 | 21 | tcurdt 22 | Torsten Curdt 23 | tcurdt at vafer.org 24 | +1 25 | 26 | 27 | 28 | 29 | 30 | Apache License 2 31 | http://www.apache.org/licenses/LICENSE-2.0.txt 32 | 33 | 34 | 35 | 36 | scm:git:git://github.com:tcurdt/jmx2snmp.git 37 | scm:git:git://github.com:tcurdt/jmx2snmp.git 38 | http://github.com/tcurdt/jmx2snmp/tree/master 39 | 40 | 41 | 42 | 43 | org.snmp4j 44 | snmp4j-agent 45 | 1.3.1 46 | 47 | 48 | org.snmp4j 49 | snmp4j 50 | 1.10.1 51 | 52 | 53 | org.weakref 54 | jmxutils 55 | 1.3 56 | 57 | 58 | 59 | org.springframework 60 | spring 61 | 2.5.6 62 | test 63 | 64 | 65 | junit 66 | junit 67 | 4.5 68 | test 69 | 70 | 71 | 72 | 73 | 74 | 75 | org.apache.maven.plugins 76 | maven-compiler-plugin 77 | 78 | 1.5 79 | 1.5 80 | UTF-8 81 | 82 | 83 | 84 | org.apache.maven.plugins 85 | maven-surefire-plugin 86 | 87 | never 88 | 89 | **/*TestCase.java 90 | 91 | 92 | **/Abstract* 93 | 94 | true 95 | false 96 | 97 | 98 | 99 | org.apache.maven.plugins 100 | maven-source-plugin 101 | 2.1 102 | 103 | true 104 | 105 | 106 | 107 | create-source-jar 108 | 109 | jar-no-fork 110 | 111 | 112 | 113 | 114 | 115 | org.apache.maven.plugins 116 | maven-shade-plugin 117 | 1.4 118 | 119 | 120 | package 121 | 122 | shade 123 | 124 | 125 | 126 | 127 | org.weakref:jmxutils 128 | org.snmp4j:snmp4j-agent 129 | org.snmp4j:snmp4j 130 | 131 | 132 | 133 | 134 | org.snmp4j 135 | org.vafer.jmx2snmp.shaded.snmp4j 136 | 137 | 138 | org.weakref.jmx 139 | org.vafer.jmx2snmp.shaded.jmxutils 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /src/main/java/org/vafer/jmx2snmp/jmx/JmxAttribute.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmx; 2 | 3 | import javax.management.JMException; 4 | import javax.management.MBeanServer; 5 | import javax.management.ObjectName; 6 | 7 | /** 8 | * Represents a property exposed through the MBeanServer 9 | * 10 | * @threadsafe yes 11 | */ 12 | public final class JmxAttribute { 13 | 14 | private final String attributeName; 15 | private final String type; 16 | private final ObjectName objectName; 17 | private final MBeanServer mbeanServer; 18 | private final String path; 19 | 20 | public JmxAttribute(String pAttributeName, String pType, ObjectName pObjectName, MBeanServer pMbeanServer) { 21 | attributeName = pAttributeName; 22 | type = pType; 23 | objectName = pObjectName; 24 | mbeanServer = pMbeanServer; 25 | 26 | String beanName = pObjectName.getKeyProperty("name"); 27 | if (beanName == null) { 28 | beanName = pObjectName.getKeyProperty("type"); 29 | } 30 | path = "." + beanName + "." + attributeName; 31 | } 32 | 33 | public String getPath() { 34 | return path; 35 | } 36 | 37 | public String getName() { 38 | return attributeName; 39 | } 40 | 41 | public String getType() { 42 | return type; 43 | } 44 | 45 | public Object getValue() throws JMException { 46 | final Object attributeValue = mbeanServer.getAttribute(objectName, attributeName); 47 | return attributeValue; 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/java/org/vafer/jmx2snmp/jmx/JmxIndex.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmx; 2 | 3 | import java.lang.management.ManagementFactory; 4 | import java.util.Collection; 5 | import java.util.Collections; 6 | import java.util.HashMap; 7 | import java.util.Iterator; 8 | import java.util.Map; 9 | import java.util.Set; 10 | import java.util.concurrent.atomic.AtomicReference; 11 | 12 | import javax.management.MBeanAttributeInfo; 13 | import javax.management.MBeanInfo; 14 | import javax.management.MBeanServer; 15 | import javax.management.ObjectInstance; 16 | import javax.management.ObjectName; 17 | 18 | /** 19 | * Represents an index of all bean paths of the MBeanServer to the individual 20 | * JMX attributes. Use the expression to formulate a javax.management.QueryExp 21 | * for MBean selection. 22 | * 23 | * @threadsafe yes 24 | */ 25 | public final class JmxIndex { 26 | 27 | private final AtomicReference> attributesRef = new AtomicReference>(Collections.unmodifiableMap(new HashMap())); 28 | private final MBeanServer mbeanServer; 29 | private final String expression; 30 | 31 | public JmxIndex() throws Exception { 32 | this(ManagementFactory.getPlatformMBeanServer(), "bean:*"); 33 | } 34 | 35 | public JmxIndex(MBeanServer pMbeanServer, String pExpression) throws Exception { 36 | mbeanServer = pMbeanServer; 37 | expression = pExpression; 38 | 39 | update(); 40 | } 41 | 42 | public void update() throws Exception { 43 | 44 | final HashMap newAttributes = new HashMap(); 45 | final Collection mbeans = mbeanServer.queryMBeans(new ObjectName(expression), null); 46 | 47 | for (Iterator it = mbeans.iterator(); it.hasNext();) { 48 | final ObjectInstance mbean = it.next(); 49 | 50 | final MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(mbean.getObjectName()); 51 | 52 | final MBeanAttributeInfo[] attributes = mbeanInfo.getAttributes(); 53 | for (final MBeanAttributeInfo attribute : attributes) { 54 | 55 | if (attribute.isReadable()) { 56 | final String attributeName = attribute.getName(); 57 | 58 | final JmxAttribute jmxAttribute = new JmxAttribute(attributeName, attribute.getType(), mbean.getObjectName(), mbeanServer); 59 | 60 | newAttributes.put(jmxAttribute.getPath(), jmxAttribute); 61 | } 62 | } 63 | } 64 | 65 | attributesRef.set(Collections.unmodifiableMap(newAttributes)); 66 | } 67 | 68 | public Set getAttributePaths() { 69 | final Map mapping = attributesRef.get(); 70 | return Collections.unmodifiableSet(mapping.keySet()); 71 | } 72 | 73 | public JmxAttribute getAttributeAtPath(String pPath) { 74 | final Map attributes = attributesRef.get(); 75 | return attributes.get(pPath); 76 | } 77 | 78 | public String toString() { 79 | final StringBuilder sb = new StringBuilder(); 80 | sb.append(super.toString()); 81 | sb.append('{').append(attributesRef.get()).append('}'); 82 | return sb.toString(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/org/vafer/jmx2snmp/jmx/JmxMib.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmx; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.Reader; 6 | import java.util.Iterator; 7 | import java.util.SortedMap; 8 | import java.util.TreeMap; 9 | import java.util.concurrent.atomic.AtomicReference; 10 | 11 | /** 12 | * The JmxMib represents a walk-able SNMP OID tree. The tree is loaded and 13 | * constructed from a load(Reader). The expect format include a definition 14 | * for the root node as "" and all other nodes. 15 | * 16 | * 1.3.6.1.4.1.27305 = 17 | * 1.3.6.1.4.1.27305.12 = bean1 18 | * 1.3.6.1.4.1.27305.12.1 = AnotherInt 19 | * 1.3.6.1.4.1.27305.12.2 = SomeColor 20 | * 1.3.6.1.4.1.27305.12.3 = TheBoolean 21 | * 22 | * @threadsafe yes 23 | */ 24 | public final class JmxMib { 25 | 26 | private final AtomicReference root = new AtomicReference(new Node(null, 0, null)); 27 | 28 | public static class Bean { 29 | public boolean leaf; 30 | public String relativePath; 31 | public String absolutePath; 32 | }; 33 | 34 | private static class Node { 35 | 36 | public final Node parent; 37 | public final int idx; 38 | public String value; 39 | public final SortedMap childs = new TreeMap(); 40 | 41 | public Node(Node parent, int idx, String value) { 42 | this.parent = parent; 43 | this.idx = idx; 44 | this.value = value; 45 | } 46 | 47 | public String getOid() { 48 | if (parent == null) { 49 | return null; 50 | } 51 | 52 | final String parentOid = parent.getOid(); 53 | 54 | if (parentOid == null) { 55 | return "" + idx; 56 | } 57 | 58 | return parentOid + '.' + idx; 59 | } 60 | 61 | public String getPath() { 62 | if (parent == null) { 63 | return null; 64 | } 65 | 66 | final String parentPath = parent.getPath(); 67 | 68 | if (parentPath == null) { 69 | return value; 70 | } 71 | 72 | return parentPath + '.' + value; 73 | } 74 | 75 | public Node getNext() { 76 | Node leaf = this; 77 | while(leaf.childs.size() > 0) { 78 | leaf = leaf.childs.get(leaf.childs.firstKey()); 79 | } 80 | 81 | if (leaf != this) { 82 | return leaf; 83 | } 84 | 85 | Node n = leaf; 86 | while(n != null) { 87 | final Node p = n.parent; 88 | 89 | if (p == null) { 90 | return null; 91 | } 92 | 93 | final Node sibling = p.childAfter(n.idx); 94 | 95 | if (sibling != null) { 96 | if (sibling.childs.size() > 0) { 97 | return sibling.getNext(); 98 | } 99 | return sibling; 100 | } 101 | 102 | n = p; 103 | } 104 | 105 | return null; 106 | } 107 | 108 | public Node childAfter(int index) { 109 | 110 | Iterator iterator = childs.keySet().iterator(); 111 | while (iterator.hasNext()) { 112 | Integer key = iterator.next(); 113 | if (key > index) { 114 | return childs.get(key); 115 | } 116 | } 117 | 118 | return null; 119 | } 120 | 121 | public int getNodeCount() { 122 | int count = 1; 123 | for(Node node : childs.values()) { 124 | count += node.getNodeCount(); 125 | } 126 | return count; 127 | } 128 | } 129 | 130 | private Node createNode( Node root, String oid ) { 131 | final String[] indexes = oid.split("\\."); 132 | Node node = root; 133 | for (int i = 0; i < indexes.length; i++) { 134 | 135 | final Integer idx = Integer.parseInt(indexes[i]); 136 | 137 | Node n = node.childs.get(idx); 138 | if (n == null) { 139 | n = new Node(node, idx, null); 140 | node.childs.put(idx, n); 141 | } 142 | 143 | node = n; 144 | } 145 | 146 | return node; 147 | } 148 | 149 | private Node lookupNode( Node root, String oid ) { 150 | final String[] indexes = oid.split("\\."); 151 | Node node = root; 152 | for (int i = 0; i < indexes.length; i++) { 153 | 154 | final Integer idx = Integer.parseInt(indexes[i]); 155 | 156 | Node n = node.childs.get(idx); 157 | if (n == null) { 158 | return null; 159 | } 160 | 161 | node = n; 162 | } 163 | 164 | return node; 165 | } 166 | 167 | public synchronized void load(Reader pConfigReader) throws IOException { 168 | final BufferedReader br = new BufferedReader(pConfigReader); 169 | 170 | final Node newRoot = new Node(null, 0, null); 171 | 172 | while(true) { 173 | final String line = br.readLine(); 174 | 175 | if (line == null) { 176 | break; 177 | } 178 | 179 | final String[] tokens = line.split("="); 180 | final String key = tokens[0].trim(); 181 | final String value = tokens[1].trim(); 182 | 183 | final Node node = createNode(newRoot, key); 184 | node.value = value; 185 | } 186 | 187 | br.close(); 188 | 189 | root.set(newRoot); 190 | } 191 | 192 | public String getPathFromOid(String oid) { 193 | final Node node = lookupNode(root.get(), oid); 194 | 195 | if (node == null) { 196 | return null; 197 | } 198 | 199 | return node.getPath(); 200 | } 201 | 202 | public String getNextOidFromOid(String oid) { 203 | final Node node = lookupNode(root.get(), oid); 204 | 205 | if (node == null) { 206 | return null; 207 | } 208 | 209 | final Node nextNode = node.getNext(); 210 | 211 | if (nextNode == null) { 212 | return null; 213 | } 214 | 215 | return nextNode.getOid(); 216 | } 217 | 218 | public int getNodeCount() { 219 | return root.get().getNodeCount() - 1; 220 | } 221 | 222 | private void fillMapping(Node node, TreeMap map) { 223 | 224 | if (node.value != null) { 225 | Bean bean = new Bean(); 226 | bean.leaf = node.childs.size() == 0; 227 | bean.relativePath = node.value; 228 | bean.absolutePath = node.getPath(); 229 | map.put(node.getOid(), bean); 230 | } 231 | 232 | for(Node child : node.childs.values()) { 233 | fillMapping(child, map); 234 | } 235 | } 236 | 237 | public TreeMap getMapping() { 238 | 239 | final TreeMap map = new TreeMap(); 240 | 241 | final Node node = root.get(); 242 | 243 | fillMapping(node, map); 244 | 245 | return map; 246 | } 247 | 248 | } 249 | -------------------------------------------------------------------------------- /src/main/java/org/vafer/jmx2snmp/jmx/JmxServer.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmx; 2 | 3 | import java.lang.management.ManagementFactory; 4 | import java.net.InetAddress; 5 | import java.net.UnknownHostException; 6 | import java.rmi.registry.LocateRegistry; 7 | import java.rmi.registry.Registry; 8 | import java.rmi.server.RMIServerSocketFactory; 9 | import java.rmi.server.UnicastRemoteObject; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | import javax.management.remote.JMXServiceURL; 14 | import javax.management.remote.rmi.RMIConnectorServer; 15 | 16 | import sun.management.jmxremote.LocalRMIServerSocketFactory; 17 | 18 | /** 19 | * This JmxServer exposes the MBeans on a dedicated interface/port combination. 20 | * 21 | * Please note: when using a non-localhost address the connecting JMX client 22 | * needs to have the class RMIServerSocketFactoryImpl available in it's classpath. 23 | * 24 | * @threadsafe no 25 | */ 26 | public final class JmxServer { 27 | 28 | private final InetAddress address; 29 | private final int protocolPort; 30 | private final int namingPort; 31 | 32 | private RMIConnectorServer rmiServer; 33 | private String url; 34 | private Registry registry; 35 | 36 | public JmxServer(InetAddress pAddress, int pProtocolPort, int pNamingPort) { 37 | address = pAddress; 38 | protocolPort = pProtocolPort; 39 | namingPort = pNamingPort; 40 | } 41 | 42 | public JmxServer() throws UnknownHostException { 43 | this(InetAddress.getLocalHost()); 44 | } 45 | 46 | public JmxServer(InetAddress pAddress) { 47 | this(pAddress, 0, 5100); 48 | } 49 | 50 | public String getUrl() { 51 | return url; 52 | } 53 | 54 | public int getProtocolPort() { 55 | return protocolPort; 56 | } 57 | 58 | public int getNamingPort() { 59 | return namingPort; 60 | } 61 | 62 | public synchronized void start() throws Exception { 63 | if (rmiServer != null) { 64 | return; 65 | } 66 | 67 | final RMIServerSocketFactory serverFactory; 68 | 69 | if (address.isLoopbackAddress()) { 70 | serverFactory = new LocalRMIServerSocketFactory(); 71 | } else { 72 | serverFactory = new RMIServerSocketFactoryImpl(address); 73 | // Make sure to have the class RMIServerSocketFactoryImpl.class in your JMX client's classpath to connect. 74 | } 75 | 76 | registry = LocateRegistry.createRegistry(namingPort, null, serverFactory); 77 | 78 | final Map env = new HashMap(); 79 | env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, serverFactory); 80 | 81 | final StringBuffer serviceUrl = new StringBuffer(); 82 | serviceUrl.append("service:jmx:"); 83 | serviceUrl.append("rmi://").append(address.getHostAddress()).append(':').append(protocolPort).append("/jndi/"); 84 | serviceUrl.append("rmi://").append(address.getHostAddress()).append(':').append(namingPort).append("/connector"); 85 | url = serviceUrl.toString(); 86 | 87 | rmiServer = new RMIConnectorServer( 88 | new JMXServiceURL(url), 89 | env, 90 | ManagementFactory.getPlatformMBeanServer() 91 | ); 92 | 93 | rmiServer.start(); 94 | } 95 | 96 | 97 | public synchronized void stop() throws Exception { 98 | rmiServer.stop(); 99 | UnicastRemoteObject.unexportObject(registry, true); 100 | registry = null; 101 | rmiServer = null; 102 | url = null; 103 | } 104 | 105 | // public static void main(String[] args) throws Exception { 106 | // final JmxServer server = new JmxServer(InetAddress.getByName("localhost")); 107 | // server.start(); 108 | // 109 | // System.out.println("jconsole " + server.getUrl()); 110 | // 111 | // System.out.println("enter 'quit' to stop..."); 112 | // final Scanner sc = new Scanner(System.in); 113 | // while(!sc.nextLine().equals("quit")); 114 | // server.stop(); 115 | // } 116 | } 117 | -------------------------------------------------------------------------------- /src/main/java/org/vafer/jmx2snmp/jmx/RMIClientSocketFactoryImpl.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmx; 2 | 3 | import java.io.IOException; 4 | import java.io.Serializable; 5 | import java.net.Socket; 6 | import java.rmi.server.RMIClientSocketFactory; 7 | 8 | import javax.net.SocketFactory; 9 | 10 | public final class RMIClientSocketFactoryImpl implements RMIClientSocketFactory, Serializable { 11 | 12 | private static final long serialVersionUID = 1L; 13 | 14 | public Socket createSocket(final String pHost, final int pPort) throws IOException { 15 | return SocketFactory.getDefault().createSocket(pHost, pPort); 16 | } 17 | 18 | public boolean equals(Object obj) { 19 | if (obj == null) { 20 | return false; 21 | } 22 | if (obj == this) { 23 | return true; 24 | } 25 | 26 | return obj.getClass().equals(getClass()); 27 | } 28 | 29 | 30 | public int hashCode() { 31 | return RMIClientSocketFactoryImpl.class.hashCode(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/vafer/jmx2snmp/jmx/RMIServerSocketFactoryImpl.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmx; 2 | 3 | import java.io.IOException; 4 | import java.net.InetAddress; 5 | import java.net.ServerSocket; 6 | import java.rmi.server.RMIServerSocketFactory; 7 | 8 | import javax.net.ServerSocketFactory; 9 | 10 | public final class RMIServerSocketFactoryImpl implements RMIServerSocketFactory { 11 | 12 | private final InetAddress localAddress; 13 | 14 | public RMIServerSocketFactoryImpl( final InetAddress pAddress ) { 15 | localAddress = pAddress; 16 | } 17 | 18 | public ServerSocket createServerSocket(final int pPort) throws IOException { 19 | // Socket created on localAddress on pPort 20 | return ServerSocketFactory.getDefault().createServerSocket(pPort, 0, localAddress); 21 | } 22 | 23 | public boolean equals(Object obj) { 24 | if (obj == null) { 25 | return false; 26 | } 27 | if (obj == this) { 28 | return true; 29 | } 30 | 31 | return obj.getClass().equals(getClass()); 32 | } 33 | 34 | 35 | public int hashCode() { 36 | return RMIServerSocketFactoryImpl.class.hashCode(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/vafer/jmx2snmp/snmp/SnmpBridge.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.snmp; 2 | 3 | import java.net.InetAddress; 4 | import java.util.HashSet; 5 | import java.util.Map; 6 | import java.util.Set; 7 | 8 | import javax.management.JMException; 9 | 10 | import org.snmp4j.CommandResponder; 11 | import org.snmp4j.CommandResponderEvent; 12 | import org.snmp4j.PDU; 13 | import org.snmp4j.Snmp; 14 | import org.snmp4j.mp.StatusInformation; 15 | import org.snmp4j.smi.Counter64; 16 | import org.snmp4j.smi.Integer32; 17 | import org.snmp4j.smi.Null; 18 | import org.snmp4j.smi.OID; 19 | import org.snmp4j.smi.OctetString; 20 | import org.snmp4j.smi.UdpAddress; 21 | import org.snmp4j.smi.Variable; 22 | import org.snmp4j.smi.VariableBinding; 23 | import org.snmp4j.transport.DefaultUdpTransportMapping; 24 | import org.vafer.jmx2snmp.jmx.JmxAttribute; 25 | import org.vafer.jmx2snmp.jmx.JmxIndex; 26 | import org.vafer.jmx2snmp.jmx.JmxMib; 27 | import org.vafer.jmx2snmp.jmx.JmxMib.Bean; 28 | 29 | /** 30 | * The SnmpBridge starts a SNMP agent and provides access to the MBean objects. 31 | * It looks up the JMX attribute path from the JmxMib mapping and looks up the 32 | * JmxAttribute from the JmxIndex. 33 | * 34 | * Calling report() on startup will log mapping incosistencies to System.err 35 | */ 36 | public final class SnmpBridge implements CommandResponder { 37 | 38 | private final InetAddress address; 39 | private final int port; 40 | private final JmxIndex jmxIndex; 41 | private final JmxMib jmxMib; 42 | 43 | private Snmp snmp; 44 | 45 | public SnmpBridge(InetAddress pAddress, int pPort, JmxIndex pJmxIndex, JmxMib pJmxMib) { 46 | address = pAddress; 47 | port = pPort; 48 | jmxIndex = pJmxIndex; 49 | jmxMib = pJmxMib; 50 | } 51 | 52 | public void report() { 53 | 54 | final Map mibMapping = jmxMib.getMapping(); 55 | 56 | final Set attributesInMib = new HashSet(); 57 | final Set attributesInIndex = new HashSet(jmxIndex.getAttributePaths()); 58 | for(Map.Entry entry : mibMapping.entrySet()) { 59 | String oid = entry.getKey(); 60 | Bean bean = entry.getValue(); 61 | if (attributesInIndex.contains(bean.absolutePath)) { 62 | if (attributesInMib.contains(bean.absolutePath)) { 63 | System.err.println("jmx2snmp: attribute mapping for [" + bean.absolutePath + "] found more than once"); 64 | } 65 | attributesInMib.add(bean.absolutePath); 66 | } else { 67 | if (bean.leaf) { 68 | System.err.println("jmx2snmp: attribute [" + bean.absolutePath + "] no longer exists at OID [" + oid + "]"); 69 | } 70 | } 71 | } 72 | 73 | attributesInIndex.removeAll(attributesInMib); 74 | 75 | for(String attribute : attributesInIndex) { 76 | System.err.println("jmx2snmp: attribute not mapped yet: " + attribute); 77 | } 78 | 79 | } 80 | 81 | public void processPdu(CommandResponderEvent pRequest) { 82 | 83 | final PDU requestPdu = pRequest.getPDU(); 84 | 85 | if (requestPdu == null) { 86 | return; 87 | } 88 | 89 | try { 90 | 91 | final PDU responsePdu = new PDU(requestPdu); 92 | responsePdu.setType(PDU.RESPONSE); 93 | 94 | if (requestPdu.getType() == PDU.GET) { 95 | 96 | for(VariableBinding binding : responsePdu.toArray()) { 97 | final OID oid = binding.getOid(); 98 | final String path = jmxMib.getPathFromOid(oid.toString()); 99 | 100 | if (path == null) { 101 | binding.setVariable(Null.noSuchObject); 102 | continue; 103 | } 104 | 105 | final JmxAttribute attribute = jmxIndex.getAttributeAtPath(path); 106 | 107 | if (attribute == null) { 108 | binding.setVariable(Null.noSuchObject); 109 | continue; 110 | } 111 | 112 | final Variable variable = getVariableFromJmxAttribute(attribute); 113 | 114 | if (variable != null) { 115 | binding.setVariable(variable); 116 | } 117 | } 118 | 119 | } else if (requestPdu.getType() == PDU.GETNEXT) { 120 | 121 | 122 | for(VariableBinding binding : responsePdu.toArray()) { 123 | final OID oid = binding.getOid(); 124 | final String next = jmxMib.getNextOidFromOid(oid.toString()); 125 | 126 | if (next == null) { 127 | binding.setVariable(Null.noSuchObject); 128 | continue; 129 | } 130 | 131 | final OID nextOid = new OID(next); 132 | 133 | binding.setOid(nextOid); 134 | 135 | final String path = jmxMib.getPathFromOid(nextOid.toString()); 136 | 137 | if (path == null) { 138 | binding.setVariable(Null.noSuchObject); 139 | continue; 140 | } 141 | 142 | final JmxAttribute attribute = jmxIndex.getAttributeAtPath(path); 143 | 144 | if (attribute == null) { 145 | binding.setVariable(Null.noSuchObject); 146 | continue; 147 | } 148 | 149 | final Variable variable = getVariableFromJmxAttribute(attribute); 150 | 151 | if (variable != null) { 152 | binding.setVariable(variable); 153 | } 154 | } 155 | 156 | } else { 157 | 158 | } 159 | 160 | pRequest.getStateReference().setTransportMapping(pRequest.getTransportMapping()); 161 | pRequest.getMessageDispatcher().returnResponsePdu( 162 | pRequest.getMessageProcessingModel(), 163 | pRequest.getSecurityModel(), 164 | pRequest.getSecurityName(), 165 | pRequest.getSecurityLevel(), 166 | responsePdu, 167 | pRequest.getMaxSizeResponsePDU(), 168 | pRequest.getStateReference(), 169 | new StatusInformation() 170 | ); 171 | 172 | } catch (Exception e) { 173 | e.printStackTrace(); 174 | } 175 | } 176 | 177 | 178 | public void start() throws Exception { 179 | snmp = new Snmp(new DefaultUdpTransportMapping(new UdpAddress(address, port))); 180 | snmp.addCommandResponder(this); 181 | snmp.listen(); 182 | } 183 | 184 | public void stop() throws Exception { 185 | snmp.close(); 186 | snmp = null; 187 | } 188 | 189 | private Variable getVariableFromJmxAttribute(JmxAttribute pAttribute) throws JMException { 190 | 191 | final Object value = pAttribute.getValue(); 192 | 193 | if (value == null) { 194 | return new Null(); 195 | } 196 | 197 | final String type = pAttribute.getType(); 198 | 199 | if ("int".equals(type)) { 200 | final Number n = (Number) value; 201 | return new Integer32(n.intValue()); 202 | } else if ("long".equals(type)) { 203 | final Number n = (Number) value; 204 | return new Counter64(n.longValue()); 205 | } else if ("boolean".equals(type)) { 206 | final Boolean b = (Boolean) value; 207 | return new Integer32(b?1:0); 208 | } else if ("java.lang.String".equals(type)) { 209 | return new OctetString(String.valueOf(value)); 210 | } else { 211 | return new OctetString("Unsupported Type: " + pAttribute.getType()); 212 | } 213 | 214 | } 215 | 216 | // public static void main(String[] args) throws Exception { 217 | // 218 | // System.out.println("starting..."); 219 | // 220 | // final MBeanExporter exporter = new MBeanExporter(ManagementFactory.getPlatformMBeanServer()); 221 | // exporter.export("bean:name=test1", new TestBeanImpl()); 222 | // 223 | // final JmxServer jmxServer = new JmxServer(InetAddress.getByName("localhost")); 224 | // jmxServer.start(); 225 | // 226 | // final URL url = JmxutilsTestCase.class.getResource("/org/vafer/jmx2snmp/mapping.properties"); 227 | // 228 | // final JmxMib jmxMib = new JmxMib(); 229 | // jmxMib.load(new FileReader(url.getFile())); 230 | // 231 | // final JmxIndex jmxIndex = new JmxIndex(); 232 | // 233 | // final SnmpBridge snmpBridge = new SnmpBridge(InetAddress.getByName("192.168.214.1"), 1161, jmxIndex, jmxMib); 234 | // snmpBridge.start(); 235 | // 236 | // System.out.println("enter 'quit' to stop..."); 237 | // final Scanner sc = new Scanner(System.in); 238 | // while(!sc.nextLine().equals("quit")); 239 | // 240 | // snmpBridge.stop(); 241 | // jmxServer.stop(); 242 | // } 243 | } 244 | -------------------------------------------------------------------------------- /src/test/java/org/vafer/jmx2snmp/beans/TestBean.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.beans; 2 | 3 | import java.util.Collection; 4 | import java.util.Map; 5 | import java.util.Set; 6 | 7 | public interface TestBean { 8 | 9 | boolean getBoolean(); 10 | 11 | void setBoolean(boolean b); 12 | 13 | long getLong(); 14 | 15 | void setLong(long l); 16 | 17 | int getSomeInt(); 18 | 19 | int getAnotherInt(); 20 | 21 | void setSomeInt(int i); 22 | 23 | String getString(); 24 | 25 | void setString(String s); 26 | 27 | String[] getStringArray(); 28 | 29 | String[][] getStringTable(); 30 | 31 | TestRow[] getTestRowArray(); 32 | 33 | Collection getSomeObjects(); 34 | 35 | int[] getIntArray(); 36 | 37 | Map getSomethingMapped(); 38 | 39 | Set getSomethingUnique(); 40 | 41 | boolean isBlue(); 42 | 43 | boolean[] getBooleanArray(); 44 | 45 | Integer[] getIntegerArray(); 46 | 47 | } -------------------------------------------------------------------------------- /src/test/java/org/vafer/jmx2snmp/beans/TestRow.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.beans; 2 | 3 | import java.io.Serializable; 4 | 5 | public class TestRow implements Serializable { 6 | 7 | private static final long serialVersionUID = 1L; 8 | 9 | public String getString() { 10 | return "String"; 11 | } 12 | 13 | public int getInt() { 14 | return 1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/org/vafer/jmx2snmp/jmx/JmxMibTestCase.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmx; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.io.StringReader; 6 | import java.util.Map; 7 | 8 | import org.junit.Test; 9 | import org.vafer.jmx2snmp.jmx.JmxMib.Bean; 10 | 11 | public final class JmxMibTestCase { 12 | 13 | @Test 14 | public void testLoading() throws Exception { 15 | final JmxMib mib = new JmxMib(); 16 | 17 | mib.load(new StringReader( 18 | "1.2.3 = \n" + 19 | "1.2.3.1 = bean1\n" + 20 | "1.2.3.1.1 = attribute1\n" + 21 | "1.2.3.1.2 = attribute2\n" + 22 | "1.2.3.2 = bean2\n" + 23 | "1.2.3.2.1 = attribute1\n" + 24 | "1.2.3.2.2 = attribute2\n" 25 | )); 26 | 27 | assertEquals(9, mib.getNodeCount()); 28 | 29 | assertEquals("", mib.getPathFromOid("1.2.3")); 30 | assertEquals(".bean1", mib.getPathFromOid("1.2.3.1")); 31 | assertEquals(".bean2", mib.getPathFromOid("1.2.3.2")); 32 | assertEquals(".bean1.attribute1", mib.getPathFromOid("1.2.3.1.1")); 33 | assertEquals(".bean1.attribute2", mib.getPathFromOid("1.2.3.1.2")); 34 | assertEquals(".bean2.attribute1", mib.getPathFromOid("1.2.3.2.1")); 35 | assertEquals(".bean2.attribute2", mib.getPathFromOid("1.2.3.2.2")); 36 | 37 | final Map mapping = mib.getMapping(); 38 | 39 | assertEquals(7, mapping.size()); 40 | 41 | mib.load(new StringReader( 42 | "2.2.3 = \n" + 43 | "2.2.3.1 = bean1\n" + 44 | "2.2.3.1.1 = attribute1\n" + 45 | "2.2.3.1.2 = attribute2\n" + 46 | "2.2.3.2 = bean2\n" + 47 | "2.2.3.2.1 = attribute1\n" + 48 | "2.2.3.2.2 = attribute2\n" 49 | )); 50 | 51 | assertEquals(9, mib.getNodeCount()); 52 | 53 | assertEquals("", mib.getPathFromOid("2.2.3")); 54 | assertEquals(".bean1", mib.getPathFromOid("2.2.3.1")); 55 | assertEquals(".bean2", mib.getPathFromOid("2.2.3.2")); 56 | assertEquals(".bean1.attribute1", mib.getPathFromOid("2.2.3.1.1")); 57 | assertEquals(".bean1.attribute2", mib.getPathFromOid("2.2.3.1.2")); 58 | assertEquals(".bean2.attribute1", mib.getPathFromOid("2.2.3.2.1")); 59 | assertEquals(".bean2.attribute2", mib.getPathFromOid("2.2.3.2.2")); 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/org/vafer/jmx2snmp/jmx/JmxServerTestCase.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmx; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.lang.management.ManagementFactory; 6 | import java.net.InetAddress; 7 | 8 | import org.weakref.jmx.MBeanExporter; 9 | 10 | import org.junit.Test; 11 | import org.vafer.jmx2snmp.jmxutils.beans.TestBeanImpl; 12 | 13 | 14 | public final class JmxServerTestCase { 15 | 16 | @Test 17 | public void testSomething() throws Exception { 18 | final MBeanExporter exporter = new MBeanExporter(ManagementFactory.getPlatformMBeanServer()); 19 | exporter.export("bean:name=test1", new TestBeanImpl()); 20 | 21 | final JmxServer jmxServer = new JmxServer(InetAddress.getByName("localhost")); 22 | jmxServer.start(); 23 | 24 | assertEquals("service:jmx:rmi://127.0.0.1:0/jndi/rmi://127.0.0.1:5100/connector", jmxServer.getUrl()); 25 | assertEquals(5100, jmxServer.getNamingPort()); 26 | assertEquals(0, jmxServer.getProtocolPort()); 27 | 28 | jmxServer.stop(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/org/vafer/jmx2snmp/jmxutils/JmxutilsTestCase.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmxutils; 2 | 3 | import static org.junit.Assert.assertNotNull; 4 | 5 | import java.io.FileReader; 6 | import java.lang.management.ManagementFactory; 7 | import java.net.InetAddress; 8 | import java.net.URL; 9 | 10 | import org.junit.Test; 11 | import org.vafer.jmx2snmp.jmx.JmxIndex; 12 | import org.vafer.jmx2snmp.jmx.JmxMib; 13 | import org.vafer.jmx2snmp.jmx.JmxServer; 14 | import org.vafer.jmx2snmp.jmxutils.beans.TestBeanImpl; 15 | import org.vafer.jmx2snmp.snmp.SnmpBridge; 16 | import org.weakref.jmx.MBeanExporter; 17 | 18 | public class JmxutilsTestCase { 19 | 20 | @Test 21 | public void testStartup() throws Exception { 22 | final MBeanExporter exporter = new MBeanExporter(ManagementFactory.getPlatformMBeanServer()); 23 | exporter.export("bean:name=test1", new TestBeanImpl()); 24 | 25 | final JmxServer jmxServer = new JmxServer(InetAddress.getByName("localhost")); 26 | jmxServer.start(); 27 | 28 | final URL url = JmxutilsTestCase.class.getResource("/org/vafer/jmx2snmp/mapping.properties"); 29 | 30 | assertNotNull(url); 31 | 32 | final JmxMib jmxMapping = new JmxMib(); 33 | jmxMapping.load(new FileReader(url.getFile())); 34 | 35 | final JmxIndex jmxIndex = new JmxIndex(); 36 | 37 | final SnmpBridge snmpBridge = new SnmpBridge(InetAddress.getByName("localhost"), 1161, jmxIndex, jmxMapping); 38 | snmpBridge.start(); 39 | 40 | assertNotNull(jmxServer); 41 | assertNotNull(jmxIndex); 42 | assertNotNull(snmpBridge); 43 | 44 | // snmpwalk -On -c public -v 1 localhost:1161 1.3.6.1.4.1.27305.12 45 | // snmpget -On -c public -v 1 localhost:1161 1.3.6.1.4.1.27305.12.8 46 | 47 | // System.out.println("enter 'quit' to stop..."); 48 | // final Scanner sc = new Scanner(System.in); 49 | // while(!sc.nextLine().equals("quit")); 50 | 51 | snmpBridge.report(); 52 | 53 | snmpBridge.stop(); 54 | jmxServer.stop(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/org/vafer/jmx2snmp/jmxutils/beans/TestBeanImpl.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.jmxutils.beans; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.HashMap; 6 | import java.util.HashSet; 7 | import java.util.Map; 8 | import java.util.Set; 9 | 10 | import org.weakref.jmx.Managed; 11 | 12 | import org.vafer.jmx2snmp.beans.TestBean; 13 | import org.vafer.jmx2snmp.beans.TestRow; 14 | 15 | 16 | public final class TestBeanImpl implements TestBean { 17 | 18 | private final Collection col = new ArrayList(); 19 | 20 | public TestBeanImpl() { 21 | col.add("row1"); 22 | col.add("row2"); 23 | } 24 | 25 | @Managed 26 | public Collection getSomeObjects() { 27 | return col; 28 | } 29 | 30 | @Managed 31 | public boolean isBlue() { 32 | return false; 33 | } 34 | 35 | @Managed 36 | public boolean getBoolean() { 37 | return true; 38 | } 39 | 40 | public void setBoolean( boolean b ) { 41 | } 42 | 43 | @Managed 44 | public long getLong() { 45 | return 1; 46 | } 47 | 48 | public void setLong(long l) { 49 | } 50 | 51 | @Managed 52 | public int getSomeInt() { 53 | return 1; 54 | } 55 | 56 | @Managed 57 | public int getAnotherInt() { 58 | return 1; 59 | } 60 | 61 | public void setSomeInt(int i) { 62 | } 63 | 64 | @Managed 65 | public String getString() { 66 | return "string"; 67 | } 68 | 69 | public void setString(String s) { 70 | } 71 | 72 | @Managed 73 | public String[][] getStringTable() { 74 | return new String[][] { 75 | { "column1row1", "column2row1" }, 76 | { "column1row2", "column2row2" } 77 | }; 78 | } 79 | 80 | @Managed 81 | public TestRow[] getTestRowArray() { 82 | return new TestRow[] { 83 | new TestRow(), 84 | new TestRow() 85 | }; 86 | } 87 | 88 | @Managed 89 | public int[] getIntArray() { 90 | return new int[] { 1, 2 }; 91 | } 92 | 93 | @Managed 94 | public String[] getStringArray() { 95 | return new String[] { "row1", "row2" }; 96 | } 97 | 98 | @Managed 99 | public boolean[] getBooleanArray() { 100 | return new boolean[] { true, true }; 101 | } 102 | 103 | @Managed 104 | public Integer[] getIntegerArray() { 105 | return new Integer[] { new Integer(1), new Integer(2)}; 106 | } 107 | 108 | @Managed 109 | public Map getSomethingMapped() { 110 | final Map map = new HashMap(); 111 | map.put("a", "valueA"); 112 | map.put("b", "valueB"); 113 | return map; 114 | } 115 | 116 | @Managed 117 | public Set getSomethingUnique() { 118 | final Set set = new HashSet(); 119 | set.add("a"); 120 | set.add("b"); 121 | return set; 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/test/java/org/vafer/jmx2snmp/snmp/SnmpBridgeTestCase.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.snmp; 2 | 3 | public final class SnmpBridgeTestCase { 4 | 5 | public void testSomething() throws Exception { 6 | 7 | // TODO this needs some serious mocking 8 | 9 | // final JmxIndex jmxIndex = null; 10 | // final JmxMib jmxMib = null; 11 | // final SnmpBridge snmpBridge = new SnmpBridge(InetAddress.getByName("localhost"), 1161, jmxIndex, jmxMib); 12 | // snmpBridge.start(); 13 | // 14 | // final CommandResponderEvent event = null; 15 | // 16 | // snmpBridge.processPdu(event); 17 | // 18 | // assertNotNull(jmxMib); 19 | // 20 | // snmpBridge.stop(); 21 | } 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/vafer/jmx2snmp/spring/SpringTestCase.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.spring; 2 | 3 | import static org.junit.Assert.assertNotNull; 4 | 5 | import java.io.FileReader; 6 | import java.net.InetAddress; 7 | import java.net.URL; 8 | 9 | import org.junit.Test; 10 | import org.springframework.beans.factory.BeanFactory; 11 | import org.springframework.beans.factory.xml.XmlBeanFactory; 12 | import org.springframework.core.io.ClassPathResource; 13 | import org.vafer.jmx2snmp.jmx.JmxIndex; 14 | import org.vafer.jmx2snmp.jmx.JmxMib; 15 | import org.vafer.jmx2snmp.jmx.JmxServer; 16 | import org.vafer.jmx2snmp.snmp.SnmpBridge; 17 | 18 | public class SpringTestCase { 19 | 20 | @Test 21 | public void testStartup() throws Exception { 22 | 23 | // BasicConfigurator.configure(); 24 | final BeanFactory factory = new XmlBeanFactory(new ClassPathResource("org/vafer/jmx2snmp/spring/beans.xml")); 25 | // this should not be required 26 | factory.getBean("exporter"); 27 | 28 | final JmxServer jmxServer = new JmxServer(InetAddress.getByName("localhost")); 29 | jmxServer.start(); 30 | 31 | final URL url = SpringTestCase.class.getResource("/org/vafer/jmx2snmp/mapping.properties"); 32 | 33 | assertNotNull(url); 34 | 35 | final JmxMib jmxMapping = new JmxMib(); 36 | jmxMapping.load(new FileReader(url.getFile())); 37 | 38 | final JmxIndex jmxIndex = new JmxIndex(); 39 | 40 | final SnmpBridge snmpBridge = new SnmpBridge(InetAddress.getByName("localhost"), 1161, jmxIndex, jmxMapping); 41 | snmpBridge.start(); 42 | 43 | assertNotNull(jmxServer); 44 | assertNotNull(jmxIndex); 45 | assertNotNull(snmpBridge); 46 | 47 | snmpBridge.stop(); 48 | jmxServer.stop(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/org/vafer/jmx2snmp/spring/beans/TestBeanImpl.java: -------------------------------------------------------------------------------- 1 | package org.vafer.jmx2snmp.spring.beans; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.HashMap; 6 | import java.util.HashSet; 7 | import java.util.Map; 8 | import java.util.Set; 9 | 10 | import org.vafer.jmx2snmp.beans.TestBean; 11 | import org.vafer.jmx2snmp.beans.TestRow; 12 | 13 | 14 | public final class TestBeanImpl implements TestBean { 15 | 16 | private final Collection col = new ArrayList(); 17 | 18 | public TestBeanImpl() { 19 | col.add("row1"); 20 | col.add("row2"); 21 | } 22 | 23 | public Collection getSomeObjects() { 24 | return col; 25 | } 26 | 27 | public boolean isBlue() { 28 | return false; 29 | } 30 | 31 | public boolean getBoolean() { 32 | return true; 33 | } 34 | 35 | public void setBoolean( boolean b ) { 36 | } 37 | 38 | public long getLong() { 39 | return 1; 40 | } 41 | 42 | public void setLong(long l) { 43 | } 44 | 45 | public int getSomeInt() { 46 | return 1; 47 | } 48 | 49 | public int getAnotherInt() { 50 | return 1; 51 | } 52 | 53 | public void setSomeInt(int i) { 54 | } 55 | 56 | public String getString() { 57 | return "string"; 58 | } 59 | 60 | public void setString(String s) { 61 | } 62 | 63 | public String[][] getStringTable() { 64 | return new String[][] { 65 | { "column1row1", "column2row1" }, 66 | { "column1row2", "column2row2" } 67 | }; 68 | } 69 | 70 | public TestRow[] getTestRowArray() { 71 | return new TestRow[] { 72 | new TestRow(), 73 | new TestRow() 74 | }; 75 | } 76 | 77 | public int[] getIntArray() { 78 | return new int[] { 1, 2 }; 79 | } 80 | 81 | public String[] getStringArray() { 82 | return new String[] { "row1", "row2" }; 83 | } 84 | 85 | public boolean[] getBooleanArray() { 86 | return new boolean[] { true, true }; 87 | } 88 | 89 | public Integer[] getIntegerArray() { 90 | return new Integer[] { new Integer(1), new Integer(2)}; 91 | } 92 | 93 | public Map getSomethingMapped() { 94 | final Map map = new HashMap(); 95 | map.put("a", "valueA"); 96 | map.put("b", "valueB"); 97 | return map; 98 | } 99 | 100 | public Set getSomethingUnique() { 101 | final Set set = new HashSet(); 102 | set.add("a"); 103 | set.add("b"); 104 | return set; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/test/resources/org/vafer/jmx2snmp/mapping.properties: -------------------------------------------------------------------------------- 1 | 1.3.6.1.4.1.27305 = 2 | 1.3.6.1.4.1.27305.12 = test1 3 | 1.3.6.1.4.1.27305.12.1 = AnotherInt 4 | 1.3.6.1.4.1.27305.12.2 = Blue 5 | 1.3.6.1.4.1.27305.12.3 = Boolean 6 | 1.3.6.1.4.1.27305.12.4 = BooleanArray 7 | 1.3.6.1.4.1.27305.12.5 = IntArray 8 | 1.3.6.1.4.1.27305.12.6 = IntegerArray 9 | 1.3.6.1.4.1.27305.12.7 = Long 10 | 1.3.6.1.4.1.27305.12.8 = SomeInt 11 | 1.3.6.1.4.1.27305.12.9 = SomeObjects 12 | 1.3.6.1.4.1.27305.12.10 = SomethingMapped 13 | 1.3.6.1.4.1.27305.12.11 = SomethingUnique 14 | 1.3.6.1.4.1.27305.12.12 = String 15 | 1.3.6.1.4.1.27305.12.13 = StringArray 16 | 1.3.6.1.4.1.27305.12.14 = StringTable 17 | 1.3.6.1.4.1.27305.12.15 = TestRowArray -------------------------------------------------------------------------------- /src/test/resources/org/vafer/jmx2snmp/spring/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | something 20 | 21 | 22 | --------------------------------------------------------------------------------