/** {@inheritDoc} */ @Override public InetAddress unmarshal(String ipAddr) throws Exception { return InetAddrUtils.addr(ipAddr); }
@Override public String toString() { return InetAddrUtils.str(m_address) + ":" + m_port; } }
@Override public byte[] getLocalEngineID() { // lovingly stolen from SNMP4J final byte[] engineID = new byte[5]; engineID[0] = (byte) (0x80 | ((s_enterpriseId >> 24) & 0xFF)); engineID[1] = (byte) ((s_enterpriseId >> 16) & 0xFF); engineID[2] = (byte) ((s_enterpriseId >> 8) & 0xFF); engineID[3] = (byte) (s_enterpriseId & 0xFF); final byte[] ip = InetAddrUtils.getLocalHostAddress().getAddress(); if (ip.length == 4) { // IPv4 engineID[4] = 1; } else if (ip.length == 16) { // IPv6 engineID[4] = 2; } else { // Text engineID[4] = 4; } final byte[] bytes = new byte[engineID.length+ip.length]; System.arraycopy(engineID, 0, bytes, 0, engineID.length); System.arraycopy(ip, 0, bytes, engineID.length, ip.length); return bytes; }
public static InetAddress getLocalHostAddress() { return addr("127.0.0.1"); }
/** {@inheritDoc} */ @Override public String marshal(InetAddress inetAddr) throws Exception { return InetAddrUtils.str(inetAddr); }
@Test public void canCollectUsingOpenNMSWorkflow() throws CollectionInitializationException, CollectionException { // create the agent OnmsNode node = mock(OnmsNode.class); OnmsIpInterface iface = mock(OnmsIpInterface.class); when(iface.getNode()).thenReturn(node); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress()); IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class); when(ifaceDao.load(1)).thenReturn(iface); PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class); final CollectionAgent agent = createAgent(1, ifaceDao, transMgr); // init() should execute without any exceptions final ServiceCollector opennmsCollector = getCollector(); initialize(opennmsCollector); // getEffectiveLocation() should execute without any exceptions // in this context there are no requirements on its return value final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID; opennmsCollector.getEffectiveLocation(targetLocation); // getRuntimeAttributes() should return a valid map final Map<String, Object> requiredParams = getRequiredParameters(); final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams)); // collect() should return a valid collection set final Map<String, Object> allParms = new HashMap<>(); allParms.putAll(requiredParams); allParms.putAll(runtimeAttrs); final CollectionSet collectionSet = opennmsCollector.collect(agent, Collections.unmodifiableMap(allParms)); assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus()); // getRrdRepository() should return a valid repository assertNotNull(opennmsCollector.getRrdRepository(getCollectionName())); }
@Override public InetAddress toInetAddress() { try { return InetAddrUtils.addr(toString()); } catch (Exception e) { return super.toInetAddress(); } }
@Override public String getHostAddress() { return address != null ? InetAddrUtils.str(address) : null; }
@Test public void canCollectUsingOpenNMSWorkflow() throws CollectionInitializationException, CollectionException { // create the agent OnmsNode node = mock(OnmsNode.class); OnmsIpInterface iface = mock(OnmsIpInterface.class); when(iface.getNode()).thenReturn(node); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress()); IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class); when(ifaceDao.load(1)).thenReturn(iface); PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class); final CollectionAgent agent = createAgent(1, ifaceDao, transMgr); // init() should execute without any exceptions final ServiceCollector opennmsCollector = getCollector(); initialize(opennmsCollector); // getEffectiveLocation() should execute without any exceptions // in this context there are no requirements on its return value final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID; opennmsCollector.getEffectiveLocation(targetLocation); // getRuntimeAttributes() should return a valid map final Map<String, Object> requiredParams = getRequiredParameters(); final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams)); // collect() should return a valid collection set final Map<String, Object> allParms = new HashMap<>(); allParms.putAll(requiredParams); allParms.putAll(runtimeAttrs); final CollectionSet collectionSet = opennmsCollector.collect(agent, Collections.unmodifiableMap(allParms)); assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus()); // getRrdRepository() should return a valid repository assertNotNull(opennmsCollector.getRrdRepository(getCollectionName())); }
public static SnmpAgentConfig fromMap(Map<String, String> map) { SnmpAgentConfig config = new SnmpAgentConfig(); if (map.get("address") != null) config.setAddress(InetAddrUtils.addr(map.get("address"))); if (map.get("proxyFor") != null) config.setProxyFor(InetAddrUtils.addr(map.get("proxyFor"))); if (map.get("port") != null) config.setPort(Integer.parseInt(map.get("port"))); if (map.get("timeout") != null) config.setTimeout(Integer.parseInt(map.get("timeout"))); if (map.get("retries") != null) config.setRetries(Integer.parseInt(map.get("retries"))); if (map.get("max-vars-per-pdu") != null) config.setMaxVarsPerPdu(Integer.parseInt(map.get("max-vars-per-pdu"))); if (map.get("max-repetitions") != null) config.setMaxRepetitions(Integer.parseInt(map.get("max-repetitions"))); if (map.get("max-request-size") != null) config.setMaxRequestSize(Integer.parseInt(map.get("max-request-size"))); if (map.get("version") != null) config.setVersion(Integer.parseInt(map.get("version"))); if (map.get("security-level") != null) config.setSecurityLevel(Integer.parseInt(map.get("security-level"))); if (map.get("security-name") != null) config.setSecurityName(map.get("security-name")); if (map.get("auth-passphrase") != null) config.setAuthPassPhrase(map.get("auth-passphrase")); if (map.get("auth-protocol") != null) config.setAuthProtocol(map.get("auth-protocol")); if (map.get("priv-passphrase") != null) config.setPrivPassPhrase(map.get("priv-passphrase")); if (map.get("priv-protocol") != null) config.setPrivProtocol(map.get("priv-protocol")); if (map.get("context-name") != null) config.setContextName(map.get("context-name")); if (map.get("engine-id") != null) config.setEngineId(map.get("engine-id")); if (map.get("context-engine-id") != null) config.setContextEngineId(map.get("context-engine-id")); if (map.get("enterprise-id") != null) config.setEnterpriseId(map.get("enterprise-id")); if (map.get("read-community") != null) config.setReadCommunity(map.get("read-community")); if (map.get("write-community") != null) config.setWriteCommunity(map.get("write-community")); return config; } }
public IpAddressSnmpValue(InetAddress addr) { this(InetAddrUtils.str(addr)); }
OnmsIpInterface iface = mock(OnmsIpInterface.class); when(iface.getNode()).thenReturn(node); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
public static void sendTest(final String destAddr, final int destPort, final String community, final SnmpPduRequest pdu) throws UnknownHostException { final InetAddress agentAddress = InetAddrUtils.addr(destAddr); for (final RegistrationInfo info : s_registrations.values()) { if (destPort == info.getPort()) { info.getHandler().snmpReceivedTrap(info.getSession(), agentAddress, destPort, new SnmpOctetString(community.getBytes()), pdu); } } }
@Override public String toString() { return String.format("CollectionAgentDTO[attributes=%s, address=%s, storeByForeignSource=%s, " + "nodeId=%d, nodeLabel=%s, foreignSource=%s, foreignId=%s, location=%s, storageDir=%s, " + "sysUpTime=%d]", attributes, address != null ? InetAddrUtils.str(address) : null, storeByForeignSource, nodeId, nodeLabel, foreignSource, foreignId, location, storageResourcePath, sysUpTime); }
OnmsIpInterface iface = mock(OnmsIpInterface.class); when(iface.getNode()).thenReturn(node); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
return getCounter64(BigInteger.valueOf(Long.valueOf(mibVal.substring("Counter64:".length()).trim()))); } else if (mibVal.startsWith("IpAddress:")) { return getIpAddress(InetAddrUtils.addr(mibVal.substring("IpAddress:".length()).trim())); } else if (mibVal.startsWith("Hex-STRING:")) { final String trimmed = mibVal.substring("Hex-STRING:".length()).trim();
public SnmpAgentTimeoutException(String name, InetAddress agentAddress) { super(String.format("Timeout retrieving '%s' for %s.", name, InetAddrUtils.str(agentAddress))); } }
private void createCollectableService() throws CollectionInitializationException, IOException { // Mock it all! OnmsIpInterface iface = mock(OnmsIpInterface.class, RETURNS_DEEP_STUBS); IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class); spec = mock(CollectionSpecification.class); scheduler = mock(Scheduler.class); SchedulingCompletedFlag schedulingCompletedFlag = mock(SchedulingCompletedFlag.class); PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class); RrdPersisterFactory persisterFactory = new RrdPersisterFactory(); persisterFactory.setRrdStrategy(rrdStrategy); ResourceStorageDao resourceStorageDao = mock(ResourceStorageDao.class); // Disable thresholding Map<String, Object> paramsMap = new HashMap<>(); paramsMap.put("thresholding-enabled", Boolean.FALSE.toString()); ServiceParameters params = new ServiceParameters(paramsMap); when(iface.getNode().getId()).thenReturn(1); when(spec.getServiceParameters()).thenReturn(params); when(spec.getRrdRepository(any())).thenReturn(createRrdRepository()); when(ifaceDao.load(any())).thenReturn(iface); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress()); service = new CollectableService(iface, ifaceDao, spec, scheduler, schedulingCompletedFlag, transMgr, persisterFactory, resourceStorageDao); }
public static void sendTest(String destAddr, int destPort, String community, SnmpPduTrap pdu) throws UnknownHostException { final InetAddress agentAddress = InetAddrUtils.addr(destAddr); for (final RegistrationInfo info : s_registrations.values()) { if (destPort == info.getPort()) { info.getHandler().snmpReceivedTrap(info.getSession(), agentAddress, destPort, new SnmpOctetString(community.getBytes()), pdu); } } }
public static String addrStr(byte[] bytes) { try { return InetAddrUtils.str(InetAddress.getByAddress(bytes)); } catch (UnknownHostException e) { throw new RuntimeException(e); } }