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();
|
}
|
}
|
}
|