insight/src/Web/Insight.Web/Pages/Monitoring/Maintenance/Index.razor.cs

152 lines
No EOL
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Insight.Infrastructure.Entities;
using Insight.Web.Constants;
using Microsoft.AspNetCore.Components;
using MongoDB.Bson;
using MongoDB.Driver;
using MudBlazor;
namespace Insight.Web.Pages.Monitoring.Maintenance;
[Route(Navigation.Monitoring.Maintenance.Index)]
public partial class Index
{
[Inject] private IMongoDatabase Database { get; init; } = default!;
[Inject] private ISnackbar Snackbar { get; init; } = default!;
private string _title = Global.Name;
private ViewModel _model = new();
private readonly List<BreadcrumbItem> _breadcrumbs = new();
protected override void OnInitialized()
{
_title = $"Monitoring » MaintenanceInsight";
_breadcrumbs.Add(new BreadcrumbItem("Home", href: Navigation.Home));
_breadcrumbs.Add(new BreadcrumbItem("Monitoring", href: "#", true));
_breadcrumbs.Add(new BreadcrumbItem("Maintenance", href: "#", true));
}
private async Task LoadDataAsync()
{
try
{
// drives (errors)
var drives = await Database.HostDrive().Aggregate()
.Match(Builders<HostDriveEntity>.Filter.Ne(p => p.Status, "OK"))
.Lookup("host", "_host", "_id", "hosts")
.Match(new BsonDocument("hosts", new BsonDocument("$exists", new BsonDocument("$ne", BsonNull.Value))))
.AppendStage<BsonDocument>(new BsonDocument("$count", "size"))
.FirstOrDefaultAsync(cancellationToken: default);
if (drives is not null && drives.Any())
{
_model.Updates = drives[0].AsInt32;
}
// storage pools (errors)
var storagePools = await Database.HostStoragePool().Aggregate()
.Match(Builders<HostStoragePoolEntity>.Filter.Ne(p => p.Health, "Healthy"))
.Lookup("host", "_host", "_id", "hosts")
.Match(new BsonDocument("hosts", new BsonDocument("$exists", new BsonDocument("$ne", BsonNull.Value))))
.AppendStage<BsonDocument>(new BsonDocument("$count", "size"))
.FirstOrDefaultAsync(cancellationToken: default);
if (storagePools is not null && storagePools.Any())
{
_model.StoragePools = storagePools[0].AsInt32;
}
// volumes (freespace)
var freespace = await Database.HostVolume().Aggregate()
.Match(Builders<HostVolumeEntity>.Filter.Ne(p => p.FreeSpace, null))
.Lookup("host", "_host", "_id", "hosts")
.Match(new BsonDocument("hosts", new BsonDocument("$exists", new BsonDocument("$ne", BsonNull.Value))))
.Project<BsonDocument>(new BsonDocument
{
{ "percent_free", new BsonDocument("$multiply", new BsonArray
{
new BsonDocument("$divide", new BsonArray
{
"$freespace",
"$size"
}), 100
})
}
})
.Match(Builders<BsonDocument>.Filter.Lt("percent_free", 5))
.AppendStage<BsonDocument>(new BsonDocument("$count", "size"))
.FirstOrDefaultAsync(cancellationToken: default);
if (freespace is not null && freespace.Any())
{
_model.Volumes = freespace[0].AsInt32;
}
// virtual maschines
var virtualMaschines = await Database.HostHypervisorVirtualMaschine().Aggregate()
.Match(new BsonDocument("health", new BsonDocument("$ne", "OK")))
.Lookup("host", "_host", "_id", "hosts")
.Match(new BsonDocument("hosts", new BsonDocument("$exists", new BsonDocument("$ne", BsonNull.Value))))
.AppendStage<BsonDocument>(new BsonDocument("$count", "size"))
.FirstOrDefaultAsync(cancellationToken: default);
if (virtualMaschines is not null && virtualMaschines.Any())
{
_model.Guests = virtualMaschines[0].AsInt32;
}
// snapshots (old)
var snapshots = await Database.HostVirtualMaschineConfig().Aggregate()
.Lookup("host_hv_vm", "virtualmaschine", "_id", "virtualmaschines")
.Project(new BsonDocument
{
{ "_id", 1 },
{ "uniqueid", 1 },
{ "host", 1 },
{ "virtualmaschine", new BsonDocument("$first", "$virtualmaschines") }
})
.Match(new BsonDocument("$expr", new BsonDocument("$ne", new BsonArray
{
"$uniqueid",
"$virtualmaschine.uniqueid"
})))
.Lookup("host", "host", "_id", "hosts")
.Match(new BsonDocument("hosts", new BsonDocument("$exists", new BsonDocument("$ne", BsonNull.Value))))
.AppendStage<BsonDocument>(new BsonDocument("$count", "size"))
.FirstOrDefaultAsync(cancellationToken: default);
if (snapshots is not null && snapshots.Any())
{
_model.Snapshots = snapshots[0].AsInt32;
}
// updates (pending)
var updates = await Database.HostUpdate().Aggregate()
.Match(Builders<HostUpdateEntity>.Filter.Eq(p => p.Pending, true))
.Lookup("host", "_host", "_id", "hosts")
.Match(new BsonDocument("hosts", new BsonDocument("$exists", new BsonDocument("$ne", BsonNull.Value))))
.AppendStage<BsonDocument>(new BsonDocument("$count", "size"))
.FirstOrDefaultAsync(cancellationToken: default);
if (updates is not null && updates.Any())
{
_model.Updates = updates[0].AsInt32;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
Notification.Error(Snackbar);
}
}
public class ViewModel
{
public int Drives { get; set; }
public int StoragePools { get; set; }
public int Volumes { get; set; }
public int Guests { get; set; }
public int Snapshots { get; set; }
public int Updates { get; set; }
}
}