feature/network-check #20
@@ -4,8 +4,8 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
// Local Namespaces
|
// Local Namespaces
|
||||||
using Watcher.Data;
|
using Watcher.Data;
|
||||||
using Watcher.Models;
|
|
||||||
using Watcher.ViewModels;
|
using Watcher.ViewModels;
|
||||||
|
using Watcher.Services;
|
||||||
|
|
||||||
namespace Watcher.Controllers
|
namespace Watcher.Controllers
|
||||||
{
|
{
|
||||||
@@ -52,9 +52,9 @@ namespace Watcher.Controllers
|
|||||||
.ToListAsync(),
|
.ToListAsync(),
|
||||||
Containers = await _context.Containers
|
Containers = await _context.Containers
|
||||||
.OrderBy(s => s.Name)
|
.OrderBy(s => s.Name)
|
||||||
.ToListAsync()
|
.ToListAsync(),
|
||||||
};
|
};
|
||||||
|
//ViewBag.NetworkConnection = ReturnNetworkStatus();
|
||||||
return View(viewModel);
|
return View(viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,5 +88,10 @@ namespace Watcher.Controllers
|
|||||||
return PartialView("_DashboardStats", model);
|
return PartialView("_DashboardStats", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String ReturnNetworkStatus()
|
||||||
|
{
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,6 +5,7 @@ using Serilog;
|
|||||||
|
|
||||||
using Watcher.Data;
|
using Watcher.Data;
|
||||||
using Watcher.Models;
|
using Watcher.Models;
|
||||||
|
using Watcher.Services;
|
||||||
//using Watcher.Services;
|
//using Watcher.Services;
|
||||||
//using Watcher.Workers;
|
//using Watcher.Workers;
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||
// Serilog konfigurieren – nur Logs, die nicht von Microsoft stammen
|
// Serilog konfigurieren – nur Logs, die nicht von Microsoft stammen
|
||||||
Log.Logger = new LoggerConfiguration()
|
Log.Logger = new LoggerConfiguration()
|
||||||
.MinimumLevel.Information()
|
.MinimumLevel.Information()
|
||||||
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning) // <--
|
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
|
||||||
.Enrich.FromLogContext()
|
.Enrich.FromLogContext()
|
||||||
.WriteTo.File(
|
.WriteTo.File(
|
||||||
"logs/watcher-.log",
|
"logs/watcher-.log",
|
||||||
@@ -33,6 +34,9 @@ builder.Services.AddControllersWithViews();
|
|||||||
// HttpContentAccessor
|
// HttpContentAccessor
|
||||||
builder.Services.AddHttpContextAccessor();
|
builder.Services.AddHttpContextAccessor();
|
||||||
|
|
||||||
|
// Background Services
|
||||||
|
builder.Services.AddHostedService<NetworkCheck>();
|
||||||
|
|
||||||
|
|
||||||
// ---------- Konfiguration ----------
|
// ---------- Konfiguration ----------
|
||||||
DotNetEnv.Env.Load();
|
DotNetEnv.Env.Load();
|
||||||
|
59
Watcher/Services/NetworkCheck.cs
Normal file
59
Watcher/Services/NetworkCheck.cs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
using System.Composition;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
|
||||||
|
namespace Watcher.Services;
|
||||||
|
public class NetworkCheck : BackgroundService
|
||||||
|
{
|
||||||
|
private readonly ILogger<NetworkCheck> _logger;
|
||||||
|
|
||||||
|
public static String networkCheckResult
|
||||||
|
{
|
||||||
|
get { return networkCheckResult; }
|
||||||
|
set { networkCheckResult = value; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public NetworkCheck(ILogger<NetworkCheck> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Ping successfull. Watcher is online.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
_logger.LogError("Ping failed. Watcher appears to have no network connection.");
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Networkcheck finished.");
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
@@ -59,7 +59,7 @@
|
|||||||
|
|
||||||
<div class="d-flex justify-content-between align-items-center mb-2">
|
<div class="d-flex justify-content-between align-items-center mb-2">
|
||||||
<span>Netzwerk</span>
|
<span>Netzwerk</span>
|
||||||
<span class="badge bg-success">OK</span>
|
<span class="badge bg-success">NetworkConnection</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="progress mb-3" style="height: 6px;">
|
<div class="progress mb-3" style="height: 6px;">
|
||||||
<div class="progress-bar bg-success w-100"></div>
|
<div class="progress-bar bg-success w-100"></div>
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user