SNMP Teil hinzugefügt
This commit is contained in:
parent
e4b51997ee
commit
e30660a446
120
app/Controllers/DashboardController.php
Normal file
120
app/Controllers/DashboardController.php
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// Strenge Typprüfung für Parameter- und Rückgabetypen aktivieren.
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controllers;
|
||||||
|
|
||||||
|
use App\Services\Snmp\SnmpServerStatusService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller für das Dashboard.
|
||||||
|
* Zuständig für:
|
||||||
|
* - Abrufen des Serverstatus (über SnmpServerStatusService)
|
||||||
|
* - Auswählen und Rendern der Dashboard-View
|
||||||
|
*/
|
||||||
|
class DashboardController
|
||||||
|
{
|
||||||
|
/** @var array<string, mixed> Vollständige Anwendungskonfiguration (aus config.php) */
|
||||||
|
private array $config;
|
||||||
|
|
||||||
|
/** @var SnmpServerStatusService Service, der den Serverstatus (später per SNMP) liefert */
|
||||||
|
private SnmpServerStatusService $snmpService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Übergibt die Konfiguration an den Controller und initialisiert den SNMP-Statusservice.
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $config Vollständige Konfiguration aus config.php
|
||||||
|
*/
|
||||||
|
public function __construct(array $config)
|
||||||
|
{
|
||||||
|
// Komplette Config lokal speichern (falls später weitere Werte benötigt werden).
|
||||||
|
$this->config = $config;
|
||||||
|
|
||||||
|
// Teilbereich "snmp" aus der Konfiguration ziehen.
|
||||||
|
// Wenn nicht vorhanden, wird ein leeres Array übergeben (der Service prüft das selbst).
|
||||||
|
$snmpConfig = $config['snmp'] ?? [];
|
||||||
|
|
||||||
|
// SNMP-Service initialisieren, der den Serverstatus liefert.
|
||||||
|
$this->snmpService = new SnmpServerStatusService($snmpConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zeigt das Dashboard an.
|
||||||
|
* Holt die Serverstatus-Daten aus dem SnmpServerStatusService und übergibt sie an die View.
|
||||||
|
*/
|
||||||
|
public function show(): void
|
||||||
|
{
|
||||||
|
// Serverstatus über den SNMP-Service ermitteln.
|
||||||
|
// In der aktuellen Version liefert der Service noch Demo-Daten.
|
||||||
|
$serverStatus = $this->snmpService->getServerStatus();
|
||||||
|
|
||||||
|
// Pfad zur Dashboard-View (Template-Datei) ermitteln.
|
||||||
|
$viewPath = __DIR__ . '/../../public/views/dashboard.php';
|
||||||
|
|
||||||
|
// Falls die View-Datei (noch) nicht existiert, Fallback-HTML direkt aus dem Controller ausgeben.
|
||||||
|
if (file_exists($viewPath) === false) {
|
||||||
|
$this->renderInlineDashboard($serverStatus);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// View-Datei einbinden. Die Variable $serverStatus steht in der View zur Verfügung.
|
||||||
|
require $viewPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fallback-Dashboard-Ausgabe direkt aus dem Controller (ohne separate View-Datei).
|
||||||
|
* Nur als Notlösung gedacht, falls die eigentliche View noch nicht vorhanden ist.
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $serverStatus Serverstatus-Daten (Hostname, CPU%, RAM%, etc.)
|
||||||
|
*/
|
||||||
|
private function renderInlineDashboard(array $serverStatus): void
|
||||||
|
{
|
||||||
|
?>
|
||||||
|
<!doctype html>
|
||||||
|
<html lang="de">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>AD Admin Tool – Dashboard</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Dashboard</h1>
|
||||||
|
|
||||||
|
<h2>Serverstatus</h2>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
Hostname:
|
||||||
|
<?php echo htmlspecialchars((string)$serverStatus['hostname'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Betriebssystem:
|
||||||
|
<?php echo htmlspecialchars((string)$serverStatus['os'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Uptime:
|
||||||
|
<?php echo htmlspecialchars((string)$serverStatus['uptime'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
CPU-Auslastung:
|
||||||
|
<?php echo (int)$serverStatus['cpu_usage']; ?>%
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
RAM-Auslastung:
|
||||||
|
<?php echo (int)$serverStatus['memory_usage']; ?>%
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Datenträger C:
|
||||||
|
<?php echo (int)$serverStatus['disk_usage_c']; ?>%
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Letzte Aktualisierung:
|
||||||
|
<?php echo htmlspecialchars((string)$serverStatus['last_update'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p><a href="index.php?route=logout">Logout</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
}
|
||||||
68
app/Services/Snmp/SnmpServerStatusService.php
Normal file
68
app/Services/Snmp/SnmpServerStatusService.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// Strenge Typprüfung für Parameter- und Rückgabetypen aktivieren.
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services\Snmp;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service zur Ermittlung des Serverstatus.
|
||||||
|
*
|
||||||
|
* In dieser ersten Version werden noch statische Demo-Daten zurückgegeben.
|
||||||
|
* Später können hier echte SNMP-Abfragen eingebaut werden, ohne dass sich
|
||||||
|
* der DashboardController oder die Views ändern müssen.
|
||||||
|
*/
|
||||||
|
class SnmpServerStatusService
|
||||||
|
{
|
||||||
|
/** @var array<string, mixed> SNMP-spezifische Konfiguration (Host, Community, Timeout, OIDs, etc.) */
|
||||||
|
private array $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erwartet den Teilbereich "snmp" aus der allgemeinen Konfiguration (config.php).
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $snmpConfig Konfiguration für die SNMP-Abfragen
|
||||||
|
*/
|
||||||
|
public function __construct(array $snmpConfig)
|
||||||
|
{
|
||||||
|
// SNMP-Konfiguration in der Instanz speichern.
|
||||||
|
$this->config = $snmpConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Liefert den aktuellen Serverstatus zurück.
|
||||||
|
*
|
||||||
|
* In dieser Version werden Demo-Daten auf Basis der Konfiguration erzeugt.
|
||||||
|
* Später können hier echte SNMP-Queries (z. B. über die PHP-SNMP-Extension) verwendet werden.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed> Assoziatives Array mit Statuswerten (Hostname, CPU%, RAM%, etc.)
|
||||||
|
*
|
||||||
|
* @throws RuntimeException wenn die SNMP-Konfiguration unvollständig ist (z. B. host fehlt)
|
||||||
|
*/
|
||||||
|
public function getServerStatus(): array
|
||||||
|
{
|
||||||
|
// Hostnamen aus der SNMP-Konfiguration lesen.
|
||||||
|
$host = (string)($this->config['host'] ?? '');
|
||||||
|
|
||||||
|
// Ohne Host ist keine sinnvolle Abfrage möglich -> Konfigurationsfehler.
|
||||||
|
if ($host === '') {
|
||||||
|
throw new RuntimeException('SNMP-Konfiguration ist unvollständig (host fehlt).');
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Später hier echte SNMP-Abfragen einbauen (z. B. snmp2_get/snmp2_walk).
|
||||||
|
// Die OIDs können aus $this->config['oids'] gelesen werden.
|
||||||
|
// Für den Anfang verwenden wir dieselben Demo-Werte wie vorher im DashboardController.
|
||||||
|
$status = [
|
||||||
|
'hostname' => $host,
|
||||||
|
'os' => 'Windows Server 2025 Datacenter',
|
||||||
|
'uptime' => '3 Tage 12 Stunden',
|
||||||
|
'cpu_usage' => 23, // CPU-Auslastung in Prozent (Demo)
|
||||||
|
'memory_usage' => 62, // RAM-Auslastung in Prozent (Demo)
|
||||||
|
'disk_usage_c' => 71, // Datenträger C in Prozent (Demo)
|
||||||
|
'last_update' => date('d.m.Y H:i:s'), // Zeitpunkt der letzten Aktualisierung
|
||||||
|
];
|
||||||
|
|
||||||
|
return $status;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user