예제 #1
0
        public void TestMgmtObjectCollectData()
        {
            MgmtObjectConfiguration conf = new MgmtObjectConfiguration()
            {
                Namespace    = @"root\cimv2",
                From         = @"WIN32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor",
                Where        = @"Name='_Total'",
                PerfCounters = new PerfCounterConfigurationList()
            };

            conf.PerfCounters.Add(new PerfCounterConfiguration()
            {
                As     = "PercentHypervisorRuntime",
                Select = "PercentHypervisorRuntime"
            });

            var mgmtObj = MgmtObject.CreateInstance(conf);
            var data    = mgmtObj.CollectData(new Dictionary <String, Object>());
            var list    = (List <Object>)data;

            Assert.IsNotNull(list);

            var jsonObj = (IDictionary <String, Object>)list.FirstOrDefault();

            Assert.IsNotNull(jsonObj["PercentHypervisorRuntime"]);
            //Prevent PropertyData object is returned
            //The value in PropertyData should be returned not the container
            Assert.IsFalse(jsonObj["PercentHypervisorRuntime"] is PropertyData);
        }
예제 #2
0
 protected MonitoringTask(MonitorConfiguration conf)
 {
     this.conf = conf;
     foreach (var mgmtObjConf in conf.MgmtObjects)
     {
         mgmtObjects.Add(MgmtObject.CreateInstance(mgmtObjConf));
     }
 }
        public MgmtObject TestDynamicMemoryMgmtObject()
        {
            MgmtObjectConfiguration conf = new MgmtObjectConfiguration()
            {
                Type = MgmtObjectType.DynamicMemory,
            };

            var mgmtObj = MgmtObject.CreateInstance(conf);

            Assert.IsNotNull(mgmtObj);
            Assert.IsTrue(mgmtObj is DynamicMemoryMgmtObject);
            return(mgmtObj);
        }
예제 #4
0
        public void TestMgmtObjectSelectPath()
        {
            MgmtObjectConfiguration conf = new MgmtObjectConfiguration()
            {
                Namespace    = @"root\cimv2",
                From         = @"WIN32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor",
                Where        = @"Name='_Total'",
                PerfCounters = new PerfCounterConfigurationList()
            };

            conf.PerfCounters.Add(new PerfCounterConfiguration()
            {
                Select = "*"
            });

            var mgmtObj = MgmtObject.CreateInstance(conf);
            var data    = mgmtObj.CollectData(new Dictionary <String, Object>()) as IList <Object>;

            Assert.IsNotNull(data);
        }
예제 #5
0
        public void TestMgmtObjectErrorHandling()
        {
            //Test namespace wrong
            try
            {
                MgmtObjectConfiguration conf = new MgmtObjectConfiguration()
                {
                    Namespace    = @"root\FALSE_NAMESPACE",
                    From         = "WIN32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor",
                    PerfCounters = new PerfCounterConfigurationList()
                    {
                        new PerfCounterConfiguration()
                    }
                };

                var mgmtObj = MgmtObject.CreateInstance(conf);
                var data    = mgmtObj.CollectData(new Dictionary <String, Object>());
                Assert.Fail("ManagementException expected");
            }
            catch (ManagementException e)
            {
                Console.WriteLine(e);
                Console.WriteLine();
            }

            //Test class name wrong
            try
            {
                MgmtObjectConfiguration conf = new MgmtObjectConfiguration()
                {
                    Namespace    = @"root\cimv2",
                    From         = @"FALSE_CLASS",
                    PerfCounters = new PerfCounterConfigurationList()
                    {
                        new PerfCounterConfiguration()
                    }
                };
                var mgmtObj = MgmtObject.CreateInstance(conf);
                var data    = mgmtObj.CollectData(new Dictionary <String, Object>());
                Assert.Fail("ManagementException expected");
            }
            catch (ManagementException e)
            {
                Console.WriteLine(e);
                Console.WriteLine();
            }

            //Test no instance found
            try
            {
                MgmtObjectConfiguration conf = new MgmtObjectConfiguration()
                {
                    Namespace    = @"root\cimv2",
                    From         = @"WIN32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor",
                    Where        = @"Name='FALSE_NAME'",
                    PerfCounters = new PerfCounterConfigurationList()
                    {
                        new PerfCounterConfiguration()
                    }
                };

                var mgmtObj = MgmtObject.CreateInstance(conf);
                var data    = mgmtObj.CollectData(new Dictionary <String, Object>());
                Assert.Fail("ManagementException expected");
            }
            catch (NotImplementedException e)
            {
                Console.WriteLine(e);
                Console.WriteLine();
            }

            //Test null perf count
            {
                MgmtObjectConfiguration conf = new MgmtObjectConfiguration()
                {
                    Namespace = @"root\cimv2",
                    From      = @"WIN32_PerfRawData_HvStats_HyperVHypervisorLogicalProcessor",
                    Where     = @"Name='_Total'",
                };

                var mgmtObj = MgmtObject.CreateInstance(conf);
                var data    = mgmtObj.CollectData(new Dictionary <String, Object>());
                Assert.IsNull(data);
            }
        }
        public void TestMetricJSON()
        {
            MonitorConfiguration conf        = MonitorConfiguration.Load(@"..\..\..\EnhancedMonitoring\Configuration\EnhancedMonitoringProviderConfig.xml");
            List <MgmtObject>    mgmtObjects = new List <MgmtObject>();

            foreach (var mgmtObjConf in conf.MgmtObjects)
            {
                mgmtObjects.Add(MgmtObject.CreateInstance(mgmtObjConf));
            }

            Assert.AreNotEqual(0, mgmtObjects.Count);


            var data = new Dictionary <String, Object>();
            var vms  = SupportedVMDetector.CreateInstance(conf.SupportedVMDetector).GetSupportedVM();

            Assert.AreNotEqual(0, vms.Count);

            var vm = vms.FirstOrDefault();

            var args = new Dictionary <String, Object>(vm);

            foreach (var mgmtObj in mgmtObjects)
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();
                try
                {
                    data.Add(mgmtObj.KeyName, mgmtObj.CollectData(args));
                }
                catch (Exception e)
                {
                    if (!mgmtObj.SuppressError)
                    {
                        Console.WriteLine(e);
                        Console.WriteLine(e.StackTrace);
                        Assert.Fail(e.Message);
                    }
                    data.Add(mgmtObj.KeyName, null);
                }
                Console.WriteLine(String.Format("{0}\t{1}", watch.ElapsedMilliseconds, mgmtObj.KeyName));
            }

            Assert.IsNotNull(data);
            Assert.AreNotEqual(0, data.Count);

            var xmlStr = DataFormatHelper.ToXml(data);

            Console.WriteLine(xmlStr);

            //var jsonStr = DataFormatHelper.ToJSON(data);

            //var encodedStr = DataFormatHelper.Base64Encode(jsonStr);

            //var dataChunks = DataFormatHelper.PackString(jsonStr, 900);

            //var packedData = DataFormatHelper.ToChunk("", dataChunks);

            //var encodedDataChunks = DataFormatHelper.PackString(encodedStr, 900);

            //var encodedPackedData = DataFormatHelper.ToChunk("", encodedDataChunks);

            //Console.WriteLine(JsonHelper.FormatJson(jsonStr));
            //Console.WriteLine(String.Format("Original   JSON      string length: {0}", jsonStr.Length));
            //Console.WriteLine(String.Format("Encoded    JSON      string length: {0}", encodedStr.Length));
            //Console.WriteLine(String.Format("Packed     JSON      string length: {0}", packedData.Select(d => d.Value.ToString().Length).Sum()));
            //Console.WriteLine(String.Format("Packed encoded  JSON string length: {0}", encodedPackedData.Select(d => d.Value.ToString().Length).Sum()));
        }