hxb
2020-09-23 2921da3e4ba99f0df28a172d47daa69e375431bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using HDLCloudMonitor.InfoMonitor;
using HDLCloudMonitor.InfoMonitor.ReqPack;
using Newtonsoft.Json;
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
 
namespace HDLCloudMonitor
{
    class Program
    {
        static readonly Computer _computer = new Computer();
 
        static void Main(string[] args)
        {
            GetCompurteInfo();          
 
            Console.ReadKey();
        }
        
        public static void GetCompurteInfo()
        {
            new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    try
                    {                     
                        var serverStatus = new ServerRunStatusReqPack();
                        serverStatus.CPU = _computer.getCurrentCpuUsage();   //CPU百分比    
                        serverStatus.Memory = _computer.GetMemoryRunStatus();//运行内存百分比
 
                        var programName = HDLCloudMonitorConfig.ProgramName.Split(',');
 
                        for (int i = 0; i < programName.Length; i++)
                        {
                            Process[] server = Process.GetProcessesByName(programName[i]);
                            if (server.Length == 0)
                            {
                                Process.Start(ConfigurationManager.AppSettings[programName[i]]);
                            }
                            else
                            {
                                foreach (var item in server)
                                {
                                    PerformanceCounter pf1 = new PerformanceCounter("Process", "Working Set - Private", item.ProcessName);
 
                                    serverStatus.ProgramRunStatus.Add(new ProgramRunStatus
                                    {
                                        ProgramName = item.ProcessName, //进程名称
                                        Threads = item.Threads.Count,   //线程数
                                        Memory = Convert.ToInt32(pf1.NextValue() / 1024 / 1024) //占用内存MB
                                    });
                                }
                            }
                        }
 
                        var reqPack = new MonitorCommonReqPack
                        {
                            Guid = HDLCloudMonitorConfig.Guid,
                            IpAddress = HDLCloudMonitorConfig.LocalIpAddress,
                            ProgramName = HDLCloudMonitorConfig.MonitorProgramName,
                            ReqType = ReqType.MonitorPacket,
                            HappenTime = DateTime.Now,
                            RunStatus = serverStatus
                        };
                        var res = HttpHp.RequestOneUrlAnswerString(HDLCloudMonitorConfig.MonitorCenterReqUrl, "post", Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(reqPack)));
 
                        Thread.Sleep(5 * 1000);
                    }
                    catch(Exception ex)
                    {
                        File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + $"/{DateTime.Now.ToString("yyyy-MM-dd")}_Log.txt", $"{ex.Message}\r\n");
                        Thread.Sleep(1000);
                    }
                }
            }))
            { IsBackground = false }.Start();
        }      
    }
}