利用windows性能计数器进行服务器性能监控

简介: using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; 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;
            }
        }
    }
}

  

<appSettings>
    <!--CPU使用率-->
    <add key="S_CPU" value="Processor#% Processor Time#_Total#IsCounter"/>
    <!--网站连接数-->
    <add key="S_WEB_PORT" value="Web Service#Anonymous Users/sec#默认网站#IsCounter"/>
    <!--数据库连接数-->
    <add key="S_CONNECT" value="SQLServer:General Statistics#User Connections##IsCounter"/>
    <!--实施人员可以无视MS TCP Loopback interface-->
    <add key="ReceiveDataInfo1" value="Network Interface#Bytes Received/sec#MS TCP Loopback interface#IsCounter"/>
    <add key="SentDataInfo1" value="Network Interface#Bytes Sent/sec#MS TCP Loopback interface#IsCounter"/> 
    <!--网卡接受字节速率-->
    <add key="S_FLOW_IN" value="Network Interface#Bytes Received/sec#Realtek PCIe GBE Family Controller#IsCounter"/>
    <!--网卡发送字节速率-->
    <add key="S_FLOW_OUT" value="Network Interface#Bytes Sent/sec#Realtek PCIe GBE Family Controller#IsCounter"/>    
    <!--与WIN2003任务管理器性能选项卡 右下角的数据相同    与WIN7此处数据不同
    % Committed Bytes In Use
    是 Memory\\Committed Bytes 与 Memory\\Commit Limit 之间的比值。
    Committed memory 指如果需要写入磁盘时已在页面文件中保留空间的处于使用中的物理内存。
    Commit Limit 是由页面文件的大小而决定的。如果扩大了页面文件,该比例就会减小。
    这个计数器只显示当前百分比;它不是一个平均值。-->
    <add key="S_MERMORY" value="Memory#% Committed Bytes In Use##IsCounter"/>
    <!--数据库连接字符串-->
    <add key="SQLConnStr" value="Data Source=192.168.1.20;uid=sa;pwd=nj0819;Initial Catalog=CCMS_SH" />
    <!--当前监视的服务器-->
    <add key="ServerID" value="1" />
  </appSettings>

  

目录
相关文章
|
1月前
|
弹性计算 监控 数据可视化
ecs自定义监控
ecs自定义监控
24 1
|
1月前
|
Arthas 弹性计算 运维
阿里云ECS监控服务
阿里云ECS监控服务
403 2
|
1月前
|
弹性计算 运维 监控
ecs实例性能监控
ecs实例性能监控
19 2
|
1月前
|
弹性计算 运维 监控
ECS资源监控
ECS资源监控涉及CPU、内存、磁盘I/O、网络流量、系统负载和进程的关键指标,通过云服务商控制台、监控服务、API与SDK、运维工具进行实时监控和告警设置。支持历史数据查询、事件监控,以及使用Windows资源监视器和Linux系统工具进行操作系统层面监控。全面监控确保ECS实例稳定运行、资源有效利用和问题及时处理。如需特定云服务商的指导,请询问。
35 3
|
28天前
|
Shell Windows
Windows服务器 开机自启动服务
Windows服务器 开机自启动服务
14 0
|
5天前
|
安全 Unix Linux
Windows如何远程连接服务器?服务器远程连接图文教程
服务器操作系统可以实现对计算机硬件与软件的直接控制和管理协调,任何计算机的运行离不开操作系统,服务器也一样,服务器操作系统主要分为四大流派:Windows Server、Netware、Unix和Linux。今天驰网飞飞将和你分享Windows server远程连接图文教程,希望可以帮助到你
21 4
Windows如何远程连接服务器?服务器远程连接图文教程
|
1月前
|
弹性计算 运维 监控
ecs监控与评估
阿里云ECS提供全面的监控与评估解决方案,包括云监控服务(实时资源指标、告警)、ECS实例详情页(运行状态查看)、资源负载评估(综合性能得分)、ECS Insight(多维度分析)、报警配置、流量管理优化(负载均衡、CDN)。这些工具帮助用户有效管理资源、识别潜在风险、优化性能,确保云服务稳定高效。
17 1
|
1月前
|
Linux 数据安全/隐私保护 Docker
linux和windows中安装emqx消息服务器
linux和windows中安装emqx消息服务器
39 0
|
1月前
|
监控 JavaScript 安全
监控内网电脑软件设计与实现:基于Node.js的服务器端架构分析
在当今信息技术高度发达的时代,监控内网电脑的需求日益增长。企业需要确保网络安全,个人用户也需要监控家庭网络以保护隐私和安全。本文将介绍一种基于Node.js的服务器端架构,用于设计和实现监控内网电脑软件。
102 0
|
1月前
|
存储 Windows
windows server 2019 云服务器看不见硬盘的解决方案
windows server 2019 云服务器看不见硬盘的解决方案