refactor: 调试提交
This commit is contained in:
68
integration/zabbixagent/handler.go
Normal file
68
integration/zabbixagent/handler.go
Normal file
@ -0,0 +1,68 @@
|
||||
package zabbixagent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"golang.zabbix.com/sdk/errs"
|
||||
"io"
|
||||
"net/http"
|
||||
"onvif-agent/config"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// HandlerFunc describes the signature all metric handler functions must have.
|
||||
type HandlerFunc func(
|
||||
ctx context.Context,
|
||||
metricParams map[string]string,
|
||||
extraParams ...string,
|
||||
) (any, error)
|
||||
|
||||
// Handler hold client and syscall implementation for request functions.
|
||||
type Handler struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func (h *Handler) GetPluginVersion(_ context.Context, _ map[string]string, _ ...string) (any, error) {
|
||||
return config.Conf.Integrations.ZabbixAgent.Plugin.Version, nil
|
||||
}
|
||||
|
||||
func (h *Handler) HTTPClient(ctx context.Context, params map[string]string, _ ...string) (any, error) {
|
||||
method := params["method"]
|
||||
if method == "" {
|
||||
method = http.MethodGet
|
||||
}
|
||||
|
||||
url := params["url"]
|
||||
if !strings.HasPrefix(url, "http") {
|
||||
url = fmt.Sprintf("http://localhost:%d/%s", config.Conf.Server.Port, url)
|
||||
}
|
||||
|
||||
body := params["body"]
|
||||
|
||||
//req, err := http.NewRequestWithContext(ctx, method, "https://api.imbytecat.com/ip", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(ctx, method, "https://api.imbytecat.com/ip", strings.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, errs.Wrapf(err, "failed to create request")
|
||||
}
|
||||
|
||||
resp, err := h.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, errs.Wrapf(err, "failed to send the request")
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, errs.Wrapf(err, "failed to read the response")
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// NewHandler creates a new handler with initialized clients for system and tcp calls.
|
||||
func NewHandler() *Handler {
|
||||
return &Handler{
|
||||
client: http.DefaultClient,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user