mirror of
https://github.com/henrygd/beszel.git
synced 2026-04-25 14:01:49 +02:00
updates
This commit is contained in:
@@ -1,5 +1,16 @@
|
||||
package probe
|
||||
|
||||
type SyncAction uint8
|
||||
|
||||
const (
|
||||
// SyncActionReplace indicates a full sync where the provided configs should replace all existing probes for the system.
|
||||
SyncActionReplace SyncAction = iota
|
||||
// SyncActionUpsert indicates an incremental sync where the provided config should be added or updated.
|
||||
SyncActionUpsert
|
||||
// SyncActionDelete indicates an incremental sync where the provided config should be removed.
|
||||
SyncActionDelete
|
||||
)
|
||||
|
||||
// Config defines a network probe task sent from hub to agent.
|
||||
type Config struct {
|
||||
// ID is the stable network_probes record ID generated by the hub.
|
||||
@@ -10,6 +21,19 @@ type Config struct {
|
||||
Interval uint16 `cbor:"4,keyasint"` // seconds
|
||||
}
|
||||
|
||||
// SyncRequest defines an incremental or full probe sync request sent to the agent.
|
||||
type SyncRequest struct {
|
||||
Action SyncAction `cbor:"0,keyasint"`
|
||||
Config Config `cbor:"1,keyasint,omitempty"`
|
||||
Configs []Config `cbor:"2,keyasint,omitempty"`
|
||||
RunNow bool `cbor:"3,keyasint,omitempty"`
|
||||
}
|
||||
|
||||
// SyncResponse returns the immediate result for an upsert when requested.
|
||||
type SyncResponse struct {
|
||||
Result Result `cbor:"0,keyasint,omitempty"`
|
||||
}
|
||||
|
||||
// Result holds aggregated probe results for a single target.
|
||||
//
|
||||
// 0: avg response in ms
|
||||
@@ -22,3 +46,11 @@ type Config struct {
|
||||
//
|
||||
// 4: packet loss percentage over the last hour (0-100)
|
||||
type Result []float64
|
||||
|
||||
// Get returns the value at the specified index or 0 if the index is out of range.
|
||||
func (r Result) Get(index int) float64 {
|
||||
if index < len(r) {
|
||||
return r[index]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -15,48 +15,106 @@ func generateProbeID(systemId string, config probe.Config) string {
|
||||
return systems.MakeStableHashId(systemId, config.Protocol, config.Target, portStr, intervalStr)
|
||||
}
|
||||
|
||||
func bindNetworkProbesEvents(h *Hub) {
|
||||
// bindNetworkProbesEvents keeps probe records and agent probe state in sync.
|
||||
func bindNetworkProbesEvents(hub *Hub) {
|
||||
// on create, make sure the id is set to a stable hash
|
||||
h.OnRecordCreate("network_probes").BindFunc(func(e *core.RecordEvent) error {
|
||||
hub.OnRecordCreate("network_probes").BindFunc(func(e *core.RecordEvent) error {
|
||||
systemID := e.Record.GetString("system")
|
||||
config := &probe.Config{
|
||||
Target: e.Record.GetString("target"),
|
||||
Protocol: e.Record.GetString("protocol"),
|
||||
Port: uint16(e.Record.GetInt("port")),
|
||||
Interval: uint16(e.Record.GetInt("interval")),
|
||||
}
|
||||
config := probeConfigFromRecord(e.Record)
|
||||
id := generateProbeID(systemID, *config)
|
||||
e.Record.Set("id", id)
|
||||
return e.Next()
|
||||
})
|
||||
|
||||
// sync probe to agent on creation
|
||||
h.OnRecordAfterCreateSuccess("network_probes").BindFunc(func(e *core.RecordEvent) error {
|
||||
systemID := e.Record.GetString("system")
|
||||
h.syncProbesToAgent(systemID)
|
||||
return e.Next()
|
||||
// sync probe to agent on creation and persist the first result immediately when available
|
||||
hub.OnRecordCreateRequest("network_probes").BindFunc(func(e *core.RecordRequestEvent) error {
|
||||
err := e.Next()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !e.Record.GetBool("enabled") {
|
||||
return nil
|
||||
}
|
||||
result, err := hub.upsertNetworkProbe(e.Record, true)
|
||||
if err != nil {
|
||||
hub.Logger().Warn("failed to sync probe to agent", "system", e.Record.GetString("system"), "probe", e.Record.Id, "err", err)
|
||||
return nil
|
||||
}
|
||||
if result == nil {
|
||||
return nil
|
||||
}
|
||||
setProbeResultFields(e.Record, *result)
|
||||
if err := e.App.SaveNoValidate(e.Record); err != nil {
|
||||
hub.Logger().Warn("failed to save initial probe result", "system", e.Record.GetString("system"), "probe", e.Record.Id, "err", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
hub.OnRecordUpdateRequest("network_probes").BindFunc(func(e *core.RecordRequestEvent) error {
|
||||
err := e.Next()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.Record.GetBool("enabled") {
|
||||
_, err = hub.upsertNetworkProbe(e.Record, false)
|
||||
} else {
|
||||
err = hub.deleteNetworkProbe(e.Record)
|
||||
}
|
||||
if err != nil {
|
||||
hub.Logger().Warn("failed to sync updated probe to agent", "system", e.Record.GetString("system"), "probe", e.Record.Id, "err", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// sync probe to agent on delete
|
||||
h.OnRecordAfterDeleteSuccess("network_probes").BindFunc(func(e *core.RecordEvent) error {
|
||||
systemID := e.Record.GetString("system")
|
||||
h.syncProbesToAgent(systemID)
|
||||
return e.Next()
|
||||
hub.OnRecordDeleteRequest("network_probes").BindFunc(func(e *core.RecordRequestEvent) error {
|
||||
err := e.Next()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := hub.deleteNetworkProbe(e.Record); err != nil {
|
||||
hub.Logger().Warn("failed to delete probe on agent", "system", e.Record.GetString("system"), "probe", e.Record.Id, "err", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
// TODO: if enabled changes, sync to agent
|
||||
}
|
||||
|
||||
// syncProbesToAgent fetches enabled probes for a system and sends them to the agent.
|
||||
func (h *Hub) syncProbesToAgent(systemID string) {
|
||||
// probeConfigFromRecord builds a probe config from a network_probes record.
|
||||
func probeConfigFromRecord(record *core.Record) *probe.Config {
|
||||
return &probe.Config{
|
||||
ID: record.Id,
|
||||
Target: record.GetString("target"),
|
||||
Protocol: record.GetString("protocol"),
|
||||
Port: uint16(record.GetInt("port")),
|
||||
Interval: uint16(record.GetInt("interval")),
|
||||
}
|
||||
}
|
||||
|
||||
// setProbeResultFields stores the latest probe result values on the record.
|
||||
func setProbeResultFields(record *core.Record, result probe.Result) {
|
||||
record.Set("res", result.Get(0))
|
||||
record.Set("resAvg1h", result.Get(1))
|
||||
record.Set("resMin1h", result.Get(2))
|
||||
record.Set("resMax1h", result.Get(3))
|
||||
record.Set("loss1h", result.Get(4))
|
||||
}
|
||||
|
||||
// upsertNetworkProbe applies the record's probe config to the target system.
|
||||
func (h *Hub) upsertNetworkProbe(record *core.Record, runNow bool) (*probe.Result, error) {
|
||||
systemID := record.GetString("system")
|
||||
system, err := h.sm.GetSystem(systemID)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
configs := h.sm.GetProbeConfigsForSystem(systemID)
|
||||
|
||||
go func() {
|
||||
if err := system.SyncNetworkProbes(configs); err != nil {
|
||||
h.Logger().Warn("failed to sync probes to agent", "system", systemID, "err", err)
|
||||
}
|
||||
}()
|
||||
return system.UpsertNetworkProbe(*probeConfigFromRecord(record), runNow)
|
||||
}
|
||||
|
||||
// deleteNetworkProbe removes the record's probe from the target system.
|
||||
func (h *Hub) deleteNetworkProbe(record *core.Record) error {
|
||||
systemID := record.GetString("system")
|
||||
system, err := h.sm.GetSystem(systemID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return system.DeleteNetworkProbe(record.Id)
|
||||
}
|
||||
|
||||
@@ -371,21 +371,21 @@ func updateNetworkProbesRecords(app core.App, data map[string]probe.Result, syst
|
||||
var record *core.Record
|
||||
record, err = app.FindRecordById(collectionName, id)
|
||||
if err == nil {
|
||||
record.Set("res", probeMetric(values, 0))
|
||||
record.Set("resAvg1h", probeMetric(values, 1))
|
||||
record.Set("resMin1h", probeMetric(values, 2))
|
||||
record.Set("resMax1h", probeMetric(values, 3))
|
||||
record.Set("loss1h", probeMetric(values, 4))
|
||||
record.Set("res", values.Get(0))
|
||||
record.Set("resAvg1h", values.Get(1))
|
||||
record.Set("resMin1h", values.Get(2))
|
||||
record.Set("resMax1h", values.Get(3))
|
||||
record.Set("loss1h", values.Get(4))
|
||||
err = app.SaveNoValidate(record)
|
||||
}
|
||||
default:
|
||||
_, err = updateQuery.Bind(dbx.Params{
|
||||
"id": id,
|
||||
"res": probeMetric(values, 0),
|
||||
"resAvg1h": probeMetric(values, 1),
|
||||
"resMin1h": probeMetric(values, 2),
|
||||
"resMax1h": probeMetric(values, 3),
|
||||
"loss1h": probeMetric(values, 4),
|
||||
"res": values.Get(0),
|
||||
"resAvg1h": values.Get(1),
|
||||
"resMin1h": values.Get(2),
|
||||
"resMax1h": values.Get(3),
|
||||
"loss1h": values.Get(4),
|
||||
"updated": nowString,
|
||||
}).Execute()
|
||||
}
|
||||
@@ -397,13 +397,6 @@ func updateNetworkProbesRecords(app core.App, data map[string]probe.Result, syst
|
||||
return nil
|
||||
}
|
||||
|
||||
func probeMetric(values probe.Result, index int) float64 {
|
||||
if index < len(values) {
|
||||
return values[index]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// createContainerRecords creates container records
|
||||
func createContainerRecords(app core.App, data []*container.Stats, systemId string) error {
|
||||
if len(data) == 0 {
|
||||
|
||||
@@ -10,48 +10,39 @@ import (
|
||||
|
||||
// SyncNetworkProbes sends probe configurations to the agent.
|
||||
func (sys *System) SyncNetworkProbes(configs []probe.Config) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
var result string
|
||||
return sys.request(ctx, common.SyncNetworkProbes, configs, &result)
|
||||
_, err := sys.syncNetworkProbes(probe.SyncRequest{Action: probe.SyncActionReplace, Configs: configs})
|
||||
return err
|
||||
}
|
||||
|
||||
// FetchNetworkProbeResults fetches probe results from the agent.
|
||||
// func (sys *System) FetchNetworkProbeResults() (map[string]probe.Result, error) {
|
||||
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
// defer cancel()
|
||||
// var results map[string]probe.Result
|
||||
// err := sys.request(ctx, common.GetNetworkProbeResults, nil, &results)
|
||||
// return results, err
|
||||
// }
|
||||
// UpsertNetworkProbe sends a single probe configuration change to the agent.
|
||||
func (sys *System) UpsertNetworkProbe(config probe.Config, runNow bool) (*probe.Result, error) {
|
||||
resp, err := sys.syncNetworkProbes(probe.SyncRequest{
|
||||
Action: probe.SyncActionUpsert,
|
||||
Config: config,
|
||||
RunNow: runNow,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(resp.Result) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
result := resp.Result
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// hasEnabledProbes returns true if this system has any enabled network probes.
|
||||
// func (sys *System) hasEnabledProbes() bool {
|
||||
// count, err := sys.manager.hub.CountRecords("network_probes",
|
||||
// dbx.NewExp("system = {:system} AND enabled = true", dbx.Params{"system": sys.Id}))
|
||||
// return err == nil && count > 0
|
||||
// }
|
||||
// DeleteNetworkProbe removes a single probe task from the agent.
|
||||
func (sys *System) DeleteNetworkProbe(id string) error {
|
||||
_, err := sys.syncNetworkProbes(probe.SyncRequest{
|
||||
Action: probe.SyncActionDelete,
|
||||
Config: probe.Config{ID: id},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// fetchAndSaveProbeResults fetches probe results and saves them to the database.
|
||||
// func (sys *System) fetchAndSaveProbeResults() {
|
||||
// hub := sys.manager.hub
|
||||
|
||||
// results, err := sys.FetchNetworkProbeResults()
|
||||
// if err != nil || len(results) == 0 {
|
||||
// return
|
||||
// }
|
||||
|
||||
// collection, err := hub.FindCachedCollectionByNameOrId("network_probe_stats")
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
// record := core.NewRecord(collection)
|
||||
// record.Set("system", sys.Id)
|
||||
// record.Set("stats", results)
|
||||
// record.Set("type", "1m")
|
||||
|
||||
// if err := hub.SaveNoValidate(record); err != nil {
|
||||
// hub.Logger().Warn("failed to save probe stats", "system", sys.Id, "err", err)
|
||||
// }
|
||||
// }
|
||||
func (sys *System) syncNetworkProbes(req probe.SyncRequest) (probe.SyncResponse, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
var result probe.SyncResponse
|
||||
return result, sys.request(ctx, common.SyncNetworkProbes, req, &result)
|
||||
}
|
||||
|
||||
@@ -66,8 +66,10 @@ export function AddProbeDialog({ systemId }: { systemId?: string }) {
|
||||
interval: probeInterval,
|
||||
enabled: true,
|
||||
})
|
||||
if (name && name !== target) {
|
||||
if (name) {
|
||||
payload.name = name
|
||||
} else if (targetName !== target) {
|
||||
payload.name = targetName
|
||||
}
|
||||
await pb.collection("network_probes").create(payload)
|
||||
resetForm()
|
||||
|
||||
Reference in New Issue
Block a user