DataCollector.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. using System.Text;
  2. using LibreHardwareMonitor.Hardware;
  3. using MinecraftStatus;
  4. using SteamQueryNet;
  5. using SteamQueryNet.Interfaces;
  6. using System.Text.Json;
  7. using Microsoft.Extensions.Configuration;
  8. using VeloeMonitorDataCollector.DatabaseConnectors;
  9. using VeloeMonitorDataCollector.Models;
  10. using Serilog;
  11. using VeloeMonitorDataCollector.Dependencies;
  12. namespace VeloeMonitorDataCollector
  13. {
  14. public class DataCollector
  15. {
  16. private Dictionary<string, Task> _updaterTasks; //TODO i can use an array here
  17. CancellationToken _token;
  18. CancellationTokenSource _cancellationTokenSource;
  19. Serilog.ILogger _logger;
  20. Computer? _computerHardware;
  21. private List<IDataSendable> _sendToDb; //TODO and here
  22. //Exception thrown on checking values
  23. /// <exception cref="ArgumentNullException">when there is no value in INI file or this value is not declared.</exception>
  24. /// <exception cref="ArgumentOutOfRangeException">when value in INI file does not match in range. Example: Port value range is 1..65565]</exception>
  25. /// <exception cref="FormatException">when value can not be parsed to needed type. Expample: Port value can't be NaN</exception>
  26. /// <exception cref="OverflowException">when provided value caused overflowed return variable in parse method.</exception>
  27. public DataCollector(in IConfiguration data,in Serilog.ILogger logger)
  28. {
  29. _updaterTasks = new Dictionary<string, Task>();
  30. _cancellationTokenSource = new CancellationTokenSource();
  31. _token = _cancellationTokenSource.Token;
  32. _logger = logger;
  33. //ini file check
  34. //is it needed?
  35. if (!File.Exists("config.ini"))
  36. {
  37. File.WriteAllText("config.ini",
  38. "#[Hardware]\n#hardware = true\n#hardwareUpdateInterval = true\n\n#[MySQL]\n#server = 127.0.0.1\n#port = 8806\n#uid = User\n#pwd = Password\n#database = values\n\n#[WebSoket]\n#url = http://192.168.1.2:5000\n\n#[MinecraftServer]\n#Ip = 127.0.0.1\n#Port = 25565\n#Type = Minecraft\n#updateInterval = 30\n\n#[SteamAPIServer]\n#Ip = 127.0.0.1\n#Port = 27015\n#Type = Steam\n#updateInterval = 30\n\n#[Gamespy3Server]\n#Ip = 127.0.0.1\n#Port = 5446\n#Type = Gamespy3\n#updateInterval = 30\n\n#[Gamespy2Server]\n#Ip = 127.0.0.1\n#Port = 10480\n#Type = Gamespy2\n#updateInterval = 30");
  39. throw new FileNotFoundException("config.ini does not exist! Default config was created as an example.");
  40. }
  41. //check read/write
  42. //is it needed
  43. FileStream config = File.Open("config.ini", FileMode.Open, FileAccess.ReadWrite);
  44. config.Close();
  45. config.Dispose();
  46. var hardware = data.GetSection("Hardware");
  47. //ini hardvare section validation
  48. if (hardware["hardware"] != "true" && hardware["hardware"] != "false" || hardware["hardware"] is null)
  49. {
  50. hardware["hardware"] = "false";
  51. }
  52. //configuring database connections
  53. var dbSections = data
  54. .GetChildren()
  55. .Where(section =>
  56. section.Path is ("MySQL" or "InfluxDB" or "TimescaleDB" or "WebSoket"))
  57. .ToArray();
  58. _sendToDb = new List<IDataSendable>();
  59. if (!dbSections.Any())
  60. {
  61. _logger.Information("No databases detected.");
  62. }
  63. else
  64. {
  65. foreach (var dbSection in dbSections)
  66. {
  67. //init db connections
  68. switch (dbSection.Path)
  69. {
  70. case "MySQL":
  71. try
  72. {
  73. MySqlConnector mySqlDb = new MySqlConnector(dbSection, logger);
  74. _sendToDb.Add(mySqlDb);
  75. }
  76. catch (Exception ex)
  77. {
  78. logger.Warning("Database connector not confugured properly. It won't be added in working configuration.");
  79. }
  80. break;
  81. case "WebSoket":
  82. try
  83. {
  84. SignalRConnector signalRWebApp = new(dbSection, logger);
  85. _sendToDb.Add(signalRWebApp);
  86. }
  87. catch (Exception ex)
  88. {
  89. logger.Warning("SignalR connector not confugured properly. It won't be added in working configuration.");
  90. }
  91. break;
  92. case "TimescaleDB":
  93. throw new NotImplementedException();
  94. case "InfluxDB":
  95. throw new NotImplementedException();
  96. }
  97. }
  98. }
  99. // checking params in game servers sections
  100. // configuring game servers
  101. var gameServersSections = data
  102. .GetChildren()
  103. .Where (section =>
  104. section.Key is not ("MySQL" or "InfluxDB" or"TimescaleDB" or "WebSoket" or "Hardware"))
  105. .ToArray();
  106. if (!gameServersSections.Any())
  107. {
  108. //add commented block with default example
  109. _logger.Information("No game servers detected.");
  110. }
  111. else
  112. foreach (var gameServer in gameServersSections)
  113. {
  114. //Console.WriteLine(gameServer.Key);
  115. if (gameServer["Ip"] == null ||
  116. gameServer["Port"] == null ||
  117. gameServer["Type"] == null)
  118. {
  119. throw new ArgumentNullException($"Some parameters for {gameServer.Key} are missing.");
  120. }
  121. if (!(Int32.Parse(gameServer["Port"]) is >= 1 and <= 65565))
  122. throw new ArgumentOutOfRangeException(gameServer["Port"]);
  123. foreach (char c in gameServer.Key)
  124. {
  125. if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')))
  126. {
  127. throw new ArgumentException($"Not allowed symbols in {gameServer.Key}. Only letters and numbers are allowed.");
  128. }
  129. }
  130. }
  131. // create task for hardware update
  132. if (bool.Parse(hardware["hardware"]))
  133. {
  134. _computerHardware = new Computer()
  135. {
  136. IsCpuEnabled = true,
  137. IsMemoryEnabled = true,
  138. IsStorageEnabled = true,
  139. };
  140. _computerHardware.Open();
  141. SetValuesTimeZero(); //why
  142. //check database table configuration
  143. foreach (var database in _sendToDb)
  144. {
  145. var hardwareConfiguration = UpdateHardware();
  146. if (database.CheckHardware(hardwareConfiguration) is false)
  147. {
  148. throw new Exception(
  149. "Table configuration for hardware is invalid. Repair it manually or drop table and restart program.");
  150. }
  151. }
  152. int updateIntervalHardware;
  153. if (!Int32.TryParse(hardware["updateIntervalHardware"], out updateIntervalHardware))
  154. {
  155. _logger.Warning("Unable to parse updateIntervalHardware. Used default value.");
  156. updateIntervalHardware = 1;
  157. }
  158. _updaterTasks.Add("hardware", new Task(async () =>
  159. {
  160. while (!_token.IsCancellationRequested)
  161. {
  162. //Console.WriteLine(_token.IsCancellationRequested);
  163. var data = UpdateHardware();
  164. if (_sendToDb != null)
  165. foreach (var dbController in _sendToDb)
  166. {
  167. dbController.SendHardware(data);
  168. }
  169. await Task.Delay(TimeSpan.FromSeconds(updateIntervalHardware));
  170. }
  171. }, _token));
  172. }
  173. //create tasks for game servers updates
  174. foreach (var section in gameServersSections)
  175. {
  176. //check database table configuration
  177. foreach (var database in _sendToDb)
  178. { //Gamespy3 servers ignores it
  179. //Tablecheck in BuildUpdater method
  180. if (database.CheckGameServer(section.Key, section["Type"]) is false)
  181. {
  182. throw new Exception(
  183. $"Table {section.Key} configuration is invalid. Repair it manually or drop table and restart program.");
  184. }
  185. }
  186. int interval;
  187. if (!Int32.TryParse(section["updateInterval"], out interval))
  188. {
  189. _logger.Information("Unable to parse updateInterval. Used default value.");
  190. interval = 1;
  191. }
  192. BuildUpdater(section.Path,
  193. section["Ip"],
  194. Int32.Parse(section["Port"]),
  195. section["Type"], interval);
  196. }
  197. }
  198. public void Start()
  199. {
  200. foreach (var task in _updaterTasks)
  201. {
  202. _logger.Information("{0} started!",task.Key);
  203. task.Value.Start();
  204. }
  205. }
  206. public void Stop()
  207. {
  208. _cancellationTokenSource.Cancel();
  209. bool tasksStillActive = true;
  210. while (tasksStillActive)
  211. {
  212. tasksStillActive = false;
  213. foreach (var task in _updaterTasks)
  214. if (task.Value.Status == TaskStatus.Running) tasksStillActive = true;
  215. Task.Delay(TimeSpan.FromMilliseconds(250));
  216. }
  217. if (_computerHardware is not null)
  218. _computerHardware.Close();
  219. //wait to Tasks to end their execution
  220. //close all db connections
  221. foreach (var database in _sendToDb)
  222. {
  223. database.Close();
  224. }
  225. }
  226. private void SetValuesTimeZero()
  227. {
  228. if (_computerHardware is null)
  229. return;
  230. foreach (var hardware in _computerHardware.Hardware)
  231. {
  232. foreach (var sensor in hardware.Sensors)
  233. {
  234. sensor.ValuesTimeWindow = TimeSpan.Zero;
  235. }
  236. }
  237. }
  238. private Dictionary<string, float> UpdateHardware()
  239. {
  240. Dictionary<string, float> output = new Dictionary<string, float>();
  241. if (_computerHardware is null)
  242. return output;
  243. foreach (var hardware in _computerHardware.Hardware)
  244. {
  245. hardware.Update();
  246. if (hardware.HardwareType is HardwareType.Cpu)
  247. output.Add("cpuload", hardware.Sensors[8].Value.GetValueOrDefault());
  248. if (hardware.HardwareType is HardwareType.Memory)
  249. {
  250. output.Add("ramavailable", hardware.Sensors[1].Value.GetValueOrDefault());
  251. output.Add("ramused", hardware.Sensors[0].Value.GetValueOrDefault());
  252. output.Add("ramload", hardware.Sensors[2].Value.GetValueOrDefault());
  253. }
  254. if (hardware.HardwareType is HardwareType.Storage)
  255. {
  256. StringBuilder sb = new StringBuilder();
  257. foreach (char c in hardware.Name.ToLower())
  258. {
  259. if (c is not (' ' or '/' or '-'))
  260. { sb.Append(c); }
  261. }
  262. output.Add(sb.ToString(), hardware.Sensors[1].Value.GetValueOrDefault());
  263. }
  264. }
  265. return output;
  266. }
  267. private void BuildUpdater(string name ,string ip, int port, string type, int interval)
  268. {
  269. switch (type)
  270. {
  271. case "Minecraft":
  272. _updaterTasks.Add(name, new Task(async () => {
  273. while (!_token.IsCancellationRequested)
  274. {
  275. await Task.Delay(TimeSpan.FromSeconds(interval));
  276. McStatus? data = null;
  277. data = UpdateMinecraft(ip, port);
  278. if (data is null)
  279. continue;
  280. if (_sendToDb is null)
  281. continue;
  282. foreach (var dbController in _sendToDb)
  283. {
  284. //_logger.Debug("Sending {0}", name);
  285. dbController.SendMinecraft(data,name);
  286. }
  287. }
  288. }, _token));
  289. break;
  290. case "Steam":
  291. _updaterTasks.Add(name, new Task(async () => {
  292. while (!_token.IsCancellationRequested)
  293. {
  294. await Task.Delay(TimeSpan.FromSeconds(interval));
  295. SteamData? data = null;
  296. data = await UpdateSteam(ip, port, interval);
  297. if (data is null)
  298. continue;
  299. if (_sendToDb is null)
  300. continue;
  301. foreach (var dbController in _sendToDb)
  302. {
  303. dbController.SendSteam(data.Value,name);
  304. }
  305. }
  306. }, _token));
  307. break;
  308. case "Gamespy3":
  309. _updaterTasks.Add(name, new Task(async () => {
  310. try
  311. {
  312. Gs3Status server = new Gs3Status(ip, port);
  313. /*
  314. bool dataIsNull = true;
  315. while (dataIsNull && !_token.IsCancellationRequested)
  316. {
  317. var data = server.GetStatus();
  318. if (data is null)
  319. continue;
  320. if (_sendToDb is null)
  321. dataIsNull = false;
  322. foreach(var dbController in _sendToDb)
  323. {
  324. dbController.CheckGamespy3(data, name);
  325. }
  326. }
  327. */
  328. while (!_token.IsCancellationRequested)
  329. {
  330. try
  331. {
  332. await Task.Delay(TimeSpan.FromSeconds(interval));
  333. var data = server.GetStatus();
  334. if (data is null)
  335. continue;
  336. if (_sendToDb is null)
  337. continue;
  338. foreach (var dbController in _sendToDb)
  339. {
  340. dbController.SendGamespy3(data, name);
  341. }
  342. }
  343. catch (System.Net.Sockets.SocketException ex)
  344. {
  345. _logger.Debug(ex.Message);
  346. }
  347. }
  348. }
  349. catch(System.Net.Sockets.SocketException ex)
  350. {
  351. _logger.Debug(ex.Message);
  352. }
  353. }, _token));
  354. break;
  355. case "Gamespy2":
  356. _updaterTasks.Add(name, new Task(async () => {
  357. try
  358. {
  359. Gs2Status server = new Gs2Status(ip, port);
  360. while (!_token.IsCancellationRequested)
  361. {
  362. try
  363. {
  364. await Task.Delay(TimeSpan.FromSeconds(interval));
  365. var data = server.GetStatus();
  366. if (data is null)
  367. continue;
  368. if (_sendToDb is null)
  369. continue;
  370. foreach (var dbController in _sendToDb)
  371. {
  372. dbController.SendGamespy2(data, name);
  373. }
  374. }
  375. catch (System.Net.Sockets.SocketException ex)
  376. {
  377. _logger.Debug(ex.Message);
  378. }
  379. }
  380. }
  381. catch (System.Net.Sockets.SocketException ex)
  382. {
  383. _logger.Debug(ex.Message);
  384. }
  385. }, _token));
  386. break;
  387. }
  388. }
  389. private McStatus? UpdateMinecraft(string ip, int port)
  390. {
  391. McStatus? status = null;
  392. try
  393. {
  394. status = McStatus.GetStatus(ip, port);
  395. }
  396. catch (Exception ex)
  397. {
  398. _logger.Debug(ex.Message);
  399. }
  400. if (status != null)
  401. return status;
  402. return null;
  403. }
  404. private async Task<SteamData?> UpdateSteam(string ip, int port, int interval)
  405. {
  406. ServerQuery serverQuery = new ServerQuery();
  407. try
  408. {
  409. serverQuery.SendTimeout = 2000;
  410. serverQuery.ReceiveTimeout = 2000;
  411. SteamData? output = null;
  412. serverQuery.Connect(ip, (ushort)port);
  413. var serverInfo = await serverQuery.GetServerInfoAsync();
  414. var serverPlayers = await serverQuery.GetPlayersAsync();
  415. output = new SteamData
  416. {
  417. ServerInfo = serverInfo,
  418. Players = serverPlayers
  419. };
  420. if (output is null)
  421. return null;
  422. return output;
  423. }
  424. catch (TimeoutException ex)
  425. {
  426. _logger.Debug($"{{0}}:{{1}} {ex.Message}", ip, port);
  427. return null;
  428. }
  429. catch (Exception ex)
  430. {
  431. _logger.Debug(ex.Message);
  432. return null;
  433. }
  434. }
  435. }
  436. }