138 lines
5.6 KiB
C#
138 lines
5.6 KiB
C#
using GWMS.Data;
|
|
using GWMS.Data.DatabaseModels;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GWMS.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 UserIdentityDbContext())
|
|
{
|
|
string description = "Try check Table IdentityUsers";
|
|
List<TableCount> recordList = new List<TableCount>();
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
try
|
|
{
|
|
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 Task.FromResult(appDb.DbSetCounts.FromSqlRaw(sqlQuery).ToList());
|
|
|
|
// 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> DbPlantTable(string dbName)
|
|
{
|
|
using (var appDb = new GWMSContext())
|
|
{
|
|
string description = "Try check Table PlantLog";
|
|
List<GWMS.Data.DatabaseModels.PlantDetailModel> recordList = new List<GWMS.Data.DatabaseModels.PlantDetailModel>();
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
try
|
|
{
|
|
// provo a controllare se ho tab utenti
|
|
recordList = await Task.FromResult(appDb.DbSetPlant.ToList()).ConfigureAwait(false);
|
|
if (recordList.Count > 0)
|
|
{
|
|
description = $"Check PlantDetail table, found {recordList.Count} records";
|
|
return HealthCheckResult.Healthy(description, healthCheckData);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Errore in esecuzione PlantDetail Table");
|
|
}
|
|
|
|
return HealthCheckResult.Degraded(description + $" {dbName}", null, healthCheckData);
|
|
}
|
|
}
|
|
|
|
public static async Task<HealthCheckResult> DbUserRoot(string dbName)
|
|
{
|
|
using (var adminDb = new AdminContext())
|
|
{
|
|
string description = "Try check MySql User table";
|
|
List<GWMS.Data.DatabaseModels.UserPriv> userList = new List<GWMS.Data.DatabaseModels.UserPriv>();
|
|
var healthCheckData = new Dictionary<string, object>();
|
|
try
|
|
{
|
|
// provo a controllare se ho tab utenti
|
|
userList = await Task.FromResult(adminDb.UserList.ToList()).ConfigureAwait(false);
|
|
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
|
|
}
|
|
} |