forked from dotnetcore/DotnetSpider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemInfo.cs
138 lines (119 loc) · 3.8 KB
/
SystemInfo.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Diagnostics;
using System.Net;
#if NET_CORE
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
#else
using System.Management;
#endif
namespace Java2Dotnet.Spider.Common
{
public class SystemInfo
{
public string Name { get; set; }
public int CpuLoad { get; set; }
public int CpuCount { get; set; }
public int FreeMemory { get; set; }
public int TotalMemory { get; set; }
public string IpAddress { get; set; }
public DateTime Timestamp { get; set; }
public string Os { get; set; }
#if !NET_CORE
private static readonly PerformanceCounter PcCpuLoad; //CPU计数器
#else
private static readonly object Locker = new object();
private static readonly int CoreCount;
private static readonly Regex ProcessorRegex = new Regex("processor");
#endif
public static readonly int PhysicalMemory;
public static readonly string HostName;
public static readonly string Ip4Address;
static SystemInfo()
{
HostName = Dns.GetHostName();
// docker 化后只会有一个IP
Ip4Address = Dns.GetHostAddressesAsync(HostName).Result[0].ToString();
#if !NET_CORE
//初始化CPU计数器
PcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total") { MachineName = "." };
PcCpuLoad.NextValue();
//获得物理内存
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
if (mo["TotalPhysicalMemory"] != null)
{
var physicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
PhysicalMemory = (int)(physicalMemory / (1024 * 1024));
}
}
#else
// cpu count
string cpuInfo = RunCommand("cat", "/proc/cpuinfo");
CoreCount = ProcessorRegex.Matches(cpuInfo).Count;
var memInfo = RunCommand("free", "-m").Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
int totalMem = int.Parse(memInfo[6]);
PhysicalMemory = totalMem;
#endif
}
public static SystemInfo GetSystemInfo()
{
SystemInfo systemInfo = new SystemInfo();
systemInfo.CpuCount = Environment.ProcessorCount;
systemInfo.TotalMemory = PhysicalMemory;
systemInfo.Name = HostName;
systemInfo.Timestamp = DateTime.Now;
systemInfo.IpAddress = Ip4Address;
#if !NET_CORE
long availablebytes = 0;
ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
foreach (var o in mos.GetInstances())
{
var mo = (ManagementObject)o;
if (mo["FreePhysicalMemory"] != null)
{
availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
}
}
systemInfo.CpuLoad = (int)(PcCpuLoad.NextValue());
systemInfo.FreeMemory = (int)(availablebytes / (1024 * 1024));
systemInfo.Os = "Windows";
#else
// cpu load
string loadAvg = RunCommand("cat", "/proc/loadavg");
systemInfo.CpuLoad = (int)(float.Parse(loadAvg.Split(' ')[2]) * 100);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var memInfo = RunCommand("free", "-m").Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
int usedMem = int.Parse(memInfo[7]);
systemInfo.FreeMemory = (PhysicalMemory - usedMem);
systemInfo.Os = "Linux";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
//todo:
systemInfo.Os = "OSX";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
//todo:
systemInfo.Os = "Windows";
}
#endif
return systemInfo;
}
public static string RunCommand(string command, string arguments)
{
ProcessStartInfo startInfo = new ProcessStartInfo(command, arguments);
startInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(1500);
return process.StandardOutput.ReadToEnd();
}
}
}