Repository: ambari Updated Branches: refs/heads/trunk fd679a97e -> 9ac82a595
AMBARI-16186. Allow UDP SNMP disptacher port to be configurable. (mpapirkovskyy) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/9ac82a59 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/9ac82a59 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/9ac82a59 Branch: refs/heads/trunk Commit: 9ac82a5958c6797aceeae9fdfdc3b9d1307b0656 Parents: fd679a9 Author: Myroslav Papirkovskyi <[email protected]> Authored: Fri Apr 29 19:05:17 2016 +0300 Committer: Myroslav Papirkovskyi <[email protected]> Committed: Fri Apr 29 19:12:05 2016 +0300 ---------------------------------------------------------------------- .../server/configuration/Configuration.java | 11 +++++ .../server/controller/ControllerModule.java | 8 +++- .../dispatchers/SNMPDispatcher.java | 26 ++++++++++- .../notifications/DispatchFactoryTest.java | 4 ++ .../dispatchers/SNMPDispatcherTest.java | 48 ++++++++++---------- 5 files changed, 72 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/9ac82a59/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java index 0afae97..87f40d5 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java @@ -695,6 +695,8 @@ public class Configuration { private static final String AUDIT_LOGGER_CAPACITY = "auditlog.logger.capacity"; private static final int AUDIT_LOGGER_CAPACITY_DEFAULT = 10000; + public static final String ALERTS_SNMP_DISPATCH_UDP_PORT = "alerts.snmp.dispatcher.udp.port"; + private static final Logger LOG = LoggerFactory.getLogger( Configuration.class); @@ -2922,4 +2924,13 @@ public class Configuration { properties.getProperty(AUDIT_LOGGER_CAPACITY), AUDIT_LOGGER_CAPACITY_DEFAULT); } + + /** + * Customized UDP port for SNMP dispatcher + * @return Integer if property exists else null + */ + public Integer getSNMPUdpBindPort() { + String udpPort = properties.getProperty(ALERTS_SNMP_DISPATCH_UDP_PORT); + return StringUtils.isEmpty(udpPort) ? null : Integer.parseInt(udpPort); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/9ac82a59/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java index 3506031..7fb93c7 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java @@ -78,6 +78,7 @@ import org.apache.ambari.server.controller.utilities.DatabaseChecker; import org.apache.ambari.server.controller.utilities.KerberosChecker; import org.apache.ambari.server.notifications.DispatchFactory; import org.apache.ambari.server.notifications.NotificationDispatcher; +import org.apache.ambari.server.notifications.dispatchers.SNMPDispatcher; import org.apache.ambari.server.orm.DBAccessor; import org.apache.ambari.server.orm.DBAccessorImpl; import org.apache.ambari.server.orm.PersistenceType; @@ -595,7 +596,12 @@ public class ControllerModule extends AbstractModule { ClassUtils.getDefaultClassLoader()); try { - NotificationDispatcher dispatcher = (NotificationDispatcher) clazz.newInstance(); + NotificationDispatcher dispatcher; + if (clazz.equals(SNMPDispatcher.class)) { + dispatcher = (NotificationDispatcher) clazz.getConstructor(Integer.class).newInstance(configuration.getSNMPUdpBindPort()); + } else { + dispatcher = (NotificationDispatcher) clazz.newInstance(); + } dispatchFactory.register(dispatcher.getType(), dispatcher); bind((Class<NotificationDispatcher>) clazz).toInstance(dispatcher); http://git-wip-us.apache.org/repos/asf/ambari/blob/9ac82a59/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java index 53b3d91..a33b815 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcher.java @@ -84,12 +84,32 @@ public class SNMPDispatcher implements NotificationDispatcher { private Snmp snmp; + private Integer port; + public SNMPDispatcher(Snmp snmp) { this.snmp = snmp; } public SNMPDispatcher() throws IOException { - this(new Snmp(new DefaultUdpTransportMapping())); + this((Integer) null); + } + + /** + * Creates SNMP server with specified port. In case port is null will be used random value as default + * @param port port + * @throws IOException + */ + public SNMPDispatcher(Integer port) throws IOException { + if(port != null && port >= 0 && port <= '\uffff') { + //restrict invalid ports to avoid exception on socket create + this.port = port; + } + if (port != null) { + LOG.info("Setting SNMP dispatch port: " + port); + this.snmp = new Snmp(new DefaultUdpTransportMapping(new UdpAddress(port), true)); + } else { + this.snmp = new Snmp(new DefaultUdpTransportMapping()); + } } /** @@ -392,4 +412,8 @@ public class SNMPDispatcher implements NotificationDispatcher { notification.Callback.onSuccess(notification.CallbackIds); } } + + public Integer getPort() { + return port; + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/9ac82a59/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java b/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java index f136f31..4f06ee2 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/notifications/DispatchFactoryTest.java @@ -47,11 +47,13 @@ public class DispatchFactoryTest { public void testDispatchFactoryRegistration() throws Exception { String sourceResourceDirectory = "src" + File.separator + "test" + File.separator + "resources"; + Integer snmpPort = 30111; Properties properties = new Properties(); properties.setProperty(Configuration.SERVER_PERSISTENCE_TYPE_KEY,"in-memory"); properties.setProperty(Configuration.OS_VERSION_KEY, "centos6"); properties.setProperty(Configuration.SHARED_RESOURCES_DIR_KEY,sourceResourceDirectory); + properties.setProperty(Configuration.ALERTS_SNMP_DISPATCH_UDP_PORT,snmpPort.toString()); Injector injector = Guice.createInjector(new AuditLoggerModule(), new ControllerModule(properties)); DispatchFactory dispatchFactory = injector.getInstance(DispatchFactory.class); @@ -73,7 +75,9 @@ public class DispatchFactoryTest { SNMPDispatcher snmpDispatcher2 = (SNMPDispatcher) dispatchFactory.getDispatcher(snmpDispatcher.getType()); Assert.assertNotNull(snmpDispatcher); + Assert.assertEquals(snmpDispatcher.getPort(), snmpPort); Assert.assertNotNull(snmpDispatcher2); + Assert.assertEquals(snmpDispatcher2.getPort(), snmpPort); // verify singleton Assert.assertEquals(snmpDispatcher, snmpDispatcher2); http://git-wip-us.apache.org/repos/asf/ambari/blob/9ac82a59/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcherTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcherTest.java b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcherTest.java index e0d81c6..e5ea0bf 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcherTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/SNMPDispatcherTest.java @@ -41,9 +41,11 @@ import static org.mockito.Mockito.*; public class SNMPDispatcherTest { + private static final int DEFAULT_SNMP_PORT = 31444; + @Test public void testDispatch_nullProperties() throws Exception { - SNMPDispatcher dispatcher = new SNMPDispatcher(); + SNMPDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = mock(Notification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); @@ -54,7 +56,7 @@ public class SNMPDispatcherTest { @Test public void testDispatch_notDefinedProperties() throws Exception { - SNMPDispatcher dispatcher = new SNMPDispatcher(); + SNMPDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = mock(Notification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); @@ -66,7 +68,7 @@ public class SNMPDispatcherTest { @Test public void testDispatch_nullRecipients() throws Exception { - SNMPDispatcher dispatcher = new SNMPDispatcher(); + SNMPDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = mock(Notification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); @@ -87,7 +89,7 @@ public class SNMPDispatcherTest { @Test public void testDispatch_noRecipients() throws Exception { - SNMPDispatcher dispatcher = new SNMPDispatcher(); + SNMPDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = mock(Notification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); @@ -109,7 +111,7 @@ public class SNMPDispatcherTest { @Test public void testDispatch_sendTrapError() throws Exception { - SNMPDispatcher dispatcher = spy(new SNMPDispatcher()); + SNMPDispatcher dispatcher = spy(new SNMPDispatcher(DEFAULT_SNMP_PORT)); Notification notification = mock(Notification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); @@ -132,7 +134,7 @@ public class SNMPDispatcherTest { @Test public void testDispatch_incorrectSnmpVersion() throws Exception { - SNMPDispatcher dispatcher = spy(new SNMPDispatcher()); + SNMPDispatcher dispatcher = spy(new SNMPDispatcher(DEFAULT_SNMP_PORT)); Notification notification = mock(Notification.class); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); @@ -154,7 +156,7 @@ public class SNMPDispatcherTest { @Test public void testDispatch_successful_v1() throws Exception { - SNMPDispatcher dispatcher = spy(new SNMPDispatcher()); + SNMPDispatcher dispatcher = spy(new SNMPDispatcher(DEFAULT_SNMP_PORT)); SNMPDispatcher.SnmpVersion snmpVersion = SNMPDispatcher.SnmpVersion.SNMPv1; Notification notification = mock(Notification.class); notification.Callback = mock(DispatchCallback.class); @@ -178,7 +180,7 @@ public class SNMPDispatcherTest { @Test public void testDispatch_successful_v2() throws Exception { - SNMPDispatcher dispatcher = spy(new SNMPDispatcher()); + SNMPDispatcher dispatcher = spy(new SNMPDispatcher(DEFAULT_SNMP_PORT)); SNMPDispatcher.SnmpVersion snmpVersion = SNMPDispatcher.SnmpVersion.SNMPv2c; Notification notification = mock(Notification.class); notification.Callback = mock(DispatchCallback.class); @@ -202,7 +204,7 @@ public class SNMPDispatcherTest { @Test public void testDispatch_successful_v3() throws Exception { - SNMPDispatcher dispatcher = new SNMPDispatcher(); + SNMPDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = new Notification(); notification.Callback = mock(DispatchCallback.class); notification.CallbackIds = mock(List.class); @@ -231,7 +233,7 @@ public class SNMPDispatcherTest { @Test public void testPrepareTrap_v1() throws Exception { SNMPDispatcher.SnmpVersion snmpVersion = SNMPDispatcher.SnmpVersion.SNMPv1; - SNMPDispatcher dispatcher = new SNMPDispatcher(); + SNMPDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = new Notification(); Map<String, String> properties = new HashMap<String, String>(); properties.put(SNMPDispatcher.SUBJECT_OID_PROPERTY, "1"); @@ -255,7 +257,7 @@ public class SNMPDispatcherTest { @Test public void testPrepareTrap_v2c() throws Exception { SNMPDispatcher.SnmpVersion snmpVersion = SNMPDispatcher.SnmpVersion.SNMPv2c; - SNMPDispatcher dispatcher = new SNMPDispatcher(); + SNMPDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); Notification notification = new Notification(); Map<String, String> properties = new HashMap<String, String>(); properties.put(SNMPDispatcher.SUBJECT_OID_PROPERTY, "1"); @@ -379,7 +381,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv1"); properties.put(SNMPDispatcher.TRAP_OID_PROPERTY, "1.3.6.1.6.3.1.1.5.4"); properties.put(SNMPDispatcher.COMMUNITY_PROPERTY, "public"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.VALID, configValidationResult.getStatus()); } @@ -393,7 +395,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.TRAP_OID_PROPERTY, "1.3.6.1.6.3.1.1.5.4"); properties.put(SNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv4"); properties.put(SNMPDispatcher.COMMUNITY_PROPERTY, "public"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.INVALID, configValidationResult.getStatus()); } @@ -406,7 +408,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.PORT_PROPERTY, "162"); properties.put(SNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv1"); properties.put(SNMPDispatcher.COMMUNITY_PROPERTY, "public"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.INVALID, configValidationResult.getStatus()); } @@ -420,7 +422,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv2c"); properties.put(SNMPDispatcher.TRAP_OID_PROPERTY, "1.3.6.1.6.3.1.1.5.4"); properties.put(SNMPDispatcher.COMMUNITY_PROPERTY, "public"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.VALID, configValidationResult.getStatus()); } @@ -433,7 +435,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.PORT_PROPERTY, "162"); properties.put(SNMPDispatcher.TRAP_OID_PROPERTY, "1.3.6.1.6.3.1.1.5.4"); properties.put(SNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv2c"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.INVALID, configValidationResult.getStatus()); } @@ -450,7 +452,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.SECURITY_AUTH_PASSPHRASE_PROPERTY, "PASSPHRASE1"); properties.put(SNMPDispatcher.SECURITY_PRIV_PASSPHRASE_PROPERTY, "PASSPHRASE2"); properties.put(SNMPDispatcher.SECURITY_LEVEL_PROPERTY, "INCORRECT"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.INVALID, configValidationResult.getStatus()); } @@ -465,7 +467,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.TRAP_OID_PROPERTY, "1.3.6.1.6.3.1.1.5.4"); properties.put(SNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); properties.put(SNMPDispatcher.SECURITY_LEVEL_PROPERTY, "NOAUTH_NOPRIV"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.VALID, configValidationResult.getStatus()); } @@ -481,7 +483,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); properties.put(SNMPDispatcher.SECURITY_AUTH_PASSPHRASE_PROPERTY, "PASSPHRASE1"); properties.put(SNMPDispatcher.SECURITY_LEVEL_PROPERTY, "AUTH_NOPRIV"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.VALID, configValidationResult.getStatus()); } @@ -496,7 +498,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.TRAP_OID_PROPERTY, "1.3.6.1.6.3.1.1.5.4"); properties.put(SNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); properties.put(SNMPDispatcher.SECURITY_LEVEL_PROPERTY, "AUTH_NOPRIV"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.INVALID, configValidationResult.getStatus()); } @@ -513,7 +515,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.SECURITY_AUTH_PASSPHRASE_PROPERTY, "PASSPHRASE1"); properties.put(SNMPDispatcher.SECURITY_PRIV_PASSPHRASE_PROPERTY, "PASSPHRASE2"); properties.put(SNMPDispatcher.SECURITY_LEVEL_PROPERTY, "AUTH_PRIV"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.VALID, configValidationResult.getStatus()); } @@ -528,7 +530,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.TRAP_OID_PROPERTY, "1.3.6.1.6.3.1.1.5.4"); properties.put(SNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); properties.put(SNMPDispatcher.SECURITY_LEVEL_PROPERTY, "AUTH_PRIV"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.INVALID, configValidationResult.getStatus()); } @@ -544,7 +546,7 @@ public class SNMPDispatcherTest { properties.put(SNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); properties.put(SNMPDispatcher.SECURITY_AUTH_PASSPHRASE_PROPERTY, "PASSPHRASE1"); properties.put(SNMPDispatcher.SECURITY_LEVEL_PROPERTY, "AUTH_PRIV"); - NotificationDispatcher dispatcher = new SNMPDispatcher(); + NotificationDispatcher dispatcher = new SNMPDispatcher(DEFAULT_SNMP_PORT); TargetConfigurationResult configValidationResult = dispatcher.validateTargetConfig(properties); assertEquals(TargetConfigurationResult.Status.INVALID, configValidationResult.getStatus()); }
