133 lines
5.3 KiB
C#
133 lines
5.3 KiB
C#
using MagMan.Data;
|
|
using MagMan.Data.Admin;
|
|
using MagMan.Data.Admin.DbModels;
|
|
using MagMan.Data.Tenant;
|
|
using MagMan.Data.Tenant.DbModels;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using MySqlConnector;
|
|
using NLog;
|
|
using Org.BouncyCastle.Pqc.Crypto.Lms;
|
|
using System.Net.NetworkInformation;
|
|
|
|
namespace MagMan.UI.Health
|
|
{
|
|
public class Checks
|
|
{
|
|
#region Private Fields
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
|
|
#region Public Methods
|
|
|
|
public static async Task<HealthCheckResult> DbIdentity(string dbName)
|
|
{
|
|
using (var appDb = new IdentityContext())
|
|
{
|
|
string description = "Try check Table IdentityUsers";
|
|
List<Data.Admin.DbModels.TableCount> recordList = new List<Data.Admin.DbModels.TableCount>();
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
try
|
|
{
|
|
var dbParam = new MySqlParameter("dbName", dbName);
|
|
string sqlQuery = $"SELECT 'AspNetUsers' AS TableName, COUNT(*) AS Count FROM information_schema.tables WHERE table_schema = @dbName AND table_name = 'AspNetUsers' LIMIT 1;";
|
|
var table = await appDb.DbSetCounts.FromSqlRaw(sqlQuery, dbParam).ToListAsync();
|
|
|
|
// provo a controllare se ho tab utenti
|
|
if (table != null && table.Count > 0)
|
|
{
|
|
// controllo valore
|
|
if (table[0].Count > 0)
|
|
{
|
|
description = $"Check IdentityUsers table, found!";
|
|
return HealthCheckResult.Healthy(description, healthCheckData);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Errore in esecuzione DbIdentity");
|
|
}
|
|
|
|
return HealthCheckResult.Degraded(description + $" {dbName}", null, healthCheckData);
|
|
}
|
|
}
|
|
|
|
public static async Task<HealthCheckResult> CustomersCount()
|
|
{
|
|
string description = "Try check CUSTOMERS table";
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
using (MultiTenantContext localDbCtx = new MultiTenantContext())
|
|
{
|
|
var dbCount = await localDbCtx
|
|
.DbSetCustomers
|
|
.CountAsync();
|
|
if (dbCount > 0)
|
|
{
|
|
description = $"Check CUSTOMERS table, found {dbCount} records";
|
|
healthCheckData.Add("Count", dbCount);
|
|
return HealthCheckResult.Healthy(description, healthCheckData);
|
|
}
|
|
}
|
|
|
|
await Task.Delay(1);
|
|
return HealthCheckResult.Unhealthy(description + $" NO RECORD found", null, healthCheckData);
|
|
}
|
|
|
|
public static async Task<HealthCheckResult> DbUserRoot(string dbName)
|
|
{
|
|
List<MagMan.Data.Admin.DbModels.UserPriv> userList = new List<MagMan.Data.Admin.DbModels.UserPriv>();
|
|
string description = "Try check MySql User table";
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
try
|
|
{
|
|
// provo a controllare se ho tab utenti
|
|
using (var adminDb = new ServerAdminContext())
|
|
{
|
|
userList = await adminDb.UserList.ToListAsync();
|
|
}
|
|
if (userList.Count > 0)
|
|
{
|
|
description = $"Check MySql User table, found {userList.Count} records";
|
|
return HealthCheckResult.Healthy(description, healthCheckData);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Errore in esecuzione DbUserRoot");
|
|
}
|
|
return HealthCheckResult.Unhealthy(description + $" {dbName}", null, healthCheckData);
|
|
}
|
|
|
|
public static async Task<HealthCheckResult> PingCheck(string hostName)
|
|
{
|
|
if (hostName.Contains("::") || hostName.Contains("0.0.0.0"))
|
|
{
|
|
return HealthCheckResult.Unhealthy($"Wrong Hostname: {hostName}");
|
|
}
|
|
else
|
|
{
|
|
using (var thePing = new Ping())
|
|
{
|
|
var pingResult = await thePing.SendPingAsync(hostName);
|
|
var description = $"Ping to {hostName}";
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
healthCheckData.Add("RoundTripMS", pingResult.RoundtripTime);
|
|
healthCheckData.Add("ActualIPAddress", pingResult.Address.ToString());
|
|
if (pingResult.Status == IPStatus.Success)
|
|
{
|
|
description += $" - {pingResult.RoundtripTime}ms";
|
|
return HealthCheckResult.Healthy(description, healthCheckData);
|
|
}
|
|
return HealthCheckResult.Unhealthy(description + $" {hostName}", null, healthCheckData);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
}
|