Commits

stingh711  committed efc7fd9

change trap from v1 to v2c

  • Participants
  • Parent commits e4418aa

Comments (0)

Files changed (1)

File src/com/zyxel/enc/netbar/agent/TrapSender.java

 import org.snmp4j.smi.IpAddress;
 import org.snmp4j.smi.OID;
 import org.snmp4j.smi.OctetString;
+import org.snmp4j.smi.TimeTicks;
 import org.snmp4j.smi.VariableBinding;
 import org.snmp4j.transport.DefaultUdpTransportMapping;
 import org.snmp4j.util.DefaultPDUFactory;
     private static final OID nsTrapVarAttackCount = new OID("1.3.6.1.4.1.890.1.5.17.1.12.3.8");
     private static final OID nsTrapVarAttackRate = new OID("1.3.6.1.4.1.890.1.5.17.1.12.3.9");
     private static final OID nsTrapVarAttackTime = new OID("1.3.6.1.4.1.890.1.5.17.1.12.3.10");
-    
+
     private static final Map<OID, VariableAdder> variableAdders = new HashMap<OID, VariableAdder>();
-    
+
     static {
         variableAdders.put(nsTrapLoginFail, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
             }
         });
         variableAdders.put(nsTrapPortUpFull, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
                 pdu.add(new VariableBinding(nsTrapVarBandwidth, new Integer32(Integer.valueOf(Config.get("nsTrapVarBandwidth")))));
             }
         });
         variableAdders.put(nsTrapPortUpHarf, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
                 pdu.add(new VariableBinding(nsTrapVarBandwidth, new Integer32(Integer.valueOf(Config.get("nsTrapVarBandwidth")))));
             }
         });
         variableAdders.put(nsTrapPortDown, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
             }
         });
         variableAdders.put(nsTrapWarmStart, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
             }
         });
         variableAdders.put(nsTrapSetScheduleStart, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
             }
         });
         variableAdders.put(nsTrapCancelScheduleStart, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
             }
         });
         variableAdders.put(nsTrapArpScan, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarHostname, new OctetString(Config.get("nsTrapVarHostname"))));
                 pdu.add(new VariableBinding(nsTrapVarMac, new OctetString(Config.get("nsTrapVarMac"))));
                 pdu.add(new VariableBinding(nsTrapVarPacketRate, new OctetString(Config.get("nsTrapVarPacketRate"))));
         });
         variableAdders.put(nsTrapDDosAttack, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarHostname, new OctetString(Config.get("nsTrapVarHostname"))));
                 pdu.add(new VariableBinding(nsTrapVarMac, new OctetString(Config.get("nsTrapVarMac"))));
                 pdu.add(new VariableBinding(nsTrapVarAttackCount, new Integer32(Integer.valueOf(Config.get("nsTrapVarAttackCount")))));
         });
         variableAdders.put(nsTrapWanAttack, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
             }
         });
         variableAdders.put(nsTrapWanAttackLast, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarAttackCount, new Integer32(Integer.valueOf(Config.get("nsTrapVarAttackCount")))));
                 pdu.add(new VariableBinding(nsTrapVarAttackRate, new OctetString(Config.get("nsTrapVarAttackRate"))));
             }
         });
         variableAdders.put(nsTrapWanAttackEnd, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarAttackTime, new Integer32(Integer.valueOf(Config.get("nsTrapVarAttackTime")))));
                 pdu.add(new VariableBinding(nsTrapVarAttackCount, new Integer32(Integer.valueOf(Config.get("nsTrapVarAttackCount")))));
                 pdu.add(new VariableBinding(nsTrapVarAttackRate, new OctetString(Config.get("nsTrapVarAttackRate"))));
         });
         variableAdders.put(nsTrapPppoeAbnormal, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
             }
         });
         variableAdders.put(nsTrapPppoeAuthFail, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
             }
         });
         variableAdders.put(nsTrapPppoeGetIPFail, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
             }
         });
         variableAdders.put(nsTrapLinkup, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
             }
         });
         variableAdders.put(nsTrapLinkdown, new VariableAdder() {
             @Override
-            public void addVariableToPdu(PDUv1 pdu) {
+            public void addVariableToPdu(PDU pdu) {
                 pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
             }
         });
     }
-    
+
     private Snmp snmp;
     private Address address;
     private CommunityTarget target;
             transport = new DefaultUdpTransportMapping();
             snmp = new Snmp(transport);
         } catch (IOException e) {
-            e.printStackTrace();
+            throw new RuntimeException(e);
         }
 
         target = new CommunityTarget();
         target.setAddress(address);
         target.setRetries(2);
         target.setTimeout(1500);
-        target.setVersion(SnmpConstants.version1);
+        target.setVersion(SnmpConstants.version2c);
     }
 
-    public void sendTrap(boolean isGeneric, OID oid) {
-        PDUv1 pdu = (PDUv1) DefaultPDUFactory.createPDU(SnmpConstants.version1);
-        pdu.setType(PDU.V1TRAP);
-        if (isGeneric) {
-            pdu.setGenericTrap(SnmpConstants.getGenericTrapID(oid));
-        } else {
-            pdu.setGenericTrap(6);
-            pdu.setEnterprise(oid);
-            if (variableAdders.containsKey(oid)) {
-                variableAdders.get(oid).addVariableToPdu(pdu);
-            }
+    public void sendV2Trap(OID oid) {
+        PDU pdu = DefaultPDUFactory.createPDU(SnmpConstants.version2c);
+        pdu.setType(PDU.TRAP);
+
+        TimeTicks sysUpTime = new TimeTicks((long)(System.currentTimeMillis()/1000));
+        pdu.add(new VariableBinding(SnmpConstants.sysUpTime, sysUpTime));
+        pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
+        if (variableAdders.containsKey(oid)) {
+            variableAdders.get(oid).addVariableToPdu(pdu);
         }
-
         try {
-            pdu.setAgentAddress(new IpAddress(InetAddress.getLocalHost()));
             snmp.send(pdu, target);
         } catch (IOException e) {
-            e.printStackTrace();
+            throw new RuntimeException(e);
         }
     }
 
     public void sendAllTraps() {
         logger.info("Begin to send traps");
         for (OID oid : oids) {
-            sendTrap(false, oid);
+            sendV2Trap(oid);
         }
         logger.info("All traps has been sent out");
     }
 
     private static interface VariableAdder {
 
-        void addVariableToPdu(PDUv1 pdu);
+        void addVariableToPdu(PDU pdu);
     }
 }