mapping = new HashMap<String, Object>(); String defaultMapping = ""; mapping.put("ackUser", nullSafeToString(alarm.getAckUser(), defaultMapping)); mapping.put("appDn", nullSafeToString(alarm.getAppDn(), defaultMapping)); mapping.put("logMsg", nullSafeToString(alarm.getLogMsg(), defaultMapping)); mapping.put("description", nullSafeToString(alarm.getDesc(), defaultMapping)); mapping.put("objectInstance", nullSafeToString(alarm.getObjectInstance(), defaultMapping)); mapping.put("objectType", nullSafeToString(alarm.getObjectType(), defaultMapping)); mapping.put("ossKey", nullSafeToString(alarm.getOssKey(),defaultMapping)); mapping.put("ossState", nullSafeToString(alarm.getOssState(), defaultMapping)); mapping.put("ticketId", nullSafeToString(alarm.getTicketId(), defaultMapping)); mapping.put("ticketState", nullSafeToString(alarm.getTicketState(), defaultMapping)); mapping.put("alarmUei", nullSafeToString(alarm.getUei(), defaultMapping)); mapping.put("alarmKey", nullSafeToString(alarm.getAlarmKey(), defaultMapping)); mapping.put("clearKey", nullSafeToString(alarm.getClearKey(), defaultMapping)); mapping.put("operInstruct", nullSafeToString(alarm.getOperInst(), defaultMapping)); mapping.put("ackTime", nullSafeToString(alarm.getAckTime(), defaultMapping)); AlarmType alarmType = alarm.getAlarmType() == null ? AlarmType.NOTIFICATION : alarm.getAlarmType(); mapping.put("alarmType", alarmType.name()); String count = alarm.getCount() == null ? "1" : alarm.getCount().toString(); mapping.put("count", count); mapping.put("alarmId", alarm.getId().toString()); mapping.put("ipAddr", nullSafeToString(alarm.getIpAddr(), defaultMapping)); if (dateFormat != null) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat); if (alarm.getFirstOccurrence() != null) { mapping.put("firstOccurrence", simpleDateFormat.format(alarm.getFirstOccurrence()));
/** * Each implementation of the AbstractNorthbounder has a nice queue (Nagle's algorithmic) and the worker thread that processes the queue * calls this method to send alarms to the northern NMS. * * @param alarms the alarms * @throws NorthbounderException the northbounder exception */ @Override public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException { if (alarms == null) { String errorMsg = "No alarms in alarms list for drools forwarding."; NorthbounderException e = new NorthbounderException(errorMsg); LOG.error(errorMsg, e); throw e; } LOG.info("Forwarding {} alarms to engine {}", alarms.size(), m_engine.getName()); alarms.forEach(a -> { LOG.debug("Begin correlation for alarm {} uei: {}", a.getId(), a.getUei()); m_kieSession.insert(a); LOG.debug("End correlation for alarm {} uei: {}", a.getId(), a.getUei()); }); }
/** * Builds an XML representation of parameter mappings. * @param alarm the alarm * @param mapping the mapping */ private void buildParmMappingXml(final NorthboundAlarm alarm, final Map<String, Object> mapping) { List<OnmsEventParameter> parms = alarm.getEventParametersCollection(); EventParms eventParms = new EventParms(parms); try { JAXBElement<EventParms> rootElement = new JAXBElement<EventParms>(new QName("eventParms"), EventParms.class, eventParms); StringWriter sw = new StringWriter(); Marshaller marshaller = ONMS_EVENT_PARM_CONTEXT.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.marshal(rootElement, sw); LOG.debug("Adding eventParmsXML mapping with contents {}.", sw); mapping.put("eventParmsXml", sw); } catch (JAXBException e) { LOG.error("Error marshalling event params to XML for alarm ID: {}", alarm.getId(), e); } }
@Override public boolean accepts(NorthboundAlarm alarm) { if (!m_config.isEnabled()) { LOG.warn("JMS Northbounder {} is currently disabled, rejecting alarm {}.", getName(), alarm.getUei()); return false; } LOG.debug("Validating UEI of alarm: {}", alarm.getUei()); if (getConfig().getUeis() == null || getConfig().getUeis().contains(alarm.getUei())) { LOG.debug("UEI: {}, accepted.", alarm.getUei()); return true; } LOG.debug("UEI: {}, rejected.", alarm.getUei()); return false; }
Integer count = alarm.getCount(); if (count > 1 && m_destination.isFirstOccurrenceOnly()) { LOG.debug("Destination {} is configured for new alarm instances only. Alarm has count of {}.", m_destination.getName(), count); continue; LOG.debug("Creating formatted log message for alarm: {}.", alarm.getId()); String syslogMessage; int level; try { LOG.debug("Making substitutions for tokens in message format for alarm: {}.", alarm.getId()); String msgFormat = m_destination.getCustomMessageFormat(alarm); if (msgFormat == null) { syslogMessage = PropertiesUtils.substitute(msgFormat, createMapping(alarm, dateFormat)); LOG.debug("Determining LOG_LEVEL for alarm: {}", alarm.getId()); level = SyslogUtils.determineLogLevel(alarm.getSeverity()); LOG.debug("Forwarding alarm: {} via syslog to destination: {}", alarm.getId(), m_destination.getName()); instance.log(level, syslogMessage);
/** * Convert alarm to text. * * @param alarm the alarm * @return the string */ private String convertAlarmToText(NorthboundAlarm alarm) { String alarmXml = null; Map<String, Object> mapping = createMapping(alarm, null); LOG.debug("Making substitutions for tokens in message format for alarm: {}.", alarm.getId()); if (m_jmsDestination.getMessageFormat() != null) { alarmXml = PropertiesUtils.substitute(m_jmsDestination.getMessageFormat(), mapping); } else { alarmXml = PropertiesUtils.substitute(m_config.getMessageFormat(), mapping); } return alarmXml; }
@Override public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException { for (final NorthboundAlarm alarm : alarms) { final Integer count = alarm.getCount(); LOG.debug("Does destination {} take only first occurances? {} Is new alarm? Has count of {}.", m_jmsDestination.getName(), m_jmsDestination.isFirstOccurrenceOnly(), count); if(count > 1 && m_jmsDestination.isFirstOccurrenceOnly()) { LOG.debug("Skipping because not new alarm."); continue; } LOG.debug("Attempting to send a message to {} of type {}", m_jmsDestination.getJmsDestination(), m_jmsDestination.getDestinationType()); try { m_template.send(m_jmsDestination.getJmsDestination(), new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { if (m_jmsDestination.isSendAsObjectMessageEnabled()) { return session.createObjectMessage(alarm); } else { return session.createTextMessage(convertAlarmToText(alarm)); } } }); LOG.debug("Sent message"); } catch (JmsException e) { LOG.error("Unable to send alarm to northbound JMS because {}", e.getLocalizedMessage(), e); } } }
@Override public boolean accepts(NorthboundAlarm alarm) { if (m_config.getAcceptableUeis() == null || m_config.getAcceptableUeis().contains(alarm.getUei())) { return true; } return false; }
/** * Builds an XML representation of parameter mappings. * @param alarm the alarm * @param mapping the mapping */ private void buildParmMappingXml(final NorthboundAlarm alarm, final Map<String, Object> mapping) { List<OnmsEventParameter> parms = alarm.getEventParametersCollection(); EventParms eventParms = new EventParms(parms); try { JAXBElement<EventParms> rootElement = new JAXBElement<EventParms>(new QName("eventParms"), EventParms.class, eventParms); StringWriter sw = new StringWriter(); Marshaller marshaller = ONMS_EVENT_PARM_CONTEXT.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.marshal(rootElement, sw); LOG.debug("Adding eventParmsXML mapping with contents {}.", sw); mapping.put("eventParmsXml", sw); } catch (JAXBException e) { LOG.error("Error marshalling event params to XML for alarm ID: {}", alarm.getId(), e); } }
mapping = new HashMap<String, Object>(); String defaultMapping = ""; mapping.put("ackUser", nullSafeToString(alarm.getAckUser(), defaultMapping)); mapping.put("appDn", nullSafeToString(alarm.getAppDn(), defaultMapping)); mapping.put("logMsg", nullSafeToString(alarm.getLogMsg(), defaultMapping)); mapping.put("description", nullSafeToString(alarm.getDesc(), defaultMapping)); mapping.put("objectInstance", nullSafeToString(alarm.getObjectInstance(), defaultMapping)); mapping.put("objectType", nullSafeToString(alarm.getObjectType(), defaultMapping)); mapping.put("ossKey", nullSafeToString(alarm.getOssKey(),defaultMapping)); mapping.put("ossState", nullSafeToString(alarm.getOssState(), defaultMapping)); mapping.put("ticketId", nullSafeToString(alarm.getTicketId(), defaultMapping)); mapping.put("ticketState", nullSafeToString(alarm.getTicketState(), defaultMapping)); mapping.put("alarmUei", nullSafeToString(alarm.getUei(), defaultMapping)); mapping.put("alarmKey", nullSafeToString(alarm.getAlarmKey(), defaultMapping)); mapping.put("clearKey", nullSafeToString(alarm.getClearKey(), defaultMapping)); mapping.put("operInstruct", nullSafeToString(alarm.getOperInst(), defaultMapping)); mapping.put("ackTime", nullSafeToString(alarm.getAckTime(), defaultMapping)); AlarmType alarmType = alarm.getAlarmType() == null ? AlarmType.NOTIFICATION : alarm.getAlarmType(); mapping.put("alarmType", alarmType.name()); String count = alarm.getCount() == null ? "1" : alarm.getCount().toString(); mapping.put("count", count); mapping.put("alarmId", alarm.getId().toString()); mapping.put("ipAddr", nullSafeToString(alarm.getIpAddr(), defaultMapping)); if (dateFormat != null) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat); if (alarm.getFirstOccurrence() != null) { mapping.put("firstOccurrence", simpleDateFormat.format(alarm.getFirstOccurrence()));
/** * The abstraction makes a call here to determine if the alarm should be placed on the queue of alarms to be sent northerly. * * @param alarm the alarm * @return true, if successful */ @Override public boolean accepts(NorthboundAlarm alarm) { if (!initialized) { LOG.warn("BSF Northbounder {} has not been properly initialized, rejecting alarm {}.", getName(), alarm.getUei()); return false; } if (!getConfig().isEnabled()) { LOG.warn("BSF Northbounder {} is currently disabled, rejecting alarm {}.", getName(), alarm.getUei()); return false; } LOG.debug("Validating UEI of alarm: {}", alarm.getUei()); if (getConfig().getUeis() == null || getConfig().getUeis().contains(alarm.getUei())) { LOG.debug("UEI: {}, accepted.", alarm.getUei()); boolean passed = m_engine.accepts(alarm); LOG.debug("Filters: {}, passed ? {}.", alarm.getUei(), passed); return passed; } LOG.debug("UEI: {}, rejected.", alarm.getUei()); return false; }
/** * The abstraction makes a call here to determine if the alarm should be placed on the queue of alarms to be sent northerly. * * @param alarm the alarm * @return true, if successful */ @Override public boolean accepts(NorthboundAlarm alarm) { if (!initialized) { LOG.warn("BSF Northbounder {} has not been properly initialized, rejecting alarm {}.", getName(), alarm.getUei()); return false; } if (!getConfig().isEnabled()) { LOG.warn("BSF Northbounder {} is currently disabled, rejecting alarm {}.", getName(), alarm.getUei()); return false; } LOG.debug("Validating UEI of alarm: {}", alarm.getUei()); if (getConfig().getUeis() == null || getConfig().getUeis().contains(alarm.getUei())) { LOG.debug("UEI: {}, accepted.", alarm.getUei()); boolean passed = m_engine.accepts(alarm); LOG.debug("Filters: {}, passed ? {}.", alarm.getUei(), passed); return passed; } LOG.debug("UEI: {}, rejected.", alarm.getUei()); return false; }
/** * The abstraction makes a call here to determine if the alarm should be placed on the queue of alarms to be sent northerly. * * @param alarm the alarm * @return true, if successful */ @Override public boolean accepts(NorthboundAlarm alarm) { if (!initialized) { LOG.warn("Email Northbounder {} has not been properly initialized, rejecting alarm {}.", getName(), alarm.getUei()); return false; } if (!getConfig().isEnabled()) { LOG.warn("Email Northbounder {} is currently disabled, rejecting alarm {}.", getName(), alarm.getUei()); return false; } LOG.debug("Validating UEI of alarm: {}", alarm.getUei()); if (getConfig().getUeis() == null || getConfig().getUeis().contains(alarm.getUei())) { LOG.debug("UEI: {}, accepted.", alarm.getUei()); boolean passed = m_destination.accepts(alarm); LOG.debug("Filters: {}, passed ? {}.", alarm.getUei(), passed); return passed; } LOG.debug("UEI: {}, rejected.", alarm.getUei()); return false; }
/** * The abstraction makes a call here to determine if the alarm should be placed on the queue of alarms to be sent northerly. * * @param alarm the alarm * @return true, if successful */ @Override public boolean accepts(NorthboundAlarm alarm) { if (!initialized) { LOG.warn("SNMP Trap Northbounder {} has not been properly initialized, rejecting alarm {}.", getName(), alarm.getUei()); return false; } if (!getConfig().isEnabled()) { LOG.warn("SNMP Trap Northbounder {} is currently disabled, rejecting alarm {}.", getName(), alarm.getUei()); return false; } LOG.debug("Validating UEI of alarm: {}", alarm.getUei()); if (getConfig().getUeis() == null || getConfig().getUeis().contains(alarm.getUei())) { LOG.debug("UEI: {}, accepted.", alarm.getUei()); boolean passed = m_trapSink.accepts(alarm); LOG.debug("Filters: {}, passed ? {}.", alarm.getUei(), passed); return passed; } LOG.debug("UEI: {}, rejected.", alarm.getUei()); return false; }
/** * The abstraction makes a call here to determine if the alarm should be placed on the queue of alarms to be sent northerly. * * @param alarm the alarm * @return true, if successful */ @Override public boolean accepts(NorthboundAlarm alarm) { if (!initialized) { LOG.warn("Drools Northbounder {} has not been properly initialized, rejecting alarm {}.", getName(), alarm.getUei()); return false; } if (!getConfig().isEnabled()) { LOG.warn("Drools Northbounder {} is currently disabled, rejecting alarm {}.", getName(), alarm.getUei()); return false; } LOG.debug("Validating UEI of alarm: {}", alarm.getUei()); if (getConfig().getUeis() == null || getConfig().getUeis().contains(alarm.getUei())) { LOG.debug("UEI: {}, accepted.", alarm.getUei()); boolean passed = m_engine.accepts(alarm); LOG.debug("Filters: {}, passed ? {}.", alarm.getUei(), passed); return passed; } LOG.debug("UEI: {}, rejected.", alarm.getUei()); return false; }
/** * The abstraction makes a call here to determine if the alarm should be placed on the queue of alarms to be sent northerly. * * @param alarm the alarm * @return true, if successful */ @Override public boolean accepts(NorthboundAlarm alarm) { if (!initialized) { LOG.warn("Email Northbounder {} has not been properly initialized, rejecting alarm {}.", getName(), alarm.getUei()); return false; } if (!getConfig().isEnabled()) { LOG.warn("Email Northbounder {} is currently disabled, rejecting alarm {}.", getName(), alarm.getUei()); return false; } LOG.debug("Validating UEI of alarm: {}", alarm.getUei()); if (getConfig().getUeis() == null || getConfig().getUeis().contains(alarm.getUei())) { LOG.debug("UEI: {}, accepted.", alarm.getUei()); boolean passed = m_destination.accepts(alarm); LOG.debug("Filters: {}, passed ? {}.", alarm.getUei(), passed); return passed; } LOG.debug("UEI: {}, rejected.", alarm.getUei()); return false; }
/** * The abstraction makes a call here to determine if the alarm should be * placed on the queue of alarms to be sent northerly. * * @param alarm the alarm * @return true, if successful */ @Override public boolean accepts(NorthboundAlarm alarm) { if (!initialized) { LOG.warn("Syslog Northbounder {} has not been properly initialized, rejecting alarm {}.", getName(), alarm.getUei()); return false; } if (!getConfig().isEnabled()) { LOG.warn("Syslog Northbounder {} is currently disabled, rejecting alarm {}.", getName(), alarm.getUei()); return false; } LOG.debug("Validating UEI of alarm: {}", alarm.getUei()); if (getConfig().getUeis() == null || getConfig().getUeis().contains(alarm.getUei())) { LOG.debug("UEI: {}, accepted.", alarm.getUei()); if (m_destination.getFilters() == null) { return true; } boolean passed = m_destination.passFilter(alarm); LOG.debug("Filters: {}, passed ? {}.", alarm.getUei(), passed); return passed; } LOG.debug("UEI: {}, rejected.", alarm.getUei()); return false; }
/** * Evaluates a SPEL expression based on a given northbound alarm * * @param expression the expression * @param alarm the northbound alarm * @return the string */ private String evaluate(String expression, NorthboundAlarm alarm) { if (expression == null) { return null; } try { StandardEvaluationContext context = new StandardEvaluationContext(alarm); ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression(expression); return (String) exp.getValue(context, String.class); } catch (Exception e) { LOG.warn("Can't evaluate expression {} for alarm {} because: {}", getValue(), alarm.getUei(), e.getMessage()); } return null; } }
/** * Accepts. * <p>If the engine doesn't have filter, the method will return true.</p> * <p>If the method has a filter, it will be evaluated.</p> * * @param alarm the alarm * @return true, if successful */ public boolean accepts(NorthboundAlarm alarm) { if (getFilter() != null) { StandardEvaluationContext context = new StandardEvaluationContext(alarm); ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression(m_filter); boolean passed = false; try { passed = (Boolean)exp.getValue(context, Boolean.class); } catch (Exception e) { LOG.warn("accepts: can't evaluate expression {} for alarm {} because: {}", getFilter(), alarm.getUei(), e.getMessage()); } LOG.debug("accepts: checking {} ? {}", m_filter, passed); return passed; } return true; }