public void TestEqual()
 {
     var left = new Counter32(100);
     var right = new Counter32(100);
     Assert.AreEqual(left, right);
     Assert.IsFalse(left.Equals(1));
 }
Example #2
0
        public void Constructors()
        {
            var a = new Counter32();

            Assert.Equal(SnmpConstants.SmiCounter32, a.Type);
            Assert.Equal(0U, a.Value);

            var b = new Counter32(10);

            Assert.Equal(10U, b.Value);

            var c = new Counter32(10);

            Assert.Equal(10U, c.Value);
            b.Value = 12;
            Assert.Equal(10U, c.Value);

            var d = new Counter32("15");

            Assert.Equal(15U, d.Value);

            var e = (Counter32)d.Clone();

            d.Value = 20;
            Assert.Equal(15U, e.Value);
        }
Example #3
0
        private static void GetOrAddFuncBenchRndCD()
        {
            var dict = new Concurrent.ConcurrentDictionary <int, string>();
            var cnt  = new Counter32();

            var benchmarkName = "======== Random GetOrAdd Func Concurrent 1M int->string Ops/sec:";

            Action <int, int> act = (i, threadBias) =>
            {
                // get some random index in [0, 1000000]
                int randomIndex = GetRandomIndex(i, threadBias, 1000000);
                dict.GetOrAdd(randomIndex, (_) => "qq");

                // after making about 1000000 adds, start with a new table
                var c = cnt;
                c.Increment();
                if (Every8K(i) && c.Value > 1000000)
                {
                    if (Interlocked.CompareExchange(ref cnt, new Counter32(), c) == c)
                    {
                        dict = new Concurrent.ConcurrentDictionary <int, string>();
                    }
                }
            };

            RunBench(benchmarkName, act);
        }
        public void TestEqual()
        {
            var left  = new Counter32(100);
            var right = new Counter32(100);

            Assert.AreEqual(left, right);
        }
Example #5
0
        public void TestParseExample1()
        {
            var i      = new Counter32();
            var result = i.Decode(CounterExample1, 0);

            Assert.Equal(3, result);
            Assert.Equal(3U, i.Value);
        }
        public void TestEqual()
        {
            var left  = new Counter32(100);
            var right = new Counter32(100);

            Assert.Equal(left, right);
            Assert.False(left.Equals(1));
        }
Example #7
0
 protected DictionaryImpl(int capacity, DictionaryImpl <TKey, TKeyStore, TValue> other)
 {
     capacity          = AlignToPowerOfTwo(capacity);
     this._entries     = new Entry[capacity];
     this._size        = other._size;
     this._topDict     = other._topDict;
     this._keyComparer = other._keyComparer;
 }
Example #8
0
        private static void Counter32Perf()
        {
            var benchmarkName = "======== Counter32 1M Ops/sec:";

            Counter32         cnt = new Counter32();
            Action <int, int> act = (_, __) => { cnt.Increment(); };

            RunBench(benchmarkName, act);
        }
Example #9
0
        internal DictionaryImpl(int capacity, ConcurrentDictionary <TKey, TValue> topDict)
        {
            capacity = Math.Max(capacity, MIN_SIZE);

            capacity      = AlignToPowerOfTwo(capacity);
            this._entries = new Entry[capacity];
            this._size    = new Counter32();
            this._topDict = topDict;
        }
Example #10
0
        private static void Counter32GetEstimatedPerf()
        {
            var benchmarkName = "======== Counter32 Estimated Get 1M Ops/sec:";

            Counter32         cnt = new Counter32();
            Action <int, int> act = (_, __) => { cnt.Increment(); var dummy = cnt.EstimatedValue; };

            RunBench(benchmarkName, act);
        }
Example #11
0
        /// <summary>
        /// Returns two LONG values, in and out octets for plotting.
        /// </summary>
        /// <param name="interfaceIndex"></param>
        /// <returns></returns>
        private long[] getInterfaceOctets(int interfaceIndex)
        {
            // Where our SNMP results are going.
            List <Variable> oidResults = new List <Variable>();

            ObjectIdentifier oidSpeed     = new ObjectIdentifier(".1.3.6.1.2.1.2.2.1.5." + interfaceIndex);
            ObjectIdentifier oidType      = new ObjectIdentifier(".1.3.6.1.2.1.2.2.1.3." + interfaceIndex);
            ObjectIdentifier oidInOctets  = new ObjectIdentifier(".1.3.6.1.2.1.2.2.1.10. " + interfaceIndex);
            ObjectIdentifier oidOutOctets = new ObjectIdentifier(".1.3.6.1.2.1.2.2.1.16." + interfaceIndex);

            // OID's we're going to retrieve.
            List <Variable> oidList = new List <Variable>();

            oidList.Add(new Variable(oidSpeed));
            oidList.Add(new Variable(oidType));
            oidList.Add(new Variable(oidInOctets));
            oidList.Add(new Variable(oidOutOctets));

            long[] returnValue = new long[3];

            oidResults = snmp.GetOidValues(oidList);

            Integer32 vIfType = (Integer32)oidResults.Find(o => o.Id == oidType).Data;
            int       dIfType = vIfType.ToInt32();

            Gauge32  guageIfSpeed = (Gauge32)oidResults.Find(o => o.Id == oidSpeed).Data;
            Variable vInOctets    = oidResults.Find(o => o.Id == oidInOctets);
            Variable vOutOctets   = oidResults.Find(o => o.Id == oidOutOctets);

            if (dIfType != 94 && dIfType != 117 && dIfType != 6 && dIfType != 131)
            {
                return new long[] { 0, 0, 0 }
            }
            ;

            if (vInOctets.Data.TypeCode == SnmpType.NoSuchInstance || vOutOctets.Data.TypeCode == SnmpType.NoSuchInstance)
            {
                return(new long[] { 0, 0, 0 });
            }
            Counter32 counterInOctets  = (Counter32)vInOctets.Data;
            Counter32 counterOutOctets = (Counter32)vOutOctets.Data;

            returnValue[0] = Convert.ToInt64(guageIfSpeed.ToUInt32());
            returnValue[1] = Convert.ToInt64(counterInOctets.ToString());
            returnValue[2] = Convert.ToInt64(counterOutOctets.ToString());

            return(returnValue);
        }
Example #12
0
        public void Diff()
        {
            var a = new Counter32(15);

            Assert.Equal(15U, a.Value);

            var b = new Counter32(10);

            Assert.Equal(10U, b.Value);

            // TODO : Is this correct?
            Assert.Equal(4U, Counter32.Diff(b, a));

            // TODO : Is this correct?
            Assert.Equal(4294967291U, Counter32.Diff(a, b));
        }
Example #13
0
        public void Generate()
        {
            var i      = new Counter32(300);
            var buffer = new MutableByte();

            i.Encode(buffer);

            var expected = new byte[]
            {
                SnmpConstants.SmiCounter32, // ASN.1 Type
                0x02,                       // Length
                0x01, 0x2C,                 // 300 in big endian
            };

            Assert.Equal(expected, buffer);
        }
 // [Test]
 public void TestExtra()
 {
     uint j = 1;
     const uint i = uint.MinValue;
     while (i + j < uint.MaxValue)
     {                
         Counter32 _int = new Counter32(i + j);
         byte[] bytes = _int.ToBytes();
         Counter32 _else = (Counter32)DataFactory.CreateSnmpData(bytes);
         Assert.AreEqual(i + j, _else.ToUInt32());
         try {
            j = j * 256; 
         } catch (Exception) {
            break; 
         }                
     }
 }
        // [Test]
        public void TestExtra()
        {
            uint       j = 1;
            const uint i = uint.MinValue;

            while (i + j < uint.MaxValue)
            {
                Counter32 _int  = new Counter32(i + j);
                byte[]    bytes = _int.ToBytes();
                Counter32 _else = (Counter32)DataFactory.CreateSnmpData(bytes);
                Assert.AreEqual(i + j, _else.ToUInt32());
                try {
                    j = j * 256;
                } catch (Exception) {
                    break;
                }
            }
        }
Example #16
0
        private ISnmpData ConvertStringValue2SnmpData(SnmpType type, string value)
        {
            ISnmpData data;

            try
            {
                switch (type)
                {
                case SnmpType.Integer32:
                    data = new Integer32(int.Parse(value));
                    break;

                case SnmpType.Counter32:
                    data = new Counter32(uint.Parse(value));
                    break;

                case SnmpType.Gauge32:
                    data = new Gauge32(uint.Parse(value));
                    break;

                case SnmpType.TimeTicks:
                    data = new TimeTicks(uint.Parse(value));
                    break;

                case SnmpType.OctetString:
                    data = new OctetString(value);
                    break;

                case SnmpType.IPAddress:
                    data = new IP((IPAddress.Parse(value)).GetAddressBytes());
                    break;

                default:
                    throw new InvalidDataType();
                }
            }
            catch (InvalidCastException)
            {
                throw new InvalidDataType();
            }

            return(data);
        }
Example #17
0
        public void TestConstructor()
        {
            byte[]    buffer2 = new byte[] { 01, 44 };
            Counter32 c2      = new Counter32(buffer2);

            Assert.AreEqual(300, c2.ToUInt32());
            Assert.AreEqual("300", c2.ToString());
            Counter32 sec = new Counter32(300);
            Counter32 thr = new Counter32(301);

            Assert.IsTrue(c2.Equals(sec));
            Assert.AreEqual(300.GetHashCode(), c2.GetHashCode());
            Assert.IsTrue(c2 == sec);
            Assert.IsTrue(c2 != thr);
            Assert.IsFalse(c2 == null);
            Assert.IsFalse(null == sec);
            Assert.IsTrue(c2 == c2);

            byte[]    buffer1 = new byte[] { 13 };
            Counter32 c1      = new Counter32(buffer1);

            Assert.AreEqual(13, c1.ToUInt32());

            byte[]    buffer3 = new byte[] { 1, 17, 112 };
            Counter32 c3      = new Counter32(buffer3);

            Assert.AreEqual(70000, c3.ToUInt32());

            byte[]    buffer4 = new byte[] { 1, 201, 195, 128 };
            Counter32 c4      = new Counter32(buffer4);

            Assert.AreEqual(30000000, c4.ToUInt32());

            byte[]    buffer5 = new byte[] { 0, 255, 255, 255, 255 };
            Counter32 c5      = new Counter32(buffer5);

            Assert.AreEqual(uint.MaxValue, c5.ToUInt32());

            byte[]    buffer0 = new byte[] { 0 };
            Counter32 c0      = new Counter32(buffer0);

            Assert.AreEqual(uint.MinValue, c0.ToUInt32());
        }
Example #18
0
        public void TestToBytes()
        {
            Counter32 c0 = new Counter32(0);
            Counter32 r0 = (Counter32)DataFactory.CreateSnmpData(c0.ToBytes());

            Assert.AreEqual(r0, c0);

            Counter32 c5 = new Counter32(uint.MaxValue);
            Counter32 r5 = (Counter32)DataFactory.CreateSnmpData(c5.ToBytes());

            Assert.AreEqual(r5, c5);

            Counter32 c4 = new Counter32(30000000);
            Counter32 r4 = (Counter32)DataFactory.CreateSnmpData(c4.ToBytes());

            Assert.AreEqual(r4, c4);

            Counter32 c3 = new Counter32(70000);
            Counter32 r3 = (Counter32)DataFactory.CreateSnmpData(c3.ToBytes());

            Assert.AreEqual(r3, c3);

            Counter32 c1 = new Counter32(13);
            Counter32 r1 = (Counter32)DataFactory.CreateSnmpData(c1.ToBytes());

            Assert.AreEqual(r1, c1);

            Counter32 c2 = new Counter32(300);
            Counter32 r2 = (Counter32)DataFactory.CreateSnmpData(c2.ToBytes());

            Assert.AreEqual(r2, c2);

            Counter32 c255 = new Counter32(255);

            Assert.AreEqual(new byte[] { 0x41, 0x02, 0x00, 0xff }, c255.ToBytes());

            Assert.AreEqual(new byte[] { 0x41, 0x01, 0x04 }, new Counter32(4).ToBytes());
        }
 public void TestConstructor()
 {
     byte[] buffer2 = new byte[] {01, 44};
     Counter32 c2 = new Counter32(buffer2);
     Assert.AreEqual(300, c2.ToUInt32());
     Assert.AreEqual("300", c2.ToString());
     Counter32 sec = new Counter32(300);
     Counter32 thr = new Counter32(301);
     Assert.IsTrue(c2.Equals(sec));
     Assert.AreEqual(300.GetHashCode(), c2.GetHashCode());
     Assert.IsTrue(c2 == sec);
     Assert.IsTrue(c2 != thr);
     Assert.IsFalse(c2 == null);
     Assert.IsFalse(null == sec);
     Assert.IsTrue(c2 == c2);
     
     byte[] buffer1 = new byte[] {13};
     Counter32 c1 = new Counter32(buffer1);
     Assert.AreEqual(13, c1.ToUInt32());
     
     byte[] buffer3 = new byte[] {1, 17, 112};
     Counter32 c3 = new Counter32(buffer3);
     Assert.AreEqual(70000, c3.ToUInt32());
     
     byte[] buffer4 = new byte[] {1, 201, 195, 128};
     Counter32 c4 = new Counter32(buffer4);
     Assert.AreEqual(30000000, c4.ToUInt32());
     
     byte[] buffer5 = new byte[] {0, 255, 255, 255, 255};
     Counter32 c5 = new Counter32(buffer5);
     Assert.AreEqual(uint.MaxValue, c5.ToUInt32());
     
     byte[] buffer0 = new byte[] {0};
     Counter32 c0 = new Counter32(buffer0);
     Assert.AreEqual(uint.MinValue, c0.ToUInt32());
 }
Example #20
0
        static void Main(string[] args)
        {
            /* Get an SNMP Object
             */
            SimpleSnmp snmpVerb = new SimpleSnmp("192.168.1.121", 161, "public");

            if (!snmpVerb.Valid)
            {
                Console.WriteLine("Seems that IP or comunauty is not cool");
                return;
            }

            /* Sample of simple Get usage on ifSpeed on interface 10
             * TODO : for sure you have to detect the right interface
             */
            Oid oidifSpeed = new Oid(".1.3.6.1.2.1.2.2.1.5.10");

            /* Getting ifSpeed
             */
            Dictionary <Oid, AsnType> snmpDataS = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifSpeed.ToString() });

            if (snmpDataS != null)
            {
                Console.WriteLine("Interface speed \"{0}\" : {1}", oidifSpeed.ToString(), snmpDataS[oidifSpeed].ToString());
            }
            else
            {
                Console.WriteLine("Not Glop!");
            }

            /* Sample of simple Get usage on ifInOctets on interface 10
             * TODO : for sure you have to detect the right interface
             */
            Oid oidifInOctets = new Oid(".1.3.6.1.2.1.2.2.1.10.10");
            Dictionary <Oid, AsnType> snmpData1;

            /* Getting it for the first time
             */
            snmpData1 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
            if (snmpData1 != null)
            {
                Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData1[oidifInOctets].ToString());
            }
            else
            {
                Console.WriteLine("Not Glop!");
            }
            int missed = 0;

            while (true)
            {
                if (missed == 0)
                {
                    /* When you detect a non refesh data, keep the last one
                     */
                    snmpData1 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
                    if (snmpData1 != null)
                    {
                        Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData1[oidifInOctets].ToString());
                    }
                    else
                    {
                        Console.WriteLine("Not Glop!");
                    }
                }

                /* Some Wait (less aproximative)
                 */
                int duration = 5;
                System.Threading.Thread.Sleep(duration * 1000); // duration seconds

                /* Getting it for the Second time
                 */
                Dictionary <Oid, AsnType> snmpData2 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
                if (snmpData2 != null)
                {
                    Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData2[oidifInOctets].ToString());
                }
                else
                {
                    Console.WriteLine("Not Glop!");
                }
                Counter32 I1 = new Counter32();
                I1.Set(snmpData1[oidifInOctets]);
                Counter32 I2 = new Counter32();
                I2.Set(snmpData2[oidifInOctets]);
                Counter32 speed = new Counter32();
                speed.Set(snmpDataS[oidifSpeed]);
                if (I2.Value == I1.Value)
                {
                    missed += 1;
                    continue;
                }
                decimal bandWithUsage = (((decimal)(I2.Value - I1.Value) * 8) * 100) / (speed * duration * (1 + missed));
                Console.WriteLine("BandWith usage : {0}%", bandWithUsage);
                missed = 0;
            }
        }
Example #21
0
        void SnmpAsyncResponseCallback(AsyncRequestResult result, SnmpPacket packet)
        {
            //If result is null then agent didn't reply or we couldn't parse the reply.
            if (result == AsyncRequestResult.NoError && packet != null)
            {
                // ErrorStatus other then 0 is an error returned by
                // the Agent - see SnmpConstants for error definitions
                if (packet.Pdu.ErrorStatus != 0)
                {
                    // agent reported an error with the request
                    TipMessage  = string.Format("SNMP应答数据包中有错误信息. Error {0} index {1}", packet.Pdu.ErrorStatus, packet.Pdu.ErrorIndex);
                    errorStatus = false;
                    NotifyPropertyChanged("TotalStatus");
                }
                else
                {
                    errorStatus = true;
                    //ifAdminStatus//ifOperStatus//ifInOctets //ifOutOctets
                    bool tempAdminStatus, tempOperStatus;
                    if (packet.Pdu.VbList[0].Value as Integer32 == 1)
                    {
                        tempAdminStatus = true;
                    }
                    else
                    {
                        tempAdminStatus = false;
                    }
                    if (packet.Pdu.VbList[1].Value as Integer32 == 1)
                    {
                        tempOperStatus = true;
                    }
                    else
                    {
                        tempOperStatus = false;
                    }
                    if (tempAdminStatus != adminStatus || tempOperStatus != operStatus)
                    {
                        adminStatus = tempAdminStatus;
                        operStatus  = tempOperStatus;
                        NotifyPropertyChanged("Status");
                        NotifyPropertyChanged("StatusDescr");
                        NotifyPropertyChanged("TotalStatus");
                        if (inSpeed != -1)
                        {
                            TipMessage = string.Format("AdminStatus:{0}\nOperStatus:{1}", adminStatus ? "up" : "down", operStatus ? "up" : "down");
                        }
                    }

                    if (inSpeed == -1)
                    {
                        //这次是第一次获取数据
                        oldInOctets   = packet.Pdu.VbList[2].Value as Counter32;
                        oldIOutOctets = packet.Pdu.VbList[3].Value as Counter32;
                        oldTime       = DateTime.Now;
                        inSpeed       = 0;
                        outSpeed      = 0;
                    }
                    else
                    {
                        newInOctets  = packet.Pdu.VbList[2].Value as Counter32;
                        newOutOctets = packet.Pdu.VbList[3].Value as Counter32;
                        DateTime now      = DateTime.Now;
                        double   interval = (now - oldTime).TotalSeconds;
                        oldTime       = now;
                        inSpeed       = Math.Round((newInOctets - oldInOctets) * 0.008 / interval, 2); //结果为 kb/s
                        outSpeed      = Math.Round((newOutOctets - oldIOutOctets) * 0.008 / interval, 2);
                        SpeedRealTime = string.Format("In: {0}kb/s; Out: {1}kb/s", inSpeed, outSpeed);
                        oldInOctets   = newInOctets;
                        oldIOutOctets = newOutOctets;
                        uiDispatcher.Invoke(new Action(() =>
                        {
                            DataPoint dpIn  = new DataPoint();
                            dpIn.XValue     = DateTime.Now;
                            dpIn.YValue     = inSpeed;
                            DataPoint dpOut = new DataPoint();
                            dpOut.XValue    = DateTime.Now;
                            dpOut.YValue    = outSpeed;
                            if (InSpeedDataPointList.Count >= 30)
                            {
                                InSpeedDataPointList.RemoveAt(0);
                            }
                            InSpeedDataPointList.Add(dpIn);
                            if (OutSpeedDataPointList.Count >= 30)
                            {
                                OutSpeedDataPointList.RemoveAt(0);
                            }
                            OutSpeedDataPointList.Add(dpOut);
                        }));
                        if (IsShowSpeedAlarm)
                        {
                            //SpeedAlarmStatus
                            bool tempSpeedAlarmStatus = (inSpeed <= maxInSpeed) && (outSpeed <= maxOutSpeed);
                            if (tempSpeedAlarmStatus != SpeedAlarmStatus)
                            {
                                SpeedAlarmStatus = tempSpeedAlarmStatus;
                                TipMessage       = string.Format("In:{0}kb/s{1};Out:{2}kb/s{3}", inSpeed, (inSpeed <= maxInSpeed) ? "正常" : "超限!", outSpeed, (outSpeed <= maxOutSpeed) ? "正常" : "超限!");
                            }
                        }
                    }
                }
            }
            else
            {
                TipMessage = "没有SNMP应答数据包";
            }
            target.Close();
        }
Example #22
0
        public void TestConstructor2()
        {
            Counter32 test = new Counter32(300);

            Assert.AreEqual(300, test.ToUInt32());
        }
 public void TestConstructor2()
 {
     Counter32 test = new Counter32(300);
     Assert.AreEqual(300, test.ToUInt32());
 }
 public void TestToBytes()
 {
     Counter32 c0 = new Counter32(0);
     Counter32 r0 = (Counter32)DataFactory.CreateSnmpData(c0.ToBytes());
     Assert.AreEqual(r0, c0);
     
     Counter32 c5 = new Counter32(uint.MaxValue);
     Counter32 r5 = (Counter32)DataFactory.CreateSnmpData(c5.ToBytes());
     Assert.AreEqual(r5, c5);
     
     Counter32 c4 = new Counter32(30000000);
     Counter32 r4 = (Counter32)DataFactory.CreateSnmpData(c4.ToBytes());
     Assert.AreEqual(r4, c4);
     
     Counter32 c3 = new Counter32(70000);
     Counter32 r3 = (Counter32)DataFactory.CreateSnmpData(c3.ToBytes());
     Assert.AreEqual(r3, c3);
     
     Counter32 c1 = new Counter32(13);
     Counter32 r1 = (Counter32)DataFactory.CreateSnmpData(c1.ToBytes());
     Assert.AreEqual(r1, c1);
     
     Counter32 c2 = new Counter32(300);
     Counter32 r2 = (Counter32)DataFactory.CreateSnmpData(c2.ToBytes());
     Assert.AreEqual(r2, c2);
     
     Counter32 c255 = new Counter32(255);
     Assert.AreEqual(new byte[] {0x41, 0x02, 0x00, 0xff}, c255.ToBytes());
     
     Assert.AreEqual(new byte[] {0x41, 0x01, 0x04}, new Counter32(4).ToBytes());
 }
Example #25
0
        public void ConvertToString()
        {
            var i1 = new Counter32(10);

            Assert.Equal("10", i1.ToString());
        }