/** * Executes the SNMP set request and returns the response * @param pdu PDU to send * @return Response event * @throws IOException IO Exception */ public ResponseEvent set(PDU pdu) throws IOException { return this.snmp.set(pdu, this.target); }
/** * Construct the PDU to perform the SNMP Get request and returns * the result in order to create the flow file. * @return {@link ResponseEvent} */ public ResponseEvent get() { try { PDU pdu = null; if(this.target.getVersion() == SnmpConstants.version3) { pdu = new ScopedPDU(); } else { pdu = new PDU(); } pdu.add(new VariableBinding(this.oid)); pdu.setType(PDU.GET); return this.snmp.get(pdu, this.target); } catch (IOException e) { logger.error("Failed to get information from SNMP agent; " + this, e); throw new ProcessException(e); } }
/** * Closes {@link Snmp} created when instance of this class was created. */ @Override public void close() throws TimeoutException, IOException { if (logger.isDebugEnabled()) { logger.debug("Closing SNMP connection"); } this.snmp.close(); }
TransportMapping transMap = new DefaultUdpTransportMapping(); snmp = new Snmp( transMap ); UdpAddress udpAddress = new UdpAddress( InetAddress.getByName( servername ), nrPort ); ResponseEvent response = null; if ( targettype.equals( target_type_Code[0] ) ) { CommunityTarget target = new CommunityTarget(); PDUv1 pdu1 = new PDUv1(); transMap.listen(); target.setCommunity( new OctetString( community ) ); target.setVersion( SnmpConstants.version1 ); target.setAddress( udpAddress ); if ( target.getAddress().isValid() ) { if ( log.isDebug() ) { logDebug( "Valid IP address" ); target.setRetries( retry ); target.setTimeout( timeOut ); pdu1.setGenericTrap( 6 ); pdu1.setSpecificTrap( PDUv1.ENTERPRISE_SPECIFIC ); pdu1.setEnterprise( new OID( Oid ) ); pdu1.add( new VariableBinding( new OID( Oid ), new OctetString( messageString ) ) ); response = snmp.send( pdu1, target ); UserTarget usertarget = new UserTarget();
public static void main(String[] args) throws Exception { // Create PDU PDU trap = new PDU(); trap.setType(PDU.TRAP); OID oid = new OID("1.2.3.4.5"); trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid)); trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000))); // put your uptime here trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description"))); //Add Payload Variable var = new OctetString("some string"); trap.add(new VariableBinding(oid, var)); // Specify receiver Address targetaddress = new UdpAddress("10.101.21.32/162"); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setVersion(SnmpConstants.version2c); target.setAddress(targetaddress); // Send Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); snmp.send(trap, target, null, null); }
/** * Builds target resource. * @param context Process context */ private void buildTargetResource(ProcessContext context) { if((this.transportMapping == null) || !this.transportMapping.isListening() || (this.snmp == null)) { try { this.transportMapping = new DefaultUdpTransportMapping(); this.snmp = new Snmp(this.transportMapping); if("SNMPv3".equals(context.getProperty(SNMP_VERSION).getValue())) { USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(usm); } this.transportMapping.listen(); } catch (Exception e) { throw new IllegalStateException("Failed to initialize UDP transport mapping", e); } } if (this.snmpTarget == null) { this.snmpTarget = this.createSnmpTarget(context); } if (this.targetResource == null) { this.targetResource = this.finishBuildingTargetResource(context); } }
final String authProtocol = context.getProperty(SNMP_AUTH_PROTOCOL).getValue(); final String privProtocol = context.getProperty(SNMP_PRIV_PROTOCOL).getValue(); OctetString aPwd = authPassword != null ? new OctetString(authPassword) : null; OctetString pPwd = privPassword != null ? new OctetString(privPassword) : null; this.snmp.getUSM().addUser(new OctetString(username), new UsmUser(new OctetString(username), SNMPUtils.getAuth(authProtocol), aPwd, SNMPUtils.getPriv(privProtocol), pPwd)); result = new UserTarget(); ((UserTarget) result).setSecurityLevel(SNMPUtils.getSecLevel(context.getProperty(SNMP_SECURITY_LEVEL).getValue())); final String securityName = context.getProperty(SNMP_SECURITY_NAME).getValue(); if(securityName != null) { ((UserTarget) result).setSecurityName(new OctetString(securityName)); result = new CommunityTarget(); String community = context.getProperty(SNMP_COMMUNITY).getValue(); if(community != null) { ((CommunityTarget) result).setCommunity(new OctetString(community)); result.setVersion(version); result.setAddress(new UdpAddress(context.getProperty(HOST).getValue() + "/" + context.getProperty(PORT).getValue())); result.setRetries(context.getProperty(SNMP_RETRIES).asInteger()); result.setTimeout(context.getProperty(SNMP_TIMEOUT).asInteger());
public SnmpHelper(String address, String community) { _target = new CommunityTarget(); _target.setCommunity(new OctetString(community)); _target.setVersion(SnmpConstants.version2c); _target.setAddress(new UdpAddress(address)); try { _snmp = new Snmp(new DefaultUdpTransportMapping()); } catch (IOException e) { _snmp = null; throw new CloudRuntimeException(" Error in crearting snmp object, " + e.getMessage()); } }
private PDU createPDU(SnmpTrapInfo snmpTrapInfo) { PDU trap = new PDU(); trap.setType(PDU.TRAP); trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(sysUpTimeTicks))); trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, getOID(CsSnmpConstants.TRAPS_PREFIX + alertType))); if (snmpTrapInfo.getDataCenterId() != 0) { trap.add(new VariableBinding(getOID(CsSnmpConstants.DATA_CENTER_ID), new UnsignedInteger32(snmpTrapInfo.getDataCenterId()))); trap.add(new VariableBinding(getOID(CsSnmpConstants.POD_ID), new UnsignedInteger32(snmpTrapInfo.getPodId()))); trap.add(new VariableBinding(getOID(CsSnmpConstants.CLUSTER_ID), new UnsignedInteger32(snmpTrapInfo.getClusterId()))); trap.add(new VariableBinding(getOID(CsSnmpConstants.MESSAGE), new OctetString(snmpTrapInfo.getMessage()))); } else { throw new CloudRuntimeException(" What is the use of alert without message "); trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME), new OctetString(snmpTrapInfo.getGenerationTime().toString()))); } else { trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME)));
if(this.snmpTarget.getVersion() == SnmpConstants.version3) { pdu = new ScopedPDU(); } else { pdu = new PDU(); pdu.setType(PDU.SET); try { ResponseEvent response = this.targetResource.set(pdu); if(response.getResponse() == null) { processSession.transfer(processSession.penalize(flowFile), REL_FAILURE); this.getLogger().error("Set request timed out or parameters are incorrect."); context.yield(); } else if(response.getResponse().getErrorStatus() == PDU.noError) { flowFile = SNMPUtils.updateFlowFileAttributesWithPduProperties(pdu, flowFile, processSession); processSession.transfer(flowFile, REL_SUCCESS); processSession.getProvenanceReporter().send(flowFile, this.snmpTarget.getAddress().toString()); } else { final String error = response.getResponse().getErrorStatusText(); flowFile = SNMPUtils.addAttribute(SNMPUtils.SNMP_PROP_PREFIX + "error", error, flowFile, processSession); processSession.transfer(processSession.penalize(flowFile), REL_FAILURE); this.getLogger().error("Failed while executing SNMP Set [{}] via " + this.targetResource + ". Error = {}", new Object[]{response.getRequest().getVariableBindings(), error});
Variable var = null; if (splits.length == 2) { // no SMI syntax defined var = new OctetString(snmpPropValue); } else { int smiSyntax = Integer.valueOf(splits[2]); VariableBinding varBind = new VariableBinding(new OID(snmpPropName), var); pdu.add(varBind); result = true;
private void test() { boolean testOK = false; String errMsg = null; String hostname = jobMeta.environmentSubstitute( wServerName.getText() ); int nrPort = Const.toInt( jobMeta.environmentSubstitute( "" + wPort.getText() ), SyslogDefs.DEFAULT_PORT ); try { UdpAddress udpAddress = new UdpAddress( InetAddress.getByName( hostname ), nrPort ); UserTarget usertarget = new UserTarget(); usertarget.setAddress( udpAddress ); testOK = usertarget.getAddress().isValid(); if ( !testOK ) { errMsg = BaseMessages.getString( PKG, "JobEntrySyslog.CanNotGetAddress", hostname ); } } catch ( Exception e ) { errMsg = e.getMessage(); } if ( testOK ) { MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_INFORMATION ); mb.setMessage( BaseMessages.getString( PKG, "JobEntrySyslog.Connected.OK", hostname ) + Const.CR ); mb.setText( BaseMessages.getString( PKG, "JobEntrySyslog.Connected.Title.Ok" ) ); mb.open(); } else { MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR ); mb.setMessage( BaseMessages.getString( PKG, "JobEntrySyslog.Connected.NOK.ConnectionBad", hostname ) + Const.CR + errMsg + Const.CR ); mb.setText( BaseMessages.getString( PKG, "JobEntrySyslog.Connected.Title.Bad" ) ); mb.open(); } }
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 { if((events != null) && !events.isEmpty() && (events.get(0).getVariableBindings() != null)) { FlowFile flowFile = processSession.create(); for (TreeEvent treeEvent : events) { this.snmpTarget.getAddress().toString() + "/" + context.getProperty(OID).getValue()); processSession.transfer(flowFile, REL_SUCCESS); } else {
/** * Will close current SNMP mapping. */ @OnStopped public void close() { try { if (this.targetResource != null) { this.targetResource.close(); } } catch (Exception e) { this.getLogger().warn("Failure while closing target resource " + this.targetResource, e); } this.targetResource = null; try { if (this.transportMapping != null) { this.transportMapping.close(); } } catch (IOException e) { this.getLogger().warn("Failure while closing UDP transport mapping", e); } this.transportMapping = null; try { if (this.snmp != null) { this.snmp.close(); } } catch (IOException e) { this.getLogger().warn("Failure while closing UDP transport mapping", e); } this.snmp = null; }
public void sendSnmpTrap(SnmpTrapInfo snmpTrapInfo) { try { if (_snmp != null) { _snmp.send(createPDU(snmpTrapInfo), _target, null, null); } } catch (IOException e) { throw new CloudRuntimeException(" Error in sending SNMP Trap, " + e.getMessage()); } }
/** * Adds a <code>TransportMapping</code> to this SNMP session. * @param transportMapping * a <code>TransportMapping</code> instance. */ public void addTransportMapping(TransportMapping<? extends Address> transportMapping) { // connect transport mapping with message dispatcher messageDispatcher.addTransportMapping(transportMapping); transportMapping.addTransportListener(messageDispatcher); }
/** * Removes the specified transport mapping from this SNMP session. * If the transport mapping is not currently part of this SNMP session, * this method will have no effect. * @param transportMapping * a previously added <code>TransportMapping</code>. */ public void removeTransportMapping(TransportMapping<? extends Address> transportMapping) { messageDispatcher.removeTransportMapping(transportMapping); transportMapping.removeTransportListener(messageDispatcher); }
private void test() { boolean testOK = false; String errMsg = null; String hostname = transMeta.environmentSubstitute( wServerName.getText() ); int nrPort = Const.toInt( transMeta.environmentSubstitute( "" + wPort.getText() ), SyslogDefs.DEFAULT_PORT ); try { UdpAddress udpAddress = new UdpAddress( InetAddress.getByName( hostname ), nrPort ); UserTarget usertarget = new UserTarget(); usertarget.setAddress( udpAddress ); testOK = usertarget.getAddress().isValid(); if ( !testOK ) { errMsg = BaseMessages.getString( PKG, "SyslogMessageDialog.CanNotGetAddress", hostname ); } } catch ( Exception e ) { errMsg = e.getMessage(); } if ( testOK ) { MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_INFORMATION ); mb.setMessage( BaseMessages.getString( PKG, "SyslogMessageDialog.Connected.OK", hostname ) + Const.CR ); mb.setText( BaseMessages.getString( PKG, "SyslogMessageDialog.Connected.Title.Ok" ) ); mb.open(); } else { MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR ); mb.setMessage( BaseMessages.getString( PKG, "SyslogMessageDialog.Connected.NOK.ConnectionBad", hostname ) + Const.CR + errMsg + Const.CR ); mb.setText( BaseMessages.getString( PKG, "SyslogMessageDialog.Connected.Title.Bad" ) ); mb.open(); } }
/** * Adds a <code>TransportMapping</code> to this SNMP session. * * @param transportMapping * a <code>TransportMapping</code> instance. */ public void addTransportMapping(TransportMapping<? extends Address> transportMapping) { // connect transport mapping with message dispatcher messageDispatcher.addTransportMapping(transportMapping); transportMapping.addTransportListener(messageDispatcher); }
private void test() { boolean testOK = false; String errMsg = null; String hostname = jobMeta.environmentSubstitute( wServerName.getText() ); int nrPort = Const.toInt( jobMeta.environmentSubstitute( "" + wPort.getText() ), JobEntrySNMPTrap.DEFAULT_PORT ); try { UdpAddress udpAddress = new UdpAddress( InetAddress.getByName( hostname ), nrPort ); UserTarget usertarget = new UserTarget(); usertarget.setAddress( udpAddress ); testOK = usertarget.getAddress().isValid(); if ( !testOK ) { errMsg = BaseMessages.getString( PKG, "JobSNMPTrap.CanNotGetAddress", hostname ); } } catch ( Exception e ) { errMsg = e.getMessage(); } if ( testOK ) { MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_INFORMATION ); mb.setMessage( BaseMessages.getString( PKG, "JobSNMPTrap.Connected.OK", hostname ) + Const.CR ); mb.setText( BaseMessages.getString( PKG, "JobSNMPTrap.Connected.Title.Ok" ) ); mb.open(); } else { MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR ); mb.setMessage( BaseMessages.getString( PKG, "JobSNMPTrap.Connected.NOK.ConnectionBad", hostname ) + Const.CR + errMsg + Const.CR ); mb.setText( BaseMessages.getString( PKG, "JobSNMPTrap.Connected.Title.Bad" ) ); mb.open(); } }