/** * Process the received events. For each event, use the EventExpander to look up matching * eventconf entry and load info from that match, expand event parms, add event to database and * send event to appropriate listeners. */ @Override public void run() { Events events = m_eventLog.getEvents(); if (events == null || events.getEventCount() <= 0) { // no events to process return; } for (final Event event : events.getEventCollection()) { final ThreadCategory log = log(); if (log.isDebugEnabled()) { // Log the uei, source, and other important aspects final String uuid = event.getUuid(); log.debug("Event {"); log.debug(" uuid = " + (uuid != null && uuid.length() > 0 ? uuid : "<not-set>")); log.debug(" uei = " + event.getUei()); log.debug(" src = " + event.getSource()); log.debug(" iface = " + event.getInterface()); log.debug(" time = " + event.getTime()); if (event.getParmCollection().size() > 0) { log.debug(" parms {"); for (final Parm parm : event.getParmCollection()) { if ((parm.getParmName() != null) && (parm.getValue().getContent() != null)) { log.debug( " (" + parm.getParmName().trim() + ", " + parm.getValue().getContent().trim() + ")"); } } log.debug(" }"); } log.debug("}"); } for (final EventProcessor eventProcessor : m_eventProcessors) { try { eventProcessor.process(m_eventLog.getHeader(), event); } catch (SQLException e) { log.warn( "Unable to process event using processor " + eventProcessor + "; not processing with any later processors. Exception: " + e, e); break; } catch (Throwable t) { log.warn( "Unknown exception processing event with processor " + eventProcessor + "; not processing with any later processors. Exception: " + t, t); break; } } } }
/** * Format each parameter * * @param parm the parameter * @return the formatted event parameter string */ public static String format(Parm parm) { Value pValue = parm.getValue(); String type = pValue.getType(); String encoding = pValue.getEncoding(); String tmp = Constants.escape(parm.getParmName(), Constants.NAME_VAL_DELIM); String name = Constants.escape(tmp, Constants.MULTIPLE_VAL_DELIM); tmp = Constants.escape(pValue.getContent(), Constants.NAME_VAL_DELIM); String value = Constants.escape(tmp, Constants.MULTIPLE_VAL_DELIM); String empty = ""; name = (name != null ? name.trim() : empty); value = (value != null ? value.trim() : empty); type = (type != null ? type.trim() : empty); encoding = (encoding != null ? encoding.trim() : empty); StringBuffer buf = new StringBuffer(); buf.append(name); buf.append(Constants.NAME_VAL_DELIM); buf.append(value); buf.append('('); buf.append(type); buf.append(Constants.DB_ATTRIB_DELIM); buf.append(encoding); buf.append(')'); return buf.toString(); // return name + Constants.NAME_VAL_DELIM + value + "(" + type + // Constants.DB_ATTRIB_DELIM + encoding + ")"; }
private boolean isReloadConfigEventTarget(final Event event) { boolean isTarget = false; for (final Parm parm : event.getParmCollection()) { if (EventConstants.PARM_DAEMON_NAME.equals(parm.getParmName()) && ("Provisiond." + NAME).equalsIgnoreCase(parm.getValue().getContent())) { isTarget = true; break; } } log() .debug( "isReloadConfigEventTarget: Provisiond." + NAME + " was target of reload event: " + isTarget); return isTarget; }
/** * matchNotificationParameters * * @param event a {@link org.opennms.netmgt.xml.event.Event} object. * @param notification a {@link org.opennms.netmgt.config.notifications.Notification} object. * @return a boolean. */ public boolean matchNotificationParameters(Event event, Notification notification) { ThreadCategory log = ThreadCategory.getInstance(getClass()); boolean parmmatch = false; if (notification.getVarbind() != null && notification.getVarbind().getVbname() != null) { String notfValue = null; String notfName = notification.getVarbind().getVbname(); if (notification.getVarbind().getVbvalue() != null) { notfValue = notification.getVarbind().getVbvalue(); } else { if (log.isDebugEnabled()) { log.debug( "BroadcastEventProcessor:matchNotificationParameters: Null value for varbind, assuming true."); } parmmatch = true; } for (final Parm parm : event.getParmCollection()) { final String parmName = parm.getParmName(); final Value parmValue = parm.getValue(); final String parmContent; if (parmValue == null) { continue; } else { parmContent = parmValue.getContent(); } if (parmName.equals(notfName) && parmContent.startsWith(notfValue)) { parmmatch = true; } } } else if (notification.getVarbind() == null || notification.getVarbind().getVbname() == null) { parmmatch = true; } return parmmatch; }
/** * Format the list of event parameters * * @param event TODO * @return the formatted event parameters string */ public static String format(final Event event) { if (event == null || event.getParmCollection() == null || event.getParmCollection().size() == 0) { return null; } boolean first = true; StringBuffer parmbuf = new StringBuffer(); for (final Parm parm : event.getParmCollection()) { if (parm.getParmName() != null && parm.getValue() != null && parm.getValue().getContent() != null) { if (!first) { parmbuf.append(Constants.MULTIPLE_VAL_DELIM); } parmbuf.append(format(parm)); first = false; } } return parmbuf.toString(); }
private static String fetchSnmpAssetString( final SnmpAgentConfig agentConfig, final MibObjs mibObjs, final String formatString) throws MissingFormatArgumentException { final List<String> aliases = new ArrayList<String>(); final List<SnmpObjId> objs = new ArrayList<SnmpObjId>(); for (final MibObj mibobj : mibObjs.getMibObj()) { aliases.add(mibobj.getAlias()); objs.add(SnmpObjId.get(mibobj.getOid())); } // Fetch the values from the SNMP agent final SnmpValue[] values = SnmpUtils.get(agentConfig, objs.toArray(new SnmpObjId[0])); if (values.length == aliases.size()) { final Properties substitutions = new Properties(); boolean foundAValue = false; for (int i = 0; i < values.length; i++) { // If the value is a NO_SUCH_OBJECT or NO_SUCH_INSTANCE error, then skip it if (values[i] == null || values[i].isError()) { // No value for this OID continue; } foundAValue = true; // Use trapd's SyntaxToEvent parser so that we format base64 // and MAC address values appropriately Parm parm = SyntaxToEvent.processSyntax(aliases.get(i), values[i]); substitutions.setProperty(aliases.get(i), parm.getValue().getContent()); } if (!foundAValue) { if (log().isDebugEnabled()) { log() .debug( "fetchSnmpAssetString: Failed to fetch any SNMP values for system " + agentConfig.toString()); } throw new MissingFormatArgumentException( "fetchSnmpAssetString: Failed to fetch any SNMP values for system " + agentConfig.toString()); } else { log() .debug( "fetchSnmpAssetString: Fetched asset properties from SNMP agent:\n" + formatPropertiesAsString(substitutions)); } if (objs.size() != substitutions.size()) { log() .warn( "fetchSnmpAssetString: Unexpected number of properties returned from SNMP GET:\n" + formatPropertiesAsString(substitutions)); } return PropertiesUtils.substitute(formatString, substitutions); } else { log() .warn( "fetchSnmpAssetString: Invalid number of SNMP parameters returned: " + values.length + " != " + aliases.size()); throw new MissingFormatArgumentException( "fetchSnmpAssetString: Invalid number of SNMP parameters returned: " + values.length + " != " + aliases.size()); } }
/** * Create an SNMP V2 trap, based on the content of the specified event, and forward the trap to * the specified address and port. It is assumed that the specified event represents an SNMP V1 or * V2 trap that was received by OpenNMS (TrapD). * * @param event The event upon which the trap content should be based * @param destAddr The address to which the trap should be forwarded * @param destPort The port to which the trap should be forwarded * @exception Throws SnmpTrapHelperException if the variable binding cannot be added to the trap * for any reason. * @throws org.opennms.netmgt.scriptd.helper.SnmpTrapHelperException if any. */ public void forwardV2Trap(Event event, String destAddr, int destPort) throws SnmpTrapHelperException { // the event must correspond to an SNMP trap Snmp snmpInfo = event.getSnmp(); if (snmpInfo == null) { throw new SnmpTrapHelperException( "Cannot forward an event with no SNMP info: " + event.getUei()); } // check the version of the original trap String version = snmpInfo.getVersion(); SnmpTrapBuilder packet = SnmpUtils.getV2TrapBuilder(); if ("v1".equals(version)) { // converting V1 trap to V2 (see RFC2576) addVarBinding( packet, SNMP_SYSUPTIME_OID, EventConstants.TYPE_SNMP_TIMETICKS, Long.toString(snmpInfo.getTimeStamp())); String oid; if (snmpInfo.getGeneric() == ENTERPRISE_SPECIFIC && snmpInfo.hasSpecific()) { oid = snmpInfo.getId() + ".0." + snmpInfo.getSpecific(); } else { oid = SNMP_TRAPS + '.' + (snmpInfo.getGeneric() + 1); } addVarBinding(packet, SNMP_TRAP_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, oid); // add the V1 var bindings boolean addrPresent = false; boolean communityPresent = false; boolean enterprisePresent = false; int i = 0; for (Parm parm : event.getParmCollection()) { Value value = parm.getValue(); try { addVarBinding( packet, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent()); } catch (SnmpTrapHelperException e) { throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]"); } if (SNMP_TRAP_ADDRESS_OID.equals(parm.getParmName())) { addrPresent = true; } else if (SNMP_TRAP_COMMUNITY_OID.equals(parm.getParmName())) { communityPresent = true; } else if (SNMP_TRAP_ENTERPRISE_OID.equals(parm.getParmName())) { enterprisePresent = true; } i++; } if (!addrPresent) { addVarBinding( packet, SNMP_TRAP_ADDRESS_OID, EventConstants.TYPE_SNMP_IPADDRESS, event.getSnmphost()); } if (!communityPresent) { addVarBinding( packet, SNMP_TRAP_COMMUNITY_OID, EventConstants.TYPE_SNMP_OCTET_STRING, snmpInfo.getCommunity()); } if (!enterprisePresent) { addVarBinding( packet, SNMP_TRAP_ENTERPRISE_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, snmpInfo.getId()); } } else if ("v2".equals(version)) { addVarBinding( packet, SNMP_SYSUPTIME_OID, EventConstants.TYPE_SNMP_TIMETICKS, Long.toString(snmpInfo.getTimeStamp())); String oid; if (snmpInfo.getGeneric() == ENTERPRISE_SPECIFIC) { oid = snmpInfo.getId() + "." + snmpInfo.getSpecific(); } else { oid = SNMP_TRAPS + '.' + (snmpInfo.getGeneric() + 1); } addVarBinding(packet, SNMP_TRAP_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, oid); int i = 0; for (Parm parm : event.getParmCollection()) { Value value = parm.getValue(); try { addVarBinding( packet, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent()); } catch (SnmpTrapHelperException e) { throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]"); } i++; } } else { throw new SnmpTrapHelperException("Invalid SNMP version: " + version); } // send the trap sendTrap(destAddr, destPort, snmpInfo.getCommunity(), packet); }
/** * Create an SNMP V1 trap, based on the content of the specified event, and forward the trap to * the specified address and port. It is assumed that the specified event represents an SNMP V1 or * V2 trap that was received by OpenNMS (TrapD). * * @param event The event upon which the trap content should be based * @param destAddr The address to which the trap should be forwarded * @param destPort The port to which the trap should be forwarded * @exception Throws SnmpTrapHelperException if the variable binding cannot be added to the trap * for any reason. * @throws org.opennms.netmgt.scriptd.helper.SnmpTrapHelperException if any. */ public void forwardV1Trap(Event event, String destAddr, int destPort) throws SnmpTrapHelperException { // the event must correspond to an SNMP trap Snmp snmpInfo = event.getSnmp(); if (snmpInfo == null) { throw new SnmpTrapHelperException( "Cannot forward an event with no SNMP info: " + event.getUei()); } // check the version of the original trap String version = snmpInfo.getVersion(); SnmpV1TrapBuilder trap = SnmpUtils.getV1TrapBuilder(); if ("v1".equals(version)) { trap.setEnterprise(SnmpObjId.get(snmpInfo.getId())); InetAddress agentAddress; agentAddress = InetAddressUtils.addr(event.getSnmphost()); if (agentAddress == null) { throw new SnmpTrapHelperException("Invalid ip address."); } trap.setAgentAddress(agentAddress); if (snmpInfo.hasGeneric()) { trap.setGeneric(snmpInfo.getGeneric()); } if (snmpInfo.hasSpecific()) { trap.setSpecific(snmpInfo.getSpecific()); } trap.setTimeStamp(snmpInfo.getTimeStamp()); // varbinds int i = 0; for (Parm parm : event.getParmCollection()) { try { Value value = parm.getValue(); addVarBinding( trap, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent()); } catch (SnmpTrapHelperException e) { throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]"); } finally { i++; } } } else if ("v2".equals(version)) { // converting V2 trap to V1 (see RFC2576) trap.setEnterprise(SnmpObjId.get(snmpInfo.getId())); String addr = null; for (Parm parm : event.getParmCollection()) { if (SNMP_TRAP_ADDRESS_OID.equals(parm.getParmName())) { addr = parm.getValue().getContent(); break; } } if (addr == null) { addr = "0.0.0.0"; } InetAddress agentAddress; agentAddress = InetAddressUtils.addr(addr); if (agentAddress == null) { throw new SnmpTrapHelperException("Invalid ip address."); } trap.setAgentAddress(agentAddress); trap.setGeneric(snmpInfo.getGeneric()); trap.setSpecific(snmpInfo.getSpecific()); trap.setTimeStamp(snmpInfo.getTimeStamp()); // varbinds int i = 0; for (Parm parm : event.getParmCollection()) { Value value = parm.getValue(); // omit any parms with type=Counter64 if (!(EventConstants.TYPE_SNMP_COUNTER64.equals(value.getType()))) { try { addVarBinding( trap, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent()); } catch (SnmpTrapHelperException e) { throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]"); } } i++; } } else { throw new SnmpTrapHelperException("Invalid SNMP version: " + version); } // send the trap sendTrap(destAddr, destPort, snmpInfo.getCommunity(), trap); }