C#WMI代码,用于计算Windows Server 2008上挂载点上的可用空间和驱动器空间

时间:2010-09-20 17:03:47

标签: c# wmi snmp

我已经找到了很多关于如何查看物理驱动器的信息,但我需要监控物理驱动器上的几个挂载点。我宁愿通过wmi这样做,但任何可以做到这一点的.NET对象都可以。

1 个答案:

答案 0 :(得分:4)

所以我终于想出了这个。 WMI可以与糟糕的文档进行持续的战斗,并且可以自己解决这些问题。

<强>资源

用于测试查询的

wbemtest

wbemtest reference

用于生成多种语言的wmi scipts的scriptomatic

scriptomatic

代码:

using System;
using System.IO;
using System.Management;
using System.Diagnostics;


namespace Monitor
{
    class Program
    {
        static void Main(string[] args)
        {

            double Free, Size, FreePercentage;
            DateTime Now = DateTime.Now;

            string scopeStr = string.Format(@"root\cimv2", "TestSqlServer");


            ManagementScope scope = new ManagementScope(scopeStr);
            scope.Connect();

            string queryString = "SELECT * FROM Win32_Volume WHERE DriveLetter IS NULL";
            SelectQuery query = new SelectQuery(queryString);
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
            {
                Console.WriteLine("Entering Volume loop: ");
                foreach (ManagementObject disk in searcher.Get())
                {
                    Console.WriteLine("foreach Volume: ");
                    //-------------------------------------------------------------------------
                    //Console.WriteLine("Free %" + double.Parse(disk["FreeSpace"].ToString()) / double.Parse(disk["Capacity"].ToString()) * 100);
                    FreePercentage = double.Parse(disk["FreeSpace"].ToString()) / double.Parse(disk["Capacity"].ToString()) * 100;
                    //-------------------------------------------------------------------------
                    string _MountPoint = disk["Name"].ToString();
                    //Console.WriteLine("Free: " + disk["FreeSpace"].ToString());
                    Free = Math.Round(Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024), 2);
                    Console.WriteLine("Free: " + Free + " MB");
                    //Console.WriteLine("Capacity: " + disk["Capacity"].ToString());
                    Size = Math.Round(Convert.ToDouble(disk["Capacity"]) / (1024 * 1024), 2);
                    Console.WriteLine("Size: " + Size + " MB");
                    if (_MountPoint[_MountPoint.Length - 1] == Path.DirectorySeparatorChar)
                    {
                        _MountPoint = _MountPoint.Remove(_MountPoint.Length - 1);
                    }
                    _MountPoint = _MountPoint.Replace("\\", "\\\\\\\\");

                    string _MountPointQueryString = "select * FROM Win32_MountPoint WHERE Directory=\"Win32_Directory.Name=\\\"" + _MountPoint + "\\\"\"";

                    SelectQuery _MountPointQuery = new SelectQuery(_MountPointQueryString);
                    using (
                        ManagementObjectSearcher mpsearcher =
                            new ManagementObjectSearcher(scope, _MountPointQuery))
                    {
                        Console.WriteLine("Entering directory Foreach loop: ");
                        foreach (ManagementObject mp in mpsearcher.Get())
                        {
                            Console.WriteLine("Foreach directory: ");

                            try
                            {
                                //Console.WriteLine("Volume: " + mp["Volume"].ToString());
                                Console.WriteLine("Directory: " + mp["Directory"].ToString());
                                string Volume = mp["Directory"].ToString().Replace("Win32_Directory.Name=", "");


                                if (FreePercentage <= 5.00)
                                {
                                    throw new Exception("\nLabel: " + Volume + "\nSeverity: " + EventLogEntryType.Error + "\nTime: " + Now + "\nMessage: disk space threshhold: " + CalculateUsedSpace(Free, Size) + " % used (" + Free + "MB" + " free)");
                                }

                            }
                            catch (Exception ex)
                            {
                                EventLog.WriteEntry("DriveStats Warning", "Message: " + ex.Message, EventLogEntryType.Error);
                            }
                        }
                    }
                }
            }


        }

        static double CalculateUsedSpace(double f, double s)
        {
            double UsedPercentage;
            if (s == 0)
            {
                return f;
            }
            else if (s >= 0 && f == 0)
            {
                UsedPercentage = 100.00;
                return UsedPercentage;
            }
            else
            {
                double UsedSpace = s - f;
                UsedPercentage = (UsedSpace / s) * 100;
                UsedPercentage = Math.Round(UsedPercentage, 2);
                //Console.WriteLine("Used Percentage: " + UsedPercentage);
                return Math.Round(UsedPercentage, 2);
            }
        }

        static double CalculateFreePercentage(double f, double s)
        {
            double FreePercentage;
            if (f == 0)
            {
                FreePercentage = 0;
                return FreePercentage;
            }
            else
            {
                FreePercentage = (f / s) * 100;
                //Console.WriteLine("Free Percentage: " + FreePercentage);
                return Math.Round(FreePercentage, 2);
            }

        }
    }
}