Simple background monitor for tracking memory, CPU usage, and server status

This commit is contained in:
Lucca Faria Ferri 2023-01-22 10:59:36 -08:00
parent b11eb31c51
commit 629ba9ea05
2 changed files with 132 additions and 3 deletions

View File

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Configuration;
namespace DiIiS_NA
{
public static class Extensions
{
/// <summary>
/// Transforms a timespan to a readable text.
/// E.g.:
/// 1 day, 2 hours, 3 minutes and 4 seconds
/// 5 hours, 6 minutes and 7 seconds
///
/// If over certain threshold (millennium or more) it will only return the number of days.
/// </summary>
/// <param name="span">The timespan to be converted</param>
/// <returns>The readable text</returns>
public static string ToText(this TimeSpan span)
{
List<string> parts = new();
// if days are divided by 365, we have years, otherwise we have months or days
if (span.Days / 365 > 0)
{
// if days are divided by 365, we have years
parts.Add($"{((double)span.Days / 365):F} year{(span.Days / 365 > 1 ? "s" : "")}");
// get months from the remaining days
int months = span.Days % 365 / 30;
if (months > 0)
parts.Add($"{months} month{(months > 1 ? "s" : "")}");
// get remaining days
int days = span.Days % 365 % 30;
if (days > 0)
parts.Add($"{days} day{(days > 1 ? "s" : "")}");
}
else if (span.Days / 30 > 0)
{
// if days are divided by 30, we have months
parts.Add($"{((double)span.Days / 30):F} month{(span.Days / 30 > 1 ? "s" : "")}");
// get remaining days
int days = span.Days % 30;
if (days > 0)
parts.Add($"{days} day{(days > 1 ? "s" : "")}");
}
else if (span.Days > 0)
// if days are not divided by 365 or 30, we have days
parts.Add( $"{span.Days} day{(span.Days > 1 ? "s" : "")}");
if (span.Hours > 0)
parts.Add($"{span.Hours} hour{(span.Hours > 1 ? "s" : "")}");
if (span.Minutes > 0)
parts.Add($"{span.Minutes} minute{(span.Minutes > 1 ? "s" : "")}");
if (span.Seconds > 0)
parts.Add($"{span.Seconds} second{(span.Seconds > 1 ? "s" : "")}");
var result = parts.ToArray();
return string.Join(", ", result[..^1]) + (result.Length > 1 ? " and " : "") + parts[^1];
}
public static string ToSmallText(this TimeSpan span)
{
List<string> parts = new();
if (span.Days / 365 > 0)
{
parts.Add($"{((double)span.Days / 365):F}y");
int months = span.Days % 365 / 30;
if (months > 0)
parts.Add($"{months}m");
int days = span.Days % 365 % 30;
if (days > 0)
parts.Add($"{days}d");
}
else if (span.Days / 30 > 0)
{
parts.Add($"{((double)span.Days / 30):F}m");
int days = span.Days % 30;
if (days > 0)
parts.Add($"{days}d");
}
else if (span.Days > 0)
parts.Add($"{span.Days}d");
if (span.Hours > 0)
parts.Add($"{span.Hours}h");
if (span.Minutes > 0)
parts.Add($"{span.Minutes}m");
if (span.Seconds > 0)
parts.Add($"{span.Seconds}s");
return string.Join(" ", parts);
}
}
}

View File

@ -47,6 +47,7 @@ using Npgsql;
using System;
//Blizzless Project 2022
using System.Data.Common;
using System.Diagnostics;
//Blizzless Project 2022
using System.Globalization;
//Blizzless Project 2022
@ -99,14 +100,50 @@ namespace DiIiS_NA
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
string Name = $"Blizzless: Build {Build}, Stage: {Stage} - {TypeBuild}";
Console.WriteLine(" " + Name);
string name = $"Blizzless: Build {Build}, Stage: {Stage} - {TypeBuild}";
Console.WriteLine(" " + name);
Console.WriteLine("Connected Module: 0x00 - Diablo 3 RoS 2.7.4.84161");
Console.WriteLine("------------------------------------------------------------------------");
Console.ResetColor();
Console.Title = Name;
Console.Title = name;
InitLoggers();
#pragma warning disable CS4014
Task.Run(async () =>
#pragma warning restore CS4014
{
Logger.Info("Started Background Monitor!");
while (true)
{
try
{
var uptime = (DateTime.Now - StartupTime).ToText();
// get total memory from process
var totalMemory =
(double)((double)Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024 / 1024);
// get CPU time
using var proc = Process.GetCurrentProcess();
var cpuTime = proc.TotalProcessorTime;
var text =
$"{name} | {PlayerManager.OnlinePlayers.Count()} onlines in {PlayerManager.OnlinePlayers.Count(s => s.InGameClient.Player.World != null)} worlds | Memory: {totalMemory:0.000} GB | CPU Time: {cpuTime.ToSmallText()} | Uptime: {uptime}";
try
{
Console.Title = text;
await Task.Delay(1000);
}
catch (Exception e)
{
Logger.Info(text);
await Task.Delay(60000);
}
}
catch (Exception e)
{
}
}
});
AchievementManager.Initialize();
Core.Storage.AccountDataBase.SessionProvider.RebuildSchema();
#if DEBUG