欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用windows性能计数器进行服务器性能监控示例分享

程序员文章站 2024-02-24 22:51:34
复制代码 代码如下:using system;using system.collections.generic;using system.text;using system...

复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.diagnostics;
using system.threading;
using system.management;
using system.runtime.interopservices;
using system.data;
using system.data.sqlclient;
using system.directoryservices;
using system.collections;
using system.configuration;
using system.collections.specialized;
using system.io;

namespace mon.client
{
    public static class sysinfo
    {
        static dictionary<string, performancecounter> dic;
        static double diskusepercent;
        static double sysdiskusepercent;
        static sysinfo()
        {
            try
            {
                dic = new dictionary<string, performancecounter>();
                foreach (string setting in configurationmanager.appsettings)
                {
                    var arr = configurationmanager.appsettings[setting].split('#');
                    if (arr[arr.length - 1] == "iscounter")
                    {
                        try
                        {
                            var pc = new performancecounter(arr[0], arr[1], arr[2]);
                            dic.add(setting, pc);
                        }
                        catch (exception ex)
                        {
                            console.writeline(arr[0] + "找不到");
                            log.writelog(ex.message);
                        }
                    }
                }
            }
            catch (exception ex)
            {
                console.writeline(ex.message);
                log.writelog(ex.message);
            }
        }
        static void initdiskinfo()
        {
            #region 备忘
            //只获取系统盘代码如下
            //managementobject cpaninfo = new managementobject(string.format("win32_logicaldisk.deviceid=\"{0}\"", system.environment.systemdirectory.substring(0, 2)));
            //cpaninfo.get();
            //var s = convert.todouble(cpaninfo["size"]);
            //var l = convert.todouble(cpaninfo["freespace"]);
            //var d = (s - l) / s * 100;
            //return d;
            #endregion
            try
            {
                driveinfo[] drives = driveinfo.getdrives();
                long totalsize = 0;
                long freesize = 0;
                long ctotalsize = 0;
                long cfreesize = 0;
                foreach (driveinfo drive in drives)
                {
                    if (drive.drivetype == drivetype.cdrom)
                    {
                        continue;
                    }
                    totalsize += drive.totalsize;
                    freesize += drive.totalfreespace;
                    if (drive.name.startswith(system.environment.systemdirectory.substring(0, 2)))
                    {
                        ctotalsize += drive.totalsize;
                        cfreesize += drive.totalfreespace;
                    }
                }
                sysdiskusepercent = (ctotalsize - cfreesize) / (double)ctotalsize * 100;
                diskusepercent = (totalsize - freesize) / (double)totalsize * 100;
            }
            catch (exception ex)
            {
                console.writeline(ex.message);
                log.writelog(ex.message);
            }
        }
        public static void init()
        {
            //如有取数之前进行的业务可放在这里,提高性能
            initdiskinfo();
        }
        public static double getsysinfo(string infokey)
        {
            try
            {
                if (dic.containskey(infokey))
                {
                    return dic[infokey].nextvalue();
                }
                else
                {
                    if (infokey == "sysdiskusepercent")
                    {
                        return sysdiskusepercent;
                    }
                    else if (infokey == "diskusepercent")
                    {
                        return diskusepercent;
                    }
                    else
                    {
                        return -1;
                    }
                }
            }
            catch (exception ex)
            {
                console.writeline(ex.message);
                log.writelog(ex.message);
                return -1;
            }
        }
    }
}