mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-21 08:57:48 +02:00
feat: add network monitors (ICMP/TCP/HTTP/DNS) (#2266)
Co-authored-by: xiaomiku01 <xiaomiku01@outlook.com> Co-authored-by: henrygd <hank@henrygd.me>
This commit is contained in:
58
internal/hub/systems/network_monitors.go
Normal file
58
internal/hub/systems/network_monitors.go
Normal file
@@ -0,0 +1,58 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user