GoDoxy/internal/watcher/health/monitor/agent_proxied.go
Yuzerion 57292f0fe8
feat: proxmox idlewatcher (#88)
* feat: idle sleep for proxmox LXCs

* refactor: replace deprecated docker api types

* chore(api): remove debug task list endpoint

* refactor: move servemux to gphttp/servemux; favicon.go to v1/favicon

* refactor: introduce Pool interface, move agent_pool to agent module

* refactor: simplify api code

* feat: introduce debug api

* refactor: remove net.URL and net.CIDR types, improved unmarshal handling

* chore: update Makefile for debug build tag, update README

* chore: add gperr.Unwrap method

* feat: relative time and duration formatting

* chore: add ROOT_DIR environment variable, refactor

* migration: move homepage override and icon cache to $BASE_DIR/data, add migration code

* fix: nil dereference on marshalling service health

* fix: wait for route deletion

* chore: enhance tasks debuggability

* feat: stdout access logger and MultiWriter

* fix(agent): remove agent properly on verify error

* fix(metrics): disk exclusion logic and added corresponding tests

* chore: update schema and prettify, fix package.json and Makefile

* fix: I/O buffer not being shrunk before putting back to pool

* feat: enhanced error handling module

* chore: deps upgrade

* feat: better value formatting and handling

---------

Co-authored-by: yusing <yusing@6uo.me>
2025-04-16 14:52:33 +08:00

74 lines
1.7 KiB
Go

package monitor
import (
"encoding/json"
"errors"
"net/http"
"net/url"
agentPkg "github.com/yusing/go-proxy/agent/pkg/agent"
"github.com/yusing/go-proxy/internal/watcher/health"
)
type (
AgentProxiedMonitor struct {
agent *agentPkg.AgentConfig
endpointURL string
*monitor
}
AgentCheckHealthTarget struct {
Scheme string
Host string
Path string
}
)
func AgentTargetFromURL(url *url.URL) *AgentCheckHealthTarget {
return &AgentCheckHealthTarget{
Scheme: url.Scheme,
Host: url.Host,
Path: url.Path,
}
}
func (target *AgentCheckHealthTarget) buildQuery() string {
query := make(url.Values, 3)
query.Set("scheme", target.Scheme)
query.Set("host", target.Host)
query.Set("path", target.Path)
return query.Encode()
}
func (target *AgentCheckHealthTarget) displayURL() *url.URL {
return &url.URL{
Scheme: target.Scheme,
Host: target.Host,
Path: target.Path,
}
}
func NewAgentProxiedMonitor(agent *agentPkg.AgentConfig, config *health.HealthCheckConfig, target *AgentCheckHealthTarget) *AgentProxiedMonitor {
mon := &AgentProxiedMonitor{
agent: agent,
endpointURL: agentPkg.EndpointHealth + "?" + target.buildQuery(),
}
mon.monitor = newMonitor(target.displayURL(), config, mon.CheckHealth)
return mon
}
func (mon *AgentProxiedMonitor) CheckHealth() (result *health.HealthCheckResult, err error) {
result = new(health.HealthCheckResult)
ctx, cancel := mon.ContextWithTimeout("timeout querying agent")
defer cancel()
data, status, err := mon.agent.Fetch(ctx, mon.endpointURL)
if err != nil {
return result, err
}
switch status {
case http.StatusOK:
err = json.Unmarshal(data, result)
default:
err = errors.New(string(data))
}
return
}