Beispiel #1
0
        //復旧トラップ
        public int recoverexecute(Class_keepResult res_cls)
        {
            TrapAgent agent   = new TrapAgent();
            string    oid_str = "";
            // Variable Binding collection to send with the trap
            VbCollection col = new VbCollection();

            oid_str = oid;
            //oidが取得できない
            if (oid_str == "" || oid_str == null)
            {
                return(-1);
            }

            //varbind
            col.Add(new Oid(oid_str + ".1"), new Integer32(0));

            // Send the trap to the localhost port 162
            //1.3.6.1.2.1.1.2.0
            agent.SendV1Trap(new IpAddress(trapServer), recieverport, communityname,
                             new Oid(oid_str), new IpAddress(res_cls.address), SnmpConstants.EnterpriseSpecific, 10, 0, col);

            //正常時
            return(1);
        }
Beispiel #2
0
        public Form1()
        {
            InitializeComponent();
            _Application = this;

            snmp_agent = new SNMP_Agent("127.0.0.1", "public");
            IP         = IPAddress.Parse("127.0.0.1");
            AddressTextBox.ReadOnly = true;

            OperationsComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            OperationsComboBox.Items.Add("Get");
            OperationsComboBox.Items.Add("Get Next");
            OperationsComboBox.Items.Add("Get Table");
            AddressTextBox.Text = "localhost";
            stopReciverToolStripMenuItem.Enabled = false;


            imageList.Images.Add(Properties.Resources.Folder);
            imageList.Images.Add(Properties.Resources.key);
            imageList.Images.Add(Properties.Resources.entry);
            imageList.Images.Add(Properties.Resources.listek);
            imageList.Images.Add(Properties.Resources.paper);
            imageList.Images.Add(Properties.Resources.table);

            mibElementsDictionary = new Dictionary <string, string>();
            loadMibTreeElements();

            initiationTrapSender();


            trapAgent = new TrapAgent();
        }
Beispiel #3
0
 public bool SendTrap(TrapAgent agent, String receiverIP, int port, String community, String oid,
                      String senderIP, int generic, int specific, uint senderUpTime, VbCollection col)
 {
     try
     {
         agent.SendV1Trap(new IpAddress(receiverIP), port, community,
                          new Oid(oid), new IpAddress(senderIP),
                          generic, specific, senderUpTime, col);
         return(true);
     }
     catch (Exception e)
     {
         return(false);
     }
 }
Beispiel #4
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);
     }
 }
Beispiel #5
0
        /*
         * private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
         * {
         *  Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
         *  IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
         *  EndPoint ep = (EndPoint)ipep;
         *  socket.Bind(ep);
         *  socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
         *  bool run = true;
         *  int inlen = -1;
         *
         *  while (run)
         *  {
         *      byte[] indata = new byte[16 * 1024];
         *      IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
         *      EndPoint inep = (EndPoint)peer;
         *      try
         *      {
         *          inlen = socket.ReceiveFrom(indata, ref inep);
         *      }
         *      catch (Exception ex)
         *      {
         *          Console.WriteLine("Exception {0}", ex.Message);
         *          inlen = -1;
         *      }
         *      if (inlen > 0)
         *      {
         *          // Check protocol version int
         *          int ver = SnmpPacket.GetProtocolVersion(indata, inlen);
         *          if (ver == (int)SnmpVersion.Ver1)
         *          {
         *              // Parse SNMP Version 1 TRAP packet
         *              SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
         *              pkt.decode(indata, inlen);
         *              string message = "";
         *              message += "** SNMP Version 1 TRAP received from: " + inep.ToString() + Environment.NewLine;
         *              message += "*** Trap generic: " + pkt.Pdu.Generic + Environment.NewLine;
         *              message += "*** Trap specific: " + pkt.Pdu.Specific + Environment.NewLine;
         *              message += "*** Agent address: " + pkt.Pdu.AgentAddress.ToString() + Environment.NewLine;
         *              message += "*** Timestamp: " + pkt.Pdu.TimeStamp.ToString() + Environment.NewLine;
         *              message += "*** VarBind count: " + pkt.Pdu.VbList.Count + Environment.NewLine;
         *              message += "*** VarBind content:" + Environment.NewLine;
         *              foreach (Vb v in pkt.Pdu.VbList)
         *              {
         *                  message += "**** " + v.Oid.ToString() + " " + SnmpConstants.GetTypeName(v.Value.Type) + " : " + v.Value.ToString() + Environment.NewLine;
         *              }
         *              message += "** End of SNMP Version 1 TRAP data.";
         *              MessageBox.Show(message, "TRAP", MessageBoxButtons.OK, MessageBoxIcon.Information,
         *                              MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000);
         *          }
         *          else
         *          {
         *              // Parse SNMP Version 2 TRAP packet
         *              SnmpV2Packet pkt = new SnmpV2Packet();
         *              pkt.decode(indata, inlen);
         *              string message = "";
         *              message += "** SNMP Version 2 TRAP received from: " +  inep.ToString() + Environment.NewLine;
         *              if ((SnmpSharpNet.PduType)pkt.Pdu.Type != PduType.V2Trap)
         *              {
         *                  Console.WriteLine("*** NOT an SNMPv2 trap ****");
         *              }
         *              else
         *              {
         *                  message += "*** Community: " + pkt.Community.ToString() + Environment.NewLine;
         *                  message += "*** VarBind count: " + pkt.Pdu.VbList.Count + Environment.NewLine;
         *                  message += "*** VarBind content:" + Environment.NewLine;
         *                  foreach (Vb v in pkt.Pdu.VbList)
         *                  {
         *                      message += "**** " + v.Oid.ToString() + " " + SnmpConstants.GetTypeName(v.Value.Type) + " : " + v.Value.ToString() + Environment.NewLine;
         *                  }
         *                  message += "** End of SNMP Version 2 TRAP data.";
         *                  MessageBox.Show(message, "TRAP", MessageBoxButtons.OK, MessageBoxIcon.Information,
         *                                  MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000);
         *              }
         *          }
         *      }
         *      else
         *      {
         *          if (inlen == 0)
         *              Console.WriteLine("Zero length packet received.");
         *      }
         *      System.Threading.Thread.Sleep(10);
         *  }
         * }*/

        private void trapButton_Click(object sender, EventArgs e)
        {
            TrapAgent agent = new TrapAgent();

            // Variable Binding collection to send with the trap
            VbCollection col = new VbCollection();

            col.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString("Test string"));
            col.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.9.1.1.0"));
            col.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324));
            col.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString("Milan"));

            // Send the trap to the localhost port 162
            agent.SendV1Trap(new IpAddress("localhost"), 162, "public",
                             new Oid("1.3.6.1.2.1.1.1.0"), new IpAddress("127.0.0.1"),
                             SnmpConstants.LinkUp, 0, 13432, col);
        }
Beispiel #6
0
        public static void SendTrap(string message, string ipAddressToSend, int portToSend)
        {
            TrapAgent agent = new TrapAgent();

            // Variable Binding collection to send with the trap
            VbCollection col = new VbCollection();

            col.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString(message));
            col.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.9.1.1.0"));
            col.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324));
            col.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString(DateTime.Now.ToString()));
            col.Add(new Oid("1.3.6.1.2.1.1.5.0"), new OctetString("1"));

            // Send the trap to the localhost port 162
            agent.SendV1Trap(new IpAddress(ipAddressToSend), portToSend, "public",
                             new Oid("1.3.6.1.2.1.1.1.0"), new IpAddress(Utils.GetLocalIPAddress()),
                             SnmpConstants.LinkDown, 0, 13432, col);
        }
Beispiel #7
0
        /// <summary>
        /// Send a trap at the IP specified.
        /// </summary>
        /// <param name="trapReceiverIp">IP of the recipient</param>
        /// <param name="trapReceiverPort">The port of the recipient</param>
        /// <param name="oidTrap"></param>
        /// <param name="oidCollection"></param>
        public static void SendTrap(string trapReceiverIp, int trapReceiverPort, Oid oidTrap, VbCollection oidCollection)
        {
            TrapAgent agent = new TrapAgent();

            agent.SendV2Trap(new IpAddress(trapReceiverIp), trapReceiverPort, AxisOidsHelper.Community, 13433, oidTrap, oidCollection);
        }
    /// <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);
    }