public SendV1Trap ( |
||
receiver | Receiver IP address | |
receiverPort | int | Receiver UDP port number |
community | string | SNMP community name |
senderSysObjectID | Oid | Senders sysObjectID |
senderIpAdress | Sender IP address | |
genericTrap | Generic trap code | |
specificTrap | Specific trap code | |
senderUpTime | Senders sysUpTime | |
varList | VbCollection | Variable binding list |
return | void |
/// <summary> /// Send SNMP Trap notification /// </summary> /// <remarks> /// Helper function to allow for seamless sending of SNMP notifications for all protocol versions. /// /// packet parameter should be appropriately formatted SNMP notification in SnmpV1TrapPacket, /// SnmpV2Packet or SnmpV3Packet class cast as SnmpPacket class. /// /// Function will determine which version of the notification is to be used by checking the type /// of the packet parameter and call appropriate TrapAgent member function to send it. /// </remarks> /// <param name="packet">SNMP trap packet</param> /// <param name="peer">Manager (receiver) IP address</param> /// <param name="port">Manager (receiver) UDP port number</param> public static void SendTrap(SnmpPacket packet, IpAddress peer, int port) { TrapAgent agent = new TrapAgent(); if (packet is SnmpV1TrapPacket) { agent.SendV1Trap((SnmpV1TrapPacket)packet, peer, port); } else if (packet is SnmpV2Packet) { agent.SendV2Trap((SnmpV2Packet)packet, peer, port); } else if (packet is SnmpV3Packet) { agent.SendV3Trap((SnmpV3Packet)packet, peer, port); } else { throw new SnmpException("Invalid SNMP packet type."); } }
/// <summary> /// Traps are only sent when using an agent and manager in the same system (localhost) /// under attack 0 /// not under attack anymore 1 /// found enemy 2 /// head bellow 50 3 /// head bellow 25 4 /// head bellow 0 5 /// legs bellow 50 6 /// legs bellow 25 7 /// legs bellow 0 8 /// </summary> /// <param name="trapCase"></param> public void sendTrap(int trapCase) { string underAttackOid = "1"; string foundEnemyOid = "2"; string headOid = "5.1"; string legsOid = "5.2"; string armsOid = "5.3"; TrapAgent agent = new TrapAgent(); // Variable Binding collection to send with the trap VbCollection col = new VbCollection(); switch (trapCase) { //under attack case 0: col.Add(new Oid("1.3.2.5." + underAttackOid), new Counter32((uint)metalGear.underAttack)); break; //not under attack anymore case 1: col.Add(new Oid("1.3.2.5." + underAttackOid), new Counter32((uint)metalGear.underAttack)); break; // found enemy case 2: col.Add(new Oid("1.3.2.5." + foundEnemyOid), new Counter32(1)); break; // head bellow 50 case 3: col.Add(new Oid("1.3.2.5." + headOid), new Counter32((uint)metalGear.HeadHealth)); break; // head bellow 25 case 4: col.Add(new Oid("1.3.2.5." + headOid), new Counter32((uint)metalGear.HeadHealth)); break; // head destroyed case 5: col.Add(new Oid("1.3.2.5." + headOid), new Counter32((uint)metalGear.HeadHealth)); break; // legs bellow 50 case 6: col.Add(new Oid("1.3.2.5." + legsOid), new Counter32((uint)metalGear.LegsHealth)); break; // legs bellow 25 case 7: col.Add(new Oid("1.3.2.5." + legsOid), new Counter32((uint)metalGear.LegsHealth)); break; // legs destroyed case 8: col.Add(new Oid("1.3.2.5." + legsOid), new Counter32((uint)metalGear.LegsHealth)); break; } Debug.Log("sending trap"); // Send the trap to the localhost port 162 agent.SendV1Trap(new IpAddress("127.0.0.1"), 16009, "public", new Oid("1.3.2.5.0"), new IpAddress("127.0.0.1"), SnmpConstants.LinkUp, 0, 13432, col); }
/// <summary> /// Send SNMP Trap notification /// </summary> /// <remarks> /// Helper function to allow for seamless sending of SNMP notifications for all protocol versions. /// /// packet parameter should be appropriately formatted SNMP notification in SnmpV1TrapPacket, /// SnmpV2Packet or SnmpV3Packet class cast as SnmpPacket class. /// /// Function will determine which version of the notification is to be used by checking the type /// of the packet parameter and call appropriate TrapAgent member function to send it. /// </remarks> /// <param name="packet">SNMP trap packet</param> /// <param name="peer">Manager (receiver) IP address</param> /// <param name="port">Manager (receiver) UDP port number</param> public static void SendTrap(SnmpPacket packet, IpAddress peer, int port) { TrapAgent agent = new TrapAgent(); if (packet is SnmpV1TrapPacket) agent.SendV1Trap((SnmpV1TrapPacket)packet, peer, port); else if (packet is SnmpV2Packet) agent.SendV2Trap((SnmpV2Packet)packet, peer, port); else if (packet is SnmpV3Packet) agent.SendV3Trap((SnmpV3Packet)packet, peer, port); else throw new SnmpException("Invalid SNMP packet type."); }
public bool SnmpTrapTest(string receiver, int receiverPort, string community, string senderSysObjectID, string senderIpAdress, VbCollection varList) { try { TrapAgent agent = new TrapAgent(); // Send the trap to the localhost port 162 agent.SendV1Trap(new IpAddress(receiver), receiverPort, community, new Oid(senderSysObjectID), new IpAddress(senderIpAdress), SnmpConstants.LinkUp, 0, 13432, varList); return true; } catch { return false; } }