AmbariSNMPDispatcher extends SNMPDispatcher { protected PDU prepareTrap(Notification notification, SnmpVersion snmpVersion) throws InvalidSnmpConfigurationException { AlertNotification alertNotification; PDU pdu = DefaultPDUFactory.createPDU(snmpVersion.getTargetVersion()); if (Notification.Type.ALERT.equals(notification.getType())) { try { alertNotification = (AlertNotification) notification; } catch (ClassCastException e) { LOG.error("Notification wasn't casted to AlertNotification. Returning empty Protocol data unit", e); return pdu; } } else { LOG.error("Notification for AmbariSNMPDispatcher should be of type AlertNotification, but it wasn't. Returning empty Protocol data unit"); return pdu; } pdu.setType(snmpVersion.getTrapType()); pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(AMBARI_ALERT_TRAP_OID))); AlertNoticeDispatchService.AlertInfo alertInfo = alertNotification.getAlertInfo(); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_ID_OID, new BigDecimal(alertInfo.getAlertDefinitionId()).intValueExact()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_NAME_OID, alertInfo.getAlertDefinition().getDefinitionName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_DEFINITION_HASH_OID, alertInfo.getAlertDefinitionHash()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_NAME_OID, alertInfo.getAlertName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_TEXT_OID, alertInfo.getAlertText()); addIntVariableBindingCheckForNull(pdu, AMBARI_ALERT_STATE_OID, alertInfo.getAlertState().getIntValue()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_HOST_NAME_OID, alertInfo.getHostName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_SERVICE_NAME_OID, alertInfo.getServiceName()); addStringVariableBindingCheckForNull(pdu, AMBARI_ALERT_COMPONENT_NAME_OID, alertInfo.getComponentName()); return pdu; } protected  AmbariSNMPDispatcher(Snmp snmp);  AmbariSNMPDispatcher(Integer port); @Override String getType(); static final String BASE_AMBARI_OID; static final String APACHE_AMBARI_TRAPS_OID; static final String AMBARI_ALERT_TRAP_OID; static final String AMBARI_ALERTS_OID; static final String AMBARI_ALERT_TABLE_OID; static final String AMBARI_ALERT_ENTRY_OID; static final String AMBARI_ALERT_DEFINITION_ID_OID; static final String AMBARI_ALERT_DEFINITION_NAME_OID; static final String AMBARI_ALERT_DEFINITION_HASH_OID; static final String AMBARI_ALERT_NAME_OID; static final String AMBARI_ALERT_TEXT_OID; static final String AMBARI_ALERT_STATE_OID; static final String AMBARI_ALERT_HOST_NAME_OID; static final String AMBARI_ALERT_SERVICE_NAME_OID; static final String AMBARI_ALERT_COMPONENT_NAME_OID; }
@Test public void testSendTraps_v1() throws Exception { AmbariSNMPDispatcher.SnmpVersion snmpVersion = AmbariSNMPDispatcher.SnmpVersion.SNMPv1; Snmp snmp = mock(Snmp.class); AmbariSNMPDispatcher dispatcher = spy(new AmbariSNMPDispatcher(snmp)); PDU trap = mock(PDU.class); Notification notification = new AlertNotification(); Map<String, String> properties = new HashMap<String, String>(); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); notification.DispatchProperties = properties; Recipient rec1 = new Recipient(); rec1.Identifier = "192.168.0.2"; notification.Recipients = Arrays.asList(rec1); doReturn(trap).when(dispatcher).prepareTrap(notification, snmpVersion); dispatcher.sendTraps(notification, snmpVersion); ArgumentCaptor<Target> argument = ArgumentCaptor.forClass(Target.class); verify(snmp, times(1)).send(eq(trap), argument.capture()); assertEquals("192.168.0.2/162", argument.getValue().getAddress().toString()); assertEquals(SnmpConstants.version1, argument.getValue().getVersion()); }
@Test public void testSendTraps_v2() throws Exception { AmbariSNMPDispatcher.SnmpVersion snmpVersion = AmbariSNMPDispatcher.SnmpVersion.SNMPv2c; Snmp snmp = mock(Snmp.class); AmbariSNMPDispatcher dispatcher = spy(new AmbariSNMPDispatcher(snmp)); PDU trap = mock(PDU.class); Notification notification = new AlertNotification(); Map<String, String> properties = new HashMap<String, String>(); properties.put(AmbariSNMPDispatcher.COMMUNITY_PROPERTY, "public"); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); notification.DispatchProperties = properties; Recipient rec1 = new Recipient(); rec1.Identifier = "192.168.0.2"; notification.Recipients = Arrays.asList(rec1); doReturn(trap).when(dispatcher).prepareTrap(notification, snmpVersion); dispatcher.sendTraps(notification, snmpVersion); ArgumentCaptor<Target> argument = ArgumentCaptor.forClass(Target.class); verify(snmp, times(1)).send(eq(trap), argument.capture()); assertEquals("192.168.0.2/162", argument.getValue().getAddress().toString()); assertEquals(SnmpConstants.version2c, argument.getValue().getVersion()); }
@Test public void testSendTraps_v3() throws Exception { AmbariSNMPDispatcher.SnmpVersion snmpVersion = AmbariSNMPDispatcher.SnmpVersion.SNMPv3; Snmp snmp = mock(Snmp.class); AmbariSNMPDispatcher dispatcher = spy(new AmbariSNMPDispatcher(snmp)); PDU trap = mock(PDU.class); Notification notification = new AlertNotification(); Map<String, String> properties = new HashMap<String, String>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); properties.put(AmbariSNMPDispatcher.SECURITY_AUTH_PASSPHRASE_PROPERTY, "PASSPHRASE1"); properties.put(AmbariSNMPDispatcher.SECURITY_PRIV_PASSPHRASE_PROPERTY, "PASSPHRASE2"); properties.put(AmbariSNMPDispatcher.SECURITY_LEVEL_PROPERTY, "AUTH_NOPRIV"); notification.DispatchProperties = properties; Recipient rec1 = new Recipient(); rec1.Identifier = "192.168.0.2"; notification.Recipients = Arrays.asList(rec1); doReturn(trap).when(dispatcher).prepareTrap(notification, snmpVersion); dispatcher.sendTraps(notification, snmpVersion); ArgumentCaptor<Target> argument = ArgumentCaptor.forClass(Target.class); verify(snmp, times(1)).send(eq(trap), argument.capture()); assertEquals("192.168.0.2/162", argument.getValue().getAddress().toString()); assertEquals(SnmpConstants.version3, argument.getValue().getVersion()); }
@Test(expected = AmbariSNMPDispatcher.InvalidSnmpConfigurationException.class) public void testSendTraps_v3_incorrectSecurityLevelVersion() throws Exception { AmbariSNMPDispatcher.SnmpVersion snmpVersion = AmbariSNMPDispatcher.SnmpVersion.SNMPv3; Snmp snmp = mock(Snmp.class); AmbariSNMPDispatcher dispatcher = spy(new AmbariSNMPDispatcher(snmp)); PDU trap = mock(PDU.class); Notification notification = new AlertNotification(); Map<String, String> properties = new HashMap<String, String>(); properties.put(AmbariSNMPDispatcher.PORT_PROPERTY, "162"); properties.put(AmbariSNMPDispatcher.SNMP_VERSION_PROPERTY, "SNMPv3"); properties.put(AmbariSNMPDispatcher.SECURITY_USERNAME_PROPERTY, "USER"); properties.put(AmbariSNMPDispatcher.SECURITY_AUTH_PASSPHRASE_PROPERTY, "PASSPHRASE1"); properties.put(AmbariSNMPDispatcher.SECURITY_PRIV_PASSPHRASE_PROPERTY, "PASSPHRASE2"); properties.put(AmbariSNMPDispatcher.SECURITY_LEVEL_PROPERTY, "INCORRECT"); notification.DispatchProperties = properties; Recipient rec1 = new Recipient(); rec1.Identifier = "192.168.0.2"; notification.Recipients = Arrays.asList(rec1); doReturn(trap).when(dispatcher).prepareTrap(notification, snmpVersion); dispatcher.sendTraps(notification, snmpVersion); }