Send SNMP Trap notifications
TrapAgent class is used to hide Socket operations from users and provide an easy method to send Trap notifications. To use the class, you can use the TrapAgent class protocol specific members, recommended when you expect to send a lot of notifications, or a static helper TrapAgent.SendTrap method which will construct a new socket for each call.
Ejemplo n.º 1
0
        /// <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.");
            }
        }
Ejemplo n.º 2
0
    /// <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);


    }
Ejemplo n.º 3
0
 /// <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.");
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 读取根节点
        /// </summary>
        /// <param name="Ip">ip地址</param>
        /// <param name="outinfo">输出到那个字典</param>
        public void SnmpAgent(Dictionary<string, List<string>> Ip_OID, Dictionary<string, Dictionary<string, string>> outinfo)
        {
            #region 新方法

            foreach (KeyValuePair<string, List<string>> MyKVP in Ip_OID)
            {
                try
                {
                    SimpleSnmp snmp = new SimpleSnmp(MyKVP.Key, "public");
                    if (!snmp.Valid)
                    {
                        continue;
                    }
                    Dictionary<string, string> Info = new Dictionary<string, string>();
                    VbCollection col = new VbCollection();
                    TrapAgent agent = new TrapAgent();
                    foreach (string bc in MyKVP.Value)
                    {
                        Dictionary<Oid, AsnType> result = snmp.Walk(SnmpVersion.Ver2, bc);
                        if (result == null)
                        {
                            continue;
                        }
                        foreach (KeyValuePair<Oid, AsnType> kvp in result)
                        {
                            try
                            {
                                if (!Info.ContainsKey(kvp.Key.ToString()))
                                {
                                    Info.Add(kvp.Key.ToString(), kvp.Value.ToString());
                                }
                                else
                                {
                                    Info.Remove(kvp.Key.ToString());
                                    Info.Add(kvp.Key.ToString(), kvp.Value.ToString());
                                }

                            }
                            catch (Exception ex)
                            {
                                msg.SaveTempMessage(ex.Source.ToString(), ex.Message.ToString(), DateTime.Now.ToString());
                            }
                        }
                    }
                    if (!outinfo.ContainsKey(MyKVP.Key))
                    {
                        outinfo.Add(MyKVP.Key, Info);
                    }
                    else
                    {
                        outinfo.Remove(MyKVP.Key);
                        outinfo[MyKVP.Key] = Info;
                    }
                }
                catch (Exception ex)
                {
                    msg.SaveTempMessage(ex.Source.ToString(), ex.Message.ToString(), DateTime.Now.ToString());
                }
            }
            #endregion
        }
Ejemplo n.º 5
0
 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;
     }
 }