Program.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Diagnostics;
  2. using Quadarax.Foundation.Core.Logging;
  3. using Quadarax.Foundation.Core.QMonitor;
  4. using Quadarax.Foundation.Core.QMonitor.Emit.TestStructures;
  5. namespace Quadarax.Foundation.Core.QMonitor.Emit // Note: actual namespace depends on the project name.
  6. {
  7. internal class Program
  8. {
  9. private static Random _rnd = new Random();
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("qmonlib.emit * qmon random activity generator");
  13. Console.WriteLine("usage: qmonlib.emit <instance_name>");
  14. var instanceIdentifier = args.Length > 0 ? args[0] : "test";
  15. using (var qmonClient = new QMonClient(instanceIdentifier, QMonClientConfiuration.CreateDefault(), new ConsoleLogger()))
  16. {
  17. var ctx = new Context
  18. {
  19. Client = qmonClient,
  20. Status = new ServiceStatus(ServiceStatus.StatusEnum.Online, DateTime.Now),
  21. AvailableUsers = CreateAvailableUsers()
  22. };
  23. var tmStatus = new Timer(OnServiceStatusTimer, ctx, TimeSpan.Zero, TimeSpan.FromSeconds(5));
  24. var tmProcesses = new Timer(OnProcessesTimer, ctx, TimeSpan.Zero, TimeSpan.FromSeconds(2));
  25. var tmUsers = new Timer(OnUsersTimer, ctx, TimeSpan.Zero, TimeSpan.FromSeconds(2));
  26. Console.WriteLine("Press any key to stop...");
  27. Console.ReadKey();
  28. tmStatus.Dispose();
  29. tmProcesses.Dispose();
  30. tmUsers.Dispose();
  31. Console.WriteLine("Stopped.");
  32. }
  33. }
  34. private static void OnUsersTimer(object? state)
  35. {
  36. Console.WriteLine("* OnUsersTimer");
  37. if (state == null) return;
  38. var ctx = (Context)state;
  39. foreach (var user in ctx.AvailableUsers)
  40. {
  41. var randomBool = _rnd.Next(2) == 1;
  42. if (randomBool)
  43. ctx.Client.Notify(user);
  44. }
  45. }
  46. private static void OnProcessesTimer(object? state)
  47. {
  48. Console.WriteLine("* OnProcessesTimer");
  49. if (state == null) return;
  50. var ctx = (Context)state;
  51. var processes = Process.GetProcesses();
  52. foreach (var process in processes)
  53. {
  54. if (process.Id == 0 || process.Id == 4) continue; // skip system idle process (it's not real process
  55. var ntfProcess = new ProcessItem()
  56. {
  57. Id = process.Id,
  58. Name = process.ProcessName,
  59. Status = process.Responding ? "Running" : "Not Responding",
  60. Path = process.ProcessName + ".exe"
  61. };
  62. ctx.Client.Notify(ntfProcess);
  63. }
  64. }
  65. private static void OnServiceStatusTimer(object? state)
  66. {
  67. Console.WriteLine("* OnServiceStatusTimer");
  68. if (state == null) return;
  69. var ctx = (Context)state;
  70. var randomBool = _rnd.Next(2) == 1;
  71. if (randomBool)
  72. {
  73. if (ctx.Status.Status == ServiceStatus.StatusEnum.Offline)
  74. {
  75. ctx.Status.Status = ServiceStatus.StatusEnum.Online;
  76. ctx.Status.Started = DateTime.Now;
  77. }
  78. }
  79. else
  80. {
  81. ctx.Status.Status = ServiceStatus.StatusEnum.Offline;
  82. }
  83. ctx.Client.Notify(ctx.Status);
  84. }
  85. public static LoggedUser[] CreateAvailableUsers()
  86. {
  87. return new[]
  88. {
  89. new LoggedUser() { Id = 1, FirstName = "Karel", LastName = "Gott", Login = "karel.gott" },
  90. new LoggedUser() { Id = 2, FirstName = "Vasil", LastName = "Bilak", Login = "vasko32" },
  91. new LoggedUser() { Id = 3, FirstName = "Alois", LastName = "Šoule", Login = "alois.soule" },
  92. new LoggedUser() { Id = 4, FirstName = "Květoslav", LastName = "Pytlík", Login = "kvetak55" },
  93. new LoggedUser() { Id = 5, FirstName = "Jindra", LastName = "Kedlub", Login = "kedlubna" },
  94. new LoggedUser() { Id = 6, FirstName = "Vlasta", LastName = "Henych", Login = "retard" },
  95. new LoggedUser() { Id = 7, FirstName = "Petr", LastName = "Volavka", Login = "petro.vol" },
  96. new LoggedUser() { Id = 8, FirstName = "Jan", LastName = "Prášil", Login = "jenda45" },
  97. new LoggedUser() { Id = 9, FirstName = "Donald", LastName = "Paprika", Login = "spicy.leco" },
  98. new LoggedUser() { Id = 10, FirstName = "Mustafa", LastName = "Novák", Login = "vlastenec48" },
  99. };
  100. }
  101. public class Context
  102. {
  103. public QMonClient Client { get; set; }
  104. public ServiceStatus Status { get; set; }
  105. public LoggedUser[] AvailableUsers { get; set; }
  106. }
  107. }
  108. }