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