Ejemplo n.º 1
0
        public static Hashtable getValues(Pdu pdu)
        {
            Hashtable htResult = new Hashtable();
            int       i        = 0;

            foreach (Vb vb in pdu)
            {
                i++;
                SnmpSyntax val  = vb.Value;
                SmiSyntax  type = val != null ? val.SmiSyntax : SmiSyntax.Null;


                Hashtable htElement = new Hashtable();

                //Transform val to a string
                StringWriter sw = new StringWriter();
                sw.Write("{0}", val);
                string myValue = sw.ToString();

                htElement.Add("value", myValue);
                htElement.Add("type", type.ToString());
                htElement.Add("oid", vb.Oid.ToString());
                htResult.Add(i, htElement);
            }
            return(htResult);
        }
Ejemplo n.º 2
0
        public static void PrintPdu(TextWriter os, string text,
                                    Pdu pdu, bool debug, object id)
        {
            lock (os)
            {
                os.WriteLine("[{0}]: {1}{2}", id, text, debug ? "\n" + pdu : "");

                // another way would be through Pdu.Vbs:
                // foreach (Vb vb in pdu.Vbs) {...}
                int i = 0;
                foreach (Vb vb in pdu)
                {
                    SnmpSyntax val  = vb.Value;
                    SmiSyntax  type = val != null ? val.SmiSyntax : SmiSyntax.Null;
                    os.WriteLine("oid [{0}]: {1}\nval [{0}]: {2} ({3})", i, vb.Oid, val, type);
                    i++;
                }
            }
        }
Ejemplo n.º 3
0
        private static void Walk(Snmp snmp, Pdu pdu, SnmpTarget target,
                                 bool asyncSync, bool show, bool debug, ref int ncalls)
        {
            Console.WriteLine("Rentre dans le Walk");
            string thName  = Thread.CurrentThread.Name;
            Oid    rootOid = pdu[0].Oid;

            while (true)
            {
                if (debug)
                {
                    PrintPdu(Console.Out, "Sending PDU to target " + target, pdu, debug);
                }

                Pdu resp = Invoke(snmp, pdu, target, asyncSync);
                ncalls++;

                Vb  nextVb  = resp[0];
                Oid nextOid = nextVb.Oid;
                if (!nextOid.StartsWith(rootOid))
                {
                    break;
                }

                if (debug)
                {
                    PrintPdu(Console.Out, "Received PDU:", resp, debug);
                }
                else
                if (show)
                {
                    SnmpSyntax val  = nextVb.Value;
                    SmiSyntax  type = val != null ? val.SmiSyntax : SmiSyntax.Null;
                    Console.WriteLine("[{0}]: {1},{2},{3}", thName, nextOid, val, type);
                }

                pdu = pdu.Clone(new Vb(nextOid));
            }
        }
Ejemplo n.º 4
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();
                }
            }
        }