[增添]添加了Prometheus服务、对应的widget展示(待美化)
This commit is contained in:
29
management-panel/app/Filament/Widgets/PrometheusMetrics.php
Normal file
29
management-panel/app/Filament/Widgets/PrometheusMetrics.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Services\PrometheusService;
|
||||
use Filament\Widgets\Widget;
|
||||
|
||||
class PrometheusMetrics extends Widget
|
||||
{
|
||||
protected static string $view = 'filament.widgets.prometheus-metrics';
|
||||
|
||||
protected function getViewData(): array
|
||||
{
|
||||
$prometheus = new PrometheusService();
|
||||
|
||||
$currentMemoryUsage = $prometheus->query('(1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100');
|
||||
$memoryUsageRange = $prometheus->queryRange(
|
||||
'(1 - (avg_over_time(node_memory_MemAvailable_bytes[1h]) / avg_over_time(node_memory_MemTotal_bytes[1h]))) * 100',
|
||||
now()->subHour()->timestamp,
|
||||
now()->timestamp,
|
||||
60
|
||||
);
|
||||
|
||||
return [
|
||||
'currentMemoryUsage' => $currentMemoryUsage,
|
||||
'memoryUsageRange' => $memoryUsageRange,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers\Filament;
|
||||
|
||||
use App\Filament\Widgets\PrometheusMetrics;
|
||||
use App\Filament\Widgets\UpdateConfigWidget;
|
||||
use Filament\Http\Middleware\Authenticate;
|
||||
use Filament\Http\Middleware\DisableBladeIconComponents;
|
||||
@@ -42,6 +43,7 @@ class AdminPanelProvider extends PanelProvider
|
||||
Widgets\AccountWidget::class,
|
||||
// Widgets\FilamentInfoWidget::class,
|
||||
UpdateConfigWidget::class,
|
||||
PrometheusMetrics::class,
|
||||
])
|
||||
->middleware([
|
||||
EncryptCookies::class,
|
||||
|
||||
62
management-panel/app/Services/PrometheusService.php
Normal file
62
management-panel/app/Services/PrometheusService.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace App\Services;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class PrometheusService
|
||||
{
|
||||
protected $client;
|
||||
protected $baseUri = 'http://localhost:9090/api/v1/'; // Prometheus 的默认地址
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client(['base_uri' => $this->baseUri]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行 Prometheus 实时查询
|
||||
*
|
||||
* @param string $query PromQL 查询语句
|
||||
* @return array 返回查询结果
|
||||
*/
|
||||
public function query($query)
|
||||
{
|
||||
$response = $this->client->get('query', [
|
||||
'query' => ['query' => $query],
|
||||
]);
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行 Prometheus 区间查询
|
||||
*
|
||||
* @param string $query PromQL 查询语句
|
||||
* @param int $start 开始时间(时间戳,单位秒)
|
||||
* @param int $end 结束时间(时间戳,单位秒)
|
||||
* @param int $step 时间间隔(单位秒)
|
||||
* @return array 返回查询结果
|
||||
*/
|
||||
public function queryRange($query, $start, $end, $step)
|
||||
{
|
||||
$response = $this->client->get('query_range', [
|
||||
'query' => [
|
||||
'query' => $query,
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
'step' => $step,
|
||||
],
|
||||
]);
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Prometheus 监控的所有目标
|
||||
*
|
||||
* @return array 返回目标信息
|
||||
*/
|
||||
public function getTargets()
|
||||
{
|
||||
$response = $this->client->get('targets');
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user