Align sysstat timezone with system

This commit is contained in:
root
2026-01-30 01:06:44 +02:00
parent 828aedcc2b
commit 230abb78b6
2 changed files with 44 additions and 4 deletions

View File

@@ -34,7 +34,8 @@ class ServerChartsWidget extends Widget
$this->refreshKey++;
$data = $this->getData();
$config = $this->rangeConfig($this->range);
$label = now()->format($config['label_format']);
$sysstat = new SysstatMetrics;
$label = now($sysstat->timezoneName())->format($config['label_format']);
$this->dispatch('server-charts-updated', [
'cpu' => $data['cpu']['usage'] ?? 0,

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Services;
use Carbon\CarbonImmutable;
use DateTimeZone;
use Symfony\Component\Process\Process;
class SysstatMetrics
@@ -18,7 +19,8 @@ class SysstatMetrics
return [];
}
$end = CarbonImmutable::now()->second(0);
$timezone = $this->systemTimezone();
$end = CarbonImmutable::now($timezone)->second(0);
if ($intervalSeconds >= 3600) {
$end = $end->minute(0);
}
@@ -40,7 +42,8 @@ class SysstatMetrics
*/
public function latest(): ?array
{
$end = CarbonImmutable::now();
$timezone = $this->systemTimezone();
$end = CarbonImmutable::now($timezone);
$start = $end->subMinutes(15);
$samples = $this->readSamples($start, $end);
@@ -63,6 +66,11 @@ class SysstatMetrics
];
}
public function timezoneName(): string
{
return $this->systemTimezone()->getName();
}
/**
* @return array<int, array{timestamp: int, load1: float, load5: float, load15: float, iowait: float, memory: float, swap: float}>
*/
@@ -227,7 +235,7 @@ class SysstatMetrics
return null;
}
$value = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $date.' '.$time, config('app.timezone'));
$value = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $date.' '.$time, $this->systemTimezone());
if ($value === false) {
return null;
}
@@ -235,6 +243,37 @@ class SysstatMetrics
return $value;
}
private function systemTimezone(): DateTimeZone
{
static $timezone = null;
if ($timezone instanceof DateTimeZone) {
return $timezone;
}
$name = getenv('TZ') ?: null;
if (! $name && is_file('/etc/timezone')) {
$name = trim((string) file_get_contents('/etc/timezone'));
}
if (! $name && is_link('/etc/localtime')) {
$target = readlink('/etc/localtime');
if (is_string($target) && str_contains($target, '/zoneinfo/')) {
$name = substr($target, strpos($target, '/zoneinfo/') + 10);
}
}
if (! $name) {
$name = config('app.timezone') ?: date_default_timezone_get();
}
try {
$timezone = new DateTimeZone($name);
} catch (\Exception $e) {
$timezone = new DateTimeZone('UTC');
}
return $timezone;
}
/**
* @param array<string, mixed> $source
*/