当前位置 : 主页 > 编程语言 > 其它开发 >

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

来源:互联网 收集:自由互联 发布时间:2022-06-22
十年河东,十年河西,莫欺少年穷 学无止境,精益求精 1、ping 服务器是否畅通 引入nuget包:System.Diagnostics.Process 代码如下: using System; using System.Net.NetworkInformation; using System.Text; names

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

学无止境,精益求精

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

@天才卧龙的自由互联

只爱少妇
上一篇:ARP协议及ARP攻击
下一篇:没有了
网友评论