0

嗨,我正在开展一个项目,该项目会关闭并收集服务器信息 CPU、MEM、HDD 使用情况等。我目前的工作版本有点混乱。我正在尝试使其更具动态性(最终结果将是从数据库中提取服务器信息)

我试图传递从我的机器收集的数据,并将数据传递到找到的每个服务器的数据网格(名称、CPU、MEM 等......)

这是我下面的代码:

<Window x:Class="Sat_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Orientation="Vertical">
            <TextBlock x:Name="Clock"/>
            <DataGrid x:Name="data"/>
        </StackPanel>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Net.NetworkInformation;
using System.Management;
using System.Windows.Threading;
using System.Collections.ObjectModel;

namespace Sat_test
{

    public partial class MainWindow : Window
    {
        private DispatcherTimer timer1;        
        public MainWindow()
        {
            InitializeComponent();
            this.SetUpTimer();
            DispatcherTimer Clock = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate { this.Clock.Text = DateTime.Now.ToString("HH:mm:ss"); }, this.Dispatcher);

        }
        private void SetUpTimer()
        {
            timer1 = new DispatcherTimer();
            timer1.Interval = TimeSpan.FromMilliseconds(100); 
            timer1.Tick += OnTimerTick;
            timer1.Start();

        }
        private void OnTimerTick(object sender, EventArgs e)
        {            
            this.Start();


        }

            private void Start()

        {

            foreach (var item in Items())
            {
                ThreadPool.QueueUserWorkItem((o) =>
                    {
                        item.CheckService();
                    });
            }


        }

        private IEnumerable<NotifyItem> Items()
        {

            yield return new NotifyItem { ServerName = "127.0.0.1", HDD1 = "'C:'", ConnectionString = "SQLConnectionstring1" };
            yield return new NotifyItem { ServerName = "127.0.0.1", HDD1 = "'C:'", ConnectionString = "SQLConnectionstring1" };
            yield return new NotifyItem { ServerName = "127.0.0.1", HDD1 = "'C:'", ConnectionString = "SQLConnectionstring1" };




        }



 public class NotifyItem
    {
        private int pingTime = 10;
        public string ConnectionString { get; set; }

        public string ServerName { get; set; }

        public Image Image { get; set; }

        public string HDD1 { get; set; }

        public void CheckService()
        {
            String host = ServerName;
            bool result = false;
            Ping p = new Ping();
            try
            {
                PingReply reply = p.Send(host, pingTime);
                if (reply.Status == IPStatus.Success)
                    result = true;
            }
            catch { }
            if (result)
            {
                //Image.Visibility = Visibility.Visible;
                DoStuffForServer();
            }
        }

        public string GetServerFullName()
        {
            string Name = "";
            ConnectionOptions options = new ConnectionOptions();
            //options.Username = usr;
            //options.Password = pwd;
            options.Impersonation = ImpersonationLevel.Impersonate;
            options.Authentication = AuthenticationLevel.Default;
            options.EnablePrivileges = true;
            ManagementScope scope = new ManagementScope("\\\\" + ServerName + "\\root\\CIMV2", options);
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
            ManagementObjectSearcher getcpu = new ManagementObjectSearcher(scope, query);

            foreach (ManagementObject obj in getcpu.Get())
            {
                Name = string.Concat(obj["Name"].ToString());
            }
            return Name;
        }

        public void DoStuffForServer()
        {                                      
            string serverFullName = GetServerFullName();
            int cpuData = Convert.ToInt16(GetCPU());
            int memData = Convert.ToInt16(GetMEM());
            double hdd1Data = Math.Truncate(GetHDD1() * 100) / 100;                      
        }

        private double GetHDD1()
        {
            ConnectionOptions options = new ConnectionOptions();
            //options.Username = usr;
            //options.Password = pwd;
            options.Impersonation = ImpersonationLevel.Impersonate;
            options.Authentication = AuthenticationLevel.Default;
            options.EnablePrivileges = true;
            ManagementScope scope = new ManagementScope("\\\\" + ServerName + "\\root\\CIMV2", options);           
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE Name = " + HDD1 + " ");
            ManagementObjectSearcher getcpu = new ManagementObjectSearcher(scope, query);
            double HDDTotal = 0;
            double HDDfree = 0;
            foreach (ManagementObject obj in getcpu.Get())
            {
                HDDfree = double.Parse(obj["FreeSpace"].ToString());
            }
            return (HDDfree / 1024 / 1024 / 1024);
        }

        private double GetMEM()
        {
            ConnectionOptions options = new ConnectionOptions();
            //options.Username = usr;
            //options.Password = pwd;
            options.Impersonation = ImpersonationLevel.Impersonate;
            options.Authentication = AuthenticationLevel.Default;
            options.EnablePrivileges = true;
            ManagementScope scope = new ManagementScope("\\\\" + ServerName + "\\root\\CIMV2", options);
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OPeratingSystem");
            ManagementObjectSearcher getcpu = new ManagementObjectSearcher(scope, query);
            double memtotal = 0;
            double memfree = 0;
            foreach (ManagementObject obj in getcpu.Get())
            {
                memtotal = double.Parse(obj["TotalVisibleMemorySize"].ToString());
                memfree = double.Parse(obj["FreePhysicalMemory"].ToString());

            }
            return (memtotal - memfree) * 100 / memtotal;
        }

        private double GetCPU()
        {
            //return 0;
            ConnectionOptions options = new ConnectionOptions();
            //options.Username = usr;
            //options.Password = pwd;
            options.Impersonation = ImpersonationLevel.Impersonate;
            options.Authentication = AuthenticationLevel.Default;
            options.EnablePrivileges = true;
            ManagementScope scope = new ManagementScope("\\\\" + ServerName + "\\root\\CIMV2", options);
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor");
            ManagementObjectSearcher getcpu = new ManagementObjectSearcher(scope, query);
            double cpu = 0;
            foreach (ManagementObject obj in getcpu.Get())
            {
                cpu = double.Parse(obj["PercentProcessorTime"].ToString());
            }
            return cpu;

        }
    }





    }
}
4

0 回答 0