Compare commits
6 Commits
main
...
69dd73a079
Author | SHA1 | Date | |
---|---|---|---|
69dd73a079 | |||
2e9d41fe60 | |||
7e75f3e49e | |||
8e362f7271 | |||
52c4243efc | |||
6248fad147 |
@@ -4,8 +4,8 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// Local Namespaces
|
||||
using Watcher.Data;
|
||||
using Watcher.Models;
|
||||
using Watcher.ViewModels;
|
||||
using Watcher.Services;
|
||||
|
||||
namespace Watcher.Controllers
|
||||
{
|
||||
@@ -18,11 +18,16 @@ namespace Watcher.Controllers
|
||||
// Logging einbinden
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
|
||||
// Daten der Backgroundchecks abrufen
|
||||
private IDashboardStore _DashboardStore;
|
||||
|
||||
// HomeController Constructor
|
||||
public HomeController(AppDbContext context, ILogger<HomeController> logger)
|
||||
public HomeController(AppDbContext context, ILogger<HomeController> logger, IDashboardStore dashboardStore)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
_DashboardStore = dashboardStore;
|
||||
|
||||
}
|
||||
|
||||
// Dashboard unter /home/index
|
||||
@@ -36,6 +41,7 @@ namespace Watcher.Controllers
|
||||
.Where(u => u.Username == preferredUserName)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
Console.WriteLine("Index" + _DashboardStore.NetworkStatus);
|
||||
var viewModel = new DashboardViewModel
|
||||
{
|
||||
ActiveServers = await _context.Servers.CountAsync(s => s.IsOnline),
|
||||
@@ -52,9 +58,11 @@ namespace Watcher.Controllers
|
||||
.ToListAsync(),
|
||||
Containers = await _context.Containers
|
||||
.OrderBy(s => s.Name)
|
||||
.ToListAsync()
|
||||
.ToListAsync(),
|
||||
NetworkStatus = _DashboardStore.NetworkStatus,
|
||||
DatabaseStatus = _DashboardStore.DatabaseStatus
|
||||
};
|
||||
|
||||
//ViewBag.NetworkConnection = _NetworkCheckStore.NetworkStatus;
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
@@ -67,6 +75,8 @@ namespace Watcher.Controllers
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
Console.WriteLine("DashboardStats" + _DashboardStore.NetworkStatus);
|
||||
|
||||
var model = new DashboardViewModel
|
||||
{
|
||||
ActiveServers = await _context.Servers.CountAsync(s => s.IsOnline),
|
||||
@@ -82,11 +92,18 @@ namespace Watcher.Controllers
|
||||
.ToListAsync(),
|
||||
Containers = await _context.Containers
|
||||
.OrderBy(s => s.Name)
|
||||
.ToListAsync()
|
||||
.ToListAsync(),
|
||||
NetworkStatus = _DashboardStore.NetworkStatus,
|
||||
DatabaseStatus = _DashboardStore.DatabaseStatus
|
||||
};
|
||||
|
||||
return PartialView("_DashboardStats", model);
|
||||
}
|
||||
|
||||
public String ReturnNetworkStatus()
|
||||
{
|
||||
return "OK";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ using Serilog;
|
||||
|
||||
using Watcher.Data;
|
||||
using Watcher.Models;
|
||||
using Watcher.Services;
|
||||
//using Watcher.Services;
|
||||
//using Watcher.Workers;
|
||||
|
||||
@@ -15,7 +16,7 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
// Serilog konfigurieren – nur Logs, die nicht von Microsoft stammen
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Information()
|
||||
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning) // <--
|
||||
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.File(
|
||||
"logs/watcher-.log",
|
||||
@@ -33,6 +34,13 @@ builder.Services.AddControllersWithViews();
|
||||
// HttpContentAccessor
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
// Storage Singleton
|
||||
builder.Services.AddSingleton<IDashboardStore, DashboardStore>();
|
||||
|
||||
// Background Services
|
||||
builder.Services.AddHostedService<NetworkCheck>();
|
||||
builder.Services.AddHostedService<DatabaseCheck>();
|
||||
|
||||
|
||||
// ---------- Konfiguration ----------
|
||||
DotNetEnv.Env.Load();
|
||||
@@ -214,4 +222,4 @@ app.MapControllerRoute(
|
||||
pattern: "{controller=Home}/{action=Index}/{id?}"
|
||||
);
|
||||
|
||||
app.Run();
|
||||
app.Run();
|
8
Watcher/Services/DashboardStore.cs
Normal file
8
Watcher/Services/DashboardStore.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Watcher.Services;
|
||||
|
||||
public class DashboardStore : IDashboardStore
|
||||
{
|
||||
public String? NetworkStatus { get; set; }
|
||||
|
||||
public String? DatabaseStatus { get; set; }
|
||||
}
|
67
Watcher/Services/DatabaseCheck.cs
Normal file
67
Watcher/Services/DatabaseCheck.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace Watcher.Services;
|
||||
|
||||
public class DatabaseCheck : BackgroundService
|
||||
{
|
||||
private readonly ILogger<DatabaseCheck> _logger;
|
||||
|
||||
private IDashboardStore _dashboardStore;
|
||||
|
||||
public DatabaseCheck(ILogger<DatabaseCheck> logger, IDashboardStore dashboardStore)
|
||||
{
|
||||
_logger = logger;
|
||||
_dashboardStore = dashboardStore;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var timer = new PeriodicTimer(TimeSpan.FromSeconds(30));
|
||||
|
||||
while (await timer.WaitForNextTickAsync(stoppingToken))
|
||||
{
|
||||
// Hintergrundprozess abwarten
|
||||
await checkDatabaseIntegrity();
|
||||
// 5 Sekdunden Offset
|
||||
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
// String sqliteConnectionString als Argument übergeben
|
||||
public Task checkDatabaseIntegrity()
|
||||
{
|
||||
using var conn = new SqliteConnection("Data Source=./persistence/watcher.db");
|
||||
_logger.LogInformation("Sqlite Integrity-Check started...");
|
||||
|
||||
try
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using var command = conn.CreateCommand();
|
||||
command.CommandText = """
|
||||
SELECT integrity_check FROM pragma_integrity_check;
|
||||
""";
|
||||
|
||||
using var reader = command.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
string status = reader.GetString(0);
|
||||
_dashboardStore.DatabaseStatus = status;
|
||||
_logger.LogInformation("Sqlite DatabaseIntegrity: ${status}", status);
|
||||
}
|
||||
|
||||
conn.Close();
|
||||
}
|
||||
catch (SqliteException e)
|
||||
{
|
||||
conn.Close();
|
||||
_logger.LogError(e.Message);
|
||||
|
||||
// TODO: LogEvent erstellen
|
||||
}
|
||||
|
||||
_logger.LogInformation("Database Integrity-Check finished.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
7
Watcher/Services/IDashboardStore.cs
Normal file
7
Watcher/Services/IDashboardStore.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Watcher.Services;
|
||||
|
||||
public interface IDashboardStore
|
||||
{
|
||||
String? NetworkStatus { get; set; }
|
||||
String? DatabaseStatus { get; set; }
|
||||
}
|
59
Watcher/Services/NetworkCheck.cs
Normal file
59
Watcher/Services/NetworkCheck.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace Watcher.Services;
|
||||
public class NetworkCheck : BackgroundService
|
||||
{
|
||||
private readonly ILogger<NetworkCheck> _logger;
|
||||
|
||||
private IDashboardStore _DashboardStore;
|
||||
|
||||
public NetworkCheck(ILogger<NetworkCheck> logger, IDashboardStore DashboardStore)
|
||||
{
|
||||
_logger = logger;
|
||||
_DashboardStore = DashboardStore;
|
||||
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var timer = new PeriodicTimer(TimeSpan.FromSeconds(30));
|
||||
|
||||
while (await timer.WaitForNextTickAsync(stoppingToken))
|
||||
{
|
||||
// Hintergrundprozess abwarten
|
||||
await checkConnectionAsync();
|
||||
|
||||
// 5 Sekdunden Offset
|
||||
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
public Task checkConnectionAsync()
|
||||
{
|
||||
_logger.LogInformation("Networkcheck started.");
|
||||
|
||||
string host = "8.8.8.8";
|
||||
Ping p = new Ping();
|
||||
|
||||
try
|
||||
{
|
||||
PingReply reply = p.Send(host, 3000);
|
||||
if (reply.Status == IPStatus.Success)
|
||||
{
|
||||
_DashboardStore.NetworkStatus = "online";
|
||||
_logger.LogInformation("Ping successfull. Watcher is online.");
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
_DashboardStore.NetworkStatus = "offline";
|
||||
_logger.LogError("Ping failed. Watcher appears to have no network connection.");
|
||||
|
||||
// LogEvent erstellen
|
||||
}
|
||||
|
||||
_logger.LogInformation("Networkcheck finished.");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
@@ -14,5 +14,8 @@ namespace Watcher.ViewModels
|
||||
public List<LogEvent> RecentEvents { get; set; } = new();
|
||||
public List<Container> Containers { get; set; } = new();
|
||||
|
||||
public String? NetworkStatus { get; set; } = "?";
|
||||
public String? DatabaseStatus { get; set; } = "?";
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,3 +1,4 @@
|
||||
@using Microsoft.IdentityModel.Tokens
|
||||
@model Watcher.ViewModels.DashboardViewModel
|
||||
|
||||
@{
|
||||
@@ -57,18 +58,54 @@
|
||||
<div class="card-body">
|
||||
<h5 class="fw-bold mb-3"><i class="bi bi-heart-pulse me-2 text-danger"></i>Systemstatus</h5>
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-2">
|
||||
<span>Netzwerk</span>
|
||||
<span class="badge bg-success">OK</span>
|
||||
</div>
|
||||
<div class="progress mb-3" style="height: 6px;">
|
||||
<div class="progress-bar bg-success w-100"></div>
|
||||
</div>
|
||||
@if (!Model.NetworkStatus.IsNullOrEmpty())
|
||||
{
|
||||
@if (Model.NetworkStatus == "online")
|
||||
{
|
||||
<div class="d-flex justify-content-between align-items-center mb-2">
|
||||
<span>Netzwerk</span>
|
||||
<span class="badge bg-success">@Model.NetworkStatus</span>
|
||||
</div>
|
||||
<div class="progress mb-3" style="height: 6px;">
|
||||
<div class="progress-bar bg-success w-100"></div>
|
||||
</div>
|
||||
} else if (Model.NetworkStatus == "offline")
|
||||
{
|
||||
<div class="d-flex justify-content-between align-items-center mb-2">
|
||||
<span>Netzwerk</span>
|
||||
<span class="badge bg-danger">@Model.NetworkStatus</span>
|
||||
</div>
|
||||
<div class="progress mb-3" style="height: 6px;">
|
||||
<div class="progress-bar bg-danger w-100"></div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-2">
|
||||
<span>Datenbank</span>
|
||||
<span class="badge bg-success">OK</span>
|
||||
</div>
|
||||
@if (!Model.DatabaseStatus.IsNullOrEmpty())
|
||||
{
|
||||
@if (Model.DatabaseStatus == "$ok")
|
||||
{
|
||||
<div class="d-flex justify-content-between align-items-center mb-2">
|
||||
<span>Datenbank</span>
|
||||
<span class="badge bg-success">OK</span>
|
||||
</div>
|
||||
} else
|
||||
{
|
||||
<div class="d-flex justify-content-between align-items-center mb-2">
|
||||
<span>Datenbank</span>
|
||||
<span class="badge bg-danger">Big Problem</span>
|
||||
</div>
|
||||
}
|
||||
} else
|
||||
{
|
||||
<div class="d-flex justify-content-between align-items-center mb-2">
|
||||
<span>Datenbank</span>
|
||||
|
||||
<span class="badge bg-primary">Missing Data</span>
|
||||
</div>
|
||||
}
|
||||
|
||||
|
||||
<div class="progress mb-3" style="height: 6px;">
|
||||
<div class="progress-bar bg-success w-100"></div>
|
||||
</div>
|
||||
@@ -123,10 +160,10 @@
|
||||
{
|
||||
<li class="list-group-item d-flex justify-content-between align-items-center serverlist">
|
||||
<span>@server.Name</span>
|
||||
<span class="badge bg-info")">
|
||||
<span class="badge bg-info" )">
|
||||
CPU: 30.45%
|
||||
</span>
|
||||
<span class="badge bg-info")">
|
||||
<span class="badge bg-info" )">
|
||||
RAM: 65.09%
|
||||
</span>
|
||||
<span class="badge @(server.IsOnline ? "bg-success" : "bg-danger")">
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user