stingh711 avatar stingh711 committed 5407528

update trap sender by add all traps defined in our mib

Comments (0)

Files changed (4)

config.properties

 interface.table.name.3=wan3
 interface.table.description.3=wan3
 interface.table.type.3=2
+
+#traps parameters
+server.ip=172.25.21.69
+nsTrapVarAdmin=sting
+nsTrapVarPort=10
+nsTrapVarBandwidth=134234
+nsTrapVarHostname=172.25.21.25
+nsTrapVarMac=00:a0:c5:11:11:11
+nsTrapVarPacketRate=123343
+nsTrapVarAttackCount=1000
+nsTrapVarAttackRate=212/s
+nsTrapVarAttackTime=3333
+

src/com/zyxel/enc/netbar/agent/Main.java

 
 public class Main {
 
-	public static void main(String[] args) {
+    public static void main(String[] args) {
         BasicConfigurator.configure();
-        
-        if (args.length == 1 && args[0].equals("sendtraps")) {
-        	sendTraps();
+
+        if (args.length == 1 && args[0].equals("trap")) {
+            sendTraps();
         } else {
-        	startAgent();
+            startAgent();
         }
-	}
+    }
 
-	private static void sendTraps() {
-		
-	}
-	
-	private static void startAgent() {
-		String address = Config.get("agent.ip");
-        System.out.println(address);
+    private static void sendTraps() {
+        TrapSender sender = new TrapSender(Config.get("server.ip"));
+        sender.sendAllTraps();
+    }
+
+    private static void startAgent() {
         Agent agent = new Agent(Config.get("agent.ip"));
         agent.run();
-	}
-
+    }
 }

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

 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.log4j.Logger;
 
 import org.snmp4j.CommunityTarget;
 import org.snmp4j.PDU;
 import org.snmp4j.mp.SnmpConstants;
 import org.snmp4j.smi.Address;
 import org.snmp4j.smi.GenericAddress;
+import org.snmp4j.smi.Integer32;
 import org.snmp4j.smi.IpAddress;
 import org.snmp4j.smi.OID;
+import org.snmp4j.smi.OctetString;
+import org.snmp4j.smi.VariableBinding;
 import org.snmp4j.transport.DefaultUdpTransportMapping;
 import org.snmp4j.util.DefaultPDUFactory;
 
 public class TrapSender {
-	private Snmp snmp;
-	private Address address;
-	
-	public TrapSender(String ip) {
-		address = GenericAddress.parse(String.format("udp:%s/162", ip));
-		TransportMapping transport;
-		try {
-			transport = new DefaultUdpTransportMapping();
-			snmp = new Snmp(transport);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-	}
-	
-	public void sendTrap(boolean isGeneric, OID oid) {
-		CommunityTarget target = new CommunityTarget();
-		target.setAddress(address);
-		target.setRetries(2);
-		target.setTimeout(1500);
-		target.setVersion(SnmpConstants.version1);
-		
-		PDUv1 pdu = (PDUv1) DefaultPDUFactory.createPDU(SnmpConstants.version1);
-		pdu.setType(PDU.V1TRAP);
-		if (isGeneric) {
-			pdu.setGenericTrap(SnmpConstants.getGenericTrapID(oid));
-		} else {
-			pdu.setEnterprise(oid);
-		}
-		
-		try {
-			pdu.setAgentAddress(new IpAddress(InetAddress.getLocalHost()));
-			snmp.send(pdu, target);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-	}
+    private static final Logger logger = Logger.getLogger("TrapSender");
+    private static final OID nsTrapLoginFail = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.1");
+    private static final OID nsTrapPortUpFull = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.2");
+    private static final OID nsTrapPortUpHarf = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.3");
+    private static final OID nsTrapPortDown = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.4");
+    private static final OID nsTrapWarmStart = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.5");
+    private static final OID nsTrapSetScheduleStart = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.6");
+    private static final OID nsTrapCancelScheduleStart = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.7");
+    private static final OID nsTrapArpScan = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.8");
+    private static final OID nsTrapDDosAttack = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.9");
+    private static final OID nsTrapWanAttack = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.10");
+    private static final OID nsTrapWanAttackLast = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.11");
+    private static final OID nsTrapWanAttackEnd = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.12");
+    private static final OID nsTrapPppoeAbnormal = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.13");
+    private static final OID nsTrapPppoeAuthFail = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.14");
+    private static final OID nsTrapPppoeGetIPFail = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.15");
+    private static final OID nsTrapLinkup = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.16");
+    private static final OID nsTrapLinkdown = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.17");
+    private static final OID nsTrapBandwidthHigh60 = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.18");
+    private static final OID nsTrapBandwidthHigh80 = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.19");
+    private static final OID nsTrapBandwidthHigh90 = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.20");
+    private static final OID nsTrapSessionHigh60 = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.21");
+    private static final OID nsTrapSessionHigh80 = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.22");
+    private static final OID nsTrapSessionHigh90 = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.23");
+    private static final OID nsTrapCPUHigh60 = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.24");
+    private static final OID nsTrapCPUHigh80 = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.25");
+    private static final OID nsTrapCPUHigh90 = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.26");
+    private static final OID nsTrapPowerFault = new OID("1.3.6.1.4.1.890.1.5.17.1.12.2.27");
+    private static final OID[] oids = new OID[]{nsTrapLoginFail, nsTrapPortUpFull, nsTrapPortUpHarf,
+        nsTrapPortDown, nsTrapWarmStart, nsTrapSetScheduleStart, nsTrapCancelScheduleStart,
+        nsTrapArpScan, nsTrapDDosAttack, nsTrapWanAttack, nsTrapWanAttackLast,
+        nsTrapWanAttackEnd, nsTrapPppoeAbnormal, nsTrapPppoeAuthFail, nsTrapPppoeGetIPFail,
+        nsTrapLinkup, nsTrapLinkdown, nsTrapBandwidthHigh60, nsTrapBandwidthHigh80,
+        nsTrapBandwidthHigh90, nsTrapSessionHigh60, nsTrapSessionHigh80, nsTrapSessionHigh90,
+        nsTrapCPUHigh60, nsTrapCPUHigh80, nsTrapCPUHigh90, nsTrapPowerFault};
+    //OIDs for trap variables
+    private static final OID nsTrapVarAdmin = new OID("1.3.6.1.4.1.890.1.5.17.1.12.3.2");
+    private static final OID nsTrapVarPort = new OID("1.3.6.1.4.1.890.1.5.17.1.12.3.3");
+    private static final OID nsTrapVarBandwidth = new OID("1.3.6.1.4.1.890.1.5.17.1.12.3.4");
+    private static final OID nsTrapVarHostname = new OID("1.3.6.1.4.1.890.1.5.17.1.12.3.5");
+    private static final OID nsTrapVarMac = new OID("1.3.6.1.4.1.890.1.5.17.1.12.3.6");
+    private static final OID nsTrapVarPacketRate = new OID("1.3.6.1.4.1.890.1.5.17.1.12.3.7");
+    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) {
+                pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
+            }
+        });
+        variableAdders.put(nsTrapPortUpFull, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 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) {
+                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) {
+                pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
+            }
+        });
+        variableAdders.put(nsTrapWarmStart, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 pdu) {
+                pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
+            }
+        });
+        variableAdders.put(nsTrapSetScheduleStart, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 pdu) {
+                pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
+            }
+        });
+        variableAdders.put(nsTrapCancelScheduleStart, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 pdu) {
+                pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
+            }
+        });
+        variableAdders.put(nsTrapArpScan, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 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) {
+                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) {
+                pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
+            }
+        });
+        variableAdders.put(nsTrapWanAttackLast, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 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) {
+                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) {
+                pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
+            }
+        });
+        variableAdders.put(nsTrapPppoeAuthFail, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 pdu) {
+                pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
+            }
+        });
+        variableAdders.put(nsTrapPppoeGetIPFail, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 pdu) {
+                pdu.add(new VariableBinding(nsTrapVarAdmin, new OctetString(Config.get("nsTrapVarAdmin"))));
+            }
+        });
+        variableAdders.put(nsTrapLinkup, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 pdu) {
+                pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
+            }
+        });
+        variableAdders.put(nsTrapLinkdown, new VariableAdder() {
+            @Override
+            public void addVariableToPdu(PDUv1 pdu) {
+                pdu.add(new VariableBinding(nsTrapVarPort, new Integer32(Integer.valueOf(Config.get("nsTrapVarPort")))));
+            }
+        });
+    }
+    
+    private Snmp snmp;
+    private Address address;
+    private CommunityTarget target;
+
+    public TrapSender(String ip) {
+        address = GenericAddress.parse(String.format("udp:%s/162", ip));
+        TransportMapping transport;
+        try {
+            transport = new DefaultUdpTransportMapping();
+            snmp = new Snmp(transport);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        target = new CommunityTarget();
+        target.setAddress(address);
+        target.setRetries(2);
+        target.setTimeout(1500);
+        target.setVersion(SnmpConstants.version1);
+    }
+
+    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);
+            }
+        }
+
+        try {
+            pdu.setAgentAddress(new IpAddress(InetAddress.getLocalHost()));
+            snmp.send(pdu, target);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void sendAllTraps() {
+        logger.info("Begin to send traps");
+        for (OID oid : oids) {
+            sendTrap(false, oid);
+        }
+        logger.info("All traps has been sent out");
+    }
+
+    public static void main(String[] args) throws Exception {
+        TrapSender sender = new TrapSender("172.25.21.69");
+        sender.sendAllTraps();
+    }
+
+    private static interface VariableAdder {
+
+        void addVariableToPdu(PDUv1 pdu);
+    }
 }
 set classpath=%path%;agent.jar;log4j.jar;snmp4j.jar;snmp4j-agent.jar
-java com/zyxel/enc/netbar/agent/Main
+java com/zyxel/enc/netbar/agent/Main %1
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.