Example #1
0
        private static void GotTrap(Snmp snmp, Pdu pdu, SnmpTarget target, object cbData)
        {
            if ((int)cbData != CB_DATA_)
            {
                Console.Error.WriteLine("*** Invalid callback data!");
            }
            if (threadId_ == null)
            {
                threadId_ = Interlocked.Increment(ref nextId_);
            }

            // Use a lock if you want do not want messages from various threads
            // to be interleaved
            ManagerUtilities.PrintPdu(Console.Out,
                                      Now() + ": " + pdu.Type + " received from " + target,
                                      pdu, true, threadId_);
            Console.Out.WriteLine(
                "Enterprise: " + pdu.NotifyEnterprise
                + "\nNotify OID      : " + pdu.NotifyId
                + "\nV1 generic-trap : " + pdu.V1GenericTrap
                + "\nV1 specific-trap: " + pdu.V1SpecificTrap
                + "\nTimestamp        : " + pdu.NotifyTimestamp);
            if (pdu.Type == PduType.V1Trap)
            {
                Console.Out.WriteLine("v1TrapAddr: " + pdu.V1TrapAddress);
            }

            if (pdu.Type == PduType.Inform)
            {
                Vb vb = new Vb("1.3.6.1", new OctetStr("this is the response"));
                pdu = new Pdu(PduType.Response, vb);
                snmp.Invoke(pdu, target);
                ManagerUtilities.PrintPdu(Console.Out,
                                          "Response sent to " + target, pdu, true, threadId_);
            }

            // Long trap processing follows...
            Thread.Sleep(5000);
        }
Example #2
0
 private static void PrintPdu(TextWriter os, string text, Pdu pdu, bool debug)
 {
     ManagerUtilities.PrintPdu(os, text, pdu, debug, Thread.CurrentThread.Name);
 }
Example #3
0
        private void ResponseCallback(IAsyncResult ar)
        {
            if (ar.AsyncState != this)
            {
                throw new ArgumentException(
                          "Fatal: invalid data passed to callback");
            }

            using (MemoryManager.GetMemoryManager())
            {
                Pdu pdu;
                try
                {
                    pdu = snmp_.EndInvoke(ar);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e);
                    barrier_.Enter();
                    return;
                }

                bool show = (nRepd_ % 1000) == 0;
                if (show || debug_)
                {
                    ManagerUtilities.PrintPdu(Console.Out,
                                              "Callback PDU:", pdu, true, id_.ToString());
                }

                Pdu nextPdu = pdu_;
                if (operType_ == OperType.Walk)
                {
                    Vb  nextVb  = pdu[0];
                    Oid nextOid = nextVb.Oid;
                    Oid rootOid = pdu_[0].Oid;
                    if (nextOid.StartsWith(rootOid))
                    {
                        if (show)
                        {
                            SnmpSyntax val  = nextVb.Value;
                            SmiSyntax  type = val != null ? val.SmiSyntax : SmiSyntax.Null;
                            Console.WriteLine("[{0}]: {1},{2},{3}", id_, nextOid, val, type);
                        }

                        nRepd_--;
                        nextPdu = pdu_.Clone(new Vb(nextOid));
                    }
                }

                nCalls_++;
                nRepd_++;
                if (nRepd_ < nRepeats_)
                {
                    AsyncDoSnmp(snmp_, target_, nextPdu,
                                new AsyncCallback(ResponseCallback), this);
                }
                else
                {
                    barrier_.Enter();
                }
            }
        }