refactor(zabbix): 使 zabbix agent 插件集成独立化

This commit is contained in:
2024-08-23 13:28:48 +08:00
parent c6803285fe
commit a5f6c3b1d9
12 changed files with 78 additions and 68 deletions

View File

@ -0,0 +1,5 @@
package zabbixagent
const (
PluginName = "OnvifAgent"
)

View File

@ -7,6 +7,7 @@ import (
"io"
"net/http"
"onvif-agent/config"
"onvif-agent/constant"
"strings"
)
@ -22,8 +23,8 @@ 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) GetAppVersion(_ context.Context, _ map[string]string, _ ...string) (any, error) {
return constant.AppVersion, nil
}
func (h *Handler) HTTPClient(ctx context.Context, params map[string]string, _ ...string) (any, error) {
@ -34,13 +35,12 @@ func (h *Handler) HTTPClient(ctx context.Context, params map[string]string, _ ..
url := params["url"]
if !strings.HasPrefix(url, "http") {
url = fmt.Sprintf("http://localhost:%d/%s", config.Conf.Server.Port, url)
url = fmt.Sprintf("http://localhost:%d/%s", config.Config.App.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))
req, err := http.NewRequestWithContext(ctx, method, "https://api.imbytecat.com/ip", strings.NewReader(body)) // TODO: if empty use http.NoBody
if err != nil {
return nil, errs.Wrapf(err, "failed to create request")
}

View File

@ -3,7 +3,6 @@ package zabbixagent
import (
"errors"
"fmt"
"onvif-agent/config"
"os"
"golang.zabbix.com/sdk/plugin/flag"
@ -37,7 +36,7 @@ const (
func Run() {
err := flag.HandleFlags(
config.Conf.Integrations.ZabbixAgent.Plugin.Name,
PluginName,
os.Args[0],
fmt.Sprintf(copyrightMessage, pluginLicenseYear),
pluginVersionRC,

View File

@ -7,12 +7,9 @@ import (
"golang.zabbix.com/sdk/plugin"
"golang.zabbix.com/sdk/plugin/container"
"golang.zabbix.com/sdk/zbxerr"
"onvif-agent/config"
"time"
)
var Name = config.Conf.Integrations.ZabbixAgent.Plugin.Name
type metricKey string
type metricBinding struct {
@ -34,7 +31,7 @@ func Launch() error {
return err
}
h, err := container.NewHandler(Name)
h, err := container.NewHandler(PluginName)
if err != nil {
return errs.Wrap(err, "failed to create new handler")
}
@ -81,11 +78,11 @@ func (p *zabbixAgentPlugin) registerMetrics() error {
p.metrics = map[metricKey]*metricBinding{
"onvif.version": {
metric: metric.New(
"ONVIF plugin version",
"ONVIF app version",
nil,
false,
),
handler: h.GetPluginVersion,
handler: h.GetAppVersion,
},
"onvif.client": {
metric: metric.New(
@ -107,7 +104,7 @@ func (p *zabbixAgentPlugin) registerMetrics() error {
metricSet[string(k)] = m.metric
}
err := plugin.RegisterMetrics(p, Name, metricSet.List()...)
err := plugin.RegisterMetrics(p, PluginName, metricSet.List()...)
if err != nil {
return errs.Wrap(err, "failed to register metrics")
}