philippurbschat.de/philcore/app/services/HealthService.php

80 lines
No EOL
2.8 KiB
PHP

<?php
class HealthService {
public static function getHealthReport() {
return [
'php_version' => PHP_VERSION,
'disk_free' => self::getDiskStatus(),
'memory' => self::formatBytes(self::getMemoryLimit()),
'env_status' => self::checkEnvFile(),
'db_status' => self::checkDatabase(),
'writable' => self::checkWritableFolders(['public', 'app'])
];
}
private static function getMemoryLimit() {
$limit = ini_get('memory_limit');
if (preg_match('/^(\d+)(.)$/', $limit, $matches)) {
$val = (int)$matches[1];
switch (strtoupper($matches[2])) {
case 'G': $val *= 1024 * 1024 * 1024; break;
case 'M': $val *= 1024 * 1024; break;
case 'K': $val *= 1024; break;
}
return $val;
}
return (int)$limit;
}
private static function formatBytes($bytes, $precision = 2) {
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
return round($bytes / pow(1024, $pow), $precision) . ' ' . $units[$pow];
}
private static function getDiskStatus() {
if (function_exists('disk_free_space') && function_exists('disk_total_space')) {
try {
$free = @disk_free_space("/");
$total = @disk_total_space("/");
if ($free !== false && $total > 0) {
return round(($free / $total) * 100) . "% frei";
}
} catch (Exception $e) {}
}
return "Eingeschränkt (Hosting)";
}
private static function checkEnvFile() {
// Springt aus app/services/ zwei Ebenen hoch zum Hauptverzeichnis
return file_exists(__DIR__ . '/../../.env') ? 'Bereit' : 'Fehlt!';
}
private static function checkDatabase() {
try {
// Checkt, ob überhaupt ein Name gesetzt ist
if (empty(Config::get('DB_NAME'))) {
return 'Deaktiviert';
}
$db = new Database();
$db->query("SELECT VERSION() as v");
$res = $db->single();
return $res['v'] ?? 'Online';
} catch (Exception $e) {
return 'Offline / Fehler';
}
}
private static function checkWritableFolders($folders) {
foreach ($folders as $folder) {
// Geht vom aktuellen Verzeichnis (app/services/) zwei Ebenen hoch
$path = realpath(__DIR__ . '/../../' . $folder);
if (!$path || !is_dir($path)) return "Fehlt: $folder";
if (!is_writable($path)) return "Rechte: $folder";
}
return "OK";
}
}