ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

NetCore3.1 ping网络是否畅通及获取服务器Cpu、内存使用率

2022-06-21 18:37:59  阅读:141  来源: 互联网

标签:Console cpuCounter ping System WriteLine using reply Cpu NetCore3.1


十年河东,十年河西,莫欺少年穷

学无止境,精益求精

1、ping 服务器是否畅通

引入nuget包:System.Diagnostics.Process

 

 代码如下:

using System;
using System.Net.NetworkInformation;
using System.Text;

namespace PingPolice
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 主机地址
            string targetHost = "www.baidu.com";
            

            Ping pingSender = new Ping();
     
            //单位毫秒
            int timeout = 3000;

            Console.WriteLine($"Pinging {targetHost}");
            //PingReply reply = pingSender.Send(targetHost, timeout, buffer, options);

            PingReply reply = pingSender.Send(targetHost, timeout);

            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine($"Address: {reply.Address}");
                Console.WriteLine($"RoundTrip time: {reply.RoundtripTime}");
                Console.WriteLine($"Time to live: {reply.Options.Ttl}");
                Console.WriteLine($"Don't fragment: {reply.Options.DontFragment}");
                Console.WriteLine($"Buffer size: {reply.Buffer.Length}");
            }
            else
            {
                Console.WriteLine(reply.Status);
            }

            Console.ReadLine();
        }
    }
}
View Code

2、获取服务器Cpu使用率、及内存占用率

引入nuget包:System.Diagnostics.PerformanceCounter

using System;
using System.Diagnostics;
using System.Management;
using System.Threading;
using System.Threading.Tasks; 

namespace Cpupolice
{
    class Program
    {
        public static void  Main(string[] args)
        {
            mer();
            cpu();
            Console.ReadKey();
        }

        /// <summary>
        /// 内存使用 
        /// </summary>
        private static void mer()
        {
            //获取总物理内存大小
            ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();
            double available = 0, capacity = 0;
            foreach (ManagementObject mo1 in moc1)
            {
                capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
            }
            moc1.Dispose();
            cimobject1.Dispose();


            //获取内存可用大小
            ManagementClass cimobject2 = new ManagementClass("Win32_PerfFormattedData_PerfOS_Memory");
            ManagementObjectCollection moc2 = cimobject2.GetInstances();
            foreach (ManagementObject mo2 in moc2)
            {
                available += ((Math.Round(Int64.Parse(mo2.Properties["AvailableMBytes"].Value.ToString()) / 1024.0, 1)));

            }
            moc2.Dispose();
            cimobject2.Dispose();

            Console.WriteLine("总内存=" + capacity.ToString() + "G");
            Console.WriteLine("可使用=" + available.ToString() + "G");
            Console.WriteLine("已使用=" + ((capacity - available)).ToString() + "G," + (Math.Round((capacity - available) / capacity * 100, 0)).ToString() + "%");
        }

        /// <summary>
        /// cpu使用率
        /// </summary>
        private static void cpu()
        {
            PerformanceCounter cpuCounter;
            PerformanceCounter ramCounter;

            cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";
            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            ramCounter = new PerformanceCounter("Memory", "Available MBytes");



            Console.WriteLine("电脑CPU使用率:" + cpuCounter.NextValue() + "%");
            Console.WriteLine("电脑可使用内存:" + ramCounter.NextValue() + "MB");
            Console.WriteLine();



            while (true)
            {
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("电脑CPU使用率:" + cpuCounter.NextValue() + " %");
                Console.WriteLine("电脑可使用内存:" + ramCounter.NextValue() + "MB");
                Console.WriteLine();

                if ((int)cpuCounter.NextValue() > 80)
                {
                    System.Threading.Thread.Sleep(1000 * 60);
                }
            }
        }
         
    }

}
View Code

需要说明的是,cpu使用率波动很大,建议多次取值求平均值,设置最大值,如80%,平均值大于80%则预警服务器资源不够

参考博客:https://www.cnblogs.com/RainFate/p/11518412.html

@天才卧龙的博客园

标签:Console,cpuCounter,ping,System,WriteLine,using,reply,Cpu,NetCore3.1
来源: https://www.cnblogs.com/chenwolong/p/16397847.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有