Search in sources :

Example 1 with TreeEvent

use of org.snmp4j.util.TreeEvent in project nifi by apache.

the class GetSNMP method onTriggerSnmp.

/**
 * Delegate method to supplement
 * {@link #onTrigger(ProcessContext, ProcessSession)}. It is implemented by
 * sub-classes to perform {@link Processor} specific functionality.
 *
 * @param context
 *            instance of {@link ProcessContext}
 * @param processSession
 *            instance of {@link ProcessSession}
 * @throws ProcessException Process exception
 */
@Override
protected void onTriggerSnmp(ProcessContext context, ProcessSession processSession) throws ProcessException {
    if ("GET".equals(context.getProperty(SNMP_STRATEGY).getValue())) {
        final ResponseEvent response = this.targetResource.get();
        if (response.getResponse() != null) {
            FlowFile flowFile = processSession.create();
            PDU pdu = response.getResponse();
            flowFile = SNMPUtils.updateFlowFileAttributesWithPduProperties(pdu, flowFile, processSession);
            flowFile = SNMPUtils.addAttribute(SNMPUtils.SNMP_PROP_PREFIX + "textualOid", context.getProperty(TEXTUAL_OID).getValue(), flowFile, processSession);
            processSession.getProvenanceReporter().receive(flowFile, this.snmpTarget.getAddress().toString() + "/" + context.getProperty(OID).getValue());
            if (pdu.getErrorStatus() == PDU.noError) {
                processSession.transfer(flowFile, REL_SUCCESS);
            } else {
                processSession.transfer(flowFile, REL_FAILURE);
            }
        } else {
            this.getLogger().error("Get request timed out or parameters are incorrect.");
            context.yield();
        }
    } else if ("WALK".equals(context.getProperty(SNMP_STRATEGY).getValue())) {
        final List<TreeEvent> events = this.targetResource.walk();
        if ((events != null) && !events.isEmpty() && (events.get(0).getVariableBindings() != null)) {
            FlowFile flowFile = processSession.create();
            for (TreeEvent treeEvent : events) {
                flowFile = SNMPUtils.updateFlowFileAttributesWithTreeEventProperties(treeEvent, flowFile, processSession);
            }
            processSession.getProvenanceReporter().receive(flowFile, this.snmpTarget.getAddress().toString() + "/" + context.getProperty(OID).getValue());
            processSession.transfer(flowFile, REL_SUCCESS);
        } else {
            this.getLogger().error("Get request timed out or parameters are incorrect.");
            context.yield();
        }
    }
}
Also used : PDU(org.snmp4j.PDU) TreeEvent(org.snmp4j.util.TreeEvent) FlowFile(org.apache.nifi.flowfile.FlowFile) ResponseEvent(org.snmp4j.event.ResponseEvent) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with TreeEvent

use of org.snmp4j.util.TreeEvent in project nifi by apache.

the class SNMPGetter method walk.

/**
 * Perform a SNMP walk and returns the list of {@link TreeEvent}
 * @return the list of {@link TreeEvent}
 */
public List<TreeEvent> walk() {
    TreeUtils treeUtils = new TreeUtils(this.snmp, new DefaultPDUFactory());
    @SuppressWarnings("unchecked") List<TreeEvent> events = treeUtils.getSubtree(this.target, this.oid);
    return events;
}
Also used : TreeEvent(org.snmp4j.util.TreeEvent) DefaultPDUFactory(org.snmp4j.util.DefaultPDUFactory) TreeUtils(org.snmp4j.util.TreeUtils)

Aggregations

TreeEvent (org.snmp4j.util.TreeEvent)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 PDU (org.snmp4j.PDU)1 ResponseEvent (org.snmp4j.event.ResponseEvent)1 DefaultPDUFactory (org.snmp4j.util.DefaultPDUFactory)1 TreeUtils (org.snmp4j.util.TreeUtils)1