MainForm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Management;
  8. using System.Reflection;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using XdCxRhDW.Dto;
  12. using XdCxRhDW.WebApi;
  13. using XdCxRhDW.UI.Lib;
  14. namespace CheckServer
  15. {
  16. public partial class MainForm : Form
  17. {
  18. EnumSvrType svrType = EnumSvrType.CheckSvr;
  19. public MainForm()
  20. {
  21. InitializeComponent();
  22. LogHelper.Logger = info =>
  23. {
  24. try
  25. {
  26. this.Invoke(new Action(() =>
  27. {
  28. this.listBox1.Items.Insert(0, $"{info.LogTime:yyyyMMddHHmmss.fff}--{info.Msg}");
  29. if (this.listBox1.Items.Count > 1000)
  30. this.listBox1.Items.RemoveAt(this.listBox1.Items.Count - 1);
  31. }));
  32. }
  33. catch
  34. { }
  35. };
  36. }
  37. private async void MainForm_LoadAsync(object sender, EventArgs e)
  38. {
  39. if (Debugger.IsAttached)//结束已启动的进程,方便调试
  40. {
  41. var pros = Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name).OrderBy(p => p.StartTime).ToList();
  42. pros.RemoveAt(pros.Count - 1);
  43. foreach (var item in pros)
  44. {
  45. try
  46. {
  47. item.Kill();
  48. }
  49. catch
  50. { }
  51. }
  52. }
  53. try
  54. {
  55. if (Directory.Exists("tmp"))
  56. Directory.Delete("tmp");
  57. }
  58. catch
  59. { }
  60. var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
  61. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  62. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  63. LogHelper.BaseUrl = posPlatformAddr + "/api/";
  64. this.Text = svrType.GetEnumDisplayName() + "-" + svrID;
  65. string localIp;
  66. string getIpUrl = $"{posPlatformAddr}/api/task/getclientip";
  67. while (true)
  68. {
  69. try
  70. {
  71. var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl);
  72. if (rsp.code == 0)
  73. {
  74. await XdCxRhDW.UI.Lib.LogHelper.Error(rsp.msg);
  75. return;
  76. }
  77. else
  78. {
  79. localIp = rsp.data;
  80. }
  81. break;
  82. }
  83. catch (Exception ex)
  84. {
  85. await XdCxRhDW.UI.Lib.LogHelper.Error($"无法连接到平台{getIpUrl},请检测地址是否正确并确保平台防火墙允许端口通过", ex);
  86. await Task.Delay(5000);
  87. }
  88. }
  89. await XdCxRhDW.UI.Lib.LogHelper.Info($"本机IP={localIp}");
  90. IpHelper.SetLocalIp(localIp);
  91. string url;
  92. if (posPlatformAddr.EndsWith("/"))
  93. url = posPlatformAddr + "api/SvrReport/Report";
  94. else
  95. url = posPlatformAddr + "/api/SvrReport/Report";
  96. try
  97. {
  98. Startup.Start(port, $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml", "XdCxRhDW.Dto.xml");
  99. await XdCxRhDW.UI.Lib.LogHelper.Info($"服务启动成功.接口地址http://{localIp}:{port}/swagger");
  100. }
  101. catch
  102. {
  103. try
  104. {
  105. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  106. {
  107. SvrID = svrID,
  108. SvrType = svrType,
  109. ReportType = 1,
  110. BaseHttpAddr = $"http://{IpHelper.GetLocalIp()}:{port}",
  111. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  112. ModuleType = EnumModuleType.Port,
  113. ModuleState = EnumModuleState.Error,
  114. });
  115. }
  116. catch (Exception ex)
  117. {
  118. XdCxRhDW.Framework.LogHelper.Error("端口状态上报异常", ex);
  119. }
  120. }
  121. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  122. {
  123. SvrType = svrType,
  124. SvrID = svrID,
  125. ReportType = 0,
  126. BaseHttpAddr = $"http://{localIp}:{port}",
  127. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  128. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  129. ModuleType = EnumModuleType.Soft,
  130. ModuleState = EnumModuleState.正常,
  131. });
  132. bool preSucceed = false;
  133. long id = 0;
  134. _ = ClearLocalFile();
  135. while (!this.Disposing)
  136. {
  137. try
  138. {
  139. if (!preSucceed)
  140. await XdCxRhDW.UI.Lib.LogHelper.Info($"服务状态上报成功![url={url}]");
  141. preSucceed = true;
  142. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  143. {
  144. SvrType = svrType,
  145. SvrID = svrID,
  146. ID = id++,
  147. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  148. ReportType = 0,
  149. BaseHttpAddr = $"http://{localIp}:{port}",
  150. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  151. ModuleType = EnumModuleType.Soft,
  152. ModuleState = EnumModuleState.空闲,
  153. });
  154. }
  155. catch (Exception ex)
  156. {
  157. await XdCxRhDW.UI.Lib.LogHelper.Error($"服务状态上报异常[url={url}]", ex);
  158. preSucceed = false;
  159. }
  160. var pro = Process.GetCurrentProcess();
  161. if ((pro.PrivateMemorySize64 >> 20) > 4096)//程序内存大于4096MB
  162. {
  163. try
  164. {
  165. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  166. {
  167. SvrType = svrType,
  168. SvrID = svrID,
  169. ReportType = 0,
  170. BaseHttpAddr = $"http://{localIp}:{port}",
  171. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  172. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  173. ModuleType = EnumModuleType.Memory,
  174. ModuleState = EnumModuleState.Error,
  175. });
  176. }
  177. catch (Exception ex)
  178. {
  179. XdCxRhDW.Framework.LogHelper.Error("内存状态上报异常", ex);
  180. }
  181. }
  182. try
  183. {
  184. ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
  185. ManagementObjectCollection disks = diskClass.GetInstances();
  186. foreach (ManagementObject disk in disks)
  187. {
  188. try
  189. {
  190. double totalSpace = Convert.ToDouble(disk["Size"]);
  191. if (totalSpace <= 0) continue;
  192. long freqSpace = Convert.ToInt64(disk["FreeSpace"]);
  193. if (freqSpace / totalSpace < 0.1 && id % 10 == 0)//磁盘可用空间不足10%
  194. {
  195. await XdCxRhDW.UI.Lib.LogHelper.Error($"{disk["Name"].ToString().Replace(":", "")}盘可用空间不足10%");
  196. await HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  197. {
  198. SvrType = svrType,
  199. SvrID = svrID,
  200. ReportType = 0,
  201. BaseHttpAddr = $"http://{localIp}:{port}",
  202. SwaggerAddr = $"http://{localIp}:{port}/Swagger",
  203. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  204. ModuleType = EnumModuleType.Disk,
  205. ModuleState = EnumModuleState.Error,
  206. });
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. XdCxRhDW.Framework.LogHelper.Error("磁盘状态上报异常", ex);
  212. }
  213. }
  214. }
  215. catch
  216. { }
  217. await Task.Delay(6000);
  218. }
  219. }
  220. //清理10分钟之前的文件
  221. private async Task ClearLocalFile()
  222. {
  223. while (true)
  224. {
  225. try
  226. {
  227. var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
  228. var files = Directory.EnumerateFiles(uploadFolder);
  229. foreach (var file in files)
  230. {
  231. FileInfo info = new FileInfo(file);
  232. if (info.CreationTime < DateTime.Now.AddMinutes(-10))
  233. {
  234. try
  235. {
  236. info.Delete();
  237. }
  238. catch (Exception ex)
  239. {
  240. await XdCxRhDW.UI.Lib.LogHelper.Error("清理wwwroot历史文件异常", ex);
  241. }
  242. }
  243. }
  244. }
  245. catch (Exception ex)
  246. {
  247. await XdCxRhDW.UI.Lib.LogHelper.Error("清理wwwroot历史文件异常", ex);
  248. }
  249. await Task.Delay(60 * 1000);
  250. }
  251. }
  252. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  253. {
  254. try
  255. {
  256. var port = Convert.ToInt32(ConfigurationManager.AppSettings["LocalHttpPort"].Trim());
  257. var svrID = ConfigurationManager.AppSettings["SvrID"].Trim();
  258. var posPlatformAddr = ConfigurationManager.AppSettings["PosPlatformAddr"].Trim();
  259. string url;
  260. if (posPlatformAddr.EndsWith("/"))
  261. url = posPlatformAddr + "api/SvrReport/Report";
  262. else
  263. url = posPlatformAddr + "/api/SvrReport/Report";
  264. _ = HttpHelper.PostRequestAsync(url, new SvrStateReportDto()
  265. {
  266. SvrID = svrID,
  267. SvrType = svrType,
  268. ReportType = 1,
  269. BaseHttpAddr = $"http://{IpHelper.GetLocalIp()}:{port}",
  270. DevId = ConfigurationManager.AppSettings["DevID"].Trim(),
  271. ModuleType = EnumModuleType.Soft,
  272. ModuleState = EnumModuleState.Error,
  273. });
  274. }
  275. catch (Exception ex)
  276. {
  277. _=XdCxRhDW.UI.Lib.LogHelper.Error("服务状态上报异常", ex);
  278. }
  279. }
  280. }
  281. }