Update snmp_status.php

snmp Api geupdatet auf snmpwalk sollte hoffentlich jetzt auch auf dem server vernünftig laufen
This commit is contained in:
GraegelTh 2025-12-10 11:10:44 +01:00
parent b5146cde65
commit 781206daa9

View File

@ -6,16 +6,12 @@ declare(strict_types=1);
* *
* Nur authentifizierte Admins dürfen auf diesen Endpunkt zugreifen. * Nur authentifizierte Admins dürfen auf diesen Endpunkt zugreifen.
* Wird alle 5s vom JavaScript im Dashboard aufgerufen. * Wird alle 5s vom JavaScript im Dashboard aufgerufen.
*
* Sicherheit: Session-Validierung + Admin-Rollenprüfung.
* Performance: Datei-basiertes Caching (10 Sekunden) um SNMP-Last zu reduzieren.
* Logging: Alle Fehler und wichtigen Daten werden in `public/logs/snmp_api.log` geschrieben.
*/ */
session_start(); session_start();
// === Authentifizierung + Autorisierung === // === Authentifizierung + Autorisierung ===
$sessionKeyUser = 'admin_user'; // aus config $sessionKeyUser = 'admin_user';
if (!isset($_SESSION[$sessionKeyUser])) { if (!isset($_SESSION[$sessionKeyUser])) {
http_response_code(401); http_response_code(401);
header('Content-Type: application/json; charset=utf-8'); header('Content-Type: application/json; charset=utf-8');
@ -40,19 +36,15 @@ function log_msg(string $msg): void {
function rotate_log_if_needed(): void { function rotate_log_if_needed(): void {
global $logFile; global $logFile;
$maxSize = 500 * 1024; // 500 KB (anpassbar) $maxSize = 500 * 1024; // 500 KB
if (file_exists($logFile) && filesize($logFile) > $maxSize) { if (file_exists($logFile) && filesize($logFile) > $maxSize) {
$backupFile = $logFile . '.old'; @rename($logFile, $logFile . '.old');
@rename($logFile, $backupFile); log_msg('=== Log rotiert ===');
log_msg('=== Log rotiert (alte Datei: .old) ===');
} }
} }
$configPath = __DIR__ . '/../../config/config.php'; $configPath = __DIR__ . '/../../config/config.php';
if (!is_readable($configPath)) { if (!is_readable($configPath)) {
log_msg('ERROR: config.php nicht lesbar');
echo json_encode(['error' => 'config_not_found']); echo json_encode(['error' => 'config_not_found']);
exit; exit;
} }
@ -62,10 +54,7 @@ $snmp = $config['snmp'] ?? [];
rotate_log_if_needed(); rotate_log_if_needed();
log_msg('--- SNMP-Abfrage gestartet ---');
// === Cache-Logik (Datei) === // === Cache-Logik (Datei) ===
// Cache-Datei wird nach 10 Sekunden als ungültig betrachtet
$cacheDir = sys_get_temp_dir(); $cacheDir = sys_get_temp_dir();
$cacheFile = $cacheDir . DIRECTORY_SEPARATOR . 'snmp_status_cache.json'; $cacheFile = $cacheDir . DIRECTORY_SEPARATOR . 'snmp_status_cache.json';
$cacheTTL = 10; // Sekunden $cacheTTL = 10; // Sekunden
@ -73,53 +62,54 @@ $cacheTTL = 10; // Sekunden
if (file_exists($cacheFile)) { if (file_exists($cacheFile)) {
$cacheAge = time() - filemtime($cacheFile); $cacheAge = time() - filemtime($cacheFile);
if ($cacheAge < $cacheTTL) { if ($cacheAge < $cacheTTL) {
// Cache ist noch gültig
$cached = file_get_contents($cacheFile); $cached = file_get_contents($cacheFile);
if ($cached !== false) { if ($cached !== false) {
log_msg("Cache HIT (Alter: {$cacheAge}s)");
echo $cached; echo $cached;
exit; exit;
} }
} else {
log_msg("Cache abgelaufen (Alter: {$cacheAge}s), neue Abfrage");
} }
} }
// === SNMP Setup ===
$host = $snmp['host'] ?? '127.0.0.1'; $host = $snmp['host'] ?? '127.0.0.1';
$community = $snmp['community'] ?? 'public'; $community = $snmp['community'] ?? 'public';
$timeout = (int)($snmp['timeout'] ?? 2);
// Timeout von Sekunden in Mikrosekunden umrechnen (wichtig für PHP snmp Funktionen)
$timeoutSec = (int)($snmp['timeout'] ?? 2);
$timeoutMicro = $timeoutSec * 1_000_000;
$retries = (int)($snmp['retries'] ?? 1); $retries = (int)($snmp['retries'] ?? 1);
log_msg("SNMP-Host: $host, Community: $community, Timeout: {$timeout}s");
if (!function_exists('snmpget')) { if (!function_exists('snmpget')) {
log_msg('ERROR: SNMP-Erweiterung nicht installiert');
echo json_encode(['error' => 'snmp_extension_missing']); echo json_encode(['error' => 'snmp_extension_missing']);
exit; exit;
} }
snmp_set_quick_print(true); // Grundeinstellungen
snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC); snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
// Hilfsfunktion: sichere snmpget-Rückgabe (numeric oder null) // Hilfsfunktion: sichere snmpget-Rückgabe
function sget(string $host, string $community, string $oid, int $timeout, int $retries) function sget(string $host, string $community, string $oid, int $timeout, int $retries) {
{
$v = @snmpget($host, $community, $oid, $timeout, $retries); $v = @snmpget($host, $community, $oid, $timeout, $retries);
if ($v === false || $v === null) return null; if ($v === false || $v === null) return null;
return is_string($v) ? trim($v) : $v; return is_string($v) ? trim($v) : $v;
} }
// System-Uptime (TimeTicks) // --- 1. Uptime ---
// Wir deaktivieren quick_print, damit wir das Format "Timeticks: (12345) 1 day..." erhalten.
// Nur so können wir die echten Ticks in der Klammer zuverlässig parsen.
snmp_set_quick_print(false);
$uptimeOid = $snmp['oids']['uptime'] ?? '1.3.6.1.2.1.1.3.0'; $uptimeOid = $snmp['oids']['uptime'] ?? '1.3.6.1.2.1.1.3.0';
$upticksRaw = @sget($host, $community, $uptimeOid, $timeout, $retries); $uptimeRaw = @sget($host, $community, $uptimeOid, $timeoutMicro, $retries);
$upticks = $upticksRaw !== null ? (int)preg_replace('/[^0-9]/', '', (string)$upticksRaw) : null; $upticks = null;
log_msg("Uptime OID: $uptimeOid, Raw: " . ($upticksRaw ?? 'null')); // Regex sucht nach Zahl in Klammern: (12345678)
if ($uptimeRaw && preg_match('/\((.*?)\)/', (string)$uptimeRaw, $matches)) {
$upticks = (int)$matches[1];
}
function format_uptime(?int $ticks): ?string function format_uptime(?int $ticks): ?string {
{
if ($ticks === null) return null; if ($ticks === null) return null;
// ticks sind Hundertstel-Sekunden
$seconds = (int)floor($ticks / 100); $seconds = (int)floor($ticks / 100);
$days = intdiv($seconds, 86400); $days = intdiv($seconds, 86400);
$seconds -= $days * 86400; $seconds -= $days * 86400;
@ -127,166 +117,131 @@ function format_uptime(?int $ticks): ?string
$seconds -= $hours * 3600; $seconds -= $hours * 3600;
$minutes = intdiv($seconds, 60); $minutes = intdiv($seconds, 60);
$seconds -= $minutes * 60; $seconds -= $minutes * 60;
$parts = []; $parts = [];
if ($days) $parts[] = $days . ' Tage'; if ($days) $parts[] = $days . ' Tage';
if ($hours) $parts[] = $hours . ' Std'; $parts[] = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
if ($minutes) $parts[] = $minutes . ' Min'; return implode(', ', $parts);
$parts[] = $seconds . ' Sek';
return implode(' ', $parts);
} }
$uptimeStr = format_uptime($upticks); $uptimeStr = format_uptime($upticks);
// CPU: ersten Eintrag aus der CPU-Tabelle lesen // Für den Rest aktivieren wir quick_print wieder, um "saubere" Werte zu bekommen
snmp_set_quick_print(true);
// --- 2. CPU (Walk über alle Kerne) ---
$cpuTable = $snmp['oids']['cpu_table'] ?? '1.3.6.1.2.1.25.3.3.1.2'; $cpuTable = $snmp['oids']['cpu_table'] ?? '1.3.6.1.2.1.25.3.3.1.2';
$cpuOid = $cpuTable . '.1'; $cpuValues = @snmpwalk($host, $community, $cpuTable, $timeoutMicro, $retries);
$cpuRaw = @sget($host, $community, $cpuOid, $timeout, $retries);
$cpu = $cpuRaw !== null ? (float)preg_replace('/[^0-9.]/', '', (string)$cpuRaw) : null;
log_msg("CPU OID: $cpuOid, Raw: " . ($cpuRaw ?? 'null') . ", Parsed: " . ($cpu ?? 'null')); $cpuUsage = 0;
if (is_array($cpuValues) && count($cpuValues) > 0) {
$totalLoad = 0;
$coreCount = 0;
foreach ($cpuValues as $val) {
$v = (int)filter_var($val, FILTER_SANITIZE_NUMBER_INT);
$totalLoad += $v;
$coreCount++;
}
if ($coreCount > 0) {
$cpuUsage = round($totalLoad / $coreCount, 2);
}
}
// Storage-Tabellen: Versuche, C: (Windows) oder Root ("/") zu finden
// --- 3. Storage (Disk & RAM) ---
$descrOid = $snmp['oids']['storage_descr'] ?? '1.3.6.1.2.1.25.2.3.1.3'; $descrOid = $snmp['oids']['storage_descr'] ?? '1.3.6.1.2.1.25.2.3.1.3';
$unitsOid = $snmp['oids']['storage_units'] ?? '1.3.6.1.2.1.25.2.3.1.4'; $unitsOid = $snmp['oids']['storage_units'] ?? '1.3.6.1.2.1.25.2.3.1.4';
$sizeOid = $snmp['oids']['storage_size'] ?? '1.3.6.1.2.1.25.2.3.1.5'; $sizeOid = $snmp['oids']['storage_size'] ?? '1.3.6.1.2.1.25.2.3.1.5';
$usedOid = $snmp['oids']['storage_used'] ?? '1.3.6.1.2.1.25.2.3.1.6'; $usedOid = $snmp['oids']['storage_used'] ?? '1.3.6.1.2.1.25.2.3.1.6';
log_msg("Starte Storage-Walk - Descr: $descrOid, Units: $unitsOid, Size: $sizeOid, Used: $usedOid"); $descrWalk = @snmprealwalk($host, $community, $descrOid, $timeoutMicro, $retries);
$unitsWalk = @snmprealwalk($host, $community, $unitsOid, $timeoutMicro, $retries);
$descrWalk = @snmprealwalk($host, $community, $descrOid); $sizeWalk = @snmprealwalk($host, $community, $sizeOid, $timeoutMicro, $retries);
$unitsWalk = @snmprealwalk($host, $community, $unitsOid); $usedWalk = @snmprealwalk($host, $community, $usedOid, $timeoutMicro, $retries);
$sizeWalk = @snmprealwalk($host, $community, $sizeOid);
$usedWalk = @snmprealwalk($host, $community, $usedOid);
if (!is_array($descrWalk)) {
log_msg('WARNING: snmprealwalk für Beschreibungen fehlgeschlagen');
$descrWalk = [];
}
if (!is_array($unitsWalk)) {
log_msg('WARNING: snmprealwalk für Units fehlgeschlagen');
$unitsWalk = [];
}
if (!is_array($sizeWalk)) {
log_msg('WARNING: snmprealwalk für Größe fehlgeschlagen');
$sizeWalk = [];
}
if (!is_array($usedWalk)) {
log_msg('WARNING: snmprealwalk für Used fehlgeschlagen');
$usedWalk = [];
}
log_msg('Storage-Einträge gefunden: ' . count($descrWalk));
$diskPercent = null; $diskPercent = null;
$memPercent = null; $memPercent = null;
$storageEntries = []; $storageEntries = []; // Fallback-Liste
// Sammeln aller Storage-Einträge für Debug-Logging if (is_array($descrWalk)) {
if (is_array($descrWalk) && count($descrWalk) > 0) {
foreach ($descrWalk as $descrOidFull => $descrRaw) { foreach ($descrWalk as $descrOidFull => $descrRaw) {
// Index extrahieren
if (!preg_match('/\.(\d+)$/', $descrOidFull, $m)) continue; if (!preg_match('/\.(\d+)$/', $descrOidFull, $m)) continue;
$idx = $m[1]; $idx = $m[1];
$descr = trim((string)$descrRaw, ' "');
// Suche nach passenden Einträgen in anderen Walks mit gleichem Index // Bereinigen
$units = null; $descr = trim(str_ireplace('STRING:', '', (string)$descrRaw), ' "');
$size = null;
$used = null;
foreach ($unitsWalk as $oid => $val) { // Helper zum Finden der Werte
$findVal = function($walkArr, $idx) {
if(!is_array($walkArr)) return null;
foreach ($walkArr as $oid => $val) {
if (preg_match('/\.(\d+)$/', $oid, $m2) && $m2[1] === $idx) { if (preg_match('/\.(\d+)$/', $oid, $m2) && $m2[1] === $idx) {
$units = (int)preg_replace('/[^0-9]/', '', (string)$val); return (int)filter_var($val, FILTER_SANITIZE_NUMBER_INT);
break;
} }
} }
return null;
};
foreach ($sizeWalk as $oid => $val) { $units = $findVal($unitsWalk, $idx);
if (preg_match('/\.(\d+)$/', $oid, $m2) && $m2[1] === $idx) { $size = $findVal($sizeWalk, $idx);
$size = (int)preg_replace('/[^0-9]/', '', (string)$val); $used = $findVal($usedWalk, $idx);
break;
}
}
foreach ($usedWalk as $oid => $val) { if ($size === null || $units === null || $used === null || $size === 0) continue;
if (preg_match('/\.(\d+)$/', $oid, $m2) && $m2[1] === $idx) {
$used = (int)preg_replace('/[^0-9]/', '', (string)$val);
break;
}
}
log_msg("Storage[$idx]: Desc='$descr', Size=$size, Used=$used, Units=$units");
if ($size === null || $units === null || $used === null || $size === 0 || $units === 0) {
log_msg("Storage[$idx]: SKIP (fehlende oder ungültige Daten)");
continue;
}
$totalBytes = $size * $units; $totalBytes = $size * $units;
$usedBytes = $used * $units; $usedBytes = $used * $units;
$percent = $totalBytes > 0 ? ($usedBytes / $totalBytes) * 100 : 0; $percent = ($totalBytes > 0) ? ($usedBytes / $totalBytes) * 100 : 0;
$storageEntries[] = [ $storageEntries[] = ['idx' => $idx, 'descr' => $descr, 'percent' => $percent, 'totalGB' => $totalBytes / (1024**3)];
'idx' => $idx,
'descr' => $descr,
'percent' => $percent,
'totalGB' => $totalBytes / (1024 ** 3),
'usedGB' => $usedBytes / (1024 ** 3),
];
// Heuristik 1: Suche nach C: oder Root
$lower = strtolower($descr); $lower = strtolower($descr);
if ($diskPercent === null && (strpos($lower, 'c:') !== false || strpos($lower, 'c:\\') !== false || strpos($lower, 'c:/') !== false || $lower === '/' || strpos($lower, 'root') !== false)) {
// DISK C: oder Root
if ($diskPercent === null) {
if (str_starts_with($lower, 'c:') || str_starts_with($lower, 'c:\\') || $lower === '/' || str_contains($lower, 'root')) {
$diskPercent = $percent; $diskPercent = $percent;
log_msg("Datenträger erkannt (Index $idx): $descr$percent%"); }
} }
// Heuristik 2: Suche nach Physical Memory // RAM
if ($memPercent === null && ((strpos($lower, 'physical') !== false && strpos($lower, 'memory') !== false) || strpos($lower, 'ram') !== false || strpos($lower, 'physical memory') !== false)) { if ($memPercent === null) {
if (str_contains($lower, 'physical memory') || str_contains($lower, 'ram')) {
$memPercent = $percent; $memPercent = $percent;
log_msg("Speicher erkannt (Index $idx): $descr$percent%"); }
} }
} }
} }
// Fallback 1: Wenn keine Disk gefunden, nimm den größten Storage-Eintrag > 1GB // Fallback Disk: Größter Speicher > 5GB
if ($diskPercent === null && count($storageEntries) > 0) { if ($diskPercent === null && count($storageEntries) > 0) {
log_msg('Fallback: Suche Disk (größter Eintrag > 1GB)');
usort($storageEntries, fn($a, $b) => $b['totalGB'] <=> $a['totalGB']); usort($storageEntries, fn($a, $b) => $b['totalGB'] <=> $a['totalGB']);
foreach ($storageEntries as $entry) { foreach($storageEntries as $entry) {
if ($entry['totalGB'] > 1) { if ($entry['totalGB'] > 5) {
$diskPercent = $entry['percent']; $diskPercent = $entry['percent'];
log_msg('Fallback Disk gefunden (Index ' . $entry['idx'] . '): ' . $entry['descr'] . ' → ' . $diskPercent . '%');
break; break;
} }
} }
} }
// Fallback 2: Wenn kein Speicher gefunden, nimm den kleinsten Eintrag (meist Physical Memory) // --- 4. Hostname ---
if ($memPercent === null && count($storageEntries) > 0) { $hostnameOid = '1.3.6.1.2.1.1.5.0';
log_msg('Fallback: Suche Memory (kleinster Eintrag)'); $hostname = @sget($host, $community, $hostnameOid, $timeoutMicro, $retries);
usort($storageEntries, fn($a, $b) => $a['totalGB'] <=> $b['totalGB']); if($hostname) $hostname = trim(str_ireplace('STRING:', '', $hostname), ' "');
$memPercent = $storageEntries[0]['percent'];
log_msg('Fallback Memory gefunden (Index ' . $storageEntries[0]['idx'] . '): ' . $storageEntries[0]['descr'] . ' → ' . $memPercent . '%');
}
// --- Ergebnis ---
$result = [ $result = [
'hostname' => @sget($host, $community, '1.3.6.1.2.1.1.5.0', $timeout, $retries) ?? null, 'hostname' => $hostname ?? 'n/a',
'uptime' => $uptimeStr, 'uptime' => $uptimeStr,
'upticks' => $upticks, 'upticks' => $upticks,
'cpu_usage' => $cpu, 'cpu_usage' => $cpuUsage,
'memory_usage' => $memPercent !== null ? round($memPercent, 2) : null, 'memory_usage' => $memPercent !== null ? round($memPercent, 2) : 0,
'disk_usage_c' => $diskPercent !== null ? round($diskPercent, 2) : null, 'disk_usage_c' => $diskPercent !== null ? round($diskPercent, 2) : 0,
'last_update' => time(), 'last_update' => time(),
]; ];
log_msg('RESULT: CPU=' . $result['cpu_usage'] . ', Mem=' . $result['memory_usage'] . ', Disk=' . $result['disk_usage_c']); log_msg('RESULT: UptimeRaw='.($uptimeRaw??'null').' CPU=' . $result['cpu_usage'] . ' Mem=' . $result['memory_usage']);
$resultJson = json_encode($result); $resultJson = json_encode($result);
// === Cache schreiben ===
@file_put_contents($cacheFile, $resultJson); @file_put_contents($cacheFile, $resultJson);
log_msg('Cache geschrieben, TTL: ' . $cacheTTL . 's');
echo $resultJson; echo $resultJson;
exit;