IESTimerTrigger.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Azure.Cosmos;
  2. using Azure.Storage.Blobs.Models;
  3. using DinkToPdf;
  4. using DinkToPdf.Contracts;
  5. using HTEXLib.COMM.Helpers;
  6. using Microsoft.Azure.Cosmos.Table;
  7. using Microsoft.Azure.Functions.Worker;
  8. using Microsoft.Azure.Functions.Worker.Http;
  9. using Microsoft.Extensions.Logging;
  10. using StackExchange.Redis;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Net;
  16. using System.Reflection;
  17. using System.Text;
  18. using System.Text.Json;
  19. using System.Threading.Tasks;
  20. using TEAMModelOS.SDK;
  21. using TEAMModelOS.SDK.DI;
  22. using TEAMModelOS.SDK.Extension;
  23. using TEAMModelOS.SDK.Models;
  24. using TEAMModelOS.SDK.Models.Cosmos.Teacher;
  25. using TEAMModelOS.SDK.Models.Service.BI;
  26. using TEAMModelOS.SDK.Models.Table;
  27. using static TEAMModelOS.SDK.Models.Teacher;
  28. namespace TEAMModelOS.FunctionV4.TimeTrigger
  29. {
  30. public class IESTimerTrigger
  31. {
  32. private readonly AzureCosmosFactory _azureCosmos;
  33. private readonly DingDing _dingDing;
  34. private readonly AzureStorageFactory _azureStorage;
  35. private readonly AzureRedisFactory _azureRedis;
  36. private readonly IConverter _converter;
  37. public IESTimerTrigger(IConverter converter, AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis)
  38. {
  39. _azureCosmos = azureCosmos;
  40. _dingDing = dingDing;
  41. _azureStorage = azureStorage;
  42. _azureRedis = azureRedis;
  43. _converter = converter;
  44. } /// <summary>
  45. /// </summary>
  46. /// <param name="req"></param>
  47. /// <param name="log"></param>
  48. /// <returns></returns>
  49. [Function("FireWallFileLog")]
  50. //https://docs.azure.cn/zh-cn/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp
  51. //0 1 * * * * 一天中每小时的第 1 分钟
  52. //0 */1 * * * * 每五分钟一次
  53. public async Task FireWallFileLog([TimerTrigger("0 1 * * * *")] TimerInfo myTimer, ILogger log)
  54. {
  55. try
  56. {
  57. string location = Environment.GetEnvironmentVariable("Option:Location");
  58. var datetime = DateTimeOffset.UtcNow.AddHours(-1);
  59. var y = datetime.Year;
  60. var m = datetime.Month >= 10 ? $"{datetime.Month}" : $"0{datetime.Month}";
  61. var d = datetime.Day >= 10 ? $"{datetime.Day}" : $"0{datetime.Day}";
  62. var h = datetime.Hour >= 10 ? $"{datetime.Hour}" : $"0{datetime.Hour}";
  63. if (location.Equals("China"))
  64. {
  65. string path = $"resourceId=/SUBSCRIPTIONS/73B7F9EF-D8B7-4444-9E8D-D80B43BF3CD4/RESOURCEGROUPS/TEAMMODELCHENGDU/PROVIDERS/MICROSOFT.NETWORK/APPLICATIONGATEWAYS/OSFIREWARE/y={y}/m={m}/d={d}/h={h}/m=00/PT1H.json";
  66. var retn = await BILogAnalyseService.GetPathAnalyse(_azureStorage, path, "LogStorage");
  67. if (retn.recCnts.IsNotEmpty())
  68. {
  69. var topApi = retn.recCnts.SelectMany(x => x.apiCnt).OrderByDescending(o => o.count).Take(5).Select(x => new { x.api, x.ip, x.count });
  70. var topIp = retn.recCnts.SelectMany(x => x.ipCnt).OrderByDescending(o => o.count).Take(5).Select(x => new { x.api, x.ip, x.count });
  71. //https://teammodelos.blob.core.chinacloudapi.cn/0-public/pie-borderRadius.html
  72. await _dingDing.SendBotMarkdown("防火墙日志记录", $"#### 防火墙日志记录\n> 记录时间:{datetime.AddHours(8).ToString("yyyy-MM-dd HH")}\n> ![screenshot]()\n> ###### 发布时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} [地址]({retn.saveUrls.First()}) \n", GroupNames.成都开发測試群組);
  73. }
  74. }
  75. else if (location.Contains("Global"))
  76. {
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. // await _dingDing.SendBotMsg($"FireWallFileLog 防火墙日志记录: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  82. }
  83. }
  84. }
  85. }