BILogAnalyseService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Azure.Storage.Blobs;
  2. using Azure.Storage.Blobs.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.SDK.Context.BI;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Models.Cosmos.BI;
  13. namespace TEAMModelOS.SDK.Models.Service.BI
  14. {
  15. public static class BILogAnalyseService
  16. {
  17. /// <summary>
  18. /// 读取全部的防火墙日志文件并分析保存至
  19. /// </summary>
  20. /// <param name="_azureStorage"></param>
  21. /// <param name="site"></param>
  22. /// <returns></returns>
  23. public static async Task<(List<RecCnt> recCnts ,List<string> saveUrls)> GetAllLogAnalyse(AzureStorageFactory _azureStorage,string site = null)
  24. {
  25. var blobClient = _azureStorage.GetBlobContainerClient($"insights-logs-applicationgatewayfirewalllog", name: BIConst.LogChina);
  26. if ($"{site}".Equals(BIConst.Global))
  27. {
  28. blobClient = _azureStorage.GetBlobContainerClient($"insights-logs-applicationgatewayfirewalllog", name: BIConst.Global);
  29. }
  30. List<RecCnt> recCnts = new();
  31. List<string> urls = new();
  32. //地址: y={year}/m={month}/d={day}/h={hour}/m=00/PT1H.json
  33. string logName = "resourceId=/SUBSCRIPTIONS/73B7F9EF-D8B7-4444-9E8D-D80B43BF3CD4/RESOURCEGROUPS/TEAMMODELCHENGDU/PROVIDERS/MICROSOFT.NETWORK/APPLICATIONGATEWAYS/OSFIREWARE";
  34. await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, logName))
  35. {
  36. StringBuilder visits = new("[");
  37. BlobClient tempBlobClient = blobClient.GetBlobClient(blobItem.Name);
  38. BlobDownloadInfo download = tempBlobClient.Download();
  39. var content = download.Content;
  40. string text;
  41. using (var streamReader = new StreamReader(content))
  42. {
  43. while ((text = streamReader.ReadLine()) != null)
  44. {
  45. if (streamReader.EndOfStream)
  46. visits.Append($"{text.ToString()}");
  47. else
  48. visits.Append($"{text.ToString()},");
  49. }
  50. visits.Append("]");
  51. streamReader.Close();
  52. }
  53. string input = visits.ToString();
  54. List<AGInfo> aGInfos = input.ToObject<List<AGInfo>>();
  55. DateTimeOffset dtime = DateTimeOffset.UtcNow;
  56. string cHour = dtime.ToString("yyyyMMddHH");
  57. string cDay = dtime.ToString("yyyyMMdd");
  58. if (aGInfos.Count > 0)
  59. {
  60. cHour = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMddHH")).First();
  61. cDay = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMdd")).First();
  62. }
  63. RecCnt saveCnts = new();
  64. List<RecAppGWInfo> recInfo = aGInfos.Select(s => new RecAppGWInfo { hour = cHour, ip = s.properties.clientIp, api = s.properties.requestUri.Split("?").ToList().Count() > 1 ? s.properties.requestUri.Split("?").ToList()[0] : s.properties.requestUri, hostName = s.properties.hostname }).ToList();
  65. List<RecApiCnt> apiCnt = recInfo.GroupBy(a => a.api).Select(g => new RecApiCnt { api = g.Key, count = g.Count(), hour = cHour, hostName = g.Select(h => h.hostName).Distinct().ToList(), ip = g.Select(i => i.ip).Distinct().ToList() }).ToList();
  66. saveCnts.apiCnt = apiCnt;
  67. List<RecIpCnt> ipCnt = recInfo.GroupBy(a => a.ip).Select(g => new RecIpCnt { ip = g.Key, count = g.Count(), hour = cHour, hostName = g.Select(h => h.hostName).Distinct().ToList(), api = g.Select(i => i.api).Distinct().ToList() }).ToList();
  68. saveCnts.ipCnt = ipCnt;
  69. recCnts.Add(saveCnts);
  70. ////保存存至Blob文件
  71. var url = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(saveCnts.ToJsonString(), $"visitCnt/{cDay}", $"{cHour}.json");
  72. urls.Add(url);
  73. }
  74. return (recCnts, urls);
  75. }
  76. /// <summary>
  77. /// 通过路径获取日志文件并分析结果
  78. /// </summary>
  79. /// <param name="_azureStorage"></param>
  80. /// <param name="path"></param>
  81. /// <param name="site"></param>
  82. /// <returns></returns>
  83. public static async Task<(List<RecCnt> recCnts, List<string> saveUrls)> GetPathAnalyse(AzureStorageFactory _azureStorage,string path, string site = null)
  84. {
  85. var blobClient = _azureStorage.GetBlobContainerClient($"insights-logs-applicationgatewayfirewalllog", name: BIConst.LogChina);
  86. if ($"{site}".Equals(BIConst.Global))
  87. {
  88. blobClient = _azureStorage.GetBlobContainerClient($"insights-logs-applicationgatewayfirewalllog", name: BIConst.LogGlobal);
  89. }
  90. List<RecCnt> recCnts = new();
  91. List<string> urls = new();
  92. string logName = "resourceId=/SUBSCRIPTIONS/73B7F9EF-D8B7-4444-9E8D-D80B43BF3CD4/RESOURCEGROUPS/TEAMMODELCHENGDU/PROVIDERS/MICROSOFT.NETWORK/APPLICATIONGATEWAYS/OSFIREWARE";
  93. await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, path))
  94. {
  95. StringBuilder visits = new("[");
  96. //BlobClient tempBlobClient = blobClient.GetBlobClient(blobItem.Name);
  97. //BlobDownloadInfo download = tempBlobClient.Download();
  98. BlobDownloadInfo download = blobClient.GetBlobClient(blobItem.Name).Download();
  99. var content = download.Content;
  100. string text;
  101. using (var streamReader = new StreamReader(content))
  102. {
  103. while ((text = streamReader.ReadLine()) != null)
  104. {
  105. if (streamReader.EndOfStream)
  106. visits.Append($"{text.ToString()}");
  107. else
  108. visits.Append($"{text.ToString()},");
  109. }
  110. visits.Append("]");
  111. streamReader.Close();
  112. }
  113. string input = visits.ToString();
  114. List<AGInfo> aGInfos = input.ToObject<List<AGInfo>>();
  115. DateTimeOffset dtime = DateTimeOffset.UtcNow;
  116. string cHour = dtime.ToString("yyyyMMddHH");
  117. string cDay = dtime.ToString("yyyyMMdd");
  118. if (aGInfos.Count > 0)
  119. {
  120. cHour = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMddHH")).First();
  121. cDay = aGInfos.Select(s => DateTimeOffset.Parse(s.time).ToString("yyyyMMdd")).First();
  122. }
  123. RecCnt saveCnts = new();
  124. List<RecAppGWInfo> recInfo = aGInfos.Select(s => new RecAppGWInfo { hour = cHour, ip = s.properties.clientIp, api = s.properties.requestUri.Split("?").ToList().Count() > 1 ? s.properties.requestUri.Split("?").ToList()[0] : s.properties.requestUri, hostName = s.properties.hostname }).ToList();
  125. List<RecApiCnt> apiCnt = recInfo.GroupBy(a => a.api).Select(g => new RecApiCnt { api = g.Key, count = g.Count(), hour = cHour, hostName = g.Select(h => h.hostName).Distinct().ToList(), ip = g.Select(i => i.ip).Distinct().ToList() }).ToList();
  126. saveCnts.apiCnt = apiCnt;
  127. List<RecIpCnt> ipCnt = recInfo.GroupBy(a => a.ip).Select(g => new RecIpCnt { ip = g.Key, count = g.Count(), hour = cHour, hostName = g.Select(h => h.hostName).Distinct().ToList(), api = g.Select(i => i.api).Distinct().ToList() }).ToList();
  128. saveCnts.ipCnt = ipCnt;
  129. recCnts.Add(saveCnts);
  130. ////保存存至Blob文件
  131. var url = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(saveCnts.ToJsonString(), $"visitCnt/{cDay}", $"{cHour}.json");
  132. urls.Add(url);
  133. }
  134. return (recCnts, urls);
  135. }
  136. }
  137. }