forked from nd0104/IT_Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.cs
363 lines (310 loc) · 12.7 KB
/
monitor.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
using System.Management;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Threading;
namespace CobasITMonitor
{
class monitor
{
#region 获取磁盘可用空间
/// <summary>
/// 获取本地磁盘,不包括光驱等
/// </summary>
/// <returns></returns>
public List<string> getFixDisk()
{
List<string> fixDiskList = new List<string>();
foreach (DriveInfo di in DriveInfo.GetDrives())
{
if (di.DriveType == DriveType.Fixed)
{
fixDiskList.Add(di.Name);
}
}
return fixDiskList;
}
[DllImport("kernel32.dll")]
private static extern bool GetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes);
public float getDiskFreePercent(string driveName)
{
ulong freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes;
if (!driveName.EndsWith(":\\")) driveName += ":\\";
GetDiskFreeSpaceEx(driveName, out freeBytesAvailable, out totalNumberOfBytes, out totalNumberOfFreeBytes);
float freeNum = float.Parse(totalNumberOfFreeBytes.ToString());
float totalNum = float.Parse(totalNumberOfBytes.ToString());
float result = 0;
if(freeNum>0 && totalNum>0)
{
result = freeNum / (1024 * 1024 * 1024);
}
return result;
}
#endregion
#region 获取CPU使用百分百
public float getCpuUsedPercent()
{
PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
float cpu = cpuCounter.NextValue();
Thread.Sleep(500);
cpu = cpuCounter.NextValue();
return cpu;
}
#endregion
#region 内存使用率
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
[DllImport("kernel32.dll")]
private static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
public uint getRamUsePercent()
{
MEMORY_INFO MemInfo;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
return MemInfo.dwMemoryLoad;
//PerformanceCounter ramCounter;
//ramCounter = new PerformanceCounter("Memory", "Available MBytes");
//return ramCounter.NextValue() + "MB";
//return MemoryLoad;
}
#endregion
#region IP连通性
public bool CmdPing(string strIp)
{
bool result = false;
Process p = new Process(); p.StartInfo.FileName = "cmd.exe";//设定程序名
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true;//重定向标准输入
p.StartInfo.RedirectStandardOutput = true;//重定向标准输出
p.StartInfo.RedirectStandardError = true;//重定向错误输出
p.StartInfo.CreateNoWindow = true;//设置不显示窗口
p.Start();
p.StandardInput.WriteLine("ping " + strIp);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.Close();
if (strRst.IndexOf("TTL=") > -1 && strRst.IndexOf("Reply from") > -1)
{
result = true;
}
if (strRst.IndexOf("TTL=") > -1 && strRst.IndexOf("回复") > -1)
{
result = true;
}
if (strRst.IndexOf("(100% loss)") > -1 )
{
result = false;
}
if (strRst.IndexOf("timed out") > -1)
{
result = false;
}
if (strRst.IndexOf("超时") > -1)
{
result = false;
}
return result;
}
#endregion
#region 日志读取
public List<EventLogEntry> getErrorEventLog(string eventType)
{
List<EventLogEntry> eventLogList = new List<EventLogEntry>();
EventLog MySystemEvent = new EventLog(); //日志对象
MySystemEvent.Log = eventType.ToString(); //日志类型
EventLogEntryCollection MyEventCollection = MySystemEvent.Entries; //获得日志的记录集合
try
{
int Count = MyEventCollection.Count; //记录的长度
for (int i = 0; i < Count; i++)
{
EventLogEntry MyEntry = MyEventCollection[Count - i - 1];
if (MyEntry.EntryType == EventLogEntryType.Error)
{
if (MyEntry.TimeGenerated.Date == DateTime.Today) //只发送当日的警报
{
eventLogList.Add(MyEntry);
}
}
}
}
catch
{
}
return eventLogList;
}
public List<EventLogEntry> getWarnEventLog(string eventType)
{
List<EventLogEntry> eventLogList = new List<EventLogEntry>();
EventLog MySystemEvent = new EventLog(); //日志对象
MySystemEvent.Log = eventType.ToString(); //日志类型
EventLogEntryCollection MyEventCollection = MySystemEvent.Entries; //获得日志的记录集合
try
{
int Count = MyEventCollection.Count; //记录的长度
for (int i = 0; i < Count; i++)
{
EventLogEntry MyEntry = MyEventCollection[Count - i - 1];
if (MyEntry.EntryType == EventLogEntryType.Warning)
{
if (MyEntry.TimeGenerated.Date == DateTime.Today) //只发送当日的警报
{
eventLogList.Add(MyEntry);
}
}
}
}
catch
{
}
return eventLogList;
}
#endregion
public string[] readparameter(string jiedian)
{
Tool_Class.DESFileClass.DecryptFile("config.txt", "345.txt", "123");
FileStream fs = new FileStream(@"345.txt", FileMode.Open);
Tool_Class.IO_tool dd = new Tool_Class.IO_tool();
string w = dd.Readfile(fs);
string[] s = Regex.Split(w, jiedian, RegexOptions.IgnoreCase);
string ee = s[1];
string[] ss = Regex.Split(ee, ";", RegexOptions.IgnoreCase);
string sourceDir = @"..\Debug";
string[] txtList = Directory.GetFiles(sourceDir, "345.txt");
foreach (string f in txtList)
{
//File.Delete(f);
}
return ss;
}
public class eeeee
{
public string eee {get; set;}
}
eeeee bbb = new eeeee();
eeeee ddd = new eeeee();
public string readconfig(string jiedian,string set)
{
Tool_Class.DESFileClass.DecryptFile("config.txt", "345.txt", "123");
FileStream fs = new FileStream(@"345.txt", FileMode.Open);
Tool_Class.IO_tool dd = new Tool_Class.IO_tool();
string w = dd.Readfile(fs);
string[] s = Regex.Split(w, jiedian, RegexOptions.IgnoreCase);
string ee = s[1];
string[] ss = Regex.Split(ee, ";", RegexOptions.IgnoreCase);
List<string> ipList = new List<string>();
foreach (string aa in ss)
{
ipList.Add(aa);
}
string ttttt = "\n" + set;
foreach (string ttt in ipList)
{
string[] ssss = Regex.Split(ttt, "=", RegexOptions.IgnoreCase);
string tttt = ttttt;
if (ssss[0].Substring(1).ToString() == tttt)
{
bbb.eee = ssss[1];
}
//return ssss[0].Substring(1).ToString();
}
string sourceDir = @"..\Debug";
string[] txtList = Directory.GetFiles(sourceDir, "345.txt");
foreach (string f in txtList)
{
File.Delete(f);
}
return bbb.eee;
}
public void writeconfig(string jiedian, string set,string value)
{
Tool_Class.DESFileClass.DecryptFile("config.txt", "345.txt", "123");
FileStream fs = new FileStream(@"345.txt", FileMode.Open);
Tool_Class.IO_tool dd = new Tool_Class.IO_tool();
string w = dd.Readfile(fs);
string[] s = Regex.Split(w, jiedian, RegexOptions.IgnoreCase);
string ee = s[1];
string[] ss = Regex.Split(ee, ";", RegexOptions.IgnoreCase);
List<string> ipList = new List<string>();
foreach (string aa in ss)
{
ipList.Add(aa);
}
string ttttt = "\n" + set;
foreach (string ttt in ipList)
{
string[] ssss = Regex.Split(ttt, "=", RegexOptions.IgnoreCase);
string tttt = ttttt;
if (ssss[0].Substring(1).ToString() == tttt)
{
bbb.eee = ssss[0] + "=" + ssss[1];
ddd.eee = ssss[0] + "=";
}
//return ssss[0].Substring(1).ToString();
}
string values = bbb.eee;
string[] wt = Regex.Split(w, values, RegexOptions.IgnoreCase);
string valuee = ddd.eee + "=" + value;
string total = wt[0] + valuee + wt[1];
string sourceDir = @"..\Debug";
string[] txtList = Directory.GetFiles(sourceDir, "345.txt");
foreach (string f in txtList)
{
File.Delete(f);
}
dd.Write2file(@"345.txt",total);
string[] configList = Directory.GetFiles(sourceDir, "config.txt");
foreach (string f in configList)
{
File.Delete(f);
}
Tool_Class.DESFileClass.EncryptFile("345.txt", "config.txt", "123");
string[] txtList2 = Directory.GetFiles(sourceDir, "345.txt");
foreach (string f in txtList2)
{
File.Delete(f);
}
}
public void jiamiconfig()
{
Tool_Class.DESFileClass.EncryptFile("345.txt", "config.txt", "123");
string sourceDir = @"..\Debug";
string[] txtList2 = Directory.GetFiles(sourceDir, "345.txt");
foreach (string f in txtList2)
{
File.Delete(f);
}
}
public void jiemiconfig()
{
Tool_Class.DESFileClass.DecryptFile("config.txt", "345.txt", "123");
string sourceDir = @"..\Debug";
string[] txtList2 = Directory.GetFiles(sourceDir, "config.txt");
foreach (string f in txtList2)
{
File.Delete(f);
}
}
}
}