mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-22 01:17:48 +02:00
Co-authored-by: Sven van Ginkel <svenvanginkel@icloud.com> Co-authored-by: xiaomiku01 <xiaomiku01@outlook.com>
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package systems
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/henrygd/beszel"
|
|
"github.com/henrygd/beszel/internal/common"
|
|
"github.com/henrygd/beszel/internal/entities/monitor"
|
|
)
|
|
|
|
// SyncNetworkMonitors sends monitor configurations to the agent.
|
|
func (sys *System) SyncNetworkMonitors(configs []monitor.Config) error {
|
|
_, err := sys.syncNetworkMonitors(monitor.SyncRequest{Action: monitor.SyncActionReplace, Configs: configs})
|
|
return err
|
|
}
|
|
|
|
// UpsertNetworkMonitor sends a single monitor configuration change to the agent.
|
|
func (sys *System) UpsertNetworkMonitor(config monitor.Config, runNow bool) (*monitor.Result, error) {
|
|
resp, err := sys.syncNetworkMonitors(monitor.SyncRequest{
|
|
Action: monitor.SyncActionUpsert,
|
|
Config: config,
|
|
RunNow: runNow,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resp.Result == (monitor.Result{}) {
|
|
return nil, nil
|
|
}
|
|
result := resp.Result
|
|
return &result, nil
|
|
}
|
|
|
|
// DeleteNetworkMonitor removes a single monitor task from the agent.
|
|
func (sys *System) DeleteNetworkMonitor(id string) error {
|
|
_, err := sys.syncNetworkMonitors(monitor.SyncRequest{
|
|
Action: monitor.SyncActionDelete,
|
|
Config: monitor.Config{ID: id},
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (sys *System) syncNetworkMonitors(req monitor.SyncRequest) (monitor.SyncResponse, error) {
|
|
if sys.agentVersion.LT(beszel.MinVersionNetworkMonitors) {
|
|
return monitor.SyncResponse{}, nil
|
|
}
|
|
timeout := 5 * time.Second
|
|
if req.Action == monitor.SyncActionUpsert && req.RunNow {
|
|
// Allow the probe to finish, including a timeout result, while preserving
|
|
// the normal request budget for transport and response handling.
|
|
timeout += monitor.MaxProbeTimeout
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
defer cancel()
|
|
var result monitor.SyncResponse
|
|
return result, sys.request(ctx, common.SyncNetworkMonitors, req, &result)
|
|
}
|