mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-21 17:07:47 +02:00
Compare commits
4 Commits
alerts-dns
...
997adc19bb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
997adc19bb | ||
|
|
08d813620c | ||
|
|
6cb302fcf6 | ||
|
|
59eed073c3 |
@@ -43,6 +43,11 @@ COPY --from=builder /app/agent/test-data/amdgpu.ids /usr/share/libdrm/amdgpu.ids
|
|||||||
# Copy smartmontools binaries and config files
|
# Copy smartmontools binaries and config files
|
||||||
COPY --from=smartmontools-builder /usr/sbin/smartctl /usr/sbin/smartctl
|
COPY --from=smartmontools-builder /usr/sbin/smartctl /usr/sbin/smartctl
|
||||||
|
|
||||||
|
# Install ZFS userspace utilities (zpool, zfs) for pool/dataset monitoring
|
||||||
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
zfsutils-linux \
|
||||||
|
&& apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Ensure data persistence across container recreations
|
# Ensure data persistence across container recreations
|
||||||
VOLUME ["/var/lib/beszel-agent"]
|
VOLUME ["/var/lib/beszel-agent"]
|
||||||
|
|
||||||
|
|||||||
@@ -65,6 +65,32 @@ RUN set -eux; \
|
|||||||
cp -v "$interp" "/out/rootfs$interp"; \
|
cp -v "$interp" "/out/rootfs$interp"; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# ZFS utilities builder stage
|
||||||
|
# --------------------------
|
||||||
|
FROM --platform=$TARGETPLATFORM debian:bookworm-slim AS zfsutils-builder
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
zfsutils-linux \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# Copy the zpool/zfs binaries and their required runtime libraries
|
||||||
|
RUN set -eux; \
|
||||||
|
mkdir -p /out/rootfs/lib /out/rootfs/lib64 /out/rootfs/usr/lib; \
|
||||||
|
for bin in /usr/sbin/zpool /usr/sbin/zfs; do \
|
||||||
|
mkdir -p "/out/rootfs$(dirname "$bin")"; \
|
||||||
|
cp -v "$bin" "/out/rootfs$bin"; \
|
||||||
|
ldd "$bin" \
|
||||||
|
| awk '{print $3}' \
|
||||||
|
| grep '^/' \
|
||||||
|
| xargs -r -I '{}' sh -c 'mkdir -p "/out/rootfs$(dirname "{}")"; cp -v "{}" "/out/rootfs{}"'; \
|
||||||
|
interp="$(ldd "$bin" | awk "/ld-linux/ {print \$1}")"; \
|
||||||
|
if [ -n "$interp" ] && [ -e "$interp" ]; then \
|
||||||
|
mkdir -p "/out/rootfs$(dirname "$interp")"; \
|
||||||
|
cp -v "$interp" "/out/rootfs$interp"; \
|
||||||
|
fi; \
|
||||||
|
done
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Final image: lightweight multi-arch NVIDIA agent (slim)
|
# Final image: lightweight multi-arch NVIDIA agent (slim)
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@@ -78,6 +104,9 @@ COPY --from=builder /app/agent/test-data/amdgpu.ids /usr/share/libdrm/amdgpu.ids
|
|||||||
COPY --from=smartmontools-builder /usr/sbin/smartctl /usr/sbin/smartctl
|
COPY --from=smartmontools-builder /usr/sbin/smartctl /usr/sbin/smartctl
|
||||||
COPY --from=smartmontools-builder /out/rootfs/ /
|
COPY --from=smartmontools-builder /out/rootfs/ /
|
||||||
|
|
||||||
|
# Copy ZFS utilities (zpool, zfs) binaries and required runtime libraries
|
||||||
|
COPY --from=zfsutils-builder /out/rootfs/ /
|
||||||
|
|
||||||
# nvidia-smi is intentionally not bundled.
|
# nvidia-smi is intentionally not bundled.
|
||||||
# Mount the host binary instead, for example:
|
# Mount the host binary instead, for example:
|
||||||
# - /usr/bin/nvidia-smi:/usr/bin/nvidia-smi:ro
|
# - /usr/bin/nvidia-smi:/usr/bin/nvidia-smi:ro
|
||||||
|
|||||||
@@ -272,7 +272,15 @@ func (sys *System) createRecords(data *system.CombinedData) (*core.Record, error
|
|||||||
|
|
||||||
// update system record (do this last because it triggers alerts and we need above records to be inserted first)
|
// update system record (do this last because it triggers alerts and we need above records to be inserted first)
|
||||||
systemRecord.Set("status", up)
|
systemRecord.Set("status", up)
|
||||||
systemRecord.Set("info", data.Info)
|
// Distinguish an idle GPU from a system without GPU data (#2312)
|
||||||
|
info := struct {
|
||||||
|
system.Info
|
||||||
|
GpuPct *float64 `json:"g,omitempty"`
|
||||||
|
}{Info: data.Info}
|
||||||
|
if len(data.Stats.GPUData) > 0 {
|
||||||
|
info.GpuPct = &data.Info.GpuPct
|
||||||
|
}
|
||||||
|
systemRecord.Set("info", info)
|
||||||
if err := txApp.SaveNoValidate(systemRecord); err != nil {
|
if err := txApp.SaveNoValidate(systemRecord); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
44
internal/hub/systems/system_gpu_test.go
Normal file
44
internal/hub/systems/system_gpu_test.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
//go:build testing
|
||||||
|
|
||||||
|
package systems
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/henrygd/beszel/internal/entities/system"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateRecordsGPUUtilization(t *testing.T) {
|
||||||
|
sys, app := newTestSystemWithHub(t)
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
gpu bool
|
||||||
|
usage float64
|
||||||
|
}{
|
||||||
|
{"no GPU", false, 0},
|
||||||
|
{"active GPU", true, 42.5},
|
||||||
|
{"idle GPU", true, 0},
|
||||||
|
{"GPU removed", false, 0},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
data := &system.CombinedData{Info: system.Info{GpuPct: tc.usage, Cpu: 12.5}}
|
||||||
|
if tc.gpu {
|
||||||
|
data.Stats.GPUData = map[string]system.GPUData{"0": {Name: "GPU", Usage: tc.usage}}
|
||||||
|
}
|
||||||
|
_, err := sys.createRecords(data)
|
||||||
|
require.NoError(t, err)
|
||||||
|
record, err := app.FindRecordById("systems", sys.Id)
|
||||||
|
require.NoError(t, err)
|
||||||
|
var info map[string]any
|
||||||
|
require.NoError(t, record.UnmarshalJSONField("info", &info))
|
||||||
|
assert.Equal(t, 12.5, info["cpu"])
|
||||||
|
if tc.gpu {
|
||||||
|
assert.Equal(t, tc.usage, info["g"])
|
||||||
|
} else {
|
||||||
|
assert.NotContains(t, info, "g")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/henrygd/beszel/internal/hub/ws"
|
"github.com/henrygd/beszel/internal/hub/ws"
|
||||||
@@ -47,6 +48,10 @@ type SystemManager struct {
|
|||||||
sshConfig *ssh.ClientConfig // SSH client configuration for system connections
|
sshConfig *ssh.ClientConfig // SSH client configuration for system connections
|
||||||
smartFetchMap *expirymap.ExpiryMap[smartFetchState] // Stores last SMART fetch time/result; TTL is only for cleanup
|
smartFetchMap *expirymap.ExpiryMap[smartFetchState] // Stores last SMART fetch time/result; TTL is only for cleanup
|
||||||
zfsFetchMap *expirymap.ExpiryMap[zfsFetchState] // Stores last ZFS fetch time/result; TTL is only for cleanup
|
zfsFetchMap *expirymap.ExpiryMap[zfsFetchState] // Stores last ZFS fetch time/result; TTL is only for cleanup
|
||||||
|
realtimeMutex sync.Mutex // Protects all realtime worker and subscription state
|
||||||
|
activeSubscriptions map[string]*subscriptionInfo // Realtime subscriptions keyed by system ID
|
||||||
|
realtimeWorkerStop chan struct{} // Stops the current realtime worker generation
|
||||||
|
realtimeWorkerRun bool // Whether a realtime worker has been started
|
||||||
ctx context.Context // Cancelled when the app terminates
|
ctx context.Context // Cancelled when the app terminates
|
||||||
cancel context.CancelFunc // Cancels ctx and all child system contexts
|
cancel context.CancelFunc // Cancels ctx and all child system contexts
|
||||||
}
|
}
|
||||||
@@ -71,6 +76,7 @@ func NewSystemManager(hub hubLike) *SystemManager {
|
|||||||
hub: hub,
|
hub: hub,
|
||||||
smartFetchMap: expirymap.New[smartFetchState](time.Hour),
|
smartFetchMap: expirymap.New[smartFetchState](time.Hour),
|
||||||
zfsFetchMap: expirymap.New[zfsFetchState](time.Hour),
|
zfsFetchMap: expirymap.New[zfsFetchState](time.Hour),
|
||||||
|
activeSubscriptions: make(map[string]*subscriptionInfo),
|
||||||
}
|
}
|
||||||
sm.ctx, sm.cancel = context.WithCancel(context.Background())
|
sm.ctx, sm.cancel = context.WithCancel(context.Background())
|
||||||
return sm
|
return sm
|
||||||
@@ -138,6 +144,7 @@ func (sm *SystemManager) bindEventHooks() {
|
|||||||
// onTerminate cancels SystemManager context on app shutdown
|
// onTerminate cancels SystemManager context on app shutdown
|
||||||
func (sm *SystemManager) onTerminate(e *core.TerminateEvent) error {
|
func (sm *SystemManager) onTerminate(e *core.TerminateEvent) error {
|
||||||
sm.cancel()
|
sm.cancel()
|
||||||
|
sm.stopRealtimeWorker()
|
||||||
return e.Next()
|
return e.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,25 +3,27 @@ package systems
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/henrygd/beszel/internal/common"
|
"github.com/henrygd/beszel/internal/common"
|
||||||
|
"github.com/henrygd/beszel/internal/hub/utils"
|
||||||
|
"github.com/pocketbase/dbx"
|
||||||
|
"github.com/pocketbase/pocketbase/apis"
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
"github.com/pocketbase/pocketbase/tools/subscriptions"
|
"github.com/pocketbase/pocketbase/tools/subscriptions"
|
||||||
)
|
)
|
||||||
|
|
||||||
type subscriptionInfo struct {
|
type subscriptionInfo struct {
|
||||||
subscription string
|
subscription string
|
||||||
connectedClients uint8
|
connectedClients int
|
||||||
|
fetching bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
type realtimeFetch struct {
|
||||||
activeSubscriptions = make(map[string]*subscriptionInfo)
|
systemID string
|
||||||
workerRunning bool
|
subscription string
|
||||||
tickerStopChan chan struct{}
|
info *subscriptionInfo
|
||||||
realtimeMutex sync.Mutex
|
}
|
||||||
)
|
|
||||||
|
|
||||||
// onRealtimeConnectRequest handles client connection events for realtime subscriptions.
|
// onRealtimeConnectRequest handles client connection events for realtime subscriptions.
|
||||||
// It cleans up existing subscriptions when a client connects.
|
// It cleans up existing subscriptions when a client connects.
|
||||||
@@ -38,6 +40,19 @@ func (sm *SystemManager) onRealtimeConnectRequest(e *core.RealtimeConnectRequest
|
|||||||
// onRealtimeSubscribeRequest handles client subscription events for realtime metrics.
|
// onRealtimeSubscribeRequest handles client subscription events for realtime metrics.
|
||||||
// It tracks new subscriptions and unsubscriptions to manage the realtime worker lifecycle.
|
// It tracks new subscriptions and unsubscriptions to manage the realtime worker lifecycle.
|
||||||
func (sm *SystemManager) onRealtimeSubscribeRequest(e *core.RealtimeSubscribeRequestEvent) error {
|
func (sm *SystemManager) onRealtimeSubscribeRequest(e *core.RealtimeSubscribeRequestEvent) error {
|
||||||
|
// Parse with PocketBase's own subscription parser before changing the real
|
||||||
|
// client. Reject the entire request if any metrics target is inaccessible.
|
||||||
|
requested := subscriptions.NewDefaultClient()
|
||||||
|
requested.Subscribe(e.Subscriptions...)
|
||||||
|
for topic, options := range requested.Subscriptions() {
|
||||||
|
if !strings.HasPrefix(topic, "rt_metrics") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
system, err := sm.GetSystem(options.Query["system"])
|
||||||
|
if err != nil || !system.HasUser(e.App, e.Auth) {
|
||||||
|
return e.NotFoundError("", nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
oldSubs := e.Client.Subscriptions()
|
oldSubs := e.Client.Subscriptions()
|
||||||
// after e.Next() is the result of the subscribe request
|
// after e.Next() is the result of the subscribe request
|
||||||
err := e.Next()
|
err := e.Next()
|
||||||
@@ -47,14 +62,7 @@ func (sm *SystemManager) onRealtimeSubscribeRequest(e *core.RealtimeSubscribeReq
|
|||||||
for k, options := range newSubs {
|
for k, options := range newSubs {
|
||||||
if _, ok := oldSubs[k]; !ok {
|
if _, ok := oldSubs[k]; !ok {
|
||||||
if strings.HasPrefix(k, "rt_metrics") {
|
if strings.HasPrefix(k, "rt_metrics") {
|
||||||
systemId := options.Query["system"]
|
sm.addRealtimeSubscription(options.Query["system"], k)
|
||||||
if _, ok := activeSubscriptions[systemId]; !ok {
|
|
||||||
activeSubscriptions[systemId] = &subscriptionInfo{
|
|
||||||
subscription: k,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
activeSubscriptions[systemId].connectedClients += 1
|
|
||||||
sm.onRealtimeSubscriptionAdded()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,72 +76,76 @@ func (sm *SystemManager) onRealtimeSubscribeRequest(e *core.RealtimeSubscribeReq
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// onRealtimeSubscriptionAdded initializes or starts the realtime worker when the first subscription is added.
|
// addRealtimeSubscription tracks a subscriber and starts a worker if necessary.
|
||||||
// It ensures only one worker runs at a time.
|
func (sm *SystemManager) addRealtimeSubscription(systemID, subscription string) {
|
||||||
func (sm *SystemManager) onRealtimeSubscriptionAdded() {
|
sm.realtimeMutex.Lock()
|
||||||
realtimeMutex.Lock()
|
defer sm.realtimeMutex.Unlock()
|
||||||
defer realtimeMutex.Unlock()
|
|
||||||
|
|
||||||
// Start the worker if it's not already running
|
if sm.activeSubscriptions == nil {
|
||||||
if !workerRunning {
|
sm.activeSubscriptions = make(map[string]*subscriptionInfo)
|
||||||
workerRunning = true
|
}
|
||||||
// Create a new stop channel for this worker instance
|
info, ok := sm.activeSubscriptions[systemID]
|
||||||
tickerStopChan = make(chan struct{})
|
if !ok {
|
||||||
go sm.startRealtimeWorker()
|
info = &subscriptionInfo{subscription: subscription}
|
||||||
|
sm.activeSubscriptions[systemID] = info
|
||||||
|
}
|
||||||
|
info.connectedClients++
|
||||||
|
|
||||||
|
if !sm.realtimeWorkerRun {
|
||||||
|
sm.realtimeWorkerRun = true
|
||||||
|
stop := make(chan struct{})
|
||||||
|
sm.realtimeWorkerStop = stop
|
||||||
|
go sm.startRealtimeWorker(stop)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkSubscriptions stops the realtime worker when there are no active subscriptions.
|
// stopRealtimeWorker stops the current worker generation, if any.
|
||||||
// This prevents unnecessary resource usage when no clients are listening for realtime data.
|
func (sm *SystemManager) stopRealtimeWorker() {
|
||||||
func (sm *SystemManager) checkSubscriptions() {
|
sm.realtimeMutex.Lock()
|
||||||
if !workerRunning || len(activeSubscriptions) > 0 {
|
defer sm.realtimeMutex.Unlock()
|
||||||
|
sm.stopRealtimeWorkerLocked()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SystemManager) stopRealtimeWorkerLocked() {
|
||||||
|
if !sm.realtimeWorkerRun {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
close(sm.realtimeWorkerStop)
|
||||||
realtimeMutex.Lock()
|
sm.realtimeWorkerStop = nil
|
||||||
defer realtimeMutex.Unlock()
|
sm.realtimeWorkerRun = false
|
||||||
|
|
||||||
// Signal the worker to stop
|
|
||||||
if tickerStopChan != nil {
|
|
||||||
select {
|
|
||||||
case tickerStopChan <- struct{}{}:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark worker as stopped (will be reset when next subscription comes in)
|
|
||||||
workerRunning = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// removeRealtimeSubscription removes a realtime subscription and checks if the worker should be stopped.
|
// removeRealtimeSubscription removes a realtime subscription and checks if the worker should be stopped.
|
||||||
// It only processes subscriptions with the "rt_metrics" prefix and triggers cleanup when subscriptions are removed.
|
// It only processes subscriptions with the "rt_metrics" prefix and triggers cleanup when subscriptions are removed.
|
||||||
func (sm *SystemManager) removeRealtimeSubscription(subscription string, options subscriptions.SubscriptionOptions) {
|
func (sm *SystemManager) removeRealtimeSubscription(subscription string, options subscriptions.SubscriptionOptions) {
|
||||||
if strings.HasPrefix(subscription, "rt_metrics") {
|
if strings.HasPrefix(subscription, "rt_metrics") {
|
||||||
systemId := options.Query["system"]
|
systemID := options.Query["system"]
|
||||||
if info, ok := activeSubscriptions[systemId]; ok {
|
sm.realtimeMutex.Lock()
|
||||||
info.connectedClients -= 1
|
if info, ok := sm.activeSubscriptions[systemID]; ok {
|
||||||
|
info.connectedClients--
|
||||||
if info.connectedClients <= 0 {
|
if info.connectedClients <= 0 {
|
||||||
delete(activeSubscriptions, systemId)
|
delete(sm.activeSubscriptions, systemID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sm.checkSubscriptions()
|
if len(sm.activeSubscriptions) == 0 {
|
||||||
|
sm.stopRealtimeWorkerLocked()
|
||||||
|
}
|
||||||
|
sm.realtimeMutex.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// startRealtimeWorker runs the main loop for fetching realtime data from agents.
|
// startRealtimeWorker runs the main loop for fetching realtime data from agents.
|
||||||
// It continuously fetches system data and broadcasts it to subscribed clients via WebSocket.
|
// It continuously fetches system data and broadcasts it to subscribed clients via WebSocket.
|
||||||
func (sm *SystemManager) startRealtimeWorker() {
|
func (sm *SystemManager) startRealtimeWorker(stop <-chan struct{}) {
|
||||||
sm.fetchRealtimeDataAndNotify()
|
sm.fetchRealtimeDataAndNotify()
|
||||||
tick := time.Tick(1 * time.Second)
|
ticker := time.NewTicker(time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-tickerStopChan:
|
case <-stop:
|
||||||
return
|
return
|
||||||
case <-tick:
|
case <-ticker.C:
|
||||||
if len(activeSubscriptions) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sm.fetchRealtimeDataAndNotify()
|
sm.fetchRealtimeDataAndNotify()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,27 +153,79 @@ func (sm *SystemManager) startRealtimeWorker() {
|
|||||||
|
|
||||||
// fetchRealtimeDataAndNotify fetches realtime data for all active subscriptions and notifies the clients.
|
// fetchRealtimeDataAndNotify fetches realtime data for all active subscriptions and notifies the clients.
|
||||||
func (sm *SystemManager) fetchRealtimeDataAndNotify() {
|
func (sm *SystemManager) fetchRealtimeDataAndNotify() {
|
||||||
for systemId, info := range activeSubscriptions {
|
for _, fetch := range sm.claimRealtimeFetches() {
|
||||||
system, err := sm.GetSystem(systemId)
|
system, err := sm.GetSystem(fetch.systemID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
sm.finishRealtimeFetch(fetch)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go func() {
|
go func(fetch realtimeFetch) {
|
||||||
|
defer sm.finishRealtimeFetch(fetch)
|
||||||
data, err := system.fetchDataFromAgent(common.DataRequestOptions{CacheTimeMs: 1000})
|
data, err := system.fetchDataFromAgent(common.DataRequestOptions{CacheTimeMs: 1000})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
bytes, err := json.Marshal(data)
|
bytes, err := json.Marshal(data)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
notify(sm.hub, info.subscription, bytes)
|
notify(sm.hub, system, fetch.subscription, bytes)
|
||||||
}
|
}
|
||||||
}()
|
}(fetch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// claimRealtimeFetches takes a stable snapshot and marks each selected system as
|
||||||
|
// in flight. Slow agents are skipped on later ticks until their fetch completes.
|
||||||
|
func (sm *SystemManager) claimRealtimeFetches() []realtimeFetch {
|
||||||
|
sm.realtimeMutex.Lock()
|
||||||
|
defer sm.realtimeMutex.Unlock()
|
||||||
|
|
||||||
|
fetches := make([]realtimeFetch, 0, len(sm.activeSubscriptions))
|
||||||
|
for systemID, info := range sm.activeSubscriptions {
|
||||||
|
if info.fetching {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
info.fetching = true
|
||||||
|
fetches = append(fetches, realtimeFetch{
|
||||||
|
systemID: systemID,
|
||||||
|
subscription: info.subscription,
|
||||||
|
info: info,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return fetches
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SystemManager) finishRealtimeFetch(fetch realtimeFetch) {
|
||||||
|
sm.realtimeMutex.Lock()
|
||||||
|
defer sm.realtimeMutex.Unlock()
|
||||||
|
// A subscription may have been removed and recreated while the old request
|
||||||
|
// was running. Only release the exact entry claimed by this request.
|
||||||
|
if info := sm.activeSubscriptions[fetch.systemID]; info == fetch.info {
|
||||||
|
info.fetching = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// notify broadcasts realtime data to all clients subscribed to a specific subscription.
|
// notify broadcasts realtime data to all clients subscribed to a specific subscription.
|
||||||
// It iterates through all connected clients and sends the data only to those with matching subscriptions.
|
// Custom topics bypass collection rules, so check current access for every
|
||||||
func notify(app core.App, subscription string, data []byte) error {
|
// recipient, including clients whose authentication or membership was revoked.
|
||||||
|
func notify(app core.App, system *System, subscription string, data []byte) error {
|
||||||
|
shareAll, _ := utils.GetEnv("SHARE_ALL_SYSTEMS")
|
||||||
|
members := make(map[string]struct{})
|
||||||
|
if shareAll != "true" {
|
||||||
|
// Refresh once per broadcast so membership changes take effect on the
|
||||||
|
// next update without querying the database for every recipient.
|
||||||
|
var recordData struct{ Users string }
|
||||||
|
if err := app.DB().NewQuery("SELECT users FROM systems WHERE id={:id}").
|
||||||
|
Bind(dbx.Params{"id": system.Id}).One(&recordData); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var userIDs []string
|
||||||
|
if err := json.Unmarshal([]byte(recordData.Users), &userIDs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, id := range userIDs {
|
||||||
|
members[id] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
message := subscriptions.Message{
|
message := subscriptions.Message{
|
||||||
Name: subscription,
|
Name: subscription,
|
||||||
Data: data,
|
Data: data,
|
||||||
@@ -170,6 +234,13 @@ func notify(app core.App, subscription string, data []byte) error {
|
|||||||
if !client.HasSubscription(subscription) {
|
if !client.HasSubscription(subscription) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
auth, _ := client.Get(apis.RealtimeClientAuthKey).(*core.Record)
|
||||||
|
if auth == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, member := members[auth.Id]; shareAll != "true" && !member {
|
||||||
|
continue
|
||||||
|
}
|
||||||
client.Send(message)
|
client.Send(message)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
229
internal/hub/systems/system_realtime_test.go
Normal file
229
internal/hub/systems/system_realtime_test.go
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
package systems
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pocketbase/pocketbase/apis"
|
||||||
|
"github.com/pocketbase/pocketbase/core"
|
||||||
|
pbtests "github.com/pocketbase/pocketbase/tests"
|
||||||
|
"github.com/pocketbase/pocketbase/tools/hook"
|
||||||
|
"github.com/pocketbase/pocketbase/tools/store"
|
||||||
|
"github.com/pocketbase/pocketbase/tools/subscriptions"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRealtimeAuthorization(t *testing.T) {
|
||||||
|
t.Setenv("SHARE_ALL_SYSTEMS", "false")
|
||||||
|
t.Setenv("BESZEL_HUB_SHARE_ALL_SYSTEMS", "")
|
||||||
|
app, err := pbtests.NewTestApp(t.TempDir())
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(app.Cleanup)
|
||||||
|
_, err = app.DB().NewQuery(`CREATE TABLE IF NOT EXISTS systems (id TEXT PRIMARY KEY, users TEXT)`).Execute()
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = app.DB().NewQuery(`INSERT INTO systems (id, users) VALUES ('target', '["member"]')`).Execute()
|
||||||
|
require.NoError(t, err)
|
||||||
|
member := core.NewRecord(core.NewAuthCollection("users"))
|
||||||
|
member.Id = "member"
|
||||||
|
outsider := core.NewRecord(member.Collection())
|
||||||
|
outsider.Id = "outsider"
|
||||||
|
system := &System{Id: "target"}
|
||||||
|
sm := newRealtimeTestManager()
|
||||||
|
sm.systems.Set(system.Id, system)
|
||||||
|
// Keep the lifecycle bookkeeping active without starting an agent worker.
|
||||||
|
sm.realtimeWorkerRun = true
|
||||||
|
sm.realtimeWorkerStop = make(chan struct{})
|
||||||
|
t.Cleanup(sm.stopRealtimeWorker)
|
||||||
|
topic := `rt_metrics?options={"query":{"system":"target"}}`
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
auth *core.Record
|
||||||
|
topic string
|
||||||
|
share bool
|
||||||
|
allowed bool
|
||||||
|
}{
|
||||||
|
{"guest", nil, topic, false, false},
|
||||||
|
{"outsider", outsider, topic, false, false},
|
||||||
|
{"member", member, topic, false, true},
|
||||||
|
{"missing system", member, `rt_metrics`, false, false},
|
||||||
|
{"unknown system", member, `rt_metrics?options={"query":{"system":"missing"}}`, false, false},
|
||||||
|
{"malformed options", member, `rt_metrics?options=invalid`, false, false},
|
||||||
|
{"prefix variant", outsider, `rt_metrics_extra?options={"query":{"system":"target"}}`, false, false},
|
||||||
|
{"shared outsider", outsider, topic, true, true},
|
||||||
|
{"shared guest", nil, topic, true, false},
|
||||||
|
{"other topic", nil, "systems/*", false, true},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
if tc.share {
|
||||||
|
t.Setenv("BESZEL_HUB_SHARE_ALL_SYSTEMS", "true")
|
||||||
|
}
|
||||||
|
client := subscriptions.NewDefaultClient()
|
||||||
|
client.Subscribe("existing")
|
||||||
|
e := &core.RealtimeSubscribeRequestEvent{
|
||||||
|
RequestEvent: &core.RequestEvent{App: app, Auth: tc.auth},
|
||||||
|
Client: client, Subscriptions: []string{tc.topic},
|
||||||
|
}
|
||||||
|
called := false
|
||||||
|
h := &hook.Hook[*core.RealtimeSubscribeRequestEvent]{}
|
||||||
|
h.BindFunc(sm.onRealtimeSubscribeRequest)
|
||||||
|
err := h.Trigger(e, func(e *core.RealtimeSubscribeRequestEvent) error {
|
||||||
|
called = true
|
||||||
|
client.Unsubscribe()
|
||||||
|
client.Subscribe(e.Subscriptions...)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if tc.allowed {
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.True(t, called)
|
||||||
|
} else {
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.False(t, called)
|
||||||
|
assert.True(t, client.HasSubscription("existing"))
|
||||||
|
assert.False(t, client.HasSubscription(tc.topic))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("broadcast checks current access", func(t *testing.T) {
|
||||||
|
client := subscriptions.NewDefaultClient()
|
||||||
|
client.Subscribe(topic)
|
||||||
|
app.SubscriptionsBroker().Register(client)
|
||||||
|
defer app.SubscriptionsBroker().Unregister(client.Id())
|
||||||
|
secondClient := subscriptions.NewDefaultClient()
|
||||||
|
secondClient.Subscribe(topic)
|
||||||
|
app.SubscriptionsBroker().Register(secondClient)
|
||||||
|
defer app.SubscriptionsBroker().Unregister(secondClient.Id())
|
||||||
|
check := func(auth *core.Record, allowed bool) {
|
||||||
|
t.Helper()
|
||||||
|
client.Set(apis.RealtimeClientAuthKey, auth)
|
||||||
|
secondClient.Set(apis.RealtimeClientAuthKey, auth)
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
notify(app, system, topic, []byte(`{"cpu":42}`))
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
// Even on failure, drain pending sends and join the broadcaster before
|
||||||
|
// unregistering clients, which closes their channels.
|
||||||
|
defer func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-client.Channel():
|
||||||
|
case <-secondClient.Channel():
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
var received [2]int
|
||||||
|
timer := time.NewTimer(time.Second)
|
||||||
|
defer timer.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case msg := <-client.Channel():
|
||||||
|
received[0]++
|
||||||
|
assert.Equal(t, topic, msg.Name)
|
||||||
|
case msg := <-secondClient.Channel():
|
||||||
|
received[1]++
|
||||||
|
assert.Equal(t, topic, msg.Name)
|
||||||
|
case <-done:
|
||||||
|
want := [2]int{}
|
||||||
|
if allowed {
|
||||||
|
want = [2]int{1, 1}
|
||||||
|
}
|
||||||
|
assert.Equal(t, want, received)
|
||||||
|
return
|
||||||
|
case <-timer.C:
|
||||||
|
t.Fatal("broadcast did not finish")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check(nil, false)
|
||||||
|
check(outsider, false)
|
||||||
|
check(member, true)
|
||||||
|
_, err := app.DB().NewQuery(`UPDATE systems SET users = '[]'`).Execute()
|
||||||
|
require.NoError(t, err)
|
||||||
|
check(member, false)
|
||||||
|
t.Setenv("BESZEL_HUB_SHARE_ALL_SYSTEMS", "true")
|
||||||
|
check(outsider, true)
|
||||||
|
check(nil, false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRealtimeTestManager() *SystemManager {
|
||||||
|
return &SystemManager{
|
||||||
|
systems: store.New(map[string]*System{}),
|
||||||
|
activeSubscriptions: make(map[string]*subscriptionInfo),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRealtimeFetchesDoNotOverlapPerSystem(t *testing.T) {
|
||||||
|
sm := newRealtimeTestManager()
|
||||||
|
sm.activeSubscriptions["one"] = &subscriptionInfo{subscription: "rt_metrics_one"}
|
||||||
|
sm.activeSubscriptions["two"] = &subscriptionInfo{subscription: "rt_metrics_two"}
|
||||||
|
|
||||||
|
first := sm.claimRealtimeFetches()
|
||||||
|
require.Len(t, first, 2)
|
||||||
|
assert.Empty(t, sm.claimRealtimeFetches())
|
||||||
|
|
||||||
|
sm.finishRealtimeFetch(first[0])
|
||||||
|
next := sm.claimRealtimeFetches()
|
||||||
|
require.Len(t, next, 1)
|
||||||
|
assert.Equal(t, first[0].systemID, next[0].systemID)
|
||||||
|
|
||||||
|
sm.finishRealtimeFetch(first[1])
|
||||||
|
sm.finishRealtimeFetch(next[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFinishingOldRealtimeFetchDoesNotReleaseReplacement(t *testing.T) {
|
||||||
|
sm := newRealtimeTestManager()
|
||||||
|
oldInfo := &subscriptionInfo{subscription: "old"}
|
||||||
|
sm.activeSubscriptions["system"] = oldInfo
|
||||||
|
|
||||||
|
fetch := sm.claimRealtimeFetches()[0]
|
||||||
|
newInfo := &subscriptionInfo{subscription: "new", fetching: true}
|
||||||
|
sm.activeSubscriptions["system"] = newInfo
|
||||||
|
|
||||||
|
sm.finishRealtimeFetch(fetch)
|
||||||
|
assert.True(t, newInfo.fetching)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRealtimeSubscriptionLifecycle(t *testing.T) {
|
||||||
|
sm := newRealtimeTestManager()
|
||||||
|
options := subscriptions.SubscriptionOptions{Query: map[string]string{"system": "system"}}
|
||||||
|
|
||||||
|
sm.addRealtimeSubscription("system", "rt_metrics")
|
||||||
|
sm.addRealtimeSubscription("system", "rt_metrics")
|
||||||
|
|
||||||
|
sm.realtimeMutex.Lock()
|
||||||
|
firstStop := sm.realtimeWorkerStop
|
||||||
|
assert.True(t, sm.realtimeWorkerRun)
|
||||||
|
assert.Equal(t, 2, sm.activeSubscriptions["system"].connectedClients)
|
||||||
|
sm.realtimeMutex.Unlock()
|
||||||
|
|
||||||
|
sm.removeRealtimeSubscription("rt_metrics", options)
|
||||||
|
sm.realtimeMutex.Lock()
|
||||||
|
assert.True(t, sm.realtimeWorkerRun)
|
||||||
|
assert.Equal(t, 1, sm.activeSubscriptions["system"].connectedClients)
|
||||||
|
sm.realtimeMutex.Unlock()
|
||||||
|
|
||||||
|
sm.removeRealtimeSubscription("rt_metrics", options)
|
||||||
|
sm.realtimeMutex.Lock()
|
||||||
|
assert.False(t, sm.realtimeWorkerRun)
|
||||||
|
assert.Empty(t, sm.activeSubscriptions)
|
||||||
|
sm.realtimeMutex.Unlock()
|
||||||
|
select {
|
||||||
|
case <-firstStop:
|
||||||
|
default:
|
||||||
|
t.Fatal("worker stop channel was not closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// A later subscription must get a new stop channel owned by its worker.
|
||||||
|
sm.addRealtimeSubscription("system", "rt_metrics")
|
||||||
|
sm.realtimeMutex.Lock()
|
||||||
|
secondStop := sm.realtimeWorkerStop
|
||||||
|
assert.NotEqual(t, firstStop, secondStop)
|
||||||
|
sm.realtimeMutex.Unlock()
|
||||||
|
sm.stopRealtimeWorker()
|
||||||
|
}
|
||||||
@@ -193,7 +193,7 @@ export function SystemsTableColumns(viewMode: "table" | "grid"): ColumnDef<Syste
|
|||||||
header: sortableHeader,
|
header: sortableHeader,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
accessorFn: ({ info }) => info.g || undefined,
|
accessorFn: ({ info }) => info.g,
|
||||||
id: "gpu",
|
id: "gpu",
|
||||||
name: () => "GPU",
|
name: () => "GPU",
|
||||||
cell: (info) => {
|
cell: (info) => {
|
||||||
|
|||||||
@@ -812,6 +812,11 @@ elif is_freebsd; then
|
|||||||
echo "Adding beszel to wheel group for self-updates"
|
echo "Adding beszel to wheel group for self-updates"
|
||||||
pw group mod wheel -m beszel
|
pw group mod wheel -m beszel
|
||||||
fi
|
fi
|
||||||
|
# Add the user to the operator group for device access (SMART, /dev/xpt0, /dev/nvme*)
|
||||||
|
if pw group show operator >/dev/null 2>&1; then
|
||||||
|
echo "Adding beszel to operator group for device access"
|
||||||
|
pw group mod operator -m beszel
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user